building.py 25 KB

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