building.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  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. AddOption('--menuconfig',
  257. dest='menuconfig',
  258. action='store_true',
  259. default=False,
  260. help='do the system configuration')
  261. if GetOption('menuconfig'):
  262. from menuconfig import config
  263. config()
  264. # we need to seperate the variant_dir for BSPs and the kernels. BSPs could
  265. # have their own components etc. If they point to the same folder, SCons
  266. # would find the wrong source code to compile.
  267. bsp_vdir = 'build'
  268. kernel_vdir = 'build/kernel'
  269. # board build script
  270. objs = SConscript('SConscript', variant_dir=bsp_vdir, duplicate=0)
  271. # include kernel
  272. objs.extend(SConscript(Rtt_Root + '/src/SConscript', variant_dir=kernel_vdir + '/src', duplicate=0))
  273. # include libcpu
  274. if not has_libcpu:
  275. objs.extend(SConscript(Rtt_Root + '/libcpu/SConscript',
  276. variant_dir=kernel_vdir + '/libcpu', duplicate=0))
  277. # include components
  278. objs.extend(SConscript(Rtt_Root + '/components/SConscript',
  279. variant_dir=kernel_vdir + '/components',
  280. duplicate=0,
  281. exports='remove_components'))
  282. return objs
  283. def PrepareModuleBuilding(env, root_directory, bsp_directory):
  284. import rtconfig
  285. global BuildOptions
  286. global Env
  287. global Rtt_Root
  288. # patch for win32 spawn
  289. if env['PLATFORM'] == 'win32':
  290. win32_spawn = Win32Spawn()
  291. win32_spawn.env = env
  292. env['SPAWN'] = win32_spawn.spawn
  293. Env = env
  294. Rtt_Root = root_directory
  295. # parse bsp rtconfig.h to get used component
  296. PreProcessor = PatchedPreProcessor()
  297. f = file(bsp_directory + '/rtconfig.h', 'r')
  298. contents = f.read()
  299. f.close()
  300. PreProcessor.process_contents(contents)
  301. BuildOptions = PreProcessor.cpp_namespace
  302. # add build/clean library option for library checking
  303. AddOption('--buildlib',
  304. dest='buildlib',
  305. type='string',
  306. help='building library of a component')
  307. AddOption('--cleanlib',
  308. dest='cleanlib',
  309. action='store_true',
  310. default=False,
  311. help='clean up the library by --buildlib')
  312. # add program path
  313. env.PrependENVPath('PATH', rtconfig.EXEC_PATH)
  314. def GetConfigValue(name):
  315. assert type(name) == str, 'GetConfigValue: only string parameter is valid'
  316. try:
  317. return BuildOptions[name]
  318. except:
  319. return ''
  320. def GetDepend(depend):
  321. building = True
  322. if type(depend) == type('str'):
  323. if not BuildOptions.has_key(depend) or BuildOptions[depend] == 0:
  324. building = False
  325. elif BuildOptions[depend] != '':
  326. return BuildOptions[depend]
  327. return building
  328. # for list type depend
  329. for item in depend:
  330. if item != '':
  331. if not BuildOptions.has_key(item) or BuildOptions[item] == 0:
  332. building = False
  333. return building
  334. def LocalOptions(config_filename):
  335. from SCons.Script import SCons
  336. # parse wiced_config.h to get used component
  337. PreProcessor = SCons.cpp.PreProcessor()
  338. f = file(config_filename, 'r')
  339. contents = f.read()
  340. f.close()
  341. PreProcessor.process_contents(contents)
  342. local_options = PreProcessor.cpp_namespace
  343. return local_options
  344. def GetLocalDepend(options, depend):
  345. building = True
  346. if type(depend) == type('str'):
  347. if not options.has_key(depend) or options[depend] == 0:
  348. building = False
  349. elif options[depend] != '':
  350. return options[depend]
  351. return building
  352. # for list type depend
  353. for item in depend:
  354. if item != '':
  355. if not options.has_key(item) or options[item] == 0:
  356. building = False
  357. return building
  358. def AddDepend(option):
  359. BuildOptions[option] = 1
  360. def MergeGroup(src_group, group):
  361. src_group['src'] = src_group['src'] + group['src']
  362. if group.has_key('CCFLAGS'):
  363. if src_group.has_key('CCFLAGS'):
  364. src_group['CCFLAGS'] = src_group['CCFLAGS'] + group['CCFLAGS']
  365. else:
  366. src_group['CCFLAGS'] = group['CCFLAGS']
  367. if group.has_key('CPPPATH'):
  368. if src_group.has_key('CPPPATH'):
  369. src_group['CPPPATH'] = src_group['CPPPATH'] + group['CPPPATH']
  370. else:
  371. src_group['CPPPATH'] = group['CPPPATH']
  372. if group.has_key('CPPDEFINES'):
  373. if src_group.has_key('CPPDEFINES'):
  374. src_group['CPPDEFINES'] = src_group['CPPDEFINES'] + group['CPPDEFINES']
  375. else:
  376. src_group['CPPDEFINES'] = group['CPPDEFINES']
  377. # for local CCFLAGS/CPPPATH/CPPDEFINES
  378. if group.has_key('LOCAL_CCFLAGS'):
  379. if src_group.has_key('LOCAL_CCFLAGS'):
  380. src_group['LOCAL_CCFLAGS'] = src_group['LOCAL_CCFLAGS'] + group['LOCAL_CCFLAGS']
  381. else:
  382. src_group['LOCAL_CCFLAGS'] = group['LOCAL_CCFLAGS']
  383. if group.has_key('LOCAL_CPPPATH'):
  384. if src_group.has_key('LOCAL_CPPPATH'):
  385. src_group['LOCAL_CPPPATH'] = src_group['LOCAL_CPPPATH'] + group['LOCAL_CPPPATH']
  386. else:
  387. src_group['LOCAL_CPPPATH'] = group['LOCAL_CPPPATH']
  388. if group.has_key('LOCAL_CPPDEFINES'):
  389. if src_group.has_key('LOCAL_CPPDEFINES'):
  390. src_group['LOCAL_CPPDEFINES'] = src_group['LOCAL_CPPDEFINES'] + group['LOCAL_CPPDEFINES']
  391. else:
  392. src_group['LOCAL_CPPDEFINES'] = group['LOCAL_CPPDEFINES']
  393. if group.has_key('LINKFLAGS'):
  394. if src_group.has_key('LINKFLAGS'):
  395. src_group['LINKFLAGS'] = src_group['LINKFLAGS'] + group['LINKFLAGS']
  396. else:
  397. src_group['LINKFLAGS'] = group['LINKFLAGS']
  398. if group.has_key('LIBS'):
  399. if src_group.has_key('LIBS'):
  400. src_group['LIBS'] = src_group['LIBS'] + group['LIBS']
  401. else:
  402. src_group['LIBS'] = group['LIBS']
  403. if group.has_key('LIBPATH'):
  404. if src_group.has_key('LIBPATH'):
  405. src_group['LIBPATH'] = src_group['LIBPATH'] + group['LIBPATH']
  406. else:
  407. src_group['LIBPATH'] = group['LIBPATH']
  408. def DefineGroup(name, src, depend, **parameters):
  409. global Env
  410. if not GetDepend(depend):
  411. return []
  412. # find exist group and get path of group
  413. group_path = ''
  414. for g in Projects:
  415. if g['name'] == name:
  416. group_path = g['path']
  417. if group_path == '':
  418. group_path = GetCurrentDir()
  419. group = parameters
  420. group['name'] = name
  421. group['path'] = group_path
  422. if type(src) == type(['src1']):
  423. group['src'] = File(src)
  424. else:
  425. group['src'] = src
  426. if group.has_key('CCFLAGS'):
  427. Env.AppendUnique(CCFLAGS = group['CCFLAGS'])
  428. if group.has_key('CPPPATH'):
  429. Env.AppendUnique(CPPPATH = group['CPPPATH'])
  430. if group.has_key('CPPDEFINES'):
  431. Env.AppendUnique(CPPDEFINES = group['CPPDEFINES'])
  432. if group.has_key('LINKFLAGS'):
  433. Env.AppendUnique(LINKFLAGS = group['LINKFLAGS'])
  434. # check whether to clean up library
  435. if GetOption('cleanlib') and os.path.exists(os.path.join(group['path'], GroupLibFullName(name, Env))):
  436. if group['src'] != []:
  437. print 'Remove library:', GroupLibFullName(name, Env)
  438. fn = os.path.join(group['path'], GroupLibFullName(name, Env))
  439. if os.path.exists(fn):
  440. os.unlink(fn)
  441. # check whether exist group library
  442. if not GetOption('buildlib') and os.path.exists(os.path.join(group['path'], GroupLibFullName(name, Env))):
  443. group['src'] = []
  444. if group.has_key('LIBS'): group['LIBS'] = group['LIBS'] + [GroupLibName(name, Env)]
  445. else : group['LIBS'] = [GroupLibName(name, Env)]
  446. if group.has_key('LIBPATH'): group['LIBPATH'] = group['LIBPATH'] + [GetCurrentDir()]
  447. else : group['LIBPATH'] = [GetCurrentDir()]
  448. if group.has_key('LIBS'):
  449. Env.AppendUnique(LIBS = group['LIBS'])
  450. if group.has_key('LIBPATH'):
  451. Env.AppendUnique(LIBPATH = group['LIBPATH'])
  452. # check whether to build group library
  453. if group.has_key('LIBRARY'):
  454. objs = Env.Library(name, group['src'])
  455. else:
  456. # only add source
  457. objs = group['src']
  458. # merge group
  459. for g in Projects:
  460. if g['name'] == name:
  461. # merge to this group
  462. MergeGroup(g, group)
  463. return objs
  464. # add a new group
  465. Projects.append(group)
  466. return objs
  467. def GetCurrentDir():
  468. conscript = File('SConscript')
  469. fn = conscript.rfile()
  470. name = fn.name
  471. path = os.path.dirname(fn.abspath)
  472. return path
  473. PREBUILDING = []
  474. def RegisterPreBuildingAction(act):
  475. global PREBUILDING
  476. assert callable(act), 'Could only register callable objects. %s received' % repr(act)
  477. PREBUILDING.append(act)
  478. def PreBuilding():
  479. global PREBUILDING
  480. for a in PREBUILDING:
  481. a()
  482. def GroupLibName(name, env):
  483. import rtconfig
  484. if rtconfig.PLATFORM == 'armcc':
  485. return name + '_rvds'
  486. elif rtconfig.PLATFORM == 'gcc':
  487. return name + '_gcc'
  488. return name
  489. def GroupLibFullName(name, env):
  490. return env['LIBPREFIX'] + GroupLibName(name, env) + env['LIBSUFFIX']
  491. def BuildLibInstallAction(target, source, env):
  492. lib_name = GetOption('buildlib')
  493. for Group in Projects:
  494. if Group['name'] == lib_name:
  495. lib_name = GroupLibFullName(Group['name'], env)
  496. dst_name = os.path.join(Group['path'], lib_name)
  497. print 'Copy %s => %s' % (lib_name, dst_name)
  498. do_copy_file(lib_name, dst_name)
  499. break
  500. def DoBuilding(target, objects):
  501. # merge all objects into one list
  502. def one_list(l):
  503. lst = []
  504. for item in l:
  505. if type(item) == type([]):
  506. lst += one_list(item)
  507. else:
  508. lst.append(item)
  509. return lst
  510. # handle local group
  511. def local_group(group, objects):
  512. if group.has_key('LOCAL_CCFLAGS') or group.has_key('LOCAL_CPPPATH') or group.has_key('LOCAL_CPPDEFINES'):
  513. CCFLAGS = Env.get('CCFLAGS', '') + group.get('LOCAL_CCFLAGS', '')
  514. CPPPATH = Env.get('CPPPATH', ['']) + group.get('LOCAL_CPPPATH', [''])
  515. CPPDEFINES = Env.get('CPPDEFINES', ['']) + group.get('LOCAL_CPPDEFINES', [''])
  516. for source in group['src']:
  517. objects.append(Env.Object(source, CCFLAGS = CCFLAGS,
  518. CPPPATH = CPPPATH, CPPDEFINES = CPPDEFINES))
  519. return True
  520. return False
  521. objects = one_list(objects)
  522. program = None
  523. # check whether special buildlib option
  524. lib_name = GetOption('buildlib')
  525. if lib_name:
  526. objects = [] # remove all of objects
  527. # build library with special component
  528. for Group in Projects:
  529. if Group['name'] == lib_name:
  530. lib_name = GroupLibName(Group['name'], Env)
  531. if not local_group(Group, objects):
  532. objects = Env.Object(Group['src'])
  533. program = Env.Library(lib_name, objects)
  534. # add library copy action
  535. Env.BuildLib(lib_name, program)
  536. break
  537. else:
  538. # remove source files with local flags setting
  539. for group in Projects:
  540. if group.has_key('LOCAL_CCFLAGS') or group.has_key('LOCAL_CPPPATH') or group.has_key('LOCAL_CPPDEFINES'):
  541. for source in group['src']:
  542. for obj in objects:
  543. if source.abspath == obj.abspath or (len(obj.sources) > 0 and source.abspath == obj.sources[0].abspath):
  544. objects.remove(obj)
  545. # re-add the source files to the objects
  546. for group in Projects:
  547. local_group(group, objects)
  548. program = Env.Program(target, objects)
  549. EndBuilding(target, program)
  550. def EndBuilding(target, program = None):
  551. import rtconfig
  552. Env.AddPostAction(target, rtconfig.POST_ACTION)
  553. if GetOption('target') == 'mdk':
  554. from keil import MDKProject
  555. from keil import MDK4Project
  556. from keil import MDK5Project
  557. template = os.path.isfile('template.Uv2')
  558. if template:
  559. MDKProject('project.Uv2', Projects)
  560. else:
  561. template = os.path.isfile('template.uvproj')
  562. if template:
  563. MDK4Project('project.uvproj', Projects)
  564. else:
  565. template = os.path.isfile('template.uvprojx')
  566. if template:
  567. MDK5Project('project.uvprojx', Projects)
  568. else:
  569. print 'No template project file found.'
  570. if GetOption('target') == 'mdk4':
  571. from keil import MDK4Project
  572. MDK4Project('project.uvproj', Projects)
  573. if GetOption('target') == 'mdk5':
  574. from keil import MDK5Project
  575. MDK5Project('project.uvprojx', Projects)
  576. if GetOption('target') == 'iar':
  577. from iar import IARProject
  578. IARProject('project.ewp', Projects)
  579. if GetOption('target') == 'vs':
  580. from vs import VSProject
  581. VSProject('project.vcproj', Projects, program)
  582. if GetOption('target') == 'vs2012':
  583. from vs2012 import VS2012Project
  584. VS2012Project('project.vcxproj', Projects, program)
  585. if GetOption('target') == 'cb':
  586. from codeblocks import CBProject
  587. CBProject('project.cbp', Projects, program)
  588. if GetOption('target') == 'ua':
  589. from ua import PrepareUA
  590. PrepareUA(Projects, Rtt_Root, str(Dir('#')))
  591. BSP_ROOT = Dir('#').abspath
  592. if GetOption('copy') and program != None:
  593. from mkdist import MakeCopy
  594. MakeCopy(program, BSP_ROOT, Rtt_Root, Env)
  595. exit(0)
  596. if GetOption('copy-header') and program != None:
  597. from mkdist import MakeCopyHeader
  598. MakeCopyHeader(program, BSP_ROOT, Rtt_Root, Env)
  599. exit(0)
  600. if GetOption('make-dist') and program != None:
  601. from mkdist import MkDist
  602. MkDist(program, BSP_ROOT, Rtt_Root, Env)
  603. exit(0)
  604. if GetOption('cscope'):
  605. from cscope import CscopeDatabase
  606. CscopeDatabase(Projects)
  607. def SrcRemove(src, remove):
  608. if not src:
  609. return
  610. for item in src:
  611. if type(item) == type('str'):
  612. if os.path.basename(item) in remove:
  613. src.remove(item)
  614. else:
  615. if os.path.basename(item.rstr()) in remove:
  616. src.remove(item)
  617. def GetVersion():
  618. import SCons.cpp
  619. import string
  620. rtdef = os.path.join(Rtt_Root, 'include', 'rtdef.h')
  621. # parse rtdef.h to get RT-Thread version
  622. prepcessor = PatchedPreProcessor()
  623. f = file(rtdef, 'r')
  624. contents = f.read()
  625. f.close()
  626. prepcessor.process_contents(contents)
  627. def_ns = prepcessor.cpp_namespace
  628. version = int(filter(lambda ch: ch in '0123456789.', def_ns['RT_VERSION']))
  629. subversion = int(filter(lambda ch: ch in '0123456789.', def_ns['RT_SUBVERSION']))
  630. if def_ns.has_key('RT_REVISION'):
  631. revision = int(filter(lambda ch: ch in '0123456789.', def_ns['RT_REVISION']))
  632. return '%d.%d.%d' % (version, subversion, revision)
  633. return '0.%d.%d' % (version, subversion)
  634. def GlobSubDir(sub_dir, ext_name):
  635. import os
  636. import glob
  637. def glob_source(sub_dir, ext_name):
  638. list = os.listdir(sub_dir)
  639. src = glob.glob(os.path.join(sub_dir, ext_name))
  640. for item in list:
  641. full_subdir = os.path.join(sub_dir, item)
  642. if os.path.isdir(full_subdir):
  643. src += glob_source(full_subdir, ext_name)
  644. return src
  645. dst = []
  646. src = glob_source(sub_dir, ext_name)
  647. for item in src:
  648. dst.append(os.path.relpath(item, sub_dir))
  649. return dst
  650. def PackageSConscript(package):
  651. from package import BuildPackage
  652. return BuildPackage(package)