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
334762Snate@binkert.orgimport functools
345522Snate@binkert.orgimport imp
35955SN/Aimport marshal
365522Snate@binkert.orgimport os
3711974Sgabeblack@google.comimport re
38955SN/Aimport subprocess
395522Snate@binkert.orgimport sys
404202Sbinkertn@umich.eduimport zlib
415742Snate@binkert.org
42955SN/Afrom os.path import basename, dirname, exists, isdir, isfile, join as joinpath
434381Sbinkertn@umich.edu
444381Sbinkertn@umich.eduimport SCons
4512246Sgabeblack@google.com
4612246Sgabeblack@google.comfrom gem5_scons import Transform
478334Snate@binkert.org
48955SN/A# This file defines how to build a particular configuration of gem5
49955SN/A# based on variable settings in the 'env' build environment.
504202Sbinkertn@umich.edu
51955SN/AImport('*')
524382Sbinkertn@umich.edu
534382Sbinkertn@umich.edu# Children need to see the environment
544382Sbinkertn@umich.eduExport('env')
556654Snate@binkert.org
565517Snate@binkert.orgbuild_env = [(opt, env[opt]) for opt in export_vars]
578614Sgblack@eecs.umich.edu
587674Snate@binkert.orgfrom m5.util import code_formatter, compareVersions
596143Snate@binkert.org
606143Snate@binkert.org########################################################################
616143Snate@binkert.org# Code for adding source files of various types
6212302Sgabeblack@google.com#
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
6612302Sgabeblack@google.comclass SourceFilter(object):
6712302Sgabeblack@google.com    def __init__(self, predicate):
6812302Sgabeblack@google.com        self.predicate = predicate
6912302Sgabeblack@google.com
7012302Sgabeblack@google.com    def __or__(self, other):
7112302Sgabeblack@google.com        return SourceFilter(lambda tags: self.predicate(tags) or
7212302Sgabeblack@google.com                                         other.predicate(tags))
7312302Sgabeblack@google.com
7412363Sgabeblack@google.com    def __and__(self, other):
7512302Sgabeblack@google.com        return SourceFilter(lambda tags: self.predicate(tags) and
7612302Sgabeblack@google.com                                         other.predicate(tags))
7712302Sgabeblack@google.com
7812363Sgabeblack@google.comdef with_tags_that(predicate):
7912302Sgabeblack@google.com    '''Return a list of sources with tags that satisfy a predicate.'''
8012302Sgabeblack@google.com    return SourceFilter(predicate)
8112302Sgabeblack@google.com
8212302Sgabeblack@google.comdef with_any_tags(*tags):
8312302Sgabeblack@google.com    '''Return a list of sources with any of the supplied tags.'''
8412302Sgabeblack@google.com    return SourceFilter(lambda stags: len(set(tags) & stags) > 0)
8512302Sgabeblack@google.com
8612363Sgabeblack@google.comdef with_all_tags(*tags):
8712302Sgabeblack@google.com    '''Return a list of sources with all of the supplied tags.'''
8812302Sgabeblack@google.com    return SourceFilter(lambda stags: set(tags) <= stags)
8912302Sgabeblack@google.com
9012302Sgabeblack@google.comdef with_tag(tag):
9111983Sgabeblack@google.com    '''Return a list of sources with the supplied tag.'''
926143Snate@binkert.org    return SourceFilter(lambda stags: tag in stags)
938233Snate@binkert.org
9412302Sgabeblack@google.comdef without_tags(*tags):
956143Snate@binkert.org    '''Return a list of sources without any of the supplied tags.'''
966143Snate@binkert.org    return SourceFilter(lambda stags: len(set(tags) & stags) == 0)
9712302Sgabeblack@google.com
984762Snate@binkert.orgdef without_tag(tag):
996143Snate@binkert.org    '''Return a list of sources with the supplied tag.'''
1008233Snate@binkert.org    return SourceFilter(lambda stags: tag not in stags)
1018233Snate@binkert.org
10212302Sgabeblack@google.comsource_filter_factories = {
10312302Sgabeblack@google.com    'with_tags_that': with_tags_that,
1046143Snate@binkert.org    'with_any_tags': with_any_tags,
10512362Sgabeblack@google.com    'with_all_tags': with_all_tags,
10612362Sgabeblack@google.com    'with_tag': with_tag,
10712362Sgabeblack@google.com    'without_tags': without_tags,
10812362Sgabeblack@google.com    'without_tag': without_tag,
10912302Sgabeblack@google.com}
11012302Sgabeblack@google.com
11112302Sgabeblack@google.comExport(source_filter_factories)
11212302Sgabeblack@google.com
11312302Sgabeblack@google.comclass SourceList(list):
11412363Sgabeblack@google.com    def apply_filter(self, f):
11512363Sgabeblack@google.com        def match(source):
11612363Sgabeblack@google.com            return f.predicate(source.tags)
11712363Sgabeblack@google.com        return SourceList(filter(match, self))
11812302Sgabeblack@google.com
11912363Sgabeblack@google.com    def __getattr__(self, name):
12012363Sgabeblack@google.com        func = source_filter_factories.get(name, None)
12112363Sgabeblack@google.com        if not func:
12212363Sgabeblack@google.com            raise AttributeError
12312363Sgabeblack@google.com
1248233Snate@binkert.org        @functools.wraps(func)
1256143Snate@binkert.org        def wrapper(*args, **kwargs):
1266143Snate@binkert.org            return self.apply_filter(func(*args, **kwargs))
1276143Snate@binkert.org        return wrapper
1286143Snate@binkert.org
1296143Snate@binkert.orgclass SourceMeta(type):
1306143Snate@binkert.org    '''Meta class for source files that keeps track of all files of a
1316143Snate@binkert.org    particular type.'''
1326143Snate@binkert.org    def __init__(cls, name, bases, dict):
1336143Snate@binkert.org        super(SourceMeta, cls).__init__(name, bases, dict)
1347065Snate@binkert.org        cls.all = SourceList()
1356143Snate@binkert.org
13612362Sgabeblack@google.comclass SourceFile(object):
13712362Sgabeblack@google.com    '''Base object that encapsulates the notion of a source file.
13812362Sgabeblack@google.com    This includes, the source node, target node, various manipulations
13912362Sgabeblack@google.com    of those.  A source file also specifies a set of tags which
14012362Sgabeblack@google.com    describing arbitrary properties of the source file.'''
14112362Sgabeblack@google.com    __metaclass__ = SourceMeta
14212362Sgabeblack@google.com
14312362Sgabeblack@google.com    static_objs = {}
14412362Sgabeblack@google.com    shared_objs = {}
14512362Sgabeblack@google.com
14612362Sgabeblack@google.com    def __init__(self, source, tags=None, add_tags=None):
14712362Sgabeblack@google.com        if tags is None:
1488233Snate@binkert.org            tags='gem5 lib'
1498233Snate@binkert.org        if isinstance(tags, basestring):
1508233Snate@binkert.org            tags = set([tags])
1518233Snate@binkert.org        if not isinstance(tags, set):
1528233Snate@binkert.org            tags = set(tags)
1538233Snate@binkert.org        self.tags = tags
1548233Snate@binkert.org
1558233Snate@binkert.org        if add_tags:
1568233Snate@binkert.org            if isinstance(add_tags, basestring):
1578233Snate@binkert.org                add_tags = set([add_tags])
1588233Snate@binkert.org            if not isinstance(add_tags, set):
1598233Snate@binkert.org                add_tags = set(add_tags)
1608233Snate@binkert.org            self.tags |= add_tags
1618233Snate@binkert.org
1628233Snate@binkert.org        tnode = source
1638233Snate@binkert.org        if not isinstance(source, SCons.Node.FS.File):
1648233Snate@binkert.org            tnode = File(source)
1658233Snate@binkert.org
1668233Snate@binkert.org        self.tnode = tnode
1678233Snate@binkert.org        self.snode = tnode.srcnode()
1688233Snate@binkert.org
1696143Snate@binkert.org        for base in type(self).__mro__:
1706143Snate@binkert.org            if issubclass(base, SourceFile):
1716143Snate@binkert.org                base.all.append(self)
1726143Snate@binkert.org
1736143Snate@binkert.org    def static(self, env):
1746143Snate@binkert.org        key = (self.tnode, env['OBJSUFFIX'])
1759982Satgutier@umich.edu        if not key in self.static_objs:
1766143Snate@binkert.org            self.static_objs[key] = env.StaticObject(self.tnode)
17712302Sgabeblack@google.com        return self.static_objs[key]
17812302Sgabeblack@google.com
17912302Sgabeblack@google.com    def shared(self, env):
18012302Sgabeblack@google.com        key = (self.tnode, env['OBJSUFFIX'])
18112302Sgabeblack@google.com        if not key in self.shared_objs:
18212302Sgabeblack@google.com            self.shared_objs[key] = env.SharedObject(self.tnode)
18312302Sgabeblack@google.com        return self.shared_objs[key]
18412302Sgabeblack@google.com
18511983Sgabeblack@google.com    @property
18611983Sgabeblack@google.com    def filename(self):
18711983Sgabeblack@google.com        return str(self.tnode)
18812302Sgabeblack@google.com
18912302Sgabeblack@google.com    @property
19012302Sgabeblack@google.com    def dirname(self):
19112302Sgabeblack@google.com        return dirname(self.filename)
19212302Sgabeblack@google.com
19312302Sgabeblack@google.com    @property
19411983Sgabeblack@google.com    def basename(self):
1956143Snate@binkert.org        return basename(self.filename)
19612305Sgabeblack@google.com
19712302Sgabeblack@google.com    @property
19812302Sgabeblack@google.com    def extname(self):
19912302Sgabeblack@google.com        index = self.basename.rfind('.')
2006143Snate@binkert.org        if index <= 0:
2016143Snate@binkert.org            # dot files aren't extensions
2026143Snate@binkert.org            return self.basename, None
2035522Snate@binkert.org
2046143Snate@binkert.org        return self.basename[:index], self.basename[index+1:]
2056143Snate@binkert.org
2066143Snate@binkert.org    def __lt__(self, other): return self.filename < other.filename
2079982Satgutier@umich.edu    def __le__(self, other): return self.filename <= other.filename
20812302Sgabeblack@google.com    def __gt__(self, other): return self.filename > other.filename
20912302Sgabeblack@google.com    def __ge__(self, other): return self.filename >= other.filename
21012302Sgabeblack@google.com    def __eq__(self, other): return self.filename == other.filename
2116143Snate@binkert.org    def __ne__(self, other): return self.filename != other.filename
2126143Snate@binkert.org
2136143Snate@binkert.orgclass Source(SourceFile):
2146143Snate@binkert.org    ungrouped_tag = 'No link group'
2155522Snate@binkert.org    source_groups = set()
2165522Snate@binkert.org
2175522Snate@binkert.org    _current_group_tag = ungrouped_tag
2185522Snate@binkert.org
2195604Snate@binkert.org    @staticmethod
2205604Snate@binkert.org    def link_group_tag(group):
2216143Snate@binkert.org        return 'link group: %s' % group
2226143Snate@binkert.org
2234762Snate@binkert.org    @classmethod
2244762Snate@binkert.org    def set_group(cls, group):
2256143Snate@binkert.org        new_tag = Source.link_group_tag(group)
2266727Ssteve.reinhardt@amd.com        Source._current_group_tag = new_tag
2276727Ssteve.reinhardt@amd.com        Source.source_groups.add(group)
2286727Ssteve.reinhardt@amd.com
2294762Snate@binkert.org    def _add_link_group_tag(self):
2306143Snate@binkert.org        self.tags.add(Source._current_group_tag)
2316143Snate@binkert.org
2326143Snate@binkert.org    '''Add a c/c++ source file to the build'''
2336143Snate@binkert.org    def __init__(self, source, tags=None, add_tags=None):
2346727Ssteve.reinhardt@amd.com        '''specify the source file, and any tags'''
2356143Snate@binkert.org        super(Source, self).__init__(source, tags, add_tags)
2367674Snate@binkert.org        self._add_link_group_tag()
2377674Snate@binkert.org
2385604Snate@binkert.orgclass PySource(SourceFile):
2396143Snate@binkert.org    '''Add a python source file to the named package'''
2406143Snate@binkert.org    invalid_sym_char = re.compile('[^A-z0-9_]')
2416143Snate@binkert.org    modules = {}
2424762Snate@binkert.org    tnodes = {}
2436143Snate@binkert.org    symnames = {}
2444762Snate@binkert.org
2454762Snate@binkert.org    def __init__(self, package, source, tags=None, add_tags=None):
2464762Snate@binkert.org        '''specify the python package, the source file, and any tags'''
2476143Snate@binkert.org        super(PySource, self).__init__(source, tags, add_tags)
2486143Snate@binkert.org
2494762Snate@binkert.org        modname,ext = self.extname
25012302Sgabeblack@google.com        assert ext == 'py'
25112302Sgabeblack@google.com
2528233Snate@binkert.org        if package:
25312302Sgabeblack@google.com            path = package.split('.')
2546143Snate@binkert.org        else:
2556143Snate@binkert.org            path = []
2564762Snate@binkert.org
2576143Snate@binkert.org        modpath = path[:]
2584762Snate@binkert.org        if modname != '__init__':
2599396Sandreas.hansson@arm.com            modpath += [ modname ]
2609396Sandreas.hansson@arm.com        modpath = '.'.join(modpath)
2619396Sandreas.hansson@arm.com
26212302Sgabeblack@google.com        arcpath = path + [ self.basename ]
26312302Sgabeblack@google.com        abspath = self.snode.abspath
26412302Sgabeblack@google.com        if not exists(abspath):
2659396Sandreas.hansson@arm.com            abspath = self.tnode.abspath
2669396Sandreas.hansson@arm.com
2679396Sandreas.hansson@arm.com        self.package = package
2689396Sandreas.hansson@arm.com        self.modname = modname
2699396Sandreas.hansson@arm.com        self.modpath = modpath
2709396Sandreas.hansson@arm.com        self.arcname = joinpath(*arcpath)
2719396Sandreas.hansson@arm.com        self.abspath = abspath
2729930Sandreas.hansson@arm.com        self.compiled = File(self.filename + 'c')
2739930Sandreas.hansson@arm.com        self.cpp = File(self.filename + '.cc')
2749396Sandreas.hansson@arm.com        self.symname = PySource.invalid_sym_char.sub('_', modpath)
2758235Snate@binkert.org
2768235Snate@binkert.org        PySource.modules[modpath] = self
2776143Snate@binkert.org        PySource.tnodes[self.tnode] = self
2788235Snate@binkert.org        PySource.symnames[self.symname] = self
2799003SAli.Saidi@ARM.com
2808235Snate@binkert.orgclass SimObject(PySource):
2818235Snate@binkert.org    '''Add a SimObject python file as a python source object and add
28212302Sgabeblack@google.com    it to a list of sim object modules'''
2838235Snate@binkert.org
28412302Sgabeblack@google.com    fixed = False
2858235Snate@binkert.org    modnames = []
2868235Snate@binkert.org
28712302Sgabeblack@google.com    def __init__(self, source, tags=None, add_tags=None):
2888235Snate@binkert.org        '''Specify the source file and any tags (automatically in
2898235Snate@binkert.org        the m5.objects package)'''
2908235Snate@binkert.org        super(SimObject, self).__init__('m5.objects', source, tags, add_tags)
2918235Snate@binkert.org        if self.fixed:
2929003SAli.Saidi@ARM.com            raise AttributeError, "Too late to call SimObject now."
29312313Sgabeblack@google.com
29412313Sgabeblack@google.com        bisect.insort_right(SimObject.modnames, self.modname)
29512313Sgabeblack@google.com
29612313Sgabeblack@google.comclass ProtoBuf(SourceFile):
29712313Sgabeblack@google.com    '''Add a Protocol Buffer to build'''
29812313Sgabeblack@google.com
29912315Sgabeblack@google.com    def __init__(self, source, tags=None, add_tags=None):
30012315Sgabeblack@google.com        '''Specify the source file, and any tags'''
30112315Sgabeblack@google.com        super(ProtoBuf, self).__init__(source, tags, add_tags)
3025584Snate@binkert.org
3034382Sbinkertn@umich.edu        # Get the file name and the extension
3044202Sbinkertn@umich.edu        modname,ext = self.extname
3054382Sbinkertn@umich.edu        assert ext == 'proto'
3064382Sbinkertn@umich.edu
3079396Sandreas.hansson@arm.com        # Currently, we stick to generating the C++ headers, so we
3085584Snate@binkert.org        # only need to track the source and header.
30912313Sgabeblack@google.com        self.cc_file = File(modname + '.pb.cc')
3104382Sbinkertn@umich.edu        self.hh_file = File(modname + '.pb.h')
3114382Sbinkertn@umich.edu
3124382Sbinkertn@umich.educlass UnitTest(object):
3138232Snate@binkert.org    '''Create a UnitTest'''
3145192Ssaidi@eecs.umich.edu
3158232Snate@binkert.org    all = []
3168232Snate@binkert.org    def __init__(self, target, *sources, **kwargs):
3178232Snate@binkert.org        '''Specify the target name and any sources.  Sources that are
3185192Ssaidi@eecs.umich.edu        not SourceFiles are evalued with Source().  All files are
3198232Snate@binkert.org        tagged with the name of the UnitTest target.'''
3205192Ssaidi@eecs.umich.edu
3215799Snate@binkert.org        srcs = SourceList()
3228232Snate@binkert.org        for src in sources:
3235192Ssaidi@eecs.umich.edu            if not isinstance(src, SourceFile):
3245192Ssaidi@eecs.umich.edu                src = Source(src, tags=str(target))
3255192Ssaidi@eecs.umich.edu            srcs.append(src)
3268232Snate@binkert.org
3275192Ssaidi@eecs.umich.edu        self.sources = srcs
3288232Snate@binkert.org        self.target = target
3295192Ssaidi@eecs.umich.edu        self.main = kwargs.get('main', False)
3305192Ssaidi@eecs.umich.edu        self.all.append(self)
3315192Ssaidi@eecs.umich.edu
3325192Ssaidi@eecs.umich.educlass GTest(UnitTest):
3334382Sbinkertn@umich.edu    '''Create a unit test based on the google test framework.'''
3344382Sbinkertn@umich.edu    all = []
3354382Sbinkertn@umich.edu    def __init__(self, *args, **kwargs):
3362667Sstever@eecs.umich.edu        isFilter = lambda arg: isinstance(arg, SourceFilter)
3372667Sstever@eecs.umich.edu        self.filters = filter(isFilter, args)
3382667Sstever@eecs.umich.edu        args = filter(lambda a: not isFilter(a), args)
3392667Sstever@eecs.umich.edu        super(GTest, self).__init__(*args, **kwargs)
3402667Sstever@eecs.umich.edu        self.dir = Dir('.')
3412667Sstever@eecs.umich.edu        self.skip_lib = kwargs.pop('skip_lib', False)
3425742Snate@binkert.org
3435742Snate@binkert.org# Children should have access
3445742Snate@binkert.orgExport('Source')
3455793Snate@binkert.orgExport('PySource')
3468334Snate@binkert.orgExport('SimObject')
3475793Snate@binkert.orgExport('ProtoBuf')
3485793Snate@binkert.orgExport('UnitTest')
3495793Snate@binkert.orgExport('GTest')
3504382Sbinkertn@umich.edu
3514762Snate@binkert.org########################################################################
3525344Sstever@gmail.com#
3534382Sbinkertn@umich.edu# Debug Flags
3545341Sstever@gmail.com#
3555742Snate@binkert.orgdebug_flags = {}
3565742Snate@binkert.orgdef DebugFlag(name, desc=None):
3575742Snate@binkert.org    if name in debug_flags:
3585742Snate@binkert.org        raise AttributeError, "Flag %s already specified" % name
3595742Snate@binkert.org    debug_flags[name] = (name, (), desc)
3604762Snate@binkert.org
3615742Snate@binkert.orgdef CompoundFlag(name, flags, desc=None):
3625742Snate@binkert.org    if name in debug_flags:
36311984Sgabeblack@google.com        raise AttributeError, "Flag %s already specified" % name
3647722Sgblack@eecs.umich.edu
3655742Snate@binkert.org    compound = tuple(flags)
3665742Snate@binkert.org    debug_flags[name] = (name, compound, desc)
3675742Snate@binkert.org
3689930Sandreas.hansson@arm.comExport('DebugFlag')
3699930Sandreas.hansson@arm.comExport('CompoundFlag')
3709930Sandreas.hansson@arm.com
3719930Sandreas.hansson@arm.com########################################################################
3729930Sandreas.hansson@arm.com#
3735742Snate@binkert.org# Set some compiler variables
3748242Sbradley.danofsky@amd.com#
3758242Sbradley.danofsky@amd.com
3768242Sbradley.danofsky@amd.com# Include file paths are rooted in this directory.  SCons will
3778242Sbradley.danofsky@amd.com# automatically expand '.' to refer to both the source directory and
3785341Sstever@gmail.com# the corresponding build directory to pick up generated include
3795742Snate@binkert.org# files.
3807722Sgblack@eecs.umich.eduenv.Append(CPPPATH=Dir('.'))
3814773Snate@binkert.org
3826108Snate@binkert.orgfor extra_dir in extras_dir_list:
3831858SN/A    env.Append(CPPPATH=Dir(extra_dir))
3841085SN/A
3856658Snate@binkert.org# Workaround for bug in SCons version > 0.97d20071212
3866658Snate@binkert.org# Scons bug id: 2006 gem5 Bug id: 308
3877673Snate@binkert.orgfor root, dirs, files in os.walk(base_dir, topdown=True):
3886658Snate@binkert.org    Dir(root[len(base_dir) + 1:])
3896658Snate@binkert.org
39011308Santhony.gutierrez@amd.com########################################################################
3916658Snate@binkert.org#
39211308Santhony.gutierrez@amd.com# Walk the tree and execute all SConscripts in subdirectories
3936658Snate@binkert.org#
3946658Snate@binkert.org
3957673Snate@binkert.orghere = Dir('.').srcnode().abspath
3967673Snate@binkert.orgfor root, dirs, files in os.walk(base_dir, topdown=True):
3977673Snate@binkert.org    if root == here:
3987673Snate@binkert.org        # we don't want to recurse back into this SConscript
3997673Snate@binkert.org        continue
4007673Snate@binkert.org
4017673Snate@binkert.org    if 'SConscript' in files:
40210467Sandreas.hansson@arm.com        build_dir = joinpath(env['BUILDDIR'], root[len(base_dir) + 1:])
4036658Snate@binkert.org        Source.set_group(build_dir)
4047673Snate@binkert.org        SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
40510467Sandreas.hansson@arm.com
40610467Sandreas.hansson@arm.comfor extra_dir in extras_dir_list:
40710467Sandreas.hansson@arm.com    prefix_len = len(dirname(extra_dir)) + 1
40810467Sandreas.hansson@arm.com
40910467Sandreas.hansson@arm.com    # Also add the corresponding build directory to pick up generated
41010467Sandreas.hansson@arm.com    # include files.
41110467Sandreas.hansson@arm.com    env.Append(CPPPATH=Dir(joinpath(env['BUILDDIR'], extra_dir[prefix_len:])))
41210467Sandreas.hansson@arm.com
41310467Sandreas.hansson@arm.com    for root, dirs, files in os.walk(extra_dir, topdown=True):
41410467Sandreas.hansson@arm.com        # if build lives in the extras directory, don't walk down it
41510467Sandreas.hansson@arm.com        if 'build' in dirs:
4167673Snate@binkert.org            dirs.remove('build')
4177673Snate@binkert.org
4187673Snate@binkert.org        if 'SConscript' in files:
4197673Snate@binkert.org            build_dir = joinpath(env['BUILDDIR'], root[prefix_len:])
4207673Snate@binkert.org            SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
4219048SAli.Saidi@ARM.com
4227673Snate@binkert.orgfor opt in export_vars:
4237673Snate@binkert.org    env.ConfigFile(opt)
4247673Snate@binkert.org
4257673Snate@binkert.orgdef makeTheISA(source, target, env):
4266658Snate@binkert.org    isas = [ src.get_contents() for src in source ]
4277756SAli.Saidi@ARM.com    target_isa = env['TARGET_ISA']
4287816Ssteve.reinhardt@amd.com    def define(isa):
4296658Snate@binkert.org        return isa.upper() + '_ISA'
43011308Santhony.gutierrez@amd.com
43111308Santhony.gutierrez@amd.com    def namespace(isa):
43211308Santhony.gutierrez@amd.com        return isa[0].upper() + isa[1:].lower() + 'ISA'
43311308Santhony.gutierrez@amd.com
43411308Santhony.gutierrez@amd.com
43511308Santhony.gutierrez@amd.com    code = code_formatter()
43611308Santhony.gutierrez@amd.com    code('''\
43711308Santhony.gutierrez@amd.com#ifndef __CONFIG_THE_ISA_HH__
43811308Santhony.gutierrez@amd.com#define __CONFIG_THE_ISA_HH__
43911308Santhony.gutierrez@amd.com
44011308Santhony.gutierrez@amd.com''')
44111308Santhony.gutierrez@amd.com
44211308Santhony.gutierrez@amd.com    # create defines for the preprocessing and compile-time determination
44311308Santhony.gutierrez@amd.com    for i,isa in enumerate(isas):
44411308Santhony.gutierrez@amd.com        code('#define $0 $1', define(isa), i + 1)
44511308Santhony.gutierrez@amd.com    code()
44611308Santhony.gutierrez@amd.com
44711308Santhony.gutierrez@amd.com    # create an enum for any run-time determination of the ISA, we
44811308Santhony.gutierrez@amd.com    # reuse the same name as the namespaces
44911308Santhony.gutierrez@amd.com    code('enum class Arch {')
45011308Santhony.gutierrez@amd.com    for i,isa in enumerate(isas):
45111308Santhony.gutierrez@amd.com        if i + 1 == len(isas):
45211308Santhony.gutierrez@amd.com            code('  $0 = $1', namespace(isa), define(isa))
45311308Santhony.gutierrez@amd.com        else:
45411308Santhony.gutierrez@amd.com            code('  $0 = $1,', namespace(isa), define(isa))
45511308Santhony.gutierrez@amd.com    code('};')
45611308Santhony.gutierrez@amd.com
45711308Santhony.gutierrez@amd.com    code('''
45811308Santhony.gutierrez@amd.com
45911308Santhony.gutierrez@amd.com#define THE_ISA ${{define(target_isa)}}
46011308Santhony.gutierrez@amd.com#define TheISA ${{namespace(target_isa)}}
46111308Santhony.gutierrez@amd.com#define THE_ISA_STR "${{target_isa}}"
46211308Santhony.gutierrez@amd.com
46311308Santhony.gutierrez@amd.com#endif // __CONFIG_THE_ISA_HH__''')
46411308Santhony.gutierrez@amd.com
46511308Santhony.gutierrez@amd.com    code.write(str(target[0]))
46611308Santhony.gutierrez@amd.com
46711308Santhony.gutierrez@amd.comenv.Command('config/the_isa.hh', map(Value, all_isa_list),
46811308Santhony.gutierrez@amd.com            MakeAction(makeTheISA, Transform("CFG ISA", 0)))
46911308Santhony.gutierrez@amd.com
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'
4754382Sbinkertn@umich.edu
4764382Sbinkertn@umich.edu    def namespace(isa):
4774762Snate@binkert.org        return isa[0].upper() + isa[1:].lower() + 'ISA'
4784762Snate@binkert.org
4794762Snate@binkert.org
4806654Snate@binkert.org    code = code_formatter()
4816654Snate@binkert.org    code('''\
4825517Snate@binkert.org#ifndef __CONFIG_THE_GPU_ISA_HH__
4835517Snate@binkert.org#define __CONFIG_THE_GPU_ISA_HH__
4845517Snate@binkert.org
4855517Snate@binkert.org''')
4865517Snate@binkert.org
4875517Snate@binkert.org    # create defines for the preprocessing and compile-time determination
4885517Snate@binkert.org    for i,isa in enumerate(isas):
4895517Snate@binkert.org        code('#define $0 $1', define(isa), i + 1)
4905517Snate@binkert.org    code()
4915517Snate@binkert.org
4925517Snate@binkert.org    # create an enum for any run-time determination of the ISA, we
4935517Snate@binkert.org    # reuse the same name as the namespaces
4945517Snate@binkert.org    code('enum class GPUArch {')
4955517Snate@binkert.org    for i,isa in enumerate(isas):
4965517Snate@binkert.org        if i + 1 == len(isas):
4975517Snate@binkert.org            code('  $0 = $1', namespace(isa), define(isa))
4985517Snate@binkert.org        else:
4996654Snate@binkert.org            code('  $0 = $1,', namespace(isa), define(isa))
5005517Snate@binkert.org    code('};')
5015517Snate@binkert.org
5025517Snate@binkert.org    code('''
5035517Snate@binkert.org
5045517Snate@binkert.org#define THE_GPU_ISA ${{define(target_gpu_isa)}}
50511802Sandreas.sandberg@arm.com#define TheGpuISA ${{namespace(target_gpu_isa)}}
5065517Snate@binkert.org#define THE_GPU_ISA_STR "${{target_gpu_isa}}"
5075517Snate@binkert.org
5086143Snate@binkert.org#endif // __CONFIG_THE_GPU_ISA_HH__''')
5096654Snate@binkert.org
5105517Snate@binkert.org    code.write(str(target[0]))
5115517Snate@binkert.org
5125517Snate@binkert.orgenv.Command('config/the_gpu_isa.hh', map(Value, all_gpu_isa_list),
5135517Snate@binkert.org            MakeAction(makeTheGPUISA, Transform("CFG ISA", 0)))
5145517Snate@binkert.org
5155517Snate@binkert.org########################################################################
5165517Snate@binkert.org#
5175517Snate@binkert.org# Prevent any SimObjects from being added after this point, they
5185517Snate@binkert.org# should all have been added in the SConscripts above
5195517Snate@binkert.org#
5205517Snate@binkert.orgSimObject.fixed = True
5215517Snate@binkert.org
5225517Snate@binkert.orgclass DictImporter(object):
5235517Snate@binkert.org    '''This importer takes a dictionary of arbitrary module names that
5246654Snate@binkert.org    map to arbitrary filenames.'''
5256654Snate@binkert.org    def __init__(self, modules):
5265517Snate@binkert.org        self.modules = modules
5275517Snate@binkert.org        self.installed = set()
5286143Snate@binkert.org
5296143Snate@binkert.org    def __del__(self):
5306143Snate@binkert.org        self.unload()
5316727Ssteve.reinhardt@amd.com
5325517Snate@binkert.org    def unload(self):
5336727Ssteve.reinhardt@amd.com        import sys
5345517Snate@binkert.org        for module in self.installed:
5355517Snate@binkert.org            del sys.modules[module]
5365517Snate@binkert.org        self.installed = set()
5376654Snate@binkert.org
5386654Snate@binkert.org    def find_module(self, fullname, path):
5397673Snate@binkert.org        if fullname == 'm5.defines':
5406654Snate@binkert.org            return self
5416654Snate@binkert.org
5426654Snate@binkert.org        if fullname == 'm5.objects':
5436654Snate@binkert.org            return self
5445517Snate@binkert.org
5455517Snate@binkert.org        if fullname.startswith('_m5'):
5465517Snate@binkert.org            return None
5476143Snate@binkert.org
5485517Snate@binkert.org        source = self.modules.get(fullname, None)
5494762Snate@binkert.org        if source is not None and fullname.startswith('m5.objects'):
5505517Snate@binkert.org            return self
5515517Snate@binkert.org
5526143Snate@binkert.org        return None
5536143Snate@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
5636143Snate@binkert.org
5645517Snate@binkert.org        if fullname == 'm5.defines':
5656654Snate@binkert.org            mod.__dict__['buildEnv'] = m5.util.SmartDict(build_env)
5666654Snate@binkert.org            return mod
5676654Snate@binkert.org
5686654Snate@binkert.org        source = self.modules[fullname]
5696654Snate@binkert.org        if source.modname == '__init__':
5706654Snate@binkert.org            mod.__path__ = source.modpath
5714762Snate@binkert.org        mod.__file__ = source.abspath
5724762Snate@binkert.org
5734762Snate@binkert.org        exec file(source.abspath, 'r') in mod.__dict__
5744762Snate@binkert.org
5754762Snate@binkert.org        return mod
5767675Snate@binkert.org
57710584Sandreas.hansson@arm.comimport m5.SimObject
5784762Snate@binkert.orgimport m5.params
5794762Snate@binkert.orgfrom m5.util import code_formatter
5804762Snate@binkert.org
5814762Snate@binkert.orgm5.SimObject.clear()
5824382Sbinkertn@umich.edum5.params.clear()
5834382Sbinkertn@umich.edu
5845517Snate@binkert.org# install the python importer so we can grab stuff from the source
5856654Snate@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.
5878126Sgblack@eecs.umich.eduimporter = DictImporter(PySource.modules)
5886654Snate@binkert.orgsys.meta_path[0:0] = [ importer ]
5897673Snate@binkert.org
5906654Snate@binkert.org# import all sim objects so we can populate the all_objects list
59111802Sandreas.sandberg@arm.com# make sure that we're working with a list, then let's sort it
5926654Snate@binkert.orgfor modname in SimObject.modnames:
5936654Snate@binkert.org    exec('from m5.objects import %s' % modname)
5946654Snate@binkert.org
5956654Snate@binkert.org# we need to unload all of the currently imported modules so that they
59611802Sandreas.sandberg@arm.com# will be re-imported the next time the sconscript is run
5976669Snate@binkert.orgimporter.unload()
59811802Sandreas.sandberg@arm.comsys.meta_path.remove(importer)
5996669Snate@binkert.org
6006669Snate@binkert.orgsim_objects = m5.SimObject.allClasses
6016669Snate@binkert.orgall_enums = m5.params.allEnums
6026669Snate@binkert.org
6036654Snate@binkert.orgfor name,obj in sorted(sim_objects.iteritems()):
6047673Snate@binkert.org    for param in obj._params.local.values():
6055517Snate@binkert.org        # load the ptype attribute now because it depends on the
6068126Sgblack@eecs.umich.edu        # current version of SimObject.allClasses, but when scons
6075798Snate@binkert.org        # actually uses the value, all versions of
6087756SAli.Saidi@ARM.com        # SimObject.allClasses will have been loaded
6097816Ssteve.reinhardt@amd.com        param.ptype
6105798Snate@binkert.org
6115798Snate@binkert.org########################################################################
6125517Snate@binkert.org#
6135517Snate@binkert.org# calculate extra dependencies
6147673Snate@binkert.org#
6155517Snate@binkert.orgmodule_depends = ["m5", "m5.SimObject", "m5.params"]
6165517Snate@binkert.orgdepends = [ PySource.modules[dep].snode for dep in module_depends ]
6177673Snate@binkert.orgdepends.sort(key = lambda x: x.name)
6187673Snate@binkert.org
6195517Snate@binkert.org########################################################################
6205798Snate@binkert.org#
6215798Snate@binkert.org# Commands for the basic automatically generated python files
6228333Snate@binkert.org#
6237816Ssteve.reinhardt@amd.com
6245798Snate@binkert.org# Generate Python file containing a dict specifying the current
6255798Snate@binkert.org# buildEnv flags.
6264762Snate@binkert.orgdef makeDefinesPyFile(target, source, env):
6274762Snate@binkert.org    build_env = source[0].get_contents()
6284762Snate@binkert.org
6294762Snate@binkert.org    code = code_formatter()
6304762Snate@binkert.org    code("""
6318596Ssteve.reinhardt@amd.comimport _m5.core
6325517Snate@binkert.orgimport m5.util
6335517Snate@binkert.org
63411997Sgabeblack@google.combuildEnv = m5.util.SmartDict($build_env)
6355517Snate@binkert.org
6365517Snate@binkert.orgcompileDate = _m5.core.compileDate
6377673Snate@binkert.org_globals = globals()
6388596Ssteve.reinhardt@amd.comfor key,val in _m5.core.__dict__.iteritems():
6397673Snate@binkert.org    if key.startswith('flag_'):
6405517Snate@binkert.org        flag = key[5:]
64110458Sandreas.hansson@arm.com        _globals[flag] = val
64210458Sandreas.hansson@arm.comdel _globals
64310458Sandreas.hansson@arm.com""")
64410458Sandreas.hansson@arm.com    code.write(target[0].abspath)
64510458Sandreas.hansson@arm.com
64610458Sandreas.hansson@arm.comdefines_info = Value(build_env)
64710458Sandreas.hansson@arm.com# Generate a file with all of the compile options in it
64810458Sandreas.hansson@arm.comenv.Command('python/m5/defines.py', defines_info,
64910458Sandreas.hansson@arm.com            MakeAction(makeDefinesPyFile, Transform("DEFINES", 0)))
65010458Sandreas.hansson@arm.comPySource('m5', 'python/m5/defines.py')
65110458Sandreas.hansson@arm.com
65210458Sandreas.hansson@arm.com# Generate python file containing info about the M5 source code
6535517Snate@binkert.orgdef makeInfoPyFile(target, source, env):
65411996Sgabeblack@google.com    code = code_formatter()
6555517Snate@binkert.org    for src in source:
65611997Sgabeblack@google.com        data = ''.join(file(src.srcnode().abspath, 'r').xreadlines())
65711996Sgabeblack@google.com        code('$src = ${{repr(data)}}')
6585517Snate@binkert.org    code.write(str(target[0]))
6595517Snate@binkert.org
6607673Snate@binkert.org# Generate a file that wraps the basic top level files
6617673Snate@binkert.orgenv.Command('python/m5/info.py',
66211996Sgabeblack@google.com            [ '#/COPYING', '#/LICENSE', '#/README', ],
66311988Sandreas.sandberg@arm.com            MakeAction(makeInfoPyFile, Transform("INFO")))
6647673Snate@binkert.orgPySource('m5', 'python/m5/info.py')
6655517Snate@binkert.org
6668596Ssteve.reinhardt@amd.com########################################################################
6675517Snate@binkert.org#
6685517Snate@binkert.org# Create all of the SimObject param headers and enum headers
66911997Sgabeblack@google.com#
6705517Snate@binkert.org
6715517Snate@binkert.orgdef createSimObjectParamStruct(target, source, env):
6727673Snate@binkert.org    assert len(target) == 1 and len(source) == 1
6737673Snate@binkert.org
6747673Snate@binkert.org    name = source[0].get_text_contents()
6755517Snate@binkert.org    obj = sim_objects[name]
67611988Sandreas.sandberg@arm.com
67711997Sgabeblack@google.com    code = code_formatter()
6788596Ssteve.reinhardt@amd.com    obj.cxx_param_decl(code)
6798596Ssteve.reinhardt@amd.com    code.write(target[0].abspath)
6808596Ssteve.reinhardt@amd.com
68111988Sandreas.sandberg@arm.comdef createSimObjectCxxConfig(is_header):
6828596Ssteve.reinhardt@amd.com    def body(target, source, env):
6838596Ssteve.reinhardt@amd.com        assert len(target) == 1 and len(source) == 1
6848596Ssteve.reinhardt@amd.com
6854762Snate@binkert.org        name = str(source[0].get_contents())
6866143Snate@binkert.org        obj = sim_objects[name]
6876143Snate@binkert.org
6886143Snate@binkert.org        code = code_formatter()
6894762Snate@binkert.org        obj.cxx_config_param_file(code, is_header)
6904762Snate@binkert.org        code.write(target[0].abspath)
6914762Snate@binkert.org    return body
6927756SAli.Saidi@ARM.com
6938596Ssteve.reinhardt@amd.comdef createEnumStrings(target, source, env):
6944762Snate@binkert.org    assert len(target) == 1 and len(source) == 2
6954762Snate@binkert.org
69610458Sandreas.hansson@arm.com    name = source[0].get_text_contents()
69710458Sandreas.hansson@arm.com    use_python = source[1].read()
69810458Sandreas.hansson@arm.com    obj = all_enums[name]
69910458Sandreas.hansson@arm.com
70010458Sandreas.hansson@arm.com    code = code_formatter()
70110458Sandreas.hansson@arm.com    obj.cxx_def(code)
70210458Sandreas.hansson@arm.com    if use_python:
70310458Sandreas.hansson@arm.com        obj.pybind_def(code)
70410458Sandreas.hansson@arm.com    code.write(target[0].abspath)
70510458Sandreas.hansson@arm.com
70610458Sandreas.hansson@arm.comdef createEnumDecls(target, source, env):
70710458Sandreas.hansson@arm.com    assert len(target) == 1 and len(source) == 1
70810458Sandreas.hansson@arm.com
70910458Sandreas.hansson@arm.com    name = source[0].get_text_contents()
71010458Sandreas.hansson@arm.com    obj = all_enums[name]
71110458Sandreas.hansson@arm.com
71210458Sandreas.hansson@arm.com    code = code_formatter()
71310458Sandreas.hansson@arm.com    obj.cxx_decl(code)
71410458Sandreas.hansson@arm.com    code.write(target[0].abspath)
71510458Sandreas.hansson@arm.com
71610458Sandreas.hansson@arm.comdef createSimObjectPyBindWrapper(target, source, env):
71710458Sandreas.hansson@arm.com    name = source[0].get_text_contents()
71810458Sandreas.hansson@arm.com    obj = sim_objects[name]
71910458Sandreas.hansson@arm.com
72010458Sandreas.hansson@arm.com    code = code_formatter()
72110458Sandreas.hansson@arm.com    obj.pybind_decl(code)
72210458Sandreas.hansson@arm.com    code.write(target[0].abspath)
72310458Sandreas.hansson@arm.com
72410458Sandreas.hansson@arm.com# Generate all of the SimObject param C++ struct header files
72510458Sandreas.hansson@arm.comparams_hh_files = []
72610458Sandreas.hansson@arm.comfor name,simobj in sorted(sim_objects.iteritems()):
72710458Sandreas.hansson@arm.com    py_source = PySource.modules[simobj.__module__]
72810458Sandreas.hansson@arm.com    extra_deps = [ py_source.tnode ]
72910458Sandreas.hansson@arm.com
73010458Sandreas.hansson@arm.com    hh_file = File('params/%s.hh' % name)
73110458Sandreas.hansson@arm.com    params_hh_files.append(hh_file)
73210458Sandreas.hansson@arm.com    env.Command(hh_file, Value(name),
73310458Sandreas.hansson@arm.com                MakeAction(createSimObjectParamStruct, Transform("SO PARAM")))
73410458Sandreas.hansson@arm.com    env.Depends(hh_file, depends + extra_deps)
73510458Sandreas.hansson@arm.com
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),
74510584Sandreas.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 +
7514762Snate@binkert.org                    [File('params/%s.hh' % name), File('sim/cxx_config.hh')])
7526143Snate@binkert.org        env.Depends(cxx_config_cc_file, depends + extra_deps +
7536143Snate@binkert.org                    [cxx_config_hh_file])
7546143Snate@binkert.org        Source(cxx_config_cc_file)
7554762Snate@binkert.org
7564762Snate@binkert.org    cxx_config_init_cc_file = File('cxx_config/init.cc')
75711996Sgabeblack@google.com
7587816Ssteve.reinhardt@amd.com    def createCxxConfigInitCC(target, source, env):
7594762Snate@binkert.org        assert len(target) == 1 and len(source) == 1
7604762Snate@binkert.org
7614762Snate@binkert.org        code = code_formatter()
7624762Snate@binkert.org
7637756SAli.Saidi@ARM.com        for name,simobj in sorted(sim_objects.iteritems()):
7648596Ssteve.reinhardt@amd.com            if not hasattr(simobj, 'abstract') or not simobj.abstract:
7654762Snate@binkert.org                code('#include "cxx_config/${name}.hh"')
7664762Snate@binkert.org        code()
76711988Sandreas.sandberg@arm.com        code('void cxxConfigInit()')
76811988Sandreas.sandberg@arm.com        code('{')
76911988Sandreas.sandberg@arm.com        code.indent()
77011988Sandreas.sandberg@arm.com        for name,simobj in sorted(sim_objects.iteritems()):
77111988Sandreas.sandberg@arm.com            not_abstract = not hasattr(simobj, 'abstract') or \
77211988Sandreas.sandberg@arm.com                not simobj.abstract
77311988Sandreas.sandberg@arm.com            if not_abstract and 'type' in simobj.__dict__:
77411988Sandreas.sandberg@arm.com                code('cxx_config_directory["${name}"] = '
77511988Sandreas.sandberg@arm.com                     '${name}CxxConfigParams::makeDirectoryEntry();')
77611988Sandreas.sandberg@arm.com        code.dedent()
77711988Sandreas.sandberg@arm.com        code('}')
7784382Sbinkertn@umich.edu        code.write(target[0].abspath)
7799396Sandreas.hansson@arm.com
7809396Sandreas.hansson@arm.com    py_source = PySource.modules[simobj.__module__]
7819396Sandreas.hansson@arm.com    extra_deps = [ py_source.tnode ]
7829396Sandreas.hansson@arm.com    env.Command(cxx_config_init_cc_file, Value(name),
7839396Sandreas.hansson@arm.com        MakeAction(createCxxConfigInitCC, Transform("CXXCINIT")))
7849396Sandreas.hansson@arm.com    cxx_param_hh_files = ["cxx_config/%s.hh" % simobj
7859396Sandreas.hansson@arm.com        for name,simobj in sorted(sim_objects.iteritems())
7869396Sandreas.hansson@arm.com        if not hasattr(simobj, 'abstract') or not simobj.abstract]
7879396Sandreas.hansson@arm.com    Depends(cxx_config_init_cc_file, cxx_param_hh_files +
7889396Sandreas.hansson@arm.com            [File('sim/cxx_config.hh')])
7899396Sandreas.hansson@arm.com    Source(cxx_config_init_cc_file)
7909396Sandreas.hansson@arm.com
7919396Sandreas.hansson@arm.com# Generate all enum header files
79212302Sgabeblack@google.comfor name,enum in sorted(all_enums.iteritems()):
7939396Sandreas.hansson@arm.com    py_source = PySource.modules[enum.__module__]
7949396Sandreas.hansson@arm.com    extra_deps = [ py_source.tnode ]
7959396Sandreas.hansson@arm.com
7969396Sandreas.hansson@arm.com    cc_file = File('enums/%s.cc' % name)
7978232Snate@binkert.org    env.Command(cc_file, [Value(name), Value(env['USE_PYTHON'])],
7988232Snate@binkert.org                MakeAction(createEnumStrings, Transform("ENUM STR")))
7998232Snate@binkert.org    env.Depends(cc_file, depends + extra_deps)
8008232Snate@binkert.org    Source(cc_file)
8018232Snate@binkert.org
8026229Snate@binkert.org    hh_file = File('enums/%s.hh' % name)
80310455SCurtis.Dunham@arm.com    env.Command(hh_file, Value(name),
8046229Snate@binkert.org                MakeAction(createEnumDecls, Transform("ENUMDECL")))
80510455SCurtis.Dunham@arm.com    env.Depends(hh_file, depends + extra_deps)
80610455SCurtis.Dunham@arm.com
80710455SCurtis.Dunham@arm.com# Generate SimObject Python bindings wrapper files
8085517Snate@binkert.orgif env['USE_PYTHON']:
8095517Snate@binkert.org    for name,simobj in sorted(sim_objects.iteritems()):
8107673Snate@binkert.org        py_source = PySource.modules[simobj.__module__]
8115517Snate@binkert.org        extra_deps = [ py_source.tnode ]
81210455SCurtis.Dunham@arm.com        cc_file = File('python/_m5/param_%s.cc' % name)
8135517Snate@binkert.org        env.Command(cc_file, Value(name),
8145517Snate@binkert.org                    MakeAction(createSimObjectPyBindWrapper,
8158232Snate@binkert.org                               Transform("SO PyBind")))
81610455SCurtis.Dunham@arm.com        env.Depends(cc_file, depends + extra_deps)
81710455SCurtis.Dunham@arm.com        Source(cc_file)
81810455SCurtis.Dunham@arm.com
8197673Snate@binkert.org# Build all protocol buffers if we have got protoc and protobuf available
8207673Snate@binkert.orgif env['HAVE_PROTOBUF']:
82110455SCurtis.Dunham@arm.com    for proto in ProtoBuf.all:
82210455SCurtis.Dunham@arm.com        # Use both the source and header as the target, and the .proto
82310455SCurtis.Dunham@arm.com        # file as the source. When executing the protoc compiler, also
8245517Snate@binkert.org        # specify the proto_path to avoid having the generated files
82510455SCurtis.Dunham@arm.com        # include the path.
82610455SCurtis.Dunham@arm.com        env.Command([proto.cc_file, proto.hh_file], proto.tnode,
82710455SCurtis.Dunham@arm.com                    MakeAction('$PROTOC --cpp_out ${TARGET.dir} '
82810455SCurtis.Dunham@arm.com                               '--proto_path ${SOURCE.dir} $SOURCE',
82910455SCurtis.Dunham@arm.com                               Transform("PROTOC")))
83010455SCurtis.Dunham@arm.com
83110455SCurtis.Dunham@arm.com        # Add the C++ source file
83210455SCurtis.Dunham@arm.com        Source(proto.cc_file, tags=proto.tags)
83310685Sandreas.hansson@arm.comelif ProtoBuf.all:
83410455SCurtis.Dunham@arm.com    print 'Got protobuf to build, but lacks support!'
83510685Sandreas.hansson@arm.com    Exit(1)
83610455SCurtis.Dunham@arm.com
8375517Snate@binkert.org#
83810455SCurtis.Dunham@arm.com# Handle debug flags
8398232Snate@binkert.org#
8408232Snate@binkert.orgdef makeDebugFlagCC(target, source, env):
8415517Snate@binkert.org    assert(len(target) == 1 and len(source) == 1)
8427673Snate@binkert.org
8435517Snate@binkert.org    code = code_formatter()
8448232Snate@binkert.org
8458232Snate@binkert.org    # delay definition of CompoundFlags until after all the definition
8465517Snate@binkert.org    # of all constituent SimpleFlags
8478232Snate@binkert.org    comp_code = code_formatter()
8488232Snate@binkert.org
8498232Snate@binkert.org    # file header
8507673Snate@binkert.org    code('''
8515517Snate@binkert.org/*
8525517Snate@binkert.org * DO NOT EDIT THIS FILE! Automatically generated by SCons.
8537673Snate@binkert.org */
8545517Snate@binkert.org
85510455SCurtis.Dunham@arm.com#include "base/debug.hh"
8565517Snate@binkert.org
8575517Snate@binkert.orgnamespace Debug {
8588232Snate@binkert.org
8598232Snate@binkert.org''')
8605517Snate@binkert.org
8618232Snate@binkert.org    for name, flag in sorted(source[0].read().iteritems()):
8628232Snate@binkert.org        n, compound, desc = flag
8635517Snate@binkert.org        assert n == name
8648232Snate@binkert.org
8658232Snate@binkert.org        if not compound:
8668232Snate@binkert.org            code('SimpleFlag $name("$name", "$desc");')
8675517Snate@binkert.org        else:
8688232Snate@binkert.org            comp_code('CompoundFlag $name("$name", "$desc",')
8698232Snate@binkert.org            comp_code.indent()
8708232Snate@binkert.org            last = len(compound) - 1
8718232Snate@binkert.org            for i,flag in enumerate(compound):
8728232Snate@binkert.org                if i != last:
8738232Snate@binkert.org                    comp_code('&$flag,')
8745517Snate@binkert.org                else:
8758232Snate@binkert.org                    comp_code('&$flag);')
8768232Snate@binkert.org            comp_code.dedent()
8775517Snate@binkert.org
8788232Snate@binkert.org    code.append(comp_code)
8797673Snate@binkert.org    code()
8805517Snate@binkert.org    code('} // namespace Debug')
8817673Snate@binkert.org
8825517Snate@binkert.org    code.write(str(target[0]))
8838232Snate@binkert.org
8848232Snate@binkert.orgdef makeDebugFlagHH(target, source, env):
8858232Snate@binkert.org    assert(len(target) == 1 and len(source) == 1)
8865192Ssaidi@eecs.umich.edu
88710454SCurtis.Dunham@arm.com    val = eval(source[0].get_contents())
88810454SCurtis.Dunham@arm.com    name, compound, desc = val
8898232Snate@binkert.org
89010455SCurtis.Dunham@arm.com    code = code_formatter()
89110455SCurtis.Dunham@arm.com
89210455SCurtis.Dunham@arm.com    # file header boilerplate
89310455SCurtis.Dunham@arm.com    code('''\
8945192Ssaidi@eecs.umich.edu/*
89511077SCurtis.Dunham@arm.com * DO NOT EDIT THIS FILE! Automatically generated by SCons.
89611330SCurtis.Dunham@arm.com */
89711077SCurtis.Dunham@arm.com
89811077SCurtis.Dunham@arm.com#ifndef __DEBUG_${name}_HH__
89911077SCurtis.Dunham@arm.com#define __DEBUG_${name}_HH__
90011330SCurtis.Dunham@arm.com
90111077SCurtis.Dunham@arm.comnamespace Debug {
9027674Snate@binkert.org''')
9035522Snate@binkert.org
9045522Snate@binkert.org    if compound:
9057674Snate@binkert.org        code('class CompoundFlag;')
9067674Snate@binkert.org    code('class SimpleFlag;')
9077674Snate@binkert.org
9087674Snate@binkert.org    if compound:
9097674Snate@binkert.org        code('extern CompoundFlag $name;')
9107674Snate@binkert.org        for flag in compound:
9117674Snate@binkert.org            code('extern SimpleFlag $flag;')
9127674Snate@binkert.org    else:
9135522Snate@binkert.org        code('extern SimpleFlag $name;')
9145522Snate@binkert.org
9155522Snate@binkert.org    code('''
9165517Snate@binkert.org}
9175522Snate@binkert.org
9185517Snate@binkert.org#endif // __DEBUG_${name}_HH__
9196143Snate@binkert.org''')
9206727Ssteve.reinhardt@amd.com
9215522Snate@binkert.org    code.write(str(target[0]))
9225522Snate@binkert.org
9235522Snate@binkert.orgfor name,flag in sorted(debug_flags.iteritems()):
9247674Snate@binkert.org    n, compound, desc = flag
9255517Snate@binkert.org    assert n == name
9267673Snate@binkert.org
9277673Snate@binkert.org    hh_file = 'debug/%s.hh' % name
9287674Snate@binkert.org    env.Command(hh_file, Value(flag),
9297673Snate@binkert.org                MakeAction(makeDebugFlagHH, Transform("TRACING", 0)))
9307674Snate@binkert.org
9317674Snate@binkert.orgenv.Command('debug/flags.cc', Value(debug_flags),
9328946Sandreas.hansson@arm.com            MakeAction(makeDebugFlagCC, Transform("TRACING", 0)))
9337674Snate@binkert.orgSource('debug/flags.cc')
9347674Snate@binkert.org
9357674Snate@binkert.org# version tags
9365522Snate@binkert.orgtags = \
9375522Snate@binkert.orgenv.Command('sim/tags.cc', None,
9387674Snate@binkert.org            MakeAction('util/cpt_upgrader.py --get-cc-file > $TARGET',
9397674Snate@binkert.org                       Transform("VER TAGS")))
94011308Santhony.gutierrez@amd.comenv.AlwaysBuild(tags)
9417674Snate@binkert.org
9427673Snate@binkert.org# Embed python files.  All .py files that have been indicated by a
9437674Snate@binkert.org# PySource() call in a SConscript need to be embedded into the M5
9447674Snate@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"
9517811Ssteve.reinhardt@amd.com        return '"%s"' % string
9527674Snate@binkert.org
9537673Snate@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
9556143Snate@binkert.org    as just bytes with a label in the data section'''
95610453SAndrew.Bardsley@arm.com
9577816Ssteve.reinhardt@amd.com    src = file(str(source[0]), 'r').read()
95812302Sgabeblack@google.com
9594382Sbinkertn@umich.edu    pysource = PySource.tnodes[source[0]]
9604382Sbinkertn@umich.edu    compiled = compile(src, pysource.abspath, 'exec')
9614382Sbinkertn@umich.edu    marshalled = marshal.dumps(compiled)
9624382Sbinkertn@umich.edu    compressed = zlib.compress(marshalled)
9634382Sbinkertn@umich.edu    data = compressed
9644382Sbinkertn@umich.edu    sym = pysource.symname
9654382Sbinkertn@umich.edu
9664382Sbinkertn@umich.edu    code = code_formatter()
96712302Sgabeblack@google.com    code('''\
9684382Sbinkertn@umich.edu#include "sim/init.hh"
9692655Sstever@eecs.umich.edu
9702655Sstever@eecs.umich.edunamespace {
9712655Sstever@eecs.umich.edu
9722655Sstever@eecs.umich.educonst uint8_t data_${sym}[] = {
97312063Sgabeblack@google.com''')
9745601Snate@binkert.org    code.indent()
9755601Snate@binkert.org    step = 16
97612222Sgabeblack@google.com    for i in xrange(0, len(data), step):
97712222Sgabeblack@google.com        x = array.array('B', data[i:i+step])
97812222Sgabeblack@google.com        code(''.join('%d,' % d for d in x))
9795522Snate@binkert.org    code.dedent()
9805863Snate@binkert.org
9815601Snate@binkert.org    code('''};
9825601Snate@binkert.org
9835601Snate@binkert.orgEmbeddedPython embedded_${sym}(
98412302Sgabeblack@google.com    ${{c_str(pysource.arcname)}},
98510453SAndrew.Bardsley@arm.com    ${{c_str(pysource.abspath)}},
98611988Sandreas.sandberg@arm.com    ${{c_str(pysource.modpath)}},
98711988Sandreas.sandberg@arm.com    data_${sym},
98810453SAndrew.Bardsley@arm.com    ${{len(data)}},
98912302Sgabeblack@google.com    ${{len(marshalled)}});
99010453SAndrew.Bardsley@arm.com
99111983Sgabeblack@google.com} // anonymous namespace
99211983Sgabeblack@google.com''')
99312302Sgabeblack@google.com    code.write(str(target[0]))
99412302Sgabeblack@google.com
99512362Sgabeblack@google.comfor source in PySource.all:
99612362Sgabeblack@google.com    env.Command(source.cpp, source.tnode,
99711983Sgabeblack@google.com                MakeAction(embedPyFile, Transform("EMBED PY")))
99812302Sgabeblack@google.com    Source(source.cpp, tags=source.tags, add_tags='python')
99912302Sgabeblack@google.com
100011983Sgabeblack@google.com########################################################################
100111983Sgabeblack@google.com#
100211983Sgabeblack@google.com# Define binaries.  Each different build type (debug, opt, etc.) gets
100312362Sgabeblack@google.com# a slightly different build environment.
100412362Sgabeblack@google.com#
100512310Sgabeblack@google.com
100612063Sgabeblack@google.com# List of constructed environments to pass back to SConstruct
100712063Sgabeblack@google.comdate_source = Source('base/date.cc', tags=[])
100812063Sgabeblack@google.com
100912310Sgabeblack@google.com# Function to create a new build environment as clone of current
101012310Sgabeblack@google.com# environment 'env' with modified object suffix and optional stripped
101112063Sgabeblack@google.com# binary.  Additional keyword arguments are appended to corresponding
101212063Sgabeblack@google.com# build environment vars.
101311983Sgabeblack@google.comdef makeEnv(env, label, objsfx, strip=False, disable_partial=False, **kwargs):
101411983Sgabeblack@google.com    # SCons doesn't know to append a library suffix when there is a '.' in the
101511983Sgabeblack@google.com    # name.  Use '_' instead.
101612310Sgabeblack@google.com    libname = 'gem5_' + label
101712310Sgabeblack@google.com    exename = 'gem5.' + label
101811983Sgabeblack@google.com    secondary_exename = 'm5.' + label
101911983Sgabeblack@google.com
102011983Sgabeblack@google.com    new_env = env.Clone(OBJSUFFIX=objsfx, SHOBJSUFFIX=objsfx + 's')
102111983Sgabeblack@google.com    new_env.Label = label
102212310Sgabeblack@google.com    new_env.Append(**kwargs)
102312310Sgabeblack@google.com
10246143Snate@binkert.org    lib_sources = Source.all.with_tag('gem5 lib')
102512362Sgabeblack@google.com
102612306Sgabeblack@google.com    # Without Python, leave out all Python content from the library
102712310Sgabeblack@google.com    # builds.  The option doesn't affect gem5 built as a program
102810453SAndrew.Bardsley@arm.com    if GetOption('without_python'):
102912362Sgabeblack@google.com        lib_sources = lib_sources.without_tag('python')
103012306Sgabeblack@google.com
103112310Sgabeblack@google.com    static_objs = []
10325554Snate@binkert.org    shared_objs = []
10335522Snate@binkert.org
10345522Snate@binkert.org    for s in lib_sources.with_tag(Source.ungrouped_tag):
10355797Snate@binkert.org        static_objs.append(s.static(new_env))
10365797Snate@binkert.org        shared_objs.append(s.shared(new_env))
10375522Snate@binkert.org
10385601Snate@binkert.org    for group in Source.source_groups:
103912362Sgabeblack@google.com        srcs = lib_sources.with_tag(Source.link_group_tag(group))
10408233Snate@binkert.org        if not srcs:
10418235Snate@binkert.org            continue
104212302Sgabeblack@google.com
104312362Sgabeblack@google.com        group_static = [ s.static(new_env) for s in srcs ]
10449003SAli.Saidi@ARM.com        group_shared = [ s.shared(new_env) for s in srcs ]
10459003SAli.Saidi@ARM.com
104612222Sgabeblack@google.com        # If partial linking is disabled, add these sources to the build
104710196SCurtis.Dunham@arm.com        # directly, and short circuit this loop.
10488235Snate@binkert.org        if disable_partial:
104912313Sgabeblack@google.com            static_objs.extend(group_static)
105012313Sgabeblack@google.com            shared_objs.extend(group_shared)
105112313Sgabeblack@google.com            continue
105212313Sgabeblack@google.com
105312313Sgabeblack@google.com        # Set up the static partially linked objects.
105412362Sgabeblack@google.com        file_name = new_env.subst("${OBJPREFIX}lib${OBJSUFFIX}.partial")
105512315Sgabeblack@google.com        target = File(joinpath(group, file_name))
105612315Sgabeblack@google.com        partial = env.PartialStatic(target=target, source=group_static)
105712313Sgabeblack@google.com        static_objs.extend(partial)
10586143Snate@binkert.org
10592655Sstever@eecs.umich.edu        # Set up the shared partially linked objects.
10606143Snate@binkert.org        file_name = new_env.subst("${SHOBJPREFIX}lib${SHOBJSUFFIX}.partial")
10616143Snate@binkert.org        target = File(joinpath(group, file_name))
106211985Sgabeblack@google.com        partial = env.PartialShared(target=target, source=group_shared)
10636143Snate@binkert.org        shared_objs.extend(partial)
10646143Snate@binkert.org
10654007Ssaidi@eecs.umich.edu    static_date = date_source.static(new_env)
10664596Sbinkertn@umich.edu    new_env.Depends(static_date, static_objs)
10674007Ssaidi@eecs.umich.edu    static_objs.extend(static_date)
10684596Sbinkertn@umich.edu
10697756SAli.Saidi@ARM.com    shared_date = date_source.shared(new_env)
10707816Ssteve.reinhardt@amd.com    new_env.Depends(shared_date, shared_objs)
10718334Snate@binkert.org    shared_objs.extend(shared_date)
10728334Snate@binkert.org
10738334Snate@binkert.org    # First make a library of everything but main() so other programs can
10748334Snate@binkert.org    # link against m5.
10755601Snate@binkert.org    static_lib = new_env.StaticLibrary(libname, static_objs)
107611993Sgabeblack@google.com    shared_lib = new_env.SharedLibrary(libname, shared_objs)
107711993Sgabeblack@google.com
107811993Sgabeblack@google.com    # Now link a stub with main() and the static library.
107912223Sgabeblack@google.com    main_objs = [ s.static(new_env) for s in Source.all.with_tag('main') ]
108011993Sgabeblack@google.com
10812655Sstever@eecs.umich.edu    for test in UnitTest.all:
10829225Sandreas.hansson@arm.com        test_sources = Source.all.with_tag(str(test.target))
10839225Sandreas.hansson@arm.com        test_objs = [ s.static(new_env) for s in test_sources ]
10849226Sandreas.hansson@arm.com        if test.main:
10859226Sandreas.hansson@arm.com            test_objs += main_objs
10869225Sandreas.hansson@arm.com        path = 'unittest/%s.%s' % (test.target, label)
10879226Sandreas.hansson@arm.com        new_env.Program(path, test_objs + static_objs)
10889226Sandreas.hansson@arm.com
10899226Sandreas.hansson@arm.com    gtest_env = new_env.Clone()
10909226Sandreas.hansson@arm.com    gtest_env.Append(LIBS=gtest_env['GTEST_LIBS'])
10919226Sandreas.hansson@arm.com    gtest_env.Append(CPPFLAGS=gtest_env['GTEST_CPPFLAGS'])
10929226Sandreas.hansson@arm.com    gtestlib_sources = Source.all.with_tag('gtest lib')
10939225Sandreas.hansson@arm.com    gtests = []
10949227Sandreas.hansson@arm.com    for test in GTest.all:
10959227Sandreas.hansson@arm.com        test_sources = test.sources
10969227Sandreas.hansson@arm.com        if not test.skip_lib:
10979227Sandreas.hansson@arm.com            test_sources += gtestlib_sources
10988946Sandreas.hansson@arm.com        for f in test.filters:
10993918Ssaidi@eecs.umich.edu            test_sources += Source.all.apply_filter(f)
11009225Sandreas.hansson@arm.com        test_objs = [ s.static(gtest_env) for s in test_sources ]
11013918Ssaidi@eecs.umich.edu        gtests.append(gtest_env.Program(
11029225Sandreas.hansson@arm.com            test.dir.File('%s.%s' % (test.target, label)), test_objs))
11039225Sandreas.hansson@arm.com
11049227Sandreas.hansson@arm.com    gtest_target = Dir(new_env['BUILDDIR']).File('unittests.%s' % label)
11059227Sandreas.hansson@arm.com    AlwaysBuild(Command(gtest_target, gtests, gtests))
11069227Sandreas.hansson@arm.com
11079226Sandreas.hansson@arm.com    progname = exename
11089225Sandreas.hansson@arm.com    if strip:
11099227Sandreas.hansson@arm.com        progname += '.unstripped'
11109227Sandreas.hansson@arm.com
11119227Sandreas.hansson@arm.com    targets = new_env.Program(progname, main_objs + static_objs)
11129227Sandreas.hansson@arm.com
11138946Sandreas.hansson@arm.com    if strip:
11149225Sandreas.hansson@arm.com        if sys.platform == 'sunos5':
11159226Sandreas.hansson@arm.com            cmd = 'cp $SOURCE $TARGET; strip $TARGET'
11169226Sandreas.hansson@arm.com        else:
11179226Sandreas.hansson@arm.com            cmd = 'strip $SOURCE -o $TARGET'
11183515Ssaidi@eecs.umich.edu        targets = new_env.Command(exename, progname,
11193918Ssaidi@eecs.umich.edu                    MakeAction(cmd, Transform("STRIP")))
11204762Snate@binkert.org
11213515Ssaidi@eecs.umich.edu    new_env.Command(secondary_exename, exename,
11228881Smarc.orr@gmail.com            MakeAction('ln $SOURCE $TARGET', Transform("HARDLINK")))
11238881Smarc.orr@gmail.com
11248881Smarc.orr@gmail.com    new_env.M5Binary = targets[0]
11258881Smarc.orr@gmail.com
11268881Smarc.orr@gmail.com    # Set up regression tests.
11279226Sandreas.hansson@arm.com    SConscript(os.path.join(env.root.abspath, 'tests', 'SConscript'),
11289226Sandreas.hansson@arm.com               variant_dir=Dir('tests').Dir(new_env.Label),
11299226Sandreas.hansson@arm.com               exports={ 'env' : new_env }, duplicate=False)
11308881Smarc.orr@gmail.com
11318881Smarc.orr@gmail.com# Start out with the compiler flags common to all compilers,
11328881Smarc.orr@gmail.com# i.e. they all use -g for opt and -g -pg for prof
11338881Smarc.orr@gmail.comccflags = {'debug' : [], 'opt' : ['-g'], 'fast' : [], 'prof' : ['-g', '-pg'],
11348881Smarc.orr@gmail.com           'perf' : ['-g']}
11358881Smarc.orr@gmail.com
11368881Smarc.orr@gmail.com# Start out with the linker flags common to all linkers, i.e. -pg for
11378881Smarc.orr@gmail.com# prof, and -lprofiler for perf. The -lprofile flag is surrounded by
11388881Smarc.orr@gmail.com# no-as-needed and as-needed as the binutils linker is too clever and
11398881Smarc.orr@gmail.com# simply doesn't link to the library otherwise.
11408881Smarc.orr@gmail.comldflags = {'debug' : [], 'opt' : [], 'fast' : [], 'prof' : ['-pg'],
11418881Smarc.orr@gmail.com           'perf' : ['-Wl,--no-as-needed', '-lprofiler', '-Wl,--as-needed']}
11428881Smarc.orr@gmail.com
11438881Smarc.orr@gmail.com# For Link Time Optimization, the optimisation flags used to compile
11448881Smarc.orr@gmail.com# individual files are decoupled from those used at link time
11458881Smarc.orr@gmail.com# (i.e. you can compile with -O3 and perform LTO with -O0), so we need
114612222Sgabeblack@google.com# to also update the linker flags based on the target.
114712222Sgabeblack@google.comif env['GCC']:
114812222Sgabeblack@google.com    if sys.platform == 'sunos5':
114912222Sgabeblack@google.com        ccflags['debug'] += ['-gstabs+']
115012222Sgabeblack@google.com    else:
115112222Sgabeblack@google.com        ccflags['debug'] += ['-ggdb3']
1152955SN/A    ldflags['debug'] += ['-O0']
115312222Sgabeblack@google.com    # opt, fast, prof and perf all share the same cc flags, also add
115412222Sgabeblack@google.com    # the optimization to the ldflags as LTO defers the optimization
115512222Sgabeblack@google.com    # to link time
115612222Sgabeblack@google.com    for target in ['opt', 'fast', 'prof', 'perf']:
115712222Sgabeblack@google.com        ccflags[target] += ['-O3']
115812222Sgabeblack@google.com        ldflags[target] += ['-O3']
1159955SN/A
116012222Sgabeblack@google.com    ccflags['fast'] += env['LTO_CCFLAGS']
116112222Sgabeblack@google.com    ldflags['fast'] += env['LTO_LDFLAGS']
116212222Sgabeblack@google.comelif env['CLANG']:
116312222Sgabeblack@google.com    ccflags['debug'] += ['-g', '-O0']
116412222Sgabeblack@google.com    # opt, fast, prof and perf all share the same cc flags
116512222Sgabeblack@google.com    for target in ['opt', 'fast', 'prof', 'perf']:
116612222Sgabeblack@google.com        ccflags[target] += ['-O3']
116712222Sgabeblack@google.comelse:
116812222Sgabeblack@google.com    print 'Unknown compiler, please fix compiler options'
116912222Sgabeblack@google.com    Exit(1)
11701869SN/A
117112222Sgabeblack@google.com
117212222Sgabeblack@google.com# To speed things up, we only instantiate the build environments we
117312222Sgabeblack@google.com# need.  We try to identify the needed environment for each target; if
117412222Sgabeblack@google.com# we can't, we fall back on instantiating all the environments just to
117512222Sgabeblack@google.com# be safe.
117612222Sgabeblack@google.comtarget_types = ['debug', 'opt', 'fast', 'prof', 'perf']
11779226Sandreas.hansson@arm.comobj2target = {'do': 'debug', 'o': 'opt', 'fo': 'fast', 'po': 'prof',
117812222Sgabeblack@google.com              'gpo' : 'perf'}
117912222Sgabeblack@google.com
118012222Sgabeblack@google.comdef identifyTarget(t):
118112222Sgabeblack@google.com    ext = t.split('.')[-1]
118212222Sgabeblack@google.com    if ext in target_types:
118312222Sgabeblack@google.com        return ext
1184    if obj2target.has_key(ext):
1185        return obj2target[ext]
1186    match = re.search(r'/tests/([^/]+)/', t)
1187    if match and match.group(1) in target_types:
1188        return match.group(1)
1189    return 'all'
1190
1191needed_envs = [identifyTarget(target) for target in BUILD_TARGETS]
1192if 'all' in needed_envs:
1193    needed_envs += target_types
1194
1195# Debug binary
1196if 'debug' in needed_envs:
1197    makeEnv(env, 'debug', '.do',
1198            CCFLAGS = Split(ccflags['debug']),
1199            CPPDEFINES = ['DEBUG', 'TRACING_ON=1'],
1200            LINKFLAGS = Split(ldflags['debug']))
1201
1202# Optimized binary
1203if 'opt' in needed_envs:
1204    makeEnv(env, 'opt', '.o',
1205            CCFLAGS = Split(ccflags['opt']),
1206            CPPDEFINES = ['TRACING_ON=1'],
1207            LINKFLAGS = Split(ldflags['opt']))
1208
1209# "Fast" binary
1210if 'fast' in needed_envs:
1211    disable_partial = \
1212            env.get('BROKEN_INCREMENTAL_LTO', False) and \
1213            GetOption('force_lto')
1214    makeEnv(env, 'fast', '.fo', strip = True,
1215            CCFLAGS = Split(ccflags['fast']),
1216            CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1217            LINKFLAGS = Split(ldflags['fast']),
1218            disable_partial=disable_partial)
1219
1220# Profiled binary using gprof
1221if 'prof' in needed_envs:
1222    makeEnv(env, 'prof', '.po',
1223            CCFLAGS = Split(ccflags['prof']),
1224            CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1225            LINKFLAGS = Split(ldflags['prof']))
1226
1227# Profiled binary using google-pprof
1228if 'perf' in needed_envs:
1229    makeEnv(env, 'perf', '.gpo',
1230            CCFLAGS = Split(ccflags['perf']),
1231            CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1232            LINKFLAGS = Split(ldflags['perf']))
1233