PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/util/compile

https://bitbucket.org/musleh123/gem5_cetus
Python | 323 lines | 270 code | 18 blank | 35 comment | 12 complexity | 6c14f59e03ebf58c63089219ae2f9b25 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. #!/usr/bin/env python
  2. # Copyright (c) 2006 The Regents of The University of Michigan
  3. # All rights reserved.
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions are
  7. # met: redistributions of source code must retain the above copyright
  8. # notice, this list of conditions and the following disclaimer;
  9. # redistributions in binary form must reproduce the above copyright
  10. # notice, this list of conditions and the following disclaimer in the
  11. # documentation and/or other materials provided with the distribution;
  12. # neither the name of the copyright holders nor the names of its
  13. # contributors may be used to endorse or promote products derived from
  14. # this software without specific prior written permission.
  15. #
  16. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  20. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. #
  28. # Authors: Nathan Binkert
  29. import os, re, sys
  30. from os.path import isdir, isfile, join as joinpath
  31. homedir = os.environ['HOME']
  32. def do_compile():
  33. #
  34. # Find SCons
  35. #
  36. search_dirs = [ joinpath(homedir, 'local/lib'), '/opt/local/lib',
  37. '/usr/local/lib', '/usr/lib' ]
  38. if os.environ.has_key("SCONS_LIB_DIR"):
  39. search_dirs.append(os.environ["SCONS_LIB_DIR"])
  40. local = re.compile(r'^scons-local-([0-9]*)\.([0-9]*)\.([0-9]*)$')
  41. standard = re.compile(r'^scons-([0-9]*)\.([0-9]*)\.([0-9]*)$')
  42. scons_dirs = []
  43. for dir in search_dirs:
  44. if not isdir(dir):
  45. continue
  46. entries = os.listdir(dir)
  47. for entry in entries:
  48. if not entry.startswith('scons'):
  49. continue
  50. version = (0,0,0)
  51. path = joinpath(dir, entry)
  52. match = local.search(entry)
  53. if not match:
  54. match = standard.search(entry)
  55. if match:
  56. version = match.group(1), match.group(2), match.group(3)
  57. scons_dirs.append((version, path))
  58. scons_dirs.sort()
  59. scons_dirs.reverse()
  60. if not scons_dirs:
  61. print >>sys.stderr, \
  62. "could not find scons in the following dirs: %s" % search_dirs
  63. sys.exit(1)
  64. sys.path = [ scons_dirs[0][1] ] + sys.path
  65. # invoke SCons
  66. import SCons.Script
  67. SCons.Script.main()
  68. #
  69. # do argument parsing
  70. #
  71. progname = sys.argv[0]
  72. import optparse
  73. usage = '''%prog [compile options] <version> [SCons options]
  74. %prog assumes that the user has a directory called ~/m5/<version> where
  75. the source tree resides, and a directory called ~/build, where %prog
  76. will create ~/build/<version> if it does not exist and build the resulting
  77. simulators there.
  78. If ~/build is set up in such a way that it points to a local disk on
  79. each host, compiles will be very efficient. For example:
  80. ~/build -> /z/<username>/.build (Assuming that /z is a local disk and
  81. not NFS mounted, whereas your home directory is NFS mounted).
  82. '''
  83. version = '%prog 0.1'
  84. parser = optparse.OptionParser(usage=usage, version=version,
  85. formatter=optparse.TitledHelpFormatter())
  86. parser.disable_interspersed_args()
  87. # current option group
  88. group = None
  89. def set_group(*args, **kwargs):
  90. '''set the current option group'''
  91. global group
  92. if not args and not kwargs:
  93. group = None
  94. else:
  95. group = parser.add_option_group(*args, **kwargs)
  96. def add_option(*args, **kwargs):
  97. if group:
  98. return group.add_option(*args, **kwargs)
  99. else:
  100. return parser.add_option(*args, **kwargs)
  101. def bool_option(name, default, help):
  102. '''add a boolean option called --name and --no-name.
  103. Display help depending on which is the default'''
  104. tname = '--%s' % name
  105. fname = '--no-%s' % name
  106. dest = name.replace('-', '_')
  107. if default:
  108. thelp = optparse.SUPPRESS_HELP
  109. fhelp = help
  110. else:
  111. thelp = help
  112. fhelp = optparse.SUPPRESS_HELP
  113. add_option(tname, action="store_true", default=default, help=thelp)
  114. add_option(fname, action="store_false", dest=dest, help=fhelp)
  115. add_option('-n', '--no-compile', default=False, action='store_true',
  116. help="don't actually compile, just echo SCons command line")
  117. add_option('--everything', default=False, action='store_true',
  118. help="compile everything that can be compiled")
  119. add_option('-E', "--experimental", action='store_true', default=False,
  120. help="enable experimental builds")
  121. add_option('-v', "--verbose", default=False, action='store_true',
  122. help="be verbose")
  123. set_group("Output binary types")
  124. bool_option("debug", default=False, help="compile debug binaries")
  125. bool_option("opt", default=False, help="compile opt binaries")
  126. bool_option("fast", default=False, help="compile fast binaries")
  127. bool_option("prof", default=False, help="compile profile binaries")
  128. add_option('-a', "--all-bin", default=False, action='store_true',
  129. help="compile debug, opt, and fast binaries")
  130. set_group("ISA options")
  131. bool_option("alpha", default=False, help="compile Alpha")
  132. bool_option("mips", default=False, help="compile MIPS")
  133. bool_option("sparc", default=False, help="compile SPARC")
  134. add_option('-i', "--all-isa", default=False, action='store_true',
  135. help="compile all ISAs")
  136. set_group("Emulation options")
  137. bool_option("syscall", default=True,
  138. help="Do not compile System Call Emulation mode")
  139. bool_option("fullsys", default=True,
  140. help="Do not compile Full System mode")
  141. def usage(exitcode=None):
  142. parser.print_help()
  143. if exitcode is not None:
  144. sys.exit(exitcode)
  145. (options, args) = parser.parse_args()
  146. if options.everything:
  147. options.all_bin = True
  148. options.prof = True
  149. options.all_isa = True
  150. if options.all_bin:
  151. options.debug = True
  152. options.opt = True
  153. options.fast = True
  154. binaries = []
  155. if options.debug:
  156. binaries.append('m5.debug')
  157. if options.opt:
  158. binaries.append('m5.opt')
  159. if options.fast:
  160. binaries.append('m5.fast')
  161. if options.prof:
  162. binaries.append('m5.prof')
  163. if not binaries:
  164. binaries.append('m5.debug')
  165. if options.all_isa:
  166. options.alpha = True
  167. options.mips = True
  168. options.sparc = True
  169. isas = []
  170. if options.alpha:
  171. isas.append('alpha')
  172. if options.mips:
  173. isas.append('mips')
  174. if options.sparc:
  175. isas.append('sparc')
  176. if not isas:
  177. isas.append('alpha')
  178. modes = []
  179. if options.syscall:
  180. modes.append('syscall')
  181. if options.fullsys:
  182. modes.append('fullsys')
  183. if not modes:
  184. sys.exit("must specify at least one mode")
  185. #
  186. # Convert options into SCons command line arguments
  187. #
  188. # valid combinations of ISA and emulation mode
  189. valid = { ('alpha', 'syscall') : 'ALPHA_SE',
  190. ('alpha', 'fullsys') : 'ALPHA_FS',
  191. ('mips', 'syscall') : 'MIPS_SE',
  192. ('sparc', 'syscall') : 'SPARC_SE' }
  193. # experimental combinations of ISA and emulation mode
  194. experiment = { ('mips', 'fullsys') : 'MIPS_FS',
  195. ('sparc', 'fullsys') : 'SPARC_FS' }
  196. if options.experimental:
  197. valid.update(experiment)
  198. builds = []
  199. for isa in isas:
  200. for mode in modes:
  201. try:
  202. build = valid[(isa, mode)]
  203. builds.append(build)
  204. except KeyError:
  205. pass
  206. if not builds:
  207. sys.exit("must specify at least one valid combination of ISA and mode")
  208. if not args:
  209. usage(2)
  210. version = args[0]
  211. del args[0]
  212. for bin in binaries:
  213. for build in builds:
  214. args.append('%s/%s' % (build, bin))
  215. #
  216. # set up compile
  217. #
  218. build_base = joinpath(homedir, 'build')
  219. m5_base = joinpath(homedir, 'm5')
  220. if not isdir(build_base):
  221. sys.exit('build directory %s not found' % build_base)
  222. if not isdir(m5_base):
  223. sys.exit('m5 base directory %s not found' % m5_base)
  224. m5_dir = joinpath(m5_base, version)
  225. if not isdir(m5_dir):
  226. sys.exit('source directory %s not found' % m5_dir)
  227. # support M5 1.x
  228. oldstyle = isfile(joinpath(m5_dir, 'SConscript'))
  229. if oldstyle:
  230. ext_dir = joinpath(m5_base, 'ext')
  231. test_dir = joinpath(m5_base, 'test.' + version)
  232. if not isdir(ext_dir):
  233. sys.exit('ext directory not found at %s' % ext_dir)
  234. if not isdir(test_dir):
  235. sys.exit('test directory not found at %s' % test_dir)
  236. build_dir = joinpath(build_base, version)
  237. if not isdir(build_dir):
  238. os.mkdir(build_dir)
  239. # need some symlinks for m5 1.x
  240. if oldstyle:
  241. os.symlink(m5_dir, joinpath(build_dir, 'm5'))
  242. os.symlink(ext_dir, joinpath(build_dir, 'ext'))
  243. os.symlink(test_dir, joinpath(build_dir, 'test'))
  244. os.symlink(joinpath(m5_dir, 'build', 'SConstruct'),
  245. joinpath(build_dir, 'SConstruct'))
  246. os.symlink(joinpath(m5_dir, 'build', 'default_options'),
  247. joinpath(build_dir, 'default_options'))
  248. sys.argv = [ progname ]
  249. if oldstyle:
  250. os.chdir(build_dir)
  251. sys.argv.extend(args)
  252. else:
  253. os.chdir(m5_dir)
  254. for arg in args:
  255. if not arg.startswith('-') and '=' not in arg:
  256. arg = joinpath(build_dir, 'build', arg)
  257. sys.argv.append(arg)
  258. if options.no_compile or options.verbose:
  259. for arg in sys.argv[1:]:
  260. print arg
  261. if not options.no_compile:
  262. do_compile()