SConscript revision 8492
1955SN/A# -*- mode:python -*-
2955SN/A
31762SN/A# Copyright (c) 2004-2005 The Regents of The University of Michigan
4955SN/A# All rights reserved.
5955SN/A#
6955SN/A# Redistribution and use in source and binary forms, with or without
7955SN/A# modification, are permitted provided that the following conditions are
8955SN/A# met: redistributions of source code must retain the above copyright
9955SN/A# notice, this list of conditions and the following disclaimer;
10955SN/A# redistributions in binary form must reproduce the above copyright
11955SN/A# notice, this list of conditions and the following disclaimer in the
12955SN/A# documentation and/or other materials provided with the distribution;
13955SN/A# neither the name of the copyright holders nor the names of its
14955SN/A# contributors may be used to endorse or promote products derived from
15955SN/A# this software without specific prior written permission.
16955SN/A#
17955SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18955SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19955SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20955SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21955SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22955SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23955SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24955SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25955SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26955SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27955SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
282665Ssaidi@eecs.umich.edu#
294762Snate@binkert.org# Authors: Nathan Binkert
30955SN/A
314762Snate@binkert.orgimport array
32955SN/Aimport bisect
33955SN/Aimport imp
344202Sbinkertn@umich.eduimport marshal
354382Sbinkertn@umich.eduimport os
364202Sbinkertn@umich.eduimport re
374762Snate@binkert.orgimport sys
384762Snate@binkert.orgimport zlib
394762Snate@binkert.org
40955SN/Afrom os.path import basename, dirname, exists, isdir, isfile, join as joinpath
414381Sbinkertn@umich.edu
424381Sbinkertn@umich.eduimport SCons
43955SN/A
44955SN/A# This file defines how to build a particular configuration of gem5
45955SN/A# based on variable settings in the 'env' build environment.
464202Sbinkertn@umich.edu
47955SN/AImport('*')
484382Sbinkertn@umich.edu
494382Sbinkertn@umich.edu# Children need to see the environment
504382Sbinkertn@umich.eduExport('env')
514762Snate@binkert.org
524762Snate@binkert.orgbuild_env = [(opt, env[opt]) for opt in export_vars]
534762Snate@binkert.org
544762Snate@binkert.orgfrom m5.util import code_formatter
554762Snate@binkert.org
564762Snate@binkert.org########################################################################
574762Snate@binkert.org# Code for adding source files of various types
584762Snate@binkert.org#
594762Snate@binkert.org# When specifying a source file of some type, a set of guards can be
604762Snate@binkert.org# specified for that file.  When get() is used to find the files, if
614762Snate@binkert.org# get specifies a set of filters, only files that match those filters
624762Snate@binkert.org# will be accepted (unspecified filters on files are assumed to be
634762Snate@binkert.org# false).  Current filters are:
644762Snate@binkert.org#     main -- specifies the gem5 main() function
654762Snate@binkert.org#     skip_lib -- do not put this file into the gem5 library
664762Snate@binkert.org#     <unittest> -- unit tests use filters based on the unit test name
674762Snate@binkert.org#
684762Snate@binkert.org# A parent can now be specified for a source file and default filter
694762Snate@binkert.org# values will be retrieved recursively from parents (children override
704762Snate@binkert.org# parents).
714762Snate@binkert.org#
724762Snate@binkert.orgclass SourceMeta(type):
734762Snate@binkert.org    '''Meta class for source files that keeps track of all files of a
744762Snate@binkert.org    particular type and has a get function for finding all functions
754762Snate@binkert.org    of a certain type that match a set of guards'''
764762Snate@binkert.org    def __init__(cls, name, bases, dict):
774762Snate@binkert.org        super(SourceMeta, cls).__init__(name, bases, dict)
784762Snate@binkert.org        cls.all = []
794762Snate@binkert.org        
804762Snate@binkert.org    def get(cls, **guards):
814762Snate@binkert.org        '''Find all files that match the specified guards.  If a source
824762Snate@binkert.org        file does not specify a flag, the default is False'''
834762Snate@binkert.org        for src in cls.all:
844382Sbinkertn@umich.edu            for flag,value in guards.iteritems():
854762Snate@binkert.org                # if the flag is found and has a different value, skip
864382Sbinkertn@umich.edu                # this file
874762Snate@binkert.org                if src.all_guards.get(flag, False) != value:
884381Sbinkertn@umich.edu                    break
894762Snate@binkert.org            else:
904762Snate@binkert.org                yield src
914762Snate@binkert.org
924762Snate@binkert.orgclass SourceFile(object):
934762Snate@binkert.org    '''Base object that encapsulates the notion of a source file.
944762Snate@binkert.org    This includes, the source node, target node, various manipulations
954762Snate@binkert.org    of those.  A source file also specifies a set of guards which
964762Snate@binkert.org    describing which builds the source file applies to.  A parent can
974762Snate@binkert.org    also be specified to get default guards from'''
984762Snate@binkert.org    __metaclass__ = SourceMeta
994762Snate@binkert.org    def __init__(self, source, parent=None, **guards):
1004762Snate@binkert.org        self.guards = guards
1014762Snate@binkert.org        self.parent = parent
1024762Snate@binkert.org
1034762Snate@binkert.org        tnode = source
1044762Snate@binkert.org        if not isinstance(source, SCons.Node.FS.File):
1054762Snate@binkert.org            tnode = File(source)
1064762Snate@binkert.org
1074762Snate@binkert.org        self.tnode = tnode
1084762Snate@binkert.org        self.snode = tnode.srcnode()
1094762Snate@binkert.org
1104762Snate@binkert.org        for base in type(self).__mro__:
1114762Snate@binkert.org            if issubclass(base, SourceFile):
1124762Snate@binkert.org                base.all.append(self)
1134762Snate@binkert.org
1144762Snate@binkert.org    @property
1154762Snate@binkert.org    def filename(self):
1164762Snate@binkert.org        return str(self.tnode)
1174762Snate@binkert.org
1184762Snate@binkert.org    @property
1194762Snate@binkert.org    def dirname(self):
1204762Snate@binkert.org        return dirname(self.filename)
1214762Snate@binkert.org
1224762Snate@binkert.org    @property
1234762Snate@binkert.org    def basename(self):
1244762Snate@binkert.org        return basename(self.filename)
1254762Snate@binkert.org
1264762Snate@binkert.org    @property
1274762Snate@binkert.org    def extname(self):
1284762Snate@binkert.org        index = self.basename.rfind('.')
129955SN/A        if index <= 0:
1304382Sbinkertn@umich.edu            # dot files aren't extensions
1314202Sbinkertn@umich.edu            return self.basename, None
1324382Sbinkertn@umich.edu
1334382Sbinkertn@umich.edu        return self.basename[:index], self.basename[index+1:]
1344382Sbinkertn@umich.edu
1354382Sbinkertn@umich.edu    @property
1364382Sbinkertn@umich.edu    def all_guards(self):
1374382Sbinkertn@umich.edu        '''find all guards for this object getting default values
1384382Sbinkertn@umich.edu        recursively from its parents'''
1394382Sbinkertn@umich.edu        guards = {}
1404382Sbinkertn@umich.edu        if self.parent:
1412667Sstever@eecs.umich.edu            guards.update(self.parent.guards)
1422667Sstever@eecs.umich.edu        guards.update(self.guards)
1432667Sstever@eecs.umich.edu        return guards
1442667Sstever@eecs.umich.edu
1452667Sstever@eecs.umich.edu    def __lt__(self, other): return self.filename < other.filename
1462667Sstever@eecs.umich.edu    def __le__(self, other): return self.filename <= other.filename
1472037SN/A    def __gt__(self, other): return self.filename > other.filename
1482037SN/A    def __ge__(self, other): return self.filename >= other.filename
1492037SN/A    def __eq__(self, other): return self.filename == other.filename
1504382Sbinkertn@umich.edu    def __ne__(self, other): return self.filename != other.filename
1514762Snate@binkert.org        
1524202Sbinkertn@umich.educlass Source(SourceFile):
1534382Sbinkertn@umich.edu    '''Add a c/c++ source file to the build'''
1544202Sbinkertn@umich.edu    def __init__(self, source, Werror=True, swig=False, **guards):
1554202Sbinkertn@umich.edu        '''specify the source file, and any guards'''
1564202Sbinkertn@umich.edu        super(Source, self).__init__(source, **guards)
1574202Sbinkertn@umich.edu
1584202Sbinkertn@umich.edu        self.Werror = Werror
1594762Snate@binkert.org        self.swig = swig
1604202Sbinkertn@umich.edu
1614202Sbinkertn@umich.educlass PySource(SourceFile):
1624202Sbinkertn@umich.edu    '''Add a python source file to the named package'''
1634202Sbinkertn@umich.edu    invalid_sym_char = re.compile('[^A-z0-9_]')
1644202Sbinkertn@umich.edu    modules = {}
1651858SN/A    tnodes = {}
1665068Sgblack@eecs.umich.edu    symnames = {}
1675068Sgblack@eecs.umich.edu    
1685068Sgblack@eecs.umich.edu    def __init__(self, package, source, **guards):
1695068Sgblack@eecs.umich.edu        '''specify the python package, the source file, and any guards'''
1705068Sgblack@eecs.umich.edu        super(PySource, self).__init__(source, **guards)
1715068Sgblack@eecs.umich.edu
1725068Sgblack@eecs.umich.edu        modname,ext = self.extname
1735068Sgblack@eecs.umich.edu        assert ext == 'py'
1745068Sgblack@eecs.umich.edu
1755068Sgblack@eecs.umich.edu        if package:
1765068Sgblack@eecs.umich.edu            path = package.split('.')
1774773Snate@binkert.org        else:
1781858SN/A            path = []
1791858SN/A
1801085SN/A        modpath = path[:]
1814382Sbinkertn@umich.edu        if modname != '__init__':
1824382Sbinkertn@umich.edu            modpath += [ modname ]
1834762Snate@binkert.org        modpath = '.'.join(modpath)
1844762Snate@binkert.org
1854762Snate@binkert.org        arcpath = path + [ self.basename ]
1864762Snate@binkert.org        abspath = self.snode.abspath
1874762Snate@binkert.org        if not exists(abspath):
1884762Snate@binkert.org            abspath = self.tnode.abspath
1894762Snate@binkert.org
1904762Snate@binkert.org        self.package = package
1914762Snate@binkert.org        self.modname = modname
1924762Snate@binkert.org        self.modpath = modpath
1934762Snate@binkert.org        self.arcname = joinpath(*arcpath)
1944762Snate@binkert.org        self.abspath = abspath
1954762Snate@binkert.org        self.compiled = File(self.filename + 'c')
1964762Snate@binkert.org        self.cpp = File(self.filename + '.cc')
1974762Snate@binkert.org        self.symname = PySource.invalid_sym_char.sub('_', modpath)
1984762Snate@binkert.org
1994762Snate@binkert.org        PySource.modules[modpath] = self
2004762Snate@binkert.org        PySource.tnodes[self.tnode] = self
2014762Snate@binkert.org        PySource.symnames[self.symname] = self
2024762Snate@binkert.org
2034762Snate@binkert.orgclass SimObject(PySource):
2044762Snate@binkert.org    '''Add a SimObject python file as a python source object and add
2054762Snate@binkert.org    it to a list of sim object modules'''
2064762Snate@binkert.org
2074762Snate@binkert.org    fixed = False
2084762Snate@binkert.org    modnames = []
2094762Snate@binkert.org
2104762Snate@binkert.org    def __init__(self, source, **guards):
2114762Snate@binkert.org        '''Specify the source file and any guards (automatically in
2124762Snate@binkert.org        the m5.objects package)'''
2134762Snate@binkert.org        super(SimObject, self).__init__('m5.objects', source, **guards)
2144762Snate@binkert.org        if self.fixed:
2154762Snate@binkert.org            raise AttributeError, "Too late to call SimObject now."
2164762Snate@binkert.org
2174762Snate@binkert.org        bisect.insort_right(SimObject.modnames, self.modname)
2184382Sbinkertn@umich.edu
2194382Sbinkertn@umich.educlass SwigSource(SourceFile):
2204762Snate@binkert.org    '''Add a swig file to build'''
2214762Snate@binkert.org
2224762Snate@binkert.org    def __init__(self, package, source, **guards):
2234382Sbinkertn@umich.edu        '''Specify the python package, the source file, and any guards'''
2244382Sbinkertn@umich.edu        super(SwigSource, self).__init__(source, **guards)
2254762Snate@binkert.org
2264382Sbinkertn@umich.edu        modname,ext = self.extname
2274382Sbinkertn@umich.edu        assert ext == 'i'
2284762Snate@binkert.org
2294382Sbinkertn@umich.edu        self.module = modname
2304382Sbinkertn@umich.edu        cc_file = joinpath(self.dirname, modname + '_wrap.cc')
2314762Snate@binkert.org        py_file = joinpath(self.dirname, modname + '.py')
2324382Sbinkertn@umich.edu
2334762Snate@binkert.org        self.cc_source = Source(cc_file, swig=True, parent=self)
2344762Snate@binkert.org        self.py_source = PySource(package, py_file, parent=self)
2354382Sbinkertn@umich.edu
2364382Sbinkertn@umich.educlass UnitTest(object):
2374762Snate@binkert.org    '''Create a UnitTest'''
2384762Snate@binkert.org
2394762Snate@binkert.org    all = []
2404762Snate@binkert.org    def __init__(self, target, *sources):
2414762Snate@binkert.org        '''Specify the target name and any sources.  Sources that are
2424762Snate@binkert.org        not SourceFiles are evalued with Source().  All files are
2434762Snate@binkert.org        guarded with a guard of the same name as the UnitTest
2444762Snate@binkert.org        target.'''
2454762Snate@binkert.org
2464762Snate@binkert.org        srcs = []
2474762Snate@binkert.org        for src in sources:
2484762Snate@binkert.org            if not isinstance(src, SourceFile):
2494762Snate@binkert.org                src = Source(src, skip_lib=True)
2504762Snate@binkert.org            src.guards[target] = True
2514762Snate@binkert.org            srcs.append(src)
2524762Snate@binkert.org
2534762Snate@binkert.org        self.sources = srcs
2544762Snate@binkert.org        self.target = target
2554762Snate@binkert.org        UnitTest.all.append(self)
2564762Snate@binkert.org
2574762Snate@binkert.org# Children should have access
2584762Snate@binkert.orgExport('Source')
2594762Snate@binkert.orgExport('PySource')
2604762Snate@binkert.orgExport('SimObject')
2614762Snate@binkert.orgExport('SwigSource')
2624762Snate@binkert.orgExport('UnitTest')
2634762Snate@binkert.org
2644762Snate@binkert.org########################################################################
2654762Snate@binkert.org#
2664762Snate@binkert.org# Debug Flags
2674762Snate@binkert.org#
2684762Snate@binkert.orgdebug_flags = {}
2694762Snate@binkert.orgdef DebugFlag(name, desc=None):
2704762Snate@binkert.org    if name in debug_flags:
2714762Snate@binkert.org        raise AttributeError, "Flag %s already specified" % name
2724762Snate@binkert.org    debug_flags[name] = (name, (), desc)
2734762Snate@binkert.org
2744762Snate@binkert.orgdef CompoundFlag(name, flags, desc=None):
2754762Snate@binkert.org    if name in debug_flags:
2764762Snate@binkert.org        raise AttributeError, "Flag %s already specified" % name
2774762Snate@binkert.org
2784762Snate@binkert.org    compound = tuple(flags)
2794762Snate@binkert.org    debug_flags[name] = (name, compound, desc)
2804762Snate@binkert.org
2814762Snate@binkert.orgExport('DebugFlag')
2824762Snate@binkert.orgExport('CompoundFlag')
2834762Snate@binkert.org
2844762Snate@binkert.org########################################################################
2854762Snate@binkert.org#
2864382Sbinkertn@umich.edu# Set some compiler variables
2874762Snate@binkert.org#
2884382Sbinkertn@umich.edu
2894762Snate@binkert.org# Include file paths are rooted in this directory.  SCons will
2904382Sbinkertn@umich.edu# automatically expand '.' to refer to both the source directory and
2914762Snate@binkert.org# the corresponding build directory to pick up generated include
2924762Snate@binkert.org# files.
2934762Snate@binkert.orgenv.Append(CPPPATH=Dir('.'))
2944762Snate@binkert.org
2954382Sbinkertn@umich.edufor extra_dir in extras_dir_list:
2964382Sbinkertn@umich.edu    env.Append(CPPPATH=Dir(extra_dir))
2974382Sbinkertn@umich.edu
2984382Sbinkertn@umich.edu# Workaround for bug in SCons version > 0.97d20071212
2994382Sbinkertn@umich.edu# Scons bug id: 2006 gem5 Bug id: 308
3004382Sbinkertn@umich.edufor root, dirs, files in os.walk(base_dir, topdown=True):
3014762Snate@binkert.org    Dir(root[len(base_dir) + 1:])
3024382Sbinkertn@umich.edu
3034382Sbinkertn@umich.edu########################################################################
3044382Sbinkertn@umich.edu#
3054382Sbinkertn@umich.edu# Walk the tree and execute all SConscripts in subdirectories
3064762Snate@binkert.org#
3074762Snate@binkert.org
3084762Snate@binkert.orghere = Dir('.').srcnode().abspath
3094382Sbinkertn@umich.edufor root, dirs, files in os.walk(base_dir, topdown=True):
3104762Snate@binkert.org    if root == here:
3114382Sbinkertn@umich.edu        # we don't want to recurse back into this SConscript
3124382Sbinkertn@umich.edu        continue
3134382Sbinkertn@umich.edu
3144762Snate@binkert.org    if 'SConscript' in files:
3154762Snate@binkert.org        build_dir = joinpath(env['BUILDDIR'], root[len(base_dir) + 1:])
3164382Sbinkertn@umich.edu        SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
3174382Sbinkertn@umich.edu
3184382Sbinkertn@umich.edufor extra_dir in extras_dir_list:
3194762Snate@binkert.org    prefix_len = len(dirname(extra_dir)) + 1
3204382Sbinkertn@umich.edu    for root, dirs, files in os.walk(extra_dir, topdown=True):
3214382Sbinkertn@umich.edu        # if build lives in the extras directory, don't walk down it
3224762Snate@binkert.org        if 'build' in dirs:
3234762Snate@binkert.org            dirs.remove('build')
3244762Snate@binkert.org
3254382Sbinkertn@umich.edu        if 'SConscript' in files:
3264382Sbinkertn@umich.edu            build_dir = joinpath(env['BUILDDIR'], root[prefix_len:])
3274382Sbinkertn@umich.edu            SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
3284382Sbinkertn@umich.edu
3294382Sbinkertn@umich.edufor opt in export_vars:
3304382Sbinkertn@umich.edu    env.ConfigFile(opt)
3314382Sbinkertn@umich.edu
3324382Sbinkertn@umich.edudef makeTheISA(source, target, env):
3334382Sbinkertn@umich.edu    isas = [ src.get_contents() for src in source ]
3344382Sbinkertn@umich.edu    target_isa = env['TARGET_ISA']
335955SN/A    def define(isa):
336955SN/A        return isa.upper() + '_ISA'
337955SN/A    
338955SN/A    def namespace(isa):
3391108SN/A        return isa[0].upper() + isa[1:].lower() + 'ISA' 
340955SN/A
341955SN/A
342955SN/A    code = code_formatter()
343955SN/A    code('''\
344955SN/A#ifndef __CONFIG_THE_ISA_HH__
345955SN/A#define __CONFIG_THE_ISA_HH__
346955SN/A
347955SN/A''')
348955SN/A
3492655Sstever@eecs.umich.edu    for i,isa in enumerate(isas):
3502655Sstever@eecs.umich.edu        code('#define $0 $1', define(isa), i + 1)
3512655Sstever@eecs.umich.edu
3522655Sstever@eecs.umich.edu    code('''
3532655Sstever@eecs.umich.edu
3542655Sstever@eecs.umich.edu#define THE_ISA ${{define(target_isa)}}
3552655Sstever@eecs.umich.edu#define TheISA ${{namespace(target_isa)}}
3562655Sstever@eecs.umich.edu
3572655Sstever@eecs.umich.edu#endif // __CONFIG_THE_ISA_HH__''')
3582655Sstever@eecs.umich.edu
3594762Snate@binkert.org    code.write(str(target[0]))
3602655Sstever@eecs.umich.edu
3612655Sstever@eecs.umich.eduenv.Command('config/the_isa.hh', map(Value, all_isa_list),
3624007Ssaidi@eecs.umich.edu            MakeAction(makeTheISA, Transform("CFG ISA", 0)))
3634596Sbinkertn@umich.edu
3644007Ssaidi@eecs.umich.edu########################################################################
3654596Sbinkertn@umich.edu#
3664596Sbinkertn@umich.edu# Prevent any SimObjects from being added after this point, they
3672655Sstever@eecs.umich.edu# should all have been added in the SConscripts above
3684382Sbinkertn@umich.edu#
3692655Sstever@eecs.umich.eduSimObject.fixed = True
3702655Sstever@eecs.umich.edu
3712655Sstever@eecs.umich.educlass DictImporter(object):
372955SN/A    '''This importer takes a dictionary of arbitrary module names that
3733918Ssaidi@eecs.umich.edu    map to arbitrary filenames.'''
3743918Ssaidi@eecs.umich.edu    def __init__(self, modules):
3753918Ssaidi@eecs.umich.edu        self.modules = modules
3763918Ssaidi@eecs.umich.edu        self.installed = set()
3773918Ssaidi@eecs.umich.edu
3783918Ssaidi@eecs.umich.edu    def __del__(self):
3793918Ssaidi@eecs.umich.edu        self.unload()
3803918Ssaidi@eecs.umich.edu
3813918Ssaidi@eecs.umich.edu    def unload(self):
3823918Ssaidi@eecs.umich.edu        import sys
3833918Ssaidi@eecs.umich.edu        for module in self.installed:
3843918Ssaidi@eecs.umich.edu            del sys.modules[module]
3853918Ssaidi@eecs.umich.edu        self.installed = set()
3863918Ssaidi@eecs.umich.edu
3873940Ssaidi@eecs.umich.edu    def find_module(self, fullname, path):
3883940Ssaidi@eecs.umich.edu        if fullname == 'm5.defines':
3893940Ssaidi@eecs.umich.edu            return self
3903942Ssaidi@eecs.umich.edu
3913940Ssaidi@eecs.umich.edu        if fullname == 'm5.objects':
3923515Ssaidi@eecs.umich.edu            return self
3933918Ssaidi@eecs.umich.edu
3944762Snate@binkert.org        if fullname.startswith('m5.internal'):
3953515Ssaidi@eecs.umich.edu            return None
3962655Sstever@eecs.umich.edu
3973918Ssaidi@eecs.umich.edu        source = self.modules.get(fullname, None)
3983619Sbinkertn@umich.edu        if source is not None and fullname.startswith('m5.objects'):
399955SN/A            return self
400955SN/A
4012655Sstever@eecs.umich.edu        return None
4023918Ssaidi@eecs.umich.edu
4033619Sbinkertn@umich.edu    def load_module(self, fullname):
404955SN/A        mod = imp.new_module(fullname)
405955SN/A        sys.modules[fullname] = mod
4062655Sstever@eecs.umich.edu        self.installed.add(fullname)
4073918Ssaidi@eecs.umich.edu
4083619Sbinkertn@umich.edu        mod.__loader__ = self
409955SN/A        if fullname == 'm5.objects':
410955SN/A            mod.__path__ = fullname.split('.')
4112655Sstever@eecs.umich.edu            return mod
4123918Ssaidi@eecs.umich.edu
4133683Sstever@eecs.umich.edu        if fullname == 'm5.defines':
4142655Sstever@eecs.umich.edu            mod.__dict__['buildEnv'] = m5.util.SmartDict(build_env)
4151869SN/A            return mod
4161869SN/A
417        source = self.modules[fullname]
418        if source.modname == '__init__':
419            mod.__path__ = source.modpath
420        mod.__file__ = source.abspath
421
422        exec file(source.abspath, 'r') in mod.__dict__
423
424        return mod
425
426import m5.SimObject
427import m5.params
428from m5.util import code_formatter
429
430m5.SimObject.clear()
431m5.params.clear()
432
433# install the python importer so we can grab stuff from the source
434# tree itself.  We can't have SimObjects added after this point or
435# else we won't know about them for the rest of the stuff.
436importer = DictImporter(PySource.modules)
437sys.meta_path[0:0] = [ importer ]
438
439# import all sim objects so we can populate the all_objects list
440# make sure that we're working with a list, then let's sort it
441for modname in SimObject.modnames:
442    exec('from m5.objects import %s' % modname)
443
444# we need to unload all of the currently imported modules so that they
445# will be re-imported the next time the sconscript is run
446importer.unload()
447sys.meta_path.remove(importer)
448
449sim_objects = m5.SimObject.allClasses
450all_enums = m5.params.allEnums
451
452all_params = {}
453for name,obj in sorted(sim_objects.iteritems()):
454    for param in obj._params.local.values():
455        # load the ptype attribute now because it depends on the
456        # current version of SimObject.allClasses, but when scons
457        # actually uses the value, all versions of
458        # SimObject.allClasses will have been loaded
459        param.ptype
460
461        if not hasattr(param, 'swig_decl'):
462            continue
463        pname = param.ptype_str
464        if pname not in all_params:
465            all_params[pname] = param
466
467########################################################################
468#
469# calculate extra dependencies
470#
471module_depends = ["m5", "m5.SimObject", "m5.params"]
472depends = [ PySource.modules[dep].snode for dep in module_depends ]
473
474########################################################################
475#
476# Commands for the basic automatically generated python files
477#
478
479# Generate Python file containing a dict specifying the current
480# buildEnv flags.
481def makeDefinesPyFile(target, source, env):
482    build_env = source[0].get_contents()
483
484    code = code_formatter()
485    code("""
486import m5.internal
487import m5.util
488
489buildEnv = m5.util.SmartDict($build_env)
490
491compileDate = m5.internal.core.compileDate
492_globals = globals()
493for key,val in m5.internal.core.__dict__.iteritems():
494    if key.startswith('flag_'):
495        flag = key[5:]
496        _globals[flag] = val
497del _globals
498""")
499    code.write(target[0].abspath)
500
501defines_info = Value(build_env)
502# Generate a file with all of the compile options in it
503env.Command('python/m5/defines.py', defines_info,
504            MakeAction(makeDefinesPyFile, Transform("DEFINES", 0)))
505PySource('m5', 'python/m5/defines.py')
506
507# Generate python file containing info about the M5 source code
508def makeInfoPyFile(target, source, env):
509    code = code_formatter()
510    for src in source:
511        data = ''.join(file(src.srcnode().abspath, 'r').xreadlines())
512        code('$src = ${{repr(data)}}')
513    code.write(str(target[0]))
514
515# Generate a file that wraps the basic top level files
516env.Command('python/m5/info.py',
517            [ '#/COPYING', '#/LICENSE', '#/README', ],
518            MakeAction(makeInfoPyFile, Transform("INFO")))
519PySource('m5', 'python/m5/info.py')
520
521########################################################################
522#
523# Create all of the SimObject param headers and enum headers
524#
525
526def createSimObjectParam(target, source, env):
527    assert len(target) == 1 and len(source) == 1
528
529    name = str(source[0].get_contents())
530    obj = sim_objects[name]
531
532    code = code_formatter()
533    obj.cxx_decl(code)
534    code.write(target[0].abspath)
535
536def createSwigParam(target, source, env):
537    assert len(target) == 1 and len(source) == 1
538
539    name = str(source[0].get_contents())
540    param = all_params[name]
541
542    code = code_formatter()
543    code('%module(package="m5.internal") $0_${name}', param.file_ext)
544    param.swig_decl(code)
545    code.write(target[0].abspath)
546
547def createEnumStrings(target, source, env):
548    assert len(target) == 1 and len(source) == 1
549
550    name = str(source[0].get_contents())
551    obj = all_enums[name]
552
553    code = code_formatter()
554    obj.cxx_def(code)
555    code.write(target[0].abspath)
556
557def createEnumParam(target, source, env):
558    assert len(target) == 1 and len(source) == 1
559
560    name = str(source[0].get_contents())
561    obj = all_enums[name]
562
563    code = code_formatter()
564    obj.cxx_decl(code)
565    code.write(target[0].abspath)
566
567def createEnumSwig(target, source, env):
568    assert len(target) == 1 and len(source) == 1
569
570    name = str(source[0].get_contents())
571    obj = all_enums[name]
572
573    code = code_formatter()
574    code('''\
575%module(package="m5.internal") enum_$name
576
577%{
578#include "enums/$name.hh"
579%}
580
581%include "enums/$name.hh"
582''')
583    code.write(target[0].abspath)
584
585# Generate all of the SimObject param struct header files
586params_hh_files = []
587for name,simobj in sorted(sim_objects.iteritems()):
588    py_source = PySource.modules[simobj.__module__]
589    extra_deps = [ py_source.tnode ]
590
591    hh_file = File('params/%s.hh' % name)
592    params_hh_files.append(hh_file)
593    env.Command(hh_file, Value(name),
594                MakeAction(createSimObjectParam, Transform("SO PARAM")))
595    env.Depends(hh_file, depends + extra_deps)
596
597# Generate any parameter header files needed
598params_i_files = []
599for name,param in all_params.iteritems():
600    i_file = File('python/m5/internal/%s_%s.i' % (param.file_ext, name))
601    params_i_files.append(i_file)
602    env.Command(i_file, Value(name),
603                MakeAction(createSwigParam, Transform("SW PARAM")))
604    env.Depends(i_file, depends)
605    SwigSource('m5.internal', i_file)
606
607# Generate all enum header files
608for name,enum in sorted(all_enums.iteritems()):
609    py_source = PySource.modules[enum.__module__]
610    extra_deps = [ py_source.tnode ]
611
612    cc_file = File('enums/%s.cc' % name)
613    env.Command(cc_file, Value(name),
614                MakeAction(createEnumStrings, Transform("ENUM STR")))
615    env.Depends(cc_file, depends + extra_deps)
616    Source(cc_file)
617
618    hh_file = File('enums/%s.hh' % name)
619    env.Command(hh_file, Value(name),
620                MakeAction(createEnumParam, Transform("EN PARAM")))
621    env.Depends(hh_file, depends + extra_deps)
622
623    i_file = File('python/m5/internal/enum_%s.i' % name)
624    env.Command(i_file, Value(name),
625                MakeAction(createEnumSwig, Transform("ENUMSWIG")))
626    env.Depends(i_file, depends + extra_deps)
627    SwigSource('m5.internal', i_file)
628
629def buildParam(target, source, env):
630    name = source[0].get_contents()
631    obj = sim_objects[name]
632    class_path = obj.cxx_class.split('::')
633    classname = class_path[-1]
634    namespaces = class_path[:-1]
635    params = obj._params.local.values()
636
637    code = code_formatter()
638
639    code('%module(package="m5.internal") param_$name')
640    code()
641    code('%{')
642    code('#include "params/$obj.hh"')
643    for param in params:
644        param.cxx_predecls(code)
645    code('%}')
646    code()
647
648    for param in params:
649        param.swig_predecls(code)
650
651    code()
652    if obj._base:
653        code('%import "python/m5/internal/param_${{obj._base}}.i"')
654    code()
655    obj.swig_objdecls(code)
656    code()
657
658    code('%include "params/$obj.hh"')
659
660    code.write(target[0].abspath)
661
662for name in sim_objects.iterkeys():
663    params_file = File('python/m5/internal/param_%s.i' % name)
664    env.Command(params_file, Value(name),
665                MakeAction(buildParam, Transform("BLDPARAM")))
666    env.Depends(params_file, depends)
667    SwigSource('m5.internal', params_file)
668
669# Generate the main swig init file
670def makeEmbeddedSwigInit(target, source, env):
671    code = code_formatter()
672    module = source[0].get_contents()
673    code('''\
674#include "sim/init.hh"
675
676extern "C" {
677    void init_${module}();
678}
679
680EmbeddedSwig embed_swig_${module}(init_${module});
681''')
682    code.write(str(target[0]))
683    
684# Build all swig modules
685for swig in SwigSource.all:
686    env.Command([swig.cc_source.tnode, swig.py_source.tnode], swig.tnode,
687                MakeAction('$SWIG $SWIGFLAGS -outdir ${TARGETS[1].dir} '
688                '-o ${TARGETS[0]} $SOURCES', Transform("SWIG")))
689    cc_file = str(swig.tnode)
690    init_file = '%s/init_%s.cc' % (dirname(cc_file), basename(cc_file))
691    env.Command(init_file, Value(swig.module),
692                MakeAction(makeEmbeddedSwigInit, Transform("EMBED SW")))
693    Source(init_file, **swig.guards)
694
695#
696# Handle debug flags
697#
698def makeDebugFlagCC(target, source, env):
699    assert(len(target) == 1 and len(source) == 1)
700
701    val = eval(source[0].get_contents())
702    name, compound, desc = val
703    compound = list(sorted(compound))
704
705    code = code_formatter()
706
707    # file header
708    code('''
709/*
710 * DO NOT EDIT THIS FILE! Automatically generated
711 */
712
713#include "base/debug.hh"
714''')
715
716    for flag in compound:
717        code('#include "debug/$flag.hh"')
718    code()
719    code('namespace Debug {')
720    code()
721
722    if not compound:
723        code('SimpleFlag $name("$name", "$desc");')
724    else:
725        code('CompoundFlag $name("$name", "$desc",')
726        code.indent()
727        last = len(compound) - 1
728        for i,flag in enumerate(compound):
729            if i != last:
730                code('$flag,')
731            else:
732                code('$flag);')
733        code.dedent()
734
735    code()
736    code('} // namespace Debug')
737
738    code.write(str(target[0]))
739
740def makeDebugFlagHH(target, source, env):
741    assert(len(target) == 1 and len(source) == 1)
742
743    val = eval(source[0].get_contents())
744    name, compound, desc = val
745
746    code = code_formatter()
747
748    # file header boilerplate
749    code('''\
750/*
751 * DO NOT EDIT THIS FILE!
752 *
753 * Automatically generated by SCons
754 */
755
756#ifndef __DEBUG_${name}_HH__
757#define __DEBUG_${name}_HH__
758
759namespace Debug {
760''')
761
762    if compound:
763        code('class CompoundFlag;')
764    code('class SimpleFlag;')
765
766    if compound:
767        code('extern CompoundFlag $name;')
768        for flag in compound:
769            code('extern SimpleFlag $flag;')
770    else:
771        code('extern SimpleFlag $name;')
772
773    code('''
774}
775
776#endif // __DEBUG_${name}_HH__
777''')
778
779    code.write(str(target[0]))
780
781for name,flag in sorted(debug_flags.iteritems()):
782    n, compound, desc = flag
783    assert n == name
784
785    env.Command('debug/%s.hh' % name, Value(flag),
786                MakeAction(makeDebugFlagHH, Transform("TRACING", 0)))
787    env.Command('debug/%s.cc' % name, Value(flag),
788                MakeAction(makeDebugFlagCC, Transform("TRACING", 0)))
789    Source('debug/%s.cc' % name)
790
791# Embed python files.  All .py files that have been indicated by a
792# PySource() call in a SConscript need to be embedded into the M5
793# library.  To do that, we compile the file to byte code, marshal the
794# byte code, compress it, and then generate a c++ file that
795# inserts the result into an array.
796def embedPyFile(target, source, env):
797    def c_str(string):
798        if string is None:
799            return "0"
800        return '"%s"' % string
801
802    '''Action function to compile a .py into a code object, marshal
803    it, compress it, and stick it into an asm file so the code appears
804    as just bytes with a label in the data section'''
805
806    src = file(str(source[0]), 'r').read()
807
808    pysource = PySource.tnodes[source[0]]
809    compiled = compile(src, pysource.abspath, 'exec')
810    marshalled = marshal.dumps(compiled)
811    compressed = zlib.compress(marshalled)
812    data = compressed
813    sym = pysource.symname
814
815    code = code_formatter()
816    code('''\
817#include "sim/init.hh"
818
819namespace {
820
821const char data_${sym}[] = {
822''')
823    code.indent()
824    step = 16
825    for i in xrange(0, len(data), step):
826        x = array.array('B', data[i:i+step])
827        code(''.join('%d,' % d for d in x))
828    code.dedent()
829    
830    code('''};
831
832EmbeddedPython embedded_${sym}(
833    ${{c_str(pysource.arcname)}},
834    ${{c_str(pysource.abspath)}},
835    ${{c_str(pysource.modpath)}},
836    data_${sym},
837    ${{len(data)}},
838    ${{len(marshalled)}});
839
840} // anonymous namespace
841''')
842    code.write(str(target[0]))
843
844for source in PySource.all:
845    env.Command(source.cpp, source.tnode, 
846                MakeAction(embedPyFile, Transform("EMBED PY")))
847    Source(source.cpp)
848
849########################################################################
850#
851# Define binaries.  Each different build type (debug, opt, etc.) gets
852# a slightly different build environment.
853#
854
855# List of constructed environments to pass back to SConstruct
856envList = []
857
858date_source = Source('base/date.cc', skip_lib=True)
859
860# Function to create a new build environment as clone of current
861# environment 'env' with modified object suffix and optional stripped
862# binary.  Additional keyword arguments are appended to corresponding
863# build environment vars.
864def makeEnv(label, objsfx, strip = False, **kwargs):
865    # SCons doesn't know to append a library suffix when there is a '.' in the
866    # name.  Use '_' instead.
867    libname = 'gem5_' + label
868    exename = 'gem5.' + label
869    secondary_exename = 'm5.' + label
870
871    new_env = env.Clone(OBJSUFFIX=objsfx, SHOBJSUFFIX=objsfx + 's')
872    new_env.Label = label
873    new_env.Append(**kwargs)
874
875    swig_env = new_env.Clone()
876    swig_env.Append(CCFLAGS='-Werror')
877    if env['GCC']:
878        swig_env.Append(CCFLAGS='-Wno-uninitialized')
879        swig_env.Append(CCFLAGS='-Wno-sign-compare')
880        swig_env.Append(CCFLAGS='-Wno-parentheses')
881
882    werror_env = new_env.Clone()
883    werror_env.Append(CCFLAGS='-Werror')
884
885    def make_obj(source, static, extra_deps = None):
886        '''This function adds the specified source to the correct
887        build environment, and returns the corresponding SCons Object
888        nodes'''
889
890        if source.swig:
891            env = swig_env
892        elif source.Werror:
893            env = werror_env
894        else:
895            env = new_env
896
897        if static:
898            obj = env.StaticObject(source.tnode)
899        else:
900            obj = env.SharedObject(source.tnode)
901
902        if extra_deps:
903            env.Depends(obj, extra_deps)
904
905        return obj
906
907    sources = Source.get(main=False, skip_lib=False)
908    static_objs = [ make_obj(s, True) for s in sources ]
909    shared_objs = [ make_obj(s, False) for s in sources ]
910
911    static_date = make_obj(date_source, static=True, extra_deps=static_objs)
912    static_objs.append(static_date)
913    
914    shared_date = make_obj(date_source, static=False, extra_deps=shared_objs)
915    shared_objs.append(shared_date)
916
917    # First make a library of everything but main() so other programs can
918    # link against m5.
919    static_lib = new_env.StaticLibrary(libname, static_objs)
920    shared_lib = new_env.SharedLibrary(libname, shared_objs)
921
922    # Now link a stub with main() and the static library.
923    main_objs = [ make_obj(s, True) for s in Source.get(main=True) ]
924
925    for test in UnitTest.all:
926        flags = { test.target : True }
927        test_sources = Source.get(**flags)
928        test_objs = [ make_obj(s, static=True) for s in test_sources ]
929        testname = "unittest/%s.%s" % (test.target, label)
930        new_env.Program(testname, main_objs + test_objs + static_objs)
931
932    progname = exename
933    if strip:
934        progname += '.unstripped'
935
936    targets = new_env.Program(progname, main_objs + static_objs)
937
938    if strip:
939        if sys.platform == 'sunos5':
940            cmd = 'cp $SOURCE $TARGET; strip $TARGET'
941        else:
942            cmd = 'strip $SOURCE -o $TARGET'
943        targets = new_env.Command(exename, progname,
944                    MakeAction(cmd, Transform("STRIP")))
945
946    new_env.Command(secondary_exename, exename,
947            MakeAction('ln $SOURCE $TARGET', Transform("HARDLINK")))
948
949    new_env.M5Binary = targets[0]
950    envList.append(new_env)
951
952# Debug binary
953ccflags = {}
954if env['GCC']:
955    if sys.platform == 'sunos5':
956        ccflags['debug'] = '-gstabs+'
957    else:
958        ccflags['debug'] = '-ggdb3'
959    ccflags['opt'] = '-g -O3'
960    ccflags['fast'] = '-O3'
961    ccflags['prof'] = '-O3 -g -pg'
962elif env['SUNCC']:
963    ccflags['debug'] = '-g0'
964    ccflags['opt'] = '-g -O'
965    ccflags['fast'] = '-fast'
966    ccflags['prof'] = '-fast -g -pg'
967elif env['ICC']:
968    ccflags['debug'] = '-g -O0'
969    ccflags['opt'] = '-g -O'
970    ccflags['fast'] = '-fast'
971    ccflags['prof'] = '-fast -g -pg'
972else:
973    print 'Unknown compiler, please fix compiler options'
974    Exit(1)
975
976makeEnv('debug', '.do',
977        CCFLAGS = Split(ccflags['debug']),
978        CPPDEFINES = ['DEBUG', 'TRACING_ON=1'])
979
980# Optimized binary
981makeEnv('opt', '.o',
982        CCFLAGS = Split(ccflags['opt']),
983        CPPDEFINES = ['TRACING_ON=1'])
984
985# "Fast" binary
986makeEnv('fast', '.fo', strip = True,
987        CCFLAGS = Split(ccflags['fast']),
988        CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'])
989
990# Profiled binary
991makeEnv('prof', '.po',
992        CCFLAGS = Split(ccflags['prof']),
993        CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
994        LINKFLAGS = '-pg')
995
996Return('envList')
997