SConscript revision 8233
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
37955SN/Aimport sys
385522Snate@binkert.orgimport zlib
394202Sbinkertn@umich.edu
405742Snate@binkert.orgfrom os.path import basename, dirname, exists, isdir, isfile, join as joinpath
41955SN/A
424381Sbinkertn@umich.eduimport SCons
434381Sbinkertn@umich.edu
44955SN/A# This file defines how to build a particular configuration of M5
45955SN/A# based on variable settings in the 'env' build environment.
46955SN/A
474202Sbinkertn@umich.eduImport('*')
48955SN/A
494382Sbinkertn@umich.edu# Children need to see the environment
504382Sbinkertn@umich.eduExport('env')
514382Sbinkertn@umich.edu
526654Snate@binkert.orgbuild_env = [(opt, env[opt]) for opt in export_vars]
535517Snate@binkert.org
547674Snate@binkert.orgfrom m5.util import code_formatter
557674Snate@binkert.org
566143Snate@binkert.org########################################################################
576143Snate@binkert.org# Code for adding source files of various types
586143Snate@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
626143Snate@binkert.org# will be accepted (unspecified filters on files are assumed to be
636143Snate@binkert.org# false).  Current filters are:
646143Snate@binkert.org#     main -- specifies the m5 main() function
656143Snate@binkert.org#     skip_lib -- do not put this file into the m5 library
666143Snate@binkert.org#     <unittest> -- unit tests use filters based on the unit test name
676143Snate@binkert.org#
686143Snate@binkert.org# A parent can now be specified for a source file and default filter
696143Snate@binkert.org# values will be retrieved recursively from parents (children override
706143Snate@binkert.org# parents).
714762Snate@binkert.org#
726143Snate@binkert.orgclass SourceMeta(type):
736143Snate@binkert.org    '''Meta class for source files that keeps track of all files of a
746143Snate@binkert.org    particular type and has a get function for finding all functions
756143Snate@binkert.org    of a certain type that match a set of guards'''
766143Snate@binkert.org    def __init__(cls, name, bases, dict):
776143Snate@binkert.org        super(SourceMeta, cls).__init__(name, bases, dict)
786143Snate@binkert.org        cls.all = []
796143Snate@binkert.org        
806143Snate@binkert.org    def get(cls, **guards):
816143Snate@binkert.org        '''Find all files that match the specified guards.  If a source
826143Snate@binkert.org        file does not specify a flag, the default is False'''
836143Snate@binkert.org        for src in cls.all:
846143Snate@binkert.org            for flag,value in guards.iteritems():
856143Snate@binkert.org                # if the flag is found and has a different value, skip
866143Snate@binkert.org                # this file
876143Snate@binkert.org                if src.all_guards.get(flag, False) != value:
886143Snate@binkert.org                    break
896143Snate@binkert.org            else:
906143Snate@binkert.org                yield src
916143Snate@binkert.org
926143Snate@binkert.orgclass SourceFile(object):
937065Snate@binkert.org    '''Base object that encapsulates the notion of a source file.
946143Snate@binkert.org    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
976143Snate@binkert.org    also be specified to get default guards from'''
986143Snate@binkert.org    __metaclass__ = SourceMeta
996143Snate@binkert.org    def __init__(self, source, parent=None, **guards):
1006143Snate@binkert.org        self.guards = guards
1016143Snate@binkert.org        self.parent = parent
1026143Snate@binkert.org
1036143Snate@binkert.org        tnode = source
1046143Snate@binkert.org        if not isinstance(source, SCons.Node.FS.File):
1056143Snate@binkert.org            tnode = File(source)
1066143Snate@binkert.org
1076143Snate@binkert.org        self.tnode = tnode
1086143Snate@binkert.org        self.snode = tnode.srcnode()
1096143Snate@binkert.org
1106143Snate@binkert.org        for base in type(self).__mro__:
1116143Snate@binkert.org            if issubclass(base, SourceFile):
1126143Snate@binkert.org                base.all.append(self)
1136143Snate@binkert.org
1146143Snate@binkert.org    @property
1155522Snate@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)
1256143Snate@binkert.org
1265522Snate@binkert.org    @property
1275522Snate@binkert.org    def extname(self):
1285522Snate@binkert.org        index = self.basename.rfind('.')
1295522Snate@binkert.org        if index <= 0:
1305604Snate@binkert.org            # dot files aren't extensions
1315604Snate@binkert.org            return self.basename, None
1326143Snate@binkert.org
1336143Snate@binkert.org        return self.basename[:index], self.basename[index+1:]
1344762Snate@binkert.org
1354762Snate@binkert.org    @property
1366143Snate@binkert.org    def all_guards(self):
1376727Ssteve.reinhardt@amd.com        '''find all guards for this object getting default values
1386727Ssteve.reinhardt@amd.com        recursively from its parents'''
1396727Ssteve.reinhardt@amd.com        guards = {}
1404762Snate@binkert.org        if self.parent:
1416143Snate@binkert.org            guards.update(self.parent.guards)
1426143Snate@binkert.org        guards.update(self.guards)
1436143Snate@binkert.org        return guards
1446143Snate@binkert.org
1456727Ssteve.reinhardt@amd.com    def __lt__(self, other): return self.filename < other.filename
1466143Snate@binkert.org    def __le__(self, other): return self.filename <= other.filename
1477674Snate@binkert.org    def __gt__(self, other): return self.filename > other.filename
1487674Snate@binkert.org    def __ge__(self, other): return self.filename >= other.filename
1495604Snate@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):
1534762Snate@binkert.org    '''Add a c/c++ source file to the build'''
1546143Snate@binkert.org    def __init__(self, source, Werror=True, swig=False, **guards):
1554762Snate@binkert.org        '''specify the source file, and any guards'''
1564762Snate@binkert.org        super(Source, self).__init__(source, **guards)
1574762Snate@binkert.org
1586143Snate@binkert.org        self.Werror = Werror
1596143Snate@binkert.org        self.swig = swig
1604762Snate@binkert.org
1616143Snate@binkert.orgclass PySource(SourceFile):
1626143Snate@binkert.org    '''Add a python source file to the named package'''
1636143Snate@binkert.org    invalid_sym_char = re.compile('[^A-z0-9_]')
1646143Snate@binkert.org    modules = {}
1654762Snate@binkert.org    tnodes = {}
1666143Snate@binkert.org    symnames = {}
1674762Snate@binkert.org    
1686143Snate@binkert.org    def __init__(self, package, source, **guards):
1694762Snate@binkert.org        '''specify the python package, the source file, and any guards'''
1706143Snate@binkert.org        super(PySource, self).__init__(source, **guards)
1716143Snate@binkert.org
1726143Snate@binkert.org        modname,ext = self.extname
1736143Snate@binkert.org        assert ext == 'py'
1746143Snate@binkert.org
1756143Snate@binkert.org        if package:
1766143Snate@binkert.org            path = package.split('.')
1776143Snate@binkert.org        else:
1786143Snate@binkert.org            path = []
1796143Snate@binkert.org
1806143Snate@binkert.org        modpath = path[:]
1816143Snate@binkert.org        if modname != '__init__':
1826143Snate@binkert.org            modpath += [ modname ]
183955SN/A        modpath = '.'.join(modpath)
1845584Snate@binkert.org
1855584Snate@binkert.org        arcpath = path + [ self.basename ]
1865584Snate@binkert.org        abspath = self.snode.abspath
1875584Snate@binkert.org        if not exists(abspath):
1886143Snate@binkert.org            abspath = self.tnode.abspath
1896143Snate@binkert.org
1906143Snate@binkert.org        self.package = package
1915584Snate@binkert.org        self.modname = modname
1924382Sbinkertn@umich.edu        self.modpath = modpath
1934202Sbinkertn@umich.edu        self.arcname = joinpath(*arcpath)
1944382Sbinkertn@umich.edu        self.abspath = abspath
1954382Sbinkertn@umich.edu        self.compiled = File(self.filename + 'c')
1964382Sbinkertn@umich.edu        self.cpp = File(self.filename + '.cc')
1975584Snate@binkert.org        self.symname = PySource.invalid_sym_char.sub('_', modpath)
1984382Sbinkertn@umich.edu
1994382Sbinkertn@umich.edu        PySource.modules[modpath] = self
2004382Sbinkertn@umich.edu        PySource.tnodes[self.tnode] = self
2018232Snate@binkert.org        PySource.symnames[self.symname] = self
2025192Ssaidi@eecs.umich.edu
2038232Snate@binkert.orgclass SimObject(PySource):
2048232Snate@binkert.org    '''Add a SimObject python file as a python source object and add
2058232Snate@binkert.org    it to a list of sim object modules'''
2065192Ssaidi@eecs.umich.edu
2078232Snate@binkert.org    fixed = False
2088232Snate@binkert.org    modnames = []
2095192Ssaidi@eecs.umich.edu
2105799Snate@binkert.org    def __init__(self, source, **guards):
2118232Snate@binkert.org        '''Specify the source file and any guards (automatically in
2125192Ssaidi@eecs.umich.edu        the m5.objects package)'''
2135192Ssaidi@eecs.umich.edu        super(SimObject, self).__init__('m5.objects', source, **guards)
2145192Ssaidi@eecs.umich.edu        if self.fixed:
2158232Snate@binkert.org            raise AttributeError, "Too late to call SimObject now."
2165192Ssaidi@eecs.umich.edu
2178232Snate@binkert.org        bisect.insort_right(SimObject.modnames, self.modname)
2185192Ssaidi@eecs.umich.edu
2195192Ssaidi@eecs.umich.educlass SwigSource(SourceFile):
2205192Ssaidi@eecs.umich.edu    '''Add a swig file to build'''
2215192Ssaidi@eecs.umich.edu
2225192Ssaidi@eecs.umich.edu    def __init__(self, package, source, **guards):
2234382Sbinkertn@umich.edu        '''Specify the python package, the source file, and any guards'''
2244382Sbinkertn@umich.edu        super(SwigSource, self).__init__(source, **guards)
2254382Sbinkertn@umich.edu
2262667Sstever@eecs.umich.edu        modname,ext = self.extname
2272667Sstever@eecs.umich.edu        assert ext == 'i'
2282667Sstever@eecs.umich.edu
2292667Sstever@eecs.umich.edu        self.module = modname
2302667Sstever@eecs.umich.edu        cc_file = joinpath(self.dirname, modname + '_wrap.cc')
2312667Sstever@eecs.umich.edu        py_file = joinpath(self.dirname, modname + '.py')
2325742Snate@binkert.org
2335742Snate@binkert.org        self.cc_source = Source(cc_file, swig=True, parent=self)
2345742Snate@binkert.org        self.py_source = PySource(package, py_file, parent=self)
2355793Snate@binkert.org
2365793Snate@binkert.orgunit_tests = []
2375793Snate@binkert.orgdef UnitTest(target, sources):
2385793Snate@binkert.org    '''Create a unit test, specify the target name and a source or
2395793Snate@binkert.org    list of sources'''
2404382Sbinkertn@umich.edu    if not isinstance(sources, (list, tuple)):
2414762Snate@binkert.org        sources = [ sources ]
2425344Sstever@gmail.com
2434382Sbinkertn@umich.edu    sources = [ Source(src, skip_lib=True) for src in sources ]
2445341Sstever@gmail.com    unit_tests.append((target, sources))
2455742Snate@binkert.org
2465742Snate@binkert.org# Children should have access
2475742Snate@binkert.orgExport('Source')
2485742Snate@binkert.orgExport('PySource')
2495742Snate@binkert.orgExport('SimObject')
2504762Snate@binkert.orgExport('SwigSource')
2515742Snate@binkert.orgExport('UnitTest')
2525742Snate@binkert.org
2537722Sgblack@eecs.umich.edu########################################################################
2545742Snate@binkert.org#
2555742Snate@binkert.org# Debug Flags
2565742Snate@binkert.org#
2575742Snate@binkert.orgdebug_flags = {}
2585341Sstever@gmail.comdef DebugFlag(name, desc=None):
2595742Snate@binkert.org    if name in debug_flags:
2607722Sgblack@eecs.umich.edu        raise AttributeError, "Flag %s already specified" % name
2614773Snate@binkert.org    debug_flags[name] = (name, (), desc)
2626108Snate@binkert.orgTraceFlag = DebugFlag
2631858SN/A
2641085SN/Adef CompoundFlag(name, flags, desc=None):
2656658Snate@binkert.org    if name in debug_flags:
2666658Snate@binkert.org        raise AttributeError, "Flag %s already specified" % name
2677673Snate@binkert.org
2686658Snate@binkert.org    compound = tuple(flags)
2696658Snate@binkert.org    debug_flags[name] = (name, compound, desc)
2706658Snate@binkert.org
2716658Snate@binkert.orgExport('DebugFlag')
2726658Snate@binkert.orgExport('TraceFlag')
2736658Snate@binkert.orgExport('CompoundFlag')
2746658Snate@binkert.org
2757673Snate@binkert.org########################################################################
2767673Snate@binkert.org#
2777673Snate@binkert.org# Set some compiler variables
2787673Snate@binkert.org#
2797673Snate@binkert.org
2807673Snate@binkert.org# Include file paths are rooted in this directory.  SCons will
2817673Snate@binkert.org# automatically expand '.' to refer to both the source directory and
2826658Snate@binkert.org# the corresponding build directory to pick up generated include
2837673Snate@binkert.org# files.
2847673Snate@binkert.orgenv.Append(CPPPATH=Dir('.'))
2857673Snate@binkert.org
2867673Snate@binkert.orgfor extra_dir in extras_dir_list:
2877673Snate@binkert.org    env.Append(CPPPATH=Dir(extra_dir))
2887673Snate@binkert.org
2897673Snate@binkert.org# Workaround for bug in SCons version > 0.97d20071212
2907673Snate@binkert.org# Scons bug id: 2006 M5 Bug id: 308 
2917673Snate@binkert.orgfor root, dirs, files in os.walk(base_dir, topdown=True):
2927673Snate@binkert.org    Dir(root[len(base_dir) + 1:])
2936658Snate@binkert.org
2947756SAli.Saidi@ARM.com########################################################################
2957816Ssteve.reinhardt@amd.com#
2966658Snate@binkert.org# Walk the tree and execute all SConscripts in subdirectories
2974382Sbinkertn@umich.edu#
2984382Sbinkertn@umich.edu
2994762Snate@binkert.orghere = Dir('.').srcnode().abspath
3004762Snate@binkert.orgfor root, dirs, files in os.walk(base_dir, topdown=True):
3014762Snate@binkert.org    if root == here:
3026654Snate@binkert.org        # we don't want to recurse back into this SConscript
3036654Snate@binkert.org        continue
3045517Snate@binkert.org
3055517Snate@binkert.org    if 'SConscript' in files:
3065517Snate@binkert.org        build_dir = joinpath(env['BUILDDIR'], root[len(base_dir) + 1:])
3075517Snate@binkert.org        SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
3085517Snate@binkert.org
3095517Snate@binkert.orgfor extra_dir in extras_dir_list:
3105517Snate@binkert.org    prefix_len = len(dirname(extra_dir)) + 1
3115517Snate@binkert.org    for root, dirs, files in os.walk(extra_dir, topdown=True):
3125517Snate@binkert.org        if 'SConscript' in files:
3135517Snate@binkert.org            build_dir = joinpath(env['BUILDDIR'], root[prefix_len:])
3145517Snate@binkert.org            SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
3155517Snate@binkert.org
3165517Snate@binkert.orgfor opt in export_vars:
3175517Snate@binkert.org    env.ConfigFile(opt)
3185517Snate@binkert.org
3195517Snate@binkert.orgdef makeTheISA(source, target, env):
3205517Snate@binkert.org    isas = [ src.get_contents() for src in source ]
3216654Snate@binkert.org    target_isa = env['TARGET_ISA']
3225517Snate@binkert.org    def define(isa):
3235517Snate@binkert.org        return isa.upper() + '_ISA'
3245517Snate@binkert.org    
3255517Snate@binkert.org    def namespace(isa):
3265517Snate@binkert.org        return isa[0].upper() + isa[1:].lower() + 'ISA' 
3275517Snate@binkert.org
3285517Snate@binkert.org
3295517Snate@binkert.org    code = code_formatter()
3306143Snate@binkert.org    code('''\
3316654Snate@binkert.org#ifndef __CONFIG_THE_ISA_HH__
3325517Snate@binkert.org#define __CONFIG_THE_ISA_HH__
3335517Snate@binkert.org
3345517Snate@binkert.org''')
3355517Snate@binkert.org
3365517Snate@binkert.org    for i,isa in enumerate(isas):
3375517Snate@binkert.org        code('#define $0 $1', define(isa), i + 1)
3385517Snate@binkert.org
3395517Snate@binkert.org    code('''
3405517Snate@binkert.org
3415517Snate@binkert.org#define THE_ISA ${{define(target_isa)}}
3425517Snate@binkert.org#define TheISA ${{namespace(target_isa)}}
3435517Snate@binkert.org
3445517Snate@binkert.org#endif // __CONFIG_THE_ISA_HH__''')
3455517Snate@binkert.org
3466654Snate@binkert.org    code.write(str(target[0]))
3476654Snate@binkert.org
3485517Snate@binkert.orgenv.Command('config/the_isa.hh', map(Value, all_isa_list),
3495517Snate@binkert.org            MakeAction(makeTheISA, Transform("CFG ISA", 0)))
3506143Snate@binkert.org
3516143Snate@binkert.org########################################################################
3526143Snate@binkert.org#
3536727Ssteve.reinhardt@amd.com# Prevent any SimObjects from being added after this point, they
3545517Snate@binkert.org# should all have been added in the SConscripts above
3556727Ssteve.reinhardt@amd.com#
3565517Snate@binkert.orgSimObject.fixed = True
3575517Snate@binkert.org
3585517Snate@binkert.orgclass DictImporter(object):
3596654Snate@binkert.org    '''This importer takes a dictionary of arbitrary module names that
3606654Snate@binkert.org    map to arbitrary filenames.'''
3617673Snate@binkert.org    def __init__(self, modules):
3626654Snate@binkert.org        self.modules = modules
3636654Snate@binkert.org        self.installed = set()
3646654Snate@binkert.org
3656654Snate@binkert.org    def __del__(self):
3665517Snate@binkert.org        self.unload()
3675517Snate@binkert.org
3685517Snate@binkert.org    def unload(self):
3696143Snate@binkert.org        import sys
3705517Snate@binkert.org        for module in self.installed:
3714762Snate@binkert.org            del sys.modules[module]
3725517Snate@binkert.org        self.installed = set()
3735517Snate@binkert.org
3746143Snate@binkert.org    def find_module(self, fullname, path):
3756143Snate@binkert.org        if fullname == 'm5.defines':
3765517Snate@binkert.org            return self
3775517Snate@binkert.org
3785517Snate@binkert.org        if fullname == 'm5.objects':
3795517Snate@binkert.org            return self
3805517Snate@binkert.org
3815517Snate@binkert.org        if fullname.startswith('m5.internal'):
3825517Snate@binkert.org            return None
3835517Snate@binkert.org
3845517Snate@binkert.org        source = self.modules.get(fullname, None)
3855517Snate@binkert.org        if source is not None and fullname.startswith('m5.objects'):
3866143Snate@binkert.org            return self
3875517Snate@binkert.org
3886654Snate@binkert.org        return None
3896654Snate@binkert.org
3906654Snate@binkert.org    def load_module(self, fullname):
3916654Snate@binkert.org        mod = imp.new_module(fullname)
3926654Snate@binkert.org        sys.modules[fullname] = mod
3936654Snate@binkert.org        self.installed.add(fullname)
3945517Snate@binkert.org
3955517Snate@binkert.org        mod.__loader__ = self
3965517Snate@binkert.org        if fullname == 'm5.objects':
3975517Snate@binkert.org            mod.__path__ = fullname.split('.')
3985517Snate@binkert.org            return mod
3994762Snate@binkert.org
4004762Snate@binkert.org        if fullname == 'm5.defines':
4014762Snate@binkert.org            mod.__dict__['buildEnv'] = m5.util.SmartDict(build_env)
4024762Snate@binkert.org            return mod
4034762Snate@binkert.org
4044762Snate@binkert.org        source = self.modules[fullname]
4057675Snate@binkert.org        if source.modname == '__init__':
4064762Snate@binkert.org            mod.__path__ = source.modpath
4074762Snate@binkert.org        mod.__file__ = source.abspath
4084762Snate@binkert.org
4094762Snate@binkert.org        exec file(source.abspath, 'r') in mod.__dict__
4104382Sbinkertn@umich.edu
4114382Sbinkertn@umich.edu        return mod
4125517Snate@binkert.org
4136654Snate@binkert.orgimport m5.SimObject
4145517Snate@binkert.orgimport m5.params
4158126Sgblack@eecs.umich.edufrom m5.util import code_formatter
4166654Snate@binkert.org
4177673Snate@binkert.orgm5.SimObject.clear()
4186654Snate@binkert.orgm5.params.clear()
4196654Snate@binkert.org
4206654Snate@binkert.org# install the python importer so we can grab stuff from the source
4216654Snate@binkert.org# tree itself.  We can't have SimObjects added after this point or
4226654Snate@binkert.org# else we won't know about them for the rest of the stuff.
4236654Snate@binkert.orgimporter = DictImporter(PySource.modules)
4246654Snate@binkert.orgsys.meta_path[0:0] = [ importer ]
4256669Snate@binkert.org
4266669Snate@binkert.org# import all sim objects so we can populate the all_objects list
4276669Snate@binkert.org# make sure that we're working with a list, then let's sort it
4286669Snate@binkert.orgfor modname in SimObject.modnames:
4296669Snate@binkert.org    exec('from m5.objects import %s' % modname)
4306669Snate@binkert.org
4316654Snate@binkert.org# we need to unload all of the currently imported modules so that they
4327673Snate@binkert.org# will be re-imported the next time the sconscript is run
4335517Snate@binkert.orgimporter.unload()
4348126Sgblack@eecs.umich.edusys.meta_path.remove(importer)
4355798Snate@binkert.org
4367756SAli.Saidi@ARM.comsim_objects = m5.SimObject.allClasses
4377816Ssteve.reinhardt@amd.comall_enums = m5.params.allEnums
4385798Snate@binkert.org
4395798Snate@binkert.orgall_params = {}
4405517Snate@binkert.orgfor name,obj in sorted(sim_objects.iteritems()):
4415517Snate@binkert.org    for param in obj._params.local.values():
4427673Snate@binkert.org        # load the ptype attribute now because it depends on the
4435517Snate@binkert.org        # current version of SimObject.allClasses, but when scons
4445517Snate@binkert.org        # actually uses the value, all versions of
4457673Snate@binkert.org        # SimObject.allClasses will have been loaded
4467673Snate@binkert.org        param.ptype
4475517Snate@binkert.org
4485798Snate@binkert.org        if not hasattr(param, 'swig_decl'):
4495798Snate@binkert.org            continue
4507974Sgblack@eecs.umich.edu        pname = param.ptype_str
4517816Ssteve.reinhardt@amd.com        if pname not in all_params:
4525798Snate@binkert.org            all_params[pname] = param
4535798Snate@binkert.org
4544762Snate@binkert.org########################################################################
4554762Snate@binkert.org#
4564762Snate@binkert.org# calculate extra dependencies
4574762Snate@binkert.org#
4584762Snate@binkert.orgmodule_depends = ["m5", "m5.SimObject", "m5.params"]
4595517Snate@binkert.orgdepends = [ PySource.modules[dep].snode for dep in module_depends ]
4605517Snate@binkert.org
4615517Snate@binkert.org########################################################################
4625517Snate@binkert.org#
4635517Snate@binkert.org# Commands for the basic automatically generated python files
4645517Snate@binkert.org#
4657673Snate@binkert.org
4667673Snate@binkert.org# Generate Python file containing a dict specifying the current
4677673Snate@binkert.org# buildEnv flags.
4685517Snate@binkert.orgdef makeDefinesPyFile(target, source, env):
4695517Snate@binkert.org    build_env = source[0].get_contents()
4705517Snate@binkert.org
4715517Snate@binkert.org    code = code_formatter()
4725517Snate@binkert.org    code("""
4735517Snate@binkert.orgimport m5.internal
4745517Snate@binkert.orgimport m5.util
4757673Snate@binkert.org
4767677Snate@binkert.orgbuildEnv = m5.util.SmartDict($build_env)
4777673Snate@binkert.org
4787673Snate@binkert.orgcompileDate = m5.internal.core.compileDate
4795517Snate@binkert.org_globals = globals()
4805517Snate@binkert.orgfor key,val in m5.internal.core.__dict__.iteritems():
4815517Snate@binkert.org    if key.startswith('flag_'):
4825517Snate@binkert.org        flag = key[5:]
4835517Snate@binkert.org        _globals[flag] = val
4845517Snate@binkert.orgdel _globals
4855517Snate@binkert.org""")
4867673Snate@binkert.org    code.write(target[0].abspath)
4877673Snate@binkert.org
4887673Snate@binkert.orgdefines_info = Value(build_env)
4895517Snate@binkert.org# Generate a file with all of the compile options in it
4905517Snate@binkert.orgenv.Command('python/m5/defines.py', defines_info,
4915517Snate@binkert.org            MakeAction(makeDefinesPyFile, Transform("DEFINES", 0)))
4925517Snate@binkert.orgPySource('m5', 'python/m5/defines.py')
4935517Snate@binkert.org
4945517Snate@binkert.org# Generate python file containing info about the M5 source code
4955517Snate@binkert.orgdef makeInfoPyFile(target, source, env):
4967673Snate@binkert.org    code = code_formatter()
4977673Snate@binkert.org    for src in source:
4987673Snate@binkert.org        data = ''.join(file(src.srcnode().abspath, 'r').xreadlines())
4995517Snate@binkert.org        code('$src = ${{repr(data)}}')
5007675Snate@binkert.org    code.write(str(target[0]))
5017675Snate@binkert.org
5027675Snate@binkert.org# Generate a file that wraps the basic top level files
5037675Snate@binkert.orgenv.Command('python/m5/info.py',
5047675Snate@binkert.org            [ '#/AUTHORS', '#/LICENSE', '#/README', ],
5057675Snate@binkert.org            MakeAction(makeInfoPyFile, Transform("INFO")))
5067675Snate@binkert.orgPySource('m5', 'python/m5/info.py')
5077675Snate@binkert.org
5087677Snate@binkert.org########################################################################
5097675Snate@binkert.org#
5107675Snate@binkert.org# Create all of the SimObject param headers and enum headers
5117675Snate@binkert.org#
5127675Snate@binkert.org
5137675Snate@binkert.orgdef createSimObjectParam(target, source, env):
5147675Snate@binkert.org    assert len(target) == 1 and len(source) == 1
5157675Snate@binkert.org
5167675Snate@binkert.org    name = str(source[0].get_contents())
5177675Snate@binkert.org    obj = sim_objects[name]
5184762Snate@binkert.org
5194762Snate@binkert.org    code = code_formatter()
5206143Snate@binkert.org    obj.cxx_decl(code)
5216143Snate@binkert.org    code.write(target[0].abspath)
5226143Snate@binkert.org
5234762Snate@binkert.orgdef createSwigParam(target, source, env):
5244762Snate@binkert.org    assert len(target) == 1 and len(source) == 1
5254762Snate@binkert.org
5267756SAli.Saidi@ARM.com    name = str(source[0].get_contents())
5277816Ssteve.reinhardt@amd.com    param = all_params[name]
5284762Snate@binkert.org
5294762Snate@binkert.org    code = code_formatter()
5304762Snate@binkert.org    code('%module(package="m5.internal") $0_${name}', param.file_ext)
5315463Snate@binkert.org    param.swig_decl(code)
5325517Snate@binkert.org    code.write(target[0].abspath)
5337677Snate@binkert.org
5345463Snate@binkert.orgdef createEnumStrings(target, source, env):
5357756SAli.Saidi@ARM.com    assert len(target) == 1 and len(source) == 1
5367816Ssteve.reinhardt@amd.com
5374762Snate@binkert.org    name = str(source[0].get_contents())
5387677Snate@binkert.org    obj = all_enums[name]
5394762Snate@binkert.org
5404762Snate@binkert.org    code = code_formatter()
5416143Snate@binkert.org    obj.cxx_def(code)
5426143Snate@binkert.org    code.write(target[0].abspath)
5436143Snate@binkert.org
5444762Snate@binkert.orgdef createEnumParam(target, source, env):
5454762Snate@binkert.org    assert len(target) == 1 and len(source) == 1
5467756SAli.Saidi@ARM.com
5477816Ssteve.reinhardt@amd.com    name = str(source[0].get_contents())
5484762Snate@binkert.org    obj = all_enums[name]
5494762Snate@binkert.org
5504762Snate@binkert.org    code = code_formatter()
5514762Snate@binkert.org    obj.cxx_decl(code)
5527756SAli.Saidi@ARM.com    code.write(target[0].abspath)
5537816Ssteve.reinhardt@amd.com
5544762Snate@binkert.orgdef createEnumSwig(target, source, env):
5554762Snate@binkert.org    assert len(target) == 1 and len(source) == 1
5567677Snate@binkert.org
5577756SAli.Saidi@ARM.com    name = str(source[0].get_contents())
5587816Ssteve.reinhardt@amd.com    obj = all_enums[name]
5597675Snate@binkert.org
5607677Snate@binkert.org    code = code_formatter()
5615517Snate@binkert.org    code('''\
5627675Snate@binkert.org%module(package="m5.internal") enum_$name
5637675Snate@binkert.org
5647675Snate@binkert.org%{
5657675Snate@binkert.org#include "enums/$name.hh"
5667675Snate@binkert.org%}
5677675Snate@binkert.org
5687675Snate@binkert.org%include "enums/$name.hh"
5695517Snate@binkert.org''')
5707673Snate@binkert.org    code.write(target[0].abspath)
5715517Snate@binkert.org
5727677Snate@binkert.org# Generate all of the SimObject param struct header files
5737675Snate@binkert.orgparams_hh_files = []
5747673Snate@binkert.orgfor name,simobj in sorted(sim_objects.iteritems()):
5757675Snate@binkert.org    py_source = PySource.modules[simobj.__module__]
5767675Snate@binkert.org    extra_deps = [ py_source.tnode ]
5777675Snate@binkert.org
5787673Snate@binkert.org    hh_file = File('params/%s.hh' % name)
5797675Snate@binkert.org    params_hh_files.append(hh_file)
5805517Snate@binkert.org    env.Command(hh_file, Value(name),
5817675Snate@binkert.org                MakeAction(createSimObjectParam, Transform("SO PARAM")))
5827675Snate@binkert.org    env.Depends(hh_file, depends + extra_deps)
5837673Snate@binkert.org
5847675Snate@binkert.org# Generate any parameter header files needed
5857675Snate@binkert.orgparams_i_files = []
5867677Snate@binkert.orgfor name,param in all_params.iteritems():
5877675Snate@binkert.org    i_file = File('python/m5/internal/%s_%s.i' % (param.file_ext, name))
5887675Snate@binkert.org    params_i_files.append(i_file)
5897675Snate@binkert.org    env.Command(i_file, Value(name),
5905517Snate@binkert.org                MakeAction(createSwigParam, Transform("SW PARAM")))
5917675Snate@binkert.org    env.Depends(i_file, depends)
5925517Snate@binkert.org    SwigSource('m5.internal', i_file)
5937673Snate@binkert.org
5945517Snate@binkert.org# Generate all enum header files
5957675Snate@binkert.orgfor name,enum in sorted(all_enums.iteritems()):
5967677Snate@binkert.org    py_source = PySource.modules[enum.__module__]
5977756SAli.Saidi@ARM.com    extra_deps = [ py_source.tnode ]
5987816Ssteve.reinhardt@amd.com
5997675Snate@binkert.org    cc_file = File('enums/%s.cc' % name)
6007677Snate@binkert.org    env.Command(cc_file, Value(name),
6014762Snate@binkert.org                MakeAction(createEnumStrings, Transform("ENUM STR")))
6027674Snate@binkert.org    env.Depends(cc_file, depends + extra_deps)
6037674Snate@binkert.org    Source(cc_file)
6047674Snate@binkert.org
6057674Snate@binkert.org    hh_file = File('enums/%s.hh' % name)
6067674Snate@binkert.org    env.Command(hh_file, Value(name),
6077674Snate@binkert.org                MakeAction(createEnumParam, Transform("EN PARAM")))
6087674Snate@binkert.org    env.Depends(hh_file, depends + extra_deps)
6097674Snate@binkert.org
6107674Snate@binkert.org    i_file = File('python/m5/internal/enum_%s.i' % name)
6117674Snate@binkert.org    env.Command(i_file, Value(name),
6127674Snate@binkert.org                MakeAction(createEnumSwig, Transform("ENUMSWIG")))
6137674Snate@binkert.org    env.Depends(i_file, depends + extra_deps)
6147674Snate@binkert.org    SwigSource('m5.internal', i_file)
6157674Snate@binkert.org
6167674Snate@binkert.orgdef buildParam(target, source, env):
6174762Snate@binkert.org    name = source[0].get_contents()
6186143Snate@binkert.org    obj = sim_objects[name]
6196143Snate@binkert.org    class_path = obj.cxx_class.split('::')
6207756SAli.Saidi@ARM.com    classname = class_path[-1]
6217816Ssteve.reinhardt@amd.com    namespaces = class_path[:-1]
6227674Snate@binkert.org    params = obj._params.local.values()
6237756SAli.Saidi@ARM.com
6247816Ssteve.reinhardt@amd.com    code = code_formatter()
6257674Snate@binkert.org
6264382Sbinkertn@umich.edu    code('%module(package="m5.internal") param_$name')
6278232Snate@binkert.org    code()
6288232Snate@binkert.org    code('%{')
6298232Snate@binkert.org    code('#include "params/$obj.hh"')
6308232Snate@binkert.org    for param in params:
6318232Snate@binkert.org        param.cxx_predecls(code)
6326229Snate@binkert.org    code('%}')
6338232Snate@binkert.org    code()
6348232Snate@binkert.org
6358232Snate@binkert.org    for param in params:
6366229Snate@binkert.org        param.swig_predecls(code)
6377673Snate@binkert.org
6385517Snate@binkert.org    code()
6395517Snate@binkert.org    if obj._base:
6407673Snate@binkert.org        code('%import "python/m5/internal/param_${{obj._base}}.i"')
6415517Snate@binkert.org    code()
6425517Snate@binkert.org    obj.swig_objdecls(code)
6435517Snate@binkert.org    code()
6445517Snate@binkert.org
6458232Snate@binkert.org    code('%include "params/$obj.hh"')
6467673Snate@binkert.org
6477673Snate@binkert.org    code.write(target[0].abspath)
6488232Snate@binkert.org
6498232Snate@binkert.orgfor name in sim_objects.iterkeys():
6508232Snate@binkert.org    params_file = File('python/m5/internal/param_%s.i' % name)
6518232Snate@binkert.org    env.Command(params_file, Value(name),
6527673Snate@binkert.org                MakeAction(buildParam, Transform("BLDPARAM")))
6535517Snate@binkert.org    env.Depends(params_file, depends)
6548232Snate@binkert.org    SwigSource('m5.internal', params_file)
6558232Snate@binkert.org
6568232Snate@binkert.org# Generate the main swig init file
6578232Snate@binkert.orgdef makeEmbeddedSwigInit(target, source, env):
6587673Snate@binkert.org    code = code_formatter()
6598232Snate@binkert.org    module = source[0].get_contents()
6608232Snate@binkert.org    code('''\
6618232Snate@binkert.org#include "sim/init.hh"
6628232Snate@binkert.org
6638232Snate@binkert.orgextern "C" {
6648232Snate@binkert.org    void init_${module}();
6657673Snate@binkert.org}
6665517Snate@binkert.org
6678232Snate@binkert.orgEmbeddedSwig embed_swig_${module}(init_${module});
6688232Snate@binkert.org''')
6695517Snate@binkert.org    code.write(str(target[0]))
6707673Snate@binkert.org    
6715517Snate@binkert.org# Build all swig modules
6728232Snate@binkert.orgfor swig in SwigSource.all:
6738232Snate@binkert.org    env.Command([swig.cc_source.tnode, swig.py_source.tnode], swig.tnode,
6745517Snate@binkert.org                MakeAction('$SWIG $SWIGFLAGS -outdir ${TARGETS[1].dir} '
6758232Snate@binkert.org                '-o ${TARGETS[0]} $SOURCES', Transform("SWIG")))
6768232Snate@binkert.org    init_file = 'python/swig/init_%s.cc' % swig.module
6778232Snate@binkert.org    env.Command(init_file, Value(swig.module),
6787673Snate@binkert.org                MakeAction(makeEmbeddedSwigInit, Transform("EMBED SW")))
6795517Snate@binkert.org    Source(init_file)
6805517Snate@binkert.org
6817673Snate@binkert.org#
6825517Snate@binkert.org# Handle debug flags
6835517Snate@binkert.org#
6845517Snate@binkert.orgdef makeDebugFlagCC(target, source, env):
6858232Snate@binkert.org    assert(len(target) == 1 and len(source) == 1)
6865517Snate@binkert.org
6875517Snate@binkert.org    val = eval(source[0].get_contents())
6888232Snate@binkert.org    name, compound, desc = val
6898232Snate@binkert.org    compound = list(sorted(compound))
6905517Snate@binkert.org
6918232Snate@binkert.org    code = code_formatter()
6928232Snate@binkert.org
6935517Snate@binkert.org    # file header
6948232Snate@binkert.org    code('''
6958232Snate@binkert.org/*
6968232Snate@binkert.org * DO NOT EDIT THIS FILE! Automatically generated
6975517Snate@binkert.org */
6988232Snate@binkert.org
6998232Snate@binkert.org#include "base/debug.hh"
7008232Snate@binkert.org''')
7018232Snate@binkert.org
7028232Snate@binkert.org    for flag in compound:
7038232Snate@binkert.org        code('#include "debug/$flag.hh"')
7045517Snate@binkert.org    code()
7058232Snate@binkert.org    code('namespace Debug {')
7068232Snate@binkert.org    code()
7075517Snate@binkert.org
7088232Snate@binkert.org    if not compound:
7097673Snate@binkert.org        code('SimpleFlag $name("$name", "$desc");')
7105517Snate@binkert.org    else:
7117673Snate@binkert.org        code('CompoundFlag $name("$name", "$desc",')
7125517Snate@binkert.org        code.indent()
7138232Snate@binkert.org        last = len(compound) - 1
7148232Snate@binkert.org        for i,flag in enumerate(compound):
7158232Snate@binkert.org            if i != last:
7165192Ssaidi@eecs.umich.edu                code('$flag,')
7178232Snate@binkert.org            else:
7188232Snate@binkert.org                code('$flag);')
7198232Snate@binkert.org        code.dedent()
7208232Snate@binkert.org
7218232Snate@binkert.org    code()
7225192Ssaidi@eecs.umich.edu    code('} // namespace Debug')
7237674Snate@binkert.org
7245522Snate@binkert.org    code.write(str(target[0]))
7255522Snate@binkert.org
7267674Snate@binkert.orgdef makeDebugFlagHH(target, source, env):
7277674Snate@binkert.org    assert(len(target) == 1 and len(source) == 1)
7287674Snate@binkert.org
7297674Snate@binkert.org    val = eval(source[0].get_contents())
7307674Snate@binkert.org    name, compound, desc = val
7317674Snate@binkert.org
7327674Snate@binkert.org    code = code_formatter()
7337674Snate@binkert.org
7345522Snate@binkert.org    # file header boilerplate
7355522Snate@binkert.org    code('''\
7365522Snate@binkert.org/*
7375517Snate@binkert.org * DO NOT EDIT THIS FILE!
7385522Snate@binkert.org *
7395517Snate@binkert.org * Automatically generated by SCons
7406143Snate@binkert.org */
7416727Ssteve.reinhardt@amd.com
7425522Snate@binkert.org#ifndef __DEBUG_${name}_HH__
7435522Snate@binkert.org#define __DEBUG_${name}_HH__
7445522Snate@binkert.org
7457674Snate@binkert.orgnamespace Debug {
7465517Snate@binkert.org''')
7477673Snate@binkert.org
7487673Snate@binkert.org    if compound:
7497674Snate@binkert.org        code('class CompoundFlag;')
7507673Snate@binkert.org    code('class SimpleFlag;')
7517674Snate@binkert.org
7527674Snate@binkert.org    if compound:
7537674Snate@binkert.org        code('extern CompoundFlag $name;')
7547674Snate@binkert.org        for flag in compound:
7557674Snate@binkert.org            code('extern SimpleFlag $flag;')
7567674Snate@binkert.org    else:
7575522Snate@binkert.org        code('extern SimpleFlag $name;')
7585522Snate@binkert.org
7597674Snate@binkert.org    code('''
7607674Snate@binkert.org}
7617674Snate@binkert.org
7627674Snate@binkert.org#endif // __DEBUG_${name}_HH__
7637673Snate@binkert.org''')
7647674Snate@binkert.org
7657674Snate@binkert.org    code.write(str(target[0]))
7667674Snate@binkert.org
7677674Snate@binkert.orgfor name,flag in sorted(debug_flags.iteritems()):
7687674Snate@binkert.org    n, compound, desc = flag
7697674Snate@binkert.org    assert n == name
7707674Snate@binkert.org
7717674Snate@binkert.org    env.Command('debug/%s.hh' % name, Value(flag),
7727811Ssteve.reinhardt@amd.com                MakeAction(makeDebugFlagHH, Transform("TRACING", 0)))
7737674Snate@binkert.org    env.Command('debug/%s.cc' % name, Value(flag),
7747673Snate@binkert.org                MakeAction(makeDebugFlagCC, Transform("TRACING", 0)))
7755522Snate@binkert.org    Source('debug/%s.cc' % name)
7766143Snate@binkert.org
7777756SAli.Saidi@ARM.com# Embed python files.  All .py files that have been indicated by a
7787816Ssteve.reinhardt@amd.com# PySource() call in a SConscript need to be embedded into the M5
7797674Snate@binkert.org# library.  To do that, we compile the file to byte code, marshal the
7804382Sbinkertn@umich.edu# byte code, compress it, and then generate a c++ file that
7814382Sbinkertn@umich.edu# inserts the result into an array.
7824382Sbinkertn@umich.edudef embedPyFile(target, source, env):
7834382Sbinkertn@umich.edu    def c_str(string):
7844382Sbinkertn@umich.edu        if string is None:
7854382Sbinkertn@umich.edu            return "0"
7864382Sbinkertn@umich.edu        return '"%s"' % string
7874382Sbinkertn@umich.edu
7884382Sbinkertn@umich.edu    '''Action function to compile a .py into a code object, marshal
7894382Sbinkertn@umich.edu    it, compress it, and stick it into an asm file so the code appears
7906143Snate@binkert.org    as just bytes with a label in the data section'''
791955SN/A
7922655Sstever@eecs.umich.edu    src = file(str(source[0]), 'r').read()
7932655Sstever@eecs.umich.edu
7942655Sstever@eecs.umich.edu    pysource = PySource.tnodes[source[0]]
7952655Sstever@eecs.umich.edu    compiled = compile(src, pysource.abspath, 'exec')
7962655Sstever@eecs.umich.edu    marshalled = marshal.dumps(compiled)
7975601Snate@binkert.org    compressed = zlib.compress(marshalled)
7985601Snate@binkert.org    data = compressed
7995601Snate@binkert.org    sym = pysource.symname
8005601Snate@binkert.org
8015522Snate@binkert.org    code = code_formatter()
8025863Snate@binkert.org    code('''\
8035601Snate@binkert.org#include "sim/init.hh"
8045601Snate@binkert.org
8055601Snate@binkert.orgnamespace {
8065863Snate@binkert.org
8076143Snate@binkert.orgconst char data_${sym}[] = {
8085559Snate@binkert.org''')
8095559Snate@binkert.org    code.indent()
8105559Snate@binkert.org    step = 16
8115559Snate@binkert.org    for i in xrange(0, len(data), step):
8125601Snate@binkert.org        x = array.array('B', data[i:i+step])
8136143Snate@binkert.org        code(''.join('%d,' % d for d in x))
8146143Snate@binkert.org    code.dedent()
8156143Snate@binkert.org    
8166143Snate@binkert.org    code('''};
8176143Snate@binkert.org
8186143Snate@binkert.orgEmbeddedPython embedded_${sym}(
8196143Snate@binkert.org    ${{c_str(pysource.arcname)}},
8206143Snate@binkert.org    ${{c_str(pysource.abspath)}},
8216143Snate@binkert.org    ${{c_str(pysource.modpath)}},
8226143Snate@binkert.org    data_${sym},
8236143Snate@binkert.org    ${{len(data)}},
8246143Snate@binkert.org    ${{len(marshalled)}});
8256143Snate@binkert.org
8266143Snate@binkert.org} // anonymous namespace
8276143Snate@binkert.org''')
8286143Snate@binkert.org    code.write(str(target[0]))
8296143Snate@binkert.org
8306143Snate@binkert.orgfor source in PySource.all:
8316143Snate@binkert.org    env.Command(source.cpp, source.tnode, 
8326143Snate@binkert.org                MakeAction(embedPyFile, Transform("EMBED PY")))
8336143Snate@binkert.org    Source(source.cpp)
8346143Snate@binkert.org
8356143Snate@binkert.org########################################################################
8366143Snate@binkert.org#
8376143Snate@binkert.org# Define binaries.  Each different build type (debug, opt, etc.) gets
8386143Snate@binkert.org# a slightly different build environment.
8396143Snate@binkert.org#
8406143Snate@binkert.org
8416143Snate@binkert.org# List of constructed environments to pass back to SConstruct
8426143Snate@binkert.orgenvList = []
8436143Snate@binkert.org
8446143Snate@binkert.orgdate_source = Source('base/date.cc', skip_lib=True)
8456240Snate@binkert.org
8465554Snate@binkert.org# Function to create a new build environment as clone of current
8475522Snate@binkert.org# environment 'env' with modified object suffix and optional stripped
8485522Snate@binkert.org# binary.  Additional keyword arguments are appended to corresponding
8495797Snate@binkert.org# build environment vars.
8505797Snate@binkert.orgdef makeEnv(label, objsfx, strip = False, **kwargs):
8515522Snate@binkert.org    # SCons doesn't know to append a library suffix when there is a '.' in the
8525584Snate@binkert.org    # name.  Use '_' instead.
8536143Snate@binkert.org    libname = 'm5_' + label
8545862Snate@binkert.org    exename = 'm5.' + label
8555584Snate@binkert.org
8565601Snate@binkert.org    new_env = env.Clone(OBJSUFFIX=objsfx, SHOBJSUFFIX=objsfx + 's')
8576143Snate@binkert.org    new_env.Label = label
8586143Snate@binkert.org    new_env.Append(**kwargs)
8592655Sstever@eecs.umich.edu
8606143Snate@binkert.org    swig_env = new_env.Clone()
8616143Snate@binkert.org    swig_env.Append(CCFLAGS='-Werror')
8626143Snate@binkert.org    if env['GCC']:
8636143Snate@binkert.org        swig_env.Append(CCFLAGS='-Wno-uninitialized')
8646143Snate@binkert.org        swig_env.Append(CCFLAGS='-Wno-sign-compare')
8654007Ssaidi@eecs.umich.edu        swig_env.Append(CCFLAGS='-Wno-parentheses')
8664596Sbinkertn@umich.edu
8674007Ssaidi@eecs.umich.edu    werror_env = new_env.Clone()
8684596Sbinkertn@umich.edu    werror_env.Append(CCFLAGS='-Werror')
8697756SAli.Saidi@ARM.com
8707816Ssteve.reinhardt@amd.com    def make_obj(source, static, extra_deps = None):
8715522Snate@binkert.org        '''This function adds the specified source to the correct
8725601Snate@binkert.org        build environment, and returns the corresponding SCons Object
8735601Snate@binkert.org        nodes'''
8742655Sstever@eecs.umich.edu
875955SN/A        if source.swig:
8763918Ssaidi@eecs.umich.edu            env = swig_env
8773918Ssaidi@eecs.umich.edu        elif source.Werror:
8783918Ssaidi@eecs.umich.edu            env = werror_env
8793918Ssaidi@eecs.umich.edu        else:
8803918Ssaidi@eecs.umich.edu            env = new_env
8813918Ssaidi@eecs.umich.edu
8823918Ssaidi@eecs.umich.edu        if static:
8833918Ssaidi@eecs.umich.edu            obj = env.StaticObject(source.tnode)
8843918Ssaidi@eecs.umich.edu        else:
8853918Ssaidi@eecs.umich.edu            obj = env.SharedObject(source.tnode)
8863918Ssaidi@eecs.umich.edu
8873918Ssaidi@eecs.umich.edu        if extra_deps:
8883918Ssaidi@eecs.umich.edu            env.Depends(obj, extra_deps)
8893918Ssaidi@eecs.umich.edu
8903940Ssaidi@eecs.umich.edu        return obj
8913940Ssaidi@eecs.umich.edu
8923940Ssaidi@eecs.umich.edu    sources = Source.get(main=False, skip_lib=False)
8933942Ssaidi@eecs.umich.edu    static_objs = [ make_obj(s, True) for s in sources ]
8943940Ssaidi@eecs.umich.edu    shared_objs = [ make_obj(s, False) for s in sources ]
8953515Ssaidi@eecs.umich.edu
8963918Ssaidi@eecs.umich.edu    static_date = make_obj(date_source, static=True, extra_deps=static_objs)
8974762Snate@binkert.org    static_objs.append(static_date)
8983515Ssaidi@eecs.umich.edu    
8992655Sstever@eecs.umich.edu    shared_date = make_obj(date_source, static=False, extra_deps=shared_objs)
9003918Ssaidi@eecs.umich.edu    shared_objs.append(shared_date)
9013619Sbinkertn@umich.edu
902955SN/A    # First make a library of everything but main() so other programs can
903955SN/A    # link against m5.
9042655Sstever@eecs.umich.edu    static_lib = new_env.StaticLibrary(libname, static_objs)
9053918Ssaidi@eecs.umich.edu    shared_lib = new_env.SharedLibrary(libname, shared_objs)
9063619Sbinkertn@umich.edu
907955SN/A    for target, sources in unit_tests:
908955SN/A        objs = [ make_obj(s, static=True) for s in sources ]
9092655Sstever@eecs.umich.edu        new_env.Program("unittest/%s.%s" % (target, label), objs + static_objs)
9103918Ssaidi@eecs.umich.edu
9113619Sbinkertn@umich.edu    # Now link a stub with main() and the static library.
912955SN/A    main_objs = [ make_obj(s, True) for s in Source.get(main=True) ]
913955SN/A
9142655Sstever@eecs.umich.edu    progname = exename
9153918Ssaidi@eecs.umich.edu    if strip:
9163683Sstever@eecs.umich.edu        progname += '.unstripped'
9172655Sstever@eecs.umich.edu
9181869SN/A    targets = new_env.Program(progname, main_objs + static_objs)
9191869SN/A
920    if strip:
921        if sys.platform == 'sunos5':
922            cmd = 'cp $SOURCE $TARGET; strip $TARGET'
923        else:
924            cmd = 'strip $SOURCE -o $TARGET'
925        targets = new_env.Command(exename, progname,
926                    MakeAction(cmd, Transform("STRIP")))
927            
928    new_env.M5Binary = targets[0]
929    envList.append(new_env)
930
931# Debug binary
932ccflags = {}
933if env['GCC']:
934    if sys.platform == 'sunos5':
935        ccflags['debug'] = '-gstabs+'
936    else:
937        ccflags['debug'] = '-ggdb3'
938    ccflags['opt'] = '-g -O3'
939    ccflags['fast'] = '-O3'
940    ccflags['prof'] = '-O3 -g -pg'
941elif env['SUNCC']:
942    ccflags['debug'] = '-g0'
943    ccflags['opt'] = '-g -O'
944    ccflags['fast'] = '-fast'
945    ccflags['prof'] = '-fast -g -pg'
946elif env['ICC']:
947    ccflags['debug'] = '-g -O0'
948    ccflags['opt'] = '-g -O'
949    ccflags['fast'] = '-fast'
950    ccflags['prof'] = '-fast -g -pg'
951else:
952    print 'Unknown compiler, please fix compiler options'
953    Exit(1)
954
955makeEnv('debug', '.do',
956        CCFLAGS = Split(ccflags['debug']),
957        CPPDEFINES = ['DEBUG', 'TRACING_ON=1'])
958
959# Optimized binary
960makeEnv('opt', '.o',
961        CCFLAGS = Split(ccflags['opt']),
962        CPPDEFINES = ['TRACING_ON=1'])
963
964# "Fast" binary
965makeEnv('fast', '.fo', strip = True,
966        CCFLAGS = Split(ccflags['fast']),
967        CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'])
968
969# Profiled binary
970makeEnv('prof', '.po',
971        CCFLAGS = Split(ccflags['prof']),
972        CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
973        LINKFLAGS = '-pg')
974
975Return('envList')
976