SConscript revision 10454
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
331608SN/Aimport imp
34955SN/Aimport marshal
35955SN/Aimport os
36955SN/Aimport re
37955SN/Aimport sys
38955SN/Aimport zlib
39955SN/A
40955SN/Afrom os.path import basename, dirname, exists, isdir, isfile, join as joinpath
41955SN/A
42955SN/Aimport SCons
43955SN/A
44955SN/A# This file defines how to build a particular configuration of gem5
45955SN/A# based on variable settings in the 'env' build environment.
46955SN/A
47955SN/AImport('*')
482023SN/A
49955SN/A# Children need to see the environment
50955SN/AExport('env')
51955SN/A
52955SN/Abuild_env = [(opt, env[opt]) for opt in export_vars]
53955SN/A
54955SN/Afrom m5.util import code_formatter, compareVersions
55955SN/A
56955SN/A########################################################################
57955SN/A# Code for adding source files of various types
581031SN/A#
59955SN/A# When specifying a source file of some type, a set of guards can be
601388SN/A# specified for that file.  When get() is used to find the files, if
61955SN/A# get specifies a set of filters, only files that match those filters
62955SN/A# will be accepted (unspecified filters on files are assumed to be
631296SN/A# false).  Current filters are:
64955SN/A#     main -- specifies the gem5 main() function
652609SN/A#     skip_lib -- do not put this file into the gem5 library
66955SN/A#     skip_no_python -- do not put this file into a no_python library
67955SN/A#       as it embeds compiled Python
68955SN/A#     <unittest> -- unit tests use filters based on the unit test name
69955SN/A#
70955SN/A# A parent can now be specified for a source file and default filter
71955SN/A# values will be retrieved recursively from parents (children override
72955SN/A# parents).
73955SN/A#
74955SN/Aclass SourceMeta(type):
75955SN/A    '''Meta class for source files that keeps track of all files of a
76955SN/A    particular type and has a get function for finding all functions
77955SN/A    of a certain type that match a set of guards'''
78955SN/A    def __init__(cls, name, bases, dict):
79955SN/A        super(SourceMeta, cls).__init__(name, bases, dict)
80955SN/A        cls.all = []
81955SN/A        
82955SN/A    def get(cls, **guards):
83955SN/A        '''Find all files that match the specified guards.  If a source
841717SN/A        file does not specify a flag, the default is False'''
852190SN/A        for src in cls.all:
862652Ssaidi@eecs.umich.edu            for flag,value in guards.iteritems():
87955SN/A                # if the flag is found and has a different value, skip
882410SN/A                # this file
89955SN/A                if src.all_guards.get(flag, False) != value:
90955SN/A                    break
911717SN/A            else:
922568SN/A                yield src
932568SN/A
942568SN/Aclass SourceFile(object):
952499SN/A    '''Base object that encapsulates the notion of a source file.
962462SN/A    This includes, the source node, target node, various manipulations
972568SN/A    of those.  A source file also specifies a set of guards which
982395SN/A    describing which builds the source file applies to.  A parent can
992405SN/A    also be specified to get default guards from'''
100955SN/A    __metaclass__ = SourceMeta
101955SN/A    def __init__(self, source, parent=None, **guards):
102955SN/A        self.guards = guards
103955SN/A        self.parent = parent
104955SN/A
1052090SN/A        tnode = source
106955SN/A        if not isinstance(source, SCons.Node.FS.File):
107955SN/A            tnode = File(source)
108955SN/A
1091696SN/A        self.tnode = tnode
110955SN/A        self.snode = tnode.srcnode()
111955SN/A
112955SN/A        for base in type(self).__mro__:
113955SN/A            if issubclass(base, SourceFile):
1141127SN/A                base.all.append(self)
115955SN/A
116955SN/A    @property
1172379SN/A    def filename(self):
118955SN/A        return str(self.tnode)
119955SN/A
120955SN/A    @property
1212155SN/A    def dirname(self):
1222155SN/A        return dirname(self.filename)
1232155SN/A
1242155SN/A    @property
1252155SN/A    def basename(self):
1262155SN/A        return basename(self.filename)
1272155SN/A
1282155SN/A    @property
1292155SN/A    def extname(self):
1302155SN/A        index = self.basename.rfind('.')
1312155SN/A        if index <= 0:
1322155SN/A            # dot files aren't extensions
1332155SN/A            return self.basename, None
1342155SN/A
1352155SN/A        return self.basename[:index], self.basename[index+1:]
1362155SN/A
1372155SN/A    @property
1382155SN/A    def all_guards(self):
1392155SN/A        '''find all guards for this object getting default values
1402155SN/A        recursively from its parents'''
1412155SN/A        guards = {}
1422155SN/A        if self.parent:
1432155SN/A            guards.update(self.parent.guards)
1442155SN/A        guards.update(self.guards)
1452155SN/A        return guards
1462155SN/A
1472155SN/A    def __lt__(self, other): return self.filename < other.filename
1482155SN/A    def __le__(self, other): return self.filename <= other.filename
1492155SN/A    def __gt__(self, other): return self.filename > other.filename
1502155SN/A    def __ge__(self, other): return self.filename >= other.filename
1512155SN/A    def __eq__(self, other): return self.filename == other.filename
1522155SN/A    def __ne__(self, other): return self.filename != other.filename
1532155SN/A
1542155SN/A    @staticmethod
1552155SN/A    def done():
1562155SN/A        def disabled(cls, name, *ignored):
1572155SN/A            raise RuntimeError("Additional SourceFile '%s'" % name,\
1582155SN/A                  "declared, but targets deps are already fixed.")
1592155SN/A        SourceFile.__init__ = disabled
1602422SN/A
1612422SN/A
1622422SN/Aclass Source(SourceFile):
1632422SN/A    '''Add a c/c++ source file to the build'''
1642422SN/A    def __init__(self, source, Werror=True, swig=False, **guards):
1652422SN/A        '''specify the source file, and any guards'''
1662422SN/A        super(Source, self).__init__(source, **guards)
1672397SN/A
1682397SN/A        self.Werror = Werror
1692422SN/A        self.swig = swig
1702422SN/A
171955SN/Aclass PySource(SourceFile):
172955SN/A    '''Add a python source file to the named package'''
173955SN/A    invalid_sym_char = re.compile('[^A-z0-9_]')
174955SN/A    modules = {}
175955SN/A    tnodes = {}
176955SN/A    symnames = {}
177955SN/A
178955SN/A    def __init__(self, package, source, **guards):
1791078SN/A        '''specify the python package, the source file, and any guards'''
180955SN/A        super(PySource, self).__init__(source, **guards)
181955SN/A
182955SN/A        modname,ext = self.extname
183955SN/A        assert ext == 'py'
1841917SN/A
185955SN/A        if package:
186955SN/A            path = package.split('.')
187955SN/A        else:
188955SN/A            path = []
189974SN/A
190955SN/A        modpath = path[:]
191955SN/A        if modname != '__init__':
192955SN/A            modpath += [ modname ]
193955SN/A        modpath = '.'.join(modpath)
1942566SN/A
1952566SN/A        arcpath = path + [ self.basename ]
196955SN/A        abspath = self.snode.abspath
197955SN/A        if not exists(abspath):
1982539SN/A            abspath = self.tnode.abspath
199955SN/A
200955SN/A        self.package = package
201955SN/A        self.modname = modname
2021817SN/A        self.modpath = modpath
2031154SN/A        self.arcname = joinpath(*arcpath)
2041840SN/A        self.abspath = abspath
2052522SN/A        self.compiled = File(self.filename + 'c')
2062522SN/A        self.cpp = File(self.filename + '.cc')
2072629SN/A        self.symname = PySource.invalid_sym_char.sub('_', modpath)
208955SN/A
209955SN/A        PySource.modules[modpath] = self
210955SN/A        PySource.tnodes[self.tnode] = self
2112539SN/A        PySource.symnames[self.symname] = self
212955SN/A
2132539SN/Aclass SimObject(PySource):
214955SN/A    '''Add a SimObject python file as a python source object and add
2151730SN/A    it to a list of sim object modules'''
216955SN/A
2171070SN/A    fixed = False
218955SN/A    modnames = []
219955SN/A
2202212SN/A    def __init__(self, source, **guards):
221955SN/A        '''Specify the source file and any guards (automatically in
2221040SN/A        the m5.objects package)'''
2232507SN/A        super(SimObject, self).__init__('m5.objects', source, **guards)
2242521SN/A        if self.fixed:
2252521SN/A            raise AttributeError, "Too late to call SimObject now."
2262507SN/A
2272507SN/A        bisect.insort_right(SimObject.modnames, self.modname)
2282507SN/A
2292521SN/Aclass SwigSource(SourceFile):
2302507SN/A    '''Add a swig file to build'''
2312507SN/A
232955SN/A    def __init__(self, package, source, **guards):
233955SN/A        '''Specify the python package, the source file, and any guards'''
234955SN/A        super(SwigSource, self).__init__(source, skip_no_python=True, **guards)
235955SN/A
236955SN/A        modname,ext = self.extname
237955SN/A        assert ext == 'i'
2381742SN/A
2391742SN/A        self.module = modname
2401742SN/A        cc_file = joinpath(self.dirname, modname + '_wrap.cc')
2411742SN/A        py_file = joinpath(self.dirname, modname + '.py')
2421742SN/A
2431742SN/A        self.cc_source = Source(cc_file, swig=True, parent=self, **guards)
2441742SN/A        self.py_source = PySource(package, py_file, parent=self, **guards)
2451742SN/A
2461742SN/Aclass ProtoBuf(SourceFile):
2471742SN/A    '''Add a Protocol Buffer to build'''
2481742SN/A
2491742SN/A    def __init__(self, source, **guards):
2501742SN/A        '''Specify the source file, and any guards'''
2511742SN/A        super(ProtoBuf, self).__init__(source, **guards)
2521742SN/A
2531742SN/A        # Get the file name and the extension
2541742SN/A        modname,ext = self.extname
2551742SN/A        assert ext == 'proto'
2561742SN/A
2571742SN/A        # Currently, we stick to generating the C++ headers, so we
258955SN/A        # only need to track the source and header.
259955SN/A        self.cc_file = File(modname + '.pb.cc')
2602520SN/A        self.hh_file = File(modname + '.pb.h')
2612517SN/A
2622253SN/Aclass UnitTest(object):
2632253SN/A    '''Create a UnitTest'''
2642253SN/A
2652253SN/A    all = []
2662553SN/A    def __init__(self, target, *sources, **kwargs):
2672553SN/A        '''Specify the target name and any sources.  Sources that are
2682553SN/A        not SourceFiles are evalued with Source().  All files are
2692553SN/A        guarded with a guard of the same name as the UnitTest
2702507SN/A        target.'''
2712470SN/A
2721744SN/A        srcs = []
2731744SN/A        for src in sources:
2742470SN/A            if not isinstance(src, SourceFile):
2752470SN/A                src = Source(src, skip_lib=True)
2762470SN/A            src.guards[target] = True
2772470SN/A            srcs.append(src)
2782470SN/A
2792470SN/A        self.sources = srcs
2802400SN/A        self.target = target
2812400SN/A        self.main = kwargs.get('main', False)
282955SN/A        UnitTest.all.append(self)
283955SN/A
2842037SN/A# Children should have access
2852037SN/AExport('Source')
2862037SN/AExport('PySource')
2872152SN/AExport('SimObject')
2882152SN/AExport('SwigSource')
2892139SN/AExport('ProtoBuf')
2902155SN/AExport('UnitTest')
2912155SN/A
2922155SN/A########################################################################
2932155SN/A#
2942155SN/A# Debug Flags
2952155SN/A#
2962155SN/Adebug_flags = {}
2972155SN/Adef DebugFlag(name, desc=None):
298955SN/A    if name in debug_flags:
2992155SN/A        raise AttributeError, "Flag %s already specified" % name
300955SN/A    debug_flags[name] = (name, (), desc)
301955SN/A
302955SN/Adef CompoundFlag(name, flags, desc=None):
3031742SN/A    if name in debug_flags:
3041742SN/A        raise AttributeError, "Flag %s already specified" % name
305955SN/A
306955SN/A    compound = tuple(flags)
307955SN/A    debug_flags[name] = (name, compound, desc)
3081858SN/A
309955SN/AExport('DebugFlag')
3101858SN/AExport('CompoundFlag')
3111858SN/A
3121858SN/A########################################################################
3131085SN/A#
314955SN/A# Set some compiler variables
315955SN/A#
316955SN/A
317955SN/A# Include file paths are rooted in this directory.  SCons will
318955SN/A# automatically expand '.' to refer to both the source directory and
319955SN/A# the corresponding build directory to pick up generated include
320955SN/A# files.
321955SN/Aenv.Append(CPPPATH=Dir('.'))
322955SN/A
323955SN/Afor extra_dir in extras_dir_list:
324955SN/A    env.Append(CPPPATH=Dir(extra_dir))
325955SN/A
3261511SN/A# Workaround for bug in SCons version > 0.97d20071212
3271045SN/A# Scons bug id: 2006 gem5 Bug id: 308
328955SN/Afor root, dirs, files in os.walk(base_dir, topdown=True):
329955SN/A    Dir(root[len(base_dir) + 1:])
330955SN/A
331955SN/A########################################################################
3321108SN/A#
333955SN/A# Walk the tree and execute all SConscripts in subdirectories
334955SN/A#
335955SN/A
336955SN/Ahere = Dir('.').srcnode().abspath
337955SN/Afor root, dirs, files in os.walk(base_dir, topdown=True):
338955SN/A    if root == here:
339955SN/A        # we don't want to recurse back into this SConscript
340955SN/A        continue
341955SN/A
342955SN/A    if 'SConscript' in files:
343955SN/A        build_dir = joinpath(env['BUILDDIR'], root[len(base_dir) + 1:])
344955SN/A        SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
345955SN/A
346955SN/Afor extra_dir in extras_dir_list:
347955SN/A    prefix_len = len(dirname(extra_dir)) + 1
348955SN/A
349955SN/A    # Also add the corresponding build directory to pick up generated
350955SN/A    # include files.
351955SN/A    env.Append(CPPPATH=Dir(joinpath(env['BUILDDIR'], extra_dir[prefix_len:])))
352955SN/A
353955SN/A    for root, dirs, files in os.walk(extra_dir, topdown=True):
354955SN/A        # if build lives in the extras directory, don't walk down it
3552655Sstever@eecs.umich.edu        if 'build' in dirs:
3562655Sstever@eecs.umich.edu            dirs.remove('build')
3572655Sstever@eecs.umich.edu
3582655Sstever@eecs.umich.edu        if 'SConscript' in files:
3592655Sstever@eecs.umich.edu            build_dir = joinpath(env['BUILDDIR'], root[prefix_len:])
3602655Sstever@eecs.umich.edu            SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
3612655Sstever@eecs.umich.edu
3622655Sstever@eecs.umich.edufor opt in export_vars:
3632655Sstever@eecs.umich.edu    env.ConfigFile(opt)
3642655Sstever@eecs.umich.edu
3652655Sstever@eecs.umich.edudef makeTheISA(source, target, env):
3662655Sstever@eecs.umich.edu    isas = [ src.get_contents() for src in source ]
3672655Sstever@eecs.umich.edu    target_isa = env['TARGET_ISA']
3682655Sstever@eecs.umich.edu    def define(isa):
3692655Sstever@eecs.umich.edu        return isa.upper() + '_ISA'
3702655Sstever@eecs.umich.edu    
3712655Sstever@eecs.umich.edu    def namespace(isa):
3722655Sstever@eecs.umich.edu        return isa[0].upper() + isa[1:].lower() + 'ISA' 
3732655Sstever@eecs.umich.edu
3742655Sstever@eecs.umich.edu
3752655Sstever@eecs.umich.edu    code = code_formatter()
3762655Sstever@eecs.umich.edu    code('''\
377955SN/A#ifndef __CONFIG_THE_ISA_HH__
3782655Sstever@eecs.umich.edu#define __CONFIG_THE_ISA_HH__
3792655Sstever@eecs.umich.edu
3802655Sstever@eecs.umich.edu''')
381955SN/A
382955SN/A    for i,isa in enumerate(isas):
3832655Sstever@eecs.umich.edu        code('#define $0 $1', define(isa), i + 1)
3842655Sstever@eecs.umich.edu
385955SN/A    code('''
386955SN/A
3872655Sstever@eecs.umich.edu#define THE_ISA ${{define(target_isa)}}
3882655Sstever@eecs.umich.edu#define TheISA ${{namespace(target_isa)}}
3892655Sstever@eecs.umich.edu#define THE_ISA_STR "${{target_isa}}"
390955SN/A
391955SN/A#endif // __CONFIG_THE_ISA_HH__''')
3922655Sstever@eecs.umich.edu
3932655Sstever@eecs.umich.edu    code.write(str(target[0]))
3942655Sstever@eecs.umich.edu
3951869SN/Aenv.Command('config/the_isa.hh', map(Value, all_isa_list),
3961869SN/A            MakeAction(makeTheISA, Transform("CFG ISA", 0)))
397
398########################################################################
399#
400# Prevent any SimObjects from being added after this point, they
401# should all have been added in the SConscripts above
402#
403SimObject.fixed = True
404
405class DictImporter(object):
406    '''This importer takes a dictionary of arbitrary module names that
407    map to arbitrary filenames.'''
408    def __init__(self, modules):
409        self.modules = modules
410        self.installed = set()
411
412    def __del__(self):
413        self.unload()
414
415    def unload(self):
416        import sys
417        for module in self.installed:
418            del sys.modules[module]
419        self.installed = set()
420
421    def find_module(self, fullname, path):
422        if fullname == 'm5.defines':
423            return self
424
425        if fullname == 'm5.objects':
426            return self
427
428        if fullname.startswith('m5.internal'):
429            return None
430
431        source = self.modules.get(fullname, None)
432        if source is not None and fullname.startswith('m5.objects'):
433            return self
434
435        return None
436
437    def load_module(self, fullname):
438        mod = imp.new_module(fullname)
439        sys.modules[fullname] = mod
440        self.installed.add(fullname)
441
442        mod.__loader__ = self
443        if fullname == 'm5.objects':
444            mod.__path__ = fullname.split('.')
445            return mod
446
447        if fullname == 'm5.defines':
448            mod.__dict__['buildEnv'] = m5.util.SmartDict(build_env)
449            return mod
450
451        source = self.modules[fullname]
452        if source.modname == '__init__':
453            mod.__path__ = source.modpath
454        mod.__file__ = source.abspath
455
456        exec file(source.abspath, 'r') in mod.__dict__
457
458        return mod
459
460import m5.SimObject
461import m5.params
462from m5.util import code_formatter
463
464m5.SimObject.clear()
465m5.params.clear()
466
467# install the python importer so we can grab stuff from the source
468# tree itself.  We can't have SimObjects added after this point or
469# else we won't know about them for the rest of the stuff.
470importer = DictImporter(PySource.modules)
471sys.meta_path[0:0] = [ importer ]
472
473# import all sim objects so we can populate the all_objects list
474# make sure that we're working with a list, then let's sort it
475for modname in SimObject.modnames:
476    exec('from m5.objects import %s' % modname)
477
478# we need to unload all of the currently imported modules so that they
479# will be re-imported the next time the sconscript is run
480importer.unload()
481sys.meta_path.remove(importer)
482
483sim_objects = m5.SimObject.allClasses
484all_enums = m5.params.allEnums
485
486if m5.SimObject.noCxxHeader:
487    print >> sys.stderr, \
488        "warning: At least one SimObject lacks a header specification. " \
489        "This can cause unexpected results in the generated SWIG " \
490        "wrappers."
491
492# Find param types that need to be explicitly wrapped with swig.
493# These will be recognized because the ParamDesc will have a
494# swig_decl() method.  Most param types are based on types that don't
495# need this, either because they're based on native types (like Int)
496# or because they're SimObjects (which get swigged independently).
497# For now the only things handled here are VectorParam types.
498params_to_swig = {}
499for name,obj in sorted(sim_objects.iteritems()):
500    for param in obj._params.local.values():
501        # load the ptype attribute now because it depends on the
502        # current version of SimObject.allClasses, but when scons
503        # actually uses the value, all versions of
504        # SimObject.allClasses will have been loaded
505        param.ptype
506
507        if not hasattr(param, 'swig_decl'):
508            continue
509        pname = param.ptype_str
510        if pname not in params_to_swig:
511            params_to_swig[pname] = param
512
513########################################################################
514#
515# calculate extra dependencies
516#
517module_depends = ["m5", "m5.SimObject", "m5.params"]
518depends = [ PySource.modules[dep].snode for dep in module_depends ]
519
520########################################################################
521#
522# Commands for the basic automatically generated python files
523#
524
525# Generate Python file containing a dict specifying the current
526# buildEnv flags.
527def makeDefinesPyFile(target, source, env):
528    build_env = source[0].get_contents()
529
530    code = code_formatter()
531    code("""
532import m5.internal
533import m5.util
534
535buildEnv = m5.util.SmartDict($build_env)
536
537compileDate = m5.internal.core.compileDate
538_globals = globals()
539for key,val in m5.internal.core.__dict__.iteritems():
540    if key.startswith('flag_'):
541        flag = key[5:]
542        _globals[flag] = val
543del _globals
544""")
545    code.write(target[0].abspath)
546
547defines_info = Value(build_env)
548# Generate a file with all of the compile options in it
549env.Command('python/m5/defines.py', defines_info,
550            MakeAction(makeDefinesPyFile, Transform("DEFINES", 0)))
551PySource('m5', 'python/m5/defines.py')
552
553# Generate python file containing info about the M5 source code
554def makeInfoPyFile(target, source, env):
555    code = code_formatter()
556    for src in source:
557        data = ''.join(file(src.srcnode().abspath, 'r').xreadlines())
558        code('$src = ${{repr(data)}}')
559    code.write(str(target[0]))
560
561# Generate a file that wraps the basic top level files
562env.Command('python/m5/info.py',
563            [ '#/COPYING', '#/LICENSE', '#/README', ],
564            MakeAction(makeInfoPyFile, Transform("INFO")))
565PySource('m5', 'python/m5/info.py')
566
567########################################################################
568#
569# Create all of the SimObject param headers and enum headers
570#
571
572def createSimObjectParamStruct(target, source, env):
573    assert len(target) == 1 and len(source) == 1
574
575    name = str(source[0].get_contents())
576    obj = sim_objects[name]
577
578    code = code_formatter()
579    obj.cxx_param_decl(code)
580    code.write(target[0].abspath)
581
582def createParamSwigWrapper(target, source, env):
583    assert len(target) == 1 and len(source) == 1
584
585    name = str(source[0].get_contents())
586    param = params_to_swig[name]
587
588    code = code_formatter()
589    param.swig_decl(code)
590    code.write(target[0].abspath)
591
592def createEnumStrings(target, source, env):
593    assert len(target) == 1 and len(source) == 1
594
595    name = str(source[0].get_contents())
596    obj = all_enums[name]
597
598    code = code_formatter()
599    obj.cxx_def(code)
600    code.write(target[0].abspath)
601
602def createEnumDecls(target, source, env):
603    assert len(target) == 1 and len(source) == 1
604
605    name = str(source[0].get_contents())
606    obj = all_enums[name]
607
608    code = code_formatter()
609    obj.cxx_decl(code)
610    code.write(target[0].abspath)
611
612def createEnumSwigWrapper(target, source, env):
613    assert len(target) == 1 and len(source) == 1
614
615    name = str(source[0].get_contents())
616    obj = all_enums[name]
617
618    code = code_formatter()
619    obj.swig_decl(code)
620    code.write(target[0].abspath)
621
622def createSimObjectSwigWrapper(target, source, env):
623    name = source[0].get_contents()
624    obj = sim_objects[name]
625
626    code = code_formatter()
627    obj.swig_decl(code)
628    code.write(target[0].abspath)
629
630# dummy target for generated code
631# we start out with all the Source files so they get copied to build/*/ also.
632SWIG = env.Dummy('swig', [s.tnode for s in Source.get()])
633
634# Generate all of the SimObject param C++ struct header files
635params_hh_files = []
636for name,simobj in sorted(sim_objects.iteritems()):
637    py_source = PySource.modules[simobj.__module__]
638    extra_deps = [ py_source.tnode ]
639
640    hh_file = File('params/%s.hh' % name)
641    params_hh_files.append(hh_file)
642    env.Command(hh_file, Value(name),
643                MakeAction(createSimObjectParamStruct, Transform("SO PARAM")))
644    env.Depends(hh_file, depends + extra_deps)
645    env.Depends(SWIG, hh_file)
646
647# Generate any needed param SWIG wrapper files
648params_i_files = []
649for name,param in params_to_swig.iteritems():
650    i_file = File('python/m5/internal/%s.i' % (param.swig_module_name()))
651    params_i_files.append(i_file)
652    env.Command(i_file, Value(name),
653                MakeAction(createParamSwigWrapper, Transform("SW PARAM")))
654    env.Depends(i_file, depends)
655    env.Depends(SWIG, i_file)
656    SwigSource('m5.internal', i_file)
657
658# Generate all enum header files
659for name,enum in sorted(all_enums.iteritems()):
660    py_source = PySource.modules[enum.__module__]
661    extra_deps = [ py_source.tnode ]
662
663    cc_file = File('enums/%s.cc' % name)
664    env.Command(cc_file, Value(name),
665                MakeAction(createEnumStrings, Transform("ENUM STR")))
666    env.Depends(cc_file, depends + extra_deps)
667    env.Depends(SWIG, cc_file)
668    Source(cc_file)
669
670    hh_file = File('enums/%s.hh' % name)
671    env.Command(hh_file, Value(name),
672                MakeAction(createEnumDecls, Transform("ENUMDECL")))
673    env.Depends(hh_file, depends + extra_deps)
674    env.Depends(SWIG, hh_file)
675
676    i_file = File('python/m5/internal/enum_%s.i' % name)
677    env.Command(i_file, Value(name),
678                MakeAction(createEnumSwigWrapper, Transform("ENUMSWIG")))
679    env.Depends(i_file, depends + extra_deps)
680    env.Depends(SWIG, i_file)
681    SwigSource('m5.internal', i_file)
682
683# Generate SimObject SWIG wrapper files
684for name,simobj in sim_objects.iteritems():
685    py_source = PySource.modules[simobj.__module__]
686    extra_deps = [ py_source.tnode ]
687
688    i_file = File('python/m5/internal/param_%s.i' % name)
689    env.Command(i_file, Value(name),
690                MakeAction(createSimObjectSwigWrapper, Transform("SO SWIG")))
691    env.Depends(i_file, depends + extra_deps)
692    SwigSource('m5.internal', i_file)
693
694# Generate the main swig init file
695def makeEmbeddedSwigInit(target, source, env):
696    code = code_formatter()
697    module = source[0].get_contents()
698    code('''\
699#include "sim/init.hh"
700
701extern "C" {
702    void init_${module}();
703}
704
705EmbeddedSwig embed_swig_${module}(init_${module});
706''')
707    code.write(str(target[0]))
708    
709# Build all swig modules
710for swig in SwigSource.all:
711    env.Command([swig.cc_source.tnode, swig.py_source.tnode], swig.tnode,
712                MakeAction('$SWIG $SWIGFLAGS -outdir ${TARGETS[1].dir} '
713                '-o ${TARGETS[0]} $SOURCES', Transform("SWIG")))
714    cc_file = str(swig.tnode)
715    init_file = '%s/%s_init.cc' % (dirname(cc_file), basename(cc_file))
716    env.Command(init_file, Value(swig.module),
717                MakeAction(makeEmbeddedSwigInit, Transform("EMBED SW")))
718    env.Depends(SWIG, init_file)
719    Source(init_file, **swig.guards)
720
721# Build all protocol buffers if we have got protoc and protobuf available
722if env['HAVE_PROTOBUF']:
723    for proto in ProtoBuf.all:
724        # Use both the source and header as the target, and the .proto
725        # file as the source. When executing the protoc compiler, also
726        # specify the proto_path to avoid having the generated files
727        # include the path.
728        env.Command([proto.cc_file, proto.hh_file], proto.tnode,
729                    MakeAction('$PROTOC --cpp_out ${TARGET.dir} '
730                               '--proto_path ${SOURCE.dir} $SOURCE',
731                               Transform("PROTOC")))
732
733        env.Depends(SWIG, [proto.cc_file, proto.hh_file])
734        # Add the C++ source file
735        Source(proto.cc_file, **proto.guards)
736elif ProtoBuf.all:
737    print 'Got protobuf to build, but lacks support!'
738    Exit(1)
739
740#
741# Handle debug flags
742#
743def makeDebugFlagCC(target, source, env):
744    assert(len(target) == 1 and len(source) == 1)
745
746    val = eval(source[0].get_contents())
747    name, compound, desc = val
748    compound = list(sorted(compound))
749
750    code = code_formatter()
751
752    # file header
753    code('''
754/*
755 * DO NOT EDIT THIS FILE! Automatically generated
756 */
757
758#include "base/debug.hh"
759''')
760
761    for flag in compound:
762        code('#include "debug/$flag.hh"')
763    code()
764    code('namespace Debug {')
765    code()
766
767    if not compound:
768        code('SimpleFlag $name("$name", "$desc");')
769    else:
770        code('CompoundFlag $name("$name", "$desc",')
771        code.indent()
772        last = len(compound) - 1
773        for i,flag in enumerate(compound):
774            if i != last:
775                code('$flag,')
776            else:
777                code('$flag);')
778        code.dedent()
779
780    code()
781    code('} // namespace Debug')
782
783    code.write(str(target[0]))
784
785def makeDebugFlagHH(target, source, env):
786    assert(len(target) == 1 and len(source) == 1)
787
788    val = eval(source[0].get_contents())
789    name, compound, desc = val
790
791    code = code_formatter()
792
793    # file header boilerplate
794    code('''\
795/*
796 * DO NOT EDIT THIS FILE!
797 *
798 * Automatically generated by SCons
799 */
800
801#ifndef __DEBUG_${name}_HH__
802#define __DEBUG_${name}_HH__
803
804namespace Debug {
805''')
806
807    if compound:
808        code('class CompoundFlag;')
809    code('class SimpleFlag;')
810
811    if compound:
812        code('extern CompoundFlag $name;')
813        for flag in compound:
814            code('extern SimpleFlag $flag;')
815    else:
816        code('extern SimpleFlag $name;')
817
818    code('''
819}
820
821#endif // __DEBUG_${name}_HH__
822''')
823
824    code.write(str(target[0]))
825
826for name,flag in sorted(debug_flags.iteritems()):
827    n, compound, desc = flag
828    assert n == name
829
830    hh_file = 'debug/%s.hh' % name
831    cc_file = 'debug/%s.cc' % name
832    env.Command(hh_file, Value(flag),
833                MakeAction(makeDebugFlagHH, Transform("TRACING", 0)))
834    env.Command(cc_file, Value(flag),
835                MakeAction(makeDebugFlagCC, Transform("TRACING", 0)))
836    env.Depends(SWIG, [hh_file, cc_file])
837    Source('debug/%s.cc' % name)
838
839# Embed python files.  All .py files that have been indicated by a
840# PySource() call in a SConscript need to be embedded into the M5
841# library.  To do that, we compile the file to byte code, marshal the
842# byte code, compress it, and then generate a c++ file that
843# inserts the result into an array.
844def embedPyFile(target, source, env):
845    def c_str(string):
846        if string is None:
847            return "0"
848        return '"%s"' % string
849
850    '''Action function to compile a .py into a code object, marshal
851    it, compress it, and stick it into an asm file so the code appears
852    as just bytes with a label in the data section'''
853
854    src = file(str(source[0]), 'r').read()
855
856    pysource = PySource.tnodes[source[0]]
857    compiled = compile(src, pysource.abspath, 'exec')
858    marshalled = marshal.dumps(compiled)
859    compressed = zlib.compress(marshalled)
860    data = compressed
861    sym = pysource.symname
862
863    code = code_formatter()
864    code('''\
865#include "sim/init.hh"
866
867namespace {
868
869const uint8_t data_${sym}[] = {
870''')
871    code.indent()
872    step = 16
873    for i in xrange(0, len(data), step):
874        x = array.array('B', data[i:i+step])
875        code(''.join('%d,' % d for d in x))
876    code.dedent()
877    
878    code('''};
879
880EmbeddedPython embedded_${sym}(
881    ${{c_str(pysource.arcname)}},
882    ${{c_str(pysource.abspath)}},
883    ${{c_str(pysource.modpath)}},
884    data_${sym},
885    ${{len(data)}},
886    ${{len(marshalled)}});
887
888} // anonymous namespace
889''')
890    code.write(str(target[0]))
891
892for source in PySource.all:
893    env.Command(source.cpp, source.tnode,
894                MakeAction(embedPyFile, Transform("EMBED PY")))
895    env.Depends(SWIG, source.cpp)
896    Source(source.cpp, skip_no_python=True)
897
898########################################################################
899#
900# Define binaries.  Each different build type (debug, opt, etc.) gets
901# a slightly different build environment.
902#
903
904# List of constructed environments to pass back to SConstruct
905date_source = Source('base/date.cc', skip_lib=True)
906
907# Capture this directory for the closure makeEnv, otherwise when it is
908# called, it won't know what directory it should use.
909variant_dir = Dir('.').path
910def variant(*path):
911    return os.path.join(variant_dir, *path)
912def variantd(*path):
913    return variant(*path)+'/'
914
915# Function to create a new build environment as clone of current
916# environment 'env' with modified object suffix and optional stripped
917# binary.  Additional keyword arguments are appended to corresponding
918# build environment vars.
919def makeEnv(env, label, objsfx, strip = False, **kwargs):
920    # SCons doesn't know to append a library suffix when there is a '.' in the
921    # name.  Use '_' instead.
922    libname = variant('gem5_' + label)
923    exename = variant('gem5.' + label)
924    secondary_exename = variant('m5.' + label)
925
926    new_env = env.Clone(OBJSUFFIX=objsfx, SHOBJSUFFIX=objsfx + 's')
927    new_env.Label = label
928    new_env.Append(**kwargs)
929
930    swig_env = new_env.Clone()
931
932    # Both gcc and clang have issues with unused labels and values in
933    # the SWIG generated code
934    swig_env.Append(CCFLAGS=['-Wno-unused-label', '-Wno-unused-value'])
935
936    # Add additional warnings here that should not be applied to
937    # the SWIG generated code
938    new_env.Append(CXXFLAGS='-Wmissing-declarations')
939
940    if env['GCC']:
941        # Depending on the SWIG version, we also need to supress
942        # warnings about uninitialized variables and missing field
943        # initializers.
944        swig_env.Append(CCFLAGS=['-Wno-uninitialized',
945                                 '-Wno-missing-field-initializers',
946                                 '-Wno-unused-but-set-variable'])
947
948        # If gcc supports it, also warn for deletion of derived
949        # classes with non-virtual desctructors. For gcc >= 4.7 we
950        # also have to disable warnings about the SWIG code having
951        # potentially uninitialized variables.
952        if compareVersions(env['GCC_VERSION'], '4.7') >= 0:
953            new_env.Append(CXXFLAGS='-Wdelete-non-virtual-dtor')
954            swig_env.Append(CCFLAGS='-Wno-maybe-uninitialized')
955    if env['CLANG']:
956        # Always enable the warning for deletion of derived classes
957        # with non-virtual destructors
958        new_env.Append(CXXFLAGS=['-Wdelete-non-virtual-dtor'])
959
960        swig_env.Append(CCFLAGS=[
961                # Some versions of SWIG can return uninitialized values
962                '-Wno-sometimes-uninitialized',
963                # Register storage is requested in a lot of places in
964                # SWIG-generated code.
965                '-Wno-deprecated-register',
966                ])
967
968    werror_env = new_env.Clone()
969    werror_env.Append(CCFLAGS='-Werror')
970
971    def make_obj(source, static, extra_deps = None):
972        '''This function adds the specified source to the correct
973        build environment, and returns the corresponding SCons Object
974        nodes'''
975
976        if source.swig:
977            env = swig_env
978        elif source.Werror:
979            env = werror_env
980        else:
981            env = new_env
982
983        if static:
984            obj = env.StaticObject(source.tnode)
985        else:
986            obj = env.SharedObject(source.tnode)
987
988        if extra_deps:
989            env.Depends(obj, extra_deps)
990
991        return obj
992
993    lib_guards = {'main': False, 'skip_lib': False}
994
995    # Without Python, leave out all SWIG and Python content from the
996    # library builds.  The option doesn't affect gem5 built as a program
997    if GetOption('without_python'):
998        lib_guards['skip_no_python'] = False
999
1000    static_objs = [ make_obj(s, True) for s in Source.get(**lib_guards) ]
1001    shared_objs = [ make_obj(s, False) for s in Source.get(**lib_guards) ]
1002
1003    static_date = make_obj(date_source, static=True, extra_deps=static_objs)
1004    static_objs.append(static_date)
1005
1006    shared_date = make_obj(date_source, static=False, extra_deps=shared_objs)
1007    shared_objs.append(shared_date)
1008
1009    # First make a library of everything but main() so other programs can
1010    # link against m5.
1011    static_lib = new_env.StaticLibrary(libname, static_objs)
1012    shared_lib = new_env.SharedLibrary(libname, shared_objs)
1013
1014    # Now link a stub with main() and the static library.
1015    main_objs = [ make_obj(s, True) for s in Source.get(main=True) ]
1016
1017    for test in UnitTest.all:
1018        flags = { test.target : True }
1019        test_sources = Source.get(**flags)
1020        test_objs = [ make_obj(s, static=True) for s in test_sources ]
1021        if test.main:
1022            test_objs += main_objs
1023        path = variant('unittest/%s.%s' % (test.target, label))
1024        new_env.Program(path, test_objs + static_objs)
1025
1026    progname = exename
1027    if strip:
1028        progname += '.unstripped'
1029
1030    targets = new_env.Program(progname, main_objs + static_objs)
1031
1032    if strip:
1033        if sys.platform == 'sunos5':
1034            cmd = 'cp $SOURCE $TARGET; strip $TARGET'
1035        else:
1036            cmd = 'strip $SOURCE -o $TARGET'
1037        targets = new_env.Command(exename, progname,
1038                    MakeAction(cmd, Transform("STRIP")))
1039
1040    new_env.Command(secondary_exename, exename,
1041            MakeAction('ln $SOURCE $TARGET', Transform("HARDLINK")))
1042
1043    new_env.M5Binary = targets[0]
1044    return new_env
1045
1046# Start out with the compiler flags common to all compilers,
1047# i.e. they all use -g for opt and -g -pg for prof
1048ccflags = {'debug' : [], 'opt' : ['-g'], 'fast' : [], 'prof' : ['-g', '-pg'],
1049           'perf' : ['-g']}
1050
1051# Start out with the linker flags common to all linkers, i.e. -pg for
1052# prof, and -lprofiler for perf. The -lprofile flag is surrounded by
1053# no-as-needed and as-needed as the binutils linker is too clever and
1054# simply doesn't link to the library otherwise.
1055ldflags = {'debug' : [], 'opt' : [], 'fast' : [], 'prof' : ['-pg'],
1056           'perf' : ['-Wl,--no-as-needed', '-lprofiler', '-Wl,--as-needed']}
1057
1058# For Link Time Optimization, the optimisation flags used to compile
1059# individual files are decoupled from those used at link time
1060# (i.e. you can compile with -O3 and perform LTO with -O0), so we need
1061# to also update the linker flags based on the target.
1062if env['GCC']:
1063    if sys.platform == 'sunos5':
1064        ccflags['debug'] += ['-gstabs+']
1065    else:
1066        ccflags['debug'] += ['-ggdb3']
1067    ldflags['debug'] += ['-O0']
1068    # opt, fast, prof and perf all share the same cc flags, also add
1069    # the optimization to the ldflags as LTO defers the optimization
1070    # to link time
1071    for target in ['opt', 'fast', 'prof', 'perf']:
1072        ccflags[target] += ['-O3']
1073        ldflags[target] += ['-O3']
1074
1075    ccflags['fast'] += env['LTO_CCFLAGS']
1076    ldflags['fast'] += env['LTO_LDFLAGS']
1077elif env['CLANG']:
1078    ccflags['debug'] += ['-g', '-O0']
1079    # opt, fast, prof and perf all share the same cc flags
1080    for target in ['opt', 'fast', 'prof', 'perf']:
1081        ccflags[target] += ['-O3']
1082else:
1083    print 'Unknown compiler, please fix compiler options'
1084    Exit(1)
1085
1086
1087# To speed things up, we only instantiate the build environments we
1088# need.  We try to identify the needed environment for each target; if
1089# we can't, we fall back on instantiating all the environments just to
1090# be safe.
1091target_types = ['debug', 'opt', 'fast', 'prof', 'perf']
1092obj2target = {'do': 'debug', 'o': 'opt', 'fo': 'fast', 'po': 'prof',
1093              'gpo' : 'perf'}
1094
1095def identifyTarget(t):
1096    ext = t.split('.')[-1]
1097    if ext in target_types:
1098        return ext
1099    if obj2target.has_key(ext):
1100        return obj2target[ext]
1101    match = re.search(r'/tests/([^/]+)/', t)
1102    if match and match.group(1) in target_types:
1103        return match.group(1)
1104    return 'all'
1105
1106needed_envs = [identifyTarget(target) for target in BUILD_TARGETS]
1107if 'all' in needed_envs:
1108    needed_envs += target_types
1109
1110gem5_root = Dir('.').up().up().abspath
1111def makeEnvirons(target, source, env):
1112    # cause any later Source() calls to be fatal, as a diagnostic.
1113    Source.done()
1114
1115    envList = []
1116
1117    # Debug binary
1118    if 'debug' in needed_envs:
1119        envList.append(
1120            makeEnv(env, 'debug', '.do',
1121                    CCFLAGS = Split(ccflags['debug']),
1122                    CPPDEFINES = ['DEBUG', 'TRACING_ON=1'],
1123                    LINKFLAGS = Split(ldflags['debug'])))
1124
1125    # Optimized binary
1126    if 'opt' in needed_envs:
1127        envList.append(
1128            makeEnv(env, 'opt', '.o',
1129                    CCFLAGS = Split(ccflags['opt']),
1130                    CPPDEFINES = ['TRACING_ON=1'],
1131                    LINKFLAGS = Split(ldflags['opt'])))
1132
1133    # "Fast" binary
1134    if 'fast' in needed_envs:
1135        envList.append(
1136            makeEnv(env, 'fast', '.fo', strip = True,
1137                    CCFLAGS = Split(ccflags['fast']),
1138                    CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1139                    LINKFLAGS = Split(ldflags['fast'])))
1140
1141    # Profiled binary using gprof
1142    if 'prof' in needed_envs:
1143        envList.append(
1144            makeEnv(env, 'prof', '.po',
1145                    CCFLAGS = Split(ccflags['prof']),
1146                    CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1147                    LINKFLAGS = Split(ldflags['prof'])))
1148
1149    # Profiled binary using google-pprof
1150    if 'perf' in needed_envs:
1151        envList.append(
1152            makeEnv(env, 'perf', '.gpo',
1153                    CCFLAGS = Split(ccflags['perf']),
1154                    CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1155                    LINKFLAGS = Split(ldflags['perf'])))
1156
1157    # Set up the regression tests for each build.
1158    for e in envList:
1159        SConscript(os.path.join(gem5_root, 'tests', 'SConscript'),
1160                   variant_dir = variantd('tests', e.Label),
1161                   exports = { 'env' : e }, duplicate = False)
1162
1163# The MakeEnvirons Builder defers the full dependency collection until
1164# after processing the ISA definition (due to dynamically generated
1165# source files).  Add this dependency to all targets so they will wait
1166# until the environments are completely set up.  Otherwise, a second
1167# process (e.g. -j2 or higher) will try to compile the requested target,
1168# not know how, and fail.
1169env.Append(BUILDERS = {'MakeEnvirons' :
1170                        Builder(action=MakeAction(makeEnvirons,
1171                                                  Transform("ENVIRONS", 1)))})
1172
1173isa_target = env['PHONY_BASE'] + '-deps'
1174environs   = env['PHONY_BASE'] + '-environs'
1175env.Depends('#all-deps',     isa_target)
1176env.Depends('#all-environs', environs)
1177env.ScanISA(isa_target, File('arch/%s/generated/inc.d' % env['TARGET_ISA']))
1178envSetup = env.MakeEnvirons(environs, isa_target)
1179
1180# make sure no -deps targets occur before all ISAs are complete
1181env.Depends(isa_target, '#all-isas')
1182# likewise for -environs targets and all the -deps targets
1183env.Depends(environs, '#all-deps')
1184