SConstruct revision 5952
1# -*- mode:python -*-
2
3# Copyright (c) 2009 The Hewlett-Packard Development Company
4# Copyright (c) 2004-2005 The Regents of The University of Michigan
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions are
9# met: redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer;
11# redistributions in binary form must reproduce the above copyright
12# notice, this list of conditions and the following disclaimer in the
13# documentation and/or other materials provided with the distribution;
14# neither the name of the copyright holders nor the names of its
15# contributors may be used to endorse or promote products derived from
16# this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29#
30# Authors: Steve Reinhardt
31#          Nathan Binkert
32
33###################################################
34#
35# SCons top-level build description (SConstruct) file.
36#
37# While in this directory ('m5'), just type 'scons' to build the default
38# configuration (see below), or type 'scons build/<CONFIG>/<binary>'
39# to build some other configuration (e.g., 'build/ALPHA_FS/m5.opt' for
40# the optimized full-system version).
41#
42# You can build M5 in a different directory as long as there is a
43# 'build/<CONFIG>' somewhere along the target path.  The build system
44# expects that all configs under the same build directory are being
45# built for the same host system.
46#
47# Examples:
48#
49#   The following two commands are equivalent.  The '-u' option tells
50#   scons to search up the directory tree for this SConstruct file.
51#   % cd <path-to-src>/m5 ; scons build/ALPHA_FS/m5.debug
52#   % cd <path-to-src>/m5/build/ALPHA_FS; scons -u m5.debug
53#
54#   The following two commands are equivalent and demonstrate building
55#   in a directory outside of the source tree.  The '-C' option tells
56#   scons to chdir to the specified directory to find this SConstruct
57#   file.
58#   % cd <path-to-src>/m5 ; scons /local/foo/build/ALPHA_FS/m5.debug
59#   % cd /local/foo/build/ALPHA_FS; scons -C <path-to-src>/m5 m5.debug
60#
61# You can use 'scons -H' to print scons options.  If you're in this
62# 'm5' directory (or use -u or -C to tell scons where to find this
63# file), you can use 'scons -h' to print all the M5-specific build
64# options as well.
65#
66###################################################
67
68# Check for recent-enough Python and SCons versions.
69try:
70    # Really old versions of scons only take two options for the
71    # function, so check once without the revision and once with the
72    # revision, the first instance will fail for stuff other than
73    # 0.98, and the second will fail for 0.98.0
74    EnsureSConsVersion(0, 98)
75    EnsureSConsVersion(0, 98, 1)
76except SystemExit, e:
77    print """
78For more details, see:
79    http://m5sim.org/wiki/index.php/Compiling_M5
80"""
81    raise
82
83# We ensure the python version early because we have stuff that
84# requires python 2.4
85try:
86    EnsurePythonVersion(2, 4)
87except SystemExit, e:
88    print """
89You can use a non-default installation of the Python interpreter by
90either (1) rearranging your PATH so that scons finds the non-default
91'python' first or (2) explicitly invoking an alternative interpreter
92on the scons script.
93
94For more details, see:
95    http://m5sim.org/wiki/index.php/Using_a_non-default_Python_installation
96"""
97    raise
98
99import os
100import re
101import subprocess
102import sys
103
104from os import mkdir, environ
105from os.path import abspath, basename, dirname, expanduser, normpath
106from os.path import exists,  isdir, isfile
107from os.path import join as joinpath, split as splitpath
108
109import SCons
110import SCons.Node
111
112def read_command(cmd, **kwargs):
113    """run the command cmd, read the results and return them
114    this is sorta like `cmd` in shell"""
115    from subprocess import Popen, PIPE, STDOUT
116
117    if isinstance(cmd, str):
118        cmd = cmd.split()
119
120    no_exception = 'exception' in kwargs
121    exception = kwargs.pop('exception', None)
122    
123    kwargs.setdefault('shell', False)
124    kwargs.setdefault('stdout', PIPE)
125    kwargs.setdefault('stderr', STDOUT)
126    kwargs.setdefault('close_fds', True)
127    try:
128        subp = Popen(cmd, **kwargs)
129    except Exception, e:
130        if no_exception:
131            return exception
132        raise
133
134    return subp.communicate()[0]
135
136# helper function: compare arrays or strings of version numbers.
137# E.g., compare_version((1,3,25), (1,4,1)')
138# returns -1, 0, 1 if v1 is <, ==, > v2
139def compare_versions(v1, v2):
140    def make_version_list(v):
141        if isinstance(v, (list,tuple)):
142            return v
143        elif isinstance(v, str):
144            return map(lambda x: int(re.match('\d+', x).group()), v.split('.'))
145        else:
146            raise TypeError
147
148    v1 = make_version_list(v1)
149    v2 = make_version_list(v2)
150    # Compare corresponding elements of lists
151    for n1,n2 in zip(v1, v2):
152        if n1 < n2: return -1
153        if n1 > n2: return  1
154    # all corresponding values are equal... see if one has extra values
155    if len(v1) < len(v2): return -1
156    if len(v1) > len(v2): return  1
157    return 0
158
159########################################################################
160#
161# Set up the base build environment.
162#
163########################################################################
164use_vars = set([ 'AS', 'AR', 'CC', 'CXX', 'HOME', 'PATH', 'RANLIB' ])
165
166use_env = {}
167for key,val in os.environ.iteritems():
168    if key in use_vars or key.startswith("M5"):
169        use_env[key] = val
170
171env = Environment(ENV=use_env)
172env.root = Dir(".")          # The current directory (where this file lives).
173env.srcdir = Dir("src")      # The source directory
174
175########################################################################
176#
177# Mercurial Stuff.
178#
179# If the M5 directory is a mercurial repository, we should do some
180# extra things.
181#
182########################################################################
183
184hgdir = env.root.Dir(".hg")
185
186mercurial_style_message = """
187You're missing the M5 style hook.
188Please install the hook so we can ensure that all code fits a common style.
189
190All you'd need to do is add the following lines to your repository .hg/hgrc
191or your personal .hgrc
192----------------
193
194[extensions]
195style = %s/util/style.py
196
197[hooks]
198pretxncommit.style = python:style.check_whitespace
199""" % (env.root)
200
201mercurial_bin_not_found = """
202Mercurial binary cannot be found, unfortunately this means that we
203cannot easily determine the version of M5 that you are running and
204this makes error messages more difficult to collect.  Please consider
205installing mercurial if you choose to post an error message
206"""
207
208mercurial_lib_not_found = """
209Mercurial libraries cannot be found, ignoring style hook
210If you are actually a M5 developer, please fix this and
211run the style hook. It is important.
212"""
213
214if hgdir.exists():
215    # 1) Grab repository revision if we know it.
216    cmd = "hg id -n -i -t -b"
217    try:
218        hg_info = read_command(cmd, cwd=env.root.abspath).strip()
219    except OSError:
220        hg_info = "Unknown"
221        print mercurial_bin_not_found
222
223    env['HG_INFO'] = hg_info
224
225    # 2) Ensure that the style hook is in place.
226    try:
227        ui = None
228        if ARGUMENTS.get('IGNORE_STYLE') != 'True':
229            from mercurial import ui
230            ui = ui.ui()
231    except ImportError:
232        print mercurial_lib_not_found
233
234    if ui is not None:
235        ui.readconfig(hgdir.File('hgrc').abspath)
236        style_hook = ui.config('hooks', 'pretxncommit.style', None)
237
238        if not style_hook:
239            print mercurial_style_message
240            sys.exit(1)
241else:
242    print ".hg directory not found"
243
244###################################################
245#
246# Figure out which configurations to set up based on the path(s) of
247# the target(s).
248#
249###################################################
250
251# Find default configuration & binary.
252Default(environ.get('M5_DEFAULT_BINARY', 'build/ALPHA_SE/m5.debug'))
253
254# helper function: find last occurrence of element in list
255def rfind(l, elt, offs = -1):
256    for i in range(len(l)+offs, 0, -1):
257        if l[i] == elt:
258            return i
259    raise ValueError, "element not found"
260
261# Each target must have 'build' in the interior of the path; the
262# directory below this will determine the build parameters.  For
263# example, for target 'foo/bar/build/ALPHA_SE/arch/alpha/blah.do' we
264# recognize that ALPHA_SE specifies the configuration because it
265# follow 'build' in the bulid path.
266
267# Generate absolute paths to targets so we can see where the build dir is
268if COMMAND_LINE_TARGETS:
269    # Ask SCons which directory it was invoked from
270    launch_dir = GetLaunchDir()
271    # Make targets relative to invocation directory
272    abs_targets = [ normpath(joinpath(launch_dir, str(x))) for x in \
273                    COMMAND_LINE_TARGETS]
274else:
275    # Default targets are relative to root of tree
276    abs_targets = [ normpath(joinpath(ROOT, str(x))) for x in \
277                    DEFAULT_TARGETS]
278
279
280# Generate a list of the unique build roots and configs that the
281# collected targets reference.
282variant_paths = []
283build_root = None
284for t in abs_targets:
285    path_dirs = t.split('/')
286    try:
287        build_top = rfind(path_dirs, 'build', -2)
288    except:
289        print "Error: no non-leaf 'build' dir found on target path", t
290        Exit(1)
291    this_build_root = joinpath('/',*path_dirs[:build_top+1])
292    if not build_root:
293        build_root = this_build_root
294    else:
295        if this_build_root != build_root:
296            print "Error: build targets not under same build root\n"\
297                  "  %s\n  %s" % (build_root, this_build_root)
298            Exit(1)
299    variant_path = joinpath('/',*path_dirs[:build_top+2])
300    if variant_path not in variant_paths:
301        variant_paths.append(variant_path)
302
303# Make sure build_root exists (might not if this is the first build there)
304if not isdir(build_root):
305    mkdir(build_root)
306
307Export('env')
308
309env.SConsignFile(joinpath(build_root, "sconsign"))
310
311# Default duplicate option is to use hard links, but this messes up
312# when you use emacs to edit a file in the target dir, as emacs moves
313# file to file~ then copies to file, breaking the link.  Symbolic
314# (soft) links work better.
315env.SetOption('duplicate', 'soft-copy')
316
317#
318# Set up global sticky variables... these are common to an entire build
319# tree (not specific to a particular build like ALPHA_SE)
320#
321
322# Variable validators & converters for global sticky variables
323def PathListMakeAbsolute(val):
324    if not val:
325        return val
326    f = lambda p: abspath(expanduser(p))
327    return ':'.join(map(f, val.split(':')))
328
329def PathListAllExist(key, val, env):
330    if not val:
331        return
332    paths = val.split(':')
333    for path in paths:
334        if not isdir(path):
335            raise SCons.Errors.UserError("Path does not exist: '%s'" % path)
336
337global_sticky_vars_file = joinpath(build_root, 'variables.global')
338
339global_sticky_vars = Variables(global_sticky_vars_file, args=ARGUMENTS)
340
341global_sticky_vars.AddVariables(
342    ('CC', 'C compiler', environ.get('CC', env['CC'])),
343    ('CXX', 'C++ compiler', environ.get('CXX', env['CXX'])),
344    ('BATCH', 'Use batch pool for build and tests', False),
345    ('BATCH_CMD', 'Batch pool submission command name', 'qdo'),
346    ('EXTRAS', 'Add Extra directories to the compilation', '',
347     PathListAllExist, PathListMakeAbsolute)
348    )    
349
350# base help text
351help_text = '''
352Usage: scons [scons options] [build options] [target(s)]
353
354Global sticky options:
355'''
356
357help_text += global_sticky_vars.GenerateHelpText(env)
358
359# Update env with values from ARGUMENTS & file global_sticky_vars_file
360global_sticky_vars.Update(env)
361
362# Save sticky variable settings back to current variables file
363global_sticky_vars.Save(global_sticky_vars_file, env)
364
365# Parse EXTRAS variable to build list of all directories where we're
366# look for sources etc.  This list is exported as base_dir_list.
367base_dir = env.srcdir.abspath
368if env['EXTRAS']:
369    extras_dir_list = env['EXTRAS'].split(':')
370else:
371    extras_dir_list = []
372
373Export('base_dir')
374Export('extras_dir_list')
375
376# M5_PLY is used by isa_parser.py to find the PLY package.
377env.Append(ENV = { 'M5_PLY' : str(Dir('ext/ply')) })
378
379CXX_version = read_command([env['CXX'],'--version'], exception=False)
380CXX_V = read_command([env['CXX'],'-V'], exception=False)
381
382env['GCC'] = CXX_version and CXX_version.find('g++') >= 0
383env['SUNCC'] = CXX_V and CXX_V.find('Sun C++') >= 0
384env['ICC'] = CXX_V and CXX_V.find('Intel') >= 0
385if env['GCC'] + env['SUNCC'] + env['ICC'] > 1:
386    print 'Error: How can we have two at the same time?'
387    Exit(1)
388
389# Set up default C++ compiler flags
390if env['GCC']:
391    env.Append(CCFLAGS='-pipe')
392    env.Append(CCFLAGS='-fno-strict-aliasing')
393    env.Append(CCFLAGS=Split('-Wall -Wno-sign-compare -Werror -Wundef'))
394    env.Append(CXXFLAGS='-Wno-deprecated')
395elif env['ICC']:
396    pass #Fix me... add warning flags once we clean up icc warnings
397elif env['SUNCC']:
398    env.Append(CCFLAGS='-Qoption ccfe')
399    env.Append(CCFLAGS='-features=gcc')
400    env.Append(CCFLAGS='-features=extensions')
401    env.Append(CCFLAGS='-library=stlport4')
402    env.Append(CCFLAGS='-xar')
403    #env.Append(CCFLAGS='-instances=semiexplicit')
404else:
405    print 'Error: Don\'t know what compiler options to use for your compiler.'
406    print '       Please fix SConstruct and src/SConscript and try again.'
407    Exit(1)
408
409# Do this after we save setting back, or else we'll tack on an
410# extra 'qdo' every time we run scons.
411if env['BATCH']:
412    env['CC']     = env['BATCH_CMD'] + ' ' + env['CC']
413    env['CXX']    = env['BATCH_CMD'] + ' ' + env['CXX']
414    env['AS']     = env['BATCH_CMD'] + ' ' + env['AS']
415    env['AR']     = env['BATCH_CMD'] + ' ' + env['AR']
416    env['RANLIB'] = env['BATCH_CMD'] + ' ' + env['RANLIB']
417
418if sys.platform == 'cygwin':
419    # cygwin has some header file issues...
420    env.Append(CCFLAGS=Split("-Wno-uninitialized"))
421env.Append(CPPPATH=[Dir('ext/dnet')])
422
423# Check for SWIG
424if not env.has_key('SWIG'):
425    print 'Error: SWIG utility not found.'
426    print '       Please install (see http://www.swig.org) and retry.'
427    Exit(1)
428
429# Check for appropriate SWIG version
430swig_version = read_command(('swig', '-version'), exception='').split()
431# First 3 words should be "SWIG Version x.y.z"
432if len(swig_version) < 3 or \
433        swig_version[0] != 'SWIG' or swig_version[1] != 'Version':
434    print 'Error determining SWIG version.'
435    Exit(1)
436
437min_swig_version = '1.3.28'
438if compare_versions(swig_version[2], min_swig_version) < 0:
439    print 'Error: SWIG version', min_swig_version, 'or newer required.'
440    print '       Installed version:', swig_version[2]
441    Exit(1)
442
443# Set up SWIG flags & scanner
444swig_flags=Split('-c++ -python -modern -templatereduce $_CPPINCFLAGS')
445env.Append(SWIGFLAGS=swig_flags)
446
447# filter out all existing swig scanners, they mess up the dependency
448# stuff for some reason
449scanners = []
450for scanner in env['SCANNERS']:
451    skeys = scanner.skeys
452    if skeys == '.i':
453        continue
454
455    if isinstance(skeys, (list, tuple)) and '.i' in skeys:
456        continue
457
458    scanners.append(scanner)
459
460# add the new swig scanner that we like better
461from SCons.Scanner import ClassicCPP as CPPScanner
462swig_inc_re = '^[ \t]*[%,#][ \t]*(?:include|import)[ \t]*(<|")([^>"]+)(>|")'
463scanners.append(CPPScanner("SwigScan", [ ".i" ], "CPPPATH", swig_inc_re))
464
465# replace the scanners list that has what we want
466env['SCANNERS'] = scanners
467
468# Add a custom Check function to the Configure context so that we can
469# figure out if the compiler adds leading underscores to global
470# variables.  This is needed for the autogenerated asm files that we
471# use for embedding the python code.
472def CheckLeading(context):
473    context.Message("Checking for leading underscore in global variables...")
474    # 1) Define a global variable called x from asm so the C compiler
475    #    won't change the symbol at all.
476    # 2) Declare that variable.
477    # 3) Use the variable
478    #
479    # If the compiler prepends an underscore, this will successfully
480    # link because the external symbol 'x' will be called '_x' which
481    # was defined by the asm statement.  If the compiler does not
482    # prepend an underscore, this will not successfully link because
483    # '_x' will have been defined by assembly, while the C portion of
484    # the code will be trying to use 'x'
485    ret = context.TryLink('''
486        asm(".globl _x; _x: .byte 0");
487        extern int x;
488        int main() { return x; }
489        ''', extension=".c")
490    context.env.Append(LEADING_UNDERSCORE=ret)
491    context.Result(ret)
492    return ret
493
494# Platform-specific configuration.  Note again that we assume that all
495# builds under a given build root run on the same host platform.
496conf = Configure(env,
497                 conf_dir = joinpath(build_root, '.scons_config'),
498                 log_file = joinpath(build_root, 'scons_config.log'),
499                 custom_tests = { 'CheckLeading' : CheckLeading })
500
501# Check for leading underscores.  Don't really need to worry either
502# way so don't need to check the return code.
503conf.CheckLeading()
504
505# Check if we should compile a 64 bit binary on Mac OS X/Darwin
506try:
507    import platform
508    uname = platform.uname()
509    if uname[0] == 'Darwin' and compare_versions(uname[2], '9.0.0') >= 0:
510        if int(read_command('sysctl -n hw.cpu64bit_capable')[0]):
511            env.Append(CCFLAGS='-arch x86_64')
512            env.Append(CFLAGS='-arch x86_64')
513            env.Append(LINKFLAGS='-arch x86_64')
514            env.Append(ASFLAGS='-arch x86_64')
515except:
516    pass
517
518# Recent versions of scons substitute a "Null" object for Configure()
519# when configuration isn't necessary, e.g., if the "--help" option is
520# present.  Unfortuantely this Null object always returns false,
521# breaking all our configuration checks.  We replace it with our own
522# more optimistic null object that returns True instead.
523if not conf:
524    def NullCheck(*args, **kwargs):
525        return True
526
527    class NullConf:
528        def __init__(self, env):
529            self.env = env
530        def Finish(self):
531            return self.env
532        def __getattr__(self, mname):
533            return NullCheck
534
535    conf = NullConf(env)
536
537# Find Python include and library directories for embedding the
538# interpreter.  For consistency, we will use the same Python
539# installation used to run scons (and thus this script).  If you want
540# to link in an alternate version, see above for instructions on how
541# to invoke scons with a different copy of the Python interpreter.
542from distutils import sysconfig
543
544py_getvar = sysconfig.get_config_var
545
546py_version = 'python' + py_getvar('VERSION')
547
548py_general_include = sysconfig.get_python_inc()
549py_platform_include = sysconfig.get_python_inc(plat_specific=True)
550py_includes = [ py_general_include ]
551if py_platform_include != py_general_include:
552    py_includes.append(py_platform_include)
553
554py_lib_path = []
555# add the prefix/lib/pythonX.Y/config dir, but only if there is no
556# shared library in prefix/lib/.
557if not py_getvar('Py_ENABLE_SHARED'):
558    py_lib_path.append('-L' + py_getvar('LIBPL'))
559
560py_libs = []
561for lib in py_getvar('LIBS').split() + py_getvar('SYSLIBS').split():
562    if lib not in py_libs:
563        py_libs.append(lib)
564py_libs.append('-l' + py_version)
565
566env.Append(CPPPATH=py_includes)
567env.Append(LIBPATH=py_lib_path)
568#env.Append(LIBS=py_libs)
569
570# verify that this stuff works
571if not conf.CheckHeader('Python.h', '<>'):
572    print "Error: can't find Python.h header in", py_includes
573    Exit(1)
574
575for lib in py_libs:
576    assert lib.startswith('-l')
577    lib = lib[2:]
578    if not conf.CheckLib(lib):
579        print "Error: can't find library %s required by python" % lib
580        Exit(1)
581
582# On Solaris you need to use libsocket for socket ops
583if not conf.CheckLibWithHeader(None, 'sys/socket.h', 'C++', 'accept(0,0,0);'):
584   if not conf.CheckLibWithHeader('socket', 'sys/socket.h', 'C++', 'accept(0,0,0);'):
585       print "Can't find library with socket calls (e.g. accept())"
586       Exit(1)
587
588# Check for zlib.  If the check passes, libz will be automatically
589# added to the LIBS environment variable.
590if not conf.CheckLibWithHeader('z', 'zlib.h', 'C++','zlibVersion();'):
591    print 'Error: did not find needed zlib compression library '\
592          'and/or zlib.h header file.'
593    print '       Please install zlib and try again.'
594    Exit(1)
595
596# Check for <fenv.h> (C99 FP environment control)
597have_fenv = conf.CheckHeader('fenv.h', '<>')
598if not have_fenv:
599    print "Warning: Header file <fenv.h> not found."
600    print "         This host has no IEEE FP rounding mode control."
601
602######################################################################
603#
604# Check for mysql.
605#
606mysql_config = WhereIs('mysql_config')
607have_mysql = bool(mysql_config)
608
609# Check MySQL version.
610if have_mysql:
611    mysql_version = read_command(mysql_config + ' --version')
612    min_mysql_version = '4.1'
613    if compare_versions(mysql_version, min_mysql_version) < 0:
614        print 'Warning: MySQL', min_mysql_version, 'or newer required.'
615        print '         Version', mysql_version, 'detected.'
616        have_mysql = False
617
618# Set up mysql_config commands.
619if have_mysql:
620    mysql_config_include = mysql_config + ' --include'
621    if os.system(mysql_config_include + ' > /dev/null') != 0:
622        # older mysql_config versions don't support --include, use
623        # --cflags instead
624        mysql_config_include = mysql_config + ' --cflags | sed s/\\\'//g'
625    # This seems to work in all versions
626    mysql_config_libs = mysql_config + ' --libs'
627
628######################################################################
629#
630# Finish the configuration
631#
632env = conf.Finish()
633
634######################################################################
635#
636# Collect all non-global variables
637#
638
639Export('env')
640
641# Define the universe of supported ISAs
642all_isa_list = [ ]
643Export('all_isa_list')
644
645# Define the universe of supported CPU models
646all_cpu_list = [ ]
647default_cpus = [ ]
648Export('all_cpu_list', 'default_cpus')
649
650# Sticky variables get saved in the variables file so they persist from
651# one invocation to the next (unless overridden, in which case the new
652# value becomes sticky).
653sticky_vars = Variables(args=ARGUMENTS)
654Export('sticky_vars')
655
656# Non-sticky variables only apply to the current build.
657nonsticky_vars = Variables(args=ARGUMENTS)
658Export('nonsticky_vars')
659
660# Walk the tree and execute all SConsopts scripts that wil add to the
661# above variables
662for bdir in [ base_dir ] + extras_dir_list:
663    for root, dirs, files in os.walk(bdir):
664        if 'SConsopts' in files:
665            print "Reading", joinpath(root, 'SConsopts')
666            SConscript(joinpath(root, 'SConsopts'))
667
668all_isa_list.sort()
669all_cpu_list.sort()
670default_cpus.sort()
671
672sticky_vars.AddVariables(
673    EnumVariable('TARGET_ISA', 'Target ISA', 'alpha', all_isa_list),
674    BoolVariable('FULL_SYSTEM', 'Full-system support', False),
675    ListVariable('CPU_MODELS', 'CPU models', default_cpus, all_cpu_list),
676    BoolVariable('NO_FAST_ALLOC', 'Disable fast object allocator', False),
677    BoolVariable('FAST_ALLOC_DEBUG', 'Enable fast object allocator debugging',
678                 False),
679    BoolVariable('FAST_ALLOC_STATS', 'Enable fast object allocator statistics',
680                 False),
681    BoolVariable('EFENCE', 'Link with Electric Fence malloc debugger',
682                 False),
683    BoolVariable('SS_COMPATIBLE_FP',
684                 'Make floating-point results compatible with SimpleScalar',
685                 False),
686    BoolVariable('USE_SSE2',
687                 'Compile for SSE2 (-msse2) to get IEEE FP on x86 hosts',
688                 False),
689    BoolVariable('USE_MYSQL', 'Use MySQL for stats output', have_mysql),
690    BoolVariable('USE_FENV', 'Use <fenv.h> IEEE mode control', have_fenv),
691    BoolVariable('USE_CHECKER', 'Use checker for detailed CPU models', False),
692    BoolVariable('CP_ANNOTATE', 'Enable critical path annotation capability', False),
693    )
694
695nonsticky_vars.AddVariables(
696    BoolVariable('update_ref', 'Update test reference outputs', False)
697    )
698
699# These variables get exported to #defines in config/*.hh (see src/SConscript).
700env.ExportVariables = ['FULL_SYSTEM', 'ALPHA_TLASER', 'USE_FENV', \
701                       'USE_MYSQL', 'NO_FAST_ALLOC', 'FAST_ALLOC_DEBUG', \
702                       'FAST_ALLOC_STATS', 'SS_COMPATIBLE_FP', \
703                       'USE_CHECKER', 'TARGET_ISA', 'CP_ANNOTATE']
704
705###################################################
706#
707# Define a SCons builder for configuration flag headers.
708#
709###################################################
710
711# This function generates a config header file that #defines the
712# variable symbol to the current variable setting (0 or 1).  The source
713# operands are the name of the variable and a Value node containing the
714# value of the variable.
715def build_config_file(target, source, env):
716    (variable, value) = [s.get_contents() for s in source]
717    f = file(str(target[0]), 'w')
718    print >> f, '#define', variable, value
719    f.close()
720    return None
721
722# Generate the message to be printed when building the config file.
723def build_config_file_string(target, source, env):
724    (variable, value) = [s.get_contents() for s in source]
725    return "Defining %s as %s in %s." % (variable, value, target[0])
726
727# Combine the two functions into a scons Action object.
728config_action = Action(build_config_file, build_config_file_string)
729
730# The emitter munges the source & target node lists to reflect what
731# we're really doing.
732def config_emitter(target, source, env):
733    # extract variable name from Builder arg
734    variable = str(target[0])
735    # True target is config header file
736    target = joinpath('config', variable.lower() + '.hh')
737    val = env[variable]
738    if isinstance(val, bool):
739        # Force value to 0/1
740        val = int(val)
741    elif isinstance(val, str):
742        val = '"' + val + '"'
743
744    # Sources are variable name & value (packaged in SCons Value nodes)
745    return ([target], [Value(variable), Value(val)])
746
747config_builder = Builder(emitter = config_emitter, action = config_action)
748
749env.Append(BUILDERS = { 'ConfigFile' : config_builder })
750
751# libelf build is shared across all configs in the build root.
752env.SConscript('ext/libelf/SConscript',
753               variant_dir = joinpath(build_root, 'libelf'))
754
755# gzstream build is shared across all configs in the build root.
756env.SConscript('ext/gzstream/SConscript',
757               variant_dir = joinpath(build_root, 'gzstream'))
758
759###################################################
760#
761# This function is used to set up a directory with switching headers
762#
763###################################################
764
765env['ALL_ISA_LIST'] = all_isa_list
766def make_switching_dir(dname, switch_headers, env):
767    # Generate the header.  target[0] is the full path of the output
768    # header to generate.  'source' is a dummy variable, since we get the
769    # list of ISAs from env['ALL_ISA_LIST'].
770    def gen_switch_hdr(target, source, env):
771        fname = str(target[0])
772        bname = basename(fname)
773        f = open(fname, 'w')
774        f.write('#include "arch/isa_specific.hh"\n')
775        cond = '#if'
776        for isa in all_isa_list:
777            f.write('%s THE_ISA == %s_ISA\n#include "%s/%s/%s"\n'
778                    % (cond, isa.upper(), dname, isa, bname))
779            cond = '#elif'
780        f.write('#else\n#error "THE_ISA not set"\n#endif\n')
781        f.close()
782        return 0
783
784    # String to print when generating header
785    def gen_switch_hdr_string(target, source, env):
786        return "Generating switch header " + str(target[0])
787
788    # Build SCons Action object. 'varlist' specifies env vars that this
789    # action depends on; when env['ALL_ISA_LIST'] changes these actions
790    # should get re-executed.
791    switch_hdr_action = Action(gen_switch_hdr, gen_switch_hdr_string,
792                               varlist=['ALL_ISA_LIST'])
793
794    # Instantiate actions for each header
795    for hdr in switch_headers:
796        env.Command(hdr, [], switch_hdr_action)
797Export('make_switching_dir')
798
799###################################################
800#
801# Define build environments for selected configurations.
802#
803###################################################
804
805# rename base env
806base_env = env
807
808for variant_path in variant_paths:
809    print "Building in", variant_path
810
811    # Make a copy of the build-root environment to use for this config.
812    env = base_env.Clone()
813    env['BUILDDIR'] = variant_path
814
815    # variant_dir is the tail component of build path, and is used to
816    # determine the build parameters (e.g., 'ALPHA_SE')
817    (build_root, variant_dir) = splitpath(variant_path)
818
819    # Set env variables according to the build directory config.
820    sticky_vars.files = []
821    # Variables for $BUILD_ROOT/$VARIANT_DIR are stored in
822    # $BUILD_ROOT/variables/$VARIANT_DIR so you can nuke
823    # $BUILD_ROOT/$VARIANT_DIR without losing your variables settings.
824    current_vars_file = joinpath(build_root, 'variables', variant_dir)
825    if isfile(current_vars_file):
826        sticky_vars.files.append(current_vars_file)
827        print "Using saved variables file %s" % current_vars_file
828    else:
829        # Build dir-specific variables file doesn't exist.
830
831        # Make sure the directory is there so we can create it later
832        opt_dir = dirname(current_vars_file)
833        if not isdir(opt_dir):
834            mkdir(opt_dir)
835
836        # Get default build variables from source tree.  Variables are
837        # normally determined by name of $VARIANT_DIR, but can be
838        # overriden by 'default=' arg on command line.
839        default_vars_file = joinpath('build_opts',
840                                     ARGUMENTS.get('default', variant_dir))
841        if isfile(default_vars_file):
842            sticky_vars.files.append(default_vars_file)
843            print "Variables file %s not found,\n  using defaults in %s" \
844                  % (current_vars_file, default_vars_file)
845        else:
846            print "Error: cannot find variables file %s or %s" \
847                  % (current_vars_file, default_vars_file)
848            Exit(1)
849
850    # Apply current variable settings to env
851    sticky_vars.Update(env)
852    nonsticky_vars.Update(env)
853
854    help_text += "\nSticky variables for %s:\n" % variant_dir \
855                 + sticky_vars.GenerateHelpText(env) \
856                 + "\nNon-sticky variables for %s:\n" % variant_dir \
857                 + nonsticky_vars.GenerateHelpText(env)
858
859    # Process variable settings.
860
861    if not have_fenv and env['USE_FENV']:
862        print "Warning: <fenv.h> not available; " \
863              "forcing USE_FENV to False in", variant_dir + "."
864        env['USE_FENV'] = False
865
866    if not env['USE_FENV']:
867        print "Warning: No IEEE FP rounding mode control in", variant_dir + "."
868        print "         FP results may deviate slightly from other platforms."
869
870    if env['EFENCE']:
871        env.Append(LIBS=['efence'])
872
873    if env['USE_MYSQL']:
874        if not have_mysql:
875            print "Warning: MySQL not available; " \
876                  "forcing USE_MYSQL to False in", variant_dir + "."
877            env['USE_MYSQL'] = False
878        else:
879            print "Compiling in", variant_dir, "with MySQL support."
880            env.ParseConfig(mysql_config_libs)
881            env.ParseConfig(mysql_config_include)
882
883    # Save sticky variable settings back to current variables file
884    sticky_vars.Save(current_vars_file, env)
885
886    if env['USE_SSE2']:
887        env.Append(CCFLAGS='-msse2')
888
889    # The src/SConscript file sets up the build rules in 'env' according
890    # to the configured variables.  It returns a list of environments,
891    # one for each variant build (debug, opt, etc.)
892    envList = SConscript('src/SConscript', variant_dir = variant_path,
893                         exports = 'env')
894
895    # Set up the regression tests for each build.
896    for e in envList:
897        SConscript('tests/SConscript',
898                   variant_dir = joinpath(variant_path, 'tests', e.Label),
899                   exports = { 'env' : e }, duplicate = False)
900
901Help(help_text)
902