SConscript revision 5623
1955SN/A# -*- mode:python -*-
2955SN/A
31762SN/A# Copyright (c) 2004-2005 The Regents of The University of Michigan
4955SN/A# All rights reserved.
5955SN/A#
6955SN/A# Redistribution and use in source and binary forms, with or without
7955SN/A# modification, are permitted provided that the following conditions are
8955SN/A# met: redistributions of source code must retain the above copyright
9955SN/A# notice, this list of conditions and the following disclaimer;
10955SN/A# redistributions in binary form must reproduce the above copyright
11955SN/A# notice, this list of conditions and the following disclaimer in the
12955SN/A# documentation and/or other materials provided with the distribution;
13955SN/A# neither the name of the copyright holders nor the names of its
14955SN/A# contributors may be used to endorse or promote products derived from
15955SN/A# this software without specific prior written permission.
16955SN/A#
17955SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18955SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19955SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20955SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21955SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22955SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23955SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24955SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25955SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26955SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27955SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
282665Ssaidi@eecs.umich.edu#
294762Snate@binkert.org# Authors: Nathan Binkert
30955SN/A
315522Snate@binkert.orgimport array
326143Snate@binkert.orgimport imp
334762Snate@binkert.orgimport marshal
345522Snate@binkert.orgimport os
35955SN/Aimport re
365522Snate@binkert.orgimport sys
37955SN/Aimport zlib
385522Snate@binkert.org
394202Sbinkertn@umich.edufrom os.path import basename, exists, isdir, isfile, join as joinpath
405742Snate@binkert.org
41955SN/Aimport SCons
424381Sbinkertn@umich.edu
434381Sbinkertn@umich.edu# This file defines how to build a particular configuration of M5
448334Snate@binkert.org# based on variable settings in the 'env' build environment.
45955SN/A
46955SN/AImport('*')
474202Sbinkertn@umich.edu
48955SN/A# Children need to see the environment
494382Sbinkertn@umich.eduExport('env')
504382Sbinkertn@umich.edu
514382Sbinkertn@umich.edubuild_env = dict([(opt, env[opt]) for opt in env.ExportOptions])
526654Snate@binkert.org
535517Snate@binkert.orgdef sort_list(_list):
548614Sgblack@eecs.umich.edu    """return a sorted copy of '_list'"""
557674Snate@binkert.org    if isinstance(_list, list):
566143Snate@binkert.org        _list = _list[:]
576143Snate@binkert.org    else:
586143Snate@binkert.org        _list = list(_list)
598233Snate@binkert.org    _list.sort()
608233Snate@binkert.org    return _list
618233Snate@binkert.org
628233Snate@binkert.orgclass PySourceFile(object):
638233Snate@binkert.org    invalid_sym_char = re.compile('[^A-z0-9_]')
648334Snate@binkert.org    def __init__(self, package, tnode):
658334Snate@binkert.org        snode = tnode.srcnode()
6610453SAndrew.Bardsley@arm.com        filename = str(tnode)
6710453SAndrew.Bardsley@arm.com        pyname = basename(filename)
688233Snate@binkert.org        assert pyname.endswith('.py')
698233Snate@binkert.org        name = pyname[:-3]
708233Snate@binkert.org        if package:
718233Snate@binkert.org            path = package.split('.')
728233Snate@binkert.org        else:
738233Snate@binkert.org            path = []
746143Snate@binkert.org
758233Snate@binkert.org        modpath = path[:]
768233Snate@binkert.org        if name != '__init__':
778233Snate@binkert.org            modpath += [name]
786143Snate@binkert.org        modpath = '.'.join(modpath)
796143Snate@binkert.org
806143Snate@binkert.org        arcpath = path + [ pyname ]
816143Snate@binkert.org        arcname = joinpath(*arcpath)
828233Snate@binkert.org
838233Snate@binkert.org        debugname = snode.abspath
848233Snate@binkert.org        if not exists(debugname):
856143Snate@binkert.org            debugname = tnode.abspath
868233Snate@binkert.org
878233Snate@binkert.org        self.tnode = tnode
888233Snate@binkert.org        self.snode = snode
898233Snate@binkert.org        self.pyname = pyname
906143Snate@binkert.org        self.package = package
916143Snate@binkert.org        self.modpath = modpath
926143Snate@binkert.org        self.arcname = arcname
934762Snate@binkert.org        self.debugname = debugname
946143Snate@binkert.org        self.compiled = File(filename + 'c')
958233Snate@binkert.org        self.assembly = File(filename + '.s')
968233Snate@binkert.org        self.symname = "PyEMB_" + self.invalid_sym_char.sub('_', modpath)
978233Snate@binkert.org        
988233Snate@binkert.org
998233Snate@binkert.org########################################################################
1006143Snate@binkert.org# Code for adding source files of various types
1018233Snate@binkert.org#
1028233Snate@binkert.orgcc_lib_sources = []
1038233Snate@binkert.orgdef Source(source):
1048233Snate@binkert.org    '''Add a source file to the libm5 build'''
1056143Snate@binkert.org    if not isinstance(source, SCons.Node.FS.File):
1066143Snate@binkert.org        source = File(source)
1076143Snate@binkert.org
1086143Snate@binkert.org    cc_lib_sources.append(source)
1096143Snate@binkert.org
1106143Snate@binkert.orgcc_bin_sources = []
1116143Snate@binkert.orgdef BinSource(source):
1126143Snate@binkert.org    '''Add a source file to the m5 binary build'''
1136143Snate@binkert.org    if not isinstance(source, SCons.Node.FS.File):
1147065Snate@binkert.org        source = File(source)
1156143Snate@binkert.org
1168233Snate@binkert.org    cc_bin_sources.append(source)
1178233Snate@binkert.org
1188233Snate@binkert.orgpy_sources = []
1198233Snate@binkert.orgdef PySource(package, source):
1208233Snate@binkert.org    '''Add a python source file to the named package'''
1218233Snate@binkert.org    if not isinstance(source, SCons.Node.FS.File):
1228233Snate@binkert.org        source = File(source)
1238233Snate@binkert.org
1248233Snate@binkert.org    source = PySourceFile(package, source)
1258233Snate@binkert.org    py_sources.append(source)
1268233Snate@binkert.org
1278233Snate@binkert.orgsim_objects_fixed = False
1288233Snate@binkert.orgsim_object_modfiles = set()
1298233Snate@binkert.orgdef SimObject(source):
1308233Snate@binkert.org    '''Add a SimObject python file as a python source object and add
1318233Snate@binkert.org    it to a list of sim object modules'''
1328233Snate@binkert.org
1338233Snate@binkert.org    if sim_objects_fixed:
1348233Snate@binkert.org        raise AttributeError, "Too late to call SimObject now."
1358233Snate@binkert.org
1368233Snate@binkert.org    if not isinstance(source, SCons.Node.FS.File):
1378233Snate@binkert.org        source = File(source)
1388233Snate@binkert.org
1398233Snate@binkert.org    PySource('m5.objects', source)
1408233Snate@binkert.org    modfile = basename(str(source))
1418233Snate@binkert.org    assert modfile.endswith('.py')
1428233Snate@binkert.org    modname = modfile[:-3]
1438233Snate@binkert.org    sim_object_modfiles.add(modname)
1448233Snate@binkert.org
1458233Snate@binkert.orgswig_sources = []
1468233Snate@binkert.orgdef SwigSource(package, source):
1476143Snate@binkert.org    '''Add a swig file to build'''
1486143Snate@binkert.org    if not isinstance(source, SCons.Node.FS.File):
1496143Snate@binkert.org        source = File(source)
1506143Snate@binkert.org    val = source,package
1516143Snate@binkert.org    swig_sources.append(val)
1526143Snate@binkert.org
1539982Satgutier@umich.eduunit_tests = []
15410196SCurtis.Dunham@arm.comdef UnitTest(target, sources):
15510196SCurtis.Dunham@arm.com    if not isinstance(sources, (list, tuple)):
15610196SCurtis.Dunham@arm.com        sources = [ sources ]
15710196SCurtis.Dunham@arm.com    
15810196SCurtis.Dunham@arm.com    srcs = []
15910196SCurtis.Dunham@arm.com    for source in sources:
16010196SCurtis.Dunham@arm.com        if not isinstance(source, SCons.Node.FS.File):
16110196SCurtis.Dunham@arm.com            source = File(source)
1626143Snate@binkert.org        srcs.append(source)
1636143Snate@binkert.org            
1648945Ssteve.reinhardt@amd.com    unit_tests.append((target, srcs))
1658233Snate@binkert.org
1668233Snate@binkert.org# Children should have access
1676143Snate@binkert.orgExport('Source')
1688945Ssteve.reinhardt@amd.comExport('BinSource')
1696143Snate@binkert.orgExport('PySource')
1706143Snate@binkert.orgExport('SimObject')
1716143Snate@binkert.orgExport('SwigSource')
1726143Snate@binkert.orgExport('UnitTest')
1735522Snate@binkert.org
1746143Snate@binkert.org########################################################################
1756143Snate@binkert.org#
1766143Snate@binkert.org# Trace Flags
1779982Satgutier@umich.edu#
1788233Snate@binkert.orgall_flags = {}
1798233Snate@binkert.orgtrace_flags = []
1808233Snate@binkert.orgdef TraceFlag(name, desc=''):
1816143Snate@binkert.org    if name in all_flags:
1826143Snate@binkert.org        raise AttributeError, "Flag %s already specified" % name
1836143Snate@binkert.org    flag = (name, (), desc)
1846143Snate@binkert.org    trace_flags.append(flag)
1855522Snate@binkert.org    all_flags[name] = ()
1865522Snate@binkert.org
1875522Snate@binkert.orgdef CompoundFlag(name, flags, desc=''):
1885522Snate@binkert.org    if name in all_flags:
1895604Snate@binkert.org        raise AttributeError, "Flag %s already specified" % name
1905604Snate@binkert.org
1916143Snate@binkert.org    compound = tuple(flags)
1926143Snate@binkert.org    for flag in compound:
1934762Snate@binkert.org        if flag not in all_flags:
1944762Snate@binkert.org            raise AttributeError, "Trace flag %s not found" % flag
1956143Snate@binkert.org        if all_flags[flag]:
1966727Ssteve.reinhardt@amd.com            raise AttributeError, \
1976727Ssteve.reinhardt@amd.com                "Compound flag can't point to another compound flag"
1986727Ssteve.reinhardt@amd.com
1994762Snate@binkert.org    flag = (name, compound, desc)
2006143Snate@binkert.org    trace_flags.append(flag)
2016143Snate@binkert.org    all_flags[name] = compound
2026143Snate@binkert.org
2036143Snate@binkert.orgExport('TraceFlag')
2046727Ssteve.reinhardt@amd.comExport('CompoundFlag')
2056143Snate@binkert.org
2067674Snate@binkert.org########################################################################
2077674Snate@binkert.org#
2085604Snate@binkert.org# Set some compiler variables
2096143Snate@binkert.org#
2106143Snate@binkert.org
2116143Snate@binkert.org# Include file paths are rooted in this directory.  SCons will
2124762Snate@binkert.org# automatically expand '.' to refer to both the source directory and
2136143Snate@binkert.org# the corresponding build directory to pick up generated include
2144762Snate@binkert.org# files.
2154762Snate@binkert.orgenv.Append(CPPPATH=Dir('.'))
2164762Snate@binkert.org
2176143Snate@binkert.org# Add a flag defining what THE_ISA should be for all compilation
2186143Snate@binkert.orgenv.Append(CPPDEFINES=[('THE_ISA','%s_ISA' % env['TARGET_ISA'].upper())])
2194762Snate@binkert.org
2208233Snate@binkert.org########################################################################
2218233Snate@binkert.org#
2228233Snate@binkert.org# Walk the tree and execute all SConscripts in subdirectories
2238233Snate@binkert.org#
2246143Snate@binkert.org
2256143Snate@binkert.orgfor base_dir in base_dir_list:
2264762Snate@binkert.org    here = Dir('.').srcnode().abspath
2276143Snate@binkert.org    for root, dirs, files in os.walk(base_dir, topdown=True):
2284762Snate@binkert.org        if root == here:
2296143Snate@binkert.org            # we don't want to recurse back into this SConscript
2304762Snate@binkert.org            continue
2316143Snate@binkert.org
2328233Snate@binkert.org        if 'SConscript' in files:
2338233Snate@binkert.org            build_dir = joinpath(env['BUILDDIR'], root[len(base_dir) + 1:])
23410453SAndrew.Bardsley@arm.com            SConscript(joinpath(root, 'SConscript'), build_dir=build_dir)
2356143Snate@binkert.org
2366143Snate@binkert.orgfor opt in env.ExportOptions:
2376143Snate@binkert.org    env.ConfigFile(opt)
2386143Snate@binkert.org
2396143Snate@binkert.org########################################################################
2406143Snate@binkert.org#
2416143Snate@binkert.org# Prevent any SimObjects from being added after this point, they
2426143Snate@binkert.org# should all have been added in the SConscripts above
24310453SAndrew.Bardsley@arm.com#
24410453SAndrew.Bardsley@arm.comclass DictImporter(object):
245955SN/A    '''This importer takes a dictionary of arbitrary module names that
2469396Sandreas.hansson@arm.com    map to arbitrary filenames.'''
2479396Sandreas.hansson@arm.com    def __init__(self, modules):
2489396Sandreas.hansson@arm.com        self.modules = modules
2499396Sandreas.hansson@arm.com        self.installed = set()
2509396Sandreas.hansson@arm.com
2519396Sandreas.hansson@arm.com    def __del__(self):
2529396Sandreas.hansson@arm.com        self.unload()
2539396Sandreas.hansson@arm.com
2549396Sandreas.hansson@arm.com    def unload(self):
2559396Sandreas.hansson@arm.com        import sys
2569396Sandreas.hansson@arm.com        for module in self.installed:
2579396Sandreas.hansson@arm.com            del sys.modules[module]
2589396Sandreas.hansson@arm.com        self.installed = set()
2599930Sandreas.hansson@arm.com
2609930Sandreas.hansson@arm.com    def find_module(self, fullname, path):
2619396Sandreas.hansson@arm.com        if fullname == '__scons':
2628235Snate@binkert.org            return self
2638235Snate@binkert.org
2646143Snate@binkert.org        if fullname == 'm5.objects':
2658235Snate@binkert.org            return self
2669003SAli.Saidi@ARM.com
2678235Snate@binkert.org        if fullname.startswith('m5.internal'):
2688235Snate@binkert.org            return None
2698235Snate@binkert.org
2708235Snate@binkert.org        if fullname in self.modules and exists(self.modules[fullname]):
2718235Snate@binkert.org            return self
2728235Snate@binkert.org
2738235Snate@binkert.org        return None
2748235Snate@binkert.org
2758235Snate@binkert.org    def load_module(self, fullname):
2768235Snate@binkert.org        mod = imp.new_module(fullname)
2778235Snate@binkert.org        sys.modules[fullname] = mod
2788235Snate@binkert.org        self.installed.add(fullname)
2798235Snate@binkert.org
2808235Snate@binkert.org        mod.__loader__ = self
2819003SAli.Saidi@ARM.com        if fullname == 'm5.objects':
2828235Snate@binkert.org            mod.__path__ = fullname.split('.')
2835584Snate@binkert.org            return mod
2844382Sbinkertn@umich.edu
2854202Sbinkertn@umich.edu        if fullname == '__scons':
2864382Sbinkertn@umich.edu            mod.__dict__['m5_build_env'] = build_env
2874382Sbinkertn@umich.edu            return mod
2884382Sbinkertn@umich.edu
2899396Sandreas.hansson@arm.com        srcfile = self.modules[fullname]
2905584Snate@binkert.org        if basename(srcfile) == '__init__.py':
2914382Sbinkertn@umich.edu            mod.__path__ = fullname.split('.')
2924382Sbinkertn@umich.edu        mod.__file__ = srcfile
2934382Sbinkertn@umich.edu
2948232Snate@binkert.org        exec file(srcfile, 'r') in mod.__dict__
2955192Ssaidi@eecs.umich.edu
2968232Snate@binkert.org        return mod
2978232Snate@binkert.org
2988232Snate@binkert.orgpy_modules = {}
2995192Ssaidi@eecs.umich.edufor source in py_sources:
3008232Snate@binkert.org    py_modules[source.modpath] = source.snode.abspath
3015192Ssaidi@eecs.umich.edu
3025799Snate@binkert.org# install the python importer so we can grab stuff from the source
3038232Snate@binkert.org# tree itself.  We can't have SimObjects added after this point or
3045192Ssaidi@eecs.umich.edu# else we won't know about them for the rest of the stuff.
3055192Ssaidi@eecs.umich.edusim_objects_fixed = True
3065192Ssaidi@eecs.umich.eduimporter = DictImporter(py_modules)
3078232Snate@binkert.orgsys.meta_path[0:0] = [ importer ]
3085192Ssaidi@eecs.umich.edu
3098232Snate@binkert.orgimport m5
3105192Ssaidi@eecs.umich.edu
3115192Ssaidi@eecs.umich.edu# import all sim objects so we can populate the all_objects list
3125192Ssaidi@eecs.umich.edu# make sure that we're working with a list, then let's sort it
3135192Ssaidi@eecs.umich.edusim_objects = list(sim_object_modfiles)
3144382Sbinkertn@umich.edusim_objects.sort()
3154382Sbinkertn@umich.edufor simobj in sim_objects:
3164382Sbinkertn@umich.edu    exec('from m5.objects import %s' % simobj)
3172667Sstever@eecs.umich.edu
3182667Sstever@eecs.umich.edu# we need to unload all of the currently imported modules so that they
3192667Sstever@eecs.umich.edu# will be re-imported the next time the sconscript is run
3202667Sstever@eecs.umich.eduimporter.unload()
3212667Sstever@eecs.umich.edusys.meta_path.remove(importer)
3222667Sstever@eecs.umich.edu
3235742Snate@binkert.orgsim_objects = m5.SimObject.allClasses
3245742Snate@binkert.orgall_enums = m5.params.allEnums
3255742Snate@binkert.org
3265793Snate@binkert.orgall_params = {}
3278334Snate@binkert.orgfor name,obj in sim_objects.iteritems():
3285793Snate@binkert.org    for param in obj._params.local.values():
3295793Snate@binkert.org        if not hasattr(param, 'swig_decl'):
3305793Snate@binkert.org            continue
3314382Sbinkertn@umich.edu        pname = param.ptype_str
3324762Snate@binkert.org        if pname not in all_params:
3335344Sstever@gmail.com            all_params[pname] = param
3344382Sbinkertn@umich.edu
3355341Sstever@gmail.com########################################################################
3365742Snate@binkert.org#
3375742Snate@binkert.org# calculate extra dependencies
3385742Snate@binkert.org#
3395742Snate@binkert.orgmodule_depends = ["m5", "m5.SimObject", "m5.params"]
3405742Snate@binkert.orgdepends = [ File(py_modules[dep]) for dep in module_depends ]
3414762Snate@binkert.org
3425742Snate@binkert.org########################################################################
3435742Snate@binkert.org#
3447722Sgblack@eecs.umich.edu# Commands for the basic automatically generated python files
3455742Snate@binkert.org#
3465742Snate@binkert.org
3475742Snate@binkert.org# Generate Python file containing a dict specifying the current
3489930Sandreas.hansson@arm.com# build_env flags.
3499930Sandreas.hansson@arm.comdef makeDefinesPyFile(target, source, env):
3509930Sandreas.hansson@arm.com    f = file(str(target[0]), 'w')
3519930Sandreas.hansson@arm.com    print >>f, "m5_build_env = ", source[0]
3529930Sandreas.hansson@arm.com    f.close()
3535742Snate@binkert.org
3548242Sbradley.danofsky@amd.com# Generate python file containing info about the M5 source code
3558242Sbradley.danofsky@amd.comdef makeInfoPyFile(target, source, env):
3568242Sbradley.danofsky@amd.com    f = file(str(target[0]), 'w')
3578242Sbradley.danofsky@amd.com    for src in source:
3585341Sstever@gmail.com        data = ''.join(file(src.srcnode().abspath, 'r').xreadlines())
3595742Snate@binkert.org        print >>f, "%s = %s" % (src, repr(data))
3607722Sgblack@eecs.umich.edu    f.close()
3614773Snate@binkert.org
3626108Snate@binkert.org# Generate the __init__.py file for m5.objects
3631858SN/Adef makeObjectsInitFile(target, source, env):
3641085SN/A    f = file(str(target[0]), 'w')
3656658Snate@binkert.org    print >>f, 'from params import *'
3666658Snate@binkert.org    print >>f, 'from m5.SimObject import *'
3677673Snate@binkert.org    for module in source:
3686658Snate@binkert.org        print >>f, 'from %s import *' % module.get_contents()
3696658Snate@binkert.org    f.close()
3706658Snate@binkert.org
3716658Snate@binkert.org# Generate a file with all of the compile options in it
3726658Snate@binkert.orgenv.Command('python/m5/defines.py', Value(build_env), makeDefinesPyFile)
3736658Snate@binkert.orgPySource('m5', 'python/m5/defines.py')
3746658Snate@binkert.org
3757673Snate@binkert.org# Generate a file that wraps the basic top level files
3767673Snate@binkert.orgenv.Command('python/m5/info.py',
3777673Snate@binkert.org            [ '#/AUTHORS', '#/LICENSE', '#/README', '#/RELEASE_NOTES' ],
3787673Snate@binkert.org            makeInfoPyFile)
3797673Snate@binkert.orgPySource('m5', 'python/m5/info.py')
3807673Snate@binkert.org
3817673Snate@binkert.org# Generate an __init__.py file for the objects package
38210467Sandreas.hansson@arm.comenv.Command('python/m5/objects/__init__.py',
3836658Snate@binkert.org            [ Value(o) for o in sort_list(sim_object_modfiles) ],
3847673Snate@binkert.org            makeObjectsInitFile)
38510467Sandreas.hansson@arm.comPySource('m5.objects', 'python/m5/objects/__init__.py')
38610467Sandreas.hansson@arm.com
38710467Sandreas.hansson@arm.com########################################################################
38810467Sandreas.hansson@arm.com#
38910467Sandreas.hansson@arm.com# Create all of the SimObject param headers and enum headers
39010467Sandreas.hansson@arm.com#
39110467Sandreas.hansson@arm.com
39210467Sandreas.hansson@arm.comdef createSimObjectParam(target, source, env):
39310467Sandreas.hansson@arm.com    assert len(target) == 1 and len(source) == 1
39410467Sandreas.hansson@arm.com
39510467Sandreas.hansson@arm.com    hh_file = file(target[0].abspath, 'w')
3967673Snate@binkert.org    name = str(source[0].get_contents())
3977673Snate@binkert.org    obj = sim_objects[name]
3987673Snate@binkert.org
3997673Snate@binkert.org    print >>hh_file, obj.cxx_decl()
4007673Snate@binkert.org
4019048SAli.Saidi@ARM.comdef createSwigParam(target, source, env):
4027673Snate@binkert.org    assert len(target) == 1 and len(source) == 1
4037673Snate@binkert.org
4047673Snate@binkert.org    i_file = file(target[0].abspath, 'w')
4057673Snate@binkert.org    name = str(source[0].get_contents())
4066658Snate@binkert.org    param = all_params[name]
4077756SAli.Saidi@ARM.com
4087816Ssteve.reinhardt@amd.com    for line in param.swig_decl():
4096658Snate@binkert.org        print >>i_file, line
4104382Sbinkertn@umich.edu
4114382Sbinkertn@umich.edudef createEnumStrings(target, source, env):
4124762Snate@binkert.org    assert len(target) == 1 and len(source) == 1
4134762Snate@binkert.org
4144762Snate@binkert.org    cc_file = file(target[0].abspath, 'w')
4156654Snate@binkert.org    name = str(source[0].get_contents())
4166654Snate@binkert.org    obj = all_enums[name]
4175517Snate@binkert.org
4185517Snate@binkert.org    print >>cc_file, obj.cxx_def()
4195517Snate@binkert.org    cc_file.close()
4205517Snate@binkert.org
4215517Snate@binkert.orgdef createEnumParam(target, source, env):
4225517Snate@binkert.org    assert len(target) == 1 and len(source) == 1
4235517Snate@binkert.org
4245517Snate@binkert.org    hh_file = file(target[0].abspath, 'w')
4255517Snate@binkert.org    name = str(source[0].get_contents())
4265517Snate@binkert.org    obj = all_enums[name]
4275517Snate@binkert.org
4285517Snate@binkert.org    print >>hh_file, obj.cxx_decl()
4295517Snate@binkert.org
4305517Snate@binkert.org# Generate all of the SimObject param struct header files
4315517Snate@binkert.orgparams_hh_files = []
4325517Snate@binkert.orgfor name,simobj in sim_objects.iteritems():
4335517Snate@binkert.org    extra_deps = [ File(py_modules[simobj.__module__]) ]
4346654Snate@binkert.org
4355517Snate@binkert.org    hh_file = File('params/%s.hh' % name)
4365517Snate@binkert.org    params_hh_files.append(hh_file)
4375517Snate@binkert.org    env.Command(hh_file, Value(name), createSimObjectParam)
4385517Snate@binkert.org    env.Depends(hh_file, depends + extra_deps)
4395517Snate@binkert.org
4405517Snate@binkert.org# Generate any parameter header files needed
4415517Snate@binkert.orgparams_i_files = []
4425517Snate@binkert.orgfor name,param in all_params.iteritems():
4436143Snate@binkert.org    if isinstance(param, m5.params.VectorParamDesc):
4446654Snate@binkert.org        ext = 'vptype'
4455517Snate@binkert.org    else:
4465517Snate@binkert.org        ext = 'ptype'
4475517Snate@binkert.org
4485517Snate@binkert.org    i_file = File('params/%s_%s.i' % (name, ext))
4495517Snate@binkert.org    params_i_files.append(i_file)
4505517Snate@binkert.org    env.Command(i_file, Value(name), createSwigParam)
4515517Snate@binkert.org    env.Depends(i_file, depends)
4525517Snate@binkert.org
4535517Snate@binkert.org# Generate all enum header files
4545517Snate@binkert.orgfor name,enum in all_enums.iteritems():
4555517Snate@binkert.org    extra_deps = [ File(py_modules[enum.__module__]) ]
4565517Snate@binkert.org
4575517Snate@binkert.org    cc_file = File('enums/%s.cc' % name)
4585517Snate@binkert.org    env.Command(cc_file, Value(name), createEnumStrings)
4596654Snate@binkert.org    env.Depends(cc_file, depends + extra_deps)
4606654Snate@binkert.org    Source(cc_file)
4615517Snate@binkert.org
4625517Snate@binkert.org    hh_file = File('enums/%s.hh' % name)
4636143Snate@binkert.org    env.Command(hh_file, Value(name), createEnumParam)
4646143Snate@binkert.org    env.Depends(hh_file, depends + extra_deps)
4656143Snate@binkert.org
4666727Ssteve.reinhardt@amd.com# Build the big monolithic swigged params module (wraps all SimObject
4675517Snate@binkert.org# param structs and enum structs)
4686727Ssteve.reinhardt@amd.comdef buildParams(target, source, env):
4695517Snate@binkert.org    names = [ s.get_contents() for s in source ]
4705517Snate@binkert.org    objs = [ sim_objects[name] for name in names ]
4715517Snate@binkert.org    out = file(target[0].abspath, 'w')
4726654Snate@binkert.org
4736654Snate@binkert.org    ordered_objs = []
4747673Snate@binkert.org    obj_seen = set()
4756654Snate@binkert.org    def order_obj(obj):
4766654Snate@binkert.org        name = str(obj)
4776654Snate@binkert.org        if name in obj_seen:
4786654Snate@binkert.org            return
4795517Snate@binkert.org
4805517Snate@binkert.org        obj_seen.add(name)
4815517Snate@binkert.org        if str(obj) != 'SimObject':
4826143Snate@binkert.org            order_obj(obj.__bases__[0])
4835517Snate@binkert.org
4844762Snate@binkert.org        ordered_objs.append(obj)
4855517Snate@binkert.org
4865517Snate@binkert.org    for obj in objs:
4876143Snate@binkert.org        order_obj(obj)
4886143Snate@binkert.org
4895517Snate@binkert.org    enums = set()
4905517Snate@binkert.org    predecls = []
4915517Snate@binkert.org    pd_seen = set()
4925517Snate@binkert.org
4935517Snate@binkert.org    def add_pds(*pds):
4945517Snate@binkert.org        for pd in pds:
4955517Snate@binkert.org            if pd not in pd_seen:
4965517Snate@binkert.org                predecls.append(pd)
4975517Snate@binkert.org                pd_seen.add(pd)
4989338SAndreas.Sandberg@arm.com
4999338SAndreas.Sandberg@arm.com    for obj in ordered_objs:
5009338SAndreas.Sandberg@arm.com        params = obj._params.local.values()
5019338SAndreas.Sandberg@arm.com        for param in params:
5029338SAndreas.Sandberg@arm.com            ptype = param.ptype
5039338SAndreas.Sandberg@arm.com            if issubclass(ptype, m5.params.Enum):
5048596Ssteve.reinhardt@amd.com                if ptype not in enums:
5058596Ssteve.reinhardt@amd.com                    enums.add(ptype)
5068596Ssteve.reinhardt@amd.com            pds = param.swig_predecls()
5078596Ssteve.reinhardt@amd.com            if isinstance(pds, (list, tuple)):
5088596Ssteve.reinhardt@amd.com                add_pds(*pds)
5098596Ssteve.reinhardt@amd.com            else:
5108596Ssteve.reinhardt@amd.com                add_pds(pds)
5116143Snate@binkert.org
5125517Snate@binkert.org    print >>out, '%module params'
5136654Snate@binkert.org
5146654Snate@binkert.org    print >>out, '%{'
5156654Snate@binkert.org    for obj in ordered_objs:
5166654Snate@binkert.org        print >>out, '#include "params/%s.hh"' % obj
5176654Snate@binkert.org    print >>out, '%}'
5186654Snate@binkert.org
5195517Snate@binkert.org    for pd in predecls:
5205517Snate@binkert.org        print >>out, pd
5215517Snate@binkert.org
5228596Ssteve.reinhardt@amd.com    enums = list(enums)
5238596Ssteve.reinhardt@amd.com    enums.sort()
5244762Snate@binkert.org    for enum in enums:
5254762Snate@binkert.org        print >>out, '%%include "enums/%s.hh"' % enum.__name__
5264762Snate@binkert.org    print >>out
5274762Snate@binkert.org
5284762Snate@binkert.org    for obj in ordered_objs:
5294762Snate@binkert.org        if obj.swig_objdecls:
5307675Snate@binkert.org            for decl in obj.swig_objdecls:
53110584Sandreas.hansson@arm.com                print >>out, decl
5324762Snate@binkert.org            continue
5334762Snate@binkert.org
5344762Snate@binkert.org        class_path = obj.cxx_class.split('::')
5354762Snate@binkert.org        classname = class_path[-1]
5364382Sbinkertn@umich.edu        namespaces = class_path[:-1]
5374382Sbinkertn@umich.edu        namespaces.reverse()
5385517Snate@binkert.org
5396654Snate@binkert.org        code = ''
5405517Snate@binkert.org
5418126Sgblack@eecs.umich.edu        if namespaces:
5426654Snate@binkert.org            code += '// avoid name conflicts\n'
5437673Snate@binkert.org            sep_string = '_COLONS_'
5446654Snate@binkert.org            flat_name = sep_string.join(class_path)
5456654Snate@binkert.org            code += '%%rename(%s) %s;\n' % (flat_name, classname)
5466654Snate@binkert.org
5476654Snate@binkert.org        code += '// stop swig from creating/wrapping default ctor/dtor\n'
5486654Snate@binkert.org        code += '%%nodefault %s;\n' % classname
5496654Snate@binkert.org        code += 'class %s ' % classname
5506654Snate@binkert.org        if obj._base:
5516669Snate@binkert.org            code += ': public %s' % obj._base.cxx_class
5526669Snate@binkert.org        code += ' {};\n'
5536669Snate@binkert.org
5546669Snate@binkert.org        for ns in namespaces:
5556669Snate@binkert.org            new_code = 'namespace %s {\n' % ns
5566669Snate@binkert.org            new_code += code
5576654Snate@binkert.org            new_code += '}\n'
5587673Snate@binkert.org            code = new_code
5595517Snate@binkert.org
5608126Sgblack@eecs.umich.edu        print >>out, code
5615798Snate@binkert.org
5627756SAli.Saidi@ARM.com    print >>out, '%%include "src/sim/sim_object_params.hh"' % obj
5637816Ssteve.reinhardt@amd.com    for obj in ordered_objs:
5645798Snate@binkert.org        print >>out, '%%include "params/%s.hh"' % obj
5655798Snate@binkert.org
5665517Snate@binkert.orgparams_file = File('params/params.i')
5675517Snate@binkert.orgnames = sort_list(sim_objects.keys())
5687673Snate@binkert.orgenv.Command(params_file, [ Value(v) for v in names ], buildParams)
5695517Snate@binkert.orgenv.Depends(params_file, params_hh_files + params_i_files + depends)
5705517Snate@binkert.orgSwigSource('m5.objects', params_file)
5717673Snate@binkert.org
5727673Snate@binkert.org# Build all swig modules
5735517Snate@binkert.orgswig_modules = []
5745798Snate@binkert.orgcc_swig_sources = []
5755798Snate@binkert.orgfor source,package in swig_sources:
5768333Snate@binkert.org    filename = str(source)
5777816Ssteve.reinhardt@amd.com    assert filename.endswith('.i')
5785798Snate@binkert.org
5795798Snate@binkert.org    base = '.'.join(filename.split('.')[:-1])
5804762Snate@binkert.org    module = basename(base)
5814762Snate@binkert.org    cc_file = base + '_wrap.cc'
5824762Snate@binkert.org    py_file = base + '.py'
5834762Snate@binkert.org
5844762Snate@binkert.org    env.Command([cc_file, py_file], source,
5858596Ssteve.reinhardt@amd.com                '$SWIG $SWIGFLAGS -outdir ${TARGETS[1].dir} '
5865517Snate@binkert.org                '-o ${TARGETS[0]} $SOURCES')
5875517Snate@binkert.org    env.Depends(py_file, source)
5885517Snate@binkert.org    env.Depends(cc_file, source)
5895517Snate@binkert.org
5905517Snate@binkert.org    swig_modules.append(Value(module))
5917673Snate@binkert.org    cc_swig_sources.append(File(cc_file))
5928596Ssteve.reinhardt@amd.com    PySource(package, py_file)
5937673Snate@binkert.org
5945517Snate@binkert.org# Generate the main swig init file
59510458Sandreas.hansson@arm.comdef makeSwigInit(target, source, env):
59610458Sandreas.hansson@arm.com    f = file(str(target[0]), 'w')
59710458Sandreas.hansson@arm.com    print >>f, 'extern "C" {'
59810458Sandreas.hansson@arm.com    for module in source:
59910458Sandreas.hansson@arm.com        print >>f, '    void init_%s();' % module.get_contents()
60010458Sandreas.hansson@arm.com    print >>f, '}'
60110458Sandreas.hansson@arm.com    print >>f, 'void initSwig() {'
60210458Sandreas.hansson@arm.com    for module in source:
60310458Sandreas.hansson@arm.com        print >>f, '    init_%s();' % module.get_contents()
60410458Sandreas.hansson@arm.com    print >>f, '}'
60510458Sandreas.hansson@arm.com    f.close()
60610458Sandreas.hansson@arm.com
6078596Ssteve.reinhardt@amd.comenv.Command('python/swig/init.cc', swig_modules, makeSwigInit)
6085517Snate@binkert.orgSource('python/swig/init.cc')
6095517Snate@binkert.org
6105517Snate@binkert.org# Generate traceflags.py
6118596Ssteve.reinhardt@amd.comdef traceFlagsPy(target, source, env):
6125517Snate@binkert.org    assert(len(target) == 1)
6137673Snate@binkert.org
6147673Snate@binkert.org    f = file(str(target[0]), 'w')
6157673Snate@binkert.org
6165517Snate@binkert.org    allFlags = []
6175517Snate@binkert.org    for s in source:
6185517Snate@binkert.org        val = eval(s.get_contents())
6195517Snate@binkert.org        allFlags.append(val)
6205517Snate@binkert.org
6215517Snate@binkert.org    print >>f, 'baseFlags = ['
6225517Snate@binkert.org    for flag, compound, desc in allFlags:
6237673Snate@binkert.org        if not compound:
6247673Snate@binkert.org            print >>f, "    '%s'," % flag
6257673Snate@binkert.org    print >>f, "    ]"
6265517Snate@binkert.org    print >>f
6278596Ssteve.reinhardt@amd.com
6285517Snate@binkert.org    print >>f, 'compoundFlags = ['
6295517Snate@binkert.org    print >>f, "    'All',"
6305517Snate@binkert.org    for flag, compound, desc in allFlags:
6315517Snate@binkert.org        if compound:
6325517Snate@binkert.org            print >>f, "    '%s'," % flag
6337673Snate@binkert.org    print >>f, "    ]"
6347673Snate@binkert.org    print >>f
6357673Snate@binkert.org
6365517Snate@binkert.org    print >>f, "allFlags = frozenset(baseFlags + compoundFlags)"
6378596Ssteve.reinhardt@amd.com    print >>f
6387675Snate@binkert.org
6397675Snate@binkert.org    print >>f, 'compoundFlagMap = {'
6407675Snate@binkert.org    all = tuple([flag for flag,compound,desc in allFlags if not compound])
6417675Snate@binkert.org    print >>f, "    'All' : %s," % (all, )
6427675Snate@binkert.org    for flag, compound, desc in allFlags:
6437675Snate@binkert.org        if compound:
6448596Ssteve.reinhardt@amd.com            print >>f, "    '%s' : %s," % (flag, compound)
6457675Snate@binkert.org    print >>f, "    }"
6467675Snate@binkert.org    print >>f
6478596Ssteve.reinhardt@amd.com
6488596Ssteve.reinhardt@amd.com    print >>f, 'flagDescriptions = {'
6498596Ssteve.reinhardt@amd.com    print >>f, "    'All' : 'All flags',"
6508596Ssteve.reinhardt@amd.com    for flag, compound, desc in allFlags:
6518596Ssteve.reinhardt@amd.com        print >>f, "    '%s' : '%s'," % (flag, desc)
6528596Ssteve.reinhardt@amd.com    print >>f, "    }"
6538596Ssteve.reinhardt@amd.com
6548596Ssteve.reinhardt@amd.com    f.close()
65510454SCurtis.Dunham@arm.com
65610454SCurtis.Dunham@arm.comdef traceFlagsCC(target, source, env):
65710454SCurtis.Dunham@arm.com    assert(len(target) == 1)
65810454SCurtis.Dunham@arm.com
6598596Ssteve.reinhardt@amd.com    f = file(str(target[0]), 'w')
6604762Snate@binkert.org
6616143Snate@binkert.org    allFlags = []
6626143Snate@binkert.org    for s in source:
6636143Snate@binkert.org        val = eval(s.get_contents())
6644762Snate@binkert.org        allFlags.append(val)
6654762Snate@binkert.org
6664762Snate@binkert.org    # file header
6677756SAli.Saidi@ARM.com    print >>f, '''
6688596Ssteve.reinhardt@amd.com/*
6694762Snate@binkert.org * DO NOT EDIT THIS FILE! Automatically generated
67010454SCurtis.Dunham@arm.com */
6714762Snate@binkert.org
67210458Sandreas.hansson@arm.com#include "base/traceflags.hh"
67310458Sandreas.hansson@arm.com
67410458Sandreas.hansson@arm.comusing namespace Trace;
67510458Sandreas.hansson@arm.com
67610458Sandreas.hansson@arm.comconst char *Trace::flagStrings[] =
67710458Sandreas.hansson@arm.com{'''
67810458Sandreas.hansson@arm.com
67910458Sandreas.hansson@arm.com    # The string array is used by SimpleEnumParam to map the strings
68010458Sandreas.hansson@arm.com    # provided by the user to enum values.
68110458Sandreas.hansson@arm.com    for flag, compound, desc in allFlags:
68210458Sandreas.hansson@arm.com        if not compound:
68310458Sandreas.hansson@arm.com            print >>f, '    "%s",' % flag
68410458Sandreas.hansson@arm.com
68510458Sandreas.hansson@arm.com    print >>f, '    "All",'
68610458Sandreas.hansson@arm.com    for flag, compound, desc in allFlags:
68710458Sandreas.hansson@arm.com        if compound:
68810458Sandreas.hansson@arm.com            print >>f, '    "%s",' % flag
68910458Sandreas.hansson@arm.com
69010458Sandreas.hansson@arm.com    print >>f, '};'
69110458Sandreas.hansson@arm.com    print >>f
69210458Sandreas.hansson@arm.com    print >>f, 'const int Trace::numFlagStrings = %d;' % (len(allFlags) + 1)
69310458Sandreas.hansson@arm.com    print >>f
69410458Sandreas.hansson@arm.com
69510458Sandreas.hansson@arm.com    #
69610458Sandreas.hansson@arm.com    # Now define the individual compound flag arrays.  There is an array
69710458Sandreas.hansson@arm.com    # for each compound flag listing the component base flags.
69810458Sandreas.hansson@arm.com    #
69910458Sandreas.hansson@arm.com    all = tuple([flag for flag,compound,desc in allFlags if not compound])
70010458Sandreas.hansson@arm.com    print >>f, 'static const Flags AllMap[] = {'
70110458Sandreas.hansson@arm.com    for flag, compound, desc in allFlags:
70210458Sandreas.hansson@arm.com        if not compound:
70310458Sandreas.hansson@arm.com            print >>f, "    %s," % flag
70410458Sandreas.hansson@arm.com    print >>f, '};'
70510458Sandreas.hansson@arm.com    print >>f
70610458Sandreas.hansson@arm.com
70710458Sandreas.hansson@arm.com    for flag, compound, desc in allFlags:
70810458Sandreas.hansson@arm.com        if not compound:
70910458Sandreas.hansson@arm.com            continue
71010458Sandreas.hansson@arm.com        print >>f, 'static const Flags %sMap[] = {' % flag
71110458Sandreas.hansson@arm.com        for flag in compound:
71210458Sandreas.hansson@arm.com            print >>f, "    %s," % flag
71310458Sandreas.hansson@arm.com        print >>f, "    (Flags)-1"
71410458Sandreas.hansson@arm.com        print >>f, '};'
71510458Sandreas.hansson@arm.com        print >>f
71610458Sandreas.hansson@arm.com
71710458Sandreas.hansson@arm.com    #
71810458Sandreas.hansson@arm.com    # Finally the compoundFlags[] array maps the compound flags
71910458Sandreas.hansson@arm.com    # to their individual arrays/
72010458Sandreas.hansson@arm.com    #
72110584Sandreas.hansson@arm.com    print >>f, 'const Flags *Trace::compoundFlags[] ='
72210458Sandreas.hansson@arm.com    print >>f, '{'
72310458Sandreas.hansson@arm.com    print >>f, '    AllMap,'
72410458Sandreas.hansson@arm.com    for flag, compound, desc in allFlags:
72510458Sandreas.hansson@arm.com        if compound:
72610458Sandreas.hansson@arm.com            print >>f, '    %sMap,' % flag
7278596Ssteve.reinhardt@amd.com    # file trailer
7285463Snate@binkert.org    print >>f, '};'
72910584Sandreas.hansson@arm.com
7308596Ssteve.reinhardt@amd.com    f.close()
7315463Snate@binkert.org
7327756SAli.Saidi@ARM.comdef traceFlagsHH(target, source, env):
7338596Ssteve.reinhardt@amd.com    assert(len(target) == 1)
7344762Snate@binkert.org
73510454SCurtis.Dunham@arm.com    f = file(str(target[0]), 'w')
7367677Snate@binkert.org
7374762Snate@binkert.org    allFlags = []
7384762Snate@binkert.org    for s in source:
7396143Snate@binkert.org        val = eval(s.get_contents())
7406143Snate@binkert.org        allFlags.append(val)
7416143Snate@binkert.org
7424762Snate@binkert.org    # file header boilerplate
7434762Snate@binkert.org    print >>f, '''
7447756SAli.Saidi@ARM.com/*
7457816Ssteve.reinhardt@amd.com * DO NOT EDIT THIS FILE!
7464762Snate@binkert.org *
74710454SCurtis.Dunham@arm.com * Automatically generated from traceflags.py
7484762Snate@binkert.org */
7494762Snate@binkert.org
7504762Snate@binkert.org#ifndef __BASE_TRACE_FLAGS_HH__
7517756SAli.Saidi@ARM.com#define __BASE_TRACE_FLAGS_HH__
7528596Ssteve.reinhardt@amd.com
7534762Snate@binkert.orgnamespace Trace {
75410454SCurtis.Dunham@arm.com
7554762Snate@binkert.orgenum Flags {'''
7567677Snate@binkert.org
7577756SAli.Saidi@ARM.com    # Generate the enum.  Base flags come first, then compound flags.
7588596Ssteve.reinhardt@amd.com    idx = 0
7597675Snate@binkert.org    for flag, compound, desc in allFlags:
76010454SCurtis.Dunham@arm.com        if not compound:
7617677Snate@binkert.org            print >>f, '    %s = %d,' % (flag, idx)
7625517Snate@binkert.org            idx += 1
7638596Ssteve.reinhardt@amd.com
76410584Sandreas.hansson@arm.com    numBaseFlags = idx
7659248SAndreas.Sandberg@arm.com    print >>f, '    NumFlags = %d,' % idx
7669248SAndreas.Sandberg@arm.com
7678596Ssteve.reinhardt@amd.com    # put a comment in here to separate base from compound flags
7688596Ssteve.reinhardt@amd.com    print >>f, '''
7698596Ssteve.reinhardt@amd.com// The remaining enum values are *not* valid indices for Trace::flags.
7709248SAndreas.Sandberg@arm.com// They are "compound" flags, which correspond to sets of base
7718596Ssteve.reinhardt@amd.com// flags, and are used by changeFlag.'''
7724762Snate@binkert.org
7737674Snate@binkert.org    print >>f, '    All = %d,' % idx
7747674Snate@binkert.org    idx += 1
7757674Snate@binkert.org    for flag, compound, desc in allFlags:
7767674Snate@binkert.org        if compound:
7777674Snate@binkert.org            print >>f, '    %s = %d,' % (flag, idx)
7787674Snate@binkert.org            idx += 1
7797674Snate@binkert.org
7807674Snate@binkert.org    numCompoundFlags = idx - numBaseFlags
7817674Snate@binkert.org    print >>f, '    NumCompoundFlags = %d' % numCompoundFlags
7827674Snate@binkert.org
7837674Snate@binkert.org    # trailer boilerplate
7847674Snate@binkert.org    print >>f, '''\
7857674Snate@binkert.org}; // enum Flags
7867674Snate@binkert.org
7877674Snate@binkert.org// Array of strings for SimpleEnumParam
7884762Snate@binkert.orgextern const char *flagStrings[];
7896143Snate@binkert.orgextern const int numFlagStrings;
7906143Snate@binkert.org
7917756SAli.Saidi@ARM.com// Array of arraay pointers: for each compound flag, gives the list of
7927816Ssteve.reinhardt@amd.com// base flags to set.  Inidividual flag arrays are terminated by -1.
7938235Snate@binkert.orgextern const Flags *compoundFlags[];
7948596Ssteve.reinhardt@amd.com
7957756SAli.Saidi@ARM.com/* namespace Trace */ }
7967816Ssteve.reinhardt@amd.com
79710454SCurtis.Dunham@arm.com#endif // __BASE_TRACE_FLAGS_HH__
7988235Snate@binkert.org'''
7994382Sbinkertn@umich.edu
8009396Sandreas.hansson@arm.com    f.close()
8019396Sandreas.hansson@arm.com
8029396Sandreas.hansson@arm.comflags = [ Value(f) for f in trace_flags ]
8039396Sandreas.hansson@arm.comenv.Command('base/traceflags.py', flags, traceFlagsPy)
8049396Sandreas.hansson@arm.comPySource('m5', 'base/traceflags.py')
8059396Sandreas.hansson@arm.com
8069396Sandreas.hansson@arm.comenv.Command('base/traceflags.hh', flags, traceFlagsHH)
8079396Sandreas.hansson@arm.comenv.Command('base/traceflags.cc', flags, traceFlagsCC)
8089396Sandreas.hansson@arm.comSource('base/traceflags.cc')
8099396Sandreas.hansson@arm.com
8109396Sandreas.hansson@arm.com# Generate program_info.cc
8119396Sandreas.hansson@arm.comdef programInfo(target, source, env):
81210454SCurtis.Dunham@arm.com    def gen_file(target, rev, node, date):
8139396Sandreas.hansson@arm.com        pi_stats = file(target, 'w')
8149396Sandreas.hansson@arm.com        print >>pi_stats, 'const char *hgRev = "%s:%s";' %  (rev, node)
8159396Sandreas.hansson@arm.com        print >>pi_stats, 'const char *hgDate = "%s";' % date
8169396Sandreas.hansson@arm.com        pi_stats.close()
8179396Sandreas.hansson@arm.com
8189396Sandreas.hansson@arm.com    target = str(target[0])
8198232Snate@binkert.org    scons_dir = str(source[0].get_contents())
8208232Snate@binkert.org    try:
8218232Snate@binkert.org        import mercurial.demandimport, mercurial.hg, mercurial.ui
8228232Snate@binkert.org        import mercurial.util, mercurial.node
8238232Snate@binkert.org        if not exists(scons_dir) or not isdir(scons_dir) or \
8246229Snate@binkert.org               not exists(joinpath(scons_dir, ".hg")):
82510455SCurtis.Dunham@arm.com            raise ValueError
8266229Snate@binkert.org        repo = mercurial.hg.repository(mercurial.ui.ui(), scons_dir)
82710455SCurtis.Dunham@arm.com        rev = mercurial.node.nullrev + repo.changelog.count()
82810455SCurtis.Dunham@arm.com        changenode = repo.changelog.node(rev)
82910455SCurtis.Dunham@arm.com        changes = repo.changelog.read(changenode)
8305517Snate@binkert.org        date = mercurial.util.datestr(changes[2])
8315517Snate@binkert.org
8327673Snate@binkert.org        gen_file(target, rev, mercurial.node.hex(changenode), date)
8335517Snate@binkert.org
83410455SCurtis.Dunham@arm.com        mercurial.demandimport.disable()
8355517Snate@binkert.org    except ImportError:
8365517Snate@binkert.org        gen_file(target, "Unknown", "Unknown", "Unknown")
8378232Snate@binkert.org
83810455SCurtis.Dunham@arm.com    except:
83910455SCurtis.Dunham@arm.com        print "in except"
84010455SCurtis.Dunham@arm.com        gen_file(target, "Unknown", "Unknown", "Unknown")
8417673Snate@binkert.org        mercurial.demandimport.disable()
8427673Snate@binkert.org
84310455SCurtis.Dunham@arm.comenv.Command('base/program_info.cc',
84410455SCurtis.Dunham@arm.com            Value(str(SCons.Node.FS.default_fs.SConstruct_dir)),
84510455SCurtis.Dunham@arm.com            programInfo)
8465517Snate@binkert.org
84710455SCurtis.Dunham@arm.com# embed python files.  All .py files that have been indicated by a
84810455SCurtis.Dunham@arm.com# PySource() call in a SConscript need to be embedded into the M5
84910455SCurtis.Dunham@arm.com# library.  To do that, we compile the file to byte code, marshal the
85010455SCurtis.Dunham@arm.com# byte code, compress it, and then generate an assembly file that
85110455SCurtis.Dunham@arm.com# inserts the result into the data section with symbols indicating the
85210455SCurtis.Dunham@arm.com# beginning, and end (and with the size at the end)
85310455SCurtis.Dunham@arm.compy_sources_tnodes = {}
85410455SCurtis.Dunham@arm.comfor pysource in py_sources:
85510685Sandreas.hansson@arm.com    py_sources_tnodes[pysource.tnode] = pysource
85610455SCurtis.Dunham@arm.com
85710685Sandreas.hansson@arm.comdef objectifyPyFile(target, source, env):
85810455SCurtis.Dunham@arm.com    '''Action function to compile a .py into a code object, marshal
8595517Snate@binkert.org    it, compress it, and stick it into an asm file so the code appears
86010455SCurtis.Dunham@arm.com    as just bytes with a label in the data section'''
8618232Snate@binkert.org
8628232Snate@binkert.org    src = file(str(source[0]), 'r').read()
8635517Snate@binkert.org    dst = file(str(target[0]), 'w')
8647673Snate@binkert.org
8655517Snate@binkert.org    pysource = py_sources_tnodes[source[0]]
8668232Snate@binkert.org    compiled = compile(src, pysource.debugname, 'exec')
8678232Snate@binkert.org    marshalled = marshal.dumps(compiled)
8685517Snate@binkert.org    compressed = zlib.compress(marshalled)
8698232Snate@binkert.org    data = compressed
8708232Snate@binkert.org
8718232Snate@binkert.org    # Some C/C++ compilers prepend an underscore to global symbol
8727673Snate@binkert.org    # names, so if they're going to do that, we need to prepend that
8735517Snate@binkert.org    # leading underscore to globals in the assembly file.
8745517Snate@binkert.org    if env['LEADING_UNDERSCORE']:
8757673Snate@binkert.org        sym = '_' + pysource.symname
8765517Snate@binkert.org    else:
87710455SCurtis.Dunham@arm.com        sym = pysource.symname
8785517Snate@binkert.org
8795517Snate@binkert.org    step = 16
8808232Snate@binkert.org    print >>dst, ".data"
8818232Snate@binkert.org    print >>dst, ".globl %s_beg" % sym
8825517Snate@binkert.org    print >>dst, ".globl %s_end" % sym
8838232Snate@binkert.org    print >>dst, "%s_beg:" % sym
8848232Snate@binkert.org    for i in xrange(0, len(data), step):
8855517Snate@binkert.org        x = array.array('B', data[i:i+step])
8868232Snate@binkert.org        print >>dst, ".byte", ','.join([str(d) for d in x])
8878232Snate@binkert.org    print >>dst, "%s_end:" % sym
8888232Snate@binkert.org    print >>dst, ".long %d" % len(marshalled)
8895517Snate@binkert.org
8908232Snate@binkert.orgfor source in py_sources:
8918232Snate@binkert.org    env.Command(source.assembly, source.tnode, objectifyPyFile)
8928232Snate@binkert.org    Source(source.assembly)
8938232Snate@binkert.org
8948232Snate@binkert.org# Generate init_python.cc which creates a bunch of EmbeddedPyModule
8958232Snate@binkert.org# structs that describe the embedded python code.  One such struct
8965517Snate@binkert.org# contains information about the importer that python uses to get at
8978232Snate@binkert.org# the embedded files, and then there's a list of all of the rest that
8988232Snate@binkert.org# the importer uses to load the rest on demand.
8995517Snate@binkert.orgpy_sources_symbols = {}
9008232Snate@binkert.orgfor pysource in py_sources:
9017673Snate@binkert.org    py_sources_symbols[pysource.symname] = pysource
9025517Snate@binkert.orgdef pythonInit(target, source, env):
9037673Snate@binkert.org    dst = file(str(target[0]), 'w')
9045517Snate@binkert.org
9058232Snate@binkert.org    def dump_mod(sym, endchar=','):
9068232Snate@binkert.org        pysource = py_sources_symbols[sym]
9078232Snate@binkert.org        print >>dst, '    { "%s",' % pysource.arcname
9085192Ssaidi@eecs.umich.edu        print >>dst, '      "%s",' % pysource.modpath
90910454SCurtis.Dunham@arm.com        print >>dst, '       %s_beg, %s_end,' % (sym, sym)
91010454SCurtis.Dunham@arm.com        print >>dst, '       %s_end - %s_beg,' % (sym, sym)
9118232Snate@binkert.org        print >>dst, '       *(int *)%s_end }%s'  % (sym, endchar)
91210455SCurtis.Dunham@arm.com    
91310455SCurtis.Dunham@arm.com    print >>dst, '#include "sim/init.hh"'
91410455SCurtis.Dunham@arm.com
91510455SCurtis.Dunham@arm.com    for sym in source:
91610455SCurtis.Dunham@arm.com        sym = sym.get_contents()
91710455SCurtis.Dunham@arm.com        print >>dst, "extern const char %s_beg[], %s_end[];" % (sym, sym)
9185192Ssaidi@eecs.umich.edu
9197674Snate@binkert.org    print >>dst, "const EmbeddedPyModule embeddedPyImporter = "
9205522Snate@binkert.org    dump_mod("PyEMB_importer", endchar=';');
9215522Snate@binkert.org    print >>dst
9227674Snate@binkert.org
9237674Snate@binkert.org    print >>dst, "const EmbeddedPyModule embeddedPyModules[] = {"
9247674Snate@binkert.org    for i,sym in enumerate(source):
9257674Snate@binkert.org        sym = sym.get_contents()
9267674Snate@binkert.org        if sym == "PyEMB_importer":
9277674Snate@binkert.org            # Skip the importer since we've already exported it
9287674Snate@binkert.org            continue
9297674Snate@binkert.org        dump_mod(sym)
9305522Snate@binkert.org    print >>dst, "    { 0, 0, 0, 0, 0, 0 }"
9315522Snate@binkert.org    print >>dst, "};"
9325522Snate@binkert.org
9335517Snate@binkert.orgsymbols = [Value(s.symname) for s in py_sources]
9345522Snate@binkert.orgenv.Command('sim/init_python.cc', symbols, pythonInit)
9355517Snate@binkert.orgSource('sim/init_python.cc')
9366143Snate@binkert.org
9376727Ssteve.reinhardt@amd.com########################################################################
9385522Snate@binkert.org#
9395522Snate@binkert.org# Define binaries.  Each different build type (debug, opt, etc.) gets
9405522Snate@binkert.org# a slightly different build environment.
9417674Snate@binkert.org#
9425517Snate@binkert.org
9437673Snate@binkert.org# List of constructed environments to pass back to SConstruct
9447673Snate@binkert.orgenvList = []
9457674Snate@binkert.org
9467673Snate@binkert.org# This function adds the specified sources to the given build
9477674Snate@binkert.org# environment, and returns a list of all the corresponding SCons
9487674Snate@binkert.org# Object nodes (including an extra one for date.cc).  We explicitly
9498946Sandreas.hansson@arm.com# add the Object nodes so we can set up special dependencies for
9507674Snate@binkert.org# date.cc.
9517674Snate@binkert.orgdef make_objs(sources, env, static):
9527674Snate@binkert.org    if static:
9535522Snate@binkert.org        XObject = env.StaticObject
9545522Snate@binkert.org    else:
9557674Snate@binkert.org        XObject = env.SharedObject
9567674Snate@binkert.org
9577674Snate@binkert.org    objs = [ XObject(s) for s in sources ]
9587674Snate@binkert.org  
9597673Snate@binkert.org    # make date.cc depend on all other objects so it always gets
9607674Snate@binkert.org    # recompiled whenever anything else does
9617674Snate@binkert.org    date_obj = XObject('base/date.cc')
9627674Snate@binkert.org
9637674Snate@binkert.org    # Make the generation of program_info.cc dependend on all 
9647674Snate@binkert.org    # the other cc files and the compiling of program_info.cc 
9657674Snate@binkert.org    # dependent on all the objects but program_info.o 
9667674Snate@binkert.org    pinfo_obj = XObject('base/program_info.cc')
9677674Snate@binkert.org    env.Depends('base/program_info.cc', sources)
9687811Ssteve.reinhardt@amd.com    env.Depends(date_obj, objs)
9697674Snate@binkert.org    env.Depends(pinfo_obj, objs)
9707673Snate@binkert.org    objs.extend([date_obj, pinfo_obj])
9715522Snate@binkert.org    return objs
9726143Snate@binkert.org
97310453SAndrew.Bardsley@arm.com# Function to create a new build environment as clone of current
9747816Ssteve.reinhardt@amd.com# environment 'env' with modified object suffix and optional stripped
97510454SCurtis.Dunham@arm.com# binary.  Additional keyword arguments are appended to corresponding
97610453SAndrew.Bardsley@arm.com# build environment vars.
9774382Sbinkertn@umich.edudef makeEnv(label, objsfx, strip = False, **kwargs):
9784382Sbinkertn@umich.edu    # SCons doesn't know to append a library suffix when there is a '.' in the
9794382Sbinkertn@umich.edu    # name.  Use '_' instead.
9804382Sbinkertn@umich.edu    libname = 'm5_' + label
9814382Sbinkertn@umich.edu    exename = 'm5.' + label
9824382Sbinkertn@umich.edu
9834382Sbinkertn@umich.edu    new_env = env.Copy(OBJSUFFIX=objsfx, SHOBJSUFFIX=objsfx + 's')
9844382Sbinkertn@umich.edu    new_env.Label = label
98510196SCurtis.Dunham@arm.com    new_env.Append(**kwargs)
9864382Sbinkertn@umich.edu
98710196SCurtis.Dunham@arm.com    swig_env = new_env.Copy()
98810196SCurtis.Dunham@arm.com    if env['GCC']:
98910196SCurtis.Dunham@arm.com        swig_env.Append(CCFLAGS='-Wno-uninitialized')
99010196SCurtis.Dunham@arm.com        swig_env.Append(CCFLAGS='-Wno-sign-compare')
99110196SCurtis.Dunham@arm.com        swig_env.Append(CCFLAGS='-Wno-parentheses')
99210196SCurtis.Dunham@arm.com
99310196SCurtis.Dunham@arm.com    static_objs = make_objs(cc_lib_sources, new_env, static=True)
994955SN/A    shared_objs = make_objs(cc_lib_sources, new_env, static=False)
9952655Sstever@eecs.umich.edu    static_objs += [ swig_env.StaticObject(s) for s in cc_swig_sources ]
9962655Sstever@eecs.umich.edu    shared_objs += [ swig_env.SharedObject(s) for s in cc_swig_sources ]
9972655Sstever@eecs.umich.edu
9982655Sstever@eecs.umich.edu    # First make a library of everything but main() so other programs can
99910196SCurtis.Dunham@arm.com    # link against m5.
10005601Snate@binkert.org    static_lib = new_env.StaticLibrary(libname, static_objs + static_objs)
10015601Snate@binkert.org    shared_lib = new_env.SharedLibrary(libname, shared_objs + shared_objs)
100210196SCurtis.Dunham@arm.com
100310196SCurtis.Dunham@arm.com    for target, sources in unit_tests:
100410196SCurtis.Dunham@arm.com        objs = [ new_env.StaticObject(s) for s in sources ]
10055522Snate@binkert.org        new_env.Program("unittest/%s.%s" % (target, label), objs + static_lib)
10065863Snate@binkert.org
10075601Snate@binkert.org    # Now link a stub with main() and the static library.
10085601Snate@binkert.org    objects = [new_env.Object(s) for s in cc_bin_sources] + static_lib
10095601Snate@binkert.org    if strip:
10105863Snate@binkert.org        unstripped_exe = exename + '.unstripped'
10119556Sandreas.hansson@arm.com        new_env.Program(unstripped_exe, objects)
10129556Sandreas.hansson@arm.com        if sys.platform == 'sunos5':
10139556Sandreas.hansson@arm.com            cmd = 'cp $SOURCE $TARGET; strip $TARGET'
10149556Sandreas.hansson@arm.com        else:
10159556Sandreas.hansson@arm.com            cmd = 'strip $SOURCE -o $TARGET'
10169556Sandreas.hansson@arm.com        targets = new_env.Command(exename, unstripped_exe, cmd)
10179556Sandreas.hansson@arm.com    else:
101810878Sandreas.hansson@arm.com        targets = new_env.Program(exename, objects)
101910878Sandreas.hansson@arm.com            
10209556Sandreas.hansson@arm.com    new_env.M5Binary = targets[0]
10215559Snate@binkert.org    envList.append(new_env)
10229556Sandreas.hansson@arm.com
10239618Ssteve.reinhardt@amd.com# Debug binary
10249618Ssteve.reinhardt@amd.comccflags = {}
10259618Ssteve.reinhardt@amd.comif env['GCC']:
102610238Sandreas.hansson@arm.com    if sys.platform == 'sunos5':
102710878Sandreas.hansson@arm.com        ccflags['debug'] = '-gstabs+'
102810878Sandreas.hansson@arm.com    else:
102910457Sandreas.hansson@arm.com        ccflags['debug'] = '-ggdb3'
103010457Sandreas.hansson@arm.com    ccflags['opt'] = '-g -O3'
103110457Sandreas.hansson@arm.com    ccflags['fast'] = '-O3'
103210457Sandreas.hansson@arm.com    ccflags['prof'] = '-O3 -g -pg'
103310457Sandreas.hansson@arm.comelif env['SUNCC']:
103410457Sandreas.hansson@arm.com    ccflags['debug'] = '-g0'
103510457Sandreas.hansson@arm.com    ccflags['opt'] = '-g -O'
103610457Sandreas.hansson@arm.com    ccflags['fast'] = '-fast'
103710457Sandreas.hansson@arm.com    ccflags['prof'] = '-fast -g -pg'
10388737Skoansin.tan@gmail.comelif env['ICC']:
103910278SAndreas.Sandberg@ARM.com    ccflags['debug'] = '-g -O0'
104010278SAndreas.Sandberg@ARM.com    ccflags['opt'] = '-g -O'
104110278SAndreas.Sandberg@ARM.com    ccflags['fast'] = '-fast'
104210278SAndreas.Sandberg@ARM.com    ccflags['prof'] = '-fast -g -pg'
104310278SAndreas.Sandberg@ARM.comelse:
104410278SAndreas.Sandberg@ARM.com    print 'Unknown compiler, please fix compiler options'
104510278SAndreas.Sandberg@ARM.com    Exit(1)
104610278SAndreas.Sandberg@ARM.com
104710457Sandreas.hansson@arm.commakeEnv('debug', '.do',
104810457Sandreas.hansson@arm.com        CCFLAGS = Split(ccflags['debug']),
104910457Sandreas.hansson@arm.com        CPPDEFINES = ['DEBUG', 'TRACING_ON=1'])
105010457Sandreas.hansson@arm.com
105110457Sandreas.hansson@arm.com# Optimized binary
105210457Sandreas.hansson@arm.commakeEnv('opt', '.o',
10538945Ssteve.reinhardt@amd.com        CCFLAGS = Split(ccflags['opt']),
105410686SAndreas.Sandberg@ARM.com        CPPDEFINES = ['TRACING_ON=1'])
105510686SAndreas.Sandberg@ARM.com
105610686SAndreas.Sandberg@ARM.com# "Fast" binary
105710686SAndreas.Sandberg@ARM.commakeEnv('fast', '.fo', strip = True,
105810686SAndreas.Sandberg@ARM.com        CCFLAGS = Split(ccflags['fast']),
105910686SAndreas.Sandberg@ARM.com        CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'])
10608945Ssteve.reinhardt@amd.com
10616143Snate@binkert.org# Profiled binary
10626143Snate@binkert.orgmakeEnv('prof', '.po',
10636143Snate@binkert.org        CCFLAGS = Split(ccflags['prof']),
10646143Snate@binkert.org        CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
10656143Snate@binkert.org        LINKFLAGS = '-pg')
10666143Snate@binkert.org
10676143Snate@binkert.orgReturn('envList')
10688945Ssteve.reinhardt@amd.com