SConscript revision 5623
1955SN/A# -*- mode:python -*-
2955SN/A
31762SN/A# Copyright (c) 2004-2005 The Regents of The University of Michigan
4955SN/A# All rights reserved.
5955SN/A#
6955SN/A# Redistribution and use in source and binary forms, with or without
7955SN/A# modification, are permitted provided that the following conditions are
8955SN/A# met: redistributions of source code must retain the above copyright
9955SN/A# notice, this list of conditions and the following disclaimer;
10955SN/A# redistributions in binary form must reproduce the above copyright
11955SN/A# notice, this list of conditions and the following disclaimer in the
12955SN/A# documentation and/or other materials provided with the distribution;
13955SN/A# neither the name of the copyright holders nor the names of its
14955SN/A# contributors may be used to endorse or promote products derived from
15955SN/A# this software without specific prior written permission.
16955SN/A#
17955SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18955SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19955SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20955SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21955SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22955SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23955SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24955SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25955SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26955SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27955SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
282665Ssaidi@eecs.umich.edu#
294762Snate@binkert.org# Authors: Nathan Binkert
30955SN/A
315522Snate@binkert.orgimport array
326143Snate@binkert.orgimport imp
334762Snate@binkert.orgimport marshal
345522Snate@binkert.orgimport os
35955SN/Aimport re
365522Snate@binkert.orgimport sys
37955SN/Aimport zlib
385522Snate@binkert.org
394202Sbinkertn@umich.edufrom os.path import basename, exists, isdir, isfile, join as joinpath
405742Snate@binkert.org
41955SN/Aimport SCons
424381Sbinkertn@umich.edu
434381Sbinkertn@umich.edu# This file defines how to build a particular configuration of M5
448334Snate@binkert.org# based on variable settings in the 'env' build environment.
45955SN/A
46955SN/AImport('*')
474202Sbinkertn@umich.edu
48955SN/A# Children need to see the environment
494382Sbinkertn@umich.eduExport('env')
504382Sbinkertn@umich.edu
514382Sbinkertn@umich.edubuild_env = dict([(opt, env[opt]) for opt in env.ExportOptions])
526654Snate@binkert.org
535517Snate@binkert.orgdef sort_list(_list):
548614Sgblack@eecs.umich.edu    """return a sorted copy of '_list'"""
557674Snate@binkert.org    if isinstance(_list, list):
566143Snate@binkert.org        _list = _list[:]
576143Snate@binkert.org    else:
586143Snate@binkert.org        _list = list(_list)
598233Snate@binkert.org    _list.sort()
608233Snate@binkert.org    return _list
618233Snate@binkert.org
628233Snate@binkert.orgclass PySourceFile(object):
638233Snate@binkert.org    invalid_sym_char = re.compile('[^A-z0-9_]')
648334Snate@binkert.org    def __init__(self, package, tnode):
658334Snate@binkert.org        snode = tnode.srcnode()
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
1516143Snate@binkert.org    swig_sources.append(val)
1526143Snate@binkert.org
1536143Snate@binkert.orgunit_tests = []
1548945Ssteve.reinhardt@amd.comdef UnitTest(target, sources):
1558233Snate@binkert.org    if not isinstance(sources, (list, tuple)):
1568233Snate@binkert.org        sources = [ sources ]
1576143Snate@binkert.org    
1588945Ssteve.reinhardt@amd.com    srcs = []
1596143Snate@binkert.org    for source in sources:
1606143Snate@binkert.org        if not isinstance(source, SCons.Node.FS.File):
1616143Snate@binkert.org            source = File(source)
1626143Snate@binkert.org        srcs.append(source)
1635522Snate@binkert.org            
1646143Snate@binkert.org    unit_tests.append((target, srcs))
1656143Snate@binkert.org
1666143Snate@binkert.org# Children should have access
1676143Snate@binkert.orgExport('Source')
1688233Snate@binkert.orgExport('BinSource')
1698233Snate@binkert.orgExport('PySource')
1708233Snate@binkert.orgExport('SimObject')
1716143Snate@binkert.orgExport('SwigSource')
1726143Snate@binkert.orgExport('UnitTest')
1736143Snate@binkert.org
1746143Snate@binkert.org########################################################################
1755522Snate@binkert.org#
1765522Snate@binkert.org# Trace Flags
1775522Snate@binkert.org#
1785522Snate@binkert.orgall_flags = {}
1795604Snate@binkert.orgtrace_flags = []
1805604Snate@binkert.orgdef TraceFlag(name, desc=''):
1816143Snate@binkert.org    if name in all_flags:
1826143Snate@binkert.org        raise AttributeError, "Flag %s already specified" % name
1834762Snate@binkert.org    flag = (name, (), desc)
1844762Snate@binkert.org    trace_flags.append(flag)
1856143Snate@binkert.org    all_flags[name] = ()
1866727Ssteve.reinhardt@amd.com
1876727Ssteve.reinhardt@amd.comdef CompoundFlag(name, flags, desc=''):
1886727Ssteve.reinhardt@amd.com    if name in all_flags:
1894762Snate@binkert.org        raise AttributeError, "Flag %s already specified" % name
1906143Snate@binkert.org
1916143Snate@binkert.org    compound = tuple(flags)
1926143Snate@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
1956143Snate@binkert.org        if all_flags[flag]:
1967674Snate@binkert.org            raise AttributeError, \
1977674Snate@binkert.org                "Compound flag can't point to another compound flag"
1985604Snate@binkert.org
1996143Snate@binkert.org    flag = (name, compound, desc)
2006143Snate@binkert.org    trace_flags.append(flag)
2016143Snate@binkert.org    all_flags[name] = compound
2024762Snate@binkert.org
2036143Snate@binkert.orgExport('TraceFlag')
2044762Snate@binkert.orgExport('CompoundFlag')
2054762Snate@binkert.org
2064762Snate@binkert.org########################################################################
2076143Snate@binkert.org#
2086143Snate@binkert.org# Set some compiler variables
2094762Snate@binkert.org#
2108233Snate@binkert.org
2118233Snate@binkert.org# Include file paths are rooted in this directory.  SCons will
2128233Snate@binkert.org# automatically expand '.' to refer to both the source directory and
2138233Snate@binkert.org# the corresponding build directory to pick up generated include
2146143Snate@binkert.org# files.
2156143Snate@binkert.orgenv.Append(CPPPATH=Dir('.'))
2164762Snate@binkert.org
2176143Snate@binkert.org# Add a flag defining what THE_ISA should be for all compilation
2184762Snate@binkert.orgenv.Append(CPPDEFINES=[('THE_ISA','%s_ISA' % env['TARGET_ISA'].upper())])
2196143Snate@binkert.org
2204762Snate@binkert.org########################################################################
2216143Snate@binkert.org#
2228233Snate@binkert.org# Walk the tree and execute all SConscripts in subdirectories
2238233Snate@binkert.org#
2248233Snate@binkert.org
2256143Snate@binkert.orgfor base_dir in base_dir_list:
2266143Snate@binkert.org    here = Dir('.').srcnode().abspath
2276143Snate@binkert.org    for root, dirs, files in os.walk(base_dir, topdown=True):
2286143Snate@binkert.org        if root == here:
2296143Snate@binkert.org            # we don't want to recurse back into this SConscript
2306143Snate@binkert.org            continue
2316143Snate@binkert.org
2326143Snate@binkert.org        if 'SConscript' in files:
2338233Snate@binkert.org            build_dir = joinpath(env['BUILDDIR'], root[len(base_dir) + 1:])
2348233Snate@binkert.org            SConscript(joinpath(root, 'SConscript'), build_dir=build_dir)
235955SN/A
2368235Snate@binkert.orgfor opt in env.ExportOptions:
2378235Snate@binkert.org    env.ConfigFile(opt)
2386143Snate@binkert.org
2398235Snate@binkert.org########################################################################
2408235Snate@binkert.org#
2418235Snate@binkert.org# Prevent any SimObjects from being added after this point, they
2428235Snate@binkert.org# should all have been added in the SConscripts above
2438235Snate@binkert.org#
2448235Snate@binkert.orgclass DictImporter(object):
2458235Snate@binkert.org    '''This importer takes a dictionary of arbitrary module names that
2468235Snate@binkert.org    map to arbitrary filenames.'''
2478235Snate@binkert.org    def __init__(self, modules):
2488235Snate@binkert.org        self.modules = modules
2498235Snate@binkert.org        self.installed = set()
2508235Snate@binkert.org
2518235Snate@binkert.org    def __del__(self):
2528235Snate@binkert.org        self.unload()
2538235Snate@binkert.org
2548235Snate@binkert.org    def unload(self):
2558235Snate@binkert.org        import sys
2565584Snate@binkert.org        for module in self.installed:
2574382Sbinkertn@umich.edu            del sys.modules[module]
2584202Sbinkertn@umich.edu        self.installed = set()
2594382Sbinkertn@umich.edu
2604382Sbinkertn@umich.edu    def find_module(self, fullname, path):
2614382Sbinkertn@umich.edu        if fullname == '__scons':
2625584Snate@binkert.org            return self
2634382Sbinkertn@umich.edu
2644382Sbinkertn@umich.edu        if fullname == 'm5.objects':
2654382Sbinkertn@umich.edu            return self
2668232Snate@binkert.org
2675192Ssaidi@eecs.umich.edu        if fullname.startswith('m5.internal'):
2688232Snate@binkert.org            return None
2698232Snate@binkert.org
2708232Snate@binkert.org        if fullname in self.modules and exists(self.modules[fullname]):
2715192Ssaidi@eecs.umich.edu            return self
2728232Snate@binkert.org
2735192Ssaidi@eecs.umich.edu        return None
2745799Snate@binkert.org
2758232Snate@binkert.org    def load_module(self, fullname):
2765192Ssaidi@eecs.umich.edu        mod = imp.new_module(fullname)
2775192Ssaidi@eecs.umich.edu        sys.modules[fullname] = mod
2785192Ssaidi@eecs.umich.edu        self.installed.add(fullname)
2798232Snate@binkert.org
2805192Ssaidi@eecs.umich.edu        mod.__loader__ = self
2818232Snate@binkert.org        if fullname == 'm5.objects':
2825192Ssaidi@eecs.umich.edu            mod.__path__ = fullname.split('.')
2835192Ssaidi@eecs.umich.edu            return mod
2845192Ssaidi@eecs.umich.edu
2855192Ssaidi@eecs.umich.edu        if fullname == '__scons':
2864382Sbinkertn@umich.edu            mod.__dict__['m5_build_env'] = build_env
2874382Sbinkertn@umich.edu            return mod
2884382Sbinkertn@umich.edu
2892667Sstever@eecs.umich.edu        srcfile = self.modules[fullname]
2902667Sstever@eecs.umich.edu        if basename(srcfile) == '__init__.py':
2912667Sstever@eecs.umich.edu            mod.__path__ = fullname.split('.')
2922667Sstever@eecs.umich.edu        mod.__file__ = srcfile
2932667Sstever@eecs.umich.edu
2942667Sstever@eecs.umich.edu        exec file(srcfile, 'r') in mod.__dict__
2955742Snate@binkert.org
2965742Snate@binkert.org        return mod
2975742Snate@binkert.org
2985793Snate@binkert.orgpy_modules = {}
2998334Snate@binkert.orgfor source in py_sources:
3005793Snate@binkert.org    py_modules[source.modpath] = source.snode.abspath
3015793Snate@binkert.org
3025793Snate@binkert.org# install the python importer so we can grab stuff from the source
3034382Sbinkertn@umich.edu# tree itself.  We can't have SimObjects added after this point or
3044762Snate@binkert.org# else we won't know about them for the rest of the stuff.
3055344Sstever@gmail.comsim_objects_fixed = True
3064382Sbinkertn@umich.eduimporter = DictImporter(py_modules)
3075341Sstever@gmail.comsys.meta_path[0:0] = [ importer ]
3085742Snate@binkert.org
3095742Snate@binkert.orgimport m5
3105742Snate@binkert.org
3115742Snate@binkert.org# import all sim objects so we can populate the all_objects list
3125742Snate@binkert.org# make sure that we're working with a list, then let's sort it
3134762Snate@binkert.orgsim_objects = list(sim_object_modfiles)
3145742Snate@binkert.orgsim_objects.sort()
3155742Snate@binkert.orgfor simobj in sim_objects:
3167722Sgblack@eecs.umich.edu    exec('from m5.objects import %s' % simobj)
3175742Snate@binkert.org
3185742Snate@binkert.org# we need to unload all of the currently imported modules so that they
3195742Snate@binkert.org# will be re-imported the next time the sconscript is run
3205742Snate@binkert.orgimporter.unload()
3218242Sbradley.danofsky@amd.comsys.meta_path.remove(importer)
3228242Sbradley.danofsky@amd.com
3238242Sbradley.danofsky@amd.comsim_objects = m5.SimObject.allClasses
3248242Sbradley.danofsky@amd.comall_enums = m5.params.allEnums
3255341Sstever@gmail.com
3265742Snate@binkert.orgall_params = {}
3277722Sgblack@eecs.umich.edufor name,obj in sim_objects.iteritems():
3284773Snate@binkert.org    for param in obj._params.local.values():
3296108Snate@binkert.org        if not hasattr(param, 'swig_decl'):
3301858SN/A            continue
3311085SN/A        pname = param.ptype_str
3326658Snate@binkert.org        if pname not in all_params:
3336658Snate@binkert.org            all_params[pname] = param
3347673Snate@binkert.org
3356658Snate@binkert.org########################################################################
3366658Snate@binkert.org#
3376658Snate@binkert.org# calculate extra dependencies
3386658Snate@binkert.org#
3396658Snate@binkert.orgmodule_depends = ["m5", "m5.SimObject", "m5.params"]
3406658Snate@binkert.orgdepends = [ File(py_modules[dep]) for dep in module_depends ]
3416658Snate@binkert.org
3427673Snate@binkert.org########################################################################
3437673Snate@binkert.org#
3447673Snate@binkert.org# Commands for the basic automatically generated python files
3457673Snate@binkert.org#
3467673Snate@binkert.org
3477673Snate@binkert.org# Generate Python file containing a dict specifying the current
3487673Snate@binkert.org# build_env flags.
3496658Snate@binkert.orgdef makeDefinesPyFile(target, source, env):
3507673Snate@binkert.org    f = file(str(target[0]), 'w')
3517673Snate@binkert.org    print >>f, "m5_build_env = ", source[0]
3527673Snate@binkert.org    f.close()
3537673Snate@binkert.org
3547673Snate@binkert.org# Generate python file containing info about the M5 source code
3557673Snate@binkert.orgdef makeInfoPyFile(target, source, env):
3567673Snate@binkert.org    f = file(str(target[0]), 'w')
3577673Snate@binkert.org    for src in source:
3587673Snate@binkert.org        data = ''.join(file(src.srcnode().abspath, 'r').xreadlines())
3597673Snate@binkert.org        print >>f, "%s = %s" % (src, repr(data))
3606658Snate@binkert.org    f.close()
3617756SAli.Saidi@ARM.com
3627816Ssteve.reinhardt@amd.com# Generate the __init__.py file for m5.objects
3636658Snate@binkert.orgdef makeObjectsInitFile(target, source, env):
3644382Sbinkertn@umich.edu    f = file(str(target[0]), 'w')
3654382Sbinkertn@umich.edu    print >>f, 'from params import *'
3664762Snate@binkert.org    print >>f, 'from m5.SimObject import *'
3674762Snate@binkert.org    for module in source:
3684762Snate@binkert.org        print >>f, 'from %s import *' % module.get_contents()
3696654Snate@binkert.org    f.close()
3706654Snate@binkert.org
3715517Snate@binkert.org# Generate a file with all of the compile options in it
3725517Snate@binkert.orgenv.Command('python/m5/defines.py', Value(build_env), makeDefinesPyFile)
3735517Snate@binkert.orgPySource('m5', 'python/m5/defines.py')
3745517Snate@binkert.org
3755517Snate@binkert.org# Generate a file that wraps the basic top level files
3765517Snate@binkert.orgenv.Command('python/m5/info.py',
3775517Snate@binkert.org            [ '#/AUTHORS', '#/LICENSE', '#/README', '#/RELEASE_NOTES' ],
3785517Snate@binkert.org            makeInfoPyFile)
3795517Snate@binkert.orgPySource('m5', 'python/m5/info.py')
3805517Snate@binkert.org
3815517Snate@binkert.org# Generate an __init__.py file for the objects package
3825517Snate@binkert.orgenv.Command('python/m5/objects/__init__.py',
3835517Snate@binkert.org            [ Value(o) for o in sort_list(sim_object_modfiles) ],
3845517Snate@binkert.org            makeObjectsInitFile)
3855517Snate@binkert.orgPySource('m5.objects', 'python/m5/objects/__init__.py')
3865517Snate@binkert.org
3875517Snate@binkert.org########################################################################
3886654Snate@binkert.org#
3895517Snate@binkert.org# Create all of the SimObject param headers and enum headers
3905517Snate@binkert.org#
3915517Snate@binkert.org
3925517Snate@binkert.orgdef createSimObjectParam(target, source, env):
3935517Snate@binkert.org    assert len(target) == 1 and len(source) == 1
3945517Snate@binkert.org
3955517Snate@binkert.org    hh_file = file(target[0].abspath, 'w')
3965517Snate@binkert.org    name = str(source[0].get_contents())
3976143Snate@binkert.org    obj = sim_objects[name]
3986654Snate@binkert.org
3995517Snate@binkert.org    print >>hh_file, obj.cxx_decl()
4005517Snate@binkert.org
4015517Snate@binkert.orgdef createSwigParam(target, source, env):
4025517Snate@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
4136654Snate@binkert.org
4146654Snate@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]
4176143Snate@binkert.org
4186143Snate@binkert.org    print >>cc_file, obj.cxx_def()
4196143Snate@binkert.org    cc_file.close()
4206727Ssteve.reinhardt@amd.com
4215517Snate@binkert.orgdef createEnumParam(target, source, env):
4226727Ssteve.reinhardt@amd.com    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())
4266654Snate@binkert.org    obj = all_enums[name]
4276654Snate@binkert.org
4287673Snate@binkert.org    print >>hh_file, obj.cxx_decl()
4296654Snate@binkert.org
4306654Snate@binkert.org# Generate all of the SimObject param struct header files
4316654Snate@binkert.orgparams_hh_files = []
4326654Snate@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)
4366143Snate@binkert.org    params_hh_files.append(hh_file)
4375517Snate@binkert.org    env.Command(hh_file, Value(name), createSimObjectParam)
4384762Snate@binkert.org    env.Depends(hh_file, depends + extra_deps)
4395517Snate@binkert.org
4405517Snate@binkert.org# Generate any parameter header files needed
4416143Snate@binkert.orgparams_i_files = []
4426143Snate@binkert.orgfor name,param in all_params.iteritems():
4435517Snate@binkert.org    if isinstance(param, m5.params.VectorParamDesc):
4445517Snate@binkert.org        ext = 'vptype'
4455517Snate@binkert.org    else:
4465517Snate@binkert.org        ext = 'ptype'
4475517Snate@binkert.org
4485517Snate@binkert.org    i_file = File('params/%s_%s.i' % (name, ext))
4495517Snate@binkert.org    params_i_files.append(i_file)
4505517Snate@binkert.org    env.Command(i_file, Value(name), createSwigParam)
4515517Snate@binkert.org    env.Depends(i_file, depends)
4528596Ssteve.reinhardt@amd.com
4538596Ssteve.reinhardt@amd.com# Generate all enum header files
4548596Ssteve.reinhardt@amd.comfor name,enum in all_enums.iteritems():
4558596Ssteve.reinhardt@amd.com    extra_deps = [ File(py_modules[enum.__module__]) ]
4568596Ssteve.reinhardt@amd.com
4578596Ssteve.reinhardt@amd.com    cc_file = File('enums/%s.cc' % name)
4588596Ssteve.reinhardt@amd.com    env.Command(cc_file, Value(name), createEnumStrings)
4596143Snate@binkert.org    env.Depends(cc_file, depends + extra_deps)
4605517Snate@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)
4656654Snate@binkert.org
4666654Snate@binkert.org# Build the big monolithic swigged params module (wraps all SimObject
4675517Snate@binkert.org# param structs and enum structs)
4685517Snate@binkert.orgdef buildParams(target, source, env):
4695517Snate@binkert.org    names = [ s.get_contents() for s in source ]
4708596Ssteve.reinhardt@amd.com    objs = [ sim_objects[name] for name in names ]
4718596Ssteve.reinhardt@amd.com    out = file(target[0].abspath, 'w')
4724762Snate@binkert.org
4734762Snate@binkert.org    ordered_objs = []
4744762Snate@binkert.org    obj_seen = set()
4754762Snate@binkert.org    def order_obj(obj):
4764762Snate@binkert.org        name = str(obj)
4774762Snate@binkert.org        if name in obj_seen:
4787675Snate@binkert.org            return
4794762Snate@binkert.org
4804762Snate@binkert.org        obj_seen.add(name)
4814762Snate@binkert.org        if str(obj) != 'SimObject':
4824762Snate@binkert.org            order_obj(obj.__bases__[0])
4834382Sbinkertn@umich.edu
4844382Sbinkertn@umich.edu        ordered_objs.append(obj)
4855517Snate@binkert.org
4866654Snate@binkert.org    for obj in objs:
4875517Snate@binkert.org        order_obj(obj)
4888126Sgblack@eecs.umich.edu
4896654Snate@binkert.org    enums = set()
4907673Snate@binkert.org    predecls = []
4916654Snate@binkert.org    pd_seen = set()
4926654Snate@binkert.org
4936654Snate@binkert.org    def add_pds(*pds):
4946654Snate@binkert.org        for pd in pds:
4956654Snate@binkert.org            if pd not in pd_seen:
4966654Snate@binkert.org                predecls.append(pd)
4976654Snate@binkert.org                pd_seen.add(pd)
4986669Snate@binkert.org
4996669Snate@binkert.org    for obj in ordered_objs:
5006669Snate@binkert.org        params = obj._params.local.values()
5016669Snate@binkert.org        for param in params:
5026669Snate@binkert.org            ptype = param.ptype
5036669Snate@binkert.org            if issubclass(ptype, m5.params.Enum):
5046654Snate@binkert.org                if ptype not in enums:
5057673Snate@binkert.org                    enums.add(ptype)
5065517Snate@binkert.org            pds = param.swig_predecls()
5078126Sgblack@eecs.umich.edu            if isinstance(pds, (list, tuple)):
5085798Snate@binkert.org                add_pds(*pds)
5097756SAli.Saidi@ARM.com            else:
5107816Ssteve.reinhardt@amd.com                add_pds(pds)
5115798Snate@binkert.org
5125798Snate@binkert.org    print >>out, '%module params'
5135517Snate@binkert.org
5145517Snate@binkert.org    print >>out, '%{'
5157673Snate@binkert.org    for obj in ordered_objs:
5165517Snate@binkert.org        print >>out, '#include "params/%s.hh"' % obj
5175517Snate@binkert.org    print >>out, '%}'
5187673Snate@binkert.org
5197673Snate@binkert.org    for pd in predecls:
5205517Snate@binkert.org        print >>out, pd
5215798Snate@binkert.org
5225798Snate@binkert.org    enums = list(enums)
5238333Snate@binkert.org    enums.sort()
5247816Ssteve.reinhardt@amd.com    for enum in enums:
5255798Snate@binkert.org        print >>out, '%%include "enums/%s.hh"' % enum.__name__
5265798Snate@binkert.org    print >>out
5274762Snate@binkert.org
5284762Snate@binkert.org    for obj in ordered_objs:
5294762Snate@binkert.org        if obj.swig_objdecls:
5304762Snate@binkert.org            for decl in obj.swig_objdecls:
5314762Snate@binkert.org                print >>out, decl
5328596Ssteve.reinhardt@amd.com            continue
5335517Snate@binkert.org
5345517Snate@binkert.org        class_path = obj.cxx_class.split('::')
5355517Snate@binkert.org        classname = class_path[-1]
5365517Snate@binkert.org        namespaces = class_path[:-1]
5375517Snate@binkert.org        namespaces.reverse()
5387673Snate@binkert.org
5398596Ssteve.reinhardt@amd.com        code = ''
5407673Snate@binkert.org
5415517Snate@binkert.org        if namespaces:
5428596Ssteve.reinhardt@amd.com            code += '// avoid name conflicts\n'
5435517Snate@binkert.org            sep_string = '_COLONS_'
5445517Snate@binkert.org            flat_name = sep_string.join(class_path)
5455517Snate@binkert.org            code += '%%rename(%s) %s;\n' % (flat_name, classname)
5468596Ssteve.reinhardt@amd.com
5475517Snate@binkert.org        code += '// stop swig from creating/wrapping default ctor/dtor\n'
5487673Snate@binkert.org        code += '%%nodefault %s;\n' % classname
5497673Snate@binkert.org        code += 'class %s ' % classname
5507673Snate@binkert.org        if obj._base:
5515517Snate@binkert.org            code += ': public %s' % obj._base.cxx_class
5525517Snate@binkert.org        code += ' {};\n'
5535517Snate@binkert.org
5545517Snate@binkert.org        for ns in namespaces:
5555517Snate@binkert.org            new_code = 'namespace %s {\n' % ns
5565517Snate@binkert.org            new_code += code
5575517Snate@binkert.org            new_code += '}\n'
5587673Snate@binkert.org            code = new_code
5597673Snate@binkert.org
5607673Snate@binkert.org        print >>out, code
5615517Snate@binkert.org
5628596Ssteve.reinhardt@amd.com    print >>out, '%%include "src/sim/sim_object_params.hh"' % obj
5635517Snate@binkert.org    for obj in ordered_objs:
5645517Snate@binkert.org        print >>out, '%%include "params/%s.hh"' % obj
5655517Snate@binkert.org
5665517Snate@binkert.orgparams_file = File('params/params.i')
5675517Snate@binkert.orgnames = sort_list(sim_objects.keys())
5687673Snate@binkert.orgenv.Command(params_file, [ Value(v) for v in names ], buildParams)
5697673Snate@binkert.orgenv.Depends(params_file, params_hh_files + params_i_files + depends)
5707673Snate@binkert.orgSwigSource('m5.objects', params_file)
5715517Snate@binkert.org
5728596Ssteve.reinhardt@amd.com# Build all swig modules
5737675Snate@binkert.orgswig_modules = []
5747675Snate@binkert.orgcc_swig_sources = []
5757675Snate@binkert.orgfor source,package in swig_sources:
5767675Snate@binkert.org    filename = str(source)
5777675Snate@binkert.org    assert filename.endswith('.i')
5787675Snate@binkert.org
5798596Ssteve.reinhardt@amd.com    base = '.'.join(filename.split('.')[:-1])
5807675Snate@binkert.org    module = basename(base)
5817675Snate@binkert.org    cc_file = base + '_wrap.cc'
5828596Ssteve.reinhardt@amd.com    py_file = base + '.py'
5838596Ssteve.reinhardt@amd.com
5848596Ssteve.reinhardt@amd.com    env.Command([cc_file, py_file], source,
5858596Ssteve.reinhardt@amd.com                '$SWIG $SWIGFLAGS -outdir ${TARGETS[1].dir} '
5868596Ssteve.reinhardt@amd.com                '-o ${TARGETS[0]} $SOURCES')
5878596Ssteve.reinhardt@amd.com    env.Depends(py_file, source)
5888596Ssteve.reinhardt@amd.com    env.Depends(cc_file, source)
5898596Ssteve.reinhardt@amd.com
5908596Ssteve.reinhardt@amd.com    swig_modules.append(Value(module))
5914762Snate@binkert.org    cc_swig_sources.append(File(cc_file))
5926143Snate@binkert.org    PySource(package, py_file)
5936143Snate@binkert.org
5946143Snate@binkert.org# Generate the main swig init file
5954762Snate@binkert.orgdef makeSwigInit(target, source, env):
5964762Snate@binkert.org    f = file(str(target[0]), 'w')
5974762Snate@binkert.org    print >>f, 'extern "C" {'
5987756SAli.Saidi@ARM.com    for module in source:
5998596Ssteve.reinhardt@amd.com        print >>f, '    void init_%s();' % module.get_contents()
6004762Snate@binkert.org    print >>f, '}'
6014762Snate@binkert.org    print >>f, 'void initSwig() {'
6028596Ssteve.reinhardt@amd.com    for module in source:
6035463Snate@binkert.org        print >>f, '    init_%s();' % module.get_contents()
6048596Ssteve.reinhardt@amd.com    print >>f, '}'
6058596Ssteve.reinhardt@amd.com    f.close()
6065463Snate@binkert.org
6077756SAli.Saidi@ARM.comenv.Command('python/swig/init.cc', swig_modules, makeSwigInit)
6088596Ssteve.reinhardt@amd.comSource('python/swig/init.cc')
6094762Snate@binkert.org
6107677Snate@binkert.org# Generate traceflags.py
6114762Snate@binkert.orgdef traceFlagsPy(target, source, env):
6124762Snate@binkert.org    assert(len(target) == 1)
6136143Snate@binkert.org
6146143Snate@binkert.org    f = file(str(target[0]), 'w')
6156143Snate@binkert.org
6164762Snate@binkert.org    allFlags = []
6174762Snate@binkert.org    for s in source:
6187756SAli.Saidi@ARM.com        val = eval(s.get_contents())
6197816Ssteve.reinhardt@amd.com        allFlags.append(val)
6204762Snate@binkert.org
6214762Snate@binkert.org    print >>f, 'baseFlags = ['
6224762Snate@binkert.org    for flag, compound, desc in allFlags:
6234762Snate@binkert.org        if not compound:
6247756SAli.Saidi@ARM.com            print >>f, "    '%s'," % flag
6258596Ssteve.reinhardt@amd.com    print >>f, "    ]"
6264762Snate@binkert.org    print >>f
6274762Snate@binkert.org
6287677Snate@binkert.org    print >>f, 'compoundFlags = ['
6297756SAli.Saidi@ARM.com    print >>f, "    'All',"
6308596Ssteve.reinhardt@amd.com    for flag, compound, desc in allFlags:
6317675Snate@binkert.org        if compound:
6327677Snate@binkert.org            print >>f, "    '%s'," % flag
6335517Snate@binkert.org    print >>f, "    ]"
6348596Ssteve.reinhardt@amd.com    print >>f
6357675Snate@binkert.org
6368596Ssteve.reinhardt@amd.com    print >>f, "allFlags = frozenset(baseFlags + compoundFlags)"
6378596Ssteve.reinhardt@amd.com    print >>f
6388596Ssteve.reinhardt@amd.com
6398596Ssteve.reinhardt@amd.com    print >>f, 'compoundFlagMap = {'
6408596Ssteve.reinhardt@amd.com    all = tuple([flag for flag,compound,desc in allFlags if not compound])
6414762Snate@binkert.org    print >>f, "    'All' : %s," % (all, )
6427674Snate@binkert.org    for flag, compound, desc in allFlags:
6437674Snate@binkert.org        if compound:
6447674Snate@binkert.org            print >>f, "    '%s' : %s," % (flag, compound)
6457674Snate@binkert.org    print >>f, "    }"
6467674Snate@binkert.org    print >>f
6477674Snate@binkert.org
6487674Snate@binkert.org    print >>f, 'flagDescriptions = {'
6497674Snate@binkert.org    print >>f, "    'All' : 'All flags',"
6507674Snate@binkert.org    for flag, compound, desc in allFlags:
6517674Snate@binkert.org        print >>f, "    '%s' : '%s'," % (flag, desc)
6527674Snate@binkert.org    print >>f, "    }"
6537674Snate@binkert.org
6547674Snate@binkert.org    f.close()
6557674Snate@binkert.org
6567674Snate@binkert.orgdef traceFlagsCC(target, source, env):
6574762Snate@binkert.org    assert(len(target) == 1)
6586143Snate@binkert.org
6596143Snate@binkert.org    f = file(str(target[0]), 'w')
6607756SAli.Saidi@ARM.com
6617816Ssteve.reinhardt@amd.com    allFlags = []
6628235Snate@binkert.org    for s in source:
6638596Ssteve.reinhardt@amd.com        val = eval(s.get_contents())
6647756SAli.Saidi@ARM.com        allFlags.append(val)
6657816Ssteve.reinhardt@amd.com
6668235Snate@binkert.org    # file header
6674382Sbinkertn@umich.edu    print >>f, '''
6688232Snate@binkert.org/*
6698232Snate@binkert.org * DO NOT EDIT THIS FILE! Automatically generated
6708232Snate@binkert.org */
6718232Snate@binkert.org
6728232Snate@binkert.org#include "base/traceflags.hh"
6736229Snate@binkert.org
6748232Snate@binkert.orgusing namespace Trace;
6758232Snate@binkert.org
6768232Snate@binkert.orgconst char *Trace::flagStrings[] =
6776229Snate@binkert.org{'''
6787673Snate@binkert.org
6795517Snate@binkert.org    # The string array is used by SimpleEnumParam to map the strings
6805517Snate@binkert.org    # provided by the user to enum values.
6817673Snate@binkert.org    for flag, compound, desc in allFlags:
6825517Snate@binkert.org        if not compound:
6835517Snate@binkert.org            print >>f, '    "%s",' % flag
6845517Snate@binkert.org
6855517Snate@binkert.org    print >>f, '    "All",'
6868232Snate@binkert.org    for flag, compound, desc in allFlags:
6877673Snate@binkert.org        if compound:
6887673Snate@binkert.org            print >>f, '    "%s",' % flag
6898232Snate@binkert.org
6908232Snate@binkert.org    print >>f, '};'
6918232Snate@binkert.org    print >>f
6928232Snate@binkert.org    print >>f, 'const int Trace::numFlagStrings = %d;' % (len(allFlags) + 1)
6937673Snate@binkert.org    print >>f
6945517Snate@binkert.org
6958232Snate@binkert.org    #
6968232Snate@binkert.org    # Now define the individual compound flag arrays.  There is an array
6978232Snate@binkert.org    # for each compound flag listing the component base flags.
6988232Snate@binkert.org    #
6997673Snate@binkert.org    all = tuple([flag for flag,compound,desc in allFlags if not compound])
7008232Snate@binkert.org    print >>f, 'static const Flags AllMap[] = {'
7018232Snate@binkert.org    for flag, compound, desc in allFlags:
7028232Snate@binkert.org        if not compound:
7038232Snate@binkert.org            print >>f, "    %s," % flag
7048232Snate@binkert.org    print >>f, '};'
7058232Snate@binkert.org    print >>f
7067673Snate@binkert.org
7075517Snate@binkert.org    for flag, compound, desc in allFlags:
7088232Snate@binkert.org        if not compound:
7098232Snate@binkert.org            continue
7105517Snate@binkert.org        print >>f, 'static const Flags %sMap[] = {' % flag
7117673Snate@binkert.org        for flag in compound:
7125517Snate@binkert.org            print >>f, "    %s," % flag
7138232Snate@binkert.org        print >>f, "    (Flags)-1"
7148232Snate@binkert.org        print >>f, '};'
7155517Snate@binkert.org        print >>f
7168232Snate@binkert.org
7178232Snate@binkert.org    #
7188232Snate@binkert.org    # Finally the compoundFlags[] array maps the compound flags
7197673Snate@binkert.org    # to their individual arrays/
7205517Snate@binkert.org    #
7215517Snate@binkert.org    print >>f, 'const Flags *Trace::compoundFlags[] ='
7227673Snate@binkert.org    print >>f, '{'
7235517Snate@binkert.org    print >>f, '    AllMap,'
7245517Snate@binkert.org    for flag, compound, desc in allFlags:
7255517Snate@binkert.org        if compound:
7268232Snate@binkert.org            print >>f, '    %sMap,' % flag
7275517Snate@binkert.org    # file trailer
7285517Snate@binkert.org    print >>f, '};'
7298232Snate@binkert.org
7308232Snate@binkert.org    f.close()
7315517Snate@binkert.org
7328232Snate@binkert.orgdef traceFlagsHH(target, source, env):
7338232Snate@binkert.org    assert(len(target) == 1)
7345517Snate@binkert.org
7358232Snate@binkert.org    f = file(str(target[0]), 'w')
7368232Snate@binkert.org
7378232Snate@binkert.org    allFlags = []
7385517Snate@binkert.org    for s in source:
7398232Snate@binkert.org        val = eval(s.get_contents())
7408232Snate@binkert.org        allFlags.append(val)
7418232Snate@binkert.org
7428232Snate@binkert.org    # file header boilerplate
7438232Snate@binkert.org    print >>f, '''
7448232Snate@binkert.org/*
7455517Snate@binkert.org * DO NOT EDIT THIS FILE!
7468232Snate@binkert.org *
7478232Snate@binkert.org * Automatically generated from traceflags.py
7485517Snate@binkert.org */
7498232Snate@binkert.org
7507673Snate@binkert.org#ifndef __BASE_TRACE_FLAGS_HH__
7515517Snate@binkert.org#define __BASE_TRACE_FLAGS_HH__
7527673Snate@binkert.org
7535517Snate@binkert.orgnamespace Trace {
7548232Snate@binkert.org
7558232Snate@binkert.orgenum Flags {'''
7568232Snate@binkert.org
7575192Ssaidi@eecs.umich.edu    # Generate the enum.  Base flags come first, then compound flags.
7588232Snate@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
7635192Ssaidi@eecs.umich.edu
7647674Snate@binkert.org    numBaseFlags = idx
7655522Snate@binkert.org    print >>f, '    NumFlags = %d,' % idx
7665522Snate@binkert.org
7677674Snate@binkert.org    # put a comment in here to separate base from compound flags
7687674Snate@binkert.org    print >>f, '''
7697674Snate@binkert.org// The remaining enum values are *not* valid indices for Trace::flags.
7707674Snate@binkert.org// They are "compound" flags, which correspond to sets of base
7717674Snate@binkert.org// flags, and are used by changeFlag.'''
7727674Snate@binkert.org
7737674Snate@binkert.org    print >>f, '    All = %d,' % idx
7747674Snate@binkert.org    idx += 1
7755522Snate@binkert.org    for flag, compound, desc in allFlags:
7765522Snate@binkert.org        if compound:
7775522Snate@binkert.org            print >>f, '    %s = %d,' % (flag, idx)
7785517Snate@binkert.org            idx += 1
7795522Snate@binkert.org
7805517Snate@binkert.org    numCompoundFlags = idx - numBaseFlags
7816143Snate@binkert.org    print >>f, '    NumCompoundFlags = %d' % numCompoundFlags
7826727Ssteve.reinhardt@amd.com
7835522Snate@binkert.org    # trailer boilerplate
7845522Snate@binkert.org    print >>f, '''\
7855522Snate@binkert.org}; // enum Flags
7867674Snate@binkert.org
7875517Snate@binkert.org// Array of strings for SimpleEnumParam
7887673Snate@binkert.orgextern const char *flagStrings[];
7897673Snate@binkert.orgextern const int numFlagStrings;
7907674Snate@binkert.org
7917673Snate@binkert.org// Array of arraay pointers: for each compound flag, gives the list of
7927674Snate@binkert.org// base flags to set.  Inidividual flag arrays are terminated by -1.
7937674Snate@binkert.orgextern const Flags *compoundFlags[];
7947674Snate@binkert.org
7957674Snate@binkert.org/* namespace Trace */ }
7967674Snate@binkert.org
7977674Snate@binkert.org#endif // __BASE_TRACE_FLAGS_HH__
7985522Snate@binkert.org'''
7995522Snate@binkert.org
8007674Snate@binkert.org    f.close()
8017674Snate@binkert.org
8027674Snate@binkert.orgflags = [ Value(f) for f in trace_flags ]
8037674Snate@binkert.orgenv.Command('base/traceflags.py', flags, traceFlagsPy)
8047673Snate@binkert.orgPySource('m5', 'base/traceflags.py')
8057674Snate@binkert.org
8067674Snate@binkert.orgenv.Command('base/traceflags.hh', flags, traceFlagsHH)
8077674Snate@binkert.orgenv.Command('base/traceflags.cc', flags, traceFlagsCC)
8087674Snate@binkert.orgSource('base/traceflags.cc')
8097674Snate@binkert.org
8107674Snate@binkert.org# Generate program_info.cc
8117674Snate@binkert.orgdef programInfo(target, source, env):
8127674Snate@binkert.org    def gen_file(target, rev, node, date):
8137811Ssteve.reinhardt@amd.com        pi_stats = file(target, 'w')
8147674Snate@binkert.org        print >>pi_stats, 'const char *hgRev = "%s:%s";' %  (rev, node)
8157673Snate@binkert.org        print >>pi_stats, 'const char *hgDate = "%s";' % date
8165522Snate@binkert.org        pi_stats.close()
8176143Snate@binkert.org
8187756SAli.Saidi@ARM.com    target = str(target[0])
8197816Ssteve.reinhardt@amd.com    scons_dir = str(source[0].get_contents())
8207674Snate@binkert.org    try:
8214382Sbinkertn@umich.edu        import mercurial.demandimport, mercurial.hg, mercurial.ui
8224382Sbinkertn@umich.edu        import mercurial.util, mercurial.node
8234382Sbinkertn@umich.edu        if not exists(scons_dir) or not isdir(scons_dir) or \
8244382Sbinkertn@umich.edu               not exists(joinpath(scons_dir, ".hg")):
8254382Sbinkertn@umich.edu            raise ValueError
8264382Sbinkertn@umich.edu        repo = mercurial.hg.repository(mercurial.ui.ui(), scons_dir)
8274382Sbinkertn@umich.edu        rev = mercurial.node.nullrev + repo.changelog.count()
8284382Sbinkertn@umich.edu        changenode = repo.changelog.node(rev)
8294382Sbinkertn@umich.edu        changes = repo.changelog.read(changenode)
8304382Sbinkertn@umich.edu        date = mercurial.util.datestr(changes[2])
8316143Snate@binkert.org
832955SN/A        gen_file(target, rev, mercurial.node.hex(changenode), date)
8332655Sstever@eecs.umich.edu
8342655Sstever@eecs.umich.edu        mercurial.demandimport.disable()
8352655Sstever@eecs.umich.edu    except ImportError:
8362655Sstever@eecs.umich.edu        gen_file(target, "Unknown", "Unknown", "Unknown")
8372655Sstever@eecs.umich.edu
8385601Snate@binkert.org    except:
8395601Snate@binkert.org        print "in except"
8408334Snate@binkert.org        gen_file(target, "Unknown", "Unknown", "Unknown")
8418334Snate@binkert.org        mercurial.demandimport.disable()
8428334Snate@binkert.org
8435522Snate@binkert.orgenv.Command('base/program_info.cc',
8445863Snate@binkert.org            Value(str(SCons.Node.FS.default_fs.SConstruct_dir)),
8455601Snate@binkert.org            programInfo)
8465601Snate@binkert.org
8475601Snate@binkert.org# embed python files.  All .py files that have been indicated by a
8485863Snate@binkert.org# PySource() call in a SConscript need to be embedded into the M5
8498945Ssteve.reinhardt@amd.com# library.  To do that, we compile the file to byte code, marshal the
8505559Snate@binkert.org# byte code, compress it, and then generate an assembly file that
8515559Snate@binkert.org# inserts the result into the data section with symbols indicating the
8525559Snate@binkert.org# beginning, and end (and with the size at the end)
8535559Snate@binkert.orgpy_sources_tnodes = {}
8548656Sandreas.hansson@arm.comfor pysource in py_sources:
8558614Sgblack@eecs.umich.edu    py_sources_tnodes[pysource.tnode] = pysource
8568614Sgblack@eecs.umich.edu
8578737Skoansin.tan@gmail.comdef objectifyPyFile(target, source, env):
8588737Skoansin.tan@gmail.com    '''Action function to compile a .py into a code object, marshal
8598737Skoansin.tan@gmail.com    it, compress it, and stick it into an asm file so the code appears
8608945Ssteve.reinhardt@amd.com    as just bytes with a label in the data section'''
8618945Ssteve.reinhardt@amd.com
8628945Ssteve.reinhardt@amd.com    src = file(str(source[0]), 'r').read()
8638945Ssteve.reinhardt@amd.com    dst = file(str(target[0]), 'w')
8646143Snate@binkert.org
8656143Snate@binkert.org    pysource = py_sources_tnodes[source[0]]
8666143Snate@binkert.org    compiled = compile(src, pysource.debugname, 'exec')
8676143Snate@binkert.org    marshalled = marshal.dumps(compiled)
8686143Snate@binkert.org    compressed = zlib.compress(marshalled)
8696143Snate@binkert.org    data = compressed
8706143Snate@binkert.org
8718945Ssteve.reinhardt@amd.com    # Some C/C++ compilers prepend an underscore to global symbol
8728945Ssteve.reinhardt@amd.com    # names, so if they're going to do that, we need to prepend that
8736143Snate@binkert.org    # leading underscore to globals in the assembly file.
8746143Snate@binkert.org    if env['LEADING_UNDERSCORE']:
8756143Snate@binkert.org        sym = '_' + pysource.symname
8766143Snate@binkert.org    else:
8776143Snate@binkert.org        sym = pysource.symname
8786143Snate@binkert.org
8796143Snate@binkert.org    step = 16
8806143Snate@binkert.org    print >>dst, ".data"
8816143Snate@binkert.org    print >>dst, ".globl %s_beg" % sym
8826143Snate@binkert.org    print >>dst, ".globl %s_end" % sym
8836143Snate@binkert.org    print >>dst, "%s_beg:" % sym
8846143Snate@binkert.org    for i in xrange(0, len(data), step):
8856143Snate@binkert.org        x = array.array('B', data[i:i+step])
8868594Snate@binkert.org        print >>dst, ".byte", ','.join([str(d) for d in x])
8878594Snate@binkert.org    print >>dst, "%s_end:" % sym
8888594Snate@binkert.org    print >>dst, ".long %d" % len(marshalled)
8898594Snate@binkert.org
8906143Snate@binkert.orgfor source in py_sources:
8916143Snate@binkert.org    env.Command(source.assembly, source.tnode, objectifyPyFile)
8926143Snate@binkert.org    Source(source.assembly)
8936143Snate@binkert.org
8946143Snate@binkert.org# Generate init_python.cc which creates a bunch of EmbeddedPyModule
8956240Snate@binkert.org# structs that describe the embedded python code.  One such struct
8965554Snate@binkert.org# contains information about the importer that python uses to get at
8975522Snate@binkert.org# the embedded files, and then there's a list of all of the rest that
8985522Snate@binkert.org# the importer uses to load the rest on demand.
8995797Snate@binkert.orgpy_sources_symbols = {}
9005797Snate@binkert.orgfor pysource in py_sources:
9015522Snate@binkert.org    py_sources_symbols[pysource.symname] = pysource
9025601Snate@binkert.orgdef pythonInit(target, source, env):
9038233Snate@binkert.org    dst = file(str(target[0]), 'w')
9048233Snate@binkert.org
9058235Snate@binkert.org    def dump_mod(sym, endchar=','):
9068235Snate@binkert.org        pysource = py_sources_symbols[sym]
9078235Snate@binkert.org        print >>dst, '    { "%s",' % pysource.arcname
9088235Snate@binkert.org        print >>dst, '      "%s",' % pysource.modpath
9098235Snate@binkert.org        print >>dst, '       %s_beg, %s_end,' % (sym, sym)
9108942Sgblack@eecs.umich.edu        print >>dst, '       %s_end - %s_beg,' % (sym, sym)
9118235Snate@binkert.org        print >>dst, '       *(int *)%s_end }%s'  % (sym, endchar)
9126143Snate@binkert.org    
9132655Sstever@eecs.umich.edu    print >>dst, '#include "sim/init.hh"'
9146143Snate@binkert.org
9156143Snate@binkert.org    for sym in source:
9168233Snate@binkert.org        sym = sym.get_contents()
9176143Snate@binkert.org        print >>dst, "extern const char %s_beg[], %s_end[];" % (sym, sym)
9186143Snate@binkert.org
9194007Ssaidi@eecs.umich.edu    print >>dst, "const EmbeddedPyModule embeddedPyImporter = "
9204596Sbinkertn@umich.edu    dump_mod("PyEMB_importer", endchar=';');
9214007Ssaidi@eecs.umich.edu    print >>dst
9224596Sbinkertn@umich.edu
9237756SAli.Saidi@ARM.com    print >>dst, "const EmbeddedPyModule embeddedPyModules[] = {"
9247816Ssteve.reinhardt@amd.com    for i,sym in enumerate(source):
9258334Snate@binkert.org        sym = sym.get_contents()
9268334Snate@binkert.org        if sym == "PyEMB_importer":
9278334Snate@binkert.org            # Skip the importer since we've already exported it
9288334Snate@binkert.org            continue
9295601Snate@binkert.org        dump_mod(sym)
9305601Snate@binkert.org    print >>dst, "    { 0, 0, 0, 0, 0, 0 }"
9312655Sstever@eecs.umich.edu    print >>dst, "};"
932955SN/A
9333918Ssaidi@eecs.umich.edusymbols = [Value(s.symname) for s in py_sources]
9348737Skoansin.tan@gmail.comenv.Command('sim/init_python.cc', symbols, pythonInit)
9353918Ssaidi@eecs.umich.eduSource('sim/init_python.cc')
9363918Ssaidi@eecs.umich.edu
9373918Ssaidi@eecs.umich.edu########################################################################
9383918Ssaidi@eecs.umich.edu#
9393918Ssaidi@eecs.umich.edu# Define binaries.  Each different build type (debug, opt, etc.) gets
9403918Ssaidi@eecs.umich.edu# a slightly different build environment.
9413918Ssaidi@eecs.umich.edu#
9423918Ssaidi@eecs.umich.edu
9433918Ssaidi@eecs.umich.edu# List of constructed environments to pass back to SConstruct
9443918Ssaidi@eecs.umich.eduenvList = []
9453918Ssaidi@eecs.umich.edu
9463918Ssaidi@eecs.umich.edu# This function adds the specified sources to the given build
9473940Ssaidi@eecs.umich.edu# environment, and returns a list of all the corresponding SCons
9483940Ssaidi@eecs.umich.edu# Object nodes (including an extra one for date.cc).  We explicitly
9493940Ssaidi@eecs.umich.edu# add the Object nodes so we can set up special dependencies for
9503942Ssaidi@eecs.umich.edu# date.cc.
9513940Ssaidi@eecs.umich.edudef make_objs(sources, env, static):
9523515Ssaidi@eecs.umich.edu    if static:
9533918Ssaidi@eecs.umich.edu        XObject = env.StaticObject
9544762Snate@binkert.org    else:
9553515Ssaidi@eecs.umich.edu        XObject = env.SharedObject
9568881Smarc.orr@gmail.com
9578881Smarc.orr@gmail.com    objs = [ XObject(s) for s in sources ]
9588881Smarc.orr@gmail.com  
9598881Smarc.orr@gmail.com    # make date.cc depend on all other objects so it always gets
9608881Smarc.orr@gmail.com    # recompiled whenever anything else does
9618881Smarc.orr@gmail.com    date_obj = XObject('base/date.cc')
9628881Smarc.orr@gmail.com
9638881Smarc.orr@gmail.com    # Make the generation of program_info.cc dependend on all 
9648881Smarc.orr@gmail.com    # the other cc files and the compiling of program_info.cc 
9658881Smarc.orr@gmail.com    # dependent on all the objects but program_info.o 
9668881Smarc.orr@gmail.com    pinfo_obj = XObject('base/program_info.cc')
9678881Smarc.orr@gmail.com    env.Depends('base/program_info.cc', sources)
9688881Smarc.orr@gmail.com    env.Depends(date_obj, objs)
9698881Smarc.orr@gmail.com    env.Depends(pinfo_obj, objs)
9708881Smarc.orr@gmail.com    objs.extend([date_obj, pinfo_obj])
9718881Smarc.orr@gmail.com    return objs
9728881Smarc.orr@gmail.com
9738881Smarc.orr@gmail.com# Function to create a new build environment as clone of current
9748881Smarc.orr@gmail.com# environment 'env' with modified object suffix and optional stripped
9758881Smarc.orr@gmail.com# binary.  Additional keyword arguments are appended to corresponding
9768881Smarc.orr@gmail.com# build environment vars.
9778881Smarc.orr@gmail.comdef makeEnv(label, objsfx, strip = False, **kwargs):
9788881Smarc.orr@gmail.com    # SCons doesn't know to append a library suffix when there is a '.' in the
9798881Smarc.orr@gmail.com    # name.  Use '_' instead.
9808881Smarc.orr@gmail.com    libname = 'm5_' + label
9818881Smarc.orr@gmail.com    exename = 'm5.' + label
9828881Smarc.orr@gmail.com
9838881Smarc.orr@gmail.com    new_env = env.Copy(OBJSUFFIX=objsfx, SHOBJSUFFIX=objsfx + 's')
984955SN/A    new_env.Label = label
985955SN/A    new_env.Append(**kwargs)
9868881Smarc.orr@gmail.com
9878881Smarc.orr@gmail.com    swig_env = new_env.Copy()
9888881Smarc.orr@gmail.com    if env['GCC']:
9898881Smarc.orr@gmail.com        swig_env.Append(CCFLAGS='-Wno-uninitialized')
990955SN/A        swig_env.Append(CCFLAGS='-Wno-sign-compare')
991955SN/A        swig_env.Append(CCFLAGS='-Wno-parentheses')
9928881Smarc.orr@gmail.com
9938881Smarc.orr@gmail.com    static_objs = make_objs(cc_lib_sources, new_env, static=True)
9948881Smarc.orr@gmail.com    shared_objs = make_objs(cc_lib_sources, new_env, static=False)
9958881Smarc.orr@gmail.com    static_objs += [ swig_env.StaticObject(s) for s in cc_swig_sources ]
996955SN/A    shared_objs += [ swig_env.SharedObject(s) for s in cc_swig_sources ]
997955SN/A
9988881Smarc.orr@gmail.com    # First make a library of everything but main() so other programs can
9998881Smarc.orr@gmail.com    # link against m5.
10008881Smarc.orr@gmail.com    static_lib = new_env.StaticLibrary(libname, static_objs + static_objs)
10018881Smarc.orr@gmail.com    shared_lib = new_env.SharedLibrary(libname, shared_objs + shared_objs)
10028881Smarc.orr@gmail.com
10031869SN/A    for target, sources in unit_tests:
10041869SN/A        objs = [ new_env.StaticObject(s) for s in sources ]
1005        new_env.Program("unittest/%s.%s" % (target, label), objs + static_lib)
1006
1007    # Now link a stub with main() and the static library.
1008    objects = [new_env.Object(s) for s in cc_bin_sources] + static_lib
1009    if strip:
1010        unstripped_exe = exename + '.unstripped'
1011        new_env.Program(unstripped_exe, objects)
1012        if sys.platform == 'sunos5':
1013            cmd = 'cp $SOURCE $TARGET; strip $TARGET'
1014        else:
1015            cmd = 'strip $SOURCE -o $TARGET'
1016        targets = new_env.Command(exename, unstripped_exe, cmd)
1017    else:
1018        targets = new_env.Program(exename, objects)
1019            
1020    new_env.M5Binary = targets[0]
1021    envList.append(new_env)
1022
1023# Debug binary
1024ccflags = {}
1025if env['GCC']:
1026    if sys.platform == 'sunos5':
1027        ccflags['debug'] = '-gstabs+'
1028    else:
1029        ccflags['debug'] = '-ggdb3'
1030    ccflags['opt'] = '-g -O3'
1031    ccflags['fast'] = '-O3'
1032    ccflags['prof'] = '-O3 -g -pg'
1033elif env['SUNCC']:
1034    ccflags['debug'] = '-g0'
1035    ccflags['opt'] = '-g -O'
1036    ccflags['fast'] = '-fast'
1037    ccflags['prof'] = '-fast -g -pg'
1038elif env['ICC']:
1039    ccflags['debug'] = '-g -O0'
1040    ccflags['opt'] = '-g -O'
1041    ccflags['fast'] = '-fast'
1042    ccflags['prof'] = '-fast -g -pg'
1043else:
1044    print 'Unknown compiler, please fix compiler options'
1045    Exit(1)
1046
1047makeEnv('debug', '.do',
1048        CCFLAGS = Split(ccflags['debug']),
1049        CPPDEFINES = ['DEBUG', 'TRACING_ON=1'])
1050
1051# Optimized binary
1052makeEnv('opt', '.o',
1053        CCFLAGS = Split(ccflags['opt']),
1054        CPPDEFINES = ['TRACING_ON=1'])
1055
1056# "Fast" binary
1057makeEnv('fast', '.fo', strip = True,
1058        CCFLAGS = Split(ccflags['fast']),
1059        CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'])
1060
1061# Profiled binary
1062makeEnv('prof', '.po',
1063        CCFLAGS = Split(ccflags['prof']),
1064        CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1065        LINKFLAGS = '-pg')
1066
1067Return('envList')
1068