1# 2# File : mkdir.py 3# This file is part of RT-Thread RTOS 4# COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team 5# 6# This program is free software; you can redistribute it and/or modify 7# it under the terms of the GNU General Public License as published by 8# the Free Software Foundation; either version 2 of the License, or 9# (at your option) any later version. 10# 11# This program is distributed in the hope that it will be useful, 12# but WITHOUT ANY WARRANTY; without even the implied warranty of 13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14# GNU General Public License for more details. 15# 16# You should have received a copy of the GNU General Public License along 17# with this program; if not, write to the Free Software Foundation, Inc., 18# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 19# 20# Change Logs: 21# Date Author Notes 22# 2017-10-04 Bernard The first version 23 24import os 25import shutil 26 27from shutil import ignore_patterns 28 29def do_copy_file(src, dst): 30 # check source file 31 if not os.path.exists(src): 32 return 33 34 path = os.path.dirname(dst) 35 # mkdir if path not exist 36 if not os.path.exists(path): 37 os.makedirs(path) 38 39 shutil.copy2(src, dst) 40 41def do_copy_folder(src_dir, dst_dir, ignore=None): 42 import shutil 43 # check source directory 44 if not os.path.exists(src_dir): 45 return 46 47 try: 48 if os.path.exists(dst_dir): 49 shutil.rmtree(dst_dir) 50 except: 51 print('Deletes folder: %s failed.' % dst_dir) 52 return 53 54 shutil.copytree(src_dir, dst_dir, ignore = ignore) 55 56source_ext = ['c', 'h', 's', 'S', 'cpp', 'xpm'] 57source_list = [] 58 59def walk_children(child): 60 global source_list 61 global source_ext 62 63 # print child 64 full_path = child.rfile().abspath 65 file_type = full_path.rsplit('.',1)[1] 66 #print file_type 67 if file_type in source_ext: 68 if full_path not in source_list: 69 source_list.append(full_path) 70 71 children = child.all_children() 72 if children != []: 73 for item in children: 74 walk_children(item) 75 76def walk_kconfig(RTT_ROOT, source_list): 77 for parent, dirnames, filenames in os.walk(RTT_ROOT): 78 if 'bsp' in parent: 79 continue 80 if '.git' in parent: 81 continue 82 if 'tools' in parent: 83 continue 84 85 if 'Kconfig' in filenames: 86 pathfile = os.path.join(parent, 'Kconfig') 87 source_list.append(pathfile) 88 if 'KConfig' in filenames: 89 pathfile = os.path.join(parent, 'KConfig') 90 source_list.append(pathfile) 91 92def bsp_copy_files(bsp_root, dist_dir): 93 # copy BSP files 94 do_copy_folder(os.path.join(bsp_root), dist_dir, 95 ignore_patterns('build', 'dist', '*.pyc', '*.old', '*.map', 'rtthread.bin', '.sconsign.dblite', '*.elf', '*.axf', 'cconfig.h')) 96 97def bsp_update_sconstruct(dist_dir): 98 with open(os.path.join(dist_dir, 'SConstruct'), 'r') as f: 99 data = f.readlines() 100 with open(os.path.join(dist_dir, 'SConstruct'), 'w') as f: 101 for line in data: 102 if line.find('RTT_ROOT') != -1: 103 if line.find('sys.path') != -1: 104 f.write('# set RTT_ROOT\n') 105 f.write('if not os.getenv("RTT_ROOT"): \n RTT_ROOT="rt-thread"\n\n') 106 f.write(line) 107 108def bsp_update_kconfig(dist_dir): 109 # change RTT_ROOT in Kconfig 110 if not os.path.isfile(os.path.join(dist_dir, 'Kconfig')): 111 return 112 113 with open(os.path.join(dist_dir, 'Kconfig'), 'r') as f: 114 data = f.readlines() 115 with open(os.path.join(dist_dir, 'Kconfig'), 'w') as f: 116 found = 0 117 for line in data: 118 if line.find('RTT_ROOT') != -1: 119 found = 1 120 if line.find('default') != -1 and found: 121 position = line.find('default') 122 line = line[0:position] + 'default "rt-thread"\n' 123 found = 0 124 f.write(line) 125 126def bsp_update_kconfig_library(dist_dir): 127 # change RTT_ROOT in Kconfig 128 if not os.path.isfile(os.path.join(dist_dir, 'Kconfig')): 129 return 130 131 with open(os.path.join(dist_dir, 'Kconfig'), 'r') as f: 132 data = f.readlines() 133 with open(os.path.join(dist_dir, 'Kconfig'), 'w') as f: 134 found = 0 135 for line in data: 136 if line.find('RTT_ROOT') != -1: 137 found = 1 138 if line.find('../libraries') != -1 and found: 139 position = line.find('../libraries') 140 line = line[0:position] + 'libraries/Kconfig"\n' 141 found = 0 142 f.write(line) 143 144def bs_update_ide_project(bsp_root, rtt_root): 145 import subprocess 146 # default update the projects which have template file 147 tgt_dict = {'mdk4':('keil', 'armcc'), 148 'mdk5':('keil', 'armcc'), 149 'iar':('iar', 'iar'), 150 'vs':('msvc', 'cl'), 151 'vs2012':('msvc', 'cl'), 152 'cdk':('gcc', 'gcc')} 153 154 scons_env = os.environ.copy() 155 scons_env['RTT_ROOT'] = rtt_root 156 157 for item in tgt_dict: 158 child = subprocess.Popen('scons --target=' + item, cwd=bsp_root, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) 159 stdout, stderr = child.communicate() 160 if child.returncode == 0: 161 print('update %s project' % item) 162 163def zip_dist(dist_dir, dist_name): 164 import zipfile 165 166 zip_filename = os.path.join(dist_dir) 167 zip = zipfile.ZipFile(zip_filename + '.zip', 'w') 168 pre_len = len(os.path.dirname(dist_dir)) 169 170 for parent, dirnames, filenames in os.walk(dist_dir): 171 for filename in filenames: 172 pathfile = os.path.join(parent, filename) 173 arcname = pathfile[pre_len:].strip(os.path.sep) 174 zip.write(pathfile, arcname) 175 176 zip.close() 177 178def MkDist_Strip(program, BSP_ROOT, RTT_ROOT, Env): 179 global source_list 180 181 print('make distribution and strip useless files....') 182 183 dist_name = os.path.basename(BSP_ROOT) 184 dist_dir = os.path.join(BSP_ROOT, 'dist-strip', dist_name) 185 target_path = os.path.join(dist_dir, 'rt-thread') 186 187 print('=> %s' % os.path.basename(BSP_ROOT)) 188 bsp_copy_files(BSP_ROOT, dist_dir) 189 190 # copy stm32 bsp libiary files 191 if os.path.basename(os.path.dirname(BSP_ROOT)) == 'stm32': 192 print("=> copy stm32 bsp library") 193 library_path = os.path.join(os.path.dirname(BSP_ROOT), 'libraries') 194 library_dir = os.path.join(dist_dir, 'libraries') 195 bsp_copy_files(os.path.join(library_path, 'HAL_Drivers'), os.path.join(library_dir, 'HAL_Drivers')) 196 bsp_copy_files(os.path.join(library_path, Env['bsp_lib_type']), os.path.join(library_dir, Env['bsp_lib_type'])) 197 shutil.copyfile(os.path.join(library_path, 'Kconfig'), os.path.join(library_dir, 'Kconfig')) 198 199 # do bsp special dist handle 200 if 'dist_handle' in Env: 201 print("=> start dist handle") 202 dist_handle = Env['dist_handle'] 203 dist_handle(BSP_ROOT) 204 205 # get all source files from program 206 for item in program: 207 walk_children(item) 208 source_list.sort() 209 210 # copy the source files without libcpu and components/libc in RT-Thread 211 target_list = [] 212 libcpu_dir = os.path.join(RTT_ROOT, 'libcpu').lower() 213 libc_dir = os.path.join(RTT_ROOT, 'components', 'libc', 'compilers').lower() 214 sal_dir = os.path.join(RTT_ROOT, 'components', 'net', 'sal_socket').lower() 215 sources_include_sal = False 216 for src in source_list: 217 if src.lower().startswith(BSP_ROOT.lower()): 218 continue 219 220 # skip libc and libcpu dir 221 if src.lower().startswith(libcpu_dir): 222 continue 223 if src.lower().startswith(libc_dir): 224 continue 225 if src.lower().startswith(sal_dir): 226 sources_include_sal = True 227 continue 228 229 if src.lower().startswith(RTT_ROOT.lower()): 230 target_list.append(src) 231 source_list = target_list 232 233 # get source directory 234 src_dir = [] 235 for src in source_list: 236 src = src.replace(RTT_ROOT, '') 237 if src[0] == os.sep or src[0] == '/': 238 src = src[1:] 239 240 path = os.path.dirname(src) 241 sub_path = path.split(os.sep) 242 full_path = RTT_ROOT 243 for item in sub_path: 244 full_path = os.path.join(full_path, item) 245 if full_path not in src_dir: 246 src_dir.append(full_path) 247 248 # add all of SConscript files 249 for item in src_dir: 250 source_list.append(os.path.join(item, 'SConscript')) 251 252 # add all of Kconfig files 253 walk_kconfig(RTT_ROOT, source_list) 254 255 # copy all files to target directory 256 source_list.sort() 257 for src in source_list: 258 dst = src.replace(RTT_ROOT, '') 259 if dst[0] == os.sep or dst[0] == '/': 260 dst = dst[1:] 261 262 print('=> %s' % dst) 263 dst = os.path.join(target_path, dst) 264 do_copy_file(src, dst) 265 266 # copy tools directory 267 print('=> tools') 268 do_copy_folder(os.path.join(RTT_ROOT, 'tools'), os.path.join(target_path, 'tools'), ignore_patterns('*.pyc')) 269 do_copy_file(os.path.join(RTT_ROOT, 'Kconfig'), os.path.join(target_path, 'Kconfig')) 270 do_copy_file(os.path.join(RTT_ROOT, 'AUTHORS'), os.path.join(target_path, 'AUTHORS')) 271 do_copy_file(os.path.join(RTT_ROOT, 'COPYING'), os.path.join(target_path, 'COPYING')) 272 do_copy_file(os.path.join(RTT_ROOT, 'README.md'), os.path.join(target_path, 'README.md')) 273 do_copy_file(os.path.join(RTT_ROOT, 'README_zh.md'), os.path.join(target_path, 'README_zh.md')) 274 275 print('=> %s' % os.path.join('components', 'libc', 'compilers')) 276 do_copy_folder(os.path.join(RTT_ROOT, 'components', 'libc', 'compilers'), os.path.join(target_path, 'components', 'libc', 'compilers')) 277 278 if sources_include_sal: 279 print('=> %s' % os.path.join('components', 'net', 'sal_socket')) 280 do_copy_folder(os.path.join(RTT_ROOT, 'components', 'net', 'sal_socket'), os.path.join(target_path, 'components', 'net', 'sal_socket')) 281 282 # copy all libcpu/ARCH directory 283 import rtconfig 284 print('=> %s' % (os.path.join('libcpu', rtconfig.ARCH, rtconfig.CPU))) 285 do_copy_folder(os.path.join(RTT_ROOT, 'libcpu', rtconfig.ARCH, rtconfig.CPU), os.path.join(target_path, 'libcpu', rtconfig.ARCH, rtconfig.CPU)) 286 if os.path.exists(os.path.join(RTT_ROOT, 'libcpu', rtconfig.ARCH, 'common')): 287 print('=> %s' % (os.path.join('libcpu', rtconfig.ARCH, 'common'))) 288 do_copy_folder(os.path.join(RTT_ROOT, 'libcpu', rtconfig.ARCH, 'common'), os.path.join(target_path, 'libcpu', rtconfig.ARCH, 'common')) 289 do_copy_file(os.path.join(RTT_ROOT, 'libcpu', 'Kconfig'), os.path.join(target_path, 'libcpu', 'Kconfig')) 290 do_copy_file(os.path.join(RTT_ROOT, 'libcpu', 'SConscript'), os.path.join(target_path, 'libcpu', 'SConscript')) 291 292 # change RTT_ROOT in SConstruct 293 bsp_update_sconstruct(dist_dir) 294 # change RTT_ROOT in Kconfig 295 bsp_update_kconfig(dist_dir) 296 bsp_update_kconfig_library(dist_dir) 297 # update all project files 298 bs_update_ide_project(dist_dir, target_path) 299 300 # make zip package 301 zip_dist(dist_dir, dist_name) 302 303 print('done!') 304 305def MkDist(program, BSP_ROOT, RTT_ROOT, Env): 306 print('make distribution....') 307 308 dist_name = os.path.basename(BSP_ROOT) 309 dist_dir = os.path.join(BSP_ROOT, 'dist', dist_name) 310 311 target_path = os.path.join(dist_dir, 'rt-thread') 312 313 # copy BSP files 314 print('=> %s' % os.path.basename(BSP_ROOT)) 315 bsp_copy_files(BSP_ROOT, dist_dir) 316 317 # copy stm32 bsp libiary files 318 if os.path.basename(os.path.dirname(BSP_ROOT)) == 'stm32': 319 print("=> copy stm32 bsp library") 320 library_path = os.path.join(os.path.dirname(BSP_ROOT), 'libraries') 321 library_dir = os.path.join(dist_dir, 'libraries') 322 bsp_copy_files(os.path.join(library_path, 'HAL_Drivers'), os.path.join(library_dir, 'HAL_Drivers')) 323 bsp_copy_files(os.path.join(library_path, Env['bsp_lib_type']), os.path.join(library_dir, Env['bsp_lib_type'])) 324 shutil.copyfile(os.path.join(library_path, 'Kconfig'), os.path.join(library_dir, 'Kconfig')) 325 326 # do bsp special dist handle 327 if 'dist_handle' in Env: 328 print("=> start dist handle") 329 dist_handle = Env['dist_handle'] 330 dist_handle(BSP_ROOT) 331 332 # copy tools directory 333 print('=> components') 334 do_copy_folder(os.path.join(RTT_ROOT, 'components'), os.path.join(target_path, 'components')) 335 336 # skip documentation directory 337 # skip examples 338 339 # copy include directory 340 print('=> include') 341 do_copy_folder(os.path.join(RTT_ROOT, 'include'), os.path.join(target_path, 'include')) 342 343 # copy all libcpu/ARCH directory 344 print('=> libcpu') 345 import rtconfig 346 do_copy_folder(os.path.join(RTT_ROOT, 'libcpu', rtconfig.ARCH), os.path.join(target_path, 'libcpu', rtconfig.ARCH)) 347 do_copy_file(os.path.join(RTT_ROOT, 'libcpu', 'Kconfig'), os.path.join(target_path, 'libcpu', 'Kconfig')) 348 do_copy_file(os.path.join(RTT_ROOT, 'libcpu', 'SConscript'), os.path.join(target_path, 'libcpu', 'SConscript')) 349 350 # copy src directory 351 print('=> src') 352 do_copy_folder(os.path.join(RTT_ROOT, 'src'), os.path.join(target_path, 'src')) 353 354 # copy tools directory 355 print('=> tools') 356 do_copy_folder(os.path.join(RTT_ROOT, 'tools'), os.path.join(target_path, 'tools'), ignore_patterns('*.pyc')) 357 358 do_copy_file(os.path.join(RTT_ROOT, 'Kconfig'), os.path.join(target_path, 'Kconfig')) 359 do_copy_file(os.path.join(RTT_ROOT, 'AUTHORS'), os.path.join(target_path, 'AUTHORS')) 360 do_copy_file(os.path.join(RTT_ROOT, 'COPYING'), os.path.join(target_path, 'COPYING')) 361 do_copy_file(os.path.join(RTT_ROOT, 'README.md'), os.path.join(target_path, 'README.md')) 362 do_copy_file(os.path.join(RTT_ROOT, 'README_zh.md'), os.path.join(target_path, 'README_zh.md')) 363 364 # change RTT_ROOT in SConstruct 365 bsp_update_sconstruct(dist_dir) 366 # change RTT_ROOT in Kconfig 367 bsp_update_kconfig(dist_dir) 368 bsp_update_kconfig_library(dist_dir) 369 # update all project files 370 bs_update_ide_project(dist_dir, target_path) 371 372 # make zip package 373 zip_dist(dist_dir, dist_name) 374 375 print('done!') 376 377