building.py 9.3 KB

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