SConscript revision 12389:cff04fd4bbc9
14561Sgblack@eecs.umich.edu# -*- mode:python -*-
24561Sgblack@eecs.umich.edu
34561Sgblack@eecs.umich.edu# Copyright (c) 2004-2005 The Regents of The University of Michigan
44561Sgblack@eecs.umich.edu# All rights reserved.
54561Sgblack@eecs.umich.edu#
64561Sgblack@eecs.umich.edu# Redistribution and use in source and binary forms, with or without
74561Sgblack@eecs.umich.edu# modification, are permitted provided that the following conditions are
84561Sgblack@eecs.umich.edu# met: redistributions of source code must retain the above copyright
94561Sgblack@eecs.umich.edu# notice, this list of conditions and the following disclaimer;
104561Sgblack@eecs.umich.edu# redistributions in binary form must reproduce the above copyright
114561Sgblack@eecs.umich.edu# notice, this list of conditions and the following disclaimer in the
124561Sgblack@eecs.umich.edu# documentation and/or other materials provided with the distribution;
134561Sgblack@eecs.umich.edu# neither the name of the copyright holders nor the names of its
144561Sgblack@eecs.umich.edu# contributors may be used to endorse or promote products derived from
154561Sgblack@eecs.umich.edu# this software without specific prior written permission.
164561Sgblack@eecs.umich.edu#
174561Sgblack@eecs.umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
184561Sgblack@eecs.umich.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
194561Sgblack@eecs.umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
204561Sgblack@eecs.umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
214561Sgblack@eecs.umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
224561Sgblack@eecs.umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
234561Sgblack@eecs.umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
244561Sgblack@eecs.umich.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
254561Sgblack@eecs.umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
264561Sgblack@eecs.umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
274561Sgblack@eecs.umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
284561Sgblack@eecs.umich.edu#
294561Sgblack@eecs.umich.edu# Authors: Nathan Binkert
304561Sgblack@eecs.umich.edu
314561Sgblack@eecs.umich.eduimport array
324561Sgblack@eecs.umich.eduimport bisect
334561Sgblack@eecs.umich.eduimport functools
344561Sgblack@eecs.umich.eduimport imp
354561Sgblack@eecs.umich.eduimport marshal
364561Sgblack@eecs.umich.eduimport os
374561Sgblack@eecs.umich.eduimport re
384561Sgblack@eecs.umich.eduimport subprocess
394561Sgblack@eecs.umich.eduimport sys
404561Sgblack@eecs.umich.eduimport zlib
414561Sgblack@eecs.umich.edu
424561Sgblack@eecs.umich.edufrom os.path import basename, dirname, exists, isdir, isfile, join as joinpath
434561Sgblack@eecs.umich.edu
444561Sgblack@eecs.umich.eduimport SCons
454561Sgblack@eecs.umich.edu
464561Sgblack@eecs.umich.edufrom gem5_scons import Transform
474561Sgblack@eecs.umich.edu
484561Sgblack@eecs.umich.edu# This file defines how to build a particular configuration of gem5
494561Sgblack@eecs.umich.edu# based on variable settings in the 'env' build environment.
504561Sgblack@eecs.umich.edu
514561Sgblack@eecs.umich.eduImport('*')
524561Sgblack@eecs.umich.edu
534561Sgblack@eecs.umich.edu# Children need to see the environment
544561Sgblack@eecs.umich.eduExport('env')
554561Sgblack@eecs.umich.edu
564561Sgblack@eecs.umich.edubuild_env = [(opt, env[opt]) for opt in export_vars]
574561Sgblack@eecs.umich.edu
584561Sgblack@eecs.umich.edufrom m5.util import code_formatter, compareVersions
594561Sgblack@eecs.umich.edu
604561Sgblack@eecs.umich.edu########################################################################
614561Sgblack@eecs.umich.edu# Code for adding source files of various types
624601Sgblack@eecs.umich.edu#
634601Sgblack@eecs.umich.edu# When specifying a source file of some type, a set of tags can be
644601Sgblack@eecs.umich.edu# specified for that file.
654601Sgblack@eecs.umich.edu
664601Sgblack@eecs.umich.educlass SourceFilter(object):
674601Sgblack@eecs.umich.edu    def __init__(self, predicate):
684601Sgblack@eecs.umich.edu        self.predicate = predicate
694601Sgblack@eecs.umich.edu
704601Sgblack@eecs.umich.edu    def __or__(self, other):
714601Sgblack@eecs.umich.edu        return SourceFilter(lambda tags: self.predicate(tags) or
724601Sgblack@eecs.umich.edu                                         other.predicate(tags))
734601Sgblack@eecs.umich.edu
744601Sgblack@eecs.umich.edu    def __and__(self, other):
754601Sgblack@eecs.umich.edu        return SourceFilter(lambda tags: self.predicate(tags) and
764601Sgblack@eecs.umich.edu                                         other.predicate(tags))
774601Sgblack@eecs.umich.edu
784601Sgblack@eecs.umich.edudef with_tags_that(predicate):
794601Sgblack@eecs.umich.edu    '''Return a list of sources with tags that satisfy a predicate.'''
804601Sgblack@eecs.umich.edu    return SourceFilter(predicate)
814601Sgblack@eecs.umich.edu
824601Sgblack@eecs.umich.edudef with_any_tags(*tags):
834601Sgblack@eecs.umich.edu    '''Return a list of sources with any of the supplied tags.'''
844601Sgblack@eecs.umich.edu    return SourceFilter(lambda stags: len(set(tags) & stags) > 0)
854601Sgblack@eecs.umich.edu
864601Sgblack@eecs.umich.edudef with_all_tags(*tags):
874601Sgblack@eecs.umich.edu    '''Return a list of sources with all of the supplied tags.'''
884601Sgblack@eecs.umich.edu    return SourceFilter(lambda stags: set(tags) <= stags)
894601Sgblack@eecs.umich.edu
904601Sgblack@eecs.umich.edudef with_tag(tag):
914601Sgblack@eecs.umich.edu    '''Return a list of sources with the supplied tag.'''
924601Sgblack@eecs.umich.edu    return SourceFilter(lambda stags: tag in stags)
934601Sgblack@eecs.umich.edu
944601Sgblack@eecs.umich.edudef without_tags(*tags):
954601Sgblack@eecs.umich.edu    '''Return a list of sources without any of the supplied tags.'''
964601Sgblack@eecs.umich.edu    return SourceFilter(lambda stags: len(set(tags) & stags) == 0)
974601Sgblack@eecs.umich.edu
984601Sgblack@eecs.umich.edudef without_tag(tag):
994601Sgblack@eecs.umich.edu    '''Return a list of sources with the supplied tag.'''
1004601Sgblack@eecs.umich.edu    return SourceFilter(lambda stags: tag not in stags)
1014601Sgblack@eecs.umich.edu
1024601Sgblack@eecs.umich.edusource_filter_factories = {
1034601Sgblack@eecs.umich.edu    'with_tags_that': with_tags_that,
1044601Sgblack@eecs.umich.edu    'with_any_tags': with_any_tags,
1054601Sgblack@eecs.umich.edu    'with_all_tags': with_all_tags,
1064601Sgblack@eecs.umich.edu    'with_tag': with_tag,
1074601Sgblack@eecs.umich.edu    'without_tags': without_tags,
1084601Sgblack@eecs.umich.edu    'without_tag': without_tag,
1094601Sgblack@eecs.umich.edu}
1104601Sgblack@eecs.umich.edu
1114601Sgblack@eecs.umich.eduExport(source_filter_factories)
1124601Sgblack@eecs.umich.edu
1134601Sgblack@eecs.umich.educlass SourceList(list):
1144587Sgblack@eecs.umich.edu    def apply_filter(self, f):
1154587Sgblack@eecs.umich.edu        def match(source):
1164587Sgblack@eecs.umich.edu            return f.predicate(source.tags)
1174587Sgblack@eecs.umich.edu        return SourceList(filter(match, self))
1184587Sgblack@eecs.umich.edu
1194587Sgblack@eecs.umich.edu    def __getattr__(self, name):
1204587Sgblack@eecs.umich.edu        func = source_filter_factories.get(name, None)
1214587Sgblack@eecs.umich.edu        if not func:
1224587Sgblack@eecs.umich.edu            raise AttributeError
1234587Sgblack@eecs.umich.edu
1244587Sgblack@eecs.umich.edu        @functools.wraps(func)
1254587Sgblack@eecs.umich.edu        def wrapper(*args, **kwargs):
1265118Sgblack@eecs.umich.edu            return self.apply_filter(func(*args, **kwargs))
1274720Sgblack@eecs.umich.edu        return wrapper
1284587Sgblack@eecs.umich.edu
1294587Sgblack@eecs.umich.educlass SourceMeta(type):
1304587Sgblack@eecs.umich.edu    '''Meta class for source files that keeps track of all files of a
1314587Sgblack@eecs.umich.edu    particular type.'''
1324587Sgblack@eecs.umich.edu    def __init__(cls, name, bases, dict):
1334587Sgblack@eecs.umich.edu        super(SourceMeta, cls).__init__(name, bases, dict)
1344587Sgblack@eecs.umich.edu        cls.all = SourceList()
1354587Sgblack@eecs.umich.edu
1364587Sgblack@eecs.umich.educlass SourceFile(object):
1374587Sgblack@eecs.umich.edu    '''Base object that encapsulates the notion of a source file.
1384587Sgblack@eecs.umich.edu    This includes, the source node, target node, various manipulations
1394587Sgblack@eecs.umich.edu    of those.  A source file also specifies a set of tags which
1404587Sgblack@eecs.umich.edu    describing arbitrary properties of the source file.'''
1414587Sgblack@eecs.umich.edu    __metaclass__ = SourceMeta
1424587Sgblack@eecs.umich.edu
1434587Sgblack@eecs.umich.edu    static_objs = {}
1444587Sgblack@eecs.umich.edu    shared_objs = {}
1454587Sgblack@eecs.umich.edu
1464587Sgblack@eecs.umich.edu    def __init__(self, source, tags=None, add_tags=None):
1474587Sgblack@eecs.umich.edu        if tags is None:
1484587Sgblack@eecs.umich.edu            tags='gem5 lib'
1494587Sgblack@eecs.umich.edu        if isinstance(tags, basestring):
1504587Sgblack@eecs.umich.edu            tags = set([tags])
1514587Sgblack@eecs.umich.edu        if not isinstance(tags, set):
1524587Sgblack@eecs.umich.edu            tags = set(tags)
1535118Sgblack@eecs.umich.edu        self.tags = tags
1544587Sgblack@eecs.umich.edu
1554587Sgblack@eecs.umich.edu        if add_tags:
1564587Sgblack@eecs.umich.edu            if isinstance(add_tags, basestring):
1574587Sgblack@eecs.umich.edu                add_tags = set([add_tags])
1584587Sgblack@eecs.umich.edu            if not isinstance(add_tags, set):
1594587Sgblack@eecs.umich.edu                add_tags = set(add_tags)
1604587Sgblack@eecs.umich.edu            self.tags |= add_tags
1614587Sgblack@eecs.umich.edu
1624587Sgblack@eecs.umich.edu        tnode = source
1634587Sgblack@eecs.umich.edu        if not isinstance(source, SCons.Node.FS.File):
1644587Sgblack@eecs.umich.edu            tnode = File(source)
1654587Sgblack@eecs.umich.edu
1664587Sgblack@eecs.umich.edu        self.tnode = tnode
1674587Sgblack@eecs.umich.edu        self.snode = tnode.srcnode()
1684587Sgblack@eecs.umich.edu
1695002Sgblack@eecs.umich.edu        for base in type(self).__mro__:
1705002Sgblack@eecs.umich.edu            if issubclass(base, SourceFile):
1714587Sgblack@eecs.umich.edu                base.all.append(self)
1724587Sgblack@eecs.umich.edu
1734587Sgblack@eecs.umich.edu    def static(self, env):
1744587Sgblack@eecs.umich.edu        key = (self.tnode, env['OBJSUFFIX'])
1754587Sgblack@eecs.umich.edu        if not key in self.static_objs:
1764587Sgblack@eecs.umich.edu            self.static_objs[key] = env.StaticObject(self.tnode)
1774587Sgblack@eecs.umich.edu        return self.static_objs[key]
1784587Sgblack@eecs.umich.edu
1794587Sgblack@eecs.umich.edu    def shared(self, env):
1804587Sgblack@eecs.umich.edu        key = (self.tnode, env['OBJSUFFIX'])
1814587Sgblack@eecs.umich.edu        if not key in self.shared_objs:
1824587Sgblack@eecs.umich.edu            self.shared_objs[key] = env.SharedObject(self.tnode)
1834587Sgblack@eecs.umich.edu        return self.shared_objs[key]
1844587Sgblack@eecs.umich.edu
1854587Sgblack@eecs.umich.edu    @property
1864587Sgblack@eecs.umich.edu    def filename(self):
1874587Sgblack@eecs.umich.edu        return str(self.tnode)
1884587Sgblack@eecs.umich.edu
1894587Sgblack@eecs.umich.edu    @property
1904587Sgblack@eecs.umich.edu    def dirname(self):
1914587Sgblack@eecs.umich.edu        return dirname(self.filename)
1924587Sgblack@eecs.umich.edu
1934587Sgblack@eecs.umich.edu    @property
1944587Sgblack@eecs.umich.edu    def basename(self):
1954587Sgblack@eecs.umich.edu        return basename(self.filename)
1964587Sgblack@eecs.umich.edu
1974587Sgblack@eecs.umich.edu    @property
1984587Sgblack@eecs.umich.edu    def extname(self):
1994587Sgblack@eecs.umich.edu        index = self.basename.rfind('.')
2005118Sgblack@eecs.umich.edu        if index <= 0:
2014767Sgblack@eecs.umich.edu            # dot files aren't extensions
2024720Sgblack@eecs.umich.edu            return self.basename, None
2034767Sgblack@eecs.umich.edu
2044720Sgblack@eecs.umich.edu        return self.basename[:index], self.basename[index+1:]
2054587Sgblack@eecs.umich.edu
2064587Sgblack@eecs.umich.edu    def __lt__(self, other): return self.filename < other.filename
2074587Sgblack@eecs.umich.edu    def __le__(self, other): return self.filename <= other.filename
2084587Sgblack@eecs.umich.edu    def __gt__(self, other): return self.filename > other.filename
2094587Sgblack@eecs.umich.edu    def __ge__(self, other): return self.filename >= other.filename
2104587Sgblack@eecs.umich.edu    def __eq__(self, other): return self.filename == other.filename
2114587Sgblack@eecs.umich.edu    def __ne__(self, other): return self.filename != other.filename
2124587Sgblack@eecs.umich.edu
2134587Sgblack@eecs.umich.educlass Source(SourceFile):
2144587Sgblack@eecs.umich.edu    ungrouped_tag = 'No link group'
2154587Sgblack@eecs.umich.edu    source_groups = set()
2164587Sgblack@eecs.umich.edu
2174587Sgblack@eecs.umich.edu    _current_group_tag = ungrouped_tag
2184587Sgblack@eecs.umich.edu
2194587Sgblack@eecs.umich.edu    @staticmethod
2204587Sgblack@eecs.umich.edu    def link_group_tag(group):
2214587Sgblack@eecs.umich.edu        return 'link group: %s' % group
2224587Sgblack@eecs.umich.edu
2234587Sgblack@eecs.umich.edu    @classmethod
2244587Sgblack@eecs.umich.edu    def set_group(cls, group):
2254587Sgblack@eecs.umich.edu        new_tag = Source.link_group_tag(group)
2264587Sgblack@eecs.umich.edu        Source._current_group_tag = new_tag
2275118Sgblack@eecs.umich.edu        Source.source_groups.add(group)
2284767Sgblack@eecs.umich.edu
2294720Sgblack@eecs.umich.edu    def _add_link_group_tag(self):
2304767Sgblack@eecs.umich.edu        self.tags.add(Source._current_group_tag)
2314720Sgblack@eecs.umich.edu
2324587Sgblack@eecs.umich.edu    '''Add a c/c++ source file to the build'''
2334587Sgblack@eecs.umich.edu    def __init__(self, source, tags=None, add_tags=None):
2344587Sgblack@eecs.umich.edu        '''specify the source file, and any tags'''
2354587Sgblack@eecs.umich.edu        super(Source, self).__init__(source, tags, add_tags)
2364587Sgblack@eecs.umich.edu        self._add_link_group_tag()
2374587Sgblack@eecs.umich.edu
2384587Sgblack@eecs.umich.educlass PySource(SourceFile):
2394587Sgblack@eecs.umich.edu    '''Add a python source file to the named package'''
2404587Sgblack@eecs.umich.edu    invalid_sym_char = re.compile('[^A-z0-9_]')
2414587Sgblack@eecs.umich.edu    modules = {}
2424587Sgblack@eecs.umich.edu    tnodes = {}
2434587Sgblack@eecs.umich.edu    symnames = {}
2444587Sgblack@eecs.umich.edu
2454587Sgblack@eecs.umich.edu    def __init__(self, package, source, tags=None, add_tags=None):
2464587Sgblack@eecs.umich.edu        '''specify the python package, the source file, and any tags'''
2474587Sgblack@eecs.umich.edu        super(PySource, self).__init__(source, tags, add_tags)
2484587Sgblack@eecs.umich.edu
2494587Sgblack@eecs.umich.edu        modname,ext = self.extname
2504587Sgblack@eecs.umich.edu        assert ext == 'py'
2514587Sgblack@eecs.umich.edu
2524587Sgblack@eecs.umich.edu        if package:
2534587Sgblack@eecs.umich.edu            path = package.split('.')
2544587Sgblack@eecs.umich.edu        else:
2554587Sgblack@eecs.umich.edu            path = []
2564587Sgblack@eecs.umich.edu
2574587Sgblack@eecs.umich.edu        modpath = path[:]
2584587Sgblack@eecs.umich.edu        if modname != '__init__':
2594587Sgblack@eecs.umich.edu            modpath += [ modname ]
2604587Sgblack@eecs.umich.edu        modpath = '.'.join(modpath)
2614561Sgblack@eecs.umich.edu
2624561Sgblack@eecs.umich.edu        arcpath = path + [ self.basename ]
2634561Sgblack@eecs.umich.edu        abspath = self.snode.abspath
2644561Sgblack@eecs.umich.edu        if not exists(abspath):
2654561Sgblack@eecs.umich.edu            abspath = self.tnode.abspath
2664561Sgblack@eecs.umich.edu
2674561Sgblack@eecs.umich.edu        self.package = package
2684561Sgblack@eecs.umich.edu        self.modname = modname
2694561Sgblack@eecs.umich.edu        self.modpath = modpath
2704561Sgblack@eecs.umich.edu        self.arcname = joinpath(*arcpath)
2714561Sgblack@eecs.umich.edu        self.abspath = abspath
2724561Sgblack@eecs.umich.edu        self.compiled = File(self.filename + 'c')
2734561Sgblack@eecs.umich.edu        self.cpp = File(self.filename + '.cc')
2744561Sgblack@eecs.umich.edu        self.symname = PySource.invalid_sym_char.sub('_', modpath)
2754561Sgblack@eecs.umich.edu
2764561Sgblack@eecs.umich.edu        PySource.modules[modpath] = self
2774561Sgblack@eecs.umich.edu        PySource.tnodes[self.tnode] = self
2784561Sgblack@eecs.umich.edu        PySource.symnames[self.symname] = self
2794561Sgblack@eecs.umich.edu
2804587Sgblack@eecs.umich.educlass SimObject(PySource):
2814587Sgblack@eecs.umich.edu    '''Add a SimObject python file as a python source object and add
2824587Sgblack@eecs.umich.edu    it to a list of sim object modules'''
2834587Sgblack@eecs.umich.edu
2844561Sgblack@eecs.umich.edu    fixed = False
2854561Sgblack@eecs.umich.edu    modnames = []
2864561Sgblack@eecs.umich.edu
2874561Sgblack@eecs.umich.edu    def __init__(self, source, tags=None, add_tags=None):
2884561Sgblack@eecs.umich.edu        '''Specify the source file and any tags (automatically in
2894561Sgblack@eecs.umich.edu        the m5.objects package)'''
2904561Sgblack@eecs.umich.edu        super(SimObject, self).__init__('m5.objects', source, tags, add_tags)
2914561Sgblack@eecs.umich.edu        if self.fixed:
2924561Sgblack@eecs.umich.edu            raise AttributeError, "Too late to call SimObject now."
2934561Sgblack@eecs.umich.edu
2944561Sgblack@eecs.umich.edu        bisect.insort_right(SimObject.modnames, self.modname)
2954561Sgblack@eecs.umich.edu
2964561Sgblack@eecs.umich.educlass ProtoBuf(SourceFile):
2974561Sgblack@eecs.umich.edu    '''Add a Protocol Buffer to build'''
2984561Sgblack@eecs.umich.edu
2994561Sgblack@eecs.umich.edu    def __init__(self, source, tags=None, add_tags=None):
3004561Sgblack@eecs.umich.edu        '''Specify the source file, and any tags'''
3014587Sgblack@eecs.umich.edu        super(ProtoBuf, self).__init__(source, tags, add_tags)
3024587Sgblack@eecs.umich.edu
3034587Sgblack@eecs.umich.edu        # Get the file name and the extension
3044587Sgblack@eecs.umich.edu        modname,ext = self.extname
3054561Sgblack@eecs.umich.edu        assert ext == 'proto'
3064561Sgblack@eecs.umich.edu
3074561Sgblack@eecs.umich.edu        # Currently, we stick to generating the C++ headers, so we
3084561Sgblack@eecs.umich.edu        # only need to track the source and header.
3094561Sgblack@eecs.umich.edu        self.cc_file = File(modname + '.pb.cc')
3104561Sgblack@eecs.umich.edu        self.hh_file = File(modname + '.pb.h')
3114561Sgblack@eecs.umich.edu
3124561Sgblack@eecs.umich.educlass UnitTest(object):
3134587Sgblack@eecs.umich.edu    '''Create a UnitTest'''
3144587Sgblack@eecs.umich.edu
3154587Sgblack@eecs.umich.edu    all = []
3164561Sgblack@eecs.umich.edu    def __init__(self, target, *sources, **kwargs):
3174587Sgblack@eecs.umich.edu        '''Specify the target name and any sources.  Sources that are
3184587Sgblack@eecs.umich.edu        not SourceFiles are evalued with Source().  All files are
3194587Sgblack@eecs.umich.edu        tagged with the name of the UnitTest target.'''
3204587Sgblack@eecs.umich.edu
3214561Sgblack@eecs.umich.edu        srcs = SourceList()
3224561Sgblack@eecs.umich.edu        for src in sources:
3234561Sgblack@eecs.umich.edu            if not isinstance(src, SourceFile):
3244561Sgblack@eecs.umich.edu                src = Source(src, tags=str(target))
3254561Sgblack@eecs.umich.edu            srcs.append(src)
3264587Sgblack@eecs.umich.edu
3274587Sgblack@eecs.umich.edu        self.sources = srcs
3285149Sgblack@eecs.umich.edu        self.target = target
3294587Sgblack@eecs.umich.edu        self.main = kwargs.get('main', False)
3304587Sgblack@eecs.umich.edu        self.all.append(self)
3314587Sgblack@eecs.umich.edu
3324587Sgblack@eecs.umich.educlass GTest(UnitTest):
3334712Sgblack@eecs.umich.edu    '''Create a unit test based on the google test framework.'''
3345149Sgblack@eecs.umich.edu    all = []
3354587Sgblack@eecs.umich.edu    def __init__(self, *args, **kwargs):
3364587Sgblack@eecs.umich.edu        isFilter = lambda arg: isinstance(arg, SourceFilter)
3374587Sgblack@eecs.umich.edu        self.filters = filter(isFilter, args)
3384587Sgblack@eecs.umich.edu        args = filter(lambda a: not isFilter(a), args)
3394587Sgblack@eecs.umich.edu        super(GTest, self).__init__(*args, **kwargs)
3404587Sgblack@eecs.umich.edu        self.dir = Dir('.')
3414587Sgblack@eecs.umich.edu        self.skip_lib = kwargs.pop('skip_lib', False)
3424587Sgblack@eecs.umich.edu
3434587Sgblack@eecs.umich.edu# Children should have access
3444587Sgblack@eecs.umich.eduExport('Source')
3454587Sgblack@eecs.umich.eduExport('PySource')
3464587Sgblack@eecs.umich.eduExport('SimObject')
3474587Sgblack@eecs.umich.eduExport('ProtoBuf')
3484587Sgblack@eecs.umich.eduExport('UnitTest')
3494587Sgblack@eecs.umich.eduExport('GTest')
3504587Sgblack@eecs.umich.edu
3514587Sgblack@eecs.umich.edu########################################################################
3524587Sgblack@eecs.umich.edu#
3534587Sgblack@eecs.umich.edu# Debug Flags
3544587Sgblack@eecs.umich.edu#
3554587Sgblack@eecs.umich.edudebug_flags = {}
3564587Sgblack@eecs.umich.edudef DebugFlag(name, desc=None):
3574587Sgblack@eecs.umich.edu    if name in debug_flags:
3584587Sgblack@eecs.umich.edu        raise AttributeError, "Flag %s already specified" % name
3594863Sgblack@eecs.umich.edu    debug_flags[name] = (name, (), desc)
3604587Sgblack@eecs.umich.edu
3615118Sgblack@eecs.umich.edudef CompoundFlag(name, flags, desc=None):
3624587Sgblack@eecs.umich.edu    if name in debug_flags:
3634587Sgblack@eecs.umich.edu        raise AttributeError, "Flag %s already specified" % name
3644587Sgblack@eecs.umich.edu
3654587Sgblack@eecs.umich.edu    compound = tuple(flags)
3664587Sgblack@eecs.umich.edu    debug_flags[name] = (name, compound, desc)
3674587Sgblack@eecs.umich.edu
3684587Sgblack@eecs.umich.eduExport('DebugFlag')
3694587Sgblack@eecs.umich.eduExport('CompoundFlag')
3704679Sgblack@eecs.umich.edu
3715118Sgblack@eecs.umich.edu########################################################################
3725118Sgblack@eecs.umich.edu#
3735118Sgblack@eecs.umich.edu# Set some compiler variables
3744587Sgblack@eecs.umich.edu#
3754587Sgblack@eecs.umich.edu
3764587Sgblack@eecs.umich.edu# Include file paths are rooted in this directory.  SCons will
3774587Sgblack@eecs.umich.edu# automatically expand '.' to refer to both the source directory and
3784587Sgblack@eecs.umich.edu# the corresponding build directory to pick up generated include
3794587Sgblack@eecs.umich.edu# files.
3804587Sgblack@eecs.umich.eduenv.Append(CPPPATH=Dir('.'))
3815149Sgblack@eecs.umich.edu
3825149Sgblack@eecs.umich.edufor extra_dir in extras_dir_list:
3834712Sgblack@eecs.umich.edu    env.Append(CPPPATH=Dir(extra_dir))
3845149Sgblack@eecs.umich.edu
3854587Sgblack@eecs.umich.edu# Workaround for bug in SCons version > 0.97d20071212
3864587Sgblack@eecs.umich.edu# Scons bug id: 2006 gem5 Bug id: 308
3874587Sgblack@eecs.umich.edufor root, dirs, files in os.walk(base_dir, topdown=True):
3884587Sgblack@eecs.umich.edu    Dir(root[len(base_dir) + 1:])
3894587Sgblack@eecs.umich.edu
3904587Sgblack@eecs.umich.edu########################################################################
3915118Sgblack@eecs.umich.edu#
3925027Sgblack@eecs.umich.edu# Walk the tree and execute all SConscripts in subdirectories
3934587Sgblack@eecs.umich.edu#
3945118Sgblack@eecs.umich.edu
3954587Sgblack@eecs.umich.eduhere = Dir('.').srcnode().abspath
3964587Sgblack@eecs.umich.edufor root, dirs, files in os.walk(base_dir, topdown=True):
3974587Sgblack@eecs.umich.edu    if root == here:
3984587Sgblack@eecs.umich.edu        # we don't want to recurse back into this SConscript
3994587Sgblack@eecs.umich.edu        continue
4004587Sgblack@eecs.umich.edu
4014587Sgblack@eecs.umich.edu    if 'SConscript' in files:
4024587Sgblack@eecs.umich.edu        build_dir = joinpath(env['BUILDDIR'], root[len(base_dir) + 1:])
4034679Sgblack@eecs.umich.edu        Source.set_group(build_dir)
4045118Sgblack@eecs.umich.edu        SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
4055118Sgblack@eecs.umich.edu
4065118Sgblack@eecs.umich.edufor extra_dir in extras_dir_list:
4074587Sgblack@eecs.umich.edu    prefix_len = len(dirname(extra_dir)) + 1
4084587Sgblack@eecs.umich.edu
4094587Sgblack@eecs.umich.edu    # Also add the corresponding build directory to pick up generated
4104587Sgblack@eecs.umich.edu    # include files.
4114587Sgblack@eecs.umich.edu    env.Append(CPPPATH=Dir(joinpath(env['BUILDDIR'], extra_dir[prefix_len:])))
4124587Sgblack@eecs.umich.edu
4134587Sgblack@eecs.umich.edu    for root, dirs, files in os.walk(extra_dir, topdown=True):
4145149Sgblack@eecs.umich.edu        # if build lives in the extras directory, don't walk down it
4155149Sgblack@eecs.umich.edu        if 'build' in dirs:
4164712Sgblack@eecs.umich.edu            dirs.remove('build')
4175149Sgblack@eecs.umich.edu
4184587Sgblack@eecs.umich.edu        if 'SConscript' in files:
4194587Sgblack@eecs.umich.edu            build_dir = joinpath(env['BUILDDIR'], root[prefix_len:])
4204587Sgblack@eecs.umich.edu            SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
4214587Sgblack@eecs.umich.edu
4224587Sgblack@eecs.umich.edufor opt in export_vars:
4234706Sgblack@eecs.umich.edu    env.ConfigFile(opt)
4245027Sgblack@eecs.umich.edu
4254601Sgblack@eecs.umich.edudef makeTheISA(source, target, env):
4264679Sgblack@eecs.umich.edu    isas = [ src.get_contents() for src in source ]
4275118Sgblack@eecs.umich.edu    target_isa = env['TARGET_ISA']
4285118Sgblack@eecs.umich.edu    def define(isa):
4295118Sgblack@eecs.umich.edu        return isa.upper() + '_ISA'
4304601Sgblack@eecs.umich.edu
4314601Sgblack@eecs.umich.edu    def namespace(isa):
4324601Sgblack@eecs.umich.edu        return isa[0].upper() + isa[1:].lower() + 'ISA'
4334601Sgblack@eecs.umich.edu
4344601Sgblack@eecs.umich.edu
4355149Sgblack@eecs.umich.edu    code = code_formatter()
4365149Sgblack@eecs.umich.edu    code('''\
4374712Sgblack@eecs.umich.edu#ifndef __CONFIG_THE_ISA_HH__
4385149Sgblack@eecs.umich.edu#define __CONFIG_THE_ISA_HH__
4394601Sgblack@eecs.umich.edu
4404601Sgblack@eecs.umich.edu''')
4414601Sgblack@eecs.umich.edu
4424601Sgblack@eecs.umich.edu    # create defines for the preprocessing and compile-time determination
4434587Sgblack@eecs.umich.edu    for i,isa in enumerate(isas):
4444587Sgblack@eecs.umich.edu        code('#define $0 $1', define(isa), i + 1)
445    code()
446
447    # create an enum for any run-time determination of the ISA, we
448    # reuse the same name as the namespaces
449    code('enum class Arch {')
450    for i,isa in enumerate(isas):
451        if i + 1 == len(isas):
452            code('  $0 = $1', namespace(isa), define(isa))
453        else:
454            code('  $0 = $1,', namespace(isa), define(isa))
455    code('};')
456
457    code('''
458
459#define THE_ISA ${{define(target_isa)}}
460#define TheISA ${{namespace(target_isa)}}
461#define THE_ISA_STR "${{target_isa}}"
462
463#endif // __CONFIG_THE_ISA_HH__''')
464
465    code.write(str(target[0]))
466
467env.Command('config/the_isa.hh', map(Value, all_isa_list),
468            MakeAction(makeTheISA, Transform("CFG ISA", 0)))
469
470def makeTheGPUISA(source, target, env):
471    isas = [ src.get_contents() for src in source ]
472    target_gpu_isa = env['TARGET_GPU_ISA']
473    def define(isa):
474        return isa.upper() + '_ISA'
475
476    def namespace(isa):
477        return isa[0].upper() + isa[1:].lower() + 'ISA'
478
479
480    code = code_formatter()
481    code('''\
482#ifndef __CONFIG_THE_GPU_ISA_HH__
483#define __CONFIG_THE_GPU_ISA_HH__
484
485''')
486
487    # create defines for the preprocessing and compile-time determination
488    for i,isa in enumerate(isas):
489        code('#define $0 $1', define(isa), i + 1)
490    code()
491
492    # create an enum for any run-time determination of the ISA, we
493    # reuse the same name as the namespaces
494    code('enum class GPUArch {')
495    for i,isa in enumerate(isas):
496        if i + 1 == len(isas):
497            code('  $0 = $1', namespace(isa), define(isa))
498        else:
499            code('  $0 = $1,', namespace(isa), define(isa))
500    code('};')
501
502    code('''
503
504#define THE_GPU_ISA ${{define(target_gpu_isa)}}
505#define TheGpuISA ${{namespace(target_gpu_isa)}}
506#define THE_GPU_ISA_STR "${{target_gpu_isa}}"
507
508#endif // __CONFIG_THE_GPU_ISA_HH__''')
509
510    code.write(str(target[0]))
511
512env.Command('config/the_gpu_isa.hh', map(Value, all_gpu_isa_list),
513            MakeAction(makeTheGPUISA, Transform("CFG ISA", 0)))
514
515########################################################################
516#
517# Prevent any SimObjects from being added after this point, they
518# should all have been added in the SConscripts above
519#
520SimObject.fixed = True
521
522class DictImporter(object):
523    '''This importer takes a dictionary of arbitrary module names that
524    map to arbitrary filenames.'''
525    def __init__(self, modules):
526        self.modules = modules
527        self.installed = set()
528
529    def __del__(self):
530        self.unload()
531
532    def unload(self):
533        import sys
534        for module in self.installed:
535            del sys.modules[module]
536        self.installed = set()
537
538    def find_module(self, fullname, path):
539        if fullname == 'm5.defines':
540            return self
541
542        if fullname == 'm5.objects':
543            return self
544
545        if fullname.startswith('_m5'):
546            return None
547
548        source = self.modules.get(fullname, None)
549        if source is not None and fullname.startswith('m5.objects'):
550            return self
551
552        return None
553
554    def load_module(self, fullname):
555        mod = imp.new_module(fullname)
556        sys.modules[fullname] = mod
557        self.installed.add(fullname)
558
559        mod.__loader__ = self
560        if fullname == 'm5.objects':
561            mod.__path__ = fullname.split('.')
562            return mod
563
564        if fullname == 'm5.defines':
565            mod.__dict__['buildEnv'] = m5.util.SmartDict(build_env)
566            return mod
567
568        source = self.modules[fullname]
569        if source.modname == '__init__':
570            mod.__path__ = source.modpath
571        mod.__file__ = source.abspath
572
573        exec file(source.abspath, 'r') in mod.__dict__
574
575        return mod
576
577import m5.SimObject
578import m5.params
579from m5.util import code_formatter
580
581m5.SimObject.clear()
582m5.params.clear()
583
584# install the python importer so we can grab stuff from the source
585# tree itself.  We can't have SimObjects added after this point or
586# else we won't know about them for the rest of the stuff.
587importer = DictImporter(PySource.modules)
588sys.meta_path[0:0] = [ importer ]
589
590# import all sim objects so we can populate the all_objects list
591# make sure that we're working with a list, then let's sort it
592for modname in SimObject.modnames:
593    exec('from m5.objects import %s' % modname)
594
595# we need to unload all of the currently imported modules so that they
596# will be re-imported the next time the sconscript is run
597importer.unload()
598sys.meta_path.remove(importer)
599
600sim_objects = m5.SimObject.allClasses
601all_enums = m5.params.allEnums
602
603for name,obj in sorted(sim_objects.iteritems()):
604    for param in obj._params.local.values():
605        # load the ptype attribute now because it depends on the
606        # current version of SimObject.allClasses, but when scons
607        # actually uses the value, all versions of
608        # SimObject.allClasses will have been loaded
609        param.ptype
610
611########################################################################
612#
613# calculate extra dependencies
614#
615module_depends = ["m5", "m5.SimObject", "m5.params"]
616depends = [ PySource.modules[dep].snode for dep in module_depends ]
617depends.sort(key = lambda x: x.name)
618
619########################################################################
620#
621# Commands for the basic automatically generated python files
622#
623
624# Generate Python file containing a dict specifying the current
625# buildEnv flags.
626def makeDefinesPyFile(target, source, env):
627    build_env = source[0].get_contents()
628
629    code = code_formatter()
630    code("""
631import _m5.core
632import m5.util
633
634buildEnv = m5.util.SmartDict($build_env)
635
636compileDate = _m5.core.compileDate
637_globals = globals()
638for key,val in _m5.core.__dict__.iteritems():
639    if key.startswith('flag_'):
640        flag = key[5:]
641        _globals[flag] = val
642del _globals
643""")
644    code.write(target[0].abspath)
645
646defines_info = Value(build_env)
647# Generate a file with all of the compile options in it
648env.Command('python/m5/defines.py', defines_info,
649            MakeAction(makeDefinesPyFile, Transform("DEFINES", 0)))
650PySource('m5', 'python/m5/defines.py')
651
652# Generate python file containing info about the M5 source code
653def makeInfoPyFile(target, source, env):
654    code = code_formatter()
655    for src in source:
656        data = ''.join(file(src.srcnode().abspath, 'r').xreadlines())
657        code('$src = ${{repr(data)}}')
658    code.write(str(target[0]))
659
660# Generate a file that wraps the basic top level files
661env.Command('python/m5/info.py',
662            [ '#/COPYING', '#/LICENSE', '#/README', ],
663            MakeAction(makeInfoPyFile, Transform("INFO")))
664PySource('m5', 'python/m5/info.py')
665
666########################################################################
667#
668# Create all of the SimObject param headers and enum headers
669#
670
671def createSimObjectParamStruct(target, source, env):
672    assert len(target) == 1 and len(source) == 1
673
674    name = source[0].get_text_contents()
675    obj = sim_objects[name]
676
677    code = code_formatter()
678    obj.cxx_param_decl(code)
679    code.write(target[0].abspath)
680
681def createSimObjectCxxConfig(is_header):
682    def body(target, source, env):
683        assert len(target) == 1 and len(source) == 1
684
685        name = str(source[0].get_contents())
686        obj = sim_objects[name]
687
688        code = code_formatter()
689        obj.cxx_config_param_file(code, is_header)
690        code.write(target[0].abspath)
691    return body
692
693def createEnumStrings(target, source, env):
694    assert len(target) == 1 and len(source) == 2
695
696    name = source[0].get_text_contents()
697    use_python = source[1].read()
698    obj = all_enums[name]
699
700    code = code_formatter()
701    obj.cxx_def(code)
702    if use_python:
703        obj.pybind_def(code)
704    code.write(target[0].abspath)
705
706def createEnumDecls(target, source, env):
707    assert len(target) == 1 and len(source) == 1
708
709    name = source[0].get_text_contents()
710    obj = all_enums[name]
711
712    code = code_formatter()
713    obj.cxx_decl(code)
714    code.write(target[0].abspath)
715
716def createSimObjectPyBindWrapper(target, source, env):
717    name = source[0].get_text_contents()
718    obj = sim_objects[name]
719
720    code = code_formatter()
721    obj.pybind_decl(code)
722    code.write(target[0].abspath)
723
724# Generate all of the SimObject param C++ struct header files
725params_hh_files = []
726for name,simobj in sorted(sim_objects.iteritems()):
727    py_source = PySource.modules[simobj.__module__]
728    extra_deps = [ py_source.tnode ]
729
730    hh_file = File('params/%s.hh' % name)
731    params_hh_files.append(hh_file)
732    env.Command(hh_file, Value(name),
733                MakeAction(createSimObjectParamStruct, Transform("SO PARAM")))
734    env.Depends(hh_file, depends + extra_deps)
735
736# C++ parameter description files
737if GetOption('with_cxx_config'):
738    for name,simobj in sorted(sim_objects.iteritems()):
739        py_source = PySource.modules[simobj.__module__]
740        extra_deps = [ py_source.tnode ]
741
742        cxx_config_hh_file = File('cxx_config/%s.hh' % name)
743        cxx_config_cc_file = File('cxx_config/%s.cc' % name)
744        env.Command(cxx_config_hh_file, Value(name),
745                    MakeAction(createSimObjectCxxConfig(True),
746                    Transform("CXXCPRHH")))
747        env.Command(cxx_config_cc_file, Value(name),
748                    MakeAction(createSimObjectCxxConfig(False),
749                    Transform("CXXCPRCC")))
750        env.Depends(cxx_config_hh_file, depends + extra_deps +
751                    [File('params/%s.hh' % name), File('sim/cxx_config.hh')])
752        env.Depends(cxx_config_cc_file, depends + extra_deps +
753                    [cxx_config_hh_file])
754        Source(cxx_config_cc_file)
755
756    cxx_config_init_cc_file = File('cxx_config/init.cc')
757
758    def createCxxConfigInitCC(target, source, env):
759        assert len(target) == 1 and len(source) == 1
760
761        code = code_formatter()
762
763        for name,simobj in sorted(sim_objects.iteritems()):
764            if not hasattr(simobj, 'abstract') or not simobj.abstract:
765                code('#include "cxx_config/${name}.hh"')
766        code()
767        code('void cxxConfigInit()')
768        code('{')
769        code.indent()
770        for name,simobj in sorted(sim_objects.iteritems()):
771            not_abstract = not hasattr(simobj, 'abstract') or \
772                not simobj.abstract
773            if not_abstract and 'type' in simobj.__dict__:
774                code('cxx_config_directory["${name}"] = '
775                     '${name}CxxConfigParams::makeDirectoryEntry();')
776        code.dedent()
777        code('}')
778        code.write(target[0].abspath)
779
780    py_source = PySource.modules[simobj.__module__]
781    extra_deps = [ py_source.tnode ]
782    env.Command(cxx_config_init_cc_file, Value(name),
783        MakeAction(createCxxConfigInitCC, Transform("CXXCINIT")))
784    cxx_param_hh_files = ["cxx_config/%s.hh" % simobj
785        for name,simobj in sorted(sim_objects.iteritems())
786        if not hasattr(simobj, 'abstract') or not simobj.abstract]
787    Depends(cxx_config_init_cc_file, cxx_param_hh_files +
788            [File('sim/cxx_config.hh')])
789    Source(cxx_config_init_cc_file)
790
791# Generate all enum header files
792for name,enum in sorted(all_enums.iteritems()):
793    py_source = PySource.modules[enum.__module__]
794    extra_deps = [ py_source.tnode ]
795
796    cc_file = File('enums/%s.cc' % name)
797    env.Command(cc_file, [Value(name), Value(env['USE_PYTHON'])],
798                MakeAction(createEnumStrings, Transform("ENUM STR")))
799    env.Depends(cc_file, depends + extra_deps)
800    Source(cc_file)
801
802    hh_file = File('enums/%s.hh' % name)
803    env.Command(hh_file, Value(name),
804                MakeAction(createEnumDecls, Transform("ENUMDECL")))
805    env.Depends(hh_file, depends + extra_deps)
806
807# Generate SimObject Python bindings wrapper files
808if env['USE_PYTHON']:
809    for name,simobj in sorted(sim_objects.iteritems()):
810        py_source = PySource.modules[simobj.__module__]
811        extra_deps = [ py_source.tnode ]
812        cc_file = File('python/_m5/param_%s.cc' % name)
813        env.Command(cc_file, Value(name),
814                    MakeAction(createSimObjectPyBindWrapper,
815                               Transform("SO PyBind")))
816        env.Depends(cc_file, depends + extra_deps)
817        Source(cc_file)
818
819# Build all protocol buffers if we have got protoc and protobuf available
820if env['HAVE_PROTOBUF']:
821    for proto in ProtoBuf.all:
822        # Use both the source and header as the target, and the .proto
823        # file as the source. When executing the protoc compiler, also
824        # specify the proto_path to avoid having the generated files
825        # include the path.
826        env.Command([proto.cc_file, proto.hh_file], proto.tnode,
827                    MakeAction('$PROTOC --cpp_out ${TARGET.dir} '
828                               '--proto_path ${SOURCE.dir} $SOURCE',
829                               Transform("PROTOC")))
830
831        # Add the C++ source file
832        Source(proto.cc_file, tags=proto.tags)
833elif ProtoBuf.all:
834    print 'Got protobuf to build, but lacks support!'
835    Exit(1)
836
837#
838# Handle debug flags
839#
840def makeDebugFlagCC(target, source, env):
841    assert(len(target) == 1 and len(source) == 1)
842
843    code = code_formatter()
844
845    # delay definition of CompoundFlags until after all the definition
846    # of all constituent SimpleFlags
847    comp_code = code_formatter()
848
849    # file header
850    code('''
851/*
852 * DO NOT EDIT THIS FILE! Automatically generated by SCons.
853 */
854
855#include "base/debug.hh"
856
857namespace Debug {
858
859''')
860
861    for name, flag in sorted(source[0].read().iteritems()):
862        n, compound, desc = flag
863        assert n == name
864
865        if not compound:
866            code('SimpleFlag $name("$name", "$desc");')
867        else:
868            comp_code('CompoundFlag $name("$name", "$desc",')
869            comp_code.indent()
870            last = len(compound) - 1
871            for i,flag in enumerate(compound):
872                if i != last:
873                    comp_code('&$flag,')
874                else:
875                    comp_code('&$flag);')
876            comp_code.dedent()
877
878    code.append(comp_code)
879    code()
880    code('} // namespace Debug')
881
882    code.write(str(target[0]))
883
884def makeDebugFlagHH(target, source, env):
885    assert(len(target) == 1 and len(source) == 1)
886
887    val = eval(source[0].get_contents())
888    name, compound, desc = val
889
890    code = code_formatter()
891
892    # file header boilerplate
893    code('''\
894/*
895 * DO NOT EDIT THIS FILE! Automatically generated by SCons.
896 */
897
898#ifndef __DEBUG_${name}_HH__
899#define __DEBUG_${name}_HH__
900
901namespace Debug {
902''')
903
904    if compound:
905        code('class CompoundFlag;')
906    code('class SimpleFlag;')
907
908    if compound:
909        code('extern CompoundFlag $name;')
910        for flag in compound:
911            code('extern SimpleFlag $flag;')
912    else:
913        code('extern SimpleFlag $name;')
914
915    code('''
916}
917
918#endif // __DEBUG_${name}_HH__
919''')
920
921    code.write(str(target[0]))
922
923for name,flag in sorted(debug_flags.iteritems()):
924    n, compound, desc = flag
925    assert n == name
926
927    hh_file = 'debug/%s.hh' % name
928    env.Command(hh_file, Value(flag),
929                MakeAction(makeDebugFlagHH, Transform("TRACING", 0)))
930
931env.Command('debug/flags.cc', Value(debug_flags),
932            MakeAction(makeDebugFlagCC, Transform("TRACING", 0)))
933Source('debug/flags.cc')
934
935# version tags
936tags = \
937env.Command('sim/tags.cc', None,
938            MakeAction('util/cpt_upgrader.py --get-cc-file > $TARGET',
939                       Transform("VER TAGS")))
940env.AlwaysBuild(tags)
941
942# Embed python files.  All .py files that have been indicated by a
943# PySource() call in a SConscript need to be embedded into the M5
944# library.  To do that, we compile the file to byte code, marshal the
945# byte code, compress it, and then generate a c++ file that
946# inserts the result into an array.
947def embedPyFile(target, source, env):
948    def c_str(string):
949        if string is None:
950            return "0"
951        return '"%s"' % string
952
953    '''Action function to compile a .py into a code object, marshal
954    it, compress it, and stick it into an asm file so the code appears
955    as just bytes with a label in the data section'''
956
957    src = file(str(source[0]), 'r').read()
958
959    pysource = PySource.tnodes[source[0]]
960    compiled = compile(src, pysource.abspath, 'exec')
961    marshalled = marshal.dumps(compiled)
962    compressed = zlib.compress(marshalled)
963    data = compressed
964    sym = pysource.symname
965
966    code = code_formatter()
967    code('''\
968#include "sim/init.hh"
969
970namespace {
971
972const uint8_t data_${sym}[] = {
973''')
974    code.indent()
975    step = 16
976    for i in xrange(0, len(data), step):
977        x = array.array('B', data[i:i+step])
978        code(''.join('%d,' % d for d in x))
979    code.dedent()
980
981    code('''};
982
983EmbeddedPython embedded_${sym}(
984    ${{c_str(pysource.arcname)}},
985    ${{c_str(pysource.abspath)}},
986    ${{c_str(pysource.modpath)}},
987    data_${sym},
988    ${{len(data)}},
989    ${{len(marshalled)}});
990
991} // anonymous namespace
992''')
993    code.write(str(target[0]))
994
995for source in PySource.all:
996    env.Command(source.cpp, source.tnode,
997                MakeAction(embedPyFile, Transform("EMBED PY")))
998    Source(source.cpp, tags=source.tags, add_tags='python')
999
1000########################################################################
1001#
1002# Define binaries.  Each different build type (debug, opt, etc.) gets
1003# a slightly different build environment.
1004#
1005
1006# List of constructed environments to pass back to SConstruct
1007date_source = Source('base/date.cc', tags=[])
1008
1009# Function to create a new build environment as clone of current
1010# environment 'env' with modified object suffix and optional stripped
1011# binary.  Additional keyword arguments are appended to corresponding
1012# build environment vars.
1013def makeEnv(env, label, objsfx, strip=False, disable_partial=False, **kwargs):
1014    # SCons doesn't know to append a library suffix when there is a '.' in the
1015    # name.  Use '_' instead.
1016    libname = 'gem5_' + label
1017    exename = 'gem5.' + label
1018    secondary_exename = 'm5.' + label
1019
1020    new_env = env.Clone(OBJSUFFIX=objsfx, SHOBJSUFFIX=objsfx + 's')
1021    new_env.Label = label
1022    new_env.Append(**kwargs)
1023
1024    lib_sources = Source.all.with_tag('gem5 lib')
1025
1026    # Without Python, leave out all Python content from the library
1027    # builds.  The option doesn't affect gem5 built as a program
1028    if GetOption('without_python'):
1029        lib_sources = lib_sources.without_tag('python')
1030
1031    static_objs = []
1032    shared_objs = []
1033
1034    for s in lib_sources.with_tag(Source.ungrouped_tag):
1035        static_objs.append(s.static(new_env))
1036        shared_objs.append(s.shared(new_env))
1037
1038    for group in Source.source_groups:
1039        srcs = lib_sources.with_tag(Source.link_group_tag(group))
1040        if not srcs:
1041            continue
1042
1043        group_static = [ s.static(new_env) for s in srcs ]
1044        group_shared = [ s.shared(new_env) for s in srcs ]
1045
1046        # If partial linking is disabled, add these sources to the build
1047        # directly, and short circuit this loop.
1048        if disable_partial:
1049            static_objs.extend(group_static)
1050            shared_objs.extend(group_shared)
1051            continue
1052
1053        # Set up the static partially linked objects.
1054        file_name = new_env.subst("${OBJPREFIX}lib${OBJSUFFIX}.partial")
1055        target = File(joinpath(group, file_name))
1056        partial = env.PartialStatic(target=target, source=group_static)
1057        static_objs.extend(partial)
1058
1059        # Set up the shared partially linked objects.
1060        file_name = new_env.subst("${SHOBJPREFIX}lib${SHOBJSUFFIX}.partial")
1061        target = File(joinpath(group, file_name))
1062        partial = env.PartialShared(target=target, source=group_shared)
1063        shared_objs.extend(partial)
1064
1065    static_date = date_source.static(new_env)
1066    new_env.Depends(static_date, static_objs)
1067    static_objs.extend(static_date)
1068
1069    shared_date = date_source.shared(new_env)
1070    new_env.Depends(shared_date, shared_objs)
1071    shared_objs.extend(shared_date)
1072
1073    # First make a library of everything but main() so other programs can
1074    # link against m5.
1075    static_lib = new_env.StaticLibrary(libname, static_objs)
1076    shared_lib = new_env.SharedLibrary(libname, shared_objs)
1077
1078    # Now link a stub with main() and the static library.
1079    main_objs = [ s.static(new_env) for s in Source.all.with_tag('main') ]
1080
1081    for test in UnitTest.all:
1082        test_sources = Source.all.with_tag(str(test.target))
1083        test_objs = [ s.static(new_env) for s in test_sources ]
1084        if test.main:
1085            test_objs += main_objs
1086        path = 'unittest/%s.%s' % (test.target, label)
1087        new_env.Program(path, test_objs + static_objs)
1088
1089    gtest_env = new_env.Clone()
1090    gtest_env.Append(LIBS=gtest_env['GTEST_LIBS'])
1091    gtest_env.Append(CPPFLAGS=gtest_env['GTEST_CPPFLAGS'])
1092    gtestlib_sources = Source.all.with_tag('gtest lib')
1093    gtest_out_dir = Dir(new_env['BUILDDIR']).Dir('unittests.%s' % label)
1094    for test in GTest.all:
1095        test_sources = list(test.sources)
1096        if not test.skip_lib:
1097            test_sources += gtestlib_sources
1098        for f in test.filters:
1099            test_sources += Source.all.apply_filter(f)
1100        test_objs = [ s.static(gtest_env) for s in test_sources ]
1101        test_binary = gtest_env.Program(
1102            test.dir.File('%s.%s' % (test.target, label)), test_objs)
1103
1104        AlwaysBuild(gtest_env.Command(
1105            gtest_out_dir.File("%s/%s.xml" % (test.dir, test.target)),
1106            test_binary, "${SOURCES[0]} --gtest_output=xml:${TARGETS[0]}"))
1107
1108    progname = exename
1109    if strip:
1110        progname += '.unstripped'
1111
1112    targets = new_env.Program(progname, main_objs + static_objs)
1113
1114    if strip:
1115        if sys.platform == 'sunos5':
1116            cmd = 'cp $SOURCE $TARGET; strip $TARGET'
1117        else:
1118            cmd = 'strip $SOURCE -o $TARGET'
1119        targets = new_env.Command(exename, progname,
1120                    MakeAction(cmd, Transform("STRIP")))
1121
1122    new_env.Command(secondary_exename, exename,
1123            MakeAction('ln $SOURCE $TARGET', Transform("HARDLINK")))
1124
1125    new_env.M5Binary = targets[0]
1126
1127    # Set up regression tests.
1128    SConscript(os.path.join(env.root.abspath, 'tests', 'SConscript'),
1129               variant_dir=Dir('tests').Dir(new_env.Label),
1130               exports={ 'env' : new_env }, duplicate=False)
1131
1132# Start out with the compiler flags common to all compilers,
1133# i.e. they all use -g for opt and -g -pg for prof
1134ccflags = {'debug' : [], 'opt' : ['-g'], 'fast' : [], 'prof' : ['-g', '-pg'],
1135           'perf' : ['-g']}
1136
1137# Start out with the linker flags common to all linkers, i.e. -pg for
1138# prof, and -lprofiler for perf. The -lprofile flag is surrounded by
1139# no-as-needed and as-needed as the binutils linker is too clever and
1140# simply doesn't link to the library otherwise.
1141ldflags = {'debug' : [], 'opt' : [], 'fast' : [], 'prof' : ['-pg'],
1142           'perf' : ['-Wl,--no-as-needed', '-lprofiler', '-Wl,--as-needed']}
1143
1144# For Link Time Optimization, the optimisation flags used to compile
1145# individual files are decoupled from those used at link time
1146# (i.e. you can compile with -O3 and perform LTO with -O0), so we need
1147# to also update the linker flags based on the target.
1148if env['GCC']:
1149    if sys.platform == 'sunos5':
1150        ccflags['debug'] += ['-gstabs+']
1151    else:
1152        ccflags['debug'] += ['-ggdb3']
1153    ldflags['debug'] += ['-O0']
1154    # opt, fast, prof and perf all share the same cc flags, also add
1155    # the optimization to the ldflags as LTO defers the optimization
1156    # to link time
1157    for target in ['opt', 'fast', 'prof', 'perf']:
1158        ccflags[target] += ['-O3']
1159        ldflags[target] += ['-O3']
1160
1161    ccflags['fast'] += env['LTO_CCFLAGS']
1162    ldflags['fast'] += env['LTO_LDFLAGS']
1163elif env['CLANG']:
1164    ccflags['debug'] += ['-g', '-O0']
1165    # opt, fast, prof and perf all share the same cc flags
1166    for target in ['opt', 'fast', 'prof', 'perf']:
1167        ccflags[target] += ['-O3']
1168else:
1169    print 'Unknown compiler, please fix compiler options'
1170    Exit(1)
1171
1172
1173# To speed things up, we only instantiate the build environments we
1174# need.  We try to identify the needed environment for each target; if
1175# we can't, we fall back on instantiating all the environments just to
1176# be safe.
1177target_types = ['debug', 'opt', 'fast', 'prof', 'perf']
1178obj2target = {'do': 'debug', 'o': 'opt', 'fo': 'fast', 'po': 'prof',
1179              'gpo' : 'perf'}
1180
1181def identifyTarget(t):
1182    ext = t.split('.')[-1]
1183    if ext in target_types:
1184        return ext
1185    if obj2target.has_key(ext):
1186        return obj2target[ext]
1187    match = re.search(r'/tests/([^/]+)/', t)
1188    if match and match.group(1) in target_types:
1189        return match.group(1)
1190    return 'all'
1191
1192needed_envs = [identifyTarget(target) for target in BUILD_TARGETS]
1193if 'all' in needed_envs:
1194    needed_envs += target_types
1195
1196# Debug binary
1197if 'debug' in needed_envs:
1198    makeEnv(env, 'debug', '.do',
1199            CCFLAGS = Split(ccflags['debug']),
1200            CPPDEFINES = ['DEBUG', 'TRACING_ON=1'],
1201            LINKFLAGS = Split(ldflags['debug']))
1202
1203# Optimized binary
1204if 'opt' in needed_envs:
1205    makeEnv(env, 'opt', '.o',
1206            CCFLAGS = Split(ccflags['opt']),
1207            CPPDEFINES = ['TRACING_ON=1'],
1208            LINKFLAGS = Split(ldflags['opt']))
1209
1210# "Fast" binary
1211if 'fast' in needed_envs:
1212    disable_partial = \
1213            env.get('BROKEN_INCREMENTAL_LTO', False) and \
1214            GetOption('force_lto')
1215    makeEnv(env, 'fast', '.fo', strip = True,
1216            CCFLAGS = Split(ccflags['fast']),
1217            CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1218            LINKFLAGS = Split(ldflags['fast']),
1219            disable_partial=disable_partial)
1220
1221# Profiled binary using gprof
1222if 'prof' in needed_envs:
1223    makeEnv(env, 'prof', '.po',
1224            CCFLAGS = Split(ccflags['prof']),
1225            CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1226            LINKFLAGS = Split(ldflags['prof']))
1227
1228# Profiled binary using google-pprof
1229if 'perf' in needed_envs:
1230    makeEnv(env, 'perf', '.gpo',
1231            CCFLAGS = Split(ccflags['perf']),
1232            CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1233            LINKFLAGS = Split(ldflags['perf']))
1234