SConscript revision 12641
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
3112563Sgabeblack@google.comfrom __future__ import print_function
3212563Sgabeblack@google.com
335522Snate@binkert.orgimport array
346143Snate@binkert.orgimport bisect
3512371Sgabeblack@google.comimport functools
364762Snate@binkert.orgimport imp
375522Snate@binkert.orgimport marshal
38955SN/Aimport os
395522Snate@binkert.orgimport re
4011974Sgabeblack@google.comimport subprocess
41955SN/Aimport sys
425522Snate@binkert.orgimport zlib
434202Sbinkertn@umich.edu
445742Snate@binkert.orgfrom os.path import basename, dirname, exists, isdir, isfile, join as joinpath
45955SN/A
464381Sbinkertn@umich.eduimport SCons
474381Sbinkertn@umich.edu
4812246Sgabeblack@google.comfrom gem5_scons import Transform
4912246Sgabeblack@google.com
508334Snate@binkert.org# This file defines how to build a particular configuration of gem5
51955SN/A# based on variable settings in the 'env' build environment.
52955SN/A
534202Sbinkertn@umich.eduImport('*')
54955SN/A
554382Sbinkertn@umich.edu# Children need to see the environment
564382Sbinkertn@umich.eduExport('env')
574382Sbinkertn@umich.edu
586654Snate@binkert.orgbuild_env = [(opt, env[opt]) for opt in export_vars]
595517Snate@binkert.org
608614Sgblack@eecs.umich.edufrom m5.util import code_formatter, compareVersions
617674Snate@binkert.org
626143Snate@binkert.org########################################################################
636143Snate@binkert.org# Code for adding source files of various types
646143Snate@binkert.org#
6512302Sgabeblack@google.com# When specifying a source file of some type, a set of tags can be
6612302Sgabeblack@google.com# specified for that file.
6712302Sgabeblack@google.com
6812371Sgabeblack@google.comclass SourceFilter(object):
6912371Sgabeblack@google.com    def __init__(self, predicate):
7012371Sgabeblack@google.com        self.predicate = predicate
7112371Sgabeblack@google.com
7212371Sgabeblack@google.com    def __or__(self, other):
7312371Sgabeblack@google.com        return SourceFilter(lambda tags: self.predicate(tags) or
7412371Sgabeblack@google.com                                         other.predicate(tags))
7512371Sgabeblack@google.com
7612371Sgabeblack@google.com    def __and__(self, other):
7712371Sgabeblack@google.com        return SourceFilter(lambda tags: self.predicate(tags) and
7812371Sgabeblack@google.com                                         other.predicate(tags))
7912371Sgabeblack@google.com
8012371Sgabeblack@google.comdef with_tags_that(predicate):
8112371Sgabeblack@google.com    '''Return a list of sources with tags that satisfy a predicate.'''
8212371Sgabeblack@google.com    return SourceFilter(predicate)
8312371Sgabeblack@google.com
8412371Sgabeblack@google.comdef with_any_tags(*tags):
8512371Sgabeblack@google.com    '''Return a list of sources with any of the supplied tags.'''
8612371Sgabeblack@google.com    return SourceFilter(lambda stags: len(set(tags) & stags) > 0)
8712371Sgabeblack@google.com
8812371Sgabeblack@google.comdef with_all_tags(*tags):
8912371Sgabeblack@google.com    '''Return a list of sources with all of the supplied tags.'''
9012371Sgabeblack@google.com    return SourceFilter(lambda stags: set(tags) <= stags)
9112371Sgabeblack@google.com
9212371Sgabeblack@google.comdef with_tag(tag):
9312371Sgabeblack@google.com    '''Return a list of sources with the supplied tag.'''
9412371Sgabeblack@google.com    return SourceFilter(lambda stags: tag in stags)
9512371Sgabeblack@google.com
9612371Sgabeblack@google.comdef without_tags(*tags):
9712371Sgabeblack@google.com    '''Return a list of sources without any of the supplied tags.'''
9812371Sgabeblack@google.com    return SourceFilter(lambda stags: len(set(tags) & stags) == 0)
9912371Sgabeblack@google.com
10012371Sgabeblack@google.comdef without_tag(tag):
10112371Sgabeblack@google.com    '''Return a list of sources with the supplied tag.'''
10212371Sgabeblack@google.com    return SourceFilter(lambda stags: tag not in stags)
10312371Sgabeblack@google.com
10412371Sgabeblack@google.comsource_filter_factories = {
10512371Sgabeblack@google.com    'with_tags_that': with_tags_that,
10612371Sgabeblack@google.com    'with_any_tags': with_any_tags,
10712371Sgabeblack@google.com    'with_all_tags': with_all_tags,
10812371Sgabeblack@google.com    'with_tag': with_tag,
10912371Sgabeblack@google.com    'without_tags': without_tags,
11012371Sgabeblack@google.com    'without_tag': without_tag,
11112371Sgabeblack@google.com}
11212371Sgabeblack@google.com
11312371Sgabeblack@google.comExport(source_filter_factories)
11412371Sgabeblack@google.com
11512302Sgabeblack@google.comclass SourceList(list):
11612371Sgabeblack@google.com    def apply_filter(self, f):
11712302Sgabeblack@google.com        def match(source):
11812371Sgabeblack@google.com            return f.predicate(source.tags)
11912302Sgabeblack@google.com        return SourceList(filter(match, self))
12012302Sgabeblack@google.com
12112371Sgabeblack@google.com    def __getattr__(self, name):
12212371Sgabeblack@google.com        func = source_filter_factories.get(name, None)
12312371Sgabeblack@google.com        if not func:
12412371Sgabeblack@google.com            raise AttributeError
12512302Sgabeblack@google.com
12612371Sgabeblack@google.com        @functools.wraps(func)
12712371Sgabeblack@google.com        def wrapper(*args, **kwargs):
12812371Sgabeblack@google.com            return self.apply_filter(func(*args, **kwargs))
12912371Sgabeblack@google.com        return wrapper
13011983Sgabeblack@google.com
1316143Snate@binkert.orgclass SourceMeta(type):
1328233Snate@binkert.org    '''Meta class for source files that keeps track of all files of a
13312302Sgabeblack@google.com    particular type.'''
1346143Snate@binkert.org    def __init__(cls, name, bases, dict):
1356143Snate@binkert.org        super(SourceMeta, cls).__init__(name, bases, dict)
13612302Sgabeblack@google.com        cls.all = SourceList()
1374762Snate@binkert.org
1386143Snate@binkert.orgclass SourceFile(object):
1398233Snate@binkert.org    '''Base object that encapsulates the notion of a source file.
1408233Snate@binkert.org    This includes, the source node, target node, various manipulations
14112302Sgabeblack@google.com    of those.  A source file also specifies a set of tags which
14212302Sgabeblack@google.com    describing arbitrary properties of the source file.'''
1436143Snate@binkert.org    __metaclass__ = SourceMeta
14412362Sgabeblack@google.com
14512362Sgabeblack@google.com    static_objs = {}
14612362Sgabeblack@google.com    shared_objs = {}
14712362Sgabeblack@google.com
14812302Sgabeblack@google.com    def __init__(self, source, tags=None, add_tags=None):
14912302Sgabeblack@google.com        if tags is None:
15012302Sgabeblack@google.com            tags='gem5 lib'
15112302Sgabeblack@google.com        if isinstance(tags, basestring):
15212302Sgabeblack@google.com            tags = set([tags])
15312363Sgabeblack@google.com        if not isinstance(tags, set):
15412363Sgabeblack@google.com            tags = set(tags)
15512363Sgabeblack@google.com        self.tags = tags
15612363Sgabeblack@google.com
15712302Sgabeblack@google.com        if add_tags:
15812363Sgabeblack@google.com            if isinstance(add_tags, basestring):
15912363Sgabeblack@google.com                add_tags = set([add_tags])
16012363Sgabeblack@google.com            if not isinstance(add_tags, set):
16112363Sgabeblack@google.com                add_tags = set(add_tags)
16212363Sgabeblack@google.com            self.tags |= add_tags
1638233Snate@binkert.org
1646143Snate@binkert.org        tnode = source
1656143Snate@binkert.org        if not isinstance(source, SCons.Node.FS.File):
1666143Snate@binkert.org            tnode = File(source)
1676143Snate@binkert.org
1686143Snate@binkert.org        self.tnode = tnode
1696143Snate@binkert.org        self.snode = tnode.srcnode()
1706143Snate@binkert.org
1716143Snate@binkert.org        for base in type(self).__mro__:
1726143Snate@binkert.org            if issubclass(base, SourceFile):
1737065Snate@binkert.org                base.all.append(self)
1746143Snate@binkert.org
17512362Sgabeblack@google.com    def static(self, env):
17612362Sgabeblack@google.com        key = (self.tnode, env['OBJSUFFIX'])
17712362Sgabeblack@google.com        if not key in self.static_objs:
17812362Sgabeblack@google.com            self.static_objs[key] = env.StaticObject(self.tnode)
17912362Sgabeblack@google.com        return self.static_objs[key]
18012362Sgabeblack@google.com
18112362Sgabeblack@google.com    def shared(self, env):
18212362Sgabeblack@google.com        key = (self.tnode, env['OBJSUFFIX'])
18312362Sgabeblack@google.com        if not key in self.shared_objs:
18412362Sgabeblack@google.com            self.shared_objs[key] = env.SharedObject(self.tnode)
18512362Sgabeblack@google.com        return self.shared_objs[key]
18612362Sgabeblack@google.com
1878233Snate@binkert.org    @property
1888233Snate@binkert.org    def filename(self):
1898233Snate@binkert.org        return str(self.tnode)
1908233Snate@binkert.org
1918233Snate@binkert.org    @property
1928233Snate@binkert.org    def dirname(self):
1938233Snate@binkert.org        return dirname(self.filename)
1948233Snate@binkert.org
1958233Snate@binkert.org    @property
1968233Snate@binkert.org    def basename(self):
1978233Snate@binkert.org        return basename(self.filename)
1988233Snate@binkert.org
1998233Snate@binkert.org    @property
2008233Snate@binkert.org    def extname(self):
2018233Snate@binkert.org        index = self.basename.rfind('.')
2028233Snate@binkert.org        if index <= 0:
2038233Snate@binkert.org            # dot files aren't extensions
2048233Snate@binkert.org            return self.basename, None
2058233Snate@binkert.org
2068233Snate@binkert.org        return self.basename[:index], self.basename[index+1:]
2078233Snate@binkert.org
2086143Snate@binkert.org    def __lt__(self, other): return self.filename < other.filename
2096143Snate@binkert.org    def __le__(self, other): return self.filename <= other.filename
2106143Snate@binkert.org    def __gt__(self, other): return self.filename > other.filename
2116143Snate@binkert.org    def __ge__(self, other): return self.filename >= other.filename
2126143Snate@binkert.org    def __eq__(self, other): return self.filename == other.filename
2136143Snate@binkert.org    def __ne__(self, other): return self.filename != other.filename
2149982Satgutier@umich.edu
2156143Snate@binkert.orgclass Source(SourceFile):
21612302Sgabeblack@google.com    ungrouped_tag = 'No link group'
21712302Sgabeblack@google.com    source_groups = set()
21812302Sgabeblack@google.com
21912302Sgabeblack@google.com    _current_group_tag = ungrouped_tag
22012302Sgabeblack@google.com
22112302Sgabeblack@google.com    @staticmethod
22212302Sgabeblack@google.com    def link_group_tag(group):
22312302Sgabeblack@google.com        return 'link group: %s' % group
22411983Sgabeblack@google.com
22511983Sgabeblack@google.com    @classmethod
22611983Sgabeblack@google.com    def set_group(cls, group):
22712302Sgabeblack@google.com        new_tag = Source.link_group_tag(group)
22812302Sgabeblack@google.com        Source._current_group_tag = new_tag
22912302Sgabeblack@google.com        Source.source_groups.add(group)
23012302Sgabeblack@google.com
23112302Sgabeblack@google.com    def _add_link_group_tag(self):
23212302Sgabeblack@google.com        self.tags.add(Source._current_group_tag)
23311983Sgabeblack@google.com
2346143Snate@binkert.org    '''Add a c/c++ source file to the build'''
23512305Sgabeblack@google.com    def __init__(self, source, tags=None, add_tags=None):
23612302Sgabeblack@google.com        '''specify the source file, and any tags'''
23712302Sgabeblack@google.com        super(Source, self).__init__(source, tags, add_tags)
23812302Sgabeblack@google.com        self._add_link_group_tag()
2396143Snate@binkert.org
2406143Snate@binkert.orgclass PySource(SourceFile):
2416143Snate@binkert.org    '''Add a python source file to the named package'''
2425522Snate@binkert.org    invalid_sym_char = re.compile('[^A-z0-9_]')
2436143Snate@binkert.org    modules = {}
2446143Snate@binkert.org    tnodes = {}
2456143Snate@binkert.org    symnames = {}
2469982Satgutier@umich.edu
24712302Sgabeblack@google.com    def __init__(self, package, source, tags=None, add_tags=None):
24812302Sgabeblack@google.com        '''specify the python package, the source file, and any tags'''
24912302Sgabeblack@google.com        super(PySource, self).__init__(source, tags, add_tags)
2506143Snate@binkert.org
2516143Snate@binkert.org        modname,ext = self.extname
2526143Snate@binkert.org        assert ext == 'py'
2536143Snate@binkert.org
2545522Snate@binkert.org        if package:
2555522Snate@binkert.org            path = package.split('.')
2565522Snate@binkert.org        else:
2575522Snate@binkert.org            path = []
2585604Snate@binkert.org
2595604Snate@binkert.org        modpath = path[:]
2606143Snate@binkert.org        if modname != '__init__':
2616143Snate@binkert.org            modpath += [ modname ]
2624762Snate@binkert.org        modpath = '.'.join(modpath)
2634762Snate@binkert.org
2646143Snate@binkert.org        arcpath = path + [ self.basename ]
2656727Ssteve.reinhardt@amd.com        abspath = self.snode.abspath
2666727Ssteve.reinhardt@amd.com        if not exists(abspath):
2676727Ssteve.reinhardt@amd.com            abspath = self.tnode.abspath
2684762Snate@binkert.org
2696143Snate@binkert.org        self.package = package
2706143Snate@binkert.org        self.modname = modname
2716143Snate@binkert.org        self.modpath = modpath
2726143Snate@binkert.org        self.arcname = joinpath(*arcpath)
2736727Ssteve.reinhardt@amd.com        self.abspath = abspath
2746143Snate@binkert.org        self.compiled = File(self.filename + 'c')
2757674Snate@binkert.org        self.cpp = File(self.filename + '.cc')
2767674Snate@binkert.org        self.symname = PySource.invalid_sym_char.sub('_', modpath)
2775604Snate@binkert.org
2786143Snate@binkert.org        PySource.modules[modpath] = self
2796143Snate@binkert.org        PySource.tnodes[self.tnode] = self
2806143Snate@binkert.org        PySource.symnames[self.symname] = self
2814762Snate@binkert.org
2826143Snate@binkert.orgclass SimObject(PySource):
2834762Snate@binkert.org    '''Add a SimObject python file as a python source object and add
2844762Snate@binkert.org    it to a list of sim object modules'''
2854762Snate@binkert.org
2866143Snate@binkert.org    fixed = False
2876143Snate@binkert.org    modnames = []
2884762Snate@binkert.org
28912302Sgabeblack@google.com    def __init__(self, source, tags=None, add_tags=None):
29012302Sgabeblack@google.com        '''Specify the source file and any tags (automatically in
2918233Snate@binkert.org        the m5.objects package)'''
29212302Sgabeblack@google.com        super(SimObject, self).__init__('m5.objects', source, tags, add_tags)
2936143Snate@binkert.org        if self.fixed:
2946143Snate@binkert.org            raise AttributeError, "Too late to call SimObject now."
2954762Snate@binkert.org
2966143Snate@binkert.org        bisect.insort_right(SimObject.modnames, self.modname)
2974762Snate@binkert.org
2989396Sandreas.hansson@arm.comclass ProtoBuf(SourceFile):
2999396Sandreas.hansson@arm.com    '''Add a Protocol Buffer to build'''
3009396Sandreas.hansson@arm.com
30112302Sgabeblack@google.com    def __init__(self, source, tags=None, add_tags=None):
30212302Sgabeblack@google.com        '''Specify the source file, and any tags'''
30312302Sgabeblack@google.com        super(ProtoBuf, self).__init__(source, tags, add_tags)
3049396Sandreas.hansson@arm.com
3059396Sandreas.hansson@arm.com        # Get the file name and the extension
3069396Sandreas.hansson@arm.com        modname,ext = self.extname
3079396Sandreas.hansson@arm.com        assert ext == 'proto'
3089396Sandreas.hansson@arm.com
3099396Sandreas.hansson@arm.com        # Currently, we stick to generating the C++ headers, so we
3109396Sandreas.hansson@arm.com        # only need to track the source and header.
3119930Sandreas.hansson@arm.com        self.cc_file = File(modname + '.pb.cc')
3129930Sandreas.hansson@arm.com        self.hh_file = File(modname + '.pb.h')
3139396Sandreas.hansson@arm.com
3148235Snate@binkert.orgclass UnitTest(object):
3158235Snate@binkert.org    '''Create a UnitTest'''
3166143Snate@binkert.org
3178235Snate@binkert.org    all = []
3189003SAli.Saidi@ARM.com    def __init__(self, target, *sources, **kwargs):
3198235Snate@binkert.org        '''Specify the target name and any sources.  Sources that are
3208235Snate@binkert.org        not SourceFiles are evalued with Source().  All files are
32112302Sgabeblack@google.com        tagged with the name of the UnitTest target.'''
3228235Snate@binkert.org
32312302Sgabeblack@google.com        srcs = SourceList()
3248235Snate@binkert.org        for src in sources:
3258235Snate@binkert.org            if not isinstance(src, SourceFile):
32612302Sgabeblack@google.com                src = Source(src, tags=str(target))
3278235Snate@binkert.org            srcs.append(src)
3288235Snate@binkert.org
3298235Snate@binkert.org        self.sources = srcs
3308235Snate@binkert.org        self.target = target
3319003SAli.Saidi@ARM.com        self.main = kwargs.get('main', False)
33212313Sgabeblack@google.com        self.all.append(self)
33312313Sgabeblack@google.com
33412313Sgabeblack@google.comclass GTest(UnitTest):
33512313Sgabeblack@google.com    '''Create a unit test based on the google test framework.'''
33612313Sgabeblack@google.com    all = []
33712315Sgabeblack@google.com    def __init__(self, *args, **kwargs):
33812371Sgabeblack@google.com        isFilter = lambda arg: isinstance(arg, SourceFilter)
33912371Sgabeblack@google.com        self.filters = filter(isFilter, args)
34012371Sgabeblack@google.com        args = filter(lambda a: not isFilter(a), args)
34112315Sgabeblack@google.com        super(GTest, self).__init__(*args, **kwargs)
34212315Sgabeblack@google.com        self.dir = Dir('.')
34312371Sgabeblack@google.com        self.skip_lib = kwargs.pop('skip_lib', False)
3445584Snate@binkert.org
3454382Sbinkertn@umich.edu# Children should have access
3464202Sbinkertn@umich.eduExport('Source')
3474382Sbinkertn@umich.eduExport('PySource')
3484382Sbinkertn@umich.eduExport('SimObject')
3499396Sandreas.hansson@arm.comExport('ProtoBuf')
3505584Snate@binkert.orgExport('UnitTest')
35112313Sgabeblack@google.comExport('GTest')
3524382Sbinkertn@umich.edu
3534382Sbinkertn@umich.edu########################################################################
3544382Sbinkertn@umich.edu#
3558232Snate@binkert.org# Debug Flags
3565192Ssaidi@eecs.umich.edu#
3578232Snate@binkert.orgdebug_flags = {}
3588232Snate@binkert.orgdef DebugFlag(name, desc=None):
3598232Snate@binkert.org    if name in debug_flags:
3605192Ssaidi@eecs.umich.edu        raise AttributeError, "Flag %s already specified" % name
3618232Snate@binkert.org    debug_flags[name] = (name, (), desc)
3625192Ssaidi@eecs.umich.edu
3635799Snate@binkert.orgdef CompoundFlag(name, flags, desc=None):
3648232Snate@binkert.org    if name in debug_flags:
3655192Ssaidi@eecs.umich.edu        raise AttributeError, "Flag %s already specified" % name
3665192Ssaidi@eecs.umich.edu
3675192Ssaidi@eecs.umich.edu    compound = tuple(flags)
3688232Snate@binkert.org    debug_flags[name] = (name, compound, desc)
3695192Ssaidi@eecs.umich.edu
3708232Snate@binkert.orgExport('DebugFlag')
3715192Ssaidi@eecs.umich.eduExport('CompoundFlag')
3725192Ssaidi@eecs.umich.edu
3735192Ssaidi@eecs.umich.edu########################################################################
3745192Ssaidi@eecs.umich.edu#
3754382Sbinkertn@umich.edu# Set some compiler variables
3764382Sbinkertn@umich.edu#
3774382Sbinkertn@umich.edu
3782667Sstever@eecs.umich.edu# Include file paths are rooted in this directory.  SCons will
3792667Sstever@eecs.umich.edu# automatically expand '.' to refer to both the source directory and
3802667Sstever@eecs.umich.edu# the corresponding build directory to pick up generated include
3812667Sstever@eecs.umich.edu# files.
3822667Sstever@eecs.umich.eduenv.Append(CPPPATH=Dir('.'))
3832667Sstever@eecs.umich.edu
3845742Snate@binkert.orgfor extra_dir in extras_dir_list:
3855742Snate@binkert.org    env.Append(CPPPATH=Dir(extra_dir))
3865742Snate@binkert.org
3875793Snate@binkert.org# Workaround for bug in SCons version > 0.97d20071212
3888334Snate@binkert.org# Scons bug id: 2006 gem5 Bug id: 308
3895793Snate@binkert.orgfor root, dirs, files in os.walk(base_dir, topdown=True):
3905793Snate@binkert.org    Dir(root[len(base_dir) + 1:])
3915793Snate@binkert.org
3924382Sbinkertn@umich.edu########################################################################
3934762Snate@binkert.org#
3945344Sstever@gmail.com# Walk the tree and execute all SConscripts in subdirectories
3954382Sbinkertn@umich.edu#
3965341Sstever@gmail.com
3975742Snate@binkert.orghere = Dir('.').srcnode().abspath
3985742Snate@binkert.orgfor root, dirs, files in os.walk(base_dir, topdown=True):
3995742Snate@binkert.org    if root == here:
4005742Snate@binkert.org        # we don't want to recurse back into this SConscript
4015742Snate@binkert.org        continue
4024762Snate@binkert.org
4035742Snate@binkert.org    if 'SConscript' in files:
4045742Snate@binkert.org        build_dir = joinpath(env['BUILDDIR'], root[len(base_dir) + 1:])
40511984Sgabeblack@google.com        Source.set_group(build_dir)
4067722Sgblack@eecs.umich.edu        SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
4075742Snate@binkert.org
4085742Snate@binkert.orgfor extra_dir in extras_dir_list:
4095742Snate@binkert.org    prefix_len = len(dirname(extra_dir)) + 1
4109930Sandreas.hansson@arm.com
4119930Sandreas.hansson@arm.com    # Also add the corresponding build directory to pick up generated
4129930Sandreas.hansson@arm.com    # include files.
4139930Sandreas.hansson@arm.com    env.Append(CPPPATH=Dir(joinpath(env['BUILDDIR'], extra_dir[prefix_len:])))
4149930Sandreas.hansson@arm.com
4155742Snate@binkert.org    for root, dirs, files in os.walk(extra_dir, topdown=True):
4168242Sbradley.danofsky@amd.com        # if build lives in the extras directory, don't walk down it
4178242Sbradley.danofsky@amd.com        if 'build' in dirs:
4188242Sbradley.danofsky@amd.com            dirs.remove('build')
4198242Sbradley.danofsky@amd.com
4205341Sstever@gmail.com        if 'SConscript' in files:
4215742Snate@binkert.org            build_dir = joinpath(env['BUILDDIR'], root[prefix_len:])
4227722Sgblack@eecs.umich.edu            SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
4234773Snate@binkert.org
4246108Snate@binkert.orgfor opt in export_vars:
4251858SN/A    env.ConfigFile(opt)
4261085SN/A
4276658Snate@binkert.orgdef makeTheISA(source, target, env):
4286658Snate@binkert.org    isas = [ src.get_contents() for src in source ]
4297673Snate@binkert.org    target_isa = env['TARGET_ISA']
4306658Snate@binkert.org    def define(isa):
4316658Snate@binkert.org        return isa.upper() + '_ISA'
43211308Santhony.gutierrez@amd.com
4336658Snate@binkert.org    def namespace(isa):
43411308Santhony.gutierrez@amd.com        return isa[0].upper() + isa[1:].lower() + 'ISA'
4356658Snate@binkert.org
4366658Snate@binkert.org
4377673Snate@binkert.org    code = code_formatter()
4387673Snate@binkert.org    code('''\
4397673Snate@binkert.org#ifndef __CONFIG_THE_ISA_HH__
4407673Snate@binkert.org#define __CONFIG_THE_ISA_HH__
4417673Snate@binkert.org
4427673Snate@binkert.org''')
4437673Snate@binkert.org
44410467Sandreas.hansson@arm.com    # create defines for the preprocessing and compile-time determination
4456658Snate@binkert.org    for i,isa in enumerate(isas):
4467673Snate@binkert.org        code('#define $0 $1', define(isa), i + 1)
44710467Sandreas.hansson@arm.com    code()
44810467Sandreas.hansson@arm.com
44910467Sandreas.hansson@arm.com    # create an enum for any run-time determination of the ISA, we
45010467Sandreas.hansson@arm.com    # reuse the same name as the namespaces
45110467Sandreas.hansson@arm.com    code('enum class Arch {')
45210467Sandreas.hansson@arm.com    for i,isa in enumerate(isas):
45310467Sandreas.hansson@arm.com        if i + 1 == len(isas):
45410467Sandreas.hansson@arm.com            code('  $0 = $1', namespace(isa), define(isa))
45510467Sandreas.hansson@arm.com        else:
45610467Sandreas.hansson@arm.com            code('  $0 = $1,', namespace(isa), define(isa))
45710467Sandreas.hansson@arm.com    code('};')
4587673Snate@binkert.org
4597673Snate@binkert.org    code('''
4607673Snate@binkert.org
4617673Snate@binkert.org#define THE_ISA ${{define(target_isa)}}
4627673Snate@binkert.org#define TheISA ${{namespace(target_isa)}}
4639048SAli.Saidi@ARM.com#define THE_ISA_STR "${{target_isa}}"
4647673Snate@binkert.org
4657673Snate@binkert.org#endif // __CONFIG_THE_ISA_HH__''')
4667673Snate@binkert.org
4677673Snate@binkert.org    code.write(str(target[0]))
4686658Snate@binkert.org
4697756SAli.Saidi@ARM.comenv.Command('config/the_isa.hh', map(Value, all_isa_list),
4707816Ssteve.reinhardt@amd.com            MakeAction(makeTheISA, Transform("CFG ISA", 0)))
4716658Snate@binkert.org
47211308Santhony.gutierrez@amd.comdef makeTheGPUISA(source, target, env):
47311308Santhony.gutierrez@amd.com    isas = [ src.get_contents() for src in source ]
47411308Santhony.gutierrez@amd.com    target_gpu_isa = env['TARGET_GPU_ISA']
47511308Santhony.gutierrez@amd.com    def define(isa):
47611308Santhony.gutierrez@amd.com        return isa.upper() + '_ISA'
47711308Santhony.gutierrez@amd.com
47811308Santhony.gutierrez@amd.com    def namespace(isa):
47911308Santhony.gutierrez@amd.com        return isa[0].upper() + isa[1:].lower() + 'ISA'
48011308Santhony.gutierrez@amd.com
48111308Santhony.gutierrez@amd.com
48211308Santhony.gutierrez@amd.com    code = code_formatter()
48311308Santhony.gutierrez@amd.com    code('''\
48411308Santhony.gutierrez@amd.com#ifndef __CONFIG_THE_GPU_ISA_HH__
48511308Santhony.gutierrez@amd.com#define __CONFIG_THE_GPU_ISA_HH__
48611308Santhony.gutierrez@amd.com
48711308Santhony.gutierrez@amd.com''')
48811308Santhony.gutierrez@amd.com
48911308Santhony.gutierrez@amd.com    # create defines for the preprocessing and compile-time determination
49011308Santhony.gutierrez@amd.com    for i,isa in enumerate(isas):
49111308Santhony.gutierrez@amd.com        code('#define $0 $1', define(isa), i + 1)
49211308Santhony.gutierrez@amd.com    code()
49311308Santhony.gutierrez@amd.com
49411308Santhony.gutierrez@amd.com    # create an enum for any run-time determination of the ISA, we
49511308Santhony.gutierrez@amd.com    # reuse the same name as the namespaces
49611308Santhony.gutierrez@amd.com    code('enum class GPUArch {')
49711308Santhony.gutierrez@amd.com    for i,isa in enumerate(isas):
49811308Santhony.gutierrez@amd.com        if i + 1 == len(isas):
49911308Santhony.gutierrez@amd.com            code('  $0 = $1', namespace(isa), define(isa))
50011308Santhony.gutierrez@amd.com        else:
50111308Santhony.gutierrez@amd.com            code('  $0 = $1,', namespace(isa), define(isa))
50211308Santhony.gutierrez@amd.com    code('};')
50311308Santhony.gutierrez@amd.com
50411308Santhony.gutierrez@amd.com    code('''
50511308Santhony.gutierrez@amd.com
50611308Santhony.gutierrez@amd.com#define THE_GPU_ISA ${{define(target_gpu_isa)}}
50711308Santhony.gutierrez@amd.com#define TheGpuISA ${{namespace(target_gpu_isa)}}
50811308Santhony.gutierrez@amd.com#define THE_GPU_ISA_STR "${{target_gpu_isa}}"
50911308Santhony.gutierrez@amd.com
51011308Santhony.gutierrez@amd.com#endif // __CONFIG_THE_GPU_ISA_HH__''')
51111308Santhony.gutierrez@amd.com
51211308Santhony.gutierrez@amd.com    code.write(str(target[0]))
51311308Santhony.gutierrez@amd.com
51411308Santhony.gutierrez@amd.comenv.Command('config/the_gpu_isa.hh', map(Value, all_gpu_isa_list),
51511308Santhony.gutierrez@amd.com            MakeAction(makeTheGPUISA, Transform("CFG ISA", 0)))
51611308Santhony.gutierrez@amd.com
5174382Sbinkertn@umich.edu########################################################################
5184382Sbinkertn@umich.edu#
5194762Snate@binkert.org# Prevent any SimObjects from being added after this point, they
5204762Snate@binkert.org# should all have been added in the SConscripts above
5214762Snate@binkert.org#
5226654Snate@binkert.orgSimObject.fixed = True
5236654Snate@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]
5385517Snate@binkert.org        self.installed = set()
5395517Snate@binkert.org
5405517Snate@binkert.org    def find_module(self, fullname, path):
5416654Snate@binkert.org        if fullname == 'm5.defines':
5425517Snate@binkert.org            return self
5435517Snate@binkert.org
5445517Snate@binkert.org        if fullname == 'm5.objects':
5455517Snate@binkert.org            return self
5465517Snate@binkert.org
54711802Sandreas.sandberg@arm.com        if fullname.startswith('_m5'):
5485517Snate@binkert.org            return None
5495517Snate@binkert.org
5506143Snate@binkert.org        source = self.modules.get(fullname, None)
5516654Snate@binkert.org        if source is not None and fullname.startswith('m5.objects'):
5525517Snate@binkert.org            return self
5535517Snate@binkert.org
5545517Snate@binkert.org        return None
5555517Snate@binkert.org
5565517Snate@binkert.org    def load_module(self, fullname):
5575517Snate@binkert.org        mod = imp.new_module(fullname)
5585517Snate@binkert.org        sys.modules[fullname] = mod
5595517Snate@binkert.org        self.installed.add(fullname)
5605517Snate@binkert.org
5615517Snate@binkert.org        mod.__loader__ = self
5625517Snate@binkert.org        if fullname == 'm5.objects':
5635517Snate@binkert.org            mod.__path__ = fullname.split('.')
5645517Snate@binkert.org            return mod
5655517Snate@binkert.org
5666654Snate@binkert.org        if fullname == 'm5.defines':
5676654Snate@binkert.org            mod.__dict__['buildEnv'] = m5.util.SmartDict(build_env)
5685517Snate@binkert.org            return mod
5695517Snate@binkert.org
5706143Snate@binkert.org        source = self.modules[fullname]
5716143Snate@binkert.org        if source.modname == '__init__':
5726143Snate@binkert.org            mod.__path__ = source.modpath
5736727Ssteve.reinhardt@amd.com        mod.__file__ = source.abspath
5745517Snate@binkert.org
5756727Ssteve.reinhardt@amd.com        exec file(source.abspath, 'r') in mod.__dict__
5765517Snate@binkert.org
5775517Snate@binkert.org        return mod
5785517Snate@binkert.org
5796654Snate@binkert.orgimport m5.SimObject
5806654Snate@binkert.orgimport m5.params
5817673Snate@binkert.orgfrom m5.util import code_formatter
5826654Snate@binkert.org
5836654Snate@binkert.orgm5.SimObject.clear()
5846654Snate@binkert.orgm5.params.clear()
5856654Snate@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.
5896143Snate@binkert.orgimporter = DictImporter(PySource.modules)
5905517Snate@binkert.orgsys.meta_path[0:0] = [ importer ]
5914762Snate@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
5946143Snate@binkert.orgfor modname in SimObject.modnames:
5956143Snate@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
6056143Snate@binkert.orgfor name,obj in sorted(sim_objects.iteritems()):
6065517Snate@binkert.org    for param in obj._params.local.values():
6076654Snate@binkert.org        # load the ptype attribute now because it depends on the
6086654Snate@binkert.org        # current version of SimObject.allClasses, but when scons
6096654Snate@binkert.org        # actually uses the value, all versions of
6106654Snate@binkert.org        # SimObject.allClasses will have been loaded
6116654Snate@binkert.org        param.ptype
6126654Snate@binkert.org
6134762Snate@binkert.org########################################################################
6144762Snate@binkert.org#
6154762Snate@binkert.org# calculate extra dependencies
6164762Snate@binkert.org#
6174762Snate@binkert.orgmodule_depends = ["m5", "m5.SimObject", "m5.params"]
6187675Snate@binkert.orgdepends = [ PySource.modules[dep].snode for dep in module_depends ]
61910584Sandreas.hansson@arm.comdepends.sort(key = lambda x: x.name)
6204762Snate@binkert.org
6214762Snate@binkert.org########################################################################
6224762Snate@binkert.org#
6234762Snate@binkert.org# Commands for the basic automatically generated python files
6244382Sbinkertn@umich.edu#
6254382Sbinkertn@umich.edu
6265517Snate@binkert.org# Generate Python file containing a dict specifying the current
6276654Snate@binkert.org# buildEnv flags.
6285517Snate@binkert.orgdef makeDefinesPyFile(target, source, env):
6298126Sgblack@eecs.umich.edu    build_env = source[0].get_contents()
6306654Snate@binkert.org
6317673Snate@binkert.org    code = code_formatter()
6326654Snate@binkert.org    code("""
63311802Sandreas.sandberg@arm.comimport _m5.core
6346654Snate@binkert.orgimport m5.util
6356654Snate@binkert.org
6366654Snate@binkert.orgbuildEnv = m5.util.SmartDict($build_env)
6376654Snate@binkert.org
63811802Sandreas.sandberg@arm.comcompileDate = _m5.core.compileDate
6396669Snate@binkert.org_globals = globals()
64011802Sandreas.sandberg@arm.comfor key,val in _m5.core.__dict__.iteritems():
6416669Snate@binkert.org    if key.startswith('flag_'):
6426669Snate@binkert.org        flag = key[5:]
6436669Snate@binkert.org        _globals[flag] = val
6446669Snate@binkert.orgdel _globals
6456654Snate@binkert.org""")
6467673Snate@binkert.org    code.write(target[0].abspath)
6475517Snate@binkert.org
6488126Sgblack@eecs.umich.edudefines_info = Value(build_env)
6495798Snate@binkert.org# Generate a file with all of the compile options in it
6507756SAli.Saidi@ARM.comenv.Command('python/m5/defines.py', defines_info,
6517816Ssteve.reinhardt@amd.com            MakeAction(makeDefinesPyFile, Transform("DEFINES", 0)))
6525798Snate@binkert.orgPySource('m5', 'python/m5/defines.py')
6535798Snate@binkert.org
6545517Snate@binkert.org# Generate python file containing info about the M5 source code
6555517Snate@binkert.orgdef makeInfoPyFile(target, source, env):
6567673Snate@binkert.org    code = code_formatter()
6575517Snate@binkert.org    for src in source:
6585517Snate@binkert.org        data = ''.join(file(src.srcnode().abspath, 'r').xreadlines())
6597673Snate@binkert.org        code('$src = ${{repr(data)}}')
6607673Snate@binkert.org    code.write(str(target[0]))
6615517Snate@binkert.org
6625798Snate@binkert.org# Generate a file that wraps the basic top level files
6635798Snate@binkert.orgenv.Command('python/m5/info.py',
6648333Snate@binkert.org            [ '#/COPYING', '#/LICENSE', '#/README', ],
6657816Ssteve.reinhardt@amd.com            MakeAction(makeInfoPyFile, Transform("INFO")))
6665798Snate@binkert.orgPySource('m5', 'python/m5/info.py')
6675798Snate@binkert.org
6684762Snate@binkert.org########################################################################
6694762Snate@binkert.org#
6704762Snate@binkert.org# Create all of the SimObject param headers and enum headers
6714762Snate@binkert.org#
6724762Snate@binkert.org
6738596Ssteve.reinhardt@amd.comdef createSimObjectParamStruct(target, source, env):
6745517Snate@binkert.org    assert len(target) == 1 and len(source) == 1
6755517Snate@binkert.org
67611997Sgabeblack@google.com    name = source[0].get_text_contents()
6775517Snate@binkert.org    obj = sim_objects[name]
6785517Snate@binkert.org
6797673Snate@binkert.org    code = code_formatter()
6808596Ssteve.reinhardt@amd.com    obj.cxx_param_decl(code)
6817673Snate@binkert.org    code.write(target[0].abspath)
6825517Snate@binkert.org
68310458Sandreas.hansson@arm.comdef createSimObjectCxxConfig(is_header):
68410458Sandreas.hansson@arm.com    def body(target, source, env):
68510458Sandreas.hansson@arm.com        assert len(target) == 1 and len(source) == 1
68610458Sandreas.hansson@arm.com
68710458Sandreas.hansson@arm.com        name = str(source[0].get_contents())
68810458Sandreas.hansson@arm.com        obj = sim_objects[name]
68910458Sandreas.hansson@arm.com
69010458Sandreas.hansson@arm.com        code = code_formatter()
69110458Sandreas.hansson@arm.com        obj.cxx_config_param_file(code, is_header)
69210458Sandreas.hansson@arm.com        code.write(target[0].abspath)
69310458Sandreas.hansson@arm.com    return body
69410458Sandreas.hansson@arm.com
6955517Snate@binkert.orgdef createEnumStrings(target, source, env):
69611996Sgabeblack@google.com    assert len(target) == 1 and len(source) == 2
6975517Snate@binkert.org
69811997Sgabeblack@google.com    name = source[0].get_text_contents()
69911996Sgabeblack@google.com    use_python = source[1].read()
7005517Snate@binkert.org    obj = all_enums[name]
7015517Snate@binkert.org
7027673Snate@binkert.org    code = code_formatter()
7037673Snate@binkert.org    obj.cxx_def(code)
70411996Sgabeblack@google.com    if use_python:
70511988Sandreas.sandberg@arm.com        obj.pybind_def(code)
7067673Snate@binkert.org    code.write(target[0].abspath)
7075517Snate@binkert.org
7088596Ssteve.reinhardt@amd.comdef createEnumDecls(target, source, env):
7095517Snate@binkert.org    assert len(target) == 1 and len(source) == 1
7105517Snate@binkert.org
71111997Sgabeblack@google.com    name = source[0].get_text_contents()
7125517Snate@binkert.org    obj = all_enums[name]
7135517Snate@binkert.org
7147673Snate@binkert.org    code = code_formatter()
7157673Snate@binkert.org    obj.cxx_decl(code)
7167673Snate@binkert.org    code.write(target[0].abspath)
7175517Snate@binkert.org
71811988Sandreas.sandberg@arm.comdef createSimObjectPyBindWrapper(target, source, env):
71911997Sgabeblack@google.com    name = source[0].get_text_contents()
7208596Ssteve.reinhardt@amd.com    obj = sim_objects[name]
7218596Ssteve.reinhardt@amd.com
7228596Ssteve.reinhardt@amd.com    code = code_formatter()
72311988Sandreas.sandberg@arm.com    obj.pybind_decl(code)
7248596Ssteve.reinhardt@amd.com    code.write(target[0].abspath)
7258596Ssteve.reinhardt@amd.com
7268596Ssteve.reinhardt@amd.com# Generate all of the SimObject param C++ struct header files
7274762Snate@binkert.orgparams_hh_files = []
7286143Snate@binkert.orgfor name,simobj in sorted(sim_objects.iteritems()):
7296143Snate@binkert.org    py_source = PySource.modules[simobj.__module__]
7306143Snate@binkert.org    extra_deps = [ py_source.tnode ]
7314762Snate@binkert.org
7324762Snate@binkert.org    hh_file = File('params/%s.hh' % name)
7334762Snate@binkert.org    params_hh_files.append(hh_file)
7347756SAli.Saidi@ARM.com    env.Command(hh_file, Value(name),
7358596Ssteve.reinhardt@amd.com                MakeAction(createSimObjectParamStruct, Transform("SO PARAM")))
7364762Snate@binkert.org    env.Depends(hh_file, depends + extra_deps)
7374762Snate@binkert.org
73810458Sandreas.hansson@arm.com# C++ parameter description files
73910458Sandreas.hansson@arm.comif GetOption('with_cxx_config'):
74010458Sandreas.hansson@arm.com    for name,simobj in sorted(sim_objects.iteritems()):
74110458Sandreas.hansson@arm.com        py_source = PySource.modules[simobj.__module__]
74210458Sandreas.hansson@arm.com        extra_deps = [ py_source.tnode ]
74310458Sandreas.hansson@arm.com
74410458Sandreas.hansson@arm.com        cxx_config_hh_file = File('cxx_config/%s.hh' % name)
74510458Sandreas.hansson@arm.com        cxx_config_cc_file = File('cxx_config/%s.cc' % name)
74610458Sandreas.hansson@arm.com        env.Command(cxx_config_hh_file, Value(name),
74710458Sandreas.hansson@arm.com                    MakeAction(createSimObjectCxxConfig(True),
74810458Sandreas.hansson@arm.com                    Transform("CXXCPRHH")))
74910458Sandreas.hansson@arm.com        env.Command(cxx_config_cc_file, Value(name),
75010458Sandreas.hansson@arm.com                    MakeAction(createSimObjectCxxConfig(False),
75110458Sandreas.hansson@arm.com                    Transform("CXXCPRCC")))
75210458Sandreas.hansson@arm.com        env.Depends(cxx_config_hh_file, depends + extra_deps +
75310458Sandreas.hansson@arm.com                    [File('params/%s.hh' % name), File('sim/cxx_config.hh')])
75410458Sandreas.hansson@arm.com        env.Depends(cxx_config_cc_file, depends + extra_deps +
75510458Sandreas.hansson@arm.com                    [cxx_config_hh_file])
75610458Sandreas.hansson@arm.com        Source(cxx_config_cc_file)
75710458Sandreas.hansson@arm.com
75810458Sandreas.hansson@arm.com    cxx_config_init_cc_file = File('cxx_config/init.cc')
75910458Sandreas.hansson@arm.com
76010458Sandreas.hansson@arm.com    def createCxxConfigInitCC(target, source, env):
76110458Sandreas.hansson@arm.com        assert len(target) == 1 and len(source) == 1
76210458Sandreas.hansson@arm.com
76310458Sandreas.hansson@arm.com        code = code_formatter()
76410458Sandreas.hansson@arm.com
76510458Sandreas.hansson@arm.com        for name,simobj in sorted(sim_objects.iteritems()):
76610458Sandreas.hansson@arm.com            if not hasattr(simobj, 'abstract') or not simobj.abstract:
76710458Sandreas.hansson@arm.com                code('#include "cxx_config/${name}.hh"')
76810458Sandreas.hansson@arm.com        code()
76910458Sandreas.hansson@arm.com        code('void cxxConfigInit()')
77010458Sandreas.hansson@arm.com        code('{')
77110458Sandreas.hansson@arm.com        code.indent()
77210458Sandreas.hansson@arm.com        for name,simobj in sorted(sim_objects.iteritems()):
77310458Sandreas.hansson@arm.com            not_abstract = not hasattr(simobj, 'abstract') or \
77410458Sandreas.hansson@arm.com                not simobj.abstract
77510458Sandreas.hansson@arm.com            if not_abstract and 'type' in simobj.__dict__:
77610458Sandreas.hansson@arm.com                code('cxx_config_directory["${name}"] = '
77710458Sandreas.hansson@arm.com                     '${name}CxxConfigParams::makeDirectoryEntry();')
77810458Sandreas.hansson@arm.com        code.dedent()
77910458Sandreas.hansson@arm.com        code('}')
78010458Sandreas.hansson@arm.com        code.write(target[0].abspath)
78110458Sandreas.hansson@arm.com
78210458Sandreas.hansson@arm.com    py_source = PySource.modules[simobj.__module__]
78310458Sandreas.hansson@arm.com    extra_deps = [ py_source.tnode ]
78410458Sandreas.hansson@arm.com    env.Command(cxx_config_init_cc_file, Value(name),
78510458Sandreas.hansson@arm.com        MakeAction(createCxxConfigInitCC, Transform("CXXCINIT")))
78610458Sandreas.hansson@arm.com    cxx_param_hh_files = ["cxx_config/%s.hh" % simobj
78710584Sandreas.hansson@arm.com        for name,simobj in sorted(sim_objects.iteritems())
78810458Sandreas.hansson@arm.com        if not hasattr(simobj, 'abstract') or not simobj.abstract]
78910458Sandreas.hansson@arm.com    Depends(cxx_config_init_cc_file, cxx_param_hh_files +
79010458Sandreas.hansson@arm.com            [File('sim/cxx_config.hh')])
79110458Sandreas.hansson@arm.com    Source(cxx_config_init_cc_file)
79210458Sandreas.hansson@arm.com
7934762Snate@binkert.org# Generate all enum header files
7946143Snate@binkert.orgfor name,enum in sorted(all_enums.iteritems()):
7956143Snate@binkert.org    py_source = PySource.modules[enum.__module__]
7966143Snate@binkert.org    extra_deps = [ py_source.tnode ]
7974762Snate@binkert.org
7984762Snate@binkert.org    cc_file = File('enums/%s.cc' % name)
79911996Sgabeblack@google.com    env.Command(cc_file, [Value(name), Value(env['USE_PYTHON'])],
8007816Ssteve.reinhardt@amd.com                MakeAction(createEnumStrings, Transform("ENUM STR")))
8014762Snate@binkert.org    env.Depends(cc_file, depends + extra_deps)
8024762Snate@binkert.org    Source(cc_file)
8034762Snate@binkert.org
8044762Snate@binkert.org    hh_file = File('enums/%s.hh' % name)
8057756SAli.Saidi@ARM.com    env.Command(hh_file, Value(name),
8068596Ssteve.reinhardt@amd.com                MakeAction(createEnumDecls, Transform("ENUMDECL")))
8074762Snate@binkert.org    env.Depends(hh_file, depends + extra_deps)
8084762Snate@binkert.org
80911988Sandreas.sandberg@arm.com# Generate SimObject Python bindings wrapper files
81011988Sandreas.sandberg@arm.comif env['USE_PYTHON']:
81111988Sandreas.sandberg@arm.com    for name,simobj in sorted(sim_objects.iteritems()):
81211988Sandreas.sandberg@arm.com        py_source = PySource.modules[simobj.__module__]
81311988Sandreas.sandberg@arm.com        extra_deps = [ py_source.tnode ]
81411988Sandreas.sandberg@arm.com        cc_file = File('python/_m5/param_%s.cc' % name)
81511988Sandreas.sandberg@arm.com        env.Command(cc_file, Value(name),
81611988Sandreas.sandberg@arm.com                    MakeAction(createSimObjectPyBindWrapper,
81711988Sandreas.sandberg@arm.com                               Transform("SO PyBind")))
81811988Sandreas.sandberg@arm.com        env.Depends(cc_file, depends + extra_deps)
81911988Sandreas.sandberg@arm.com        Source(cc_file)
8204382Sbinkertn@umich.edu
8219396Sandreas.hansson@arm.com# Build all protocol buffers if we have got protoc and protobuf available
8229396Sandreas.hansson@arm.comif env['HAVE_PROTOBUF']:
8239396Sandreas.hansson@arm.com    for proto in ProtoBuf.all:
8249396Sandreas.hansson@arm.com        # Use both the source and header as the target, and the .proto
8259396Sandreas.hansson@arm.com        # file as the source. When executing the protoc compiler, also
8269396Sandreas.hansson@arm.com        # specify the proto_path to avoid having the generated files
8279396Sandreas.hansson@arm.com        # include the path.
8289396Sandreas.hansson@arm.com        env.Command([proto.cc_file, proto.hh_file], proto.tnode,
8299396Sandreas.hansson@arm.com                    MakeAction('$PROTOC --cpp_out ${TARGET.dir} '
8309396Sandreas.hansson@arm.com                               '--proto_path ${SOURCE.dir} $SOURCE',
8319396Sandreas.hansson@arm.com                               Transform("PROTOC")))
8329396Sandreas.hansson@arm.com
8339396Sandreas.hansson@arm.com        # Add the C++ source file
83412302Sgabeblack@google.com        Source(proto.cc_file, tags=proto.tags)
8359396Sandreas.hansson@arm.comelif ProtoBuf.all:
83612563Sgabeblack@google.com    print('Got protobuf to build, but lacks support!')
8379396Sandreas.hansson@arm.com    Exit(1)
8389396Sandreas.hansson@arm.com
8398232Snate@binkert.org#
8408232Snate@binkert.org# Handle debug flags
8418232Snate@binkert.org#
8428232Snate@binkert.orgdef makeDebugFlagCC(target, source, env):
8438232Snate@binkert.org    assert(len(target) == 1 and len(source) == 1)
8446229Snate@binkert.org
84510455SCurtis.Dunham@arm.com    code = code_formatter()
8466229Snate@binkert.org
84710455SCurtis.Dunham@arm.com    # delay definition of CompoundFlags until after all the definition
84810455SCurtis.Dunham@arm.com    # of all constituent SimpleFlags
84910455SCurtis.Dunham@arm.com    comp_code = code_formatter()
8505517Snate@binkert.org
8515517Snate@binkert.org    # file header
8527673Snate@binkert.org    code('''
8535517Snate@binkert.org/*
85410455SCurtis.Dunham@arm.com * DO NOT EDIT THIS FILE! Automatically generated by SCons.
8555517Snate@binkert.org */
8565517Snate@binkert.org
8578232Snate@binkert.org#include "base/debug.hh"
85810455SCurtis.Dunham@arm.com
85910455SCurtis.Dunham@arm.comnamespace Debug {
86010455SCurtis.Dunham@arm.com
8617673Snate@binkert.org''')
8627673Snate@binkert.org
86310455SCurtis.Dunham@arm.com    for name, flag in sorted(source[0].read().iteritems()):
86410455SCurtis.Dunham@arm.com        n, compound, desc = flag
86510455SCurtis.Dunham@arm.com        assert n == name
8665517Snate@binkert.org
86710455SCurtis.Dunham@arm.com        if not compound:
86810455SCurtis.Dunham@arm.com            code('SimpleFlag $name("$name", "$desc");')
86910455SCurtis.Dunham@arm.com        else:
87010455SCurtis.Dunham@arm.com            comp_code('CompoundFlag $name("$name", "$desc",')
87110455SCurtis.Dunham@arm.com            comp_code.indent()
87210455SCurtis.Dunham@arm.com            last = len(compound) - 1
87310455SCurtis.Dunham@arm.com            for i,flag in enumerate(compound):
87410455SCurtis.Dunham@arm.com                if i != last:
87510685Sandreas.hansson@arm.com                    comp_code('&$flag,')
87610455SCurtis.Dunham@arm.com                else:
87710685Sandreas.hansson@arm.com                    comp_code('&$flag);')
87810455SCurtis.Dunham@arm.com            comp_code.dedent()
8795517Snate@binkert.org
88010455SCurtis.Dunham@arm.com    code.append(comp_code)
8818232Snate@binkert.org    code()
8828232Snate@binkert.org    code('} // namespace Debug')
8835517Snate@binkert.org
8847673Snate@binkert.org    code.write(str(target[0]))
8855517Snate@binkert.org
8868232Snate@binkert.orgdef makeDebugFlagHH(target, source, env):
8878232Snate@binkert.org    assert(len(target) == 1 and len(source) == 1)
8885517Snate@binkert.org
8898232Snate@binkert.org    val = eval(source[0].get_contents())
8908232Snate@binkert.org    name, compound, desc = val
8918232Snate@binkert.org
8927673Snate@binkert.org    code = code_formatter()
8935517Snate@binkert.org
8945517Snate@binkert.org    # file header boilerplate
8957673Snate@binkert.org    code('''\
8965517Snate@binkert.org/*
89710455SCurtis.Dunham@arm.com * DO NOT EDIT THIS FILE! Automatically generated by SCons.
8985517Snate@binkert.org */
8995517Snate@binkert.org
9008232Snate@binkert.org#ifndef __DEBUG_${name}_HH__
9018232Snate@binkert.org#define __DEBUG_${name}_HH__
9025517Snate@binkert.org
9038232Snate@binkert.orgnamespace Debug {
9048232Snate@binkert.org''')
9055517Snate@binkert.org
9068232Snate@binkert.org    if compound:
9078232Snate@binkert.org        code('class CompoundFlag;')
9088232Snate@binkert.org    code('class SimpleFlag;')
9095517Snate@binkert.org
9108232Snate@binkert.org    if compound:
9118232Snate@binkert.org        code('extern CompoundFlag $name;')
9128232Snate@binkert.org        for flag in compound:
9138232Snate@binkert.org            code('extern SimpleFlag $flag;')
9148232Snate@binkert.org    else:
9158232Snate@binkert.org        code('extern SimpleFlag $name;')
9165517Snate@binkert.org
9178232Snate@binkert.org    code('''
9188232Snate@binkert.org}
9195517Snate@binkert.org
9208232Snate@binkert.org#endif // __DEBUG_${name}_HH__
9217673Snate@binkert.org''')
9225517Snate@binkert.org
9237673Snate@binkert.org    code.write(str(target[0]))
9245517Snate@binkert.org
9258232Snate@binkert.orgfor name,flag in sorted(debug_flags.iteritems()):
9268232Snate@binkert.org    n, compound, desc = flag
9278232Snate@binkert.org    assert n == name
9285192Ssaidi@eecs.umich.edu
92910454SCurtis.Dunham@arm.com    hh_file = 'debug/%s.hh' % name
93010454SCurtis.Dunham@arm.com    env.Command(hh_file, Value(flag),
9318232Snate@binkert.org                MakeAction(makeDebugFlagHH, Transform("TRACING", 0)))
93210455SCurtis.Dunham@arm.com
93310455SCurtis.Dunham@arm.comenv.Command('debug/flags.cc', Value(debug_flags),
93410455SCurtis.Dunham@arm.com            MakeAction(makeDebugFlagCC, Transform("TRACING", 0)))
93510455SCurtis.Dunham@arm.comSource('debug/flags.cc')
9365192Ssaidi@eecs.umich.edu
93711077SCurtis.Dunham@arm.com# version tags
93811330SCurtis.Dunham@arm.comtags = \
93911077SCurtis.Dunham@arm.comenv.Command('sim/tags.cc', None,
94011077SCurtis.Dunham@arm.com            MakeAction('util/cpt_upgrader.py --get-cc-file > $TARGET',
94111077SCurtis.Dunham@arm.com                       Transform("VER TAGS")))
94211330SCurtis.Dunham@arm.comenv.AlwaysBuild(tags)
94311077SCurtis.Dunham@arm.com
9447674Snate@binkert.org# Embed python files.  All .py files that have been indicated by a
9455522Snate@binkert.org# PySource() call in a SConscript need to be embedded into the M5
9465522Snate@binkert.org# library.  To do that, we compile the file to byte code, marshal the
9477674Snate@binkert.org# byte code, compress it, and then generate a c++ file that
9487674Snate@binkert.org# inserts the result into an array.
9497674Snate@binkert.orgdef embedPyFile(target, source, env):
9507674Snate@binkert.org    def c_str(string):
9517674Snate@binkert.org        if string is None:
9527674Snate@binkert.org            return "0"
9537674Snate@binkert.org        return '"%s"' % string
9547674Snate@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'''
9585517Snate@binkert.org
9595522Snate@binkert.org    src = file(str(source[0]), 'r').read()
9605517Snate@binkert.org
9616143Snate@binkert.org    pysource = PySource.tnodes[source[0]]
9626727Ssteve.reinhardt@amd.com    compiled = compile(src, pysource.abspath, 'exec')
9635522Snate@binkert.org    marshalled = marshal.dumps(compiled)
9645522Snate@binkert.org    compressed = zlib.compress(marshalled)
9655522Snate@binkert.org    data = compressed
9667674Snate@binkert.org    sym = pysource.symname
9675517Snate@binkert.org
9687673Snate@binkert.org    code = code_formatter()
9697673Snate@binkert.org    code('''\
9707674Snate@binkert.org#include "sim/init.hh"
9717673Snate@binkert.org
9727674Snate@binkert.orgnamespace {
9737674Snate@binkert.org
9748946Sandreas.hansson@arm.comconst uint8_t data_${sym}[] = {
9757674Snate@binkert.org''')
9767674Snate@binkert.org    code.indent()
9777674Snate@binkert.org    step = 16
9785522Snate@binkert.org    for i in xrange(0, len(data), step):
9795522Snate@binkert.org        x = array.array('B', data[i:i+step])
9807674Snate@binkert.org        code(''.join('%d,' % d for d in x))
9817674Snate@binkert.org    code.dedent()
98211308Santhony.gutierrez@amd.com
9837674Snate@binkert.org    code('''};
9847673Snate@binkert.org
9857674Snate@binkert.orgEmbeddedPython embedded_${sym}(
9867674Snate@binkert.org    ${{c_str(pysource.arcname)}},
9877674Snate@binkert.org    ${{c_str(pysource.abspath)}},
9887674Snate@binkert.org    ${{c_str(pysource.modpath)}},
9897674Snate@binkert.org    data_${sym},
9907674Snate@binkert.org    ${{len(data)}},
9917674Snate@binkert.org    ${{len(marshalled)}});
9927674Snate@binkert.org
9937811Ssteve.reinhardt@amd.com} // anonymous namespace
9947674Snate@binkert.org''')
9957673Snate@binkert.org    code.write(str(target[0]))
9965522Snate@binkert.org
9976143Snate@binkert.orgfor source in PySource.all:
99810453SAndrew.Bardsley@arm.com    env.Command(source.cpp, source.tnode,
9997816Ssteve.reinhardt@amd.com                MakeAction(embedPyFile, Transform("EMBED PY")))
100012302Sgabeblack@google.com    Source(source.cpp, tags=source.tags, add_tags='python')
10014382Sbinkertn@umich.edu
10024382Sbinkertn@umich.edu########################################################################
10034382Sbinkertn@umich.edu#
10044382Sbinkertn@umich.edu# Define binaries.  Each different build type (debug, opt, etc.) gets
10054382Sbinkertn@umich.edu# a slightly different build environment.
10064382Sbinkertn@umich.edu#
10074382Sbinkertn@umich.edu
10084382Sbinkertn@umich.edu# List of constructed environments to pass back to SConstruct
100912302Sgabeblack@google.comdate_source = Source('base/date.cc', tags=[])
10104382Sbinkertn@umich.edu
10112655Sstever@eecs.umich.edu# Function to create a new build environment as clone of current
10122655Sstever@eecs.umich.edu# environment 'env' with modified object suffix and optional stripped
10132655Sstever@eecs.umich.edu# binary.  Additional keyword arguments are appended to corresponding
10142655Sstever@eecs.umich.edu# build environment vars.
101512063Sgabeblack@google.comdef makeEnv(env, label, objsfx, strip=False, disable_partial=False, **kwargs):
10165601Snate@binkert.org    # SCons doesn't know to append a library suffix when there is a '.' in the
10175601Snate@binkert.org    # name.  Use '_' instead.
101812222Sgabeblack@google.com    libname = 'gem5_' + label
101912222Sgabeblack@google.com    exename = 'gem5.' + label
102012222Sgabeblack@google.com    secondary_exename = 'm5.' + label
10215522Snate@binkert.org
10225863Snate@binkert.org    new_env = env.Clone(OBJSUFFIX=objsfx, SHOBJSUFFIX=objsfx + 's')
10235601Snate@binkert.org    new_env.Label = label
10245601Snate@binkert.org    new_env.Append(**kwargs)
10255601Snate@binkert.org
102612302Sgabeblack@google.com    lib_sources = Source.all.with_tag('gem5 lib')
102710453SAndrew.Bardsley@arm.com
102811988Sandreas.sandberg@arm.com    # Without Python, leave out all Python content from the library
102911988Sandreas.sandberg@arm.com    # builds.  The option doesn't affect gem5 built as a program
103010453SAndrew.Bardsley@arm.com    if GetOption('without_python'):
103112302Sgabeblack@google.com        lib_sources = lib_sources.without_tag('python')
103210453SAndrew.Bardsley@arm.com
103311983Sgabeblack@google.com    static_objs = []
103411983Sgabeblack@google.com    shared_objs = []
103512302Sgabeblack@google.com
103612302Sgabeblack@google.com    for s in lib_sources.with_tag(Source.ungrouped_tag):
103712362Sgabeblack@google.com        static_objs.append(s.static(new_env))
103812362Sgabeblack@google.com        shared_objs.append(s.shared(new_env))
103911983Sgabeblack@google.com
104012302Sgabeblack@google.com    for group in Source.source_groups:
104112302Sgabeblack@google.com        srcs = lib_sources.with_tag(Source.link_group_tag(group))
104211983Sgabeblack@google.com        if not srcs:
104311983Sgabeblack@google.com            continue
104411983Sgabeblack@google.com
104512362Sgabeblack@google.com        group_static = [ s.static(new_env) for s in srcs ]
104612362Sgabeblack@google.com        group_shared = [ s.shared(new_env) for s in srcs ]
104712310Sgabeblack@google.com
104812063Sgabeblack@google.com        # If partial linking is disabled, add these sources to the build
104912063Sgabeblack@google.com        # directly, and short circuit this loop.
105012063Sgabeblack@google.com        if disable_partial:
105112310Sgabeblack@google.com            static_objs.extend(group_static)
105212310Sgabeblack@google.com            shared_objs.extend(group_shared)
105312063Sgabeblack@google.com            continue
105412063Sgabeblack@google.com
105511983Sgabeblack@google.com        # Set up the static partially linked objects.
105611983Sgabeblack@google.com        file_name = new_env.subst("${OBJPREFIX}lib${OBJSUFFIX}.partial")
105711983Sgabeblack@google.com        target = File(joinpath(group, file_name))
105812310Sgabeblack@google.com        partial = env.PartialStatic(target=target, source=group_static)
105912310Sgabeblack@google.com        static_objs.extend(partial)
106011983Sgabeblack@google.com
106111983Sgabeblack@google.com        # Set up the shared partially linked objects.
106211983Sgabeblack@google.com        file_name = new_env.subst("${SHOBJPREFIX}lib${SHOBJSUFFIX}.partial")
106311983Sgabeblack@google.com        target = File(joinpath(group, file_name))
106412310Sgabeblack@google.com        partial = env.PartialShared(target=target, source=group_shared)
106512310Sgabeblack@google.com        shared_objs.extend(partial)
10666143Snate@binkert.org
106712362Sgabeblack@google.com    static_date = date_source.static(new_env)
106812306Sgabeblack@google.com    new_env.Depends(static_date, static_objs)
106912310Sgabeblack@google.com    static_objs.extend(static_date)
107010453SAndrew.Bardsley@arm.com
107112362Sgabeblack@google.com    shared_date = date_source.shared(new_env)
107212306Sgabeblack@google.com    new_env.Depends(shared_date, shared_objs)
107312310Sgabeblack@google.com    shared_objs.extend(shared_date)
10745554Snate@binkert.org
10755522Snate@binkert.org    # First make a library of everything but main() so other programs can
10765522Snate@binkert.org    # link against m5.
10775797Snate@binkert.org    static_lib = new_env.StaticLibrary(libname, static_objs)
10785797Snate@binkert.org    shared_lib = new_env.SharedLibrary(libname, shared_objs)
10795522Snate@binkert.org
10805601Snate@binkert.org    # Now link a stub with main() and the static library.
108112362Sgabeblack@google.com    main_objs = [ s.static(new_env) for s in Source.all.with_tag('main') ]
10828233Snate@binkert.org
10838235Snate@binkert.org    for test in UnitTest.all:
108412302Sgabeblack@google.com        test_sources = Source.all.with_tag(str(test.target))
108512362Sgabeblack@google.com        test_objs = [ s.static(new_env) for s in test_sources ]
10869003SAli.Saidi@ARM.com        if test.main:
10879003SAli.Saidi@ARM.com            test_objs += main_objs
108812222Sgabeblack@google.com        path = 'unittest/%s.%s' % (test.target, label)
108910196SCurtis.Dunham@arm.com        new_env.Program(path, test_objs + static_objs)
10908235Snate@binkert.org
109112313Sgabeblack@google.com    gtest_env = new_env.Clone()
109212313Sgabeblack@google.com    gtest_env.Append(LIBS=gtest_env['GTEST_LIBS'])
109312313Sgabeblack@google.com    gtest_env.Append(CPPFLAGS=gtest_env['GTEST_CPPFLAGS'])
109412371Sgabeblack@google.com    gtestlib_sources = Source.all.with_tag('gtest lib')
109512388Sandreas.sandberg@arm.com    gtest_out_dir = Dir(new_env['BUILDDIR']).Dir('unittests.%s' % label)
109612313Sgabeblack@google.com    for test in GTest.all:
109712389Sandreas.sandberg@arm.com        test_sources = list(test.sources)
109812371Sgabeblack@google.com        if not test.skip_lib:
109912371Sgabeblack@google.com            test_sources += gtestlib_sources
110012371Sgabeblack@google.com        for f in test.filters:
110112371Sgabeblack@google.com            test_sources += Source.all.apply_filter(f)
110212362Sgabeblack@google.com        test_objs = [ s.static(gtest_env) for s in test_sources ]
110312388Sandreas.sandberg@arm.com        test_binary = gtest_env.Program(
110412388Sandreas.sandberg@arm.com            test.dir.File('%s.%s' % (test.target, label)), test_objs)
110512370Sgabeblack@google.com
110612388Sandreas.sandberg@arm.com        AlwaysBuild(gtest_env.Command(
110712388Sandreas.sandberg@arm.com            gtest_out_dir.File("%s/%s.xml" % (test.dir, test.target)),
110812388Sandreas.sandberg@arm.com            test_binary, "${SOURCES[0]} --gtest_output=xml:${TARGETS[0]}"))
110912313Sgabeblack@google.com
11106143Snate@binkert.org    progname = exename
11112655Sstever@eecs.umich.edu    if strip:
11126143Snate@binkert.org        progname += '.unstripped'
11136143Snate@binkert.org
111411985Sgabeblack@google.com    targets = new_env.Program(progname, main_objs + static_objs)
11156143Snate@binkert.org
11166143Snate@binkert.org    if strip:
11174007Ssaidi@eecs.umich.edu        if sys.platform == 'sunos5':
11184596Sbinkertn@umich.edu            cmd = 'cp $SOURCE $TARGET; strip $TARGET'
11194007Ssaidi@eecs.umich.edu        else:
11204596Sbinkertn@umich.edu            cmd = 'strip $SOURCE -o $TARGET'
11217756SAli.Saidi@ARM.com        targets = new_env.Command(exename, progname,
11227816Ssteve.reinhardt@amd.com                    MakeAction(cmd, Transform("STRIP")))
11238334Snate@binkert.org
11248334Snate@binkert.org    new_env.Command(secondary_exename, exename,
11258334Snate@binkert.org            MakeAction('ln $SOURCE $TARGET', Transform("HARDLINK")))
11268334Snate@binkert.org
11275601Snate@binkert.org    new_env.M5Binary = targets[0]
112811993Sgabeblack@google.com
112911993Sgabeblack@google.com    # Set up regression tests.
113011993Sgabeblack@google.com    SConscript(os.path.join(env.root.abspath, 'tests', 'SConscript'),
113112223Sgabeblack@google.com               variant_dir=Dir('tests').Dir(new_env.Label),
113211993Sgabeblack@google.com               exports={ 'env' : new_env }, duplicate=False)
11332655Sstever@eecs.umich.edu
11349225Sandreas.hansson@arm.com# Start out with the compiler flags common to all compilers,
11359225Sandreas.hansson@arm.com# i.e. they all use -g for opt and -g -pg for prof
11369226Sandreas.hansson@arm.comccflags = {'debug' : [], 'opt' : ['-g'], 'fast' : [], 'prof' : ['-g', '-pg'],
11379226Sandreas.hansson@arm.com           'perf' : ['-g']}
11389225Sandreas.hansson@arm.com
11399226Sandreas.hansson@arm.com# Start out with the linker flags common to all linkers, i.e. -pg for
11409226Sandreas.hansson@arm.com# prof, and -lprofiler for perf. The -lprofile flag is surrounded by
11419226Sandreas.hansson@arm.com# no-as-needed and as-needed as the binutils linker is too clever and
11429226Sandreas.hansson@arm.com# simply doesn't link to the library otherwise.
11439226Sandreas.hansson@arm.comldflags = {'debug' : [], 'opt' : [], 'fast' : [], 'prof' : ['-pg'],
11449226Sandreas.hansson@arm.com           'perf' : ['-Wl,--no-as-needed', '-lprofiler', '-Wl,--as-needed']}
11459225Sandreas.hansson@arm.com
11469227Sandreas.hansson@arm.com# For Link Time Optimization, the optimisation flags used to compile
11479227Sandreas.hansson@arm.com# individual files are decoupled from those used at link time
11489227Sandreas.hansson@arm.com# (i.e. you can compile with -O3 and perform LTO with -O0), so we need
11499227Sandreas.hansson@arm.com# to also update the linker flags based on the target.
11508946Sandreas.hansson@arm.comif env['GCC']:
11513918Ssaidi@eecs.umich.edu    if sys.platform == 'sunos5':
11529225Sandreas.hansson@arm.com        ccflags['debug'] += ['-gstabs+']
11533918Ssaidi@eecs.umich.edu    else:
11549225Sandreas.hansson@arm.com        ccflags['debug'] += ['-ggdb3']
11559225Sandreas.hansson@arm.com    ldflags['debug'] += ['-O0']
11569227Sandreas.hansson@arm.com    # opt, fast, prof and perf all share the same cc flags, also add
11579227Sandreas.hansson@arm.com    # the optimization to the ldflags as LTO defers the optimization
11589227Sandreas.hansson@arm.com    # to link time
11599226Sandreas.hansson@arm.com    for target in ['opt', 'fast', 'prof', 'perf']:
11609225Sandreas.hansson@arm.com        ccflags[target] += ['-O3']
11619227Sandreas.hansson@arm.com        ldflags[target] += ['-O3']
11629227Sandreas.hansson@arm.com
11639227Sandreas.hansson@arm.com    ccflags['fast'] += env['LTO_CCFLAGS']
11649227Sandreas.hansson@arm.com    ldflags['fast'] += env['LTO_LDFLAGS']
11658946Sandreas.hansson@arm.comelif env['CLANG']:
11669225Sandreas.hansson@arm.com    ccflags['debug'] += ['-g', '-O0']
11679226Sandreas.hansson@arm.com    # opt, fast, prof and perf all share the same cc flags
11689226Sandreas.hansson@arm.com    for target in ['opt', 'fast', 'prof', 'perf']:
11699226Sandreas.hansson@arm.com        ccflags[target] += ['-O3']
11703515Ssaidi@eecs.umich.eduelse:
117112563Sgabeblack@google.com    print('Unknown compiler, please fix compiler options')
11724762Snate@binkert.org    Exit(1)
11733515Ssaidi@eecs.umich.edu
11748881Smarc.orr@gmail.com
11758881Smarc.orr@gmail.com# To speed things up, we only instantiate the build environments we
11768881Smarc.orr@gmail.com# need.  We try to identify the needed environment for each target; if
11778881Smarc.orr@gmail.com# we can't, we fall back on instantiating all the environments just to
11788881Smarc.orr@gmail.com# be safe.
11799226Sandreas.hansson@arm.comtarget_types = ['debug', 'opt', 'fast', 'prof', 'perf']
11809226Sandreas.hansson@arm.comobj2target = {'do': 'debug', 'o': 'opt', 'fo': 'fast', 'po': 'prof',
11819226Sandreas.hansson@arm.com              'gpo' : 'perf'}
11828881Smarc.orr@gmail.com
11838881Smarc.orr@gmail.comdef identifyTarget(t):
11848881Smarc.orr@gmail.com    ext = t.split('.')[-1]
11858881Smarc.orr@gmail.com    if ext in target_types:
11868881Smarc.orr@gmail.com        return ext
11878881Smarc.orr@gmail.com    if obj2target.has_key(ext):
11888881Smarc.orr@gmail.com        return obj2target[ext]
11898881Smarc.orr@gmail.com    match = re.search(r'/tests/([^/]+)/', t)
11908881Smarc.orr@gmail.com    if match and match.group(1) in target_types:
11918881Smarc.orr@gmail.com        return match.group(1)
11928881Smarc.orr@gmail.com    return 'all'
11938881Smarc.orr@gmail.com
11948881Smarc.orr@gmail.comneeded_envs = [identifyTarget(target) for target in BUILD_TARGETS]
11958881Smarc.orr@gmail.comif 'all' in needed_envs:
11968881Smarc.orr@gmail.com    needed_envs += target_types
11978881Smarc.orr@gmail.com
119812222Sgabeblack@google.com# Debug binary
119912222Sgabeblack@google.comif 'debug' in needed_envs:
120012222Sgabeblack@google.com    makeEnv(env, 'debug', '.do',
120112222Sgabeblack@google.com            CCFLAGS = Split(ccflags['debug']),
120212222Sgabeblack@google.com            CPPDEFINES = ['DEBUG', 'TRACING_ON=1'],
120312222Sgabeblack@google.com            LINKFLAGS = Split(ldflags['debug']))
1204955SN/A
120512222Sgabeblack@google.com# Optimized binary
120612222Sgabeblack@google.comif 'opt' in needed_envs:
120712222Sgabeblack@google.com    makeEnv(env, 'opt', '.o',
120812222Sgabeblack@google.com            CCFLAGS = Split(ccflags['opt']),
120912222Sgabeblack@google.com            CPPDEFINES = ['TRACING_ON=1'],
121012222Sgabeblack@google.com            LINKFLAGS = Split(ldflags['opt']))
1211955SN/A
121212222Sgabeblack@google.com# "Fast" binary
121312222Sgabeblack@google.comif 'fast' in needed_envs:
121412222Sgabeblack@google.com    disable_partial = \
121512222Sgabeblack@google.com            env.get('BROKEN_INCREMENTAL_LTO', False) and \
121612222Sgabeblack@google.com            GetOption('force_lto')
121712222Sgabeblack@google.com    makeEnv(env, 'fast', '.fo', strip = True,
121812222Sgabeblack@google.com            CCFLAGS = Split(ccflags['fast']),
121912222Sgabeblack@google.com            CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
122012222Sgabeblack@google.com            LINKFLAGS = Split(ldflags['fast']),
122112222Sgabeblack@google.com            disable_partial=disable_partial)
12221869SN/A
122312222Sgabeblack@google.com# Profiled binary using gprof
122412222Sgabeblack@google.comif 'prof' in needed_envs:
122512222Sgabeblack@google.com    makeEnv(env, 'prof', '.po',
122612222Sgabeblack@google.com            CCFLAGS = Split(ccflags['prof']),
122712222Sgabeblack@google.com            CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
122812222Sgabeblack@google.com            LINKFLAGS = Split(ldflags['prof']))
12299226Sandreas.hansson@arm.com
123012222Sgabeblack@google.com# Profiled binary using google-pprof
123112222Sgabeblack@google.comif 'perf' in needed_envs:
123212222Sgabeblack@google.com    makeEnv(env, 'perf', '.gpo',
123312222Sgabeblack@google.com            CCFLAGS = Split(ccflags['perf']),
123412222Sgabeblack@google.com            CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
123512222Sgabeblack@google.com            LINKFLAGS = Split(ldflags['perf']))
1236