SConscript revision 12757
1955SN/A# -*- mode:python -*-
2955SN/A
31762SN/A# Copyright (c) 2004-2005 The Regents of The University of Michigan
4955SN/A# All rights reserved.
5955SN/A#
6955SN/A# Redistribution and use in source and binary forms, with or without
7955SN/A# modification, are permitted provided that the following conditions are
8955SN/A# met: redistributions of source code must retain the above copyright
9955SN/A# notice, this list of conditions and the following disclaimer;
10955SN/A# redistributions in binary form must reproduce the above copyright
11955SN/A# notice, this list of conditions and the following disclaimer in the
12955SN/A# documentation and/or other materials provided with the distribution;
13955SN/A# neither the name of the copyright holders nor the names of its
14955SN/A# contributors may be used to endorse or promote products derived from
15955SN/A# this software without specific prior written permission.
16955SN/A#
17955SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18955SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19955SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20955SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21955SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22955SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23955SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24955SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25955SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26955SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27955SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
282665Ssaidi@eecs.umich.edu#
294762Snate@binkert.org# Authors: Nathan Binkert
30955SN/A
315522Snate@binkert.orgfrom __future__ import print_function
324762Snate@binkert.org
335522Snate@binkert.orgimport array
34955SN/Aimport bisect
355522Snate@binkert.orgimport functools
36955SN/Aimport imp
375522Snate@binkert.orgimport marshal
384202Sbinkertn@umich.eduimport os
395342Sstever@gmail.comimport re
40955SN/Aimport subprocess
414381Sbinkertn@umich.eduimport sys
424381Sbinkertn@umich.eduimport zlib
43955SN/A
44955SN/Afrom os.path import basename, dirname, exists, isdir, isfile, join as joinpath
45955SN/A
464202Sbinkertn@umich.eduimport SCons
47955SN/A
484382Sbinkertn@umich.edufrom gem5_scons import Transform
494382Sbinkertn@umich.edu
504382Sbinkertn@umich.edu# This file defines how to build a particular configuration of gem5
515517Snate@binkert.org# based on variable settings in the 'env' build environment.
525517Snate@binkert.org
534762Snate@binkert.orgImport('*')
544762Snate@binkert.org
554762Snate@binkert.org# Children need to see the environment
564762Snate@binkert.orgExport('env')
574762Snate@binkert.org
584762Snate@binkert.orgbuild_env = [(opt, env[opt]) for opt in export_vars]
594762Snate@binkert.org
604762Snate@binkert.orgfrom m5.util import code_formatter, compareVersions
614762Snate@binkert.org
624762Snate@binkert.org########################################################################
635522Snate@binkert.org# Code for adding source files of various types
644762Snate@binkert.org#
654762Snate@binkert.org# When specifying a source file of some type, a set of tags can be
664762Snate@binkert.org# specified for that file.
674762Snate@binkert.org
684762Snate@binkert.orgclass SourceFilter(object):
695522Snate@binkert.org    def __init__(self, predicate):
705522Snate@binkert.org        self.predicate = predicate
715522Snate@binkert.org
725522Snate@binkert.org    def __or__(self, other):
734762Snate@binkert.org        return SourceFilter(lambda tags: self.predicate(tags) or
744762Snate@binkert.org                                         other.predicate(tags))
754762Snate@binkert.org
764762Snate@binkert.org    def __and__(self, other):
774762Snate@binkert.org        return SourceFilter(lambda tags: self.predicate(tags) and
785522Snate@binkert.org                                         other.predicate(tags))
794762Snate@binkert.org
804762Snate@binkert.orgdef with_tags_that(predicate):
815522Snate@binkert.org    '''Return a list of sources with tags that satisfy a predicate.'''
825522Snate@binkert.org    return SourceFilter(predicate)
834762Snate@binkert.org
844762Snate@binkert.orgdef with_any_tags(*tags):
854762Snate@binkert.org    '''Return a list of sources with any of the supplied tags.'''
864762Snate@binkert.org    return SourceFilter(lambda stags: len(set(tags) & stags) > 0)
874762Snate@binkert.org
884762Snate@binkert.orgdef with_all_tags(*tags):
895522Snate@binkert.org    '''Return a list of sources with all of the supplied tags.'''
905522Snate@binkert.org    return SourceFilter(lambda stags: set(tags) <= stags)
915522Snate@binkert.org
924762Snate@binkert.orgdef with_tag(tag):
934382Sbinkertn@umich.edu    '''Return a list of sources with the supplied tag.'''
944762Snate@binkert.org    return SourceFilter(lambda stags: tag in stags)
954382Sbinkertn@umich.edu
965522Snate@binkert.orgdef without_tags(*tags):
974381Sbinkertn@umich.edu    '''Return a list of sources without any of the supplied tags.'''
985522Snate@binkert.org    return SourceFilter(lambda stags: len(set(tags) & stags) == 0)
994762Snate@binkert.org
1004762Snate@binkert.orgdef without_tag(tag):
1014762Snate@binkert.org    '''Return a list of sources with the supplied tag.'''
1025522Snate@binkert.org    return SourceFilter(lambda stags: tag not in stags)
1035522Snate@binkert.org
1045522Snate@binkert.orgsource_filter_factories = {
1055522Snate@binkert.org    'with_tags_that': with_tags_that,
1065522Snate@binkert.org    'with_any_tags': with_any_tags,
1075522Snate@binkert.org    'with_all_tags': with_all_tags,
1085522Snate@binkert.org    'with_tag': with_tag,
1095522Snate@binkert.org    'without_tags': without_tags,
1105522Snate@binkert.org    'without_tag': without_tag,
1114762Snate@binkert.org}
1124762Snate@binkert.org
1134762Snate@binkert.orgExport(source_filter_factories)
1144762Snate@binkert.org
1154762Snate@binkert.orgclass SourceList(list):
1164762Snate@binkert.org    def apply_filter(self, f):
1174762Snate@binkert.org        def match(source):
1184762Snate@binkert.org            return f.predicate(source.tags)
1194762Snate@binkert.org        return SourceList(filter(match, self))
1204762Snate@binkert.org
1214762Snate@binkert.org    def __getattr__(self, name):
1224762Snate@binkert.org        func = source_filter_factories.get(name, None)
1234762Snate@binkert.org        if not func:
1244762Snate@binkert.org            raise AttributeError
1254762Snate@binkert.org
1264762Snate@binkert.org        @functools.wraps(func)
1274762Snate@binkert.org        def wrapper(*args, **kwargs):
1284762Snate@binkert.org            return self.apply_filter(func(*args, **kwargs))
1294762Snate@binkert.org        return wrapper
1304762Snate@binkert.org
1314762Snate@binkert.orgclass SourceMeta(type):
1324762Snate@binkert.org    '''Meta class for source files that keeps track of all files of a
1334762Snate@binkert.org    particular type.'''
1344762Snate@binkert.org    def __init__(cls, name, bases, dict):
1354762Snate@binkert.org        super(SourceMeta, cls).__init__(name, bases, dict)
1364762Snate@binkert.org        cls.all = SourceList()
1374762Snate@binkert.org
1384762Snate@binkert.orgclass SourceFile(object):
1394762Snate@binkert.org    '''Base object that encapsulates the notion of a source file.
1404762Snate@binkert.org    This includes, the source node, target node, various manipulations
1414762Snate@binkert.org    of those.  A source file also specifies a set of tags which
1424762Snate@binkert.org    describing arbitrary properties of the source file.'''
1434762Snate@binkert.org    __metaclass__ = SourceMeta
1444762Snate@binkert.org
1454762Snate@binkert.org    static_objs = {}
146955SN/A    shared_objs = {}
1474382Sbinkertn@umich.edu
1484202Sbinkertn@umich.edu    def __init__(self, source, tags=None, add_tags=None):
1495522Snate@binkert.org        if tags is None:
1504382Sbinkertn@umich.edu            tags='gem5 lib'
1514382Sbinkertn@umich.edu        if isinstance(tags, basestring):
1524382Sbinkertn@umich.edu            tags = set([tags])
1534382Sbinkertn@umich.edu        if not isinstance(tags, set):
1544382Sbinkertn@umich.edu            tags = set(tags)
1554382Sbinkertn@umich.edu        self.tags = tags
1565192Ssaidi@eecs.umich.edu
1575192Ssaidi@eecs.umich.edu        if add_tags:
1585192Ssaidi@eecs.umich.edu            if isinstance(add_tags, basestring):
1595192Ssaidi@eecs.umich.edu                add_tags = set([add_tags])
1605192Ssaidi@eecs.umich.edu            if not isinstance(add_tags, set):
1615192Ssaidi@eecs.umich.edu                add_tags = set(add_tags)
1625192Ssaidi@eecs.umich.edu            self.tags |= add_tags
1635192Ssaidi@eecs.umich.edu
1645192Ssaidi@eecs.umich.edu        tnode = source
1655192Ssaidi@eecs.umich.edu        if not isinstance(source, SCons.Node.FS.File):
1665192Ssaidi@eecs.umich.edu            tnode = File(source)
1675192Ssaidi@eecs.umich.edu
1685192Ssaidi@eecs.umich.edu        self.tnode = tnode
1695192Ssaidi@eecs.umich.edu        self.snode = tnode.srcnode()
1705192Ssaidi@eecs.umich.edu
1715192Ssaidi@eecs.umich.edu        for base in type(self).__mro__:
1725192Ssaidi@eecs.umich.edu            if issubclass(base, SourceFile):
1735192Ssaidi@eecs.umich.edu                base.all.append(self)
1745192Ssaidi@eecs.umich.edu
1755192Ssaidi@eecs.umich.edu    def static(self, env):
1765192Ssaidi@eecs.umich.edu        key = (self.tnode, env['OBJSUFFIX'])
1775192Ssaidi@eecs.umich.edu        if not key in self.static_objs:
1785192Ssaidi@eecs.umich.edu            self.static_objs[key] = env.StaticObject(self.tnode)
1795192Ssaidi@eecs.umich.edu        return self.static_objs[key]
1805192Ssaidi@eecs.umich.edu
1815192Ssaidi@eecs.umich.edu    def shared(self, env):
1825192Ssaidi@eecs.umich.edu        key = (self.tnode, env['OBJSUFFIX'])
1835192Ssaidi@eecs.umich.edu        if not key in self.shared_objs:
1845192Ssaidi@eecs.umich.edu            self.shared_objs[key] = env.SharedObject(self.tnode)
1855192Ssaidi@eecs.umich.edu        return self.shared_objs[key]
1865192Ssaidi@eecs.umich.edu
1875192Ssaidi@eecs.umich.edu    @property
1884382Sbinkertn@umich.edu    def filename(self):
1894382Sbinkertn@umich.edu        return str(self.tnode)
1904382Sbinkertn@umich.edu
1912667Sstever@eecs.umich.edu    @property
1922667Sstever@eecs.umich.edu    def dirname(self):
1932667Sstever@eecs.umich.edu        return dirname(self.filename)
1942667Sstever@eecs.umich.edu
1952667Sstever@eecs.umich.edu    @property
1962667Sstever@eecs.umich.edu    def basename(self):
1972037SN/A        return basename(self.filename)
1982037SN/A
1992037SN/A    @property
2004382Sbinkertn@umich.edu    def extname(self):
2014762Snate@binkert.org        index = self.basename.rfind('.')
2025344Sstever@gmail.com        if index <= 0:
2034382Sbinkertn@umich.edu            # dot files aren't extensions
2045341Sstever@gmail.com            return self.basename, None
2055341Sstever@gmail.com
2065341Sstever@gmail.com        return self.basename[:index], self.basename[index+1:]
2075344Sstever@gmail.com
2085341Sstever@gmail.com    def __lt__(self, other): return self.filename < other.filename
2095341Sstever@gmail.com    def __le__(self, other): return self.filename <= other.filename
2105341Sstever@gmail.com    def __gt__(self, other): return self.filename > other.filename
2114762Snate@binkert.org    def __ge__(self, other): return self.filename >= other.filename
2125341Sstever@gmail.com    def __eq__(self, other): return self.filename == other.filename
2135344Sstever@gmail.com    def __ne__(self, other): return self.filename != other.filename
2145341Sstever@gmail.com
2154773Snate@binkert.orgclass Source(SourceFile):
2161858SN/A    ungrouped_tag = 'No link group'
2171858SN/A    source_groups = set()
2181085SN/A
2194382Sbinkertn@umich.edu    _current_group_tag = ungrouped_tag
2204382Sbinkertn@umich.edu
2214762Snate@binkert.org    @staticmethod
2224762Snate@binkert.org    def link_group_tag(group):
2234762Snate@binkert.org        return 'link group: %s' % group
2245517Snate@binkert.org
2255517Snate@binkert.org    @classmethod
2265517Snate@binkert.org    def set_group(cls, group):
2275517Snate@binkert.org        new_tag = Source.link_group_tag(group)
2285517Snate@binkert.org        Source._current_group_tag = new_tag
2295517Snate@binkert.org        Source.source_groups.add(group)
2305517Snate@binkert.org
2315517Snate@binkert.org    def _add_link_group_tag(self):
2325517Snate@binkert.org        self.tags.add(Source._current_group_tag)
2335517Snate@binkert.org
2345517Snate@binkert.org    '''Add a c/c++ source file to the build'''
2355517Snate@binkert.org    def __init__(self, source, tags=None, add_tags=None):
2365517Snate@binkert.org        '''specify the source file, and any tags'''
2375517Snate@binkert.org        super(Source, self).__init__(source, tags, add_tags)
2385517Snate@binkert.org        self._add_link_group_tag()
2395517Snate@binkert.org
2405517Snate@binkert.orgclass PySource(SourceFile):
2415517Snate@binkert.org    '''Add a python source file to the named package'''
2425517Snate@binkert.org    invalid_sym_char = re.compile('[^A-z0-9_]')
2435517Snate@binkert.org    modules = {}
2445517Snate@binkert.org    tnodes = {}
2455517Snate@binkert.org    symnames = {}
2465517Snate@binkert.org
2475517Snate@binkert.org    def __init__(self, package, source, tags=None, add_tags=None):
2485517Snate@binkert.org        '''specify the python package, the source file, and any tags'''
2495517Snate@binkert.org        super(PySource, self).__init__(source, tags, add_tags)
2505517Snate@binkert.org
2515517Snate@binkert.org        modname,ext = self.extname
2525517Snate@binkert.org        assert ext == 'py'
2535517Snate@binkert.org
2545517Snate@binkert.org        if package:
2555517Snate@binkert.org            path = package.split('.')
2565517Snate@binkert.org        else:
2575517Snate@binkert.org            path = []
2585517Snate@binkert.org
2595517Snate@binkert.org        modpath = path[:]
2605517Snate@binkert.org        if modname != '__init__':
2615517Snate@binkert.org            modpath += [ modname ]
2625517Snate@binkert.org        modpath = '.'.join(modpath)
2635517Snate@binkert.org
2645517Snate@binkert.org        arcpath = path + [ self.basename ]
2655517Snate@binkert.org        abspath = self.snode.abspath
2665517Snate@binkert.org        if not exists(abspath):
2675517Snate@binkert.org            abspath = self.tnode.abspath
2685517Snate@binkert.org
2695517Snate@binkert.org        self.package = package
2705517Snate@binkert.org        self.modname = modname
2715517Snate@binkert.org        self.modpath = modpath
2725517Snate@binkert.org        self.arcname = joinpath(*arcpath)
2735517Snate@binkert.org        self.abspath = abspath
2745517Snate@binkert.org        self.compiled = File(self.filename + 'c')
2755517Snate@binkert.org        self.cpp = File(self.filename + '.cc')
2765517Snate@binkert.org        self.symname = PySource.invalid_sym_char.sub('_', modpath)
2775517Snate@binkert.org
2785517Snate@binkert.org        PySource.modules[modpath] = self
2795517Snate@binkert.org        PySource.tnodes[self.tnode] = self
2805522Snate@binkert.org        PySource.symnames[self.symname] = self
2815517Snate@binkert.org
2825517Snate@binkert.orgclass SimObject(PySource):
2835517Snate@binkert.org    '''Add a SimObject python file as a python source object and add
2845517Snate@binkert.org    it to a list of sim object modules'''
2854762Snate@binkert.org
2865517Snate@binkert.org    fixed = False
2875517Snate@binkert.org    modnames = []
2884762Snate@binkert.org
2895517Snate@binkert.org    def __init__(self, source, tags=None, add_tags=None):
2904762Snate@binkert.org        '''Specify the source file and any tags (automatically in
2915517Snate@binkert.org        the m5.objects package)'''
2925517Snate@binkert.org        super(SimObject, self).__init__('m5.objects', source, tags, add_tags)
2935517Snate@binkert.org        if self.fixed:
2945517Snate@binkert.org            raise AttributeError, "Too late to call SimObject now."
2955517Snate@binkert.org
2965517Snate@binkert.org        bisect.insort_right(SimObject.modnames, self.modname)
2975517Snate@binkert.org
2985517Snate@binkert.orgclass ProtoBuf(SourceFile):
2995517Snate@binkert.org    '''Add a Protocol Buffer to build'''
3005517Snate@binkert.org
3015517Snate@binkert.org    def __init__(self, source, tags=None, add_tags=None):
3025517Snate@binkert.org        '''Specify the source file, and any tags'''
3035517Snate@binkert.org        super(ProtoBuf, self).__init__(source, tags, add_tags)
3045517Snate@binkert.org
3055517Snate@binkert.org        # Get the file name and the extension
3065517Snate@binkert.org        modname,ext = self.extname
3075517Snate@binkert.org        assert ext == 'proto'
3085517Snate@binkert.org
3095517Snate@binkert.org        # Currently, we stick to generating the C++ headers, so we
3105517Snate@binkert.org        # only need to track the source and header.
3115517Snate@binkert.org        self.cc_file = File(modname + '.pb.cc')
3125517Snate@binkert.org        self.hh_file = File(modname + '.pb.h')
3135517Snate@binkert.org
3144762Snate@binkert.orgclass UnitTest(object):
3154762Snate@binkert.org    '''Create a UnitTest'''
3164762Snate@binkert.org
3174762Snate@binkert.org    all = []
3184762Snate@binkert.org    def __init__(self, target, *srcs_and_filts, **kwargs):
3194762Snate@binkert.org        '''Specify the target name and any sources. Sources that are
3205517Snate@binkert.org        not SourceFiles are evalued with Source().'''
3214762Snate@binkert.org
3224762Snate@binkert.org        isFilter = lambda arg: isinstance(arg, SourceFilter)
3234762Snate@binkert.org        self.filters = filter(isFilter, srcs_and_filts)
3244762Snate@binkert.org        sources = filter(lambda a: not isFilter(a), srcs_and_filts)
3254382Sbinkertn@umich.edu
3264382Sbinkertn@umich.edu        srcs = SourceList()
3275517Snate@binkert.org        for src in sources:
3285517Snate@binkert.org            if not isinstance(src, SourceFile):
3295517Snate@binkert.org                src = Source(src, tags=[])
3305517Snate@binkert.org            srcs.append(src)
3315517Snate@binkert.org
3325517Snate@binkert.org        self.sources = srcs
3335517Snate@binkert.org        self.target = target
3345517Snate@binkert.org        self.main = kwargs.get('main', False)
3355517Snate@binkert.org        self.all.append(self)
3365517Snate@binkert.org        self.dir = Dir('.')
3375517Snate@binkert.org
3385517Snate@binkert.orgclass GTest(UnitTest):
3395517Snate@binkert.org    '''Create a unit test based on the google test framework.'''
3405517Snate@binkert.org    all = []
3415517Snate@binkert.org    def __init__(self, *args, **kwargs):
3425517Snate@binkert.org        super(GTest, self).__init__(*args, **kwargs)
3435517Snate@binkert.org        self.skip_lib = kwargs.pop('skip_lib', False)
3445517Snate@binkert.org
3455517Snate@binkert.org# Children should have access
3465517Snate@binkert.orgExport('Source')
3475517Snate@binkert.orgExport('PySource')
3485517Snate@binkert.orgExport('SimObject')
3495517Snate@binkert.orgExport('ProtoBuf')
3505517Snate@binkert.orgExport('UnitTest')
3514762Snate@binkert.orgExport('GTest')
3525517Snate@binkert.org
3534382Sbinkertn@umich.edu########################################################################
3544382Sbinkertn@umich.edu#
3554762Snate@binkert.org# Debug Flags
3564382Sbinkertn@umich.edu#
3574382Sbinkertn@umich.edudebug_flags = {}
3585517Snate@binkert.orgdef DebugFlag(name, desc=None):
3594382Sbinkertn@umich.edu    if name in debug_flags:
3604382Sbinkertn@umich.edu        raise AttributeError, "Flag %s already specified" % name
3614762Snate@binkert.org    debug_flags[name] = (name, (), desc)
3624382Sbinkertn@umich.edu
3634762Snate@binkert.orgdef CompoundFlag(name, flags, desc=None):
3645517Snate@binkert.org    if name in debug_flags:
3654382Sbinkertn@umich.edu        raise AttributeError, "Flag %s already specified" % name
3664382Sbinkertn@umich.edu
3674762Snate@binkert.org    compound = tuple(flags)
3684762Snate@binkert.org    debug_flags[name] = (name, compound, desc)
3694762Snate@binkert.org
3704762Snate@binkert.orgExport('DebugFlag')
3714762Snate@binkert.orgExport('CompoundFlag')
3725517Snate@binkert.org
3735517Snate@binkert.org########################################################################
3745517Snate@binkert.org#
3755517Snate@binkert.org# Set some compiler variables
3765517Snate@binkert.org#
3775517Snate@binkert.org
3785517Snate@binkert.org# Include file paths are rooted in this directory.  SCons will
3795517Snate@binkert.org# automatically expand '.' to refer to both the source directory and
3805517Snate@binkert.org# the corresponding build directory to pick up generated include
3815517Snate@binkert.org# files.
3825517Snate@binkert.orgenv.Append(CPPPATH=Dir('.'))
3835517Snate@binkert.org
3845517Snate@binkert.orgfor extra_dir in extras_dir_list:
3855517Snate@binkert.org    env.Append(CPPPATH=Dir(extra_dir))
3865517Snate@binkert.org
3875517Snate@binkert.org# Workaround for bug in SCons version > 0.97d20071212
3885517Snate@binkert.org# Scons bug id: 2006 gem5 Bug id: 308
3895517Snate@binkert.orgfor root, dirs, files in os.walk(base_dir, topdown=True):
3905517Snate@binkert.org    Dir(root[len(base_dir) + 1:])
3915517Snate@binkert.org
3925517Snate@binkert.org########################################################################
3935517Snate@binkert.org#
3945517Snate@binkert.org# Walk the tree and execute all SConscripts in subdirectories
3955517Snate@binkert.org#
3965517Snate@binkert.org
3975517Snate@binkert.orghere = Dir('.').srcnode().abspath
3985517Snate@binkert.orgfor root, dirs, files in os.walk(base_dir, topdown=True):
3995517Snate@binkert.org    if root == here:
4005517Snate@binkert.org        # we don't want to recurse back into this SConscript
4015517Snate@binkert.org        continue
4025517Snate@binkert.org
4035517Snate@binkert.org    if 'SConscript' in files:
4045517Snate@binkert.org        build_dir = joinpath(env['BUILDDIR'], root[len(base_dir) + 1:])
4055517Snate@binkert.org        Source.set_group(build_dir)
4065517Snate@binkert.org        SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
4075517Snate@binkert.org
4085517Snate@binkert.orgfor extra_dir in extras_dir_list:
4095517Snate@binkert.org    prefix_len = len(dirname(extra_dir)) + 1
4104762Snate@binkert.org
4114762Snate@binkert.org    # Also add the corresponding build directory to pick up generated
4125517Snate@binkert.org    # include files.
4135517Snate@binkert.org    env.Append(CPPPATH=Dir(joinpath(env['BUILDDIR'], extra_dir[prefix_len:])))
4144762Snate@binkert.org
4154762Snate@binkert.org    for root, dirs, files in os.walk(extra_dir, topdown=True):
4164762Snate@binkert.org        # if build lives in the extras directory, don't walk down it
4175517Snate@binkert.org        if 'build' in dirs:
4184762Snate@binkert.org            dirs.remove('build')
4194762Snate@binkert.org
4204762Snate@binkert.org        if 'SConscript' in files:
4215463Snate@binkert.org            build_dir = joinpath(env['BUILDDIR'], root[prefix_len:])
4225517Snate@binkert.org            SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
4234762Snate@binkert.org
4244762Snate@binkert.orgfor opt in export_vars:
4254762Snate@binkert.org    env.ConfigFile(opt)
4264762Snate@binkert.org
4274762Snate@binkert.orgdef makeTheISA(source, target, env):
4284762Snate@binkert.org    isas = [ src.get_contents() for src in source ]
4295463Snate@binkert.org    target_isa = env['TARGET_ISA']
4305517Snate@binkert.org    def define(isa):
4314762Snate@binkert.org        return isa.upper() + '_ISA'
4324762Snate@binkert.org
4334762Snate@binkert.org    def namespace(isa):
4345517Snate@binkert.org        return isa[0].upper() + isa[1:].lower() + 'ISA'
4355517Snate@binkert.org
4364762Snate@binkert.org
4374762Snate@binkert.org    code = code_formatter()
4385517Snate@binkert.org    code('''\
4394762Snate@binkert.org#ifndef __CONFIG_THE_ISA_HH__
4404762Snate@binkert.org#define __CONFIG_THE_ISA_HH__
4414762Snate@binkert.org
4424762Snate@binkert.org''')
4435517Snate@binkert.org
4444762Snate@binkert.org    # create defines for the preprocessing and compile-time determination
4454762Snate@binkert.org    for i,isa in enumerate(isas):
4464762Snate@binkert.org        code('#define $0 $1', define(isa), i + 1)
4474762Snate@binkert.org    code()
4485517Snate@binkert.org
4495517Snate@binkert.org    # create an enum for any run-time determination of the ISA, we
4505517Snate@binkert.org    # reuse the same name as the namespaces
4515517Snate@binkert.org    code('enum class Arch {')
4525517Snate@binkert.org    for i,isa in enumerate(isas):
4535517Snate@binkert.org        if i + 1 == len(isas):
4545517Snate@binkert.org            code('  $0 = $1', namespace(isa), define(isa))
4555517Snate@binkert.org        else:
4565517Snate@binkert.org            code('  $0 = $1,', namespace(isa), define(isa))
4575517Snate@binkert.org    code('};')
4585517Snate@binkert.org
4595517Snate@binkert.org    code('''
4605517Snate@binkert.org
4615517Snate@binkert.org#define THE_ISA ${{define(target_isa)}}
4625517Snate@binkert.org#define TheISA ${{namespace(target_isa)}}
4635517Snate@binkert.org#define THE_ISA_STR "${{target_isa}}"
4645517Snate@binkert.org
4655517Snate@binkert.org#endif // __CONFIG_THE_ISA_HH__''')
4665517Snate@binkert.org
4675517Snate@binkert.org    code.write(str(target[0]))
4685517Snate@binkert.org
4695517Snate@binkert.orgenv.Command('config/the_isa.hh', map(Value, all_isa_list),
4705517Snate@binkert.org            MakeAction(makeTheISA, Transform("CFG ISA", 0)))
4715517Snate@binkert.org
4725517Snate@binkert.orgdef makeTheGPUISA(source, target, env):
4735517Snate@binkert.org    isas = [ src.get_contents() for src in source ]
4745517Snate@binkert.org    target_gpu_isa = env['TARGET_GPU_ISA']
4755517Snate@binkert.org    def define(isa):
4765517Snate@binkert.org        return isa.upper() + '_ISA'
4775517Snate@binkert.org
4785517Snate@binkert.org    def namespace(isa):
4795517Snate@binkert.org        return isa[0].upper() + isa[1:].lower() + 'ISA'
4805517Snate@binkert.org
4815517Snate@binkert.org
4825517Snate@binkert.org    code = code_formatter()
4835517Snate@binkert.org    code('''\
4845517Snate@binkert.org#ifndef __CONFIG_THE_GPU_ISA_HH__
4855517Snate@binkert.org#define __CONFIG_THE_GPU_ISA_HH__
4865517Snate@binkert.org
4875517Snate@binkert.org''')
4885517Snate@binkert.org
4895517Snate@binkert.org    # create defines for the preprocessing and compile-time determination
4905517Snate@binkert.org    for i,isa in enumerate(isas):
4915517Snate@binkert.org        code('#define $0 $1', define(isa), i + 1)
4925517Snate@binkert.org    code()
4935517Snate@binkert.org
4945517Snate@binkert.org    # create an enum for any run-time determination of the ISA, we
4955517Snate@binkert.org    # reuse the same name as the namespaces
4965517Snate@binkert.org    code('enum class GPUArch {')
4975517Snate@binkert.org    for i,isa in enumerate(isas):
4985517Snate@binkert.org        if i + 1 == len(isas):
4995517Snate@binkert.org            code('  $0 = $1', namespace(isa), define(isa))
5005517Snate@binkert.org        else:
5015517Snate@binkert.org            code('  $0 = $1,', namespace(isa), define(isa))
5025517Snate@binkert.org    code('};')
5035517Snate@binkert.org
5045517Snate@binkert.org    code('''
5055517Snate@binkert.org
5065517Snate@binkert.org#define THE_GPU_ISA ${{define(target_gpu_isa)}}
5075517Snate@binkert.org#define TheGpuISA ${{namespace(target_gpu_isa)}}
5085517Snate@binkert.org#define THE_GPU_ISA_STR "${{target_gpu_isa}}"
5095517Snate@binkert.org
5105517Snate@binkert.org#endif // __CONFIG_THE_GPU_ISA_HH__''')
5115517Snate@binkert.org
5125517Snate@binkert.org    code.write(str(target[0]))
5135517Snate@binkert.org
5145517Snate@binkert.orgenv.Command('config/the_gpu_isa.hh', map(Value, all_gpu_isa_list),
5155517Snate@binkert.org            MakeAction(makeTheGPUISA, Transform("CFG ISA", 0)))
5165517Snate@binkert.org
5175517Snate@binkert.org########################################################################
5185517Snate@binkert.org#
5195517Snate@binkert.org# Prevent any SimObjects from being added after this point, they
5205517Snate@binkert.org# should all have been added in the SConscripts above
5215517Snate@binkert.org#
5225517Snate@binkert.orgSimObject.fixed = True
5235517Snate@binkert.org
5245517Snate@binkert.orgclass DictImporter(object):
5255517Snate@binkert.org    '''This importer takes a dictionary of arbitrary module names that
5265517Snate@binkert.org    map to arbitrary filenames.'''
5275517Snate@binkert.org    def __init__(self, modules):
5285517Snate@binkert.org        self.modules = modules
5295517Snate@binkert.org        self.installed = set()
5305517Snate@binkert.org
5315517Snate@binkert.org    def __del__(self):
5325517Snate@binkert.org        self.unload()
5335517Snate@binkert.org
5345517Snate@binkert.org    def unload(self):
5355517Snate@binkert.org        import sys
5365517Snate@binkert.org        for module in self.installed:
5375517Snate@binkert.org            del sys.modules[module]
5384762Snate@binkert.org        self.installed = set()
5395517Snate@binkert.org
5405517Snate@binkert.org    def find_module(self, fullname, path):
5415463Snate@binkert.org        if fullname == 'm5.defines':
5424762Snate@binkert.org            return self
5434762Snate@binkert.org
5444762Snate@binkert.org        if fullname == 'm5.objects':
5454382Sbinkertn@umich.edu            return self
5465554Snate@binkert.org
5474762Snate@binkert.org        if fullname.startswith('_m5'):
5484382Sbinkertn@umich.edu            return None
5494762Snate@binkert.org
5504382Sbinkertn@umich.edu        source = self.modules.get(fullname, None)
5514762Snate@binkert.org        if source is not None and fullname.startswith('m5.objects'):
5524762Snate@binkert.org            return self
5534762Snate@binkert.org
5544762Snate@binkert.org        return None
5554382Sbinkertn@umich.edu
5564382Sbinkertn@umich.edu    def load_module(self, fullname):
5574382Sbinkertn@umich.edu        mod = imp.new_module(fullname)
5584382Sbinkertn@umich.edu        sys.modules[fullname] = mod
5594382Sbinkertn@umich.edu        self.installed.add(fullname)
5604382Sbinkertn@umich.edu
5614762Snate@binkert.org        mod.__loader__ = self
5624382Sbinkertn@umich.edu        if fullname == 'm5.objects':
5635554Snate@binkert.org            mod.__path__ = fullname.split('.')
5644382Sbinkertn@umich.edu            return mod
5654382Sbinkertn@umich.edu
5664762Snate@binkert.org        if fullname == 'm5.defines':
5675517Snate@binkert.org            mod.__dict__['buildEnv'] = m5.util.SmartDict(build_env)
5685517Snate@binkert.org            return mod
5695517Snate@binkert.org
5705517Snate@binkert.org        source = self.modules[fullname]
5715517Snate@binkert.org        if source.modname == '__init__':
5725517Snate@binkert.org            mod.__path__ = source.modpath
5735522Snate@binkert.org        mod.__file__ = source.abspath
5745517Snate@binkert.org
5755517Snate@binkert.org        exec file(source.abspath, 'r') in mod.__dict__
5765517Snate@binkert.org
5775517Snate@binkert.org        return mod
5785517Snate@binkert.org
5795522Snate@binkert.orgimport m5.SimObject
5805522Snate@binkert.orgimport m5.params
5814382Sbinkertn@umich.edufrom m5.util import code_formatter
5825192Ssaidi@eecs.umich.edu
5835517Snate@binkert.orgm5.SimObject.clear()
5845517Snate@binkert.orgm5.params.clear()
5855517Snate@binkert.org
5865517Snate@binkert.org# install the python importer so we can grab stuff from the source
5875517Snate@binkert.org# tree itself.  We can't have SimObjects added after this point or
5885517Snate@binkert.org# else we won't know about them for the rest of the stuff.
5895517Snate@binkert.orgimporter = DictImporter(PySource.modules)
5905517Snate@binkert.orgsys.meta_path[0:0] = [ importer ]
5915517Snate@binkert.org
5925517Snate@binkert.org# import all sim objects so we can populate the all_objects list
5935517Snate@binkert.org# make sure that we're working with a list, then let's sort it
5945517Snate@binkert.orgfor modname in SimObject.modnames:
5955517Snate@binkert.org    exec('from m5.objects import %s' % modname)
5965517Snate@binkert.org
5975517Snate@binkert.org# we need to unload all of the currently imported modules so that they
5985517Snate@binkert.org# will be re-imported the next time the sconscript is run
5995517Snate@binkert.orgimporter.unload()
6005517Snate@binkert.orgsys.meta_path.remove(importer)
6015517Snate@binkert.org
6025517Snate@binkert.orgsim_objects = m5.SimObject.allClasses
6035517Snate@binkert.orgall_enums = m5.params.allEnums
6045517Snate@binkert.org
6055517Snate@binkert.orgfor name,obj in sorted(sim_objects.iteritems()):
6065517Snate@binkert.org    for param in obj._params.local.values():
6075517Snate@binkert.org        # load the ptype attribute now because it depends on the
6085517Snate@binkert.org        # current version of SimObject.allClasses, but when scons
6095517Snate@binkert.org        # actually uses the value, all versions of
6105517Snate@binkert.org        # SimObject.allClasses will have been loaded
6115517Snate@binkert.org        param.ptype
6125517Snate@binkert.org
6135517Snate@binkert.org########################################################################
6145517Snate@binkert.org#
6155517Snate@binkert.org# calculate extra dependencies
6165517Snate@binkert.org#
6175517Snate@binkert.orgmodule_depends = ["m5", "m5.SimObject", "m5.params"]
6185517Snate@binkert.orgdepends = [ PySource.modules[dep].snode for dep in module_depends ]
6195517Snate@binkert.orgdepends.sort(key = lambda x: x.name)
6205517Snate@binkert.org
6215517Snate@binkert.org########################################################################
6225517Snate@binkert.org#
6235517Snate@binkert.org# Commands for the basic automatically generated python files
6245517Snate@binkert.org#
6255517Snate@binkert.org
6265517Snate@binkert.org# Generate Python file containing a dict specifying the current
6275517Snate@binkert.org# buildEnv flags.
6285517Snate@binkert.orgdef makeDefinesPyFile(target, source, env):
6295517Snate@binkert.org    build_env = source[0].get_contents()
6305517Snate@binkert.org
6315517Snate@binkert.org    code = code_formatter()
6325517Snate@binkert.org    code("""
6335517Snate@binkert.orgimport _m5.core
6345517Snate@binkert.orgimport m5.util
6355517Snate@binkert.org
6365517Snate@binkert.orgbuildEnv = m5.util.SmartDict($build_env)
6375517Snate@binkert.org
6385517Snate@binkert.orgcompileDate = _m5.core.compileDate
6395517Snate@binkert.org_globals = globals()
6405517Snate@binkert.orgfor key,val in _m5.core.__dict__.iteritems():
6415517Snate@binkert.org    if key.startswith('flag_'):
6425517Snate@binkert.org        flag = key[5:]
6435517Snate@binkert.org        _globals[flag] = val
6445517Snate@binkert.orgdel _globals
6455517Snate@binkert.org""")
6465517Snate@binkert.org    code.write(target[0].abspath)
6475517Snate@binkert.org
6485517Snate@binkert.orgdefines_info = Value(build_env)
6495517Snate@binkert.org# Generate a file with all of the compile options in it
6505517Snate@binkert.orgenv.Command('python/m5/defines.py', defines_info,
6515517Snate@binkert.org            MakeAction(makeDefinesPyFile, Transform("DEFINES", 0)))
6525517Snate@binkert.orgPySource('m5', 'python/m5/defines.py')
6535517Snate@binkert.org
6545517Snate@binkert.org# Generate python file containing info about the M5 source code
6555517Snate@binkert.orgdef makeInfoPyFile(target, source, env):
6565517Snate@binkert.org    code = code_formatter()
6575517Snate@binkert.org    for src in source:
6585517Snate@binkert.org        data = ''.join(file(src.srcnode().abspath, 'r').xreadlines())
6595517Snate@binkert.org        code('$src = ${{repr(data)}}')
6605517Snate@binkert.org    code.write(str(target[0]))
6615517Snate@binkert.org
6625517Snate@binkert.org# Generate a file that wraps the basic top level files
6635517Snate@binkert.orgenv.Command('python/m5/info.py',
6645517Snate@binkert.org            [ '#/COPYING', '#/LICENSE', '#/README', ],
6655517Snate@binkert.org            MakeAction(makeInfoPyFile, Transform("INFO")))
6665517Snate@binkert.orgPySource('m5', 'python/m5/info.py')
6675517Snate@binkert.org
6685517Snate@binkert.org########################################################################
6695517Snate@binkert.org#
6705517Snate@binkert.org# Create all of the SimObject param headers and enum headers
6715517Snate@binkert.org#
6725517Snate@binkert.org
6735517Snate@binkert.orgdef createSimObjectParamStruct(target, source, env):
6745517Snate@binkert.org    assert len(target) == 1 and len(source) == 1
6755517Snate@binkert.org
6765517Snate@binkert.org    name = source[0].get_text_contents()
6775517Snate@binkert.org    obj = sim_objects[name]
6785517Snate@binkert.org
6795517Snate@binkert.org    code = code_formatter()
6805517Snate@binkert.org    obj.cxx_param_decl(code)
6815517Snate@binkert.org    code.write(target[0].abspath)
6825517Snate@binkert.org
6835517Snate@binkert.orgdef createSimObjectCxxConfig(is_header):
6845517Snate@binkert.org    def body(target, source, env):
6855517Snate@binkert.org        assert len(target) == 1 and len(source) == 1
6865517Snate@binkert.org
6875517Snate@binkert.org        name = str(source[0].get_contents())
6885517Snate@binkert.org        obj = sim_objects[name]
6895517Snate@binkert.org
6905517Snate@binkert.org        code = code_formatter()
6915517Snate@binkert.org        obj.cxx_config_param_file(code, is_header)
6925517Snate@binkert.org        code.write(target[0].abspath)
6935517Snate@binkert.org    return body
6945517Snate@binkert.org
6955517Snate@binkert.orgdef createEnumStrings(target, source, env):
6965517Snate@binkert.org    assert len(target) == 1 and len(source) == 2
6975517Snate@binkert.org
6985517Snate@binkert.org    name = source[0].get_text_contents()
6995517Snate@binkert.org    use_python = source[1].read()
7005517Snate@binkert.org    obj = all_enums[name]
7015517Snate@binkert.org
7025517Snate@binkert.org    code = code_formatter()
7035517Snate@binkert.org    obj.cxx_def(code)
7045517Snate@binkert.org    if use_python:
7055517Snate@binkert.org        obj.pybind_def(code)
7065517Snate@binkert.org    code.write(target[0].abspath)
7075517Snate@binkert.org
7085517Snate@binkert.orgdef createEnumDecls(target, source, env):
7095517Snate@binkert.org    assert len(target) == 1 and len(source) == 1
7105517Snate@binkert.org
7115517Snate@binkert.org    name = source[0].get_text_contents()
7125517Snate@binkert.org    obj = all_enums[name]
7135517Snate@binkert.org
7145517Snate@binkert.org    code = code_formatter()
7155517Snate@binkert.org    obj.cxx_decl(code)
7165517Snate@binkert.org    code.write(target[0].abspath)
7175517Snate@binkert.org
7185517Snate@binkert.orgdef createSimObjectPyBindWrapper(target, source, env):
7195517Snate@binkert.org    name = source[0].get_text_contents()
7205517Snate@binkert.org    obj = sim_objects[name]
7215517Snate@binkert.org
7225517Snate@binkert.org    code = code_formatter()
7235517Snate@binkert.org    obj.pybind_decl(code)
7245517Snate@binkert.org    code.write(target[0].abspath)
7255517Snate@binkert.org
7265517Snate@binkert.org# Generate all of the SimObject param C++ struct header files
7275517Snate@binkert.orgparams_hh_files = []
7285517Snate@binkert.orgfor name,simobj in sorted(sim_objects.iteritems()):
7295517Snate@binkert.org    py_source = PySource.modules[simobj.__module__]
7305517Snate@binkert.org    extra_deps = [ py_source.tnode ]
7315517Snate@binkert.org
7325517Snate@binkert.org    hh_file = File('params/%s.hh' % name)
7335517Snate@binkert.org    params_hh_files.append(hh_file)
7345517Snate@binkert.org    env.Command(hh_file, Value(name),
7355517Snate@binkert.org                MakeAction(createSimObjectParamStruct, Transform("SO PARAM")))
7365517Snate@binkert.org    env.Depends(hh_file, depends + extra_deps)
7375517Snate@binkert.org
7385517Snate@binkert.org# C++ parameter description files
7395517Snate@binkert.orgif GetOption('with_cxx_config'):
7405517Snate@binkert.org    for name,simobj in sorted(sim_objects.iteritems()):
7415517Snate@binkert.org        py_source = PySource.modules[simobj.__module__]
7425517Snate@binkert.org        extra_deps = [ py_source.tnode ]
7435517Snate@binkert.org
7445517Snate@binkert.org        cxx_config_hh_file = File('cxx_config/%s.hh' % name)
7455517Snate@binkert.org        cxx_config_cc_file = File('cxx_config/%s.cc' % name)
7465517Snate@binkert.org        env.Command(cxx_config_hh_file, Value(name),
7475517Snate@binkert.org                    MakeAction(createSimObjectCxxConfig(True),
7485517Snate@binkert.org                    Transform("CXXCPRHH")))
7495517Snate@binkert.org        env.Command(cxx_config_cc_file, Value(name),
7505517Snate@binkert.org                    MakeAction(createSimObjectCxxConfig(False),
7515517Snate@binkert.org                    Transform("CXXCPRCC")))
7525517Snate@binkert.org        env.Depends(cxx_config_hh_file, depends + extra_deps +
7535517Snate@binkert.org                    [File('params/%s.hh' % name), File('sim/cxx_config.hh')])
7545517Snate@binkert.org        env.Depends(cxx_config_cc_file, depends + extra_deps +
7555517Snate@binkert.org                    [cxx_config_hh_file])
7565517Snate@binkert.org        Source(cxx_config_cc_file)
7575517Snate@binkert.org
7585517Snate@binkert.org    cxx_config_init_cc_file = File('cxx_config/init.cc')
7595517Snate@binkert.org
7605517Snate@binkert.org    def createCxxConfigInitCC(target, source, env):
7615517Snate@binkert.org        assert len(target) == 1 and len(source) == 1
7625517Snate@binkert.org
7635517Snate@binkert.org        code = code_formatter()
7645517Snate@binkert.org
7655517Snate@binkert.org        for name,simobj in sorted(sim_objects.iteritems()):
7665517Snate@binkert.org            if not hasattr(simobj, 'abstract') or not simobj.abstract:
7675517Snate@binkert.org                code('#include "cxx_config/${name}.hh"')
7685517Snate@binkert.org        code()
7695517Snate@binkert.org        code('void cxxConfigInit()')
7705517Snate@binkert.org        code('{')
7715517Snate@binkert.org        code.indent()
7725517Snate@binkert.org        for name,simobj in sorted(sim_objects.iteritems()):
7735517Snate@binkert.org            not_abstract = not hasattr(simobj, 'abstract') or \
7745192Ssaidi@eecs.umich.edu                not simobj.abstract
7755517Snate@binkert.org            if not_abstract and 'type' in simobj.__dict__:
7765192Ssaidi@eecs.umich.edu                code('cxx_config_directory["${name}"] = '
7775192Ssaidi@eecs.umich.edu                     '${name}CxxConfigParams::makeDirectoryEntry();')
7785517Snate@binkert.org        code.dedent()
7795517Snate@binkert.org        code('}')
7805192Ssaidi@eecs.umich.edu        code.write(target[0].abspath)
7815192Ssaidi@eecs.umich.edu
7825456Ssaidi@eecs.umich.edu    py_source = PySource.modules[simobj.__module__]
7835517Snate@binkert.org    extra_deps = [ py_source.tnode ]
7845517Snate@binkert.org    env.Command(cxx_config_init_cc_file, Value(name),
7855517Snate@binkert.org        MakeAction(createCxxConfigInitCC, Transform("CXXCINIT")))
7865517Snate@binkert.org    cxx_param_hh_files = ["cxx_config/%s.hh" % simobj
7875517Snate@binkert.org        for name,simobj in sorted(sim_objects.iteritems())
7885517Snate@binkert.org        if not hasattr(simobj, 'abstract') or not simobj.abstract]
7895517Snate@binkert.org    Depends(cxx_config_init_cc_file, cxx_param_hh_files +
7905517Snate@binkert.org            [File('sim/cxx_config.hh')])
7915517Snate@binkert.org    Source(cxx_config_init_cc_file)
7925517Snate@binkert.org
7935517Snate@binkert.org# Generate all enum header files
7945517Snate@binkert.orgfor name,enum in sorted(all_enums.iteritems()):
7955517Snate@binkert.org    py_source = PySource.modules[enum.__module__]
7965517Snate@binkert.org    extra_deps = [ py_source.tnode ]
7975517Snate@binkert.org
7985517Snate@binkert.org    cc_file = File('enums/%s.cc' % name)
7995517Snate@binkert.org    env.Command(cc_file, [Value(name), Value(env['USE_PYTHON'])],
8005517Snate@binkert.org                MakeAction(createEnumStrings, Transform("ENUM STR")))
8015517Snate@binkert.org    env.Depends(cc_file, depends + extra_deps)
8025517Snate@binkert.org    Source(cc_file)
8035517Snate@binkert.org
8045517Snate@binkert.org    hh_file = File('enums/%s.hh' % name)
8055517Snate@binkert.org    env.Command(hh_file, Value(name),
8065517Snate@binkert.org                MakeAction(createEnumDecls, Transform("ENUMDECL")))
8075517Snate@binkert.org    env.Depends(hh_file, depends + extra_deps)
8085517Snate@binkert.org
8095517Snate@binkert.org# Generate SimObject Python bindings wrapper files
8105517Snate@binkert.orgif env['USE_PYTHON']:
8115517Snate@binkert.org    for name,simobj in sorted(sim_objects.iteritems()):
8125517Snate@binkert.org        py_source = PySource.modules[simobj.__module__]
8135517Snate@binkert.org        extra_deps = [ py_source.tnode ]
8145517Snate@binkert.org        cc_file = File('python/_m5/param_%s.cc' % name)
8155456Ssaidi@eecs.umich.edu        env.Command(cc_file, Value(name),
8165461Snate@binkert.org                    MakeAction(createSimObjectPyBindWrapper,
8175517Snate@binkert.org                               Transform("SO PyBind")))
8185456Ssaidi@eecs.umich.edu        env.Depends(cc_file, depends + extra_deps)
8195522Snate@binkert.org        Source(cc_file)
8205522Snate@binkert.org
8215522Snate@binkert.org# Build all protocol buffers if we have got protoc and protobuf available
8225522Snate@binkert.orgif env['HAVE_PROTOBUF']:
8235522Snate@binkert.org    for proto in ProtoBuf.all:
8245522Snate@binkert.org        # Use both the source and header as the target, and the .proto
8255522Snate@binkert.org        # file as the source. When executing the protoc compiler, also
8265522Snate@binkert.org        # specify the proto_path to avoid having the generated files
8275522Snate@binkert.org        # include the path.
8285517Snate@binkert.org        env.Command([proto.cc_file, proto.hh_file], proto.tnode,
8295522Snate@binkert.org                    MakeAction('$PROTOC --cpp_out ${TARGET.dir} '
8305522Snate@binkert.org                               '--proto_path ${SOURCE.dir} $SOURCE',
8315522Snate@binkert.org                               Transform("PROTOC")))
8325522Snate@binkert.org
8335517Snate@binkert.org        # Add the C++ source file
8345522Snate@binkert.org        Source(proto.cc_file, tags=proto.tags)
8355522Snate@binkert.orgelif ProtoBuf.all:
8365517Snate@binkert.org    print('Got protobuf to build, but lacks support!')
8375522Snate@binkert.org    Exit(1)
8385522Snate@binkert.org
8395522Snate@binkert.org#
8405522Snate@binkert.org# Handle debug flags
8415522Snate@binkert.org#
8425517Snate@binkert.orgdef makeDebugFlagCC(target, source, env):
8435522Snate@binkert.org    assert(len(target) == 1 and len(source) == 1)
8445522Snate@binkert.org
8455522Snate@binkert.org    code = code_formatter()
8465522Snate@binkert.org
8475522Snate@binkert.org    # delay definition of CompoundFlags until after all the definition
8485522Snate@binkert.org    # of all constituent SimpleFlags
8495522Snate@binkert.org    comp_code = code_formatter()
8505522Snate@binkert.org
8515522Snate@binkert.org    # file header
8525522Snate@binkert.org    code('''
8535522Snate@binkert.org/*
8545522Snate@binkert.org * DO NOT EDIT THIS FILE! Automatically generated by SCons.
8555522Snate@binkert.org */
8565522Snate@binkert.org
8575522Snate@binkert.org#include "base/debug.hh"
8585522Snate@binkert.org
8595522Snate@binkert.orgnamespace Debug {
8605522Snate@binkert.org
8615522Snate@binkert.org''')
8624382Sbinkertn@umich.edu
8635522Snate@binkert.org    for name, flag in sorted(source[0].read().iteritems()):
8645522Snate@binkert.org        n, compound, desc = flag
8654382Sbinkertn@umich.edu        assert n == name
8665522Snate@binkert.org
8675522Snate@binkert.org        if not compound:
8685522Snate@binkert.org            code('SimpleFlag $name("$name", "$desc");')
8695522Snate@binkert.org        else:
8705522Snate@binkert.org            comp_code('CompoundFlag $name("$name", "$desc",')
8715522Snate@binkert.org            comp_code.indent()
8725522Snate@binkert.org            last = len(compound) - 1
8735522Snate@binkert.org            for i,flag in enumerate(compound):
8745522Snate@binkert.org                if i != last:
8755522Snate@binkert.org                    comp_code('&$flag,')
8764382Sbinkertn@umich.edu                else:
8775522Snate@binkert.org                    comp_code('&$flag);')
8785522Snate@binkert.org            comp_code.dedent()
8795522Snate@binkert.org
8805522Snate@binkert.org    code.append(comp_code)
8815522Snate@binkert.org    code()
8825522Snate@binkert.org    code('} // namespace Debug')
8835522Snate@binkert.org
8845522Snate@binkert.org    code.write(str(target[0]))
8855522Snate@binkert.org
8865522Snate@binkert.orgdef makeDebugFlagHH(target, source, env):
8875522Snate@binkert.org    assert(len(target) == 1 and len(source) == 1)
8885522Snate@binkert.org
8895522Snate@binkert.org    val = eval(source[0].get_contents())
8905522Snate@binkert.org    name, compound, desc = val
8915522Snate@binkert.org
8925522Snate@binkert.org    code = code_formatter()
8935522Snate@binkert.org
8945522Snate@binkert.org    # file header boilerplate
8955522Snate@binkert.org    code('''\
8965522Snate@binkert.org/*
8975522Snate@binkert.org * DO NOT EDIT THIS FILE! Automatically generated by SCons.
8985522Snate@binkert.org */
8995522Snate@binkert.org
9005522Snate@binkert.org#ifndef __DEBUG_${name}_HH__
9015522Snate@binkert.org#define __DEBUG_${name}_HH__
9025522Snate@binkert.org
9035522Snate@binkert.orgnamespace Debug {
9045522Snate@binkert.org''')
9055522Snate@binkert.org
9065522Snate@binkert.org    if compound:
9075522Snate@binkert.org        code('class CompoundFlag;')
9084382Sbinkertn@umich.edu    code('class SimpleFlag;')
9094382Sbinkertn@umich.edu
9104382Sbinkertn@umich.edu    if compound:
9114382Sbinkertn@umich.edu        code('extern CompoundFlag $name;')
9124382Sbinkertn@umich.edu        for flag in compound:
9134382Sbinkertn@umich.edu            code('extern SimpleFlag $flag;')
9144382Sbinkertn@umich.edu    else:
9154382Sbinkertn@umich.edu        code('extern SimpleFlag $name;')
9164382Sbinkertn@umich.edu
9174382Sbinkertn@umich.edu    code('''
918955SN/A}
919955SN/A
920955SN/A#endif // __DEBUG_${name}_HH__
921955SN/A''')
9221108SN/A
923955SN/A    code.write(str(target[0]))
924955SN/A
9255456Ssaidi@eecs.umich.edufor name,flag in sorted(debug_flags.iteritems()):
926955SN/A    n, compound, desc = flag
927955SN/A    assert n == name
928955SN/A
9295456Ssaidi@eecs.umich.edu    hh_file = 'debug/%s.hh' % name
9305456Ssaidi@eecs.umich.edu    env.Command(hh_file, Value(flag),
9315456Ssaidi@eecs.umich.edu                MakeAction(makeDebugFlagHH, Transform("TRACING", 0)))
9325456Ssaidi@eecs.umich.edu
9335456Ssaidi@eecs.umich.eduenv.Command('debug/flags.cc', Value(debug_flags),
9345456Ssaidi@eecs.umich.edu            MakeAction(makeDebugFlagCC, Transform("TRACING", 0)))
935955SN/ASource('debug/flags.cc')
9365456Ssaidi@eecs.umich.edu
9375456Ssaidi@eecs.umich.edu# version tags
938955SN/Atags = \
939955SN/Aenv.Command('sim/tags.cc', None,
9402655Sstever@eecs.umich.edu            MakeAction('util/cpt_upgrader.py --get-cc-file > $TARGET',
9412655Sstever@eecs.umich.edu                       Transform("VER TAGS")))
9422655Sstever@eecs.umich.eduenv.AlwaysBuild(tags)
9432655Sstever@eecs.umich.edu
9442655Sstever@eecs.umich.edu# Embed python files.  All .py files that have been indicated by a
9452655Sstever@eecs.umich.edu# PySource() call in a SConscript need to be embedded into the M5
9462655Sstever@eecs.umich.edu# library.  To do that, we compile the file to byte code, marshal the
9472655Sstever@eecs.umich.edu# byte code, compress it, and then generate a c++ file that
9485522Snate@binkert.org# inserts the result into an array.
9495554Snate@binkert.orgdef embedPyFile(target, source, env):
9505554Snate@binkert.org    def c_str(string):
9515554Snate@binkert.org        if string is None:
9525554Snate@binkert.org            return "0"
9535522Snate@binkert.org        return '"%s"' % string
9545522Snate@binkert.org
9555522Snate@binkert.org    '''Action function to compile a .py into a code object, marshal
9565522Snate@binkert.org    it, compress it, and stick it into an asm file so the code appears
9575522Snate@binkert.org    as just bytes with a label in the data section'''
9585554Snate@binkert.org
9595554Snate@binkert.org    src = file(str(source[0]), 'r').read()
9605554Snate@binkert.org
9615522Snate@binkert.org    pysource = PySource.tnodes[source[0]]
9625522Snate@binkert.org    compiled = compile(src, pysource.abspath, 'exec')
9632655Sstever@eecs.umich.edu    marshalled = marshal.dumps(compiled)
9645522Snate@binkert.org    compressed = zlib.compress(marshalled)
9652655Sstever@eecs.umich.edu    data = compressed
9665522Snate@binkert.org    sym = pysource.symname
9675522Snate@binkert.org
9684007Ssaidi@eecs.umich.edu    code = code_formatter()
9694596Sbinkertn@umich.edu    code('''\
9704007Ssaidi@eecs.umich.edu#include "sim/init.hh"
9714596Sbinkertn@umich.edu
9725522Snate@binkert.orgnamespace {
9735522Snate@binkert.org
9745522Snate@binkert.orgconst uint8_t data_${sym}[] = {
9755522Snate@binkert.org''')
9762655Sstever@eecs.umich.edu    code.indent()
9772655Sstever@eecs.umich.edu    step = 16
9782655Sstever@eecs.umich.edu    for i in xrange(0, len(data), step):
979955SN/A        x = array.array('B', data[i:i+step])
9803918Ssaidi@eecs.umich.edu        code(''.join('%d,' % d for d in x))
9813918Ssaidi@eecs.umich.edu    code.dedent()
9823918Ssaidi@eecs.umich.edu
9833918Ssaidi@eecs.umich.edu    code('''};
9843918Ssaidi@eecs.umich.edu
9853918Ssaidi@eecs.umich.eduEmbeddedPython embedded_${sym}(
9863918Ssaidi@eecs.umich.edu    ${{c_str(pysource.arcname)}},
9873918Ssaidi@eecs.umich.edu    ${{c_str(pysource.abspath)}},
9883918Ssaidi@eecs.umich.edu    ${{c_str(pysource.modpath)}},
9893918Ssaidi@eecs.umich.edu    data_${sym},
9903918Ssaidi@eecs.umich.edu    ${{len(data)}},
9913918Ssaidi@eecs.umich.edu    ${{len(marshalled)}});
9923918Ssaidi@eecs.umich.edu
9933918Ssaidi@eecs.umich.edu} // anonymous namespace
9943940Ssaidi@eecs.umich.edu''')
9953940Ssaidi@eecs.umich.edu    code.write(str(target[0]))
9963940Ssaidi@eecs.umich.edu
9973942Ssaidi@eecs.umich.edufor source in PySource.all:
9983940Ssaidi@eecs.umich.edu    env.Command(source.cpp, source.tnode,
9993515Ssaidi@eecs.umich.edu                MakeAction(embedPyFile, Transform("EMBED PY")))
10003918Ssaidi@eecs.umich.edu    Source(source.cpp, tags=source.tags, add_tags='python')
10014762Snate@binkert.org
10023515Ssaidi@eecs.umich.edu########################################################################
10032655Sstever@eecs.umich.edu#
10043918Ssaidi@eecs.umich.edu# Define binaries.  Each different build type (debug, opt, etc.) gets
10053619Sbinkertn@umich.edu# a slightly different build environment.
1006955SN/A#
1007955SN/A
10082655Sstever@eecs.umich.edu# List of constructed environments to pass back to SConstruct
10093918Ssaidi@eecs.umich.edudate_source = Source('base/date.cc', tags=[])
10103619Sbinkertn@umich.edu
1011955SN/A# Function to create a new build environment as clone of current
1012955SN/A# environment 'env' with modified object suffix and optional stripped
10132655Sstever@eecs.umich.edu# binary.  Additional keyword arguments are appended to corresponding
10143918Ssaidi@eecs.umich.edu# build environment vars.
10153619Sbinkertn@umich.edudef makeEnv(env, label, objsfx, strip=False, disable_partial=False, **kwargs):
1016955SN/A    # SCons doesn't know to append a library suffix when there is a '.' in the
1017955SN/A    # name.  Use '_' instead.
10182655Sstever@eecs.umich.edu    libname = 'gem5_' + label
10193918Ssaidi@eecs.umich.edu    exename = 'gem5.' + label
10203683Sstever@eecs.umich.edu    secondary_exename = 'm5.' + label
10212655Sstever@eecs.umich.edu
10221869SN/A    new_env = env.Clone(OBJSUFFIX=objsfx, SHOBJSUFFIX=objsfx + 's')
10231869SN/A    new_env.Label = label
1024    new_env.Append(**kwargs)
1025
1026    lib_sources = Source.all.with_tag('gem5 lib')
1027
1028    # Without Python, leave out all Python content from the library
1029    # builds.  The option doesn't affect gem5 built as a program
1030    if GetOption('without_python'):
1031        lib_sources = lib_sources.without_tag('python')
1032
1033    static_objs = []
1034    shared_objs = []
1035
1036    for s in lib_sources.with_tag(Source.ungrouped_tag):
1037        static_objs.append(s.static(new_env))
1038        shared_objs.append(s.shared(new_env))
1039
1040    for group in Source.source_groups:
1041        srcs = lib_sources.with_tag(Source.link_group_tag(group))
1042        if not srcs:
1043            continue
1044
1045        group_static = [ s.static(new_env) for s in srcs ]
1046        group_shared = [ s.shared(new_env) for s in srcs ]
1047
1048        # If partial linking is disabled, add these sources to the build
1049        # directly, and short circuit this loop.
1050        if disable_partial:
1051            static_objs.extend(group_static)
1052            shared_objs.extend(group_shared)
1053            continue
1054
1055        # Set up the static partially linked objects.
1056        file_name = new_env.subst("${OBJPREFIX}lib${OBJSUFFIX}.partial")
1057        target = File(joinpath(group, file_name))
1058        partial = env.PartialStatic(target=target, source=group_static)
1059        static_objs.extend(partial)
1060
1061        # Set up the shared partially linked objects.
1062        file_name = new_env.subst("${SHOBJPREFIX}lib${SHOBJSUFFIX}.partial")
1063        target = File(joinpath(group, file_name))
1064        partial = env.PartialShared(target=target, source=group_shared)
1065        shared_objs.extend(partial)
1066
1067    static_date = date_source.static(new_env)
1068    new_env.Depends(static_date, static_objs)
1069    static_objs.extend(static_date)
1070
1071    shared_date = date_source.shared(new_env)
1072    new_env.Depends(shared_date, shared_objs)
1073    shared_objs.extend(shared_date)
1074
1075    # First make a library of everything but main() so other programs can
1076    # link against m5.
1077    static_lib = new_env.StaticLibrary(libname, static_objs)
1078    shared_lib = new_env.SharedLibrary(libname, shared_objs)
1079
1080    # Now link a stub with main() and the static library.
1081    main_objs = [ s.static(new_env) for s in Source.all.with_tag('main') ]
1082
1083    for test in UnitTest.all:
1084        test_sources = list(test.sources)
1085        for f in test.filters:
1086            test_sources += Source.all.apply_filter(f)
1087        test_objs = [ s.static(new_env) for s in test_sources ]
1088        if test.main:
1089            test_objs += main_objs
1090        new_env.Program(test.dir.File('%s.%s' % (test.target, label)),
1091                        test_objs + static_objs)
1092
1093    gtest_env = new_env.Clone()
1094    gtest_env.Append(LIBS=gtest_env['GTEST_LIBS'])
1095    gtest_env.Append(CPPFLAGS=gtest_env['GTEST_CPPFLAGS'])
1096    gtestlib_sources = Source.all.with_tag('gtest lib')
1097    gtest_out_dir = Dir(new_env['BUILDDIR']).Dir('unittests.%s' % label)
1098    for test in GTest.all:
1099        test_sources = list(test.sources)
1100        if not test.skip_lib:
1101            test_sources += gtestlib_sources
1102        for f in test.filters:
1103            test_sources += Source.all.apply_filter(f)
1104        test_objs = [ s.static(gtest_env) for s in test_sources ]
1105        test_binary = gtest_env.Program(
1106            test.dir.File('%s.%s' % (test.target, label)), test_objs)
1107
1108        AlwaysBuild(gtest_env.Command(
1109            gtest_out_dir.File("%s/%s.xml" % (test.dir, test.target)),
1110            test_binary, "${SOURCES[0]} --gtest_output=xml:${TARGETS[0]}"))
1111
1112    progname = exename
1113    if strip:
1114        progname += '.unstripped'
1115
1116    targets = new_env.Program(progname, main_objs + static_objs)
1117
1118    if strip:
1119        if sys.platform == 'sunos5':
1120            cmd = 'cp $SOURCE $TARGET; strip $TARGET'
1121        else:
1122            cmd = 'strip $SOURCE -o $TARGET'
1123        targets = new_env.Command(exename, progname,
1124                    MakeAction(cmd, Transform("STRIP")))
1125
1126    new_env.Command(secondary_exename, exename,
1127            MakeAction('ln $SOURCE $TARGET', Transform("HARDLINK")))
1128
1129    new_env.M5Binary = targets[0]
1130
1131    # Set up regression tests.
1132    SConscript(os.path.join(env.root.abspath, 'tests', 'SConscript'),
1133               variant_dir=Dir('tests').Dir(new_env.Label),
1134               exports={ 'env' : new_env }, duplicate=False)
1135
1136# Start out with the compiler flags common to all compilers,
1137# i.e. they all use -g for opt and -g -pg for prof
1138ccflags = {'debug' : [], 'opt' : ['-g'], 'fast' : [], 'prof' : ['-g', '-pg'],
1139           'perf' : ['-g']}
1140
1141# Start out with the linker flags common to all linkers, i.e. -pg for
1142# prof, and -lprofiler for perf. The -lprofile flag is surrounded by
1143# no-as-needed and as-needed as the binutils linker is too clever and
1144# simply doesn't link to the library otherwise.
1145ldflags = {'debug' : [], 'opt' : [], 'fast' : [], 'prof' : ['-pg'],
1146           'perf' : ['-Wl,--no-as-needed', '-lprofiler', '-Wl,--as-needed']}
1147
1148# For Link Time Optimization, the optimisation flags used to compile
1149# individual files are decoupled from those used at link time
1150# (i.e. you can compile with -O3 and perform LTO with -O0), so we need
1151# to also update the linker flags based on the target.
1152if env['GCC']:
1153    if sys.platform == 'sunos5':
1154        ccflags['debug'] += ['-gstabs+']
1155    else:
1156        ccflags['debug'] += ['-ggdb3']
1157    ldflags['debug'] += ['-O0']
1158    # opt, fast, prof and perf all share the same cc flags, also add
1159    # the optimization to the ldflags as LTO defers the optimization
1160    # to link time
1161    for target in ['opt', 'fast', 'prof', 'perf']:
1162        ccflags[target] += ['-O3']
1163        ldflags[target] += ['-O3']
1164
1165    ccflags['fast'] += env['LTO_CCFLAGS']
1166    ldflags['fast'] += env['LTO_LDFLAGS']
1167elif env['CLANG']:
1168    ccflags['debug'] += ['-g', '-O0']
1169    # opt, fast, prof and perf all share the same cc flags
1170    for target in ['opt', 'fast', 'prof', 'perf']:
1171        ccflags[target] += ['-O3']
1172else:
1173    print('Unknown compiler, please fix compiler options')
1174    Exit(1)
1175
1176
1177# To speed things up, we only instantiate the build environments we
1178# need.  We try to identify the needed environment for each target; if
1179# we can't, we fall back on instantiating all the environments just to
1180# be safe.
1181target_types = ['debug', 'opt', 'fast', 'prof', 'perf']
1182obj2target = {'do': 'debug', 'o': 'opt', 'fo': 'fast', 'po': 'prof',
1183              'gpo' : 'perf'}
1184
1185def identifyTarget(t):
1186    ext = t.split('.')[-1]
1187    if ext in target_types:
1188        return ext
1189    if obj2target.has_key(ext):
1190        return obj2target[ext]
1191    match = re.search(r'/tests/([^/]+)/', t)
1192    if match and match.group(1) in target_types:
1193        return match.group(1)
1194    return 'all'
1195
1196needed_envs = [identifyTarget(target) for target in BUILD_TARGETS]
1197if 'all' in needed_envs:
1198    needed_envs += target_types
1199
1200# Debug binary
1201if 'debug' in needed_envs:
1202    makeEnv(env, 'debug', '.do',
1203            CCFLAGS = Split(ccflags['debug']),
1204            CPPDEFINES = ['DEBUG', 'TRACING_ON=1'],
1205            LINKFLAGS = Split(ldflags['debug']))
1206
1207# Optimized binary
1208if 'opt' in needed_envs:
1209    makeEnv(env, 'opt', '.o',
1210            CCFLAGS = Split(ccflags['opt']),
1211            CPPDEFINES = ['TRACING_ON=1'],
1212            LINKFLAGS = Split(ldflags['opt']))
1213
1214# "Fast" binary
1215if 'fast' in needed_envs:
1216    disable_partial = \
1217            env.get('BROKEN_INCREMENTAL_LTO', False) and \
1218            GetOption('force_lto')
1219    makeEnv(env, 'fast', '.fo', strip = True,
1220            CCFLAGS = Split(ccflags['fast']),
1221            CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1222            LINKFLAGS = Split(ldflags['fast']),
1223            disable_partial=disable_partial)
1224
1225# Profiled binary using gprof
1226if 'prof' in needed_envs:
1227    makeEnv(env, 'prof', '.po',
1228            CCFLAGS = Split(ccflags['prof']),
1229            CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1230            LINKFLAGS = Split(ldflags['prof']))
1231
1232# Profiled binary using google-pprof
1233if 'perf' in needed_envs:
1234    makeEnv(env, 'perf', '.gpo',
1235            CCFLAGS = Split(ccflags['perf']),
1236            CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1237            LINKFLAGS = Split(ldflags['perf']))
1238