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