2018-05-30 20:58:04 +08:00
|
|
|
#
|
|
|
|
# File : mkdir.py
|
|
|
|
# This file is part of RT-Thread RTOS
|
|
|
|
# COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
#
|
|
|
|
# Change Logs:
|
|
|
|
# Date Author Notes
|
|
|
|
# 2017-10-04 Bernard The first version
|
|
|
|
|
2017-11-04 17:39:45 +08:00
|
|
|
import os
|
2023-03-20 11:27:28 +08:00
|
|
|
import subprocess
|
2017-11-04 17:39:45 +08:00
|
|
|
import shutil
|
|
|
|
from shutil import ignore_patterns
|
2023-03-20 11:27:28 +08:00
|
|
|
from SCons.Script import *
|
2017-11-04 17:39:45 +08:00
|
|
|
|
|
|
|
def do_copy_file(src, dst):
|
|
|
|
# check source file
|
|
|
|
if not os.path.exists(src):
|
|
|
|
return
|
|
|
|
|
|
|
|
path = os.path.dirname(dst)
|
|
|
|
# mkdir if path not exist
|
|
|
|
if not os.path.exists(path):
|
|
|
|
os.makedirs(path)
|
|
|
|
|
|
|
|
shutil.copy2(src, dst)
|
|
|
|
|
|
|
|
def do_copy_folder(src_dir, dst_dir, ignore=None):
|
|
|
|
import shutil
|
|
|
|
# check source directory
|
|
|
|
if not os.path.exists(src_dir):
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
if os.path.exists(dst_dir):
|
|
|
|
shutil.rmtree(dst_dir)
|
|
|
|
except:
|
|
|
|
print('Deletes folder: %s failed.' % dst_dir)
|
|
|
|
return
|
|
|
|
|
|
|
|
shutil.copytree(src_dir, dst_dir, ignore = ignore)
|
|
|
|
|
2018-08-03 11:51:52 +08:00
|
|
|
source_ext = ['c', 'h', 's', 'S', 'cpp', 'xpm']
|
2017-11-04 17:39:45 +08:00
|
|
|
source_list = []
|
|
|
|
|
|
|
|
def walk_children(child):
|
|
|
|
global source_list
|
|
|
|
global source_ext
|
|
|
|
|
|
|
|
# print child
|
|
|
|
full_path = child.rfile().abspath
|
|
|
|
file_type = full_path.rsplit('.',1)[1]
|
|
|
|
#print file_type
|
|
|
|
if file_type in source_ext:
|
|
|
|
if full_path not in source_list:
|
|
|
|
source_list.append(full_path)
|
|
|
|
|
|
|
|
children = child.all_children()
|
|
|
|
if children != []:
|
|
|
|
for item in children:
|
|
|
|
walk_children(item)
|
|
|
|
|
|
|
|
def walk_kconfig(RTT_ROOT, source_list):
|
|
|
|
for parent, dirnames, filenames in os.walk(RTT_ROOT):
|
|
|
|
if 'bsp' in parent:
|
|
|
|
continue
|
|
|
|
if '.git' in parent:
|
|
|
|
continue
|
|
|
|
if 'tools' in parent:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if 'Kconfig' in filenames:
|
|
|
|
pathfile = os.path.join(parent, 'Kconfig')
|
|
|
|
source_list.append(pathfile)
|
|
|
|
if 'KConfig' in filenames:
|
|
|
|
pathfile = os.path.join(parent, 'KConfig')
|
|
|
|
source_list.append(pathfile)
|
|
|
|
|
2018-08-03 11:09:01 +08:00
|
|
|
def bsp_copy_files(bsp_root, dist_dir):
|
|
|
|
# copy BSP files
|
|
|
|
do_copy_folder(os.path.join(bsp_root), dist_dir,
|
|
|
|
ignore_patterns('build', 'dist', '*.pyc', '*.old', '*.map', 'rtthread.bin', '.sconsign.dblite', '*.elf', '*.axf', 'cconfig.h'))
|
|
|
|
|
|
|
|
def bsp_update_sconstruct(dist_dir):
|
2018-08-03 11:51:52 +08:00
|
|
|
with open(os.path.join(dist_dir, 'SConstruct'), 'r') as f:
|
2018-07-24 10:19:19 +08:00
|
|
|
data = f.readlines()
|
2018-08-03 11:51:52 +08:00
|
|
|
with open(os.path.join(dist_dir, 'SConstruct'), 'w') as f:
|
2018-07-24 10:19:19 +08:00
|
|
|
for line in data:
|
|
|
|
if line.find('RTT_ROOT') != -1:
|
|
|
|
if line.find('sys.path') != -1:
|
|
|
|
f.write('# set RTT_ROOT\n')
|
2018-08-03 11:51:52 +08:00
|
|
|
f.write('if not os.getenv("RTT_ROOT"): \n RTT_ROOT="rt-thread"\n\n')
|
2018-07-24 10:19:19 +08:00
|
|
|
f.write(line)
|
2017-11-04 17:39:45 +08:00
|
|
|
|
2021-05-14 17:07:58 +08:00
|
|
|
def bsp_update_kconfig_testcases(dist_dir):
|
|
|
|
# delete testcases in rt-thread/Kconfig
|
|
|
|
if not os.path.isfile(os.path.join(dist_dir, 'rt-thread/Kconfig')):
|
|
|
|
return
|
|
|
|
|
|
|
|
with open(os.path.join(dist_dir, 'rt-thread/Kconfig'), 'r') as f:
|
|
|
|
data = f.readlines()
|
|
|
|
with open(os.path.join(dist_dir, 'rt-thread/Kconfig'), 'w') as f:
|
|
|
|
for line in data:
|
|
|
|
if line.find('examples/utest/testcases/Kconfig') == -1:
|
|
|
|
f.write(line)
|
|
|
|
|
2018-07-24 10:19:19 +08:00
|
|
|
def bsp_update_kconfig(dist_dir):
|
|
|
|
# change RTT_ROOT in Kconfig
|
2018-08-03 11:51:52 +08:00
|
|
|
if not os.path.isfile(os.path.join(dist_dir, 'Kconfig')):
|
|
|
|
return
|
|
|
|
|
|
|
|
with open(os.path.join(dist_dir, 'Kconfig'), 'r') as f:
|
2018-07-24 10:19:19 +08:00
|
|
|
data = f.readlines()
|
2018-08-03 11:51:52 +08:00
|
|
|
with open(os.path.join(dist_dir, 'Kconfig'), 'w') as f:
|
2018-07-24 10:19:19 +08:00
|
|
|
for line in data:
|
2024-06-11 13:20:34 +08:00
|
|
|
if line.find('RTT_DIR') != -1 and line.find(':=') != -1:
|
|
|
|
line = 'RTT_DIR := rt-thread\n'
|
2018-07-24 10:19:19 +08:00
|
|
|
f.write(line)
|
2020-04-17 22:14:37 +08:00
|
|
|
|
2018-12-13 10:56:09 +08:00
|
|
|
def bsp_update_kconfig_library(dist_dir):
|
|
|
|
# change RTT_ROOT in Kconfig
|
|
|
|
if not os.path.isfile(os.path.join(dist_dir, 'Kconfig')):
|
|
|
|
return
|
|
|
|
|
|
|
|
with open(os.path.join(dist_dir, 'Kconfig'), 'r') as f:
|
|
|
|
data = f.readlines()
|
|
|
|
with open(os.path.join(dist_dir, 'Kconfig'), 'w') as f:
|
|
|
|
for line in data:
|
2024-06-11 13:20:34 +08:00
|
|
|
if line.find('source') != -1 and line.find('../libraries') != -1:
|
|
|
|
line = line.replace('../libraries', 'libraries')
|
2018-12-13 10:56:09 +08:00
|
|
|
f.write(line)
|
2018-07-24 10:19:19 +08:00
|
|
|
|
2020-04-17 22:14:37 +08:00
|
|
|
# change board/kconfig path
|
2019-07-18 10:25:15 +08:00
|
|
|
if not os.path.isfile(os.path.join(dist_dir, 'board/Kconfig')):
|
|
|
|
return
|
|
|
|
|
|
|
|
with open(os.path.join(dist_dir, 'board/Kconfig'), 'r') as f:
|
|
|
|
data = f.readlines()
|
|
|
|
with open(os.path.join(dist_dir, 'board/Kconfig'), 'w') as f:
|
|
|
|
for line in data:
|
2024-06-11 13:20:34 +08:00
|
|
|
if line.find('source') != -1 and line.find('../libraries') != -1:
|
|
|
|
line = line.replace('../libraries', 'libraries')
|
2019-07-18 10:25:15 +08:00
|
|
|
f.write(line)
|
|
|
|
|
2018-08-03 16:18:27 +08:00
|
|
|
def zip_dist(dist_dir, dist_name):
|
2018-07-24 10:19:19 +08:00
|
|
|
import zipfile
|
2017-11-04 17:39:45 +08:00
|
|
|
|
2018-08-03 16:18:27 +08:00
|
|
|
zip_filename = os.path.join(dist_dir)
|
2018-08-03 11:51:52 +08:00
|
|
|
zip = zipfile.ZipFile(zip_filename + '.zip', 'w')
|
2018-07-24 10:19:19 +08:00
|
|
|
pre_len = len(os.path.dirname(dist_dir))
|
2017-11-04 17:39:45 +08:00
|
|
|
|
2018-07-24 10:19:19 +08:00
|
|
|
for parent, dirnames, filenames in os.walk(dist_dir):
|
|
|
|
for filename in filenames:
|
|
|
|
pathfile = os.path.join(parent, filename)
|
|
|
|
arcname = pathfile[pre_len:].strip(os.path.sep)
|
|
|
|
zip.write(pathfile, arcname)
|
2017-11-04 17:39:45 +08:00
|
|
|
|
2018-07-24 10:19:19 +08:00
|
|
|
zip.close()
|
2017-11-04 17:39:45 +08:00
|
|
|
|
2022-12-19 14:12:08 +08:00
|
|
|
def MkDist(program, BSP_ROOT, RTT_ROOT, Env, project_name, project_path):
|
2018-08-03 11:51:52 +08:00
|
|
|
print('make distribution....')
|
2017-11-04 17:39:45 +08:00
|
|
|
|
2022-12-19 14:12:08 +08:00
|
|
|
if project_path == None:
|
|
|
|
dist_dir = os.path.join(BSP_ROOT, 'dist', project_name)
|
2019-06-18 10:29:49 +08:00
|
|
|
else:
|
2022-12-19 14:12:08 +08:00
|
|
|
dist_dir = project_path
|
2017-11-04 17:39:45 +08:00
|
|
|
|
2023-03-20 11:27:28 +08:00
|
|
|
rtt_dir_path = os.path.join(dist_dir, 'rt-thread')
|
2018-07-24 10:19:19 +08:00
|
|
|
|
2017-11-04 17:39:45 +08:00
|
|
|
# copy BSP files
|
2018-08-03 11:51:52 +08:00
|
|
|
print('=> %s' % os.path.basename(BSP_ROOT))
|
|
|
|
bsp_copy_files(BSP_ROOT, dist_dir)
|
2017-11-04 17:39:45 +08:00
|
|
|
|
2018-12-17 18:46:48 +08:00
|
|
|
# do bsp special dist handle
|
|
|
|
if 'dist_handle' in Env:
|
|
|
|
print("=> start dist handle")
|
|
|
|
dist_handle = Env['dist_handle']
|
2020-05-25 14:30:22 +08:00
|
|
|
dist_handle(BSP_ROOT, dist_dir)
|
2018-12-17 18:46:48 +08:00
|
|
|
|
2018-07-24 10:19:19 +08:00
|
|
|
# copy tools directory
|
2018-08-03 11:51:52 +08:00
|
|
|
print('=> components')
|
2023-03-20 11:27:28 +08:00
|
|
|
do_copy_folder(os.path.join(RTT_ROOT, 'components'), os.path.join(rtt_dir_path, 'components'))
|
2017-11-04 17:39:45 +08:00
|
|
|
|
2018-07-24 10:19:19 +08:00
|
|
|
# skip documentation directory
|
|
|
|
# skip examples
|
2017-11-04 17:39:45 +08:00
|
|
|
|
2018-07-24 10:19:19 +08:00
|
|
|
# copy include directory
|
2018-08-03 11:51:52 +08:00
|
|
|
print('=> include')
|
2023-03-20 11:27:28 +08:00
|
|
|
do_copy_folder(os.path.join(RTT_ROOT, 'include'), os.path.join(rtt_dir_path, 'include'))
|
2017-11-04 17:39:45 +08:00
|
|
|
|
2018-07-24 10:19:19 +08:00
|
|
|
# copy all libcpu/ARCH directory
|
|
|
|
print('=> libcpu')
|
|
|
|
import rtconfig
|
2023-03-20 11:27:28 +08:00
|
|
|
do_copy_folder(os.path.join(RTT_ROOT, 'libcpu', rtconfig.ARCH), os.path.join(rtt_dir_path, 'libcpu', rtconfig.ARCH))
|
|
|
|
do_copy_file(os.path.join(RTT_ROOT, 'libcpu', 'Kconfig'), os.path.join(rtt_dir_path, 'libcpu', 'Kconfig'))
|
|
|
|
do_copy_file(os.path.join(RTT_ROOT, 'libcpu', 'SConscript'), os.path.join(rtt_dir_path, 'libcpu', 'SConscript'))
|
2017-11-04 17:39:45 +08:00
|
|
|
|
2018-07-24 10:19:19 +08:00
|
|
|
# copy src directory
|
2018-08-03 11:51:52 +08:00
|
|
|
print('=> src')
|
2023-03-20 11:27:28 +08:00
|
|
|
do_copy_folder(os.path.join(RTT_ROOT, 'src'), os.path.join(rtt_dir_path, 'src'))
|
2017-11-04 17:39:45 +08:00
|
|
|
|
|
|
|
# copy tools directory
|
2018-08-03 11:51:52 +08:00
|
|
|
print('=> tools')
|
2023-03-20 11:27:28 +08:00
|
|
|
do_copy_folder(os.path.join(RTT_ROOT, 'tools'), os.path.join(rtt_dir_path, 'tools'), ignore_patterns('*.pyc'))
|
2018-07-24 10:19:19 +08:00
|
|
|
|
2023-03-20 11:27:28 +08:00
|
|
|
do_copy_file(os.path.join(RTT_ROOT, 'Kconfig'), os.path.join(rtt_dir_path, 'Kconfig'))
|
|
|
|
do_copy_file(os.path.join(RTT_ROOT, 'AUTHORS'), os.path.join(rtt_dir_path, 'AUTHORS'))
|
|
|
|
do_copy_file(os.path.join(RTT_ROOT, 'COPYING'), os.path.join(rtt_dir_path, 'COPYING'))
|
|
|
|
do_copy_file(os.path.join(RTT_ROOT, 'README.md'), os.path.join(rtt_dir_path, 'README.md'))
|
|
|
|
do_copy_file(os.path.join(RTT_ROOT, 'README_zh.md'), os.path.join(rtt_dir_path, 'README_zh.md'))
|
2017-11-04 17:39:45 +08:00
|
|
|
|
2021-05-14 17:07:58 +08:00
|
|
|
print('Update configuration files...')
|
2017-11-04 17:39:45 +08:00
|
|
|
# change RTT_ROOT in SConstruct
|
2018-07-24 10:19:19 +08:00
|
|
|
bsp_update_sconstruct(dist_dir)
|
2017-11-30 20:12:53 +08:00
|
|
|
# change RTT_ROOT in Kconfig
|
2018-07-24 10:19:19 +08:00
|
|
|
bsp_update_kconfig(dist_dir)
|
2018-12-13 10:56:09 +08:00
|
|
|
bsp_update_kconfig_library(dist_dir)
|
2021-05-14 17:07:58 +08:00
|
|
|
# delete testcases in Kconfig
|
|
|
|
bsp_update_kconfig_testcases(dist_dir)
|
2017-11-04 17:39:45 +08:00
|
|
|
|
2023-03-20 11:27:28 +08:00
|
|
|
target_project_type = GetOption('target')
|
|
|
|
if target_project_type:
|
2023-07-24 06:07:30 +08:00
|
|
|
child = subprocess.Popen('scons --target={} --project-name="{}"'.format(target_project_type, project_name), cwd=dist_dir, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
|
2023-03-20 11:27:28 +08:00
|
|
|
stdout, stderr = child.communicate()
|
|
|
|
if child.returncode == 0:
|
|
|
|
print(stdout)
|
|
|
|
else:
|
|
|
|
print(stderr)
|
|
|
|
else:
|
2023-07-24 06:07:30 +08:00
|
|
|
print('suggest to use command scons --dist [--target=xxx] [--project-name="xxx"] [--project-path="xxx"]')
|
2018-08-03 11:09:01 +08:00
|
|
|
|
2023-09-15 06:45:30 +08:00
|
|
|
# make zip package
|
|
|
|
if project_path == None:
|
|
|
|
zip_dist(dist_dir, project_name)
|
|
|
|
|
2023-03-20 11:27:28 +08:00
|
|
|
print('dist project successfully!')
|