misc: Merged release-staging-v19.0.0.0 into develop
[gem5.git] / util / compile
1 #!/usr/bin/env python2.7
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 import os, re, sys
29 from os.path import isdir, isfile, join as joinpath
30
31 homedir = os.environ['HOME']
32
33 def do_compile():
34 #
35 # Find SCons
36 #
37 search_dirs = [ joinpath(homedir, 'local/lib'), '/opt/local/lib',
38 '/usr/local/lib', '/usr/lib' ]
39
40 if os.environ.has_key("SCONS_LIB_DIR"):
41 search_dirs.append(os.environ["SCONS_LIB_DIR"])
42
43 local = re.compile(r'^scons-local-([0-9]*)\.([0-9]*)\.([0-9]*)$')
44 standard = re.compile(r'^scons-([0-9]*)\.([0-9]*)\.([0-9]*)$')
45
46 scons_dirs = []
47 for dir in search_dirs:
48 if not isdir(dir):
49 continue
50
51 entries = os.listdir(dir)
52 for entry in entries:
53 if not entry.startswith('scons'):
54 continue
55
56 version = (0,0,0)
57 path = joinpath(dir, entry)
58
59 match = local.search(entry)
60 if not match:
61 match = standard.search(entry)
62
63 if match:
64 version = match.group(1), match.group(2), match.group(3)
65
66 scons_dirs.append((version, path))
67
68 scons_dirs.sort()
69 scons_dirs.reverse()
70
71 if not scons_dirs:
72 print >>sys.stderr, \
73 "could not find scons in the following dirs: %s" % search_dirs
74 sys.exit(1)
75
76 sys.path = [ scons_dirs[0][1] ] + sys.path
77
78 # invoke SCons
79 import SCons.Script
80 SCons.Script.main()
81
82 #
83 # do argument parsing
84 #
85 progname = sys.argv[0]
86
87 import optparse
88
89 usage = '''%prog [compile options] <version> [SCons options]
90
91 %prog assumes that the user has a directory called ~/m5/<version> where
92 the source tree resides, and a directory called ~/build, where %prog
93 will create ~/build/<version> if it does not exist and build the resulting
94 simulators there.
95
96 If ~/build is set up in such a way that it points to a local disk on
97 each host, compiles will be very efficient. For example:
98 ~/build -> /z/<username>/.build (Assuming that /z is a local disk and
99 not NFS mounted, whereas your home directory is NFS mounted).
100 '''
101 version = '%prog 0.1'
102 parser = optparse.OptionParser(usage=usage, version=version,
103 formatter=optparse.TitledHelpFormatter())
104 parser.disable_interspersed_args()
105
106 # current option group
107 group = None
108
109 def set_group(*args, **kwargs):
110 '''set the current option group'''
111 global group
112 if not args and not kwargs:
113 group = None
114 else:
115 group = parser.add_option_group(*args, **kwargs)
116
117 def add_option(*args, **kwargs):
118 if group:
119 return group.add_option(*args, **kwargs)
120 else:
121 return parser.add_option(*args, **kwargs)
122
123 def bool_option(name, default, help):
124 '''add a boolean option called --name and --no-name.
125 Display help depending on which is the default'''
126
127 tname = '--%s' % name
128 fname = '--no-%s' % name
129 dest = name.replace('-', '_')
130 if default:
131 thelp = optparse.SUPPRESS_HELP
132 fhelp = help
133 else:
134 thelp = help
135 fhelp = optparse.SUPPRESS_HELP
136
137 add_option(tname, action="store_true", default=default, help=thelp)
138 add_option(fname, action="store_false", dest=dest, help=fhelp)
139
140 add_option('-n', '--no-compile', default=False, action='store_true',
141 help="don't actually compile, just echo SCons command line")
142 add_option('--everything', default=False, action='store_true',
143 help="compile everything that can be compiled")
144 add_option('-E', "--experimental", action='store_true', default=False,
145 help="enable experimental builds")
146 add_option('-v', "--verbose", default=False, action='store_true',
147 help="be verbose")
148
149 set_group("Output binary types")
150 bool_option("debug", default=False, help="compile debug binaries")
151 bool_option("opt", default=False, help="compile opt binaries")
152 bool_option("fast", default=False, help="compile fast binaries")
153 bool_option("prof", default=False, help="compile profile binaries")
154 add_option('-a', "--all-bin", default=False, action='store_true',
155 help="compile debug, opt, and fast binaries")
156
157 set_group("ISA options")
158 bool_option("alpha", default=False, help="compile Alpha")
159 bool_option("mips", default=False, help="compile MIPS")
160 bool_option("sparc", default=False, help="compile SPARC")
161 add_option('-i', "--all-isa", default=False, action='store_true',
162 help="compile all ISAs")
163
164 set_group("Emulation options")
165 bool_option("syscall", default=True,
166 help="Do not compile System Call Emulation mode")
167 bool_option("fullsys", default=True,
168 help="Do not compile Full System mode")
169
170 def usage(exitcode=None):
171 parser.print_help()
172 if exitcode is not None:
173 sys.exit(exitcode)
174
175 (options, args) = parser.parse_args()
176
177 if options.everything:
178 options.all_bin = True
179 options.prof = True
180 options.all_isa = True
181
182 if options.all_bin:
183 options.debug = True
184 options.opt = True
185 options.fast = True
186
187 binaries = []
188 if options.debug:
189 binaries.append('m5.debug')
190 if options.opt:
191 binaries.append('m5.opt')
192 if options.fast:
193 binaries.append('m5.fast')
194 if options.prof:
195 binaries.append('m5.prof')
196
197 if not binaries:
198 binaries.append('m5.debug')
199
200 if options.all_isa:
201 options.alpha = True
202 options.mips = True
203 options.sparc = True
204
205 isas = []
206 if options.alpha:
207 isas.append('alpha')
208 if options.mips:
209 isas.append('mips')
210 if options.sparc:
211 isas.append('sparc')
212
213 if not isas:
214 isas.append('alpha')
215
216 modes = []
217 if options.syscall:
218 modes.append('syscall')
219 if options.fullsys:
220 modes.append('fullsys')
221
222 if not modes:
223 sys.exit("must specify at least one mode")
224
225 #
226 # Convert options into SCons command line arguments
227 #
228
229 # valid combinations of ISA and emulation mode
230 valid = { ('alpha', 'syscall') : 'ALPHA_SE',
231 ('alpha', 'fullsys') : 'ALPHA_FS',
232 ('mips', 'syscall') : 'MIPS_SE',
233 ('sparc', 'syscall') : 'SPARC_SE' }
234
235 # experimental combinations of ISA and emulation mode
236 experiment = { ('mips', 'fullsys') : 'MIPS_FS',
237 ('sparc', 'fullsys') : 'SPARC_FS' }
238
239 if options.experimental:
240 valid.update(experiment)
241
242 builds = []
243 for isa in isas:
244 for mode in modes:
245 try:
246 build = valid[(isa, mode)]
247 builds.append(build)
248 except KeyError:
249 pass
250
251 if not builds:
252 sys.exit("must specify at least one valid combination of ISA and mode")
253
254 if not args:
255 usage(2)
256
257 version = args[0]
258 del args[0]
259
260 for bin in binaries:
261 for build in builds:
262 args.append('%s/%s' % (build, bin))
263
264 #
265 # set up compile
266 #
267 build_base = joinpath(homedir, 'build')
268 m5_base = joinpath(homedir, 'm5')
269
270 if not isdir(build_base):
271 sys.exit('build directory %s not found' % build_base)
272
273 if not isdir(m5_base):
274 sys.exit('m5 base directory %s not found' % m5_base)
275
276 m5_dir = joinpath(m5_base, version)
277 if not isdir(m5_dir):
278 sys.exit('source directory %s not found' % m5_dir)
279
280 # support M5 1.x
281 oldstyle = isfile(joinpath(m5_dir, 'SConscript'))
282 if oldstyle:
283 ext_dir = joinpath(m5_base, 'ext')
284 test_dir = joinpath(m5_base, 'test.' + version)
285
286 if not isdir(ext_dir):
287 sys.exit('ext directory not found at %s' % ext_dir)
288
289 if not isdir(test_dir):
290 sys.exit('test directory not found at %s' % test_dir)
291
292 build_dir = joinpath(build_base, version)
293 if not isdir(build_dir):
294 os.mkdir(build_dir)
295 # need some symlinks for m5 1.x
296 if oldstyle:
297 os.symlink(m5_dir, joinpath(build_dir, 'm5'))
298 os.symlink(ext_dir, joinpath(build_dir, 'ext'))
299 os.symlink(test_dir, joinpath(build_dir, 'test'))
300 os.symlink(joinpath(m5_dir, 'build', 'SConstruct'),
301 joinpath(build_dir, 'SConstruct'))
302 os.symlink(joinpath(m5_dir, 'build', 'default_options'),
303 joinpath(build_dir, 'default_options'))
304
305 sys.argv = [ progname ]
306 if oldstyle:
307 os.chdir(build_dir)
308 sys.argv.extend(args)
309 else:
310 os.chdir(m5_dir)
311 for arg in args:
312 if not arg.startswith('-') and '=' not in arg:
313 arg = joinpath(build_dir, 'build', arg)
314 sys.argv.append(arg)
315
316 if options.no_compile or options.verbose:
317 for arg in sys.argv[1:]:
318 print arg
319
320 if not options.no_compile:
321 do_compile()