SConscript revision 10458
12929Sktlim@umich.edu# -*- mode:python -*-
22929Sktlim@umich.edu
32932Sktlim@umich.edu# Copyright (c) 2004-2005 The Regents of The University of Michigan
42929Sktlim@umich.edu# All rights reserved.
52929Sktlim@umich.edu#
62929Sktlim@umich.edu# Redistribution and use in source and binary forms, with or without
72929Sktlim@umich.edu# modification, are permitted provided that the following conditions are
82929Sktlim@umich.edu# met: redistributions of source code must retain the above copyright
92929Sktlim@umich.edu# notice, this list of conditions and the following disclaimer;
102929Sktlim@umich.edu# redistributions in binary form must reproduce the above copyright
112929Sktlim@umich.edu# notice, this list of conditions and the following disclaimer in the
122929Sktlim@umich.edu# documentation and/or other materials provided with the distribution;
132929Sktlim@umich.edu# neither the name of the copyright holders nor the names of its
142929Sktlim@umich.edu# contributors may be used to endorse or promote products derived from
152929Sktlim@umich.edu# this software without specific prior written permission.
162929Sktlim@umich.edu#
172929Sktlim@umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
182929Sktlim@umich.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
192929Sktlim@umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
202929Sktlim@umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
212929Sktlim@umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
222929Sktlim@umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
232929Sktlim@umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
242929Sktlim@umich.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
252929Sktlim@umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
262929Sktlim@umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
272929Sktlim@umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
282932Sktlim@umich.edu#
292932Sktlim@umich.edu# Authors: Nathan Binkert
302932Sktlim@umich.edu
312929Sktlim@umich.eduimport array
326007Ssteve.reinhardt@amd.comimport bisect
337735SAli.Saidi@ARM.comimport imp
342929Sktlim@umich.eduimport marshal
352929Sktlim@umich.eduimport os
362929Sktlim@umich.eduimport re
372929Sktlim@umich.eduimport sys
382929Sktlim@umich.eduimport zlib
392929Sktlim@umich.edu
402929Sktlim@umich.edufrom os.path import basename, dirname, exists, isdir, isfile, join as joinpath
412929Sktlim@umich.edu
422929Sktlim@umich.eduimport SCons
432929Sktlim@umich.edu
442929Sktlim@umich.edu# This file defines how to build a particular configuration of gem5
452929Sktlim@umich.edu# based on variable settings in the 'env' build environment.
462929Sktlim@umich.edu
476007Ssteve.reinhardt@amd.comImport('*')
486007Ssteve.reinhardt@amd.com
496007Ssteve.reinhardt@amd.com# Children need to see the environment
506007Ssteve.reinhardt@amd.comExport('env')
516007Ssteve.reinhardt@amd.com
526007Ssteve.reinhardt@amd.combuild_env = [(opt, env[opt]) for opt in export_vars]
536007Ssteve.reinhardt@amd.com
546007Ssteve.reinhardt@amd.comfrom m5.util import code_formatter, compareVersions
556007Ssteve.reinhardt@amd.com
566007Ssteve.reinhardt@amd.com########################################################################
576007Ssteve.reinhardt@amd.com# Code for adding source files of various types
586007Ssteve.reinhardt@amd.com#
596007Ssteve.reinhardt@amd.com# When specifying a source file of some type, a set of guards can be
606007Ssteve.reinhardt@amd.com# specified for that file.  When get() is used to find the files, if
616007Ssteve.reinhardt@amd.com# get specifies a set of filters, only files that match those filters
626007Ssteve.reinhardt@amd.com# will be accepted (unspecified filters on files are assumed to be
636007Ssteve.reinhardt@amd.com# false).  Current filters are:
646007Ssteve.reinhardt@amd.com#     main -- specifies the gem5 main() function
656007Ssteve.reinhardt@amd.com#     skip_lib -- do not put this file into the gem5 library
666007Ssteve.reinhardt@amd.com#     skip_no_python -- do not put this file into a no_python library
676007Ssteve.reinhardt@amd.com#       as it embeds compiled Python
686007Ssteve.reinhardt@amd.com#     <unittest> -- unit tests use filters based on the unit test name
696007Ssteve.reinhardt@amd.com#
706007Ssteve.reinhardt@amd.com# A parent can now be specified for a source file and default filter
716007Ssteve.reinhardt@amd.com# values will be retrieved recursively from parents (children override
726007Ssteve.reinhardt@amd.com# parents).
736007Ssteve.reinhardt@amd.com#
746007Ssteve.reinhardt@amd.comclass SourceMeta(type):
756007Ssteve.reinhardt@amd.com    '''Meta class for source files that keeps track of all files of a
762929Sktlim@umich.edu    particular type and has a get function for finding all functions
772929Sktlim@umich.edu    of a certain type that match a set of guards'''
782929Sktlim@umich.edu    def __init__(cls, name, bases, dict):
796007Ssteve.reinhardt@amd.com        super(SourceMeta, cls).__init__(name, bases, dict)
806007Ssteve.reinhardt@amd.com        cls.all = []
816007Ssteve.reinhardt@amd.com        
826007Ssteve.reinhardt@amd.com    def get(cls, **guards):
836007Ssteve.reinhardt@amd.com        '''Find all files that match the specified guards.  If a source
846007Ssteve.reinhardt@amd.com        file does not specify a flag, the default is False'''
852929Sktlim@umich.edu        for src in cls.all:
862929Sktlim@umich.edu            for flag,value in guards.iteritems():
872929Sktlim@umich.edu                # if the flag is found and has a different value, skip
882929Sktlim@umich.edu                # this file
892929Sktlim@umich.edu                if src.all_guards.get(flag, False) != value:
906011Ssteve.reinhardt@amd.com                    break
916007Ssteve.reinhardt@amd.com            else:
926007Ssteve.reinhardt@amd.com                yield src
936007Ssteve.reinhardt@amd.com
946007Ssteve.reinhardt@amd.comclass SourceFile(object):
956007Ssteve.reinhardt@amd.com    '''Base object that encapsulates the notion of a source file.
966007Ssteve.reinhardt@amd.com    This includes, the source node, target node, various manipulations
976007Ssteve.reinhardt@amd.com    of those.  A source file also specifies a set of guards which
986007Ssteve.reinhardt@amd.com    describing which builds the source file applies to.  A parent can
996007Ssteve.reinhardt@amd.com    also be specified to get default guards from'''
1006007Ssteve.reinhardt@amd.com    __metaclass__ = SourceMeta
1016007Ssteve.reinhardt@amd.com    def __init__(self, source, parent=None, **guards):
1026007Ssteve.reinhardt@amd.com        self.guards = guards
1036007Ssteve.reinhardt@amd.com        self.parent = parent
1046007Ssteve.reinhardt@amd.com
1057735SAli.Saidi@ARM.com        tnode = source
1066011Ssteve.reinhardt@amd.com        if not isinstance(source, SCons.Node.FS.File):
1076007Ssteve.reinhardt@amd.com            tnode = File(source)
1086007Ssteve.reinhardt@amd.com
1096007Ssteve.reinhardt@amd.com        self.tnode = tnode
1106007Ssteve.reinhardt@amd.com        self.snode = tnode.srcnode()
1117735SAli.Saidi@ARM.com
1127735SAli.Saidi@ARM.com        for base in type(self).__mro__:
1137735SAli.Saidi@ARM.com            if issubclass(base, SourceFile):
1147735SAli.Saidi@ARM.com                base.all.append(self)
1157735SAli.Saidi@ARM.com
1167735SAli.Saidi@ARM.com    @property
1177735SAli.Saidi@ARM.com    def filename(self):
1187735SAli.Saidi@ARM.com        return str(self.tnode)
1197735SAli.Saidi@ARM.com
1207735SAli.Saidi@ARM.com    @property
1217735SAli.Saidi@ARM.com    def dirname(self):
1227735SAli.Saidi@ARM.com        return dirname(self.filename)
1237735SAli.Saidi@ARM.com
1247735SAli.Saidi@ARM.com    @property
1256007Ssteve.reinhardt@amd.com    def basename(self):
1267685Ssteve.reinhardt@amd.com        return basename(self.filename)
1276007Ssteve.reinhardt@amd.com
1286011Ssteve.reinhardt@amd.com    @property
1296007Ssteve.reinhardt@amd.com    def extname(self):
1306007Ssteve.reinhardt@amd.com        index = self.basename.rfind('.')
1316007Ssteve.reinhardt@amd.com        if index <= 0:
1326007Ssteve.reinhardt@amd.com            # dot files aren't extensions
1336007Ssteve.reinhardt@amd.com            return self.basename, None
1346007Ssteve.reinhardt@amd.com
1356011Ssteve.reinhardt@amd.com        return self.basename[:index], self.basename[index+1:]
1366007Ssteve.reinhardt@amd.com
1376007Ssteve.reinhardt@amd.com    @property
1386007Ssteve.reinhardt@amd.com    def all_guards(self):
1396007Ssteve.reinhardt@amd.com        '''find all guards for this object getting default values
1406007Ssteve.reinhardt@amd.com        recursively from its parents'''
1416008Ssteve.reinhardt@amd.com        guards = {}
1426007Ssteve.reinhardt@amd.com        if self.parent:
1436008Ssteve.reinhardt@amd.com            guards.update(self.parent.guards)
1446008Ssteve.reinhardt@amd.com        guards.update(self.guards)
1456008Ssteve.reinhardt@amd.com        return guards
1466008Ssteve.reinhardt@amd.com
1476008Ssteve.reinhardt@amd.com    def __lt__(self, other): return self.filename < other.filename
1486008Ssteve.reinhardt@amd.com    def __le__(self, other): return self.filename <= other.filename
1496008Ssteve.reinhardt@amd.com    def __gt__(self, other): return self.filename > other.filename
1506007Ssteve.reinhardt@amd.com    def __ge__(self, other): return self.filename >= other.filename
1516007Ssteve.reinhardt@amd.com    def __eq__(self, other): return self.filename == other.filename
1526007Ssteve.reinhardt@amd.com    def __ne__(self, other): return self.filename != other.filename
1536007Ssteve.reinhardt@amd.com
1546007Ssteve.reinhardt@amd.com    @staticmethod
1552929Sktlim@umich.edu    def done():
1562929Sktlim@umich.edu        def disabled(cls, name, *ignored):
1572929Sktlim@umich.edu            raise RuntimeError("Additional SourceFile '%s'" % name,\
1582929Sktlim@umich.edu                  "declared, but targets deps are already fixed.")
1596007Ssteve.reinhardt@amd.com        SourceFile.__init__ = disabled
1606007Ssteve.reinhardt@amd.com
1612929Sktlim@umich.edu
1622929Sktlim@umich.educlass Source(SourceFile):
1632929Sktlim@umich.edu    '''Add a c/c++ source file to the build'''
1642929Sktlim@umich.edu    def __init__(self, source, Werror=True, swig=False, **guards):
1656007Ssteve.reinhardt@amd.com        '''specify the source file, and any guards'''
1666007Ssteve.reinhardt@amd.com        super(Source, self).__init__(source, **guards)
1672929Sktlim@umich.edu
1682929Sktlim@umich.edu        self.Werror = Werror
1696007Ssteve.reinhardt@amd.com        self.swig = swig
1702929Sktlim@umich.edu
1712929Sktlim@umich.educlass PySource(SourceFile):
1722929Sktlim@umich.edu    '''Add a python source file to the named package'''
1732929Sktlim@umich.edu    invalid_sym_char = re.compile('[^A-z0-9_]')
1742929Sktlim@umich.edu    modules = {}
1752929Sktlim@umich.edu    tnodes = {}
1762929Sktlim@umich.edu    symnames = {}
1774937Sstever@gmail.com
1784937Sstever@gmail.com    def __init__(self, package, source, **guards):
1794937Sstever@gmail.com        '''specify the python package, the source file, and any guards'''
1804937Sstever@gmail.com        super(PySource, self).__init__(source, **guards)
1818120Sgblack@eecs.umich.edu
1824937Sstever@gmail.com        modname,ext = self.extname
1834937Sstever@gmail.com        assert ext == 'py'
1844937Sstever@gmail.com
1854937Sstever@gmail.com        if package:
1865773Snate@binkert.org            path = package.split('.')
1874937Sstever@gmail.com        else:
1884937Sstever@gmail.com            path = []
1894937Sstever@gmail.com
1902929Sktlim@umich.edu        modpath = path[:]
1912929Sktlim@umich.edu        if modname != '__init__':
1922929Sktlim@umich.edu            modpath += [ modname ]
1935773Snate@binkert.org        modpath = '.'.join(modpath)
1942929Sktlim@umich.edu
1952929Sktlim@umich.edu        arcpath = path + [ self.basename ]
1962929Sktlim@umich.edu        abspath = self.snode.abspath
1972929Sktlim@umich.edu        if not exists(abspath):
1982929Sktlim@umich.edu            abspath = self.tnode.abspath
1992929Sktlim@umich.edu
2004937Sstever@gmail.com        self.package = package
2014937Sstever@gmail.com        self.modname = modname
2024937Sstever@gmail.com        self.modpath = modpath
2034937Sstever@gmail.com        self.arcname = joinpath(*arcpath)
2044937Sstever@gmail.com        self.abspath = abspath
2054937Sstever@gmail.com        self.compiled = File(self.filename + 'c')
2064937Sstever@gmail.com        self.cpp = File(self.filename + '.cc')
2074937Sstever@gmail.com        self.symname = PySource.invalid_sym_char.sub('_', modpath)
2084937Sstever@gmail.com
2094937Sstever@gmail.com        PySource.modules[modpath] = self
2104937Sstever@gmail.com        PySource.tnodes[self.tnode] = self
2114937Sstever@gmail.com        PySource.symnames[self.symname] = self
2124937Sstever@gmail.com
2134937Sstever@gmail.comclass SimObject(PySource):
2144937Sstever@gmail.com    '''Add a SimObject python file as a python source object and add
2152929Sktlim@umich.edu    it to a list of sim object modules'''
2162929Sktlim@umich.edu
2172929Sktlim@umich.edu    fixed = False
2182929Sktlim@umich.edu    modnames = []
2192929Sktlim@umich.edu
2202929Sktlim@umich.edu    def __init__(self, source, **guards):
2212929Sktlim@umich.edu        '''Specify the source file and any guards (automatically in
2226011Ssteve.reinhardt@amd.com        the m5.objects package)'''
2232929Sktlim@umich.edu        super(SimObject, self).__init__('m5.objects', source, **guards)
2242929Sktlim@umich.edu        if self.fixed:
2252929Sktlim@umich.edu            raise AttributeError, "Too late to call SimObject now."
2262929Sktlim@umich.edu
2272929Sktlim@umich.edu        bisect.insort_right(SimObject.modnames, self.modname)
2282929Sktlim@umich.edu
2292929Sktlim@umich.educlass SwigSource(SourceFile):
2302929Sktlim@umich.edu    '''Add a swig file to build'''
2312997Sstever@eecs.umich.edu
2322997Sstever@eecs.umich.edu    def __init__(self, package, source, **guards):
2332929Sktlim@umich.edu        '''Specify the python package, the source file, and any guards'''
2342997Sstever@eecs.umich.edu        super(SwigSource, self).__init__(source, skip_no_python=True, **guards)
2352997Sstever@eecs.umich.edu
2362929Sktlim@umich.edu        modname,ext = self.extname
2372997Sstever@eecs.umich.edu        assert ext == 'i'
2382997Sstever@eecs.umich.edu
2392997Sstever@eecs.umich.edu        self.module = modname
2402929Sktlim@umich.edu        cc_file = joinpath(self.dirname, modname + '_wrap.cc')
2412997Sstever@eecs.umich.edu        py_file = joinpath(self.dirname, modname + '.py')
2422997Sstever@eecs.umich.edu
2432997Sstever@eecs.umich.edu        self.cc_source = Source(cc_file, swig=True, parent=self, **guards)
2442997Sstever@eecs.umich.edu        self.py_source = PySource(package, py_file, parent=self, **guards)
2455773Snate@binkert.org
2465773Snate@binkert.orgclass ProtoBuf(SourceFile):
2472997Sstever@eecs.umich.edu    '''Add a Protocol Buffer to build'''
2482997Sstever@eecs.umich.edu
2496007Ssteve.reinhardt@amd.com    def __init__(self, source, **guards):
2506007Ssteve.reinhardt@amd.com        '''Specify the source file, and any guards'''
2512997Sstever@eecs.umich.edu        super(ProtoBuf, self).__init__(source, **guards)
2522929Sktlim@umich.edu
2532997Sstever@eecs.umich.edu        # Get the file name and the extension
2548120Sgblack@eecs.umich.edu        modname,ext = self.extname
2552997Sstever@eecs.umich.edu        assert ext == 'proto'
2562997Sstever@eecs.umich.edu
2572997Sstever@eecs.umich.edu        # Currently, we stick to generating the C++ headers, so we
2582997Sstever@eecs.umich.edu        # only need to track the source and header.
2592997Sstever@eecs.umich.edu        self.cc_file = File(modname + '.pb.cc')
2602929Sktlim@umich.edu        self.hh_file = File(modname + '.pb.h')
2612997Sstever@eecs.umich.edu
2622929Sktlim@umich.educlass UnitTest(object):
2632929Sktlim@umich.edu    '''Create a UnitTest'''
2643005Sstever@eecs.umich.edu
2653005Sstever@eecs.umich.edu    all = []
2663005Sstever@eecs.umich.edu    def __init__(self, target, *sources, **kwargs):
2673005Sstever@eecs.umich.edu        '''Specify the target name and any sources.  Sources that are
2686025Snate@binkert.org        not SourceFiles are evalued with Source().  All files are
2696025Snate@binkert.org        guarded with a guard of the same name as the UnitTest
2706025Snate@binkert.org        target.'''
2716025Snate@binkert.org
2726025Snate@binkert.org        srcs = []
2736025Snate@binkert.org        for src in sources:
2744130Ssaidi@eecs.umich.edu            if not isinstance(src, SourceFile):
2754130Ssaidi@eecs.umich.edu                src = Source(src, skip_lib=True)
2764130Ssaidi@eecs.umich.edu            src.guards[target] = True
2777735SAli.Saidi@ARM.com            srcs.append(src)
2787735SAli.Saidi@ARM.com
2797735SAli.Saidi@ARM.com        self.sources = srcs
2807926Sgblack@eecs.umich.edu        self.target = target
2817926Sgblack@eecs.umich.edu        self.main = kwargs.get('main', False)
2827926Sgblack@eecs.umich.edu        UnitTest.all.append(self)
2833691Shsul@eecs.umich.edu
2843005Sstever@eecs.umich.edu# Children should have access
2855721Shsul@eecs.umich.eduExport('Source')
2866194Sksewell@umich.eduExport('PySource')
2876928SBrad.Beckmann@amd.comExport('SimObject')
2883005Sstever@eecs.umich.eduExport('SwigSource')
2896168Snate@binkert.orgExport('ProtoBuf')
2906928SBrad.Beckmann@amd.comExport('UnitTest')
2916928SBrad.Beckmann@amd.com
2926928SBrad.Beckmann@amd.com########################################################################
2936928SBrad.Beckmann@amd.com#
2946928SBrad.Beckmann@amd.com# Debug Flags
2956928SBrad.Beckmann@amd.com#
2966928SBrad.Beckmann@amd.comdebug_flags = {}
2976928SBrad.Beckmann@amd.comdef DebugFlag(name, desc=None):
2986928SBrad.Beckmann@amd.com    if name in debug_flags:
2996928SBrad.Beckmann@amd.com        raise AttributeError, "Flag %s already specified" % name
3006928SBrad.Beckmann@amd.com    debug_flags[name] = (name, (), desc)
3016928SBrad.Beckmann@amd.com
3026928SBrad.Beckmann@amd.comdef CompoundFlag(name, flags, desc=None):
3036928SBrad.Beckmann@amd.com    if name in debug_flags:
3046166Ssteve.reinhardt@amd.com        raise AttributeError, "Flag %s already specified" % name
3052929Sktlim@umich.edu
3062929Sktlim@umich.edu    compound = tuple(flags)
3073005Sstever@eecs.umich.edu    debug_flags[name] = (name, compound, desc)
3082997Sstever@eecs.umich.edu
3092997Sstever@eecs.umich.eduExport('DebugFlag')
3106293Ssteve.reinhardt@amd.comExport('CompoundFlag')
3116293Ssteve.reinhardt@amd.com
3122929Sktlim@umich.edu########################################################################
313#
314# Set some compiler variables
315#
316
317# Include file paths are rooted in this directory.  SCons will
318# automatically expand '.' to refer to both the source directory and
319# the corresponding build directory to pick up generated include
320# files.
321env.Append(CPPPATH=Dir('.'))
322
323for extra_dir in extras_dir_list:
324    env.Append(CPPPATH=Dir(extra_dir))
325
326# Workaround for bug in SCons version > 0.97d20071212
327# Scons bug id: 2006 gem5 Bug id: 308
328for root, dirs, files in os.walk(base_dir, topdown=True):
329    Dir(root[len(base_dir) + 1:])
330
331########################################################################
332#
333# Walk the tree and execute all SConscripts in subdirectories
334#
335
336here = Dir('.').srcnode().abspath
337for root, dirs, files in os.walk(base_dir, topdown=True):
338    if root == here:
339        # we don't want to recurse back into this SConscript
340        continue
341
342    if 'SConscript' in files:
343        build_dir = joinpath(env['BUILDDIR'], root[len(base_dir) + 1:])
344        SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
345
346for extra_dir in extras_dir_list:
347    prefix_len = len(dirname(extra_dir)) + 1
348
349    # Also add the corresponding build directory to pick up generated
350    # include files.
351    env.Append(CPPPATH=Dir(joinpath(env['BUILDDIR'], extra_dir[prefix_len:])))
352
353    for root, dirs, files in os.walk(extra_dir, topdown=True):
354        # if build lives in the extras directory, don't walk down it
355        if 'build' in dirs:
356            dirs.remove('build')
357
358        if 'SConscript' in files:
359            build_dir = joinpath(env['BUILDDIR'], root[prefix_len:])
360            SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
361
362for opt in export_vars:
363    env.ConfigFile(opt)
364
365def makeTheISA(source, target, env):
366    isas = [ src.get_contents() for src in source ]
367    target_isa = env['TARGET_ISA']
368    def define(isa):
369        return isa.upper() + '_ISA'
370    
371    def namespace(isa):
372        return isa[0].upper() + isa[1:].lower() + 'ISA' 
373
374
375    code = code_formatter()
376    code('''\
377#ifndef __CONFIG_THE_ISA_HH__
378#define __CONFIG_THE_ISA_HH__
379
380''')
381
382    for i,isa in enumerate(isas):
383        code('#define $0 $1', define(isa), i + 1)
384
385    code('''
386
387#define THE_ISA ${{define(target_isa)}}
388#define TheISA ${{namespace(target_isa)}}
389#define THE_ISA_STR "${{target_isa}}"
390
391#endif // __CONFIG_THE_ISA_HH__''')
392
393    code.write(str(target[0]))
394
395env.Command('config/the_isa.hh', map(Value, all_isa_list),
396            MakeAction(makeTheISA, Transform("CFG ISA", 0)))
397
398########################################################################
399#
400# Prevent any SimObjects from being added after this point, they
401# should all have been added in the SConscripts above
402#
403SimObject.fixed = True
404
405class DictImporter(object):
406    '''This importer takes a dictionary of arbitrary module names that
407    map to arbitrary filenames.'''
408    def __init__(self, modules):
409        self.modules = modules
410        self.installed = set()
411
412    def __del__(self):
413        self.unload()
414
415    def unload(self):
416        import sys
417        for module in self.installed:
418            del sys.modules[module]
419        self.installed = set()
420
421    def find_module(self, fullname, path):
422        if fullname == 'm5.defines':
423            return self
424
425        if fullname == 'm5.objects':
426            return self
427
428        if fullname.startswith('m5.internal'):
429            return None
430
431        source = self.modules.get(fullname, None)
432        if source is not None and fullname.startswith('m5.objects'):
433            return self
434
435        return None
436
437    def load_module(self, fullname):
438        mod = imp.new_module(fullname)
439        sys.modules[fullname] = mod
440        self.installed.add(fullname)
441
442        mod.__loader__ = self
443        if fullname == 'm5.objects':
444            mod.__path__ = fullname.split('.')
445            return mod
446
447        if fullname == 'm5.defines':
448            mod.__dict__['buildEnv'] = m5.util.SmartDict(build_env)
449            return mod
450
451        source = self.modules[fullname]
452        if source.modname == '__init__':
453            mod.__path__ = source.modpath
454        mod.__file__ = source.abspath
455
456        exec file(source.abspath, 'r') in mod.__dict__
457
458        return mod
459
460import m5.SimObject
461import m5.params
462from m5.util import code_formatter
463
464m5.SimObject.clear()
465m5.params.clear()
466
467# install the python importer so we can grab stuff from the source
468# tree itself.  We can't have SimObjects added after this point or
469# else we won't know about them for the rest of the stuff.
470importer = DictImporter(PySource.modules)
471sys.meta_path[0:0] = [ importer ]
472
473# import all sim objects so we can populate the all_objects list
474# make sure that we're working with a list, then let's sort it
475for modname in SimObject.modnames:
476    exec('from m5.objects import %s' % modname)
477
478# we need to unload all of the currently imported modules so that they
479# will be re-imported the next time the sconscript is run
480importer.unload()
481sys.meta_path.remove(importer)
482
483sim_objects = m5.SimObject.allClasses
484all_enums = m5.params.allEnums
485
486if m5.SimObject.noCxxHeader:
487    print >> sys.stderr, \
488        "warning: At least one SimObject lacks a header specification. " \
489        "This can cause unexpected results in the generated SWIG " \
490        "wrappers."
491
492# Find param types that need to be explicitly wrapped with swig.
493# These will be recognized because the ParamDesc will have a
494# swig_decl() method.  Most param types are based on types that don't
495# need this, either because they're based on native types (like Int)
496# or because they're SimObjects (which get swigged independently).
497# For now the only things handled here are VectorParam types.
498params_to_swig = {}
499for name,obj in sorted(sim_objects.iteritems()):
500    for param in obj._params.local.values():
501        # load the ptype attribute now because it depends on the
502        # current version of SimObject.allClasses, but when scons
503        # actually uses the value, all versions of
504        # SimObject.allClasses will have been loaded
505        param.ptype
506
507        if not hasattr(param, 'swig_decl'):
508            continue
509        pname = param.ptype_str
510        if pname not in params_to_swig:
511            params_to_swig[pname] = param
512
513########################################################################
514#
515# calculate extra dependencies
516#
517module_depends = ["m5", "m5.SimObject", "m5.params"]
518depends = [ PySource.modules[dep].snode for dep in module_depends ]
519
520########################################################################
521#
522# Commands for the basic automatically generated python files
523#
524
525# Generate Python file containing a dict specifying the current
526# buildEnv flags.
527def makeDefinesPyFile(target, source, env):
528    build_env = source[0].get_contents()
529
530    code = code_formatter()
531    code("""
532import m5.internal
533import m5.util
534
535buildEnv = m5.util.SmartDict($build_env)
536
537compileDate = m5.internal.core.compileDate
538_globals = globals()
539for key,val in m5.internal.core.__dict__.iteritems():
540    if key.startswith('flag_'):
541        flag = key[5:]
542        _globals[flag] = val
543del _globals
544""")
545    code.write(target[0].abspath)
546
547defines_info = Value(build_env)
548# Generate a file with all of the compile options in it
549env.Command('python/m5/defines.py', defines_info,
550            MakeAction(makeDefinesPyFile, Transform("DEFINES", 0)))
551PySource('m5', 'python/m5/defines.py')
552
553# Generate python file containing info about the M5 source code
554def makeInfoPyFile(target, source, env):
555    code = code_formatter()
556    for src in source:
557        data = ''.join(file(src.srcnode().abspath, 'r').xreadlines())
558        code('$src = ${{repr(data)}}')
559    code.write(str(target[0]))
560
561# Generate a file that wraps the basic top level files
562env.Command('python/m5/info.py',
563            [ '#/COPYING', '#/LICENSE', '#/README', ],
564            MakeAction(makeInfoPyFile, Transform("INFO")))
565PySource('m5', 'python/m5/info.py')
566
567########################################################################
568#
569# Create all of the SimObject param headers and enum headers
570#
571
572def createSimObjectParamStruct(target, source, env):
573    assert len(target) == 1 and len(source) == 1
574
575    name = str(source[0].get_contents())
576    obj = sim_objects[name]
577
578    code = code_formatter()
579    obj.cxx_param_decl(code)
580    code.write(target[0].abspath)
581
582def createSimObjectCxxConfig(is_header):
583    def body(target, source, env):
584        assert len(target) == 1 and len(source) == 1
585
586        name = str(source[0].get_contents())
587        obj = sim_objects[name]
588
589        code = code_formatter()
590        obj.cxx_config_param_file(code, is_header)
591        code.write(target[0].abspath)
592    return body
593
594def createParamSwigWrapper(target, source, env):
595    assert len(target) == 1 and len(source) == 1
596
597    name = str(source[0].get_contents())
598    param = params_to_swig[name]
599
600    code = code_formatter()
601    param.swig_decl(code)
602    code.write(target[0].abspath)
603
604def createEnumStrings(target, source, env):
605    assert len(target) == 1 and len(source) == 1
606
607    name = str(source[0].get_contents())
608    obj = all_enums[name]
609
610    code = code_formatter()
611    obj.cxx_def(code)
612    code.write(target[0].abspath)
613
614def createEnumDecls(target, source, env):
615    assert len(target) == 1 and len(source) == 1
616
617    name = str(source[0].get_contents())
618    obj = all_enums[name]
619
620    code = code_formatter()
621    obj.cxx_decl(code)
622    code.write(target[0].abspath)
623
624def createEnumSwigWrapper(target, source, env):
625    assert len(target) == 1 and len(source) == 1
626
627    name = str(source[0].get_contents())
628    obj = all_enums[name]
629
630    code = code_formatter()
631    obj.swig_decl(code)
632    code.write(target[0].abspath)
633
634def createSimObjectSwigWrapper(target, source, env):
635    name = source[0].get_contents()
636    obj = sim_objects[name]
637
638    code = code_formatter()
639    obj.swig_decl(code)
640    code.write(target[0].abspath)
641
642# dummy target for generated code
643# we start out with all the Source files so they get copied to build/*/ also.
644SWIG = env.Dummy('swig', [s.tnode for s in Source.get()])
645
646# Generate all of the SimObject param C++ struct header files
647params_hh_files = []
648for name,simobj in sorted(sim_objects.iteritems()):
649    py_source = PySource.modules[simobj.__module__]
650    extra_deps = [ py_source.tnode ]
651
652    hh_file = File('params/%s.hh' % name)
653    params_hh_files.append(hh_file)
654    env.Command(hh_file, Value(name),
655                MakeAction(createSimObjectParamStruct, Transform("SO PARAM")))
656    env.Depends(hh_file, depends + extra_deps)
657    env.Depends(SWIG, hh_file)
658
659# C++ parameter description files
660if GetOption('with_cxx_config'):
661    for name,simobj in sorted(sim_objects.iteritems()):
662        py_source = PySource.modules[simobj.__module__]
663        extra_deps = [ py_source.tnode ]
664
665        cxx_config_hh_file = File('cxx_config/%s.hh' % name)
666        cxx_config_cc_file = File('cxx_config/%s.cc' % name)
667        env.Command(cxx_config_hh_file, Value(name),
668                    MakeAction(createSimObjectCxxConfig(True),
669                    Transform("CXXCPRHH")))
670        env.Command(cxx_config_cc_file, Value(name),
671                    MakeAction(createSimObjectCxxConfig(False),
672                    Transform("CXXCPRCC")))
673        env.Depends(cxx_config_hh_file, depends + extra_deps +
674                    [File('params/%s.hh' % name), File('sim/cxx_config.hh')])
675        env.Depends(cxx_config_cc_file, depends + extra_deps +
676                    [cxx_config_hh_file])
677        Source(cxx_config_cc_file)
678
679    cxx_config_init_cc_file = File('cxx_config/init.cc')
680
681    def createCxxConfigInitCC(target, source, env):
682        assert len(target) == 1 and len(source) == 1
683
684        code = code_formatter()
685
686        for name,simobj in sorted(sim_objects.iteritems()):
687            if not hasattr(simobj, 'abstract') or not simobj.abstract:
688                code('#include "cxx_config/${name}.hh"')
689        code()
690        code('void cxxConfigInit()')
691        code('{')
692        code.indent()
693        for name,simobj in sorted(sim_objects.iteritems()):
694            not_abstract = not hasattr(simobj, 'abstract') or \
695                not simobj.abstract
696            if not_abstract and 'type' in simobj.__dict__:
697                code('cxx_config_directory["${name}"] = '
698                     '${name}CxxConfigParams::makeDirectoryEntry();')
699        code.dedent()
700        code('}')
701        code.write(target[0].abspath)
702
703    py_source = PySource.modules[simobj.__module__]
704    extra_deps = [ py_source.tnode ]
705    env.Command(cxx_config_init_cc_file, Value(name),
706        MakeAction(createCxxConfigInitCC, Transform("CXXCINIT")))
707    cxx_param_hh_files = ["cxx_config/%s.hh" % simobj
708        for simobj in sorted(sim_objects.itervalues())
709        if not hasattr(simobj, 'abstract') or not simobj.abstract]
710    Depends(cxx_config_init_cc_file, cxx_param_hh_files +
711            [File('sim/cxx_config.hh')])
712    Source(cxx_config_init_cc_file)
713
714# Generate any needed param SWIG wrapper files
715params_i_files = []
716for name,param in params_to_swig.iteritems():
717    i_file = File('python/m5/internal/%s.i' % (param.swig_module_name()))
718    params_i_files.append(i_file)
719    env.Command(i_file, Value(name),
720                MakeAction(createParamSwigWrapper, Transform("SW PARAM")))
721    env.Depends(i_file, depends)
722    env.Depends(SWIG, i_file)
723    SwigSource('m5.internal', i_file)
724
725# Generate all enum header files
726for name,enum in sorted(all_enums.iteritems()):
727    py_source = PySource.modules[enum.__module__]
728    extra_deps = [ py_source.tnode ]
729
730    cc_file = File('enums/%s.cc' % name)
731    env.Command(cc_file, Value(name),
732                MakeAction(createEnumStrings, Transform("ENUM STR")))
733    env.Depends(cc_file, depends + extra_deps)
734    env.Depends(SWIG, cc_file)
735    Source(cc_file)
736
737    hh_file = File('enums/%s.hh' % name)
738    env.Command(hh_file, Value(name),
739                MakeAction(createEnumDecls, Transform("ENUMDECL")))
740    env.Depends(hh_file, depends + extra_deps)
741    env.Depends(SWIG, hh_file)
742
743    i_file = File('python/m5/internal/enum_%s.i' % name)
744    env.Command(i_file, Value(name),
745                MakeAction(createEnumSwigWrapper, Transform("ENUMSWIG")))
746    env.Depends(i_file, depends + extra_deps)
747    env.Depends(SWIG, i_file)
748    SwigSource('m5.internal', i_file)
749
750# Generate SimObject SWIG wrapper files
751for name,simobj in sim_objects.iteritems():
752    py_source = PySource.modules[simobj.__module__]
753    extra_deps = [ py_source.tnode ]
754
755    i_file = File('python/m5/internal/param_%s.i' % name)
756    env.Command(i_file, Value(name),
757                MakeAction(createSimObjectSwigWrapper, Transform("SO SWIG")))
758    env.Depends(i_file, depends + extra_deps)
759    SwigSource('m5.internal', i_file)
760
761# Generate the main swig init file
762def makeEmbeddedSwigInit(target, source, env):
763    code = code_formatter()
764    module = source[0].get_contents()
765    code('''\
766#include "sim/init.hh"
767
768extern "C" {
769    void init_${module}();
770}
771
772EmbeddedSwig embed_swig_${module}(init_${module});
773''')
774    code.write(str(target[0]))
775    
776# Build all swig modules
777for swig in SwigSource.all:
778    env.Command([swig.cc_source.tnode, swig.py_source.tnode], swig.tnode,
779                MakeAction('$SWIG $SWIGFLAGS -outdir ${TARGETS[1].dir} '
780                '-o ${TARGETS[0]} $SOURCES', Transform("SWIG")))
781    cc_file = str(swig.tnode)
782    init_file = '%s/%s_init.cc' % (dirname(cc_file), basename(cc_file))
783    env.Command(init_file, Value(swig.module),
784                MakeAction(makeEmbeddedSwigInit, Transform("EMBED SW")))
785    env.Depends(SWIG, init_file)
786    Source(init_file, **swig.guards)
787
788# Build all protocol buffers if we have got protoc and protobuf available
789if env['HAVE_PROTOBUF']:
790    for proto in ProtoBuf.all:
791        # Use both the source and header as the target, and the .proto
792        # file as the source. When executing the protoc compiler, also
793        # specify the proto_path to avoid having the generated files
794        # include the path.
795        env.Command([proto.cc_file, proto.hh_file], proto.tnode,
796                    MakeAction('$PROTOC --cpp_out ${TARGET.dir} '
797                               '--proto_path ${SOURCE.dir} $SOURCE',
798                               Transform("PROTOC")))
799
800        env.Depends(SWIG, [proto.cc_file, proto.hh_file])
801        # Add the C++ source file
802        Source(proto.cc_file, **proto.guards)
803elif ProtoBuf.all:
804    print 'Got protobuf to build, but lacks support!'
805    Exit(1)
806
807#
808# Handle debug flags
809#
810def makeDebugFlagCC(target, source, env):
811    assert(len(target) == 1 and len(source) == 1)
812
813    code = code_formatter()
814
815    # delay definition of CompoundFlags until after all the definition
816    # of all constituent SimpleFlags
817    comp_code = code_formatter()
818
819    # file header
820    code('''
821/*
822 * DO NOT EDIT THIS FILE! Automatically generated by SCons.
823 */
824
825#include "base/debug.hh"
826
827namespace Debug {
828
829''')
830
831    for name, flag in sorted(source[0].read().iteritems()):
832        n, compound, desc = flag
833        assert n == name
834
835        if not compound:
836            code('SimpleFlag $name("$name", "$desc");')
837        else:
838            comp_code('CompoundFlag $name("$name", "$desc",')
839            comp_code.indent()
840            last = len(compound) - 1
841            for i,flag in enumerate(compound):
842                if i != last:
843                    comp_code('$flag,')
844                else:
845                    comp_code('$flag);')
846            comp_code.dedent()
847
848    code.append(comp_code)
849    code()
850    code('} // namespace Debug')
851
852    code.write(str(target[0]))
853
854def makeDebugFlagHH(target, source, env):
855    assert(len(target) == 1 and len(source) == 1)
856
857    val = eval(source[0].get_contents())
858    name, compound, desc = val
859
860    code = code_formatter()
861
862    # file header boilerplate
863    code('''\
864/*
865 * DO NOT EDIT THIS FILE! Automatically generated by SCons.
866 */
867
868#ifndef __DEBUG_${name}_HH__
869#define __DEBUG_${name}_HH__
870
871namespace Debug {
872''')
873
874    if compound:
875        code('class CompoundFlag;')
876    code('class SimpleFlag;')
877
878    if compound:
879        code('extern CompoundFlag $name;')
880        for flag in compound:
881            code('extern SimpleFlag $flag;')
882    else:
883        code('extern SimpleFlag $name;')
884
885    code('''
886}
887
888#endif // __DEBUG_${name}_HH__
889''')
890
891    code.write(str(target[0]))
892
893for name,flag in sorted(debug_flags.iteritems()):
894    n, compound, desc = flag
895    assert n == name
896
897    hh_file = 'debug/%s.hh' % name
898    env.Command(hh_file, Value(flag),
899                MakeAction(makeDebugFlagHH, Transform("TRACING", 0)))
900    env.Depends(SWIG, hh_file)
901
902env.Command('debug/flags.cc', Value(debug_flags),
903            MakeAction(makeDebugFlagCC, Transform("TRACING", 0)))
904env.Depends(SWIG, 'debug/flags.cc')
905Source('debug/flags.cc')
906
907# Embed python files.  All .py files that have been indicated by a
908# PySource() call in a SConscript need to be embedded into the M5
909# library.  To do that, we compile the file to byte code, marshal the
910# byte code, compress it, and then generate a c++ file that
911# inserts the result into an array.
912def embedPyFile(target, source, env):
913    def c_str(string):
914        if string is None:
915            return "0"
916        return '"%s"' % string
917
918    '''Action function to compile a .py into a code object, marshal
919    it, compress it, and stick it into an asm file so the code appears
920    as just bytes with a label in the data section'''
921
922    src = file(str(source[0]), 'r').read()
923
924    pysource = PySource.tnodes[source[0]]
925    compiled = compile(src, pysource.abspath, 'exec')
926    marshalled = marshal.dumps(compiled)
927    compressed = zlib.compress(marshalled)
928    data = compressed
929    sym = pysource.symname
930
931    code = code_formatter()
932    code('''\
933#include "sim/init.hh"
934
935namespace {
936
937const uint8_t data_${sym}[] = {
938''')
939    code.indent()
940    step = 16
941    for i in xrange(0, len(data), step):
942        x = array.array('B', data[i:i+step])
943        code(''.join('%d,' % d for d in x))
944    code.dedent()
945    
946    code('''};
947
948EmbeddedPython embedded_${sym}(
949    ${{c_str(pysource.arcname)}},
950    ${{c_str(pysource.abspath)}},
951    ${{c_str(pysource.modpath)}},
952    data_${sym},
953    ${{len(data)}},
954    ${{len(marshalled)}});
955
956} // anonymous namespace
957''')
958    code.write(str(target[0]))
959
960for source in PySource.all:
961    env.Command(source.cpp, source.tnode,
962                MakeAction(embedPyFile, Transform("EMBED PY")))
963    env.Depends(SWIG, source.cpp)
964    Source(source.cpp, skip_no_python=True)
965
966########################################################################
967#
968# Define binaries.  Each different build type (debug, opt, etc.) gets
969# a slightly different build environment.
970#
971
972# List of constructed environments to pass back to SConstruct
973date_source = Source('base/date.cc', skip_lib=True)
974
975# Capture this directory for the closure makeEnv, otherwise when it is
976# called, it won't know what directory it should use.
977variant_dir = Dir('.').path
978def variant(*path):
979    return os.path.join(variant_dir, *path)
980def variantd(*path):
981    return variant(*path)+'/'
982
983# Function to create a new build environment as clone of current
984# environment 'env' with modified object suffix and optional stripped
985# binary.  Additional keyword arguments are appended to corresponding
986# build environment vars.
987def makeEnv(env, label, objsfx, strip = False, **kwargs):
988    # SCons doesn't know to append a library suffix when there is a '.' in the
989    # name.  Use '_' instead.
990    libname = variant('gem5_' + label)
991    exename = variant('gem5.' + label)
992    secondary_exename = variant('m5.' + label)
993
994    new_env = env.Clone(OBJSUFFIX=objsfx, SHOBJSUFFIX=objsfx + 's')
995    new_env.Label = label
996    new_env.Append(**kwargs)
997
998    swig_env = new_env.Clone()
999
1000    # Both gcc and clang have issues with unused labels and values in
1001    # the SWIG generated code
1002    swig_env.Append(CCFLAGS=['-Wno-unused-label', '-Wno-unused-value'])
1003
1004    # Add additional warnings here that should not be applied to
1005    # the SWIG generated code
1006    new_env.Append(CXXFLAGS='-Wmissing-declarations')
1007
1008    if env['GCC']:
1009        # Depending on the SWIG version, we also need to supress
1010        # warnings about uninitialized variables and missing field
1011        # initializers.
1012        swig_env.Append(CCFLAGS=['-Wno-uninitialized',
1013                                 '-Wno-missing-field-initializers',
1014                                 '-Wno-unused-but-set-variable'])
1015
1016        # If gcc supports it, also warn for deletion of derived
1017        # classes with non-virtual desctructors. For gcc >= 4.7 we
1018        # also have to disable warnings about the SWIG code having
1019        # potentially uninitialized variables.
1020        if compareVersions(env['GCC_VERSION'], '4.7') >= 0:
1021            new_env.Append(CXXFLAGS='-Wdelete-non-virtual-dtor')
1022            swig_env.Append(CCFLAGS='-Wno-maybe-uninitialized')
1023
1024        # Only gcc >= 4.9 supports UBSan, so check both the version
1025        # and the command-line option before adding the compiler and
1026        # linker flags.
1027        if GetOption('with_ubsan') and \
1028                compareVersions(env['GCC_VERSION'], '4.9') >= 0:
1029            new_env.Append(CCFLAGS='-fsanitize=undefined')
1030            new_env.Append(LINKFLAGS='-fsanitize=undefined')
1031
1032    if env['CLANG']:
1033        # Always enable the warning for deletion of derived classes
1034        # with non-virtual destructors
1035        new_env.Append(CXXFLAGS=['-Wdelete-non-virtual-dtor'])
1036
1037        swig_env.Append(CCFLAGS=[
1038                # Some versions of SWIG can return uninitialized values
1039                '-Wno-sometimes-uninitialized',
1040                # Register storage is requested in a lot of places in
1041                # SWIG-generated code.
1042                '-Wno-deprecated-register',
1043                ])
1044
1045        # All supported clang versions have support for UBSan, so if
1046        # asked to use it, append the compiler and linker flags.
1047        if GetOption('with_ubsan'):
1048            new_env.Append(CCFLAGS='-fsanitize=undefined')
1049            new_env.Append(LINKFLAGS='-fsanitize=undefined')
1050
1051    werror_env = new_env.Clone()
1052    werror_env.Append(CCFLAGS='-Werror')
1053
1054    def make_obj(source, static, extra_deps = None):
1055        '''This function adds the specified source to the correct
1056        build environment, and returns the corresponding SCons Object
1057        nodes'''
1058
1059        if source.swig:
1060            env = swig_env
1061        elif source.Werror:
1062            env = werror_env
1063        else:
1064            env = new_env
1065
1066        if static:
1067            obj = env.StaticObject(source.tnode)
1068        else:
1069            obj = env.SharedObject(source.tnode)
1070
1071        if extra_deps:
1072            env.Depends(obj, extra_deps)
1073
1074        return obj
1075
1076    lib_guards = {'main': False, 'skip_lib': False}
1077
1078    # Without Python, leave out all SWIG and Python content from the
1079    # library builds.  The option doesn't affect gem5 built as a program
1080    if GetOption('without_python'):
1081        lib_guards['skip_no_python'] = False
1082
1083    static_objs = [ make_obj(s, True) for s in Source.get(**lib_guards) ]
1084    shared_objs = [ make_obj(s, False) for s in Source.get(**lib_guards) ]
1085
1086    static_date = make_obj(date_source, static=True, extra_deps=static_objs)
1087    static_objs.append(static_date)
1088
1089    shared_date = make_obj(date_source, static=False, extra_deps=shared_objs)
1090    shared_objs.append(shared_date)
1091
1092    # First make a library of everything but main() so other programs can
1093    # link against m5.
1094    static_lib = new_env.StaticLibrary(libname, static_objs)
1095    shared_lib = new_env.SharedLibrary(libname, shared_objs)
1096
1097    # Now link a stub with main() and the static library.
1098    main_objs = [ make_obj(s, True) for s in Source.get(main=True) ]
1099
1100    for test in UnitTest.all:
1101        flags = { test.target : True }
1102        test_sources = Source.get(**flags)
1103        test_objs = [ make_obj(s, static=True) for s in test_sources ]
1104        if test.main:
1105            test_objs += main_objs
1106        path = variant('unittest/%s.%s' % (test.target, label))
1107        new_env.Program(path, test_objs + static_objs)
1108
1109    progname = exename
1110    if strip:
1111        progname += '.unstripped'
1112
1113    targets = new_env.Program(progname, main_objs + static_objs)
1114
1115    if strip:
1116        if sys.platform == 'sunos5':
1117            cmd = 'cp $SOURCE $TARGET; strip $TARGET'
1118        else:
1119            cmd = 'strip $SOURCE -o $TARGET'
1120        targets = new_env.Command(exename, progname,
1121                    MakeAction(cmd, Transform("STRIP")))
1122
1123    new_env.Command(secondary_exename, exename,
1124            MakeAction('ln $SOURCE $TARGET', Transform("HARDLINK")))
1125
1126    new_env.M5Binary = targets[0]
1127    return new_env
1128
1129# Start out with the compiler flags common to all compilers,
1130# i.e. they all use -g for opt and -g -pg for prof
1131ccflags = {'debug' : [], 'opt' : ['-g'], 'fast' : [], 'prof' : ['-g', '-pg'],
1132           'perf' : ['-g']}
1133
1134# Start out with the linker flags common to all linkers, i.e. -pg for
1135# prof, and -lprofiler for perf. The -lprofile flag is surrounded by
1136# no-as-needed and as-needed as the binutils linker is too clever and
1137# simply doesn't link to the library otherwise.
1138ldflags = {'debug' : [], 'opt' : [], 'fast' : [], 'prof' : ['-pg'],
1139           'perf' : ['-Wl,--no-as-needed', '-lprofiler', '-Wl,--as-needed']}
1140
1141# For Link Time Optimization, the optimisation flags used to compile
1142# individual files are decoupled from those used at link time
1143# (i.e. you can compile with -O3 and perform LTO with -O0), so we need
1144# to also update the linker flags based on the target.
1145if env['GCC']:
1146    if sys.platform == 'sunos5':
1147        ccflags['debug'] += ['-gstabs+']
1148    else:
1149        ccflags['debug'] += ['-ggdb3']
1150    ldflags['debug'] += ['-O0']
1151    # opt, fast, prof and perf all share the same cc flags, also add
1152    # the optimization to the ldflags as LTO defers the optimization
1153    # to link time
1154    for target in ['opt', 'fast', 'prof', 'perf']:
1155        ccflags[target] += ['-O3']
1156        ldflags[target] += ['-O3']
1157
1158    ccflags['fast'] += env['LTO_CCFLAGS']
1159    ldflags['fast'] += env['LTO_LDFLAGS']
1160elif env['CLANG']:
1161    ccflags['debug'] += ['-g', '-O0']
1162    # opt, fast, prof and perf all share the same cc flags
1163    for target in ['opt', 'fast', 'prof', 'perf']:
1164        ccflags[target] += ['-O3']
1165else:
1166    print 'Unknown compiler, please fix compiler options'
1167    Exit(1)
1168
1169
1170# To speed things up, we only instantiate the build environments we
1171# need.  We try to identify the needed environment for each target; if
1172# we can't, we fall back on instantiating all the environments just to
1173# be safe.
1174target_types = ['debug', 'opt', 'fast', 'prof', 'perf']
1175obj2target = {'do': 'debug', 'o': 'opt', 'fo': 'fast', 'po': 'prof',
1176              'gpo' : 'perf'}
1177
1178def identifyTarget(t):
1179    ext = t.split('.')[-1]
1180    if ext in target_types:
1181        return ext
1182    if obj2target.has_key(ext):
1183        return obj2target[ext]
1184    match = re.search(r'/tests/([^/]+)/', t)
1185    if match and match.group(1) in target_types:
1186        return match.group(1)
1187    return 'all'
1188
1189needed_envs = [identifyTarget(target) for target in BUILD_TARGETS]
1190if 'all' in needed_envs:
1191    needed_envs += target_types
1192
1193gem5_root = Dir('.').up().up().abspath
1194def makeEnvirons(target, source, env):
1195    # cause any later Source() calls to be fatal, as a diagnostic.
1196    Source.done()
1197
1198    envList = []
1199
1200    # Debug binary
1201    if 'debug' in needed_envs:
1202        envList.append(
1203            makeEnv(env, 'debug', '.do',
1204                    CCFLAGS = Split(ccflags['debug']),
1205                    CPPDEFINES = ['DEBUG', 'TRACING_ON=1'],
1206                    LINKFLAGS = Split(ldflags['debug'])))
1207
1208    # Optimized binary
1209    if 'opt' in needed_envs:
1210        envList.append(
1211            makeEnv(env, 'opt', '.o',
1212                    CCFLAGS = Split(ccflags['opt']),
1213                    CPPDEFINES = ['TRACING_ON=1'],
1214                    LINKFLAGS = Split(ldflags['opt'])))
1215
1216    # "Fast" binary
1217    if 'fast' in needed_envs:
1218        envList.append(
1219            makeEnv(env, 'fast', '.fo', strip = True,
1220                    CCFLAGS = Split(ccflags['fast']),
1221                    CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1222                    LINKFLAGS = Split(ldflags['fast'])))
1223
1224    # Profiled binary using gprof
1225    if 'prof' in needed_envs:
1226        envList.append(
1227            makeEnv(env, 'prof', '.po',
1228                    CCFLAGS = Split(ccflags['prof']),
1229                    CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1230                    LINKFLAGS = Split(ldflags['prof'])))
1231
1232    # Profiled binary using google-pprof
1233    if 'perf' in needed_envs:
1234        envList.append(
1235            makeEnv(env, 'perf', '.gpo',
1236                    CCFLAGS = Split(ccflags['perf']),
1237                    CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1238                    LINKFLAGS = Split(ldflags['perf'])))
1239
1240    # Set up the regression tests for each build.
1241    for e in envList:
1242        SConscript(os.path.join(gem5_root, 'tests', 'SConscript'),
1243                   variant_dir = variantd('tests', e.Label),
1244                   exports = { 'env' : e }, duplicate = False)
1245
1246# The MakeEnvirons Builder defers the full dependency collection until
1247# after processing the ISA definition (due to dynamically generated
1248# source files).  Add this dependency to all targets so they will wait
1249# until the environments are completely set up.  Otherwise, a second
1250# process (e.g. -j2 or higher) will try to compile the requested target,
1251# not know how, and fail.
1252env.Append(BUILDERS = {'MakeEnvirons' :
1253                        Builder(action=MakeAction(makeEnvirons,
1254                                                  Transform("ENVIRONS", 1)))})
1255
1256isa_target = env['PHONY_BASE'] + '-deps'
1257environs   = env['PHONY_BASE'] + '-environs'
1258env.Depends('#all-deps',     isa_target)
1259env.Depends('#all-environs', environs)
1260env.ScanISA(isa_target, File('arch/%s/generated/inc.d' % env['TARGET_ISA']))
1261envSetup = env.MakeEnvirons(environs, isa_target)
1262
1263# make sure no -deps targets occur before all ISAs are complete
1264env.Depends(isa_target, '#all-isas')
1265# likewise for -environs targets and all the -deps targets
1266env.Depends(environs, '#all-deps')
1267