SConscript revision 10810
16019Shines@cs.fsu.edu# -*- mode:python -*-
26019Shines@cs.fsu.edu
37100Sgblack@eecs.umich.edu# Copyright (c) 2004-2005 The Regents of The University of Michigan
47100Sgblack@eecs.umich.edu# All rights reserved.
57100Sgblack@eecs.umich.edu#
67100Sgblack@eecs.umich.edu# Redistribution and use in source and binary forms, with or without
77100Sgblack@eecs.umich.edu# modification, are permitted provided that the following conditions are
87100Sgblack@eecs.umich.edu# met: redistributions of source code must retain the above copyright
97100Sgblack@eecs.umich.edu# notice, this list of conditions and the following disclaimer;
107100Sgblack@eecs.umich.edu# redistributions in binary form must reproduce the above copyright
117100Sgblack@eecs.umich.edu# notice, this list of conditions and the following disclaimer in the
127100Sgblack@eecs.umich.edu# documentation and/or other materials provided with the distribution;
137100Sgblack@eecs.umich.edu# neither the name of the copyright holders nor the names of its
147100Sgblack@eecs.umich.edu# contributors may be used to endorse or promote products derived from
156019Shines@cs.fsu.edu# this software without specific prior written permission.
166019Shines@cs.fsu.edu#
176019Shines@cs.fsu.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
186019Shines@cs.fsu.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
196019Shines@cs.fsu.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
206019Shines@cs.fsu.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
216019Shines@cs.fsu.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
226019Shines@cs.fsu.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
236019Shines@cs.fsu.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
246019Shines@cs.fsu.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
256019Shines@cs.fsu.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
266019Shines@cs.fsu.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
276019Shines@cs.fsu.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
286019Shines@cs.fsu.edu#
296019Shines@cs.fsu.edu# Authors: Nathan Binkert
306019Shines@cs.fsu.edu
316019Shines@cs.fsu.eduimport array
326019Shines@cs.fsu.eduimport bisect
336019Shines@cs.fsu.eduimport imp
346019Shines@cs.fsu.eduimport marshal
356019Shines@cs.fsu.eduimport os
366019Shines@cs.fsu.eduimport re
376019Shines@cs.fsu.eduimport sys
386019Shines@cs.fsu.eduimport zlib
396019Shines@cs.fsu.edu
406019Shines@cs.fsu.edufrom os.path import basename, dirname, exists, isdir, isfile, join as joinpath
416019Shines@cs.fsu.edu
426757SAli.Saidi@ARM.comimport SCons
436019Shines@cs.fsu.edu
446019Shines@cs.fsu.edu# This file defines how to build a particular configuration of gem5
456019Shines@cs.fsu.edu# based on variable settings in the 'env' build environment.
466019Shines@cs.fsu.edu
476019Shines@cs.fsu.eduImport('*')
486019Shines@cs.fsu.edu
496019Shines@cs.fsu.edu# Children need to see the environment
506019Shines@cs.fsu.eduExport('env')
517170Sgblack@eecs.umich.edu
526253Sgblack@eecs.umich.edubuild_env = [(opt, env[opt]) for opt in export_vars]
537202Sgblack@eecs.umich.edu
546253Sgblack@eecs.umich.edufrom m5.util import code_formatter, compareVersions
556253Sgblack@eecs.umich.edu
567396Sgblack@eecs.umich.edu########################################################################
577405SAli.Saidi@ARM.com# Code for adding source files of various types
587259Sgblack@eecs.umich.edu#
597423Sgblack@eecs.umich.edu# When specifying a source file of some type, a set of guards can be
606397Sgblack@eecs.umich.edu# specified for that file.  When get() is used to find the files, if
616019Shines@cs.fsu.edu# get specifies a set of filters, only files that match those filters
626757SAli.Saidi@ARM.com# will be accepted (unspecified filters on files are assumed to be
636019Shines@cs.fsu.edu# false).  Current filters are:
646397Sgblack@eecs.umich.edu#     main -- specifies the gem5 main() function
656019Shines@cs.fsu.edu#     skip_lib -- do not put this file into the gem5 library
666397Sgblack@eecs.umich.edu#     skip_no_python -- do not put this file into a no_python library
676019Shines@cs.fsu.edu#       as it embeds compiled Python
687404SAli.Saidi@ARM.com#     <unittest> -- unit tests use filters based on the unit test name
696735Sgblack@eecs.umich.edu#
707100Sgblack@eecs.umich.edu# A parent can now be specified for a source file and default filter
716019Shines@cs.fsu.edu# values will be retrieved recursively from parents (children override
726757SAli.Saidi@ARM.com# parents).
736757SAli.Saidi@ARM.com#
746757SAli.Saidi@ARM.comclass SourceMeta(type):
757694SAli.Saidi@ARM.com    '''Meta class for source files that keeps track of all files of a
767585SAli.Saidi@arm.com    particular type and has a get function for finding all functions
777404SAli.Saidi@ARM.com    of a certain type that match a set of guards'''
786757SAli.Saidi@ARM.com    def __init__(cls, name, bases, dict):
796757SAli.Saidi@ARM.com        super(SourceMeta, cls).__init__(name, bases, dict)
806757SAli.Saidi@ARM.com        cls.all = []
816019Shines@cs.fsu.edu        
826019Shines@cs.fsu.edu    def get(cls, **guards):
836019Shines@cs.fsu.edu        '''Find all files that match the specified guards.  If a source
846019Shines@cs.fsu.edu        file does not specify a flag, the default is False'''
856019Shines@cs.fsu.edu        for src in cls.all:
866019Shines@cs.fsu.edu            for flag,value in guards.iteritems():
876019Shines@cs.fsu.edu                # if the flag is found and has a different value, skip
886019Shines@cs.fsu.edu                # this file
896019Shines@cs.fsu.edu                if src.all_guards.get(flag, False) != value:
906019Shines@cs.fsu.edu                    break
916019Shines@cs.fsu.edu            else:
926019Shines@cs.fsu.edu                yield src
93
94class SourceFile(object):
95    '''Base object that encapsulates the notion of a source file.
96    This includes, the source node, target node, various manipulations
97    of those.  A source file also specifies a set of guards which
98    describing which builds the source file applies to.  A parent can
99    also be specified to get default guards from'''
100    __metaclass__ = SourceMeta
101    def __init__(self, source, parent=None, **guards):
102        self.guards = guards
103        self.parent = parent
104
105        tnode = source
106        if not isinstance(source, SCons.Node.FS.File):
107            tnode = File(source)
108
109        self.tnode = tnode
110        self.snode = tnode.srcnode()
111
112        for base in type(self).__mro__:
113            if issubclass(base, SourceFile):
114                base.all.append(self)
115
116    @property
117    def filename(self):
118        return str(self.tnode)
119
120    @property
121    def dirname(self):
122        return dirname(self.filename)
123
124    @property
125    def basename(self):
126        return basename(self.filename)
127
128    @property
129    def extname(self):
130        index = self.basename.rfind('.')
131        if index <= 0:
132            # dot files aren't extensions
133            return self.basename, None
134
135        return self.basename[:index], self.basename[index+1:]
136
137    @property
138    def all_guards(self):
139        '''find all guards for this object getting default values
140        recursively from its parents'''
141        guards = {}
142        if self.parent:
143            guards.update(self.parent.guards)
144        guards.update(self.guards)
145        return guards
146
147    def __lt__(self, other): return self.filename < other.filename
148    def __le__(self, other): return self.filename <= other.filename
149    def __gt__(self, other): return self.filename > other.filename
150    def __ge__(self, other): return self.filename >= other.filename
151    def __eq__(self, other): return self.filename == other.filename
152    def __ne__(self, other): return self.filename != other.filename
153
154    @staticmethod
155    def done():
156        def disabled(cls, name, *ignored):
157            raise RuntimeError("Additional SourceFile '%s'" % name,\
158                  "declared, but targets deps are already fixed.")
159        SourceFile.__init__ = disabled
160
161
162class Source(SourceFile):
163    '''Add a c/c++ source file to the build'''
164    def __init__(self, source, Werror=True, swig=False, **guards):
165        '''specify the source file, and any guards'''
166        super(Source, self).__init__(source, **guards)
167
168        self.Werror = Werror
169        self.swig = swig
170
171class PySource(SourceFile):
172    '''Add a python source file to the named package'''
173    invalid_sym_char = re.compile('[^A-z0-9_]')
174    modules = {}
175    tnodes = {}
176    symnames = {}
177
178    def __init__(self, package, source, **guards):
179        '''specify the python package, the source file, and any guards'''
180        super(PySource, self).__init__(source, **guards)
181
182        modname,ext = self.extname
183        assert ext == 'py'
184
185        if package:
186            path = package.split('.')
187        else:
188            path = []
189
190        modpath = path[:]
191        if modname != '__init__':
192            modpath += [ modname ]
193        modpath = '.'.join(modpath)
194
195        arcpath = path + [ self.basename ]
196        abspath = self.snode.abspath
197        if not exists(abspath):
198            abspath = self.tnode.abspath
199
200        self.package = package
201        self.modname = modname
202        self.modpath = modpath
203        self.arcname = joinpath(*arcpath)
204        self.abspath = abspath
205        self.compiled = File(self.filename + 'c')
206        self.cpp = File(self.filename + '.cc')
207        self.symname = PySource.invalid_sym_char.sub('_', modpath)
208
209        PySource.modules[modpath] = self
210        PySource.tnodes[self.tnode] = self
211        PySource.symnames[self.symname] = self
212
213class SimObject(PySource):
214    '''Add a SimObject python file as a python source object and add
215    it to a list of sim object modules'''
216
217    fixed = False
218    modnames = []
219
220    def __init__(self, source, **guards):
221        '''Specify the source file and any guards (automatically in
222        the m5.objects package)'''
223        super(SimObject, self).__init__('m5.objects', source, **guards)
224        if self.fixed:
225            raise AttributeError, "Too late to call SimObject now."
226
227        bisect.insort_right(SimObject.modnames, self.modname)
228
229class SwigSource(SourceFile):
230    '''Add a swig file to build'''
231
232    def __init__(self, package, source, **guards):
233        '''Specify the python package, the source file, and any guards'''
234        super(SwigSource, self).__init__(source, skip_no_python=True, **guards)
235
236        modname,ext = self.extname
237        assert ext == 'i'
238
239        self.module = modname
240        cc_file = joinpath(self.dirname, modname + '_wrap.cc')
241        py_file = joinpath(self.dirname, modname + '.py')
242
243        self.cc_source = Source(cc_file, swig=True, parent=self, **guards)
244        self.py_source = PySource(package, py_file, parent=self, **guards)
245
246class ProtoBuf(SourceFile):
247    '''Add a Protocol Buffer to build'''
248
249    def __init__(self, source, **guards):
250        '''Specify the source file, and any guards'''
251        super(ProtoBuf, self).__init__(source, **guards)
252
253        # Get the file name and the extension
254        modname,ext = self.extname
255        assert ext == 'proto'
256
257        # Currently, we stick to generating the C++ headers, so we
258        # only need to track the source and header.
259        self.cc_file = File(modname + '.pb.cc')
260        self.hh_file = File(modname + '.pb.h')
261
262class UnitTest(object):
263    '''Create a UnitTest'''
264
265    all = []
266    def __init__(self, target, *sources, **kwargs):
267        '''Specify the target name and any sources.  Sources that are
268        not SourceFiles are evalued with Source().  All files are
269        guarded with a guard of the same name as the UnitTest
270        target.'''
271
272        srcs = []
273        for src in sources:
274            if not isinstance(src, SourceFile):
275                src = Source(src, skip_lib=True)
276            src.guards[target] = True
277            srcs.append(src)
278
279        self.sources = srcs
280        self.target = target
281        self.main = kwargs.get('main', False)
282        UnitTest.all.append(self)
283
284# Children should have access
285Export('Source')
286Export('PySource')
287Export('SimObject')
288Export('SwigSource')
289Export('ProtoBuf')
290Export('UnitTest')
291
292########################################################################
293#
294# Debug Flags
295#
296debug_flags = {}
297def DebugFlag(name, desc=None):
298    if name in debug_flags:
299        raise AttributeError, "Flag %s already specified" % name
300    debug_flags[name] = (name, (), desc)
301
302def CompoundFlag(name, flags, desc=None):
303    if name in debug_flags:
304        raise AttributeError, "Flag %s already specified" % name
305
306    compound = tuple(flags)
307    debug_flags[name] = (name, compound, desc)
308
309Export('DebugFlag')
310Export('CompoundFlag')
311
312########################################################################
313#
314# Set some compiler variables
315#
316
317# Include file paths are rooted in this directory.  SCons will
318# automatically expand '.' to refer to both the source directory and
319# the corresponding build directory to pick up generated include
320# files.
321env.Append(CPPPATH=Dir('.'))
322
323for extra_dir in extras_dir_list:
324    env.Append(CPPPATH=Dir(extra_dir))
325
326# Workaround for bug in SCons version > 0.97d20071212
327# Scons bug id: 2006 gem5 Bug id: 308
328for root, dirs, files in os.walk(base_dir, topdown=True):
329    Dir(root[len(base_dir) + 1:])
330
331########################################################################
332#
333# Walk the tree and execute all SConscripts in subdirectories
334#
335
336here = Dir('.').srcnode().abspath
337for root, dirs, files in os.walk(base_dir, topdown=True):
338    if root == here:
339        # we don't want to recurse back into this SConscript
340        continue
341
342    if 'SConscript' in files:
343        build_dir = joinpath(env['BUILDDIR'], root[len(base_dir) + 1:])
344        SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
345
346for extra_dir in extras_dir_list:
347    prefix_len = len(dirname(extra_dir)) + 1
348
349    # Also add the corresponding build directory to pick up generated
350    # include files.
351    env.Append(CPPPATH=Dir(joinpath(env['BUILDDIR'], extra_dir[prefix_len:])))
352
353    for root, dirs, files in os.walk(extra_dir, topdown=True):
354        # if build lives in the extras directory, don't walk down it
355        if 'build' in dirs:
356            dirs.remove('build')
357
358        if 'SConscript' in files:
359            build_dir = joinpath(env['BUILDDIR'], root[prefix_len:])
360            SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
361
362for opt in export_vars:
363    env.ConfigFile(opt)
364
365def makeTheISA(source, target, env):
366    isas = [ src.get_contents() for src in source ]
367    target_isa = env['TARGET_ISA']
368    def define(isa):
369        return isa.upper() + '_ISA'
370    
371    def namespace(isa):
372        return isa[0].upper() + isa[1:].lower() + 'ISA' 
373
374
375    code = code_formatter()
376    code('''\
377#ifndef __CONFIG_THE_ISA_HH__
378#define __CONFIG_THE_ISA_HH__
379
380''')
381
382    # create defines for the preprocessing and compile-time determination
383    for i,isa in enumerate(isas):
384        code('#define $0 $1', define(isa), i + 1)
385    code()
386
387    # create an enum for any run-time determination of the ISA, we
388    # reuse the same name as the namespaces
389    code('enum class Arch {')
390    for i,isa in enumerate(isas):
391        if i + 1 == len(isas):
392            code('  $0 = $1', namespace(isa), define(isa))
393        else:
394            code('  $0 = $1,', namespace(isa), define(isa))
395    code('};')
396
397    code('''
398
399#define THE_ISA ${{define(target_isa)}}
400#define TheISA ${{namespace(target_isa)}}
401#define THE_ISA_STR "${{target_isa}}"
402
403#endif // __CONFIG_THE_ISA_HH__''')
404
405    code.write(str(target[0]))
406
407env.Command('config/the_isa.hh', map(Value, all_isa_list),
408            MakeAction(makeTheISA, Transform("CFG ISA", 0)))
409
410########################################################################
411#
412# Prevent any SimObjects from being added after this point, they
413# should all have been added in the SConscripts above
414#
415SimObject.fixed = True
416
417class DictImporter(object):
418    '''This importer takes a dictionary of arbitrary module names that
419    map to arbitrary filenames.'''
420    def __init__(self, modules):
421        self.modules = modules
422        self.installed = set()
423
424    def __del__(self):
425        self.unload()
426
427    def unload(self):
428        import sys
429        for module in self.installed:
430            del sys.modules[module]
431        self.installed = set()
432
433    def find_module(self, fullname, path):
434        if fullname == 'm5.defines':
435            return self
436
437        if fullname == 'm5.objects':
438            return self
439
440        if fullname.startswith('m5.internal'):
441            return None
442
443        source = self.modules.get(fullname, None)
444        if source is not None and fullname.startswith('m5.objects'):
445            return self
446
447        return None
448
449    def load_module(self, fullname):
450        mod = imp.new_module(fullname)
451        sys.modules[fullname] = mod
452        self.installed.add(fullname)
453
454        mod.__loader__ = self
455        if fullname == 'm5.objects':
456            mod.__path__ = fullname.split('.')
457            return mod
458
459        if fullname == 'm5.defines':
460            mod.__dict__['buildEnv'] = m5.util.SmartDict(build_env)
461            return mod
462
463        source = self.modules[fullname]
464        if source.modname == '__init__':
465            mod.__path__ = source.modpath
466        mod.__file__ = source.abspath
467
468        exec file(source.abspath, 'r') in mod.__dict__
469
470        return mod
471
472import m5.SimObject
473import m5.params
474from m5.util import code_formatter
475
476m5.SimObject.clear()
477m5.params.clear()
478
479# install the python importer so we can grab stuff from the source
480# tree itself.  We can't have SimObjects added after this point or
481# else we won't know about them for the rest of the stuff.
482importer = DictImporter(PySource.modules)
483sys.meta_path[0:0] = [ importer ]
484
485# import all sim objects so we can populate the all_objects list
486# make sure that we're working with a list, then let's sort it
487for modname in SimObject.modnames:
488    exec('from m5.objects import %s' % modname)
489
490# we need to unload all of the currently imported modules so that they
491# will be re-imported the next time the sconscript is run
492importer.unload()
493sys.meta_path.remove(importer)
494
495sim_objects = m5.SimObject.allClasses
496all_enums = m5.params.allEnums
497
498if m5.SimObject.noCxxHeader:
499    print >> sys.stderr, \
500        "warning: At least one SimObject lacks a header specification. " \
501        "This can cause unexpected results in the generated SWIG " \
502        "wrappers."
503
504# Find param types that need to be explicitly wrapped with swig.
505# These will be recognized because the ParamDesc will have a
506# swig_decl() method.  Most param types are based on types that don't
507# need this, either because they're based on native types (like Int)
508# or because they're SimObjects (which get swigged independently).
509# For now the only things handled here are VectorParam types.
510params_to_swig = {}
511for name,obj in sorted(sim_objects.iteritems()):
512    for param in obj._params.local.values():
513        # load the ptype attribute now because it depends on the
514        # current version of SimObject.allClasses, but when scons
515        # actually uses the value, all versions of
516        # SimObject.allClasses will have been loaded
517        param.ptype
518
519        if not hasattr(param, 'swig_decl'):
520            continue
521        pname = param.ptype_str
522        if pname not in params_to_swig:
523            params_to_swig[pname] = param
524
525########################################################################
526#
527# calculate extra dependencies
528#
529module_depends = ["m5", "m5.SimObject", "m5.params"]
530depends = [ PySource.modules[dep].snode for dep in module_depends ]
531depends.sort(key = lambda x: x.name)
532
533########################################################################
534#
535# Commands for the basic automatically generated python files
536#
537
538# Generate Python file containing a dict specifying the current
539# buildEnv flags.
540def makeDefinesPyFile(target, source, env):
541    build_env = source[0].get_contents()
542
543    code = code_formatter()
544    code("""
545import m5.internal
546import m5.util
547
548buildEnv = m5.util.SmartDict($build_env)
549
550compileDate = m5.internal.core.compileDate
551_globals = globals()
552for key,val in m5.internal.core.__dict__.iteritems():
553    if key.startswith('flag_'):
554        flag = key[5:]
555        _globals[flag] = val
556del _globals
557""")
558    code.write(target[0].abspath)
559
560defines_info = Value(build_env)
561# Generate a file with all of the compile options in it
562env.Command('python/m5/defines.py', defines_info,
563            MakeAction(makeDefinesPyFile, Transform("DEFINES", 0)))
564PySource('m5', 'python/m5/defines.py')
565
566# Generate python file containing info about the M5 source code
567def makeInfoPyFile(target, source, env):
568    code = code_formatter()
569    for src in source:
570        data = ''.join(file(src.srcnode().abspath, 'r').xreadlines())
571        code('$src = ${{repr(data)}}')
572    code.write(str(target[0]))
573
574# Generate a file that wraps the basic top level files
575env.Command('python/m5/info.py',
576            [ '#/COPYING', '#/LICENSE', '#/README', ],
577            MakeAction(makeInfoPyFile, Transform("INFO")))
578PySource('m5', 'python/m5/info.py')
579
580########################################################################
581#
582# Create all of the SimObject param headers and enum headers
583#
584
585def createSimObjectParamStruct(target, source, env):
586    assert len(target) == 1 and len(source) == 1
587
588    name = str(source[0].get_contents())
589    obj = sim_objects[name]
590
591    code = code_formatter()
592    obj.cxx_param_decl(code)
593    code.write(target[0].abspath)
594
595def createSimObjectCxxConfig(is_header):
596    def body(target, source, env):
597        assert len(target) == 1 and len(source) == 1
598
599        name = str(source[0].get_contents())
600        obj = sim_objects[name]
601
602        code = code_formatter()
603        obj.cxx_config_param_file(code, is_header)
604        code.write(target[0].abspath)
605    return body
606
607def createParamSwigWrapper(target, source, env):
608    assert len(target) == 1 and len(source) == 1
609
610    name = str(source[0].get_contents())
611    param = params_to_swig[name]
612
613    code = code_formatter()
614    param.swig_decl(code)
615    code.write(target[0].abspath)
616
617def createEnumStrings(target, source, env):
618    assert len(target) == 1 and len(source) == 1
619
620    name = str(source[0].get_contents())
621    obj = all_enums[name]
622
623    code = code_formatter()
624    obj.cxx_def(code)
625    code.write(target[0].abspath)
626
627def createEnumDecls(target, source, env):
628    assert len(target) == 1 and len(source) == 1
629
630    name = str(source[0].get_contents())
631    obj = all_enums[name]
632
633    code = code_formatter()
634    obj.cxx_decl(code)
635    code.write(target[0].abspath)
636
637def createEnumSwigWrapper(target, source, env):
638    assert len(target) == 1 and len(source) == 1
639
640    name = str(source[0].get_contents())
641    obj = all_enums[name]
642
643    code = code_formatter()
644    obj.swig_decl(code)
645    code.write(target[0].abspath)
646
647def createSimObjectSwigWrapper(target, source, env):
648    name = source[0].get_contents()
649    obj = sim_objects[name]
650
651    code = code_formatter()
652    obj.swig_decl(code)
653    code.write(target[0].abspath)
654
655# dummy target for generated code
656# we start out with all the Source files so they get copied to build/*/ also.
657SWIG = env.Dummy('swig', [s.tnode for s in Source.get()])
658
659# Generate all of the SimObject param C++ struct header files
660params_hh_files = []
661for name,simobj in sorted(sim_objects.iteritems()):
662    py_source = PySource.modules[simobj.__module__]
663    extra_deps = [ py_source.tnode ]
664
665    hh_file = File('params/%s.hh' % name)
666    params_hh_files.append(hh_file)
667    env.Command(hh_file, Value(name),
668                MakeAction(createSimObjectParamStruct, Transform("SO PARAM")))
669    env.Depends(hh_file, depends + extra_deps)
670    env.Depends(SWIG, hh_file)
671
672# C++ parameter description files
673if GetOption('with_cxx_config'):
674    for name,simobj in sorted(sim_objects.iteritems()):
675        py_source = PySource.modules[simobj.__module__]
676        extra_deps = [ py_source.tnode ]
677
678        cxx_config_hh_file = File('cxx_config/%s.hh' % name)
679        cxx_config_cc_file = File('cxx_config/%s.cc' % name)
680        env.Command(cxx_config_hh_file, Value(name),
681                    MakeAction(createSimObjectCxxConfig(True),
682                    Transform("CXXCPRHH")))
683        env.Command(cxx_config_cc_file, Value(name),
684                    MakeAction(createSimObjectCxxConfig(False),
685                    Transform("CXXCPRCC")))
686        env.Depends(cxx_config_hh_file, depends + extra_deps +
687                    [File('params/%s.hh' % name), File('sim/cxx_config.hh')])
688        env.Depends(cxx_config_cc_file, depends + extra_deps +
689                    [cxx_config_hh_file])
690        Source(cxx_config_cc_file)
691
692    cxx_config_init_cc_file = File('cxx_config/init.cc')
693
694    def createCxxConfigInitCC(target, source, env):
695        assert len(target) == 1 and len(source) == 1
696
697        code = code_formatter()
698
699        for name,simobj in sorted(sim_objects.iteritems()):
700            if not hasattr(simobj, 'abstract') or not simobj.abstract:
701                code('#include "cxx_config/${name}.hh"')
702        code()
703        code('void cxxConfigInit()')
704        code('{')
705        code.indent()
706        for name,simobj in sorted(sim_objects.iteritems()):
707            not_abstract = not hasattr(simobj, 'abstract') or \
708                not simobj.abstract
709            if not_abstract and 'type' in simobj.__dict__:
710                code('cxx_config_directory["${name}"] = '
711                     '${name}CxxConfigParams::makeDirectoryEntry();')
712        code.dedent()
713        code('}')
714        code.write(target[0].abspath)
715
716    py_source = PySource.modules[simobj.__module__]
717    extra_deps = [ py_source.tnode ]
718    env.Command(cxx_config_init_cc_file, Value(name),
719        MakeAction(createCxxConfigInitCC, Transform("CXXCINIT")))
720    cxx_param_hh_files = ["cxx_config/%s.hh" % simobj
721        for name,simobj in sorted(sim_objects.iteritems())
722        if not hasattr(simobj, 'abstract') or not simobj.abstract]
723    Depends(cxx_config_init_cc_file, cxx_param_hh_files +
724            [File('sim/cxx_config.hh')])
725    Source(cxx_config_init_cc_file)
726
727# Generate any needed param SWIG wrapper files
728params_i_files = []
729for name,param in sorted(params_to_swig.iteritems()):
730    i_file = File('python/m5/internal/%s.i' % (param.swig_module_name()))
731    params_i_files.append(i_file)
732    env.Command(i_file, Value(name),
733                MakeAction(createParamSwigWrapper, Transform("SW PARAM")))
734    env.Depends(i_file, depends)
735    env.Depends(SWIG, i_file)
736    SwigSource('m5.internal', i_file)
737
738# Generate all enum header files
739for name,enum in sorted(all_enums.iteritems()):
740    py_source = PySource.modules[enum.__module__]
741    extra_deps = [ py_source.tnode ]
742
743    cc_file = File('enums/%s.cc' % name)
744    env.Command(cc_file, Value(name),
745                MakeAction(createEnumStrings, Transform("ENUM STR")))
746    env.Depends(cc_file, depends + extra_deps)
747    env.Depends(SWIG, cc_file)
748    Source(cc_file)
749
750    hh_file = File('enums/%s.hh' % name)
751    env.Command(hh_file, Value(name),
752                MakeAction(createEnumDecls, Transform("ENUMDECL")))
753    env.Depends(hh_file, depends + extra_deps)
754    env.Depends(SWIG, hh_file)
755
756    i_file = File('python/m5/internal/enum_%s.i' % name)
757    env.Command(i_file, Value(name),
758                MakeAction(createEnumSwigWrapper, Transform("ENUMSWIG")))
759    env.Depends(i_file, depends + extra_deps)
760    env.Depends(SWIG, i_file)
761    SwigSource('m5.internal', i_file)
762
763# Generate SimObject SWIG wrapper files
764for name,simobj in sorted(sim_objects.iteritems()):
765    py_source = PySource.modules[simobj.__module__]
766    extra_deps = [ py_source.tnode ]
767    i_file = File('python/m5/internal/param_%s.i' % name)
768    env.Command(i_file, Value(name),
769                MakeAction(createSimObjectSwigWrapper, Transform("SO SWIG")))
770    env.Depends(i_file, depends + extra_deps)
771    SwigSource('m5.internal', i_file)
772
773# Generate the main swig init file
774def makeEmbeddedSwigInit(target, source, env):
775    code = code_formatter()
776    module = source[0].get_contents()
777    code('''\
778#include "sim/init.hh"
779
780extern "C" {
781    void init_${module}();
782}
783
784EmbeddedSwig embed_swig_${module}(init_${module});
785''')
786    code.write(str(target[0]))
787    
788# Build all swig modules
789for swig in SwigSource.all:
790    env.Command([swig.cc_source.tnode, swig.py_source.tnode], swig.tnode,
791                MakeAction('$SWIG $SWIGFLAGS -outdir ${TARGETS[1].dir} '
792                '-o ${TARGETS[0]} $SOURCES', Transform("SWIG")))
793    cc_file = str(swig.tnode)
794    init_file = '%s/%s_init.cc' % (dirname(cc_file), basename(cc_file))
795    env.Command(init_file, Value(swig.module),
796                MakeAction(makeEmbeddedSwigInit, Transform("EMBED SW")))
797    env.Depends(SWIG, init_file)
798    Source(init_file, **swig.guards)
799
800# Build all protocol buffers if we have got protoc and protobuf available
801if env['HAVE_PROTOBUF']:
802    for proto in ProtoBuf.all:
803        # Use both the source and header as the target, and the .proto
804        # file as the source. When executing the protoc compiler, also
805        # specify the proto_path to avoid having the generated files
806        # include the path.
807        env.Command([proto.cc_file, proto.hh_file], proto.tnode,
808                    MakeAction('$PROTOC --cpp_out ${TARGET.dir} '
809                               '--proto_path ${SOURCE.dir} $SOURCE',
810                               Transform("PROTOC")))
811
812        env.Depends(SWIG, [proto.cc_file, proto.hh_file])
813        # Add the C++ source file
814        Source(proto.cc_file, **proto.guards)
815elif ProtoBuf.all:
816    print 'Got protobuf to build, but lacks support!'
817    Exit(1)
818
819#
820# Handle debug flags
821#
822def makeDebugFlagCC(target, source, env):
823    assert(len(target) == 1 and len(source) == 1)
824
825    code = code_formatter()
826
827    # delay definition of CompoundFlags until after all the definition
828    # of all constituent SimpleFlags
829    comp_code = code_formatter()
830
831    # file header
832    code('''
833/*
834 * DO NOT EDIT THIS FILE! Automatically generated by SCons.
835 */
836
837#include "base/debug.hh"
838
839namespace Debug {
840
841''')
842
843    for name, flag in sorted(source[0].read().iteritems()):
844        n, compound, desc = flag
845        assert n == name
846
847        if not compound:
848            code('SimpleFlag $name("$name", "$desc");')
849        else:
850            comp_code('CompoundFlag $name("$name", "$desc",')
851            comp_code.indent()
852            last = len(compound) - 1
853            for i,flag in enumerate(compound):
854                if i != last:
855                    comp_code('&$flag,')
856                else:
857                    comp_code('&$flag);')
858            comp_code.dedent()
859
860    code.append(comp_code)
861    code()
862    code('} // namespace Debug')
863
864    code.write(str(target[0]))
865
866def makeDebugFlagHH(target, source, env):
867    assert(len(target) == 1 and len(source) == 1)
868
869    val = eval(source[0].get_contents())
870    name, compound, desc = val
871
872    code = code_formatter()
873
874    # file header boilerplate
875    code('''\
876/*
877 * DO NOT EDIT THIS FILE! Automatically generated by SCons.
878 */
879
880#ifndef __DEBUG_${name}_HH__
881#define __DEBUG_${name}_HH__
882
883namespace Debug {
884''')
885
886    if compound:
887        code('class CompoundFlag;')
888    code('class SimpleFlag;')
889
890    if compound:
891        code('extern CompoundFlag $name;')
892        for flag in compound:
893            code('extern SimpleFlag $flag;')
894    else:
895        code('extern SimpleFlag $name;')
896
897    code('''
898}
899
900#endif // __DEBUG_${name}_HH__
901''')
902
903    code.write(str(target[0]))
904
905for name,flag in sorted(debug_flags.iteritems()):
906    n, compound, desc = flag
907    assert n == name
908
909    hh_file = 'debug/%s.hh' % name
910    env.Command(hh_file, Value(flag),
911                MakeAction(makeDebugFlagHH, Transform("TRACING", 0)))
912    env.Depends(SWIG, hh_file)
913
914env.Command('debug/flags.cc', Value(debug_flags),
915            MakeAction(makeDebugFlagCC, Transform("TRACING", 0)))
916env.Depends(SWIG, 'debug/flags.cc')
917Source('debug/flags.cc')
918
919# Embed python files.  All .py files that have been indicated by a
920# PySource() call in a SConscript need to be embedded into the M5
921# library.  To do that, we compile the file to byte code, marshal the
922# byte code, compress it, and then generate a c++ file that
923# inserts the result into an array.
924def embedPyFile(target, source, env):
925    def c_str(string):
926        if string is None:
927            return "0"
928        return '"%s"' % string
929
930    '''Action function to compile a .py into a code object, marshal
931    it, compress it, and stick it into an asm file so the code appears
932    as just bytes with a label in the data section'''
933
934    src = file(str(source[0]), 'r').read()
935
936    pysource = PySource.tnodes[source[0]]
937    compiled = compile(src, pysource.abspath, 'exec')
938    marshalled = marshal.dumps(compiled)
939    compressed = zlib.compress(marshalled)
940    data = compressed
941    sym = pysource.symname
942
943    code = code_formatter()
944    code('''\
945#include "sim/init.hh"
946
947namespace {
948
949const uint8_t data_${sym}[] = {
950''')
951    code.indent()
952    step = 16
953    for i in xrange(0, len(data), step):
954        x = array.array('B', data[i:i+step])
955        code(''.join('%d,' % d for d in x))
956    code.dedent()
957    
958    code('''};
959
960EmbeddedPython embedded_${sym}(
961    ${{c_str(pysource.arcname)}},
962    ${{c_str(pysource.abspath)}},
963    ${{c_str(pysource.modpath)}},
964    data_${sym},
965    ${{len(data)}},
966    ${{len(marshalled)}});
967
968} // anonymous namespace
969''')
970    code.write(str(target[0]))
971
972for source in PySource.all:
973    env.Command(source.cpp, source.tnode,
974                MakeAction(embedPyFile, Transform("EMBED PY")))
975    env.Depends(SWIG, source.cpp)
976    Source(source.cpp, skip_no_python=True)
977
978########################################################################
979#
980# Define binaries.  Each different build type (debug, opt, etc.) gets
981# a slightly different build environment.
982#
983
984# List of constructed environments to pass back to SConstruct
985date_source = Source('base/date.cc', skip_lib=True)
986
987# Capture this directory for the closure makeEnv, otherwise when it is
988# called, it won't know what directory it should use.
989variant_dir = Dir('.').path
990def variant(*path):
991    return os.path.join(variant_dir, *path)
992def variantd(*path):
993    return variant(*path)+'/'
994
995# Function to create a new build environment as clone of current
996# environment 'env' with modified object suffix and optional stripped
997# binary.  Additional keyword arguments are appended to corresponding
998# build environment vars.
999def makeEnv(env, label, objsfx, strip = False, **kwargs):
1000    # SCons doesn't know to append a library suffix when there is a '.' in the
1001    # name.  Use '_' instead.
1002    libname = variant('gem5_' + label)
1003    exename = variant('gem5.' + label)
1004    secondary_exename = variant('m5.' + label)
1005
1006    new_env = env.Clone(OBJSUFFIX=objsfx, SHOBJSUFFIX=objsfx + 's')
1007    new_env.Label = label
1008    new_env.Append(**kwargs)
1009
1010    swig_env = new_env.Clone()
1011
1012    # Both gcc and clang have issues with unused labels and values in
1013    # the SWIG generated code
1014    swig_env.Append(CCFLAGS=['-Wno-unused-label', '-Wno-unused-value'])
1015
1016    # Add additional warnings here that should not be applied to
1017    # the SWIG generated code
1018    new_env.Append(CXXFLAGS='-Wmissing-declarations')
1019
1020    if env['GCC']:
1021        # Depending on the SWIG version, we also need to supress
1022        # warnings about uninitialized variables and missing field
1023        # initializers.
1024        swig_env.Append(CCFLAGS=['-Wno-uninitialized',
1025                                 '-Wno-missing-field-initializers',
1026                                 '-Wno-unused-but-set-variable'])
1027
1028        # If gcc supports it, also warn for deletion of derived
1029        # classes with non-virtual desctructors. For gcc >= 4.7 we
1030        # also have to disable warnings about the SWIG code having
1031        # potentially uninitialized variables.
1032        if compareVersions(env['GCC_VERSION'], '4.7') >= 0:
1033            new_env.Append(CXXFLAGS='-Wdelete-non-virtual-dtor')
1034            swig_env.Append(CCFLAGS='-Wno-maybe-uninitialized')
1035
1036        # Only gcc >= 4.9 supports UBSan, so check both the version
1037        # and the command-line option before adding the compiler and
1038        # linker flags.
1039        if GetOption('with_ubsan') and \
1040                compareVersions(env['GCC_VERSION'], '4.9') >= 0:
1041            new_env.Append(CCFLAGS='-fsanitize=undefined')
1042            new_env.Append(LINKFLAGS='-fsanitize=undefined')
1043
1044    if env['CLANG']:
1045        # Always enable the warning for deletion of derived classes
1046        # with non-virtual destructors
1047        new_env.Append(CXXFLAGS=['-Wdelete-non-virtual-dtor'])
1048
1049        swig_env.Append(CCFLAGS=[
1050                # Some versions of SWIG can return uninitialized values
1051                '-Wno-sometimes-uninitialized',
1052                # Register storage is requested in a lot of places in
1053                # SWIG-generated code.
1054                '-Wno-deprecated-register',
1055                ])
1056
1057        # All supported clang versions have support for UBSan, so if
1058        # asked to use it, append the compiler and linker flags.
1059        if GetOption('with_ubsan'):
1060            new_env.Append(CCFLAGS='-fsanitize=undefined')
1061            new_env.Append(LINKFLAGS='-fsanitize=undefined')
1062
1063    werror_env = new_env.Clone()
1064    # Treat warnings as errors but white list some warnings that we
1065    # want to allow (e.g., deprecation warnings).
1066    werror_env.Append(CCFLAGS=['-Werror',
1067                               '-Wno-error=deprecated-declarations',
1068                               '-Wno-error=deprecated',
1069                               ])
1070
1071    def make_obj(source, static, extra_deps = None):
1072        '''This function adds the specified source to the correct
1073        build environment, and returns the corresponding SCons Object
1074        nodes'''
1075
1076        if source.swig:
1077            env = swig_env
1078        elif source.Werror:
1079            env = werror_env
1080        else:
1081            env = new_env
1082
1083        if static:
1084            obj = env.StaticObject(source.tnode)
1085        else:
1086            obj = env.SharedObject(source.tnode)
1087
1088        if extra_deps:
1089            env.Depends(obj, extra_deps)
1090
1091        return obj
1092
1093    lib_guards = {'main': False, 'skip_lib': False}
1094
1095    # Without Python, leave out all SWIG and Python content from the
1096    # library builds.  The option doesn't affect gem5 built as a program
1097    if GetOption('without_python'):
1098        lib_guards['skip_no_python'] = False
1099
1100    static_objs = [ make_obj(s, True) for s in Source.get(**lib_guards) ]
1101    shared_objs = [ make_obj(s, False) for s in Source.get(**lib_guards) ]
1102
1103    static_date = make_obj(date_source, static=True, extra_deps=static_objs)
1104    static_objs.append(static_date)
1105
1106    shared_date = make_obj(date_source, static=False, extra_deps=shared_objs)
1107    shared_objs.append(shared_date)
1108
1109    # First make a library of everything but main() so other programs can
1110    # link against m5.
1111    static_lib = new_env.StaticLibrary(libname, static_objs)
1112    shared_lib = new_env.SharedLibrary(libname, shared_objs)
1113
1114    # Now link a stub with main() and the static library.
1115    main_objs = [ make_obj(s, True) for s in Source.get(main=True) ]
1116
1117    for test in UnitTest.all:
1118        flags = { test.target : True }
1119        test_sources = Source.get(**flags)
1120        test_objs = [ make_obj(s, static=True) for s in test_sources ]
1121        if test.main:
1122            test_objs += main_objs
1123        path = variant('unittest/%s.%s' % (test.target, label))
1124        new_env.Program(path, test_objs + static_objs)
1125
1126    progname = exename
1127    if strip:
1128        progname += '.unstripped'
1129
1130    targets = new_env.Program(progname, main_objs + static_objs)
1131
1132    if strip:
1133        if sys.platform == 'sunos5':
1134            cmd = 'cp $SOURCE $TARGET; strip $TARGET'
1135        else:
1136            cmd = 'strip $SOURCE -o $TARGET'
1137        targets = new_env.Command(exename, progname,
1138                    MakeAction(cmd, Transform("STRIP")))
1139
1140    new_env.Command(secondary_exename, exename,
1141            MakeAction('ln $SOURCE $TARGET', Transform("HARDLINK")))
1142
1143    new_env.M5Binary = targets[0]
1144    return new_env
1145
1146# Start out with the compiler flags common to all compilers,
1147# i.e. they all use -g for opt and -g -pg for prof
1148ccflags = {'debug' : [], 'opt' : ['-g'], 'fast' : [], 'prof' : ['-g', '-pg'],
1149           'perf' : ['-g']}
1150
1151# Start out with the linker flags common to all linkers, i.e. -pg for
1152# prof, and -lprofiler for perf. The -lprofile flag is surrounded by
1153# no-as-needed and as-needed as the binutils linker is too clever and
1154# simply doesn't link to the library otherwise.
1155ldflags = {'debug' : [], 'opt' : [], 'fast' : [], 'prof' : ['-pg'],
1156           'perf' : ['-Wl,--no-as-needed', '-lprofiler', '-Wl,--as-needed']}
1157
1158# For Link Time Optimization, the optimisation flags used to compile
1159# individual files are decoupled from those used at link time
1160# (i.e. you can compile with -O3 and perform LTO with -O0), so we need
1161# to also update the linker flags based on the target.
1162if env['GCC']:
1163    if sys.platform == 'sunos5':
1164        ccflags['debug'] += ['-gstabs+']
1165    else:
1166        ccflags['debug'] += ['-ggdb3']
1167    ldflags['debug'] += ['-O0']
1168    # opt, fast, prof and perf all share the same cc flags, also add
1169    # the optimization to the ldflags as LTO defers the optimization
1170    # to link time
1171    for target in ['opt', 'fast', 'prof', 'perf']:
1172        ccflags[target] += ['-O3']
1173        ldflags[target] += ['-O3']
1174
1175    ccflags['fast'] += env['LTO_CCFLAGS']
1176    ldflags['fast'] += env['LTO_LDFLAGS']
1177elif env['CLANG']:
1178    ccflags['debug'] += ['-g', '-O0']
1179    # opt, fast, prof and perf all share the same cc flags
1180    for target in ['opt', 'fast', 'prof', 'perf']:
1181        ccflags[target] += ['-O3']
1182else:
1183    print 'Unknown compiler, please fix compiler options'
1184    Exit(1)
1185
1186
1187# To speed things up, we only instantiate the build environments we
1188# need.  We try to identify the needed environment for each target; if
1189# we can't, we fall back on instantiating all the environments just to
1190# be safe.
1191target_types = ['debug', 'opt', 'fast', 'prof', 'perf']
1192obj2target = {'do': 'debug', 'o': 'opt', 'fo': 'fast', 'po': 'prof',
1193              'gpo' : 'perf'}
1194
1195def identifyTarget(t):
1196    ext = t.split('.')[-1]
1197    if ext in target_types:
1198        return ext
1199    if obj2target.has_key(ext):
1200        return obj2target[ext]
1201    match = re.search(r'/tests/([^/]+)/', t)
1202    if match and match.group(1) in target_types:
1203        return match.group(1)
1204    return 'all'
1205
1206needed_envs = [identifyTarget(target) for target in BUILD_TARGETS]
1207if 'all' in needed_envs:
1208    needed_envs += target_types
1209
1210gem5_root = Dir('.').up().up().abspath
1211def makeEnvirons(target, source, env):
1212    # cause any later Source() calls to be fatal, as a diagnostic.
1213    Source.done()
1214
1215    envList = []
1216
1217    # Debug binary
1218    if 'debug' in needed_envs:
1219        envList.append(
1220            makeEnv(env, 'debug', '.do',
1221                    CCFLAGS = Split(ccflags['debug']),
1222                    CPPDEFINES = ['DEBUG', 'TRACING_ON=1'],
1223                    LINKFLAGS = Split(ldflags['debug'])))
1224
1225    # Optimized binary
1226    if 'opt' in needed_envs:
1227        envList.append(
1228            makeEnv(env, 'opt', '.o',
1229                    CCFLAGS = Split(ccflags['opt']),
1230                    CPPDEFINES = ['TRACING_ON=1'],
1231                    LINKFLAGS = Split(ldflags['opt'])))
1232
1233    # "Fast" binary
1234    if 'fast' in needed_envs:
1235        envList.append(
1236            makeEnv(env, 'fast', '.fo', strip = True,
1237                    CCFLAGS = Split(ccflags['fast']),
1238                    CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1239                    LINKFLAGS = Split(ldflags['fast'])))
1240
1241    # Profiled binary using gprof
1242    if 'prof' in needed_envs:
1243        envList.append(
1244            makeEnv(env, 'prof', '.po',
1245                    CCFLAGS = Split(ccflags['prof']),
1246                    CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1247                    LINKFLAGS = Split(ldflags['prof'])))
1248
1249    # Profiled binary using google-pprof
1250    if 'perf' in needed_envs:
1251        envList.append(
1252            makeEnv(env, 'perf', '.gpo',
1253                    CCFLAGS = Split(ccflags['perf']),
1254                    CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1255                    LINKFLAGS = Split(ldflags['perf'])))
1256
1257    # Set up the regression tests for each build.
1258    for e in envList:
1259        SConscript(os.path.join(gem5_root, 'tests', 'SConscript'),
1260                   variant_dir = variantd('tests', e.Label),
1261                   exports = { 'env' : e }, duplicate = False)
1262
1263# The MakeEnvirons Builder defers the full dependency collection until
1264# after processing the ISA definition (due to dynamically generated
1265# source files).  Add this dependency to all targets so they will wait
1266# until the environments are completely set up.  Otherwise, a second
1267# process (e.g. -j2 or higher) will try to compile the requested target,
1268# not know how, and fail.
1269env.Append(BUILDERS = {'MakeEnvirons' :
1270                        Builder(action=MakeAction(makeEnvirons,
1271                                                  Transform("ENVIRONS", 1)))})
1272
1273isa_target = env['PHONY_BASE'] + '-deps'
1274environs   = env['PHONY_BASE'] + '-environs'
1275env.Depends('#all-deps',     isa_target)
1276env.Depends('#all-environs', environs)
1277env.ScanISA(isa_target, File('arch/%s/generated/inc.d' % env['TARGET_ISA']))
1278envSetup = env.MakeEnvirons(environs, isa_target)
1279
1280# make sure no -deps targets occur before all ISAs are complete
1281env.Depends(isa_target, '#all-isas')
1282# likewise for -environs targets and all the -deps targets
1283env.Depends(environs, '#all-deps')
1284