building.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. import os
  2. import sys
  3. import string
  4. from SCons.Script import *
  5. BuildOptions = {}
  6. Projects = []
  7. Rtt_Root = ''
  8. Env = None
  9. def _get_filetype(fn):
  10. if fn.rfind('.c') != -1 or fn.rfind('.C') != -1 or fn.rfind('.cpp') != -1:
  11. return 1
  12. # assimble file type
  13. if fn.rfind('.s') != -1 or fn.rfind('.S') != -1:
  14. return 2
  15. # header type
  16. if fn.rfind('.h') != -1:
  17. return 5
  18. # other filetype
  19. return 5
  20. def splitall(loc):
  21. """
  22. Return a list of the path components in loc. (Used by relpath_).
  23. The first item in the list will be either ``os.curdir``, ``os.pardir``, empty,
  24. or the root directory of loc (for example, ``/`` or ``C:\\).
  25. The other items in the list will be strings.
  26. Adapted from *path.py* by Jason Orendorff.
  27. """
  28. parts = []
  29. while loc != os.curdir and loc != os.pardir:
  30. prev = loc
  31. loc, child = os.path.split(prev)
  32. if loc == prev:
  33. break
  34. parts.append(child)
  35. parts.append(loc)
  36. parts.reverse()
  37. return parts
  38. def _make_path_relative(origin, dest):
  39. """
  40. Return the relative path between origin and dest.
  41. If it's not possible return dest.
  42. If they are identical return ``os.curdir``
  43. Adapted from `path.py <http://www.jorendorff.com/articles/python/path/>`_ by Jason Orendorff.
  44. """
  45. origin = os.path.abspath(origin).replace('\\', '/')
  46. dest = os.path.abspath(dest).replace('\\', '/')
  47. #
  48. orig_list = splitall(os.path.normcase(origin))
  49. # Don't normcase dest! We want to preserve the case.
  50. dest_list = splitall(dest)
  51. #
  52. if orig_list[0] != os.path.normcase(dest_list[0]):
  53. # Can't get here from there.
  54. return dest
  55. #
  56. # Find the location where the two paths start to differ.
  57. i = 0
  58. for start_seg, dest_seg in zip(orig_list, dest_list):
  59. if start_seg != os.path.normcase(dest_seg):
  60. break
  61. i += 1
  62. #
  63. # Now i is the point where the two paths diverge.
  64. # Need a certain number of "os.pardir"s to work up
  65. # from the origin to the point of divergence.
  66. segments = [os.pardir] * (len(orig_list) - i)
  67. # Need to add the diverging part of dest_list.
  68. segments += dest_list[i:]
  69. if len(segments) == 0:
  70. # If they happen to be identical, use os.curdir.
  71. return os.curdir
  72. else:
  73. # return os.path.join(*segments).replace('\\', '/')
  74. return os.path.join(*segments)
  75. def IARProject(target, script):
  76. import xml.etree.ElementTree as etree
  77. project = file(target, "wb")
  78. project_path = os.path.dirname(os.path.abspath(target))
  79. tree = etree.parse('template.ewp')
  80. tree.write('project.ewp')
  81. def MDKProject(target, script):
  82. template = file('template.Uv2', "rb")
  83. lines = template.readlines()
  84. project = file(target, "wb")
  85. project_path = os.path.dirname(os.path.abspath(target))
  86. line_index = 5
  87. # write group
  88. for group in script:
  89. lines.insert(line_index, 'Group (%s)\r\n' % group['name'])
  90. line_index += 1
  91. lines.insert(line_index, '\r\n')
  92. line_index += 1
  93. # write file
  94. ProjectFiles = []
  95. CPPPATH = []
  96. CPPDEFINES = []
  97. LINKFLAGS = ''
  98. CCFLAGS = ''
  99. # number of groups
  100. group_index = 1
  101. for group in script:
  102. # print group['name']
  103. # get each include path
  104. if group.has_key('CPPPATH') and group['CPPPATH']:
  105. if CPPPATH:
  106. CPPPATH += group['CPPPATH']
  107. else:
  108. CPPPATH += group['CPPPATH']
  109. # get each group's definitions
  110. if group.has_key('CPPDEFINES') and group['CPPDEFINES']:
  111. if CPPDEFINES:
  112. CPPDEFINES += ';' + group['CPPDEFINES']
  113. else:
  114. CPPDEFINES += group['CPPDEFINES']
  115. # get each group's link flags
  116. if group.has_key('LINKFLAGS') and group['LINKFLAGS']:
  117. if LINKFLAGS:
  118. LINKFLAGS += ' ' + group['LINKFLAGS']
  119. else:
  120. LINKFLAGS += group['LINKFLAGS']
  121. # generate file items
  122. for node in group['src']:
  123. fn = node.rfile()
  124. name = fn.name
  125. path = os.path.dirname(fn.abspath)
  126. basename = os.path.basename(path)
  127. path = _make_path_relative(project_path, path)
  128. path = os.path.join(path, name)
  129. if ProjectFiles.count(name):
  130. name = basename + '_' + name
  131. ProjectFiles.append(name)
  132. lines.insert(line_index, 'File %d,%d,<%s><%s>\r\n'
  133. % (group_index, _get_filetype(name), path, name))
  134. line_index += 1
  135. group_index = group_index + 1
  136. lines.insert(line_index, '\r\n')
  137. line_index += 1
  138. # remove repeat path
  139. paths = set()
  140. for path in CPPPATH:
  141. inc = _make_path_relative(project_path, os.path.normpath(path))
  142. paths.add(inc) #.replace('\\', '/')
  143. paths = [i for i in paths]
  144. CPPPATH = string.join(paths, ';')
  145. definitions = [i for i in set(CPPDEFINES)]
  146. CPPDEFINES = string.join(definitions, ', ')
  147. while line_index < len(lines):
  148. if lines[line_index].startswith(' ADSCINCD '):
  149. lines[line_index] = ' ADSCINCD (' + CPPPATH + ')\r\n'
  150. if lines[line_index].startswith(' ADSLDMC ('):
  151. lines[line_index] = ' ADSLDMC (' + LINKFLAGS + ')\r\n'
  152. if lines[line_index].startswith(' ADSCDEFN ('):
  153. lines[line_index] = ' ADSCDEFN (' + CPPDEFINES + ')\r\n'
  154. line_index += 1
  155. # write project
  156. for line in lines:
  157. project.write(line)
  158. project.close()
  159. def BuilderProject(target, script):
  160. project = file(target, "wb")
  161. project_path = os.path.dirname(os.path.abspath(target))
  162. # write file
  163. CPPPATH = []
  164. CPPDEFINES = []
  165. LINKFLAGS = ''
  166. CCFLAGS = ''
  167. # number of groups
  168. group_index = 1
  169. for group in script:
  170. # print group['name']
  171. # generate file items
  172. for node in group['src']:
  173. fn = node.rfile()
  174. name = fn.name
  175. path = os.path.dirname(fn.abspath)
  176. path = _make_path_relative(project_path, path)
  177. path = os.path.join(path, name)
  178. project.write('%s\r\n' % path)
  179. group_index = group_index + 1
  180. project.close()
  181. class Win32Spawn:
  182. def spawn(self, sh, escape, cmd, args, env):
  183. import subprocess
  184. newargs = string.join(args[1:], ' ')
  185. cmdline = cmd + " " + newargs
  186. startupinfo = subprocess.STARTUPINFO()
  187. # startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
  188. proc = subprocess.Popen(cmdline, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
  189. stderr=subprocess.PIPE, startupinfo=startupinfo, shell = False)
  190. data, err = proc.communicate()
  191. rv = proc.wait()
  192. if rv:
  193. print err
  194. return rv
  195. if data:
  196. print data
  197. return 0
  198. def PrepareBuilding(env, root_directory, has_libcpu=False):
  199. import SCons.cpp
  200. import rtconfig
  201. global BuildOptions
  202. global Projects
  203. global Env
  204. global Rtt_Root
  205. Env = env
  206. Rtt_Root = root_directory
  207. # patch for win32 spawn
  208. if env['PLATFORM'] == 'win32' and rtconfig.PLATFORM == 'gcc' and sys.version_info < (2, 6, 0):
  209. win32_spawn = Win32Spawn()
  210. win32_spawn.env = env
  211. env['SPAWN'] = win32_spawn.spawn
  212. # add program path
  213. env.PrependENVPath('PATH', rtconfig.EXEC_PATH)
  214. # parse rtconfig.h to get used component
  215. PreProcessor = SCons.cpp.PreProcessor()
  216. f = file('rtconfig.h', 'r')
  217. contents = f.read()
  218. f.close()
  219. PreProcessor.process_contents(contents)
  220. BuildOptions = PreProcessor.cpp_namespace
  221. if (GetDepend('RT_USING_NEWLIB') == False and GetDepend('RT_USING_NOLIBC') == False) and rtconfig.PLATFORM == 'gcc':
  222. AddDepend('RT_USING_MINILIBC')
  223. if (GetDepend('RT_USING_LWIP') == True and GetDepend('RT_LWIP_VER140') == False):
  224. AddDepend('RT_LWIP_VER130')
  225. # add target option
  226. AddOption('--target',
  227. dest='target',
  228. type='string',
  229. help='set target project: mdk')
  230. if GetOption('target'):
  231. SetOption('no_exec', 1)
  232. #env['CCCOMSTR'] = "CC $TARGET"
  233. #env['ASCOMSTR'] = "AS $TARGET"
  234. #env['LINKCOMSTR'] = "Link $TARGET"
  235. # board build script
  236. objs = SConscript('SConscript', variant_dir='build/bsp', duplicate=0)
  237. Repository(Rtt_Root)
  238. # include kernel
  239. objs.append(SConscript('src/SConscript', variant_dir='build/src', duplicate=0))
  240. # include libcpu
  241. if not has_libcpu:
  242. objs.append(SConscript('libcpu/SConscript', variant_dir='build/libcpu', duplicate=0))
  243. # include components
  244. objs.append(SConscript('components/SConscript', variant_dir='build/components', duplicate=0))
  245. return objs
  246. def GetDepend(depend):
  247. building = True
  248. if type(depend) == type('str'):
  249. if not BuildOptions.has_key(depend):
  250. building = False
  251. return building
  252. # for list type depend
  253. for item in depend:
  254. if item != '':
  255. if not BuildOptions.has_key(item):
  256. building = False
  257. return building
  258. def AddDepend(option):
  259. BuildOptions[option] = 1
  260. def DefineGroup(name, src, depend, **parameters):
  261. global Env
  262. if not GetDepend(depend):
  263. return []
  264. group = parameters
  265. group['name'] = name
  266. if type(src) == type(['src1', 'str2']):
  267. group['src'] = File(src)
  268. else:
  269. group['src'] = src
  270. Projects.append(group)
  271. if group.has_key('CCFLAGS'):
  272. Env.Append(CCFLAGS = group['CCFLAGS'])
  273. if group.has_key('CPPPATH'):
  274. Env.Append(CPPPATH = group['CPPPATH'])
  275. if group.has_key('CPPDEFINES'):
  276. Env.Append(CPPDEFINES = group['CPPDEFINES'])
  277. if group.has_key('LINKFLAGS'):
  278. Env.Append(LINKFLAGS = group['LINKFLAGS'])
  279. objs = Env.Object(group['src'])
  280. if group.has_key('LIBRARY'):
  281. objs = Env.Library(name, objs)
  282. return objs
  283. def EndBuilding(target):
  284. import rtconfig
  285. Env.AddPostAction(target, rtconfig.POST_ACTION)
  286. if GetOption('target') == 'iar':
  287. IARProject('project.ewp', Projects)
  288. if GetOption('target') == 'mdk':
  289. MDKProject('project.Uv2', Projects)