SConscript revision 8607:5fb918115c07
1955SN/A# -*- mode:python -*-
2955SN/A
39812Sandreas.hansson@arm.com# Copyright (c) 2004-2005 The Regents of The University of Michigan
49812Sandreas.hansson@arm.com# All rights reserved.
59812Sandreas.hansson@arm.com#
69812Sandreas.hansson@arm.com# Redistribution and use in source and binary forms, with or without
79812Sandreas.hansson@arm.com# modification, are permitted provided that the following conditions are
89812Sandreas.hansson@arm.com# met: redistributions of source code must retain the above copyright
99812Sandreas.hansson@arm.com# notice, this list of conditions and the following disclaimer;
109812Sandreas.hansson@arm.com# redistributions in binary form must reproduce the above copyright
119812Sandreas.hansson@arm.com# notice, this list of conditions and the following disclaimer in the
129812Sandreas.hansson@arm.com# documentation and/or other materials provided with the distribution;
139812Sandreas.hansson@arm.com# neither the name of the copyright holders nor the names of its
149812Sandreas.hansson@arm.com# contributors may be used to endorse or promote products derived from
157816Ssteve.reinhardt@amd.com# this software without specific prior written permission.
165871Snate@binkert.org#
171762SN/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.
28955SN/A#
29955SN/A# Authors: Nathan Binkert
30955SN/A
31955SN/Aimport array
32955SN/Aimport bisect
33955SN/Aimport imp
34955SN/Aimport marshal
35955SN/Aimport os
36955SN/Aimport re
37955SN/Aimport sys
38955SN/Aimport zlib
39955SN/A
40955SN/Afrom os.path import basename, dirname, exists, isdir, isfile, join as joinpath
41955SN/A
422665Ssaidi@eecs.umich.eduimport SCons
432665Ssaidi@eecs.umich.edu
445863Snate@binkert.org# This file defines how to build a particular configuration of gem5
45955SN/A# based on variable settings in the 'env' build environment.
46955SN/A
47955SN/AImport('*')
48955SN/A
49955SN/A# Children need to see the environment
508878Ssteve.reinhardt@amd.comExport('env')
512632Sstever@eecs.umich.edu
528878Ssteve.reinhardt@amd.combuild_env = [(opt, env[opt]) for opt in export_vars]
532632Sstever@eecs.umich.edu
54955SN/Afrom m5.util import code_formatter
558878Ssteve.reinhardt@amd.com
562632Sstever@eecs.umich.edu########################################################################
572761Sstever@eecs.umich.edu# Code for adding source files of various types
582632Sstever@eecs.umich.edu#
592632Sstever@eecs.umich.edu# When specifying a source file of some type, a set of guards can be
602632Sstever@eecs.umich.edu# specified for that file.  When get() is used to find the files, if
612761Sstever@eecs.umich.edu# get specifies a set of filters, only files that match those filters
622761Sstever@eecs.umich.edu# will be accepted (unspecified filters on files are assumed to be
632761Sstever@eecs.umich.edu# false).  Current filters are:
648878Ssteve.reinhardt@amd.com#     main -- specifies the gem5 main() function
658878Ssteve.reinhardt@amd.com#     skip_lib -- do not put this file into the gem5 library
662761Sstever@eecs.umich.edu#     <unittest> -- unit tests use filters based on the unit test name
672761Sstever@eecs.umich.edu#
682761Sstever@eecs.umich.edu# A parent can now be specified for a source file and default filter
692761Sstever@eecs.umich.edu# values will be retrieved recursively from parents (children override
702761Sstever@eecs.umich.edu# parents).
718878Ssteve.reinhardt@amd.com#
728878Ssteve.reinhardt@amd.comclass SourceMeta(type):
732632Sstever@eecs.umich.edu    '''Meta class for source files that keeps track of all files of a
742632Sstever@eecs.umich.edu    particular type and has a get function for finding all functions
758878Ssteve.reinhardt@amd.com    of a certain type that match a set of guards'''
768878Ssteve.reinhardt@amd.com    def __init__(cls, name, bases, dict):
772632Sstever@eecs.umich.edu        super(SourceMeta, cls).__init__(name, bases, dict)
78955SN/A        cls.all = []
79955SN/A        
80955SN/A    def get(cls, **guards):
815863Snate@binkert.org        '''Find all files that match the specified guards.  If a source
825863Snate@binkert.org        file does not specify a flag, the default is False'''
835863Snate@binkert.org        for src in cls.all:
845863Snate@binkert.org            for flag,value in guards.iteritems():
855863Snate@binkert.org                # if the flag is found and has a different value, skip
865863Snate@binkert.org                # this file
875863Snate@binkert.org                if src.all_guards.get(flag, False) != value:
885863Snate@binkert.org                    break
895863Snate@binkert.org            else:
905863Snate@binkert.org                yield src
915863Snate@binkert.org
928878Ssteve.reinhardt@amd.comclass SourceFile(object):
935863Snate@binkert.org    '''Base object that encapsulates the notion of a source file.
945863Snate@binkert.org    This includes, the source node, target node, various manipulations
955863Snate@binkert.org    of those.  A source file also specifies a set of guards which
969812Sandreas.hansson@arm.com    describing which builds the source file applies to.  A parent can
979812Sandreas.hansson@arm.com    also be specified to get default guards from'''
985863Snate@binkert.org    __metaclass__ = SourceMeta
999812Sandreas.hansson@arm.com    def __init__(self, source, parent=None, **guards):
1005863Snate@binkert.org        self.guards = guards
1015863Snate@binkert.org        self.parent = parent
1025863Snate@binkert.org
1039812Sandreas.hansson@arm.com        tnode = source
1049812Sandreas.hansson@arm.com        if not isinstance(source, SCons.Node.FS.File):
1055863Snate@binkert.org            tnode = File(source)
1065863Snate@binkert.org
1078878Ssteve.reinhardt@amd.com        self.tnode = tnode
1085863Snate@binkert.org        self.snode = tnode.srcnode()
1095863Snate@binkert.org
1105863Snate@binkert.org        for base in type(self).__mro__:
1116654Snate@binkert.org            if issubclass(base, SourceFile):
112955SN/A                base.all.append(self)
1135396Ssaidi@eecs.umich.edu
1145863Snate@binkert.org    @property
1155863Snate@binkert.org    def filename(self):
1164202Sbinkertn@umich.edu        return str(self.tnode)
1175863Snate@binkert.org
1185863Snate@binkert.org    @property
1195863Snate@binkert.org    def dirname(self):
1205863Snate@binkert.org        return dirname(self.filename)
121955SN/A
1226654Snate@binkert.org    @property
1235273Sstever@gmail.com    def basename(self):
1245871Snate@binkert.org        return basename(self.filename)
1255273Sstever@gmail.com
1266655Snate@binkert.org    @property
1278878Ssteve.reinhardt@amd.com    def extname(self):
1286655Snate@binkert.org        index = self.basename.rfind('.')
1296655Snate@binkert.org        if index <= 0:
1309219Spower.jg@gmail.com            # dot files aren't extensions
1316655Snate@binkert.org            return self.basename, None
1325871Snate@binkert.org
1336654Snate@binkert.org        return self.basename[:index], self.basename[index+1:]
1348947Sandreas.hansson@arm.com
1355396Ssaidi@eecs.umich.edu    @property
1368120Sgblack@eecs.umich.edu    def all_guards(self):
1378120Sgblack@eecs.umich.edu        '''find all guards for this object getting default values
1388120Sgblack@eecs.umich.edu        recursively from its parents'''
1398120Sgblack@eecs.umich.edu        guards = {}
1408120Sgblack@eecs.umich.edu        if self.parent:
1418120Sgblack@eecs.umich.edu            guards.update(self.parent.guards)
1428120Sgblack@eecs.umich.edu        guards.update(self.guards)
1438120Sgblack@eecs.umich.edu        return guards
1448879Ssteve.reinhardt@amd.com
1458879Ssteve.reinhardt@amd.com    def __lt__(self, other): return self.filename < other.filename
1468879Ssteve.reinhardt@amd.com    def __le__(self, other): return self.filename <= other.filename
1478879Ssteve.reinhardt@amd.com    def __gt__(self, other): return self.filename > other.filename
1488879Ssteve.reinhardt@amd.com    def __ge__(self, other): return self.filename >= other.filename
1498879Ssteve.reinhardt@amd.com    def __eq__(self, other): return self.filename == other.filename
1508879Ssteve.reinhardt@amd.com    def __ne__(self, other): return self.filename != other.filename
1518879Ssteve.reinhardt@amd.com        
1528879Ssteve.reinhardt@amd.comclass Source(SourceFile):
1538879Ssteve.reinhardt@amd.com    '''Add a c/c++ source file to the build'''
1548879Ssteve.reinhardt@amd.com    def __init__(self, source, Werror=True, swig=False, **guards):
1558879Ssteve.reinhardt@amd.com        '''specify the source file, and any guards'''
1568879Ssteve.reinhardt@amd.com        super(Source, self).__init__(source, **guards)
1578120Sgblack@eecs.umich.edu
1588120Sgblack@eecs.umich.edu        self.Werror = Werror
1598120Sgblack@eecs.umich.edu        self.swig = swig
1608120Sgblack@eecs.umich.edu
1618120Sgblack@eecs.umich.educlass PySource(SourceFile):
1628120Sgblack@eecs.umich.edu    '''Add a python source file to the named package'''
1638120Sgblack@eecs.umich.edu    invalid_sym_char = re.compile('[^A-z0-9_]')
1648120Sgblack@eecs.umich.edu    modules = {}
1658120Sgblack@eecs.umich.edu    tnodes = {}
1668120Sgblack@eecs.umich.edu    symnames = {}
1678120Sgblack@eecs.umich.edu    
1688120Sgblack@eecs.umich.edu    def __init__(self, package, source, **guards):
1698120Sgblack@eecs.umich.edu        '''specify the python package, the source file, and any guards'''
1708120Sgblack@eecs.umich.edu        super(PySource, self).__init__(source, **guards)
1718879Ssteve.reinhardt@amd.com
1728879Ssteve.reinhardt@amd.com        modname,ext = self.extname
1738879Ssteve.reinhardt@amd.com        assert ext == 'py'
1748879Ssteve.reinhardt@amd.com
1758879Ssteve.reinhardt@amd.com        if package:
1768879Ssteve.reinhardt@amd.com            path = package.split('.')
1778879Ssteve.reinhardt@amd.com        else:
1788879Ssteve.reinhardt@amd.com            path = []
1799227Sandreas.hansson@arm.com
1809227Sandreas.hansson@arm.com        modpath = path[:]
1818879Ssteve.reinhardt@amd.com        if modname != '__init__':
1828879Ssteve.reinhardt@amd.com            modpath += [ modname ]
1838879Ssteve.reinhardt@amd.com        modpath = '.'.join(modpath)
1848879Ssteve.reinhardt@amd.com
1858120Sgblack@eecs.umich.edu        arcpath = path + [ self.basename ]
1868947Sandreas.hansson@arm.com        abspath = self.snode.abspath
1877816Ssteve.reinhardt@amd.com        if not exists(abspath):
1885871Snate@binkert.org            abspath = self.tnode.abspath
1895871Snate@binkert.org
1906121Snate@binkert.org        self.package = package
1915871Snate@binkert.org        self.modname = modname
1925871Snate@binkert.org        self.modpath = modpath
1939119Sandreas.hansson@arm.com        self.arcname = joinpath(*arcpath)
1949396Sandreas.hansson@arm.com        self.abspath = abspath
1959396Sandreas.hansson@arm.com        self.compiled = File(self.filename + 'c')
196955SN/A        self.cpp = File(self.filename + '.cc')
1979416SAndreas.Sandberg@ARM.com        self.symname = PySource.invalid_sym_char.sub('_', modpath)
1989416SAndreas.Sandberg@ARM.com
1999416SAndreas.Sandberg@ARM.com        PySource.modules[modpath] = self
2009416SAndreas.Sandberg@ARM.com        PySource.tnodes[self.tnode] = self
2019416SAndreas.Sandberg@ARM.com        PySource.symnames[self.symname] = self
2029416SAndreas.Sandberg@ARM.com
2039416SAndreas.Sandberg@ARM.comclass SimObject(PySource):
2045871Snate@binkert.org    '''Add a SimObject python file as a python source object and add
2055871Snate@binkert.org    it to a list of sim object modules'''
2069416SAndreas.Sandberg@ARM.com
2079416SAndreas.Sandberg@ARM.com    fixed = False
2085871Snate@binkert.org    modnames = []
209955SN/A
2106121Snate@binkert.org    def __init__(self, source, **guards):
2118881Smarc.orr@gmail.com        '''Specify the source file and any guards (automatically in
2126121Snate@binkert.org        the m5.objects package)'''
2136121Snate@binkert.org        super(SimObject, self).__init__('m5.objects', source, **guards)
2141533SN/A        if self.fixed:
2159239Sandreas.hansson@arm.com            raise AttributeError, "Too late to call SimObject now."
2169239Sandreas.hansson@arm.com
2179239Sandreas.hansson@arm.com        bisect.insort_right(SimObject.modnames, self.modname)
2189239Sandreas.hansson@arm.com
2199239Sandreas.hansson@arm.comclass SwigSource(SourceFile):
2209239Sandreas.hansson@arm.com    '''Add a swig file to build'''
2219239Sandreas.hansson@arm.com
2229239Sandreas.hansson@arm.com    def __init__(self, package, source, **guards):
2239239Sandreas.hansson@arm.com        '''Specify the python package, the source file, and any guards'''
2249239Sandreas.hansson@arm.com        super(SwigSource, self).__init__(source, **guards)
2259239Sandreas.hansson@arm.com
2269239Sandreas.hansson@arm.com        modname,ext = self.extname
2276655Snate@binkert.org        assert ext == 'i'
2286655Snate@binkert.org
2296655Snate@binkert.org        self.module = modname
2306655Snate@binkert.org        cc_file = joinpath(self.dirname, modname + '_wrap.cc')
2315871Snate@binkert.org        py_file = joinpath(self.dirname, modname + '.py')
2325871Snate@binkert.org
2335863Snate@binkert.org        self.cc_source = Source(cc_file, swig=True, parent=self)
2345871Snate@binkert.org        self.py_source = PySource(package, py_file, parent=self)
2358878Ssteve.reinhardt@amd.com
2365871Snate@binkert.orgclass UnitTest(object):
2375871Snate@binkert.org    '''Create a UnitTest'''
2385871Snate@binkert.org
2395863Snate@binkert.org    all = []
2406121Snate@binkert.org    def __init__(self, target, *sources):
2415863Snate@binkert.org        '''Specify the target name and any sources.  Sources that are
2425871Snate@binkert.org        not SourceFiles are evalued with Source().  All files are
2438336Ssteve.reinhardt@amd.com        guarded with a guard of the same name as the UnitTest
2448336Ssteve.reinhardt@amd.com        target.'''
2458336Ssteve.reinhardt@amd.com
2468336Ssteve.reinhardt@amd.com        srcs = []
2474678Snate@binkert.org        for src in sources:
2488336Ssteve.reinhardt@amd.com            if not isinstance(src, SourceFile):
2498336Ssteve.reinhardt@amd.com                src = Source(src, skip_lib=True)
2508336Ssteve.reinhardt@amd.com            src.guards[target] = True
2514678Snate@binkert.org            srcs.append(src)
2524678Snate@binkert.org
2534678Snate@binkert.org        self.sources = srcs
2544678Snate@binkert.org        self.target = target
2557827Snate@binkert.org        UnitTest.all.append(self)
2567827Snate@binkert.org
2578336Ssteve.reinhardt@amd.com# Children should have access
2584678Snate@binkert.orgExport('Source')
2598336Ssteve.reinhardt@amd.comExport('PySource')
2608336Ssteve.reinhardt@amd.comExport('SimObject')
2618336Ssteve.reinhardt@amd.comExport('SwigSource')
2628336Ssteve.reinhardt@amd.comExport('UnitTest')
2638336Ssteve.reinhardt@amd.com
2648336Ssteve.reinhardt@amd.com########################################################################
2655871Snate@binkert.org#
2665871Snate@binkert.org# Debug Flags
2678336Ssteve.reinhardt@amd.com#
2688336Ssteve.reinhardt@amd.comdebug_flags = {}
2698336Ssteve.reinhardt@amd.comdef DebugFlag(name, desc=None):
2708336Ssteve.reinhardt@amd.com    if name in debug_flags:
2718336Ssteve.reinhardt@amd.com        raise AttributeError, "Flag %s already specified" % name
2725871Snate@binkert.org    debug_flags[name] = (name, (), desc)
2738336Ssteve.reinhardt@amd.com
2748336Ssteve.reinhardt@amd.comdef CompoundFlag(name, flags, desc=None):
2758336Ssteve.reinhardt@amd.com    if name in debug_flags:
2768336Ssteve.reinhardt@amd.com        raise AttributeError, "Flag %s already specified" % name
2778336Ssteve.reinhardt@amd.com
2784678Snate@binkert.org    compound = tuple(flags)
2795871Snate@binkert.org    debug_flags[name] = (name, compound, desc)
2804678Snate@binkert.org
2818336Ssteve.reinhardt@amd.comExport('DebugFlag')
2828336Ssteve.reinhardt@amd.comExport('CompoundFlag')
2838336Ssteve.reinhardt@amd.com
2848336Ssteve.reinhardt@amd.com########################################################################
2858336Ssteve.reinhardt@amd.com#
2868336Ssteve.reinhardt@amd.com# Set some compiler variables
2878336Ssteve.reinhardt@amd.com#
2888336Ssteve.reinhardt@amd.com
2898336Ssteve.reinhardt@amd.com# Include file paths are rooted in this directory.  SCons will
2908336Ssteve.reinhardt@amd.com# automatically expand '.' to refer to both the source directory and
2918336Ssteve.reinhardt@amd.com# the corresponding build directory to pick up generated include
2928336Ssteve.reinhardt@amd.com# files.
2938336Ssteve.reinhardt@amd.comenv.Append(CPPPATH=Dir('.'))
2948336Ssteve.reinhardt@amd.com
2958336Ssteve.reinhardt@amd.comfor extra_dir in extras_dir_list:
2968336Ssteve.reinhardt@amd.com    env.Append(CPPPATH=Dir(extra_dir))
2978336Ssteve.reinhardt@amd.com
2985871Snate@binkert.org# Workaround for bug in SCons version > 0.97d20071212
2996121Snate@binkert.org# Scons bug id: 2006 gem5 Bug id: 308
300955SN/Afor root, dirs, files in os.walk(base_dir, topdown=True):
301955SN/A    Dir(root[len(base_dir) + 1:])
3022632Sstever@eecs.umich.edu
3032632Sstever@eecs.umich.edu########################################################################
304955SN/A#
305955SN/A# Walk the tree and execute all SConscripts in subdirectories
306955SN/A#
307955SN/A
3088878Ssteve.reinhardt@amd.comhere = Dir('.').srcnode().abspath
309955SN/Afor root, dirs, files in os.walk(base_dir, topdown=True):
3102632Sstever@eecs.umich.edu    if root == here:
3112632Sstever@eecs.umich.edu        # we don't want to recurse back into this SConscript
3122632Sstever@eecs.umich.edu        continue
3132632Sstever@eecs.umich.edu
3142632Sstever@eecs.umich.edu    if 'SConscript' in files:
3152632Sstever@eecs.umich.edu        build_dir = joinpath(env['BUILDDIR'], root[len(base_dir) + 1:])
3162632Sstever@eecs.umich.edu        SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
3178268Ssteve.reinhardt@amd.com
3188268Ssteve.reinhardt@amd.comfor extra_dir in extras_dir_list:
3198268Ssteve.reinhardt@amd.com    prefix_len = len(dirname(extra_dir)) + 1
3208268Ssteve.reinhardt@amd.com    for root, dirs, files in os.walk(extra_dir, topdown=True):
3218268Ssteve.reinhardt@amd.com        # if build lives in the extras directory, don't walk down it
3228268Ssteve.reinhardt@amd.com        if 'build' in dirs:
3238268Ssteve.reinhardt@amd.com            dirs.remove('build')
3242632Sstever@eecs.umich.edu
3252632Sstever@eecs.umich.edu        if 'SConscript' in files:
3262632Sstever@eecs.umich.edu            build_dir = joinpath(env['BUILDDIR'], root[prefix_len:])
3272632Sstever@eecs.umich.edu            SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
3288268Ssteve.reinhardt@amd.com
3292632Sstever@eecs.umich.edufor opt in export_vars:
3308268Ssteve.reinhardt@amd.com    env.ConfigFile(opt)
3318268Ssteve.reinhardt@amd.com
3328268Ssteve.reinhardt@amd.comdef makeTheISA(source, target, env):
3338268Ssteve.reinhardt@amd.com    isas = [ src.get_contents() for src in source ]
3343718Sstever@eecs.umich.edu    target_isa = env['TARGET_ISA']
3352634Sstever@eecs.umich.edu    def define(isa):
3362634Sstever@eecs.umich.edu        return isa.upper() + '_ISA'
3375863Snate@binkert.org    
3382638Sstever@eecs.umich.edu    def namespace(isa):
3398268Ssteve.reinhardt@amd.com        return isa[0].upper() + isa[1:].lower() + 'ISA' 
3402632Sstever@eecs.umich.edu
3412632Sstever@eecs.umich.edu
3422632Sstever@eecs.umich.edu    code = code_formatter()
3432632Sstever@eecs.umich.edu    code('''\
3442632Sstever@eecs.umich.edu#ifndef __CONFIG_THE_ISA_HH__
3451858SN/A#define __CONFIG_THE_ISA_HH__
3463716Sstever@eecs.umich.edu
3472638Sstever@eecs.umich.edu''')
3482638Sstever@eecs.umich.edu
3492638Sstever@eecs.umich.edu    for i,isa in enumerate(isas):
3502638Sstever@eecs.umich.edu        code('#define $0 $1', define(isa), i + 1)
3512638Sstever@eecs.umich.edu
3522638Sstever@eecs.umich.edu    code('''
3532638Sstever@eecs.umich.edu
3545863Snate@binkert.org#define THE_ISA ${{define(target_isa)}}
3555863Snate@binkert.org#define TheISA ${{namespace(target_isa)}}
3565863Snate@binkert.org
357955SN/A#endif // __CONFIG_THE_ISA_HH__''')
3585341Sstever@gmail.com
3595341Sstever@gmail.com    code.write(str(target[0]))
3605863Snate@binkert.org
3617756SAli.Saidi@ARM.comenv.Command('config/the_isa.hh', map(Value, all_isa_list),
3625341Sstever@gmail.com            MakeAction(makeTheISA, Transform("CFG ISA", 0)))
3636121Snate@binkert.org
3644494Ssaidi@eecs.umich.edu########################################################################
3656121Snate@binkert.org#
3661105SN/A# Prevent any SimObjects from being added after this point, they
3672667Sstever@eecs.umich.edu# should all have been added in the SConscripts above
3682667Sstever@eecs.umich.edu#
3692667Sstever@eecs.umich.eduSimObject.fixed = True
3702667Sstever@eecs.umich.edu
3716121Snate@binkert.orgclass DictImporter(object):
3722667Sstever@eecs.umich.edu    '''This importer takes a dictionary of arbitrary module names that
3735341Sstever@gmail.com    map to arbitrary filenames.'''
3745863Snate@binkert.org    def __init__(self, modules):
3755341Sstever@gmail.com        self.modules = modules
3765341Sstever@gmail.com        self.installed = set()
3775341Sstever@gmail.com
3788120Sgblack@eecs.umich.edu    def __del__(self):
3795341Sstever@gmail.com        self.unload()
3808120Sgblack@eecs.umich.edu
3815341Sstever@gmail.com    def unload(self):
3828120Sgblack@eecs.umich.edu        import sys
3836121Snate@binkert.org        for module in self.installed:
3846121Snate@binkert.org            del sys.modules[module]
3858980Ssteve.reinhardt@amd.com        self.installed = set()
3869396Sandreas.hansson@arm.com
3875397Ssaidi@eecs.umich.edu    def find_module(self, fullname, path):
3885397Ssaidi@eecs.umich.edu        if fullname == 'm5.defines':
3897727SAli.Saidi@ARM.com            return self
3908268Ssteve.reinhardt@amd.com
3916168Snate@binkert.org        if fullname == 'm5.objects':
3925341Sstever@gmail.com            return self
3938120Sgblack@eecs.umich.edu
3948120Sgblack@eecs.umich.edu        if fullname.startswith('m5.internal'):
3958120Sgblack@eecs.umich.edu            return None
3966814Sgblack@eecs.umich.edu
3975863Snate@binkert.org        source = self.modules.get(fullname, None)
3988120Sgblack@eecs.umich.edu        if source is not None and fullname.startswith('m5.objects'):
3995341Sstever@gmail.com            return self
4005863Snate@binkert.org
4018268Ssteve.reinhardt@amd.com        return None
4026121Snate@binkert.org
4036121Snate@binkert.org    def load_module(self, fullname):
4048268Ssteve.reinhardt@amd.com        mod = imp.new_module(fullname)
4055742Snate@binkert.org        sys.modules[fullname] = mod
4065742Snate@binkert.org        self.installed.add(fullname)
4075341Sstever@gmail.com
4085742Snate@binkert.org        mod.__loader__ = self
4095742Snate@binkert.org        if fullname == 'm5.objects':
4105341Sstever@gmail.com            mod.__path__ = fullname.split('.')
4116017Snate@binkert.org            return mod
4126121Snate@binkert.org
4136017Snate@binkert.org        if fullname == 'm5.defines':
4147816Ssteve.reinhardt@amd.com            mod.__dict__['buildEnv'] = m5.util.SmartDict(build_env)
4157756SAli.Saidi@ARM.com            return mod
4167756SAli.Saidi@ARM.com
4177756SAli.Saidi@ARM.com        source = self.modules[fullname]
4187756SAli.Saidi@ARM.com        if source.modname == '__init__':
4197756SAli.Saidi@ARM.com            mod.__path__ = source.modpath
4207756SAli.Saidi@ARM.com        mod.__file__ = source.abspath
4217756SAli.Saidi@ARM.com
4227756SAli.Saidi@ARM.com        exec file(source.abspath, 'r') in mod.__dict__
4237816Ssteve.reinhardt@amd.com
4247816Ssteve.reinhardt@amd.com        return mod
4257816Ssteve.reinhardt@amd.com
4267816Ssteve.reinhardt@amd.comimport m5.SimObject
4277816Ssteve.reinhardt@amd.comimport m5.params
4287816Ssteve.reinhardt@amd.comfrom m5.util import code_formatter
4297816Ssteve.reinhardt@amd.com
4307816Ssteve.reinhardt@amd.comm5.SimObject.clear()
4317816Ssteve.reinhardt@amd.comm5.params.clear()
4327816Ssteve.reinhardt@amd.com
4337756SAli.Saidi@ARM.com# install the python importer so we can grab stuff from the source
4347816Ssteve.reinhardt@amd.com# tree itself.  We can't have SimObjects added after this point or
4357816Ssteve.reinhardt@amd.com# else we won't know about them for the rest of the stuff.
4367816Ssteve.reinhardt@amd.comimporter = DictImporter(PySource.modules)
4377816Ssteve.reinhardt@amd.comsys.meta_path[0:0] = [ importer ]
4387816Ssteve.reinhardt@amd.com
4397816Ssteve.reinhardt@amd.com# import all sim objects so we can populate the all_objects list
4407816Ssteve.reinhardt@amd.com# make sure that we're working with a list, then let's sort it
4417816Ssteve.reinhardt@amd.comfor modname in SimObject.modnames:
4427816Ssteve.reinhardt@amd.com    exec('from m5.objects import %s' % modname)
4437816Ssteve.reinhardt@amd.com
4447816Ssteve.reinhardt@amd.com# we need to unload all of the currently imported modules so that they
4457816Ssteve.reinhardt@amd.com# will be re-imported the next time the sconscript is run
4467816Ssteve.reinhardt@amd.comimporter.unload()
4477816Ssteve.reinhardt@amd.comsys.meta_path.remove(importer)
4487816Ssteve.reinhardt@amd.com
4497816Ssteve.reinhardt@amd.comsim_objects = m5.SimObject.allClasses
4507816Ssteve.reinhardt@amd.comall_enums = m5.params.allEnums
4517816Ssteve.reinhardt@amd.com
4527816Ssteve.reinhardt@amd.com# Find param types that need to be explicitly wrapped with swig.
4537816Ssteve.reinhardt@amd.com# These will be recognized because the ParamDesc will have a
4547816Ssteve.reinhardt@amd.com# swig_decl() method.  Most param types are based on types that don't
4557816Ssteve.reinhardt@amd.com# need this, either because they're based on native types (like Int)
4567816Ssteve.reinhardt@amd.com# or because they're SimObjects (which get swigged independently).
4577816Ssteve.reinhardt@amd.com# For now the only things handled here are VectorParam types.
4587816Ssteve.reinhardt@amd.comparams_to_swig = {}
4597816Ssteve.reinhardt@amd.comfor name,obj in sorted(sim_objects.iteritems()):
4607816Ssteve.reinhardt@amd.com    for param in obj._params.local.values():
4617816Ssteve.reinhardt@amd.com        # load the ptype attribute now because it depends on the
4627816Ssteve.reinhardt@amd.com        # current version of SimObject.allClasses, but when scons
4637816Ssteve.reinhardt@amd.com        # actually uses the value, all versions of
4647816Ssteve.reinhardt@amd.com        # SimObject.allClasses will have been loaded
4657816Ssteve.reinhardt@amd.com        param.ptype
4667816Ssteve.reinhardt@amd.com
4677816Ssteve.reinhardt@amd.com        if not hasattr(param, 'swig_decl'):
4687816Ssteve.reinhardt@amd.com            continue
4697816Ssteve.reinhardt@amd.com        pname = param.ptype_str
4707816Ssteve.reinhardt@amd.com        if pname not in params_to_swig:
4717816Ssteve.reinhardt@amd.com            params_to_swig[pname] = param
4727816Ssteve.reinhardt@amd.com
4737816Ssteve.reinhardt@amd.com########################################################################
4747816Ssteve.reinhardt@amd.com#
4757816Ssteve.reinhardt@amd.com# calculate extra dependencies
4767816Ssteve.reinhardt@amd.com#
4777816Ssteve.reinhardt@amd.commodule_depends = ["m5", "m5.SimObject", "m5.params"]
4787816Ssteve.reinhardt@amd.comdepends = [ PySource.modules[dep].snode for dep in module_depends ]
4797816Ssteve.reinhardt@amd.com
4807816Ssteve.reinhardt@amd.com########################################################################
4817816Ssteve.reinhardt@amd.com#
4827816Ssteve.reinhardt@amd.com# Commands for the basic automatically generated python files
4837816Ssteve.reinhardt@amd.com#
4847816Ssteve.reinhardt@amd.com
4857816Ssteve.reinhardt@amd.com# Generate Python file containing a dict specifying the current
4867816Ssteve.reinhardt@amd.com# buildEnv flags.
4877816Ssteve.reinhardt@amd.comdef makeDefinesPyFile(target, source, env):
4887816Ssteve.reinhardt@amd.com    build_env = source[0].get_contents()
4897816Ssteve.reinhardt@amd.com
4907816Ssteve.reinhardt@amd.com    code = code_formatter()
4917816Ssteve.reinhardt@amd.com    code("""
4927816Ssteve.reinhardt@amd.comimport m5.internal
4937816Ssteve.reinhardt@amd.comimport m5.util
4947816Ssteve.reinhardt@amd.com
4958947Sandreas.hansson@arm.combuildEnv = m5.util.SmartDict($build_env)
4968947Sandreas.hansson@arm.com
4977756SAli.Saidi@ARM.comcompileDate = m5.internal.core.compileDate
4988120Sgblack@eecs.umich.edu_globals = globals()
4997756SAli.Saidi@ARM.comfor key,val in m5.internal.core.__dict__.iteritems():
5007756SAli.Saidi@ARM.com    if key.startswith('flag_'):
5017756SAli.Saidi@ARM.com        flag = key[5:]
5027756SAli.Saidi@ARM.com        _globals[flag] = val
5037816Ssteve.reinhardt@amd.comdel _globals
5047816Ssteve.reinhardt@amd.com""")
5057816Ssteve.reinhardt@amd.com    code.write(target[0].abspath)
5067816Ssteve.reinhardt@amd.com
5077816Ssteve.reinhardt@amd.comdefines_info = Value(build_env)
5087816Ssteve.reinhardt@amd.com# Generate a file with all of the compile options in it
5097816Ssteve.reinhardt@amd.comenv.Command('python/m5/defines.py', defines_info,
5107816Ssteve.reinhardt@amd.com            MakeAction(makeDefinesPyFile, Transform("DEFINES", 0)))
5117816Ssteve.reinhardt@amd.comPySource('m5', 'python/m5/defines.py')
5127816Ssteve.reinhardt@amd.com
5137756SAli.Saidi@ARM.com# Generate python file containing info about the M5 source code
5147756SAli.Saidi@ARM.comdef makeInfoPyFile(target, source, env):
5159227Sandreas.hansson@arm.com    code = code_formatter()
5169227Sandreas.hansson@arm.com    for src in source:
5179227Sandreas.hansson@arm.com        data = ''.join(file(src.srcnode().abspath, 'r').xreadlines())
5189227Sandreas.hansson@arm.com        code('$src = ${{repr(data)}}')
5199590Sandreas@sandberg.pp.se    code.write(str(target[0]))
5209590Sandreas@sandberg.pp.se
5219590Sandreas@sandberg.pp.se# Generate a file that wraps the basic top level files
5229590Sandreas@sandberg.pp.seenv.Command('python/m5/info.py',
5239590Sandreas@sandberg.pp.se            [ '#/COPYING', '#/LICENSE', '#/README', ],
5249590Sandreas@sandberg.pp.se            MakeAction(makeInfoPyFile, Transform("INFO")))
5256654Snate@binkert.orgPySource('m5', 'python/m5/info.py')
5266654Snate@binkert.org
5275871Snate@binkert.org########################################################################
5286121Snate@binkert.org#
5298946Sandreas.hansson@arm.com# Create all of the SimObject param headers and enum headers
5309419Sandreas.hansson@arm.com#
5313940Ssaidi@eecs.umich.edu
5323918Ssaidi@eecs.umich.edudef createSimObjectParamStruct(target, source, env):
5333918Ssaidi@eecs.umich.edu    assert len(target) == 1 and len(source) == 1
5341858SN/A
5359556Sandreas.hansson@arm.com    name = str(source[0].get_contents())
5369556Sandreas.hansson@arm.com    obj = sim_objects[name]
5379556Sandreas.hansson@arm.com
5389556Sandreas.hansson@arm.com    code = code_formatter()
5399556Sandreas.hansson@arm.com    obj.cxx_param_decl(code)
5409556Sandreas.hansson@arm.com    code.write(target[0].abspath)
5419556Sandreas.hansson@arm.com
5429556Sandreas.hansson@arm.comdef createParamSwigWrapper(target, source, env):
5439556Sandreas.hansson@arm.com    assert len(target) == 1 and len(source) == 1
5449556Sandreas.hansson@arm.com
5459556Sandreas.hansson@arm.com    name = str(source[0].get_contents())
5469556Sandreas.hansson@arm.com    param = params_to_swig[name]
5479556Sandreas.hansson@arm.com
5489556Sandreas.hansson@arm.com    code = code_formatter()
5499556Sandreas.hansson@arm.com    param.swig_decl(code)
5509556Sandreas.hansson@arm.com    code.write(target[0].abspath)
5519556Sandreas.hansson@arm.com
5529556Sandreas.hansson@arm.comdef createEnumStrings(target, source, env):
5539556Sandreas.hansson@arm.com    assert len(target) == 1 and len(source) == 1
5549556Sandreas.hansson@arm.com
5559556Sandreas.hansson@arm.com    name = str(source[0].get_contents())
5569556Sandreas.hansson@arm.com    obj = all_enums[name]
5579556Sandreas.hansson@arm.com
5589556Sandreas.hansson@arm.com    code = code_formatter()
5599556Sandreas.hansson@arm.com    obj.cxx_def(code)
5609556Sandreas.hansson@arm.com    code.write(target[0].abspath)
5619556Sandreas.hansson@arm.com
5629556Sandreas.hansson@arm.comdef createEnumDecls(target, source, env):
5639556Sandreas.hansson@arm.com    assert len(target) == 1 and len(source) == 1
5649556Sandreas.hansson@arm.com
5659556Sandreas.hansson@arm.com    name = str(source[0].get_contents())
5669556Sandreas.hansson@arm.com    obj = all_enums[name]
5676121Snate@binkert.org
5689420Sandreas.hansson@arm.com    code = code_formatter()
5699420Sandreas.hansson@arm.com    obj.cxx_decl(code)
5709420Sandreas.hansson@arm.com    code.write(target[0].abspath)
5719420Sandreas.hansson@arm.com
5729420Sandreas.hansson@arm.comdef createEnumSwigWrapper(target, source, env):
5739420Sandreas.hansson@arm.com    assert len(target) == 1 and len(source) == 1
5749420Sandreas.hansson@arm.com
5759420Sandreas.hansson@arm.com    name = str(source[0].get_contents())
5769420Sandreas.hansson@arm.com    obj = all_enums[name]
5779420Sandreas.hansson@arm.com
5789420Sandreas.hansson@arm.com    code = code_formatter()
5797618SAli.Saidi@arm.com    obj.swig_decl(code)
5807618SAli.Saidi@arm.com    code.write(target[0].abspath)
5817618SAli.Saidi@arm.com
5827739Sgblack@eecs.umich.edudef createSimObjectSwigWrapper(target, source, env):
5839227Sandreas.hansson@arm.com    name = source[0].get_contents()
5849227Sandreas.hansson@arm.com    obj = sim_objects[name]
5859227Sandreas.hansson@arm.com
5869227Sandreas.hansson@arm.com    code = code_formatter()
5879227Sandreas.hansson@arm.com    obj.swig_decl(code)
5889227Sandreas.hansson@arm.com    code.write(target[0].abspath)
5899227Sandreas.hansson@arm.com
5909227Sandreas.hansson@arm.com# Generate all of the SimObject param C++ struct header files
5919227Sandreas.hansson@arm.comparams_hh_files = []
5929227Sandreas.hansson@arm.comfor name,simobj in sorted(sim_objects.iteritems()):
5939227Sandreas.hansson@arm.com    py_source = PySource.modules[simobj.__module__]
5949227Sandreas.hansson@arm.com    extra_deps = [ py_source.tnode ]
5959227Sandreas.hansson@arm.com
5969227Sandreas.hansson@arm.com    hh_file = File('params/%s.hh' % name)
5979227Sandreas.hansson@arm.com    params_hh_files.append(hh_file)
5989227Sandreas.hansson@arm.com    env.Command(hh_file, Value(name),
5999227Sandreas.hansson@arm.com                MakeAction(createSimObjectParamStruct, Transform("SO PARAM")))
6009227Sandreas.hansson@arm.com    env.Depends(hh_file, depends + extra_deps)
6019590Sandreas@sandberg.pp.se
6029590Sandreas@sandberg.pp.se# Generate any needed param SWIG wrapper files
6039590Sandreas@sandberg.pp.separams_i_files = []
6048737Skoansin.tan@gmail.comfor name,param in params_to_swig.iteritems():
6059420Sandreas.hansson@arm.com    i_file = File('python/m5/internal/%s.i' % (param.swig_module_name()))
6069420Sandreas.hansson@arm.com    params_i_files.append(i_file)
6079420Sandreas.hansson@arm.com    env.Command(i_file, Value(name),
6088737Skoansin.tan@gmail.com                MakeAction(createParamSwigWrapper, Transform("SW PARAM")))
6098737Skoansin.tan@gmail.com    env.Depends(i_file, depends)
6108737Skoansin.tan@gmail.com    SwigSource('m5.internal', i_file)
6118737Skoansin.tan@gmail.com
6128737Skoansin.tan@gmail.com# Generate all enum header files
6138737Skoansin.tan@gmail.comfor name,enum in sorted(all_enums.iteritems()):
6148737Skoansin.tan@gmail.com    py_source = PySource.modules[enum.__module__]
6158737Skoansin.tan@gmail.com    extra_deps = [ py_source.tnode ]
6168737Skoansin.tan@gmail.com
6178737Skoansin.tan@gmail.com    cc_file = File('enums/%s.cc' % name)
6188737Skoansin.tan@gmail.com    env.Command(cc_file, Value(name),
6198737Skoansin.tan@gmail.com                MakeAction(createEnumStrings, Transform("ENUM STR")))
6209556Sandreas.hansson@arm.com    env.Depends(cc_file, depends + extra_deps)
6219556Sandreas.hansson@arm.com    Source(cc_file)
6229556Sandreas.hansson@arm.com
6239556Sandreas.hansson@arm.com    hh_file = File('enums/%s.hh' % name)
6249556Sandreas.hansson@arm.com    env.Command(hh_file, Value(name),
6259556Sandreas.hansson@arm.com                MakeAction(createEnumDecls, Transform("ENUMDECL")))
6269556Sandreas.hansson@arm.com    env.Depends(hh_file, depends + extra_deps)
6279556Sandreas.hansson@arm.com
6289556Sandreas.hansson@arm.com    i_file = File('python/m5/internal/enum_%s.i' % name)
6299556Sandreas.hansson@arm.com    env.Command(i_file, Value(name),
6309590Sandreas@sandberg.pp.se                MakeAction(createEnumSwigWrapper, Transform("ENUMSWIG")))
6319590Sandreas@sandberg.pp.se    env.Depends(i_file, depends + extra_deps)
6329420Sandreas.hansson@arm.com    SwigSource('m5.internal', i_file)
6339846Sandreas.hansson@arm.com
6349846Sandreas.hansson@arm.com# Generate SimObject SWIG wrapper files
6359846Sandreas.hansson@arm.comfor name in sim_objects.iterkeys():
6369846Sandreas.hansson@arm.com    i_file = File('python/m5/internal/param_%s.i' % name)
6378946Sandreas.hansson@arm.com    env.Command(i_file, Value(name),
6383918Ssaidi@eecs.umich.edu                MakeAction(createSimObjectSwigWrapper, Transform("SO SWIG")))
6399068SAli.Saidi@ARM.com    env.Depends(i_file, depends)
6409068SAli.Saidi@ARM.com    SwigSource('m5.internal', i_file)
6419068SAli.Saidi@ARM.com
6429068SAli.Saidi@ARM.com# Generate the main swig init file
6439068SAli.Saidi@ARM.comdef makeEmbeddedSwigInit(target, source, env):
6449068SAli.Saidi@ARM.com    code = code_formatter()
6459068SAli.Saidi@ARM.com    module = source[0].get_contents()
6469068SAli.Saidi@ARM.com    code('''\
6479068SAli.Saidi@ARM.com#include "sim/init.hh"
6489419Sandreas.hansson@arm.com
6499068SAli.Saidi@ARM.comextern "C" {
6509068SAli.Saidi@ARM.com    void init_${module}();
6519068SAli.Saidi@ARM.com}
6529068SAli.Saidi@ARM.com
6539068SAli.Saidi@ARM.comEmbeddedSwig embed_swig_${module}(init_${module});
6549068SAli.Saidi@ARM.com''')
6553918Ssaidi@eecs.umich.edu    code.write(str(target[0]))
6563918Ssaidi@eecs.umich.edu    
6576157Snate@binkert.org# Build all swig modules
6586157Snate@binkert.orgfor swig in SwigSource.all:
6596157Snate@binkert.org    env.Command([swig.cc_source.tnode, swig.py_source.tnode], swig.tnode,
6606157Snate@binkert.org                MakeAction('$SWIG $SWIGFLAGS -outdir ${TARGETS[1].dir} '
6615397Ssaidi@eecs.umich.edu                '-o ${TARGETS[0]} $SOURCES', Transform("SWIG")))
6625397Ssaidi@eecs.umich.edu    cc_file = str(swig.tnode)
6636121Snate@binkert.org    init_file = '%s/%s_init.cc' % (dirname(cc_file), basename(cc_file))
6646121Snate@binkert.org    env.Command(init_file, Value(swig.module),
6656121Snate@binkert.org                MakeAction(makeEmbeddedSwigInit, Transform("EMBED SW")))
6666121Snate@binkert.org    Source(init_file, **swig.guards)
6676121Snate@binkert.org
6686121Snate@binkert.org#
6695397Ssaidi@eecs.umich.edu# Handle debug flags
6701851SN/A#
6711851SN/Adef makeDebugFlagCC(target, source, env):
6727739Sgblack@eecs.umich.edu    assert(len(target) == 1 and len(source) == 1)
673955SN/A
6749396Sandreas.hansson@arm.com    val = eval(source[0].get_contents())
6759396Sandreas.hansson@arm.com    name, compound, desc = val
6769396Sandreas.hansson@arm.com    compound = list(sorted(compound))
6779396Sandreas.hansson@arm.com
6789396Sandreas.hansson@arm.com    code = code_formatter()
6799396Sandreas.hansson@arm.com
6809396Sandreas.hansson@arm.com    # file header
6819396Sandreas.hansson@arm.com    code('''
6829396Sandreas.hansson@arm.com/*
6839396Sandreas.hansson@arm.com * DO NOT EDIT THIS FILE! Automatically generated
6849396Sandreas.hansson@arm.com */
6859396Sandreas.hansson@arm.com
6869396Sandreas.hansson@arm.com#include "base/debug.hh"
6879396Sandreas.hansson@arm.com''')
6889396Sandreas.hansson@arm.com
6899396Sandreas.hansson@arm.com    for flag in compound:
6909477Sandreas.hansson@arm.com        code('#include "debug/$flag.hh"')
6919477Sandreas.hansson@arm.com    code()
6929477Sandreas.hansson@arm.com    code('namespace Debug {')
6939477Sandreas.hansson@arm.com    code()
6949477Sandreas.hansson@arm.com
6959477Sandreas.hansson@arm.com    if not compound:
6969477Sandreas.hansson@arm.com        code('SimpleFlag $name("$name", "$desc");')
6979477Sandreas.hansson@arm.com    else:
6989477Sandreas.hansson@arm.com        code('CompoundFlag $name("$name", "$desc",')
6999477Sandreas.hansson@arm.com        code.indent()
7009477Sandreas.hansson@arm.com        last = len(compound) - 1
7019477Sandreas.hansson@arm.com        for i,flag in enumerate(compound):
7029477Sandreas.hansson@arm.com            if i != last:
7039477Sandreas.hansson@arm.com                code('$flag,')
7049477Sandreas.hansson@arm.com            else:
7059477Sandreas.hansson@arm.com                code('$flag);')
7069477Sandreas.hansson@arm.com        code.dedent()
7079477Sandreas.hansson@arm.com
7089477Sandreas.hansson@arm.com    code()
7099477Sandreas.hansson@arm.com    code('} // namespace Debug')
7109477Sandreas.hansson@arm.com
7119477Sandreas.hansson@arm.com    code.write(str(target[0]))
7129396Sandreas.hansson@arm.com
7133053Sstever@eecs.umich.edudef makeDebugFlagHH(target, source, env):
7146121Snate@binkert.org    assert(len(target) == 1 and len(source) == 1)
7153053Sstever@eecs.umich.edu
7163053Sstever@eecs.umich.edu    val = eval(source[0].get_contents())
7173053Sstever@eecs.umich.edu    name, compound, desc = val
7183053Sstever@eecs.umich.edu
7193053Sstever@eecs.umich.edu    code = code_formatter()
7209072Sandreas.hansson@arm.com
7213053Sstever@eecs.umich.edu    # file header boilerplate
7224742Sstever@eecs.umich.edu    code('''\
7234742Sstever@eecs.umich.edu/*
7243053Sstever@eecs.umich.edu * DO NOT EDIT THIS FILE!
7253053Sstever@eecs.umich.edu *
7263053Sstever@eecs.umich.edu * Automatically generated by SCons
7278960Ssteve.reinhardt@amd.com */
7286654Snate@binkert.org
7293053Sstever@eecs.umich.edu#ifndef __DEBUG_${name}_HH__
7303053Sstever@eecs.umich.edu#define __DEBUG_${name}_HH__
7313053Sstever@eecs.umich.edu
7323053Sstever@eecs.umich.edunamespace Debug {
7339877Sandreas.hansson@arm.com''')
7349877Sandreas.hansson@arm.com
7359877Sandreas.hansson@arm.com    if compound:
7369877Sandreas.hansson@arm.com        code('class CompoundFlag;')
7379877Sandreas.hansson@arm.com    code('class SimpleFlag;')
7389585Sandreas@sandberg.pp.se
7399877Sandreas.hansson@arm.com    if compound:
7409585Sandreas@sandberg.pp.se        code('extern CompoundFlag $name;')
7419877Sandreas.hansson@arm.com        for flag in compound:
7429877Sandreas.hansson@arm.com            code('extern SimpleFlag $flag;')
7439585Sandreas@sandberg.pp.se    else:
7442667Sstever@eecs.umich.edu        code('extern SimpleFlag $name;')
7454554Sbinkertn@umich.edu
7466121Snate@binkert.org    code('''
7472667Sstever@eecs.umich.edu}
7484554Sbinkertn@umich.edu
7494554Sbinkertn@umich.edu#endif // __DEBUG_${name}_HH__
7504554Sbinkertn@umich.edu''')
7516121Snate@binkert.org
7524554Sbinkertn@umich.edu    code.write(str(target[0]))
7534554Sbinkertn@umich.edu
7544554Sbinkertn@umich.edufor name,flag in sorted(debug_flags.iteritems()):
7554781Snate@binkert.org    n, compound, desc = flag
7564554Sbinkertn@umich.edu    assert n == name
7574554Sbinkertn@umich.edu
7582667Sstever@eecs.umich.edu    env.Command('debug/%s.hh' % name, Value(flag),
7594554Sbinkertn@umich.edu                MakeAction(makeDebugFlagHH, Transform("TRACING", 0)))
7604554Sbinkertn@umich.edu    env.Command('debug/%s.cc' % name, Value(flag),
7614554Sbinkertn@umich.edu                MakeAction(makeDebugFlagCC, Transform("TRACING", 0)))
7624554Sbinkertn@umich.edu    Source('debug/%s.cc' % name)
7632667Sstever@eecs.umich.edu
7644554Sbinkertn@umich.edu# Embed python files.  All .py files that have been indicated by a
7652667Sstever@eecs.umich.edu# PySource() call in a SConscript need to be embedded into the M5
7664554Sbinkertn@umich.edu# library.  To do that, we compile the file to byte code, marshal the
7676121Snate@binkert.org# byte code, compress it, and then generate a c++ file that
7682667Sstever@eecs.umich.edu# inserts the result into an array.
7695522Snate@binkert.orgdef embedPyFile(target, source, env):
7705522Snate@binkert.org    def c_str(string):
7715522Snate@binkert.org        if string is None:
7725522Snate@binkert.org            return "0"
7735522Snate@binkert.org        return '"%s"' % string
7745522Snate@binkert.org
7755522Snate@binkert.org    '''Action function to compile a .py into a code object, marshal
7765522Snate@binkert.org    it, compress it, and stick it into an asm file so the code appears
7775522Snate@binkert.org    as just bytes with a label in the data section'''
7785522Snate@binkert.org
7795522Snate@binkert.org    src = file(str(source[0]), 'r').read()
7805522Snate@binkert.org
7815522Snate@binkert.org    pysource = PySource.tnodes[source[0]]
7825522Snate@binkert.org    compiled = compile(src, pysource.abspath, 'exec')
7835522Snate@binkert.org    marshalled = marshal.dumps(compiled)
7845522Snate@binkert.org    compressed = zlib.compress(marshalled)
7855522Snate@binkert.org    data = compressed
7865522Snate@binkert.org    sym = pysource.symname
7875522Snate@binkert.org
7885522Snate@binkert.org    code = code_formatter()
7895522Snate@binkert.org    code('''\
7905522Snate@binkert.org#include "sim/init.hh"
7915522Snate@binkert.org
7925522Snate@binkert.orgnamespace {
7935522Snate@binkert.org
7945522Snate@binkert.orgconst char data_${sym}[] = {
7952638Sstever@eecs.umich.edu''')
7962638Sstever@eecs.umich.edu    code.indent()
7976121Snate@binkert.org    step = 16
7983716Sstever@eecs.umich.edu    for i in xrange(0, len(data), step):
7995522Snate@binkert.org        x = array.array('B', data[i:i+step])
8009420Sandreas.hansson@arm.com        code(''.join('%d,' % d for d in x))
8015522Snate@binkert.org    code.dedent()
8025522Snate@binkert.org    
8035522Snate@binkert.org    code('''};
8045522Snate@binkert.org
8051858SN/AEmbeddedPython embedded_${sym}(
8065227Ssaidi@eecs.umich.edu    ${{c_str(pysource.arcname)}},
8075227Ssaidi@eecs.umich.edu    ${{c_str(pysource.abspath)}},
8085227Ssaidi@eecs.umich.edu    ${{c_str(pysource.modpath)}},
8095227Ssaidi@eecs.umich.edu    data_${sym},
8106654Snate@binkert.org    ${{len(data)}},
8116654Snate@binkert.org    ${{len(marshalled)}});
8127769SAli.Saidi@ARM.com
8137769SAli.Saidi@ARM.com} // anonymous namespace
8147769SAli.Saidi@ARM.com''')
8157769SAli.Saidi@ARM.com    code.write(str(target[0]))
8165227Ssaidi@eecs.umich.edu
8175227Ssaidi@eecs.umich.edufor source in PySource.all:
8185227Ssaidi@eecs.umich.edu    env.Command(source.cpp, source.tnode, 
8195204Sstever@gmail.com                MakeAction(embedPyFile, Transform("EMBED PY")))
8205204Sstever@gmail.com    Source(source.cpp)
8215204Sstever@gmail.com
8225204Sstever@gmail.com########################################################################
8235204Sstever@gmail.com#
8245204Sstever@gmail.com# Define binaries.  Each different build type (debug, opt, etc.) gets
8255204Sstever@gmail.com# a slightly different build environment.
8265204Sstever@gmail.com#
8275204Sstever@gmail.com
8285204Sstever@gmail.com# List of constructed environments to pass back to SConstruct
8295204Sstever@gmail.comenvList = []
8305204Sstever@gmail.com
8315204Sstever@gmail.comdate_source = Source('base/date.cc', skip_lib=True)
8325204Sstever@gmail.com
8335204Sstever@gmail.com# Function to create a new build environment as clone of current
8345204Sstever@gmail.com# environment 'env' with modified object suffix and optional stripped
8355204Sstever@gmail.com# binary.  Additional keyword arguments are appended to corresponding
8366121Snate@binkert.org# build environment vars.
8375204Sstever@gmail.comdef makeEnv(label, objsfx, strip = False, **kwargs):
8387727SAli.Saidi@ARM.com    # SCons doesn't know to append a library suffix when there is a '.' in the
8397727SAli.Saidi@ARM.com    # name.  Use '_' instead.
8407727SAli.Saidi@ARM.com    libname = 'gem5_' + label
8417727SAli.Saidi@ARM.com    exename = 'gem5.' + label
8427727SAli.Saidi@ARM.com    secondary_exename = 'm5.' + label
8439812Sandreas.hansson@arm.com
8449812Sandreas.hansson@arm.com    new_env = env.Clone(OBJSUFFIX=objsfx, SHOBJSUFFIX=objsfx + 's')
8459812Sandreas.hansson@arm.com    new_env.Label = label
8469812Sandreas.hansson@arm.com    new_env.Append(**kwargs)
8479812Sandreas.hansson@arm.com
8489812Sandreas.hansson@arm.com    swig_env = new_env.Clone()
8499812Sandreas.hansson@arm.com    swig_env.Append(CCFLAGS='-Werror')
8509812Sandreas.hansson@arm.com    if env['GCC']:
8519812Sandreas.hansson@arm.com        swig_env.Append(CCFLAGS='-Wno-uninitialized')
8529812Sandreas.hansson@arm.com        swig_env.Append(CCFLAGS='-Wno-sign-compare')
8539812Sandreas.hansson@arm.com        swig_env.Append(CCFLAGS='-Wno-parentheses')
8549812Sandreas.hansson@arm.com        swig_env.Append(CCFLAGS='-Wno-unused-label')
8559812Sandreas.hansson@arm.com        swig_env.Append(CCFLAGS='-Wno-unused-but-set-variable')
8569812Sandreas.hansson@arm.com
8579812Sandreas.hansson@arm.com    werror_env = new_env.Clone()
8589812Sandreas.hansson@arm.com    werror_env.Append(CCFLAGS='-Werror')
8599812Sandreas.hansson@arm.com
8609812Sandreas.hansson@arm.com    def make_obj(source, static, extra_deps = None):
8619812Sandreas.hansson@arm.com        '''This function adds the specified source to the correct
8629812Sandreas.hansson@arm.com        build environment, and returns the corresponding SCons Object
8639812Sandreas.hansson@arm.com        nodes'''
8649812Sandreas.hansson@arm.com
8659812Sandreas.hansson@arm.com        if source.swig:
8669812Sandreas.hansson@arm.com            env = swig_env
8677727SAli.Saidi@ARM.com        elif source.Werror:
8685863Snate@binkert.org            env = werror_env
8693118Sstever@eecs.umich.edu        else:
8705863Snate@binkert.org            env = new_env
8719239Sandreas.hansson@arm.com
8723118Sstever@eecs.umich.edu        if static:
8733118Sstever@eecs.umich.edu            obj = env.StaticObject(source.tnode)
8745863Snate@binkert.org        else:
8755863Snate@binkert.org            obj = env.SharedObject(source.tnode)
8765863Snate@binkert.org
8775863Snate@binkert.org        if extra_deps:
8783118Sstever@eecs.umich.edu            env.Depends(obj, extra_deps)
8793483Ssaidi@eecs.umich.edu
8803494Ssaidi@eecs.umich.edu        return obj
8813494Ssaidi@eecs.umich.edu
8823483Ssaidi@eecs.umich.edu    static_objs = \
8833483Ssaidi@eecs.umich.edu        [ make_obj(s, True) for s in Source.get(main=False, skip_lib=False) ]
8843483Ssaidi@eecs.umich.edu    shared_objs = \
8853053Sstever@eecs.umich.edu        [ make_obj(s, False) for s in Source.get(main=False, skip_lib=False) ]
8863053Sstever@eecs.umich.edu
8873918Ssaidi@eecs.umich.edu    static_date = make_obj(date_source, static=True, extra_deps=static_objs)
8883053Sstever@eecs.umich.edu    static_objs.append(static_date)
8893053Sstever@eecs.umich.edu    
8903053Sstever@eecs.umich.edu    shared_date = make_obj(date_source, static=False, extra_deps=shared_objs)
8913053Sstever@eecs.umich.edu    shared_objs.append(shared_date)
8923053Sstever@eecs.umich.edu
8939396Sandreas.hansson@arm.com    # First make a library of everything but main() so other programs can
8949396Sandreas.hansson@arm.com    # link against m5.
8959396Sandreas.hansson@arm.com    static_lib = new_env.StaticLibrary(libname, static_objs)
8969396Sandreas.hansson@arm.com    shared_lib = new_env.SharedLibrary(libname, shared_objs)
8979396Sandreas.hansson@arm.com
8989396Sandreas.hansson@arm.com    # Now link a stub with main() and the static library.
8999396Sandreas.hansson@arm.com    main_objs = [ make_obj(s, True) for s in Source.get(main=True) ]
9009396Sandreas.hansson@arm.com
9019396Sandreas.hansson@arm.com    for test in UnitTest.all:
9029477Sandreas.hansson@arm.com        flags = { test.target : True }
9039396Sandreas.hansson@arm.com        test_sources = Source.get(**flags)
9049477Sandreas.hansson@arm.com        test_objs = [ make_obj(s, static=True) for s in test_sources ]
9059477Sandreas.hansson@arm.com        testname = "unittest/%s.%s" % (test.target, label)
9069477Sandreas.hansson@arm.com        new_env.Program(testname, main_objs + test_objs + static_objs)
9079477Sandreas.hansson@arm.com
9089396Sandreas.hansson@arm.com    progname = exename
9097840Snate@binkert.org    if strip:
9107865Sgblack@eecs.umich.edu        progname += '.unstripped'
9117865Sgblack@eecs.umich.edu
9127865Sgblack@eecs.umich.edu    targets = new_env.Program(progname, main_objs + static_objs)
9137865Sgblack@eecs.umich.edu
9147865Sgblack@eecs.umich.edu    if strip:
9157840Snate@binkert.org        if sys.platform == 'sunos5':
9169900Sandreas@sandberg.pp.se            cmd = 'cp $SOURCE $TARGET; strip $TARGET'
9179900Sandreas@sandberg.pp.se        else:
9189900Sandreas@sandberg.pp.se            cmd = 'strip $SOURCE -o $TARGET'
9199900Sandreas@sandberg.pp.se        targets = new_env.Command(exename, progname,
9209591Sandreas@sandberg.pp.se                    MakeAction(cmd, Transform("STRIP")))
9219591Sandreas@sandberg.pp.se
9229591Sandreas@sandberg.pp.se    new_env.Command(secondary_exename, exename,
9239590Sandreas@sandberg.pp.se            MakeAction('ln $SOURCE $TARGET', Transform("HARDLINK")))
9249590Sandreas@sandberg.pp.se
9259045SAli.Saidi@ARM.com    new_env.M5Binary = targets[0]
9269045SAli.Saidi@ARM.com    envList.append(new_env)
9279071Sandreas.hansson@arm.com
9289071Sandreas.hansson@arm.com# Debug binary
9299045SAli.Saidi@ARM.comccflags = {}
9307840Snate@binkert.orgif env['GCC']:
9317840Snate@binkert.org    if sys.platform == 'sunos5':
9327840Snate@binkert.org        ccflags['debug'] = '-gstabs+'
9331858SN/A    else:
9341858SN/A        ccflags['debug'] = '-ggdb3'
9351858SN/A    ccflags['opt'] = '-g -O3'
9361858SN/A    ccflags['fast'] = '-O3'
9371858SN/A    ccflags['prof'] = '-O3 -g -pg'
9381858SN/Aelif env['SUNCC']:
9399903Sandreas.hansson@arm.com    ccflags['debug'] = '-g0'
9409903Sandreas.hansson@arm.com    ccflags['opt'] = '-g -O'
9419903Sandreas.hansson@arm.com    ccflags['fast'] = '-fast'
9429903Sandreas.hansson@arm.com    ccflags['prof'] = '-fast -g -pg'
9439903Sandreas.hansson@arm.comelif env['ICC']:
9449903Sandreas.hansson@arm.com    ccflags['debug'] = '-g -O0'
9459651SAndreas.Sandberg@ARM.com    ccflags['opt'] = '-g -O'
9469903Sandreas.hansson@arm.com    ccflags['fast'] = '-fast'
9479651SAndreas.Sandberg@ARM.com    ccflags['prof'] = '-fast -g -pg'
9489651SAndreas.Sandberg@ARM.comelse:
9499651SAndreas.Sandberg@ARM.com    print 'Unknown compiler, please fix compiler options'
9509651SAndreas.Sandberg@ARM.com    Exit(1)
9519651SAndreas.Sandberg@ARM.com
9529657Sandreas.sandberg@arm.commakeEnv('debug', '.do',
9539883Sandreas@sandberg.pp.se        CCFLAGS = Split(ccflags['debug']),
9549651SAndreas.Sandberg@ARM.com        CPPDEFINES = ['DEBUG', 'TRACING_ON=1'])
9559651SAndreas.Sandberg@ARM.com
9569651SAndreas.Sandberg@ARM.com# Optimized binary
9579651SAndreas.Sandberg@ARM.commakeEnv('opt', '.o',
9589651SAndreas.Sandberg@ARM.com        CCFLAGS = Split(ccflags['opt']),
9599651SAndreas.Sandberg@ARM.com        CPPDEFINES = ['TRACING_ON=1'])
9609651SAndreas.Sandberg@ARM.com
9619651SAndreas.Sandberg@ARM.com# "Fast" binary
9629651SAndreas.Sandberg@ARM.commakeEnv('fast', '.fo', strip = True,
9639651SAndreas.Sandberg@ARM.com        CCFLAGS = Split(ccflags['fast']),
9649651SAndreas.Sandberg@ARM.com        CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'])
9655863Snate@binkert.org
9665863Snate@binkert.org# Profiled binary
9675863Snate@binkert.orgmakeEnv('prof', '.po',
9685863Snate@binkert.org        CCFLAGS = Split(ccflags['prof']),
9696121Snate@binkert.org        CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
9701858SN/A        LINKFLAGS = '-pg')
9715863Snate@binkert.org
9725863Snate@binkert.orgReturn('envList')
9735863Snate@binkert.org