SConscript revision 8232
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#
292665Ssaidi@eecs.umich.edu# Authors: Nathan Binkert
30955SN/A
31955SN/Aimport array
32955SN/Aimport bisect
33955SN/Aimport imp
34955SN/Aimport marshal
352632Sstever@eecs.umich.eduimport os
362632Sstever@eecs.umich.eduimport re
372632Sstever@eecs.umich.eduimport sys
382632Sstever@eecs.umich.eduimport zlib
39955SN/A
402632Sstever@eecs.umich.edufrom os.path import basename, dirname, exists, isdir, isfile, join as joinpath
412632Sstever@eecs.umich.edu
422761Sstever@eecs.umich.eduimport SCons
432632Sstever@eecs.umich.edu
442632Sstever@eecs.umich.edu# This file defines how to build a particular configuration of M5
452632Sstever@eecs.umich.edu# based on variable settings in the 'env' build environment.
462761Sstever@eecs.umich.edu
472761Sstever@eecs.umich.eduImport('*')
482761Sstever@eecs.umich.edu
492632Sstever@eecs.umich.edu# Children need to see the environment
502632Sstever@eecs.umich.eduExport('env')
512761Sstever@eecs.umich.edu
522761Sstever@eecs.umich.edubuild_env = [(opt, env[opt]) for opt in export_vars]
532761Sstever@eecs.umich.edu
542761Sstever@eecs.umich.edufrom m5.util import code_formatter
552761Sstever@eecs.umich.edu
562632Sstever@eecs.umich.edu########################################################################
572632Sstever@eecs.umich.edu# Code for adding source files of various types
582632Sstever@eecs.umich.edu#
592632Sstever@eecs.umich.educlass SourceMeta(type):
602632Sstever@eecs.umich.edu    def __init__(cls, name, bases, dict):
612632Sstever@eecs.umich.edu        super(SourceMeta, cls).__init__(name, bases, dict)
622632Sstever@eecs.umich.edu        cls.all = []
63955SN/A        
64955SN/A    def get(cls, **kwargs):
65955SN/A        for src in cls.all:
66955SN/A            for attr,value in kwargs.iteritems():
67955SN/A                if getattr(src, attr) != value:
68955SN/A                    break
69955SN/A            else:
702656Sstever@eecs.umich.edu                yield src
712656Sstever@eecs.umich.edu
722656Sstever@eecs.umich.educlass SourceFile(object):
732656Sstever@eecs.umich.edu    __metaclass__ = SourceMeta
742656Sstever@eecs.umich.edu    def __init__(self, source):
752656Sstever@eecs.umich.edu        tnode = source
762656Sstever@eecs.umich.edu        if not isinstance(source, SCons.Node.FS.File):
772653Sstever@eecs.umich.edu            tnode = File(source)
782653Sstever@eecs.umich.edu
792653Sstever@eecs.umich.edu        self.tnode = tnode
802653Sstever@eecs.umich.edu        self.snode = tnode.srcnode()
812653Sstever@eecs.umich.edu        self.filename = str(tnode)
822653Sstever@eecs.umich.edu        self.dirname = dirname(self.filename)
832653Sstever@eecs.umich.edu        self.basename = basename(self.filename)
842653Sstever@eecs.umich.edu        index = self.basename.rfind('.')
852653Sstever@eecs.umich.edu        if index <= 0:
862653Sstever@eecs.umich.edu            # dot files aren't extensions
872653Sstever@eecs.umich.edu            self.extname = self.basename, None
881852SN/A        else:
89955SN/A            self.extname = self.basename[:index], self.basename[index+1:]
90955SN/A
91955SN/A        for base in type(self).__mro__:
922632Sstever@eecs.umich.edu            if issubclass(base, SourceFile):
932632Sstever@eecs.umich.edu                base.all.append(self)
94955SN/A
951533SN/A    def __lt__(self, other): return self.filename < other.filename
962632Sstever@eecs.umich.edu    def __le__(self, other): return self.filename <= other.filename
971533SN/A    def __gt__(self, other): return self.filename > other.filename
98955SN/A    def __ge__(self, other): return self.filename >= other.filename
99955SN/A    def __eq__(self, other): return self.filename == other.filename
1002632Sstever@eecs.umich.edu    def __ne__(self, other): return self.filename != other.filename
1012632Sstever@eecs.umich.edu        
102955SN/Aclass Source(SourceFile):
103955SN/A    '''Add a c/c++ source file to the build'''
104955SN/A    def __init__(self, source, Werror=True, swig=False, bin_only=False,
105955SN/A                 skip_lib=False):
1062632Sstever@eecs.umich.edu        super(Source, self).__init__(source)
107955SN/A
1082632Sstever@eecs.umich.edu        self.Werror = Werror
109955SN/A        self.swig = swig
110955SN/A        self.bin_only = bin_only
1112632Sstever@eecs.umich.edu        self.skip_lib = bin_only or skip_lib
1122632Sstever@eecs.umich.edu
1132632Sstever@eecs.umich.educlass PySource(SourceFile):
1142632Sstever@eecs.umich.edu    '''Add a python source file to the named package'''
1152632Sstever@eecs.umich.edu    invalid_sym_char = re.compile('[^A-z0-9_]')
1162632Sstever@eecs.umich.edu    modules = {}
1172632Sstever@eecs.umich.edu    tnodes = {}
1182632Sstever@eecs.umich.edu    symnames = {}
1192632Sstever@eecs.umich.edu    
1202632Sstever@eecs.umich.edu    def __init__(self, package, source):
1212632Sstever@eecs.umich.edu        super(PySource, self).__init__(source)
1223053Sstever@eecs.umich.edu
1233053Sstever@eecs.umich.edu        modname,ext = self.extname
1243053Sstever@eecs.umich.edu        assert ext == 'py'
1253053Sstever@eecs.umich.edu
1263053Sstever@eecs.umich.edu        if package:
1273053Sstever@eecs.umich.edu            path = package.split('.')
1283053Sstever@eecs.umich.edu        else:
1293053Sstever@eecs.umich.edu            path = []
1303053Sstever@eecs.umich.edu
1313053Sstever@eecs.umich.edu        modpath = path[:]
1323053Sstever@eecs.umich.edu        if modname != '__init__':
1333053Sstever@eecs.umich.edu            modpath += [ modname ]
1343053Sstever@eecs.umich.edu        modpath = '.'.join(modpath)
1353053Sstever@eecs.umich.edu
1363053Sstever@eecs.umich.edu        arcpath = path + [ self.basename ]
1373053Sstever@eecs.umich.edu        abspath = self.snode.abspath
1382632Sstever@eecs.umich.edu        if not exists(abspath):
1392632Sstever@eecs.umich.edu            abspath = self.tnode.abspath
1402632Sstever@eecs.umich.edu
1412632Sstever@eecs.umich.edu        self.package = package
1422632Sstever@eecs.umich.edu        self.modname = modname
1432632Sstever@eecs.umich.edu        self.modpath = modpath
1442634Sstever@eecs.umich.edu        self.arcname = joinpath(*arcpath)
1452634Sstever@eecs.umich.edu        self.abspath = abspath
1462632Sstever@eecs.umich.edu        self.compiled = File(self.filename + 'c')
1472638Sstever@eecs.umich.edu        self.cpp = File(self.filename + '.cc')
1482632Sstever@eecs.umich.edu        self.symname = PySource.invalid_sym_char.sub('_', modpath)
1492632Sstever@eecs.umich.edu
1502632Sstever@eecs.umich.edu        PySource.modules[modpath] = self
1512632Sstever@eecs.umich.edu        PySource.tnodes[self.tnode] = self
1522632Sstever@eecs.umich.edu        PySource.symnames[self.symname] = self
1532632Sstever@eecs.umich.edu
1541858SN/Aclass SimObject(PySource):
1552638Sstever@eecs.umich.edu    '''Add a SimObject python file as a python source object and add
1562638Sstever@eecs.umich.edu    it to a list of sim object modules'''
1572638Sstever@eecs.umich.edu
1582638Sstever@eecs.umich.edu    fixed = False
1592638Sstever@eecs.umich.edu    modnames = []
1602638Sstever@eecs.umich.edu
1612638Sstever@eecs.umich.edu    def __init__(self, source):
1622638Sstever@eecs.umich.edu        super(SimObject, self).__init__('m5.objects', source)
1632634Sstever@eecs.umich.edu        if self.fixed:
1642634Sstever@eecs.umich.edu            raise AttributeError, "Too late to call SimObject now."
1652634Sstever@eecs.umich.edu
166955SN/A        bisect.insort_right(SimObject.modnames, self.modname)
167955SN/A
168955SN/Aclass SwigSource(SourceFile):
169955SN/A    '''Add a swig file to build'''
170955SN/A
171955SN/A    def __init__(self, package, source):
172955SN/A        super(SwigSource, self).__init__(source)
173955SN/A
1741858SN/A        modname,ext = self.extname
1751858SN/A        assert ext == 'i'
1762632Sstever@eecs.umich.edu
177955SN/A        self.module = modname
1783643Ssaidi@eecs.umich.edu        cc_file = joinpath(self.dirname, modname + '_wrap.cc')
1793643Ssaidi@eecs.umich.edu        py_file = joinpath(self.dirname, modname + '.py')
1803643Ssaidi@eecs.umich.edu
1813643Ssaidi@eecs.umich.edu        self.cc_source = Source(cc_file, swig=True)
1823643Ssaidi@eecs.umich.edu        self.py_source = PySource(package, py_file)
1833643Ssaidi@eecs.umich.edu
1843643Ssaidi@eecs.umich.eduunit_tests = []
1853643Ssaidi@eecs.umich.edudef UnitTest(target, sources):
1862776Sstever@eecs.umich.edu    if not isinstance(sources, (list, tuple)):
1871105SN/A        sources = [ sources ]
1882667Sstever@eecs.umich.edu
1892667Sstever@eecs.umich.edu    sources = [ Source(src, skip_lib=True) for src in sources ]
1902667Sstever@eecs.umich.edu    unit_tests.append((target, sources))
1912667Sstever@eecs.umich.edu
1922667Sstever@eecs.umich.edu# Children should have access
1932667Sstever@eecs.umich.eduExport('Source')
1941869SN/AExport('PySource')
1951869SN/AExport('SimObject')
1961869SN/AExport('SwigSource')
1971869SN/AExport('UnitTest')
1981869SN/A
1991065SN/A########################################################################
2002632Sstever@eecs.umich.edu#
2012632Sstever@eecs.umich.edu# Debug Flags
202955SN/A#
2031858SN/Adebug_flags = {}
2041858SN/Adef DebugFlag(name, desc=None):
2051858SN/A    if name in debug_flags:
2061858SN/A        raise AttributeError, "Flag %s already specified" % name
2071851SN/A    debug_flags[name] = (name, (), desc)
2081851SN/ATraceFlag = DebugFlag
2091858SN/A
2102632Sstever@eecs.umich.edudef CompoundFlag(name, flags, desc=None):
211955SN/A    if name in debug_flags:
2123053Sstever@eecs.umich.edu        raise AttributeError, "Flag %s already specified" % name
2133053Sstever@eecs.umich.edu
2143053Sstever@eecs.umich.edu    compound = tuple(flags)
2153053Sstever@eecs.umich.edu    debug_flags[name] = (name, compound, desc)
2163053Sstever@eecs.umich.edu
2173053Sstever@eecs.umich.eduExport('DebugFlag')
2183053Sstever@eecs.umich.eduExport('TraceFlag')
2193053Sstever@eecs.umich.eduExport('CompoundFlag')
2203053Sstever@eecs.umich.edu
2213053Sstever@eecs.umich.edu########################################################################
2223053Sstever@eecs.umich.edu#
2233053Sstever@eecs.umich.edu# Set some compiler variables
2243053Sstever@eecs.umich.edu#
2253053Sstever@eecs.umich.edu
2263053Sstever@eecs.umich.edu# Include file paths are rooted in this directory.  SCons will
2273053Sstever@eecs.umich.edu# automatically expand '.' to refer to both the source directory and
2283053Sstever@eecs.umich.edu# the corresponding build directory to pick up generated include
2293053Sstever@eecs.umich.edu# files.
2303053Sstever@eecs.umich.eduenv.Append(CPPPATH=Dir('.'))
2312667Sstever@eecs.umich.edu
2322667Sstever@eecs.umich.edufor extra_dir in extras_dir_list:
2332667Sstever@eecs.umich.edu    env.Append(CPPPATH=Dir(extra_dir))
2342667Sstever@eecs.umich.edu
2352667Sstever@eecs.umich.edu# Workaround for bug in SCons version > 0.97d20071212
2362667Sstever@eecs.umich.edu# Scons bug id: 2006 M5 Bug id: 308 
2372667Sstever@eecs.umich.edufor root, dirs, files in os.walk(base_dir, topdown=True):
2382667Sstever@eecs.umich.edu    Dir(root[len(base_dir) + 1:])
2392667Sstever@eecs.umich.edu
2402667Sstever@eecs.umich.edu########################################################################
2412667Sstever@eecs.umich.edu#
2422667Sstever@eecs.umich.edu# Walk the tree and execute all SConscripts in subdirectories
2432638Sstever@eecs.umich.edu#
2442638Sstever@eecs.umich.edu
2452638Sstever@eecs.umich.eduhere = Dir('.').srcnode().abspath
2462638Sstever@eecs.umich.edufor root, dirs, files in os.walk(base_dir, topdown=True):
2472638Sstever@eecs.umich.edu    if root == here:
2481858SN/A        # we don't want to recurse back into this SConscript
2493118Sstever@eecs.umich.edu        continue
2503118Sstever@eecs.umich.edu
2513118Sstever@eecs.umich.edu    if 'SConscript' in files:
2523118Sstever@eecs.umich.edu        build_dir = joinpath(env['BUILDDIR'], root[len(base_dir) + 1:])
2533118Sstever@eecs.umich.edu        SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
2543118Sstever@eecs.umich.edu
2553118Sstever@eecs.umich.edufor extra_dir in extras_dir_list:
2563118Sstever@eecs.umich.edu    prefix_len = len(dirname(extra_dir)) + 1
2573118Sstever@eecs.umich.edu    for root, dirs, files in os.walk(extra_dir, topdown=True):
2583118Sstever@eecs.umich.edu        if 'SConscript' in files:
2593118Sstever@eecs.umich.edu            build_dir = joinpath(env['BUILDDIR'], root[prefix_len:])
2603118Sstever@eecs.umich.edu            SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
2613118Sstever@eecs.umich.edu
2623118Sstever@eecs.umich.edufor opt in export_vars:
2633118Sstever@eecs.umich.edu    env.ConfigFile(opt)
2643118Sstever@eecs.umich.edu
2653118Sstever@eecs.umich.edudef makeTheISA(source, target, env):
2663118Sstever@eecs.umich.edu    isas = [ src.get_contents() for src in source ]
2673118Sstever@eecs.umich.edu    target_isa = env['TARGET_ISA']
2683118Sstever@eecs.umich.edu    def define(isa):
2693118Sstever@eecs.umich.edu        return isa.upper() + '_ISA'
2703118Sstever@eecs.umich.edu    
2713118Sstever@eecs.umich.edu    def namespace(isa):
2723118Sstever@eecs.umich.edu        return isa[0].upper() + isa[1:].lower() + 'ISA' 
2733118Sstever@eecs.umich.edu
2743118Sstever@eecs.umich.edu
2753118Sstever@eecs.umich.edu    code = code_formatter()
2763118Sstever@eecs.umich.edu    code('''\
2773118Sstever@eecs.umich.edu#ifndef __CONFIG_THE_ISA_HH__
2783118Sstever@eecs.umich.edu#define __CONFIG_THE_ISA_HH__
2793118Sstever@eecs.umich.edu
2803118Sstever@eecs.umich.edu''')
2813483Ssaidi@eecs.umich.edu
2823494Ssaidi@eecs.umich.edu    for i,isa in enumerate(isas):
2833494Ssaidi@eecs.umich.edu        code('#define $0 $1', define(isa), i + 1)
2843483Ssaidi@eecs.umich.edu
2853483Ssaidi@eecs.umich.edu    code('''
2863483Ssaidi@eecs.umich.edu
2873053Sstever@eecs.umich.edu#define THE_ISA ${{define(target_isa)}}
2883053Sstever@eecs.umich.edu#define TheISA ${{namespace(target_isa)}}
2893053Sstever@eecs.umich.edu
2903053Sstever@eecs.umich.edu#endif // __CONFIG_THE_ISA_HH__''')
2913053Sstever@eecs.umich.edu
2923053Sstever@eecs.umich.edu    code.write(str(target[0]))
2933053Sstever@eecs.umich.edu
2943053Sstever@eecs.umich.eduenv.Command('config/the_isa.hh', map(Value, all_isa_list),
2951858SN/A            MakeAction(makeTheISA, Transform("CFG ISA", 0)))
2961858SN/A
2971858SN/A########################################################################
2981858SN/A#
2991858SN/A# Prevent any SimObjects from being added after this point, they
3001858SN/A# should all have been added in the SConscripts above
3011859SN/A#
3021858SN/ASimObject.fixed = True
3031858SN/A
3041858SN/Aclass DictImporter(object):
3051859SN/A    '''This importer takes a dictionary of arbitrary module names that
3061859SN/A    map to arbitrary filenames.'''
3071862SN/A    def __init__(self, modules):
3083053Sstever@eecs.umich.edu        self.modules = modules
3093053Sstever@eecs.umich.edu        self.installed = set()
3103053Sstever@eecs.umich.edu
3113053Sstever@eecs.umich.edu    def __del__(self):
3121859SN/A        self.unload()
3131859SN/A
3141859SN/A    def unload(self):
3151859SN/A        import sys
3161859SN/A        for module in self.installed:
3171859SN/A            del sys.modules[module]
3181859SN/A        self.installed = set()
3191859SN/A
3201862SN/A    def find_module(self, fullname, path):
3211859SN/A        if fullname == 'm5.defines':
3221859SN/A            return self
3231859SN/A
3241858SN/A        if fullname == 'm5.objects':
3251858SN/A            return self
3262139SN/A
3272139SN/A        if fullname.startswith('m5.internal'):
3282139SN/A            return None
3292155SN/A
3302623SN/A        source = self.modules.get(fullname, None)
3313583Sbinkertn@umich.edu        if source is not None and fullname.startswith('m5.objects'):
3323583Sbinkertn@umich.edu            return self
3333583Sbinkertn@umich.edu
3343583Sbinkertn@umich.edu        return None
3352155SN/A
3361869SN/A    def load_module(self, fullname):
3371869SN/A        mod = imp.new_module(fullname)
3381869SN/A        sys.modules[fullname] = mod
3391869SN/A        self.installed.add(fullname)
3401869SN/A
3412139SN/A        mod.__loader__ = self
3421869SN/A        if fullname == 'm5.objects':
3432508SN/A            mod.__path__ = fullname.split('.')
3442508SN/A            return mod
3452508SN/A
3462508SN/A        if fullname == 'm5.defines':
3473685Sktlim@umich.edu            mod.__dict__['buildEnv'] = m5.util.SmartDict(build_env)
3482635Sstever@eecs.umich.edu            return mod
3491869SN/A
3501869SN/A        source = self.modules[fullname]
3511869SN/A        if source.modname == '__init__':
3521869SN/A            mod.__path__ = source.modpath
3531869SN/A        mod.__file__ = source.abspath
3541869SN/A
3551869SN/A        exec file(source.abspath, 'r') in mod.__dict__
3561869SN/A
3571965SN/A        return mod
3581965SN/A
3591965SN/Aimport m5.SimObject
3601869SN/Aimport m5.params
3611869SN/Afrom m5.util import code_formatter
3622733Sktlim@umich.edu
3631869SN/Am5.SimObject.clear()
3641884SN/Am5.params.clear()
3651884SN/A
3663356Sbinkertn@umich.edu# install the python importer so we can grab stuff from the source
3673356Sbinkertn@umich.edu# tree itself.  We can't have SimObjects added after this point or
3683356Sbinkertn@umich.edu# else we won't know about them for the rest of the stuff.
3693356Sbinkertn@umich.eduimporter = DictImporter(PySource.modules)
3701869SN/Asys.meta_path[0:0] = [ importer ]
3711858SN/A
3721869SN/A# import all sim objects so we can populate the all_objects list
3731869SN/A# make sure that we're working with a list, then let's sort it
3741869SN/Afor modname in SimObject.modnames:
3751869SN/A    exec('from m5.objects import %s' % modname)
3761869SN/A
3771858SN/A# we need to unload all of the currently imported modules so that they
3782761Sstever@eecs.umich.edu# will be re-imported the next time the sconscript is run
3791869SN/Aimporter.unload()
3802733Sktlim@umich.edusys.meta_path.remove(importer)
3813584Ssaidi@eecs.umich.edu
3821869SN/Asim_objects = m5.SimObject.allClasses
3831869SN/Aall_enums = m5.params.allEnums
3841869SN/A
3851869SN/Aall_params = {}
3861869SN/Afor name,obj in sorted(sim_objects.iteritems()):
3871869SN/A    for param in obj._params.local.values():
3881858SN/A        # load the ptype attribute now because it depends on the
389955SN/A        # current version of SimObject.allClasses, but when scons
390955SN/A        # actually uses the value, all versions of
3911869SN/A        # SimObject.allClasses will have been loaded
3921869SN/A        param.ptype
3931869SN/A
3941869SN/A        if not hasattr(param, 'swig_decl'):
3951869SN/A            continue
3961869SN/A        pname = param.ptype_str
3971869SN/A        if pname not in all_params:
3981869SN/A            all_params[pname] = param
3991869SN/A
4001869SN/A########################################################################
4011869SN/A#
4021869SN/A# calculate extra dependencies
4031869SN/A#
4041869SN/Amodule_depends = ["m5", "m5.SimObject", "m5.params"]
4051869SN/Adepends = [ PySource.modules[dep].snode for dep in module_depends ]
4061869SN/A
4071869SN/A########################################################################
4081869SN/A#
4091869SN/A# Commands for the basic automatically generated python files
4101869SN/A#
4111869SN/A
4121869SN/A# Generate Python file containing a dict specifying the current
4131869SN/A# buildEnv flags.
4141869SN/Adef makeDefinesPyFile(target, source, env):
4151869SN/A    build_env = source[0].get_contents()
4161869SN/A
4171869SN/A    code = code_formatter()
4181869SN/A    code("""
4191869SN/Aimport m5.internal
4201869SN/Aimport m5.util
4213356Sbinkertn@umich.edu
4223356Sbinkertn@umich.edubuildEnv = m5.util.SmartDict($build_env)
4233356Sbinkertn@umich.edu
4243356Sbinkertn@umich.educompileDate = m5.internal.core.compileDate
4253356Sbinkertn@umich.edu_globals = globals()
4263356Sbinkertn@umich.edufor key,val in m5.internal.core.__dict__.iteritems():
4273356Sbinkertn@umich.edu    if key.startswith('flag_'):
4281869SN/A        flag = key[5:]
4291869SN/A        _globals[flag] = val
4301869SN/Adel _globals
4311869SN/A""")
4321869SN/A    code.write(target[0].abspath)
4331869SN/A
4341869SN/Adefines_info = Value(build_env)
4352655Sstever@eecs.umich.edu# Generate a file with all of the compile options in it
4362655Sstever@eecs.umich.eduenv.Command('python/m5/defines.py', defines_info,
4372655Sstever@eecs.umich.edu            MakeAction(makeDefinesPyFile, Transform("DEFINES", 0)))
4382655Sstever@eecs.umich.eduPySource('m5', 'python/m5/defines.py')
4392655Sstever@eecs.umich.edu
4402655Sstever@eecs.umich.edu# Generate python file containing info about the M5 source code
4412655Sstever@eecs.umich.edudef makeInfoPyFile(target, source, env):
4422655Sstever@eecs.umich.edu    code = code_formatter()
4432655Sstever@eecs.umich.edu    for src in source:
4442655Sstever@eecs.umich.edu        data = ''.join(file(src.srcnode().abspath, 'r').xreadlines())
4452655Sstever@eecs.umich.edu        code('$src = ${{repr(data)}}')
4462655Sstever@eecs.umich.edu    code.write(str(target[0]))
4472655Sstever@eecs.umich.edu
4482655Sstever@eecs.umich.edu# Generate a file that wraps the basic top level files
4492655Sstever@eecs.umich.eduenv.Command('python/m5/info.py',
4502655Sstever@eecs.umich.edu            [ '#/AUTHORS', '#/LICENSE', '#/README', ],
4512655Sstever@eecs.umich.edu            MakeAction(makeInfoPyFile, Transform("INFO")))
4522655Sstever@eecs.umich.eduPySource('m5', 'python/m5/info.py')
4532655Sstever@eecs.umich.edu
4542655Sstever@eecs.umich.edu########################################################################
4552655Sstever@eecs.umich.edu#
4562655Sstever@eecs.umich.edu# Create all of the SimObject param headers and enum headers
4572655Sstever@eecs.umich.edu#
4582655Sstever@eecs.umich.edu
4592655Sstever@eecs.umich.edudef createSimObjectParam(target, source, env):
4602655Sstever@eecs.umich.edu    assert len(target) == 1 and len(source) == 1
4612634Sstever@eecs.umich.edu
4622634Sstever@eecs.umich.edu    name = str(source[0].get_contents())
4632634Sstever@eecs.umich.edu    obj = sim_objects[name]
4642634Sstever@eecs.umich.edu
4652634Sstever@eecs.umich.edu    code = code_formatter()
4662634Sstever@eecs.umich.edu    obj.cxx_decl(code)
4672638Sstever@eecs.umich.edu    code.write(target[0].abspath)
4682638Sstever@eecs.umich.edu
4692638Sstever@eecs.umich.edudef createSwigParam(target, source, env):
4702638Sstever@eecs.umich.edu    assert len(target) == 1 and len(source) == 1
4712638Sstever@eecs.umich.edu
4721869SN/A    name = str(source[0].get_contents())
4731869SN/A    param = all_params[name]
4743546Sgblack@eecs.umich.edu
4753546Sgblack@eecs.umich.edu    code = code_formatter()
4763546Sgblack@eecs.umich.edu    code('%module(package="m5.internal") $0_${name}', param.file_ext)
4773546Sgblack@eecs.umich.edu    param.swig_decl(code)
4783546Sgblack@eecs.umich.edu    code.write(target[0].abspath)
4793546Sgblack@eecs.umich.edu
4803546Sgblack@eecs.umich.edudef createEnumStrings(target, source, env):
4813546Sgblack@eecs.umich.edu    assert len(target) == 1 and len(source) == 1
4823546Sgblack@eecs.umich.edu
4833546Sgblack@eecs.umich.edu    name = str(source[0].get_contents())
4843546Sgblack@eecs.umich.edu    obj = all_enums[name]
4853546Sgblack@eecs.umich.edu
4863546Sgblack@eecs.umich.edu    code = code_formatter()
4873546Sgblack@eecs.umich.edu    obj.cxx_def(code)
4883546Sgblack@eecs.umich.edu    code.write(target[0].abspath)
4893546Sgblack@eecs.umich.edu
4903546Sgblack@eecs.umich.edudef createEnumParam(target, source, env):
4913546Sgblack@eecs.umich.edu    assert len(target) == 1 and len(source) == 1
4923546Sgblack@eecs.umich.edu
4933546Sgblack@eecs.umich.edu    name = str(source[0].get_contents())
4943546Sgblack@eecs.umich.edu    obj = all_enums[name]
4953546Sgblack@eecs.umich.edu
4963546Sgblack@eecs.umich.edu    code = code_formatter()
4973546Sgblack@eecs.umich.edu    obj.cxx_decl(code)
4983546Sgblack@eecs.umich.edu    code.write(target[0].abspath)
4993546Sgblack@eecs.umich.edu
5003546Sgblack@eecs.umich.edudef createEnumSwig(target, source, env):
5013546Sgblack@eecs.umich.edu    assert len(target) == 1 and len(source) == 1
5023546Sgblack@eecs.umich.edu
5033546Sgblack@eecs.umich.edu    name = str(source[0].get_contents())
5043546Sgblack@eecs.umich.edu    obj = all_enums[name]
5053546Sgblack@eecs.umich.edu
5063546Sgblack@eecs.umich.edu    code = code_formatter()
5073546Sgblack@eecs.umich.edu    code('''\
5083546Sgblack@eecs.umich.edu%module(package="m5.internal") enum_$name
5093546Sgblack@eecs.umich.edu
5103546Sgblack@eecs.umich.edu%{
5113546Sgblack@eecs.umich.edu#include "enums/$name.hh"
5123546Sgblack@eecs.umich.edu%}
5133546Sgblack@eecs.umich.edu
514955SN/A%include "enums/$name.hh"
515955SN/A''')
516955SN/A    code.write(target[0].abspath)
517955SN/A
5181858SN/A# Generate all of the SimObject param struct header files
5191858SN/Aparams_hh_files = []
5201858SN/Afor name,simobj in sorted(sim_objects.iteritems()):
5212632Sstever@eecs.umich.edu    py_source = PySource.modules[simobj.__module__]
5222632Sstever@eecs.umich.edu    extra_deps = [ py_source.tnode ]
5232632Sstever@eecs.umich.edu
5242632Sstever@eecs.umich.edu    hh_file = File('params/%s.hh' % name)
5252632Sstever@eecs.umich.edu    params_hh_files.append(hh_file)
5262634Sstever@eecs.umich.edu    env.Command(hh_file, Value(name),
5272638Sstever@eecs.umich.edu                MakeAction(createSimObjectParam, Transform("SO PARAM")))
5282023SN/A    env.Depends(hh_file, depends + extra_deps)
5292632Sstever@eecs.umich.edu
5302632Sstever@eecs.umich.edu# Generate any parameter header files needed
5312632Sstever@eecs.umich.eduparams_i_files = []
5322632Sstever@eecs.umich.edufor name,param in all_params.iteritems():
5332632Sstever@eecs.umich.edu    i_file = File('python/m5/internal/%s_%s.i' % (param.file_ext, name))
5342632Sstever@eecs.umich.edu    params_i_files.append(i_file)
5352632Sstever@eecs.umich.edu    env.Command(i_file, Value(name),
5362632Sstever@eecs.umich.edu                MakeAction(createSwigParam, Transform("SW PARAM")))
5372632Sstever@eecs.umich.edu    env.Depends(i_file, depends)
5382632Sstever@eecs.umich.edu    SwigSource('m5.internal', i_file)
5392632Sstever@eecs.umich.edu
5402023SN/A# Generate all enum header files
5412632Sstever@eecs.umich.edufor name,enum in sorted(all_enums.iteritems()):
5422632Sstever@eecs.umich.edu    py_source = PySource.modules[enum.__module__]
5431889SN/A    extra_deps = [ py_source.tnode ]
5441889SN/A
5452632Sstever@eecs.umich.edu    cc_file = File('enums/%s.cc' % name)
5462632Sstever@eecs.umich.edu    env.Command(cc_file, Value(name),
5472632Sstever@eecs.umich.edu                MakeAction(createEnumStrings, Transform("ENUM STR")))
5482632Sstever@eecs.umich.edu    env.Depends(cc_file, depends + extra_deps)
5492632Sstever@eecs.umich.edu    Source(cc_file)
5502632Sstever@eecs.umich.edu
5512632Sstever@eecs.umich.edu    hh_file = File('enums/%s.hh' % name)
5522632Sstever@eecs.umich.edu    env.Command(hh_file, Value(name),
5532632Sstever@eecs.umich.edu                MakeAction(createEnumParam, Transform("EN PARAM")))
5542632Sstever@eecs.umich.edu    env.Depends(hh_file, depends + extra_deps)
5552632Sstever@eecs.umich.edu
5562632Sstever@eecs.umich.edu    i_file = File('python/m5/internal/enum_%s.i' % name)
5572632Sstever@eecs.umich.edu    env.Command(i_file, Value(name),
5582632Sstever@eecs.umich.edu                MakeAction(createEnumSwig, Transform("ENUMSWIG")))
5591888SN/A    env.Depends(i_file, depends + extra_deps)
5601888SN/A    SwigSource('m5.internal', i_file)
5611869SN/A
5621869SN/Adef buildParam(target, source, env):
5631858SN/A    name = source[0].get_contents()
5642598SN/A    obj = sim_objects[name]
5652598SN/A    class_path = obj.cxx_class.split('::')
5662598SN/A    classname = class_path[-1]
5672598SN/A    namespaces = class_path[:-1]
5682598SN/A    params = obj._params.local.values()
5691858SN/A
5701858SN/A    code = code_formatter()
5711858SN/A
5721858SN/A    code('%module(package="m5.internal") param_$name')
5731858SN/A    code()
5741858SN/A    code('%{')
5751858SN/A    code('#include "params/$obj.hh"')
5761858SN/A    for param in params:
5771858SN/A        param.cxx_predecls(code)
5781871SN/A    code('%}')
5791858SN/A    code()
5801858SN/A
5811858SN/A    for param in params:
5821858SN/A        param.swig_predecls(code)
5831858SN/A
5841858SN/A    code()
5851858SN/A    if obj._base:
5861858SN/A        code('%import "python/m5/internal/param_${{obj._base}}.i"')
5871858SN/A    code()
5881858SN/A    obj.swig_objdecls(code)
5891858SN/A    code()
5901859SN/A
5911859SN/A    code('%include "params/$obj.hh"')
5921869SN/A
5931888SN/A    code.write(target[0].abspath)
5942632Sstever@eecs.umich.edu
5951869SN/Afor name in sim_objects.iterkeys():
5961884SN/A    params_file = File('python/m5/internal/param_%s.i' % name)
5971884SN/A    env.Command(params_file, Value(name),
5981884SN/A                MakeAction(buildParam, Transform("BLDPARAM")))
5991884SN/A    env.Depends(params_file, depends)
6001884SN/A    SwigSource('m5.internal', params_file)
6011884SN/A
6021965SN/A# Generate the main swig init file
6031965SN/Adef makeEmbeddedSwigInit(target, source, env):
6041965SN/A    code = code_formatter()
6052761Sstever@eecs.umich.edu    module = source[0].get_contents()
6061869SN/A    code('''\
6071869SN/A#include "sim/init.hh"
6082632Sstever@eecs.umich.edu
6092667Sstever@eecs.umich.eduextern "C" {
6101869SN/A    void init_${module}();
6111869SN/A}
6122929Sktlim@umich.edu
6132929Sktlim@umich.eduEmbeddedSwig embed_swig_${module}(init_${module});
6143036Sstever@eecs.umich.edu''')
6152929Sktlim@umich.edu    code.write(str(target[0]))
616955SN/A    
6172598SN/A# Build all swig modules
6182598SN/Afor swig in SwigSource.all:
6193546Sgblack@eecs.umich.edu    env.Command([swig.cc_source.tnode, swig.py_source.tnode], swig.tnode,
620955SN/A                MakeAction('$SWIG $SWIGFLAGS -outdir ${TARGETS[1].dir} '
621955SN/A                '-o ${TARGETS[0]} $SOURCES', Transform("SWIG")))
622955SN/A    init_file = 'python/swig/init_%s.cc' % swig.module
6231530SN/A    env.Command(init_file, Value(swig.module),
624955SN/A                MakeAction(makeEmbeddedSwigInit, Transform("EMBED SW")))
625955SN/A    Source(init_file)
626955SN/A
627#
628# Handle debug flags
629#
630def makeDebugFlagCC(target, source, env):
631    assert(len(target) == 1 and len(source) == 1)
632
633    val = eval(source[0].get_contents())
634    name, compound, desc = val
635    compound = list(sorted(compound))
636
637    code = code_formatter()
638
639    # file header
640    code('''
641/*
642 * DO NOT EDIT THIS FILE! Automatically generated
643 */
644
645#include "base/debug.hh"
646''')
647
648    for flag in compound:
649        code('#include "debug/$flag.hh"')
650    code()
651    code('namespace Debug {')
652    code()
653
654    if not compound:
655        code('SimpleFlag $name("$name", "$desc");')
656    else:
657        code('CompoundFlag $name("$name", "$desc",')
658        code.indent()
659        last = len(compound) - 1
660        for i,flag in enumerate(compound):
661            if i != last:
662                code('$flag,')
663            else:
664                code('$flag);')
665        code.dedent()
666
667    code()
668    code('} // namespace Debug')
669
670    code.write(str(target[0]))
671
672def makeDebugFlagHH(target, source, env):
673    assert(len(target) == 1 and len(source) == 1)
674
675    val = eval(source[0].get_contents())
676    name, compound, desc = val
677
678    code = code_formatter()
679
680    # file header boilerplate
681    code('''\
682/*
683 * DO NOT EDIT THIS FILE!
684 *
685 * Automatically generated by SCons
686 */
687
688#ifndef __DEBUG_${name}_HH__
689#define __DEBUG_${name}_HH__
690
691namespace Debug {
692''')
693
694    if compound:
695        code('class CompoundFlag;')
696    code('class SimpleFlag;')
697
698    if compound:
699        code('extern CompoundFlag $name;')
700        for flag in compound:
701            code('extern SimpleFlag $flag;')
702    else:
703        code('extern SimpleFlag $name;')
704
705    code('''
706}
707
708#endif // __DEBUG_${name}_HH__
709''')
710
711    code.write(str(target[0]))
712
713for name,flag in sorted(debug_flags.iteritems()):
714    n, compound, desc = flag
715    assert n == name
716
717    env.Command('debug/%s.hh' % name, Value(flag),
718                MakeAction(makeDebugFlagHH, Transform("TRACING", 0)))
719    env.Command('debug/%s.cc' % name, Value(flag),
720                MakeAction(makeDebugFlagCC, Transform("TRACING", 0)))
721    Source('debug/%s.cc' % name)
722
723# Embed python files.  All .py files that have been indicated by a
724# PySource() call in a SConscript need to be embedded into the M5
725# library.  To do that, we compile the file to byte code, marshal the
726# byte code, compress it, and then generate a c++ file that
727# inserts the result into an array.
728def embedPyFile(target, source, env):
729    def c_str(string):
730        if string is None:
731            return "0"
732        return '"%s"' % string
733
734    '''Action function to compile a .py into a code object, marshal
735    it, compress it, and stick it into an asm file so the code appears
736    as just bytes with a label in the data section'''
737
738    src = file(str(source[0]), 'r').read()
739
740    pysource = PySource.tnodes[source[0]]
741    compiled = compile(src, pysource.abspath, 'exec')
742    marshalled = marshal.dumps(compiled)
743    compressed = zlib.compress(marshalled)
744    data = compressed
745    sym = pysource.symname
746
747    code = code_formatter()
748    code('''\
749#include "sim/init.hh"
750
751namespace {
752
753const char data_${sym}[] = {
754''')
755    code.indent()
756    step = 16
757    for i in xrange(0, len(data), step):
758        x = array.array('B', data[i:i+step])
759        code(''.join('%d,' % d for d in x))
760    code.dedent()
761    
762    code('''};
763
764EmbeddedPython embedded_${sym}(
765    ${{c_str(pysource.arcname)}},
766    ${{c_str(pysource.abspath)}},
767    ${{c_str(pysource.modpath)}},
768    data_${sym},
769    ${{len(data)}},
770    ${{len(marshalled)}});
771
772} // anonymous namespace
773''')
774    code.write(str(target[0]))
775
776for source in PySource.all:
777    env.Command(source.cpp, source.tnode, 
778                MakeAction(embedPyFile, Transform("EMBED PY")))
779    Source(source.cpp)
780
781########################################################################
782#
783# Define binaries.  Each different build type (debug, opt, etc.) gets
784# a slightly different build environment.
785#
786
787# List of constructed environments to pass back to SConstruct
788envList = []
789
790date_source = Source('base/date.cc', skip_lib=True)
791
792# Function to create a new build environment as clone of current
793# environment 'env' with modified object suffix and optional stripped
794# binary.  Additional keyword arguments are appended to corresponding
795# build environment vars.
796def makeEnv(label, objsfx, strip = False, **kwargs):
797    # SCons doesn't know to append a library suffix when there is a '.' in the
798    # name.  Use '_' instead.
799    libname = 'm5_' + label
800    exename = 'm5.' + label
801
802    new_env = env.Clone(OBJSUFFIX=objsfx, SHOBJSUFFIX=objsfx + 's')
803    new_env.Label = label
804    new_env.Append(**kwargs)
805
806    swig_env = new_env.Clone()
807    swig_env.Append(CCFLAGS='-Werror')
808    if env['GCC']:
809        swig_env.Append(CCFLAGS='-Wno-uninitialized')
810        swig_env.Append(CCFLAGS='-Wno-sign-compare')
811        swig_env.Append(CCFLAGS='-Wno-parentheses')
812
813    werror_env = new_env.Clone()
814    werror_env.Append(CCFLAGS='-Werror')
815
816    def make_obj(source, static, extra_deps = None):
817        '''This function adds the specified source to the correct
818        build environment, and returns the corresponding SCons Object
819        nodes'''
820
821        if source.swig:
822            env = swig_env
823        elif source.Werror:
824            env = werror_env
825        else:
826            env = new_env
827
828        if static:
829            obj = env.StaticObject(source.tnode)
830        else:
831            obj = env.SharedObject(source.tnode)
832
833        if extra_deps:
834            env.Depends(obj, extra_deps)
835
836        return obj
837
838    static_objs = [ make_obj(s, True) for s in Source.get(skip_lib=False)]
839    shared_objs = [ make_obj(s, False) for s in Source.get(skip_lib=False)]
840
841    static_date = make_obj(date_source, static=True, extra_deps=static_objs)
842    static_objs.append(static_date)
843    
844    shared_date = make_obj(date_source, static=False, extra_deps=shared_objs)
845    shared_objs.append(shared_date)
846
847    # First make a library of everything but main() so other programs can
848    # link against m5.
849    static_lib = new_env.StaticLibrary(libname, static_objs)
850    shared_lib = new_env.SharedLibrary(libname, shared_objs)
851
852    for target, sources in unit_tests:
853        objs = [ make_obj(s, static=True) for s in sources ]
854        new_env.Program("unittest/%s.%s" % (target, label), objs + static_objs)
855
856    # Now link a stub with main() and the static library.
857    bin_objs = [make_obj(s, True) for s in Source.get(bin_only=True) ]
858    progname = exename
859    if strip:
860        progname += '.unstripped'
861
862    targets = new_env.Program(progname, bin_objs + static_objs)
863
864    if strip:
865        if sys.platform == 'sunos5':
866            cmd = 'cp $SOURCE $TARGET; strip $TARGET'
867        else:
868            cmd = 'strip $SOURCE -o $TARGET'
869        targets = new_env.Command(exename, progname,
870                    MakeAction(cmd, Transform("STRIP")))
871            
872    new_env.M5Binary = targets[0]
873    envList.append(new_env)
874
875# Debug binary
876ccflags = {}
877if env['GCC']:
878    if sys.platform == 'sunos5':
879        ccflags['debug'] = '-gstabs+'
880    else:
881        ccflags['debug'] = '-ggdb3'
882    ccflags['opt'] = '-g -O3'
883    ccflags['fast'] = '-O3'
884    ccflags['prof'] = '-O3 -g -pg'
885elif env['SUNCC']:
886    ccflags['debug'] = '-g0'
887    ccflags['opt'] = '-g -O'
888    ccflags['fast'] = '-fast'
889    ccflags['prof'] = '-fast -g -pg'
890elif env['ICC']:
891    ccflags['debug'] = '-g -O0'
892    ccflags['opt'] = '-g -O'
893    ccflags['fast'] = '-fast'
894    ccflags['prof'] = '-fast -g -pg'
895else:
896    print 'Unknown compiler, please fix compiler options'
897    Exit(1)
898
899makeEnv('debug', '.do',
900        CCFLAGS = Split(ccflags['debug']),
901        CPPDEFINES = ['DEBUG', 'TRACING_ON=1'])
902
903# Optimized binary
904makeEnv('opt', '.o',
905        CCFLAGS = Split(ccflags['opt']),
906        CPPDEFINES = ['TRACING_ON=1'])
907
908# "Fast" binary
909makeEnv('fast', '.fo', strip = True,
910        CCFLAGS = Split(ccflags['fast']),
911        CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'])
912
913# Profiled binary
914makeEnv('prof', '.po',
915        CCFLAGS = Split(ccflags['prof']),
916        CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
917        LINKFLAGS = '-pg')
918
919Return('envList')
920