3 # Copyright (c) 2018 ARM Limited
5 # The license below extends only to copyright in the software and shall
6 # not be construed as granting a license to any other intellectual
7 # property including but not limited to intellectual property relating
8 # to a hardware implementation of the functionality of the software
9 # licensed hereunder. You may use the software subject to the license
10 # terms below provided that you ensure that this notice is replicated
11 # unmodified and in its entirety in all distributions of the software,
12 # modified or unmodified, in source code or in binary form.
14 # Copyright (c) 2004-2005 The Regents of The University of Michigan
15 # All rights reserved.
17 # Redistribution and use in source and binary forms, with or without
18 # modification, are permitted provided that the following conditions are
19 # met: redistributions of source code must retain the above copyright
20 # notice, this list of conditions and the following disclaimer;
21 # redistributions in binary form must reproduce the above copyright
22 # notice, this list of conditions and the following disclaimer in the
23 # documentation and/or other materials provided with the distribution;
24 # neither the name of the copyright holders nor the names of its
25 # contributors may be used to endorse or promote products derived from
26 # this software without specific prior written permission.
28 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 # Authors: Nathan Binkert
42 from __future__ import print_function
55 from os.path import basename, dirname, exists, isdir, isfile, join as joinpath
59 from gem5_scons import Transform
61 # This file defines how to build a particular configuration of gem5
62 # based on variable settings in the 'env' build environment.
66 # Children need to see the environment
69 build_env = [(opt, env[opt]) for opt in export_vars]
71 from m5.util import code_formatter, compareVersions
73 ########################################################################
74 # Code for adding source files of various types
76 # When specifying a source file of some type, a set of tags can be
77 # specified for that file.
79 class SourceFilter(object):
80 def __init__(self, predicate):
81 self.predicate = predicate
83 def __or__(self, other):
84 return SourceFilter(lambda tags: self.predicate(tags) or
85 other.predicate(tags))
87 def __and__(self, other):
88 return SourceFilter(lambda tags: self.predicate(tags) and
89 other.predicate(tags))
91 def with_tags_that(predicate):
92 '''Return a list of sources with tags that satisfy a predicate.'''
93 return SourceFilter(predicate)
95 def with_any_tags(*tags):
96 '''Return a list of sources with any of the supplied tags.'''
97 return SourceFilter(lambda stags: len(set(tags) & stags) > 0)
99 def with_all_tags(*tags):
100 '''Return a list of sources with all of the supplied tags.'''
101 return SourceFilter(lambda stags: set(tags) <= stags)
104 '''Return a list of sources with the supplied tag.'''
105 return SourceFilter(lambda stags: tag in stags)
107 def without_tags(*tags):
108 '''Return a list of sources without any of the supplied tags.'''
109 return SourceFilter(lambda stags: len(set(tags) & stags) == 0)
111 def without_tag(tag):
112 '''Return a list of sources with the supplied tag.'''
113 return SourceFilter(lambda stags: tag not in stags)
115 source_filter_factories = {
116 'with_tags_that': with_tags_that,
117 'with_any_tags': with_any_tags,
118 'with_all_tags': with_all_tags,
119 'with_tag': with_tag,
120 'without_tags': without_tags,
121 'without_tag': without_tag,
124 Export(source_filter_factories)
126 class SourceList(list):
127 def apply_filter(self, f):
129 return f.predicate(source.tags)
130 return SourceList(filter(match, self))
132 def __getattr__(self, name):
133 func = source_filter_factories.get(name, None)
137 @functools.wraps(func)
138 def wrapper(*args, **kwargs):
139 return self.apply_filter(func(*args, **kwargs))
142 class SourceMeta(type):
143 '''Meta class for source files that keeps track of all files of a
145 def __init__(cls, name, bases, dict):
146 super(SourceMeta, cls).__init__(name, bases, dict)
147 cls.all = SourceList()
149 class SourceFile(object):
150 '''Base object that encapsulates the notion of a source file.
151 This includes, the source node, target node, various manipulations
152 of those. A source file also specifies a set of tags which
153 describing arbitrary properties of the source file.'''
154 __metaclass__ = SourceMeta
159 def __init__(self, source, tags=None, add_tags=None):
162 if isinstance(tags, basestring):
164 if not isinstance(tags, set):
169 if isinstance(add_tags, basestring):
170 add_tags = set([add_tags])
171 if not isinstance(add_tags, set):
172 add_tags = set(add_tags)
173 self.tags |= add_tags
176 if not isinstance(source, SCons.Node.FS.File):
180 self.snode = tnode.srcnode()
182 for base in type(self).__mro__:
183 if issubclass(base, SourceFile):
184 base.all.append(self)
186 def static(self, env):
187 key = (self.tnode, env['OBJSUFFIX'])
188 if not key in self.static_objs:
189 self.static_objs[key] = env.StaticObject(self.tnode)
190 return self.static_objs[key]
192 def shared(self, env):
193 key = (self.tnode, env['OBJSUFFIX'])
194 if not key in self.shared_objs:
195 self.shared_objs[key] = env.SharedObject(self.tnode)
196 return self.shared_objs[key]
200 return str(self.tnode)
204 return dirname(self.filename)
208 return basename(self.filename)
212 index = self.basename.rfind('.')
214 # dot files aren't extensions
215 return self.basename, None
217 return self.basename[:index], self.basename[index+1:]
219 def __lt__(self, other): return self.filename < other.filename
220 def __le__(self, other): return self.filename <= other.filename
221 def __gt__(self, other): return self.filename > other.filename
222 def __ge__(self, other): return self.filename >= other.filename
223 def __eq__(self, other): return self.filename == other.filename
224 def __ne__(self, other): return self.filename != other.filename
226 def blobToCpp(data, symbol, cpp_code, hpp_code=None, namespace=None):
228 Convert bytes data into C++ .cpp and .hh uint8_t byte array
229 code containing that binary data.
231 :param data: binary data to be converted to C++
232 :param symbol: name of the symbol
233 :param cpp_code: append the generated cpp_code to this object
234 :param hpp_code: append the generated hpp_code to this object
235 If None, ignore it. Otherwise, also include it
237 :param namespace: namespace to put the symbol into. If None,
238 don't put the symbols into any namespace.
240 symbol_len_declaration = 'const std::size_t {}_len'.format(symbol)
241 symbol_declaration = 'const std::uint8_t {}[]'.format(symbol)
242 if hpp_code is not None:
244 #include "blobs/{}.hh"
250 if namespace is not None:
251 hpp_code('namespace {} {{'.format(namespace))
252 hpp_code('extern ' + symbol_len_declaration + ';')
253 hpp_code('extern ' + symbol_declaration + ';')
254 if namespace is not None:
256 if namespace is not None:
257 cpp_code('namespace {} {{'.format(namespace))
258 cpp_code(symbol_len_declaration + ' = {};'.format(len(data)))
259 cpp_code(symbol_declaration + ' = {')
262 for i in xrange(0, len(data), step):
263 x = array.array('B', data[i:i+step])
264 cpp_code(''.join('%d,' % d for d in x))
267 if namespace is not None:
270 def Blob(blob_path, symbol):
272 Embed an arbitrary blob into the gem5 executable,
273 and make it accessible to C++ as a byte array.
275 blob_path = os.path.abspath(blob_path)
276 blob_out_dir = os.path.join(env['BUILDDIR'], 'blobs')
277 path_noext = joinpath(blob_out_dir, symbol)
278 cpp_path = path_noext + '.cc'
279 hpp_path = path_noext + '.hh'
280 def embedBlob(target, source, env):
281 data = file(str(source[0]), 'r').read()
282 cpp_code = code_formatter()
283 hpp_code = code_formatter()
284 blobToCpp(data, symbol, cpp_code, hpp_code, namespace='Blobs')
285 cpp_path = str(target[0])
286 hpp_path = str(target[1])
287 cpp_dir = os.path.split(cpp_path)[0]
288 if not os.path.exists(cpp_dir):
290 cpp_code.write(cpp_path)
291 hpp_code.write(hpp_path)
292 env.Command([cpp_path, hpp_path], blob_path,
293 MakeAction(embedBlob, Transform("EMBED BLOB")))
296 def GdbXml(xml_id, symbol):
297 Blob(joinpath(gdb_xml_dir, xml_id), symbol)
299 class Source(SourceFile):
300 ungrouped_tag = 'No link group'
301 source_groups = set()
303 _current_group_tag = ungrouped_tag
306 def link_group_tag(group):
307 return 'link group: %s' % group
310 def set_group(cls, group):
311 new_tag = Source.link_group_tag(group)
312 Source._current_group_tag = new_tag
313 Source.source_groups.add(group)
315 def _add_link_group_tag(self):
316 self.tags.add(Source._current_group_tag)
318 '''Add a c/c++ source file to the build'''
319 def __init__(self, source, tags=None, add_tags=None):
320 '''specify the source file, and any tags'''
321 super(Source, self).__init__(source, tags, add_tags)
322 self._add_link_group_tag()
324 class PySource(SourceFile):
325 '''Add a python source file to the named package'''
326 invalid_sym_char = re.compile('[^A-z0-9_]')
331 def __init__(self, package, source, tags=None, add_tags=None):
332 '''specify the python package, the source file, and any tags'''
333 super(PySource, self).__init__(source, tags, add_tags)
335 modname,ext = self.extname
339 path = package.split('.')
344 if modname != '__init__':
345 modpath += [ modname ]
346 modpath = '.'.join(modpath)
348 arcpath = path + [ self.basename ]
349 abspath = self.snode.abspath
350 if not exists(abspath):
351 abspath = self.tnode.abspath
353 self.package = package
354 self.modname = modname
355 self.modpath = modpath
356 self.arcname = joinpath(*arcpath)
357 self.abspath = abspath
358 self.compiled = File(self.filename + 'c')
359 self.cpp = File(self.filename + '.cc')
360 self.symname = PySource.invalid_sym_char.sub('_', modpath)
362 PySource.modules[modpath] = self
363 PySource.tnodes[self.tnode] = self
364 PySource.symnames[self.symname] = self
366 class SimObject(PySource):
367 '''Add a SimObject python file as a python source object and add
368 it to a list of sim object modules'''
373 def __init__(self, source, tags=None, add_tags=None):
374 '''Specify the source file and any tags (automatically in
375 the m5.objects package)'''
376 super(SimObject, self).__init__('m5.objects', source, tags, add_tags)
378 raise AttributeError, "Too late to call SimObject now."
380 bisect.insort_right(SimObject.modnames, self.modname)
382 class ProtoBuf(SourceFile):
383 '''Add a Protocol Buffer to build'''
385 def __init__(self, source, tags=None, add_tags=None):
386 '''Specify the source file, and any tags'''
387 super(ProtoBuf, self).__init__(source, tags, add_tags)
389 # Get the file name and the extension
390 modname,ext = self.extname
391 assert ext == 'proto'
393 # Currently, we stick to generating the C++ headers, so we
394 # only need to track the source and header.
395 self.cc_file = File(modname + '.pb.cc')
396 self.hh_file = File(modname + '.pb.h')
399 exectuable_classes = []
400 class ExecutableMeta(type):
401 '''Meta class for Executables.'''
404 def __init__(cls, name, bases, d):
405 if not d.pop('abstract', False):
406 ExecutableMeta.all.append(cls)
407 super(ExecutableMeta, cls).__init__(name, bases, d)
411 class Executable(object):
412 '''Base class for creating an executable from sources.'''
413 __metaclass__ = ExecutableMeta
417 def __init__(self, target, *srcs_and_filts):
418 '''Specify the target name and any sources. Sources that are
419 not SourceFiles are evalued with Source().'''
420 super(Executable, self).__init__()
421 self.all.append(self)
424 isFilter = lambda arg: isinstance(arg, SourceFilter)
425 self.filters = filter(isFilter, srcs_and_filts)
426 sources = filter(lambda a: not isFilter(a), srcs_and_filts)
430 if not isinstance(src, SourceFile):
431 src = Source(src, tags=[])
438 return self.dir.File(self.target + '.' + env['EXE_SUFFIX'])
440 def srcs_to_objs(self, env, sources):
441 return list([ s.static(env) for s in sources ])
444 def declare_all(cls, env):
445 return list([ instance.declare(env) for instance in cls.all ])
447 def declare(self, env, objs=None):
449 objs = self.srcs_to_objs(env, self.sources)
451 if env['STRIP_EXES']:
452 stripped = self.path(env)
453 unstripped = env.File(str(stripped) + '.unstripped')
454 if sys.platform == 'sunos5':
455 cmd = 'cp $SOURCE $TARGET; strip $TARGET'
457 cmd = 'strip $SOURCE -o $TARGET'
458 env.Program(unstripped, objs)
459 return env.Command(stripped, unstripped,
460 MakeAction(cmd, Transform("STRIP")))
462 return env.Program(self.path(env), objs)
464 class UnitTest(Executable):
465 '''Create a UnitTest'''
466 def __init__(self, target, *srcs_and_filts, **kwargs):
467 super(UnitTest, self).__init__(target, *srcs_and_filts)
469 self.main = kwargs.get('main', False)
471 def declare(self, env):
472 sources = list(self.sources)
473 for f in self.filters:
474 sources = Source.all.apply_filter(f)
475 objs = self.srcs_to_objs(env, sources) + env['STATIC_OBJS']
477 objs += env['MAIN_OBJS']
478 return super(UnitTest, self).declare(env, objs)
480 class GTest(Executable):
481 '''Create a unit test based on the google test framework.'''
483 def __init__(self, *srcs_and_filts, **kwargs):
484 super(GTest, self).__init__(*srcs_and_filts)
486 self.skip_lib = kwargs.pop('skip_lib', False)
489 def declare_all(cls, env):
491 env.Append(LIBS=env['GTEST_LIBS'])
492 env.Append(CPPFLAGS=env['GTEST_CPPFLAGS'])
493 env['GTEST_LIB_SOURCES'] = Source.all.with_tag('gtest lib')
494 env['GTEST_OUT_DIR'] = \
495 Dir(env['BUILDDIR']).Dir('unittests.' + env['EXE_SUFFIX'])
496 return super(GTest, cls).declare_all(env)
498 def declare(self, env):
499 sources = list(self.sources)
500 if not self.skip_lib:
501 sources += env['GTEST_LIB_SOURCES']
502 for f in self.filters:
503 sources += Source.all.apply_filter(f)
504 objs = self.srcs_to_objs(env, sources)
506 binary = super(GTest, self).declare(env, objs)
508 out_dir = env['GTEST_OUT_DIR']
509 xml_file = out_dir.Dir(str(self.dir)).File(self.target + '.xml')
510 AlwaysBuild(env.Command(xml_file, binary,
511 "${SOURCES[0]} --gtest_output=xml:${TARGETS[0]}"))
515 class Gem5(Executable):
516 '''Create a gem5 executable.'''
518 def __init__(self, target):
519 super(Gem5, self).__init__(target)
521 def declare(self, env):
522 objs = env['MAIN_OBJS'] + env['STATIC_OBJS']
523 return super(Gem5, self).declare(env, objs)
526 # Children should have access
537 ########################################################################
542 def DebugFlag(name, desc=None):
543 if name in debug_flags:
544 raise AttributeError, "Flag %s already specified" % name
545 debug_flags[name] = (name, (), desc)
547 def CompoundFlag(name, flags, desc=None):
548 if name in debug_flags:
549 raise AttributeError, "Flag %s already specified" % name
551 compound = tuple(flags)
552 debug_flags[name] = (name, compound, desc)
555 Export('CompoundFlag')
557 ########################################################################
559 # Set some compiler variables
562 # Include file paths are rooted in this directory. SCons will
563 # automatically expand '.' to refer to both the source directory and
564 # the corresponding build directory to pick up generated include
566 env.Append(CPPPATH=Dir('.'))
568 for extra_dir in extras_dir_list:
569 env.Append(CPPPATH=Dir(extra_dir))
571 # Workaround for bug in SCons version > 0.97d20071212
572 # Scons bug id: 2006 gem5 Bug id: 308
573 for root, dirs, files in os.walk(base_dir, topdown=True):
574 Dir(root[len(base_dir) + 1:])
576 ########################################################################
578 # Walk the tree and execute all SConscripts in subdirectories
581 here = Dir('.').srcnode().abspath
582 for root, dirs, files in os.walk(base_dir, topdown=True):
584 # we don't want to recurse back into this SConscript
587 if 'SConscript' in files:
588 build_dir = joinpath(env['BUILDDIR'], root[len(base_dir) + 1:])
589 Source.set_group(build_dir)
590 SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
592 for extra_dir in extras_dir_list:
593 prefix_len = len(dirname(extra_dir)) + 1
595 # Also add the corresponding build directory to pick up generated
597 env.Append(CPPPATH=Dir(joinpath(env['BUILDDIR'], extra_dir[prefix_len:])))
599 for root, dirs, files in os.walk(extra_dir, topdown=True):
600 # if build lives in the extras directory, don't walk down it
604 if 'SConscript' in files:
605 build_dir = joinpath(env['BUILDDIR'], root[prefix_len:])
606 SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
608 for opt in export_vars:
611 def makeTheISA(source, target, env):
612 isas = [ src.get_contents() for src in source ]
613 target_isa = env['TARGET_ISA']
615 return isa.upper() + '_ISA'
618 return isa[0].upper() + isa[1:].lower() + 'ISA'
621 code = code_formatter()
623 #ifndef __CONFIG_THE_ISA_HH__
624 #define __CONFIG_THE_ISA_HH__
628 # create defines for the preprocessing and compile-time determination
629 for i,isa in enumerate(isas):
630 code('#define $0 $1', define(isa), i + 1)
633 # create an enum for any run-time determination of the ISA, we
634 # reuse the same name as the namespaces
635 code('enum class Arch {')
636 for i,isa in enumerate(isas):
637 if i + 1 == len(isas):
638 code(' $0 = $1', namespace(isa), define(isa))
640 code(' $0 = $1,', namespace(isa), define(isa))
645 #define THE_ISA ${{define(target_isa)}}
646 #define TheISA ${{namespace(target_isa)}}
647 #define THE_ISA_STR "${{target_isa}}"
649 #endif // __CONFIG_THE_ISA_HH__''')
651 code.write(str(target[0]))
653 env.Command('config/the_isa.hh', map(Value, all_isa_list),
654 MakeAction(makeTheISA, Transform("CFG ISA", 0)))
656 def makeTheGPUISA(source, target, env):
657 isas = [ src.get_contents() for src in source ]
658 target_gpu_isa = env['TARGET_GPU_ISA']
660 return isa.upper() + '_ISA'
663 return isa[0].upper() + isa[1:].lower() + 'ISA'
666 code = code_formatter()
668 #ifndef __CONFIG_THE_GPU_ISA_HH__
669 #define __CONFIG_THE_GPU_ISA_HH__
673 # create defines for the preprocessing and compile-time determination
674 for i,isa in enumerate(isas):
675 code('#define $0 $1', define(isa), i + 1)
678 # create an enum for any run-time determination of the ISA, we
679 # reuse the same name as the namespaces
680 code('enum class GPUArch {')
681 for i,isa in enumerate(isas):
682 if i + 1 == len(isas):
683 code(' $0 = $1', namespace(isa), define(isa))
685 code(' $0 = $1,', namespace(isa), define(isa))
690 #define THE_GPU_ISA ${{define(target_gpu_isa)}}
691 #define TheGpuISA ${{namespace(target_gpu_isa)}}
692 #define THE_GPU_ISA_STR "${{target_gpu_isa}}"
694 #endif // __CONFIG_THE_GPU_ISA_HH__''')
696 code.write(str(target[0]))
698 env.Command('config/the_gpu_isa.hh', map(Value, all_gpu_isa_list),
699 MakeAction(makeTheGPUISA, Transform("CFG ISA", 0)))
701 ########################################################################
703 # Prevent any SimObjects from being added after this point, they
704 # should all have been added in the SConscripts above
706 SimObject.fixed = True
708 class DictImporter(object):
709 '''This importer takes a dictionary of arbitrary module names that
710 map to arbitrary filenames.'''
711 def __init__(self, modules):
712 self.modules = modules
713 self.installed = set()
720 for module in self.installed:
721 del sys.modules[module]
722 self.installed = set()
724 def find_module(self, fullname, path):
725 if fullname == 'm5.defines':
728 if fullname == 'm5.objects':
731 if fullname.startswith('_m5'):
734 source = self.modules.get(fullname, None)
735 if source is not None and fullname.startswith('m5.objects'):
740 def load_module(self, fullname):
741 mod = imp.new_module(fullname)
742 sys.modules[fullname] = mod
743 self.installed.add(fullname)
745 mod.__loader__ = self
746 if fullname == 'm5.objects':
747 mod.__path__ = fullname.split('.')
750 if fullname == 'm5.defines':
751 mod.__dict__['buildEnv'] = m5.util.SmartDict(build_env)
754 source = self.modules[fullname]
755 if source.modname == '__init__':
756 mod.__path__ = source.modpath
757 mod.__file__ = source.abspath
759 exec file(source.abspath, 'r') in mod.__dict__
765 from m5.util import code_formatter
770 # install the python importer so we can grab stuff from the source
771 # tree itself. We can't have SimObjects added after this point or
772 # else we won't know about them for the rest of the stuff.
773 importer = DictImporter(PySource.modules)
774 sys.meta_path[0:0] = [ importer ]
776 # import all sim objects so we can populate the all_objects list
777 # make sure that we're working with a list, then let's sort it
778 for modname in SimObject.modnames:
779 exec('from m5.objects import %s' % modname)
781 # we need to unload all of the currently imported modules so that they
782 # will be re-imported the next time the sconscript is run
784 sys.meta_path.remove(importer)
786 sim_objects = m5.SimObject.allClasses
787 all_enums = m5.params.allEnums
789 for name,obj in sorted(sim_objects.iteritems()):
790 for param in obj._params.local.values():
791 # load the ptype attribute now because it depends on the
792 # current version of SimObject.allClasses, but when scons
793 # actually uses the value, all versions of
794 # SimObject.allClasses will have been loaded
797 ########################################################################
799 # calculate extra dependencies
801 module_depends = ["m5", "m5.SimObject", "m5.params"]
802 depends = [ PySource.modules[dep].snode for dep in module_depends ]
803 depends.sort(key = lambda x: x.name)
805 ########################################################################
807 # Commands for the basic automatically generated python files
810 # Generate Python file containing a dict specifying the current
812 def makeDefinesPyFile(target, source, env):
813 build_env = source[0].get_contents()
815 code = code_formatter()
820 buildEnv = m5.util.SmartDict($build_env)
822 compileDate = _m5.core.compileDate
824 for key,val in _m5.core.__dict__.iteritems():
825 if key.startswith('flag_'):
830 code.write(target[0].abspath)
832 defines_info = Value(build_env)
833 # Generate a file with all of the compile options in it
834 env.Command('python/m5/defines.py', defines_info,
835 MakeAction(makeDefinesPyFile, Transform("DEFINES", 0)))
836 PySource('m5', 'python/m5/defines.py')
838 # Generate python file containing info about the M5 source code
839 def makeInfoPyFile(target, source, env):
840 code = code_formatter()
842 data = ''.join(file(src.srcnode().abspath, 'r').xreadlines())
843 code('$src = ${{repr(data)}}')
844 code.write(str(target[0]))
846 # Generate a file that wraps the basic top level files
847 env.Command('python/m5/info.py',
848 [ '#/COPYING', '#/LICENSE', '#/README', ],
849 MakeAction(makeInfoPyFile, Transform("INFO")))
850 PySource('m5', 'python/m5/info.py')
852 ########################################################################
854 # Create all of the SimObject param headers and enum headers
857 def createSimObjectParamStruct(target, source, env):
858 assert len(target) == 1 and len(source) == 1
860 name = source[0].get_text_contents()
861 obj = sim_objects[name]
863 code = code_formatter()
864 obj.cxx_param_decl(code)
865 code.write(target[0].abspath)
867 def createSimObjectCxxConfig(is_header):
868 def body(target, source, env):
869 assert len(target) == 1 and len(source) == 1
871 name = str(source[0].get_contents())
872 obj = sim_objects[name]
874 code = code_formatter()
875 obj.cxx_config_param_file(code, is_header)
876 code.write(target[0].abspath)
879 def createEnumStrings(target, source, env):
880 assert len(target) == 1 and len(source) == 2
882 name = source[0].get_text_contents()
883 use_python = source[1].read()
884 obj = all_enums[name]
886 code = code_formatter()
890 code.write(target[0].abspath)
892 def createEnumDecls(target, source, env):
893 assert len(target) == 1 and len(source) == 1
895 name = source[0].get_text_contents()
896 obj = all_enums[name]
898 code = code_formatter()
900 code.write(target[0].abspath)
902 def createSimObjectPyBindWrapper(target, source, env):
903 name = source[0].get_text_contents()
904 obj = sim_objects[name]
906 code = code_formatter()
907 obj.pybind_decl(code)
908 code.write(target[0].abspath)
910 # Generate all of the SimObject param C++ struct header files
912 for name,simobj in sorted(sim_objects.iteritems()):
913 py_source = PySource.modules[simobj.__module__]
914 extra_deps = [ py_source.tnode ]
916 hh_file = File('params/%s.hh' % name)
917 params_hh_files.append(hh_file)
918 env.Command(hh_file, Value(name),
919 MakeAction(createSimObjectParamStruct, Transform("SO PARAM")))
920 env.Depends(hh_file, depends + extra_deps)
922 # C++ parameter description files
923 if GetOption('with_cxx_config'):
924 for name,simobj in sorted(sim_objects.iteritems()):
925 py_source = PySource.modules[simobj.__module__]
926 extra_deps = [ py_source.tnode ]
928 cxx_config_hh_file = File('cxx_config/%s.hh' % name)
929 cxx_config_cc_file = File('cxx_config/%s.cc' % name)
930 env.Command(cxx_config_hh_file, Value(name),
931 MakeAction(createSimObjectCxxConfig(True),
932 Transform("CXXCPRHH")))
933 env.Command(cxx_config_cc_file, Value(name),
934 MakeAction(createSimObjectCxxConfig(False),
935 Transform("CXXCPRCC")))
936 env.Depends(cxx_config_hh_file, depends + extra_deps +
937 [File('params/%s.hh' % name), File('sim/cxx_config.hh')])
938 env.Depends(cxx_config_cc_file, depends + extra_deps +
939 [cxx_config_hh_file])
940 Source(cxx_config_cc_file)
942 cxx_config_init_cc_file = File('cxx_config/init.cc')
944 def createCxxConfigInitCC(target, source, env):
945 assert len(target) == 1 and len(source) == 1
947 code = code_formatter()
949 for name,simobj in sorted(sim_objects.iteritems()):
950 if not hasattr(simobj, 'abstract') or not simobj.abstract:
951 code('#include "cxx_config/${name}.hh"')
953 code('void cxxConfigInit()')
956 for name,simobj in sorted(sim_objects.iteritems()):
957 not_abstract = not hasattr(simobj, 'abstract') or \
959 if not_abstract and 'type' in simobj.__dict__:
960 code('cxx_config_directory["${name}"] = '
961 '${name}CxxConfigParams::makeDirectoryEntry();')
964 code.write(target[0].abspath)
966 py_source = PySource.modules[simobj.__module__]
967 extra_deps = [ py_source.tnode ]
968 env.Command(cxx_config_init_cc_file, Value(name),
969 MakeAction(createCxxConfigInitCC, Transform("CXXCINIT")))
970 cxx_param_hh_files = ["cxx_config/%s.hh" % simobj
971 for name,simobj in sorted(sim_objects.iteritems())
972 if not hasattr(simobj, 'abstract') or not simobj.abstract]
973 Depends(cxx_config_init_cc_file, cxx_param_hh_files +
974 [File('sim/cxx_config.hh')])
975 Source(cxx_config_init_cc_file)
977 # Generate all enum header files
978 for name,enum in sorted(all_enums.iteritems()):
979 py_source = PySource.modules[enum.__module__]
980 extra_deps = [ py_source.tnode ]
982 cc_file = File('enums/%s.cc' % name)
983 env.Command(cc_file, [Value(name), Value(env['USE_PYTHON'])],
984 MakeAction(createEnumStrings, Transform("ENUM STR")))
985 env.Depends(cc_file, depends + extra_deps)
988 hh_file = File('enums/%s.hh' % name)
989 env.Command(hh_file, Value(name),
990 MakeAction(createEnumDecls, Transform("ENUMDECL")))
991 env.Depends(hh_file, depends + extra_deps)
993 # Generate SimObject Python bindings wrapper files
994 if env['USE_PYTHON']:
995 for name,simobj in sorted(sim_objects.iteritems()):
996 py_source = PySource.modules[simobj.__module__]
997 extra_deps = [ py_source.tnode ]
998 cc_file = File('python/_m5/param_%s.cc' % name)
999 env.Command(cc_file, Value(name),
1000 MakeAction(createSimObjectPyBindWrapper,
1001 Transform("SO PyBind")))
1002 env.Depends(cc_file, depends + extra_deps)
1005 # Build all protocol buffers if we have got protoc and protobuf available
1006 if env['HAVE_PROTOBUF']:
1007 for proto in ProtoBuf.all:
1008 # Use both the source and header as the target, and the .proto
1009 # file as the source. When executing the protoc compiler, also
1010 # specify the proto_path to avoid having the generated files
1012 env.Command([proto.cc_file, proto.hh_file], proto.tnode,
1013 MakeAction('$PROTOC --cpp_out ${TARGET.dir} '
1014 '--proto_path ${SOURCE.dir} $SOURCE',
1015 Transform("PROTOC")))
1017 # Add the C++ source file
1018 Source(proto.cc_file, tags=proto.tags)
1020 print('Got protobuf to build, but lacks support!')
1024 # Handle debug flags
1026 def makeDebugFlagCC(target, source, env):
1027 assert(len(target) == 1 and len(source) == 1)
1029 code = code_formatter()
1031 # delay definition of CompoundFlags until after all the definition
1032 # of all constituent SimpleFlags
1033 comp_code = code_formatter()
1038 * DO NOT EDIT THIS FILE! Automatically generated by SCons.
1041 #include "base/debug.hh"
1047 for name, flag in sorted(source[0].read().iteritems()):
1048 n, compound, desc = flag
1052 code('SimpleFlag $name("$name", "$desc");')
1054 comp_code('CompoundFlag $name("$name", "$desc",')
1056 last = len(compound) - 1
1057 for i,flag in enumerate(compound):
1059 comp_code('&$flag,')
1061 comp_code('&$flag);')
1064 code.append(comp_code)
1066 code('} // namespace Debug')
1068 code.write(str(target[0]))
1070 def makeDebugFlagHH(target, source, env):
1071 assert(len(target) == 1 and len(source) == 1)
1073 val = eval(source[0].get_contents())
1074 name, compound, desc = val
1076 code = code_formatter()
1078 # file header boilerplate
1081 * DO NOT EDIT THIS FILE! Automatically generated by SCons.
1084 #ifndef __DEBUG_${name}_HH__
1085 #define __DEBUG_${name}_HH__
1091 code('class CompoundFlag;')
1092 code('class SimpleFlag;')
1095 code('extern CompoundFlag $name;')
1096 for flag in compound:
1097 code('extern SimpleFlag $flag;')
1099 code('extern SimpleFlag $name;')
1104 #endif // __DEBUG_${name}_HH__
1107 code.write(str(target[0]))
1109 for name,flag in sorted(debug_flags.iteritems()):
1110 n, compound, desc = flag
1113 hh_file = 'debug/%s.hh' % name
1114 env.Command(hh_file, Value(flag),
1115 MakeAction(makeDebugFlagHH, Transform("TRACING", 0)))
1117 env.Command('debug/flags.cc', Value(debug_flags),
1118 MakeAction(makeDebugFlagCC, Transform("TRACING", 0)))
1119 Source('debug/flags.cc')
1123 env.Command('sim/tags.cc', None,
1124 MakeAction('util/cpt_upgrader.py --get-cc-file > $TARGET',
1125 Transform("VER TAGS")))
1126 env.AlwaysBuild(tags)
1128 # Embed python files. All .py files that have been indicated by a
1129 # PySource() call in a SConscript need to be embedded into the M5
1130 # library. To do that, we compile the file to byte code, marshal the
1131 # byte code, compress it, and then generate a c++ file that
1132 # inserts the result into an array.
1133 def embedPyFile(target, source, env):
1137 return '"%s"' % string
1139 '''Action function to compile a .py into a code object, marshal
1140 it, compress it, and stick it into an asm file so the code appears
1141 as just bytes with a label in the data section'''
1143 src = file(str(source[0]), 'r').read()
1145 pysource = PySource.tnodes[source[0]]
1146 compiled = compile(src, pysource.abspath, 'exec')
1147 marshalled = marshal.dumps(compiled)
1148 compressed = zlib.compress(marshalled)
1150 sym = pysource.symname
1152 code = code_formatter()
1154 #include "sim/init.hh"
1159 blobToCpp(data, 'data_' + sym, code)
1163 EmbeddedPython embedded_${sym}(
1164 ${{c_str(pysource.arcname)}},
1165 ${{c_str(pysource.abspath)}},
1166 ${{c_str(pysource.modpath)}},
1169 ${{len(marshalled)}});
1171 } // anonymous namespace
1173 code.write(str(target[0]))
1175 for source in PySource.all:
1176 env.Command(source.cpp, source.tnode,
1177 MakeAction(embedPyFile, Transform("EMBED PY")))
1178 Source(source.cpp, tags=source.tags, add_tags='python')
1180 ########################################################################
1182 # Define binaries. Each different build type (debug, opt, etc.) gets
1183 # a slightly different build environment.
1186 # List of constructed environments to pass back to SConstruct
1187 date_source = Source('base/date.cc', tags=[])
1189 gem5_binary = Gem5('gem5')
1191 # Function to create a new build environment as clone of current
1192 # environment 'env' with modified object suffix and optional stripped
1193 # binary. Additional keyword arguments are appended to corresponding
1194 # build environment vars.
1195 def makeEnv(env, label, objsfx, strip=False, disable_partial=False, **kwargs):
1196 # SCons doesn't know to append a library suffix when there is a '.' in the
1197 # name. Use '_' instead.
1198 libname = 'gem5_' + label
1199 secondary_exename = 'm5.' + label
1201 new_env = env.Clone(OBJSUFFIX=objsfx, SHOBJSUFFIX=objsfx + 's')
1202 new_env.Label = label
1203 new_env.Append(**kwargs)
1205 lib_sources = Source.all.with_tag('gem5 lib')
1207 # Without Python, leave out all Python content from the library
1208 # builds. The option doesn't affect gem5 built as a program
1209 if GetOption('without_python'):
1210 lib_sources = lib_sources.without_tag('python')
1215 for s in lib_sources.with_tag(Source.ungrouped_tag):
1216 static_objs.append(s.static(new_env))
1217 shared_objs.append(s.shared(new_env))
1219 for group in Source.source_groups:
1220 srcs = lib_sources.with_tag(Source.link_group_tag(group))
1224 group_static = [ s.static(new_env) for s in srcs ]
1225 group_shared = [ s.shared(new_env) for s in srcs ]
1227 # If partial linking is disabled, add these sources to the build
1228 # directly, and short circuit this loop.
1230 static_objs.extend(group_static)
1231 shared_objs.extend(group_shared)
1234 # Set up the static partially linked objects.
1235 file_name = new_env.subst("${OBJPREFIX}lib${OBJSUFFIX}.partial")
1236 target = File(joinpath(group, file_name))
1237 partial = env.PartialStatic(target=target, source=group_static)
1238 static_objs.extend(partial)
1240 # Set up the shared partially linked objects.
1241 file_name = new_env.subst("${SHOBJPREFIX}lib${SHOBJSUFFIX}.partial")
1242 target = File(joinpath(group, file_name))
1243 partial = env.PartialShared(target=target, source=group_shared)
1244 shared_objs.extend(partial)
1246 static_date = date_source.static(new_env)
1247 new_env.Depends(static_date, static_objs)
1248 static_objs.extend(static_date)
1250 shared_date = date_source.shared(new_env)
1251 new_env.Depends(shared_date, shared_objs)
1252 shared_objs.extend(shared_date)
1254 main_objs = [ s.static(new_env) for s in Source.all.with_tag('main') ]
1256 # First make a library of everything but main() so other programs can
1258 static_lib = new_env.StaticLibrary(libname, static_objs)
1259 shared_lib = new_env.SharedLibrary(libname, shared_objs)
1261 # Keep track of the object files generated so far so Executables can
1263 new_env['STATIC_OBJS'] = static_objs
1264 new_env['SHARED_OBJS'] = shared_objs
1265 new_env['MAIN_OBJS'] = main_objs
1267 new_env['STATIC_LIB'] = static_lib
1268 new_env['SHARED_LIB'] = shared_lib
1270 # Record some settings for building Executables.
1271 new_env['EXE_SUFFIX'] = label
1272 new_env['STRIP_EXES'] = strip
1274 for cls in ExecutableMeta.all:
1275 cls.declare_all(new_env)
1277 new_env.M5Binary = File(gem5_binary.path(new_env))
1279 new_env.Command(secondary_exename, new_env.M5Binary,
1280 MakeAction('ln $SOURCE $TARGET', Transform("HARDLINK")))
1282 # Set up regression tests.
1283 SConscript(os.path.join(env.root.abspath, 'tests', 'SConscript'),
1284 variant_dir=Dir('tests').Dir(new_env.Label),
1285 exports={ 'env' : new_env }, duplicate=False)
1287 # Start out with the compiler flags common to all compilers,
1288 # i.e. they all use -g for opt and -g -pg for prof
1289 ccflags = {'debug' : [], 'opt' : ['-g'], 'fast' : [], 'prof' : ['-g', '-pg'],
1292 # Start out with the linker flags common to all linkers, i.e. -pg for
1293 # prof, and -lprofiler for perf. The -lprofile flag is surrounded by
1294 # no-as-needed and as-needed as the binutils linker is too clever and
1295 # simply doesn't link to the library otherwise.
1296 ldflags = {'debug' : [], 'opt' : [], 'fast' : [], 'prof' : ['-pg'],
1297 'perf' : ['-Wl,--no-as-needed', '-lprofiler', '-Wl,--as-needed']}
1299 # For Link Time Optimization, the optimisation flags used to compile
1300 # individual files are decoupled from those used at link time
1301 # (i.e. you can compile with -O3 and perform LTO with -O0), so we need
1302 # to also update the linker flags based on the target.
1304 if sys.platform == 'sunos5':
1305 ccflags['debug'] += ['-gstabs+']
1307 ccflags['debug'] += ['-ggdb3']
1308 ldflags['debug'] += ['-O0']
1309 # opt, fast, prof and perf all share the same cc flags, also add
1310 # the optimization to the ldflags as LTO defers the optimization
1312 for target in ['opt', 'fast', 'prof', 'perf']:
1313 ccflags[target] += ['-O3']
1314 ldflags[target] += ['-O3']
1316 ccflags['fast'] += env['LTO_CCFLAGS']
1317 ldflags['fast'] += env['LTO_LDFLAGS']
1319 ccflags['debug'] += ['-g', '-O0']
1320 # opt, fast, prof and perf all share the same cc flags
1321 for target in ['opt', 'fast', 'prof', 'perf']:
1322 ccflags[target] += ['-O3']
1324 print('Unknown compiler, please fix compiler options')
1328 # To speed things up, we only instantiate the build environments we
1329 # need. We try to identify the needed environment for each target; if
1330 # we can't, we fall back on instantiating all the environments just to
1332 target_types = ['debug', 'opt', 'fast', 'prof', 'perf']
1333 obj2target = {'do': 'debug', 'o': 'opt', 'fo': 'fast', 'po': 'prof',
1336 def identifyTarget(t):
1337 ext = t.split('.')[-1]
1338 if ext in target_types:
1340 if obj2target.has_key(ext):
1341 return obj2target[ext]
1342 match = re.search(r'/tests/([^/]+)/', t)
1343 if match and match.group(1) in target_types:
1344 return match.group(1)
1347 needed_envs = [identifyTarget(target) for target in BUILD_TARGETS]
1348 if 'all' in needed_envs:
1349 needed_envs += target_types
1351 disable_partial = False
1352 if env['PLATFORM'] == 'darwin':
1353 # Up until Apple LLVM version 10.0.0 (clang-1000.11.45.5), partial
1354 # linked objects do not expose symbols that are marked with the
1355 # hidden visibility and consequently building gem5 on Mac OS
1356 # fails. As a workaround, we disable partial linking, however, we
1357 # may want to revisit in the future.
1358 disable_partial = True
1361 if 'debug' in needed_envs:
1362 makeEnv(env, 'debug', '.do',
1363 CCFLAGS = Split(ccflags['debug']),
1364 CPPDEFINES = ['DEBUG', 'TRACING_ON=1'],
1365 LINKFLAGS = Split(ldflags['debug']),
1366 disable_partial=disable_partial)
1369 if 'opt' in needed_envs:
1370 makeEnv(env, 'opt', '.o',
1371 CCFLAGS = Split(ccflags['opt']),
1372 CPPDEFINES = ['TRACING_ON=1'],
1373 LINKFLAGS = Split(ldflags['opt']),
1374 disable_partial=disable_partial)
1377 if 'fast' in needed_envs:
1378 disable_partial = disable_partial and \
1379 env.get('BROKEN_INCREMENTAL_LTO', False) and \
1380 GetOption('force_lto')
1381 makeEnv(env, 'fast', '.fo', strip = True,
1382 CCFLAGS = Split(ccflags['fast']),
1383 CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1384 LINKFLAGS = Split(ldflags['fast']),
1385 disable_partial=disable_partial)
1387 # Profiled binary using gprof
1388 if 'prof' in needed_envs:
1389 makeEnv(env, 'prof', '.po',
1390 CCFLAGS = Split(ccflags['prof']),
1391 CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1392 LINKFLAGS = Split(ldflags['prof']),
1393 disable_partial=disable_partial)
1395 # Profiled binary using google-pprof
1396 if 'perf' in needed_envs:
1397 makeEnv(env, 'perf', '.gpo',
1398 CCFLAGS = Split(ccflags['perf']),
1399 CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1400 LINKFLAGS = Split(ldflags['perf']),
1401 disable_partial=disable_partial)