SConscript revision 5642
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()
668233Snate@binkert.org        filename = str(tnode)
678233Snate@binkert.org        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('.')
726143Snate@binkert.org        else:
738233Snate@binkert.org            path = []
748233Snate@binkert.org
758233Snate@binkert.org        modpath = path[:]
766143Snate@binkert.org        if name != '__init__':
776143Snate@binkert.org            modpath += [name]
786143Snate@binkert.org        modpath = '.'.join(modpath)
796143Snate@binkert.org
808233Snate@binkert.org        arcpath = path + [ pyname ]
818233Snate@binkert.org        arcname = joinpath(*arcpath)
828233Snate@binkert.org
836143Snate@binkert.org        debugname = snode.abspath
848233Snate@binkert.org        if not exists(debugname):
858233Snate@binkert.org            debugname = tnode.abspath
868233Snate@binkert.org
878233Snate@binkert.org        self.tnode = tnode
886143Snate@binkert.org        self.snode = snode
896143Snate@binkert.org        self.pyname = pyname
906143Snate@binkert.org        self.package = package
914762Snate@binkert.org        self.modpath = modpath
926143Snate@binkert.org        self.arcname = arcname
938233Snate@binkert.org        self.debugname = debugname
948233Snate@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        
986143Snate@binkert.org
998233Snate@binkert.org########################################################################
1008233Snate@binkert.org# Code for adding source files of various types
1018233Snate@binkert.org#
1028233Snate@binkert.orgcc_lib_sources = []
1036143Snate@binkert.orgdef Source(source):
1046143Snate@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):
1127065Snate@binkert.org    '''Add a source file to the m5 binary build'''
1136143Snate@binkert.org    if not isinstance(source, SCons.Node.FS.File):
1148233Snate@binkert.org        source = File(source)
1158233Snate@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
1456143Snate@binkert.orgswig_sources = []
1466143Snate@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
1519982Satgutier@umich.edu    swig_sources.append(val)
15210196SCurtis.Dunham@arm.com
15310196SCurtis.Dunham@arm.comunit_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:
1606143Snate@binkert.org        if not isinstance(source, SCons.Node.FS.File):
1616143Snate@binkert.org            source = File(source)
1628945Ssteve.reinhardt@amd.com        srcs.append(source)
1638233Snate@binkert.org            
1648233Snate@binkert.org    unit_tests.append((target, srcs))
1656143Snate@binkert.org
1668945Ssteve.reinhardt@amd.com# Children should have access
1676143Snate@binkert.orgExport('Source')
1686143Snate@binkert.orgExport('BinSource')
1696143Snate@binkert.orgExport('PySource')
1706143Snate@binkert.orgExport('SimObject')
1715522Snate@binkert.orgExport('SwigSource')
1726143Snate@binkert.orgExport('UnitTest')
1736143Snate@binkert.org
1746143Snate@binkert.org########################################################################
1759982Satgutier@umich.edu#
1768233Snate@binkert.org# Trace Flags
1778233Snate@binkert.org#
1788233Snate@binkert.orgall_flags = {}
1796143Snate@binkert.orgtrace_flags = []
1806143Snate@binkert.orgdef TraceFlag(name, desc=''):
1816143Snate@binkert.org    if name in all_flags:
1826143Snate@binkert.org        raise AttributeError, "Flag %s already specified" % name
1835522Snate@binkert.org    flag = (name, (), desc)
1845522Snate@binkert.org    trace_flags.append(flag)
1855522Snate@binkert.org    all_flags[name] = ()
1865522Snate@binkert.org
1875604Snate@binkert.orgdef CompoundFlag(name, flags, desc=''):
1885604Snate@binkert.org    if name in all_flags:
1896143Snate@binkert.org        raise AttributeError, "Flag %s already specified" % name
1906143Snate@binkert.org
1914762Snate@binkert.org    compound = tuple(flags)
1924762Snate@binkert.org    for flag in compound:
1936143Snate@binkert.org        if flag not in all_flags:
1946727Ssteve.reinhardt@amd.com            raise AttributeError, "Trace flag %s not found" % flag
1956727Ssteve.reinhardt@amd.com        if all_flags[flag]:
1966727Ssteve.reinhardt@amd.com            raise AttributeError, \
1974762Snate@binkert.org                "Compound flag can't point to another compound flag"
1986143Snate@binkert.org
1996143Snate@binkert.org    flag = (name, compound, desc)
2006143Snate@binkert.org    trace_flags.append(flag)
2016143Snate@binkert.org    all_flags[name] = compound
2026727Ssteve.reinhardt@amd.com
2036143Snate@binkert.orgExport('TraceFlag')
2047674Snate@binkert.orgExport('CompoundFlag')
2057674Snate@binkert.org
2065604Snate@binkert.org########################################################################
2076143Snate@binkert.org#
2086143Snate@binkert.org# Set some compiler variables
2096143Snate@binkert.org#
2104762Snate@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
2134762Snate@binkert.org# the corresponding build directory to pick up generated include
2144762Snate@binkert.org# files.
2156143Snate@binkert.orgenv.Append(CPPPATH=Dir('.'))
2166143Snate@binkert.org
2174762Snate@binkert.org# Add a flag defining what THE_ISA should be for all compilation
2188233Snate@binkert.orgenv.Append(CPPDEFINES=[('THE_ISA','%s_ISA' % env['TARGET_ISA'].upper())])
2198233Snate@binkert.org
2208233Snate@binkert.org########################################################################
2218233Snate@binkert.org#
2226143Snate@binkert.org# Walk the tree and execute all SConscripts in subdirectories
2236143Snate@binkert.org#
2244762Snate@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
2308233Snate@binkert.org            continue
2318233Snate@binkert.org
2328233Snate@binkert.org        if 'SConscript' in files:
2336143Snate@binkert.org            build_dir = joinpath(env['BUILDDIR'], root[len(base_dir) + 1:])
2346143Snate@binkert.org            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#
2418233Snate@binkert.org# Prevent any SimObjects from being added after this point, they
2428233Snate@binkert.org# should all have been added in the SConscripts above
243955SN/A#
2449396Sandreas.hansson@arm.comclass DictImporter(object):
2459396Sandreas.hansson@arm.com    '''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:
2579930Sandreas.hansson@arm.com            del sys.modules[module]
2589930Sandreas.hansson@arm.com        self.installed = set()
2599396Sandreas.hansson@arm.com
2608235Snate@binkert.org    def find_module(self, fullname, path):
2618235Snate@binkert.org        if fullname == '__scons':
2626143Snate@binkert.org            return self
2638235Snate@binkert.org
2649003SAli.Saidi@ARM.com        if fullname == 'm5.objects':
2658235Snate@binkert.org            return self
2668235Snate@binkert.org
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)
2799003SAli.Saidi@ARM.com
2808235Snate@binkert.org        mod.__loader__ = self
2815584Snate@binkert.org        if fullname == 'm5.objects':
2824382Sbinkertn@umich.edu            mod.__path__ = fullname.split('.')
2834202Sbinkertn@umich.edu            return mod
2844382Sbinkertn@umich.edu
2854382Sbinkertn@umich.edu        if fullname == '__scons':
2864382Sbinkertn@umich.edu            mod.__dict__['m5_build_env'] = build_env
2879396Sandreas.hansson@arm.com            return mod
2885584Snate@binkert.org
2894382Sbinkertn@umich.edu        srcfile = self.modules[fullname]
2904382Sbinkertn@umich.edu        if basename(srcfile) == '__init__.py':
2914382Sbinkertn@umich.edu            mod.__path__ = fullname.split('.')
2928232Snate@binkert.org        mod.__file__ = srcfile
2935192Ssaidi@eecs.umich.edu
2948232Snate@binkert.org        exec file(srcfile, 'r') in mod.__dict__
2958232Snate@binkert.org
2968232Snate@binkert.org        return mod
2975192Ssaidi@eecs.umich.edu
2988232Snate@binkert.orgpy_modules = {}
2995192Ssaidi@eecs.umich.edufor source in py_sources:
3005799Snate@binkert.org    py_modules[source.modpath] = source.snode.abspath
3018232Snate@binkert.org
3025192Ssaidi@eecs.umich.edu# install the python importer so we can grab stuff from the source
3035192Ssaidi@eecs.umich.edu# 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.
3058232Snate@binkert.orgsim_objects_fixed = True
3065192Ssaidi@eecs.umich.eduimporter = DictImporter(py_modules)
3078232Snate@binkert.orgsys.meta_path[0:0] = [ importer ]
3085192Ssaidi@eecs.umich.edu
3095192Ssaidi@eecs.umich.eduimport m5
3105192Ssaidi@eecs.umich.edu
3115192Ssaidi@eecs.umich.edu# import all sim objects so we can populate the all_objects list
3124382Sbinkertn@umich.edu# make sure that we're working with a list, then let's sort it
3134382Sbinkertn@umich.edusim_objects = list(sim_object_modfiles)
3144382Sbinkertn@umich.edusim_objects.sort()
3152667Sstever@eecs.umich.edufor simobj in sim_objects:
3162667Sstever@eecs.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()
3215742Snate@binkert.orgsys.meta_path.remove(importer)
3225742Snate@binkert.org
3235742Snate@binkert.orgsim_objects = m5.SimObject.allClasses
3245793Snate@binkert.orgall_enums = m5.params.allEnums
3258334Snate@binkert.org
3265793Snate@binkert.orgall_params = {}
3275793Snate@binkert.orgfor name,obj in sim_objects.iteritems():
3285793Snate@binkert.org    for param in obj._params.local.values():
3294382Sbinkertn@umich.edu        if not hasattr(param, 'swig_decl'):
3304762Snate@binkert.org            continue
3315344Sstever@gmail.com        pname = param.ptype_str
3324382Sbinkertn@umich.edu        if pname not in all_params:
3335341Sstever@gmail.com            all_params[pname] = param
3345742Snate@binkert.org
3355742Snate@binkert.org########################################################################
3365742Snate@binkert.org#
3375742Snate@binkert.org# calculate extra dependencies
3385742Snate@binkert.org#
3394762Snate@binkert.orgmodule_depends = ["m5", "m5.SimObject", "m5.params"]
3405742Snate@binkert.orgdepends = [ File(py_modules[dep]) for dep in module_depends ]
3415742Snate@binkert.org
3427722Sgblack@eecs.umich.edu########################################################################
3435742Snate@binkert.org#
3445742Snate@binkert.org# Commands for the basic automatically generated python files
3455742Snate@binkert.org#
3469930Sandreas.hansson@arm.com
3479930Sandreas.hansson@arm.com# 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')
3515742Snate@binkert.org    print >>f, "m5_build_env = ", source[0]
3528242Sbradley.danofsky@amd.com    f.close()
3538242Sbradley.danofsky@amd.com
3548242Sbradley.danofsky@amd.com# Generate python file containing info about the M5 source code
3558242Sbradley.danofsky@amd.comdef makeInfoPyFile(target, source, env):
3565341Sstever@gmail.com    f = file(str(target[0]), 'w')
3575742Snate@binkert.org    for src in source:
3587722Sgblack@eecs.umich.edu        data = ''.join(file(src.srcnode().abspath, 'r').xreadlines())
3594773Snate@binkert.org        print >>f, "%s = %s" % (src, repr(data))
3606108Snate@binkert.org    f.close()
3611858SN/A
3621085SN/A# Generate the __init__.py file for m5.objects
3636658Snate@binkert.orgdef makeObjectsInitFile(target, source, env):
3646658Snate@binkert.org    f = file(str(target[0]), 'w')
3657673Snate@binkert.org    print >>f, 'from params import *'
3666658Snate@binkert.org    print >>f, 'from m5.SimObject import *'
3676658Snate@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)
3737673Snate@binkert.orgPySource('m5', 'python/m5/defines.py')
3747673Snate@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')
3806658Snate@binkert.org
3817673Snate@binkert.org# Generate an __init__.py file for the objects package
3827673Snate@binkert.orgenv.Command('python/m5/objects/__init__.py',
3837673Snate@binkert.org            [ Value(o) for o in sort_list(sim_object_modfiles) ],
3847673Snate@binkert.org            makeObjectsInitFile)
3857673Snate@binkert.orgPySource('m5.objects', 'python/m5/objects/__init__.py')
3867673Snate@binkert.org
3879048SAli.Saidi@ARM.com########################################################################
3887673Snate@binkert.org#
3897673Snate@binkert.org# Create all of the SimObject param headers and enum headers
3907673Snate@binkert.org#
3917673Snate@binkert.org
3926658Snate@binkert.orgdef createSimObjectParam(target, source, env):
3937756SAli.Saidi@ARM.com    assert len(target) == 1 and len(source) == 1
3947816Ssteve.reinhardt@amd.com
3956658Snate@binkert.org    hh_file = file(target[0].abspath, 'w')
3964382Sbinkertn@umich.edu    name = str(source[0].get_contents())
3974382Sbinkertn@umich.edu    obj = sim_objects[name]
3984762Snate@binkert.org
3994762Snate@binkert.org    print >>hh_file, obj.cxx_decl()
4004762Snate@binkert.org
4016654Snate@binkert.orgdef createSwigParam(target, source, env):
4026654Snate@binkert.org    assert len(target) == 1 and len(source) == 1
4035517Snate@binkert.org
4045517Snate@binkert.org    i_file = file(target[0].abspath, 'w')
4055517Snate@binkert.org    name = str(source[0].get_contents())
4065517Snate@binkert.org    param = all_params[name]
4075517Snate@binkert.org
4085517Snate@binkert.org    for line in param.swig_decl():
4095517Snate@binkert.org        print >>i_file, line
4105517Snate@binkert.org
4115517Snate@binkert.orgdef createEnumStrings(target, source, env):
4125517Snate@binkert.org    assert len(target) == 1 and len(source) == 1
4135517Snate@binkert.org
4145517Snate@binkert.org    cc_file = file(target[0].abspath, 'w')
4155517Snate@binkert.org    name = str(source[0].get_contents())
4165517Snate@binkert.org    obj = all_enums[name]
4175517Snate@binkert.org
4185517Snate@binkert.org    print >>cc_file, obj.cxx_def()
4195517Snate@binkert.org    cc_file.close()
4206654Snate@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()
4296143Snate@binkert.org
4306654Snate@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__]) ]
4345517Snate@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():
4435517Snate@binkert.org    if isinstance(param, m5.params.VectorParamDesc):
4445517Snate@binkert.org        ext = 'vptype'
4456654Snate@binkert.org    else:
4466654Snate@binkert.org        ext = 'ptype'
4475517Snate@binkert.org
4485517Snate@binkert.org    i_file = File('params/%s_%s.i' % (name, ext))
4496143Snate@binkert.org    params_i_files.append(i_file)
4506143Snate@binkert.org    env.Command(i_file, Value(name), createSwigParam)
4516143Snate@binkert.org    env.Depends(i_file, depends)
4526727Ssteve.reinhardt@amd.com
4535517Snate@binkert.org# Generate all enum header files
4546727Ssteve.reinhardt@amd.comfor 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)
4586654Snate@binkert.org    env.Command(cc_file, Value(name), createEnumStrings)
4596654Snate@binkert.org    env.Depends(cc_file, depends + extra_deps)
4607673Snate@binkert.org    Source(cc_file)
4616654Snate@binkert.org
4626654Snate@binkert.org    hh_file = File('enums/%s.hh' % name)
4636654Snate@binkert.org    env.Command(hh_file, Value(name), createEnumParam)
4646654Snate@binkert.org    env.Depends(hh_file, depends + extra_deps)
4655517Snate@binkert.org
4665517Snate@binkert.org# Build the big monolithic swigged params module (wraps all SimObject
4675517Snate@binkert.org# param structs and enum structs)
4686143Snate@binkert.orgdef buildParams(target, source, env):
4695517Snate@binkert.org    names = [ s.get_contents() for s in source ]
4704762Snate@binkert.org    objs = [ sim_objects[name] for name in names ]
4715517Snate@binkert.org    out = file(target[0].abspath, 'w')
4725517Snate@binkert.org
4736143Snate@binkert.org    ordered_objs = []
4746143Snate@binkert.org    obj_seen = set()
4755517Snate@binkert.org    def order_obj(obj):
4765517Snate@binkert.org        name = str(obj)
4775517Snate@binkert.org        if name in obj_seen:
4785517Snate@binkert.org            return
4795517Snate@binkert.org
4805517Snate@binkert.org        obj_seen.add(name)
4815517Snate@binkert.org        if str(obj) != 'SimObject':
4825517Snate@binkert.org            order_obj(obj.__bases__[0])
4835517Snate@binkert.org
4849338SAndreas.Sandberg@arm.com        ordered_objs.append(obj)
4859338SAndreas.Sandberg@arm.com
4869338SAndreas.Sandberg@arm.com    for obj in objs:
4879338SAndreas.Sandberg@arm.com        order_obj(obj)
4889338SAndreas.Sandberg@arm.com
4899338SAndreas.Sandberg@arm.com    enums = set()
4908596Ssteve.reinhardt@amd.com    predecls = []
4918596Ssteve.reinhardt@amd.com    pd_seen = set()
4928596Ssteve.reinhardt@amd.com
4938596Ssteve.reinhardt@amd.com    def add_pds(*pds):
4948596Ssteve.reinhardt@amd.com        for pd in pds:
4958596Ssteve.reinhardt@amd.com            if pd not in pd_seen:
4968596Ssteve.reinhardt@amd.com                predecls.append(pd)
4976143Snate@binkert.org                pd_seen.add(pd)
4985517Snate@binkert.org
4996654Snate@binkert.org    for obj in ordered_objs:
5006654Snate@binkert.org        params = obj._params.local.values()
5016654Snate@binkert.org        for param in params:
5026654Snate@binkert.org            ptype = param.ptype
5036654Snate@binkert.org            if issubclass(ptype, m5.params.Enum):
5046654Snate@binkert.org                if ptype not in enums:
5055517Snate@binkert.org                    enums.add(ptype)
5065517Snate@binkert.org            pds = param.swig_predecls()
5075517Snate@binkert.org            if isinstance(pds, (list, tuple)):
5088596Ssteve.reinhardt@amd.com                add_pds(*pds)
5098596Ssteve.reinhardt@amd.com            else:
5104762Snate@binkert.org                add_pds(pds)
5114762Snate@binkert.org
5124762Snate@binkert.org    print >>out, '%module params'
5134762Snate@binkert.org
5144762Snate@binkert.org    print >>out, '%{'
5154762Snate@binkert.org    for obj in ordered_objs:
5167675Snate@binkert.org        print >>out, '#include "params/%s.hh"' % obj
5174762Snate@binkert.org    print >>out, '%}'
5184762Snate@binkert.org
5194762Snate@binkert.org    for pd in predecls:
5204762Snate@binkert.org        print >>out, pd
5214382Sbinkertn@umich.edu
5224382Sbinkertn@umich.edu    enums = list(enums)
5235517Snate@binkert.org    enums.sort()
5246654Snate@binkert.org    for enum in enums:
5255517Snate@binkert.org        print >>out, '%%include "enums/%s.hh"' % enum.__name__
5268126Sgblack@eecs.umich.edu    print >>out
5276654Snate@binkert.org
5287673Snate@binkert.org    for obj in ordered_objs:
5296654Snate@binkert.org        if obj.swig_objdecls:
5306654Snate@binkert.org            for decl in obj.swig_objdecls:
5316654Snate@binkert.org                print >>out, decl
5326654Snate@binkert.org            continue
5336654Snate@binkert.org
5346654Snate@binkert.org        class_path = obj.cxx_class.split('::')
5356654Snate@binkert.org        classname = class_path[-1]
5366669Snate@binkert.org        namespaces = class_path[:-1]
5376669Snate@binkert.org        namespaces.reverse()
5386669Snate@binkert.org
5396669Snate@binkert.org        code = ''
5406669Snate@binkert.org
5416669Snate@binkert.org        if namespaces:
5426654Snate@binkert.org            code += '// avoid name conflicts\n'
5437673Snate@binkert.org            sep_string = '_COLONS_'
5445517Snate@binkert.org            flat_name = sep_string.join(class_path)
5458126Sgblack@eecs.umich.edu            code += '%%rename(%s) %s;\n' % (flat_name, classname)
5465798Snate@binkert.org
5477756SAli.Saidi@ARM.com        code += '// stop swig from creating/wrapping default ctor/dtor\n'
5487816Ssteve.reinhardt@amd.com        code += '%%nodefault %s;\n' % classname
5495798Snate@binkert.org        code += 'class %s ' % classname
5505798Snate@binkert.org        if obj._base:
5515517Snate@binkert.org            code += ': public %s' % obj._base.cxx_class
5525517Snate@binkert.org        code += ' {};\n'
5537673Snate@binkert.org
5545517Snate@binkert.org        for ns in namespaces:
5555517Snate@binkert.org            new_code = 'namespace %s {\n' % ns
5567673Snate@binkert.org            new_code += code
5577673Snate@binkert.org            new_code += '}\n'
5585517Snate@binkert.org            code = new_code
5595798Snate@binkert.org
5605798Snate@binkert.org        print >>out, code
5618333Snate@binkert.org
5627816Ssteve.reinhardt@amd.com    print >>out, '%%include "src/sim/sim_object_params.hh"' % obj
5635798Snate@binkert.org    for obj in ordered_objs:
5645798Snate@binkert.org        print >>out, '%%include "params/%s.hh"' % obj
5654762Snate@binkert.org
5664762Snate@binkert.orgparams_file = File('params/params.i')
5674762Snate@binkert.orgnames = sort_list(sim_objects.keys())
5684762Snate@binkert.orgenv.Command(params_file, [ Value(v) for v in names ], buildParams)
5694762Snate@binkert.orgenv.Depends(params_file, params_hh_files + params_i_files + depends)
5708596Ssteve.reinhardt@amd.comSwigSource('m5.objects', params_file)
5715517Snate@binkert.org
5725517Snate@binkert.org# Build all swig modules
5735517Snate@binkert.orgswig_modules = []
5745517Snate@binkert.orgcc_swig_sources = []
5755517Snate@binkert.orgfor source,package in swig_sources:
5767673Snate@binkert.org    filename = str(source)
5778596Ssteve.reinhardt@amd.com    assert filename.endswith('.i')
5787673Snate@binkert.org
5795517Snate@binkert.org    base = '.'.join(filename.split('.')[:-1])
5808596Ssteve.reinhardt@amd.com    module = basename(base)
5815517Snate@binkert.org    cc_file = base + '_wrap.cc'
5825517Snate@binkert.org    py_file = base + '.py'
5835517Snate@binkert.org
5848596Ssteve.reinhardt@amd.com    env.Command([cc_file, py_file], source,
5855517Snate@binkert.org                '$SWIG $SWIGFLAGS -outdir ${TARGETS[1].dir} '
5867673Snate@binkert.org                '-o ${TARGETS[0]} $SOURCES')
5877673Snate@binkert.org    env.Depends(py_file, source)
5887673Snate@binkert.org    env.Depends(cc_file, source)
5895517Snate@binkert.org
5905517Snate@binkert.org    swig_modules.append(Value(module))
5915517Snate@binkert.org    cc_swig_sources.append(File(cc_file))
5925517Snate@binkert.org    PySource(package, py_file)
5935517Snate@binkert.org
5945517Snate@binkert.org# Generate the main swig init file
5955517Snate@binkert.orgdef makeSwigInit(target, source, env):
5967673Snate@binkert.org    f = file(str(target[0]), 'w')
5977673Snate@binkert.org    print >>f, 'extern "C" {'
5987673Snate@binkert.org    for module in source:
5995517Snate@binkert.org        print >>f, '    void init_%s();' % module.get_contents()
6008596Ssteve.reinhardt@amd.com    print >>f, '}'
6015517Snate@binkert.org    print >>f, 'void initSwig() {'
6025517Snate@binkert.org    for module in source:
6035517Snate@binkert.org        print >>f, '    init_%s();' % module.get_contents()
6045517Snate@binkert.org    print >>f, '}'
6055517Snate@binkert.org    f.close()
6067673Snate@binkert.org
6077673Snate@binkert.orgenv.Command('python/swig/init.cc', swig_modules, makeSwigInit)
6087673Snate@binkert.orgSource('python/swig/init.cc')
6095517Snate@binkert.org
6108596Ssteve.reinhardt@amd.com# Generate traceflags.py
6117675Snate@binkert.orgdef traceFlagsPy(target, source, env):
6127675Snate@binkert.org    assert(len(target) == 1)
6137675Snate@binkert.org
6147675Snate@binkert.org    f = file(str(target[0]), 'w')
6157675Snate@binkert.org
6167675Snate@binkert.org    allFlags = []
6178596Ssteve.reinhardt@amd.com    for s in source:
6187675Snate@binkert.org        val = eval(s.get_contents())
6197675Snate@binkert.org        allFlags.append(val)
6208596Ssteve.reinhardt@amd.com
6218596Ssteve.reinhardt@amd.com    print >>f, 'baseFlags = ['
6228596Ssteve.reinhardt@amd.com    for flag, compound, desc in allFlags:
6238596Ssteve.reinhardt@amd.com        if not compound:
6248596Ssteve.reinhardt@amd.com            print >>f, "    '%s'," % flag
6258596Ssteve.reinhardt@amd.com    print >>f, "    ]"
6268596Ssteve.reinhardt@amd.com    print >>f
6278596Ssteve.reinhardt@amd.com
6288596Ssteve.reinhardt@amd.com    print >>f, 'compoundFlags = ['
6294762Snate@binkert.org    print >>f, "    'All',"
6306143Snate@binkert.org    for flag, compound, desc in allFlags:
6316143Snate@binkert.org        if compound:
6326143Snate@binkert.org            print >>f, "    '%s'," % flag
6334762Snate@binkert.org    print >>f, "    ]"
6344762Snate@binkert.org    print >>f
6354762Snate@binkert.org
6367756SAli.Saidi@ARM.com    print >>f, "allFlags = frozenset(baseFlags + compoundFlags)"
6378596Ssteve.reinhardt@amd.com    print >>f
6384762Snate@binkert.org
6394762Snate@binkert.org    print >>f, 'compoundFlagMap = {'
6408596Ssteve.reinhardt@amd.com    all = tuple([flag for flag,compound,desc in allFlags if not compound])
6415463Snate@binkert.org    print >>f, "    'All' : %s," % (all, )
6428596Ssteve.reinhardt@amd.com    for flag, compound, desc in allFlags:
6438596Ssteve.reinhardt@amd.com        if compound:
6445463Snate@binkert.org            print >>f, "    '%s' : %s," % (flag, compound)
6457756SAli.Saidi@ARM.com    print >>f, "    }"
6468596Ssteve.reinhardt@amd.com    print >>f
6474762Snate@binkert.org
6487677Snate@binkert.org    print >>f, 'flagDescriptions = {'
6494762Snate@binkert.org    print >>f, "    'All' : 'All flags',"
6504762Snate@binkert.org    for flag, compound, desc in allFlags:
6516143Snate@binkert.org        print >>f, "    '%s' : '%s'," % (flag, desc)
6526143Snate@binkert.org    print >>f, "    }"
6536143Snate@binkert.org
6544762Snate@binkert.org    f.close()
6554762Snate@binkert.org
6567756SAli.Saidi@ARM.comdef traceFlagsCC(target, source, env):
6577816Ssteve.reinhardt@amd.com    assert(len(target) == 1)
6584762Snate@binkert.org
6594762Snate@binkert.org    f = file(str(target[0]), 'w')
6604762Snate@binkert.org
6614762Snate@binkert.org    allFlags = []
6627756SAli.Saidi@ARM.com    for s in source:
6638596Ssteve.reinhardt@amd.com        val = eval(s.get_contents())
6644762Snate@binkert.org        allFlags.append(val)
6654762Snate@binkert.org
6667677Snate@binkert.org    # file header
6677756SAli.Saidi@ARM.com    print >>f, '''
6688596Ssteve.reinhardt@amd.com/*
6697675Snate@binkert.org * DO NOT EDIT THIS FILE! Automatically generated
6707677Snate@binkert.org */
6715517Snate@binkert.org
6728596Ssteve.reinhardt@amd.com#include "base/traceflags.hh"
6739248SAndreas.Sandberg@arm.com
6749248SAndreas.Sandberg@arm.comusing namespace Trace;
6759248SAndreas.Sandberg@arm.com
6769248SAndreas.Sandberg@arm.comconst char *Trace::flagStrings[] =
6778596Ssteve.reinhardt@amd.com{'''
6788596Ssteve.reinhardt@amd.com
6798596Ssteve.reinhardt@amd.com    # The string array is used by SimpleEnumParam to map the strings
6809248SAndreas.Sandberg@arm.com    # provided by the user to enum values.
6818596Ssteve.reinhardt@amd.com    for flag, compound, desc in allFlags:
6824762Snate@binkert.org        if not compound:
6837674Snate@binkert.org            print >>f, '    "%s",' % flag
6847674Snate@binkert.org
6857674Snate@binkert.org    print >>f, '    "All",'
6867674Snate@binkert.org    for flag, compound, desc in allFlags:
6877674Snate@binkert.org        if compound:
6887674Snate@binkert.org            print >>f, '    "%s",' % flag
6897674Snate@binkert.org
6907674Snate@binkert.org    print >>f, '};'
6917674Snate@binkert.org    print >>f
6927674Snate@binkert.org    print >>f, 'const int Trace::numFlagStrings = %d;' % (len(allFlags) + 1)
6937674Snate@binkert.org    print >>f
6947674Snate@binkert.org
6957674Snate@binkert.org    #
6967674Snate@binkert.org    # Now define the individual compound flag arrays.  There is an array
6977674Snate@binkert.org    # for each compound flag listing the component base flags.
6984762Snate@binkert.org    #
6996143Snate@binkert.org    all = tuple([flag for flag,compound,desc in allFlags if not compound])
7006143Snate@binkert.org    print >>f, 'static const Flags AllMap[] = {'
7017756SAli.Saidi@ARM.com    for flag, compound, desc in allFlags:
7027816Ssteve.reinhardt@amd.com        if not compound:
7038235Snate@binkert.org            print >>f, "    %s," % flag
7048596Ssteve.reinhardt@amd.com    print >>f, '};'
7057756SAli.Saidi@ARM.com    print >>f
7067816Ssteve.reinhardt@amd.com
7078235Snate@binkert.org    for flag, compound, desc in allFlags:
7084382Sbinkertn@umich.edu        if not compound:
7099396Sandreas.hansson@arm.com            continue
7109396Sandreas.hansson@arm.com        print >>f, 'static const Flags %sMap[] = {' % flag
7119396Sandreas.hansson@arm.com        for flag in compound:
7129396Sandreas.hansson@arm.com            print >>f, "    %s," % flag
7139396Sandreas.hansson@arm.com        print >>f, "    (Flags)-1"
7149396Sandreas.hansson@arm.com        print >>f, '};'
7159396Sandreas.hansson@arm.com        print >>f
7169396Sandreas.hansson@arm.com
7179396Sandreas.hansson@arm.com    #
7189396Sandreas.hansson@arm.com    # Finally the compoundFlags[] array maps the compound flags
7199396Sandreas.hansson@arm.com    # to their individual arrays/
7209396Sandreas.hansson@arm.com    #
7219396Sandreas.hansson@arm.com    print >>f, 'const Flags *Trace::compoundFlags[] ='
7229396Sandreas.hansson@arm.com    print >>f, '{'
7239396Sandreas.hansson@arm.com    print >>f, '    AllMap,'
7249396Sandreas.hansson@arm.com    for flag, compound, desc in allFlags:
7259396Sandreas.hansson@arm.com        if compound:
7269396Sandreas.hansson@arm.com            print >>f, '    %sMap,' % flag
7278232Snate@binkert.org    # file trailer
7288232Snate@binkert.org    print >>f, '};'
7298232Snate@binkert.org
7308232Snate@binkert.org    f.close()
7318232Snate@binkert.org
7326229Snate@binkert.orgdef traceFlagsHH(target, source, env):
7338232Snate@binkert.org    assert(len(target) == 1)
7348232Snate@binkert.org
7358232Snate@binkert.org    f = file(str(target[0]), 'w')
7366229Snate@binkert.org
7377673Snate@binkert.org    allFlags = []
7385517Snate@binkert.org    for s in source:
7395517Snate@binkert.org        val = eval(s.get_contents())
7407673Snate@binkert.org        allFlags.append(val)
7415517Snate@binkert.org
7425517Snate@binkert.org    # file header boilerplate
7435517Snate@binkert.org    print >>f, '''
7445517Snate@binkert.org/*
7458232Snate@binkert.org * DO NOT EDIT THIS FILE!
7467673Snate@binkert.org *
7477673Snate@binkert.org * Automatically generated from traceflags.py
7488232Snate@binkert.org */
7498232Snate@binkert.org
7508232Snate@binkert.org#ifndef __BASE_TRACE_FLAGS_HH__
7518232Snate@binkert.org#define __BASE_TRACE_FLAGS_HH__
7527673Snate@binkert.org
7535517Snate@binkert.orgnamespace Trace {
7548232Snate@binkert.org
7558232Snate@binkert.orgenum Flags {'''
7568232Snate@binkert.org
7578232Snate@binkert.org    # Generate the enum.  Base flags come first, then compound flags.
7587673Snate@binkert.org    idx = 0
7598232Snate@binkert.org    for flag, compound, desc in allFlags:
7608232Snate@binkert.org        if not compound:
7618232Snate@binkert.org            print >>f, '    %s = %d,' % (flag, idx)
7628232Snate@binkert.org            idx += 1
7638232Snate@binkert.org
7648232Snate@binkert.org    numBaseFlags = idx
7657673Snate@binkert.org    print >>f, '    NumFlags = %d,' % idx
7665517Snate@binkert.org
7678232Snate@binkert.org    # put a comment in here to separate base from compound flags
7688232Snate@binkert.org    print >>f, '''
7695517Snate@binkert.org// The remaining enum values are *not* valid indices for Trace::flags.
7707673Snate@binkert.org// They are "compound" flags, which correspond to sets of base
7715517Snate@binkert.org// flags, and are used by changeFlag.'''
7728232Snate@binkert.org
7738232Snate@binkert.org    print >>f, '    All = %d,' % idx
7745517Snate@binkert.org    idx += 1
7758232Snate@binkert.org    for flag, compound, desc in allFlags:
7768232Snate@binkert.org        if compound:
7778232Snate@binkert.org            print >>f, '    %s = %d,' % (flag, idx)
7787673Snate@binkert.org            idx += 1
7795517Snate@binkert.org
7805517Snate@binkert.org    numCompoundFlags = idx - numBaseFlags
7817673Snate@binkert.org    print >>f, '    NumCompoundFlags = %d' % numCompoundFlags
7825517Snate@binkert.org
7835517Snate@binkert.org    # trailer boilerplate
7845517Snate@binkert.org    print >>f, '''\
7858232Snate@binkert.org}; // enum Flags
7865517Snate@binkert.org
7875517Snate@binkert.org// Array of strings for SimpleEnumParam
7888232Snate@binkert.orgextern const char *flagStrings[];
7898232Snate@binkert.orgextern const int numFlagStrings;
7905517Snate@binkert.org
7918232Snate@binkert.org// Array of arraay pointers: for each compound flag, gives the list of
7928232Snate@binkert.org// base flags to set.  Inidividual flag arrays are terminated by -1.
7935517Snate@binkert.orgextern const Flags *compoundFlags[];
7948232Snate@binkert.org
7958232Snate@binkert.org/* namespace Trace */ }
7968232Snate@binkert.org
7975517Snate@binkert.org#endif // __BASE_TRACE_FLAGS_HH__
7988232Snate@binkert.org'''
7998232Snate@binkert.org
8008232Snate@binkert.org    f.close()
8018232Snate@binkert.org
8028232Snate@binkert.orgflags = [ Value(f) for f in trace_flags ]
8038232Snate@binkert.orgenv.Command('base/traceflags.py', flags, traceFlagsPy)
8045517Snate@binkert.orgPySource('m5', 'base/traceflags.py')
8058232Snate@binkert.org
8068232Snate@binkert.orgenv.Command('base/traceflags.hh', flags, traceFlagsHH)
8075517Snate@binkert.orgenv.Command('base/traceflags.cc', flags, traceFlagsCC)
8088232Snate@binkert.orgSource('base/traceflags.cc')
8097673Snate@binkert.org
8105517Snate@binkert.org# Generate program_info.cc
8117673Snate@binkert.orgdef programInfo(target, source, env):
8125517Snate@binkert.org    def gen_file(target, rev, node, date):
8138232Snate@binkert.org        pi_stats = file(target, 'w')
8148232Snate@binkert.org        print >>pi_stats, 'const char *hgRev = "%s:%s";' %  (rev, node)
8158232Snate@binkert.org        print >>pi_stats, 'const char *hgDate = "%s";' % date
8165192Ssaidi@eecs.umich.edu        pi_stats.close()
8178232Snate@binkert.org
8188232Snate@binkert.org    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
8225192Ssaidi@eecs.umich.edu        import mercurial.util, mercurial.node
8237674Snate@binkert.org        if not exists(scons_dir) or not isdir(scons_dir) or \
8245522Snate@binkert.org               not exists(joinpath(scons_dir, ".hg")):
8255522Snate@binkert.org            raise ValueError
8267674Snate@binkert.org        repo = mercurial.hg.repository(mercurial.ui.ui(), scons_dir)
8277674Snate@binkert.org        rev = mercurial.node.nullrev + repo.changelog.count()
8287674Snate@binkert.org        changenode = repo.changelog.node(rev)
8297674Snate@binkert.org        changes = repo.changelog.read(changenode)
8307674Snate@binkert.org        date = mercurial.util.datestr(changes[2])
8317674Snate@binkert.org
8327674Snate@binkert.org        gen_file(target, rev, mercurial.node.hex(changenode), date)
8337674Snate@binkert.org
8345522Snate@binkert.org        mercurial.demandimport.disable()
8355522Snate@binkert.org    except ImportError:
8365522Snate@binkert.org        gen_file(target, "Unknown", "Unknown", "Unknown")
8375517Snate@binkert.org
8385522Snate@binkert.org    except:
8395517Snate@binkert.org        print "in except"
8406143Snate@binkert.org        gen_file(target, "Unknown", "Unknown", "Unknown")
8416727Ssteve.reinhardt@amd.com        mercurial.demandimport.disable()
8425522Snate@binkert.org
8435522Snate@binkert.orgenv.Command('base/program_info.cc',
8445522Snate@binkert.org            Value(str(SCons.Node.FS.default_fs.SConstruct_dir)),
8457674Snate@binkert.org            programInfo)
8465517Snate@binkert.org
8477673Snate@binkert.org# embed python files.  All .py files that have been indicated by a
8487673Snate@binkert.org# PySource() call in a SConscript need to be embedded into the M5
8497674Snate@binkert.org# library.  To do that, we compile the file to byte code, marshal the
8507673Snate@binkert.org# byte code, compress it, and then generate an assembly file that
8517674Snate@binkert.org# inserts the result into the data section with symbols indicating the
8527674Snate@binkert.org# beginning, and end (and with the size at the end)
8538946Sandreas.hansson@arm.compy_sources_tnodes = {}
8547674Snate@binkert.orgfor pysource in py_sources:
8557674Snate@binkert.org    py_sources_tnodes[pysource.tnode] = pysource
8567674Snate@binkert.org
8575522Snate@binkert.orgdef objectifyPyFile(target, source, env):
8585522Snate@binkert.org    '''Action function to compile a .py into a code object, marshal
8597674Snate@binkert.org    it, compress it, and stick it into an asm file so the code appears
8607674Snate@binkert.org    as just bytes with a label in the data section'''
8617674Snate@binkert.org
8627674Snate@binkert.org    src = file(str(source[0]), 'r').read()
8637673Snate@binkert.org    dst = file(str(target[0]), 'w')
8647674Snate@binkert.org
8657674Snate@binkert.org    pysource = py_sources_tnodes[source[0]]
8667674Snate@binkert.org    compiled = compile(src, pysource.debugname, 'exec')
8677674Snate@binkert.org    marshalled = marshal.dumps(compiled)
8687674Snate@binkert.org    compressed = zlib.compress(marshalled)
8697674Snate@binkert.org    data = compressed
8707674Snate@binkert.org
8717674Snate@binkert.org    # Some C/C++ compilers prepend an underscore to global symbol
8727811Ssteve.reinhardt@amd.com    # names, so if they're going to do that, we need to prepend that
8737674Snate@binkert.org    # leading underscore to globals in the assembly file.
8747673Snate@binkert.org    if env['LEADING_UNDERSCORE']:
8755522Snate@binkert.org        sym = '_' + pysource.symname
8766143Snate@binkert.org    else:
8777756SAli.Saidi@ARM.com        sym = pysource.symname
8787816Ssteve.reinhardt@amd.com
8797674Snate@binkert.org    step = 16
8804382Sbinkertn@umich.edu    print >>dst, ".data"
8814382Sbinkertn@umich.edu    print >>dst, ".globl %s_beg" % sym
8824382Sbinkertn@umich.edu    print >>dst, ".globl %s_end" % sym
8834382Sbinkertn@umich.edu    print >>dst, "%s_beg:" % sym
8844382Sbinkertn@umich.edu    for i in xrange(0, len(data), step):
8854382Sbinkertn@umich.edu        x = array.array('B', data[i:i+step])
8864382Sbinkertn@umich.edu        print >>dst, ".byte", ','.join([str(d) for d in x])
8874382Sbinkertn@umich.edu    print >>dst, "%s_end:" % sym
88810196SCurtis.Dunham@arm.com    print >>dst, ".long %d" % len(marshalled)
8894382Sbinkertn@umich.edu
89010196SCurtis.Dunham@arm.comfor source in py_sources:
89110196SCurtis.Dunham@arm.com    env.Command(source.assembly, source.tnode, objectifyPyFile)
89210196SCurtis.Dunham@arm.com    Source(source.assembly)
89310196SCurtis.Dunham@arm.com
89410196SCurtis.Dunham@arm.com# Generate init_python.cc which creates a bunch of EmbeddedPyModule
89510196SCurtis.Dunham@arm.com# structs that describe the embedded python code.  One such struct
89610196SCurtis.Dunham@arm.com# contains information about the importer that python uses to get at
897955SN/A# the embedded files, and then there's a list of all of the rest that
8982655Sstever@eecs.umich.edu# the importer uses to load the rest on demand.
8992655Sstever@eecs.umich.edupy_sources_symbols = {}
9002655Sstever@eecs.umich.edufor pysource in py_sources:
9012655Sstever@eecs.umich.edu    py_sources_symbols[pysource.symname] = pysource
90210196SCurtis.Dunham@arm.comdef pythonInit(target, source, env):
9035601Snate@binkert.org    dst = file(str(target[0]), 'w')
9045601Snate@binkert.org
90510196SCurtis.Dunham@arm.com    def dump_mod(sym, endchar=','):
90610196SCurtis.Dunham@arm.com        pysource = py_sources_symbols[sym]
90710196SCurtis.Dunham@arm.com        print >>dst, '    { "%s",' % pysource.arcname
9085522Snate@binkert.org        print >>dst, '      "%s",' % pysource.modpath
9095863Snate@binkert.org        print >>dst, '       %s_beg, %s_end,' % (sym, sym)
9105601Snate@binkert.org        print >>dst, '       %s_end - %s_beg,' % (sym, sym)
9115601Snate@binkert.org        print >>dst, '       *(int *)%s_end }%s'  % (sym, endchar)
9125601Snate@binkert.org    
9135863Snate@binkert.org    print >>dst, '#include "sim/init.hh"'
9149556Sandreas.hansson@arm.com
9159556Sandreas.hansson@arm.com    for sym in source:
9169556Sandreas.hansson@arm.com        sym = sym.get_contents()
9179556Sandreas.hansson@arm.com        print >>dst, "extern const char %s_beg[], %s_end[];" % (sym, sym)
9189556Sandreas.hansson@arm.com
9199556Sandreas.hansson@arm.com    print >>dst, "const EmbeddedPyModule embeddedPyImporter = "
9209556Sandreas.hansson@arm.com    dump_mod("PyEMB_importer", endchar=';');
9219556Sandreas.hansson@arm.com    print >>dst
9229556Sandreas.hansson@arm.com
9235559Snate@binkert.org    print >>dst, "const EmbeddedPyModule embeddedPyModules[] = {"
9249556Sandreas.hansson@arm.com    for i,sym in enumerate(source):
9259618Ssteve.reinhardt@amd.com        sym = sym.get_contents()
9269618Ssteve.reinhardt@amd.com        if sym == "PyEMB_importer":
9279618Ssteve.reinhardt@amd.com            # Skip the importer since we've already exported it
92810238Sandreas.hansson@arm.com            continue
92910238Sandreas.hansson@arm.com        dump_mod(sym)
9309554Sandreas.hansson@arm.com    print >>dst, "    { 0, 0, 0, 0, 0, 0 }"
9319556Sandreas.hansson@arm.com    print >>dst, "};"
9329556Sandreas.hansson@arm.com
9339556Sandreas.hansson@arm.comsymbols = [Value(s.symname) for s in py_sources]
9349556Sandreas.hansson@arm.comenv.Command('sim/init_python.cc', symbols, pythonInit)
9359555Sandreas.hansson@arm.comSource('sim/init_python.cc')
9369555Sandreas.hansson@arm.com
9379556Sandreas.hansson@arm.com########################################################################
9388737Skoansin.tan@gmail.com#
9399556Sandreas.hansson@arm.com# Define binaries.  Each different build type (debug, opt, etc.) gets
9409556Sandreas.hansson@arm.com# a slightly different build environment.
9419556Sandreas.hansson@arm.com#
9429554Sandreas.hansson@arm.com
94310278SAndreas.Sandberg@ARM.com# List of constructed environments to pass back to SConstruct
94410278SAndreas.Sandberg@ARM.comenvList = []
94510278SAndreas.Sandberg@ARM.com
94610278SAndreas.Sandberg@ARM.com# This function adds the specified sources to the given build
94710278SAndreas.Sandberg@ARM.com# environment, and returns a list of all the corresponding SCons
94810278SAndreas.Sandberg@ARM.com# Object nodes (including an extra one for date.cc).  We explicitly
94910278SAndreas.Sandberg@ARM.com# add the Object nodes so we can set up special dependencies for
95010278SAndreas.Sandberg@ARM.com# date.cc.
9518945Ssteve.reinhardt@amd.comdef make_objs(sources, env, static):
9528945Ssteve.reinhardt@amd.com    if static:
9538945Ssteve.reinhardt@amd.com        XObject = env.StaticObject
9546143Snate@binkert.org    else:
9556143Snate@binkert.org        XObject = env.SharedObject
9566143Snate@binkert.org
9576143Snate@binkert.org    objs = [ XObject(s) for s in sources ]
9586143Snate@binkert.org  
9596143Snate@binkert.org    # make date.cc depend on all other objects so it always gets
9606143Snate@binkert.org    # recompiled whenever anything else does
9618945Ssteve.reinhardt@amd.com    date_obj = XObject('base/date.cc')
9628945Ssteve.reinhardt@amd.com
9636143Snate@binkert.org    # Make the generation of program_info.cc dependend on all 
9646143Snate@binkert.org    # the other cc files and the compiling of program_info.cc 
9656143Snate@binkert.org    # dependent on all the objects but program_info.o 
9666143Snate@binkert.org    pinfo_obj = XObject('base/program_info.cc')
9676143Snate@binkert.org    env.Depends('base/program_info.cc', sources)
9686143Snate@binkert.org    env.Depends(date_obj, objs)
9696143Snate@binkert.org    env.Depends(pinfo_obj, objs)
9706143Snate@binkert.org    objs.extend([date_obj, pinfo_obj])
9716143Snate@binkert.org    return objs
9726143Snate@binkert.org
9736143Snate@binkert.org# Function to create a new build environment as clone of current
9746143Snate@binkert.org# environment 'env' with modified object suffix and optional stripped
9756143Snate@binkert.org# binary.  Additional keyword arguments are appended to corresponding
9768594Snate@binkert.org# build environment vars.
9778594Snate@binkert.orgdef makeEnv(label, objsfx, strip = False, **kwargs):
9788594Snate@binkert.org    # SCons doesn't know to append a library suffix when there is a '.' in the
9798594Snate@binkert.org    # name.  Use '_' instead.
9806143Snate@binkert.org    libname = 'm5_' + label
9816143Snate@binkert.org    exename = 'm5.' + label
9826143Snate@binkert.org
9836143Snate@binkert.org    new_env = env.Copy(OBJSUFFIX=objsfx, SHOBJSUFFIX=objsfx + 's')
9846143Snate@binkert.org    new_env.Label = label
9856240Snate@binkert.org    new_env.Append(**kwargs)
9865554Snate@binkert.org
9875522Snate@binkert.org    swig_env = new_env.Copy()
9885522Snate@binkert.org    if env['GCC']:
9895797Snate@binkert.org        swig_env.Append(CCFLAGS='-Wno-uninitialized')
9905797Snate@binkert.org        swig_env.Append(CCFLAGS='-Wno-sign-compare')
9915522Snate@binkert.org        swig_env.Append(CCFLAGS='-Wno-parentheses')
9925601Snate@binkert.org
9938233Snate@binkert.org    static_objs = make_objs(cc_lib_sources, new_env, static=True)
9948233Snate@binkert.org    shared_objs = make_objs(cc_lib_sources, new_env, static=False)
9958235Snate@binkert.org    static_objs += [ swig_env.StaticObject(s) for s in cc_swig_sources ]
9968235Snate@binkert.org    shared_objs += [ swig_env.SharedObject(s) for s in cc_swig_sources ]
9978235Snate@binkert.org
9988235Snate@binkert.org    # First make a library of everything but main() so other programs can
9999003SAli.Saidi@ARM.com    # link against m5.
10009003SAli.Saidi@ARM.com    static_lib = new_env.StaticLibrary(libname, static_objs + static_objs)
100110196SCurtis.Dunham@arm.com    shared_lib = new_env.SharedLibrary(libname, shared_objs + shared_objs)
100210196SCurtis.Dunham@arm.com
10038235Snate@binkert.org    for target, sources in unit_tests:
10046143Snate@binkert.org        objs = [ new_env.StaticObject(s) for s in sources ]
10052655Sstever@eecs.umich.edu        new_env.Program("unittest/%s.%s" % (target, label), objs + static_lib)
10066143Snate@binkert.org
10076143Snate@binkert.org    # Now link a stub with main() and the static library.
10088233Snate@binkert.org    objects = [new_env.Object(s) for s in cc_bin_sources] + static_lib
10096143Snate@binkert.org    if strip:
10106143Snate@binkert.org        unstripped_exe = exename + '.unstripped'
10114007Ssaidi@eecs.umich.edu        new_env.Program(unstripped_exe, objects)
10124596Sbinkertn@umich.edu        if sys.platform == 'sunos5':
10134007Ssaidi@eecs.umich.edu            cmd = 'cp $SOURCE $TARGET; strip $TARGET'
10144596Sbinkertn@umich.edu        else:
10157756SAli.Saidi@ARM.com            cmd = 'strip $SOURCE -o $TARGET'
10167816Ssteve.reinhardt@amd.com        targets = new_env.Command(exename, unstripped_exe, cmd)
10178334Snate@binkert.org    else:
10188334Snate@binkert.org        targets = new_env.Program(exename, objects)
10198334Snate@binkert.org            
10208334Snate@binkert.org    new_env.M5Binary = targets[0]
10215601Snate@binkert.org    envList.append(new_env)
102210196SCurtis.Dunham@arm.com
10232655Sstever@eecs.umich.edu# Debug binary
10249225Sandreas.hansson@arm.comccflags = {}
10259225Sandreas.hansson@arm.comif env['GCC']:
10269226Sandreas.hansson@arm.com    if sys.platform == 'sunos5':
10279226Sandreas.hansson@arm.com        ccflags['debug'] = '-gstabs+'
10289225Sandreas.hansson@arm.com    else:
10299226Sandreas.hansson@arm.com        ccflags['debug'] = '-ggdb3'
10309226Sandreas.hansson@arm.com    ccflags['opt'] = '-g -O3'
10319226Sandreas.hansson@arm.com    ccflags['fast'] = '-O3'
10329226Sandreas.hansson@arm.com    ccflags['prof'] = '-O3 -g -pg'
10339226Sandreas.hansson@arm.comelif env['SUNCC']:
10349226Sandreas.hansson@arm.com    ccflags['debug'] = '-g0'
10359225Sandreas.hansson@arm.com    ccflags['opt'] = '-g -O'
10369227Sandreas.hansson@arm.com    ccflags['fast'] = '-fast'
10379227Sandreas.hansson@arm.com    ccflags['prof'] = '-fast -g -pg'
10389227Sandreas.hansson@arm.comelif env['ICC']:
10399227Sandreas.hansson@arm.com    ccflags['debug'] = '-g -O0'
10408946Sandreas.hansson@arm.com    ccflags['opt'] = '-g -O'
10413918Ssaidi@eecs.umich.edu    ccflags['fast'] = '-fast'
10429225Sandreas.hansson@arm.com    ccflags['prof'] = '-fast -g -pg'
10433918Ssaidi@eecs.umich.eduelse:
10449225Sandreas.hansson@arm.com    print 'Unknown compiler, please fix compiler options'
10459225Sandreas.hansson@arm.com    Exit(1)
10469227Sandreas.hansson@arm.com
10479227Sandreas.hansson@arm.commakeEnv('debug', '.do',
10489227Sandreas.hansson@arm.com        CCFLAGS = Split(ccflags['debug']),
10499226Sandreas.hansson@arm.com        CPPDEFINES = ['DEBUG', 'TRACING_ON=1'])
10509225Sandreas.hansson@arm.com
10519227Sandreas.hansson@arm.com# Optimized binary
10529227Sandreas.hansson@arm.commakeEnv('opt', '.o',
10539227Sandreas.hansson@arm.com        CCFLAGS = Split(ccflags['opt']),
10549227Sandreas.hansson@arm.com        CPPDEFINES = ['TRACING_ON=1'])
10558946Sandreas.hansson@arm.com
10569225Sandreas.hansson@arm.com# "Fast" binary
10579226Sandreas.hansson@arm.commakeEnv('fast', '.fo', strip = True,
10589226Sandreas.hansson@arm.com        CCFLAGS = Split(ccflags['fast']),
10599226Sandreas.hansson@arm.com        CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'])
10603515Ssaidi@eecs.umich.edu
10613918Ssaidi@eecs.umich.edu# Profiled binary
10624762Snate@binkert.orgmakeEnv('prof', '.po',
10633515Ssaidi@eecs.umich.edu        CCFLAGS = Split(ccflags['prof']),
10648881Smarc.orr@gmail.com        CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
10658881Smarc.orr@gmail.com        LINKFLAGS = '-pg')
10668881Smarc.orr@gmail.com
10678881Smarc.orr@gmail.comReturn('envList')
10688881Smarc.orr@gmail.com