building.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. #
  2. # File : building.py
  3. # This file is part of RT-Thread RTOS
  4. # COPYRIGHT (C) 2006 - 2015, 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. # 2015-01-20 Bernard Add copyright information
  23. # 2015-07-25 Bernard Add LOCAL_CCFLAGS/LOCAL_CPPPATH/LOCAL_CPPDEFINES for
  24. # group definition.
  25. #
  26. import os
  27. import sys
  28. import string
  29. from SCons.Script import *
  30. from utils import _make_path_relative
  31. BuildOptions = {}
  32. Projects = []
  33. Rtt_Root = ''
  34. Env = None
  35. class Win32Spawn:
  36. def spawn(self, sh, escape, cmd, args, env):
  37. # deal with the cmd build-in commands which cannot be used in
  38. # subprocess.Popen
  39. if cmd == 'del':
  40. for f in args[1:]:
  41. try:
  42. os.remove(f)
  43. except Exception as e:
  44. print 'Error removing file: %s' % e
  45. return -1
  46. return 0
  47. import subprocess
  48. newargs = string.join(args[1:], ' ')
  49. cmdline = cmd + " " + newargs
  50. # Make sure the env is constructed by strings
  51. _e = dict([(k, str(v)) for k, v in env.items()])
  52. # Windows(tm) CreateProcess does not use the env passed to it to find
  53. # the executables. So we have to modify our own PATH to make Popen
  54. # work.
  55. old_path = os.environ['PATH']
  56. os.environ['PATH'] = _e['PATH']
  57. try:
  58. proc = subprocess.Popen(cmdline, env=_e, shell=False)
  59. except Exception as e:
  60. print 'Error in calling:\n%s' % cmdline
  61. print 'Exception: %s: %s' % (e, os.strerror(e.errno))
  62. return e.errno
  63. finally:
  64. os.environ['PATH'] = old_path
  65. return proc.wait()
  66. def PrepareBuilding(env, root_directory, has_libcpu=False, remove_components = []):
  67. import SCons.cpp
  68. import rtconfig
  69. global BuildOptions
  70. global Projects
  71. global Env
  72. global Rtt_Root
  73. Env = env
  74. Rtt_Root = root_directory
  75. # add compability with Keil MDK 4.6 which changes the directory of armcc.exe
  76. if rtconfig.PLATFORM == 'armcc':
  77. if not os.path.isfile(os.path.join(rtconfig.EXEC_PATH, 'armcc.exe')):
  78. if rtconfig.EXEC_PATH.find('bin40') > 0:
  79. rtconfig.EXEC_PATH = rtconfig.EXEC_PATH.replace('bin40', 'armcc/bin')
  80. Env['LINKFLAGS']=Env['LINKFLAGS'].replace('RV31', 'armcc')
  81. # reset AR command flags
  82. env['ARCOM'] = '$AR --create $TARGET $SOURCES'
  83. env['LIBPREFIX'] = ''
  84. env['LIBSUFFIX'] = '.lib'
  85. env['LIBLINKPREFIX'] = ''
  86. env['LIBLINKSUFFIX'] = '.lib'
  87. env['LIBDIRPREFIX'] = '--userlibpath '
  88. # patch for win32 spawn
  89. if env['PLATFORM'] == 'win32':
  90. win32_spawn = Win32Spawn()
  91. win32_spawn.env = env
  92. env['SPAWN'] = win32_spawn.spawn
  93. if env['PLATFORM'] == 'win32':
  94. os.environ['PATH'] = rtconfig.EXEC_PATH + ";" + os.environ['PATH']
  95. else:
  96. os.environ['PATH'] = rtconfig.EXEC_PATH + ":" + os.environ['PATH']
  97. # add program path
  98. env.PrependENVPath('PATH', rtconfig.EXEC_PATH)
  99. # add rtconfig.h path
  100. env.Append(CPPPATH = [str(Dir('#').abspath)])
  101. # add library build action
  102. act = SCons.Action.Action(BuildLibInstallAction, 'Install compiled library... $TARGET')
  103. bld = Builder(action = act)
  104. Env.Append(BUILDERS = {'BuildLib': bld})
  105. # parse rtconfig.h to get used component
  106. PreProcessor = SCons.cpp.PreProcessor()
  107. f = file('rtconfig.h', 'r')
  108. contents = f.read()
  109. f.close()
  110. PreProcessor.process_contents(contents)
  111. BuildOptions = PreProcessor.cpp_namespace
  112. # add copy option
  113. AddOption('--copy',
  114. dest='copy',
  115. action='store_true',
  116. default=False,
  117. help='copy rt-thread directory to local.')
  118. AddOption('--copy-header',
  119. dest='copy-header',
  120. action='store_true',
  121. default=False,
  122. help='copy header of rt-thread directory to local.')
  123. AddOption('--cscope',
  124. dest='cscope',
  125. action='store_true',
  126. default=False,
  127. help='Build Cscope cross reference database. Requires cscope installed.')
  128. AddOption('--clang-analyzer',
  129. dest='clang-analyzer',
  130. action='store_true',
  131. default=False,
  132. help='Perform static analyze with Clang-analyzer. '+\
  133. 'Requires Clang installed.\n'+\
  134. 'It is recommended to use with scan-build like this:\n'+\
  135. '`scan-build scons --clang-analyzer`\n'+\
  136. 'If things goes well, scan-build will instruct you to invoke scan-view.')
  137. if GetOption('clang-analyzer'):
  138. # perform what scan-build does
  139. env.Replace(
  140. CC = 'ccc-analyzer',
  141. CXX = 'c++-analyzer',
  142. # skip as and link
  143. LINK = 'true',
  144. AS = 'true',)
  145. env["ENV"].update(x for x in os.environ.items() if x[0].startswith("CCC_"))
  146. # only check, don't compile. ccc-analyzer use CCC_CC as the CC.
  147. # fsyntax-only will give us some additional warning messages
  148. env['ENV']['CCC_CC'] = 'clang'
  149. env.Append(CFLAGS=['-fsyntax-only', '-Wall', '-Wno-invalid-source-encoding'])
  150. env['ENV']['CCC_CXX'] = 'clang++'
  151. env.Append(CXXFLAGS=['-fsyntax-only', '-Wall', '-Wno-invalid-source-encoding'])
  152. # remove the POST_ACTION as it will cause meaningless errors(file not
  153. # found or something like that).
  154. rtconfig.POST_ACTION = ''
  155. # add build library option
  156. AddOption('--buildlib',
  157. dest='buildlib',
  158. type='string',
  159. help='building library of a component')
  160. AddOption('--cleanlib',
  161. dest='cleanlib',
  162. action='store_true',
  163. default=False,
  164. help='clean up the library by --buildlib')
  165. # add target option
  166. AddOption('--target',
  167. dest='target',
  168. type='string',
  169. help='set target project: mdk/mdk4/iar/vs/ua')
  170. #{target_name:(CROSS_TOOL, PLATFORM)}
  171. tgt_dict = {'mdk':('keil', 'armcc'),
  172. 'mdk4':('keil', 'armcc'),
  173. 'mdk5':('keil', 'armcc'),
  174. 'iar':('iar', 'iar'),
  175. 'vs':('msvc', 'cl'),
  176. 'vs2012':('msvc', 'cl'),
  177. 'cb':('keil', 'armcc'),
  178. 'ua':('gcc', 'gcc')}
  179. tgt_name = GetOption('target')
  180. if tgt_name:
  181. # --target will change the toolchain settings which clang-analyzer is
  182. # depend on
  183. if GetOption('clang-analyzer'):
  184. print '--clang-analyzer cannot be used with --target'
  185. sys.exit(1)
  186. SetOption('no_exec', 1)
  187. try:
  188. rtconfig.CROSS_TOOL, rtconfig.PLATFORM = tgt_dict[tgt_name]
  189. except KeyError:
  190. print 'Unknow target: %s. Avaible targets: %s' % \
  191. (tgt_name, ', '.join(tgt_dict.keys()))
  192. sys.exit(1)
  193. elif (GetDepend('RT_USING_NEWLIB') == False and GetDepend('RT_USING_NOLIBC') == False) \
  194. and rtconfig.PLATFORM == 'gcc':
  195. AddDepend('RT_USING_MINILIBC')
  196. # add comstr option
  197. AddOption('--verbose',
  198. dest='verbose',
  199. action='store_true',
  200. default=False,
  201. help='print verbose information during build')
  202. if not GetOption('verbose'):
  203. # override the default verbose command string
  204. env.Replace(
  205. ARCOMSTR = 'AR $TARGET',
  206. ASCOMSTR = 'AS $TARGET',
  207. ASPPCOMSTR = 'AS $TARGET',
  208. CCCOMSTR = 'CC $TARGET',
  209. CXXCOMSTR = 'CXX $TARGET',
  210. LINKCOMSTR = 'LINK $TARGET'
  211. )
  212. # we need to seperate the variant_dir for BSPs and the kernels. BSPs could
  213. # have their own components etc. If they point to the same folder, SCons
  214. # would find the wrong source code to compile.
  215. bsp_vdir = 'build/bsp'
  216. kernel_vdir = 'build/kernel'
  217. # board build script
  218. objs = SConscript('SConscript', variant_dir=bsp_vdir, duplicate=0)
  219. # include kernel
  220. objs.extend(SConscript(Rtt_Root + '/src/SConscript', variant_dir=kernel_vdir + '/src', duplicate=0))
  221. # include libcpu
  222. if not has_libcpu:
  223. objs.extend(SConscript(Rtt_Root + '/libcpu/SConscript',
  224. variant_dir=kernel_vdir + '/libcpu', duplicate=0))
  225. # include components
  226. objs.extend(SConscript(Rtt_Root + '/components/SConscript',
  227. variant_dir=kernel_vdir + '/components',
  228. duplicate=0,
  229. exports='remove_components'))
  230. return objs
  231. def PrepareModuleBuilding(env, root_directory, bsp_directory):
  232. import rtconfig
  233. global BuildOptions
  234. global Env
  235. global Rtt_Root
  236. # patch for win32 spawn
  237. if env['PLATFORM'] == 'win32':
  238. win32_spawn = Win32Spawn()
  239. win32_spawn.env = env
  240. env['SPAWN'] = win32_spawn.spawn
  241. Env = env
  242. Rtt_Root = root_directory
  243. # parse bsp rtconfig.h to get used component
  244. PreProcessor = SCons.cpp.PreProcessor()
  245. f = file(bsp_directory + '/rtconfig.h', 'r')
  246. contents = f.read()
  247. f.close()
  248. PreProcessor.process_contents(contents)
  249. BuildOptions = PreProcessor.cpp_namespace
  250. # add build/clean library option for library checking
  251. AddOption('--buildlib',
  252. dest='buildlib',
  253. type='string',
  254. help='building library of a component')
  255. AddOption('--cleanlib',
  256. dest='cleanlib',
  257. action='store_true',
  258. default=False,
  259. help='clean up the library by --buildlib')
  260. # add program path
  261. env.PrependENVPath('PATH', rtconfig.EXEC_PATH)
  262. def GetConfigValue(name):
  263. assert type(name) == str, 'GetConfigValue: only string parameter is valid'
  264. try:
  265. return BuildOptions[name]
  266. except:
  267. return ''
  268. def GetDepend(depend):
  269. building = True
  270. if type(depend) == type('str'):
  271. if not BuildOptions.has_key(depend) or BuildOptions[depend] == 0:
  272. building = False
  273. elif BuildOptions[depend] != '':
  274. return BuildOptions[depend]
  275. return building
  276. # for list type depend
  277. for item in depend:
  278. if item != '':
  279. if not BuildOptions.has_key(item) or BuildOptions[item] == 0:
  280. building = False
  281. return building
  282. def AddDepend(option):
  283. BuildOptions[option] = 1
  284. def MergeGroup(src_group, group):
  285. src_group['src'] = src_group['src'] + group['src']
  286. if group.has_key('CCFLAGS'):
  287. if src_group.has_key('CCFLAGS'):
  288. src_group['CCFLAGS'] = src_group['CCFLAGS'] + group['CCFLAGS']
  289. else:
  290. src_group['CCFLAGS'] = group['CCFLAGS']
  291. if group.has_key('CPPPATH'):
  292. if src_group.has_key('CPPPATH'):
  293. src_group['CPPPATH'] = src_group['CPPPATH'] + group['CPPPATH']
  294. else:
  295. src_group['CPPPATH'] = group['CPPPATH']
  296. if group.has_key('CPPDEFINES'):
  297. if src_group.has_key('CPPDEFINES'):
  298. src_group['CPPDEFINES'] = src_group['CPPDEFINES'] + group['CPPDEFINES']
  299. else:
  300. src_group['CPPDEFINES'] = group['CPPDEFINES']
  301. # for local CCFLAGS/CPPPATH/CPPDEFINES
  302. if group.has_key('LOCAL_CCFLAGS'):
  303. if src_group.has_key('LOCAL_CCFLAGS'):
  304. src_group['LOCAL_CCFLAGS'] = src_group['LOCAL_CCFLAGS'] + group['LOCAL_CCFLAGS']
  305. else:
  306. src_group['LOCAL_CCFLAGS'] = group['LOCAL_CCFLAGS']
  307. if group.has_key('LOCAL_CPPPATH'):
  308. if src_group.has_key('LOCAL_CPPPATH'):
  309. src_group['LOCAL_CPPPATH'] = src_group['LOCAL_CPPPATH'] + group['LOCAL_CPPPATH']
  310. else:
  311. src_group['LOCAL_CPPPATH'] = group['LOCAL_CPPPATH']
  312. if group.has_key('LOCAL_CPPDEFINES'):
  313. if src_group.has_key('LOCAL_CPPDEFINES'):
  314. src_group['LOCAL_CPPDEFINES'] = src_group['LOCAL_CPPDEFINES'] + group['LOCAL_CPPDEFINES']
  315. else:
  316. src_group['LOCAL_CPPDEFINES'] = group['LOCAL_CPPDEFINES']
  317. if group.has_key('LINKFLAGS'):
  318. if src_group.has_key('LINKFLAGS'):
  319. src_group['LINKFLAGS'] = src_group['LINKFLAGS'] + group['LINKFLAGS']
  320. else:
  321. src_group['LINKFLAGS'] = group['LINKFLAGS']
  322. if group.has_key('LIBS'):
  323. if src_group.has_key('LIBS'):
  324. src_group['LIBS'] = src_group['LIBS'] + group['LIBS']
  325. else:
  326. src_group['LIBS'] = group['LIBS']
  327. if group.has_key('LIBPATH'):
  328. if src_group.has_key('LIBPATH'):
  329. src_group['LIBPATH'] = src_group['LIBPATH'] + group['LIBPATH']
  330. else:
  331. src_group['LIBPATH'] = group['LIBPATH']
  332. def DefineGroup(name, src, depend, **parameters):
  333. global Env
  334. if not GetDepend(depend):
  335. return []
  336. # find exist group and get path of group
  337. group_path = ''
  338. for g in Projects:
  339. if g['name'] == name:
  340. group_path = g['path']
  341. if group_path == '':
  342. group_path = GetCurrentDir()
  343. group = parameters
  344. group['name'] = name
  345. group['path'] = group_path
  346. if type(src) == type(['src1']):
  347. group['src'] = File(src)
  348. else:
  349. group['src'] = src
  350. if group.has_key('CCFLAGS'):
  351. Env.AppendUnique(CCFLAGS = group['CCFLAGS'])
  352. if group.has_key('CPPPATH'):
  353. Env.AppendUnique(CPPPATH = group['CPPPATH'])
  354. if group.has_key('CPPDEFINES'):
  355. Env.AppendUnique(CPPDEFINES = group['CPPDEFINES'])
  356. if group.has_key('LINKFLAGS'):
  357. Env.AppendUnique(LINKFLAGS = group['LINKFLAGS'])
  358. # check whether to clean up library
  359. if GetOption('cleanlib') and os.path.exists(os.path.join(group['path'], GroupLibFullName(name, Env))):
  360. if group['src'] != []:
  361. print 'Remove library:', GroupLibFullName(name, Env)
  362. do_rm_file(os.path.join(group['path'], GroupLibFullName(name, Env)))
  363. # check whether exist group library
  364. if not GetOption('buildlib') and os.path.exists(os.path.join(group['path'], GroupLibFullName(name, Env))):
  365. group['src'] = []
  366. if group.has_key('LIBS'): group['LIBS'] = group['LIBS'] + [GroupLibName(name, Env)]
  367. else : group['LIBS'] = [GroupLibName(name, Env)]
  368. if group.has_key('LIBPATH'): group['LIBPATH'] = group['LIBPATH'] + [GetCurrentDir()]
  369. else : group['LIBPATH'] = [GetCurrentDir()]
  370. if group.has_key('LIBS'):
  371. Env.AppendUnique(LIBS = group['LIBS'])
  372. if group.has_key('LIBPATH'):
  373. Env.AppendUnique(LIBPATH = group['LIBPATH'])
  374. # check whether to build group library
  375. if group.has_key('LIBRARY'):
  376. objs = Env.Library(name, group['src'])
  377. else:
  378. # only add source
  379. objs = group['src']
  380. # merge group
  381. for g in Projects:
  382. if g['name'] == name:
  383. # merge to this group
  384. MergeGroup(g, group)
  385. return objs
  386. # add a new group
  387. Projects.append(group)
  388. return objs
  389. def GetCurrentDir():
  390. conscript = File('SConscript')
  391. fn = conscript.rfile()
  392. name = fn.name
  393. path = os.path.dirname(fn.abspath)
  394. return path
  395. PREBUILDING = []
  396. def RegisterPreBuildingAction(act):
  397. global PREBUILDING
  398. assert callable(act), 'Could only register callable objects. %s received' % repr(act)
  399. PREBUILDING.append(act)
  400. def PreBuilding():
  401. global PREBUILDING
  402. for a in PREBUILDING:
  403. a()
  404. def GroupLibName(name, env):
  405. import rtconfig
  406. if rtconfig.PLATFORM == 'armcc':
  407. return name + '_rvds'
  408. elif rtconfig.PLATFORM == 'gcc':
  409. return name + '_gcc'
  410. return name
  411. def GroupLibFullName(name, env):
  412. return env['LIBPREFIX'] + GroupLibName(name, env) + env['LIBSUFFIX']
  413. def BuildLibInstallAction(target, source, env):
  414. lib_name = GetOption('buildlib')
  415. for Group in Projects:
  416. if Group['name'] == lib_name:
  417. lib_name = GroupLibFullName(Group['name'], env)
  418. dst_name = os.path.join(Group['path'], lib_name)
  419. print 'Copy %s => %s' % (lib_name, dst_name)
  420. do_copy_file(lib_name, dst_name)
  421. break
  422. def DoBuilding(target, objects):
  423. # merge all objects into one list
  424. def one_list(l):
  425. lst = []
  426. for item in l:
  427. if type(item) == type([]):
  428. lst += one_list(item)
  429. else:
  430. lst.append(item)
  431. return lst
  432. # handle local group
  433. def local_group(group, objects):
  434. if group.has_key('LOCAL_CCFLAGS') or group.has_key('LOCAL_CPPPATH') or group.has_key('LOCAL_CPPDEFINES'):
  435. CCFLAGS = Env.get('CCFLAGS', '') + group.get('LOCAL_CCFLAGS', '')
  436. CPPPATH = Env.get('CPPPATH', ['']) + group.get('LOCAL_CPPPATH', [''])
  437. CPPDEFINES = Env.get('CPPDEFINES', ['']) + group.get('LOCAL_CPPDEFINES', [''])
  438. for source in group['src']:
  439. objects.append(Env.Object(source, CCFLAGS = CCFLAGS,
  440. CPPPATH = CPPPATH, CPPDEFINES = CPPDEFINES))
  441. return True
  442. return False
  443. objects = one_list(objects)
  444. program = None
  445. # check whether special buildlib option
  446. lib_name = GetOption('buildlib')
  447. if lib_name:
  448. objects = [] # remove all of objects
  449. # build library with special component
  450. for Group in Projects:
  451. if Group['name'] == lib_name:
  452. lib_name = GroupLibName(Group['name'], Env)
  453. if not local_group(Group, objects):
  454. objects = Env.Object(Group['src'])
  455. program = Env.Library(lib_name, objects)
  456. # add library copy action
  457. Env.BuildLib(lib_name, program)
  458. break
  459. else:
  460. # remove source files with local flags setting
  461. for group in Projects:
  462. if group.has_key('LOCAL_CCFLAGS') or group.has_key('LOCAL_CPPPATH') or group.has_key('LOCAL_CPPDEFINES'):
  463. for source in group['src']:
  464. for obj in objects:
  465. if source.abspath == obj.abspath or (len(obj.sources) > 0 and source.abspath == obj.sources[0].abspath):
  466. objects.remove(obj)
  467. # re-add the source files to the objects
  468. for group in Projects:
  469. local_group(group, objects)
  470. program = Env.Program(target, objects)
  471. EndBuilding(target, program)
  472. def EndBuilding(target, program = None):
  473. import rtconfig
  474. Env.AddPostAction(target, rtconfig.POST_ACTION)
  475. if GetOption('target') == 'mdk':
  476. from keil import MDKProject
  477. from keil import MDK4Project
  478. from keil import MDK5Project
  479. template = os.path.isfile('template.Uv2')
  480. if template:
  481. MDKProject('project.Uv2', Projects)
  482. else:
  483. template = os.path.isfile('template.uvproj')
  484. if template:
  485. MDK4Project('project.uvproj', Projects)
  486. else:
  487. template = os.path.isfile('template.uvprojx')
  488. if template:
  489. MDK5Project('project.uvprojx', Projects)
  490. else:
  491. print 'No template project file found.'
  492. if GetOption('target') == 'mdk4':
  493. from keil import MDK4Project
  494. MDK4Project('project.uvproj', Projects)
  495. if GetOption('target') == 'mdk5':
  496. from keil import MDK5Project
  497. MDK5Project('project.uvprojx', Projects)
  498. if GetOption('target') == 'iar':
  499. from iar import IARProject
  500. IARProject('project.ewp', Projects)
  501. if GetOption('target') == 'vs':
  502. from vs import VSProject
  503. VSProject('project.vcproj', Projects, program)
  504. if GetOption('target') == 'vs2012':
  505. from vs2012 import VS2012Project
  506. VS2012Project('project.vcxproj', Projects, program)
  507. if GetOption('target') == 'cb':
  508. from codeblocks import CBProject
  509. CBProject('project.cbp', Projects, program)
  510. if GetOption('target') == 'ua':
  511. from ua import PrepareUA
  512. PrepareUA(Projects, Rtt_Root, str(Dir('#')))
  513. if GetOption('copy') and program != None:
  514. MakeCopy(program)
  515. if GetOption('copy-header') and program != None:
  516. MakeCopyHeader(program)
  517. if GetOption('cscope'):
  518. from cscope import CscopeDatabase
  519. CscopeDatabase(Projects)
  520. def SrcRemove(src, remove):
  521. if not src:
  522. return
  523. if type(src[0]) == type('str'):
  524. for item in src:
  525. if os.path.basename(item) in remove:
  526. src.remove(item)
  527. return
  528. for item in src:
  529. if os.path.basename(item.rstr()) in remove:
  530. src.remove(item)
  531. def GetVersion():
  532. import SCons.cpp
  533. import string
  534. rtdef = os.path.join(Rtt_Root, 'include', 'rtdef.h')
  535. # parse rtdef.h to get RT-Thread version
  536. prepcessor = SCons.cpp.PreProcessor()
  537. f = file(rtdef, 'r')
  538. contents = f.read()
  539. f.close()
  540. prepcessor.process_contents(contents)
  541. def_ns = prepcessor.cpp_namespace
  542. version = int(filter(lambda ch: ch in '0123456789.', def_ns['RT_VERSION']))
  543. subversion = int(filter(lambda ch: ch in '0123456789.', def_ns['RT_SUBVERSION']))
  544. if def_ns.has_key('RT_REVISION'):
  545. revision = int(filter(lambda ch: ch in '0123456789.', def_ns['RT_REVISION']))
  546. return '%d.%d.%d' % (version, subversion, revision)
  547. return '0.%d.%d' % (version, subversion)
  548. def GlobSubDir(sub_dir, ext_name):
  549. import os
  550. import glob
  551. def glob_source(sub_dir, ext_name):
  552. list = os.listdir(sub_dir)
  553. src = glob.glob(os.path.join(sub_dir, ext_name))
  554. for item in list:
  555. full_subdir = os.path.join(sub_dir, item)
  556. if os.path.isdir(full_subdir):
  557. src += glob_source(full_subdir, ext_name)
  558. return src
  559. dst = []
  560. src = glob_source(sub_dir, ext_name)
  561. for item in src:
  562. dst.append(os.path.relpath(item, sub_dir))
  563. return dst
  564. def PackageSConscript(package):
  565. from package import BuildPackage
  566. return BuildPackage(package)
  567. def file_path_exist(path, *args):
  568. return os.path.exists(os.path.join(path, *args))
  569. def do_rm_file(src):
  570. if os.path.exists(src):
  571. os.unlink(src)
  572. def do_copy_file(src, dst):
  573. import shutil
  574. # check source file
  575. if not os.path.exists(src):
  576. return
  577. path = os.path.dirname(dst)
  578. # mkdir if path not exist
  579. if not os.path.exists(path):
  580. os.makedirs(path)
  581. shutil.copy2(src, dst)
  582. def do_copy_folder(src_dir, dst_dir):
  583. import shutil
  584. # check source directory
  585. if not os.path.exists(src_dir):
  586. return
  587. if os.path.exists(dst_dir):
  588. shutil.rmtree(dst_dir)
  589. shutil.copytree(src_dir, dst_dir)
  590. source_ext = ["c", "h", "s", "S", "cpp", "xpm"]
  591. source_list = []
  592. def walk_children(child):
  593. global source_list
  594. global source_ext
  595. # print child
  596. full_path = child.rfile().abspath
  597. file_type = full_path.rsplit('.',1)[1]
  598. #print file_type
  599. if file_type in source_ext:
  600. if full_path not in source_list:
  601. source_list.append(full_path)
  602. children = child.all_children()
  603. if children != []:
  604. for item in children:
  605. walk_children(item)
  606. def MakeCopy(program):
  607. global source_list
  608. global Rtt_Root
  609. global Env
  610. target_path = os.path.join(Dir('#').abspath, 'rt-thread')
  611. if Env['PLATFORM'] == 'win32':
  612. RTT_ROOT = Rtt_Root.lower()
  613. else:
  614. RTT_ROOT = Rtt_Root
  615. if target_path.startswith(RTT_ROOT):
  616. return
  617. for item in program:
  618. walk_children(item)
  619. source_list.sort()
  620. # filte source file in RT-Thread
  621. target_list = []
  622. for src in source_list:
  623. if Env['PLATFORM'] == 'win32':
  624. src = src.lower()
  625. if src.startswith(RTT_ROOT):
  626. target_list.append(src)
  627. source_list = target_list
  628. # get source path
  629. src_dir = []
  630. for src in source_list:
  631. src = src.replace(RTT_ROOT, '')
  632. if src[0] == os.sep or src[0] == '/':
  633. src = src[1:]
  634. path = os.path.dirname(src)
  635. sub_path = path.split(os.sep)
  636. full_path = RTT_ROOT
  637. for item in sub_path:
  638. full_path = os.path.join(full_path, item)
  639. if full_path not in src_dir:
  640. src_dir.append(full_path)
  641. for item in src_dir:
  642. source_list.append(os.path.join(item, 'SConscript'))
  643. for src in source_list:
  644. dst = src.replace(RTT_ROOT, '')
  645. if dst[0] == os.sep or dst[0] == '/':
  646. dst = dst[1:]
  647. print '=> ', dst
  648. dst = os.path.join(target_path, dst)
  649. do_copy_file(src, dst)
  650. # copy tools directory
  651. print "=> tools"
  652. do_copy_folder(os.path.join(RTT_ROOT, "tools"), os.path.join(target_path, "tools"))
  653. do_copy_file(os.path.join(RTT_ROOT, 'AUTHORS'), os.path.join(target_path, 'AUTHORS'))
  654. do_copy_file(os.path.join(RTT_ROOT, 'COPYING'), os.path.join(target_path, 'COPYING'))
  655. def MakeCopyHeader(program):
  656. global source_ext
  657. source_ext = []
  658. source_ext = ["h", "xpm"]
  659. global source_list
  660. global Rtt_Root
  661. global Env
  662. target_path = os.path.join(Dir('#').abspath, 'rt-thread')
  663. if Env['PLATFORM'] == 'win32':
  664. RTT_ROOT = Rtt_Root.lower()
  665. else:
  666. RTT_ROOT = Rtt_Root
  667. if target_path.startswith(RTT_ROOT):
  668. return
  669. for item in program:
  670. walk_children(item)
  671. source_list.sort()
  672. # filte source file in RT-Thread
  673. target_list = []
  674. for src in source_list:
  675. if Env['PLATFORM'] == 'win32':
  676. src = src.lower()
  677. if src.startswith(RTT_ROOT):
  678. target_list.append(src)
  679. source_list = target_list
  680. for src in source_list:
  681. dst = src.replace(RTT_ROOT, '')
  682. if dst[0] == os.sep or dst[0] == '/':
  683. dst = dst[1:]
  684. print '=> ', dst
  685. dst = os.path.join(target_path, dst)
  686. do_copy_file(src, dst)
  687. # copy tools directory
  688. print "=> tools"
  689. do_copy_folder(os.path.join(RTT_ROOT, "tools"), os.path.join(target_path, "tools"))
  690. do_copy_file(os.path.join(RTT_ROOT, 'AUTHORS'), os.path.join(target_path, 'AUTHORS'))
  691. do_copy_file(os.path.join(RTT_ROOT, 'COPYING'), os.path.join(target_path, 'COPYING'))