SConscript revision 9048
1955SN/A# -*- mode:python -*-
2955SN/A
31762SN/A# Copyright (c) 2004-2005 The Regents of The University of Michigan
4955SN/A# All rights reserved.
5955SN/A#
6955SN/A# Redistribution and use in source and binary forms, with or without
7955SN/A# modification, are permitted provided that the following conditions are
8955SN/A# met: redistributions of source code must retain the above copyright
9955SN/A# notice, this list of conditions and the following disclaimer;
10955SN/A# redistributions in binary form must reproduce the above copyright
11955SN/A# notice, this list of conditions and the following disclaimer in the
12955SN/A# documentation and/or other materials provided with the distribution;
13955SN/A# neither the name of the copyright holders nor the names of its
14955SN/A# contributors may be used to endorse or promote products derived from
15955SN/A# this software without specific prior written permission.
16955SN/A#
17955SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18955SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19955SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20955SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21955SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22955SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23955SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24955SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25955SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26955SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27955SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
282665Ssaidi@eecs.umich.edu#
294762Snate@binkert.org# Authors: Nathan Binkert
30955SN/A
315522Snate@binkert.orgimport array
326143Snate@binkert.orgimport bisect
334762Snate@binkert.orgimport imp
345522Snate@binkert.orgimport marshal
35955SN/Aimport os
365522Snate@binkert.orgimport re
3711974Sgabeblack@google.comimport sys
38955SN/Aimport zlib
395522Snate@binkert.org
404202Sbinkertn@umich.edufrom os.path import basename, dirname, exists, isdir, isfile, join as joinpath
415742Snate@binkert.org
42955SN/Aimport SCons
434381Sbinkertn@umich.edu
444381Sbinkertn@umich.edu# This file defines how to build a particular configuration of gem5
4512246Sgabeblack@google.com# based on variable settings in the 'env' build environment.
4612246Sgabeblack@google.com
478334Snate@binkert.orgImport('*')
48955SN/A
49955SN/A# Children need to see the environment
504202Sbinkertn@umich.eduExport('env')
51955SN/A
524382Sbinkertn@umich.edubuild_env = [(opt, env[opt]) for opt in export_vars]
534382Sbinkertn@umich.edu
544382Sbinkertn@umich.edufrom m5.util import code_formatter, compareVersions
556654Snate@binkert.org
565517Snate@binkert.org########################################################################
578614Sgblack@eecs.umich.edu# Code for adding source files of various types
587674Snate@binkert.org#
596143Snate@binkert.org# When specifying a source file of some type, a set of guards can be
606143Snate@binkert.org# specified for that file.  When get() is used to find the files, if
616143Snate@binkert.org# get specifies a set of filters, only files that match those filters
6212302Sgabeblack@google.com# will be accepted (unspecified filters on files are assumed to be
6312302Sgabeblack@google.com# false).  Current filters are:
6412302Sgabeblack@google.com#     main -- specifies the gem5 main() function
6512302Sgabeblack@google.com#     skip_lib -- do not put this file into the gem5 library
6612302Sgabeblack@google.com#     <unittest> -- unit tests use filters based on the unit test name
6712302Sgabeblack@google.com#
6812302Sgabeblack@google.com# A parent can now be specified for a source file and default filter
6912302Sgabeblack@google.com# values will be retrieved recursively from parents (children override
7012302Sgabeblack@google.com# parents).
7112302Sgabeblack@google.com#
7212302Sgabeblack@google.comclass SourceMeta(type):
7312302Sgabeblack@google.com    '''Meta class for source files that keeps track of all files of a
7412302Sgabeblack@google.com    particular type and has a get function for finding all functions
7512302Sgabeblack@google.com    of a certain type that match a set of guards'''
7612302Sgabeblack@google.com    def __init__(cls, name, bases, dict):
7712302Sgabeblack@google.com        super(SourceMeta, cls).__init__(name, bases, dict)
7812302Sgabeblack@google.com        cls.all = []
7912302Sgabeblack@google.com        
8012302Sgabeblack@google.com    def get(cls, **guards):
8112302Sgabeblack@google.com        '''Find all files that match the specified guards.  If a source
8212302Sgabeblack@google.com        file does not specify a flag, the default is False'''
8312302Sgabeblack@google.com        for src in cls.all:
8412302Sgabeblack@google.com            for flag,value in guards.iteritems():
8512302Sgabeblack@google.com                # if the flag is found and has a different value, skip
8612302Sgabeblack@google.com                # this file
8712302Sgabeblack@google.com                if src.all_guards.get(flag, False) != value:
8812302Sgabeblack@google.com                    break
8912302Sgabeblack@google.com            else:
9012302Sgabeblack@google.com                yield src
9111983Sgabeblack@google.com
926143Snate@binkert.orgclass SourceFile(object):
938233Snate@binkert.org    '''Base object that encapsulates the notion of a source file.
9412302Sgabeblack@google.com    This includes, the source node, target node, various manipulations
956143Snate@binkert.org    of those.  A source file also specifies a set of guards which
966143Snate@binkert.org    describing which builds the source file applies to.  A parent can
9712302Sgabeblack@google.com    also be specified to get default guards from'''
984762Snate@binkert.org    __metaclass__ = SourceMeta
996143Snate@binkert.org    def __init__(self, source, parent=None, **guards):
1008233Snate@binkert.org        self.guards = guards
1018233Snate@binkert.org        self.parent = parent
10212302Sgabeblack@google.com
10312302Sgabeblack@google.com        tnode = source
1046143Snate@binkert.org        if not isinstance(source, SCons.Node.FS.File):
10512302Sgabeblack@google.com            tnode = File(source)
10612302Sgabeblack@google.com
10712302Sgabeblack@google.com        self.tnode = tnode
10812302Sgabeblack@google.com        self.snode = tnode.srcnode()
10912302Sgabeblack@google.com
11012302Sgabeblack@google.com        for base in type(self).__mro__:
11112302Sgabeblack@google.com            if issubclass(base, SourceFile):
11212302Sgabeblack@google.com                base.all.append(self)
11312302Sgabeblack@google.com
11412302Sgabeblack@google.com    @property
1158233Snate@binkert.org    def filename(self):
1166143Snate@binkert.org        return str(self.tnode)
1176143Snate@binkert.org
1186143Snate@binkert.org    @property
1196143Snate@binkert.org    def dirname(self):
1206143Snate@binkert.org        return dirname(self.filename)
1216143Snate@binkert.org
1226143Snate@binkert.org    @property
1236143Snate@binkert.org    def basename(self):
1246143Snate@binkert.org        return basename(self.filename)
1257065Snate@binkert.org
1266143Snate@binkert.org    @property
1278233Snate@binkert.org    def extname(self):
1288233Snate@binkert.org        index = self.basename.rfind('.')
1298233Snate@binkert.org        if index <= 0:
1308233Snate@binkert.org            # dot files aren't extensions
1318233Snate@binkert.org            return self.basename, None
1328233Snate@binkert.org
1338233Snate@binkert.org        return self.basename[:index], self.basename[index+1:]
1348233Snate@binkert.org
1358233Snate@binkert.org    @property
1368233Snate@binkert.org    def all_guards(self):
1378233Snate@binkert.org        '''find all guards for this object getting default values
1388233Snate@binkert.org        recursively from its parents'''
1398233Snate@binkert.org        guards = {}
1408233Snate@binkert.org        if self.parent:
1418233Snate@binkert.org            guards.update(self.parent.guards)
1428233Snate@binkert.org        guards.update(self.guards)
1438233Snate@binkert.org        return guards
1448233Snate@binkert.org
1458233Snate@binkert.org    def __lt__(self, other): return self.filename < other.filename
1468233Snate@binkert.org    def __le__(self, other): return self.filename <= other.filename
1478233Snate@binkert.org    def __gt__(self, other): return self.filename > other.filename
1486143Snate@binkert.org    def __ge__(self, other): return self.filename >= other.filename
1496143Snate@binkert.org    def __eq__(self, other): return self.filename == other.filename
1506143Snate@binkert.org    def __ne__(self, other): return self.filename != other.filename
1516143Snate@binkert.org        
1526143Snate@binkert.orgclass Source(SourceFile):
1536143Snate@binkert.org    '''Add a c/c++ source file to the build'''
1549982Satgutier@umich.edu    def __init__(self, source, Werror=True, swig=False, **guards):
1556143Snate@binkert.org        '''specify the source file, and any guards'''
15612302Sgabeblack@google.com        super(Source, self).__init__(source, **guards)
15712302Sgabeblack@google.com
15812302Sgabeblack@google.com        self.Werror = Werror
15912302Sgabeblack@google.com        self.swig = swig
16012302Sgabeblack@google.com
16112302Sgabeblack@google.comclass PySource(SourceFile):
16212302Sgabeblack@google.com    '''Add a python source file to the named package'''
16312302Sgabeblack@google.com    invalid_sym_char = re.compile('[^A-z0-9_]')
16411983Sgabeblack@google.com    modules = {}
16511983Sgabeblack@google.com    tnodes = {}
16611983Sgabeblack@google.com    symnames = {}
16712302Sgabeblack@google.com    
16812302Sgabeblack@google.com    def __init__(self, package, source, **guards):
16912302Sgabeblack@google.com        '''specify the python package, the source file, and any guards'''
17012302Sgabeblack@google.com        super(PySource, self).__init__(source, **guards)
17112302Sgabeblack@google.com
17212302Sgabeblack@google.com        modname,ext = self.extname
17311983Sgabeblack@google.com        assert ext == 'py'
1746143Snate@binkert.org
17512305Sgabeblack@google.com        if package:
17612302Sgabeblack@google.com            path = package.split('.')
17712302Sgabeblack@google.com        else:
17812302Sgabeblack@google.com            path = []
1796143Snate@binkert.org
1806143Snate@binkert.org        modpath = path[:]
1816143Snate@binkert.org        if modname != '__init__':
1825522Snate@binkert.org            modpath += [ modname ]
1836143Snate@binkert.org        modpath = '.'.join(modpath)
1846143Snate@binkert.org
1856143Snate@binkert.org        arcpath = path + [ self.basename ]
1869982Satgutier@umich.edu        abspath = self.snode.abspath
18712302Sgabeblack@google.com        if not exists(abspath):
18812302Sgabeblack@google.com            abspath = self.tnode.abspath
18912302Sgabeblack@google.com
1906143Snate@binkert.org        self.package = package
1916143Snate@binkert.org        self.modname = modname
1926143Snate@binkert.org        self.modpath = modpath
1936143Snate@binkert.org        self.arcname = joinpath(*arcpath)
1945522Snate@binkert.org        self.abspath = abspath
1955522Snate@binkert.org        self.compiled = File(self.filename + 'c')
1965522Snate@binkert.org        self.cpp = File(self.filename + '.cc')
1975522Snate@binkert.org        self.symname = PySource.invalid_sym_char.sub('_', modpath)
1985604Snate@binkert.org
1995604Snate@binkert.org        PySource.modules[modpath] = self
2006143Snate@binkert.org        PySource.tnodes[self.tnode] = self
2016143Snate@binkert.org        PySource.symnames[self.symname] = self
2024762Snate@binkert.org
2034762Snate@binkert.orgclass SimObject(PySource):
2046143Snate@binkert.org    '''Add a SimObject python file as a python source object and add
2056727Ssteve.reinhardt@amd.com    it to a list of sim object modules'''
2066727Ssteve.reinhardt@amd.com
2076727Ssteve.reinhardt@amd.com    fixed = False
2084762Snate@binkert.org    modnames = []
2096143Snate@binkert.org
2106143Snate@binkert.org    def __init__(self, source, **guards):
2116143Snate@binkert.org        '''Specify the source file and any guards (automatically in
2126143Snate@binkert.org        the m5.objects package)'''
2136727Ssteve.reinhardt@amd.com        super(SimObject, self).__init__('m5.objects', source, **guards)
2146143Snate@binkert.org        if self.fixed:
2157674Snate@binkert.org            raise AttributeError, "Too late to call SimObject now."
2167674Snate@binkert.org
2175604Snate@binkert.org        bisect.insort_right(SimObject.modnames, self.modname)
2186143Snate@binkert.org
2196143Snate@binkert.orgclass SwigSource(SourceFile):
2206143Snate@binkert.org    '''Add a swig file to build'''
2214762Snate@binkert.org
2226143Snate@binkert.org    def __init__(self, package, source, **guards):
2234762Snate@binkert.org        '''Specify the python package, the source file, and any guards'''
2244762Snate@binkert.org        super(SwigSource, self).__init__(source, **guards)
2254762Snate@binkert.org
2266143Snate@binkert.org        modname,ext = self.extname
2276143Snate@binkert.org        assert ext == 'i'
2284762Snate@binkert.org
22912302Sgabeblack@google.com        self.module = modname
23012302Sgabeblack@google.com        cc_file = joinpath(self.dirname, modname + '_wrap.cc')
2318233Snate@binkert.org        py_file = joinpath(self.dirname, modname + '.py')
23212302Sgabeblack@google.com
2336143Snate@binkert.org        self.cc_source = Source(cc_file, swig=True, parent=self)
2346143Snate@binkert.org        self.py_source = PySource(package, py_file, parent=self)
2354762Snate@binkert.org
2366143Snate@binkert.orgclass UnitTest(object):
2374762Snate@binkert.org    '''Create a UnitTest'''
2389396Sandreas.hansson@arm.com
2399396Sandreas.hansson@arm.com    all = []
2409396Sandreas.hansson@arm.com    def __init__(self, target, *sources, **kwargs):
24112302Sgabeblack@google.com        '''Specify the target name and any sources.  Sources that are
24212302Sgabeblack@google.com        not SourceFiles are evalued with Source().  All files are
24312302Sgabeblack@google.com        guarded with a guard of the same name as the UnitTest
2449396Sandreas.hansson@arm.com        target.'''
2459396Sandreas.hansson@arm.com
2469396Sandreas.hansson@arm.com        srcs = []
2479396Sandreas.hansson@arm.com        for src in sources:
2489396Sandreas.hansson@arm.com            if not isinstance(src, SourceFile):
2499396Sandreas.hansson@arm.com                src = Source(src, skip_lib=True)
2509396Sandreas.hansson@arm.com            src.guards[target] = True
2519930Sandreas.hansson@arm.com            srcs.append(src)
2529930Sandreas.hansson@arm.com
2539396Sandreas.hansson@arm.com        self.sources = srcs
2548235Snate@binkert.org        self.target = target
2558235Snate@binkert.org        self.main = kwargs.get('main', False)
2566143Snate@binkert.org        UnitTest.all.append(self)
2578235Snate@binkert.org
2589003SAli.Saidi@ARM.com# Children should have access
2598235Snate@binkert.orgExport('Source')
2608235Snate@binkert.orgExport('PySource')
26112302Sgabeblack@google.comExport('SimObject')
2628235Snate@binkert.orgExport('SwigSource')
26312302Sgabeblack@google.comExport('UnitTest')
2648235Snate@binkert.org
2658235Snate@binkert.org########################################################################
26612302Sgabeblack@google.com#
2678235Snate@binkert.org# Debug Flags
2688235Snate@binkert.org#
2698235Snate@binkert.orgdebug_flags = {}
2708235Snate@binkert.orgdef DebugFlag(name, desc=None):
2719003SAli.Saidi@ARM.com    if name in debug_flags:
27212313Sgabeblack@google.com        raise AttributeError, "Flag %s already specified" % name
27312313Sgabeblack@google.com    debug_flags[name] = (name, (), desc)
27412313Sgabeblack@google.com
27512313Sgabeblack@google.comdef CompoundFlag(name, flags, desc=None):
27612313Sgabeblack@google.com    if name in debug_flags:
27712313Sgabeblack@google.com        raise AttributeError, "Flag %s already specified" % name
2785584Snate@binkert.org
2794382Sbinkertn@umich.edu    compound = tuple(flags)
2804202Sbinkertn@umich.edu    debug_flags[name] = (name, compound, desc)
2814382Sbinkertn@umich.edu
2824382Sbinkertn@umich.eduExport('DebugFlag')
2839396Sandreas.hansson@arm.comExport('CompoundFlag')
2845584Snate@binkert.org
28512313Sgabeblack@google.com########################################################################
2864382Sbinkertn@umich.edu#
2874382Sbinkertn@umich.edu# Set some compiler variables
2884382Sbinkertn@umich.edu#
2898232Snate@binkert.org
2905192Ssaidi@eecs.umich.edu# Include file paths are rooted in this directory.  SCons will
2918232Snate@binkert.org# automatically expand '.' to refer to both the source directory and
2928232Snate@binkert.org# the corresponding build directory to pick up generated include
2938232Snate@binkert.org# files.
2945192Ssaidi@eecs.umich.eduenv.Append(CPPPATH=Dir('.'))
2958232Snate@binkert.org
2965192Ssaidi@eecs.umich.edufor extra_dir in extras_dir_list:
2975799Snate@binkert.org    env.Append(CPPPATH=Dir(extra_dir))
2988232Snate@binkert.org
2995192Ssaidi@eecs.umich.edu# Workaround for bug in SCons version > 0.97d20071212
3005192Ssaidi@eecs.umich.edu# Scons bug id: 2006 gem5 Bug id: 308
3015192Ssaidi@eecs.umich.edufor root, dirs, files in os.walk(base_dir, topdown=True):
3028232Snate@binkert.org    Dir(root[len(base_dir) + 1:])
3035192Ssaidi@eecs.umich.edu
3048232Snate@binkert.org########################################################################
3055192Ssaidi@eecs.umich.edu#
3065192Ssaidi@eecs.umich.edu# Walk the tree and execute all SConscripts in subdirectories
3075192Ssaidi@eecs.umich.edu#
3085192Ssaidi@eecs.umich.edu
3094382Sbinkertn@umich.eduhere = Dir('.').srcnode().abspath
3104382Sbinkertn@umich.edufor root, dirs, files in os.walk(base_dir, topdown=True):
3114382Sbinkertn@umich.edu    if root == here:
3122667Sstever@eecs.umich.edu        # we don't want to recurse back into this SConscript
3132667Sstever@eecs.umich.edu        continue
3142667Sstever@eecs.umich.edu
3152667Sstever@eecs.umich.edu    if 'SConscript' in files:
3162667Sstever@eecs.umich.edu        build_dir = joinpath(env['BUILDDIR'], root[len(base_dir) + 1:])
3172667Sstever@eecs.umich.edu        SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
3185742Snate@binkert.org
3195742Snate@binkert.orgfor extra_dir in extras_dir_list:
3205742Snate@binkert.org    prefix_len = len(dirname(extra_dir)) + 1
3215793Snate@binkert.org    for root, dirs, files in os.walk(extra_dir, topdown=True):
3228334Snate@binkert.org        # if build lives in the extras directory, don't walk down it
3235793Snate@binkert.org        if 'build' in dirs:
3245793Snate@binkert.org            dirs.remove('build')
3255793Snate@binkert.org
3264382Sbinkertn@umich.edu        if 'SConscript' in files:
3274762Snate@binkert.org            build_dir = joinpath(env['BUILDDIR'], root[prefix_len:])
3285344Sstever@gmail.com            SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
3294382Sbinkertn@umich.edu
3305341Sstever@gmail.comfor opt in export_vars:
3315742Snate@binkert.org    env.ConfigFile(opt)
3325742Snate@binkert.org
3335742Snate@binkert.orgdef makeTheISA(source, target, env):
3345742Snate@binkert.org    isas = [ src.get_contents() for src in source ]
3355742Snate@binkert.org    target_isa = env['TARGET_ISA']
3364762Snate@binkert.org    def define(isa):
3375742Snate@binkert.org        return isa.upper() + '_ISA'
3385742Snate@binkert.org    
33911984Sgabeblack@google.com    def namespace(isa):
3407722Sgblack@eecs.umich.edu        return isa[0].upper() + isa[1:].lower() + 'ISA' 
3415742Snate@binkert.org
3425742Snate@binkert.org
3435742Snate@binkert.org    code = code_formatter()
3449930Sandreas.hansson@arm.com    code('''\
3459930Sandreas.hansson@arm.com#ifndef __CONFIG_THE_ISA_HH__
3469930Sandreas.hansson@arm.com#define __CONFIG_THE_ISA_HH__
3479930Sandreas.hansson@arm.com
3489930Sandreas.hansson@arm.com''')
3495742Snate@binkert.org
3508242Sbradley.danofsky@amd.com    for i,isa in enumerate(isas):
3518242Sbradley.danofsky@amd.com        code('#define $0 $1', define(isa), i + 1)
3528242Sbradley.danofsky@amd.com
3538242Sbradley.danofsky@amd.com    code('''
3545341Sstever@gmail.com
3555742Snate@binkert.org#define THE_ISA ${{define(target_isa)}}
3567722Sgblack@eecs.umich.edu#define TheISA ${{namespace(target_isa)}}
3574773Snate@binkert.org#define THE_ISA_STR "${{target_isa}}"
3586108Snate@binkert.org
3591858SN/A#endif // __CONFIG_THE_ISA_HH__''')
3601085SN/A
3616658Snate@binkert.org    code.write(str(target[0]))
3626658Snate@binkert.org
3637673Snate@binkert.orgenv.Command('config/the_isa.hh', map(Value, all_isa_list),
3646658Snate@binkert.org            MakeAction(makeTheISA, Transform("CFG ISA", 0)))
3656658Snate@binkert.org
36611308Santhony.gutierrez@amd.com########################################################################
3676658Snate@binkert.org#
36811308Santhony.gutierrez@amd.com# Prevent any SimObjects from being added after this point, they
3696658Snate@binkert.org# should all have been added in the SConscripts above
3706658Snate@binkert.org#
3717673Snate@binkert.orgSimObject.fixed = True
3727673Snate@binkert.org
3737673Snate@binkert.orgclass DictImporter(object):
3747673Snate@binkert.org    '''This importer takes a dictionary of arbitrary module names that
3757673Snate@binkert.org    map to arbitrary filenames.'''
3767673Snate@binkert.org    def __init__(self, modules):
3777673Snate@binkert.org        self.modules = modules
37810467Sandreas.hansson@arm.com        self.installed = set()
3796658Snate@binkert.org
3807673Snate@binkert.org    def __del__(self):
38110467Sandreas.hansson@arm.com        self.unload()
38210467Sandreas.hansson@arm.com
38310467Sandreas.hansson@arm.com    def unload(self):
38410467Sandreas.hansson@arm.com        import sys
38510467Sandreas.hansson@arm.com        for module in self.installed:
38610467Sandreas.hansson@arm.com            del sys.modules[module]
38710467Sandreas.hansson@arm.com        self.installed = set()
38810467Sandreas.hansson@arm.com
38910467Sandreas.hansson@arm.com    def find_module(self, fullname, path):
39010467Sandreas.hansson@arm.com        if fullname == 'm5.defines':
39110467Sandreas.hansson@arm.com            return self
3927673Snate@binkert.org
3937673Snate@binkert.org        if fullname == 'm5.objects':
3947673Snate@binkert.org            return self
3957673Snate@binkert.org
3967673Snate@binkert.org        if fullname.startswith('m5.internal'):
3979048SAli.Saidi@ARM.com            return None
3987673Snate@binkert.org
3997673Snate@binkert.org        source = self.modules.get(fullname, None)
4007673Snate@binkert.org        if source is not None and fullname.startswith('m5.objects'):
4017673Snate@binkert.org            return self
4026658Snate@binkert.org
4037756SAli.Saidi@ARM.com        return None
4047816Ssteve.reinhardt@amd.com
4056658Snate@binkert.org    def load_module(self, fullname):
40611308Santhony.gutierrez@amd.com        mod = imp.new_module(fullname)
40711308Santhony.gutierrez@amd.com        sys.modules[fullname] = mod
40811308Santhony.gutierrez@amd.com        self.installed.add(fullname)
40911308Santhony.gutierrez@amd.com
41011308Santhony.gutierrez@amd.com        mod.__loader__ = self
41111308Santhony.gutierrez@amd.com        if fullname == 'm5.objects':
41211308Santhony.gutierrez@amd.com            mod.__path__ = fullname.split('.')
41311308Santhony.gutierrez@amd.com            return mod
41411308Santhony.gutierrez@amd.com
41511308Santhony.gutierrez@amd.com        if fullname == 'm5.defines':
41611308Santhony.gutierrez@amd.com            mod.__dict__['buildEnv'] = m5.util.SmartDict(build_env)
41711308Santhony.gutierrez@amd.com            return mod
41811308Santhony.gutierrez@amd.com
41911308Santhony.gutierrez@amd.com        source = self.modules[fullname]
42011308Santhony.gutierrez@amd.com        if source.modname == '__init__':
42111308Santhony.gutierrez@amd.com            mod.__path__ = source.modpath
42211308Santhony.gutierrez@amd.com        mod.__file__ = source.abspath
42311308Santhony.gutierrez@amd.com
42411308Santhony.gutierrez@amd.com        exec file(source.abspath, 'r') in mod.__dict__
42511308Santhony.gutierrez@amd.com
42611308Santhony.gutierrez@amd.com        return mod
42711308Santhony.gutierrez@amd.com
42811308Santhony.gutierrez@amd.comimport m5.SimObject
42911308Santhony.gutierrez@amd.comimport m5.params
43011308Santhony.gutierrez@amd.comfrom m5.util import code_formatter
43111308Santhony.gutierrez@amd.com
43211308Santhony.gutierrez@amd.comm5.SimObject.clear()
43311308Santhony.gutierrez@amd.comm5.params.clear()
43411308Santhony.gutierrez@amd.com
43511308Santhony.gutierrez@amd.com# install the python importer so we can grab stuff from the source
43611308Santhony.gutierrez@amd.com# tree itself.  We can't have SimObjects added after this point or
43711308Santhony.gutierrez@amd.com# else we won't know about them for the rest of the stuff.
43811308Santhony.gutierrez@amd.comimporter = DictImporter(PySource.modules)
43911308Santhony.gutierrez@amd.comsys.meta_path[0:0] = [ importer ]
44011308Santhony.gutierrez@amd.com
44111308Santhony.gutierrez@amd.com# import all sim objects so we can populate the all_objects list
44211308Santhony.gutierrez@amd.com# make sure that we're working with a list, then let's sort it
44311308Santhony.gutierrez@amd.comfor modname in SimObject.modnames:
44411308Santhony.gutierrez@amd.com    exec('from m5.objects import %s' % modname)
44511308Santhony.gutierrez@amd.com
44611308Santhony.gutierrez@amd.com# we need to unload all of the currently imported modules so that they
44711308Santhony.gutierrez@amd.com# will be re-imported the next time the sconscript is run
44811308Santhony.gutierrez@amd.comimporter.unload()
44911308Santhony.gutierrez@amd.comsys.meta_path.remove(importer)
45011308Santhony.gutierrez@amd.com
4514382Sbinkertn@umich.edusim_objects = m5.SimObject.allClasses
4524382Sbinkertn@umich.eduall_enums = m5.params.allEnums
4534762Snate@binkert.org
4544762Snate@binkert.org# Find param types that need to be explicitly wrapped with swig.
4554762Snate@binkert.org# These will be recognized because the ParamDesc will have a
4566654Snate@binkert.org# swig_decl() method.  Most param types are based on types that don't
4576654Snate@binkert.org# need this, either because they're based on native types (like Int)
4585517Snate@binkert.org# or because they're SimObjects (which get swigged independently).
4595517Snate@binkert.org# For now the only things handled here are VectorParam types.
4605517Snate@binkert.orgparams_to_swig = {}
4615517Snate@binkert.orgfor name,obj in sorted(sim_objects.iteritems()):
4625517Snate@binkert.org    for param in obj._params.local.values():
4635517Snate@binkert.org        # load the ptype attribute now because it depends on the
4645517Snate@binkert.org        # current version of SimObject.allClasses, but when scons
4655517Snate@binkert.org        # actually uses the value, all versions of
4665517Snate@binkert.org        # SimObject.allClasses will have been loaded
4675517Snate@binkert.org        param.ptype
4685517Snate@binkert.org
4695517Snate@binkert.org        if not hasattr(param, 'swig_decl'):
4705517Snate@binkert.org            continue
4715517Snate@binkert.org        pname = param.ptype_str
4725517Snate@binkert.org        if pname not in params_to_swig:
4735517Snate@binkert.org            params_to_swig[pname] = param
4745517Snate@binkert.org
4756654Snate@binkert.org########################################################################
4765517Snate@binkert.org#
4775517Snate@binkert.org# calculate extra dependencies
4785517Snate@binkert.org#
4795517Snate@binkert.orgmodule_depends = ["m5", "m5.SimObject", "m5.params"]
4805517Snate@binkert.orgdepends = [ PySource.modules[dep].snode for dep in module_depends ]
48111802Sandreas.sandberg@arm.com
4825517Snate@binkert.org########################################################################
4835517Snate@binkert.org#
4846143Snate@binkert.org# Commands for the basic automatically generated python files
4856654Snate@binkert.org#
4865517Snate@binkert.org
4875517Snate@binkert.org# Generate Python file containing a dict specifying the current
4885517Snate@binkert.org# buildEnv flags.
4895517Snate@binkert.orgdef makeDefinesPyFile(target, source, env):
4905517Snate@binkert.org    build_env = source[0].get_contents()
4915517Snate@binkert.org
4925517Snate@binkert.org    code = code_formatter()
4935517Snate@binkert.org    code("""
4945517Snate@binkert.orgimport m5.internal
4955517Snate@binkert.orgimport m5.util
4965517Snate@binkert.org
4975517Snate@binkert.orgbuildEnv = m5.util.SmartDict($build_env)
4985517Snate@binkert.org
4995517Snate@binkert.orgcompileDate = m5.internal.core.compileDate
5006654Snate@binkert.org_globals = globals()
5016654Snate@binkert.orgfor key,val in m5.internal.core.__dict__.iteritems():
5025517Snate@binkert.org    if key.startswith('flag_'):
5035517Snate@binkert.org        flag = key[5:]
5046143Snate@binkert.org        _globals[flag] = val
5056143Snate@binkert.orgdel _globals
5066143Snate@binkert.org""")
5076727Ssteve.reinhardt@amd.com    code.write(target[0].abspath)
5085517Snate@binkert.org
5096727Ssteve.reinhardt@amd.comdefines_info = Value(build_env)
5105517Snate@binkert.org# Generate a file with all of the compile options in it
5115517Snate@binkert.orgenv.Command('python/m5/defines.py', defines_info,
5125517Snate@binkert.org            MakeAction(makeDefinesPyFile, Transform("DEFINES", 0)))
5136654Snate@binkert.orgPySource('m5', 'python/m5/defines.py')
5146654Snate@binkert.org
5157673Snate@binkert.org# Generate python file containing info about the M5 source code
5166654Snate@binkert.orgdef makeInfoPyFile(target, source, env):
5176654Snate@binkert.org    code = code_formatter()
5186654Snate@binkert.org    for src in source:
5196654Snate@binkert.org        data = ''.join(file(src.srcnode().abspath, 'r').xreadlines())
5205517Snate@binkert.org        code('$src = ${{repr(data)}}')
5215517Snate@binkert.org    code.write(str(target[0]))
5225517Snate@binkert.org
5236143Snate@binkert.org# Generate a file that wraps the basic top level files
5245517Snate@binkert.orgenv.Command('python/m5/info.py',
5254762Snate@binkert.org            [ '#/COPYING', '#/LICENSE', '#/README', ],
5265517Snate@binkert.org            MakeAction(makeInfoPyFile, Transform("INFO")))
5275517Snate@binkert.orgPySource('m5', 'python/m5/info.py')
5286143Snate@binkert.org
5296143Snate@binkert.org########################################################################
5305517Snate@binkert.org#
5315517Snate@binkert.org# Create all of the SimObject param headers and enum headers
5325517Snate@binkert.org#
5335517Snate@binkert.org
5345517Snate@binkert.orgdef createSimObjectParamStruct(target, source, env):
5355517Snate@binkert.org    assert len(target) == 1 and len(source) == 1
5365517Snate@binkert.org
5375517Snate@binkert.org    name = str(source[0].get_contents())
5385517Snate@binkert.org    obj = sim_objects[name]
5396143Snate@binkert.org
5405517Snate@binkert.org    code = code_formatter()
5416654Snate@binkert.org    obj.cxx_param_decl(code)
5426654Snate@binkert.org    code.write(target[0].abspath)
5436654Snate@binkert.org
5446654Snate@binkert.orgdef createParamSwigWrapper(target, source, env):
5456654Snate@binkert.org    assert len(target) == 1 and len(source) == 1
5466654Snate@binkert.org
5474762Snate@binkert.org    name = str(source[0].get_contents())
5484762Snate@binkert.org    param = params_to_swig[name]
5494762Snate@binkert.org
5504762Snate@binkert.org    code = code_formatter()
5514762Snate@binkert.org    param.swig_decl(code)
5527675Snate@binkert.org    code.write(target[0].abspath)
55310584Sandreas.hansson@arm.com
5544762Snate@binkert.orgdef createEnumStrings(target, source, env):
5554762Snate@binkert.org    assert len(target) == 1 and len(source) == 1
5564762Snate@binkert.org
5574762Snate@binkert.org    name = str(source[0].get_contents())
5584382Sbinkertn@umich.edu    obj = all_enums[name]
5594382Sbinkertn@umich.edu
5605517Snate@binkert.org    code = code_formatter()
5616654Snate@binkert.org    obj.cxx_def(code)
5625517Snate@binkert.org    code.write(target[0].abspath)
5638126Sgblack@eecs.umich.edu
5646654Snate@binkert.orgdef createEnumDecls(target, source, env):
5657673Snate@binkert.org    assert len(target) == 1 and len(source) == 1
5666654Snate@binkert.org
56711802Sandreas.sandberg@arm.com    name = str(source[0].get_contents())
5686654Snate@binkert.org    obj = all_enums[name]
5696654Snate@binkert.org
5706654Snate@binkert.org    code = code_formatter()
5716654Snate@binkert.org    obj.cxx_decl(code)
57211802Sandreas.sandberg@arm.com    code.write(target[0].abspath)
5736669Snate@binkert.org
57411802Sandreas.sandberg@arm.comdef createEnumSwigWrapper(target, source, env):
5756669Snate@binkert.org    assert len(target) == 1 and len(source) == 1
5766669Snate@binkert.org
5776669Snate@binkert.org    name = str(source[0].get_contents())
5786669Snate@binkert.org    obj = all_enums[name]
5796654Snate@binkert.org
5807673Snate@binkert.org    code = code_formatter()
5815517Snate@binkert.org    obj.swig_decl(code)
5828126Sgblack@eecs.umich.edu    code.write(target[0].abspath)
5835798Snate@binkert.org
5847756SAli.Saidi@ARM.comdef createSimObjectSwigWrapper(target, source, env):
5857816Ssteve.reinhardt@amd.com    name = source[0].get_contents()
5865798Snate@binkert.org    obj = sim_objects[name]
5875798Snate@binkert.org
5885517Snate@binkert.org    code = code_formatter()
5895517Snate@binkert.org    obj.swig_decl(code)
5907673Snate@binkert.org    code.write(target[0].abspath)
5915517Snate@binkert.org
5925517Snate@binkert.org# Generate all of the SimObject param C++ struct header files
5937673Snate@binkert.orgparams_hh_files = []
5947673Snate@binkert.orgfor name,simobj in sorted(sim_objects.iteritems()):
5955517Snate@binkert.org    py_source = PySource.modules[simobj.__module__]
5965798Snate@binkert.org    extra_deps = [ py_source.tnode ]
5975798Snate@binkert.org
5988333Snate@binkert.org    hh_file = File('params/%s.hh' % name)
5997816Ssteve.reinhardt@amd.com    params_hh_files.append(hh_file)
6005798Snate@binkert.org    env.Command(hh_file, Value(name),
6015798Snate@binkert.org                MakeAction(createSimObjectParamStruct, Transform("SO PARAM")))
6024762Snate@binkert.org    env.Depends(hh_file, depends + extra_deps)
6034762Snate@binkert.org
6044762Snate@binkert.org# Generate any needed param SWIG wrapper files
6054762Snate@binkert.orgparams_i_files = []
6064762Snate@binkert.orgfor name,param in params_to_swig.iteritems():
6078596Ssteve.reinhardt@amd.com    i_file = File('python/m5/internal/%s.i' % (param.swig_module_name()))
6085517Snate@binkert.org    params_i_files.append(i_file)
6095517Snate@binkert.org    env.Command(i_file, Value(name),
61011997Sgabeblack@google.com                MakeAction(createParamSwigWrapper, Transform("SW PARAM")))
6115517Snate@binkert.org    env.Depends(i_file, depends)
6125517Snate@binkert.org    SwigSource('m5.internal', i_file)
6137673Snate@binkert.org
6148596Ssteve.reinhardt@amd.com# Generate all enum header files
6157673Snate@binkert.orgfor name,enum in sorted(all_enums.iteritems()):
6165517Snate@binkert.org    py_source = PySource.modules[enum.__module__]
61710458Sandreas.hansson@arm.com    extra_deps = [ py_source.tnode ]
61810458Sandreas.hansson@arm.com
61910458Sandreas.hansson@arm.com    cc_file = File('enums/%s.cc' % name)
62010458Sandreas.hansson@arm.com    env.Command(cc_file, Value(name),
62110458Sandreas.hansson@arm.com                MakeAction(createEnumStrings, Transform("ENUM STR")))
62210458Sandreas.hansson@arm.com    env.Depends(cc_file, depends + extra_deps)
62310458Sandreas.hansson@arm.com    Source(cc_file)
62410458Sandreas.hansson@arm.com
62510458Sandreas.hansson@arm.com    hh_file = File('enums/%s.hh' % name)
62610458Sandreas.hansson@arm.com    env.Command(hh_file, Value(name),
62710458Sandreas.hansson@arm.com                MakeAction(createEnumDecls, Transform("ENUMDECL")))
62810458Sandreas.hansson@arm.com    env.Depends(hh_file, depends + extra_deps)
6295517Snate@binkert.org
63011996Sgabeblack@google.com    i_file = File('python/m5/internal/enum_%s.i' % name)
6315517Snate@binkert.org    env.Command(i_file, Value(name),
63211997Sgabeblack@google.com                MakeAction(createEnumSwigWrapper, Transform("ENUMSWIG")))
63311996Sgabeblack@google.com    env.Depends(i_file, depends + extra_deps)
6345517Snate@binkert.org    SwigSource('m5.internal', i_file)
6355517Snate@binkert.org
6367673Snate@binkert.org# Generate SimObject SWIG wrapper files
6377673Snate@binkert.orgfor name in sim_objects.iterkeys():
63811996Sgabeblack@google.com    i_file = File('python/m5/internal/param_%s.i' % name)
63911988Sandreas.sandberg@arm.com    env.Command(i_file, Value(name),
6407673Snate@binkert.org                MakeAction(createSimObjectSwigWrapper, Transform("SO SWIG")))
6415517Snate@binkert.org    env.Depends(i_file, depends)
6428596Ssteve.reinhardt@amd.com    SwigSource('m5.internal', i_file)
6435517Snate@binkert.org
6445517Snate@binkert.org# Generate the main swig init file
64511997Sgabeblack@google.comdef makeEmbeddedSwigInit(target, source, env):
6465517Snate@binkert.org    code = code_formatter()
6475517Snate@binkert.org    module = source[0].get_contents()
6487673Snate@binkert.org    code('''\
6497673Snate@binkert.org#include "sim/init.hh"
6507673Snate@binkert.org
6515517Snate@binkert.orgextern "C" {
65211988Sandreas.sandberg@arm.com    void init_${module}();
65311997Sgabeblack@google.com}
6548596Ssteve.reinhardt@amd.com
6558596Ssteve.reinhardt@amd.comEmbeddedSwig embed_swig_${module}(init_${module});
6568596Ssteve.reinhardt@amd.com''')
65711988Sandreas.sandberg@arm.com    code.write(str(target[0]))
6588596Ssteve.reinhardt@amd.com    
6598596Ssteve.reinhardt@amd.com# Build all swig modules
6608596Ssteve.reinhardt@amd.comfor swig in SwigSource.all:
6614762Snate@binkert.org    env.Command([swig.cc_source.tnode, swig.py_source.tnode], swig.tnode,
6626143Snate@binkert.org                MakeAction('$SWIG $SWIGFLAGS -outdir ${TARGETS[1].dir} '
6636143Snate@binkert.org                '-o ${TARGETS[0]} $SOURCES', Transform("SWIG")))
6646143Snate@binkert.org    cc_file = str(swig.tnode)
6654762Snate@binkert.org    init_file = '%s/%s_init.cc' % (dirname(cc_file), basename(cc_file))
6664762Snate@binkert.org    env.Command(init_file, Value(swig.module),
6674762Snate@binkert.org                MakeAction(makeEmbeddedSwigInit, Transform("EMBED SW")))
6687756SAli.Saidi@ARM.com    Source(init_file, **swig.guards)
6698596Ssteve.reinhardt@amd.com
6704762Snate@binkert.org#
6714762Snate@binkert.org# Handle debug flags
67210458Sandreas.hansson@arm.com#
67310458Sandreas.hansson@arm.comdef makeDebugFlagCC(target, source, env):
67410458Sandreas.hansson@arm.com    assert(len(target) == 1 and len(source) == 1)
67510458Sandreas.hansson@arm.com
67610458Sandreas.hansson@arm.com    val = eval(source[0].get_contents())
67710458Sandreas.hansson@arm.com    name, compound, desc = val
67810458Sandreas.hansson@arm.com    compound = list(sorted(compound))
67910458Sandreas.hansson@arm.com
68010458Sandreas.hansson@arm.com    code = code_formatter()
68110458Sandreas.hansson@arm.com
68210458Sandreas.hansson@arm.com    # file header
68310458Sandreas.hansson@arm.com    code('''
68410458Sandreas.hansson@arm.com/*
68510458Sandreas.hansson@arm.com * DO NOT EDIT THIS FILE! Automatically generated
68610458Sandreas.hansson@arm.com */
68710458Sandreas.hansson@arm.com
68810458Sandreas.hansson@arm.com#include "base/debug.hh"
68910458Sandreas.hansson@arm.com''')
69010458Sandreas.hansson@arm.com
69110458Sandreas.hansson@arm.com    for flag in compound:
69210458Sandreas.hansson@arm.com        code('#include "debug/$flag.hh"')
69310458Sandreas.hansson@arm.com    code()
69410458Sandreas.hansson@arm.com    code('namespace Debug {')
69510458Sandreas.hansson@arm.com    code()
69610458Sandreas.hansson@arm.com
69710458Sandreas.hansson@arm.com    if not compound:
69810458Sandreas.hansson@arm.com        code('SimpleFlag $name("$name", "$desc");')
69910458Sandreas.hansson@arm.com    else:
70010458Sandreas.hansson@arm.com        code('CompoundFlag $name("$name", "$desc",')
70110458Sandreas.hansson@arm.com        code.indent()
70210458Sandreas.hansson@arm.com        last = len(compound) - 1
70310458Sandreas.hansson@arm.com        for i,flag in enumerate(compound):
70410458Sandreas.hansson@arm.com            if i != last:
70510458Sandreas.hansson@arm.com                code('$flag,')
70610458Sandreas.hansson@arm.com            else:
70710458Sandreas.hansson@arm.com                code('$flag);')
70810458Sandreas.hansson@arm.com        code.dedent()
70910458Sandreas.hansson@arm.com
71010458Sandreas.hansson@arm.com    code()
71110458Sandreas.hansson@arm.com    code('} // namespace Debug')
71210458Sandreas.hansson@arm.com
71310458Sandreas.hansson@arm.com    code.write(str(target[0]))
71410458Sandreas.hansson@arm.com
71510458Sandreas.hansson@arm.comdef makeDebugFlagHH(target, source, env):
71610458Sandreas.hansson@arm.com    assert(len(target) == 1 and len(source) == 1)
71710458Sandreas.hansson@arm.com
71810458Sandreas.hansson@arm.com    val = eval(source[0].get_contents())
71910458Sandreas.hansson@arm.com    name, compound, desc = val
72010458Sandreas.hansson@arm.com
72110584Sandreas.hansson@arm.com    code = code_formatter()
72210458Sandreas.hansson@arm.com
72310458Sandreas.hansson@arm.com    # file header boilerplate
72410458Sandreas.hansson@arm.com    code('''\
72510458Sandreas.hansson@arm.com/*
72610458Sandreas.hansson@arm.com * DO NOT EDIT THIS FILE!
7274762Snate@binkert.org *
7286143Snate@binkert.org * Automatically generated by SCons
7296143Snate@binkert.org */
7306143Snate@binkert.org
7314762Snate@binkert.org#ifndef __DEBUG_${name}_HH__
7324762Snate@binkert.org#define __DEBUG_${name}_HH__
73311996Sgabeblack@google.com
7347816Ssteve.reinhardt@amd.comnamespace Debug {
7354762Snate@binkert.org''')
7364762Snate@binkert.org
7374762Snate@binkert.org    if compound:
7384762Snate@binkert.org        code('class CompoundFlag;')
7397756SAli.Saidi@ARM.com    code('class SimpleFlag;')
7408596Ssteve.reinhardt@amd.com
7414762Snate@binkert.org    if compound:
7424762Snate@binkert.org        code('extern CompoundFlag $name;')
74311988Sandreas.sandberg@arm.com        for flag in compound:
74411988Sandreas.sandberg@arm.com            code('extern SimpleFlag $flag;')
74511988Sandreas.sandberg@arm.com    else:
74611988Sandreas.sandberg@arm.com        code('extern SimpleFlag $name;')
74711988Sandreas.sandberg@arm.com
74811988Sandreas.sandberg@arm.com    code('''
74911988Sandreas.sandberg@arm.com}
75011988Sandreas.sandberg@arm.com
75111988Sandreas.sandberg@arm.com#endif // __DEBUG_${name}_HH__
75211988Sandreas.sandberg@arm.com''')
75311988Sandreas.sandberg@arm.com
7544382Sbinkertn@umich.edu    code.write(str(target[0]))
7559396Sandreas.hansson@arm.com
7569396Sandreas.hansson@arm.comfor name,flag in sorted(debug_flags.iteritems()):
7579396Sandreas.hansson@arm.com    n, compound, desc = flag
7589396Sandreas.hansson@arm.com    assert n == name
7599396Sandreas.hansson@arm.com
7609396Sandreas.hansson@arm.com    env.Command('debug/%s.hh' % name, Value(flag),
7619396Sandreas.hansson@arm.com                MakeAction(makeDebugFlagHH, Transform("TRACING", 0)))
7629396Sandreas.hansson@arm.com    env.Command('debug/%s.cc' % name, Value(flag),
7639396Sandreas.hansson@arm.com                MakeAction(makeDebugFlagCC, Transform("TRACING", 0)))
7649396Sandreas.hansson@arm.com    Source('debug/%s.cc' % name)
7659396Sandreas.hansson@arm.com
7669396Sandreas.hansson@arm.com# Embed python files.  All .py files that have been indicated by a
7679396Sandreas.hansson@arm.com# PySource() call in a SConscript need to be embedded into the M5
76812302Sgabeblack@google.com# library.  To do that, we compile the file to byte code, marshal the
7699396Sandreas.hansson@arm.com# byte code, compress it, and then generate a c++ file that
7709396Sandreas.hansson@arm.com# inserts the result into an array.
7719396Sandreas.hansson@arm.comdef embedPyFile(target, source, env):
7729396Sandreas.hansson@arm.com    def c_str(string):
7738232Snate@binkert.org        if string is None:
7748232Snate@binkert.org            return "0"
7758232Snate@binkert.org        return '"%s"' % string
7768232Snate@binkert.org
7778232Snate@binkert.org    '''Action function to compile a .py into a code object, marshal
7786229Snate@binkert.org    it, compress it, and stick it into an asm file so the code appears
77910455SCurtis.Dunham@arm.com    as just bytes with a label in the data section'''
7806229Snate@binkert.org
78110455SCurtis.Dunham@arm.com    src = file(str(source[0]), 'r').read()
78210455SCurtis.Dunham@arm.com
78310455SCurtis.Dunham@arm.com    pysource = PySource.tnodes[source[0]]
7845517Snate@binkert.org    compiled = compile(src, pysource.abspath, 'exec')
7855517Snate@binkert.org    marshalled = marshal.dumps(compiled)
7867673Snate@binkert.org    compressed = zlib.compress(marshalled)
7875517Snate@binkert.org    data = compressed
78810455SCurtis.Dunham@arm.com    sym = pysource.symname
7895517Snate@binkert.org
7905517Snate@binkert.org    code = code_formatter()
7918232Snate@binkert.org    code('''\
79210455SCurtis.Dunham@arm.com#include "sim/init.hh"
79310455SCurtis.Dunham@arm.com
79410455SCurtis.Dunham@arm.comnamespace {
7957673Snate@binkert.org
7967673Snate@binkert.orgconst uint8_t data_${sym}[] = {
79710455SCurtis.Dunham@arm.com''')
79810455SCurtis.Dunham@arm.com    code.indent()
79910455SCurtis.Dunham@arm.com    step = 16
8005517Snate@binkert.org    for i in xrange(0, len(data), step):
80110455SCurtis.Dunham@arm.com        x = array.array('B', data[i:i+step])
80210455SCurtis.Dunham@arm.com        code(''.join('%d,' % d for d in x))
80310455SCurtis.Dunham@arm.com    code.dedent()
80410455SCurtis.Dunham@arm.com    
80510455SCurtis.Dunham@arm.com    code('''};
80610455SCurtis.Dunham@arm.com
80710455SCurtis.Dunham@arm.comEmbeddedPython embedded_${sym}(
80810455SCurtis.Dunham@arm.com    ${{c_str(pysource.arcname)}},
80910685Sandreas.hansson@arm.com    ${{c_str(pysource.abspath)}},
81010455SCurtis.Dunham@arm.com    ${{c_str(pysource.modpath)}},
81110685Sandreas.hansson@arm.com    data_${sym},
81210455SCurtis.Dunham@arm.com    ${{len(data)}},
8135517Snate@binkert.org    ${{len(marshalled)}});
81410455SCurtis.Dunham@arm.com
8158232Snate@binkert.org} // anonymous namespace
8168232Snate@binkert.org''')
8175517Snate@binkert.org    code.write(str(target[0]))
8187673Snate@binkert.org
8195517Snate@binkert.orgfor source in PySource.all:
8208232Snate@binkert.org    env.Command(source.cpp, source.tnode, 
8218232Snate@binkert.org                MakeAction(embedPyFile, Transform("EMBED PY")))
8225517Snate@binkert.org    Source(source.cpp)
8238232Snate@binkert.org
8248232Snate@binkert.org########################################################################
8258232Snate@binkert.org#
8267673Snate@binkert.org# Define binaries.  Each different build type (debug, opt, etc.) gets
8275517Snate@binkert.org# a slightly different build environment.
8285517Snate@binkert.org#
8297673Snate@binkert.org
8305517Snate@binkert.org# List of constructed environments to pass back to SConstruct
83110455SCurtis.Dunham@arm.comenvList = []
8325517Snate@binkert.org
8335517Snate@binkert.orgdate_source = Source('base/date.cc', skip_lib=True)
8348232Snate@binkert.org
8358232Snate@binkert.org# Function to create a new build environment as clone of current
8365517Snate@binkert.org# environment 'env' with modified object suffix and optional stripped
8378232Snate@binkert.org# binary.  Additional keyword arguments are appended to corresponding
8388232Snate@binkert.org# build environment vars.
8395517Snate@binkert.orgdef makeEnv(label, objsfx, strip = False, **kwargs):
8408232Snate@binkert.org    # SCons doesn't know to append a library suffix when there is a '.' in the
8418232Snate@binkert.org    # name.  Use '_' instead.
8428232Snate@binkert.org    libname = 'gem5_' + label
8435517Snate@binkert.org    exename = 'gem5.' + label
8448232Snate@binkert.org    secondary_exename = 'm5.' + label
8458232Snate@binkert.org
8468232Snate@binkert.org    new_env = env.Clone(OBJSUFFIX=objsfx, SHOBJSUFFIX=objsfx + 's')
8478232Snate@binkert.org    new_env.Label = label
8488232Snate@binkert.org    new_env.Append(**kwargs)
8498232Snate@binkert.org
8505517Snate@binkert.org    swig_env = new_env.Clone()
8518232Snate@binkert.org    swig_env.Append(CCFLAGS='-Werror')
8528232Snate@binkert.org    if env['GCC']:
8535517Snate@binkert.org        swig_env.Append(CCFLAGS='-Wno-uninitialized')
8548232Snate@binkert.org        swig_env.Append(CCFLAGS='-Wno-sign-compare')
8557673Snate@binkert.org        swig_env.Append(CCFLAGS='-Wno-parentheses')
8565517Snate@binkert.org        swig_env.Append(CCFLAGS='-Wno-unused-label')
8577673Snate@binkert.org        if compareVersions(env['GCC_VERSION'], '4.6') >= 0:
8585517Snate@binkert.org            swig_env.Append(CCFLAGS='-Wno-unused-but-set-variable')
8598232Snate@binkert.org    if env['CLANG']:
8608232Snate@binkert.org        swig_env.Append(CCFLAGS=['-Wno-unused-label'])
8618232Snate@binkert.org
8625192Ssaidi@eecs.umich.edu
86310454SCurtis.Dunham@arm.com    werror_env = new_env.Clone()
86410454SCurtis.Dunham@arm.com    werror_env.Append(CCFLAGS='-Werror')
8658232Snate@binkert.org
86610455SCurtis.Dunham@arm.com    def make_obj(source, static, extra_deps = None):
86710455SCurtis.Dunham@arm.com        '''This function adds the specified source to the correct
86810455SCurtis.Dunham@arm.com        build environment, and returns the corresponding SCons Object
86910455SCurtis.Dunham@arm.com        nodes'''
8705192Ssaidi@eecs.umich.edu
87111077SCurtis.Dunham@arm.com        if source.swig:
87211330SCurtis.Dunham@arm.com            env = swig_env
87311077SCurtis.Dunham@arm.com        elif source.Werror:
87411077SCurtis.Dunham@arm.com            env = werror_env
87511077SCurtis.Dunham@arm.com        else:
87611330SCurtis.Dunham@arm.com            env = new_env
87711077SCurtis.Dunham@arm.com
8787674Snate@binkert.org        if static:
8795522Snate@binkert.org            obj = env.StaticObject(source.tnode)
8805522Snate@binkert.org        else:
8817674Snate@binkert.org            obj = env.SharedObject(source.tnode)
8827674Snate@binkert.org
8837674Snate@binkert.org        if extra_deps:
8847674Snate@binkert.org            env.Depends(obj, extra_deps)
8857674Snate@binkert.org
8867674Snate@binkert.org        return obj
8877674Snate@binkert.org
8887674Snate@binkert.org    static_objs = \
8895522Snate@binkert.org        [ make_obj(s, True) for s in Source.get(main=False, skip_lib=False) ]
8905522Snate@binkert.org    shared_objs = \
8915522Snate@binkert.org        [ make_obj(s, False) for s in Source.get(main=False, skip_lib=False) ]
8925517Snate@binkert.org
8935522Snate@binkert.org    static_date = make_obj(date_source, static=True, extra_deps=static_objs)
8945517Snate@binkert.org    static_objs.append(static_date)
8956143Snate@binkert.org    
8966727Ssteve.reinhardt@amd.com    shared_date = make_obj(date_source, static=False, extra_deps=shared_objs)
8975522Snate@binkert.org    shared_objs.append(shared_date)
8985522Snate@binkert.org
8995522Snate@binkert.org    # First make a library of everything but main() so other programs can
9007674Snate@binkert.org    # link against m5.
9015517Snate@binkert.org    static_lib = new_env.StaticLibrary(libname, static_objs)
9027673Snate@binkert.org    shared_lib = new_env.SharedLibrary(libname, shared_objs)
9037673Snate@binkert.org
9047674Snate@binkert.org    # Now link a stub with main() and the static library.
9057673Snate@binkert.org    main_objs = [ make_obj(s, True) for s in Source.get(main=True) ]
9067674Snate@binkert.org
9077674Snate@binkert.org    for test in UnitTest.all:
9088946Sandreas.hansson@arm.com        flags = { test.target : True }
9097674Snate@binkert.org        test_sources = Source.get(**flags)
9107674Snate@binkert.org        test_objs = [ make_obj(s, static=True) for s in test_sources ]
9117674Snate@binkert.org        if test.main:
9125522Snate@binkert.org            test_objs += main_objs
9135522Snate@binkert.org        testname = "unittest/%s.%s" % (test.target, label)
9147674Snate@binkert.org        new_env.Program(testname, test_objs + static_objs)
9157674Snate@binkert.org
91611308Santhony.gutierrez@amd.com    progname = exename
9177674Snate@binkert.org    if strip:
9187673Snate@binkert.org        progname += '.unstripped'
9197674Snate@binkert.org
9207674Snate@binkert.org    targets = new_env.Program(progname, main_objs + static_objs)
9217674Snate@binkert.org
9227674Snate@binkert.org    if strip:
9237674Snate@binkert.org        if sys.platform == 'sunos5':
9247674Snate@binkert.org            cmd = 'cp $SOURCE $TARGET; strip $TARGET'
9257674Snate@binkert.org        else:
9267674Snate@binkert.org            cmd = 'strip $SOURCE -o $TARGET'
9277811Ssteve.reinhardt@amd.com        targets = new_env.Command(exename, progname,
9287674Snate@binkert.org                    MakeAction(cmd, Transform("STRIP")))
9297673Snate@binkert.org
9305522Snate@binkert.org    new_env.Command(secondary_exename, exename,
9316143Snate@binkert.org            MakeAction('ln $SOURCE $TARGET', Transform("HARDLINK")))
93210453SAndrew.Bardsley@arm.com
9337816Ssteve.reinhardt@amd.com    new_env.M5Binary = targets[0]
93412302Sgabeblack@google.com    envList.append(new_env)
9354382Sbinkertn@umich.edu
9364382Sbinkertn@umich.edu# Debug binary
9374382Sbinkertn@umich.educcflags = {}
9384382Sbinkertn@umich.eduif env['GCC']:
9394382Sbinkertn@umich.edu    if sys.platform == 'sunos5':
9404382Sbinkertn@umich.edu        ccflags['debug'] = '-gstabs+'
9414382Sbinkertn@umich.edu    else:
9424382Sbinkertn@umich.edu        ccflags['debug'] = '-ggdb3'
94312302Sgabeblack@google.com    ccflags['opt'] = '-g -O3'
9444382Sbinkertn@umich.edu    ccflags['fast'] = '-O3'
9452655Sstever@eecs.umich.edu    ccflags['prof'] = '-O3 -g -pg'
9462655Sstever@eecs.umich.eduelif env['SUNCC']:
9472655Sstever@eecs.umich.edu    ccflags['debug'] = '-g0'
9482655Sstever@eecs.umich.edu    ccflags['opt'] = '-g -O'
94912063Sgabeblack@google.com    ccflags['fast'] = '-fast'
9505601Snate@binkert.org    ccflags['prof'] = '-fast -g -pg'
9515601Snate@binkert.orgelif env['ICC']:
95212222Sgabeblack@google.com    ccflags['debug'] = '-g -O0'
95312222Sgabeblack@google.com    ccflags['opt'] = '-g -O'
95412222Sgabeblack@google.com    ccflags['fast'] = '-fast'
9555522Snate@binkert.org    ccflags['prof'] = '-fast -g -pg'
9565863Snate@binkert.orgelif env['CLANG']:
9575601Snate@binkert.org    ccflags['debug'] = '-g -O0'
9585601Snate@binkert.org    ccflags['opt'] = '-g -O3'
9595601Snate@binkert.org    ccflags['fast'] = '-O3'
96012307Sgabeblack@google.com    ccflags['prof'] = '-O3 -g -pg'
96112307Sgabeblack@google.comelse:
9626143Snate@binkert.org    print 'Unknown compiler, please fix compiler options'
96312302Sgabeblack@google.com    Exit(1)
96410453SAndrew.Bardsley@arm.com
96511988Sandreas.sandberg@arm.com
96611988Sandreas.sandberg@arm.com# To speed things up, we only instantiate the build environments we
96710453SAndrew.Bardsley@arm.com# need.  We try to identify the needed environment for each target; if
96812302Sgabeblack@google.com# we can't, we fall back on instantiating all the environments just to
96910453SAndrew.Bardsley@arm.com# be safe.
97011983Sgabeblack@google.comtarget_types = ['debug', 'opt', 'fast', 'prof']
97111983Sgabeblack@google.comobj2target = {'do': 'debug', 'o': 'opt', 'fo': 'fast', 'po': 'prof'}
97212302Sgabeblack@google.com
97312302Sgabeblack@google.comdef identifyTarget(t):
97412307Sgabeblack@google.com    ext = t.split('.')[-1]
97512307Sgabeblack@google.com    if ext in target_types:
97611983Sgabeblack@google.com        return ext
97712302Sgabeblack@google.com    if obj2target.has_key(ext):
97812302Sgabeblack@google.com        return obj2target[ext]
97911983Sgabeblack@google.com    match = re.search(r'/tests/([^/]+)/', t)
98011983Sgabeblack@google.com    if match and match.group(1) in target_types:
98111983Sgabeblack@google.com        return match.group(1)
98212310Sgabeblack@google.com    return 'all'
98312310Sgabeblack@google.com
98412310Sgabeblack@google.comneeded_envs = [identifyTarget(target) for target in BUILD_TARGETS]
98512063Sgabeblack@google.comif 'all' in needed_envs:
98612063Sgabeblack@google.com    needed_envs += target_types
98712063Sgabeblack@google.com
98812310Sgabeblack@google.com# Debug binary
98912310Sgabeblack@google.comif 'debug' in needed_envs:
99012063Sgabeblack@google.com    makeEnv('debug', '.do',
99112063Sgabeblack@google.com            CCFLAGS = Split(ccflags['debug']),
99211983Sgabeblack@google.com            CPPDEFINES = ['DEBUG', 'TRACING_ON=1'])
99311983Sgabeblack@google.com
99411983Sgabeblack@google.com# Optimized binary
99512310Sgabeblack@google.comif 'opt' in needed_envs:
99612310Sgabeblack@google.com    makeEnv('opt', '.o',
99711983Sgabeblack@google.com            CCFLAGS = Split(ccflags['opt']),
99811983Sgabeblack@google.com            CPPDEFINES = ['TRACING_ON=1'])
99911983Sgabeblack@google.com
100011983Sgabeblack@google.com# "Fast" binary
100112310Sgabeblack@google.comif 'fast' in needed_envs:
100212310Sgabeblack@google.com    makeEnv('fast', '.fo', strip = True,
10036143Snate@binkert.org            CCFLAGS = Split(ccflags['fast']),
100412307Sgabeblack@google.com            CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'])
100512306Sgabeblack@google.com
100612310Sgabeblack@google.com# Profiled binary
100710453SAndrew.Bardsley@arm.comif 'prof' in needed_envs:
100812307Sgabeblack@google.com    makeEnv('prof', '.po',
100912306Sgabeblack@google.com            CCFLAGS = Split(ccflags['prof']),
101012310Sgabeblack@google.com            CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
10115554Snate@binkert.org            LINKFLAGS = '-pg')
10125522Snate@binkert.org
10135522Snate@binkert.orgReturn('envList')
10145797Snate@binkert.org