building.py 27 KB

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