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
41 from os.path import basename, dirname, exists, isdir, isfile, join as joinpath
45 from gem5_scons import Transform
47 # This file defines how to build a particular configuration of gem5
48 # based on variable settings in the 'env' build environment.
52 # Children need to see the environment
55 build_env = [(opt, env[opt]) for opt in export_vars]
57 from m5.util import code_formatter, compareVersions
59 ########################################################################
60 # Code for adding source files of various types
62 # When specifying a source file of some type, a set of guards can be
63 # specified for that file. When get() is used to find the files, if
64 # get specifies a set of filters, only files that match those filters
65 # will be accepted (unspecified filters on files are assumed to be
66 # false). Current filters are:
67 # main -- specifies the gem5 main() function
68 # skip_lib -- do not put this file into the gem5 library
69 # skip_no_python -- do not put this file into a no_python library
70 # as it embeds compiled Python
71 # <unittest> -- unit tests use filters based on the unit test name
73 # A parent can now be specified for a source file and default filter
74 # values will be retrieved recursively from parents (children override
77 def guarded_source_iterator(sources, **guards):
78 '''Iterate over a set of sources, gated by a set of guards.'''
80 for flag,value in guards.iteritems():
81 # if the flag is found and has a different value, skip
83 if src.all_guards.get(flag, False) != value:
88 class SourceMeta(type):
89 '''Meta class for source files that keeps track of all files of a
90 particular type and has a get function for finding all functions
91 of a certain type that match a set of guards'''
92 def __init__(cls, name, bases, dict):
93 super(SourceMeta, cls).__init__(name, bases, dict)
96 def get(cls, **guards):
97 '''Find all files that match the specified guards. If a source
98 file does not specify a flag, the default is False'''
99 for s in guarded_source_iterator(cls.all, **guards):
102 class SourceFile(object):
103 '''Base object that encapsulates the notion of a source file.
104 This includes, the source node, target node, various manipulations
105 of those. A source file also specifies a set of guards which
106 describing which builds the source file applies to. A parent can
107 also be specified to get default guards from'''
108 __metaclass__ = SourceMeta
109 def __init__(self, source, parent=None, **guards):
114 if not isinstance(source, SCons.Node.FS.File):
118 self.snode = tnode.srcnode()
120 for base in type(self).__mro__:
121 if issubclass(base, SourceFile):
122 base.all.append(self)
126 return str(self.tnode)
130 return dirname(self.filename)
134 return basename(self.filename)
138 index = self.basename.rfind('.')
140 # dot files aren't extensions
141 return self.basename, None
143 return self.basename[:index], self.basename[index+1:]
146 def all_guards(self):
147 '''find all guards for this object getting default values
148 recursively from its parents'''
151 guards.update(self.parent.guards)
152 guards.update(self.guards)
155 def __lt__(self, other): return self.filename < other.filename
156 def __le__(self, other): return self.filename <= other.filename
157 def __gt__(self, other): return self.filename > other.filename
158 def __ge__(self, other): return self.filename >= other.filename
159 def __eq__(self, other): return self.filename == other.filename
160 def __ne__(self, other): return self.filename != other.filename
164 def disabled(cls, name, *ignored):
165 raise RuntimeError("Additional SourceFile '%s'" % name,\
166 "declared, but targets deps are already fixed.")
167 SourceFile.__init__ = disabled
170 class Source(SourceFile):
172 source_groups = { None : [] }
175 def set_group(cls, group):
176 if not group in Source.source_groups:
177 Source.source_groups[group] = []
178 Source.current_group = group
180 '''Add a c/c++ source file to the build'''
181 def __init__(self, source, Werror=True, **guards):
182 '''specify the source file, and any guards'''
183 super(Source, self).__init__(source, **guards)
187 Source.source_groups[Source.current_group].append(self)
189 class PySource(SourceFile):
190 '''Add a python source file to the named package'''
191 invalid_sym_char = re.compile('[^A-z0-9_]')
196 def __init__(self, package, source, **guards):
197 '''specify the python package, the source file, and any guards'''
198 super(PySource, self).__init__(source, **guards)
200 modname,ext = self.extname
204 path = package.split('.')
209 if modname != '__init__':
210 modpath += [ modname ]
211 modpath = '.'.join(modpath)
213 arcpath = path + [ self.basename ]
214 abspath = self.snode.abspath
215 if not exists(abspath):
216 abspath = self.tnode.abspath
218 self.package = package
219 self.modname = modname
220 self.modpath = modpath
221 self.arcname = joinpath(*arcpath)
222 self.abspath = abspath
223 self.compiled = File(self.filename + 'c')
224 self.cpp = File(self.filename + '.cc')
225 self.symname = PySource.invalid_sym_char.sub('_', modpath)
227 PySource.modules[modpath] = self
228 PySource.tnodes[self.tnode] = self
229 PySource.symnames[self.symname] = self
231 class SimObject(PySource):
232 '''Add a SimObject python file as a python source object and add
233 it to a list of sim object modules'''
238 def __init__(self, source, **guards):
239 '''Specify the source file and any guards (automatically in
240 the m5.objects package)'''
241 super(SimObject, self).__init__('m5.objects', source, **guards)
243 raise AttributeError, "Too late to call SimObject now."
245 bisect.insort_right(SimObject.modnames, self.modname)
247 class ProtoBuf(SourceFile):
248 '''Add a Protocol Buffer to build'''
250 def __init__(self, source, **guards):
251 '''Specify the source file, and any guards'''
252 super(ProtoBuf, self).__init__(source, **guards)
254 # Get the file name and the extension
255 modname,ext = self.extname
256 assert ext == 'proto'
258 # Currently, we stick to generating the C++ headers, so we
259 # only need to track the source and header.
260 self.cc_file = File(modname + '.pb.cc')
261 self.hh_file = File(modname + '.pb.h')
263 class UnitTest(object):
264 '''Create a UnitTest'''
267 def __init__(self, target, *sources, **kwargs):
268 '''Specify the target name and any sources. Sources that are
269 not SourceFiles are evalued with Source(). All files are
270 guarded with a guard of the same name as the UnitTest
275 if not isinstance(src, SourceFile):
276 src = Source(src, skip_lib=True)
277 src.guards[target] = True
282 self.main = kwargs.get('main', False)
283 UnitTest.all.append(self)
285 # Children should have access
292 ########################################################################
297 def DebugFlag(name, desc=None):
298 if name in debug_flags:
299 raise AttributeError, "Flag %s already specified" % name
300 debug_flags[name] = (name, (), desc)
302 def CompoundFlag(name, flags, desc=None):
303 if name in debug_flags:
304 raise AttributeError, "Flag %s already specified" % name
306 compound = tuple(flags)
307 debug_flags[name] = (name, compound, desc)
310 Export('CompoundFlag')
312 ########################################################################
314 # Set some compiler variables
317 # Include file paths are rooted in this directory. SCons will
318 # automatically expand '.' to refer to both the source directory and
319 # the corresponding build directory to pick up generated include
321 env.Append(CPPPATH=Dir('.'))
323 for extra_dir in extras_dir_list:
324 env.Append(CPPPATH=Dir(extra_dir))
326 # Workaround for bug in SCons version > 0.97d20071212
327 # Scons bug id: 2006 gem5 Bug id: 308
328 for root, dirs, files in os.walk(base_dir, topdown=True):
329 Dir(root[len(base_dir) + 1:])
331 ########################################################################
333 # Walk the tree and execute all SConscripts in subdirectories
336 here = Dir('.').srcnode().abspath
337 for root, dirs, files in os.walk(base_dir, topdown=True):
339 # we don't want to recurse back into this SConscript
342 if 'SConscript' in files:
343 build_dir = joinpath(env['BUILDDIR'], root[len(base_dir) + 1:])
344 Source.set_group(build_dir)
345 SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
347 for extra_dir in extras_dir_list:
348 prefix_len = len(dirname(extra_dir)) + 1
350 # Also add the corresponding build directory to pick up generated
352 env.Append(CPPPATH=Dir(joinpath(env['BUILDDIR'], extra_dir[prefix_len:])))
354 for root, dirs, files in os.walk(extra_dir, topdown=True):
355 # if build lives in the extras directory, don't walk down it
359 if 'SConscript' in files:
360 build_dir = joinpath(env['BUILDDIR'], root[prefix_len:])
361 SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
363 for opt in export_vars:
366 def makeTheISA(source, target, env):
367 isas = [ src.get_contents() for src in source ]
368 target_isa = env['TARGET_ISA']
370 return isa.upper() + '_ISA'
373 return isa[0].upper() + isa[1:].lower() + 'ISA'
376 code = code_formatter()
378 #ifndef __CONFIG_THE_ISA_HH__
379 #define __CONFIG_THE_ISA_HH__
383 # create defines for the preprocessing and compile-time determination
384 for i,isa in enumerate(isas):
385 code('#define $0 $1', define(isa), i + 1)
388 # create an enum for any run-time determination of the ISA, we
389 # reuse the same name as the namespaces
390 code('enum class Arch {')
391 for i,isa in enumerate(isas):
392 if i + 1 == len(isas):
393 code(' $0 = $1', namespace(isa), define(isa))
395 code(' $0 = $1,', namespace(isa), define(isa))
400 #define THE_ISA ${{define(target_isa)}}
401 #define TheISA ${{namespace(target_isa)}}
402 #define THE_ISA_STR "${{target_isa}}"
404 #endif // __CONFIG_THE_ISA_HH__''')
406 code.write(str(target[0]))
408 env.Command('config/the_isa.hh', map(Value, all_isa_list),
409 MakeAction(makeTheISA, Transform("CFG ISA", 0)))
411 def makeTheGPUISA(source, target, env):
412 isas = [ src.get_contents() for src in source ]
413 target_gpu_isa = env['TARGET_GPU_ISA']
415 return isa.upper() + '_ISA'
418 return isa[0].upper() + isa[1:].lower() + 'ISA'
421 code = code_formatter()
423 #ifndef __CONFIG_THE_GPU_ISA_HH__
424 #define __CONFIG_THE_GPU_ISA_HH__
428 # create defines for the preprocessing and compile-time determination
429 for i,isa in enumerate(isas):
430 code('#define $0 $1', define(isa), i + 1)
433 # create an enum for any run-time determination of the ISA, we
434 # reuse the same name as the namespaces
435 code('enum class GPUArch {')
436 for i,isa in enumerate(isas):
437 if i + 1 == len(isas):
438 code(' $0 = $1', namespace(isa), define(isa))
440 code(' $0 = $1,', namespace(isa), define(isa))
445 #define THE_GPU_ISA ${{define(target_gpu_isa)}}
446 #define TheGpuISA ${{namespace(target_gpu_isa)}}
447 #define THE_GPU_ISA_STR "${{target_gpu_isa}}"
449 #endif // __CONFIG_THE_GPU_ISA_HH__''')
451 code.write(str(target[0]))
453 env.Command('config/the_gpu_isa.hh', map(Value, all_gpu_isa_list),
454 MakeAction(makeTheGPUISA, Transform("CFG ISA", 0)))
456 ########################################################################
458 # Prevent any SimObjects from being added after this point, they
459 # should all have been added in the SConscripts above
461 SimObject.fixed = True
463 class DictImporter(object):
464 '''This importer takes a dictionary of arbitrary module names that
465 map to arbitrary filenames.'''
466 def __init__(self, modules):
467 self.modules = modules
468 self.installed = set()
475 for module in self.installed:
476 del sys.modules[module]
477 self.installed = set()
479 def find_module(self, fullname, path):
480 if fullname == 'm5.defines':
483 if fullname == 'm5.objects':
486 if fullname.startswith('_m5'):
489 source = self.modules.get(fullname, None)
490 if source is not None and fullname.startswith('m5.objects'):
495 def load_module(self, fullname):
496 mod = imp.new_module(fullname)
497 sys.modules[fullname] = mod
498 self.installed.add(fullname)
500 mod.__loader__ = self
501 if fullname == 'm5.objects':
502 mod.__path__ = fullname.split('.')
505 if fullname == 'm5.defines':
506 mod.__dict__['buildEnv'] = m5.util.SmartDict(build_env)
509 source = self.modules[fullname]
510 if source.modname == '__init__':
511 mod.__path__ = source.modpath
512 mod.__file__ = source.abspath
514 exec file(source.abspath, 'r') in mod.__dict__
520 from m5.util import code_formatter
525 # install the python importer so we can grab stuff from the source
526 # tree itself. We can't have SimObjects added after this point or
527 # else we won't know about them for the rest of the stuff.
528 importer = DictImporter(PySource.modules)
529 sys.meta_path[0:0] = [ importer ]
531 # import all sim objects so we can populate the all_objects list
532 # make sure that we're working with a list, then let's sort it
533 for modname in SimObject.modnames:
534 exec('from m5.objects import %s' % modname)
536 # we need to unload all of the currently imported modules so that they
537 # will be re-imported the next time the sconscript is run
539 sys.meta_path.remove(importer)
541 sim_objects = m5.SimObject.allClasses
542 all_enums = m5.params.allEnums
544 for name,obj in sorted(sim_objects.iteritems()):
545 for param in obj._params.local.values():
546 # load the ptype attribute now because it depends on the
547 # current version of SimObject.allClasses, but when scons
548 # actually uses the value, all versions of
549 # SimObject.allClasses will have been loaded
552 ########################################################################
554 # calculate extra dependencies
556 module_depends = ["m5", "m5.SimObject", "m5.params"]
557 depends = [ PySource.modules[dep].snode for dep in module_depends ]
558 depends.sort(key = lambda x: x.name)
560 ########################################################################
562 # Commands for the basic automatically generated python files
565 # Generate Python file containing a dict specifying the current
567 def makeDefinesPyFile(target, source, env):
568 build_env = source[0].get_contents()
570 code = code_formatter()
575 buildEnv = m5.util.SmartDict($build_env)
577 compileDate = _m5.core.compileDate
579 for key,val in _m5.core.__dict__.iteritems():
580 if key.startswith('flag_'):
585 code.write(target[0].abspath)
587 defines_info = Value(build_env)
588 # Generate a file with all of the compile options in it
589 env.Command('python/m5/defines.py', defines_info,
590 MakeAction(makeDefinesPyFile, Transform("DEFINES", 0)))
591 PySource('m5', 'python/m5/defines.py')
593 # Generate python file containing info about the M5 source code
594 def makeInfoPyFile(target, source, env):
595 code = code_formatter()
597 data = ''.join(file(src.srcnode().abspath, 'r').xreadlines())
598 code('$src = ${{repr(data)}}')
599 code.write(str(target[0]))
601 # Generate a file that wraps the basic top level files
602 env.Command('python/m5/info.py',
603 [ '#/COPYING', '#/LICENSE', '#/README', ],
604 MakeAction(makeInfoPyFile, Transform("INFO")))
605 PySource('m5', 'python/m5/info.py')
607 ########################################################################
609 # Create all of the SimObject param headers and enum headers
612 def createSimObjectParamStruct(target, source, env):
613 assert len(target) == 1 and len(source) == 1
615 name = source[0].get_text_contents()
616 obj = sim_objects[name]
618 code = code_formatter()
619 obj.cxx_param_decl(code)
620 code.write(target[0].abspath)
622 def createSimObjectCxxConfig(is_header):
623 def body(target, source, env):
624 assert len(target) == 1 and len(source) == 1
626 name = str(source[0].get_contents())
627 obj = sim_objects[name]
629 code = code_formatter()
630 obj.cxx_config_param_file(code, is_header)
631 code.write(target[0].abspath)
634 def createEnumStrings(target, source, env):
635 assert len(target) == 1 and len(source) == 2
637 name = source[0].get_text_contents()
638 use_python = source[1].read()
639 obj = all_enums[name]
641 code = code_formatter()
645 code.write(target[0].abspath)
647 def createEnumDecls(target, source, env):
648 assert len(target) == 1 and len(source) == 1
650 name = source[0].get_text_contents()
651 obj = all_enums[name]
653 code = code_formatter()
655 code.write(target[0].abspath)
657 def createSimObjectPyBindWrapper(target, source, env):
658 name = source[0].get_text_contents()
659 obj = sim_objects[name]
661 code = code_formatter()
662 obj.pybind_decl(code)
663 code.write(target[0].abspath)
665 # Generate all of the SimObject param C++ struct header files
667 for name,simobj in sorted(sim_objects.iteritems()):
668 py_source = PySource.modules[simobj.__module__]
669 extra_deps = [ py_source.tnode ]
671 hh_file = File('params/%s.hh' % name)
672 params_hh_files.append(hh_file)
673 env.Command(hh_file, Value(name),
674 MakeAction(createSimObjectParamStruct, Transform("SO PARAM")))
675 env.Depends(hh_file, depends + extra_deps)
677 # C++ parameter description files
678 if GetOption('with_cxx_config'):
679 for name,simobj in sorted(sim_objects.iteritems()):
680 py_source = PySource.modules[simobj.__module__]
681 extra_deps = [ py_source.tnode ]
683 cxx_config_hh_file = File('cxx_config/%s.hh' % name)
684 cxx_config_cc_file = File('cxx_config/%s.cc' % name)
685 env.Command(cxx_config_hh_file, Value(name),
686 MakeAction(createSimObjectCxxConfig(True),
687 Transform("CXXCPRHH")))
688 env.Command(cxx_config_cc_file, Value(name),
689 MakeAction(createSimObjectCxxConfig(False),
690 Transform("CXXCPRCC")))
691 env.Depends(cxx_config_hh_file, depends + extra_deps +
692 [File('params/%s.hh' % name), File('sim/cxx_config.hh')])
693 env.Depends(cxx_config_cc_file, depends + extra_deps +
694 [cxx_config_hh_file])
695 Source(cxx_config_cc_file)
697 cxx_config_init_cc_file = File('cxx_config/init.cc')
699 def createCxxConfigInitCC(target, source, env):
700 assert len(target) == 1 and len(source) == 1
702 code = code_formatter()
704 for name,simobj in sorted(sim_objects.iteritems()):
705 if not hasattr(simobj, 'abstract') or not simobj.abstract:
706 code('#include "cxx_config/${name}.hh"')
708 code('void cxxConfigInit()')
711 for name,simobj in sorted(sim_objects.iteritems()):
712 not_abstract = not hasattr(simobj, 'abstract') or \
714 if not_abstract and 'type' in simobj.__dict__:
715 code('cxx_config_directory["${name}"] = '
716 '${name}CxxConfigParams::makeDirectoryEntry();')
719 code.write(target[0].abspath)
721 py_source = PySource.modules[simobj.__module__]
722 extra_deps = [ py_source.tnode ]
723 env.Command(cxx_config_init_cc_file, Value(name),
724 MakeAction(createCxxConfigInitCC, Transform("CXXCINIT")))
725 cxx_param_hh_files = ["cxx_config/%s.hh" % simobj
726 for name,simobj in sorted(sim_objects.iteritems())
727 if not hasattr(simobj, 'abstract') or not simobj.abstract]
728 Depends(cxx_config_init_cc_file, cxx_param_hh_files +
729 [File('sim/cxx_config.hh')])
730 Source(cxx_config_init_cc_file)
732 # Generate all enum header files
733 for name,enum in sorted(all_enums.iteritems()):
734 py_source = PySource.modules[enum.__module__]
735 extra_deps = [ py_source.tnode ]
737 cc_file = File('enums/%s.cc' % name)
738 env.Command(cc_file, [Value(name), Value(env['USE_PYTHON'])],
739 MakeAction(createEnumStrings, Transform("ENUM STR")))
740 env.Depends(cc_file, depends + extra_deps)
743 hh_file = File('enums/%s.hh' % name)
744 env.Command(hh_file, Value(name),
745 MakeAction(createEnumDecls, Transform("ENUMDECL")))
746 env.Depends(hh_file, depends + extra_deps)
748 # Generate SimObject Python bindings wrapper files
749 if env['USE_PYTHON']:
750 for name,simobj in sorted(sim_objects.iteritems()):
751 py_source = PySource.modules[simobj.__module__]
752 extra_deps = [ py_source.tnode ]
753 cc_file = File('python/_m5/param_%s.cc' % name)
754 env.Command(cc_file, Value(name),
755 MakeAction(createSimObjectPyBindWrapper,
756 Transform("SO PyBind")))
757 env.Depends(cc_file, depends + extra_deps)
760 # Build all protocol buffers if we have got protoc and protobuf available
761 if env['HAVE_PROTOBUF']:
762 for proto in ProtoBuf.all:
763 # Use both the source and header as the target, and the .proto
764 # file as the source. When executing the protoc compiler, also
765 # specify the proto_path to avoid having the generated files
767 env.Command([proto.cc_file, proto.hh_file], proto.tnode,
768 MakeAction('$PROTOC --cpp_out ${TARGET.dir} '
769 '--proto_path ${SOURCE.dir} $SOURCE',
770 Transform("PROTOC")))
772 # Add the C++ source file
773 Source(proto.cc_file, **proto.guards)
775 print 'Got protobuf to build, but lacks support!'
781 def makeDebugFlagCC(target, source, env):
782 assert(len(target) == 1 and len(source) == 1)
784 code = code_formatter()
786 # delay definition of CompoundFlags until after all the definition
787 # of all constituent SimpleFlags
788 comp_code = code_formatter()
793 * DO NOT EDIT THIS FILE! Automatically generated by SCons.
796 #include "base/debug.hh"
802 for name, flag in sorted(source[0].read().iteritems()):
803 n, compound, desc = flag
807 code('SimpleFlag $name("$name", "$desc");')
809 comp_code('CompoundFlag $name("$name", "$desc",')
811 last = len(compound) - 1
812 for i,flag in enumerate(compound):
816 comp_code('&$flag);')
819 code.append(comp_code)
821 code('} // namespace Debug')
823 code.write(str(target[0]))
825 def makeDebugFlagHH(target, source, env):
826 assert(len(target) == 1 and len(source) == 1)
828 val = eval(source[0].get_contents())
829 name, compound, desc = val
831 code = code_formatter()
833 # file header boilerplate
836 * DO NOT EDIT THIS FILE! Automatically generated by SCons.
839 #ifndef __DEBUG_${name}_HH__
840 #define __DEBUG_${name}_HH__
846 code('class CompoundFlag;')
847 code('class SimpleFlag;')
850 code('extern CompoundFlag $name;')
851 for flag in compound:
852 code('extern SimpleFlag $flag;')
854 code('extern SimpleFlag $name;')
859 #endif // __DEBUG_${name}_HH__
862 code.write(str(target[0]))
864 for name,flag in sorted(debug_flags.iteritems()):
865 n, compound, desc = flag
868 hh_file = 'debug/%s.hh' % name
869 env.Command(hh_file, Value(flag),
870 MakeAction(makeDebugFlagHH, Transform("TRACING", 0)))
872 env.Command('debug/flags.cc', Value(debug_flags),
873 MakeAction(makeDebugFlagCC, Transform("TRACING", 0)))
874 Source('debug/flags.cc')
878 env.Command('sim/tags.cc', None,
879 MakeAction('util/cpt_upgrader.py --get-cc-file > $TARGET',
880 Transform("VER TAGS")))
881 env.AlwaysBuild(tags)
883 # Embed python files. All .py files that have been indicated by a
884 # PySource() call in a SConscript need to be embedded into the M5
885 # library. To do that, we compile the file to byte code, marshal the
886 # byte code, compress it, and then generate a c++ file that
887 # inserts the result into an array.
888 def embedPyFile(target, source, env):
892 return '"%s"' % string
894 '''Action function to compile a .py into a code object, marshal
895 it, compress it, and stick it into an asm file so the code appears
896 as just bytes with a label in the data section'''
898 src = file(str(source[0]), 'r').read()
900 pysource = PySource.tnodes[source[0]]
901 compiled = compile(src, pysource.abspath, 'exec')
902 marshalled = marshal.dumps(compiled)
903 compressed = zlib.compress(marshalled)
905 sym = pysource.symname
907 code = code_formatter()
909 #include "sim/init.hh"
913 const uint8_t data_${sym}[] = {
917 for i in xrange(0, len(data), step):
918 x = array.array('B', data[i:i+step])
919 code(''.join('%d,' % d for d in x))
924 EmbeddedPython embedded_${sym}(
925 ${{c_str(pysource.arcname)}},
926 ${{c_str(pysource.abspath)}},
927 ${{c_str(pysource.modpath)}},
930 ${{len(marshalled)}});
932 } // anonymous namespace
934 code.write(str(target[0]))
936 for source in PySource.all:
937 env.Command(source.cpp, source.tnode,
938 MakeAction(embedPyFile, Transform("EMBED PY")))
939 Source(source.cpp, skip_no_python=True)
941 ########################################################################
943 # Define binaries. Each different build type (debug, opt, etc.) gets
944 # a slightly different build environment.
947 # List of constructed environments to pass back to SConstruct
948 date_source = Source('base/date.cc', skip_lib=True)
950 # Function to create a new build environment as clone of current
951 # environment 'env' with modified object suffix and optional stripped
952 # binary. Additional keyword arguments are appended to corresponding
953 # build environment vars.
954 def makeEnv(env, label, objsfx, strip=False, disable_partial=False, **kwargs):
955 # SCons doesn't know to append a library suffix when there is a '.' in the
956 # name. Use '_' instead.
957 libname = 'gem5_' + label
958 exename = 'gem5.' + label
959 secondary_exename = 'm5.' + label
961 new_env = env.Clone(OBJSUFFIX=objsfx, SHOBJSUFFIX=objsfx + 's')
962 new_env.Label = label
963 new_env.Append(**kwargs)
966 # The address sanitizer is available for gcc >= 4.8
967 if GetOption('with_asan'):
968 if GetOption('with_ubsan') and \
969 compareVersions(env['GCC_VERSION'], '4.9') >= 0:
970 new_env.Append(CCFLAGS=['-fsanitize=address,undefined',
971 '-fno-omit-frame-pointer'])
972 new_env.Append(LINKFLAGS='-fsanitize=address,undefined')
974 new_env.Append(CCFLAGS=['-fsanitize=address',
975 '-fno-omit-frame-pointer'])
976 new_env.Append(LINKFLAGS='-fsanitize=address')
977 # Only gcc >= 4.9 supports UBSan, so check both the version
978 # and the command-line option before adding the compiler and
980 elif GetOption('with_ubsan') and \
981 compareVersions(env['GCC_VERSION'], '4.9') >= 0:
982 new_env.Append(CCFLAGS='-fsanitize=undefined')
983 new_env.Append(LINKFLAGS='-fsanitize=undefined')
987 # We require clang >= 3.1, so there is no need to check any
989 if GetOption('with_ubsan'):
990 if GetOption('with_asan'):
991 new_env.Append(CCFLAGS=['-fsanitize=address,undefined',
992 '-fno-omit-frame-pointer'])
993 new_env.Append(LINKFLAGS='-fsanitize=address,undefined')
995 new_env.Append(CCFLAGS='-fsanitize=undefined')
996 new_env.Append(LINKFLAGS='-fsanitize=undefined')
998 elif GetOption('with_asan'):
999 new_env.Append(CCFLAGS=['-fsanitize=address',
1000 '-fno-omit-frame-pointer'])
1001 new_env.Append(LINKFLAGS='-fsanitize=address')
1003 werror_env = new_env.Clone()
1004 # Treat warnings as errors but white list some warnings that we
1005 # want to allow (e.g., deprecation warnings).
1006 werror_env.Append(CCFLAGS=['-Werror',
1007 '-Wno-error=deprecated-declarations',
1008 '-Wno-error=deprecated',
1011 def make_obj(source, static, extra_deps = None):
1012 '''This function adds the specified source to the correct
1013 build environment, and returns the corresponding SCons Object
1022 obj = env.StaticObject(source.tnode)
1024 obj = env.SharedObject(source.tnode)
1027 env.Depends(obj, extra_deps)
1031 lib_guards = {'main': False, 'skip_lib': False}
1033 # Without Python, leave out all Python content from the library
1034 # builds. The option doesn't affect gem5 built as a program
1035 if GetOption('without_python'):
1036 lib_guards['skip_no_python'] = False
1040 for s in guarded_source_iterator(Source.source_groups[None], **lib_guards):
1041 static_objs.append(make_obj(s, True))
1042 shared_objs.append(make_obj(s, False))
1045 for group, all_srcs in Source.source_groups.iteritems():
1046 # If these are the ungrouped source files, skip them.
1050 # Get a list of the source files compatible with the current guards.
1051 srcs = [ s for s in guarded_source_iterator(all_srcs, **lib_guards) ]
1052 # If there aren't any left, skip this group.
1056 # If partial linking is disabled, add these sources to the build
1057 # directly, and short circuit this loop.
1060 static_objs.append(make_obj(s, True))
1061 shared_objs.append(make_obj(s, False))
1064 # Set up the static partially linked objects.
1065 source_objs = [ make_obj(s, True) for s in srcs ]
1066 file_name = new_env.subst("${OBJPREFIX}lib${OBJSUFFIX}.partial")
1067 target = File(joinpath(group, file_name))
1068 partial = env.PartialStatic(target=target, source=source_objs)
1069 static_objs.append(partial)
1071 # Set up the shared partially linked objects.
1072 source_objs = [ make_obj(s, False) for s in srcs ]
1073 file_name = new_env.subst("${SHOBJPREFIX}lib${SHOBJSUFFIX}.partial")
1074 target = File(joinpath(group, file_name))
1075 partial = env.PartialShared(target=target, source=source_objs)
1076 shared_objs.append(partial)
1078 static_date = make_obj(date_source, static=True, extra_deps=static_objs)
1079 static_objs.append(static_date)
1081 shared_date = make_obj(date_source, static=False, extra_deps=shared_objs)
1082 shared_objs.append(shared_date)
1084 # First make a library of everything but main() so other programs can
1086 static_lib = new_env.StaticLibrary(libname, static_objs)
1087 shared_lib = new_env.SharedLibrary(libname, shared_objs)
1089 # Now link a stub with main() and the static library.
1090 main_objs = [ make_obj(s, True) for s in Source.get(main=True) ]
1092 for test in UnitTest.all:
1093 flags = { test.target : True }
1094 test_sources = Source.get(**flags)
1095 test_objs = [ make_obj(s, static=True) for s in test_sources ]
1097 test_objs += main_objs
1098 path = 'unittest/%s.%s' % (test.target, label)
1099 new_env.Program(path, test_objs + static_objs)
1103 progname += '.unstripped'
1105 targets = new_env.Program(progname, main_objs + static_objs)
1108 if sys.platform == 'sunos5':
1109 cmd = 'cp $SOURCE $TARGET; strip $TARGET'
1111 cmd = 'strip $SOURCE -o $TARGET'
1112 targets = new_env.Command(exename, progname,
1113 MakeAction(cmd, Transform("STRIP")))
1115 new_env.Command(secondary_exename, exename,
1116 MakeAction('ln $SOURCE $TARGET', Transform("HARDLINK")))
1118 new_env.M5Binary = targets[0]
1120 # Set up regression tests.
1121 SConscript(os.path.join(env.root.abspath, 'tests', 'SConscript'),
1122 variant_dir=Dir('tests').Dir(new_env.Label),
1123 exports={ 'env' : new_env }, duplicate=False)
1125 # Start out with the compiler flags common to all compilers,
1126 # i.e. they all use -g for opt and -g -pg for prof
1127 ccflags = {'debug' : [], 'opt' : ['-g'], 'fast' : [], 'prof' : ['-g', '-pg'],
1130 # Start out with the linker flags common to all linkers, i.e. -pg for
1131 # prof, and -lprofiler for perf. The -lprofile flag is surrounded by
1132 # no-as-needed and as-needed as the binutils linker is too clever and
1133 # simply doesn't link to the library otherwise.
1134 ldflags = {'debug' : [], 'opt' : [], 'fast' : [], 'prof' : ['-pg'],
1135 'perf' : ['-Wl,--no-as-needed', '-lprofiler', '-Wl,--as-needed']}
1137 # For Link Time Optimization, the optimisation flags used to compile
1138 # individual files are decoupled from those used at link time
1139 # (i.e. you can compile with -O3 and perform LTO with -O0), so we need
1140 # to also update the linker flags based on the target.
1142 if sys.platform == 'sunos5':
1143 ccflags['debug'] += ['-gstabs+']
1145 ccflags['debug'] += ['-ggdb3']
1146 ldflags['debug'] += ['-O0']
1147 # opt, fast, prof and perf all share the same cc flags, also add
1148 # the optimization to the ldflags as LTO defers the optimization
1150 for target in ['opt', 'fast', 'prof', 'perf']:
1151 ccflags[target] += ['-O3']
1152 ldflags[target] += ['-O3']
1154 ccflags['fast'] += env['LTO_CCFLAGS']
1155 ldflags['fast'] += env['LTO_LDFLAGS']
1157 ccflags['debug'] += ['-g', '-O0']
1158 # opt, fast, prof and perf all share the same cc flags
1159 for target in ['opt', 'fast', 'prof', 'perf']:
1160 ccflags[target] += ['-O3']
1162 print 'Unknown compiler, please fix compiler options'
1166 # To speed things up, we only instantiate the build environments we
1167 # need. We try to identify the needed environment for each target; if
1168 # we can't, we fall back on instantiating all the environments just to
1170 target_types = ['debug', 'opt', 'fast', 'prof', 'perf']
1171 obj2target = {'do': 'debug', 'o': 'opt', 'fo': 'fast', 'po': 'prof',
1174 def identifyTarget(t):
1175 ext = t.split('.')[-1]
1176 if ext in target_types:
1178 if obj2target.has_key(ext):
1179 return obj2target[ext]
1180 match = re.search(r'/tests/([^/]+)/', t)
1181 if match and match.group(1) in target_types:
1182 return match.group(1)
1185 needed_envs = [identifyTarget(target) for target in BUILD_TARGETS]
1186 if 'all' in needed_envs:
1187 needed_envs += target_types
1190 if 'debug' in needed_envs:
1191 makeEnv(env, 'debug', '.do',
1192 CCFLAGS = Split(ccflags['debug']),
1193 CPPDEFINES = ['DEBUG', 'TRACING_ON=1'],
1194 LINKFLAGS = Split(ldflags['debug']))
1197 if 'opt' in needed_envs:
1198 makeEnv(env, 'opt', '.o',
1199 CCFLAGS = Split(ccflags['opt']),
1200 CPPDEFINES = ['TRACING_ON=1'],
1201 LINKFLAGS = Split(ldflags['opt']))
1204 if 'fast' in needed_envs:
1206 env.get('BROKEN_INCREMENTAL_LTO', False) and \
1207 GetOption('force_lto')
1208 makeEnv(env, 'fast', '.fo', strip = True,
1209 CCFLAGS = Split(ccflags['fast']),
1210 CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1211 LINKFLAGS = Split(ldflags['fast']),
1212 disable_partial=disable_partial)
1214 # Profiled binary using gprof
1215 if 'prof' in needed_envs:
1216 makeEnv(env, 'prof', '.po',
1217 CCFLAGS = Split(ccflags['prof']),
1218 CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1219 LINKFLAGS = Split(ldflags['prof']))
1221 # Profiled binary using google-pprof
1222 if 'perf' in needed_envs:
1223 makeEnv(env, 'perf', '.gpo',
1224 CCFLAGS = Split(ccflags['perf']),
1225 CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1226 LINKFLAGS = Split(ldflags['perf']))