SConscript revision 9157
14202Sbinkertn@umich.edu# -*- mode:python -*-
24202Sbinkertn@umich.edu
34202Sbinkertn@umich.edu# Copyright (c) 2004-2005 The Regents of The University of Michigan
44202Sbinkertn@umich.edu# All rights reserved.
54202Sbinkertn@umich.edu#
64202Sbinkertn@umich.edu# Redistribution and use in source and binary forms, with or without
74202Sbinkertn@umich.edu# modification, are permitted provided that the following conditions are
84202Sbinkertn@umich.edu# met: redistributions of source code must retain the above copyright
94202Sbinkertn@umich.edu# notice, this list of conditions and the following disclaimer;
104202Sbinkertn@umich.edu# redistributions in binary form must reproduce the above copyright
114202Sbinkertn@umich.edu# notice, this list of conditions and the following disclaimer in the
124202Sbinkertn@umich.edu# documentation and/or other materials provided with the distribution;
134202Sbinkertn@umich.edu# neither the name of the copyright holders nor the names of its
144202Sbinkertn@umich.edu# contributors may be used to endorse or promote products derived from
154202Sbinkertn@umich.edu# this software without specific prior written permission.
164202Sbinkertn@umich.edu#
174202Sbinkertn@umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
184202Sbinkertn@umich.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
194202Sbinkertn@umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
204202Sbinkertn@umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
214202Sbinkertn@umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
224202Sbinkertn@umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
234202Sbinkertn@umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
244202Sbinkertn@umich.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
254202Sbinkertn@umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
264202Sbinkertn@umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
274202Sbinkertn@umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
284202Sbinkertn@umich.edu#
294202Sbinkertn@umich.edu# Authors: Nathan Binkert
304202Sbinkertn@umich.edu
314202Sbinkertn@umich.eduimport array
324202Sbinkertn@umich.eduimport bisect
339398Sandreas.hansson@arm.comimport imp
349398Sandreas.hansson@arm.comimport marshal
359398Sandreas.hansson@arm.comimport os
369398Sandreas.hansson@arm.comimport re
379398Sandreas.hansson@arm.comimport sys
389398Sandreas.hansson@arm.comimport zlib
399850Sandreas.hansson@arm.com
409259SAli.Saidi@ARM.comfrom os.path import basename, dirname, exists, isdir, isfile, join as joinpath
414486Sbinkertn@umich.edu
424486Sbinkertn@umich.eduimport SCons
4310146Sandreas.hansson@arm.com
446165Ssanchezd@stanford.edu# This file defines how to build a particular configuration of gem5
459850Sandreas.hansson@arm.com# based on variable settings in the 'env' build environment.
466168Snate@binkert.org
479850Sandreas.hansson@arm.comImport('*')
489259SAli.Saidi@ARM.com
494202Sbinkertn@umich.edu# Children need to see the environment
504202Sbinkertn@umich.eduExport('env')
519036Sandreas.hansson@arm.com
5210146Sandreas.hansson@arm.combuild_env = [(opt, env[opt]) for opt in export_vars]
534202Sbinkertn@umich.edu
548761Sgblack@eecs.umich.edufrom m5.util import code_formatter, compareVersions
559036Sandreas.hansson@arm.com
564202Sbinkertn@umich.edu########################################################################
574202Sbinkertn@umich.edu# Code for adding source files of various types
588914Sandreas.hansson@arm.com#
594202Sbinkertn@umich.edu# When specifying a source file of some type, a set of guards can be
608853Sandreas.hansson@arm.com# specified for that file.  When get() is used to find the files, if
619850Sandreas.hansson@arm.com# get specifies a set of filters, only files that match those filters
629850Sandreas.hansson@arm.com# will be accepted (unspecified filters on files are assumed to be
636168Snate@binkert.org# false).  Current filters are:
649850Sandreas.hansson@arm.com#     main -- specifies the gem5 main() function
659850Sandreas.hansson@arm.com#     skip_lib -- do not put this file into the gem5 library
669850Sandreas.hansson@arm.com#     <unittest> -- unit tests use filters based on the unit test name
678763Sgblack@eecs.umich.edu#
687768SAli.Saidi@ARM.com# A parent can now be specified for a source file and default filter
6910131Sandreas.hansson@arm.com# values will be retrieved recursively from parents (children override
7010131Sandreas.hansson@arm.com# parents).
7110131Sandreas.hansson@arm.com#
7210131Sandreas.hansson@arm.comclass SourceMeta(type):
7310066Sandreas.hansson@arm.com    '''Meta class for source files that keeps track of all files of a
749036Sandreas.hansson@arm.com    particular type and has a get function for finding all functions
758335Snate@binkert.org    of a certain type that match a set of guards'''
769036Sandreas.hansson@arm.com    def __init__(cls, name, bases, dict):
779036Sandreas.hansson@arm.com        super(SourceMeta, cls).__init__(name, bases, dict)
789036Sandreas.hansson@arm.com        cls.all = []
799036Sandreas.hansson@arm.com        
809036Sandreas.hansson@arm.com    def get(cls, **guards):
819164Sandreas.hansson@arm.com        '''Find all files that match the specified guards.  If a source
828981Sandreas.hansson@arm.com        file does not specify a flag, the default is False'''
839243Sandreas.hansson@arm.com        for src in cls.all:
8410247Sandreas.hansson@arm.com            for flag,value in guards.iteritems():
8510208Sandreas.hansson@arm.com                # if the flag is found and has a different value, skip
868335Snate@binkert.org                # this file
878335Snate@binkert.org                if src.all_guards.get(flag, False) != value:
888335Snate@binkert.org                    break
898914Sandreas.hansson@arm.com            else:
907780Snilay@cs.wisc.edu                yield src
9110066Sandreas.hansson@arm.com
92class SourceFile(object):
93    '''Base object that encapsulates the notion of a source file.
94    This includes, the source node, target node, various manipulations
95    of those.  A source file also specifies a set of guards which
96    describing which builds the source file applies to.  A parent can
97    also be specified to get default guards from'''
98    __metaclass__ = SourceMeta
99    def __init__(self, source, parent=None, **guards):
100        self.guards = guards
101        self.parent = parent
102
103        tnode = source
104        if not isinstance(source, SCons.Node.FS.File):
105            tnode = File(source)
106
107        self.tnode = tnode
108        self.snode = tnode.srcnode()
109
110        for base in type(self).__mro__:
111            if issubclass(base, SourceFile):
112                base.all.append(self)
113
114    @property
115    def filename(self):
116        return str(self.tnode)
117
118    @property
119    def dirname(self):
120        return dirname(self.filename)
121
122    @property
123    def basename(self):
124        return basename(self.filename)
125
126    @property
127    def extname(self):
128        index = self.basename.rfind('.')
129        if index <= 0:
130            # dot files aren't extensions
131            return self.basename, None
132
133        return self.basename[:index], self.basename[index+1:]
134
135    @property
136    def all_guards(self):
137        '''find all guards for this object getting default values
138        recursively from its parents'''
139        guards = {}
140        if self.parent:
141            guards.update(self.parent.guards)
142        guards.update(self.guards)
143        return guards
144
145    def __lt__(self, other): return self.filename < other.filename
146    def __le__(self, other): return self.filename <= other.filename
147    def __gt__(self, other): return self.filename > other.filename
148    def __ge__(self, other): return self.filename >= other.filename
149    def __eq__(self, other): return self.filename == other.filename
150    def __ne__(self, other): return self.filename != other.filename
151        
152class Source(SourceFile):
153    '''Add a c/c++ source file to the build'''
154    def __init__(self, source, Werror=True, swig=False, **guards):
155        '''specify the source file, and any guards'''
156        super(Source, self).__init__(source, **guards)
157
158        self.Werror = Werror
159        self.swig = swig
160
161class PySource(SourceFile):
162    '''Add a python source file to the named package'''
163    invalid_sym_char = re.compile('[^A-z0-9_]')
164    modules = {}
165    tnodes = {}
166    symnames = {}
167    
168    def __init__(self, package, source, **guards):
169        '''specify the python package, the source file, and any guards'''
170        super(PySource, self).__init__(source, **guards)
171
172        modname,ext = self.extname
173        assert ext == 'py'
174
175        if package:
176            path = package.split('.')
177        else:
178            path = []
179
180        modpath = path[:]
181        if modname != '__init__':
182            modpath += [ modname ]
183        modpath = '.'.join(modpath)
184
185        arcpath = path + [ self.basename ]
186        abspath = self.snode.abspath
187        if not exists(abspath):
188            abspath = self.tnode.abspath
189
190        self.package = package
191        self.modname = modname
192        self.modpath = modpath
193        self.arcname = joinpath(*arcpath)
194        self.abspath = abspath
195        self.compiled = File(self.filename + 'c')
196        self.cpp = File(self.filename + '.cc')
197        self.symname = PySource.invalid_sym_char.sub('_', modpath)
198
199        PySource.modules[modpath] = self
200        PySource.tnodes[self.tnode] = self
201        PySource.symnames[self.symname] = self
202
203class SimObject(PySource):
204    '''Add a SimObject python file as a python source object and add
205    it to a list of sim object modules'''
206
207    fixed = False
208    modnames = []
209
210    def __init__(self, source, **guards):
211        '''Specify the source file and any guards (automatically in
212        the m5.objects package)'''
213        super(SimObject, self).__init__('m5.objects', source, **guards)
214        if self.fixed:
215            raise AttributeError, "Too late to call SimObject now."
216
217        bisect.insort_right(SimObject.modnames, self.modname)
218
219class SwigSource(SourceFile):
220    '''Add a swig file to build'''
221
222    def __init__(self, package, source, **guards):
223        '''Specify the python package, the source file, and any guards'''
224        super(SwigSource, self).__init__(source, **guards)
225
226        modname,ext = self.extname
227        assert ext == 'i'
228
229        self.module = modname
230        cc_file = joinpath(self.dirname, modname + '_wrap.cc')
231        py_file = joinpath(self.dirname, modname + '.py')
232
233        self.cc_source = Source(cc_file, swig=True, parent=self)
234        self.py_source = PySource(package, py_file, parent=self)
235
236class UnitTest(object):
237    '''Create a UnitTest'''
238
239    all = []
240    def __init__(self, target, *sources, **kwargs):
241        '''Specify the target name and any sources.  Sources that are
242        not SourceFiles are evalued with Source().  All files are
243        guarded with a guard of the same name as the UnitTest
244        target.'''
245
246        srcs = []
247        for src in sources:
248            if not isinstance(src, SourceFile):
249                src = Source(src, skip_lib=True)
250            src.guards[target] = True
251            srcs.append(src)
252
253        self.sources = srcs
254        self.target = target
255        self.main = kwargs.get('main', False)
256        UnitTest.all.append(self)
257
258# Children should have access
259Export('Source')
260Export('PySource')
261Export('SimObject')
262Export('SwigSource')
263Export('UnitTest')
264
265########################################################################
266#
267# Debug Flags
268#
269debug_flags = {}
270def DebugFlag(name, desc=None):
271    if name in debug_flags:
272        raise AttributeError, "Flag %s already specified" % name
273    debug_flags[name] = (name, (), desc)
274
275def CompoundFlag(name, flags, desc=None):
276    if name in debug_flags:
277        raise AttributeError, "Flag %s already specified" % name
278
279    compound = tuple(flags)
280    debug_flags[name] = (name, compound, desc)
281
282Export('DebugFlag')
283Export('CompoundFlag')
284
285########################################################################
286#
287# Set some compiler variables
288#
289
290# Include file paths are rooted in this directory.  SCons will
291# automatically expand '.' to refer to both the source directory and
292# the corresponding build directory to pick up generated include
293# files.
294env.Append(CPPPATH=Dir('.'))
295
296for extra_dir in extras_dir_list:
297    env.Append(CPPPATH=Dir(extra_dir))
298
299# Workaround for bug in SCons version > 0.97d20071212
300# Scons bug id: 2006 gem5 Bug id: 308
301for root, dirs, files in os.walk(base_dir, topdown=True):
302    Dir(root[len(base_dir) + 1:])
303
304########################################################################
305#
306# Walk the tree and execute all SConscripts in subdirectories
307#
308
309here = Dir('.').srcnode().abspath
310for root, dirs, files in os.walk(base_dir, topdown=True):
311    if root == here:
312        # we don't want to recurse back into this SConscript
313        continue
314
315    if 'SConscript' in files:
316        build_dir = joinpath(env['BUILDDIR'], root[len(base_dir) + 1:])
317        SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
318
319for extra_dir in extras_dir_list:
320    prefix_len = len(dirname(extra_dir)) + 1
321    for root, dirs, files in os.walk(extra_dir, topdown=True):
322        # if build lives in the extras directory, don't walk down it
323        if 'build' in dirs:
324            dirs.remove('build')
325
326        if 'SConscript' in files:
327            build_dir = joinpath(env['BUILDDIR'], root[prefix_len:])
328            SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
329
330for opt in export_vars:
331    env.ConfigFile(opt)
332
333def makeTheISA(source, target, env):
334    isas = [ src.get_contents() for src in source ]
335    target_isa = env['TARGET_ISA']
336    def define(isa):
337        return isa.upper() + '_ISA'
338    
339    def namespace(isa):
340        return isa[0].upper() + isa[1:].lower() + 'ISA' 
341
342
343    code = code_formatter()
344    code('''\
345#ifndef __CONFIG_THE_ISA_HH__
346#define __CONFIG_THE_ISA_HH__
347
348''')
349
350    for i,isa in enumerate(isas):
351        code('#define $0 $1', define(isa), i + 1)
352
353    code('''
354
355#define THE_ISA ${{define(target_isa)}}
356#define TheISA ${{namespace(target_isa)}}
357#define THE_ISA_STR "${{target_isa}}"
358
359#endif // __CONFIG_THE_ISA_HH__''')
360
361    code.write(str(target[0]))
362
363env.Command('config/the_isa.hh', map(Value, all_isa_list),
364            MakeAction(makeTheISA, Transform("CFG ISA", 0)))
365
366########################################################################
367#
368# Prevent any SimObjects from being added after this point, they
369# should all have been added in the SConscripts above
370#
371SimObject.fixed = True
372
373class DictImporter(object):
374    '''This importer takes a dictionary of arbitrary module names that
375    map to arbitrary filenames.'''
376    def __init__(self, modules):
377        self.modules = modules
378        self.installed = set()
379
380    def __del__(self):
381        self.unload()
382
383    def unload(self):
384        import sys
385        for module in self.installed:
386            del sys.modules[module]
387        self.installed = set()
388
389    def find_module(self, fullname, path):
390        if fullname == 'm5.defines':
391            return self
392
393        if fullname == 'm5.objects':
394            return self
395
396        if fullname.startswith('m5.internal'):
397            return None
398
399        source = self.modules.get(fullname, None)
400        if source is not None and fullname.startswith('m5.objects'):
401            return self
402
403        return None
404
405    def load_module(self, fullname):
406        mod = imp.new_module(fullname)
407        sys.modules[fullname] = mod
408        self.installed.add(fullname)
409
410        mod.__loader__ = self
411        if fullname == 'm5.objects':
412            mod.__path__ = fullname.split('.')
413            return mod
414
415        if fullname == 'm5.defines':
416            mod.__dict__['buildEnv'] = m5.util.SmartDict(build_env)
417            return mod
418
419        source = self.modules[fullname]
420        if source.modname == '__init__':
421            mod.__path__ = source.modpath
422        mod.__file__ = source.abspath
423
424        exec file(source.abspath, 'r') in mod.__dict__
425
426        return mod
427
428import m5.SimObject
429import m5.params
430from m5.util import code_formatter
431
432m5.SimObject.clear()
433m5.params.clear()
434
435# install the python importer so we can grab stuff from the source
436# tree itself.  We can't have SimObjects added after this point or
437# else we won't know about them for the rest of the stuff.
438importer = DictImporter(PySource.modules)
439sys.meta_path[0:0] = [ importer ]
440
441# import all sim objects so we can populate the all_objects list
442# make sure that we're working with a list, then let's sort it
443for modname in SimObject.modnames:
444    exec('from m5.objects import %s' % modname)
445
446# we need to unload all of the currently imported modules so that they
447# will be re-imported the next time the sconscript is run
448importer.unload()
449sys.meta_path.remove(importer)
450
451sim_objects = m5.SimObject.allClasses
452all_enums = m5.params.allEnums
453
454# Find param types that need to be explicitly wrapped with swig.
455# These will be recognized because the ParamDesc will have a
456# swig_decl() method.  Most param types are based on types that don't
457# need this, either because they're based on native types (like Int)
458# or because they're SimObjects (which get swigged independently).
459# For now the only things handled here are VectorParam types.
460params_to_swig = {}
461for name,obj in sorted(sim_objects.iteritems()):
462    for param in obj._params.local.values():
463        # load the ptype attribute now because it depends on the
464        # current version of SimObject.allClasses, but when scons
465        # actually uses the value, all versions of
466        # SimObject.allClasses will have been loaded
467        param.ptype
468
469        if not hasattr(param, 'swig_decl'):
470            continue
471        pname = param.ptype_str
472        if pname not in params_to_swig:
473            params_to_swig[pname] = param
474
475########################################################################
476#
477# calculate extra dependencies
478#
479module_depends = ["m5", "m5.SimObject", "m5.params"]
480depends = [ PySource.modules[dep].snode for dep in module_depends ]
481
482########################################################################
483#
484# Commands for the basic automatically generated python files
485#
486
487# Generate Python file containing a dict specifying the current
488# buildEnv flags.
489def makeDefinesPyFile(target, source, env):
490    build_env = source[0].get_contents()
491
492    code = code_formatter()
493    code("""
494import m5.internal
495import m5.util
496
497buildEnv = m5.util.SmartDict($build_env)
498
499compileDate = m5.internal.core.compileDate
500_globals = globals()
501for key,val in m5.internal.core.__dict__.iteritems():
502    if key.startswith('flag_'):
503        flag = key[5:]
504        _globals[flag] = val
505del _globals
506""")
507    code.write(target[0].abspath)
508
509defines_info = Value(build_env)
510# Generate a file with all of the compile options in it
511env.Command('python/m5/defines.py', defines_info,
512            MakeAction(makeDefinesPyFile, Transform("DEFINES", 0)))
513PySource('m5', 'python/m5/defines.py')
514
515# Generate python file containing info about the M5 source code
516def makeInfoPyFile(target, source, env):
517    code = code_formatter()
518    for src in source:
519        data = ''.join(file(src.srcnode().abspath, 'r').xreadlines())
520        code('$src = ${{repr(data)}}')
521    code.write(str(target[0]))
522
523# Generate a file that wraps the basic top level files
524env.Command('python/m5/info.py',
525            [ '#/COPYING', '#/LICENSE', '#/README', ],
526            MakeAction(makeInfoPyFile, Transform("INFO")))
527PySource('m5', 'python/m5/info.py')
528
529########################################################################
530#
531# Create all of the SimObject param headers and enum headers
532#
533
534def createSimObjectParamStruct(target, source, env):
535    assert len(target) == 1 and len(source) == 1
536
537    name = str(source[0].get_contents())
538    obj = sim_objects[name]
539
540    code = code_formatter()
541    obj.cxx_param_decl(code)
542    code.write(target[0].abspath)
543
544def createParamSwigWrapper(target, source, env):
545    assert len(target) == 1 and len(source) == 1
546
547    name = str(source[0].get_contents())
548    param = params_to_swig[name]
549
550    code = code_formatter()
551    param.swig_decl(code)
552    code.write(target[0].abspath)
553
554def createEnumStrings(target, source, env):
555    assert len(target) == 1 and len(source) == 1
556
557    name = str(source[0].get_contents())
558    obj = all_enums[name]
559
560    code = code_formatter()
561    obj.cxx_def(code)
562    code.write(target[0].abspath)
563
564def createEnumDecls(target, source, env):
565    assert len(target) == 1 and len(source) == 1
566
567    name = str(source[0].get_contents())
568    obj = all_enums[name]
569
570    code = code_formatter()
571    obj.cxx_decl(code)
572    code.write(target[0].abspath)
573
574def createEnumSwigWrapper(target, source, env):
575    assert len(target) == 1 and len(source) == 1
576
577    name = str(source[0].get_contents())
578    obj = all_enums[name]
579
580    code = code_formatter()
581    obj.swig_decl(code)
582    code.write(target[0].abspath)
583
584def createSimObjectSwigWrapper(target, source, env):
585    name = source[0].get_contents()
586    obj = sim_objects[name]
587
588    code = code_formatter()
589    obj.swig_decl(code)
590    code.write(target[0].abspath)
591
592# Generate all of the SimObject param C++ struct header files
593params_hh_files = []
594for name,simobj in sorted(sim_objects.iteritems()):
595    py_source = PySource.modules[simobj.__module__]
596    extra_deps = [ py_source.tnode ]
597
598    hh_file = File('params/%s.hh' % name)
599    params_hh_files.append(hh_file)
600    env.Command(hh_file, Value(name),
601                MakeAction(createSimObjectParamStruct, Transform("SO PARAM")))
602    env.Depends(hh_file, depends + extra_deps)
603
604# Generate any needed param SWIG wrapper files
605params_i_files = []
606for name,param in params_to_swig.iteritems():
607    i_file = File('python/m5/internal/%s.i' % (param.swig_module_name()))
608    params_i_files.append(i_file)
609    env.Command(i_file, Value(name),
610                MakeAction(createParamSwigWrapper, Transform("SW PARAM")))
611    env.Depends(i_file, depends)
612    SwigSource('m5.internal', i_file)
613
614# Generate all enum header files
615for name,enum in sorted(all_enums.iteritems()):
616    py_source = PySource.modules[enum.__module__]
617    extra_deps = [ py_source.tnode ]
618
619    cc_file = File('enums/%s.cc' % name)
620    env.Command(cc_file, Value(name),
621                MakeAction(createEnumStrings, Transform("ENUM STR")))
622    env.Depends(cc_file, depends + extra_deps)
623    Source(cc_file)
624
625    hh_file = File('enums/%s.hh' % name)
626    env.Command(hh_file, Value(name),
627                MakeAction(createEnumDecls, Transform("ENUMDECL")))
628    env.Depends(hh_file, depends + extra_deps)
629
630    i_file = File('python/m5/internal/enum_%s.i' % name)
631    env.Command(i_file, Value(name),
632                MakeAction(createEnumSwigWrapper, Transform("ENUMSWIG")))
633    env.Depends(i_file, depends + extra_deps)
634    SwigSource('m5.internal', i_file)
635
636# Generate SimObject SWIG wrapper files
637for name in sim_objects.iterkeys():
638    i_file = File('python/m5/internal/param_%s.i' % name)
639    env.Command(i_file, Value(name),
640                MakeAction(createSimObjectSwigWrapper, Transform("SO SWIG")))
641    env.Depends(i_file, depends)
642    SwigSource('m5.internal', i_file)
643
644# Generate the main swig init file
645def makeEmbeddedSwigInit(target, source, env):
646    code = code_formatter()
647    module = source[0].get_contents()
648    code('''\
649#include "sim/init.hh"
650
651extern "C" {
652    void init_${module}();
653}
654
655EmbeddedSwig embed_swig_${module}(init_${module});
656''')
657    code.write(str(target[0]))
658    
659# Build all swig modules
660for swig in SwigSource.all:
661    env.Command([swig.cc_source.tnode, swig.py_source.tnode], swig.tnode,
662                MakeAction('$SWIG $SWIGFLAGS -outdir ${TARGETS[1].dir} '
663                '-o ${TARGETS[0]} $SOURCES', Transform("SWIG")))
664    cc_file = str(swig.tnode)
665    init_file = '%s/%s_init.cc' % (dirname(cc_file), basename(cc_file))
666    env.Command(init_file, Value(swig.module),
667                MakeAction(makeEmbeddedSwigInit, Transform("EMBED SW")))
668    Source(init_file, **swig.guards)
669
670#
671# Handle debug flags
672#
673def makeDebugFlagCC(target, source, env):
674    assert(len(target) == 1 and len(source) == 1)
675
676    val = eval(source[0].get_contents())
677    name, compound, desc = val
678    compound = list(sorted(compound))
679
680    code = code_formatter()
681
682    # file header
683    code('''
684/*
685 * DO NOT EDIT THIS FILE! Automatically generated
686 */
687
688#include "base/debug.hh"
689''')
690
691    for flag in compound:
692        code('#include "debug/$flag.hh"')
693    code()
694    code('namespace Debug {')
695    code()
696
697    if not compound:
698        code('SimpleFlag $name("$name", "$desc");')
699    else:
700        code('CompoundFlag $name("$name", "$desc",')
701        code.indent()
702        last = len(compound) - 1
703        for i,flag in enumerate(compound):
704            if i != last:
705                code('$flag,')
706            else:
707                code('$flag);')
708        code.dedent()
709
710    code()
711    code('} // namespace Debug')
712
713    code.write(str(target[0]))
714
715def makeDebugFlagHH(target, source, env):
716    assert(len(target) == 1 and len(source) == 1)
717
718    val = eval(source[0].get_contents())
719    name, compound, desc = val
720
721    code = code_formatter()
722
723    # file header boilerplate
724    code('''\
725/*
726 * DO NOT EDIT THIS FILE!
727 *
728 * Automatically generated by SCons
729 */
730
731#ifndef __DEBUG_${name}_HH__
732#define __DEBUG_${name}_HH__
733
734namespace Debug {
735''')
736
737    if compound:
738        code('class CompoundFlag;')
739    code('class SimpleFlag;')
740
741    if compound:
742        code('extern CompoundFlag $name;')
743        for flag in compound:
744            code('extern SimpleFlag $flag;')
745    else:
746        code('extern SimpleFlag $name;')
747
748    code('''
749}
750
751#endif // __DEBUG_${name}_HH__
752''')
753
754    code.write(str(target[0]))
755
756for name,flag in sorted(debug_flags.iteritems()):
757    n, compound, desc = flag
758    assert n == name
759
760    env.Command('debug/%s.hh' % name, Value(flag),
761                MakeAction(makeDebugFlagHH, Transform("TRACING", 0)))
762    env.Command('debug/%s.cc' % name, Value(flag),
763                MakeAction(makeDebugFlagCC, Transform("TRACING", 0)))
764    Source('debug/%s.cc' % name)
765
766# Embed python files.  All .py files that have been indicated by a
767# PySource() call in a SConscript need to be embedded into the M5
768# library.  To do that, we compile the file to byte code, marshal the
769# byte code, compress it, and then generate a c++ file that
770# inserts the result into an array.
771def embedPyFile(target, source, env):
772    def c_str(string):
773        if string is None:
774            return "0"
775        return '"%s"' % string
776
777    '''Action function to compile a .py into a code object, marshal
778    it, compress it, and stick it into an asm file so the code appears
779    as just bytes with a label in the data section'''
780
781    src = file(str(source[0]), 'r').read()
782
783    pysource = PySource.tnodes[source[0]]
784    compiled = compile(src, pysource.abspath, 'exec')
785    marshalled = marshal.dumps(compiled)
786    compressed = zlib.compress(marshalled)
787    data = compressed
788    sym = pysource.symname
789
790    code = code_formatter()
791    code('''\
792#include "sim/init.hh"
793
794namespace {
795
796const uint8_t data_${sym}[] = {
797''')
798    code.indent()
799    step = 16
800    for i in xrange(0, len(data), step):
801        x = array.array('B', data[i:i+step])
802        code(''.join('%d,' % d for d in x))
803    code.dedent()
804    
805    code('''};
806
807EmbeddedPython embedded_${sym}(
808    ${{c_str(pysource.arcname)}},
809    ${{c_str(pysource.abspath)}},
810    ${{c_str(pysource.modpath)}},
811    data_${sym},
812    ${{len(data)}},
813    ${{len(marshalled)}});
814
815} // anonymous namespace
816''')
817    code.write(str(target[0]))
818
819for source in PySource.all:
820    env.Command(source.cpp, source.tnode, 
821                MakeAction(embedPyFile, Transform("EMBED PY")))
822    Source(source.cpp)
823
824########################################################################
825#
826# Define binaries.  Each different build type (debug, opt, etc.) gets
827# a slightly different build environment.
828#
829
830# List of constructed environments to pass back to SConstruct
831envList = []
832
833date_source = Source('base/date.cc', skip_lib=True)
834
835# Function to create a new build environment as clone of current
836# environment 'env' with modified object suffix and optional stripped
837# binary.  Additional keyword arguments are appended to corresponding
838# build environment vars.
839def makeEnv(label, objsfx, strip = False, **kwargs):
840    # SCons doesn't know to append a library suffix when there is a '.' in the
841    # name.  Use '_' instead.
842    libname = 'gem5_' + label
843    exename = 'gem5.' + label
844    secondary_exename = 'm5.' + label
845
846    new_env = env.Clone(OBJSUFFIX=objsfx, SHOBJSUFFIX=objsfx + 's')
847    new_env.Label = label
848    new_env.Append(**kwargs)
849
850    swig_env = new_env.Clone()
851    swig_env.Append(CCFLAGS='-Werror')
852    if env['GCC']:
853        swig_env.Append(CCFLAGS='-Wno-uninitialized')
854        swig_env.Append(CCFLAGS='-Wno-sign-compare')
855        swig_env.Append(CCFLAGS='-Wno-parentheses')
856        swig_env.Append(CCFLAGS='-Wno-unused-label')
857        if compareVersions(env['GCC_VERSION'], '4.6') >= 0:
858            swig_env.Append(CCFLAGS='-Wno-unused-but-set-variable')
859    if env['CLANG']:
860        swig_env.Append(CCFLAGS=['-Wno-unused-label'])
861
862
863    werror_env = new_env.Clone()
864    werror_env.Append(CCFLAGS='-Werror')
865
866    def make_obj(source, static, extra_deps = None):
867        '''This function adds the specified source to the correct
868        build environment, and returns the corresponding SCons Object
869        nodes'''
870
871        if source.swig:
872            env = swig_env
873        elif source.Werror:
874            env = werror_env
875        else:
876            env = new_env
877
878        if static:
879            obj = env.StaticObject(source.tnode)
880        else:
881            obj = env.SharedObject(source.tnode)
882
883        if extra_deps:
884            env.Depends(obj, extra_deps)
885
886        return obj
887
888    static_objs = \
889        [ make_obj(s, True) for s in Source.get(main=False, skip_lib=False) ]
890    shared_objs = \
891        [ make_obj(s, False) for s in Source.get(main=False, skip_lib=False) ]
892
893    static_date = make_obj(date_source, static=True, extra_deps=static_objs)
894    static_objs.append(static_date)
895    
896    shared_date = make_obj(date_source, static=False, extra_deps=shared_objs)
897    shared_objs.append(shared_date)
898
899    # First make a library of everything but main() so other programs can
900    # link against m5.
901    static_lib = new_env.StaticLibrary(libname, static_objs)
902    shared_lib = new_env.SharedLibrary(libname, shared_objs)
903
904    # Now link a stub with main() and the static library.
905    main_objs = [ make_obj(s, True) for s in Source.get(main=True) ]
906
907    for test in UnitTest.all:
908        flags = { test.target : True }
909        test_sources = Source.get(**flags)
910        test_objs = [ make_obj(s, static=True) for s in test_sources ]
911        if test.main:
912            test_objs += main_objs
913        testname = "unittest/%s.%s" % (test.target, label)
914        new_env.Program(testname, test_objs + static_objs)
915
916    progname = exename
917    if strip:
918        progname += '.unstripped'
919
920    targets = new_env.Program(progname, main_objs + static_objs)
921
922    if strip:
923        if sys.platform == 'sunos5':
924            cmd = 'cp $SOURCE $TARGET; strip $TARGET'
925        else:
926            cmd = 'strip $SOURCE -o $TARGET'
927        targets = new_env.Command(exename, progname,
928                    MakeAction(cmd, Transform("STRIP")))
929
930    new_env.Command(secondary_exename, exename,
931            MakeAction('ln $SOURCE $TARGET', Transform("HARDLINK")))
932
933    new_env.M5Binary = targets[0]
934    envList.append(new_env)
935
936# Debug binary
937ccflags = {}
938if env['GCC']:
939    if sys.platform == 'sunos5':
940        ccflags['debug'] = '-gstabs+'
941    else:
942        ccflags['debug'] = '-ggdb3'
943    ccflags['opt'] = '-g -O3'
944    ccflags['fast'] = '-O3'
945    ccflags['prof'] = '-O3 -g -pg'
946elif env['SUNCC']:
947    ccflags['debug'] = '-g0'
948    ccflags['opt'] = '-g -O'
949    ccflags['fast'] = '-fast'
950    ccflags['prof'] = '-fast -g -pg'
951elif env['ICC']:
952    ccflags['debug'] = '-g -O0'
953    ccflags['opt'] = '-g -O'
954    ccflags['fast'] = '-fast'
955    ccflags['prof'] = '-fast -g -pg'
956elif env['CLANG']:
957    ccflags['debug'] = '-g -O0'
958    ccflags['opt'] = '-g -O3'
959    ccflags['fast'] = '-O3'
960    ccflags['prof'] = '-O3 -g -pg'
961else:
962    print 'Unknown compiler, please fix compiler options'
963    Exit(1)
964
965
966# To speed things up, we only instantiate the build environments we
967# need.  We try to identify the needed environment for each target; if
968# we can't, we fall back on instantiating all the environments just to
969# be safe.
970target_types = ['debug', 'opt', 'fast', 'prof']
971obj2target = {'do': 'debug', 'o': 'opt', 'fo': 'fast', 'po': 'prof'}
972
973def identifyTarget(t):
974    ext = t.split('.')[-1]
975    if ext in target_types:
976        return ext
977    if obj2target.has_key(ext):
978        return obj2target[ext]
979    match = re.search(r'/tests/([^/]+)/', t)
980    if match and match.group(1) in target_types:
981        return match.group(1)
982    return 'all'
983
984needed_envs = [identifyTarget(target) for target in BUILD_TARGETS]
985if 'all' in needed_envs:
986    needed_envs += target_types
987
988# Debug binary
989if 'debug' in needed_envs:
990    makeEnv('debug', '.do',
991            CCFLAGS = Split(ccflags['debug']),
992            CPPDEFINES = ['DEBUG', 'TRACING_ON=1'])
993
994# Optimized binary
995if 'opt' in needed_envs:
996    makeEnv('opt', '.o',
997            CCFLAGS = Split(ccflags['opt']),
998            CPPDEFINES = ['TRACING_ON=1'])
999
1000# "Fast" binary
1001if 'fast' in needed_envs:
1002    makeEnv('fast', '.fo', strip = True,
1003            CCFLAGS = Split(ccflags['fast']),
1004            CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'])
1005
1006# Profiled binary
1007if 'prof' in needed_envs:
1008    makeEnv('prof', '.po',
1009            CCFLAGS = Split(ccflags['prof']),
1010            CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1011            LINKFLAGS = '-pg')
1012
1013Return('envList')
1014