building.py 26 KB

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