3 # Copyright (c) 2004-2005 The Regents of The University of Michigan
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are
8 # met: redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer;
10 # redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution;
13 # neither the name of the copyright holders nor the names of its
14 # contributors may be used to endorse or promote products derived from
15 # this software without specific prior written permission.
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 # Authors: Nathan Binkert
40 from os.path import basename, dirname, exists, isdir, isfile, join as joinpath
44 # This file defines how to build a particular configuration of gem5
45 # based on variable settings in the 'env' build environment.
49 # Children need to see the environment
52 build_env = [(opt, env[opt]) for opt in export_vars]
54 from m5.util import code_formatter, compareVersions
56 ########################################################################
57 # Code for adding source files of various types
59 # When specifying a source file of some type, a set of guards can be
60 # specified for that file. When get() is used to find the files, if
61 # get specifies a set of filters, only files that match those filters
62 # will be accepted (unspecified filters on files are assumed to be
63 # false). Current filters are:
64 # main -- specifies the gem5 main() function
65 # skip_lib -- do not put this file into the gem5 library
66 # <unittest> -- unit tests use filters based on the unit test name
68 # A parent can now be specified for a source file and default filter
69 # values will be retrieved recursively from parents (children override
72 class SourceMeta(type):
73 '''Meta class for source files that keeps track of all files of a
74 particular type and has a get function for finding all functions
75 of a certain type that match a set of guards'''
76 def __init__(cls, name, bases, dict):
77 super(SourceMeta, cls).__init__(name, bases, dict)
80 def get(cls, **guards):
81 '''Find all files that match the specified guards. If a source
82 file does not specify a flag, the default is False'''
84 for flag,value in guards.iteritems():
85 # if the flag is found and has a different value, skip
87 if src.all_guards.get(flag, False) != value:
92 class SourceFile(object):
93 '''Base object that encapsulates the notion of a source file.
94 This includes, the source node, target node, various manipulations
95 of those. A source file also specifies a set of guards which
96 describing which builds the source file applies to. A parent can
97 also be specified to get default guards from'''
98 __metaclass__ = SourceMeta
99 def __init__(self, source, parent=None, **guards):
104 if not isinstance(source, SCons.Node.FS.File):
108 self.snode = tnode.srcnode()
110 for base in type(self).__mro__:
111 if issubclass(base, SourceFile):
112 base.all.append(self)
116 return str(self.tnode)
120 return dirname(self.filename)
124 return basename(self.filename)
128 index = self.basename.rfind('.')
130 # dot files aren't extensions
131 return self.basename, None
133 return self.basename[:index], self.basename[index+1:]
136 def all_guards(self):
137 '''find all guards for this object getting default values
138 recursively from its parents'''
141 guards.update(self.parent.guards)
142 guards.update(self.guards)
145 def __lt__(self, other): return self.filename < other.filename
146 def __le__(self, other): return self.filename <= other.filename
147 def __gt__(self, other): return self.filename > other.filename
148 def __ge__(self, other): return self.filename >= other.filename
149 def __eq__(self, other): return self.filename == other.filename
150 def __ne__(self, other): return self.filename != other.filename
154 def disabled(cls, name, *ignored):
155 raise RuntimeError("Additional SourceFile '%s'" % name,\
156 "declared, but targets deps are already fixed.")
157 SourceFile.__init__ = disabled
160 class Source(SourceFile):
161 '''Add a c/c++ source file to the build'''
162 def __init__(self, source, Werror=True, swig=False, **guards):
163 '''specify the source file, and any guards'''
164 super(Source, self).__init__(source, **guards)
169 class PySource(SourceFile):
170 '''Add a python source file to the named package'''
171 invalid_sym_char = re.compile('[^A-z0-9_]')
176 def __init__(self, package, source, **guards):
177 '''specify the python package, the source file, and any guards'''
178 super(PySource, self).__init__(source, **guards)
180 modname,ext = self.extname
184 path = package.split('.')
189 if modname != '__init__':
190 modpath += [ modname ]
191 modpath = '.'.join(modpath)
193 arcpath = path + [ self.basename ]
194 abspath = self.snode.abspath
195 if not exists(abspath):
196 abspath = self.tnode.abspath
198 self.package = package
199 self.modname = modname
200 self.modpath = modpath
201 self.arcname = joinpath(*arcpath)
202 self.abspath = abspath
203 self.compiled = File(self.filename + 'c')
204 self.cpp = File(self.filename + '.cc')
205 self.symname = PySource.invalid_sym_char.sub('_', modpath)
207 PySource.modules[modpath] = self
208 PySource.tnodes[self.tnode] = self
209 PySource.symnames[self.symname] = self
211 class SimObject(PySource):
212 '''Add a SimObject python file as a python source object and add
213 it to a list of sim object modules'''
218 def __init__(self, source, **guards):
219 '''Specify the source file and any guards (automatically in
220 the m5.objects package)'''
221 super(SimObject, self).__init__('m5.objects', source, **guards)
223 raise AttributeError, "Too late to call SimObject now."
225 bisect.insort_right(SimObject.modnames, self.modname)
227 class SwigSource(SourceFile):
228 '''Add a swig file to build'''
230 def __init__(self, package, source, **guards):
231 '''Specify the python package, the source file, and any guards'''
232 super(SwigSource, self).__init__(source, **guards)
234 modname,ext = self.extname
237 self.module = modname
238 cc_file = joinpath(self.dirname, modname + '_wrap.cc')
239 py_file = joinpath(self.dirname, modname + '.py')
241 self.cc_source = Source(cc_file, swig=True, parent=self)
242 self.py_source = PySource(package, py_file, parent=self)
244 class ProtoBuf(SourceFile):
245 '''Add a Protocol Buffer to build'''
247 def __init__(self, source, **guards):
248 '''Specify the source file, and any guards'''
249 super(ProtoBuf, self).__init__(source, **guards)
251 # Get the file name and the extension
252 modname,ext = self.extname
253 assert ext == 'proto'
255 # Currently, we stick to generating the C++ headers, so we
256 # only need to track the source and header.
257 self.cc_file = File(modname + '.pb.cc')
258 self.hh_file = File(modname + '.pb.h')
260 class UnitTest(object):
261 '''Create a UnitTest'''
264 def __init__(self, target, *sources, **kwargs):
265 '''Specify the target name and any sources. Sources that are
266 not SourceFiles are evalued with Source(). All files are
267 guarded with a guard of the same name as the UnitTest
272 if not isinstance(src, SourceFile):
273 src = Source(src, skip_lib=True)
274 src.guards[target] = True
279 self.main = kwargs.get('main', False)
280 UnitTest.all.append(self)
282 # Children should have access
290 ########################################################################
295 def DebugFlag(name, desc=None):
296 if name in debug_flags:
297 raise AttributeError, "Flag %s already specified" % name
298 debug_flags[name] = (name, (), desc)
300 def CompoundFlag(name, flags, desc=None):
301 if name in debug_flags:
302 raise AttributeError, "Flag %s already specified" % name
304 compound = tuple(flags)
305 debug_flags[name] = (name, compound, desc)
308 Export('CompoundFlag')
310 ########################################################################
312 # Set some compiler variables
315 # Include file paths are rooted in this directory. SCons will
316 # automatically expand '.' to refer to both the source directory and
317 # the corresponding build directory to pick up generated include
319 env.Append(CPPPATH=Dir('.'))
321 for extra_dir in extras_dir_list:
322 env.Append(CPPPATH=Dir(extra_dir))
324 # Workaround for bug in SCons version > 0.97d20071212
325 # Scons bug id: 2006 gem5 Bug id: 308
326 for root, dirs, files in os.walk(base_dir, topdown=True):
327 Dir(root[len(base_dir) + 1:])
329 ########################################################################
331 # Walk the tree and execute all SConscripts in subdirectories
334 here = Dir('.').srcnode().abspath
335 for root, dirs, files in os.walk(base_dir, topdown=True):
337 # we don't want to recurse back into this SConscript
340 if 'SConscript' in files:
341 build_dir = joinpath(env['BUILDDIR'], root[len(base_dir) + 1:])
342 SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
344 for extra_dir in extras_dir_list:
345 prefix_len = len(dirname(extra_dir)) + 1
347 # Also add the corresponding build directory to pick up generated
349 env.Append(CPPPATH=Dir(joinpath(env['BUILDDIR'], extra_dir[prefix_len:])))
351 for root, dirs, files in os.walk(extra_dir, topdown=True):
352 # if build lives in the extras directory, don't walk down it
356 if 'SConscript' in files:
357 build_dir = joinpath(env['BUILDDIR'], root[prefix_len:])
358 SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
360 for opt in export_vars:
363 def makeTheISA(source, target, env):
364 isas = [ src.get_contents() for src in source ]
365 target_isa = env['TARGET_ISA']
367 return isa.upper() + '_ISA'
370 return isa[0].upper() + isa[1:].lower() + 'ISA'
373 code = code_formatter()
375 #ifndef __CONFIG_THE_ISA_HH__
376 #define __CONFIG_THE_ISA_HH__
380 for i,isa in enumerate(isas):
381 code('#define $0 $1', define(isa), i + 1)
385 #define THE_ISA ${{define(target_isa)}}
386 #define TheISA ${{namespace(target_isa)}}
387 #define THE_ISA_STR "${{target_isa}}"
389 #endif // __CONFIG_THE_ISA_HH__''')
391 code.write(str(target[0]))
393 env.Command('config/the_isa.hh', map(Value, all_isa_list),
394 MakeAction(makeTheISA, Transform("CFG ISA", 0)))
396 ########################################################################
398 # Prevent any SimObjects from being added after this point, they
399 # should all have been added in the SConscripts above
401 SimObject.fixed = True
403 class DictImporter(object):
404 '''This importer takes a dictionary of arbitrary module names that
405 map to arbitrary filenames.'''
406 def __init__(self, modules):
407 self.modules = modules
408 self.installed = set()
415 for module in self.installed:
416 del sys.modules[module]
417 self.installed = set()
419 def find_module(self, fullname, path):
420 if fullname == 'm5.defines':
423 if fullname == 'm5.objects':
426 if fullname.startswith('m5.internal'):
429 source = self.modules.get(fullname, None)
430 if source is not None and fullname.startswith('m5.objects'):
435 def load_module(self, fullname):
436 mod = imp.new_module(fullname)
437 sys.modules[fullname] = mod
438 self.installed.add(fullname)
440 mod.__loader__ = self
441 if fullname == 'm5.objects':
442 mod.__path__ = fullname.split('.')
445 if fullname == 'm5.defines':
446 mod.__dict__['buildEnv'] = m5.util.SmartDict(build_env)
449 source = self.modules[fullname]
450 if source.modname == '__init__':
451 mod.__path__ = source.modpath
452 mod.__file__ = source.abspath
454 exec file(source.abspath, 'r') in mod.__dict__
460 from m5.util import code_formatter
465 # install the python importer so we can grab stuff from the source
466 # tree itself. We can't have SimObjects added after this point or
467 # else we won't know about them for the rest of the stuff.
468 importer = DictImporter(PySource.modules)
469 sys.meta_path[0:0] = [ importer ]
471 # import all sim objects so we can populate the all_objects list
472 # make sure that we're working with a list, then let's sort it
473 for modname in SimObject.modnames:
474 exec('from m5.objects import %s' % modname)
476 # we need to unload all of the currently imported modules so that they
477 # will be re-imported the next time the sconscript is run
479 sys.meta_path.remove(importer)
481 sim_objects = m5.SimObject.allClasses
482 all_enums = m5.params.allEnums
484 if m5.SimObject.noCxxHeader:
485 print >> sys.stderr, \
486 "warning: At least one SimObject lacks a header specification. " \
487 "This can cause unexpected results in the generated SWIG " \
490 # Find param types that need to be explicitly wrapped with swig.
491 # These will be recognized because the ParamDesc will have a
492 # swig_decl() method. Most param types are based on types that don't
493 # need this, either because they're based on native types (like Int)
494 # or because they're SimObjects (which get swigged independently).
495 # For now the only things handled here are VectorParam types.
497 for name,obj in sorted(sim_objects.iteritems()):
498 for param in obj._params.local.values():
499 # load the ptype attribute now because it depends on the
500 # current version of SimObject.allClasses, but when scons
501 # actually uses the value, all versions of
502 # SimObject.allClasses will have been loaded
505 if not hasattr(param, 'swig_decl'):
507 pname = param.ptype_str
508 if pname not in params_to_swig:
509 params_to_swig[pname] = param
511 ########################################################################
513 # calculate extra dependencies
515 module_depends = ["m5", "m5.SimObject", "m5.params"]
516 depends = [ PySource.modules[dep].snode for dep in module_depends ]
518 ########################################################################
520 # Commands for the basic automatically generated python files
523 # Generate Python file containing a dict specifying the current
525 def makeDefinesPyFile(target, source, env):
526 build_env = source[0].get_contents()
528 code = code_formatter()
533 buildEnv = m5.util.SmartDict($build_env)
535 compileDate = m5.internal.core.compileDate
537 for key,val in m5.internal.core.__dict__.iteritems():
538 if key.startswith('flag_'):
543 code.write(target[0].abspath)
545 defines_info = Value(build_env)
546 # Generate a file with all of the compile options in it
547 env.Command('python/m5/defines.py', defines_info,
548 MakeAction(makeDefinesPyFile, Transform("DEFINES", 0)))
549 PySource('m5', 'python/m5/defines.py')
551 # Generate python file containing info about the M5 source code
552 def makeInfoPyFile(target, source, env):
553 code = code_formatter()
555 data = ''.join(file(src.srcnode().abspath, 'r').xreadlines())
556 code('$src = ${{repr(data)}}')
557 code.write(str(target[0]))
559 # Generate a file that wraps the basic top level files
560 env.Command('python/m5/info.py',
561 [ '#/COPYING', '#/LICENSE', '#/README', ],
562 MakeAction(makeInfoPyFile, Transform("INFO")))
563 PySource('m5', 'python/m5/info.py')
565 ########################################################################
567 # Create all of the SimObject param headers and enum headers
570 def createSimObjectParamStruct(target, source, env):
571 assert len(target) == 1 and len(source) == 1
573 name = str(source[0].get_contents())
574 obj = sim_objects[name]
576 code = code_formatter()
577 obj.cxx_param_decl(code)
578 code.write(target[0].abspath)
580 def createParamSwigWrapper(target, source, env):
581 assert len(target) == 1 and len(source) == 1
583 name = str(source[0].get_contents())
584 param = params_to_swig[name]
586 code = code_formatter()
587 param.swig_decl(code)
588 code.write(target[0].abspath)
590 def createEnumStrings(target, source, env):
591 assert len(target) == 1 and len(source) == 1
593 name = str(source[0].get_contents())
594 obj = all_enums[name]
596 code = code_formatter()
598 code.write(target[0].abspath)
600 def createEnumDecls(target, source, env):
601 assert len(target) == 1 and len(source) == 1
603 name = str(source[0].get_contents())
604 obj = all_enums[name]
606 code = code_formatter()
608 code.write(target[0].abspath)
610 def createEnumSwigWrapper(target, source, env):
611 assert len(target) == 1 and len(source) == 1
613 name = str(source[0].get_contents())
614 obj = all_enums[name]
616 code = code_formatter()
618 code.write(target[0].abspath)
620 def createSimObjectSwigWrapper(target, source, env):
621 name = source[0].get_contents()
622 obj = sim_objects[name]
624 code = code_formatter()
626 code.write(target[0].abspath)
628 # Generate all of the SimObject param C++ struct header files
630 for name,simobj in sorted(sim_objects.iteritems()):
631 py_source = PySource.modules[simobj.__module__]
632 extra_deps = [ py_source.tnode ]
634 hh_file = File('params/%s.hh' % name)
635 params_hh_files.append(hh_file)
636 env.Command(hh_file, Value(name),
637 MakeAction(createSimObjectParamStruct, Transform("SO PARAM")))
638 env.Depends(hh_file, depends + extra_deps)
640 # Generate any needed param SWIG wrapper files
642 for name,param in params_to_swig.iteritems():
643 i_file = File('python/m5/internal/%s.i' % (param.swig_module_name()))
644 params_i_files.append(i_file)
645 env.Command(i_file, Value(name),
646 MakeAction(createParamSwigWrapper, Transform("SW PARAM")))
647 env.Depends(i_file, depends)
648 SwigSource('m5.internal', i_file)
650 # Generate all enum header files
651 for name,enum in sorted(all_enums.iteritems()):
652 py_source = PySource.modules[enum.__module__]
653 extra_deps = [ py_source.tnode ]
655 cc_file = File('enums/%s.cc' % name)
656 env.Command(cc_file, Value(name),
657 MakeAction(createEnumStrings, Transform("ENUM STR")))
658 env.Depends(cc_file, depends + extra_deps)
661 hh_file = File('enums/%s.hh' % name)
662 env.Command(hh_file, Value(name),
663 MakeAction(createEnumDecls, Transform("ENUMDECL")))
664 env.Depends(hh_file, depends + extra_deps)
666 i_file = File('python/m5/internal/enum_%s.i' % name)
667 env.Command(i_file, Value(name),
668 MakeAction(createEnumSwigWrapper, Transform("ENUMSWIG")))
669 env.Depends(i_file, depends + extra_deps)
670 SwigSource('m5.internal', i_file)
672 # Generate SimObject SWIG wrapper files
673 for name,simobj in sim_objects.iteritems():
674 py_source = PySource.modules[simobj.__module__]
675 extra_deps = [ py_source.tnode ]
677 i_file = File('python/m5/internal/param_%s.i' % name)
678 env.Command(i_file, Value(name),
679 MakeAction(createSimObjectSwigWrapper, Transform("SO SWIG")))
680 env.Depends(i_file, depends + extra_deps)
681 SwigSource('m5.internal', i_file)
683 # Generate the main swig init file
684 def makeEmbeddedSwigInit(target, source, env):
685 code = code_formatter()
686 module = source[0].get_contents()
688 #include "sim/init.hh"
691 void init_${module}();
694 EmbeddedSwig embed_swig_${module}(init_${module});
696 code.write(str(target[0]))
698 # Build all swig modules
699 for swig in SwigSource.all:
700 env.Command([swig.cc_source.tnode, swig.py_source.tnode], swig.tnode,
701 MakeAction('$SWIG $SWIGFLAGS -outdir ${TARGETS[1].dir} '
702 '-o ${TARGETS[0]} $SOURCES', Transform("SWIG")))
703 cc_file = str(swig.tnode)
704 init_file = '%s/%s_init.cc' % (dirname(cc_file), basename(cc_file))
705 env.Command(init_file, Value(swig.module),
706 MakeAction(makeEmbeddedSwigInit, Transform("EMBED SW")))
707 Source(init_file, **swig.guards)
709 # Build all protocol buffers if we have got protoc and protobuf available
710 if env['HAVE_PROTOBUF']:
711 for proto in ProtoBuf.all:
712 # Use both the source and header as the target, and the .proto
713 # file as the source. When executing the protoc compiler, also
714 # specify the proto_path to avoid having the generated files
716 env.Command([proto.cc_file, proto.hh_file], proto.tnode,
717 MakeAction('$PROTOC --cpp_out ${TARGET.dir} '
718 '--proto_path ${SOURCE.dir} $SOURCE',
719 Transform("PROTOC")))
721 # Add the C++ source file
722 Source(proto.cc_file, **proto.guards)
724 print 'Got protobuf to build, but lacks support!'
730 def makeDebugFlagCC(target, source, env):
731 assert(len(target) == 1 and len(source) == 1)
733 val = eval(source[0].get_contents())
734 name, compound, desc = val
735 compound = list(sorted(compound))
737 code = code_formatter()
742 * DO NOT EDIT THIS FILE! Automatically generated
745 #include "base/debug.hh"
748 for flag in compound:
749 code('#include "debug/$flag.hh"')
751 code('namespace Debug {')
755 code('SimpleFlag $name("$name", "$desc");')
757 code('CompoundFlag $name("$name", "$desc",')
759 last = len(compound) - 1
760 for i,flag in enumerate(compound):
768 code('} // namespace Debug')
770 code.write(str(target[0]))
772 def makeDebugFlagHH(target, source, env):
773 assert(len(target) == 1 and len(source) == 1)
775 val = eval(source[0].get_contents())
776 name, compound, desc = val
778 code = code_formatter()
780 # file header boilerplate
783 * DO NOT EDIT THIS FILE!
785 * Automatically generated by SCons
788 #ifndef __DEBUG_${name}_HH__
789 #define __DEBUG_${name}_HH__
795 code('class CompoundFlag;')
796 code('class SimpleFlag;')
799 code('extern CompoundFlag $name;')
800 for flag in compound:
801 code('extern SimpleFlag $flag;')
803 code('extern SimpleFlag $name;')
808 #endif // __DEBUG_${name}_HH__
811 code.write(str(target[0]))
813 for name,flag in sorted(debug_flags.iteritems()):
814 n, compound, desc = flag
817 env.Command('debug/%s.hh' % name, Value(flag),
818 MakeAction(makeDebugFlagHH, Transform("TRACING", 0)))
819 env.Command('debug/%s.cc' % name, Value(flag),
820 MakeAction(makeDebugFlagCC, Transform("TRACING", 0)))
821 Source('debug/%s.cc' % name)
823 # Embed python files. All .py files that have been indicated by a
824 # PySource() call in a SConscript need to be embedded into the M5
825 # library. To do that, we compile the file to byte code, marshal the
826 # byte code, compress it, and then generate a c++ file that
827 # inserts the result into an array.
828 def embedPyFile(target, source, env):
832 return '"%s"' % string
834 '''Action function to compile a .py into a code object, marshal
835 it, compress it, and stick it into an asm file so the code appears
836 as just bytes with a label in the data section'''
838 src = file(str(source[0]), 'r').read()
840 pysource = PySource.tnodes[source[0]]
841 compiled = compile(src, pysource.abspath, 'exec')
842 marshalled = marshal.dumps(compiled)
843 compressed = zlib.compress(marshalled)
845 sym = pysource.symname
847 code = code_formatter()
849 #include "sim/init.hh"
853 const uint8_t data_${sym}[] = {
857 for i in xrange(0, len(data), step):
858 x = array.array('B', data[i:i+step])
859 code(''.join('%d,' % d for d in x))
864 EmbeddedPython embedded_${sym}(
865 ${{c_str(pysource.arcname)}},
866 ${{c_str(pysource.abspath)}},
867 ${{c_str(pysource.modpath)}},
870 ${{len(marshalled)}});
872 } // anonymous namespace
874 code.write(str(target[0]))
876 for source in PySource.all:
877 env.Command(source.cpp, source.tnode,
878 MakeAction(embedPyFile, Transform("EMBED PY")))
881 ########################################################################
883 # Define binaries. Each different build type (debug, opt, etc.) gets
884 # a slightly different build environment.
887 # List of constructed environments to pass back to SConstruct
888 date_source = Source('base/date.cc', skip_lib=True)
890 # Capture this directory for the closure makeEnv, otherwise when it is
891 # called, it won't know what directory it should use.
892 variant_dir = Dir('.').path
894 return os.path.join(variant_dir, *path)
896 return variant(*path)+'/'
898 # Function to create a new build environment as clone of current
899 # environment 'env' with modified object suffix and optional stripped
900 # binary. Additional keyword arguments are appended to corresponding
901 # build environment vars.
902 def makeEnv(env, label, objsfx, strip = False, **kwargs):
903 # SCons doesn't know to append a library suffix when there is a '.' in the
904 # name. Use '_' instead.
905 libname = variant('gem5_' + label)
906 exename = variant('gem5.' + label)
907 secondary_exename = variant('m5.' + label)
909 new_env = env.Clone(OBJSUFFIX=objsfx, SHOBJSUFFIX=objsfx + 's')
910 new_env.Label = label
911 new_env.Append(**kwargs)
913 swig_env = new_env.Clone()
915 # Both gcc and clang have issues with unused labels and values in
916 # the SWIG generated code
917 swig_env.Append(CCFLAGS=['-Wno-unused-label', '-Wno-unused-value'])
919 # Add additional warnings here that should not be applied to
920 # the SWIG generated code
921 new_env.Append(CXXFLAGS='-Wmissing-declarations')
924 # Depending on the SWIG version, we also need to supress
925 # warnings about uninitialized variables and missing field
927 swig_env.Append(CCFLAGS=['-Wno-uninitialized',
928 '-Wno-missing-field-initializers'])
930 if compareVersions(env['GCC_VERSION'], '4.6') >= 0:
931 swig_env.Append(CCFLAGS='-Wno-unused-but-set-variable')
933 # If gcc supports it, also warn for deletion of derived
934 # classes with non-virtual desctructors. For gcc >= 4.7 we
935 # also have to disable warnings about the SWIG code having
936 # potentially uninitialized variables.
937 if compareVersions(env['GCC_VERSION'], '4.7') >= 0:
938 new_env.Append(CXXFLAGS='-Wdelete-non-virtual-dtor')
939 swig_env.Append(CCFLAGS='-Wno-maybe-uninitialized')
941 # Always enable the warning for deletion of derived classes
942 # with non-virtual destructors
943 new_env.Append(CXXFLAGS=['-Wdelete-non-virtual-dtor'])
945 werror_env = new_env.Clone()
946 werror_env.Append(CCFLAGS='-Werror')
948 def make_obj(source, static, extra_deps = None):
949 '''This function adds the specified source to the correct
950 build environment, and returns the corresponding SCons Object
961 obj = env.StaticObject(source.tnode)
963 obj = env.SharedObject(source.tnode)
966 env.Depends(obj, extra_deps)
971 [ make_obj(s, True) for s in Source.get(main=False, skip_lib=False) ]
973 [ make_obj(s, False) for s in Source.get(main=False, skip_lib=False) ]
975 static_date = make_obj(date_source, static=True, extra_deps=static_objs)
976 static_objs.append(static_date)
978 shared_date = make_obj(date_source, static=False, extra_deps=shared_objs)
979 shared_objs.append(shared_date)
981 # First make a library of everything but main() so other programs can
983 static_lib = new_env.StaticLibrary(libname, static_objs)
984 shared_lib = new_env.SharedLibrary(libname, shared_objs)
986 # Now link a stub with main() and the static library.
987 main_objs = [ make_obj(s, True) for s in Source.get(main=True) ]
989 for test in UnitTest.all:
990 flags = { test.target : True }
991 test_sources = Source.get(**flags)
992 test_objs = [ make_obj(s, static=True) for s in test_sources ]
994 test_objs += main_objs
995 path = variant('unittest/%s.%s' % (test.target, label))
996 new_env.Program(path, test_objs + static_objs)
1000 progname += '.unstripped'
1002 targets = new_env.Program(progname, main_objs + static_objs)
1005 if sys.platform == 'sunos5':
1006 cmd = 'cp $SOURCE $TARGET; strip $TARGET'
1008 cmd = 'strip $SOURCE -o $TARGET'
1009 targets = new_env.Command(exename, progname,
1010 MakeAction(cmd, Transform("STRIP")))
1012 new_env.Command(secondary_exename, exename,
1013 MakeAction('ln $SOURCE $TARGET', Transform("HARDLINK")))
1015 new_env.M5Binary = targets[0]
1018 # Start out with the compiler flags common to all compilers,
1019 # i.e. they all use -g for opt and -g -pg for prof
1020 ccflags = {'debug' : [], 'opt' : ['-g'], 'fast' : [], 'prof' : ['-g', '-pg'],
1023 # Start out with the linker flags common to all linkers, i.e. -pg for
1024 # prof, and -lprofiler for perf. The -lprofile flag is surrounded by
1025 # no-as-needed and as-needed as the binutils linker is too clever and
1026 # simply doesn't link to the library otherwise.
1027 ldflags = {'debug' : [], 'opt' : [], 'fast' : [], 'prof' : ['-pg'],
1028 'perf' : ['-Wl,--no-as-needed', '-lprofiler', '-Wl,--as-needed']}
1030 # For Link Time Optimization, the optimisation flags used to compile
1031 # individual files are decoupled from those used at link time
1032 # (i.e. you can compile with -O3 and perform LTO with -O0), so we need
1033 # to also update the linker flags based on the target.
1035 if sys.platform == 'sunos5':
1036 ccflags['debug'] += ['-gstabs+']
1038 ccflags['debug'] += ['-ggdb3']
1039 ldflags['debug'] += ['-O0']
1040 # opt, fast, prof and perf all share the same cc flags, also add
1041 # the optimization to the ldflags as LTO defers the optimization
1043 for target in ['opt', 'fast', 'prof', 'perf']:
1044 ccflags[target] += ['-O3']
1045 ldflags[target] += ['-O3']
1047 ccflags['fast'] += env['LTO_CCFLAGS']
1048 ldflags['fast'] += env['LTO_LDFLAGS']
1050 ccflags['debug'] += ['-g', '-O0']
1051 # opt, fast, prof and perf all share the same cc flags
1052 for target in ['opt', 'fast', 'prof', 'perf']:
1053 ccflags[target] += ['-O3']
1055 print 'Unknown compiler, please fix compiler options'
1059 # To speed things up, we only instantiate the build environments we
1060 # need. We try to identify the needed environment for each target; if
1061 # we can't, we fall back on instantiating all the environments just to
1063 target_types = ['debug', 'opt', 'fast', 'prof', 'perf']
1064 obj2target = {'do': 'debug', 'o': 'opt', 'fo': 'fast', 'po': 'prof',
1067 def identifyTarget(t):
1068 ext = t.split('.')[-1]
1069 if ext in target_types:
1071 if obj2target.has_key(ext):
1072 return obj2target[ext]
1073 match = re.search(r'/tests/([^/]+)/', t)
1074 if match and match.group(1) in target_types:
1075 return match.group(1)
1078 needed_envs = [identifyTarget(target) for target in BUILD_TARGETS]
1079 if 'all' in needed_envs:
1080 needed_envs += target_types
1082 gem5_root = Dir('.').up().up().abspath
1083 def makeEnvirons(target, source, env):
1084 # cause any later Source() calls to be fatal, as a diagnostic.
1090 if 'debug' in needed_envs:
1092 makeEnv(env, 'debug', '.do',
1093 CCFLAGS = Split(ccflags['debug']),
1094 CPPDEFINES = ['DEBUG', 'TRACING_ON=1'],
1095 LINKFLAGS = Split(ldflags['debug'])))
1098 if 'opt' in needed_envs:
1100 makeEnv(env, 'opt', '.o',
1101 CCFLAGS = Split(ccflags['opt']),
1102 CPPDEFINES = ['TRACING_ON=1'],
1103 LINKFLAGS = Split(ldflags['opt'])))
1106 if 'fast' in needed_envs:
1108 makeEnv(env, 'fast', '.fo', strip = True,
1109 CCFLAGS = Split(ccflags['fast']),
1110 CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1111 LINKFLAGS = Split(ldflags['fast'])))
1113 # Profiled binary using gprof
1114 if 'prof' in needed_envs:
1116 makeEnv(env, 'prof', '.po',
1117 CCFLAGS = Split(ccflags['prof']),
1118 CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1119 LINKFLAGS = Split(ldflags['prof'])))
1121 # Profiled binary using google-pprof
1122 if 'perf' in needed_envs:
1124 makeEnv(env, 'perf', '.gpo',
1125 CCFLAGS = Split(ccflags['perf']),
1126 CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1127 LINKFLAGS = Split(ldflags['perf'])))
1129 # Set up the regression tests for each build.
1131 SConscript(os.path.join(gem5_root, 'tests', 'SConscript'),
1132 variant_dir = variantd('tests', e.Label),
1133 exports = { 'env' : e }, duplicate = False)
1135 # The MakeEnvirons Builder defers the full dependency collection until
1136 # after processing the ISA definition (due to dynamically generated
1137 # source files). Add this dependency to all targets so they will wait
1138 # until the environments are completely set up. Otherwise, a second
1139 # process (e.g. -j2 or higher) will try to compile the requested target,
1140 # not know how, and fail.
1141 env.Append(BUILDERS = {'MakeEnvirons' :
1142 Builder(action=MakeAction(makeEnvirons,
1143 Transform("ENVIRONS", 1)))})
1145 isa_target = env['PHONY_BASE'] + '-deps'
1146 environs = env['PHONY_BASE'] + '-environs'
1147 env.Depends('#all-deps', isa_target)
1148 env.Depends('#all-environs', environs)
1149 env.ScanISA(isa_target, File('arch/%s/generated/inc.d' % env['TARGET_ISA']))
1150 envSetup = env.MakeEnvirons(environs, isa_target)
1152 # make sure no -deps targets occur before all ISAs are complete
1153 env.Depends(isa_target, '#all-isas')
1154 # likewise for -environs targets and all the -deps targets
1155 env.Depends(environs, '#all-deps')