SConscript revision 7811:a8fc35183c10
1955SN/A# -*- mode:python -*-
2955SN/A
35871Snate@binkert.org# Copyright (c) 2004-2005 The Regents of The University of Michigan
41762SN/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.
28955SN/A#
292665Ssaidi@eecs.umich.edu# Authors: Nathan Binkert
302665Ssaidi@eecs.umich.edu
315863Snate@binkert.orgimport array
32955SN/Aimport bisect
33955SN/Aimport imp
34955SN/Aimport marshal
35955SN/Aimport os
36955SN/Aimport re
372632Sstever@eecs.umich.eduimport sys
382632Sstever@eecs.umich.eduimport zlib
392632Sstever@eecs.umich.edu
402632Sstever@eecs.umich.edufrom os.path import basename, dirname, exists, isdir, isfile, join as joinpath
41955SN/A
422632Sstever@eecs.umich.eduimport SCons
432632Sstever@eecs.umich.edu
442761Sstever@eecs.umich.edu# This file defines how to build a particular configuration of M5
452632Sstever@eecs.umich.edu# based on variable settings in the 'env' build environment.
462632Sstever@eecs.umich.edu
472632Sstever@eecs.umich.eduImport('*')
482761Sstever@eecs.umich.edu
492761Sstever@eecs.umich.edu# Children need to see the environment
502761Sstever@eecs.umich.eduExport('env')
512632Sstever@eecs.umich.edu
522632Sstever@eecs.umich.edubuild_env = [(opt, env[opt]) for opt in export_vars]
532761Sstever@eecs.umich.edu
542761Sstever@eecs.umich.edufrom m5.util import code_formatter
552761Sstever@eecs.umich.edu
562761Sstever@eecs.umich.edu########################################################################
572761Sstever@eecs.umich.edu# Code for adding source files of various types
582632Sstever@eecs.umich.edu#
592632Sstever@eecs.umich.educlass SourceMeta(type):
602632Sstever@eecs.umich.edu    def __init__(cls, name, bases, dict):
612632Sstever@eecs.umich.edu        super(SourceMeta, cls).__init__(name, bases, dict)
622632Sstever@eecs.umich.edu        cls.all = []
632632Sstever@eecs.umich.edu        
642632Sstever@eecs.umich.edu    def get(cls, **kwargs):
65955SN/A        for src in cls.all:
66955SN/A            for attr,value in kwargs.iteritems():
67955SN/A                if getattr(src, attr) != value:
685863Snate@binkert.org                    break
695863Snate@binkert.org            else:
705863Snate@binkert.org                yield src
715863Snate@binkert.org
725863Snate@binkert.orgclass SourceFile(object):
735863Snate@binkert.org    __metaclass__ = SourceMeta
745863Snate@binkert.org    def __init__(self, source):
755863Snate@binkert.org        tnode = source
765863Snate@binkert.org        if not isinstance(source, SCons.Node.FS.File):
775863Snate@binkert.org            tnode = File(source)
785863Snate@binkert.org
795863Snate@binkert.org        self.tnode = tnode
805863Snate@binkert.org        self.snode = tnode.srcnode()
815863Snate@binkert.org        self.filename = str(tnode)
825863Snate@binkert.org        self.dirname = dirname(self.filename)
835863Snate@binkert.org        self.basename = basename(self.filename)
845863Snate@binkert.org        index = self.basename.rfind('.')
855863Snate@binkert.org        if index <= 0:
865863Snate@binkert.org            # dot files aren't extensions
875863Snate@binkert.org            self.extname = self.basename, None
885863Snate@binkert.org        else:
895863Snate@binkert.org            self.extname = self.basename[:index], self.basename[index+1:]
905863Snate@binkert.org
915863Snate@binkert.org        for base in type(self).__mro__:
925863Snate@binkert.org            if issubclass(base, SourceFile):
935863Snate@binkert.org                base.all.append(self)
945863Snate@binkert.org
955863Snate@binkert.org    def __lt__(self, other): return self.filename < other.filename
965863Snate@binkert.org    def __le__(self, other): return self.filename <= other.filename
975863Snate@binkert.org    def __gt__(self, other): return self.filename > other.filename
985863Snate@binkert.org    def __ge__(self, other): return self.filename >= other.filename
99955SN/A    def __eq__(self, other): return self.filename == other.filename
1005396Ssaidi@eecs.umich.edu    def __ne__(self, other): return self.filename != other.filename
1015863Snate@binkert.org        
1025863Snate@binkert.orgclass Source(SourceFile):
1034202Sbinkertn@umich.edu    '''Add a c/c++ source file to the build'''
1045863Snate@binkert.org    def __init__(self, source, Werror=True, swig=False, bin_only=False,
1055863Snate@binkert.org                 skip_lib=False):
1065863Snate@binkert.org        super(Source, self).__init__(source)
1075863Snate@binkert.org
108955SN/A        self.Werror = Werror
1095273Sstever@gmail.com        self.swig = swig
1105871Snate@binkert.org        self.bin_only = bin_only
1115273Sstever@gmail.com        self.skip_lib = bin_only or skip_lib
1125871Snate@binkert.org
1135863Snate@binkert.orgclass PySource(SourceFile):
1145863Snate@binkert.org    '''Add a python source file to the named package'''
1155863Snate@binkert.org    invalid_sym_char = re.compile('[^A-z0-9_]')
1165871Snate@binkert.org    modules = {}
1175872Snate@binkert.org    tnodes = {}
1185872Snate@binkert.org    symnames = {}
1195872Snate@binkert.org    
1205871Snate@binkert.org    def __init__(self, package, source):
1215871Snate@binkert.org        super(PySource, self).__init__(source)
1225871Snate@binkert.org
1235871Snate@binkert.org        modname,ext = self.extname
1245871Snate@binkert.org        assert ext == 'py'
1255871Snate@binkert.org
1265871Snate@binkert.org        if package:
1275871Snate@binkert.org            path = package.split('.')
1285871Snate@binkert.org        else:
1295871Snate@binkert.org            path = []
1305871Snate@binkert.org
1315871Snate@binkert.org        modpath = path[:]
1325871Snate@binkert.org        if modname != '__init__':
1335871Snate@binkert.org            modpath += [ modname ]
1345863Snate@binkert.org        modpath = '.'.join(modpath)
1355227Ssaidi@eecs.umich.edu
1365396Ssaidi@eecs.umich.edu        arcpath = path + [ self.basename ]
1375396Ssaidi@eecs.umich.edu        abspath = self.snode.abspath
1385396Ssaidi@eecs.umich.edu        if not exists(abspath):
1395396Ssaidi@eecs.umich.edu            abspath = self.tnode.abspath
1405396Ssaidi@eecs.umich.edu
1415396Ssaidi@eecs.umich.edu        self.package = package
1425396Ssaidi@eecs.umich.edu        self.modname = modname
1435396Ssaidi@eecs.umich.edu        self.modpath = modpath
1445588Ssaidi@eecs.umich.edu        self.arcname = joinpath(*arcpath)
1455396Ssaidi@eecs.umich.edu        self.abspath = abspath
1465396Ssaidi@eecs.umich.edu        self.compiled = File(self.filename + 'c')
1475396Ssaidi@eecs.umich.edu        self.cpp = File(self.filename + '.cc')
1485396Ssaidi@eecs.umich.edu        self.symname = PySource.invalid_sym_char.sub('_', modpath)
1495396Ssaidi@eecs.umich.edu
1505396Ssaidi@eecs.umich.edu        PySource.modules[modpath] = self
1515396Ssaidi@eecs.umich.edu        PySource.tnodes[self.tnode] = self
1525396Ssaidi@eecs.umich.edu        PySource.symnames[self.symname] = self
1535396Ssaidi@eecs.umich.edu
1545396Ssaidi@eecs.umich.educlass SimObject(PySource):
1555396Ssaidi@eecs.umich.edu    '''Add a SimObject python file as a python source object and add
1565396Ssaidi@eecs.umich.edu    it to a list of sim object modules'''
1575396Ssaidi@eecs.umich.edu
1585396Ssaidi@eecs.umich.edu    fixed = False
1595871Snate@binkert.org    modnames = []
1605871Snate@binkert.org
1615871Snate@binkert.org    def __init__(self, source):
1625871Snate@binkert.org        super(SimObject, self).__init__('m5.objects', source)
1635871Snate@binkert.org        if self.fixed:
1646003Snate@binkert.org            raise AttributeError, "Too late to call SimObject now."
1656003Snate@binkert.org
166955SN/A        bisect.insort_right(SimObject.modnames, self.modname)
1675871Snate@binkert.org
1685871Snate@binkert.orgclass SwigSource(SourceFile):
1695871Snate@binkert.org    '''Add a swig file to build'''
1705871Snate@binkert.org
171955SN/A    def __init__(self, package, source):
1725871Snate@binkert.org        super(SwigSource, self).__init__(source)
1735871Snate@binkert.org
1745871Snate@binkert.org        modname,ext = self.extname
1751533SN/A        assert ext == 'i'
1765871Snate@binkert.org
1775871Snate@binkert.org        self.module = modname
1785863Snate@binkert.org        cc_file = joinpath(self.dirname, modname + '_wrap.cc')
1795871Snate@binkert.org        py_file = joinpath(self.dirname, modname + '.py')
1805871Snate@binkert.org
1815871Snate@binkert.org        self.cc_source = Source(cc_file, swig=True)
1825871Snate@binkert.org        self.py_source = PySource(package, py_file)
1835871Snate@binkert.org
1845863Snate@binkert.orgunit_tests = []
1855871Snate@binkert.orgdef UnitTest(target, sources):
1865863Snate@binkert.org    if not isinstance(sources, (list, tuple)):
1875871Snate@binkert.org        sources = [ sources ]
1884678Snate@binkert.org
1894678Snate@binkert.org    sources = [ Source(src, skip_lib=True) for src in sources ]
1904678Snate@binkert.org    unit_tests.append((target, sources))
1914678Snate@binkert.org
1924678Snate@binkert.org# Children should have access
1934678Snate@binkert.orgExport('Source')
1944678Snate@binkert.orgExport('PySource')
1954678Snate@binkert.orgExport('SimObject')
1964678Snate@binkert.orgExport('SwigSource')
1974678Snate@binkert.orgExport('UnitTest')
1984678Snate@binkert.org
1994678Snate@binkert.org########################################################################
2005871Snate@binkert.org#
2014678Snate@binkert.org# Trace Flags
2025871Snate@binkert.org#
2035871Snate@binkert.orgtrace_flags = {}
2045871Snate@binkert.orgdef TraceFlag(name, desc=None):
2055871Snate@binkert.org    if name in trace_flags:
2065871Snate@binkert.org        raise AttributeError, "Flag %s already specified" % name
2075871Snate@binkert.org    trace_flags[name] = (name, (), desc)
2085871Snate@binkert.org
2095871Snate@binkert.orgdef CompoundFlag(name, flags, desc=None):
2105871Snate@binkert.org    if name in trace_flags:
2115871Snate@binkert.org        raise AttributeError, "Flag %s already specified" % name
2125871Snate@binkert.org
2135871Snate@binkert.org    compound = tuple(flags)
2145871Snate@binkert.org    trace_flags[name] = (name, compound, desc)
2155990Ssaidi@eecs.umich.edu
2165871Snate@binkert.orgExport('TraceFlag')
2175871Snate@binkert.orgExport('CompoundFlag')
2185871Snate@binkert.org
2194678Snate@binkert.org########################################################################
2205871Snate@binkert.org#
2215871Snate@binkert.org# Set some compiler variables
2225871Snate@binkert.org#
2235871Snate@binkert.org
2245871Snate@binkert.org# Include file paths are rooted in this directory.  SCons will
2255871Snate@binkert.org# automatically expand '.' to refer to both the source directory and
2265871Snate@binkert.org# the corresponding build directory to pick up generated include
2275871Snate@binkert.org# files.
2285871Snate@binkert.orgenv.Append(CPPPATH=Dir('.'))
2295871Snate@binkert.org
2304678Snate@binkert.orgfor extra_dir in extras_dir_list:
2315871Snate@binkert.org    env.Append(CPPPATH=Dir(extra_dir))
2324678Snate@binkert.org
2335871Snate@binkert.org# Workaround for bug in SCons version > 0.97d20071212
2345871Snate@binkert.org# Scons bug id: 2006 M5 Bug id: 308 
2355871Snate@binkert.orgfor root, dirs, files in os.walk(base_dir, topdown=True):
2365871Snate@binkert.org    Dir(root[len(base_dir) + 1:])
2375871Snate@binkert.org
2385871Snate@binkert.org########################################################################
2395871Snate@binkert.org#
2405871Snate@binkert.org# Walk the tree and execute all SConscripts in subdirectories
2415871Snate@binkert.org#
2425990Ssaidi@eecs.umich.edu
2435863Snate@binkert.orghere = Dir('.').srcnode().abspath
244955SN/Afor root, dirs, files in os.walk(base_dir, topdown=True):
245955SN/A    if root == here:
2462632Sstever@eecs.umich.edu        # we don't want to recurse back into this SConscript
2472632Sstever@eecs.umich.edu        continue
248955SN/A
249955SN/A    if 'SConscript' in files:
250955SN/A        build_dir = joinpath(env['BUILDDIR'], root[len(base_dir) + 1:])
251955SN/A        SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
2525863Snate@binkert.org
253955SN/Afor extra_dir in extras_dir_list:
2542632Sstever@eecs.umich.edu    prefix_len = len(dirname(extra_dir)) + 1
2552632Sstever@eecs.umich.edu    for root, dirs, files in os.walk(extra_dir, topdown=True):
2562632Sstever@eecs.umich.edu        if 'SConscript' in files:
2572632Sstever@eecs.umich.edu            build_dir = joinpath(env['BUILDDIR'], root[prefix_len:])
2582632Sstever@eecs.umich.edu            SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
2592632Sstever@eecs.umich.edu
2602632Sstever@eecs.umich.edufor opt in export_vars:
2612632Sstever@eecs.umich.edu    env.ConfigFile(opt)
2622632Sstever@eecs.umich.edu
2632632Sstever@eecs.umich.edudef makeTheISA(source, target, env):
2642632Sstever@eecs.umich.edu    isas = [ src.get_contents() for src in source ]
2652632Sstever@eecs.umich.edu    target_isa = env['TARGET_ISA']
2662632Sstever@eecs.umich.edu    def define(isa):
2673718Sstever@eecs.umich.edu        return isa.upper() + '_ISA'
2683718Sstever@eecs.umich.edu    
2693718Sstever@eecs.umich.edu    def namespace(isa):
2703718Sstever@eecs.umich.edu        return isa[0].upper() + isa[1:].lower() + 'ISA' 
2713718Sstever@eecs.umich.edu
2725863Snate@binkert.org
2735863Snate@binkert.org    code = code_formatter()
2743718Sstever@eecs.umich.edu    code('''\
2753718Sstever@eecs.umich.edu#ifndef __CONFIG_THE_ISA_HH__
2766113Snate@binkert.org#define __CONFIG_THE_ISA_HH__
2775863Snate@binkert.org
2783718Sstever@eecs.umich.edu''')
2793718Sstever@eecs.umich.edu
2802634Sstever@eecs.umich.edu    for i,isa in enumerate(isas):
2812634Sstever@eecs.umich.edu        code('#define $0 $1', define(isa), i + 1)
2825863Snate@binkert.org
2832638Sstever@eecs.umich.edu    code('''
2842632Sstever@eecs.umich.edu
2852632Sstever@eecs.umich.edu#define THE_ISA ${{define(target_isa)}}
2862632Sstever@eecs.umich.edu#define TheISA ${{namespace(target_isa)}}
2872632Sstever@eecs.umich.edu
2882632Sstever@eecs.umich.edu#endif // __CONFIG_THE_ISA_HH__''')
2892632Sstever@eecs.umich.edu
2901858SN/A    code.write(str(target[0]))
2913716Sstever@eecs.umich.edu
2922638Sstever@eecs.umich.eduenv.Command('config/the_isa.hh', map(Value, all_isa_list),
2932638Sstever@eecs.umich.edu            MakeAction(makeTheISA, " [ CFG ISA] $STRIP_TARGET"))
2942638Sstever@eecs.umich.edu
2952638Sstever@eecs.umich.edu########################################################################
2962638Sstever@eecs.umich.edu#
2972638Sstever@eecs.umich.edu# Prevent any SimObjects from being added after this point, they
2982638Sstever@eecs.umich.edu# should all have been added in the SConscripts above
2995863Snate@binkert.org#
3005863Snate@binkert.orgSimObject.fixed = True
3015863Snate@binkert.org
302955SN/Aclass DictImporter(object):
3035341Sstever@gmail.com    '''This importer takes a dictionary of arbitrary module names that
3045341Sstever@gmail.com    map to arbitrary filenames.'''
3055863Snate@binkert.org    def __init__(self, modules):
3065341Sstever@gmail.com        self.modules = modules
3074494Ssaidi@eecs.umich.edu        self.installed = set()
3084494Ssaidi@eecs.umich.edu
3095863Snate@binkert.org    def __del__(self):
3101105SN/A        self.unload()
3112667Sstever@eecs.umich.edu
3122667Sstever@eecs.umich.edu    def unload(self):
3132667Sstever@eecs.umich.edu        import sys
3142667Sstever@eecs.umich.edu        for module in self.installed:
3152667Sstever@eecs.umich.edu            del sys.modules[module]
3162667Sstever@eecs.umich.edu        self.installed = set()
3175341Sstever@gmail.com
3185863Snate@binkert.org    def find_module(self, fullname, path):
3195341Sstever@gmail.com        if fullname == 'm5.defines':
3205341Sstever@gmail.com            return self
3215341Sstever@gmail.com
3225863Snate@binkert.org        if fullname == 'm5.objects':
3235341Sstever@gmail.com            return self
3245341Sstever@gmail.com
3255341Sstever@gmail.com        if fullname.startswith('m5.internal'):
3265863Snate@binkert.org            return None
3275341Sstever@gmail.com
3285341Sstever@gmail.com        source = self.modules.get(fullname, None)
3295341Sstever@gmail.com        if source is not None and fullname.startswith('m5.objects'):
3305341Sstever@gmail.com            return self
3315341Sstever@gmail.com
3325341Sstever@gmail.com        return None
3335341Sstever@gmail.com
3345341Sstever@gmail.com    def load_module(self, fullname):
3355341Sstever@gmail.com        mod = imp.new_module(fullname)
3365341Sstever@gmail.com        sys.modules[fullname] = mod
3375863Snate@binkert.org        self.installed.add(fullname)
3385341Sstever@gmail.com
3395863Snate@binkert.org        mod.__loader__ = self
3405341Sstever@gmail.com        if fullname == 'm5.objects':
3415863Snate@binkert.org            mod.__path__ = fullname.split('.')
3425863Snate@binkert.org            return mod
3435863Snate@binkert.org
3445397Ssaidi@eecs.umich.edu        if fullname == 'm5.defines':
3455397Ssaidi@eecs.umich.edu            mod.__dict__['buildEnv'] = m5.util.SmartDict(build_env)
3465341Sstever@gmail.com            return mod
3475341Sstever@gmail.com
3485341Sstever@gmail.com        source = self.modules[fullname]
3495341Sstever@gmail.com        if source.modname == '__init__':
3505341Sstever@gmail.com            mod.__path__ = source.modpath
3515341Sstever@gmail.com        mod.__file__ = source.abspath
3525341Sstever@gmail.com
3535341Sstever@gmail.com        exec file(source.abspath, 'r') in mod.__dict__
3545863Snate@binkert.org
3555341Sstever@gmail.com        return mod
3565341Sstever@gmail.com
3575863Snate@binkert.orgimport m5.SimObject
3585341Sstever@gmail.comimport m5.params
3595863Snate@binkert.orgfrom m5.util import code_formatter
3605863Snate@binkert.org
3615341Sstever@gmail.comm5.SimObject.clear()
3625863Snate@binkert.orgm5.params.clear()
3635863Snate@binkert.org
3645341Sstever@gmail.com# install the python importer so we can grab stuff from the source
3655863Snate@binkert.org# tree itself.  We can't have SimObjects added after this point or
3665341Sstever@gmail.com# else we won't know about them for the rest of the stuff.
3675871Snate@binkert.orgimporter = DictImporter(PySource.modules)
3685341Sstever@gmail.comsys.meta_path[0:0] = [ importer ]
3695742Snate@binkert.org
3705742Snate@binkert.org# import all sim objects so we can populate the all_objects list
3715742Snate@binkert.org# make sure that we're working with a list, then let's sort it
3725341Sstever@gmail.comfor modname in SimObject.modnames:
3735742Snate@binkert.org    exec('from m5.objects import %s' % modname)
3745742Snate@binkert.org
3755341Sstever@gmail.com# we need to unload all of the currently imported modules so that they
3766017Snate@binkert.org# will be re-imported the next time the sconscript is run
3776017Snate@binkert.orgimporter.unload()
3786017Snate@binkert.orgsys.meta_path.remove(importer)
3792632Sstever@eecs.umich.edu
3806016Snate@binkert.orgsim_objects = m5.SimObject.allClasses
3815871Snate@binkert.orgall_enums = m5.params.allEnums
3825871Snate@binkert.org
3835871Snate@binkert.orgall_params = {}
3845871Snate@binkert.orgfor name,obj in sorted(sim_objects.iteritems()):
3855871Snate@binkert.org    for param in obj._params.local.values():
3865871Snate@binkert.org        # load the ptype attribute now because it depends on the
3875871Snate@binkert.org        # current version of SimObject.allClasses, but when scons
3883942Ssaidi@eecs.umich.edu        # actually uses the value, all versions of
3893940Ssaidi@eecs.umich.edu        # SimObject.allClasses will have been loaded
3903918Ssaidi@eecs.umich.edu        param.ptype
3913918Ssaidi@eecs.umich.edu
3921858SN/A        if not hasattr(param, 'swig_decl'):
3933918Ssaidi@eecs.umich.edu            continue
3943918Ssaidi@eecs.umich.edu        pname = param.ptype_str
3953918Ssaidi@eecs.umich.edu        if pname not in all_params:
3963918Ssaidi@eecs.umich.edu            all_params[pname] = param
3975571Snate@binkert.org
3983940Ssaidi@eecs.umich.edu########################################################################
3993940Ssaidi@eecs.umich.edu#
4003918Ssaidi@eecs.umich.edu# calculate extra dependencies
4013918Ssaidi@eecs.umich.edu#
4023918Ssaidi@eecs.umich.edumodule_depends = ["m5", "m5.SimObject", "m5.params"]
4033918Ssaidi@eecs.umich.edudepends = [ PySource.modules[dep].snode for dep in module_depends ]
4043918Ssaidi@eecs.umich.edu
4053918Ssaidi@eecs.umich.edu########################################################################
4065871Snate@binkert.org#
4073918Ssaidi@eecs.umich.edu# Commands for the basic automatically generated python files
4083918Ssaidi@eecs.umich.edu#
4093940Ssaidi@eecs.umich.edu
4103918Ssaidi@eecs.umich.edu# Generate Python file containing a dict specifying the current
4113918Ssaidi@eecs.umich.edu# buildEnv flags.
4125397Ssaidi@eecs.umich.edudef makeDefinesPyFile(target, source, env):
4135397Ssaidi@eecs.umich.edu    build_env, hg_info = [ x.get_contents() for x in source ]
4145397Ssaidi@eecs.umich.edu
4155708Ssaidi@eecs.umich.edu    code = code_formatter()
4165708Ssaidi@eecs.umich.edu    code("""
4175708Ssaidi@eecs.umich.eduimport m5.internal
4185708Ssaidi@eecs.umich.eduimport m5.util
4195708Ssaidi@eecs.umich.edu
4205397Ssaidi@eecs.umich.edubuildEnv = m5.util.SmartDict($build_env)
4211851SN/AhgRev = '$hg_info'
4221851SN/A
4231858SN/AcompileDate = m5.internal.core.compileDate
424955SN/A_globals = globals()
4253053Sstever@eecs.umich.edufor key,val in m5.internal.core.__dict__.iteritems():
4263053Sstever@eecs.umich.edu    if key.startswith('flag_'):
4273053Sstever@eecs.umich.edu        flag = key[5:]
4283053Sstever@eecs.umich.edu        _globals[flag] = val
4293053Sstever@eecs.umich.edudel _globals
4303053Sstever@eecs.umich.edu""")
4313053Sstever@eecs.umich.edu    code.write(target[0].abspath)
4325871Snate@binkert.org
4333053Sstever@eecs.umich.edudefines_info = [ Value(build_env), Value(env['HG_INFO']) ]
4344742Sstever@eecs.umich.edu# Generate a file with all of the compile options in it
4354742Sstever@eecs.umich.eduenv.Command('python/m5/defines.py', defines_info,
4363053Sstever@eecs.umich.edu            MakeAction(makeDefinesPyFile, " [ DEFINES] $STRIP_TARGET"))
4373053Sstever@eecs.umich.eduPySource('m5', 'python/m5/defines.py')
4383053Sstever@eecs.umich.edu
4393053Sstever@eecs.umich.edu# Generate python file containing info about the M5 source code
4403053Sstever@eecs.umich.edudef makeInfoPyFile(target, source, env):
4413053Sstever@eecs.umich.edu    code = code_formatter()
4423053Sstever@eecs.umich.edu    for src in source:
4433053Sstever@eecs.umich.edu        data = ''.join(file(src.srcnode().abspath, 'r').xreadlines())
4443053Sstever@eecs.umich.edu        code('$src = ${{repr(data)}}')
4452667Sstever@eecs.umich.edu    code.write(str(target[0]))
4464554Sbinkertn@umich.edu
4474554Sbinkertn@umich.edu# Generate a file that wraps the basic top level files
4482667Sstever@eecs.umich.eduenv.Command('python/m5/info.py',
4494554Sbinkertn@umich.edu            [ '#/AUTHORS', '#/LICENSE', '#/README', '#/RELEASE_NOTES' ],
4504554Sbinkertn@umich.edu            MakeAction(makeInfoPyFile, " [    INFO] $STRIP_TARGET"))
4514554Sbinkertn@umich.eduPySource('m5', 'python/m5/info.py')
4524554Sbinkertn@umich.edu
4534554Sbinkertn@umich.edu########################################################################
4544554Sbinkertn@umich.edu#
4554554Sbinkertn@umich.edu# Create all of the SimObject param headers and enum headers
4564781Snate@binkert.org#
4574554Sbinkertn@umich.edu
4584554Sbinkertn@umich.edudef createSimObjectParam(target, source, env):
4592667Sstever@eecs.umich.edu    assert len(target) == 1 and len(source) == 1
4604554Sbinkertn@umich.edu
4614554Sbinkertn@umich.edu    name = str(source[0].get_contents())
4624554Sbinkertn@umich.edu    obj = sim_objects[name]
4634554Sbinkertn@umich.edu
4642667Sstever@eecs.umich.edu    code = code_formatter()
4654554Sbinkertn@umich.edu    obj.cxx_decl(code)
4662667Sstever@eecs.umich.edu    code.write(target[0].abspath)
4674554Sbinkertn@umich.edu
4684554Sbinkertn@umich.edudef createSwigParam(target, source, env):
4692667Sstever@eecs.umich.edu    assert len(target) == 1 and len(source) == 1
4705522Snate@binkert.org
4715522Snate@binkert.org    name = str(source[0].get_contents())
4725522Snate@binkert.org    param = all_params[name]
4735522Snate@binkert.org
4745522Snate@binkert.org    code = code_formatter()
4755522Snate@binkert.org    code('%module(package="m5.internal") $0_${name}', param.file_ext)
4765522Snate@binkert.org    param.swig_decl(code)
4775522Snate@binkert.org    code.write(target[0].abspath)
4785522Snate@binkert.org
4795522Snate@binkert.orgdef createEnumStrings(target, source, env):
4805522Snate@binkert.org    assert len(target) == 1 and len(source) == 1
4815522Snate@binkert.org
4825522Snate@binkert.org    name = str(source[0].get_contents())
4835522Snate@binkert.org    obj = all_enums[name]
4845522Snate@binkert.org
4855522Snate@binkert.org    code = code_formatter()
4865522Snate@binkert.org    obj.cxx_def(code)
4875522Snate@binkert.org    code.write(target[0].abspath)
4885522Snate@binkert.org
4895522Snate@binkert.orgdef createEnumParam(target, source, env):
4905522Snate@binkert.org    assert len(target) == 1 and len(source) == 1
4915522Snate@binkert.org
4925522Snate@binkert.org    name = str(source[0].get_contents())
4935522Snate@binkert.org    obj = all_enums[name]
4945522Snate@binkert.org
4955522Snate@binkert.org    code = code_formatter()
4962638Sstever@eecs.umich.edu    obj.cxx_decl(code)
4972638Sstever@eecs.umich.edu    code.write(target[0].abspath)
4982638Sstever@eecs.umich.edu
4993716Sstever@eecs.umich.edudef createEnumSwig(target, source, env):
5005522Snate@binkert.org    assert len(target) == 1 and len(source) == 1
5015522Snate@binkert.org
5025522Snate@binkert.org    name = str(source[0].get_contents())
5035522Snate@binkert.org    obj = all_enums[name]
5045522Snate@binkert.org
5055522Snate@binkert.org    code = code_formatter()
5061858SN/A    code('''\
5075227Ssaidi@eecs.umich.edu%module(package="m5.internal") enum_$name
5085227Ssaidi@eecs.umich.edu
5095227Ssaidi@eecs.umich.edu%{
5105227Ssaidi@eecs.umich.edu#include "enums/$name.hh"
5115227Ssaidi@eecs.umich.edu%}
5125863Snate@binkert.org
5135227Ssaidi@eecs.umich.edu%include "enums/$name.hh"
5145227Ssaidi@eecs.umich.edu''')
5155227Ssaidi@eecs.umich.edu    code.write(target[0].abspath)
5165227Ssaidi@eecs.umich.edu
5175227Ssaidi@eecs.umich.edu# Generate all of the SimObject param struct header files
5185227Ssaidi@eecs.umich.eduparams_hh_files = []
5195227Ssaidi@eecs.umich.edufor name,simobj in sorted(sim_objects.iteritems()):
5205204Sstever@gmail.com    py_source = PySource.modules[simobj.__module__]
5215204Sstever@gmail.com    extra_deps = [ py_source.tnode ]
5225204Sstever@gmail.com
5235204Sstever@gmail.com    hh_file = File('params/%s.hh' % name)
5245204Sstever@gmail.com    params_hh_files.append(hh_file)
5255204Sstever@gmail.com    env.Command(hh_file, Value(name),
5265204Sstever@gmail.com                MakeAction(createSimObjectParam, " [SO PARAM] $STRIP_TARGET"))
5275204Sstever@gmail.com    env.Depends(hh_file, depends + extra_deps)
5285204Sstever@gmail.com
5295204Sstever@gmail.com# Generate any parameter header files needed
5305204Sstever@gmail.comparams_i_files = []
5315204Sstever@gmail.comfor name,param in all_params.iteritems():
5325204Sstever@gmail.com    i_file = File('python/m5/internal/%s_%s.i' % (param.file_ext, name))
5335204Sstever@gmail.com    params_i_files.append(i_file)
5345204Sstever@gmail.com    env.Command(i_file, Value(name),
5355204Sstever@gmail.com                MakeAction(createSwigParam, " [SW PARAM] $STRIP_TARGET"))
5365204Sstever@gmail.com    env.Depends(i_file, depends)
5375204Sstever@gmail.com    SwigSource('m5.internal', i_file)
5385204Sstever@gmail.com
5393118Sstever@eecs.umich.edu# Generate all enum header files
5403118Sstever@eecs.umich.edufor name,enum in sorted(all_enums.iteritems()):
5413118Sstever@eecs.umich.edu    py_source = PySource.modules[enum.__module__]
5423118Sstever@eecs.umich.edu    extra_deps = [ py_source.tnode ]
5433118Sstever@eecs.umich.edu
5445863Snate@binkert.org    cc_file = File('enums/%s.cc' % name)
5453118Sstever@eecs.umich.edu    env.Command(cc_file, Value(name),
5465863Snate@binkert.org                MakeAction(createEnumStrings, " [ENUM STR] $STRIP_TARGET"))
5473118Sstever@eecs.umich.edu    env.Depends(cc_file, depends + extra_deps)
5485863Snate@binkert.org    Source(cc_file)
5495863Snate@binkert.org
5505863Snate@binkert.org    hh_file = File('enums/%s.hh' % name)
5515863Snate@binkert.org    env.Command(hh_file, Value(name),
5525863Snate@binkert.org                MakeAction(createEnumParam, " [EN PARAM] $STRIP_TARGET"))
5535863Snate@binkert.org    env.Depends(hh_file, depends + extra_deps)
5545863Snate@binkert.org
5555863Snate@binkert.org    i_file = File('python/m5/internal/enum_%s.i' % name)
5566003Snate@binkert.org    env.Command(i_file, Value(name),
5575863Snate@binkert.org                MakeAction(createEnumSwig, " [ENUMSWIG] $STRIP_TARGET"))
5585863Snate@binkert.org    env.Depends(i_file, depends + extra_deps)
5595863Snate@binkert.org    SwigSource('m5.internal', i_file)
5606120Snate@binkert.org
5615863Snate@binkert.orgdef buildParam(target, source, env):
5625863Snate@binkert.org    name = source[0].get_contents()
5635863Snate@binkert.org    obj = sim_objects[name]
5646120Snate@binkert.org    class_path = obj.cxx_class.split('::')
5656120Snate@binkert.org    classname = class_path[-1]
5665863Snate@binkert.org    namespaces = class_path[:-1]
5675863Snate@binkert.org    params = obj._params.local.values()
5686120Snate@binkert.org
5695863Snate@binkert.org    code = code_formatter()
5705863Snate@binkert.org
5715863Snate@binkert.org    code('%module(package="m5.internal") param_$name')
5725863Snate@binkert.org    code()
5735863Snate@binkert.org    code('%{')
5743118Sstever@eecs.umich.edu    code('#include "params/$obj.hh"')
5755863Snate@binkert.org    for param in params:
5763118Sstever@eecs.umich.edu        param.cxx_predecls(code)
5773118Sstever@eecs.umich.edu    code('%}')
5785863Snate@binkert.org    code()
5795863Snate@binkert.org
5805863Snate@binkert.org    for param in params:
5815863Snate@binkert.org        param.swig_predecls(code)
5823118Sstever@eecs.umich.edu
5833483Ssaidi@eecs.umich.edu    code()
5843494Ssaidi@eecs.umich.edu    if obj._base:
5853494Ssaidi@eecs.umich.edu        code('%import "python/m5/internal/param_${{obj._base}}.i"')
5863483Ssaidi@eecs.umich.edu    code()
5873483Ssaidi@eecs.umich.edu    obj.swig_objdecls(code)
5883483Ssaidi@eecs.umich.edu    code()
5893053Sstever@eecs.umich.edu
5903053Sstever@eecs.umich.edu    code('%include "params/$obj.hh"')
5913918Ssaidi@eecs.umich.edu
5923053Sstever@eecs.umich.edu    code.write(target[0].abspath)
5933053Sstever@eecs.umich.edu
5943053Sstever@eecs.umich.edufor name in sim_objects.iterkeys():
5953053Sstever@eecs.umich.edu    params_file = File('python/m5/internal/param_%s.i' % name)
5963053Sstever@eecs.umich.edu    env.Command(params_file, Value(name),
5971858SN/A                MakeAction(buildParam, " [BLDPARAM] $STRIP_TARGET"))
5981858SN/A    env.Depends(params_file, depends)
5991858SN/A    SwigSource('m5.internal', params_file)
6001858SN/A
6011858SN/A# Generate the main swig init file
6021858SN/Adef makeEmbeddedSwigInit(target, source, env):
6035863Snate@binkert.org    code = code_formatter()
6045863Snate@binkert.org    module = source[0].get_contents()
6051859SN/A    code('''\
6065863Snate@binkert.org#include "sim/init.hh"
6071858SN/A
6085863Snate@binkert.orgextern "C" {
6091858SN/A    void init_${module}();
6101859SN/A}
6111859SN/A
6125863Snate@binkert.orgEmbeddedSwig embed_swig_${module}(init_${module});
6133053Sstever@eecs.umich.edu''')
6143053Sstever@eecs.umich.edu    code.write(str(target[0]))
6153053Sstever@eecs.umich.edu    
6163053Sstever@eecs.umich.edu# Build all swig modules
6171859SN/Afor swig in SwigSource.all:
6181859SN/A    env.Command([swig.cc_source.tnode, swig.py_source.tnode], swig.tnode,
6191859SN/A                MakeAction('$SWIG $SWIGFLAGS -outdir ${TARGETS[1].dir} '
6201859SN/A                '-o ${TARGETS[0]} $SOURCES', " [    SWIG] $STRIP_TARGET"))
6211859SN/A    init_file = 'python/swig/init_%s.cc' % swig.module
6221859SN/A    env.Command(init_file, Value(swig.module),
6231859SN/A                MakeAction(makeEmbeddedSwigInit, " [EMBED SW] $STRIP_TARGET"))
6241859SN/A    Source(init_file)
6251862SN/A
6261859SN/Adef getFlags(source_flags):
6271859SN/A    flagsMap = {}
6281859SN/A    flagsList = []
6295863Snate@binkert.org    for s in source_flags:
6305863Snate@binkert.org        val = eval(s.get_contents())
6315863Snate@binkert.org        name, compound, desc = val
6325863Snate@binkert.org        flagsList.append(val)
6331858SN/A        flagsMap[name] = bool(compound)
6341858SN/A    
6355863Snate@binkert.org    for name, compound, desc in flagsList:
6365863Snate@binkert.org        for flag in compound:
6375863Snate@binkert.org            if flag not in flagsMap:
6385863Snate@binkert.org                raise AttributeError, "Trace flag %s not found" % flag
6395863Snate@binkert.org            if flagsMap[flag]:
6405871Snate@binkert.org                raise AttributeError, \
6415871Snate@binkert.org                    "Compound flag can't point to another compound flag"
6422139SN/A
6434202Sbinkertn@umich.edu    flagsList.sort()
6444202Sbinkertn@umich.edu    return flagsList
6452139SN/A
6462155SN/A
6474202Sbinkertn@umich.edu# Generate traceflags.py
6484202Sbinkertn@umich.edudef traceFlagsPy(target, source, env):
6494202Sbinkertn@umich.edu    assert(len(target) == 1)
6502155SN/A    code = code_formatter()
6515863Snate@binkert.org
6521869SN/A    allFlags = getFlags(source)
6531869SN/A
6545863Snate@binkert.org    code('basic = [')
6555863Snate@binkert.org    code.indent()
6564202Sbinkertn@umich.edu    for flag, compound, desc in allFlags:
6576108Snate@binkert.org        if not compound:
6586108Snate@binkert.org            code("'$flag',")
6596108Snate@binkert.org    code(']')
6606108Snate@binkert.org    code.dedent()
6615863Snate@binkert.org    code()
6625863Snate@binkert.org
6635863Snate@binkert.org    code('compound = [')
6644202Sbinkertn@umich.edu    code.indent()
6654202Sbinkertn@umich.edu    code("'All',")
6665863Snate@binkert.org    for flag, compound, desc in allFlags:
6675742Snate@binkert.org        if compound:
6685742Snate@binkert.org            code("'$flag',")
6695341Sstever@gmail.com    code("]")
6705342Sstever@gmail.com    code.dedent()
6715342Sstever@gmail.com    code()
6724202Sbinkertn@umich.edu
6734202Sbinkertn@umich.edu    code("all = frozenset(basic + compound)")
6744202Sbinkertn@umich.edu    code()
6754202Sbinkertn@umich.edu
6764202Sbinkertn@umich.edu    code('compoundMap = {')
6775863Snate@binkert.org    code.indent()
6785863Snate@binkert.org    all = tuple([flag for flag,compound,desc in allFlags if not compound])
6795863Snate@binkert.org    code("'All' : $all,")
6805863Snate@binkert.org    for flag, compound, desc in allFlags:
6815863Snate@binkert.org        if compound:
6825863Snate@binkert.org            code("'$flag' : $compound,")
6835863Snate@binkert.org    code('}')
6845863Snate@binkert.org    code.dedent()
6855863Snate@binkert.org    code()
6865863Snate@binkert.org
6875863Snate@binkert.org    code('descriptions = {')
6885863Snate@binkert.org    code.indent()
6895863Snate@binkert.org    code("'All' : 'All flags',")
6905863Snate@binkert.org    for flag, compound, desc in allFlags:
6915863Snate@binkert.org        code("'$flag' : '$desc',")
6925863Snate@binkert.org    code("}")
6935863Snate@binkert.org    code.dedent()
6945863Snate@binkert.org
6955863Snate@binkert.org    code.write(str(target[0]))
6965863Snate@binkert.org
6975952Ssaidi@eecs.umich.edudef traceFlagsCC(target, source, env):
6981869SN/A    assert(len(target) == 1)
6991858SN/A
7005863Snate@binkert.org    allFlags = getFlags(source)
7015863Snate@binkert.org    code = code_formatter()
7021869SN/A
7031858SN/A    # file header
7045863Snate@binkert.org    code('''
7056108Snate@binkert.org/*
7066108Snate@binkert.org * DO NOT EDIT THIS FILE! Automatically generated
7076108Snate@binkert.org */
7081858SN/A
709955SN/A#include "base/traceflags.hh"
710955SN/A
7111869SN/Ausing namespace Trace;
7121869SN/A
7131869SN/Aconst char *Trace::flagStrings[] =
7141869SN/A{''')
7151869SN/A
7165863Snate@binkert.org    code.indent()
7175863Snate@binkert.org    # The string array is used by SimpleEnumParam to map the strings
7185863Snate@binkert.org    # provided by the user to enum values.
7191869SN/A    for flag, compound, desc in allFlags:
7205863Snate@binkert.org        if not compound:
7211869SN/A            code('"$flag",')
7225863Snate@binkert.org
7231869SN/A    code('"All",')
7241869SN/A    for flag, compound, desc in allFlags:
7251869SN/A        if compound:
7261869SN/A            code('"$flag",')
7271869SN/A    code.dedent()
7285863Snate@binkert.org
7295863Snate@binkert.org    code('''\
7301869SN/A};
7311869SN/A
7321869SN/Aconst int Trace::numFlagStrings = ${{len(allFlags) + 1}};
7331869SN/A
7341869SN/A''')
7351869SN/A
7361869SN/A    # Now define the individual compound flag arrays.  There is an array
7375863Snate@binkert.org    # for each compound flag listing the component base flags.
7385863Snate@binkert.org    all = tuple([flag for flag,compound,desc in allFlags if not compound])
7391869SN/A    code('static const Flags AllMap[] = {')
7405863Snate@binkert.org    code.indent()
7415863Snate@binkert.org    for flag, compound, desc in allFlags:
7423356Sbinkertn@umich.edu        if not compound:
7433356Sbinkertn@umich.edu            code('$flag,')
7443356Sbinkertn@umich.edu    code.dedent()
7453356Sbinkertn@umich.edu    code('};')
7463356Sbinkertn@umich.edu    code()
7474781Snate@binkert.org
7485863Snate@binkert.org    for flag, compound, desc in allFlags:
7495863Snate@binkert.org        if not compound:
7501869SN/A            continue
7511869SN/A        code('static const Flags ${flag}Map[] = {')
7521869SN/A        code.indent()
7531869SN/A        for flag in compound:
7541869SN/A            code('$flag,')
7552638Sstever@eecs.umich.edu        code('(Flags)-1')
7562638Sstever@eecs.umich.edu        code.dedent()
7575871Snate@binkert.org        code('};')
7582638Sstever@eecs.umich.edu        code()
7595749Scws3k@cs.virginia.edu
7605749Scws3k@cs.virginia.edu    # Finally the compoundFlags[] array maps the compound flags
7615871Snate@binkert.org    # to their individual arrays/
7625749Scws3k@cs.virginia.edu    code('const Flags *Trace::compoundFlags[] = {')
7631869SN/A    code.indent()
7641869SN/A    code('AllMap,')
7653546Sgblack@eecs.umich.edu    for flag, compound, desc in allFlags:
7663546Sgblack@eecs.umich.edu        if compound:
7673546Sgblack@eecs.umich.edu            code('${flag}Map,')
7683546Sgblack@eecs.umich.edu    # file trailer
7694202Sbinkertn@umich.edu    code.dedent()
7705863Snate@binkert.org    code('};')
7713546Sgblack@eecs.umich.edu
7723546Sgblack@eecs.umich.edu    code.write(str(target[0]))
7733546Sgblack@eecs.umich.edu
7743546Sgblack@eecs.umich.edudef traceFlagsHH(target, source, env):
7754781Snate@binkert.org    assert(len(target) == 1)
7765863Snate@binkert.org
7774781Snate@binkert.org    allFlags = getFlags(source)
7784781Snate@binkert.org    code = code_formatter()
7794781Snate@binkert.org
7804781Snate@binkert.org    # file header boilerplate
7814781Snate@binkert.org    code('''\
7825863Snate@binkert.org/*
7834781Snate@binkert.org * DO NOT EDIT THIS FILE!
7844781Snate@binkert.org *
7854781Snate@binkert.org * Automatically generated from traceflags.py
7864781Snate@binkert.org */
7873546Sgblack@eecs.umich.edu
7883546Sgblack@eecs.umich.edu#ifndef __BASE_TRACE_FLAGS_HH__
7893546Sgblack@eecs.umich.edu#define __BASE_TRACE_FLAGS_HH__
7904781Snate@binkert.org
7913546Sgblack@eecs.umich.edunamespace Trace {
7923546Sgblack@eecs.umich.edu
7933546Sgblack@eecs.umich.eduenum Flags {''')
7943546Sgblack@eecs.umich.edu
7953546Sgblack@eecs.umich.edu    # Generate the enum.  Base flags come first, then compound flags.
7963546Sgblack@eecs.umich.edu    idx = 0
7973546Sgblack@eecs.umich.edu    code.indent()
7983546Sgblack@eecs.umich.edu    for flag, compound, desc in allFlags:
7993546Sgblack@eecs.umich.edu        if not compound:
8003546Sgblack@eecs.umich.edu            code('$flag = $idx,')
8014202Sbinkertn@umich.edu            idx += 1
8023546Sgblack@eecs.umich.edu
8033546Sgblack@eecs.umich.edu    numBaseFlags = idx
8043546Sgblack@eecs.umich.edu    code('NumFlags = $idx,')
805955SN/A    code.dedent()
806955SN/A    code()
807955SN/A
808955SN/A    # put a comment in here to separate base from compound flags
8091858SN/A    code('''
8101858SN/A// The remaining enum values are *not* valid indices for Trace::flags.
8111858SN/A// They are "compound" flags, which correspond to sets of base
8125863Snate@binkert.org// flags, and are used by changeFlag.''')
8135863Snate@binkert.org
8145343Sstever@gmail.com    code.indent()
8155343Sstever@gmail.com    code('All = $idx,')
8165863Snate@binkert.org    idx += 1
8175863Snate@binkert.org    for flag, compound, desc in allFlags:
8184773Snate@binkert.org        if compound:
8195863Snate@binkert.org            code('$flag = $idx,')
8202632Sstever@eecs.umich.edu            idx += 1
8215863Snate@binkert.org
8222023SN/A    numCompoundFlags = idx - numBaseFlags
8235863Snate@binkert.org    code('NumCompoundFlags = $numCompoundFlags')
8245863Snate@binkert.org    code.dedent()
8255863Snate@binkert.org
8265863Snate@binkert.org    # trailer boilerplate
8275863Snate@binkert.org    code('''\
8285863Snate@binkert.org}; // enum Flags
8295863Snate@binkert.org
8305863Snate@binkert.org// Array of strings for SimpleEnumParam
8315863Snate@binkert.orgextern const char *flagStrings[];
8322632Sstever@eecs.umich.eduextern const int numFlagStrings;
8335863Snate@binkert.org
8342023SN/A// Array of arraay pointers: for each compound flag, gives the list of
8352632Sstever@eecs.umich.edu// base flags to set.  Inidividual flag arrays are terminated by -1.
8365863Snate@binkert.orgextern const Flags *compoundFlags[];
8375342Sstever@gmail.com
8385863Snate@binkert.org} // namespace Trace
8392632Sstever@eecs.umich.edu
8405863Snate@binkert.org#endif // __BASE_TRACE_FLAGS_HH__
8415863Snate@binkert.org''')
8422632Sstever@eecs.umich.edu
8435863Snate@binkert.org    code.write(str(target[0]))
8445863Snate@binkert.org
8455863Snate@binkert.orgflags = map(Value, trace_flags.values())
8465863Snate@binkert.orgenv.Command('base/traceflags.py', flags, 
8475863Snate@binkert.org            MakeAction(traceFlagsPy, " [ TRACING] $STRIP_TARGET"))
8485863Snate@binkert.orgPySource('m5', 'base/traceflags.py')
8492632Sstever@eecs.umich.edu
8505863Snate@binkert.orgenv.Command('base/traceflags.hh', flags,
8515863Snate@binkert.org            MakeAction(traceFlagsHH, " [ TRACING] $STRIP_TARGET"))
8522632Sstever@eecs.umich.eduenv.Command('base/traceflags.cc', flags, 
8531888SN/A            MakeAction(traceFlagsCC, " [ TRACING] $STRIP_TARGET"))
8545863Snate@binkert.orgSource('base/traceflags.cc')
8555863Snate@binkert.org
8565863Snate@binkert.org# Embed python files.  All .py files that have been indicated by a
8571858SN/A# PySource() call in a SConscript need to be embedded into the M5
8585863Snate@binkert.org# library.  To do that, we compile the file to byte code, marshal the
8595863Snate@binkert.org# byte code, compress it, and then generate a c++ file that
8605863Snate@binkert.org# inserts the result into an array.
8615863Snate@binkert.orgdef embedPyFile(target, source, env):
8622598SN/A    def c_str(string):
8635863Snate@binkert.org        if string is None:
8641858SN/A            return "0"
8651858SN/A        return '"%s"' % string
8661858SN/A
8675863Snate@binkert.org    '''Action function to compile a .py into a code object, marshal
8681858SN/A    it, compress it, and stick it into an asm file so the code appears
8691858SN/A    as just bytes with a label in the data section'''
8701858SN/A
8715863Snate@binkert.org    src = file(str(source[0]), 'r').read()
8721871SN/A
8731858SN/A    pysource = PySource.tnodes[source[0]]
8741858SN/A    compiled = compile(src, pysource.abspath, 'exec')
8751858SN/A    marshalled = marshal.dumps(compiled)
8761858SN/A    compressed = zlib.compress(marshalled)
8771858SN/A    data = compressed
8781858SN/A    sym = pysource.symname
8791858SN/A
8805863Snate@binkert.org    code = code_formatter()
8811858SN/A    code('''\
8821858SN/A#include "sim/init.hh"
8835863Snate@binkert.org
8841859SN/Anamespace {
8851859SN/A
8861869SN/Aconst char data_${sym}[] = {
8875863Snate@binkert.org''')
8885863Snate@binkert.org    code.indent()
8891869SN/A    step = 16
8901965SN/A    for i in xrange(0, len(data), step):
8911965SN/A        x = array.array('B', data[i:i+step])
8921965SN/A        code(''.join('%d,' % d for d in x))
8932761Sstever@eecs.umich.edu    code.dedent()
8945863Snate@binkert.org    
8951869SN/A    code('''};
8965863Snate@binkert.org
8972667Sstever@eecs.umich.eduEmbeddedPython embedded_${sym}(
8981869SN/A    ${{c_str(pysource.arcname)}},
8991869SN/A    ${{c_str(pysource.abspath)}},
9002929Sktlim@umich.edu    ${{c_str(pysource.modpath)}},
9012929Sktlim@umich.edu    data_${sym},
9025863Snate@binkert.org    ${{len(data)}},
9032929Sktlim@umich.edu    ${{len(marshalled)}});
904955SN/A
9052598SN/A} // anonymous namespace
906''')
907    code.write(str(target[0]))
908
909for source in PySource.all:
910    env.Command(source.cpp, source.tnode, 
911                MakeAction(embedPyFile, " [EMBED PY] $STRIP_TARGET"))
912    Source(source.cpp)
913
914########################################################################
915#
916# Define binaries.  Each different build type (debug, opt, etc.) gets
917# a slightly different build environment.
918#
919
920# List of constructed environments to pass back to SConstruct
921envList = []
922
923date_source = Source('base/date.cc', skip_lib=True)
924
925# Function to create a new build environment as clone of current
926# environment 'env' with modified object suffix and optional stripped
927# binary.  Additional keyword arguments are appended to corresponding
928# build environment vars.
929def makeEnv(label, objsfx, strip = False, **kwargs):
930    # SCons doesn't know to append a library suffix when there is a '.' in the
931    # name.  Use '_' instead.
932    libname = 'm5_' + label
933    exename = 'm5.' + label
934
935    new_env = env.Clone(OBJSUFFIX=objsfx, SHOBJSUFFIX=objsfx + 's')
936    new_env.Label = label
937    new_env.Append(**kwargs)
938
939    swig_env = new_env.Clone()
940    swig_env.Append(CCFLAGS='-Werror')
941    if env['GCC']:
942        swig_env.Append(CCFLAGS='-Wno-uninitialized')
943        swig_env.Append(CCFLAGS='-Wno-sign-compare')
944        swig_env.Append(CCFLAGS='-Wno-parentheses')
945
946    werror_env = new_env.Clone()
947    werror_env.Append(CCFLAGS='-Werror')
948
949    def make_obj(source, static, extra_deps = None):
950        '''This function adds the specified source to the correct
951        build environment, and returns the corresponding SCons Object
952        nodes'''
953
954        if source.swig:
955            env = swig_env
956        elif source.Werror:
957            env = werror_env
958        else:
959            env = new_env
960
961        if static:
962            obj = env.StaticObject(source.tnode)
963        else:
964            obj = env.SharedObject(source.tnode)
965
966        if extra_deps:
967            env.Depends(obj, extra_deps)
968
969        return obj
970
971    static_objs = [ make_obj(s, True) for s in Source.get(skip_lib=False)]
972    shared_objs = [ make_obj(s, False) for s in Source.get(skip_lib=False)]
973
974    static_date = make_obj(date_source, static=True, extra_deps=static_objs)
975    static_objs.append(static_date)
976    
977    shared_date = make_obj(date_source, static=False, extra_deps=shared_objs)
978    shared_objs.append(shared_date)
979
980    # First make a library of everything but main() so other programs can
981    # link against m5.
982    static_lib = new_env.StaticLibrary(libname, static_objs)
983    shared_lib = new_env.SharedLibrary(libname, shared_objs)
984
985    for target, sources in unit_tests:
986        objs = [ make_obj(s, static=True) for s in sources ]
987        new_env.Program("unittest/%s.%s" % (target, label), objs + static_objs)
988
989    # Now link a stub with main() and the static library.
990    bin_objs = [make_obj(s, True) for s in Source.get(bin_only=True) ]
991    progname = exename
992    if strip:
993        progname += '.unstripped'
994
995    targets = new_env.Program(progname, bin_objs + static_objs)
996
997    if strip:
998        if sys.platform == 'sunos5':
999            cmd = 'cp $SOURCE $TARGET; strip $TARGET'
1000        else:
1001            cmd = 'strip $SOURCE -o $TARGET'
1002        targets = new_env.Command(exename, progname,
1003                    MakeAction(cmd, " [   STRIP] $STRIP_TARGET"))
1004            
1005    new_env.M5Binary = targets[0]
1006    envList.append(new_env)
1007
1008# Debug binary
1009ccflags = {}
1010if env['GCC']:
1011    if sys.platform == 'sunos5':
1012        ccflags['debug'] = '-gstabs+'
1013    else:
1014        ccflags['debug'] = '-ggdb3'
1015    ccflags['opt'] = '-g -O3'
1016    ccflags['fast'] = '-O3'
1017    ccflags['prof'] = '-O3 -g -pg'
1018elif env['SUNCC']:
1019    ccflags['debug'] = '-g0'
1020    ccflags['opt'] = '-g -O'
1021    ccflags['fast'] = '-fast'
1022    ccflags['prof'] = '-fast -g -pg'
1023elif env['ICC']:
1024    ccflags['debug'] = '-g -O0'
1025    ccflags['opt'] = '-g -O'
1026    ccflags['fast'] = '-fast'
1027    ccflags['prof'] = '-fast -g -pg'
1028else:
1029    print 'Unknown compiler, please fix compiler options'
1030    Exit(1)
1031
1032makeEnv('debug', '.do',
1033        CCFLAGS = Split(ccflags['debug']),
1034        CPPDEFINES = ['DEBUG', 'TRACING_ON=1'])
1035
1036# Optimized binary
1037makeEnv('opt', '.o',
1038        CCFLAGS = Split(ccflags['opt']),
1039        CPPDEFINES = ['TRACING_ON=1'])
1040
1041# "Fast" binary
1042makeEnv('fast', '.fo', strip = True,
1043        CCFLAGS = Split(ccflags['fast']),
1044        CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'])
1045
1046# Profiled binary
1047makeEnv('prof', '.po',
1048        CCFLAGS = Split(ccflags['prof']),
1049        CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1050        LINKFLAGS = '-pg')
1051
1052Return('envList')
1053