SConscript revision 5798
112837Sgabeblack@google.com# -*- mode:python -*-
212837Sgabeblack@google.com
312837Sgabeblack@google.com# Copyright (c) 2004-2005 The Regents of The University of Michigan
412837Sgabeblack@google.com# All rights reserved.
512837Sgabeblack@google.com#
612837Sgabeblack@google.com# Redistribution and use in source and binary forms, with or without
712837Sgabeblack@google.com# modification, are permitted provided that the following conditions are
812837Sgabeblack@google.com# met: redistributions of source code must retain the above copyright
912837Sgabeblack@google.com# notice, this list of conditions and the following disclaimer;
1012837Sgabeblack@google.com# redistributions in binary form must reproduce the above copyright
1112837Sgabeblack@google.com# notice, this list of conditions and the following disclaimer in the
1212837Sgabeblack@google.com# documentation and/or other materials provided with the distribution;
1312837Sgabeblack@google.com# neither the name of the copyright holders nor the names of its
1412837Sgabeblack@google.com# contributors may be used to endorse or promote products derived from
1512837Sgabeblack@google.com# this software without specific prior written permission.
1612837Sgabeblack@google.com#
1712837Sgabeblack@google.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1812837Sgabeblack@google.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1912837Sgabeblack@google.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2012837Sgabeblack@google.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2112837Sgabeblack@google.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2212837Sgabeblack@google.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2312837Sgabeblack@google.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2412837Sgabeblack@google.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2512837Sgabeblack@google.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2612837Sgabeblack@google.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2712837Sgabeblack@google.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2812837Sgabeblack@google.com#
2912837Sgabeblack@google.com# Authors: Nathan Binkert
3013039Sgabeblack@google.com
3113039Sgabeblack@google.comimport array
3212837Sgabeblack@google.comimport imp
3312989Sgabeblack@google.comimport marshal
3412986Sgabeblack@google.comimport os
3513039Sgabeblack@google.comimport re
3612837Sgabeblack@google.comimport sys
3712837Sgabeblack@google.comimport zlib
3812837Sgabeblack@google.com
3912837Sgabeblack@google.comfrom os.path import basename, dirname, exists, isdir, isfile, join as joinpath
4012837Sgabeblack@google.com
4112983Sgabeblack@google.comimport SCons
4212837Sgabeblack@google.com
4312983Sgabeblack@google.com# This file defines how to build a particular configuration of M5
4412983Sgabeblack@google.com# based on variable settings in the 'env' build environment.
4512983Sgabeblack@google.com
4612983Sgabeblack@google.comImport('*')
4712983Sgabeblack@google.com
4812983Sgabeblack@google.com# Children need to see the environment
4912983Sgabeblack@google.comExport('env')
5012983Sgabeblack@google.com
5112983Sgabeblack@google.combuild_env = dict([(opt, env[opt]) for opt in env.ExportOptions])
5212983Sgabeblack@google.com
5312983Sgabeblack@google.comdef sort_list(_list):
5412983Sgabeblack@google.com    """return a sorted copy of '_list'"""
5512983Sgabeblack@google.com    if isinstance(_list, list):
5612983Sgabeblack@google.com        _list = _list[:]
5712983Sgabeblack@google.com    else:
5812983Sgabeblack@google.com        _list = list(_list)
5912983Sgabeblack@google.com    _list.sort()
6012983Sgabeblack@google.com    return _list
6112983Sgabeblack@google.com
6213039Sgabeblack@google.comclass PySourceFile(object):
6313039Sgabeblack@google.com    invalid_sym_char = re.compile('[^A-z0-9_]')
6413039Sgabeblack@google.com    def __init__(self, package, tnode):
6513039Sgabeblack@google.com        snode = tnode.srcnode()
6613039Sgabeblack@google.com        filename = str(tnode)
6713039Sgabeblack@google.com        pyname = basename(filename)
6813039Sgabeblack@google.com        assert pyname.endswith('.py')
6913039Sgabeblack@google.com        name = pyname[:-3]
7013039Sgabeblack@google.com        if package:
7113039Sgabeblack@google.com            path = package.split('.')
7213039Sgabeblack@google.com        else:
7313039Sgabeblack@google.com            path = []
7413039Sgabeblack@google.com
7513039Sgabeblack@google.com        modpath = path[:]
7613039Sgabeblack@google.com        if name != '__init__':
7712986Sgabeblack@google.com            modpath += [name]
7813039Sgabeblack@google.com        modpath = '.'.join(modpath)
7912986Sgabeblack@google.com
8013039Sgabeblack@google.com        arcpath = path + [ pyname ]
8113039Sgabeblack@google.com        arcname = joinpath(*arcpath)
8213039Sgabeblack@google.com
8313039Sgabeblack@google.com        debugname = snode.abspath
8413039Sgabeblack@google.com        if not exists(debugname):
8513039Sgabeblack@google.com            debugname = tnode.abspath
8613039Sgabeblack@google.com
8712986Sgabeblack@google.com        self.tnode = tnode
8813039Sgabeblack@google.com        self.snode = snode
8913039Sgabeblack@google.com        self.pyname = pyname
9013039Sgabeblack@google.com        self.package = package
9112986Sgabeblack@google.com        self.modpath = modpath
9212986Sgabeblack@google.com        self.arcname = arcname
9312986Sgabeblack@google.com        self.debugname = debugname
9413039Sgabeblack@google.com        self.compiled = File(filename + 'c')
9513039Sgabeblack@google.com        self.assembly = File(filename + '.s')
9613039Sgabeblack@google.com        self.symname = "PyEMB_" + self.invalid_sym_char.sub('_', modpath)
9713039Sgabeblack@google.com        
9812986Sgabeblack@google.com
9912986Sgabeblack@google.com########################################################################
10013039Sgabeblack@google.com# Code for adding source files of various types
10113039Sgabeblack@google.com#
10213039Sgabeblack@google.comcc_lib_sources = []
10313039Sgabeblack@google.comdef Source(source):
10413039Sgabeblack@google.com    '''Add a source file to the libm5 build'''
10513039Sgabeblack@google.com    if not isinstance(source, SCons.Node.FS.File):
10613039Sgabeblack@google.com        source = File(source)
10713039Sgabeblack@google.com
10813039Sgabeblack@google.com    cc_lib_sources.append(source)
10913039Sgabeblack@google.com
11013039Sgabeblack@google.comcc_bin_sources = []
11113039Sgabeblack@google.comdef BinSource(source):
11213039Sgabeblack@google.com    '''Add a source file to the m5 binary build'''
11313039Sgabeblack@google.com    if not isinstance(source, SCons.Node.FS.File):
11413039Sgabeblack@google.com        source = File(source)
11513039Sgabeblack@google.com
11613039Sgabeblack@google.com    cc_bin_sources.append(source)
11713039Sgabeblack@google.com
11813039Sgabeblack@google.compy_sources = []
11913039Sgabeblack@google.comdef PySource(package, source):
12013039Sgabeblack@google.com    '''Add a python source file to the named package'''
12113039Sgabeblack@google.com    if not isinstance(source, SCons.Node.FS.File):
12213039Sgabeblack@google.com        source = File(source)
12313039Sgabeblack@google.com
12413039Sgabeblack@google.com    source = PySourceFile(package, source)
12513039Sgabeblack@google.com    py_sources.append(source)
12613039Sgabeblack@google.com
12713039Sgabeblack@google.comsim_objects_fixed = False
12813039Sgabeblack@google.comsim_object_modfiles = set()
12913039Sgabeblack@google.comdef SimObject(source):
13013039Sgabeblack@google.com    '''Add a SimObject python file as a python source object and add
13113039Sgabeblack@google.com    it to a list of sim object modules'''
13213039Sgabeblack@google.com
13313039Sgabeblack@google.com    if sim_objects_fixed:
13413039Sgabeblack@google.com        raise AttributeError, "Too late to call SimObject now."
13513039Sgabeblack@google.com
13613039Sgabeblack@google.com    if not isinstance(source, SCons.Node.FS.File):
13712983Sgabeblack@google.com        source = File(source)
13812983Sgabeblack@google.com
13912983Sgabeblack@google.com    PySource('m5.objects', source)
14012983Sgabeblack@google.com    modfile = basename(str(source))
14112983Sgabeblack@google.com    assert modfile.endswith('.py')
14212983Sgabeblack@google.com    modname = modfile[:-3]
14312983Sgabeblack@google.com    sim_object_modfiles.add(modname)
14413039Sgabeblack@google.com
14513039Sgabeblack@google.comswig_sources = []
14612837Sgabeblack@google.comdef SwigSource(package, source):
14712837Sgabeblack@google.com    '''Add a swig file to build'''
14812983Sgabeblack@google.com    if not isinstance(source, SCons.Node.FS.File):
14912837Sgabeblack@google.com        source = File(source)
15012983Sgabeblack@google.com    val = source,package
15112837Sgabeblack@google.com    swig_sources.append(val)
15212837Sgabeblack@google.com
15312925Sgabeblack@google.comunit_tests = []
15412925Sgabeblack@google.comdef UnitTest(target, sources):
15512925Sgabeblack@google.com    if not isinstance(sources, (list, tuple)):
15612925Sgabeblack@google.com        sources = [ sources ]
15712925Sgabeblack@google.com    
15812925Sgabeblack@google.com    srcs = []
15912925Sgabeblack@google.com    for source in sources:
16012925Sgabeblack@google.com        if not isinstance(source, SCons.Node.FS.File):
16112925Sgabeblack@google.com            source = File(source)
16212925Sgabeblack@google.com        srcs.append(source)
16312837Sgabeblack@google.com            
16412983Sgabeblack@google.com    unit_tests.append((target, srcs))
16512837Sgabeblack@google.com
16612983Sgabeblack@google.com# Children should have access
16712837Sgabeblack@google.comExport('Source')
16812837Sgabeblack@google.comExport('BinSource')
16912837Sgabeblack@google.comExport('PySource')
17012837Sgabeblack@google.comExport('SimObject')
17112837Sgabeblack@google.comExport('SwigSource')
17212837Sgabeblack@google.comExport('UnitTest')
17312983Sgabeblack@google.com
17412837Sgabeblack@google.com########################################################################
17512837Sgabeblack@google.com#
17612837Sgabeblack@google.com# Trace Flags
17712837Sgabeblack@google.com#
17812837Sgabeblack@google.comall_flags = {}
17912837Sgabeblack@google.comtrace_flags = []
18012837Sgabeblack@google.comdef TraceFlag(name, desc=''):
18112837Sgabeblack@google.com    if name in all_flags:
18212837Sgabeblack@google.com        raise AttributeError, "Flag %s already specified" % name
18312837Sgabeblack@google.com    flag = (name, (), desc)
18412837Sgabeblack@google.com    trace_flags.append(flag)
18512837Sgabeblack@google.com    all_flags[name] = ()
18612837Sgabeblack@google.com
18712837Sgabeblack@google.comdef CompoundFlag(name, flags, desc=''):
18812837Sgabeblack@google.com    if name in all_flags:
18912837Sgabeblack@google.com        raise AttributeError, "Flag %s already specified" % name
19012837Sgabeblack@google.com
19112837Sgabeblack@google.com    compound = tuple(flags)
19212837Sgabeblack@google.com    for flag in compound:
19312837Sgabeblack@google.com        if flag not in all_flags:
19412837Sgabeblack@google.com            raise AttributeError, "Trace flag %s not found" % flag
19512837Sgabeblack@google.com        if all_flags[flag]:
19612837Sgabeblack@google.com            raise AttributeError, \
19712983Sgabeblack@google.com                "Compound flag can't point to another compound flag"
19812837Sgabeblack@google.com
19912983Sgabeblack@google.com    flag = (name, compound, desc)
20012837Sgabeblack@google.com    trace_flags.append(flag)
20112837Sgabeblack@google.com    all_flags[name] = compound
20212837Sgabeblack@google.com
20312983Sgabeblack@google.comExport('TraceFlag')
20412837Sgabeblack@google.comExport('CompoundFlag')
20512983Sgabeblack@google.com
20612837Sgabeblack@google.com########################################################################
20712837Sgabeblack@google.com#
20812837Sgabeblack@google.com# Set some compiler variables
20912983Sgabeblack@google.com#
21012837Sgabeblack@google.com
21112983Sgabeblack@google.com# Include file paths are rooted in this directory.  SCons will
21212837Sgabeblack@google.com# automatically expand '.' to refer to both the source directory and
21312837Sgabeblack@google.com# the corresponding build directory to pick up generated include
21412837Sgabeblack@google.com# files.
21512983Sgabeblack@google.comenv.Append(CPPPATH=Dir('.'))
21612837Sgabeblack@google.com
21712983Sgabeblack@google.comfor extra_dir in extras_dir_list:
21812837Sgabeblack@google.com    env.Append(CPPPATH=Dir(extra_dir))
21912837Sgabeblack@google.com
22012837Sgabeblack@google.com# Add a flag defining what THE_ISA should be for all compilation
22112983Sgabeblack@google.comenv.Append(CPPDEFINES=[('THE_ISA','%s_ISA' % env['TARGET_ISA'].upper())])
22212837Sgabeblack@google.com
22312983Sgabeblack@google.com# Workaround for bug in SCons version > 0.97d20071212
22412837Sgabeblack@google.com# Scons bug id: 2006 M5 Bug id: 308 
22512837Sgabeblack@google.comfor root, dirs, files in os.walk(base_dir, topdown=True):
22612837Sgabeblack@google.com    Dir(root[len(base_dir) + 1:])
22712983Sgabeblack@google.com
22812837Sgabeblack@google.com########################################################################
22912983Sgabeblack@google.com#
23012837Sgabeblack@google.com# Walk the tree and execute all SConscripts in subdirectories
23112837Sgabeblack@google.com#
23212837Sgabeblack@google.com
23312983Sgabeblack@google.comhere = Dir('.').srcnode().abspath
23412837Sgabeblack@google.comfor root, dirs, files in os.walk(base_dir, topdown=True):
23512983Sgabeblack@google.com    if root == here:
23612837Sgabeblack@google.com        # we don't want to recurse back into this SConscript
23712837Sgabeblack@google.com        continue
23812837Sgabeblack@google.com
23912837Sgabeblack@google.com    if 'SConscript' in files:
24012983Sgabeblack@google.com        build_dir = joinpath(env['BUILDDIR'], root[len(base_dir) + 1:])
24112837Sgabeblack@google.com        SConscript(joinpath(root, 'SConscript'), build_dir=build_dir)
24212983Sgabeblack@google.com
24312837Sgabeblack@google.comfor extra_dir in extras_dir_list:
24412837Sgabeblack@google.com    prefix_len = len(dirname(extra_dir)) + 1
24512837Sgabeblack@google.com    for root, dirs, files in os.walk(extra_dir, topdown=True):
24612837Sgabeblack@google.com        if 'SConscript' in files:
24712837Sgabeblack@google.com            build_dir = joinpath(env['BUILDDIR'], root[prefix_len:])
24812837Sgabeblack@google.com            SConscript(joinpath(root, 'SConscript'), build_dir=build_dir)
24912837Sgabeblack@google.com
25012837Sgabeblack@google.comfor opt in env.ExportOptions:
25112837Sgabeblack@google.com    env.ConfigFile(opt)
25212837Sgabeblack@google.com
25312837Sgabeblack@google.com########################################################################
25412837Sgabeblack@google.com#
25512837Sgabeblack@google.com# Prevent any SimObjects from being added after this point, they
25612837Sgabeblack@google.com# should all have been added in the SConscripts above
25712837Sgabeblack@google.com#
25812837Sgabeblack@google.comclass DictImporter(object):
25912837Sgabeblack@google.com    '''This importer takes a dictionary of arbitrary module names that
26012837Sgabeblack@google.com    map to arbitrary filenames.'''
26112983Sgabeblack@google.com    def __init__(self, modules):
26212837Sgabeblack@google.com        self.modules = modules
26312983Sgabeblack@google.com        self.installed = set()
26412983Sgabeblack@google.com
26512983Sgabeblack@google.com    def __del__(self):
26612983Sgabeblack@google.com        self.unload()
26712983Sgabeblack@google.com
26812983Sgabeblack@google.com    def unload(self):
26912983Sgabeblack@google.com        import sys
27012983Sgabeblack@google.com        for module in self.installed:
27112983Sgabeblack@google.com            del sys.modules[module]
27212983Sgabeblack@google.com        self.installed = set()
27312983Sgabeblack@google.com
27412983Sgabeblack@google.com    def find_module(self, fullname, path):
27512983Sgabeblack@google.com        if fullname == 'defines':
27612837Sgabeblack@google.com            return self
27712837Sgabeblack@google.com
27812923Sgabeblack@google.com        if fullname == 'm5.objects':
27912983Sgabeblack@google.com            return self
28012923Sgabeblack@google.com
28112983Sgabeblack@google.com        if fullname.startswith('m5.internal'):
28212983Sgabeblack@google.com            return None
28312983Sgabeblack@google.com
28412923Sgabeblack@google.com        if fullname in self.modules and exists(self.modules[fullname]):
28512923Sgabeblack@google.com            return self
28612923Sgabeblack@google.com
28712923Sgabeblack@google.com        return None
28812923Sgabeblack@google.com
28912923Sgabeblack@google.com    def load_module(self, fullname):
29012923Sgabeblack@google.com        mod = imp.new_module(fullname)
29112923Sgabeblack@google.com        sys.modules[fullname] = mod
29212923Sgabeblack@google.com        self.installed.add(fullname)
29312923Sgabeblack@google.com
29412923Sgabeblack@google.com        mod.__loader__ = self
29512923Sgabeblack@google.com        if fullname == 'm5.objects':
29612923Sgabeblack@google.com            mod.__path__ = fullname.split('.')
29712923Sgabeblack@google.com            return mod
29812923Sgabeblack@google.com
29912923Sgabeblack@google.com        if fullname == 'defines':
30012837Sgabeblack@google.com            mod.__dict__['buildEnv'] = build_env
30112989Sgabeblack@google.com            return mod
30212837Sgabeblack@google.com
30312989Sgabeblack@google.com        srcfile = self.modules[fullname]
30412837Sgabeblack@google.com        if basename(srcfile) == '__init__.py':
30512837Sgabeblack@google.com            mod.__path__ = fullname.split('.')
30612837Sgabeblack@google.com        mod.__file__ = srcfile
30712989Sgabeblack@google.com
30812837Sgabeblack@google.com        exec file(srcfile, 'r') in mod.__dict__
30912989Sgabeblack@google.com
31012837Sgabeblack@google.com        return mod
31112837Sgabeblack@google.com
31212837Sgabeblack@google.compy_modules = {}
31312837Sgabeblack@google.comfor source in py_sources:
31412837Sgabeblack@google.com    py_modules[source.modpath] = source.snode.abspath
31512837Sgabeblack@google.com
31612837Sgabeblack@google.com# install the python importer so we can grab stuff from the source
31712837Sgabeblack@google.com# tree itself.  We can't have SimObjects added after this point or
31812837Sgabeblack@google.com# else we won't know about them for the rest of the stuff.
31912837Sgabeblack@google.comsim_objects_fixed = True
32012837Sgabeblack@google.comimporter = DictImporter(py_modules)
32112837Sgabeblack@google.comsys.meta_path[0:0] = [ importer ]
32212837Sgabeblack@google.com
32312837Sgabeblack@google.comimport m5
32412837Sgabeblack@google.com
32512837Sgabeblack@google.com# import all sim objects so we can populate the all_objects list
32612837Sgabeblack@google.com# make sure that we're working with a list, then let's sort it
32712837Sgabeblack@google.comsim_objects = list(sim_object_modfiles)
32812837Sgabeblack@google.comsim_objects.sort()
32912837Sgabeblack@google.comfor simobj in sim_objects:
33012837Sgabeblack@google.com    exec('from m5.objects import %s' % simobj)
33112837Sgabeblack@google.com
33212837Sgabeblack@google.com# we need to unload all of the currently imported modules so that they
33312837Sgabeblack@google.com# will be re-imported the next time the sconscript is run
33412837Sgabeblack@google.comimporter.unload()
33512837Sgabeblack@google.comsys.meta_path.remove(importer)
33612837Sgabeblack@google.com
33712837Sgabeblack@google.comsim_objects = m5.SimObject.allClasses
33812837Sgabeblack@google.comall_enums = m5.params.allEnums
33912837Sgabeblack@google.com
34012837Sgabeblack@google.comall_params = {}
34112983Sgabeblack@google.comfor name,obj in sim_objects.iteritems():
34212837Sgabeblack@google.com    for param in obj._params.local.values():
34312983Sgabeblack@google.com        if not hasattr(param, 'swig_decl'):
34412837Sgabeblack@google.com            continue
34512837Sgabeblack@google.com        pname = param.ptype_str
34612837Sgabeblack@google.com        if pname not in all_params:
34712837Sgabeblack@google.com            all_params[pname] = param
34812837Sgabeblack@google.com
34912837Sgabeblack@google.com########################################################################
35012837Sgabeblack@google.com#
35112837Sgabeblack@google.com# calculate extra dependencies
35212837Sgabeblack@google.com#
35312837Sgabeblack@google.commodule_depends = ["m5", "m5.SimObject", "m5.params"]
35412837Sgabeblack@google.comdepends = [ File(py_modules[dep]) for dep in module_depends ]
35512837Sgabeblack@google.com
35612837Sgabeblack@google.com########################################################################
35712837Sgabeblack@google.com#
35812837Sgabeblack@google.com# Commands for the basic automatically generated python files
35912837Sgabeblack@google.com#
36012837Sgabeblack@google.com
36112837Sgabeblack@google.comscons_dir = str(SCons.Node.FS.default_fs.SConstruct_dir)
36212837Sgabeblack@google.com
36312837Sgabeblack@google.comhg_info = ("Unknown", "Unknown", "Unknown")
36412837Sgabeblack@google.comhg_demandimport = False
36512989Sgabeblack@google.comtry:
36612989Sgabeblack@google.com    if not exists(scons_dir) or not isdir(scons_dir) or \
36712837Sgabeblack@google.com           not exists(joinpath(scons_dir, ".hg")):
36812837Sgabeblack@google.com        raise ValueError(".hg directory not found")
36912916Sgabeblack@google.com
37012916Sgabeblack@google.com    import mercurial.demandimport, mercurial.hg, mercurial.ui
37112916Sgabeblack@google.com    import mercurial.util, mercurial.node
37212916Sgabeblack@google.com    hg_demandimport = True
37312916Sgabeblack@google.com
37412916Sgabeblack@google.com    repo = mercurial.hg.repository(mercurial.ui.ui(), scons_dir)
37512916Sgabeblack@google.com    rev = mercurial.node.nullrev + repo.changelog.count()
37612916Sgabeblack@google.com    changenode = repo.changelog.node(rev)
37712916Sgabeblack@google.com    changes = repo.changelog.read(changenode)
37812916Sgabeblack@google.com    id = mercurial.node.hex(changenode)
37912916Sgabeblack@google.com    date = mercurial.util.datestr(changes[2])
38012916Sgabeblack@google.com
38112916Sgabeblack@google.com    hg_info = (rev, id, date)
38212927Sgabeblack@google.comexcept ImportError, e:
38312927Sgabeblack@google.com    print "Mercurial not found"
38412927Sgabeblack@google.comexcept ValueError, e:
38512927Sgabeblack@google.com    print e
38612927Sgabeblack@google.comexcept Exception, e:
38712927Sgabeblack@google.com    print "Other mercurial exception: %s" % e
38812927Sgabeblack@google.com
38912927Sgabeblack@google.comif hg_demandimport:
39012927Sgabeblack@google.com    mercurial.demandimport.disable()
39112927Sgabeblack@google.com
39212927Sgabeblack@google.com# Generate Python file containing a dict specifying the current
39312927Sgabeblack@google.com# build_env flags.
39412927Sgabeblack@google.comdef makeDefinesPyFile(target, source, env):
39512927Sgabeblack@google.com    f = file(str(target[0]), 'w')
39612927Sgabeblack@google.com    build_env, hg_info = [ x.get_contents() for x in source ]
39712927Sgabeblack@google.com    print >>f, "buildEnv = %s" % build_env
39812927Sgabeblack@google.com    print >>f, "hgRev, hgId, hgDate = %s" % hg_info
39912927Sgabeblack@google.com    f.close()
40012927Sgabeblack@google.com
40112927Sgabeblack@google.comdefines_info = [ Value(build_env), Value(hg_info) ]
40212927Sgabeblack@google.com# Generate a file with all of the compile options in it
40312927Sgabeblack@google.comenv.Command('python/m5/defines.py', defines_info, makeDefinesPyFile)
40412927Sgabeblack@google.comPySource('m5', 'python/m5/defines.py')
40512927Sgabeblack@google.com
40612927Sgabeblack@google.com# Generate python file containing info about the M5 source code
40712927Sgabeblack@google.comdef makeInfoPyFile(target, source, env):
40812927Sgabeblack@google.com    f = file(str(target[0]), 'w')
40912927Sgabeblack@google.com    for src in source:
41012927Sgabeblack@google.com        data = ''.join(file(src.srcnode().abspath, 'r').xreadlines())
41112927Sgabeblack@google.com        print >>f, "%s = %s" % (src, repr(data))
41212927Sgabeblack@google.com    f.close()
41312927Sgabeblack@google.com
41412927Sgabeblack@google.com# Generate a file that wraps the basic top level files
41512927Sgabeblack@google.comenv.Command('python/m5/info.py',
41612927Sgabeblack@google.com            [ '#/AUTHORS', '#/LICENSE', '#/README', '#/RELEASE_NOTES' ],
41712927Sgabeblack@google.com            makeInfoPyFile)
41812927Sgabeblack@google.comPySource('m5', 'python/m5/info.py')
41912927Sgabeblack@google.com
42012927Sgabeblack@google.com# Generate the __init__.py file for m5.objects
42112927Sgabeblack@google.comdef makeObjectsInitFile(target, source, env):
42212837Sgabeblack@google.com    f = file(str(target[0]), 'w')
423    print >>f, 'from params import *'
424    print >>f, 'from m5.SimObject import *'
425    for module in source:
426        print >>f, 'from %s import *' % module.get_contents()
427    f.close()
428
429# Generate an __init__.py file for the objects package
430env.Command('python/m5/objects/__init__.py',
431            [ Value(o) for o in sort_list(sim_object_modfiles) ],
432            makeObjectsInitFile)
433PySource('m5.objects', 'python/m5/objects/__init__.py')
434
435########################################################################
436#
437# Create all of the SimObject param headers and enum headers
438#
439
440def createSimObjectParam(target, source, env):
441    assert len(target) == 1 and len(source) == 1
442
443    hh_file = file(target[0].abspath, 'w')
444    name = str(source[0].get_contents())
445    obj = sim_objects[name]
446
447    print >>hh_file, obj.cxx_decl()
448
449def createSwigParam(target, source, env):
450    assert len(target) == 1 and len(source) == 1
451
452    i_file = file(target[0].abspath, 'w')
453    name = str(source[0].get_contents())
454    param = all_params[name]
455
456    for line in param.swig_decl():
457        print >>i_file, line
458
459def createEnumStrings(target, source, env):
460    assert len(target) == 1 and len(source) == 1
461
462    cc_file = file(target[0].abspath, 'w')
463    name = str(source[0].get_contents())
464    obj = all_enums[name]
465
466    print >>cc_file, obj.cxx_def()
467    cc_file.close()
468
469def createEnumParam(target, source, env):
470    assert len(target) == 1 and len(source) == 1
471
472    hh_file = file(target[0].abspath, 'w')
473    name = str(source[0].get_contents())
474    obj = all_enums[name]
475
476    print >>hh_file, obj.cxx_decl()
477
478# Generate all of the SimObject param struct header files
479params_hh_files = []
480for name,simobj in sim_objects.iteritems():
481    extra_deps = [ File(py_modules[simobj.__module__]) ]
482
483    hh_file = File('params/%s.hh' % name)
484    params_hh_files.append(hh_file)
485    env.Command(hh_file, Value(name), createSimObjectParam)
486    env.Depends(hh_file, depends + extra_deps)
487
488# Generate any parameter header files needed
489params_i_files = []
490for name,param in all_params.iteritems():
491    if isinstance(param, m5.params.VectorParamDesc):
492        ext = 'vptype'
493    else:
494        ext = 'ptype'
495
496    i_file = File('params/%s_%s.i' % (name, ext))
497    params_i_files.append(i_file)
498    env.Command(i_file, Value(name), createSwigParam)
499    env.Depends(i_file, depends)
500
501# Generate all enum header files
502for name,enum in all_enums.iteritems():
503    extra_deps = [ File(py_modules[enum.__module__]) ]
504
505    cc_file = File('enums/%s.cc' % name)
506    env.Command(cc_file, Value(name), createEnumStrings)
507    env.Depends(cc_file, depends + extra_deps)
508    Source(cc_file)
509
510    hh_file = File('enums/%s.hh' % name)
511    env.Command(hh_file, Value(name), createEnumParam)
512    env.Depends(hh_file, depends + extra_deps)
513
514# Build the big monolithic swigged params module (wraps all SimObject
515# param structs and enum structs)
516def buildParams(target, source, env):
517    names = [ s.get_contents() for s in source ]
518    objs = [ sim_objects[name] for name in names ]
519    out = file(target[0].abspath, 'w')
520
521    ordered_objs = []
522    obj_seen = set()
523    def order_obj(obj):
524        name = str(obj)
525        if name in obj_seen:
526            return
527
528        obj_seen.add(name)
529        if str(obj) != 'SimObject':
530            order_obj(obj.__bases__[0])
531
532        ordered_objs.append(obj)
533
534    for obj in objs:
535        order_obj(obj)
536
537    enums = set()
538    predecls = []
539    pd_seen = set()
540
541    def add_pds(*pds):
542        for pd in pds:
543            if pd not in pd_seen:
544                predecls.append(pd)
545                pd_seen.add(pd)
546
547    for obj in ordered_objs:
548        params = obj._params.local.values()
549        for param in params:
550            ptype = param.ptype
551            if issubclass(ptype, m5.params.Enum):
552                if ptype not in enums:
553                    enums.add(ptype)
554            pds = param.swig_predecls()
555            if isinstance(pds, (list, tuple)):
556                add_pds(*pds)
557            else:
558                add_pds(pds)
559
560    print >>out, '%module params'
561
562    print >>out, '%{'
563    for obj in ordered_objs:
564        print >>out, '#include "params/%s.hh"' % obj
565    print >>out, '%}'
566
567    for pd in predecls:
568        print >>out, pd
569
570    enums = list(enums)
571    enums.sort()
572    for enum in enums:
573        print >>out, '%%include "enums/%s.hh"' % enum.__name__
574    print >>out
575
576    for obj in ordered_objs:
577        if obj.swig_objdecls:
578            for decl in obj.swig_objdecls:
579                print >>out, decl
580            continue
581
582        class_path = obj.cxx_class.split('::')
583        classname = class_path[-1]
584        namespaces = class_path[:-1]
585        namespaces.reverse()
586
587        code = ''
588
589        if namespaces:
590            code += '// avoid name conflicts\n'
591            sep_string = '_COLONS_'
592            flat_name = sep_string.join(class_path)
593            code += '%%rename(%s) %s;\n' % (flat_name, classname)
594
595        code += '// stop swig from creating/wrapping default ctor/dtor\n'
596        code += '%%nodefault %s;\n' % classname
597        code += 'class %s ' % classname
598        if obj._base:
599            code += ': public %s' % obj._base.cxx_class
600        code += ' {};\n'
601
602        for ns in namespaces:
603            new_code = 'namespace %s {\n' % ns
604            new_code += code
605            new_code += '}\n'
606            code = new_code
607
608        print >>out, code
609
610    print >>out, '%%include "src/sim/sim_object_params.hh"' % obj
611    for obj in ordered_objs:
612        print >>out, '%%include "params/%s.hh"' % obj
613
614params_file = File('params/params.i')
615names = sort_list(sim_objects.keys())
616env.Command(params_file, [ Value(v) for v in names ], buildParams)
617env.Depends(params_file, params_hh_files + params_i_files + depends)
618SwigSource('m5.objects', params_file)
619
620# Build all swig modules
621swig_modules = []
622cc_swig_sources = []
623for source,package in swig_sources:
624    filename = str(source)
625    assert filename.endswith('.i')
626
627    base = '.'.join(filename.split('.')[:-1])
628    module = basename(base)
629    cc_file = base + '_wrap.cc'
630    py_file = base + '.py'
631
632    env.Command([cc_file, py_file], source,
633                '$SWIG $SWIGFLAGS -outdir ${TARGETS[1].dir} '
634                '-o ${TARGETS[0]} $SOURCES')
635    env.Depends(py_file, source)
636    env.Depends(cc_file, source)
637
638    swig_modules.append(Value(module))
639    cc_swig_sources.append(File(cc_file))
640    PySource(package, py_file)
641
642# Generate the main swig init file
643def makeSwigInit(target, source, env):
644    f = file(str(target[0]), 'w')
645    print >>f, 'extern "C" {'
646    for module in source:
647        print >>f, '    void init_%s();' % module.get_contents()
648    print >>f, '}'
649    print >>f, 'void initSwig() {'
650    for module in source:
651        print >>f, '    init_%s();' % module.get_contents()
652    print >>f, '}'
653    f.close()
654
655env.Command('python/swig/init.cc', swig_modules, makeSwigInit)
656Source('python/swig/init.cc')
657
658# Generate traceflags.py
659def traceFlagsPy(target, source, env):
660    assert(len(target) == 1)
661
662    f = file(str(target[0]), 'w')
663
664    allFlags = []
665    for s in source:
666        val = eval(s.get_contents())
667        allFlags.append(val)
668
669    print >>f, 'baseFlags = ['
670    for flag, compound, desc in allFlags:
671        if not compound:
672            print >>f, "    '%s'," % flag
673    print >>f, "    ]"
674    print >>f
675
676    print >>f, 'compoundFlags = ['
677    print >>f, "    'All',"
678    for flag, compound, desc in allFlags:
679        if compound:
680            print >>f, "    '%s'," % flag
681    print >>f, "    ]"
682    print >>f
683
684    print >>f, "allFlags = frozenset(baseFlags + compoundFlags)"
685    print >>f
686
687    print >>f, 'compoundFlagMap = {'
688    all = tuple([flag for flag,compound,desc in allFlags if not compound])
689    print >>f, "    'All' : %s," % (all, )
690    for flag, compound, desc in allFlags:
691        if compound:
692            print >>f, "    '%s' : %s," % (flag, compound)
693    print >>f, "    }"
694    print >>f
695
696    print >>f, 'flagDescriptions = {'
697    print >>f, "    'All' : 'All flags',"
698    for flag, compound, desc in allFlags:
699        print >>f, "    '%s' : '%s'," % (flag, desc)
700    print >>f, "    }"
701
702    f.close()
703
704def traceFlagsCC(target, source, env):
705    assert(len(target) == 1)
706
707    f = file(str(target[0]), 'w')
708
709    allFlags = []
710    for s in source:
711        val = eval(s.get_contents())
712        allFlags.append(val)
713
714    # file header
715    print >>f, '''
716/*
717 * DO NOT EDIT THIS FILE! Automatically generated
718 */
719
720#include "base/traceflags.hh"
721
722using namespace Trace;
723
724const char *Trace::flagStrings[] =
725{'''
726
727    # The string array is used by SimpleEnumParam to map the strings
728    # provided by the user to enum values.
729    for flag, compound, desc in allFlags:
730        if not compound:
731            print >>f, '    "%s",' % flag
732
733    print >>f, '    "All",'
734    for flag, compound, desc in allFlags:
735        if compound:
736            print >>f, '    "%s",' % flag
737
738    print >>f, '};'
739    print >>f
740    print >>f, 'const int Trace::numFlagStrings = %d;' % (len(allFlags) + 1)
741    print >>f
742
743    #
744    # Now define the individual compound flag arrays.  There is an array
745    # for each compound flag listing the component base flags.
746    #
747    all = tuple([flag for flag,compound,desc in allFlags if not compound])
748    print >>f, 'static const Flags AllMap[] = {'
749    for flag, compound, desc in allFlags:
750        if not compound:
751            print >>f, "    %s," % flag
752    print >>f, '};'
753    print >>f
754
755    for flag, compound, desc in allFlags:
756        if not compound:
757            continue
758        print >>f, 'static const Flags %sMap[] = {' % flag
759        for flag in compound:
760            print >>f, "    %s," % flag
761        print >>f, "    (Flags)-1"
762        print >>f, '};'
763        print >>f
764
765    #
766    # Finally the compoundFlags[] array maps the compound flags
767    # to their individual arrays/
768    #
769    print >>f, 'const Flags *Trace::compoundFlags[] ='
770    print >>f, '{'
771    print >>f, '    AllMap,'
772    for flag, compound, desc in allFlags:
773        if compound:
774            print >>f, '    %sMap,' % flag
775    # file trailer
776    print >>f, '};'
777
778    f.close()
779
780def traceFlagsHH(target, source, env):
781    assert(len(target) == 1)
782
783    f = file(str(target[0]), 'w')
784
785    allFlags = []
786    for s in source:
787        val = eval(s.get_contents())
788        allFlags.append(val)
789
790    # file header boilerplate
791    print >>f, '''
792/*
793 * DO NOT EDIT THIS FILE!
794 *
795 * Automatically generated from traceflags.py
796 */
797
798#ifndef __BASE_TRACE_FLAGS_HH__
799#define __BASE_TRACE_FLAGS_HH__
800
801namespace Trace {
802
803enum Flags {'''
804
805    # Generate the enum.  Base flags come first, then compound flags.
806    idx = 0
807    for flag, compound, desc in allFlags:
808        if not compound:
809            print >>f, '    %s = %d,' % (flag, idx)
810            idx += 1
811
812    numBaseFlags = idx
813    print >>f, '    NumFlags = %d,' % idx
814
815    # put a comment in here to separate base from compound flags
816    print >>f, '''
817// The remaining enum values are *not* valid indices for Trace::flags.
818// They are "compound" flags, which correspond to sets of base
819// flags, and are used by changeFlag.'''
820
821    print >>f, '    All = %d,' % idx
822    idx += 1
823    for flag, compound, desc in allFlags:
824        if compound:
825            print >>f, '    %s = %d,' % (flag, idx)
826            idx += 1
827
828    numCompoundFlags = idx - numBaseFlags
829    print >>f, '    NumCompoundFlags = %d' % numCompoundFlags
830
831    # trailer boilerplate
832    print >>f, '''\
833}; // enum Flags
834
835// Array of strings for SimpleEnumParam
836extern const char *flagStrings[];
837extern const int numFlagStrings;
838
839// Array of arraay pointers: for each compound flag, gives the list of
840// base flags to set.  Inidividual flag arrays are terminated by -1.
841extern const Flags *compoundFlags[];
842
843/* namespace Trace */ }
844
845#endif // __BASE_TRACE_FLAGS_HH__
846'''
847
848    f.close()
849
850flags = [ Value(f) for f in trace_flags ]
851env.Command('base/traceflags.py', flags, traceFlagsPy)
852PySource('m5', 'base/traceflags.py')
853
854env.Command('base/traceflags.hh', flags, traceFlagsHH)
855env.Command('base/traceflags.cc', flags, traceFlagsCC)
856Source('base/traceflags.cc')
857
858# embed python files.  All .py files that have been indicated by a
859# PySource() call in a SConscript need to be embedded into the M5
860# library.  To do that, we compile the file to byte code, marshal the
861# byte code, compress it, and then generate an assembly file that
862# inserts the result into the data section with symbols indicating the
863# beginning, and end (and with the size at the end)
864py_sources_tnodes = {}
865for pysource in py_sources:
866    py_sources_tnodes[pysource.tnode] = pysource
867
868def objectifyPyFile(target, source, env):
869    '''Action function to compile a .py into a code object, marshal
870    it, compress it, and stick it into an asm file so the code appears
871    as just bytes with a label in the data section'''
872
873    src = file(str(source[0]), 'r').read()
874    dst = file(str(target[0]), 'w')
875
876    pysource = py_sources_tnodes[source[0]]
877    compiled = compile(src, pysource.debugname, 'exec')
878    marshalled = marshal.dumps(compiled)
879    compressed = zlib.compress(marshalled)
880    data = compressed
881
882    # Some C/C++ compilers prepend an underscore to global symbol
883    # names, so if they're going to do that, we need to prepend that
884    # leading underscore to globals in the assembly file.
885    if env['LEADING_UNDERSCORE']:
886        sym = '_' + pysource.symname
887    else:
888        sym = pysource.symname
889
890    step = 16
891    print >>dst, ".data"
892    print >>dst, ".globl %s_beg" % sym
893    print >>dst, ".globl %s_end" % sym
894    print >>dst, "%s_beg:" % sym
895    for i in xrange(0, len(data), step):
896        x = array.array('B', data[i:i+step])
897        print >>dst, ".byte", ','.join([str(d) for d in x])
898    print >>dst, "%s_end:" % sym
899    print >>dst, ".long %d" % len(marshalled)
900
901for source in py_sources:
902    env.Command(source.assembly, source.tnode, objectifyPyFile)
903    Source(source.assembly)
904
905# Generate init_python.cc which creates a bunch of EmbeddedPyModule
906# structs that describe the embedded python code.  One such struct
907# contains information about the importer that python uses to get at
908# the embedded files, and then there's a list of all of the rest that
909# the importer uses to load the rest on demand.
910py_sources_symbols = {}
911for pysource in py_sources:
912    py_sources_symbols[pysource.symname] = pysource
913def pythonInit(target, source, env):
914    dst = file(str(target[0]), 'w')
915
916    def dump_mod(sym, endchar=','):
917        pysource = py_sources_symbols[sym]
918        print >>dst, '    { "%s",' % pysource.arcname
919        print >>dst, '      "%s",' % pysource.modpath
920        print >>dst, '       %s_beg, %s_end,' % (sym, sym)
921        print >>dst, '       %s_end - %s_beg,' % (sym, sym)
922        print >>dst, '       *(int *)%s_end }%s'  % (sym, endchar)
923    
924    print >>dst, '#include "sim/init.hh"'
925
926    for sym in source:
927        sym = sym.get_contents()
928        print >>dst, "extern const char %s_beg[], %s_end[];" % (sym, sym)
929
930    print >>dst, "const EmbeddedPyModule embeddedPyImporter = "
931    dump_mod("PyEMB_importer", endchar=';');
932    print >>dst
933
934    print >>dst, "const EmbeddedPyModule embeddedPyModules[] = {"
935    for i,sym in enumerate(source):
936        sym = sym.get_contents()
937        if sym == "PyEMB_importer":
938            # Skip the importer since we've already exported it
939            continue
940        dump_mod(sym)
941    print >>dst, "    { 0, 0, 0, 0, 0, 0 }"
942    print >>dst, "};"
943
944symbols = [Value(s.symname) for s in py_sources]
945env.Command('sim/init_python.cc', symbols, pythonInit)
946Source('sim/init_python.cc')
947
948########################################################################
949#
950# Define binaries.  Each different build type (debug, opt, etc.) gets
951# a slightly different build environment.
952#
953
954# List of constructed environments to pass back to SConstruct
955envList = []
956
957# This function adds the specified sources to the given build
958# environment, and returns a list of all the corresponding SCons
959# Object nodes (including an extra one for date.cc).  We explicitly
960# add the Object nodes so we can set up special dependencies for
961# date.cc.
962def make_objs(sources, env, static):
963    if static:
964        XObject = env.StaticObject
965    else:
966        XObject = env.SharedObject
967
968    objs = [ XObject(s) for s in sources ]
969  
970    # make date.cc depend on all other objects so it always gets
971    # recompiled whenever anything else does
972    date_obj = XObject('base/date.cc')
973
974    env.Depends(date_obj, objs)
975    objs.append(date_obj)
976    return objs
977
978# Function to create a new build environment as clone of current
979# environment 'env' with modified object suffix and optional stripped
980# binary.  Additional keyword arguments are appended to corresponding
981# build environment vars.
982def makeEnv(label, objsfx, strip = False, **kwargs):
983    # SCons doesn't know to append a library suffix when there is a '.' in the
984    # name.  Use '_' instead.
985    libname = 'm5_' + label
986    exename = 'm5.' + label
987
988    new_env = env.Copy(OBJSUFFIX=objsfx, SHOBJSUFFIX=objsfx + 's')
989    new_env.Label = label
990    new_env.Append(**kwargs)
991
992    swig_env = new_env.Copy()
993    if env['GCC']:
994        swig_env.Append(CCFLAGS='-Wno-uninitialized')
995        swig_env.Append(CCFLAGS='-Wno-sign-compare')
996        swig_env.Append(CCFLAGS='-Wno-parentheses')
997
998    static_objs = make_objs(cc_lib_sources, new_env, static=True)
999    shared_objs = make_objs(cc_lib_sources, new_env, static=False)
1000    static_objs += [ swig_env.StaticObject(s) for s in cc_swig_sources ]
1001    shared_objs += [ swig_env.SharedObject(s) for s in cc_swig_sources ]
1002
1003    # First make a library of everything but main() so other programs can
1004    # link against m5.
1005    static_lib = new_env.StaticLibrary(libname, static_objs)
1006    shared_lib = new_env.SharedLibrary(libname, shared_objs)
1007
1008    for target, sources in unit_tests:
1009        objs = [ new_env.StaticObject(s) for s in sources ]
1010        new_env.Program("unittest/%s.%s" % (target, label), objs + static_lib)
1011
1012    # Now link a stub with main() and the static library.
1013    objects = [new_env.Object(s) for s in cc_bin_sources] + static_lib
1014    if strip:
1015        unstripped_exe = exename + '.unstripped'
1016        new_env.Program(unstripped_exe, objects)
1017        if sys.platform == 'sunos5':
1018            cmd = 'cp $SOURCE $TARGET; strip $TARGET'
1019        else:
1020            cmd = 'strip $SOURCE -o $TARGET'
1021        targets = new_env.Command(exename, unstripped_exe, cmd)
1022    else:
1023        targets = new_env.Program(exename, objects)
1024            
1025    new_env.M5Binary = targets[0]
1026    envList.append(new_env)
1027
1028# Debug binary
1029ccflags = {}
1030if env['GCC']:
1031    if sys.platform == 'sunos5':
1032        ccflags['debug'] = '-gstabs+'
1033    else:
1034        ccflags['debug'] = '-ggdb3'
1035    ccflags['opt'] = '-g -O3'
1036    ccflags['fast'] = '-O3'
1037    ccflags['prof'] = '-O3 -g -pg'
1038elif env['SUNCC']:
1039    ccflags['debug'] = '-g0'
1040    ccflags['opt'] = '-g -O'
1041    ccflags['fast'] = '-fast'
1042    ccflags['prof'] = '-fast -g -pg'
1043elif env['ICC']:
1044    ccflags['debug'] = '-g -O0'
1045    ccflags['opt'] = '-g -O'
1046    ccflags['fast'] = '-fast'
1047    ccflags['prof'] = '-fast -g -pg'
1048else:
1049    print 'Unknown compiler, please fix compiler options'
1050    Exit(1)
1051
1052makeEnv('debug', '.do',
1053        CCFLAGS = Split(ccflags['debug']),
1054        CPPDEFINES = ['DEBUG', 'TRACING_ON=1'])
1055
1056# Optimized binary
1057makeEnv('opt', '.o',
1058        CCFLAGS = Split(ccflags['opt']),
1059        CPPDEFINES = ['TRACING_ON=1'])
1060
1061# "Fast" binary
1062makeEnv('fast', '.fo', strip = True,
1063        CCFLAGS = Split(ccflags['fast']),
1064        CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'])
1065
1066# Profiled binary
1067makeEnv('prof', '.po',
1068        CCFLAGS = Split(ccflags['prof']),
1069        CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1070        LINKFLAGS = '-pg')
1071
1072Return('envList')
1073