ua.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. # File : ua.py
  2. # Tool Script for building User Applications
  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-02-07 Bernard The firstly version
  23. #
  24. import os
  25. import sys
  26. from SCons.Script import *
  27. Rtt_Root = ''
  28. BSP_Root = ''
  29. Env = None
  30. def BuildEnv(BSP_ROOT, RTT_ROOT):
  31. if BSP_ROOT == None:
  32. if os.getenv('BSP_ROOT'):
  33. BSP_ROOT = os.getenv('BSP_ROOT')
  34. else:
  35. print 'Please set BSP(board support package) directory!'
  36. exit(-1)
  37. if not os.path.exists(BSP_ROOT):
  38. print 'No BSP(board support package) directory found!'
  39. exit(-1)
  40. if RTT_ROOT == None:
  41. # get RTT_ROOT from BSP_ROOT
  42. sys.path = sys.path + [BSP_ROOT]
  43. try:
  44. import rtconfig
  45. RTT_ROOT = rtconfig.RTT_ROOT
  46. except Exception as e:
  47. print 'Import rtconfig.py in BSP(board support package) failed.'
  48. print e
  49. exit(-1)
  50. global Rtt_Root
  51. global BSP_Root
  52. Rtt_Root = RTT_ROOT
  53. BSP_Root = BSP_ROOT
  54. def BuildHostApplication(TARGET, SConscriptFile):
  55. import platform
  56. platform_type = platform.system()
  57. if platform_type == 'Windows' or platform_type.find('MINGW') != -1:
  58. TARGET = TARGET.replace('.mo', '.exe')
  59. sys.path = sys.path + [os.path.join(os.getcwd(), 'tools', 'host')]
  60. from building import PrepareHostModuleBuilding
  61. HostRtt = os.path.join(os.getcwd(), 'tools', 'host', 'rtthread')
  62. Env = Environment()
  63. if not GetOption('verbose'):
  64. # override the default verbose command string
  65. Env.Replace(
  66. ARCOMSTR = 'AR $TARGET',
  67. ASCOMSTR = 'AS $TARGET',
  68. ASPPCOMSTR = 'AS $TARGET',
  69. CCCOMSTR = 'CC $TARGET',
  70. CXXCOMSTR = 'CXX $TARGET',
  71. LINKCOMSTR = 'LINK $TARGET'
  72. )
  73. PrepareHostModuleBuilding(Env)
  74. objs = SConscript(SConscriptFile)
  75. objs += SConscript(HostRtt + '/SConscript')
  76. target = Env.Program(TARGET, objs)
  77. return
  78. def BuildHostLibrary(TARGET, SConscriptFile):
  79. import platform
  80. platform_type = platform.system()
  81. if platform_type == 'Windows' or platform_type.find('MINGW') != -1:
  82. TARGET = TARGET.replace('.mo', '.exe')
  83. sys.path = sys.path + [os.path.join(os.getcwd(), 'tools', 'host')]
  84. from building import PrepareHostModuleBuilding
  85. HostRtt = os.path.join(os.getcwd(), 'tools', 'host', 'rtthread')
  86. Env = Environment()
  87. if not GetOption('verbose'):
  88. # override the default verbose command string
  89. Env.Replace(
  90. ARCOMSTR = 'AR $TARGET',
  91. ASCOMSTR = 'AS $TARGET',
  92. ASPPCOMSTR = 'AS $TARGET',
  93. CCCOMSTR = 'CC $TARGET',
  94. CXXCOMSTR = 'CXX $TARGET',
  95. LINKCOMSTR = 'LINK $TARGET'
  96. )
  97. PrepareHostModuleBuilding(Env)
  98. objs = SConscript(SConscriptFile)
  99. objs += SConscript(HostRtt + '/SConscript')
  100. target = Env.Program(TARGET, objs)
  101. return
  102. def BuildApplication(TARGET, SConscriptFile, BSP_ROOT = None, RTT_ROOT = None):
  103. global Env
  104. global Rtt_Root
  105. global BSP_Root
  106. # add comstr option
  107. AddOption('--verbose',
  108. dest='verbose',
  109. action='store_true',
  110. default=False,
  111. help='print verbose information during build')
  112. # build application in host
  113. if BSP_ROOT == None and RTT_ROOT == None and not os.getenv('BSP_ROOT'):
  114. BuildHostApplication(TARGET, SConscriptFile)
  115. return
  116. if RTT_ROOT == None and os.getenv('RTT_ROOT'):
  117. RTT_ROOT = os.getenv('RTT_ROOT')
  118. # handle BSP_ROOT and RTT_ROOT
  119. BuildEnv(BSP_ROOT, RTT_ROOT)
  120. sys.path = sys.path + [os.path.join(Rtt_Root, 'tools'), BSP_Root]
  121. # get configuration from BSP
  122. import rtconfig
  123. from rtua import GetCPPPATH
  124. from rtua import GetCPPDEFINES
  125. from building import PrepareModuleBuilding
  126. from eclipse import TargetEclipse
  127. CPPPATH = GetCPPPATH(BSP_Root, Rtt_Root)
  128. cflags = rtconfig.CFLAGS + ' -mlong-calls -fPIC '
  129. Env = Environment(tools = ['mingw'],
  130. AS = rtconfig.AS, ASFLAGS = rtconfig.AFLAGS,
  131. CC = rtconfig.CC, CCFLAGS = cflags,
  132. CPPDEFINES = GetCPPDEFINES(),
  133. CXX = rtconfig.CXX, AR = rtconfig.AR, ARFLAGS = '-rc',
  134. LINK = rtconfig.LINK, LINKFLAGS = cflags + '-Wl,-z,max-page-size=0x4 -nostartfiles -nostdlib -static-libgcc -shared -e main',
  135. CPPPATH = CPPPATH)
  136. if not GetOption('verbose'):
  137. # override the default verbose command string
  138. Env.Replace(
  139. ARCOMSTR = 'AR $TARGET',
  140. ASCOMSTR = 'AS $TARGET',
  141. ASPPCOMSTR = 'AS $TARGET',
  142. CCCOMSTR = 'CC $TARGET',
  143. CXXCOMSTR = 'CXX $TARGET',
  144. LINKCOMSTR = 'LINK $TARGET'
  145. )
  146. PrepareModuleBuilding(Env, Rtt_Root, BSP_Root)
  147. objs = SConscript(SConscriptFile)
  148. need_exit = False
  149. # build program
  150. target = Env.Program(TARGET, objs)
  151. Env['target'] = target
  152. basename = os.path.basename(TARGET)
  153. if GetOption('target') == 'eclipse':
  154. from eclipse import TargetEclipse
  155. TargetEclipse(Env, GetOption('reset-project-config'), basename.split(".")[0])
  156. need_exit = True
  157. if hasattr(rtconfig, 'M_POST_ACTION'):
  158. Env.AddPostAction(target, rtconfig.M_POST_ACTION)
  159. if hasattr(rtconfig, 'M_BIN_PATH'):
  160. Env.AddPostAction(target, [Copy(rtconfig.M_BIN_PATH, TARGET)])
  161. if need_exit:
  162. exit(0)
  163. def BuildLibrary(TARGET, SConscriptFile, BSP_ROOT = None, RTT_ROOT = None):
  164. global Env
  165. global Rtt_Root
  166. global BSP_Root
  167. # add comstr option
  168. AddOption('--verbose',
  169. dest='verbose',
  170. action='store_true',
  171. default=False,
  172. help='print verbose information during build')
  173. # build application in host
  174. if BSP_ROOT == None and RTT_ROOT == None and not os.getenv('BSP_ROOT'):
  175. BuildHostLibrary(TARGET, SConscriptFile)
  176. return
  177. if RTT_ROOT == None and os.getenv('RTT_ROOT'):
  178. RTT_ROOT = os.getenv('RTT_ROOT')
  179. # handle BSP_ROOT and RTT_ROOT
  180. BuildEnv(BSP_ROOT, RTT_ROOT)
  181. sys.path = sys.path + [os.path.join(Rtt_Root, 'tools'), BSP_Root]
  182. # get configuration from BSP
  183. import rtconfig
  184. from rtua import GetCPPPATH
  185. from rtua import GetCPPDEFINES
  186. from building import PrepareModuleBuilding
  187. linkflags = rtconfig.M_LFLAGS + ' -e 0'
  188. CPPPATH = GetCPPPATH(BSP_Root, Rtt_Root)
  189. Env = Environment(tools = ['mingw'],
  190. AS = rtconfig.AS, ASFLAGS = rtconfig.AFLAGS,
  191. CC = rtconfig.CC, CCFLAGS = rtconfig.M_CFLAGS,
  192. CPPDEFINES = GetCPPDEFINES(),
  193. CXX = rtconfig.CXX, AR = rtconfig.AR, ARFLAGS = '-rc',
  194. LINK = rtconfig.LINK, LINKFLAGS = linkflags,
  195. CPPPATH = CPPPATH)
  196. if not GetOption('verbose'):
  197. # override the default verbose command string
  198. Env.Replace(
  199. ARCOMSTR = 'AR $TARGET',
  200. ASCOMSTR = 'AS $TARGET',
  201. ASPPCOMSTR = 'AS $TARGET',
  202. CCCOMSTR = 'CC $TARGET',
  203. CXXCOMSTR = 'CXX $TARGET',
  204. LINKCOMSTR = 'LINK $TARGET'
  205. )
  206. PrepareModuleBuilding(Env, Rtt_Root, BSP_Root)
  207. objs = SConscript(SConscriptFile)
  208. # build program
  209. target = Env.Program(TARGET, objs)
  210. if hasattr(rtconfig, 'M_POST_ACTION'):
  211. Env.AddPostAction(target, rtconfig.M_POST_ACTION)