SConscript revision 7756
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
448334Snate@binkert.org# 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
548614Sgblack@eecs.umich.edufrom 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#
598233Snate@binkert.orgclass SourceMeta(type):
608233Snate@binkert.org    def __init__(cls, name, bases, dict):
618233Snate@binkert.org        super(SourceMeta, cls).__init__(name, bases, dict)
628233Snate@binkert.org        cls.all = []
638233Snate@binkert.org        
648334Snate@binkert.org    def get(cls, **kwargs):
658334Snate@binkert.org        for src in cls.all:
6610453SAndrew.Bardsley@arm.com            for attr,value in kwargs.iteritems():
6710453SAndrew.Bardsley@arm.com                if getattr(src, attr) != value:
688233Snate@binkert.org                    break
698233Snate@binkert.org            else:
708233Snate@binkert.org                yield src
718233Snate@binkert.org
728233Snate@binkert.orgclass SourceFile(object):
738233Snate@binkert.org    __metaclass__ = SourceMeta
746143Snate@binkert.org    def __init__(self, source):
758233Snate@binkert.org        tnode = source
768233Snate@binkert.org        if not isinstance(source, SCons.Node.FS.File):
778233Snate@binkert.org            tnode = File(source)
786143Snate@binkert.org
796143Snate@binkert.org        self.tnode = tnode
806143Snate@binkert.org        self.snode = tnode.srcnode()
8111308Santhony.gutierrez@amd.com        self.filename = str(tnode)
828233Snate@binkert.org        self.dirname = dirname(self.filename)
838233Snate@binkert.org        self.basename = basename(self.filename)
848233Snate@binkert.org        index = self.basename.rfind('.')
856143Snate@binkert.org        if index <= 0:
868233Snate@binkert.org            # dot files aren't extensions
878233Snate@binkert.org            self.extname = self.basename, None
888233Snate@binkert.org        else:
898233Snate@binkert.org            self.extname = self.basename[:index], self.basename[index+1:]
906143Snate@binkert.org
916143Snate@binkert.org        for base in type(self).__mro__:
926143Snate@binkert.org            if issubclass(base, SourceFile):
934762Snate@binkert.org                base.all.append(self)
946143Snate@binkert.org
958233Snate@binkert.org    def __lt__(self, other): return self.filename < other.filename
968233Snate@binkert.org    def __le__(self, other): return self.filename <= other.filename
978233Snate@binkert.org    def __gt__(self, other): return self.filename > other.filename
988233Snate@binkert.org    def __ge__(self, other): return self.filename >= other.filename
998233Snate@binkert.org    def __eq__(self, other): return self.filename == other.filename
1006143Snate@binkert.org    def __ne__(self, other): return self.filename != other.filename
1018233Snate@binkert.org        
1028233Snate@binkert.orgclass Source(SourceFile):
1038233Snate@binkert.org    '''Add a c/c++ source file to the build'''
1048233Snate@binkert.org    def __init__(self, source, Werror=True, swig=False, bin_only=False,
1056143Snate@binkert.org                 skip_lib=False):
1066143Snate@binkert.org        super(Source, self).__init__(source)
1076143Snate@binkert.org
1086143Snate@binkert.org        self.Werror = Werror
1096143Snate@binkert.org        self.swig = swig
1106143Snate@binkert.org        self.bin_only = bin_only
1116143Snate@binkert.org        self.skip_lib = bin_only or skip_lib
1126143Snate@binkert.org
1136143Snate@binkert.orgclass PySource(SourceFile):
1147065Snate@binkert.org    '''Add a python source file to the named package'''
1156143Snate@binkert.org    invalid_sym_char = re.compile('[^A-z0-9_]')
1168233Snate@binkert.org    modules = {}
1178233Snate@binkert.org    tnodes = {}
1188233Snate@binkert.org    symnames = {}
1198233Snate@binkert.org    
1208233Snate@binkert.org    def __init__(self, package, source):
1218233Snate@binkert.org        super(PySource, self).__init__(source)
1228233Snate@binkert.org
1238233Snate@binkert.org        modname,ext = self.extname
1248233Snate@binkert.org        assert ext == 'py'
1258233Snate@binkert.org
1268233Snate@binkert.org        if package:
1278233Snate@binkert.org            path = package.split('.')
1288233Snate@binkert.org        else:
1298233Snate@binkert.org            path = []
1308233Snate@binkert.org
1318233Snate@binkert.org        modpath = path[:]
1328233Snate@binkert.org        if modname != '__init__':
1338233Snate@binkert.org            modpath += [ modname ]
1348233Snate@binkert.org        modpath = '.'.join(modpath)
1358233Snate@binkert.org
1368233Snate@binkert.org        arcpath = path + [ self.basename ]
1378233Snate@binkert.org        abspath = self.snode.abspath
1388233Snate@binkert.org        if not exists(abspath):
1398233Snate@binkert.org            abspath = self.tnode.abspath
1408233Snate@binkert.org
1418233Snate@binkert.org        self.package = package
1428233Snate@binkert.org        self.modname = modname
1438233Snate@binkert.org        self.modpath = modpath
1448233Snate@binkert.org        self.arcname = joinpath(*arcpath)
1458233Snate@binkert.org        self.abspath = abspath
1468233Snate@binkert.org        self.compiled = File(self.filename + 'c')
1476143Snate@binkert.org        self.cpp = File(self.filename + '.cc')
1486143Snate@binkert.org        self.symname = PySource.invalid_sym_char.sub('_', modpath)
1496143Snate@binkert.org
1506143Snate@binkert.org        PySource.modules[modpath] = self
1516143Snate@binkert.org        PySource.tnodes[self.tnode] = self
1526143Snate@binkert.org        PySource.symnames[self.symname] = self
1539982Satgutier@umich.edu
15410196SCurtis.Dunham@arm.comclass SimObject(PySource):
15510196SCurtis.Dunham@arm.com    '''Add a SimObject python file as a python source object and add
15610196SCurtis.Dunham@arm.com    it to a list of sim object modules'''
15710196SCurtis.Dunham@arm.com
15810196SCurtis.Dunham@arm.com    fixed = False
15910196SCurtis.Dunham@arm.com    modnames = []
16010196SCurtis.Dunham@arm.com
16110196SCurtis.Dunham@arm.com    def __init__(self, source):
1626143Snate@binkert.org        super(SimObject, self).__init__('m5.objects', source)
1636143Snate@binkert.org        if self.fixed:
1648945Ssteve.reinhardt@amd.com            raise AttributeError, "Too late to call SimObject now."
1658233Snate@binkert.org
1668233Snate@binkert.org        bisect.insort_right(SimObject.modnames, self.modname)
1676143Snate@binkert.org
1688945Ssteve.reinhardt@amd.comclass SwigSource(SourceFile):
1696143Snate@binkert.org    '''Add a swig file to build'''
1706143Snate@binkert.org
1716143Snate@binkert.org    def __init__(self, package, source):
1726143Snate@binkert.org        super(SwigSource, self).__init__(source)
1735522Snate@binkert.org
1746143Snate@binkert.org        modname,ext = self.extname
1756143Snate@binkert.org        assert ext == 'i'
1766143Snate@binkert.org
1779982Satgutier@umich.edu        self.module = modname
1788233Snate@binkert.org        cc_file = joinpath(self.dirname, modname + '_wrap.cc')
1798233Snate@binkert.org        py_file = joinpath(self.dirname, modname + '.py')
1808233Snate@binkert.org
1816143Snate@binkert.org        self.cc_source = Source(cc_file, swig=True)
1826143Snate@binkert.org        self.py_source = PySource(package, py_file)
1836143Snate@binkert.org
1846143Snate@binkert.orgunit_tests = []
1855522Snate@binkert.orgdef UnitTest(target, sources):
1865522Snate@binkert.org    if not isinstance(sources, (list, tuple)):
1875522Snate@binkert.org        sources = [ sources ]
1885522Snate@binkert.org
1895604Snate@binkert.org    sources = [ Source(src, skip_lib=True) for src in sources ]
1905604Snate@binkert.org    unit_tests.append((target, sources))
1916143Snate@binkert.org
1926143Snate@binkert.org# Children should have access
1934762Snate@binkert.orgExport('Source')
1944762Snate@binkert.orgExport('PySource')
1956143Snate@binkert.orgExport('SimObject')
1966727Ssteve.reinhardt@amd.comExport('SwigSource')
1976727Ssteve.reinhardt@amd.comExport('UnitTest')
1986727Ssteve.reinhardt@amd.com
1994762Snate@binkert.org########################################################################
2006143Snate@binkert.org#
2016143Snate@binkert.org# Trace Flags
2026143Snate@binkert.org#
2036143Snate@binkert.orgtrace_flags = {}
2046727Ssteve.reinhardt@amd.comdef TraceFlag(name, desc=None):
2056143Snate@binkert.org    if name in trace_flags:
2067674Snate@binkert.org        raise AttributeError, "Flag %s already specified" % name
2077674Snate@binkert.org    trace_flags[name] = (name, (), desc)
2085604Snate@binkert.org
2096143Snate@binkert.orgdef CompoundFlag(name, flags, desc=None):
2106143Snate@binkert.org    if name in trace_flags:
2116143Snate@binkert.org        raise AttributeError, "Flag %s already specified" % name
2124762Snate@binkert.org
2136143Snate@binkert.org    compound = tuple(flags)
2144762Snate@binkert.org    trace_flags[name] = (name, compound, desc)
2154762Snate@binkert.org
2164762Snate@binkert.orgExport('TraceFlag')
2176143Snate@binkert.orgExport('CompoundFlag')
2186143Snate@binkert.org
2194762Snate@binkert.org########################################################################
2208233Snate@binkert.org#
2218233Snate@binkert.org# Set some compiler variables
2228233Snate@binkert.org#
2238233Snate@binkert.org
2246143Snate@binkert.org# Include file paths are rooted in this directory.  SCons will
2256143Snate@binkert.org# automatically expand '.' to refer to both the source directory and
2264762Snate@binkert.org# the corresponding build directory to pick up generated include
2276143Snate@binkert.org# files.
2284762Snate@binkert.orgenv.Append(CPPPATH=Dir('.'))
2296143Snate@binkert.org
2304762Snate@binkert.orgfor extra_dir in extras_dir_list:
2316143Snate@binkert.org    env.Append(CPPPATH=Dir(extra_dir))
2328233Snate@binkert.org
2338233Snate@binkert.org# Workaround for bug in SCons version > 0.97d20071212
23410453SAndrew.Bardsley@arm.com# Scons bug id: 2006 M5 Bug id: 308 
2356143Snate@binkert.orgfor root, dirs, files in os.walk(base_dir, topdown=True):
2366143Snate@binkert.org    Dir(root[len(base_dir) + 1:])
2376143Snate@binkert.org
2386143Snate@binkert.org########################################################################
2396143Snate@binkert.org#
2406143Snate@binkert.org# Walk the tree and execute all SConscripts in subdirectories
2416143Snate@binkert.org#
2426143Snate@binkert.org
24310453SAndrew.Bardsley@arm.comhere = Dir('.').srcnode().abspath
24410453SAndrew.Bardsley@arm.comfor root, dirs, files in os.walk(base_dir, topdown=True):
245955SN/A    if root == here:
2469396Sandreas.hansson@arm.com        # we don't want to recurse back into this SConscript
2479396Sandreas.hansson@arm.com        continue
2489396Sandreas.hansson@arm.com
2499396Sandreas.hansson@arm.com    if 'SConscript' in files:
2509396Sandreas.hansson@arm.com        build_dir = joinpath(env['BUILDDIR'], root[len(base_dir) + 1:])
2519396Sandreas.hansson@arm.com        SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
2529396Sandreas.hansson@arm.com
2539396Sandreas.hansson@arm.comfor extra_dir in extras_dir_list:
2549396Sandreas.hansson@arm.com    prefix_len = len(dirname(extra_dir)) + 1
2559396Sandreas.hansson@arm.com    for root, dirs, files in os.walk(extra_dir, topdown=True):
2569396Sandreas.hansson@arm.com        if 'SConscript' in files:
2579396Sandreas.hansson@arm.com            build_dir = joinpath(env['BUILDDIR'], root[prefix_len:])
2589396Sandreas.hansson@arm.com            SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
2599930Sandreas.hansson@arm.com
2609930Sandreas.hansson@arm.comfor opt in export_vars:
2619396Sandreas.hansson@arm.com    env.ConfigFile(opt)
2628235Snate@binkert.org
2638235Snate@binkert.orgdef makeTheISA(source, target, env):
2646143Snate@binkert.org    isas = [ src.get_contents() for src in source ]
2658235Snate@binkert.org    target_isa = env['TARGET_ISA']
2669003SAli.Saidi@ARM.com    def define(isa):
2678235Snate@binkert.org        return isa.upper() + '_ISA'
2688235Snate@binkert.org    
2698235Snate@binkert.org    def namespace(isa):
2708235Snate@binkert.org        return isa[0].upper() + isa[1:].lower() + 'ISA' 
2718235Snate@binkert.org
2728235Snate@binkert.org
2738235Snate@binkert.org    code = code_formatter()
2748235Snate@binkert.org    code('''\
2758235Snate@binkert.org#ifndef __CONFIG_THE_ISA_HH__
2768235Snate@binkert.org#define __CONFIG_THE_ISA_HH__
2778235Snate@binkert.org
2788235Snate@binkert.org''')
2798235Snate@binkert.org
2808235Snate@binkert.org    for i,isa in enumerate(isas):
2819003SAli.Saidi@ARM.com        code('#define $0 $1', define(isa), i + 1)
2828235Snate@binkert.org
2835584Snate@binkert.org    code('''
2844382Sbinkertn@umich.edu
2854202Sbinkertn@umich.edu#define THE_ISA ${{define(target_isa)}}
2864382Sbinkertn@umich.edu#define TheISA ${{namespace(target_isa)}}
2874382Sbinkertn@umich.edu
2884382Sbinkertn@umich.edu#endif // __CONFIG_THE_ISA_HH__''')
2899396Sandreas.hansson@arm.com
2905584Snate@binkert.org    code.write(str(target[0]))
2914382Sbinkertn@umich.edu
2924382Sbinkertn@umich.eduenv.Command('config/the_isa.hh', map(Value, all_isa_list),
2934382Sbinkertn@umich.edu            MakeAction(makeTheISA, " [ CFG ISA] $STRIP_TARGET"))
2948232Snate@binkert.org
2955192Ssaidi@eecs.umich.edu########################################################################
2968232Snate@binkert.org#
2978232Snate@binkert.org# Prevent any SimObjects from being added after this point, they
2988232Snate@binkert.org# should all have been added in the SConscripts above
2995192Ssaidi@eecs.umich.edu#
3008232Snate@binkert.orgSimObject.fixed = True
3015192Ssaidi@eecs.umich.edu
3025799Snate@binkert.orgclass DictImporter(object):
3038232Snate@binkert.org    '''This importer takes a dictionary of arbitrary module names that
3045192Ssaidi@eecs.umich.edu    map to arbitrary filenames.'''
3055192Ssaidi@eecs.umich.edu    def __init__(self, modules):
3065192Ssaidi@eecs.umich.edu        self.modules = modules
3078232Snate@binkert.org        self.installed = set()
3085192Ssaidi@eecs.umich.edu
3098232Snate@binkert.org    def __del__(self):
3105192Ssaidi@eecs.umich.edu        self.unload()
3115192Ssaidi@eecs.umich.edu
3125192Ssaidi@eecs.umich.edu    def unload(self):
3135192Ssaidi@eecs.umich.edu        import sys
3144382Sbinkertn@umich.edu        for module in self.installed:
3154382Sbinkertn@umich.edu            del sys.modules[module]
3164382Sbinkertn@umich.edu        self.installed = set()
3172667Sstever@eecs.umich.edu
3182667Sstever@eecs.umich.edu    def find_module(self, fullname, path):
3192667Sstever@eecs.umich.edu        if fullname == 'm5.defines':
3202667Sstever@eecs.umich.edu            return self
3212667Sstever@eecs.umich.edu
3222667Sstever@eecs.umich.edu        if fullname == 'm5.objects':
3235742Snate@binkert.org            return self
3245742Snate@binkert.org
3255742Snate@binkert.org        if fullname.startswith('m5.internal'):
3265793Snate@binkert.org            return None
3278334Snate@binkert.org
3285793Snate@binkert.org        source = self.modules.get(fullname, None)
3295793Snate@binkert.org        if source is not None and fullname.startswith('m5.objects'):
3305793Snate@binkert.org            return self
3314382Sbinkertn@umich.edu
3324762Snate@binkert.org        return None
3335344Sstever@gmail.com
3344382Sbinkertn@umich.edu    def load_module(self, fullname):
3355341Sstever@gmail.com        mod = imp.new_module(fullname)
3365742Snate@binkert.org        sys.modules[fullname] = mod
3375742Snate@binkert.org        self.installed.add(fullname)
3385742Snate@binkert.org
3395742Snate@binkert.org        mod.__loader__ = self
3405742Snate@binkert.org        if fullname == 'm5.objects':
3414762Snate@binkert.org            mod.__path__ = fullname.split('.')
3425742Snate@binkert.org            return mod
3435742Snate@binkert.org
3447722Sgblack@eecs.umich.edu        if fullname == 'm5.defines':
3455742Snate@binkert.org            mod.__dict__['buildEnv'] = m5.util.SmartDict(build_env)
3465742Snate@binkert.org            return mod
3475742Snate@binkert.org
3489930Sandreas.hansson@arm.com        source = self.modules[fullname]
3499930Sandreas.hansson@arm.com        if source.modname == '__init__':
3509930Sandreas.hansson@arm.com            mod.__path__ = source.modpath
3519930Sandreas.hansson@arm.com        mod.__file__ = source.abspath
3529930Sandreas.hansson@arm.com
3535742Snate@binkert.org        exec file(source.abspath, 'r') in mod.__dict__
3548242Sbradley.danofsky@amd.com
3558242Sbradley.danofsky@amd.com        return mod
3568242Sbradley.danofsky@amd.com
3578242Sbradley.danofsky@amd.comimport m5.SimObject
3585341Sstever@gmail.comimport m5.params
3595742Snate@binkert.orgfrom m5.util import code_formatter
3607722Sgblack@eecs.umich.edu
3614773Snate@binkert.orgm5.SimObject.clear()
3626108Snate@binkert.orgm5.params.clear()
3631858SN/A
3641085SN/A# install the python importer so we can grab stuff from the source
3656658Snate@binkert.org# tree itself.  We can't have SimObjects added after this point or
3666658Snate@binkert.org# else we won't know about them for the rest of the stuff.
3677673Snate@binkert.orgimporter = DictImporter(PySource.modules)
3686658Snate@binkert.orgsys.meta_path[0:0] = [ importer ]
3696658Snate@binkert.org
37011308Santhony.gutierrez@amd.com# import all sim objects so we can populate the all_objects list
3716658Snate@binkert.org# make sure that we're working with a list, then let's sort it
37211308Santhony.gutierrez@amd.comfor modname in SimObject.modnames:
3736658Snate@binkert.org    exec('from m5.objects import %s' % modname)
3746658Snate@binkert.org
3757673Snate@binkert.org# we need to unload all of the currently imported modules so that they
3767673Snate@binkert.org# will be re-imported the next time the sconscript is run
3777673Snate@binkert.orgimporter.unload()
3787673Snate@binkert.orgsys.meta_path.remove(importer)
3797673Snate@binkert.org
3807673Snate@binkert.orgsim_objects = m5.SimObject.allClasses
3817673Snate@binkert.orgall_enums = m5.params.allEnums
38210467Sandreas.hansson@arm.com
3836658Snate@binkert.orgall_params = {}
3847673Snate@binkert.orgfor name,obj in sorted(sim_objects.iteritems()):
38510467Sandreas.hansson@arm.com    for param in obj._params.local.values():
38610467Sandreas.hansson@arm.com        # load the ptype attribute now because it depends on the
38710467Sandreas.hansson@arm.com        # current version of SimObject.allClasses, but when scons
38810467Sandreas.hansson@arm.com        # actually uses the value, all versions of
38910467Sandreas.hansson@arm.com        # SimObject.allClasses will have been loaded
39010467Sandreas.hansson@arm.com        param.ptype
39110467Sandreas.hansson@arm.com
39210467Sandreas.hansson@arm.com        if not hasattr(param, 'swig_decl'):
39310467Sandreas.hansson@arm.com            continue
39410467Sandreas.hansson@arm.com        pname = param.ptype_str
39510467Sandreas.hansson@arm.com        if pname not in all_params:
3967673Snate@binkert.org            all_params[pname] = param
3977673Snate@binkert.org
3987673Snate@binkert.org########################################################################
3997673Snate@binkert.org#
4007673Snate@binkert.org# calculate extra dependencies
4019048SAli.Saidi@ARM.com#
4027673Snate@binkert.orgmodule_depends = ["m5", "m5.SimObject", "m5.params"]
4037673Snate@binkert.orgdepends = [ PySource.modules[dep].snode for dep in module_depends ]
4047673Snate@binkert.org
4057673Snate@binkert.org########################################################################
4066658Snate@binkert.org#
4077756SAli.Saidi@ARM.com# Commands for the basic automatically generated python files
4087816Ssteve.reinhardt@amd.com#
4096658Snate@binkert.org
41011308Santhony.gutierrez@amd.com# Generate Python file containing a dict specifying the current
41111308Santhony.gutierrez@amd.com# buildEnv flags.
41211308Santhony.gutierrez@amd.comdef makeDefinesPyFile(target, source, env):
41311308Santhony.gutierrez@amd.com    build_env, hg_info = [ x.get_contents() for x in source ]
41411308Santhony.gutierrez@amd.com
41511308Santhony.gutierrez@amd.com    code = code_formatter()
41611308Santhony.gutierrez@amd.com    code("""
41711308Santhony.gutierrez@amd.comimport m5.internal
41811308Santhony.gutierrez@amd.comimport m5.util
41911308Santhony.gutierrez@amd.com
42011308Santhony.gutierrez@amd.combuildEnv = m5.util.SmartDict($build_env)
42111308Santhony.gutierrez@amd.comhgRev = '$hg_info'
42211308Santhony.gutierrez@amd.com
42311308Santhony.gutierrez@amd.comcompileDate = m5.internal.core.compileDate
42411308Santhony.gutierrez@amd.com_globals = globals()
42511308Santhony.gutierrez@amd.comfor key,val in m5.internal.core.__dict__.iteritems():
42611308Santhony.gutierrez@amd.com    if key.startswith('flag_'):
42711308Santhony.gutierrez@amd.com        flag = key[5:]
42811308Santhony.gutierrez@amd.com        _globals[flag] = val
42911308Santhony.gutierrez@amd.comdel _globals
43011308Santhony.gutierrez@amd.com""")
43111308Santhony.gutierrez@amd.com    code.write(target[0].abspath)
43211308Santhony.gutierrez@amd.com
43311308Santhony.gutierrez@amd.comdefines_info = [ Value(build_env), Value(env['HG_INFO']) ]
43411308Santhony.gutierrez@amd.com# Generate a file with all of the compile options in it
43511308Santhony.gutierrez@amd.comenv.Command('python/m5/defines.py', defines_info,
43611308Santhony.gutierrez@amd.com            MakeAction(makeDefinesPyFile, " [ DEFINES] $STRIP_TARGET"))
43711308Santhony.gutierrez@amd.comPySource('m5', 'python/m5/defines.py')
43811308Santhony.gutierrez@amd.com
43911308Santhony.gutierrez@amd.com# Generate python file containing info about the M5 source code
44011308Santhony.gutierrez@amd.comdef makeInfoPyFile(target, source, env):
44111308Santhony.gutierrez@amd.com    code = code_formatter()
44211308Santhony.gutierrez@amd.com    for src in source:
44311308Santhony.gutierrez@amd.com        data = ''.join(file(src.srcnode().abspath, 'r').xreadlines())
44411308Santhony.gutierrez@amd.com        code('$src = ${{repr(data)}}')
44511308Santhony.gutierrez@amd.com    code.write(str(target[0]))
44611308Santhony.gutierrez@amd.com
44711308Santhony.gutierrez@amd.com# Generate a file that wraps the basic top level files
44811308Santhony.gutierrez@amd.comenv.Command('python/m5/info.py',
44911308Santhony.gutierrez@amd.com            [ '#/AUTHORS', '#/LICENSE', '#/README', '#/RELEASE_NOTES' ],
45011308Santhony.gutierrez@amd.com            MakeAction(makeInfoPyFile, " [    INFO] $STRIP_TARGET"))
45111308Santhony.gutierrez@amd.comPySource('m5', 'python/m5/info.py')
45211308Santhony.gutierrez@amd.com
45311308Santhony.gutierrez@amd.com########################################################################
45411308Santhony.gutierrez@amd.com#
4554382Sbinkertn@umich.edu# Create all of the SimObject param headers and enum headers
4564382Sbinkertn@umich.edu#
4574762Snate@binkert.org
4584762Snate@binkert.orgdef createSimObjectParam(target, source, env):
4594762Snate@binkert.org    assert len(target) == 1 and len(source) == 1
4606654Snate@binkert.org
4616654Snate@binkert.org    name = str(source[0].get_contents())
4625517Snate@binkert.org    obj = sim_objects[name]
4635517Snate@binkert.org
4645517Snate@binkert.org    code = code_formatter()
4655517Snate@binkert.org    obj.cxx_decl(code)
4665517Snate@binkert.org    code.write(target[0].abspath)
4675517Snate@binkert.org
4685517Snate@binkert.orgdef createSwigParam(target, source, env):
4695517Snate@binkert.org    assert len(target) == 1 and len(source) == 1
4705517Snate@binkert.org
4715517Snate@binkert.org    name = str(source[0].get_contents())
4725517Snate@binkert.org    param = all_params[name]
4735517Snate@binkert.org
4745517Snate@binkert.org    code = code_formatter()
4755517Snate@binkert.org    code('%module(package="m5.internal") $0_${name}', param.file_ext)
4765517Snate@binkert.org    param.swig_decl(code)
4775517Snate@binkert.org    code.write(target[0].abspath)
4785517Snate@binkert.org
4796654Snate@binkert.orgdef createEnumStrings(target, source, env):
4805517Snate@binkert.org    assert len(target) == 1 and len(source) == 1
4815517Snate@binkert.org
4825517Snate@binkert.org    name = str(source[0].get_contents())
4835517Snate@binkert.org    obj = all_enums[name]
4845517Snate@binkert.org
4855517Snate@binkert.org    code = code_formatter()
4865517Snate@binkert.org    obj.cxx_def(code)
4875517Snate@binkert.org    code.write(target[0].abspath)
4886143Snate@binkert.org
4896654Snate@binkert.orgdef createEnumParam(target, source, env):
4905517Snate@binkert.org    assert len(target) == 1 and len(source) == 1
4915517Snate@binkert.org
4925517Snate@binkert.org    name = str(source[0].get_contents())
4935517Snate@binkert.org    obj = all_enums[name]
4945517Snate@binkert.org
4955517Snate@binkert.org    code = code_formatter()
4965517Snate@binkert.org    obj.cxx_decl(code)
4975517Snate@binkert.org    code.write(target[0].abspath)
4985517Snate@binkert.org
4995517Snate@binkert.orgdef createEnumSwig(target, source, env):
5005517Snate@binkert.org    assert len(target) == 1 and len(source) == 1
5015517Snate@binkert.org
5025517Snate@binkert.org    name = str(source[0].get_contents())
5035517Snate@binkert.org    obj = all_enums[name]
5046654Snate@binkert.org
5056654Snate@binkert.org    code = code_formatter()
5065517Snate@binkert.org    code('''\
5075517Snate@binkert.org%module(package="m5.internal") enum_$name
5086143Snate@binkert.org
5096143Snate@binkert.org%{
5106143Snate@binkert.org#include "enums/$name.hh"
5116727Ssteve.reinhardt@amd.com%}
5125517Snate@binkert.org
5136727Ssteve.reinhardt@amd.com%include "enums/$name.hh"
5145517Snate@binkert.org''')
5155517Snate@binkert.org    code.write(target[0].abspath)
5165517Snate@binkert.org
5176654Snate@binkert.org# Generate all of the SimObject param struct header files
5186654Snate@binkert.orgparams_hh_files = []
5197673Snate@binkert.orgfor name,simobj in sorted(sim_objects.iteritems()):
5206654Snate@binkert.org    py_source = PySource.modules[simobj.__module__]
5216654Snate@binkert.org    extra_deps = [ py_source.tnode ]
5226654Snate@binkert.org
5236654Snate@binkert.org    hh_file = File('params/%s.hh' % name)
5245517Snate@binkert.org    params_hh_files.append(hh_file)
5255517Snate@binkert.org    env.Command(hh_file, Value(name),
5265517Snate@binkert.org                MakeAction(createSimObjectParam, " [SO PARAM] $STRIP_TARGET"))
5276143Snate@binkert.org    env.Depends(hh_file, depends + extra_deps)
5285517Snate@binkert.org
5294762Snate@binkert.org# Generate any parameter header files needed
5305517Snate@binkert.orgparams_i_files = []
5315517Snate@binkert.orgfor name,param in all_params.iteritems():
5326143Snate@binkert.org    i_file = File('python/m5/internal/%s_%s.i' % (param.file_ext, name))
5336143Snate@binkert.org    params_i_files.append(i_file)
5345517Snate@binkert.org    env.Command(i_file, Value(name),
5355517Snate@binkert.org                MakeAction(createSwigParam, " [SW PARAM] $STRIP_TARGET"))
5365517Snate@binkert.org    env.Depends(i_file, depends)
5375517Snate@binkert.org    SwigSource('m5.internal', i_file)
5385517Snate@binkert.org
5395517Snate@binkert.org# Generate all enum header files
5405517Snate@binkert.orgfor name,enum in sorted(all_enums.iteritems()):
5415517Snate@binkert.org    py_source = PySource.modules[enum.__module__]
5425517Snate@binkert.org    extra_deps = [ py_source.tnode ]
5439338SAndreas.Sandberg@arm.com
5449338SAndreas.Sandberg@arm.com    cc_file = File('enums/%s.cc' % name)
5459338SAndreas.Sandberg@arm.com    env.Command(cc_file, Value(name),
5469338SAndreas.Sandberg@arm.com                MakeAction(createEnumStrings, " [ENUM STR] $STRIP_TARGET"))
5479338SAndreas.Sandberg@arm.com    env.Depends(cc_file, depends + extra_deps)
5489338SAndreas.Sandberg@arm.com    Source(cc_file)
5498596Ssteve.reinhardt@amd.com
5508596Ssteve.reinhardt@amd.com    hh_file = File('enums/%s.hh' % name)
5518596Ssteve.reinhardt@amd.com    env.Command(hh_file, Value(name),
5528596Ssteve.reinhardt@amd.com                MakeAction(createEnumParam, " [EN PARAM] $STRIP_TARGET"))
5538596Ssteve.reinhardt@amd.com    env.Depends(hh_file, depends + extra_deps)
5548596Ssteve.reinhardt@amd.com
5558596Ssteve.reinhardt@amd.com    i_file = File('python/m5/internal/enum_%s.i' % name)
5566143Snate@binkert.org    env.Command(i_file, Value(name),
5575517Snate@binkert.org                MakeAction(createEnumSwig, " [ENUMSWIG] $STRIP_TARGET"))
5586654Snate@binkert.org    env.Depends(i_file, depends + extra_deps)
5596654Snate@binkert.org    SwigSource('m5.internal', i_file)
5606654Snate@binkert.org
5616654Snate@binkert.orgdef buildParam(target, source, env):
5626654Snate@binkert.org    name = source[0].get_contents()
5636654Snate@binkert.org    obj = sim_objects[name]
5645517Snate@binkert.org    class_path = obj.cxx_class.split('::')
5655517Snate@binkert.org    classname = class_path[-1]
5665517Snate@binkert.org    namespaces = class_path[:-1]
5678596Ssteve.reinhardt@amd.com    params = obj._params.local.values()
5688596Ssteve.reinhardt@amd.com
5694762Snate@binkert.org    code = code_formatter()
5704762Snate@binkert.org
5714762Snate@binkert.org    code('%module(package="m5.internal") param_$name')
5724762Snate@binkert.org    code()
5734762Snate@binkert.org    code('%{')
5744762Snate@binkert.org    code('#include "params/$obj.hh"')
5757675Snate@binkert.org    for param in params:
57610584Sandreas.hansson@arm.com        param.cxx_predecls(code)
5774762Snate@binkert.org    code('%}')
5784762Snate@binkert.org    code()
5794762Snate@binkert.org
5804762Snate@binkert.org    for param in params:
5814382Sbinkertn@umich.edu        param.swig_predecls(code)
5824382Sbinkertn@umich.edu
5835517Snate@binkert.org    code()
5846654Snate@binkert.org    if obj._base:
5855517Snate@binkert.org        code('%import "python/m5/internal/param_${{obj._base}}.i"')
5868126Sgblack@eecs.umich.edu    code()
5876654Snate@binkert.org    obj.swig_objdecls(code)
5887673Snate@binkert.org    code()
5896654Snate@binkert.org
5906654Snate@binkert.org    code('%include "params/$obj.hh"')
5916654Snate@binkert.org
5926654Snate@binkert.org    code.write(target[0].abspath)
5936654Snate@binkert.org
5946654Snate@binkert.orgfor name in sim_objects.iterkeys():
5956654Snate@binkert.org    params_file = File('python/m5/internal/param_%s.i' % name)
5966669Snate@binkert.org    env.Command(params_file, Value(name),
5976669Snate@binkert.org                MakeAction(buildParam, " [BLDPARAM] $STRIP_TARGET"))
5986669Snate@binkert.org    env.Depends(params_file, depends)
5996669Snate@binkert.org    SwigSource('m5.internal', params_file)
6006669Snate@binkert.org
6016669Snate@binkert.org# Generate the main swig init file
6026654Snate@binkert.orgdef makeEmbeddedSwigInit(target, source, env):
6037673Snate@binkert.org    code = code_formatter()
6045517Snate@binkert.org    module = source[0].get_contents()
6058126Sgblack@eecs.umich.edu    code('''\
6065798Snate@binkert.org#include "sim/init.hh"
6077756SAli.Saidi@ARM.com
6087816Ssteve.reinhardt@amd.comextern "C" {
6095798Snate@binkert.org    void init_${module}();
6105798Snate@binkert.org}
6115517Snate@binkert.org
6125517Snate@binkert.orgEmbeddedSwig embed_swig_${module}(init_${module});
6137673Snate@binkert.org''')
6145517Snate@binkert.org    code.write(str(target[0]))
6155517Snate@binkert.org    
6167673Snate@binkert.org# Build all swig modules
6177673Snate@binkert.orgfor swig in SwigSource.all:
6185517Snate@binkert.org    env.Command([swig.cc_source.tnode, swig.py_source.tnode], swig.tnode,
6195798Snate@binkert.org                MakeAction('$SWIG $SWIGFLAGS -outdir ${TARGETS[1].dir} '
6205798Snate@binkert.org                '-o ${TARGETS[0]} $SOURCES', " [    SWIG] $STRIP_TARGET"))
6218333Snate@binkert.org    init_file = 'python/swig/init_%s.cc' % swig.module
6227816Ssteve.reinhardt@amd.com    env.Command(init_file, Value(swig.module),
6235798Snate@binkert.org                MakeAction(makeEmbeddedSwigInit, " [EMBED SW] $STRIP_TARGET"))
6245798Snate@binkert.org    Source(init_file)
6254762Snate@binkert.org    env.Depends(swig.py_source.tnode, swig.tnode)
6264762Snate@binkert.org    env.Depends(swig.cc_source.tnode, swig.tnode)
6274762Snate@binkert.org
6284762Snate@binkert.orgdef getFlags(source_flags):
6294762Snate@binkert.org    flagsMap = {}
6308596Ssteve.reinhardt@amd.com    flagsList = []
6315517Snate@binkert.org    for s in source_flags:
6325517Snate@binkert.org        val = eval(s.get_contents())
6335517Snate@binkert.org        name, compound, desc = val
6345517Snate@binkert.org        flagsList.append(val)
6355517Snate@binkert.org        flagsMap[name] = bool(compound)
6367673Snate@binkert.org    
6378596Ssteve.reinhardt@amd.com    for name, compound, desc in flagsList:
6387673Snate@binkert.org        for flag in compound:
6395517Snate@binkert.org            if flag not in flagsMap:
64010458Sandreas.hansson@arm.com                raise AttributeError, "Trace flag %s not found" % flag
64110458Sandreas.hansson@arm.com            if flagsMap[flag]:
64210458Sandreas.hansson@arm.com                raise AttributeError, \
64310458Sandreas.hansson@arm.com                    "Compound flag can't point to another compound flag"
64410458Sandreas.hansson@arm.com
64510458Sandreas.hansson@arm.com    flagsList.sort()
64610458Sandreas.hansson@arm.com    return flagsList
64710458Sandreas.hansson@arm.com
64810458Sandreas.hansson@arm.com
64910458Sandreas.hansson@arm.com# Generate traceflags.py
65010458Sandreas.hansson@arm.comdef traceFlagsPy(target, source, env):
65110458Sandreas.hansson@arm.com    assert(len(target) == 1)
6528596Ssteve.reinhardt@amd.com    code = code_formatter()
6535517Snate@binkert.org
6545517Snate@binkert.org    allFlags = getFlags(source)
6555517Snate@binkert.org
6568596Ssteve.reinhardt@amd.com    code('basic = [')
6575517Snate@binkert.org    code.indent()
6587673Snate@binkert.org    for flag, compound, desc in allFlags:
6597673Snate@binkert.org        if not compound:
6607673Snate@binkert.org            code("'$flag',")
6615517Snate@binkert.org    code(']')
6625517Snate@binkert.org    code.dedent()
6635517Snate@binkert.org    code()
6645517Snate@binkert.org
6655517Snate@binkert.org    code('compound = [')
6665517Snate@binkert.org    code.indent()
6675517Snate@binkert.org    code("'All',")
6687673Snate@binkert.org    for flag, compound, desc in allFlags:
6697673Snate@binkert.org        if compound:
6707673Snate@binkert.org            code("'$flag',")
6715517Snate@binkert.org    code("]")
6728596Ssteve.reinhardt@amd.com    code.dedent()
6735517Snate@binkert.org    code()
6745517Snate@binkert.org
6755517Snate@binkert.org    code("all = frozenset(basic + compound)")
6765517Snate@binkert.org    code()
6775517Snate@binkert.org
6787673Snate@binkert.org    code('compoundMap = {')
6797673Snate@binkert.org    code.indent()
6807673Snate@binkert.org    all = tuple([flag for flag,compound,desc in allFlags if not compound])
6815517Snate@binkert.org    code("'All' : $all,")
6828596Ssteve.reinhardt@amd.com    for flag, compound, desc in allFlags:
6837675Snate@binkert.org        if compound:
6847675Snate@binkert.org            code("'$flag' : $compound,")
6857675Snate@binkert.org    code('}')
6867675Snate@binkert.org    code.dedent()
6877675Snate@binkert.org    code()
6887675Snate@binkert.org
6898596Ssteve.reinhardt@amd.com    code('descriptions = {')
6907675Snate@binkert.org    code.indent()
6917675Snate@binkert.org    code("'All' : 'All flags',")
6928596Ssteve.reinhardt@amd.com    for flag, compound, desc in allFlags:
6938596Ssteve.reinhardt@amd.com        code("'$flag' : '$desc',")
6948596Ssteve.reinhardt@amd.com    code("}")
6958596Ssteve.reinhardt@amd.com    code.dedent()
6968596Ssteve.reinhardt@amd.com
6978596Ssteve.reinhardt@amd.com    code.write(str(target[0]))
6988596Ssteve.reinhardt@amd.com
6998596Ssteve.reinhardt@amd.comdef traceFlagsCC(target, source, env):
70010454SCurtis.Dunham@arm.com    assert(len(target) == 1)
70110454SCurtis.Dunham@arm.com
70210454SCurtis.Dunham@arm.com    allFlags = getFlags(source)
70310454SCurtis.Dunham@arm.com    code = code_formatter()
7048596Ssteve.reinhardt@amd.com
7054762Snate@binkert.org    # file header
7066143Snate@binkert.org    code('''
7076143Snate@binkert.org/*
7086143Snate@binkert.org * DO NOT EDIT THIS FILE! Automatically generated
7094762Snate@binkert.org */
7104762Snate@binkert.org
7114762Snate@binkert.org#include "base/traceflags.hh"
7127756SAli.Saidi@ARM.com
7138596Ssteve.reinhardt@amd.comusing namespace Trace;
7144762Snate@binkert.org
71510454SCurtis.Dunham@arm.comconst char *Trace::flagStrings[] =
7164762Snate@binkert.org{''')
71710458Sandreas.hansson@arm.com
71810458Sandreas.hansson@arm.com    code.indent()
71910458Sandreas.hansson@arm.com    # The string array is used by SimpleEnumParam to map the strings
72010458Sandreas.hansson@arm.com    # provided by the user to enum values.
72110458Sandreas.hansson@arm.com    for flag, compound, desc in allFlags:
72210458Sandreas.hansson@arm.com        if not compound:
72310458Sandreas.hansson@arm.com            code('"$flag",')
72410458Sandreas.hansson@arm.com
72510458Sandreas.hansson@arm.com    code('"All",')
72610458Sandreas.hansson@arm.com    for flag, compound, desc in allFlags:
72710458Sandreas.hansson@arm.com        if compound:
72810458Sandreas.hansson@arm.com            code('"$flag",')
72910458Sandreas.hansson@arm.com    code.dedent()
73010458Sandreas.hansson@arm.com
73110458Sandreas.hansson@arm.com    code('''\
73210458Sandreas.hansson@arm.com};
73310458Sandreas.hansson@arm.com
73410458Sandreas.hansson@arm.comconst int Trace::numFlagStrings = ${{len(allFlags) + 1}};
73510458Sandreas.hansson@arm.com
73610458Sandreas.hansson@arm.com''')
73710458Sandreas.hansson@arm.com
73810458Sandreas.hansson@arm.com    # Now define the individual compound flag arrays.  There is an array
73910458Sandreas.hansson@arm.com    # for each compound flag listing the component base flags.
74010458Sandreas.hansson@arm.com    all = tuple([flag for flag,compound,desc in allFlags if not compound])
74110458Sandreas.hansson@arm.com    code('static const Flags AllMap[] = {')
74210458Sandreas.hansson@arm.com    code.indent()
74310458Sandreas.hansson@arm.com    for flag, compound, desc in allFlags:
74410458Sandreas.hansson@arm.com        if not compound:
74510458Sandreas.hansson@arm.com            code('$flag,')
74610458Sandreas.hansson@arm.com    code.dedent()
74710458Sandreas.hansson@arm.com    code('};')
74810458Sandreas.hansson@arm.com    code()
74910458Sandreas.hansson@arm.com
75010458Sandreas.hansson@arm.com    for flag, compound, desc in allFlags:
75110458Sandreas.hansson@arm.com        if not compound:
75210458Sandreas.hansson@arm.com            continue
75310458Sandreas.hansson@arm.com        code('static const Flags ${flag}Map[] = {')
75410458Sandreas.hansson@arm.com        code.indent()
75510458Sandreas.hansson@arm.com        for flag in compound:
75610458Sandreas.hansson@arm.com            code('$flag,')
75710458Sandreas.hansson@arm.com        code('(Flags)-1')
75810458Sandreas.hansson@arm.com        code.dedent()
75910458Sandreas.hansson@arm.com        code('};')
76010458Sandreas.hansson@arm.com        code()
76110458Sandreas.hansson@arm.com
76210458Sandreas.hansson@arm.com    # Finally the compoundFlags[] array maps the compound flags
76310458Sandreas.hansson@arm.com    # to their individual arrays/
76410458Sandreas.hansson@arm.com    code('const Flags *Trace::compoundFlags[] = {')
76510458Sandreas.hansson@arm.com    code.indent()
76610584Sandreas.hansson@arm.com    code('AllMap,')
76710458Sandreas.hansson@arm.com    for flag, compound, desc in allFlags:
76810458Sandreas.hansson@arm.com        if compound:
76910458Sandreas.hansson@arm.com            code('${flag}Map,')
77010458Sandreas.hansson@arm.com    # file trailer
77110458Sandreas.hansson@arm.com    code.dedent()
7728596Ssteve.reinhardt@amd.com    code('};')
7735463Snate@binkert.org
77410584Sandreas.hansson@arm.com    code.write(str(target[0]))
7758596Ssteve.reinhardt@amd.com
7765463Snate@binkert.orgdef traceFlagsHH(target, source, env):
7777756SAli.Saidi@ARM.com    assert(len(target) == 1)
7788596Ssteve.reinhardt@amd.com
7794762Snate@binkert.org    allFlags = getFlags(source)
78010454SCurtis.Dunham@arm.com    code = code_formatter()
7817677Snate@binkert.org
7824762Snate@binkert.org    # file header boilerplate
7834762Snate@binkert.org    code('''\
7846143Snate@binkert.org/*
7856143Snate@binkert.org * DO NOT EDIT THIS FILE!
7866143Snate@binkert.org *
7874762Snate@binkert.org * Automatically generated from traceflags.py
7884762Snate@binkert.org */
7897756SAli.Saidi@ARM.com
7907816Ssteve.reinhardt@amd.com#ifndef __BASE_TRACE_FLAGS_HH__
7914762Snate@binkert.org#define __BASE_TRACE_FLAGS_HH__
79210454SCurtis.Dunham@arm.com
7934762Snate@binkert.orgnamespace Trace {
7944762Snate@binkert.org
7954762Snate@binkert.orgenum Flags {''')
7967756SAli.Saidi@ARM.com
7978596Ssteve.reinhardt@amd.com    # Generate the enum.  Base flags come first, then compound flags.
7984762Snate@binkert.org    idx = 0
79910454SCurtis.Dunham@arm.com    code.indent()
8004762Snate@binkert.org    for flag, compound, desc in allFlags:
8017677Snate@binkert.org        if not compound:
8027756SAli.Saidi@ARM.com            code('$flag = $idx,')
8038596Ssteve.reinhardt@amd.com            idx += 1
8047675Snate@binkert.org
80510454SCurtis.Dunham@arm.com    numBaseFlags = idx
8067677Snate@binkert.org    code('NumFlags = $idx,')
8075517Snate@binkert.org    code.dedent()
8088596Ssteve.reinhardt@amd.com    code()
80910584Sandreas.hansson@arm.com
8109248SAndreas.Sandberg@arm.com    # put a comment in here to separate base from compound flags
8119248SAndreas.Sandberg@arm.com    code('''
8128596Ssteve.reinhardt@amd.com// The remaining enum values are *not* valid indices for Trace::flags.
8138596Ssteve.reinhardt@amd.com// They are "compound" flags, which correspond to sets of base
8148596Ssteve.reinhardt@amd.com// flags, and are used by changeFlag.''')
8159248SAndreas.Sandberg@arm.com
8168596Ssteve.reinhardt@amd.com    code.indent()
8174762Snate@binkert.org    code('All = $idx,')
8187674Snate@binkert.org    idx += 1
8197674Snate@binkert.org    for flag, compound, desc in allFlags:
8207674Snate@binkert.org        if compound:
8217674Snate@binkert.org            code('$flag = $idx,')
8227674Snate@binkert.org            idx += 1
8237674Snate@binkert.org
8247674Snate@binkert.org    numCompoundFlags = idx - numBaseFlags
8257674Snate@binkert.org    code('NumCompoundFlags = $numCompoundFlags')
8267674Snate@binkert.org    code.dedent()
8277674Snate@binkert.org
8287674Snate@binkert.org    # trailer boilerplate
8297674Snate@binkert.org    code('''\
8307674Snate@binkert.org}; // enum Flags
8317674Snate@binkert.org
83211308Santhony.gutierrez@amd.com// Array of strings for SimpleEnumParam
8334762Snate@binkert.orgextern const char *flagStrings[];
8346143Snate@binkert.orgextern const int numFlagStrings;
8356143Snate@binkert.org
8367756SAli.Saidi@ARM.com// Array of arraay pointers: for each compound flag, gives the list of
8377816Ssteve.reinhardt@amd.com// base flags to set.  Inidividual flag arrays are terminated by -1.
8388235Snate@binkert.orgextern const Flags *compoundFlags[];
8398596Ssteve.reinhardt@amd.com
8407756SAli.Saidi@ARM.com/* namespace Trace */ }
8417816Ssteve.reinhardt@amd.com
84210454SCurtis.Dunham@arm.com#endif // __BASE_TRACE_FLAGS_HH__
8438235Snate@binkert.org''')
8444382Sbinkertn@umich.edu
8459396Sandreas.hansson@arm.com    code.write(str(target[0]))
8469396Sandreas.hansson@arm.com
8479396Sandreas.hansson@arm.comflags = map(Value, trace_flags.values())
8489396Sandreas.hansson@arm.comenv.Command('base/traceflags.py', flags, 
8499396Sandreas.hansson@arm.com            MakeAction(traceFlagsPy, " [ TRACING] $STRIP_TARGET"))
8509396Sandreas.hansson@arm.comPySource('m5', 'base/traceflags.py')
8519396Sandreas.hansson@arm.com
8529396Sandreas.hansson@arm.comenv.Command('base/traceflags.hh', flags,
8539396Sandreas.hansson@arm.com            MakeAction(traceFlagsHH, " [ TRACING] $STRIP_TARGET"))
8549396Sandreas.hansson@arm.comenv.Command('base/traceflags.cc', flags, 
8559396Sandreas.hansson@arm.com            MakeAction(traceFlagsCC, " [ TRACING] $STRIP_TARGET"))
8569396Sandreas.hansson@arm.comSource('base/traceflags.cc')
85710454SCurtis.Dunham@arm.com
8589396Sandreas.hansson@arm.com# Embed python files.  All .py files that have been indicated by a
8599396Sandreas.hansson@arm.com# PySource() call in a SConscript need to be embedded into the M5
8609396Sandreas.hansson@arm.com# library.  To do that, we compile the file to byte code, marshal the
8619396Sandreas.hansson@arm.com# byte code, compress it, and then generate a c++ file that
8629396Sandreas.hansson@arm.com# inserts the result into an array.
8639396Sandreas.hansson@arm.comdef embedPyFile(target, source, env):
8648232Snate@binkert.org    def c_str(string):
8658232Snate@binkert.org        if string is None:
8668232Snate@binkert.org            return "0"
8678232Snate@binkert.org        return '"%s"' % string
8688232Snate@binkert.org
8696229Snate@binkert.org    '''Action function to compile a .py into a code object, marshal
87010455SCurtis.Dunham@arm.com    it, compress it, and stick it into an asm file so the code appears
8716229Snate@binkert.org    as just bytes with a label in the data section'''
87210455SCurtis.Dunham@arm.com
87310455SCurtis.Dunham@arm.com    src = file(str(source[0]), 'r').read()
87410455SCurtis.Dunham@arm.com
8755517Snate@binkert.org    pysource = PySource.tnodes[source[0]]
8765517Snate@binkert.org    compiled = compile(src, pysource.abspath, 'exec')
8777673Snate@binkert.org    marshalled = marshal.dumps(compiled)
8785517Snate@binkert.org    compressed = zlib.compress(marshalled)
87910455SCurtis.Dunham@arm.com    data = compressed
8805517Snate@binkert.org    sym = pysource.symname
8815517Snate@binkert.org
8828232Snate@binkert.org    code = code_formatter()
88310455SCurtis.Dunham@arm.com    code('''\
88410455SCurtis.Dunham@arm.com#include "sim/init.hh"
88510455SCurtis.Dunham@arm.com
8867673Snate@binkert.orgnamespace {
8877673Snate@binkert.org
88810455SCurtis.Dunham@arm.comconst char data_${sym}[] = {
88910455SCurtis.Dunham@arm.com''')
89010455SCurtis.Dunham@arm.com    code.indent()
8915517Snate@binkert.org    step = 16
89210455SCurtis.Dunham@arm.com    for i in xrange(0, len(data), step):
89310455SCurtis.Dunham@arm.com        x = array.array('B', data[i:i+step])
89410455SCurtis.Dunham@arm.com        code(''.join('%d,' % d for d in x))
89510455SCurtis.Dunham@arm.com    code.dedent()
89610455SCurtis.Dunham@arm.com    
89710455SCurtis.Dunham@arm.com    code('''};
89810455SCurtis.Dunham@arm.com
89910455SCurtis.Dunham@arm.comEmbeddedPython embedded_${sym}(
90010685Sandreas.hansson@arm.com    ${{c_str(pysource.arcname)}},
90110455SCurtis.Dunham@arm.com    ${{c_str(pysource.abspath)}},
90210685Sandreas.hansson@arm.com    ${{c_str(pysource.modpath)}},
90310455SCurtis.Dunham@arm.com    data_${sym},
9045517Snate@binkert.org    ${{len(data)}},
90510455SCurtis.Dunham@arm.com    ${{len(marshalled)}});
9068232Snate@binkert.org
9078232Snate@binkert.org/* namespace */ }
9085517Snate@binkert.org''')
9097673Snate@binkert.org    code.write(str(target[0]))
9105517Snate@binkert.org
9118232Snate@binkert.orgfor source in PySource.all:
9128232Snate@binkert.org    env.Command(source.cpp, source.tnode, 
9135517Snate@binkert.org                MakeAction(embedPyFile, " [EMBED PY] $STRIP_TARGET"))
9148232Snate@binkert.org    Source(source.cpp)
9158232Snate@binkert.org
9168232Snate@binkert.org########################################################################
9177673Snate@binkert.org#
9185517Snate@binkert.org# Define binaries.  Each different build type (debug, opt, etc.) gets
9195517Snate@binkert.org# a slightly different build environment.
9207673Snate@binkert.org#
9215517Snate@binkert.org
92210455SCurtis.Dunham@arm.com# List of constructed environments to pass back to SConstruct
9235517Snate@binkert.orgenvList = []
9245517Snate@binkert.org
9258232Snate@binkert.orgdate_source = Source('base/date.cc', skip_lib=True)
9268232Snate@binkert.org
9275517Snate@binkert.org# Function to create a new build environment as clone of current
9288232Snate@binkert.org# environment 'env' with modified object suffix and optional stripped
9298232Snate@binkert.org# binary.  Additional keyword arguments are appended to corresponding
9305517Snate@binkert.org# build environment vars.
9318232Snate@binkert.orgdef makeEnv(label, objsfx, strip = False, **kwargs):
9328232Snate@binkert.org    # SCons doesn't know to append a library suffix when there is a '.' in the
9338232Snate@binkert.org    # name.  Use '_' instead.
9345517Snate@binkert.org    libname = 'm5_' + label
9358232Snate@binkert.org    exename = 'm5.' + label
9368232Snate@binkert.org
9378232Snate@binkert.org    new_env = env.Clone(OBJSUFFIX=objsfx, SHOBJSUFFIX=objsfx + 's')
9388232Snate@binkert.org    new_env.Label = label
9398232Snate@binkert.org    new_env.Append(**kwargs)
9408232Snate@binkert.org
9415517Snate@binkert.org    swig_env = new_env.Clone()
9428232Snate@binkert.org    swig_env.Append(CCFLAGS='-Werror')
9438232Snate@binkert.org    if env['GCC']:
9445517Snate@binkert.org        swig_env.Append(CCFLAGS='-Wno-uninitialized')
9458232Snate@binkert.org        swig_env.Append(CCFLAGS='-Wno-sign-compare')
9467673Snate@binkert.org        swig_env.Append(CCFLAGS='-Wno-parentheses')
9475517Snate@binkert.org
9487673Snate@binkert.org    werror_env = new_env.Clone()
9495517Snate@binkert.org    werror_env.Append(CCFLAGS='-Werror')
9508232Snate@binkert.org
9518232Snate@binkert.org    def make_obj(source, static, extra_deps = None):
9528232Snate@binkert.org        '''This function adds the specified source to the correct
9535192Ssaidi@eecs.umich.edu        build environment, and returns the corresponding SCons Object
95410454SCurtis.Dunham@arm.com        nodes'''
95510454SCurtis.Dunham@arm.com
9568232Snate@binkert.org        if source.swig:
95710455SCurtis.Dunham@arm.com            env = swig_env
95810455SCurtis.Dunham@arm.com        elif source.Werror:
95910455SCurtis.Dunham@arm.com            env = werror_env
96010455SCurtis.Dunham@arm.com        else:
96110455SCurtis.Dunham@arm.com            env = new_env
96210455SCurtis.Dunham@arm.com
9635192Ssaidi@eecs.umich.edu        if static:
96411077SCurtis.Dunham@arm.com            obj = env.StaticObject(source.tnode)
96511077SCurtis.Dunham@arm.com        else:
96611077SCurtis.Dunham@arm.com            obj = env.SharedObject(source.tnode)
96711077SCurtis.Dunham@arm.com
96811077SCurtis.Dunham@arm.com        if extra_deps:
9697674Snate@binkert.org            env.Depends(obj, extra_deps)
9705522Snate@binkert.org
9715522Snate@binkert.org        return obj
9727674Snate@binkert.org
9737674Snate@binkert.org    static_objs = [ make_obj(s, True) for s in Source.get(skip_lib=False)]
9747674Snate@binkert.org    shared_objs = [ make_obj(s, False) for s in Source.get(skip_lib=False)]
9757674Snate@binkert.org
9767674Snate@binkert.org    static_date = make_obj(date_source, static=True, extra_deps=static_objs)
9777674Snate@binkert.org    static_objs.append(static_date)
9787674Snate@binkert.org    
9797674Snate@binkert.org    shared_date = make_obj(date_source, static=False, extra_deps=shared_objs)
9805522Snate@binkert.org    shared_objs.append(shared_date)
9815522Snate@binkert.org
9825522Snate@binkert.org    # First make a library of everything but main() so other programs can
9835517Snate@binkert.org    # link against m5.
9845522Snate@binkert.org    static_lib = new_env.StaticLibrary(libname, static_objs)
9855517Snate@binkert.org    shared_lib = new_env.SharedLibrary(libname, shared_objs)
9866143Snate@binkert.org
9876727Ssteve.reinhardt@amd.com    for target, sources in unit_tests:
9885522Snate@binkert.org        objs = [ make_obj(s, static=True) for s in sources ]
9895522Snate@binkert.org        new_env.Program("unittest/%s.%s" % (target, label), objs + static_objs)
9905522Snate@binkert.org
9917674Snate@binkert.org    # Now link a stub with main() and the static library.
9925517Snate@binkert.org    bin_objs = [make_obj(s, True) for s in Source.get(bin_only=True) ]
9937673Snate@binkert.org    progname = exename
9947673Snate@binkert.org    if strip:
9957674Snate@binkert.org        progname += '.unstripped'
9967673Snate@binkert.org
9977674Snate@binkert.org    targets = new_env.Program(progname, bin_objs + static_objs)
9987674Snate@binkert.org
9998946Sandreas.hansson@arm.com    if strip:
10007674Snate@binkert.org        if sys.platform == 'sunos5':
10017674Snate@binkert.org            cmd = 'cp $SOURCE $TARGET; strip $TARGET'
10027674Snate@binkert.org        else:
10035522Snate@binkert.org            cmd = 'strip $SOURCE -o $TARGET'
10045522Snate@binkert.org        targets = new_env.Command(exename, progname,
10057674Snate@binkert.org                    MakeAction(cmd, " [   STRIP] $STRIP_TARGET"))
10067674Snate@binkert.org            
100711308Santhony.gutierrez@amd.com    new_env.M5Binary = targets[0]
10087674Snate@binkert.org    envList.append(new_env)
10097673Snate@binkert.org
10107674Snate@binkert.org# Debug binary
10117674Snate@binkert.orgccflags = {}
10127674Snate@binkert.orgif env['GCC']:
10137674Snate@binkert.org    if sys.platform == 'sunos5':
10147674Snate@binkert.org        ccflags['debug'] = '-gstabs+'
10157674Snate@binkert.org    else:
10167674Snate@binkert.org        ccflags['debug'] = '-ggdb3'
10177674Snate@binkert.org    ccflags['opt'] = '-g -O3'
10187811Ssteve.reinhardt@amd.com    ccflags['fast'] = '-O3'
10197674Snate@binkert.org    ccflags['prof'] = '-O3 -g -pg'
10207673Snate@binkert.orgelif env['SUNCC']:
10215522Snate@binkert.org    ccflags['debug'] = '-g0'
10226143Snate@binkert.org    ccflags['opt'] = '-g -O'
102310453SAndrew.Bardsley@arm.com    ccflags['fast'] = '-fast'
10247816Ssteve.reinhardt@amd.com    ccflags['prof'] = '-fast -g -pg'
102510454SCurtis.Dunham@arm.comelif env['ICC']:
102610453SAndrew.Bardsley@arm.com    ccflags['debug'] = '-g -O0'
10274382Sbinkertn@umich.edu    ccflags['opt'] = '-g -O'
10284382Sbinkertn@umich.edu    ccflags['fast'] = '-fast'
10294382Sbinkertn@umich.edu    ccflags['prof'] = '-fast -g -pg'
10304382Sbinkertn@umich.eduelse:
10314382Sbinkertn@umich.edu    print 'Unknown compiler, please fix compiler options'
10324382Sbinkertn@umich.edu    Exit(1)
10334382Sbinkertn@umich.edu
10344382Sbinkertn@umich.edumakeEnv('debug', '.do',
103510196SCurtis.Dunham@arm.com        CCFLAGS = Split(ccflags['debug']),
10364382Sbinkertn@umich.edu        CPPDEFINES = ['DEBUG', 'TRACING_ON=1'])
103710196SCurtis.Dunham@arm.com
103810196SCurtis.Dunham@arm.com# Optimized binary
103910196SCurtis.Dunham@arm.commakeEnv('opt', '.o',
104010196SCurtis.Dunham@arm.com        CCFLAGS = Split(ccflags['opt']),
104110196SCurtis.Dunham@arm.com        CPPDEFINES = ['TRACING_ON=1'])
104210196SCurtis.Dunham@arm.com
104310196SCurtis.Dunham@arm.com# "Fast" binary
1044955SN/AmakeEnv('fast', '.fo', strip = True,
10452655Sstever@eecs.umich.edu        CCFLAGS = Split(ccflags['fast']),
10462655Sstever@eecs.umich.edu        CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'])
10472655Sstever@eecs.umich.edu
10482655Sstever@eecs.umich.edu# Profiled binary
104910196SCurtis.Dunham@arm.commakeEnv('prof', '.po',
10505601Snate@binkert.org        CCFLAGS = Split(ccflags['prof']),
10515601Snate@binkert.org        CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
105210196SCurtis.Dunham@arm.com        LINKFLAGS = '-pg')
105310196SCurtis.Dunham@arm.com
105410196SCurtis.Dunham@arm.comReturn('envList')
10555522Snate@binkert.org