SConscript revision 13630
12315SN/A# -*- mode:python -*-
22332SN/A
32315SN/A# Copyright (c) 2018 ARM Limited
42315SN/A#
52315SN/A# The license below extends only to copyright in the software and shall
62315SN/A# not be construed as granting a license to any other intellectual
72315SN/A# property including but not limited to intellectual property relating
82315SN/A# to a hardware implementation of the functionality of the software
92315SN/A# licensed hereunder.  You may use the software subject to the license
102315SN/A# terms below provided that you ensure that this notice is replicated
112315SN/A# unmodified and in its entirety in all distributions of the software,
122315SN/A# modified or unmodified, in source code or in binary form.
132315SN/A#
142315SN/A# Copyright (c) 2004-2005 The Regents of The University of Michigan
152315SN/A# All rights reserved.
162315SN/A#
172315SN/A# Redistribution and use in source and binary forms, with or without
182315SN/A# modification, are permitted provided that the following conditions are
192315SN/A# met: redistributions of source code must retain the above copyright
202315SN/A# notice, this list of conditions and the following disclaimer;
212315SN/A# redistributions in binary form must reproduce the above copyright
222315SN/A# notice, this list of conditions and the following disclaimer in the
232315SN/A# documentation and/or other materials provided with the distribution;
242315SN/A# neither the name of the copyright holders nor the names of its
252315SN/A# contributors may be used to endorse or promote products derived from
262315SN/A# this software without specific prior written permission.
272689Sktlim@umich.edu#
282689Sktlim@umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
292315SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
302315SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
312315SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
322315SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
332315SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
342315SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
352315SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
362315SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
372315SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
382669Sktlim@umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
392315SN/A#
402315SN/A# Authors: Nathan Binkert
412315SN/A
422315SN/Afrom __future__ import print_function
432683Sktlim@umich.edu
442315SN/Aimport array
452315SN/Aimport bisect
462315SN/Aimport functools
472315SN/Aimport imp
482315SN/Aimport marshal
492315SN/Aimport os
502315SN/Aimport re
512315SN/Aimport subprocess
522315SN/Aimport sys
532315SN/Aimport zlib
542315SN/A
552315SN/Afrom os.path import basename, dirname, exists, isdir, isfile, join as joinpath
562315SN/A
572315SN/Aimport SCons
582315SN/A
592315SN/Afrom gem5_scons import Transform
602315SN/A
612315SN/A# This file defines how to build a particular configuration of gem5
622315SN/A# based on variable settings in the 'env' build environment.
632315SN/A
642315SN/AImport('*')
652680Sktlim@umich.edu
662315SN/A# Children need to see the environment
672315SN/AExport('env')
682669Sktlim@umich.edu
692315SN/Abuild_env = [(opt, env[opt]) for opt in export_vars]
702350SN/A
712350SN/Afrom m5.util import code_formatter, compareVersions
722350SN/A
732350SN/A########################################################################
742350SN/A# Code for adding source files of various types
752350SN/A#
762350SN/A# When specifying a source file of some type, a set of tags can be
772350SN/A# specified for that file.
782350SN/A
792350SN/Aclass SourceFilter(object):
802680Sktlim@umich.edu    def __init__(self, predicate):
812683Sktlim@umich.edu        self.predicate = predicate
822680Sktlim@umich.edu
832350SN/A    def __or__(self, other):
842680Sktlim@umich.edu        return SourceFilter(lambda tags: self.predicate(tags) or
852350SN/A                                         other.predicate(tags))
862315SN/A
872315SN/A    def __and__(self, other):
882315SN/A        return SourceFilter(lambda tags: self.predicate(tags) and
892315SN/A                                         other.predicate(tags))
902669Sktlim@umich.edu
912669Sktlim@umich.edudef with_tags_that(predicate):
922315SN/A    '''Return a list of sources with tags that satisfy a predicate.'''
932315SN/A    return SourceFilter(predicate)
942315SN/A
952315SN/Adef with_any_tags(*tags):
962315SN/A    '''Return a list of sources with any of the supplied tags.'''
972315SN/A    return SourceFilter(lambda stags: len(set(tags) & stags) > 0)
982315SN/A
992315SN/Adef with_all_tags(*tags):
1002315SN/A    '''Return a list of sources with all of the supplied tags.'''
1012315SN/A    return SourceFilter(lambda stags: set(tags) <= stags)
1022315SN/A
1032315SN/Adef with_tag(tag):
1042315SN/A    '''Return a list of sources with the supplied tag.'''
1052732Sktlim@umich.edu    return SourceFilter(lambda stags: tag in stags)
1062315SN/A
1072315SN/Adef without_tags(*tags):
1082315SN/A    '''Return a list of sources without any of the supplied tags.'''
1092315SN/A    return SourceFilter(lambda stags: len(set(tags) & stags) == 0)
1102315SN/A
1112315SN/Adef without_tag(tag):
1122679Sktlim@umich.edu    '''Return a list of sources with the supplied tag.'''
1132679Sktlim@umich.edu    return SourceFilter(lambda stags: tag not in stags)
1142669Sktlim@umich.edu
1152315SN/Asource_filter_factories = {
1162669Sktlim@umich.edu    'with_tags_that': with_tags_that,
1172315SN/A    'with_any_tags': with_any_tags,
1182315SN/A    'with_all_tags': with_all_tags,
1192315SN/A    'with_tag': with_tag,
1202315SN/A    'without_tags': without_tags,
1212679Sktlim@umich.edu    'without_tag': without_tag,
1222679Sktlim@umich.edu}
1232679Sktlim@umich.edu
1242679Sktlim@umich.eduExport(source_filter_factories)
1252679Sktlim@umich.edu
1262679Sktlim@umich.educlass SourceList(list):
1272679Sktlim@umich.edu    def apply_filter(self, f):
1282679Sktlim@umich.edu        def match(source):
1292679Sktlim@umich.edu            return f.predicate(source.tags)
1302871Sktlim@umich.edu        return SourceList(filter(match, self))
1312871Sktlim@umich.edu
1322871Sktlim@umich.edu    def __getattr__(self, name):
1332871Sktlim@umich.edu        func = source_filter_factories.get(name, None)
1342871Sktlim@umich.edu        if not func:
1352871Sktlim@umich.edu            raise AttributeError
1362315SN/A
1372683Sktlim@umich.edu        @functools.wraps(func)
1382683Sktlim@umich.edu        def wrapper(*args, **kwargs):
1392315SN/A            return self.apply_filter(func(*args, **kwargs))
1402680Sktlim@umich.edu        return wrapper
1412315SN/A
1422315SN/Aclass SourceMeta(type):
1432315SN/A    '''Meta class for source files that keeps track of all files of a
1442315SN/A    particular type.'''
1452315SN/A    def __init__(cls, name, bases, dict):
1462315SN/A        super(SourceMeta, cls).__init__(name, bases, dict)
1472315SN/A        cls.all = SourceList()
1482315SN/A
1492315SN/Aclass SourceFile(object):
1502315SN/A    '''Base object that encapsulates the notion of a source file.
1512315SN/A    This includes, the source node, target node, various manipulations
1522315SN/A    of those.  A source file also specifies a set of tags which
1532315SN/A    describing arbitrary properties of the source file.'''
1542315SN/A    __metaclass__ = SourceMeta
1552315SN/A
1562315SN/A    static_objs = {}
1572315SN/A    shared_objs = {}
1582315SN/A
1592315SN/A    def __init__(self, source, tags=None, add_tags=None):
1602679Sktlim@umich.edu        if tags is None:
1612679Sktlim@umich.edu            tags='gem5 lib'
1622315SN/A        if isinstance(tags, basestring):
1632315SN/A            tags = set([tags])
1642315SN/A        if not isinstance(tags, set):
1652315SN/A            tags = set(tags)
1662315SN/A        self.tags = tags
1672315SN/A
1682315SN/A        if add_tags:
1692315SN/A            if isinstance(add_tags, basestring):
1702315SN/A                add_tags = set([add_tags])
1712315SN/A            if not isinstance(add_tags, set):
1722315SN/A                add_tags = set(add_tags)
1732315SN/A            self.tags |= add_tags
1742315SN/A
1752315SN/A        tnode = source
1762315SN/A        if not isinstance(source, SCons.Node.FS.File):
1772315SN/A            tnode = File(source)
1782315SN/A
1792315SN/A        self.tnode = tnode
1802315SN/A        self.snode = tnode.srcnode()
1812315SN/A
1822315SN/A        for base in type(self).__mro__:
1832315SN/A            if issubclass(base, SourceFile):
1842315SN/A                base.all.append(self)
1852315SN/A
1862315SN/A    def static(self, env):
1872315SN/A        key = (self.tnode, env['OBJSUFFIX'])
1882315SN/A        if not key in self.static_objs:
1892315SN/A            self.static_objs[key] = env.StaticObject(self.tnode)
1902315SN/A        return self.static_objs[key]
1912315SN/A
1922315SN/A    def shared(self, env):
1932315SN/A        key = (self.tnode, env['OBJSUFFIX'])
1942315SN/A        if not key in self.shared_objs:
1952315SN/A            self.shared_objs[key] = env.SharedObject(self.tnode)
1962315SN/A        return self.shared_objs[key]
1972315SN/A
1982315SN/A    @property
1992315SN/A    def filename(self):
2002315SN/A        return str(self.tnode)
2012315SN/A
2022315SN/A    @property
2032315SN/A    def dirname(self):
2042315SN/A        return dirname(self.filename)
2052315SN/A
2062315SN/A    @property
2072315SN/A    def basename(self):
2082315SN/A        return basename(self.filename)
2092315SN/A
2102315SN/A    @property
2112315SN/A    def extname(self):
2122315SN/A        index = self.basename.rfind('.')
2132315SN/A        if index <= 0:
2142315SN/A            # dot files aren't extensions
2152315SN/A            return self.basename, None
2162315SN/A
2172315SN/A        return self.basename[:index], self.basename[index+1:]
2182315SN/A
2192315SN/A    def __lt__(self, other): return self.filename < other.filename
2202315SN/A    def __le__(self, other): return self.filename <= other.filename
2212683Sktlim@umich.edu    def __gt__(self, other): return self.filename > other.filename
2222315SN/A    def __ge__(self, other): return self.filename >= other.filename
2232315SN/A    def __eq__(self, other): return self.filename == other.filename
2242669Sktlim@umich.edu    def __ne__(self, other): return self.filename != other.filename
2252315SN/A
2262315SN/Adef blobToCpp(data, symbol, cpp_code, hpp_code=None, namespace=None):
2272683Sktlim@umich.edu    '''
2282315SN/A    Convert bytes data into C++ .cpp and .hh uint8_t byte array
2292315SN/A    code containing that binary data.
2302669Sktlim@umich.edu
2312315SN/A    :param data: binary data to be converted to C++
2322315SN/A    :param symbol: name of the symbol
2332683Sktlim@umich.edu    :param cpp_code: append the generated cpp_code to this object
2342315SN/A    :param hpp_code: append the generated hpp_code to this object
2352315SN/A                     If None, ignore it. Otherwise, also include it
2362669Sktlim@umich.edu                     in the .cpp file.
2372315SN/A    :param namespace: namespace to put the symbol into. If None,
2382315SN/A                      don't put the symbols into any namespace.
2392683Sktlim@umich.edu    '''
2402669Sktlim@umich.edu    symbol_len_declaration = 'const std::size_t {}_len'.format(symbol)
2412669Sktlim@umich.edu    symbol_declaration = 'const std::uint8_t {}[]'.format(symbol)
2422669Sktlim@umich.edu    if hpp_code is not None:
2432669Sktlim@umich.edu        cpp_code('''\
2442669Sktlim@umich.edu#include "blobs/{}.hh"
2452683Sktlim@umich.edu'''.format(symbol))
2462315SN/A        hpp_code('''\
2472315SN/A#include <cstddef>
2482315SN/A#include <cstdint>
2492315SN/A''')
2502683Sktlim@umich.edu        if namespace is not None:
2512315SN/A            hpp_code('namespace {} {{'.format(namespace))
2522315SN/A        hpp_code('extern ' + symbol_len_declaration + ';')
2532315SN/A        hpp_code('extern ' + symbol_declaration + ';')
2542669Sktlim@umich.edu        if namespace is not None:
2552315SN/A            hpp_code('}')
2562315SN/A    if namespace is not None:
2572683Sktlim@umich.edu        cpp_code('namespace {} {{'.format(namespace))
2582669Sktlim@umich.edu    if hpp_code is not None:
2592669Sktlim@umich.edu        cpp_code(symbol_len_declaration + ' = {};'.format(len(data)))
2602669Sktlim@umich.edu    cpp_code(symbol_declaration + ' = {')
2612669Sktlim@umich.edu    cpp_code.indent()
2622669Sktlim@umich.edu    step = 16
2632669Sktlim@umich.edu    for i in xrange(0, len(data), step):
2642669Sktlim@umich.edu        x = array.array('B', data[i:i+step])
2652669Sktlim@umich.edu        cpp_code(''.join('%d,' % d for d in x))
2662669Sktlim@umich.edu    cpp_code.dedent()
2672669Sktlim@umich.edu    cpp_code('};')
2682669Sktlim@umich.edu    if namespace is not None:
2692669Sktlim@umich.edu        cpp_code('}')
2702669Sktlim@umich.edu
2712683Sktlim@umich.edudef Blob(blob_path, symbol):
2722315SN/A    '''
2732315SN/A    Embed an arbitrary blob into the gem5 executable,
2742315SN/A    and make it accessible to C++ as a byte array.
2752669Sktlim@umich.edu    '''
2762669Sktlim@umich.edu    blob_path = os.path.abspath(blob_path)
2772315SN/A    blob_out_dir = os.path.join(env['BUILDDIR'], 'blobs')
2782315SN/A    path_noext = joinpath(blob_out_dir, symbol)
2792683Sktlim@umich.edu    cpp_path = path_noext + '.cc'
2802669Sktlim@umich.edu    hpp_path = path_noext + '.hh'
2812315SN/A    def embedBlob(target, source, env):
2822315SN/A        data = file(str(source[0]), 'r').read()
2832669Sktlim@umich.edu        cpp_code = code_formatter()
2842315SN/A        hpp_code = code_formatter()
2852315SN/A        blobToCpp(data, symbol, cpp_code, hpp_code, namespace='Blobs')
2862683Sktlim@umich.edu        cpp_path = str(target[0])
2872315SN/A        hpp_path = str(target[1])
2882315SN/A        cpp_dir = os.path.split(cpp_path)[0]
2892315SN/A        if not os.path.exists(cpp_dir):
2902683Sktlim@umich.edu            os.makedirs(cpp_dir)
2912669Sktlim@umich.edu        cpp_code.write(cpp_path)
2922683Sktlim@umich.edu        hpp_code.write(hpp_path)
2932669Sktlim@umich.edu    env.Command([cpp_path, hpp_path], blob_path,
2942315SN/A                MakeAction(embedBlob, Transform("EMBED BLOB")))
2952683Sktlim@umich.edu    Source(cpp_path)
2962315SN/A
2972315SN/Adef GdbXml(xml_id, symbol):
2982315SN/A    Blob(joinpath(gdb_xml_dir, xml_id), symbol)
2992315SN/A
3002683Sktlim@umich.educlass Source(SourceFile):
3012315SN/A    ungrouped_tag = 'No link group'
3022315SN/A    source_groups = set()
3032315SN/A
3042315SN/A    _current_group_tag = ungrouped_tag
3052683Sktlim@umich.edu
3062315SN/A    @staticmethod
3072315SN/A    def link_group_tag(group):
3082315SN/A        return 'link group: %s' % group
3092315SN/A
3102315SN/A    @classmethod
3112315SN/A    def set_group(cls, group):
3122683Sktlim@umich.edu        new_tag = Source.link_group_tag(group)
3132315SN/A        Source._current_group_tag = new_tag
3142315SN/A        Source.source_groups.add(group)
3152315SN/A
3162315SN/A    def _add_link_group_tag(self):
3172315SN/A        self.tags.add(Source._current_group_tag)
3182683Sktlim@umich.edu
3192315SN/A    '''Add a c/c++ source file to the build'''
3202315SN/A    def __init__(self, source, tags=None, add_tags=None):
3212315SN/A        '''specify the source file, and any tags'''
3222315SN/A        super(Source, self).__init__(source, tags, add_tags)
3232315SN/A        self._add_link_group_tag()
3242669Sktlim@umich.edu
3252669Sktlim@umich.educlass PySource(SourceFile):
3262669Sktlim@umich.edu    '''Add a python source file to the named package'''
3272315SN/A    invalid_sym_char = re.compile('[^A-z0-9_]')
3282315SN/A    modules = {}
3292683Sktlim@umich.edu    tnodes = {}
3302683Sktlim@umich.edu    symnames = {}
3312683Sktlim@umich.edu
3322683Sktlim@umich.edu    def __init__(self, package, source, tags=None, add_tags=None):
3332690Sktlim@umich.edu        '''specify the python package, the source file, and any tags'''
3342683Sktlim@umich.edu        super(PySource, self).__init__(source, tags, add_tags)
3352315SN/A
3362315SN/A        modname,ext = self.extname
3372332SN/A        assert ext == 'py'
3382669Sktlim@umich.edu
3392315SN/A        if package:
3402315SN/A            path = package.split('.')
3412315SN/A        else:
3422315SN/A            path = []
3432315SN/A
3442732Sktlim@umich.edu        modpath = path[:]
3452315SN/A        if modname != '__init__':
3462732Sktlim@umich.edu            modpath += [ modname ]
3472669Sktlim@umich.edu        modpath = '.'.join(modpath)
3482315SN/A
3492732Sktlim@umich.edu        arcpath = path + [ self.basename ]
3502732Sktlim@umich.edu        abspath = self.snode.abspath
3512680Sktlim@umich.edu        if not exists(abspath):
3522683Sktlim@umich.edu            abspath = self.tnode.abspath
3532315SN/A
3542315SN/A        self.package = package
3552669Sktlim@umich.edu        self.modname = modname
3562679Sktlim@umich.edu        self.modpath = modpath
3572315SN/A        self.arcname = joinpath(*arcpath)
3582315SN/A        self.abspath = abspath
3592315SN/A        self.compiled = File(self.filename + 'c')
3602315SN/A        self.cpp = File(self.filename + '.cc')
3612315SN/A        self.symname = PySource.invalid_sym_char.sub('_', modpath)
3622315SN/A
3632732Sktlim@umich.edu        PySource.modules[modpath] = self
3642315SN/A        PySource.tnodes[self.tnode] = self
3652315SN/A        PySource.symnames[self.symname] = self
3662315SN/A
3672315SN/Aclass SimObject(PySource):
3682350SN/A    '''Add a SimObject python file as a python source object and add
3692350SN/A    it to a list of sim object modules'''
3702350SN/A
3712350SN/A    fixed = False
3722350SN/A    modnames = []
3732350SN/A
3742315SN/A    def __init__(self, source, tags=None, add_tags=None):
3752315SN/A        '''Specify the source file and any tags (automatically in
3762315SN/A        the m5.objects package)'''
3772315SN/A        super(SimObject, self).__init__('m5.objects', source, tags, add_tags)
3782315SN/A        if self.fixed:
3792315SN/A            raise AttributeError, "Too late to call SimObject now."
3802315SN/A
3812315SN/A        bisect.insort_right(SimObject.modnames, self.modname)
3822840Sktlim@umich.edu
3832315SN/Aclass ProtoBuf(SourceFile):
3842315SN/A    '''Add a Protocol Buffer to build'''
3852732Sktlim@umich.edu
3862315SN/A    def __init__(self, source, tags=None, add_tags=None):
3872315SN/A        '''Specify the source file, and any tags'''
3882315SN/A        super(ProtoBuf, self).__init__(source, tags, add_tags)
3892315SN/A
3902315SN/A        # Get the file name and the extension
3912732Sktlim@umich.edu        modname,ext = self.extname
3922732Sktlim@umich.edu        assert ext == 'proto'
3932732Sktlim@umich.edu
3942732Sktlim@umich.edu        # Currently, we stick to generating the C++ headers, so we
3952732Sktlim@umich.edu        # only need to track the source and header.
3962732Sktlim@umich.edu        self.cc_file = File(modname + '.pb.cc')
3972732Sktlim@umich.edu        self.hh_file = File(modname + '.pb.h')
3982732Sktlim@umich.edu
3992732Sktlim@umich.edu
4002732Sktlim@umich.eduexectuable_classes = []
4012732Sktlim@umich.educlass ExecutableMeta(type):
4022315SN/A    '''Meta class for Executables.'''
4032315SN/A    all = []
4042315SN/A
4052315SN/A    def __init__(cls, name, bases, d):
4062315SN/A        if not d.pop('abstract', False):
4072315SN/A            ExecutableMeta.all.append(cls)
408        super(ExecutableMeta, cls).__init__(name, bases, d)
409
410        cls.all = []
411
412class Executable(object):
413    '''Base class for creating an executable from sources.'''
414    __metaclass__ = ExecutableMeta
415
416    abstract = True
417
418    def __init__(self, target, *srcs_and_filts):
419        '''Specify the target name and any sources. Sources that are
420        not SourceFiles are evalued with Source().'''
421        super(Executable, self).__init__()
422        self.all.append(self)
423        self.target = target
424
425        isFilter = lambda arg: isinstance(arg, SourceFilter)
426        self.filters = filter(isFilter, srcs_and_filts)
427        sources = filter(lambda a: not isFilter(a), srcs_and_filts)
428
429        srcs = SourceList()
430        for src in sources:
431            if not isinstance(src, SourceFile):
432                src = Source(src, tags=[])
433            srcs.append(src)
434
435        self.sources = srcs
436        self.dir = Dir('.')
437
438    def path(self, env):
439        return self.dir.File(self.target + '.' + env['EXE_SUFFIX'])
440
441    def srcs_to_objs(self, env, sources):
442        return list([ s.static(env) for s in sources ])
443
444    @classmethod
445    def declare_all(cls, env):
446        return list([ instance.declare(env) for instance in cls.all ])
447
448    def declare(self, env, objs=None):
449        if objs is None:
450            objs = self.srcs_to_objs(env, self.sources)
451
452        if env['STRIP_EXES']:
453            stripped = self.path(env)
454            unstripped = env.File(str(stripped) + '.unstripped')
455            if sys.platform == 'sunos5':
456                cmd = 'cp $SOURCE $TARGET; strip $TARGET'
457            else:
458                cmd = 'strip $SOURCE -o $TARGET'
459            env.Program(unstripped, objs)
460            return env.Command(stripped, unstripped,
461                               MakeAction(cmd, Transform("STRIP")))
462        else:
463            return env.Program(self.path(env), objs)
464
465class UnitTest(Executable):
466    '''Create a UnitTest'''
467    def __init__(self, target, *srcs_and_filts, **kwargs):
468        super(UnitTest, self).__init__(target, *srcs_and_filts)
469
470        self.main = kwargs.get('main', False)
471
472    def declare(self, env):
473        sources = list(self.sources)
474        for f in self.filters:
475            sources = Source.all.apply_filter(f)
476        objs = self.srcs_to_objs(env, sources) + env['STATIC_OBJS']
477        if self.main:
478            objs += env['MAIN_OBJS']
479        return super(UnitTest, self).declare(env, objs)
480
481class GTest(Executable):
482    '''Create a unit test based on the google test framework.'''
483    all = []
484    def __init__(self, *srcs_and_filts, **kwargs):
485        super(GTest, self).__init__(*srcs_and_filts)
486
487        self.skip_lib = kwargs.pop('skip_lib', False)
488
489    @classmethod
490    def declare_all(cls, env):
491        env = env.Clone()
492        env.Append(LIBS=env['GTEST_LIBS'])
493        env.Append(CPPFLAGS=env['GTEST_CPPFLAGS'])
494        env['GTEST_LIB_SOURCES'] = Source.all.with_tag('gtest lib')
495        env['GTEST_OUT_DIR'] = \
496            Dir(env['BUILDDIR']).Dir('unittests.' + env['EXE_SUFFIX'])
497        return super(GTest, cls).declare_all(env)
498
499    def declare(self, env):
500        sources = list(self.sources)
501        if not self.skip_lib:
502            sources += env['GTEST_LIB_SOURCES']
503        for f in self.filters:
504            sources += Source.all.apply_filter(f)
505        objs = self.srcs_to_objs(env, sources)
506
507        binary = super(GTest, self).declare(env, objs)
508
509        out_dir = env['GTEST_OUT_DIR']
510        xml_file = out_dir.Dir(str(self.dir)).File(self.target + '.xml')
511        AlwaysBuild(env.Command(xml_file, binary,
512            "${SOURCES[0]} --gtest_output=xml:${TARGETS[0]}"))
513
514        return binary
515
516class Gem5(Executable):
517    '''Create a gem5 executable.'''
518
519    def __init__(self, target):
520        super(Gem5, self).__init__(target)
521
522    def declare(self, env):
523        objs = env['MAIN_OBJS'] + env['STATIC_OBJS']
524        return super(Gem5, self).declare(env, objs)
525
526
527# Children should have access
528Export('Blob')
529Export('GdbXml')
530Export('Source')
531Export('PySource')
532Export('SimObject')
533Export('ProtoBuf')
534Export('Executable')
535Export('UnitTest')
536Export('GTest')
537
538########################################################################
539#
540# Debug Flags
541#
542debug_flags = {}
543def DebugFlag(name, desc=None):
544    if name in debug_flags:
545        raise AttributeError, "Flag %s already specified" % name
546    debug_flags[name] = (name, (), desc)
547
548def CompoundFlag(name, flags, desc=None):
549    if name in debug_flags:
550        raise AttributeError, "Flag %s already specified" % name
551
552    compound = tuple(flags)
553    debug_flags[name] = (name, compound, desc)
554
555Export('DebugFlag')
556Export('CompoundFlag')
557
558########################################################################
559#
560# Set some compiler variables
561#
562
563# Include file paths are rooted in this directory.  SCons will
564# automatically expand '.' to refer to both the source directory and
565# the corresponding build directory to pick up generated include
566# files.
567env.Append(CPPPATH=Dir('.'))
568
569for extra_dir in extras_dir_list:
570    env.Append(CPPPATH=Dir(extra_dir))
571
572# Workaround for bug in SCons version > 0.97d20071212
573# Scons bug id: 2006 gem5 Bug id: 308
574for root, dirs, files in os.walk(base_dir, topdown=True):
575    Dir(root[len(base_dir) + 1:])
576
577########################################################################
578#
579# Walk the tree and execute all SConscripts in subdirectories
580#
581
582here = Dir('.').srcnode().abspath
583for root, dirs, files in os.walk(base_dir, topdown=True):
584    if root == here:
585        # we don't want to recurse back into this SConscript
586        continue
587
588    if 'SConscript' in files:
589        build_dir = joinpath(env['BUILDDIR'], root[len(base_dir) + 1:])
590        Source.set_group(build_dir)
591        SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
592
593for extra_dir in extras_dir_list:
594    prefix_len = len(dirname(extra_dir)) + 1
595
596    # Also add the corresponding build directory to pick up generated
597    # include files.
598    env.Append(CPPPATH=Dir(joinpath(env['BUILDDIR'], extra_dir[prefix_len:])))
599
600    for root, dirs, files in os.walk(extra_dir, topdown=True):
601        # if build lives in the extras directory, don't walk down it
602        if 'build' in dirs:
603            dirs.remove('build')
604
605        if 'SConscript' in files:
606            build_dir = joinpath(env['BUILDDIR'], root[prefix_len:])
607            SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
608
609for opt in export_vars:
610    env.ConfigFile(opt)
611
612def makeTheISA(source, target, env):
613    isas = [ src.get_contents() for src in source ]
614    target_isa = env['TARGET_ISA']
615    def define(isa):
616        return isa.upper() + '_ISA'
617
618    def namespace(isa):
619        return isa[0].upper() + isa[1:].lower() + 'ISA'
620
621
622    code = code_formatter()
623    code('''\
624#ifndef __CONFIG_THE_ISA_HH__
625#define __CONFIG_THE_ISA_HH__
626
627''')
628
629    # create defines for the preprocessing and compile-time determination
630    for i,isa in enumerate(isas):
631        code('#define $0 $1', define(isa), i + 1)
632    code()
633
634    # create an enum for any run-time determination of the ISA, we
635    # reuse the same name as the namespaces
636    code('enum class Arch {')
637    for i,isa in enumerate(isas):
638        if i + 1 == len(isas):
639            code('  $0 = $1', namespace(isa), define(isa))
640        else:
641            code('  $0 = $1,', namespace(isa), define(isa))
642    code('};')
643
644    code('''
645
646#define THE_ISA ${{define(target_isa)}}
647#define TheISA ${{namespace(target_isa)}}
648#define THE_ISA_STR "${{target_isa}}"
649
650#endif // __CONFIG_THE_ISA_HH__''')
651
652    code.write(str(target[0]))
653
654env.Command('config/the_isa.hh', map(Value, all_isa_list),
655            MakeAction(makeTheISA, Transform("CFG ISA", 0)))
656
657def makeTheGPUISA(source, target, env):
658    isas = [ src.get_contents() for src in source ]
659    target_gpu_isa = env['TARGET_GPU_ISA']
660    def define(isa):
661        return isa.upper() + '_ISA'
662
663    def namespace(isa):
664        return isa[0].upper() + isa[1:].lower() + 'ISA'
665
666
667    code = code_formatter()
668    code('''\
669#ifndef __CONFIG_THE_GPU_ISA_HH__
670#define __CONFIG_THE_GPU_ISA_HH__
671
672''')
673
674    # create defines for the preprocessing and compile-time determination
675    for i,isa in enumerate(isas):
676        code('#define $0 $1', define(isa), i + 1)
677    code()
678
679    # create an enum for any run-time determination of the ISA, we
680    # reuse the same name as the namespaces
681    code('enum class GPUArch {')
682    for i,isa in enumerate(isas):
683        if i + 1 == len(isas):
684            code('  $0 = $1', namespace(isa), define(isa))
685        else:
686            code('  $0 = $1,', namespace(isa), define(isa))
687    code('};')
688
689    code('''
690
691#define THE_GPU_ISA ${{define(target_gpu_isa)}}
692#define TheGpuISA ${{namespace(target_gpu_isa)}}
693#define THE_GPU_ISA_STR "${{target_gpu_isa}}"
694
695#endif // __CONFIG_THE_GPU_ISA_HH__''')
696
697    code.write(str(target[0]))
698
699env.Command('config/the_gpu_isa.hh', map(Value, all_gpu_isa_list),
700            MakeAction(makeTheGPUISA, Transform("CFG ISA", 0)))
701
702########################################################################
703#
704# Prevent any SimObjects from being added after this point, they
705# should all have been added in the SConscripts above
706#
707SimObject.fixed = True
708
709class DictImporter(object):
710    '''This importer takes a dictionary of arbitrary module names that
711    map to arbitrary filenames.'''
712    def __init__(self, modules):
713        self.modules = modules
714        self.installed = set()
715
716    def __del__(self):
717        self.unload()
718
719    def unload(self):
720        import sys
721        for module in self.installed:
722            del sys.modules[module]
723        self.installed = set()
724
725    def find_module(self, fullname, path):
726        if fullname == 'm5.defines':
727            return self
728
729        if fullname == 'm5.objects':
730            return self
731
732        if fullname.startswith('_m5'):
733            return None
734
735        source = self.modules.get(fullname, None)
736        if source is not None and fullname.startswith('m5.objects'):
737            return self
738
739        return None
740
741    def load_module(self, fullname):
742        mod = imp.new_module(fullname)
743        sys.modules[fullname] = mod
744        self.installed.add(fullname)
745
746        mod.__loader__ = self
747        if fullname == 'm5.objects':
748            mod.__path__ = fullname.split('.')
749            return mod
750
751        if fullname == 'm5.defines':
752            mod.__dict__['buildEnv'] = m5.util.SmartDict(build_env)
753            return mod
754
755        source = self.modules[fullname]
756        if source.modname == '__init__':
757            mod.__path__ = source.modpath
758        mod.__file__ = source.abspath
759
760        exec file(source.abspath, 'r') in mod.__dict__
761
762        return mod
763
764import m5.SimObject
765import m5.params
766from m5.util import code_formatter
767
768m5.SimObject.clear()
769m5.params.clear()
770
771# install the python importer so we can grab stuff from the source
772# tree itself.  We can't have SimObjects added after this point or
773# else we won't know about them for the rest of the stuff.
774importer = DictImporter(PySource.modules)
775sys.meta_path[0:0] = [ importer ]
776
777# import all sim objects so we can populate the all_objects list
778# make sure that we're working with a list, then let's sort it
779for modname in SimObject.modnames:
780    exec('from m5.objects import %s' % modname)
781
782# we need to unload all of the currently imported modules so that they
783# will be re-imported the next time the sconscript is run
784importer.unload()
785sys.meta_path.remove(importer)
786
787sim_objects = m5.SimObject.allClasses
788all_enums = m5.params.allEnums
789
790for name,obj in sorted(sim_objects.iteritems()):
791    for param in obj._params.local.values():
792        # load the ptype attribute now because it depends on the
793        # current version of SimObject.allClasses, but when scons
794        # actually uses the value, all versions of
795        # SimObject.allClasses will have been loaded
796        param.ptype
797
798########################################################################
799#
800# calculate extra dependencies
801#
802module_depends = ["m5", "m5.SimObject", "m5.params"]
803depends = [ PySource.modules[dep].snode for dep in module_depends ]
804depends.sort(key = lambda x: x.name)
805
806########################################################################
807#
808# Commands for the basic automatically generated python files
809#
810
811# Generate Python file containing a dict specifying the current
812# buildEnv flags.
813def makeDefinesPyFile(target, source, env):
814    build_env = source[0].get_contents()
815
816    code = code_formatter()
817    code("""
818import _m5.core
819import m5.util
820
821buildEnv = m5.util.SmartDict($build_env)
822
823compileDate = _m5.core.compileDate
824_globals = globals()
825for key,val in _m5.core.__dict__.iteritems():
826    if key.startswith('flag_'):
827        flag = key[5:]
828        _globals[flag] = val
829del _globals
830""")
831    code.write(target[0].abspath)
832
833defines_info = Value(build_env)
834# Generate a file with all of the compile options in it
835env.Command('python/m5/defines.py', defines_info,
836            MakeAction(makeDefinesPyFile, Transform("DEFINES", 0)))
837PySource('m5', 'python/m5/defines.py')
838
839# Generate python file containing info about the M5 source code
840def makeInfoPyFile(target, source, env):
841    code = code_formatter()
842    for src in source:
843        data = ''.join(file(src.srcnode().abspath, 'r').xreadlines())
844        code('$src = ${{repr(data)}}')
845    code.write(str(target[0]))
846
847# Generate a file that wraps the basic top level files
848env.Command('python/m5/info.py',
849            [ '#/COPYING', '#/LICENSE', '#/README', ],
850            MakeAction(makeInfoPyFile, Transform("INFO")))
851PySource('m5', 'python/m5/info.py')
852
853########################################################################
854#
855# Create all of the SimObject param headers and enum headers
856#
857
858def createSimObjectParamStruct(target, source, env):
859    assert len(target) == 1 and len(source) == 1
860
861    name = source[0].get_text_contents()
862    obj = sim_objects[name]
863
864    code = code_formatter()
865    obj.cxx_param_decl(code)
866    code.write(target[0].abspath)
867
868def createSimObjectCxxConfig(is_header):
869    def body(target, source, env):
870        assert len(target) == 1 and len(source) == 1
871
872        name = str(source[0].get_contents())
873        obj = sim_objects[name]
874
875        code = code_formatter()
876        obj.cxx_config_param_file(code, is_header)
877        code.write(target[0].abspath)
878    return body
879
880def createEnumStrings(target, source, env):
881    assert len(target) == 1 and len(source) == 2
882
883    name = source[0].get_text_contents()
884    use_python = source[1].read()
885    obj = all_enums[name]
886
887    code = code_formatter()
888    obj.cxx_def(code)
889    if use_python:
890        obj.pybind_def(code)
891    code.write(target[0].abspath)
892
893def createEnumDecls(target, source, env):
894    assert len(target) == 1 and len(source) == 1
895
896    name = source[0].get_text_contents()
897    obj = all_enums[name]
898
899    code = code_formatter()
900    obj.cxx_decl(code)
901    code.write(target[0].abspath)
902
903def createSimObjectPyBindWrapper(target, source, env):
904    name = source[0].get_text_contents()
905    obj = sim_objects[name]
906
907    code = code_formatter()
908    obj.pybind_decl(code)
909    code.write(target[0].abspath)
910
911# Generate all of the SimObject param C++ struct header files
912params_hh_files = []
913for name,simobj in sorted(sim_objects.iteritems()):
914    py_source = PySource.modules[simobj.__module__]
915    extra_deps = [ py_source.tnode ]
916
917    hh_file = File('params/%s.hh' % name)
918    params_hh_files.append(hh_file)
919    env.Command(hh_file, Value(name),
920                MakeAction(createSimObjectParamStruct, Transform("SO PARAM")))
921    env.Depends(hh_file, depends + extra_deps)
922
923# C++ parameter description files
924if GetOption('with_cxx_config'):
925    for name,simobj in sorted(sim_objects.iteritems()):
926        py_source = PySource.modules[simobj.__module__]
927        extra_deps = [ py_source.tnode ]
928
929        cxx_config_hh_file = File('cxx_config/%s.hh' % name)
930        cxx_config_cc_file = File('cxx_config/%s.cc' % name)
931        env.Command(cxx_config_hh_file, Value(name),
932                    MakeAction(createSimObjectCxxConfig(True),
933                    Transform("CXXCPRHH")))
934        env.Command(cxx_config_cc_file, Value(name),
935                    MakeAction(createSimObjectCxxConfig(False),
936                    Transform("CXXCPRCC")))
937        env.Depends(cxx_config_hh_file, depends + extra_deps +
938                    [File('params/%s.hh' % name), File('sim/cxx_config.hh')])
939        env.Depends(cxx_config_cc_file, depends + extra_deps +
940                    [cxx_config_hh_file])
941        Source(cxx_config_cc_file)
942
943    cxx_config_init_cc_file = File('cxx_config/init.cc')
944
945    def createCxxConfigInitCC(target, source, env):
946        assert len(target) == 1 and len(source) == 1
947
948        code = code_formatter()
949
950        for name,simobj in sorted(sim_objects.iteritems()):
951            if not hasattr(simobj, 'abstract') or not simobj.abstract:
952                code('#include "cxx_config/${name}.hh"')
953        code()
954        code('void cxxConfigInit()')
955        code('{')
956        code.indent()
957        for name,simobj in sorted(sim_objects.iteritems()):
958            not_abstract = not hasattr(simobj, 'abstract') or \
959                not simobj.abstract
960            if not_abstract and 'type' in simobj.__dict__:
961                code('cxx_config_directory["${name}"] = '
962                     '${name}CxxConfigParams::makeDirectoryEntry();')
963        code.dedent()
964        code('}')
965        code.write(target[0].abspath)
966
967    py_source = PySource.modules[simobj.__module__]
968    extra_deps = [ py_source.tnode ]
969    env.Command(cxx_config_init_cc_file, Value(name),
970        MakeAction(createCxxConfigInitCC, Transform("CXXCINIT")))
971    cxx_param_hh_files = ["cxx_config/%s.hh" % simobj
972        for name,simobj in sorted(sim_objects.iteritems())
973        if not hasattr(simobj, 'abstract') or not simobj.abstract]
974    Depends(cxx_config_init_cc_file, cxx_param_hh_files +
975            [File('sim/cxx_config.hh')])
976    Source(cxx_config_init_cc_file)
977
978# Generate all enum header files
979for name,enum in sorted(all_enums.iteritems()):
980    py_source = PySource.modules[enum.__module__]
981    extra_deps = [ py_source.tnode ]
982
983    cc_file = File('enums/%s.cc' % name)
984    env.Command(cc_file, [Value(name), Value(env['USE_PYTHON'])],
985                MakeAction(createEnumStrings, Transform("ENUM STR")))
986    env.Depends(cc_file, depends + extra_deps)
987    Source(cc_file)
988
989    hh_file = File('enums/%s.hh' % name)
990    env.Command(hh_file, Value(name),
991                MakeAction(createEnumDecls, Transform("ENUMDECL")))
992    env.Depends(hh_file, depends + extra_deps)
993
994# Generate SimObject Python bindings wrapper files
995if env['USE_PYTHON']:
996    for name,simobj in sorted(sim_objects.iteritems()):
997        py_source = PySource.modules[simobj.__module__]
998        extra_deps = [ py_source.tnode ]
999        cc_file = File('python/_m5/param_%s.cc' % name)
1000        env.Command(cc_file, Value(name),
1001                    MakeAction(createSimObjectPyBindWrapper,
1002                               Transform("SO PyBind")))
1003        env.Depends(cc_file, depends + extra_deps)
1004        Source(cc_file)
1005
1006# Build all protocol buffers if we have got protoc and protobuf available
1007if env['HAVE_PROTOBUF']:
1008    for proto in ProtoBuf.all:
1009        # Use both the source and header as the target, and the .proto
1010        # file as the source. When executing the protoc compiler, also
1011        # specify the proto_path to avoid having the generated files
1012        # include the path.
1013        env.Command([proto.cc_file, proto.hh_file], proto.tnode,
1014                    MakeAction('$PROTOC --cpp_out ${TARGET.dir} '
1015                               '--proto_path ${SOURCE.dir} $SOURCE',
1016                               Transform("PROTOC")))
1017
1018        # Add the C++ source file
1019        Source(proto.cc_file, tags=proto.tags)
1020elif ProtoBuf.all:
1021    print('Got protobuf to build, but lacks support!')
1022    Exit(1)
1023
1024#
1025# Handle debug flags
1026#
1027def makeDebugFlagCC(target, source, env):
1028    assert(len(target) == 1 and len(source) == 1)
1029
1030    code = code_formatter()
1031
1032    # delay definition of CompoundFlags until after all the definition
1033    # of all constituent SimpleFlags
1034    comp_code = code_formatter()
1035
1036    # file header
1037    code('''
1038/*
1039 * DO NOT EDIT THIS FILE! Automatically generated by SCons.
1040 */
1041
1042#include "base/debug.hh"
1043
1044namespace Debug {
1045
1046''')
1047
1048    for name, flag in sorted(source[0].read().iteritems()):
1049        n, compound, desc = flag
1050        assert n == name
1051
1052        if not compound:
1053            code('SimpleFlag $name("$name", "$desc");')
1054        else:
1055            comp_code('CompoundFlag $name("$name", "$desc",')
1056            comp_code.indent()
1057            last = len(compound) - 1
1058            for i,flag in enumerate(compound):
1059                if i != last:
1060                    comp_code('&$flag,')
1061                else:
1062                    comp_code('&$flag);')
1063            comp_code.dedent()
1064
1065    code.append(comp_code)
1066    code()
1067    code('} // namespace Debug')
1068
1069    code.write(str(target[0]))
1070
1071def makeDebugFlagHH(target, source, env):
1072    assert(len(target) == 1 and len(source) == 1)
1073
1074    val = eval(source[0].get_contents())
1075    name, compound, desc = val
1076
1077    code = code_formatter()
1078
1079    # file header boilerplate
1080    code('''\
1081/*
1082 * DO NOT EDIT THIS FILE! Automatically generated by SCons.
1083 */
1084
1085#ifndef __DEBUG_${name}_HH__
1086#define __DEBUG_${name}_HH__
1087
1088namespace Debug {
1089''')
1090
1091    if compound:
1092        code('class CompoundFlag;')
1093    code('class SimpleFlag;')
1094
1095    if compound:
1096        code('extern CompoundFlag $name;')
1097        for flag in compound:
1098            code('extern SimpleFlag $flag;')
1099    else:
1100        code('extern SimpleFlag $name;')
1101
1102    code('''
1103}
1104
1105#endif // __DEBUG_${name}_HH__
1106''')
1107
1108    code.write(str(target[0]))
1109
1110for name,flag in sorted(debug_flags.iteritems()):
1111    n, compound, desc = flag
1112    assert n == name
1113
1114    hh_file = 'debug/%s.hh' % name
1115    env.Command(hh_file, Value(flag),
1116                MakeAction(makeDebugFlagHH, Transform("TRACING", 0)))
1117
1118env.Command('debug/flags.cc', Value(debug_flags),
1119            MakeAction(makeDebugFlagCC, Transform("TRACING", 0)))
1120Source('debug/flags.cc')
1121
1122# version tags
1123tags = \
1124env.Command('sim/tags.cc', None,
1125            MakeAction('util/cpt_upgrader.py --get-cc-file > $TARGET',
1126                       Transform("VER TAGS")))
1127env.AlwaysBuild(tags)
1128
1129# Embed python files.  All .py files that have been indicated by a
1130# PySource() call in a SConscript need to be embedded into the M5
1131# library.  To do that, we compile the file to byte code, marshal the
1132# byte code, compress it, and then generate a c++ file that
1133# inserts the result into an array.
1134def embedPyFile(target, source, env):
1135    def c_str(string):
1136        if string is None:
1137            return "0"
1138        return '"%s"' % string
1139
1140    '''Action function to compile a .py into a code object, marshal
1141    it, compress it, and stick it into an asm file so the code appears
1142    as just bytes with a label in the data section'''
1143
1144    src = file(str(source[0]), 'r').read()
1145
1146    pysource = PySource.tnodes[source[0]]
1147    compiled = compile(src, pysource.abspath, 'exec')
1148    marshalled = marshal.dumps(compiled)
1149    compressed = zlib.compress(marshalled)
1150    data = compressed
1151    sym = pysource.symname
1152
1153    code = code_formatter()
1154    code('''\
1155#include "sim/init.hh"
1156
1157namespace {
1158
1159''')
1160    blobToCpp(data, 'data_' + sym, code)
1161    code('''\
1162
1163
1164EmbeddedPython embedded_${sym}(
1165    ${{c_str(pysource.arcname)}},
1166    ${{c_str(pysource.abspath)}},
1167    ${{c_str(pysource.modpath)}},
1168    data_${sym},
1169    ${{len(data)}},
1170    ${{len(marshalled)}});
1171
1172} // anonymous namespace
1173''')
1174    code.write(str(target[0]))
1175
1176for source in PySource.all:
1177    env.Command(source.cpp, source.tnode,
1178                MakeAction(embedPyFile, Transform("EMBED PY")))
1179    Source(source.cpp, tags=source.tags, add_tags='python')
1180
1181########################################################################
1182#
1183# Define binaries.  Each different build type (debug, opt, etc.) gets
1184# a slightly different build environment.
1185#
1186
1187# List of constructed environments to pass back to SConstruct
1188date_source = Source('base/date.cc', tags=[])
1189
1190gem5_binary = Gem5('gem5')
1191
1192# Function to create a new build environment as clone of current
1193# environment 'env' with modified object suffix and optional stripped
1194# binary.  Additional keyword arguments are appended to corresponding
1195# build environment vars.
1196def makeEnv(env, label, objsfx, strip=False, disable_partial=False, **kwargs):
1197    # SCons doesn't know to append a library suffix when there is a '.' in the
1198    # name.  Use '_' instead.
1199    libname = 'gem5_' + label
1200    secondary_exename = 'm5.' + label
1201
1202    new_env = env.Clone(OBJSUFFIX=objsfx, SHOBJSUFFIX=objsfx + 's')
1203    new_env.Label = label
1204    new_env.Append(**kwargs)
1205
1206    lib_sources = Source.all.with_tag('gem5 lib')
1207
1208    # Without Python, leave out all Python content from the library
1209    # builds.  The option doesn't affect gem5 built as a program
1210    if GetOption('without_python'):
1211        lib_sources = lib_sources.without_tag('python')
1212
1213    static_objs = []
1214    shared_objs = []
1215
1216    for s in lib_sources.with_tag(Source.ungrouped_tag):
1217        static_objs.append(s.static(new_env))
1218        shared_objs.append(s.shared(new_env))
1219
1220    for group in Source.source_groups:
1221        srcs = lib_sources.with_tag(Source.link_group_tag(group))
1222        if not srcs:
1223            continue
1224
1225        group_static = [ s.static(new_env) for s in srcs ]
1226        group_shared = [ s.shared(new_env) for s in srcs ]
1227
1228        # If partial linking is disabled, add these sources to the build
1229        # directly, and short circuit this loop.
1230        if disable_partial:
1231            static_objs.extend(group_static)
1232            shared_objs.extend(group_shared)
1233            continue
1234
1235        # Set up the static partially linked objects.
1236        file_name = new_env.subst("${OBJPREFIX}lib${OBJSUFFIX}.partial")
1237        target = File(joinpath(group, file_name))
1238        partial = env.PartialStatic(target=target, source=group_static)
1239        static_objs.extend(partial)
1240
1241        # Set up the shared partially linked objects.
1242        file_name = new_env.subst("${SHOBJPREFIX}lib${SHOBJSUFFIX}.partial")
1243        target = File(joinpath(group, file_name))
1244        partial = env.PartialShared(target=target, source=group_shared)
1245        shared_objs.extend(partial)
1246
1247    static_date = date_source.static(new_env)
1248    new_env.Depends(static_date, static_objs)
1249    static_objs.extend(static_date)
1250
1251    shared_date = date_source.shared(new_env)
1252    new_env.Depends(shared_date, shared_objs)
1253    shared_objs.extend(shared_date)
1254
1255    main_objs = [ s.static(new_env) for s in Source.all.with_tag('main') ]
1256
1257    # First make a library of everything but main() so other programs can
1258    # link against m5.
1259    static_lib = new_env.StaticLibrary(libname, static_objs)
1260    shared_lib = new_env.SharedLibrary(libname, shared_objs)
1261
1262    # Keep track of the object files generated so far so Executables can
1263    # include them.
1264    new_env['STATIC_OBJS'] = static_objs
1265    new_env['SHARED_OBJS'] = shared_objs
1266    new_env['MAIN_OBJS'] = main_objs
1267
1268    new_env['STATIC_LIB'] = static_lib
1269    new_env['SHARED_LIB'] = shared_lib
1270
1271    # Record some settings for building Executables.
1272    new_env['EXE_SUFFIX'] = label
1273    new_env['STRIP_EXES'] = strip
1274
1275    for cls in ExecutableMeta.all:
1276        cls.declare_all(new_env)
1277
1278    new_env.M5Binary = File(gem5_binary.path(new_env))
1279
1280    new_env.Command(secondary_exename, new_env.M5Binary,
1281            MakeAction('ln $SOURCE $TARGET', Transform("HARDLINK")))
1282
1283    # Set up regression tests.
1284    SConscript(os.path.join(env.root.abspath, 'tests', 'SConscript'),
1285               variant_dir=Dir('tests').Dir(new_env.Label),
1286               exports={ 'env' : new_env }, duplicate=False)
1287
1288# Start out with the compiler flags common to all compilers,
1289# i.e. they all use -g for opt and -g -pg for prof
1290ccflags = {'debug' : [], 'opt' : ['-g'], 'fast' : [], 'prof' : ['-g', '-pg'],
1291           'perf' : ['-g']}
1292
1293# Start out with the linker flags common to all linkers, i.e. -pg for
1294# prof, and -lprofiler for perf. The -lprofile flag is surrounded by
1295# no-as-needed and as-needed as the binutils linker is too clever and
1296# simply doesn't link to the library otherwise.
1297ldflags = {'debug' : [], 'opt' : [], 'fast' : [], 'prof' : ['-pg'],
1298           'perf' : ['-Wl,--no-as-needed', '-lprofiler', '-Wl,--as-needed']}
1299
1300# For Link Time Optimization, the optimisation flags used to compile
1301# individual files are decoupled from those used at link time
1302# (i.e. you can compile with -O3 and perform LTO with -O0), so we need
1303# to also update the linker flags based on the target.
1304if env['GCC']:
1305    if sys.platform == 'sunos5':
1306        ccflags['debug'] += ['-gstabs+']
1307    else:
1308        ccflags['debug'] += ['-ggdb3']
1309    ldflags['debug'] += ['-O0']
1310    # opt, fast, prof and perf all share the same cc flags, also add
1311    # the optimization to the ldflags as LTO defers the optimization
1312    # to link time
1313    for target in ['opt', 'fast', 'prof', 'perf']:
1314        ccflags[target] += ['-O3']
1315        ldflags[target] += ['-O3']
1316
1317    ccflags['fast'] += env['LTO_CCFLAGS']
1318    ldflags['fast'] += env['LTO_LDFLAGS']
1319elif env['CLANG']:
1320    ccflags['debug'] += ['-g', '-O0']
1321    # opt, fast, prof and perf all share the same cc flags
1322    for target in ['opt', 'fast', 'prof', 'perf']:
1323        ccflags[target] += ['-O3']
1324else:
1325    print('Unknown compiler, please fix compiler options')
1326    Exit(1)
1327
1328
1329# To speed things up, we only instantiate the build environments we
1330# need.  We try to identify the needed environment for each target; if
1331# we can't, we fall back on instantiating all the environments just to
1332# be safe.
1333target_types = ['debug', 'opt', 'fast', 'prof', 'perf']
1334obj2target = {'do': 'debug', 'o': 'opt', 'fo': 'fast', 'po': 'prof',
1335              'gpo' : 'perf'}
1336
1337def identifyTarget(t):
1338    ext = t.split('.')[-1]
1339    if ext in target_types:
1340        return ext
1341    if obj2target.has_key(ext):
1342        return obj2target[ext]
1343    match = re.search(r'/tests/([^/]+)/', t)
1344    if match and match.group(1) in target_types:
1345        return match.group(1)
1346    return 'all'
1347
1348needed_envs = [identifyTarget(target) for target in BUILD_TARGETS]
1349if 'all' in needed_envs:
1350    needed_envs += target_types
1351
1352disable_partial = False
1353if env['PLATFORM'] == 'darwin':
1354    # Up until Apple LLVM version 10.0.0 (clang-1000.11.45.5), partial
1355    # linked objects do not expose symbols that are marked with the
1356    # hidden visibility and consequently building gem5 on Mac OS
1357    # fails. As a workaround, we disable partial linking, however, we
1358    # may want to revisit in the future.
1359    disable_partial = True
1360
1361# Debug binary
1362if 'debug' in needed_envs:
1363    makeEnv(env, 'debug', '.do',
1364            CCFLAGS = Split(ccflags['debug']),
1365            CPPDEFINES = ['DEBUG', 'TRACING_ON=1'],
1366            LINKFLAGS = Split(ldflags['debug']),
1367            disable_partial=disable_partial)
1368
1369# Optimized binary
1370if 'opt' in needed_envs:
1371    makeEnv(env, 'opt', '.o',
1372            CCFLAGS = Split(ccflags['opt']),
1373            CPPDEFINES = ['TRACING_ON=1'],
1374            LINKFLAGS = Split(ldflags['opt']),
1375            disable_partial=disable_partial)
1376
1377# "Fast" binary
1378if 'fast' in needed_envs:
1379    disable_partial = disable_partial and \
1380            env.get('BROKEN_INCREMENTAL_LTO', False) and \
1381            GetOption('force_lto')
1382    makeEnv(env, 'fast', '.fo', strip = True,
1383            CCFLAGS = Split(ccflags['fast']),
1384            CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1385            LINKFLAGS = Split(ldflags['fast']),
1386            disable_partial=disable_partial)
1387
1388# Profiled binary using gprof
1389if 'prof' in needed_envs:
1390    makeEnv(env, 'prof', '.po',
1391            CCFLAGS = Split(ccflags['prof']),
1392            CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1393            LINKFLAGS = Split(ldflags['prof']),
1394            disable_partial=disable_partial)
1395
1396# Profiled binary using google-pprof
1397if 'perf' in needed_envs:
1398    makeEnv(env, 'perf', '.gpo',
1399            CCFLAGS = Split(ccflags['perf']),
1400            CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1401            LINKFLAGS = Split(ldflags['perf']),
1402            disable_partial=disable_partial)
1403