SConscript revision 5584
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):
547674Snate@binkert.org    """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, source):
658334Snate@binkert.org        filename = str(source)
668233Snate@binkert.org        pyname = basename(filename)
678233Snate@binkert.org        assert pyname.endswith('.py')
688233Snate@binkert.org        name = pyname[:-3]
698233Snate@binkert.org        if package:
708233Snate@binkert.org            path = package.split('.')
718233Snate@binkert.org        else:
726143Snate@binkert.org            path = []
738233Snate@binkert.org        modpath = path
748233Snate@binkert.org        if name != '__init__':
758233Snate@binkert.org            modpath += [name]
766143Snate@binkert.org        modpath = '.'.join(modpath)
776143Snate@binkert.org
786143Snate@binkert.org        arcpath = path + [ pyname ]
796143Snate@binkert.org        arcname = joinpath(*arcpath)
808233Snate@binkert.org
818233Snate@binkert.org        self.tnode = source
828233Snate@binkert.org        self.snode = source.srcnode()
836143Snate@binkert.org        self.pyname = pyname
848233Snate@binkert.org        self.package = package
858233Snate@binkert.org        self.modpath = modpath
868233Snate@binkert.org        self.arcname = arcname
878233Snate@binkert.org        self.filename = filename
886143Snate@binkert.org        self.compiled = File(filename + 'c')
896143Snate@binkert.org        self.assembly = File(filename + '.s')
906143Snate@binkert.org        self.symname = "PyEMB_" + self.invalid_sym_char.sub('_', modpath)
914762Snate@binkert.org        
926143Snate@binkert.org
938233Snate@binkert.org########################################################################
948233Snate@binkert.org# Code for adding source files of various types
958233Snate@binkert.org#
968233Snate@binkert.orgcc_lib_sources = []
978233Snate@binkert.orgdef Source(source):
986143Snate@binkert.org    '''Add a source file to the libm5 build'''
998233Snate@binkert.org    if not isinstance(source, SCons.Node.FS.File):
1008233Snate@binkert.org        source = File(source)
1018233Snate@binkert.org
1028233Snate@binkert.org    cc_lib_sources.append(source)
1036143Snate@binkert.org
1046143Snate@binkert.orgcc_bin_sources = []
1056143Snate@binkert.orgdef BinSource(source):
1066143Snate@binkert.org    '''Add a source file to the m5 binary build'''
1076143Snate@binkert.org    if not isinstance(source, SCons.Node.FS.File):
1086143Snate@binkert.org        source = File(source)
1096143Snate@binkert.org
1106143Snate@binkert.org    cc_bin_sources.append(source)
1116143Snate@binkert.org
1127065Snate@binkert.orgpy_sources = []
1136143Snate@binkert.orgdef PySource(package, source):
1148233Snate@binkert.org    '''Add a python source file to the named package'''
1158233Snate@binkert.org    if not isinstance(source, SCons.Node.FS.File):
1168233Snate@binkert.org        source = File(source)
1178233Snate@binkert.org
1188233Snate@binkert.org    source = PySourceFile(package, source)
1198233Snate@binkert.org    py_sources.append(source)
1208233Snate@binkert.org
1218233Snate@binkert.orgsim_objects_fixed = False
1228233Snate@binkert.orgsim_object_modfiles = set()
1238233Snate@binkert.orgdef SimObject(source):
1248233Snate@binkert.org    '''Add a SimObject python file as a python source object and add
1258233Snate@binkert.org    it to a list of sim object modules'''
1268233Snate@binkert.org
1278233Snate@binkert.org    if sim_objects_fixed:
1288233Snate@binkert.org        raise AttributeError, "Too late to call SimObject now."
1298233Snate@binkert.org
1308233Snate@binkert.org    if not isinstance(source, SCons.Node.FS.File):
1318233Snate@binkert.org        source = File(source)
1328233Snate@binkert.org
1338233Snate@binkert.org    PySource('m5.objects', source)
1348233Snate@binkert.org    modfile = basename(str(source))
1358233Snate@binkert.org    assert modfile.endswith('.py')
1368233Snate@binkert.org    modname = modfile[:-3]
1378233Snate@binkert.org    sim_object_modfiles.add(modname)
1388233Snate@binkert.org
1398233Snate@binkert.orgswig_sources = []
1408233Snate@binkert.orgdef SwigSource(package, source):
1418233Snate@binkert.org    '''Add a swig file to build'''
1428233Snate@binkert.org    if not isinstance(source, SCons.Node.FS.File):
1438233Snate@binkert.org        source = File(source)
1448233Snate@binkert.org    val = source,package
1456143Snate@binkert.org    swig_sources.append(val)
1466143Snate@binkert.org
1476143Snate@binkert.orgunit_tests = []
1486143Snate@binkert.orgdef UnitTest(target, sources):
1496143Snate@binkert.org    if not isinstance(sources, (list, tuple)):
1506143Snate@binkert.org        sources = [ sources ]
1516143Snate@binkert.org    
1526143Snate@binkert.org    srcs = []
1536143Snate@binkert.org    for source in sources:
1548233Snate@binkert.org        if not isinstance(source, SCons.Node.FS.File):
1558233Snate@binkert.org            source = File(source)
1568233Snate@binkert.org        srcs.append(source)
1576143Snate@binkert.org            
1586143Snate@binkert.org    unit_tests.append((target, srcs))
1596143Snate@binkert.org
1606143Snate@binkert.org# Children should have access
1616143Snate@binkert.orgExport('Source')
1626143Snate@binkert.orgExport('BinSource')
1635522Snate@binkert.orgExport('PySource')
1646143Snate@binkert.orgExport('SimObject')
1656143Snate@binkert.orgExport('SwigSource')
1666143Snate@binkert.orgExport('UnitTest')
1676143Snate@binkert.org
1688233Snate@binkert.org########################################################################
1698233Snate@binkert.org#
1708233Snate@binkert.org# Trace Flags
1716143Snate@binkert.org#
1726143Snate@binkert.orgall_flags = {}
1736143Snate@binkert.orgtrace_flags = []
1746143Snate@binkert.orgdef TraceFlag(name, desc=''):
1755522Snate@binkert.org    if name in all_flags:
1765522Snate@binkert.org        raise AttributeError, "Flag %s already specified" % name
1775522Snate@binkert.org    flag = (name, (), desc)
1785522Snate@binkert.org    trace_flags.append(flag)
1795604Snate@binkert.org    all_flags[name] = ()
1805604Snate@binkert.org
1816143Snate@binkert.orgdef CompoundFlag(name, flags, desc=''):
1826143Snate@binkert.org    if name in all_flags:
1834762Snate@binkert.org        raise AttributeError, "Flag %s already specified" % name
1844762Snate@binkert.org
1856143Snate@binkert.org    compound = tuple(flags)
1866727Ssteve.reinhardt@amd.com    for flag in compound:
1876727Ssteve.reinhardt@amd.com        if flag not in all_flags:
1886727Ssteve.reinhardt@amd.com            raise AttributeError, "Trace flag %s not found" % flag
1894762Snate@binkert.org        if all_flags[flag]:
1906143Snate@binkert.org            raise AttributeError, \
1916143Snate@binkert.org                "Compound flag can't point to another compound flag"
1926143Snate@binkert.org
1936143Snate@binkert.org    flag = (name, compound, desc)
1946727Ssteve.reinhardt@amd.com    trace_flags.append(flag)
1956143Snate@binkert.org    all_flags[name] = compound
1967674Snate@binkert.org
1977674Snate@binkert.orgExport('TraceFlag')
1985604Snate@binkert.orgExport('CompoundFlag')
1996143Snate@binkert.org
2006143Snate@binkert.org########################################################################
2016143Snate@binkert.org#
2024762Snate@binkert.org# Set some compiler variables
2036143Snate@binkert.org#
2044762Snate@binkert.org
2054762Snate@binkert.org# Include file paths are rooted in this directory.  SCons will
2064762Snate@binkert.org# automatically expand '.' to refer to both the source directory and
2076143Snate@binkert.org# the corresponding build directory to pick up generated include
2086143Snate@binkert.org# files.
2094762Snate@binkert.orgenv.Append(CPPPATH=Dir('.'))
2108233Snate@binkert.org
2118233Snate@binkert.org# Add a flag defining what THE_ISA should be for all compilation
2128233Snate@binkert.orgenv.Append(CPPDEFINES=[('THE_ISA','%s_ISA' % env['TARGET_ISA'].upper())])
2138233Snate@binkert.org
2146143Snate@binkert.org########################################################################
2156143Snate@binkert.org#
2164762Snate@binkert.org# Walk the tree and execute all SConscripts in subdirectories
2176143Snate@binkert.org#
2184762Snate@binkert.org
2196143Snate@binkert.orgfor base_dir in base_dir_list:
2204762Snate@binkert.org    here = Dir('.').srcnode().abspath
2216143Snate@binkert.org    for root, dirs, files in os.walk(base_dir, topdown=True):
2228233Snate@binkert.org        if root == here:
2238233Snate@binkert.org            # we don't want to recurse back into this SConscript
2248233Snate@binkert.org            continue
2256143Snate@binkert.org
2266143Snate@binkert.org        if 'SConscript' in files:
2276143Snate@binkert.org            build_dir = joinpath(env['BUILDDIR'], root[len(base_dir) + 1:])
2286143Snate@binkert.org            SConscript(joinpath(root, 'SConscript'), build_dir=build_dir)
2296143Snate@binkert.org
2306143Snate@binkert.orgfor opt in env.ExportOptions:
2316143Snate@binkert.org    env.ConfigFile(opt)
2326143Snate@binkert.org
2338233Snate@binkert.org########################################################################
2348233Snate@binkert.org#
235955SN/A# Prevent any SimObjects from being added after this point, they
2368235Snate@binkert.org# should all have been added in the SConscripts above
2378235Snate@binkert.org#
2386143Snate@binkert.orgclass DictImporter(object):
2398235Snate@binkert.org    '''This importer takes a dictionary of arbitrary module names that
2408235Snate@binkert.org    map to arbitrary filenames.'''
2418235Snate@binkert.org    def __init__(self, modules):
2428235Snate@binkert.org        self.modules = modules
2438235Snate@binkert.org        self.installed = set()
2448235Snate@binkert.org
2458235Snate@binkert.org    def __del__(self):
2468235Snate@binkert.org        self.unload()
2478235Snate@binkert.org
2488235Snate@binkert.org    def unload(self):
2498235Snate@binkert.org        import sys
2508235Snate@binkert.org        for module in self.installed:
2518235Snate@binkert.org            del sys.modules[module]
2528235Snate@binkert.org        self.installed = set()
2538235Snate@binkert.org
2548235Snate@binkert.org    def find_module(self, fullname, path):
2558235Snate@binkert.org        if fullname == '__scons':
2565584Snate@binkert.org            return self
2574382Sbinkertn@umich.edu
2584202Sbinkertn@umich.edu        if fullname == 'm5.objects':
2594382Sbinkertn@umich.edu            return self
2604382Sbinkertn@umich.edu
2614382Sbinkertn@umich.edu        if fullname.startswith('m5.internal'):
2625584Snate@binkert.org            return None
2634382Sbinkertn@umich.edu
2644382Sbinkertn@umich.edu        if fullname in self.modules and exists(self.modules[fullname]):
2654382Sbinkertn@umich.edu            return self
2668232Snate@binkert.org
2675192Ssaidi@eecs.umich.edu        return None
2688232Snate@binkert.org
2698232Snate@binkert.org    def load_module(self, fullname):
2708232Snate@binkert.org        mod = imp.new_module(fullname)
2715192Ssaidi@eecs.umich.edu        sys.modules[fullname] = mod
2728232Snate@binkert.org        self.installed.add(fullname)
2738232Snate@binkert.org
2745192Ssaidi@eecs.umich.edu        mod.__loader__ = self
2755799Snate@binkert.org        if fullname == 'm5.objects':
2768232Snate@binkert.org            mod.__path__ = fullname.split('.')
2775192Ssaidi@eecs.umich.edu            return mod
2785192Ssaidi@eecs.umich.edu
2795192Ssaidi@eecs.umich.edu        if fullname == '__scons':
2808232Snate@binkert.org            mod.__dict__['m5_build_env'] = build_env
2815192Ssaidi@eecs.umich.edu            return mod
2828232Snate@binkert.org
2835192Ssaidi@eecs.umich.edu        srcfile = self.modules[fullname]
2845192Ssaidi@eecs.umich.edu        if basename(srcfile) == '__init__.py':
2855192Ssaidi@eecs.umich.edu            mod.__path__ = fullname.split('.')
2865192Ssaidi@eecs.umich.edu        mod.__file__ = srcfile
2875192Ssaidi@eecs.umich.edu
2884382Sbinkertn@umich.edu        exec file(srcfile, 'r') in mod.__dict__
2894382Sbinkertn@umich.edu
2904382Sbinkertn@umich.edu        return mod
2912667Sstever@eecs.umich.edu
2922667Sstever@eecs.umich.edupy_modules = {}
2932667Sstever@eecs.umich.edufor source in py_sources:
2942667Sstever@eecs.umich.edu    py_modules[source.modpath] = source.snode.abspath
2952667Sstever@eecs.umich.edu
2962667Sstever@eecs.umich.edu# install the python importer so we can grab stuff from the source
2975742Snate@binkert.org# tree itself.  We can't have SimObjects added after this point or
2985742Snate@binkert.org# else we won't know about them for the rest of the stuff.
2995742Snate@binkert.orgsim_objects_fixed = True
3005793Snate@binkert.orgimporter = DictImporter(py_modules)
3018334Snate@binkert.orgsys.meta_path[0:0] = [ importer ]
3025793Snate@binkert.org
3035793Snate@binkert.orgimport m5
3045793Snate@binkert.org
3054382Sbinkertn@umich.edu# import all sim objects so we can populate the all_objects list
3064762Snate@binkert.org# make sure that we're working with a list, then let's sort it
3075344Sstever@gmail.comsim_objects = list(sim_object_modfiles)
3084382Sbinkertn@umich.edusim_objects.sort()
3095341Sstever@gmail.comfor simobj in sim_objects:
3105742Snate@binkert.org    exec('from m5.objects import %s' % simobj)
3115742Snate@binkert.org
3125742Snate@binkert.org# we need to unload all of the currently imported modules so that they
3135742Snate@binkert.org# will be re-imported the next time the sconscript is run
3145742Snate@binkert.orgimporter.unload()
3154762Snate@binkert.orgsys.meta_path.remove(importer)
3165742Snate@binkert.org
3175742Snate@binkert.orgsim_objects = m5.SimObject.allClasses
3187722Sgblack@eecs.umich.eduall_enums = m5.params.allEnums
3195742Snate@binkert.org
3205742Snate@binkert.orgall_params = {}
3215742Snate@binkert.orgfor name,obj in sim_objects.iteritems():
3225742Snate@binkert.org    for param in obj._params.local.values():
3238242Sbradley.danofsky@amd.com        if not hasattr(param, 'swig_decl'):
3248242Sbradley.danofsky@amd.com            continue
3258242Sbradley.danofsky@amd.com        pname = param.ptype_str
3268242Sbradley.danofsky@amd.com        if pname not in all_params:
3275341Sstever@gmail.com            all_params[pname] = param
3285742Snate@binkert.org
3297722Sgblack@eecs.umich.edu########################################################################
3304773Snate@binkert.org#
3316108Snate@binkert.org# calculate extra dependencies
3321858SN/A#
3331085SN/Amodule_depends = ["m5", "m5.SimObject", "m5.params"]
3346658Snate@binkert.orgdepends = [ File(py_modules[dep]) for dep in module_depends ]
3356658Snate@binkert.org
3367673Snate@binkert.org########################################################################
3376658Snate@binkert.org#
3386658Snate@binkert.org# Commands for the basic automatically generated python files
3396658Snate@binkert.org#
3406658Snate@binkert.org
3416658Snate@binkert.org# Generate Python file containing a dict specifying the current
3426658Snate@binkert.org# build_env flags.
3436658Snate@binkert.orgdef makeDefinesPyFile(target, source, env):
3447673Snate@binkert.org    f = file(str(target[0]), 'w')
3457673Snate@binkert.org    print >>f, "m5_build_env = ", source[0]
3467673Snate@binkert.org    f.close()
3477673Snate@binkert.org
3487673Snate@binkert.org# Generate python file containing info about the M5 source code
3497673Snate@binkert.orgdef makeInfoPyFile(target, source, env):
3507673Snate@binkert.org    f = file(str(target[0]), 'w')
3516658Snate@binkert.org    for src in source:
3527673Snate@binkert.org        data = ''.join(file(src.srcnode().abspath, 'r').xreadlines())
3537673Snate@binkert.org        print >>f, "%s = %s" % (src, repr(data))
3547673Snate@binkert.org    f.close()
3557673Snate@binkert.org
3567673Snate@binkert.org# Generate the __init__.py file for m5.objects
3577673Snate@binkert.orgdef makeObjectsInitFile(target, source, env):
3587673Snate@binkert.org    f = file(str(target[0]), 'w')
3597673Snate@binkert.org    print >>f, 'from params import *'
3607673Snate@binkert.org    print >>f, 'from m5.SimObject import *'
3617673Snate@binkert.org    for module in source:
3626658Snate@binkert.org        print >>f, 'from %s import *' % module.get_contents()
3637756SAli.Saidi@ARM.com    f.close()
3647816Ssteve.reinhardt@amd.com
3656658Snate@binkert.org# Generate a file with all of the compile options in it
3664382Sbinkertn@umich.eduenv.Command('python/m5/defines.py', Value(build_env), makeDefinesPyFile)
3674382Sbinkertn@umich.eduPySource('m5', 'python/m5/defines.py')
3684762Snate@binkert.org
3694762Snate@binkert.org# Generate a file that wraps the basic top level files
3704762Snate@binkert.orgenv.Command('python/m5/info.py',
3716654Snate@binkert.org            [ '#/AUTHORS', '#/LICENSE', '#/README', '#/RELEASE_NOTES' ],
3726654Snate@binkert.org            makeInfoPyFile)
3735517Snate@binkert.orgPySource('m5', 'python/m5/info.py')
3745517Snate@binkert.org
3755517Snate@binkert.org# Generate an __init__.py file for the objects package
3765517Snate@binkert.orgenv.Command('python/m5/objects/__init__.py',
3775517Snate@binkert.org            [ Value(o) for o in sort_list(sim_object_modfiles) ],
3785517Snate@binkert.org            makeObjectsInitFile)
3795517Snate@binkert.orgPySource('m5.objects', 'python/m5/objects/__init__.py')
3805517Snate@binkert.org
3815517Snate@binkert.org########################################################################
3825517Snate@binkert.org#
3835517Snate@binkert.org# Create all of the SimObject param headers and enum headers
3845517Snate@binkert.org#
3855517Snate@binkert.org
3865517Snate@binkert.orgdef createSimObjectParam(target, source, env):
3875517Snate@binkert.org    assert len(target) == 1 and len(source) == 1
3885517Snate@binkert.org
3895517Snate@binkert.org    hh_file = file(target[0].abspath, 'w')
3906654Snate@binkert.org    name = str(source[0].get_contents())
3915517Snate@binkert.org    obj = sim_objects[name]
3925517Snate@binkert.org
3935517Snate@binkert.org    print >>hh_file, obj.cxx_decl()
3945517Snate@binkert.org
3955517Snate@binkert.orgdef createSwigParam(target, source, env):
3965517Snate@binkert.org    assert len(target) == 1 and len(source) == 1
3975517Snate@binkert.org
3985517Snate@binkert.org    i_file = file(target[0].abspath, 'w')
3996143Snate@binkert.org    name = str(source[0].get_contents())
4006654Snate@binkert.org    param = all_params[name]
4015517Snate@binkert.org
4025517Snate@binkert.org    for line in param.swig_decl():
4035517Snate@binkert.org        print >>i_file, line
4045517Snate@binkert.org
4055517Snate@binkert.orgdef createEnumStrings(target, source, env):
4065517Snate@binkert.org    assert len(target) == 1 and len(source) == 1
4075517Snate@binkert.org
4085517Snate@binkert.org    cc_file = file(target[0].abspath, 'w')
4095517Snate@binkert.org    name = str(source[0].get_contents())
4105517Snate@binkert.org    obj = all_enums[name]
4115517Snate@binkert.org
4125517Snate@binkert.org    print >>cc_file, obj.cxx_def()
4135517Snate@binkert.org    cc_file.close()
4145517Snate@binkert.org
4156654Snate@binkert.orgdef createEnumParam(target, source, env):
4166654Snate@binkert.org    assert len(target) == 1 and len(source) == 1
4175517Snate@binkert.org
4185517Snate@binkert.org    hh_file = file(target[0].abspath, 'w')
4196143Snate@binkert.org    name = str(source[0].get_contents())
4206143Snate@binkert.org    obj = all_enums[name]
4216143Snate@binkert.org
4226727Ssteve.reinhardt@amd.com    print >>hh_file, obj.cxx_decl()
4235517Snate@binkert.org
4246727Ssteve.reinhardt@amd.com# Generate all of the SimObject param struct header files
4255517Snate@binkert.orgparams_hh_files = []
4265517Snate@binkert.orgfor name,simobj in sim_objects.iteritems():
4275517Snate@binkert.org    extra_deps = [ File(py_modules[simobj.__module__]) ]
4286654Snate@binkert.org
4296654Snate@binkert.org    hh_file = File('params/%s.hh' % name)
4307673Snate@binkert.org    params_hh_files.append(hh_file)
4316654Snate@binkert.org    env.Command(hh_file, Value(name), createSimObjectParam)
4326654Snate@binkert.org    env.Depends(hh_file, depends + extra_deps)
4336654Snate@binkert.org
4346654Snate@binkert.org# Generate any parameter header files needed
4355517Snate@binkert.orgparams_i_files = []
4365517Snate@binkert.orgfor name,param in all_params.iteritems():
4375517Snate@binkert.org    if isinstance(param, m5.params.VectorParamDesc):
4386143Snate@binkert.org        ext = 'vptype'
4395517Snate@binkert.org    else:
4404762Snate@binkert.org        ext = 'ptype'
4415517Snate@binkert.org
4425517Snate@binkert.org    i_file = File('params/%s_%s.i' % (name, ext))
4436143Snate@binkert.org    params_i_files.append(i_file)
4446143Snate@binkert.org    env.Command(i_file, Value(name), createSwigParam)
4455517Snate@binkert.org    env.Depends(i_file, depends)
4465517Snate@binkert.org
4475517Snate@binkert.org# Generate all enum header files
4485517Snate@binkert.orgfor name,enum in all_enums.iteritems():
4495517Snate@binkert.org    extra_deps = [ File(py_modules[enum.__module__]) ]
4505517Snate@binkert.org
4515517Snate@binkert.org    cc_file = File('enums/%s.cc' % name)
4525517Snate@binkert.org    env.Command(cc_file, Value(name), createEnumStrings)
4535517Snate@binkert.org    env.Depends(cc_file, depends + extra_deps)
4545517Snate@binkert.org    Source(cc_file)
4556143Snate@binkert.org
4565517Snate@binkert.org    hh_file = File('enums/%s.hh' % name)
4576654Snate@binkert.org    env.Command(hh_file, Value(name), createEnumParam)
4586654Snate@binkert.org    env.Depends(hh_file, depends + extra_deps)
4596654Snate@binkert.org
4606654Snate@binkert.org# Build the big monolithic swigged params module (wraps all SimObject
4616654Snate@binkert.org# param structs and enum structs)
4626654Snate@binkert.orgdef buildParams(target, source, env):
4635517Snate@binkert.org    names = [ s.get_contents() for s in source ]
4645517Snate@binkert.org    objs = [ sim_objects[name] for name in names ]
4655517Snate@binkert.org    out = file(target[0].abspath, 'w')
4665517Snate@binkert.org
4675517Snate@binkert.org    ordered_objs = []
4684762Snate@binkert.org    obj_seen = set()
4694762Snate@binkert.org    def order_obj(obj):
4704762Snate@binkert.org        name = str(obj)
4714762Snate@binkert.org        if name in obj_seen:
4724762Snate@binkert.org            return
4734762Snate@binkert.org
4747675Snate@binkert.org        obj_seen.add(name)
4754762Snate@binkert.org        if str(obj) != 'SimObject':
4764762Snate@binkert.org            order_obj(obj.__bases__[0])
4774762Snate@binkert.org
4784762Snate@binkert.org        ordered_objs.append(obj)
4794382Sbinkertn@umich.edu
4804382Sbinkertn@umich.edu    for obj in objs:
4815517Snate@binkert.org        order_obj(obj)
4826654Snate@binkert.org
4835517Snate@binkert.org    enums = set()
4848126Sgblack@eecs.umich.edu    predecls = []
4856654Snate@binkert.org    pd_seen = set()
4867673Snate@binkert.org
4876654Snate@binkert.org    def add_pds(*pds):
4886654Snate@binkert.org        for pd in pds:
4896654Snate@binkert.org            if pd not in pd_seen:
4906654Snate@binkert.org                predecls.append(pd)
4916654Snate@binkert.org                pd_seen.add(pd)
4926654Snate@binkert.org
4936654Snate@binkert.org    for obj in ordered_objs:
4946669Snate@binkert.org        params = obj._params.local.values()
4956669Snate@binkert.org        for param in params:
4966669Snate@binkert.org            ptype = param.ptype
4976669Snate@binkert.org            if issubclass(ptype, m5.params.Enum):
4986669Snate@binkert.org                if ptype not in enums:
4996669Snate@binkert.org                    enums.add(ptype)
5006654Snate@binkert.org            pds = param.swig_predecls()
5017673Snate@binkert.org            if isinstance(pds, (list, tuple)):
5025517Snate@binkert.org                add_pds(*pds)
5038126Sgblack@eecs.umich.edu            else:
5045798Snate@binkert.org                add_pds(pds)
5057756SAli.Saidi@ARM.com
5067816Ssteve.reinhardt@amd.com    print >>out, '%module params'
5075798Snate@binkert.org
5085798Snate@binkert.org    print >>out, '%{'
5095517Snate@binkert.org    for obj in ordered_objs:
5105517Snate@binkert.org        print >>out, '#include "params/%s.hh"' % obj
5117673Snate@binkert.org    print >>out, '%}'
5125517Snate@binkert.org
5135517Snate@binkert.org    for pd in predecls:
5147673Snate@binkert.org        print >>out, pd
5157673Snate@binkert.org
5165517Snate@binkert.org    enums = list(enums)
5175798Snate@binkert.org    enums.sort()
5185798Snate@binkert.org    for enum in enums:
5198333Snate@binkert.org        print >>out, '%%include "enums/%s.hh"' % enum.__name__
5207816Ssteve.reinhardt@amd.com    print >>out
5215798Snate@binkert.org
5225798Snate@binkert.org    for obj in ordered_objs:
5234762Snate@binkert.org        if obj.swig_objdecls:
5244762Snate@binkert.org            for decl in obj.swig_objdecls:
5254762Snate@binkert.org                print >>out, decl
5264762Snate@binkert.org            continue
5274762Snate@binkert.org
5285517Snate@binkert.org        code = ''
5295517Snate@binkert.org        base = obj.get_base()
5305517Snate@binkert.org
5315517Snate@binkert.org        code += '// stop swig from creating/wrapping default ctor/dtor\n'
5325517Snate@binkert.org        code += '%%nodefault %s;\n' % obj.cxx_class
5335517Snate@binkert.org        code += 'class %s ' % obj.cxx_class
5347673Snate@binkert.org        if base:
5357673Snate@binkert.org            code += ': public %s' % base
5367673Snate@binkert.org        code += ' {};\n'
5375517Snate@binkert.org
5385517Snate@binkert.org        klass = obj.cxx_class;
5395517Snate@binkert.org        if hasattr(obj, 'cxx_namespace'):
5405517Snate@binkert.org            new_code = 'namespace %s {\n' % obj.cxx_namespace
5415517Snate@binkert.org            new_code += code
5425517Snate@binkert.org            new_code += '}\n'
5435517Snate@binkert.org            code = new_code
5447673Snate@binkert.org            klass = '%s::%s' % (obj.cxx_namespace, klass)
5457677Snate@binkert.org
5467673Snate@binkert.org        print >>out, code
5477673Snate@binkert.org
5485517Snate@binkert.org    print >>out, '%%include "src/sim/sim_object_params.hh"' % obj
5495517Snate@binkert.org    for obj in ordered_objs:
5505517Snate@binkert.org        print >>out, '%%include "params/%s.hh"' % obj
5515517Snate@binkert.org
5525517Snate@binkert.orgparams_file = File('params/params.i')
5535517Snate@binkert.orgnames = sort_list(sim_objects.keys())
5545517Snate@binkert.orgenv.Command(params_file, [ Value(v) for v in names ], buildParams)
5557673Snate@binkert.orgenv.Depends(params_file, params_hh_files + params_i_files + depends)
5567673Snate@binkert.orgSwigSource('m5.objects', params_file)
5577673Snate@binkert.org
5585517Snate@binkert.org# Build all swig modules
5595517Snate@binkert.orgswig_modules = []
5605517Snate@binkert.orgcc_swig_sources = []
5615517Snate@binkert.orgfor source,package in swig_sources:
5625517Snate@binkert.org    filename = str(source)
5635517Snate@binkert.org    assert filename.endswith('.i')
5645517Snate@binkert.org
5657673Snate@binkert.org    base = '.'.join(filename.split('.')[:-1])
5667673Snate@binkert.org    module = basename(base)
5677673Snate@binkert.org    cc_file = base + '_wrap.cc'
5685517Snate@binkert.org    py_file = base + '.py'
5697675Snate@binkert.org
5707675Snate@binkert.org    env.Command([cc_file, py_file], source,
5717675Snate@binkert.org                '$SWIG $SWIGFLAGS -outdir ${TARGETS[1].dir} '
5727675Snate@binkert.org                '-o ${TARGETS[0]} $SOURCES')
5737675Snate@binkert.org    env.Depends(py_file, source)
5747675Snate@binkert.org    env.Depends(cc_file, source)
5757675Snate@binkert.org
5767675Snate@binkert.org    swig_modules.append(Value(module))
5777677Snate@binkert.org    cc_swig_sources.append(File(cc_file))
5787675Snate@binkert.org    PySource(package, py_file)
5797675Snate@binkert.org
5807675Snate@binkert.org# Generate the main swig init file
5817675Snate@binkert.orgdef makeSwigInit(target, source, env):
5827675Snate@binkert.org    f = file(str(target[0]), 'w')
5837675Snate@binkert.org    print >>f, 'extern "C" {'
5847675Snate@binkert.org    for module in source:
5857675Snate@binkert.org        print >>f, '    void init_%s();' % module.get_contents()
5867675Snate@binkert.org    print >>f, '}'
5874762Snate@binkert.org    print >>f, 'void initSwig() {'
5884762Snate@binkert.org    for module in source:
5896143Snate@binkert.org        print >>f, '    init_%s();' % module.get_contents()
5906143Snate@binkert.org    print >>f, '}'
5916143Snate@binkert.org    f.close()
5924762Snate@binkert.org
5934762Snate@binkert.orgenv.Command('python/swig/init.cc', swig_modules, makeSwigInit)
5944762Snate@binkert.orgSource('python/swig/init.cc')
5957756SAli.Saidi@ARM.com
5967816Ssteve.reinhardt@amd.com# Generate traceflags.py
5974762Snate@binkert.orgdef traceFlagsPy(target, source, env):
5984762Snate@binkert.org    assert(len(target) == 1)
5994762Snate@binkert.org
6005463Snate@binkert.org    f = file(str(target[0]), 'w')
6015517Snate@binkert.org
6027677Snate@binkert.org    allFlags = []
6035463Snate@binkert.org    for s in source:
6047756SAli.Saidi@ARM.com        val = eval(s.get_contents())
6057816Ssteve.reinhardt@amd.com        allFlags.append(val)
6064762Snate@binkert.org
6077677Snate@binkert.org    print >>f, 'baseFlags = ['
6084762Snate@binkert.org    for flag, compound, desc in allFlags:
6094762Snate@binkert.org        if not compound:
6106143Snate@binkert.org            print >>f, "    '%s'," % flag
6116143Snate@binkert.org    print >>f, "    ]"
6126143Snate@binkert.org    print >>f
6134762Snate@binkert.org
6144762Snate@binkert.org    print >>f, 'compoundFlags = ['
6157756SAli.Saidi@ARM.com    print >>f, "    'All',"
6167816Ssteve.reinhardt@amd.com    for flag, compound, desc in allFlags:
6174762Snate@binkert.org        if compound:
6184762Snate@binkert.org            print >>f, "    '%s'," % flag
6194762Snate@binkert.org    print >>f, "    ]"
6204762Snate@binkert.org    print >>f
6217756SAli.Saidi@ARM.com
6227816Ssteve.reinhardt@amd.com    print >>f, "allFlags = frozenset(baseFlags + compoundFlags)"
6234762Snate@binkert.org    print >>f
6244762Snate@binkert.org
6257677Snate@binkert.org    print >>f, 'compoundFlagMap = {'
6267756SAli.Saidi@ARM.com    all = tuple([flag for flag,compound,desc in allFlags if not compound])
6277816Ssteve.reinhardt@amd.com    print >>f, "    'All' : %s," % (all, )
6287675Snate@binkert.org    for flag, compound, desc in allFlags:
6297677Snate@binkert.org        if compound:
6305517Snate@binkert.org            print >>f, "    '%s' : %s," % (flag, compound)
6317675Snate@binkert.org    print >>f, "    }"
6327675Snate@binkert.org    print >>f
6337675Snate@binkert.org
6347675Snate@binkert.org    print >>f, 'flagDescriptions = {'
6357675Snate@binkert.org    print >>f, "    'All' : 'All flags',"
6367675Snate@binkert.org    for flag, compound, desc in allFlags:
6377675Snate@binkert.org        print >>f, "    '%s' : '%s'," % (flag, desc)
6385517Snate@binkert.org    print >>f, "    }"
6397673Snate@binkert.org
6405517Snate@binkert.org    f.close()
6417677Snate@binkert.org
6427675Snate@binkert.orgdef traceFlagsCC(target, source, env):
6437673Snate@binkert.org    assert(len(target) == 1)
6447675Snate@binkert.org
6457675Snate@binkert.org    f = file(str(target[0]), 'w')
6467675Snate@binkert.org
6477673Snate@binkert.org    allFlags = []
6487675Snate@binkert.org    for s in source:
6495517Snate@binkert.org        val = eval(s.get_contents())
6507675Snate@binkert.org        allFlags.append(val)
6517675Snate@binkert.org
6527673Snate@binkert.org    # file header
6537675Snate@binkert.org    print >>f, '''
6547675Snate@binkert.org/*
6557677Snate@binkert.org * DO NOT EDIT THIS FILE! Automatically generated
6567675Snate@binkert.org */
6577675Snate@binkert.org
6587675Snate@binkert.org#include "base/traceflags.hh"
6595517Snate@binkert.org
6607675Snate@binkert.orgusing namespace Trace;
6615517Snate@binkert.org
6627673Snate@binkert.orgconst char *Trace::flagStrings[] =
6635517Snate@binkert.org{'''
6647675Snate@binkert.org
6657677Snate@binkert.org    # The string array is used by SimpleEnumParam to map the strings
6667756SAli.Saidi@ARM.com    # provided by the user to enum values.
6677816Ssteve.reinhardt@amd.com    for flag, compound, desc in allFlags:
6687675Snate@binkert.org        if not compound:
6697677Snate@binkert.org            print >>f, '    "%s",' % flag
6704762Snate@binkert.org
6717674Snate@binkert.org    print >>f, '    "All",'
6727674Snate@binkert.org    for flag, compound, desc in allFlags:
6737674Snate@binkert.org        if compound:
6747674Snate@binkert.org            print >>f, '    "%s",' % flag
6757674Snate@binkert.org
6767674Snate@binkert.org    print >>f, '};'
6777674Snate@binkert.org    print >>f
6787674Snate@binkert.org    print >>f, 'const int Trace::numFlagStrings = %d;' % (len(allFlags) + 1)
6797674Snate@binkert.org    print >>f
6807674Snate@binkert.org
6817674Snate@binkert.org    #
6827674Snate@binkert.org    # Now define the individual compound flag arrays.  There is an array
6837674Snate@binkert.org    # for each compound flag listing the component base flags.
6847674Snate@binkert.org    #
6857674Snate@binkert.org    all = tuple([flag for flag,compound,desc in allFlags if not compound])
6864762Snate@binkert.org    print >>f, 'static const Flags AllMap[] = {'
6876143Snate@binkert.org    for flag, compound, desc in allFlags:
6886143Snate@binkert.org        if not compound:
6897756SAli.Saidi@ARM.com            print >>f, "    %s," % flag
6907816Ssteve.reinhardt@amd.com    print >>f, '};'
6918235Snate@binkert.org    print >>f
6928235Snate@binkert.org
6937756SAli.Saidi@ARM.com    for flag, compound, desc in allFlags:
6947816Ssteve.reinhardt@amd.com        if not compound:
6958235Snate@binkert.org            continue
6964382Sbinkertn@umich.edu        print >>f, 'static const Flags %sMap[] = {' % flag
6978232Snate@binkert.org        for flag in compound:
6988232Snate@binkert.org            print >>f, "    %s," % flag
6998232Snate@binkert.org        print >>f, "    (Flags)-1"
7008232Snate@binkert.org        print >>f, '};'
7018232Snate@binkert.org        print >>f
7026229Snate@binkert.org
7038232Snate@binkert.org    #
7048232Snate@binkert.org    # Finally the compoundFlags[] array maps the compound flags
7058232Snate@binkert.org    # to their individual arrays/
7066229Snate@binkert.org    #
7077673Snate@binkert.org    print >>f, 'const Flags *Trace::compoundFlags[] ='
7085517Snate@binkert.org    print >>f, '{'
7095517Snate@binkert.org    print >>f, '    AllMap,'
7107673Snate@binkert.org    for flag, compound, desc in allFlags:
7115517Snate@binkert.org        if compound:
7125517Snate@binkert.org            print >>f, '    %sMap,' % flag
7135517Snate@binkert.org    # file trailer
7145517Snate@binkert.org    print >>f, '};'
7158232Snate@binkert.org
7167673Snate@binkert.org    f.close()
7177673Snate@binkert.org
7188232Snate@binkert.orgdef traceFlagsHH(target, source, env):
7198232Snate@binkert.org    assert(len(target) == 1)
7208232Snate@binkert.org
7218232Snate@binkert.org    f = file(str(target[0]), 'w')
7227673Snate@binkert.org
7235517Snate@binkert.org    allFlags = []
7248232Snate@binkert.org    for s in source:
7258232Snate@binkert.org        val = eval(s.get_contents())
7268232Snate@binkert.org        allFlags.append(val)
7278232Snate@binkert.org
7287673Snate@binkert.org    # file header boilerplate
7298232Snate@binkert.org    print >>f, '''
7308232Snate@binkert.org/*
7318232Snate@binkert.org * DO NOT EDIT THIS FILE!
7328232Snate@binkert.org *
7338232Snate@binkert.org * Automatically generated from traceflags.py
7348232Snate@binkert.org */
7357673Snate@binkert.org
7365517Snate@binkert.org#ifndef __BASE_TRACE_FLAGS_HH__
7378232Snate@binkert.org#define __BASE_TRACE_FLAGS_HH__
7388232Snate@binkert.org
7395517Snate@binkert.orgnamespace Trace {
7407673Snate@binkert.org
7415517Snate@binkert.orgenum Flags {'''
7428232Snate@binkert.org
7438232Snate@binkert.org    # Generate the enum.  Base flags come first, then compound flags.
7445517Snate@binkert.org    idx = 0
7458232Snate@binkert.org    for flag, compound, desc in allFlags:
7468232Snate@binkert.org        if not compound:
7478232Snate@binkert.org            print >>f, '    %s = %d,' % (flag, idx)
7487673Snate@binkert.org            idx += 1
7495517Snate@binkert.org
7505517Snate@binkert.org    numBaseFlags = idx
7517673Snate@binkert.org    print >>f, '    NumFlags = %d,' % idx
7525517Snate@binkert.org
7535517Snate@binkert.org    # put a comment in here to separate base from compound flags
7545517Snate@binkert.org    print >>f, '''
7558232Snate@binkert.org// The remaining enum values are *not* valid indices for Trace::flags.
7565517Snate@binkert.org// They are "compound" flags, which correspond to sets of base
7575517Snate@binkert.org// flags, and are used by changeFlag.'''
7588232Snate@binkert.org
7598232Snate@binkert.org    print >>f, '    All = %d,' % idx
7605517Snate@binkert.org    idx += 1
7618232Snate@binkert.org    for flag, compound, desc in allFlags:
7628232Snate@binkert.org        if compound:
7635517Snate@binkert.org            print >>f, '    %s = %d,' % (flag, idx)
7648232Snate@binkert.org            idx += 1
7658232Snate@binkert.org
7668232Snate@binkert.org    numCompoundFlags = idx - numBaseFlags
7675517Snate@binkert.org    print >>f, '    NumCompoundFlags = %d' % numCompoundFlags
7688232Snate@binkert.org
7698232Snate@binkert.org    # trailer boilerplate
7708232Snate@binkert.org    print >>f, '''\
7718232Snate@binkert.org}; // enum Flags
7728232Snate@binkert.org
7738232Snate@binkert.org// Array of strings for SimpleEnumParam
7745517Snate@binkert.orgextern const char *flagStrings[];
7758232Snate@binkert.orgextern const int numFlagStrings;
7768232Snate@binkert.org
7775517Snate@binkert.org// Array of arraay pointers: for each compound flag, gives the list of
7788232Snate@binkert.org// base flags to set.  Inidividual flag arrays are terminated by -1.
7797673Snate@binkert.orgextern const Flags *compoundFlags[];
7805517Snate@binkert.org
7817673Snate@binkert.org/* namespace Trace */ }
7825517Snate@binkert.org
7838232Snate@binkert.org#endif // __BASE_TRACE_FLAGS_HH__
7848232Snate@binkert.org'''
7858232Snate@binkert.org
7865192Ssaidi@eecs.umich.edu    f.close()
7878232Snate@binkert.org
7888232Snate@binkert.orgflags = [ Value(f) for f in trace_flags ]
7898232Snate@binkert.orgenv.Command('base/traceflags.py', flags, traceFlagsPy)
7908232Snate@binkert.orgPySource('m5', 'base/traceflags.py')
7918232Snate@binkert.org
7925192Ssaidi@eecs.umich.eduenv.Command('base/traceflags.hh', flags, traceFlagsHH)
7937674Snate@binkert.orgenv.Command('base/traceflags.cc', flags, traceFlagsCC)
7945522Snate@binkert.orgSource('base/traceflags.cc')
7955522Snate@binkert.org
7967674Snate@binkert.org# Generate program_info.cc
7977674Snate@binkert.orgdef programInfo(target, source, env):
7987674Snate@binkert.org    def gen_file(target, rev, node, date):
7997674Snate@binkert.org        pi_stats = file(target, 'w')
8007674Snate@binkert.org        print >>pi_stats, 'const char *hgRev = "%s:%s";' %  (rev, node)
8017674Snate@binkert.org        print >>pi_stats, 'const char *hgDate = "%s";' % date
8027674Snate@binkert.org        pi_stats.close()
8037674Snate@binkert.org
8045522Snate@binkert.org    target = str(target[0])
8055522Snate@binkert.org    scons_dir = str(source[0].get_contents())
8065522Snate@binkert.org    try:
8075517Snate@binkert.org        import mercurial.demandimport, mercurial.hg, mercurial.ui
8085522Snate@binkert.org        import mercurial.util, mercurial.node
8095517Snate@binkert.org        if not exists(scons_dir) or not isdir(scons_dir) or \
8106143Snate@binkert.org               not exists(joinpath(scons_dir, ".hg")):
8116727Ssteve.reinhardt@amd.com            raise ValueError
8125522Snate@binkert.org        repo = mercurial.hg.repository(mercurial.ui.ui(), scons_dir)
8135522Snate@binkert.org        rev = mercurial.node.nullrev + repo.changelog.count()
8145522Snate@binkert.org        changenode = repo.changelog.node(rev)
8157674Snate@binkert.org        changes = repo.changelog.read(changenode)
8165517Snate@binkert.org        date = mercurial.util.datestr(changes[2])
8177673Snate@binkert.org
8187673Snate@binkert.org        gen_file(target, rev, mercurial.node.hex(changenode), date)
8197674Snate@binkert.org
8207673Snate@binkert.org        mercurial.demandimport.disable()
8217674Snate@binkert.org    except ImportError:
8227674Snate@binkert.org        gen_file(target, "Unknown", "Unknown", "Unknown")
8237674Snate@binkert.org
8247674Snate@binkert.org    except:
8257674Snate@binkert.org        print "in except"
8267674Snate@binkert.org        gen_file(target, "Unknown", "Unknown", "Unknown")
8275522Snate@binkert.org        mercurial.demandimport.disable()
8285522Snate@binkert.org
8297674Snate@binkert.orgenv.Command('base/program_info.cc',
8307674Snate@binkert.org            Value(str(SCons.Node.FS.default_fs.SConstruct_dir)),
8317674Snate@binkert.org            programInfo)
8327674Snate@binkert.org
8337673Snate@binkert.org# embed python files.  All .py files that have been indicated by a
8347674Snate@binkert.org# PySource() call in a SConscript need to be embedded into the M5
8357674Snate@binkert.org# library.  To do that, we compile the file to byte code, marshal the
8367674Snate@binkert.org# byte code, compress it, and then generate an assembly file that
8377674Snate@binkert.org# inserts the result into the data section with symbols indicating the
8387674Snate@binkert.org# beginning, and end (and with the size at the end)
8397674Snate@binkert.orgpy_sources_tnodes = {}
8407674Snate@binkert.orgfor pysource in py_sources:
8417674Snate@binkert.org    py_sources_tnodes[pysource.tnode] = pysource
8427811Ssteve.reinhardt@amd.com
8437674Snate@binkert.orgdef objectifyPyFile(target, source, env):
8447673Snate@binkert.org    '''Action function to compile a .py into a code object, marshal
8455522Snate@binkert.org    it, compress it, and stick it into an asm file so the code appears
8466143Snate@binkert.org    as just bytes with a label in the data section'''
8477756SAli.Saidi@ARM.com
8487816Ssteve.reinhardt@amd.com    src = file(str(source[0]), 'r').read()
8497674Snate@binkert.org    dst = file(str(target[0]), 'w')
8504382Sbinkertn@umich.edu
8514382Sbinkertn@umich.edu    pysource = py_sources_tnodes[source[0]]
8524382Sbinkertn@umich.edu    compiled = compile(src, pysource.snode.path, 'exec')
8534382Sbinkertn@umich.edu    marshalled = marshal.dumps(compiled)
8544382Sbinkertn@umich.edu    compressed = zlib.compress(marshalled)
8554382Sbinkertn@umich.edu    data = compressed
8564382Sbinkertn@umich.edu
8574382Sbinkertn@umich.edu    # Some C/C++ compilers prepend an underscore to global symbol
8584382Sbinkertn@umich.edu    # names, so if they're going to do that, we need to prepend that
8594382Sbinkertn@umich.edu    # leading underscore to globals in the assembly file.
8606143Snate@binkert.org    if env['LEADING_UNDERSCORE']:
861955SN/A        sym = '_' + pysource.symname
8622655Sstever@eecs.umich.edu    else:
8632655Sstever@eecs.umich.edu        sym = pysource.symname
8642655Sstever@eecs.umich.edu
8652655Sstever@eecs.umich.edu    step = 16
8662655Sstever@eecs.umich.edu    print >>dst, ".data"
8675601Snate@binkert.org    print >>dst, ".globl %s_beg" % sym
8685601Snate@binkert.org    print >>dst, ".globl %s_end" % sym
8698334Snate@binkert.org    print >>dst, "%s_beg:" % sym
8708334Snate@binkert.org    for i in xrange(0, len(data), step):
8718334Snate@binkert.org        x = array.array('B', data[i:i+step])
8725522Snate@binkert.org        print >>dst, ".byte", ','.join([str(d) for d in x])
8735863Snate@binkert.org    print >>dst, "%s_end:" % sym
8745601Snate@binkert.org    print >>dst, ".long %d" % len(marshalled)
8755601Snate@binkert.org
8765601Snate@binkert.orgfor source in py_sources:
8775863Snate@binkert.org    env.Command(source.assembly, source.tnode, objectifyPyFile)
8786143Snate@binkert.org    Source(source.assembly)
8795559Snate@binkert.org
8805559Snate@binkert.org# Generate init_python.cc which creates a bunch of EmbeddedPyModule
8815559Snate@binkert.org# structs that describe the embedded python code.  One such struct
8825559Snate@binkert.org# contains information about the importer that python uses to get at
8835601Snate@binkert.org# the embedded files, and then there's a list of all of the rest that
8846143Snate@binkert.org# the importer uses to load the rest on demand.
8856143Snate@binkert.orgpy_sources_symbols = {}
8866143Snate@binkert.orgfor pysource in py_sources:
8876143Snate@binkert.org    py_sources_symbols[pysource.symname] = pysource
8886143Snate@binkert.orgdef pythonInit(target, source, env):
8896143Snate@binkert.org    dst = file(str(target[0]), 'w')
8906143Snate@binkert.org
8916143Snate@binkert.org    def dump_mod(sym, endchar=','):
8926143Snate@binkert.org        pysource = py_sources_symbols[sym]
8936143Snate@binkert.org        print >>dst, '    { "%s",' % pysource.arcname
8946143Snate@binkert.org        print >>dst, '      "%s",' % pysource.modpath
8956143Snate@binkert.org        print >>dst, '       %s_beg, %s_end,' % (sym, sym)
8966143Snate@binkert.org        print >>dst, '       %s_end - %s_beg,' % (sym, sym)
8976143Snate@binkert.org        print >>dst, '       *(int *)%s_end }%s'  % (sym, endchar)
8986143Snate@binkert.org    
8996143Snate@binkert.org    print >>dst, '#include "sim/init.hh"'
9006143Snate@binkert.org
9016143Snate@binkert.org    for sym in source:
9026143Snate@binkert.org        sym = sym.get_contents()
9036143Snate@binkert.org        print >>dst, "extern const char %s_beg[], %s_end[];" % (sym, sym)
9046143Snate@binkert.org
9056143Snate@binkert.org    print >>dst, "const EmbeddedPyModule embeddedPyImporter = "
9066143Snate@binkert.org    dump_mod("PyEMB_importer", endchar=';');
9076143Snate@binkert.org    print >>dst
9086143Snate@binkert.org
9098233Snate@binkert.org    print >>dst, "const EmbeddedPyModule embeddedPyModules[] = {"
9108233Snate@binkert.org    for i,sym in enumerate(source):
9118233Snate@binkert.org        sym = sym.get_contents()
9126143Snate@binkert.org        if sym == "PyEMB_importer":
9136143Snate@binkert.org            # Skip the importer since we've already exported it
9146143Snate@binkert.org            continue
9156143Snate@binkert.org        dump_mod(sym)
9166143Snate@binkert.org    print >>dst, "    { 0, 0, 0, 0, 0, 0 }"
9176240Snate@binkert.org    print >>dst, "};"
9185554Snate@binkert.org
9195522Snate@binkert.orgsymbols = [Value(s.symname) for s in py_sources]
9205522Snate@binkert.orgenv.Command('sim/init_python.cc', symbols, pythonInit)
9215797Snate@binkert.orgSource('sim/init_python.cc')
9225797Snate@binkert.org
9235522Snate@binkert.org########################################################################
9245601Snate@binkert.org#
9258233Snate@binkert.org# Define binaries.  Each different build type (debug, opt, etc.) gets
9268233Snate@binkert.org# a slightly different build environment.
9278235Snate@binkert.org#
9288235Snate@binkert.org
9298235Snate@binkert.org# List of constructed environments to pass back to SConstruct
9308235Snate@binkert.orgenvList = []
9318235Snate@binkert.org
9328235Snate@binkert.org# This function adds the specified sources to the given build
9338235Snate@binkert.org# environment, and returns a list of all the corresponding SCons
9346143Snate@binkert.org# Object nodes (including an extra one for date.cc).  We explicitly
9352655Sstever@eecs.umich.edu# add the Object nodes so we can set up special dependencies for
9366143Snate@binkert.org# date.cc.
9376143Snate@binkert.orgdef make_objs(sources, env):
9388233Snate@binkert.org    objs = [env.Object(s) for s in sources]
9396143Snate@binkert.org  
9406143Snate@binkert.org    # make date.cc depend on all other objects so it always gets
9414007Ssaidi@eecs.umich.edu    # recompiled whenever anything else does
9424596Sbinkertn@umich.edu    date_obj = env.Object('base/date.cc')
9434007Ssaidi@eecs.umich.edu
9444596Sbinkertn@umich.edu    # Make the generation of program_info.cc dependend on all 
9457756SAli.Saidi@ARM.com    # the other cc files and the compiling of program_info.cc 
9467816Ssteve.reinhardt@amd.com    # dependent on all the objects but program_info.o 
9478334Snate@binkert.org    pinfo_obj = env.Object('base/program_info.cc')
9488334Snate@binkert.org    env.Depends('base/program_info.cc', sources)
9498334Snate@binkert.org    env.Depends(date_obj, objs)
9508334Snate@binkert.org    env.Depends(pinfo_obj, objs)
9515601Snate@binkert.org    objs.extend([date_obj,pinfo_obj])
9525601Snate@binkert.org    return objs
9532655Sstever@eecs.umich.edu
954955SN/A# Function to create a new build environment as clone of current
9553918Ssaidi@eecs.umich.edu# environment 'env' with modified object suffix and optional stripped
9563918Ssaidi@eecs.umich.edu# binary.  Additional keyword arguments are appended to corresponding
9573918Ssaidi@eecs.umich.edu# build environment vars.
9583918Ssaidi@eecs.umich.edudef makeEnv(label, objsfx, strip = False, **kwargs):
9593918Ssaidi@eecs.umich.edu    newEnv = env.Copy(OBJSUFFIX=objsfx)
9603918Ssaidi@eecs.umich.edu    newEnv.Label = label
9613918Ssaidi@eecs.umich.edu    newEnv.Append(**kwargs)
9623918Ssaidi@eecs.umich.edu
9633918Ssaidi@eecs.umich.edu    swig_env = newEnv.Copy()
9643918Ssaidi@eecs.umich.edu    if env['GCC']:
9653918Ssaidi@eecs.umich.edu        swig_env.Append(CCFLAGS='-Wno-uninitialized')
9663918Ssaidi@eecs.umich.edu        swig_env.Append(CCFLAGS='-Wno-sign-compare')
9673918Ssaidi@eecs.umich.edu        swig_env.Append(CCFLAGS='-Wno-parentheses')
9683918Ssaidi@eecs.umich.edu    swig_objs = [ swig_env.Object(s) for s in cc_swig_sources ]
9693940Ssaidi@eecs.umich.edu
9703940Ssaidi@eecs.umich.edu    # First make a library of everything but main() so other programs can
9713940Ssaidi@eecs.umich.edu    # link against m5.
9723942Ssaidi@eecs.umich.edu    #
9733940Ssaidi@eecs.umich.edu    # SCons doesn't know to append a library suffix when there is a '.' in the
9743515Ssaidi@eecs.umich.edu    # name.  Use '_' instead.
9753918Ssaidi@eecs.umich.edu
9764762Snate@binkert.org    m5lib = newEnv.Library('m5_' + label,
9773515Ssaidi@eecs.umich.edu        make_objs(cc_lib_sources, newEnv) + swig_objs)
9782655Sstever@eecs.umich.edu
9793918Ssaidi@eecs.umich.edu    for target, sources in unit_tests:
9803619Sbinkertn@umich.edu        objs = [ newEnv.StaticObject(s) for s in sources ]
981955SN/A        newEnv.Program("unittest/%s.%s" % (target, label), objs + m5lib)
982955SN/A
9832655Sstever@eecs.umich.edu    # Now link a stub with main() and the library.
9843918Ssaidi@eecs.umich.edu    exe = 'm5.' + label  # final executable
9853619Sbinkertn@umich.edu    objects = [newEnv.Object(s) for s in cc_bin_sources] + m5lib
986955SN/A    if strip:
987955SN/A        unstripped_exe = exe + '.unstripped'
9882655Sstever@eecs.umich.edu        newEnv.Program(unstripped_exe, objects)
9893918Ssaidi@eecs.umich.edu        if sys.platform == 'sunos5':
9903619Sbinkertn@umich.edu            cmd = 'cp $SOURCE $TARGET; strip $TARGET'
991955SN/A        else:
992955SN/A            cmd = 'strip $SOURCE -o $TARGET'
9932655Sstever@eecs.umich.edu        targets = newEnv.Command(exe, unstripped_exe, cmd)
9943918Ssaidi@eecs.umich.edu    else:
9953683Sstever@eecs.umich.edu        targets = newEnv.Program(exe, objects)
9962655Sstever@eecs.umich.edu            
9971869SN/A    newEnv.M5Binary = targets[0]
9981869SN/A    envList.append(newEnv)
999
1000# Debug binary
1001ccflags = {}
1002if env['GCC']:
1003    if sys.platform == 'sunos5':
1004        ccflags['debug'] = '-gstabs+'
1005    else:
1006        ccflags['debug'] = '-ggdb3'
1007    ccflags['opt'] = '-g -O3'
1008    ccflags['fast'] = '-O3'
1009    ccflags['prof'] = '-O3 -g -pg'
1010elif env['SUNCC']:
1011    ccflags['debug'] = '-g0'
1012    ccflags['opt'] = '-g -O'
1013    ccflags['fast'] = '-fast'
1014    ccflags['prof'] = '-fast -g -pg'
1015elif env['ICC']:
1016    ccflags['debug'] = '-g -O0'
1017    ccflags['opt'] = '-g -O'
1018    ccflags['fast'] = '-fast'
1019    ccflags['prof'] = '-fast -g -pg'
1020else:
1021    print 'Unknown compiler, please fix compiler options'
1022    Exit(1)
1023
1024makeEnv('debug', '.do',
1025        CCFLAGS = Split(ccflags['debug']),
1026        CPPDEFINES = ['DEBUG', 'TRACING_ON=1'])
1027
1028# Optimized binary
1029makeEnv('opt', '.o',
1030        CCFLAGS = Split(ccflags['opt']),
1031        CPPDEFINES = ['TRACING_ON=1'])
1032
1033# "Fast" binary
1034makeEnv('fast', '.fo', strip = True,
1035        CCFLAGS = Split(ccflags['fast']),
1036        CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'])
1037
1038# Profiled binary
1039makeEnv('prof', '.po',
1040        CCFLAGS = Split(ccflags['prof']),
1041        CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1042        LINKFLAGS = '-pg')
1043
1044Return('envList')
1045