SConscript revision 12371
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.orgimport array
326143Snate@binkert.orgimport bisect
3312371Sgabeblack@google.comimport functools
344762Snate@binkert.orgimport imp
355522Snate@binkert.orgimport marshal
36955SN/Aimport os
375522Snate@binkert.orgimport re
3811974Sgabeblack@google.comimport subprocess
39955SN/Aimport sys
405522Snate@binkert.orgimport zlib
414202Sbinkertn@umich.edu
425742Snate@binkert.orgfrom os.path import basename, dirname, exists, isdir, isfile, join as joinpath
43955SN/A
444381Sbinkertn@umich.eduimport SCons
454381Sbinkertn@umich.edu
4612246Sgabeblack@google.comfrom gem5_scons import Transform
4712246Sgabeblack@google.com
488334Snate@binkert.org# This file defines how to build a particular configuration of gem5
49955SN/A# based on variable settings in the 'env' build environment.
50955SN/A
514202Sbinkertn@umich.eduImport('*')
52955SN/A
534382Sbinkertn@umich.edu# Children need to see the environment
544382Sbinkertn@umich.eduExport('env')
554382Sbinkertn@umich.edu
566654Snate@binkert.orgbuild_env = [(opt, env[opt]) for opt in export_vars]
575517Snate@binkert.org
588614Sgblack@eecs.umich.edufrom m5.util import code_formatter, compareVersions
597674Snate@binkert.org
606143Snate@binkert.org########################################################################
616143Snate@binkert.org# Code for adding source files of various types
626143Snate@binkert.org#
6312302Sgabeblack@google.com# When specifying a source file of some type, a set of tags can be
6412302Sgabeblack@google.com# specified for that file.
6512302Sgabeblack@google.com
6612371Sgabeblack@google.comclass SourceFilter(object):
6712371Sgabeblack@google.com    def __init__(self, predicate):
6812371Sgabeblack@google.com        self.predicate = predicate
6912371Sgabeblack@google.com
7012371Sgabeblack@google.com    def __or__(self, other):
7112371Sgabeblack@google.com        return SourceFilter(lambda tags: self.predicate(tags) or
7212371Sgabeblack@google.com                                         other.predicate(tags))
7312371Sgabeblack@google.com
7412371Sgabeblack@google.com    def __and__(self, other):
7512371Sgabeblack@google.com        return SourceFilter(lambda tags: self.predicate(tags) and
7612371Sgabeblack@google.com                                         other.predicate(tags))
7712371Sgabeblack@google.com
7812371Sgabeblack@google.comdef with_tags_that(predicate):
7912371Sgabeblack@google.com    '''Return a list of sources with tags that satisfy a predicate.'''
8012371Sgabeblack@google.com    return SourceFilter(predicate)
8112371Sgabeblack@google.com
8212371Sgabeblack@google.comdef with_any_tags(*tags):
8312371Sgabeblack@google.com    '''Return a list of sources with any of the supplied tags.'''
8412371Sgabeblack@google.com    return SourceFilter(lambda stags: len(set(tags) & stags) > 0)
8512371Sgabeblack@google.com
8612371Sgabeblack@google.comdef with_all_tags(*tags):
8712371Sgabeblack@google.com    '''Return a list of sources with all of the supplied tags.'''
8812371Sgabeblack@google.com    return SourceFilter(lambda stags: set(tags) <= stags)
8912371Sgabeblack@google.com
9012371Sgabeblack@google.comdef with_tag(tag):
9112371Sgabeblack@google.com    '''Return a list of sources with the supplied tag.'''
9212371Sgabeblack@google.com    return SourceFilter(lambda stags: tag in stags)
9312371Sgabeblack@google.com
9412371Sgabeblack@google.comdef without_tags(*tags):
9512371Sgabeblack@google.com    '''Return a list of sources without any of the supplied tags.'''
9612371Sgabeblack@google.com    return SourceFilter(lambda stags: len(set(tags) & stags) == 0)
9712371Sgabeblack@google.com
9812371Sgabeblack@google.comdef without_tag(tag):
9912371Sgabeblack@google.com    '''Return a list of sources with the supplied tag.'''
10012371Sgabeblack@google.com    return SourceFilter(lambda stags: tag not in stags)
10112371Sgabeblack@google.com
10212371Sgabeblack@google.comsource_filter_factories = {
10312371Sgabeblack@google.com    'with_tags_that': with_tags_that,
10412371Sgabeblack@google.com    'with_any_tags': with_any_tags,
10512371Sgabeblack@google.com    'with_all_tags': with_all_tags,
10612371Sgabeblack@google.com    'with_tag': with_tag,
10712371Sgabeblack@google.com    'without_tags': without_tags,
10812371Sgabeblack@google.com    'without_tag': without_tag,
10912371Sgabeblack@google.com}
11012371Sgabeblack@google.com
11112371Sgabeblack@google.comExport(source_filter_factories)
11212371Sgabeblack@google.com
11312302Sgabeblack@google.comclass SourceList(list):
11412371Sgabeblack@google.com    def apply_filter(self, f):
11512302Sgabeblack@google.com        def match(source):
11612371Sgabeblack@google.com            return f.predicate(source.tags)
11712302Sgabeblack@google.com        return SourceList(filter(match, self))
11812302Sgabeblack@google.com
11912371Sgabeblack@google.com    def __getattr__(self, name):
12012371Sgabeblack@google.com        func = source_filter_factories.get(name, None)
12112371Sgabeblack@google.com        if not func:
12212371Sgabeblack@google.com            raise AttributeError
12312302Sgabeblack@google.com
12412371Sgabeblack@google.com        @functools.wraps(func)
12512371Sgabeblack@google.com        def wrapper(*args, **kwargs):
12612371Sgabeblack@google.com            return self.apply_filter(func(*args, **kwargs))
12712371Sgabeblack@google.com        return wrapper
12811983Sgabeblack@google.com
1296143Snate@binkert.orgclass SourceMeta(type):
1308233Snate@binkert.org    '''Meta class for source files that keeps track of all files of a
13112302Sgabeblack@google.com    particular type.'''
1326143Snate@binkert.org    def __init__(cls, name, bases, dict):
1336143Snate@binkert.org        super(SourceMeta, cls).__init__(name, bases, dict)
13412302Sgabeblack@google.com        cls.all = SourceList()
1354762Snate@binkert.org
1366143Snate@binkert.orgclass SourceFile(object):
1378233Snate@binkert.org    '''Base object that encapsulates the notion of a source file.
1388233Snate@binkert.org    This includes, the source node, target node, various manipulations
13912302Sgabeblack@google.com    of those.  A source file also specifies a set of tags which
14012302Sgabeblack@google.com    describing arbitrary properties of the source file.'''
1416143Snate@binkert.org    __metaclass__ = SourceMeta
14212362Sgabeblack@google.com
14312362Sgabeblack@google.com    static_objs = {}
14412362Sgabeblack@google.com    shared_objs = {}
14512362Sgabeblack@google.com
14612302Sgabeblack@google.com    def __init__(self, source, tags=None, add_tags=None):
14712302Sgabeblack@google.com        if tags is None:
14812302Sgabeblack@google.com            tags='gem5 lib'
14912302Sgabeblack@google.com        if isinstance(tags, basestring):
15012302Sgabeblack@google.com            tags = set([tags])
15112363Sgabeblack@google.com        if not isinstance(tags, set):
15212363Sgabeblack@google.com            tags = set(tags)
15312363Sgabeblack@google.com        self.tags = tags
15412363Sgabeblack@google.com
15512302Sgabeblack@google.com        if add_tags:
15612363Sgabeblack@google.com            if isinstance(add_tags, basestring):
15712363Sgabeblack@google.com                add_tags = set([add_tags])
15812363Sgabeblack@google.com            if not isinstance(add_tags, set):
15912363Sgabeblack@google.com                add_tags = set(add_tags)
16012363Sgabeblack@google.com            self.tags |= add_tags
1618233Snate@binkert.org
1626143Snate@binkert.org        tnode = source
1636143Snate@binkert.org        if not isinstance(source, SCons.Node.FS.File):
1646143Snate@binkert.org            tnode = File(source)
1656143Snate@binkert.org
1666143Snate@binkert.org        self.tnode = tnode
1676143Snate@binkert.org        self.snode = tnode.srcnode()
1686143Snate@binkert.org
1696143Snate@binkert.org        for base in type(self).__mro__:
1706143Snate@binkert.org            if issubclass(base, SourceFile):
1717065Snate@binkert.org                base.all.append(self)
1726143Snate@binkert.org
17312362Sgabeblack@google.com    def static(self, env):
17412362Sgabeblack@google.com        key = (self.tnode, env['OBJSUFFIX'])
17512362Sgabeblack@google.com        if not key in self.static_objs:
17612362Sgabeblack@google.com            self.static_objs[key] = env.StaticObject(self.tnode)
17712362Sgabeblack@google.com        return self.static_objs[key]
17812362Sgabeblack@google.com
17912362Sgabeblack@google.com    def shared(self, env):
18012362Sgabeblack@google.com        key = (self.tnode, env['OBJSUFFIX'])
18112362Sgabeblack@google.com        if not key in self.shared_objs:
18212362Sgabeblack@google.com            self.shared_objs[key] = env.SharedObject(self.tnode)
18312362Sgabeblack@google.com        return self.shared_objs[key]
18412362Sgabeblack@google.com
1858233Snate@binkert.org    @property
1868233Snate@binkert.org    def filename(self):
1878233Snate@binkert.org        return str(self.tnode)
1888233Snate@binkert.org
1898233Snate@binkert.org    @property
1908233Snate@binkert.org    def dirname(self):
1918233Snate@binkert.org        return dirname(self.filename)
1928233Snate@binkert.org
1938233Snate@binkert.org    @property
1948233Snate@binkert.org    def basename(self):
1958233Snate@binkert.org        return basename(self.filename)
1968233Snate@binkert.org
1978233Snate@binkert.org    @property
1988233Snate@binkert.org    def extname(self):
1998233Snate@binkert.org        index = self.basename.rfind('.')
2008233Snate@binkert.org        if index <= 0:
2018233Snate@binkert.org            # dot files aren't extensions
2028233Snate@binkert.org            return self.basename, None
2038233Snate@binkert.org
2048233Snate@binkert.org        return self.basename[:index], self.basename[index+1:]
2058233Snate@binkert.org
2066143Snate@binkert.org    def __lt__(self, other): return self.filename < other.filename
2076143Snate@binkert.org    def __le__(self, other): return self.filename <= other.filename
2086143Snate@binkert.org    def __gt__(self, other): return self.filename > other.filename
2096143Snate@binkert.org    def __ge__(self, other): return self.filename >= other.filename
2106143Snate@binkert.org    def __eq__(self, other): return self.filename == other.filename
2116143Snate@binkert.org    def __ne__(self, other): return self.filename != other.filename
2129982Satgutier@umich.edu
2136143Snate@binkert.orgclass Source(SourceFile):
21412302Sgabeblack@google.com    ungrouped_tag = 'No link group'
21512302Sgabeblack@google.com    source_groups = set()
21612302Sgabeblack@google.com
21712302Sgabeblack@google.com    _current_group_tag = ungrouped_tag
21812302Sgabeblack@google.com
21912302Sgabeblack@google.com    @staticmethod
22012302Sgabeblack@google.com    def link_group_tag(group):
22112302Sgabeblack@google.com        return 'link group: %s' % group
22211983Sgabeblack@google.com
22311983Sgabeblack@google.com    @classmethod
22411983Sgabeblack@google.com    def set_group(cls, group):
22512302Sgabeblack@google.com        new_tag = Source.link_group_tag(group)
22612302Sgabeblack@google.com        Source._current_group_tag = new_tag
22712302Sgabeblack@google.com        Source.source_groups.add(group)
22812302Sgabeblack@google.com
22912302Sgabeblack@google.com    def _add_link_group_tag(self):
23012302Sgabeblack@google.com        self.tags.add(Source._current_group_tag)
23111983Sgabeblack@google.com
2326143Snate@binkert.org    '''Add a c/c++ source file to the build'''
23312305Sgabeblack@google.com    def __init__(self, source, tags=None, add_tags=None):
23412302Sgabeblack@google.com        '''specify the source file, and any tags'''
23512302Sgabeblack@google.com        super(Source, self).__init__(source, tags, add_tags)
23612302Sgabeblack@google.com        self._add_link_group_tag()
2376143Snate@binkert.org
2386143Snate@binkert.orgclass PySource(SourceFile):
2396143Snate@binkert.org    '''Add a python source file to the named package'''
2405522Snate@binkert.org    invalid_sym_char = re.compile('[^A-z0-9_]')
2416143Snate@binkert.org    modules = {}
2426143Snate@binkert.org    tnodes = {}
2436143Snate@binkert.org    symnames = {}
2449982Satgutier@umich.edu
24512302Sgabeblack@google.com    def __init__(self, package, source, tags=None, add_tags=None):
24612302Sgabeblack@google.com        '''specify the python package, the source file, and any tags'''
24712302Sgabeblack@google.com        super(PySource, self).__init__(source, tags, add_tags)
2486143Snate@binkert.org
2496143Snate@binkert.org        modname,ext = self.extname
2506143Snate@binkert.org        assert ext == 'py'
2516143Snate@binkert.org
2525522Snate@binkert.org        if package:
2535522Snate@binkert.org            path = package.split('.')
2545522Snate@binkert.org        else:
2555522Snate@binkert.org            path = []
2565604Snate@binkert.org
2575604Snate@binkert.org        modpath = path[:]
2586143Snate@binkert.org        if modname != '__init__':
2596143Snate@binkert.org            modpath += [ modname ]
2604762Snate@binkert.org        modpath = '.'.join(modpath)
2614762Snate@binkert.org
2626143Snate@binkert.org        arcpath = path + [ self.basename ]
2636727Ssteve.reinhardt@amd.com        abspath = self.snode.abspath
2646727Ssteve.reinhardt@amd.com        if not exists(abspath):
2656727Ssteve.reinhardt@amd.com            abspath = self.tnode.abspath
2664762Snate@binkert.org
2676143Snate@binkert.org        self.package = package
2686143Snate@binkert.org        self.modname = modname
2696143Snate@binkert.org        self.modpath = modpath
2706143Snate@binkert.org        self.arcname = joinpath(*arcpath)
2716727Ssteve.reinhardt@amd.com        self.abspath = abspath
2726143Snate@binkert.org        self.compiled = File(self.filename + 'c')
2737674Snate@binkert.org        self.cpp = File(self.filename + '.cc')
2747674Snate@binkert.org        self.symname = PySource.invalid_sym_char.sub('_', modpath)
2755604Snate@binkert.org
2766143Snate@binkert.org        PySource.modules[modpath] = self
2776143Snate@binkert.org        PySource.tnodes[self.tnode] = self
2786143Snate@binkert.org        PySource.symnames[self.symname] = self
2794762Snate@binkert.org
2806143Snate@binkert.orgclass SimObject(PySource):
2814762Snate@binkert.org    '''Add a SimObject python file as a python source object and add
2824762Snate@binkert.org    it to a list of sim object modules'''
2834762Snate@binkert.org
2846143Snate@binkert.org    fixed = False
2856143Snate@binkert.org    modnames = []
2864762Snate@binkert.org
28712302Sgabeblack@google.com    def __init__(self, source, tags=None, add_tags=None):
28812302Sgabeblack@google.com        '''Specify the source file and any tags (automatically in
2898233Snate@binkert.org        the m5.objects package)'''
29012302Sgabeblack@google.com        super(SimObject, self).__init__('m5.objects', source, tags, add_tags)
2916143Snate@binkert.org        if self.fixed:
2926143Snate@binkert.org            raise AttributeError, "Too late to call SimObject now."
2934762Snate@binkert.org
2946143Snate@binkert.org        bisect.insort_right(SimObject.modnames, self.modname)
2954762Snate@binkert.org
2969396Sandreas.hansson@arm.comclass ProtoBuf(SourceFile):
2979396Sandreas.hansson@arm.com    '''Add a Protocol Buffer to build'''
2989396Sandreas.hansson@arm.com
29912302Sgabeblack@google.com    def __init__(self, source, tags=None, add_tags=None):
30012302Sgabeblack@google.com        '''Specify the source file, and any tags'''
30112302Sgabeblack@google.com        super(ProtoBuf, self).__init__(source, tags, add_tags)
3029396Sandreas.hansson@arm.com
3039396Sandreas.hansson@arm.com        # Get the file name and the extension
3049396Sandreas.hansson@arm.com        modname,ext = self.extname
3059396Sandreas.hansson@arm.com        assert ext == 'proto'
3069396Sandreas.hansson@arm.com
3079396Sandreas.hansson@arm.com        # Currently, we stick to generating the C++ headers, so we
3089396Sandreas.hansson@arm.com        # only need to track the source and header.
3099930Sandreas.hansson@arm.com        self.cc_file = File(modname + '.pb.cc')
3109930Sandreas.hansson@arm.com        self.hh_file = File(modname + '.pb.h')
3119396Sandreas.hansson@arm.com
3128235Snate@binkert.orgclass UnitTest(object):
3138235Snate@binkert.org    '''Create a UnitTest'''
3146143Snate@binkert.org
3158235Snate@binkert.org    all = []
3169003SAli.Saidi@ARM.com    def __init__(self, target, *sources, **kwargs):
3178235Snate@binkert.org        '''Specify the target name and any sources.  Sources that are
3188235Snate@binkert.org        not SourceFiles are evalued with Source().  All files are
31912302Sgabeblack@google.com        tagged with the name of the UnitTest target.'''
3208235Snate@binkert.org
32112302Sgabeblack@google.com        srcs = SourceList()
3228235Snate@binkert.org        for src in sources:
3238235Snate@binkert.org            if not isinstance(src, SourceFile):
32412302Sgabeblack@google.com                src = Source(src, tags=str(target))
3258235Snate@binkert.org            srcs.append(src)
3268235Snate@binkert.org
3278235Snate@binkert.org        self.sources = srcs
3288235Snate@binkert.org        self.target = target
3299003SAli.Saidi@ARM.com        self.main = kwargs.get('main', False)
33012313Sgabeblack@google.com        self.all.append(self)
33112313Sgabeblack@google.com
33212313Sgabeblack@google.comclass GTest(UnitTest):
33312313Sgabeblack@google.com    '''Create a unit test based on the google test framework.'''
33412313Sgabeblack@google.com    all = []
33512315Sgabeblack@google.com    def __init__(self, *args, **kwargs):
33612371Sgabeblack@google.com        isFilter = lambda arg: isinstance(arg, SourceFilter)
33712371Sgabeblack@google.com        self.filters = filter(isFilter, args)
33812371Sgabeblack@google.com        args = filter(lambda a: not isFilter(a), args)
33912315Sgabeblack@google.com        super(GTest, self).__init__(*args, **kwargs)
34012315Sgabeblack@google.com        self.dir = Dir('.')
34112371Sgabeblack@google.com        self.skip_lib = kwargs.pop('skip_lib', False)
3425584Snate@binkert.org
3434382Sbinkertn@umich.edu# Children should have access
3444202Sbinkertn@umich.eduExport('Source')
3454382Sbinkertn@umich.eduExport('PySource')
3464382Sbinkertn@umich.eduExport('SimObject')
3479396Sandreas.hansson@arm.comExport('ProtoBuf')
3485584Snate@binkert.orgExport('UnitTest')
34912313Sgabeblack@google.comExport('GTest')
3504382Sbinkertn@umich.edu
3514382Sbinkertn@umich.edu########################################################################
3524382Sbinkertn@umich.edu#
3538232Snate@binkert.org# Debug Flags
3545192Ssaidi@eecs.umich.edu#
3558232Snate@binkert.orgdebug_flags = {}
3568232Snate@binkert.orgdef DebugFlag(name, desc=None):
3578232Snate@binkert.org    if name in debug_flags:
3585192Ssaidi@eecs.umich.edu        raise AttributeError, "Flag %s already specified" % name
3598232Snate@binkert.org    debug_flags[name] = (name, (), desc)
3605192Ssaidi@eecs.umich.edu
3615799Snate@binkert.orgdef CompoundFlag(name, flags, desc=None):
3628232Snate@binkert.org    if name in debug_flags:
3635192Ssaidi@eecs.umich.edu        raise AttributeError, "Flag %s already specified" % name
3645192Ssaidi@eecs.umich.edu
3655192Ssaidi@eecs.umich.edu    compound = tuple(flags)
3668232Snate@binkert.org    debug_flags[name] = (name, compound, desc)
3675192Ssaidi@eecs.umich.edu
3688232Snate@binkert.orgExport('DebugFlag')
3695192Ssaidi@eecs.umich.eduExport('CompoundFlag')
3705192Ssaidi@eecs.umich.edu
3715192Ssaidi@eecs.umich.edu########################################################################
3725192Ssaidi@eecs.umich.edu#
3734382Sbinkertn@umich.edu# Set some compiler variables
3744382Sbinkertn@umich.edu#
3754382Sbinkertn@umich.edu
3762667Sstever@eecs.umich.edu# Include file paths are rooted in this directory.  SCons will
3772667Sstever@eecs.umich.edu# automatically expand '.' to refer to both the source directory and
3782667Sstever@eecs.umich.edu# the corresponding build directory to pick up generated include
3792667Sstever@eecs.umich.edu# files.
3802667Sstever@eecs.umich.eduenv.Append(CPPPATH=Dir('.'))
3812667Sstever@eecs.umich.edu
3825742Snate@binkert.orgfor extra_dir in extras_dir_list:
3835742Snate@binkert.org    env.Append(CPPPATH=Dir(extra_dir))
3845742Snate@binkert.org
3855793Snate@binkert.org# Workaround for bug in SCons version > 0.97d20071212
3868334Snate@binkert.org# Scons bug id: 2006 gem5 Bug id: 308
3875793Snate@binkert.orgfor root, dirs, files in os.walk(base_dir, topdown=True):
3885793Snate@binkert.org    Dir(root[len(base_dir) + 1:])
3895793Snate@binkert.org
3904382Sbinkertn@umich.edu########################################################################
3914762Snate@binkert.org#
3925344Sstever@gmail.com# Walk the tree and execute all SConscripts in subdirectories
3934382Sbinkertn@umich.edu#
3945341Sstever@gmail.com
3955742Snate@binkert.orghere = Dir('.').srcnode().abspath
3965742Snate@binkert.orgfor root, dirs, files in os.walk(base_dir, topdown=True):
3975742Snate@binkert.org    if root == here:
3985742Snate@binkert.org        # we don't want to recurse back into this SConscript
3995742Snate@binkert.org        continue
4004762Snate@binkert.org
4015742Snate@binkert.org    if 'SConscript' in files:
4025742Snate@binkert.org        build_dir = joinpath(env['BUILDDIR'], root[len(base_dir) + 1:])
40311984Sgabeblack@google.com        Source.set_group(build_dir)
4047722Sgblack@eecs.umich.edu        SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
4055742Snate@binkert.org
4065742Snate@binkert.orgfor extra_dir in extras_dir_list:
4075742Snate@binkert.org    prefix_len = len(dirname(extra_dir)) + 1
4089930Sandreas.hansson@arm.com
4099930Sandreas.hansson@arm.com    # Also add the corresponding build directory to pick up generated
4109930Sandreas.hansson@arm.com    # include files.
4119930Sandreas.hansson@arm.com    env.Append(CPPPATH=Dir(joinpath(env['BUILDDIR'], extra_dir[prefix_len:])))
4129930Sandreas.hansson@arm.com
4135742Snate@binkert.org    for root, dirs, files in os.walk(extra_dir, topdown=True):
4148242Sbradley.danofsky@amd.com        # if build lives in the extras directory, don't walk down it
4158242Sbradley.danofsky@amd.com        if 'build' in dirs:
4168242Sbradley.danofsky@amd.com            dirs.remove('build')
4178242Sbradley.danofsky@amd.com
4185341Sstever@gmail.com        if 'SConscript' in files:
4195742Snate@binkert.org            build_dir = joinpath(env['BUILDDIR'], root[prefix_len:])
4207722Sgblack@eecs.umich.edu            SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
4214773Snate@binkert.org
4226108Snate@binkert.orgfor opt in export_vars:
4231858SN/A    env.ConfigFile(opt)
4241085SN/A
4256658Snate@binkert.orgdef makeTheISA(source, target, env):
4266658Snate@binkert.org    isas = [ src.get_contents() for src in source ]
4277673Snate@binkert.org    target_isa = env['TARGET_ISA']
4286658Snate@binkert.org    def define(isa):
4296658Snate@binkert.org        return isa.upper() + '_ISA'
43011308Santhony.gutierrez@amd.com
4316658Snate@binkert.org    def namespace(isa):
43211308Santhony.gutierrez@amd.com        return isa[0].upper() + isa[1:].lower() + 'ISA'
4336658Snate@binkert.org
4346658Snate@binkert.org
4357673Snate@binkert.org    code = code_formatter()
4367673Snate@binkert.org    code('''\
4377673Snate@binkert.org#ifndef __CONFIG_THE_ISA_HH__
4387673Snate@binkert.org#define __CONFIG_THE_ISA_HH__
4397673Snate@binkert.org
4407673Snate@binkert.org''')
4417673Snate@binkert.org
44210467Sandreas.hansson@arm.com    # create defines for the preprocessing and compile-time determination
4436658Snate@binkert.org    for i,isa in enumerate(isas):
4447673Snate@binkert.org        code('#define $0 $1', define(isa), i + 1)
44510467Sandreas.hansson@arm.com    code()
44610467Sandreas.hansson@arm.com
44710467Sandreas.hansson@arm.com    # create an enum for any run-time determination of the ISA, we
44810467Sandreas.hansson@arm.com    # reuse the same name as the namespaces
44910467Sandreas.hansson@arm.com    code('enum class Arch {')
45010467Sandreas.hansson@arm.com    for i,isa in enumerate(isas):
45110467Sandreas.hansson@arm.com        if i + 1 == len(isas):
45210467Sandreas.hansson@arm.com            code('  $0 = $1', namespace(isa), define(isa))
45310467Sandreas.hansson@arm.com        else:
45410467Sandreas.hansson@arm.com            code('  $0 = $1,', namespace(isa), define(isa))
45510467Sandreas.hansson@arm.com    code('};')
4567673Snate@binkert.org
4577673Snate@binkert.org    code('''
4587673Snate@binkert.org
4597673Snate@binkert.org#define THE_ISA ${{define(target_isa)}}
4607673Snate@binkert.org#define TheISA ${{namespace(target_isa)}}
4619048SAli.Saidi@ARM.com#define THE_ISA_STR "${{target_isa}}"
4627673Snate@binkert.org
4637673Snate@binkert.org#endif // __CONFIG_THE_ISA_HH__''')
4647673Snate@binkert.org
4657673Snate@binkert.org    code.write(str(target[0]))
4666658Snate@binkert.org
4677756SAli.Saidi@ARM.comenv.Command('config/the_isa.hh', map(Value, all_isa_list),
4687816Ssteve.reinhardt@amd.com            MakeAction(makeTheISA, Transform("CFG ISA", 0)))
4696658Snate@binkert.org
47011308Santhony.gutierrez@amd.comdef makeTheGPUISA(source, target, env):
47111308Santhony.gutierrez@amd.com    isas = [ src.get_contents() for src in source ]
47211308Santhony.gutierrez@amd.com    target_gpu_isa = env['TARGET_GPU_ISA']
47311308Santhony.gutierrez@amd.com    def define(isa):
47411308Santhony.gutierrez@amd.com        return isa.upper() + '_ISA'
47511308Santhony.gutierrez@amd.com
47611308Santhony.gutierrez@amd.com    def namespace(isa):
47711308Santhony.gutierrez@amd.com        return isa[0].upper() + isa[1:].lower() + 'ISA'
47811308Santhony.gutierrez@amd.com
47911308Santhony.gutierrez@amd.com
48011308Santhony.gutierrez@amd.com    code = code_formatter()
48111308Santhony.gutierrez@amd.com    code('''\
48211308Santhony.gutierrez@amd.com#ifndef __CONFIG_THE_GPU_ISA_HH__
48311308Santhony.gutierrez@amd.com#define __CONFIG_THE_GPU_ISA_HH__
48411308Santhony.gutierrez@amd.com
48511308Santhony.gutierrez@amd.com''')
48611308Santhony.gutierrez@amd.com
48711308Santhony.gutierrez@amd.com    # create defines for the preprocessing and compile-time determination
48811308Santhony.gutierrez@amd.com    for i,isa in enumerate(isas):
48911308Santhony.gutierrez@amd.com        code('#define $0 $1', define(isa), i + 1)
49011308Santhony.gutierrez@amd.com    code()
49111308Santhony.gutierrez@amd.com
49211308Santhony.gutierrez@amd.com    # create an enum for any run-time determination of the ISA, we
49311308Santhony.gutierrez@amd.com    # reuse the same name as the namespaces
49411308Santhony.gutierrez@amd.com    code('enum class GPUArch {')
49511308Santhony.gutierrez@amd.com    for i,isa in enumerate(isas):
49611308Santhony.gutierrez@amd.com        if i + 1 == len(isas):
49711308Santhony.gutierrez@amd.com            code('  $0 = $1', namespace(isa), define(isa))
49811308Santhony.gutierrez@amd.com        else:
49911308Santhony.gutierrez@amd.com            code('  $0 = $1,', namespace(isa), define(isa))
50011308Santhony.gutierrez@amd.com    code('};')
50111308Santhony.gutierrez@amd.com
50211308Santhony.gutierrez@amd.com    code('''
50311308Santhony.gutierrez@amd.com
50411308Santhony.gutierrez@amd.com#define THE_GPU_ISA ${{define(target_gpu_isa)}}
50511308Santhony.gutierrez@amd.com#define TheGpuISA ${{namespace(target_gpu_isa)}}
50611308Santhony.gutierrez@amd.com#define THE_GPU_ISA_STR "${{target_gpu_isa}}"
50711308Santhony.gutierrez@amd.com
50811308Santhony.gutierrez@amd.com#endif // __CONFIG_THE_GPU_ISA_HH__''')
50911308Santhony.gutierrez@amd.com
51011308Santhony.gutierrez@amd.com    code.write(str(target[0]))
51111308Santhony.gutierrez@amd.com
51211308Santhony.gutierrez@amd.comenv.Command('config/the_gpu_isa.hh', map(Value, all_gpu_isa_list),
51311308Santhony.gutierrez@amd.com            MakeAction(makeTheGPUISA, Transform("CFG ISA", 0)))
51411308Santhony.gutierrez@amd.com
5154382Sbinkertn@umich.edu########################################################################
5164382Sbinkertn@umich.edu#
5174762Snate@binkert.org# Prevent any SimObjects from being added after this point, they
5184762Snate@binkert.org# should all have been added in the SConscripts above
5194762Snate@binkert.org#
5206654Snate@binkert.orgSimObject.fixed = True
5216654Snate@binkert.org
5225517Snate@binkert.orgclass DictImporter(object):
5235517Snate@binkert.org    '''This importer takes a dictionary of arbitrary module names that
5245517Snate@binkert.org    map to arbitrary filenames.'''
5255517Snate@binkert.org    def __init__(self, modules):
5265517Snate@binkert.org        self.modules = modules
5275517Snate@binkert.org        self.installed = set()
5285517Snate@binkert.org
5295517Snate@binkert.org    def __del__(self):
5305517Snate@binkert.org        self.unload()
5315517Snate@binkert.org
5325517Snate@binkert.org    def unload(self):
5335517Snate@binkert.org        import sys
5345517Snate@binkert.org        for module in self.installed:
5355517Snate@binkert.org            del sys.modules[module]
5365517Snate@binkert.org        self.installed = set()
5375517Snate@binkert.org
5385517Snate@binkert.org    def find_module(self, fullname, path):
5396654Snate@binkert.org        if fullname == 'm5.defines':
5405517Snate@binkert.org            return self
5415517Snate@binkert.org
5425517Snate@binkert.org        if fullname == 'm5.objects':
5435517Snate@binkert.org            return self
5445517Snate@binkert.org
54511802Sandreas.sandberg@arm.com        if fullname.startswith('_m5'):
5465517Snate@binkert.org            return None
5475517Snate@binkert.org
5486143Snate@binkert.org        source = self.modules.get(fullname, None)
5496654Snate@binkert.org        if source is not None and fullname.startswith('m5.objects'):
5505517Snate@binkert.org            return self
5515517Snate@binkert.org
5525517Snate@binkert.org        return None
5535517Snate@binkert.org
5545517Snate@binkert.org    def load_module(self, fullname):
5555517Snate@binkert.org        mod = imp.new_module(fullname)
5565517Snate@binkert.org        sys.modules[fullname] = mod
5575517Snate@binkert.org        self.installed.add(fullname)
5585517Snate@binkert.org
5595517Snate@binkert.org        mod.__loader__ = self
5605517Snate@binkert.org        if fullname == 'm5.objects':
5615517Snate@binkert.org            mod.__path__ = fullname.split('.')
5625517Snate@binkert.org            return mod
5635517Snate@binkert.org
5646654Snate@binkert.org        if fullname == 'm5.defines':
5656654Snate@binkert.org            mod.__dict__['buildEnv'] = m5.util.SmartDict(build_env)
5665517Snate@binkert.org            return mod
5675517Snate@binkert.org
5686143Snate@binkert.org        source = self.modules[fullname]
5696143Snate@binkert.org        if source.modname == '__init__':
5706143Snate@binkert.org            mod.__path__ = source.modpath
5716727Ssteve.reinhardt@amd.com        mod.__file__ = source.abspath
5725517Snate@binkert.org
5736727Ssteve.reinhardt@amd.com        exec file(source.abspath, 'r') in mod.__dict__
5745517Snate@binkert.org
5755517Snate@binkert.org        return mod
5765517Snate@binkert.org
5776654Snate@binkert.orgimport m5.SimObject
5786654Snate@binkert.orgimport m5.params
5797673Snate@binkert.orgfrom m5.util import code_formatter
5806654Snate@binkert.org
5816654Snate@binkert.orgm5.SimObject.clear()
5826654Snate@binkert.orgm5.params.clear()
5836654Snate@binkert.org
5845517Snate@binkert.org# install the python importer so we can grab stuff from the source
5855517Snate@binkert.org# tree itself.  We can't have SimObjects added after this point or
5865517Snate@binkert.org# else we won't know about them for the rest of the stuff.
5876143Snate@binkert.orgimporter = DictImporter(PySource.modules)
5885517Snate@binkert.orgsys.meta_path[0:0] = [ importer ]
5894762Snate@binkert.org
5905517Snate@binkert.org# import all sim objects so we can populate the all_objects list
5915517Snate@binkert.org# make sure that we're working with a list, then let's sort it
5926143Snate@binkert.orgfor modname in SimObject.modnames:
5936143Snate@binkert.org    exec('from m5.objects import %s' % modname)
5945517Snate@binkert.org
5955517Snate@binkert.org# we need to unload all of the currently imported modules so that they
5965517Snate@binkert.org# will be re-imported the next time the sconscript is run
5975517Snate@binkert.orgimporter.unload()
5985517Snate@binkert.orgsys.meta_path.remove(importer)
5995517Snate@binkert.org
6005517Snate@binkert.orgsim_objects = m5.SimObject.allClasses
6015517Snate@binkert.orgall_enums = m5.params.allEnums
6025517Snate@binkert.org
6036143Snate@binkert.orgfor name,obj in sorted(sim_objects.iteritems()):
6045517Snate@binkert.org    for param in obj._params.local.values():
6056654Snate@binkert.org        # load the ptype attribute now because it depends on the
6066654Snate@binkert.org        # current version of SimObject.allClasses, but when scons
6076654Snate@binkert.org        # actually uses the value, all versions of
6086654Snate@binkert.org        # SimObject.allClasses will have been loaded
6096654Snate@binkert.org        param.ptype
6106654Snate@binkert.org
6114762Snate@binkert.org########################################################################
6124762Snate@binkert.org#
6134762Snate@binkert.org# calculate extra dependencies
6144762Snate@binkert.org#
6154762Snate@binkert.orgmodule_depends = ["m5", "m5.SimObject", "m5.params"]
6167675Snate@binkert.orgdepends = [ PySource.modules[dep].snode for dep in module_depends ]
61710584Sandreas.hansson@arm.comdepends.sort(key = lambda x: x.name)
6184762Snate@binkert.org
6194762Snate@binkert.org########################################################################
6204762Snate@binkert.org#
6214762Snate@binkert.org# Commands for the basic automatically generated python files
6224382Sbinkertn@umich.edu#
6234382Sbinkertn@umich.edu
6245517Snate@binkert.org# Generate Python file containing a dict specifying the current
6256654Snate@binkert.org# buildEnv flags.
6265517Snate@binkert.orgdef makeDefinesPyFile(target, source, env):
6278126Sgblack@eecs.umich.edu    build_env = source[0].get_contents()
6286654Snate@binkert.org
6297673Snate@binkert.org    code = code_formatter()
6306654Snate@binkert.org    code("""
63111802Sandreas.sandberg@arm.comimport _m5.core
6326654Snate@binkert.orgimport m5.util
6336654Snate@binkert.org
6346654Snate@binkert.orgbuildEnv = m5.util.SmartDict($build_env)
6356654Snate@binkert.org
63611802Sandreas.sandberg@arm.comcompileDate = _m5.core.compileDate
6376669Snate@binkert.org_globals = globals()
63811802Sandreas.sandberg@arm.comfor key,val in _m5.core.__dict__.iteritems():
6396669Snate@binkert.org    if key.startswith('flag_'):
6406669Snate@binkert.org        flag = key[5:]
6416669Snate@binkert.org        _globals[flag] = val
6426669Snate@binkert.orgdel _globals
6436654Snate@binkert.org""")
6447673Snate@binkert.org    code.write(target[0].abspath)
6455517Snate@binkert.org
6468126Sgblack@eecs.umich.edudefines_info = Value(build_env)
6475798Snate@binkert.org# Generate a file with all of the compile options in it
6487756SAli.Saidi@ARM.comenv.Command('python/m5/defines.py', defines_info,
6497816Ssteve.reinhardt@amd.com            MakeAction(makeDefinesPyFile, Transform("DEFINES", 0)))
6505798Snate@binkert.orgPySource('m5', 'python/m5/defines.py')
6515798Snate@binkert.org
6525517Snate@binkert.org# Generate python file containing info about the M5 source code
6535517Snate@binkert.orgdef makeInfoPyFile(target, source, env):
6547673Snate@binkert.org    code = code_formatter()
6555517Snate@binkert.org    for src in source:
6565517Snate@binkert.org        data = ''.join(file(src.srcnode().abspath, 'r').xreadlines())
6577673Snate@binkert.org        code('$src = ${{repr(data)}}')
6587673Snate@binkert.org    code.write(str(target[0]))
6595517Snate@binkert.org
6605798Snate@binkert.org# Generate a file that wraps the basic top level files
6615798Snate@binkert.orgenv.Command('python/m5/info.py',
6628333Snate@binkert.org            [ '#/COPYING', '#/LICENSE', '#/README', ],
6637816Ssteve.reinhardt@amd.com            MakeAction(makeInfoPyFile, Transform("INFO")))
6645798Snate@binkert.orgPySource('m5', 'python/m5/info.py')
6655798Snate@binkert.org
6664762Snate@binkert.org########################################################################
6674762Snate@binkert.org#
6684762Snate@binkert.org# Create all of the SimObject param headers and enum headers
6694762Snate@binkert.org#
6704762Snate@binkert.org
6718596Ssteve.reinhardt@amd.comdef createSimObjectParamStruct(target, source, env):
6725517Snate@binkert.org    assert len(target) == 1 and len(source) == 1
6735517Snate@binkert.org
67411997Sgabeblack@google.com    name = source[0].get_text_contents()
6755517Snate@binkert.org    obj = sim_objects[name]
6765517Snate@binkert.org
6777673Snate@binkert.org    code = code_formatter()
6788596Ssteve.reinhardt@amd.com    obj.cxx_param_decl(code)
6797673Snate@binkert.org    code.write(target[0].abspath)
6805517Snate@binkert.org
68110458Sandreas.hansson@arm.comdef createSimObjectCxxConfig(is_header):
68210458Sandreas.hansson@arm.com    def body(target, source, env):
68310458Sandreas.hansson@arm.com        assert len(target) == 1 and len(source) == 1
68410458Sandreas.hansson@arm.com
68510458Sandreas.hansson@arm.com        name = str(source[0].get_contents())
68610458Sandreas.hansson@arm.com        obj = sim_objects[name]
68710458Sandreas.hansson@arm.com
68810458Sandreas.hansson@arm.com        code = code_formatter()
68910458Sandreas.hansson@arm.com        obj.cxx_config_param_file(code, is_header)
69010458Sandreas.hansson@arm.com        code.write(target[0].abspath)
69110458Sandreas.hansson@arm.com    return body
69210458Sandreas.hansson@arm.com
6935517Snate@binkert.orgdef createEnumStrings(target, source, env):
69411996Sgabeblack@google.com    assert len(target) == 1 and len(source) == 2
6955517Snate@binkert.org
69611997Sgabeblack@google.com    name = source[0].get_text_contents()
69711996Sgabeblack@google.com    use_python = source[1].read()
6985517Snate@binkert.org    obj = all_enums[name]
6995517Snate@binkert.org
7007673Snate@binkert.org    code = code_formatter()
7017673Snate@binkert.org    obj.cxx_def(code)
70211996Sgabeblack@google.com    if use_python:
70311988Sandreas.sandberg@arm.com        obj.pybind_def(code)
7047673Snate@binkert.org    code.write(target[0].abspath)
7055517Snate@binkert.org
7068596Ssteve.reinhardt@amd.comdef createEnumDecls(target, source, env):
7075517Snate@binkert.org    assert len(target) == 1 and len(source) == 1
7085517Snate@binkert.org
70911997Sgabeblack@google.com    name = source[0].get_text_contents()
7105517Snate@binkert.org    obj = all_enums[name]
7115517Snate@binkert.org
7127673Snate@binkert.org    code = code_formatter()
7137673Snate@binkert.org    obj.cxx_decl(code)
7147673Snate@binkert.org    code.write(target[0].abspath)
7155517Snate@binkert.org
71611988Sandreas.sandberg@arm.comdef createSimObjectPyBindWrapper(target, source, env):
71711997Sgabeblack@google.com    name = source[0].get_text_contents()
7188596Ssteve.reinhardt@amd.com    obj = sim_objects[name]
7198596Ssteve.reinhardt@amd.com
7208596Ssteve.reinhardt@amd.com    code = code_formatter()
72111988Sandreas.sandberg@arm.com    obj.pybind_decl(code)
7228596Ssteve.reinhardt@amd.com    code.write(target[0].abspath)
7238596Ssteve.reinhardt@amd.com
7248596Ssteve.reinhardt@amd.com# Generate all of the SimObject param C++ struct header files
7254762Snate@binkert.orgparams_hh_files = []
7266143Snate@binkert.orgfor name,simobj in sorted(sim_objects.iteritems()):
7276143Snate@binkert.org    py_source = PySource.modules[simobj.__module__]
7286143Snate@binkert.org    extra_deps = [ py_source.tnode ]
7294762Snate@binkert.org
7304762Snate@binkert.org    hh_file = File('params/%s.hh' % name)
7314762Snate@binkert.org    params_hh_files.append(hh_file)
7327756SAli.Saidi@ARM.com    env.Command(hh_file, Value(name),
7338596Ssteve.reinhardt@amd.com                MakeAction(createSimObjectParamStruct, Transform("SO PARAM")))
7344762Snate@binkert.org    env.Depends(hh_file, depends + extra_deps)
7354762Snate@binkert.org
73610458Sandreas.hansson@arm.com# C++ parameter description files
73710458Sandreas.hansson@arm.comif GetOption('with_cxx_config'):
73810458Sandreas.hansson@arm.com    for name,simobj in sorted(sim_objects.iteritems()):
73910458Sandreas.hansson@arm.com        py_source = PySource.modules[simobj.__module__]
74010458Sandreas.hansson@arm.com        extra_deps = [ py_source.tnode ]
74110458Sandreas.hansson@arm.com
74210458Sandreas.hansson@arm.com        cxx_config_hh_file = File('cxx_config/%s.hh' % name)
74310458Sandreas.hansson@arm.com        cxx_config_cc_file = File('cxx_config/%s.cc' % name)
74410458Sandreas.hansson@arm.com        env.Command(cxx_config_hh_file, Value(name),
74510458Sandreas.hansson@arm.com                    MakeAction(createSimObjectCxxConfig(True),
74610458Sandreas.hansson@arm.com                    Transform("CXXCPRHH")))
74710458Sandreas.hansson@arm.com        env.Command(cxx_config_cc_file, Value(name),
74810458Sandreas.hansson@arm.com                    MakeAction(createSimObjectCxxConfig(False),
74910458Sandreas.hansson@arm.com                    Transform("CXXCPRCC")))
75010458Sandreas.hansson@arm.com        env.Depends(cxx_config_hh_file, depends + extra_deps +
75110458Sandreas.hansson@arm.com                    [File('params/%s.hh' % name), File('sim/cxx_config.hh')])
75210458Sandreas.hansson@arm.com        env.Depends(cxx_config_cc_file, depends + extra_deps +
75310458Sandreas.hansson@arm.com                    [cxx_config_hh_file])
75410458Sandreas.hansson@arm.com        Source(cxx_config_cc_file)
75510458Sandreas.hansson@arm.com
75610458Sandreas.hansson@arm.com    cxx_config_init_cc_file = File('cxx_config/init.cc')
75710458Sandreas.hansson@arm.com
75810458Sandreas.hansson@arm.com    def createCxxConfigInitCC(target, source, env):
75910458Sandreas.hansson@arm.com        assert len(target) == 1 and len(source) == 1
76010458Sandreas.hansson@arm.com
76110458Sandreas.hansson@arm.com        code = code_formatter()
76210458Sandreas.hansson@arm.com
76310458Sandreas.hansson@arm.com        for name,simobj in sorted(sim_objects.iteritems()):
76410458Sandreas.hansson@arm.com            if not hasattr(simobj, 'abstract') or not simobj.abstract:
76510458Sandreas.hansson@arm.com                code('#include "cxx_config/${name}.hh"')
76610458Sandreas.hansson@arm.com        code()
76710458Sandreas.hansson@arm.com        code('void cxxConfigInit()')
76810458Sandreas.hansson@arm.com        code('{')
76910458Sandreas.hansson@arm.com        code.indent()
77010458Sandreas.hansson@arm.com        for name,simobj in sorted(sim_objects.iteritems()):
77110458Sandreas.hansson@arm.com            not_abstract = not hasattr(simobj, 'abstract') or \
77210458Sandreas.hansson@arm.com                not simobj.abstract
77310458Sandreas.hansson@arm.com            if not_abstract and 'type' in simobj.__dict__:
77410458Sandreas.hansson@arm.com                code('cxx_config_directory["${name}"] = '
77510458Sandreas.hansson@arm.com                     '${name}CxxConfigParams::makeDirectoryEntry();')
77610458Sandreas.hansson@arm.com        code.dedent()
77710458Sandreas.hansson@arm.com        code('}')
77810458Sandreas.hansson@arm.com        code.write(target[0].abspath)
77910458Sandreas.hansson@arm.com
78010458Sandreas.hansson@arm.com    py_source = PySource.modules[simobj.__module__]
78110458Sandreas.hansson@arm.com    extra_deps = [ py_source.tnode ]
78210458Sandreas.hansson@arm.com    env.Command(cxx_config_init_cc_file, Value(name),
78310458Sandreas.hansson@arm.com        MakeAction(createCxxConfigInitCC, Transform("CXXCINIT")))
78410458Sandreas.hansson@arm.com    cxx_param_hh_files = ["cxx_config/%s.hh" % simobj
78510584Sandreas.hansson@arm.com        for name,simobj in sorted(sim_objects.iteritems())
78610458Sandreas.hansson@arm.com        if not hasattr(simobj, 'abstract') or not simobj.abstract]
78710458Sandreas.hansson@arm.com    Depends(cxx_config_init_cc_file, cxx_param_hh_files +
78810458Sandreas.hansson@arm.com            [File('sim/cxx_config.hh')])
78910458Sandreas.hansson@arm.com    Source(cxx_config_init_cc_file)
79010458Sandreas.hansson@arm.com
7914762Snate@binkert.org# Generate all enum header files
7926143Snate@binkert.orgfor name,enum in sorted(all_enums.iteritems()):
7936143Snate@binkert.org    py_source = PySource.modules[enum.__module__]
7946143Snate@binkert.org    extra_deps = [ py_source.tnode ]
7954762Snate@binkert.org
7964762Snate@binkert.org    cc_file = File('enums/%s.cc' % name)
79711996Sgabeblack@google.com    env.Command(cc_file, [Value(name), Value(env['USE_PYTHON'])],
7987816Ssteve.reinhardt@amd.com                MakeAction(createEnumStrings, Transform("ENUM STR")))
7994762Snate@binkert.org    env.Depends(cc_file, depends + extra_deps)
8004762Snate@binkert.org    Source(cc_file)
8014762Snate@binkert.org
8024762Snate@binkert.org    hh_file = File('enums/%s.hh' % name)
8037756SAli.Saidi@ARM.com    env.Command(hh_file, Value(name),
8048596Ssteve.reinhardt@amd.com                MakeAction(createEnumDecls, Transform("ENUMDECL")))
8054762Snate@binkert.org    env.Depends(hh_file, depends + extra_deps)
8064762Snate@binkert.org
80711988Sandreas.sandberg@arm.com# Generate SimObject Python bindings wrapper files
80811988Sandreas.sandberg@arm.comif env['USE_PYTHON']:
80911988Sandreas.sandberg@arm.com    for name,simobj in sorted(sim_objects.iteritems()):
81011988Sandreas.sandberg@arm.com        py_source = PySource.modules[simobj.__module__]
81111988Sandreas.sandberg@arm.com        extra_deps = [ py_source.tnode ]
81211988Sandreas.sandberg@arm.com        cc_file = File('python/_m5/param_%s.cc' % name)
81311988Sandreas.sandberg@arm.com        env.Command(cc_file, Value(name),
81411988Sandreas.sandberg@arm.com                    MakeAction(createSimObjectPyBindWrapper,
81511988Sandreas.sandberg@arm.com                               Transform("SO PyBind")))
81611988Sandreas.sandberg@arm.com        env.Depends(cc_file, depends + extra_deps)
81711988Sandreas.sandberg@arm.com        Source(cc_file)
8184382Sbinkertn@umich.edu
8199396Sandreas.hansson@arm.com# Build all protocol buffers if we have got protoc and protobuf available
8209396Sandreas.hansson@arm.comif env['HAVE_PROTOBUF']:
8219396Sandreas.hansson@arm.com    for proto in ProtoBuf.all:
8229396Sandreas.hansson@arm.com        # Use both the source and header as the target, and the .proto
8239396Sandreas.hansson@arm.com        # file as the source. When executing the protoc compiler, also
8249396Sandreas.hansson@arm.com        # specify the proto_path to avoid having the generated files
8259396Sandreas.hansson@arm.com        # include the path.
8269396Sandreas.hansson@arm.com        env.Command([proto.cc_file, proto.hh_file], proto.tnode,
8279396Sandreas.hansson@arm.com                    MakeAction('$PROTOC --cpp_out ${TARGET.dir} '
8289396Sandreas.hansson@arm.com                               '--proto_path ${SOURCE.dir} $SOURCE',
8299396Sandreas.hansson@arm.com                               Transform("PROTOC")))
8309396Sandreas.hansson@arm.com
8319396Sandreas.hansson@arm.com        # Add the C++ source file
83212302Sgabeblack@google.com        Source(proto.cc_file, tags=proto.tags)
8339396Sandreas.hansson@arm.comelif ProtoBuf.all:
8349396Sandreas.hansson@arm.com    print 'Got protobuf to build, but lacks support!'
8359396Sandreas.hansson@arm.com    Exit(1)
8369396Sandreas.hansson@arm.com
8378232Snate@binkert.org#
8388232Snate@binkert.org# Handle debug flags
8398232Snate@binkert.org#
8408232Snate@binkert.orgdef makeDebugFlagCC(target, source, env):
8418232Snate@binkert.org    assert(len(target) == 1 and len(source) == 1)
8426229Snate@binkert.org
84310455SCurtis.Dunham@arm.com    code = code_formatter()
8446229Snate@binkert.org
84510455SCurtis.Dunham@arm.com    # delay definition of CompoundFlags until after all the definition
84610455SCurtis.Dunham@arm.com    # of all constituent SimpleFlags
84710455SCurtis.Dunham@arm.com    comp_code = code_formatter()
8485517Snate@binkert.org
8495517Snate@binkert.org    # file header
8507673Snate@binkert.org    code('''
8515517Snate@binkert.org/*
85210455SCurtis.Dunham@arm.com * DO NOT EDIT THIS FILE! Automatically generated by SCons.
8535517Snate@binkert.org */
8545517Snate@binkert.org
8558232Snate@binkert.org#include "base/debug.hh"
85610455SCurtis.Dunham@arm.com
85710455SCurtis.Dunham@arm.comnamespace Debug {
85810455SCurtis.Dunham@arm.com
8597673Snate@binkert.org''')
8607673Snate@binkert.org
86110455SCurtis.Dunham@arm.com    for name, flag in sorted(source[0].read().iteritems()):
86210455SCurtis.Dunham@arm.com        n, compound, desc = flag
86310455SCurtis.Dunham@arm.com        assert n == name
8645517Snate@binkert.org
86510455SCurtis.Dunham@arm.com        if not compound:
86610455SCurtis.Dunham@arm.com            code('SimpleFlag $name("$name", "$desc");')
86710455SCurtis.Dunham@arm.com        else:
86810455SCurtis.Dunham@arm.com            comp_code('CompoundFlag $name("$name", "$desc",')
86910455SCurtis.Dunham@arm.com            comp_code.indent()
87010455SCurtis.Dunham@arm.com            last = len(compound) - 1
87110455SCurtis.Dunham@arm.com            for i,flag in enumerate(compound):
87210455SCurtis.Dunham@arm.com                if i != last:
87310685Sandreas.hansson@arm.com                    comp_code('&$flag,')
87410455SCurtis.Dunham@arm.com                else:
87510685Sandreas.hansson@arm.com                    comp_code('&$flag);')
87610455SCurtis.Dunham@arm.com            comp_code.dedent()
8775517Snate@binkert.org
87810455SCurtis.Dunham@arm.com    code.append(comp_code)
8798232Snate@binkert.org    code()
8808232Snate@binkert.org    code('} // namespace Debug')
8815517Snate@binkert.org
8827673Snate@binkert.org    code.write(str(target[0]))
8835517Snate@binkert.org
8848232Snate@binkert.orgdef makeDebugFlagHH(target, source, env):
8858232Snate@binkert.org    assert(len(target) == 1 and len(source) == 1)
8865517Snate@binkert.org
8878232Snate@binkert.org    val = eval(source[0].get_contents())
8888232Snate@binkert.org    name, compound, desc = val
8898232Snate@binkert.org
8907673Snate@binkert.org    code = code_formatter()
8915517Snate@binkert.org
8925517Snate@binkert.org    # file header boilerplate
8937673Snate@binkert.org    code('''\
8945517Snate@binkert.org/*
89510455SCurtis.Dunham@arm.com * DO NOT EDIT THIS FILE! Automatically generated by SCons.
8965517Snate@binkert.org */
8975517Snate@binkert.org
8988232Snate@binkert.org#ifndef __DEBUG_${name}_HH__
8998232Snate@binkert.org#define __DEBUG_${name}_HH__
9005517Snate@binkert.org
9018232Snate@binkert.orgnamespace Debug {
9028232Snate@binkert.org''')
9035517Snate@binkert.org
9048232Snate@binkert.org    if compound:
9058232Snate@binkert.org        code('class CompoundFlag;')
9068232Snate@binkert.org    code('class SimpleFlag;')
9075517Snate@binkert.org
9088232Snate@binkert.org    if compound:
9098232Snate@binkert.org        code('extern CompoundFlag $name;')
9108232Snate@binkert.org        for flag in compound:
9118232Snate@binkert.org            code('extern SimpleFlag $flag;')
9128232Snate@binkert.org    else:
9138232Snate@binkert.org        code('extern SimpleFlag $name;')
9145517Snate@binkert.org
9158232Snate@binkert.org    code('''
9168232Snate@binkert.org}
9175517Snate@binkert.org
9188232Snate@binkert.org#endif // __DEBUG_${name}_HH__
9197673Snate@binkert.org''')
9205517Snate@binkert.org
9217673Snate@binkert.org    code.write(str(target[0]))
9225517Snate@binkert.org
9238232Snate@binkert.orgfor name,flag in sorted(debug_flags.iteritems()):
9248232Snate@binkert.org    n, compound, desc = flag
9258232Snate@binkert.org    assert n == name
9265192Ssaidi@eecs.umich.edu
92710454SCurtis.Dunham@arm.com    hh_file = 'debug/%s.hh' % name
92810454SCurtis.Dunham@arm.com    env.Command(hh_file, Value(flag),
9298232Snate@binkert.org                MakeAction(makeDebugFlagHH, Transform("TRACING", 0)))
93010455SCurtis.Dunham@arm.com
93110455SCurtis.Dunham@arm.comenv.Command('debug/flags.cc', Value(debug_flags),
93210455SCurtis.Dunham@arm.com            MakeAction(makeDebugFlagCC, Transform("TRACING", 0)))
93310455SCurtis.Dunham@arm.comSource('debug/flags.cc')
9345192Ssaidi@eecs.umich.edu
93511077SCurtis.Dunham@arm.com# version tags
93611330SCurtis.Dunham@arm.comtags = \
93711077SCurtis.Dunham@arm.comenv.Command('sim/tags.cc', None,
93811077SCurtis.Dunham@arm.com            MakeAction('util/cpt_upgrader.py --get-cc-file > $TARGET',
93911077SCurtis.Dunham@arm.com                       Transform("VER TAGS")))
94011330SCurtis.Dunham@arm.comenv.AlwaysBuild(tags)
94111077SCurtis.Dunham@arm.com
9427674Snate@binkert.org# Embed python files.  All .py files that have been indicated by a
9435522Snate@binkert.org# PySource() call in a SConscript need to be embedded into the M5
9445522Snate@binkert.org# library.  To do that, we compile the file to byte code, marshal the
9457674Snate@binkert.org# byte code, compress it, and then generate a c++ file that
9467674Snate@binkert.org# inserts the result into an array.
9477674Snate@binkert.orgdef embedPyFile(target, source, env):
9487674Snate@binkert.org    def c_str(string):
9497674Snate@binkert.org        if string is None:
9507674Snate@binkert.org            return "0"
9517674Snate@binkert.org        return '"%s"' % string
9527674Snate@binkert.org
9535522Snate@binkert.org    '''Action function to compile a .py into a code object, marshal
9545522Snate@binkert.org    it, compress it, and stick it into an asm file so the code appears
9555522Snate@binkert.org    as just bytes with a label in the data section'''
9565517Snate@binkert.org
9575522Snate@binkert.org    src = file(str(source[0]), 'r').read()
9585517Snate@binkert.org
9596143Snate@binkert.org    pysource = PySource.tnodes[source[0]]
9606727Ssteve.reinhardt@amd.com    compiled = compile(src, pysource.abspath, 'exec')
9615522Snate@binkert.org    marshalled = marshal.dumps(compiled)
9625522Snate@binkert.org    compressed = zlib.compress(marshalled)
9635522Snate@binkert.org    data = compressed
9647674Snate@binkert.org    sym = pysource.symname
9655517Snate@binkert.org
9667673Snate@binkert.org    code = code_formatter()
9677673Snate@binkert.org    code('''\
9687674Snate@binkert.org#include "sim/init.hh"
9697673Snate@binkert.org
9707674Snate@binkert.orgnamespace {
9717674Snate@binkert.org
9728946Sandreas.hansson@arm.comconst uint8_t data_${sym}[] = {
9737674Snate@binkert.org''')
9747674Snate@binkert.org    code.indent()
9757674Snate@binkert.org    step = 16
9765522Snate@binkert.org    for i in xrange(0, len(data), step):
9775522Snate@binkert.org        x = array.array('B', data[i:i+step])
9787674Snate@binkert.org        code(''.join('%d,' % d for d in x))
9797674Snate@binkert.org    code.dedent()
98011308Santhony.gutierrez@amd.com
9817674Snate@binkert.org    code('''};
9827673Snate@binkert.org
9837674Snate@binkert.orgEmbeddedPython embedded_${sym}(
9847674Snate@binkert.org    ${{c_str(pysource.arcname)}},
9857674Snate@binkert.org    ${{c_str(pysource.abspath)}},
9867674Snate@binkert.org    ${{c_str(pysource.modpath)}},
9877674Snate@binkert.org    data_${sym},
9887674Snate@binkert.org    ${{len(data)}},
9897674Snate@binkert.org    ${{len(marshalled)}});
9907674Snate@binkert.org
9917811Ssteve.reinhardt@amd.com} // anonymous namespace
9927674Snate@binkert.org''')
9937673Snate@binkert.org    code.write(str(target[0]))
9945522Snate@binkert.org
9956143Snate@binkert.orgfor source in PySource.all:
99610453SAndrew.Bardsley@arm.com    env.Command(source.cpp, source.tnode,
9977816Ssteve.reinhardt@amd.com                MakeAction(embedPyFile, Transform("EMBED PY")))
99812302Sgabeblack@google.com    Source(source.cpp, tags=source.tags, add_tags='python')
9994382Sbinkertn@umich.edu
10004382Sbinkertn@umich.edu########################################################################
10014382Sbinkertn@umich.edu#
10024382Sbinkertn@umich.edu# Define binaries.  Each different build type (debug, opt, etc.) gets
10034382Sbinkertn@umich.edu# a slightly different build environment.
10044382Sbinkertn@umich.edu#
10054382Sbinkertn@umich.edu
10064382Sbinkertn@umich.edu# List of constructed environments to pass back to SConstruct
100712302Sgabeblack@google.comdate_source = Source('base/date.cc', tags=[])
10084382Sbinkertn@umich.edu
10092655Sstever@eecs.umich.edu# Function to create a new build environment as clone of current
10102655Sstever@eecs.umich.edu# environment 'env' with modified object suffix and optional stripped
10112655Sstever@eecs.umich.edu# binary.  Additional keyword arguments are appended to corresponding
10122655Sstever@eecs.umich.edu# build environment vars.
101312063Sgabeblack@google.comdef makeEnv(env, label, objsfx, strip=False, disable_partial=False, **kwargs):
10145601Snate@binkert.org    # SCons doesn't know to append a library suffix when there is a '.' in the
10155601Snate@binkert.org    # name.  Use '_' instead.
101612222Sgabeblack@google.com    libname = 'gem5_' + label
101712222Sgabeblack@google.com    exename = 'gem5.' + label
101812222Sgabeblack@google.com    secondary_exename = 'm5.' + label
10195522Snate@binkert.org
10205863Snate@binkert.org    new_env = env.Clone(OBJSUFFIX=objsfx, SHOBJSUFFIX=objsfx + 's')
10215601Snate@binkert.org    new_env.Label = label
10225601Snate@binkert.org    new_env.Append(**kwargs)
10235601Snate@binkert.org
102412302Sgabeblack@google.com    lib_sources = Source.all.with_tag('gem5 lib')
102510453SAndrew.Bardsley@arm.com
102611988Sandreas.sandberg@arm.com    # Without Python, leave out all Python content from the library
102711988Sandreas.sandberg@arm.com    # builds.  The option doesn't affect gem5 built as a program
102810453SAndrew.Bardsley@arm.com    if GetOption('without_python'):
102912302Sgabeblack@google.com        lib_sources = lib_sources.without_tag('python')
103010453SAndrew.Bardsley@arm.com
103111983Sgabeblack@google.com    static_objs = []
103211983Sgabeblack@google.com    shared_objs = []
103312302Sgabeblack@google.com
103412302Sgabeblack@google.com    for s in lib_sources.with_tag(Source.ungrouped_tag):
103512362Sgabeblack@google.com        static_objs.append(s.static(new_env))
103612362Sgabeblack@google.com        shared_objs.append(s.shared(new_env))
103711983Sgabeblack@google.com
103812302Sgabeblack@google.com    for group in Source.source_groups:
103912302Sgabeblack@google.com        srcs = lib_sources.with_tag(Source.link_group_tag(group))
104011983Sgabeblack@google.com        if not srcs:
104111983Sgabeblack@google.com            continue
104211983Sgabeblack@google.com
104312362Sgabeblack@google.com        group_static = [ s.static(new_env) for s in srcs ]
104412362Sgabeblack@google.com        group_shared = [ s.shared(new_env) for s in srcs ]
104512310Sgabeblack@google.com
104612063Sgabeblack@google.com        # If partial linking is disabled, add these sources to the build
104712063Sgabeblack@google.com        # directly, and short circuit this loop.
104812063Sgabeblack@google.com        if disable_partial:
104912310Sgabeblack@google.com            static_objs.extend(group_static)
105012310Sgabeblack@google.com            shared_objs.extend(group_shared)
105112063Sgabeblack@google.com            continue
105212063Sgabeblack@google.com
105311983Sgabeblack@google.com        # Set up the static partially linked objects.
105411983Sgabeblack@google.com        file_name = new_env.subst("${OBJPREFIX}lib${OBJSUFFIX}.partial")
105511983Sgabeblack@google.com        target = File(joinpath(group, file_name))
105612310Sgabeblack@google.com        partial = env.PartialStatic(target=target, source=group_static)
105712310Sgabeblack@google.com        static_objs.extend(partial)
105811983Sgabeblack@google.com
105911983Sgabeblack@google.com        # Set up the shared partially linked objects.
106011983Sgabeblack@google.com        file_name = new_env.subst("${SHOBJPREFIX}lib${SHOBJSUFFIX}.partial")
106111983Sgabeblack@google.com        target = File(joinpath(group, file_name))
106212310Sgabeblack@google.com        partial = env.PartialShared(target=target, source=group_shared)
106312310Sgabeblack@google.com        shared_objs.extend(partial)
10646143Snate@binkert.org
106512362Sgabeblack@google.com    static_date = date_source.static(new_env)
106612306Sgabeblack@google.com    new_env.Depends(static_date, static_objs)
106712310Sgabeblack@google.com    static_objs.extend(static_date)
106810453SAndrew.Bardsley@arm.com
106912362Sgabeblack@google.com    shared_date = date_source.shared(new_env)
107012306Sgabeblack@google.com    new_env.Depends(shared_date, shared_objs)
107112310Sgabeblack@google.com    shared_objs.extend(shared_date)
10725554Snate@binkert.org
10735522Snate@binkert.org    # First make a library of everything but main() so other programs can
10745522Snate@binkert.org    # link against m5.
10755797Snate@binkert.org    static_lib = new_env.StaticLibrary(libname, static_objs)
10765797Snate@binkert.org    shared_lib = new_env.SharedLibrary(libname, shared_objs)
10775522Snate@binkert.org
10785601Snate@binkert.org    # Now link a stub with main() and the static library.
107912362Sgabeblack@google.com    main_objs = [ s.static(new_env) for s in Source.all.with_tag('main') ]
10808233Snate@binkert.org
10818235Snate@binkert.org    for test in UnitTest.all:
108212302Sgabeblack@google.com        test_sources = Source.all.with_tag(str(test.target))
108312362Sgabeblack@google.com        test_objs = [ s.static(new_env) for s in test_sources ]
10849003SAli.Saidi@ARM.com        if test.main:
10859003SAli.Saidi@ARM.com            test_objs += main_objs
108612222Sgabeblack@google.com        path = 'unittest/%s.%s' % (test.target, label)
108710196SCurtis.Dunham@arm.com        new_env.Program(path, test_objs + static_objs)
10888235Snate@binkert.org
108912313Sgabeblack@google.com    gtest_env = new_env.Clone()
109012313Sgabeblack@google.com    gtest_env.Append(LIBS=gtest_env['GTEST_LIBS'])
109112313Sgabeblack@google.com    gtest_env.Append(CPPFLAGS=gtest_env['GTEST_CPPFLAGS'])
109212371Sgabeblack@google.com    gtestlib_sources = Source.all.with_tag('gtest lib')
109312370Sgabeblack@google.com    gtests = []
109412313Sgabeblack@google.com    for test in GTest.all:
109512371Sgabeblack@google.com        test_sources = test.sources
109612371Sgabeblack@google.com        if not test.skip_lib:
109712371Sgabeblack@google.com            test_sources += gtestlib_sources
109812371Sgabeblack@google.com        for f in test.filters:
109912371Sgabeblack@google.com            test_sources += Source.all.apply_filter(f)
110012362Sgabeblack@google.com        test_objs = [ s.static(gtest_env) for s in test_sources ]
110112370Sgabeblack@google.com        gtests.append(gtest_env.Program(
110212370Sgabeblack@google.com            test.dir.File('%s.%s' % (test.target, label)), test_objs))
110312370Sgabeblack@google.com
110412370Sgabeblack@google.com    gtest_target = Dir(new_env['BUILDDIR']).File('unittests.%s' % label)
110512370Sgabeblack@google.com    AlwaysBuild(Command(gtest_target, gtests, gtests))
110612313Sgabeblack@google.com
11076143Snate@binkert.org    progname = exename
11082655Sstever@eecs.umich.edu    if strip:
11096143Snate@binkert.org        progname += '.unstripped'
11106143Snate@binkert.org
111111985Sgabeblack@google.com    targets = new_env.Program(progname, main_objs + static_objs)
11126143Snate@binkert.org
11136143Snate@binkert.org    if strip:
11144007Ssaidi@eecs.umich.edu        if sys.platform == 'sunos5':
11154596Sbinkertn@umich.edu            cmd = 'cp $SOURCE $TARGET; strip $TARGET'
11164007Ssaidi@eecs.umich.edu        else:
11174596Sbinkertn@umich.edu            cmd = 'strip $SOURCE -o $TARGET'
11187756SAli.Saidi@ARM.com        targets = new_env.Command(exename, progname,
11197816Ssteve.reinhardt@amd.com                    MakeAction(cmd, Transform("STRIP")))
11208334Snate@binkert.org
11218334Snate@binkert.org    new_env.Command(secondary_exename, exename,
11228334Snate@binkert.org            MakeAction('ln $SOURCE $TARGET', Transform("HARDLINK")))
11238334Snate@binkert.org
11245601Snate@binkert.org    new_env.M5Binary = targets[0]
112511993Sgabeblack@google.com
112611993Sgabeblack@google.com    # Set up regression tests.
112711993Sgabeblack@google.com    SConscript(os.path.join(env.root.abspath, 'tests', 'SConscript'),
112812223Sgabeblack@google.com               variant_dir=Dir('tests').Dir(new_env.Label),
112911993Sgabeblack@google.com               exports={ 'env' : new_env }, duplicate=False)
11302655Sstever@eecs.umich.edu
11319225Sandreas.hansson@arm.com# Start out with the compiler flags common to all compilers,
11329225Sandreas.hansson@arm.com# i.e. they all use -g for opt and -g -pg for prof
11339226Sandreas.hansson@arm.comccflags = {'debug' : [], 'opt' : ['-g'], 'fast' : [], 'prof' : ['-g', '-pg'],
11349226Sandreas.hansson@arm.com           'perf' : ['-g']}
11359225Sandreas.hansson@arm.com
11369226Sandreas.hansson@arm.com# Start out with the linker flags common to all linkers, i.e. -pg for
11379226Sandreas.hansson@arm.com# prof, and -lprofiler for perf. The -lprofile flag is surrounded by
11389226Sandreas.hansson@arm.com# no-as-needed and as-needed as the binutils linker is too clever and
11399226Sandreas.hansson@arm.com# simply doesn't link to the library otherwise.
11409226Sandreas.hansson@arm.comldflags = {'debug' : [], 'opt' : [], 'fast' : [], 'prof' : ['-pg'],
11419226Sandreas.hansson@arm.com           'perf' : ['-Wl,--no-as-needed', '-lprofiler', '-Wl,--as-needed']}
11429225Sandreas.hansson@arm.com
11439227Sandreas.hansson@arm.com# For Link Time Optimization, the optimisation flags used to compile
11449227Sandreas.hansson@arm.com# individual files are decoupled from those used at link time
11459227Sandreas.hansson@arm.com# (i.e. you can compile with -O3 and perform LTO with -O0), so we need
11469227Sandreas.hansson@arm.com# to also update the linker flags based on the target.
11478946Sandreas.hansson@arm.comif env['GCC']:
11483918Ssaidi@eecs.umich.edu    if sys.platform == 'sunos5':
11499225Sandreas.hansson@arm.com        ccflags['debug'] += ['-gstabs+']
11503918Ssaidi@eecs.umich.edu    else:
11519225Sandreas.hansson@arm.com        ccflags['debug'] += ['-ggdb3']
11529225Sandreas.hansson@arm.com    ldflags['debug'] += ['-O0']
11539227Sandreas.hansson@arm.com    # opt, fast, prof and perf all share the same cc flags, also add
11549227Sandreas.hansson@arm.com    # the optimization to the ldflags as LTO defers the optimization
11559227Sandreas.hansson@arm.com    # to link time
11569226Sandreas.hansson@arm.com    for target in ['opt', 'fast', 'prof', 'perf']:
11579225Sandreas.hansson@arm.com        ccflags[target] += ['-O3']
11589227Sandreas.hansson@arm.com        ldflags[target] += ['-O3']
11599227Sandreas.hansson@arm.com
11609227Sandreas.hansson@arm.com    ccflags['fast'] += env['LTO_CCFLAGS']
11619227Sandreas.hansson@arm.com    ldflags['fast'] += env['LTO_LDFLAGS']
11628946Sandreas.hansson@arm.comelif env['CLANG']:
11639225Sandreas.hansson@arm.com    ccflags['debug'] += ['-g', '-O0']
11649226Sandreas.hansson@arm.com    # opt, fast, prof and perf all share the same cc flags
11659226Sandreas.hansson@arm.com    for target in ['opt', 'fast', 'prof', 'perf']:
11669226Sandreas.hansson@arm.com        ccflags[target] += ['-O3']
11673515Ssaidi@eecs.umich.eduelse:
11683918Ssaidi@eecs.umich.edu    print 'Unknown compiler, please fix compiler options'
11694762Snate@binkert.org    Exit(1)
11703515Ssaidi@eecs.umich.edu
11718881Smarc.orr@gmail.com
11728881Smarc.orr@gmail.com# To speed things up, we only instantiate the build environments we
11738881Smarc.orr@gmail.com# need.  We try to identify the needed environment for each target; if
11748881Smarc.orr@gmail.com# we can't, we fall back on instantiating all the environments just to
11758881Smarc.orr@gmail.com# be safe.
11769226Sandreas.hansson@arm.comtarget_types = ['debug', 'opt', 'fast', 'prof', 'perf']
11779226Sandreas.hansson@arm.comobj2target = {'do': 'debug', 'o': 'opt', 'fo': 'fast', 'po': 'prof',
11789226Sandreas.hansson@arm.com              'gpo' : 'perf'}
11798881Smarc.orr@gmail.com
11808881Smarc.orr@gmail.comdef identifyTarget(t):
11818881Smarc.orr@gmail.com    ext = t.split('.')[-1]
11828881Smarc.orr@gmail.com    if ext in target_types:
11838881Smarc.orr@gmail.com        return ext
11848881Smarc.orr@gmail.com    if obj2target.has_key(ext):
11858881Smarc.orr@gmail.com        return obj2target[ext]
11868881Smarc.orr@gmail.com    match = re.search(r'/tests/([^/]+)/', t)
11878881Smarc.orr@gmail.com    if match and match.group(1) in target_types:
11888881Smarc.orr@gmail.com        return match.group(1)
11898881Smarc.orr@gmail.com    return 'all'
11908881Smarc.orr@gmail.com
11918881Smarc.orr@gmail.comneeded_envs = [identifyTarget(target) for target in BUILD_TARGETS]
11928881Smarc.orr@gmail.comif 'all' in needed_envs:
11938881Smarc.orr@gmail.com    needed_envs += target_types
11948881Smarc.orr@gmail.com
119512222Sgabeblack@google.com# Debug binary
119612222Sgabeblack@google.comif 'debug' in needed_envs:
119712222Sgabeblack@google.com    makeEnv(env, 'debug', '.do',
119812222Sgabeblack@google.com            CCFLAGS = Split(ccflags['debug']),
119912222Sgabeblack@google.com            CPPDEFINES = ['DEBUG', 'TRACING_ON=1'],
120012222Sgabeblack@google.com            LINKFLAGS = Split(ldflags['debug']))
1201955SN/A
120212222Sgabeblack@google.com# Optimized binary
120312222Sgabeblack@google.comif 'opt' in needed_envs:
120412222Sgabeblack@google.com    makeEnv(env, 'opt', '.o',
120512222Sgabeblack@google.com            CCFLAGS = Split(ccflags['opt']),
120612222Sgabeblack@google.com            CPPDEFINES = ['TRACING_ON=1'],
120712222Sgabeblack@google.com            LINKFLAGS = Split(ldflags['opt']))
1208955SN/A
120912222Sgabeblack@google.com# "Fast" binary
121012222Sgabeblack@google.comif 'fast' in needed_envs:
121112222Sgabeblack@google.com    disable_partial = \
121212222Sgabeblack@google.com            env.get('BROKEN_INCREMENTAL_LTO', False) and \
121312222Sgabeblack@google.com            GetOption('force_lto')
121412222Sgabeblack@google.com    makeEnv(env, 'fast', '.fo', strip = True,
121512222Sgabeblack@google.com            CCFLAGS = Split(ccflags['fast']),
121612222Sgabeblack@google.com            CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
121712222Sgabeblack@google.com            LINKFLAGS = Split(ldflags['fast']),
121812222Sgabeblack@google.com            disable_partial=disable_partial)
12191869SN/A
122012222Sgabeblack@google.com# Profiled binary using gprof
122112222Sgabeblack@google.comif 'prof' in needed_envs:
122212222Sgabeblack@google.com    makeEnv(env, 'prof', '.po',
122312222Sgabeblack@google.com            CCFLAGS = Split(ccflags['prof']),
122412222Sgabeblack@google.com            CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
122512222Sgabeblack@google.com            LINKFLAGS = Split(ldflags['prof']))
12269226Sandreas.hansson@arm.com
122712222Sgabeblack@google.com# Profiled binary using google-pprof
122812222Sgabeblack@google.comif 'perf' in needed_envs:
122912222Sgabeblack@google.com    makeEnv(env, 'perf', '.gpo',
123012222Sgabeblack@google.com            CCFLAGS = Split(ccflags['perf']),
123112222Sgabeblack@google.com            CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
123212222Sgabeblack@google.com            LINKFLAGS = Split(ldflags['perf']))
1233