SConstruct revision 6814
12929Sktlim@umich.edu# -*- mode:python -*-
22929Sktlim@umich.edu
32932Sktlim@umich.edu# Copyright (c) 2009 The Hewlett-Packard Development Company
42929Sktlim@umich.edu# Copyright (c) 2004-2005 The Regents of The University of Michigan
52929Sktlim@umich.edu# All rights reserved.
62929Sktlim@umich.edu#
72929Sktlim@umich.edu# Redistribution and use in source and binary forms, with or without
82929Sktlim@umich.edu# modification, are permitted provided that the following conditions are
92929Sktlim@umich.edu# met: redistributions of source code must retain the above copyright
102929Sktlim@umich.edu# notice, this list of conditions and the following disclaimer;
112929Sktlim@umich.edu# redistributions in binary form must reproduce the above copyright
122929Sktlim@umich.edu# notice, this list of conditions and the following disclaimer in the
132929Sktlim@umich.edu# documentation and/or other materials provided with the distribution;
142929Sktlim@umich.edu# neither the name of the copyright holders nor the names of its
152929Sktlim@umich.edu# contributors may be used to endorse or promote products derived from
162929Sktlim@umich.edu# this software without specific prior written permission.
172929Sktlim@umich.edu#
182929Sktlim@umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
192929Sktlim@umich.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
202929Sktlim@umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
212929Sktlim@umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
222929Sktlim@umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
232929Sktlim@umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
242929Sktlim@umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
252929Sktlim@umich.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
262929Sktlim@umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
272929Sktlim@umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
282932Sktlim@umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
292932Sktlim@umich.edu#
302932Sktlim@umich.edu# Authors: Steve Reinhardt
312929Sktlim@umich.edu#          Nathan Binkert
326007Ssteve.reinhardt@amd.com
337735SAli.Saidi@ARM.com###################################################
342929Sktlim@umich.edu#
352929Sktlim@umich.edu# SCons top-level build description (SConstruct) file.
362929Sktlim@umich.edu#
372929Sktlim@umich.edu# While in this directory ('m5'), just type 'scons' to build the default
382929Sktlim@umich.edu# configuration (see below), or type 'scons build/<CONFIG>/<binary>'
392929Sktlim@umich.edu# to build some other configuration (e.g., 'build/ALPHA_FS/m5.opt' for
402929Sktlim@umich.edu# the optimized full-system version).
412929Sktlim@umich.edu#
422929Sktlim@umich.edu# You can build M5 in a different directory as long as there is a
432929Sktlim@umich.edu# 'build/<CONFIG>' somewhere along the target path.  The build system
442929Sktlim@umich.edu# expects that all configs under the same build directory are being
452929Sktlim@umich.edu# built for the same host system.
462929Sktlim@umich.edu#
476007Ssteve.reinhardt@amd.com# Examples:
486007Ssteve.reinhardt@amd.com#
496007Ssteve.reinhardt@amd.com#   The following two commands are equivalent.  The '-u' option tells
506007Ssteve.reinhardt@amd.com#   scons to search up the directory tree for this SConstruct file.
516007Ssteve.reinhardt@amd.com#   % cd <path-to-src>/m5 ; scons build/ALPHA_FS/m5.debug
526007Ssteve.reinhardt@amd.com#   % cd <path-to-src>/m5/build/ALPHA_FS; scons -u m5.debug
536007Ssteve.reinhardt@amd.com#
546007Ssteve.reinhardt@amd.com#   The following two commands are equivalent and demonstrate building
556007Ssteve.reinhardt@amd.com#   in a directory outside of the source tree.  The '-C' option tells
566007Ssteve.reinhardt@amd.com#   scons to chdir to the specified directory to find this SConstruct
576007Ssteve.reinhardt@amd.com#   file.
586007Ssteve.reinhardt@amd.com#   % cd <path-to-src>/m5 ; scons /local/foo/build/ALPHA_FS/m5.debug
596007Ssteve.reinhardt@amd.com#   % cd /local/foo/build/ALPHA_FS; scons -C <path-to-src>/m5 m5.debug
606007Ssteve.reinhardt@amd.com#
616007Ssteve.reinhardt@amd.com# You can use 'scons -H' to print scons options.  If you're in this
626007Ssteve.reinhardt@amd.com# 'm5' directory (or use -u or -C to tell scons where to find this
636007Ssteve.reinhardt@amd.com# file), you can use 'scons -h' to print all the M5-specific build
646007Ssteve.reinhardt@amd.com# options as well.
656007Ssteve.reinhardt@amd.com#
666007Ssteve.reinhardt@amd.com###################################################
676007Ssteve.reinhardt@amd.com
686007Ssteve.reinhardt@amd.com# Check for recent-enough Python and SCons versions.
696007Ssteve.reinhardt@amd.comtry:
706007Ssteve.reinhardt@amd.com    # Really old versions of scons only take two options for the
716007Ssteve.reinhardt@amd.com    # function, so check once without the revision and once with the
726007Ssteve.reinhardt@amd.com    # revision, the first instance will fail for stuff other than
736007Ssteve.reinhardt@amd.com    # 0.98, and the second will fail for 0.98.0
746007Ssteve.reinhardt@amd.com    EnsureSConsVersion(0, 98)
756007Ssteve.reinhardt@amd.com    EnsureSConsVersion(0, 98, 1)
762929Sktlim@umich.eduexcept SystemExit, e:
772929Sktlim@umich.edu    print """
782929Sktlim@umich.eduFor more details, see:
796007Ssteve.reinhardt@amd.com    http://m5sim.org/wiki/index.php/Compiling_M5
806007Ssteve.reinhardt@amd.com"""
816007Ssteve.reinhardt@amd.com    raise
826007Ssteve.reinhardt@amd.com
836007Ssteve.reinhardt@amd.com# We ensure the python version early because we have stuff that
846007Ssteve.reinhardt@amd.com# requires python 2.4
852929Sktlim@umich.edutry:
862929Sktlim@umich.edu    EnsurePythonVersion(2, 4)
872929Sktlim@umich.eduexcept SystemExit, e:
882929Sktlim@umich.edu    print """
892929Sktlim@umich.eduYou can use a non-default installation of the Python interpreter by
906011Ssteve.reinhardt@amd.comeither (1) rearranging your PATH so that scons finds the non-default
916007Ssteve.reinhardt@amd.com'python' first or (2) explicitly invoking an alternative interpreter
926007Ssteve.reinhardt@amd.comon the scons script.
936007Ssteve.reinhardt@amd.com
946007Ssteve.reinhardt@amd.comFor more details, see:
956007Ssteve.reinhardt@amd.com    http://m5sim.org/wiki/index.php/Using_a_non-default_Python_installation
966007Ssteve.reinhardt@amd.com"""
976007Ssteve.reinhardt@amd.com    raise
986007Ssteve.reinhardt@amd.com
996007Ssteve.reinhardt@amd.com# Global Python includes
1006007Ssteve.reinhardt@amd.comimport os
1016007Ssteve.reinhardt@amd.comimport re
1026007Ssteve.reinhardt@amd.comimport subprocess
1036007Ssteve.reinhardt@amd.comimport sys
1046007Ssteve.reinhardt@amd.com
1057735SAli.Saidi@ARM.comfrom os import mkdir, environ
1066011Ssteve.reinhardt@amd.comfrom os.path import abspath, basename, dirname, expanduser, normpath
1076007Ssteve.reinhardt@amd.comfrom os.path import exists,  isdir, isfile
1086007Ssteve.reinhardt@amd.comfrom os.path import join as joinpath, split as splitpath
1096007Ssteve.reinhardt@amd.com
1106007Ssteve.reinhardt@amd.com# SCons includes
1117735SAli.Saidi@ARM.comimport SCons
1127735SAli.Saidi@ARM.comimport SCons.Node
1137735SAli.Saidi@ARM.com
1147735SAli.Saidi@ARM.comextra_python_paths = [
1157735SAli.Saidi@ARM.com    Dir('src/python').srcnode().abspath, # M5 includes
1167735SAli.Saidi@ARM.com    Dir('ext/ply').srcnode().abspath, # ply is used by several files
1177735SAli.Saidi@ARM.com    ]
1187735SAli.Saidi@ARM.com    
1197735SAli.Saidi@ARM.comsys.path[1:1] = extra_python_paths
1207735SAli.Saidi@ARM.com
1217735SAli.Saidi@ARM.comfrom m5.util import compareVersions, readCommand
1227735SAli.Saidi@ARM.com
1237735SAli.Saidi@ARM.com########################################################################
1247735SAli.Saidi@ARM.com#
1256007Ssteve.reinhardt@amd.com# Set up the main build environment.
1267685Ssteve.reinhardt@amd.com#
1276007Ssteve.reinhardt@amd.com########################################################################
1286011Ssteve.reinhardt@amd.comuse_vars = set([ 'AS', 'AR', 'CC', 'CXX', 'HOME', 'LD_LIBRARY_PATH', 'PATH',
1296007Ssteve.reinhardt@amd.com                 'PYTHONPATH', 'RANLIB' ])
1306007Ssteve.reinhardt@amd.com
1316007Ssteve.reinhardt@amd.comuse_env = {}
1326007Ssteve.reinhardt@amd.comfor key,val in os.environ.iteritems():
1336007Ssteve.reinhardt@amd.com    if key in use_vars or key.startswith("M5"):
1346007Ssteve.reinhardt@amd.com        use_env[key] = val
1356011Ssteve.reinhardt@amd.com
1366007Ssteve.reinhardt@amd.commain = Environment(ENV=use_env)
1376007Ssteve.reinhardt@amd.commain.root = Dir(".")         # The current directory (where this file lives).
1386007Ssteve.reinhardt@amd.commain.srcdir = Dir("src")     # The source directory
1396007Ssteve.reinhardt@amd.com
1406007Ssteve.reinhardt@amd.com# add useful python code PYTHONPATH so it can be used by subprocesses
1416008Ssteve.reinhardt@amd.com# as well
1426007Ssteve.reinhardt@amd.commain.AppendENVPath('PYTHONPATH', extra_python_paths)
1436008Ssteve.reinhardt@amd.com
1446008Ssteve.reinhardt@amd.com########################################################################
1456008Ssteve.reinhardt@amd.com#
1466008Ssteve.reinhardt@amd.com# Mercurial Stuff.
1476008Ssteve.reinhardt@amd.com#
1486008Ssteve.reinhardt@amd.com# If the M5 directory is a mercurial repository, we should do some
1496008Ssteve.reinhardt@amd.com# extra things.
1506007Ssteve.reinhardt@amd.com#
1516007Ssteve.reinhardt@amd.com########################################################################
1526007Ssteve.reinhardt@amd.com
1536007Ssteve.reinhardt@amd.comhgdir = main.root.Dir(".hg")
1546007Ssteve.reinhardt@amd.com
1552929Sktlim@umich.edumercurial_style_message = """
1562929Sktlim@umich.eduYou're missing the M5 style hook.
1572929Sktlim@umich.eduPlease install the hook so we can ensure that all code fits a common style.
1582929Sktlim@umich.edu
1596007Ssteve.reinhardt@amd.comAll you'd need to do is add the following lines to your repository .hg/hgrc
1606007Ssteve.reinhardt@amd.comor your personal .hgrc
1612929Sktlim@umich.edu----------------
1622929Sktlim@umich.edu
1632929Sktlim@umich.edu[extensions]
1642929Sktlim@umich.edustyle = %s/util/style.py
1656007Ssteve.reinhardt@amd.com
1666007Ssteve.reinhardt@amd.com[hooks]
1672929Sktlim@umich.edupretxncommit.style = python:style.check_whitespace
1682929Sktlim@umich.edu""" % (main.root)
1696007Ssteve.reinhardt@amd.com
1702929Sktlim@umich.edumercurial_bin_not_found = """
1712929Sktlim@umich.eduMercurial binary cannot be found, unfortunately this means that we
1722929Sktlim@umich.educannot easily determine the version of M5 that you are running and
1732929Sktlim@umich.eduthis makes error messages more difficult to collect.  Please consider
1742929Sktlim@umich.eduinstalling mercurial if you choose to post an error message
1752929Sktlim@umich.edu"""
1762929Sktlim@umich.edu
1774937Sstever@gmail.commercurial_lib_not_found = """
1784937Sstever@gmail.comMercurial libraries cannot be found, ignoring style hook
1794937Sstever@gmail.comIf you are actually a M5 developer, please fix this and
1804937Sstever@gmail.comrun the style hook. It is important.
1818120Sgblack@eecs.umich.edu"""
1824937Sstever@gmail.com
1834937Sstever@gmail.comhg_info = "Unknown"
1844937Sstever@gmail.comif hgdir.exists():
1854937Sstever@gmail.com    # 1) Grab repository revision if we know it.
1865773Snate@binkert.org    cmd = "hg id -n -i -t -b"
1874937Sstever@gmail.com    try:
1884937Sstever@gmail.com        hg_info = readCommand(cmd, cwd=main.root.abspath).strip()
1894937Sstever@gmail.com    except OSError:
1902929Sktlim@umich.edu        print mercurial_bin_not_found
1912929Sktlim@umich.edu
1922929Sktlim@umich.edu    # 2) Ensure that the style hook is in place.
1935773Snate@binkert.org    try:
1942929Sktlim@umich.edu        ui = None
1952929Sktlim@umich.edu        if ARGUMENTS.get('IGNORE_STYLE') != 'True':
1962929Sktlim@umich.edu            from mercurial import ui
1972929Sktlim@umich.edu            ui = ui.ui()
1982929Sktlim@umich.edu    except ImportError:
1992929Sktlim@umich.edu        print mercurial_lib_not_found
2004937Sstever@gmail.com
2014937Sstever@gmail.com    if ui is not None:
2024937Sstever@gmail.com        ui.readconfig(hgdir.File('hgrc').abspath)
2034937Sstever@gmail.com        style_hook = ui.config('hooks', 'pretxncommit.style', None)
2044937Sstever@gmail.com
2054937Sstever@gmail.com        if not style_hook:
2064937Sstever@gmail.com            print mercurial_style_message
2074937Sstever@gmail.com            sys.exit(1)
2084937Sstever@gmail.comelse:
2094937Sstever@gmail.com    print ".hg directory not found"
2104937Sstever@gmail.com
2114937Sstever@gmail.commain['HG_INFO'] = hg_info
2124937Sstever@gmail.com
2134937Sstever@gmail.com###################################################
2144937Sstever@gmail.com#
2152929Sktlim@umich.edu# Figure out which configurations to set up based on the path(s) of
2162929Sktlim@umich.edu# the target(s).
2172929Sktlim@umich.edu#
2182929Sktlim@umich.edu###################################################
2192929Sktlim@umich.edu
2202929Sktlim@umich.edu# Find default configuration & binary.
2212929Sktlim@umich.eduDefault(environ.get('M5_DEFAULT_BINARY', 'build/ALPHA_SE/m5.debug'))
2226011Ssteve.reinhardt@amd.com
2232929Sktlim@umich.edu# helper function: find last occurrence of element in list
2242929Sktlim@umich.edudef rfind(l, elt, offs = -1):
2252929Sktlim@umich.edu    for i in range(len(l)+offs, 0, -1):
2262929Sktlim@umich.edu        if l[i] == elt:
2272929Sktlim@umich.edu            return i
2282929Sktlim@umich.edu    raise ValueError, "element not found"
2292929Sktlim@umich.edu
2302929Sktlim@umich.edu# Each target must have 'build' in the interior of the path; the
2312997Sstever@eecs.umich.edu# directory below this will determine the build parameters.  For
2322997Sstever@eecs.umich.edu# example, for target 'foo/bar/build/ALPHA_SE/arch/alpha/blah.do' we
2332929Sktlim@umich.edu# recognize that ALPHA_SE specifies the configuration because it
2342997Sstever@eecs.umich.edu# follow 'build' in the bulid path.
2352997Sstever@eecs.umich.edu
2362929Sktlim@umich.edu# Generate absolute paths to targets so we can see where the build dir is
2372997Sstever@eecs.umich.eduif COMMAND_LINE_TARGETS:
2382997Sstever@eecs.umich.edu    # Ask SCons which directory it was invoked from
2392997Sstever@eecs.umich.edu    launch_dir = GetLaunchDir()
2402929Sktlim@umich.edu    # Make targets relative to invocation directory
2412997Sstever@eecs.umich.edu    abs_targets = [ normpath(joinpath(launch_dir, str(x))) for x in \
2422997Sstever@eecs.umich.edu                    COMMAND_LINE_TARGETS]
2432997Sstever@eecs.umich.eduelse:
2442997Sstever@eecs.umich.edu    # Default targets are relative to root of tree
2455773Snate@binkert.org    abs_targets = [ normpath(joinpath(main.root.abspath, str(x))) for x in \
2465773Snate@binkert.org                    DEFAULT_TARGETS]
2472997Sstever@eecs.umich.edu
2482997Sstever@eecs.umich.edu
2496007Ssteve.reinhardt@amd.com# Generate a list of the unique build roots and configs that the
2506007Ssteve.reinhardt@amd.com# collected targets reference.
2512997Sstever@eecs.umich.eduvariant_paths = []
2522929Sktlim@umich.edubuild_root = None
2532997Sstever@eecs.umich.edufor t in abs_targets:
2548120Sgblack@eecs.umich.edu    path_dirs = t.split('/')
2552997Sstever@eecs.umich.edu    try:
2562997Sstever@eecs.umich.edu        build_top = rfind(path_dirs, 'build', -2)
2572997Sstever@eecs.umich.edu    except:
2582997Sstever@eecs.umich.edu        print "Error: no non-leaf 'build' dir found on target path", t
2592997Sstever@eecs.umich.edu        Exit(1)
2602929Sktlim@umich.edu    this_build_root = joinpath('/',*path_dirs[:build_top+1])
2612997Sstever@eecs.umich.edu    if not build_root:
2622929Sktlim@umich.edu        build_root = this_build_root
2632929Sktlim@umich.edu    else:
2643005Sstever@eecs.umich.edu        if this_build_root != build_root:
2653005Sstever@eecs.umich.edu            print "Error: build targets not under same build root\n"\
2663005Sstever@eecs.umich.edu                  "  %s\n  %s" % (build_root, this_build_root)
2673005Sstever@eecs.umich.edu            Exit(1)
2686025Snate@binkert.org    variant_path = joinpath('/',*path_dirs[:build_top+2])
2696025Snate@binkert.org    if variant_path not in variant_paths:
2706025Snate@binkert.org        variant_paths.append(variant_path)
2716025Snate@binkert.org
2726025Snate@binkert.org# Make sure build_root exists (might not if this is the first build there)
2736025Snate@binkert.orgif not isdir(build_root):
2744130Ssaidi@eecs.umich.edu    mkdir(build_root)
2754130Ssaidi@eecs.umich.edu
2764130Ssaidi@eecs.umich.eduExport('main')
2777735SAli.Saidi@ARM.com
2787735SAli.Saidi@ARM.commain.SConsignFile(joinpath(build_root, "sconsign"))
2797735SAli.Saidi@ARM.com
2807926Sgblack@eecs.umich.edu# Default duplicate option is to use hard links, but this messes up
2817926Sgblack@eecs.umich.edu# when you use emacs to edit a file in the target dir, as emacs moves
2827926Sgblack@eecs.umich.edu# file to file~ then copies to file, breaking the link.  Symbolic
2833691Shsul@eecs.umich.edu# (soft) links work better.
2843005Sstever@eecs.umich.edumain.SetOption('duplicate', 'soft-copy')
2855721Shsul@eecs.umich.edu
2866194Sksewell@umich.edu#
2876928SBrad.Beckmann@amd.com# Set up global sticky variables... these are common to an entire build
2883005Sstever@eecs.umich.edu# tree (not specific to a particular build like ALPHA_SE)
2896168Snate@binkert.org#
2906928SBrad.Beckmann@amd.com
2916928SBrad.Beckmann@amd.com# Variable validators & converters for global sticky variables
2926928SBrad.Beckmann@amd.comdef PathListMakeAbsolute(val):
2936928SBrad.Beckmann@amd.com    if not val:
2946928SBrad.Beckmann@amd.com        return val
2956928SBrad.Beckmann@amd.com    f = lambda p: abspath(expanduser(p))
2966928SBrad.Beckmann@amd.com    return ':'.join(map(f, val.split(':')))
2976928SBrad.Beckmann@amd.com
2986928SBrad.Beckmann@amd.comdef PathListAllExist(key, val, env):
2996928SBrad.Beckmann@amd.com    if not val:
3006928SBrad.Beckmann@amd.com        return
3016928SBrad.Beckmann@amd.com    paths = val.split(':')
3026928SBrad.Beckmann@amd.com    for path in paths:
3036928SBrad.Beckmann@amd.com        if not isdir(path):
3046166Ssteve.reinhardt@amd.com            raise SCons.Errors.UserError("Path does not exist: '%s'" % path)
3052929Sktlim@umich.edu
3062929Sktlim@umich.eduglobal_sticky_vars_file = joinpath(build_root, 'variables.global')
3073005Sstever@eecs.umich.edu
3082997Sstever@eecs.umich.eduglobal_sticky_vars = Variables(global_sticky_vars_file, args=ARGUMENTS)
3092997Sstever@eecs.umich.edu
3106293Ssteve.reinhardt@amd.comglobal_sticky_vars.AddVariables(
3116293Ssteve.reinhardt@amd.com    ('CC', 'C compiler', environ.get('CC', main['CC'])),
3122929Sktlim@umich.edu    ('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    BoolVariable('RUBY', 'Build with Ruby', False),
318    )
319
320# base help text
321help_text = '''
322Usage: scons [scons options] [build options] [target(s)]
323
324Global sticky options:
325'''
326
327# Update main environment with values from ARGUMENTS & global_sticky_vars_file
328global_sticky_vars.Update(main)
329
330help_text += global_sticky_vars.GenerateHelpText(main)
331
332# Save sticky variable settings back to current variables file
333global_sticky_vars.Save(global_sticky_vars_file, main)
334
335# Parse EXTRAS variable to build list of all directories where we're
336# look for sources etc.  This list is exported as base_dir_list.
337base_dir = main.srcdir.abspath
338if main['EXTRAS']:
339    extras_dir_list = main['EXTRAS'].split(':')
340else:
341    extras_dir_list = []
342
343Export('base_dir')
344Export('extras_dir_list')
345
346# the ext directory should be on the #includes path
347main.Append(CPPPATH=[Dir('ext')])
348
349CXX_version = readCommand([main['CXX'],'--version'], exception=False)
350CXX_V = readCommand([main['CXX'],'-V'], exception=False)
351
352main['GCC'] = CXX_version and CXX_version.find('g++') >= 0
353main['SUNCC'] = CXX_V and CXX_V.find('Sun C++') >= 0
354main['ICC'] = CXX_V and CXX_V.find('Intel') >= 0
355if main['GCC'] + main['SUNCC'] + main['ICC'] > 1:
356    print 'Error: How can we have two at the same time?'
357    Exit(1)
358
359# Set up default C++ compiler flags
360if main['GCC']:
361    main.Append(CCFLAGS='-pipe')
362    main.Append(CCFLAGS='-fno-strict-aliasing')
363    main.Append(CCFLAGS=['-Wall', '-Wno-sign-compare', '-Wundef'])
364    main.Append(CXXFLAGS='-Wno-deprecated')
365elif main['ICC']:
366    pass #Fix me... add warning flags once we clean up icc warnings
367elif main['SUNCC']:
368    main.Append(CCFLAGS='-Qoption ccfe')
369    main.Append(CCFLAGS='-features=gcc')
370    main.Append(CCFLAGS='-features=extensions')
371    main.Append(CCFLAGS='-library=stlport4')
372    main.Append(CCFLAGS='-xar')
373    #main.Append(CCFLAGS='-instances=semiexplicit')
374else:
375    print 'Error: Don\'t know what compiler options to use for your compiler.'
376    print '       Please fix SConstruct and src/SConscript and try again.'
377    Exit(1)
378
379# Set up common yacc/bison flags (needed for Ruby)
380main['YACCFLAGS'] = '-d'
381main['YACCHXXFILESUFFIX'] = '.hh'
382
383# Do this after we save setting back, or else we'll tack on an
384# extra 'qdo' every time we run scons.
385if main['BATCH']:
386    main['CC']     = main['BATCH_CMD'] + ' ' + main['CC']
387    main['CXX']    = main['BATCH_CMD'] + ' ' + main['CXX']
388    main['AS']     = main['BATCH_CMD'] + ' ' + main['AS']
389    main['AR']     = main['BATCH_CMD'] + ' ' + main['AR']
390    main['RANLIB'] = main['BATCH_CMD'] + ' ' + main['RANLIB']
391
392if sys.platform == 'cygwin':
393    # cygwin has some header file issues...
394    main.Append(CCFLAGS="-Wno-uninitialized")
395
396# Check for SWIG
397if not main.has_key('SWIG'):
398    print 'Error: SWIG utility not found.'
399    print '       Please install (see http://www.swig.org) and retry.'
400    Exit(1)
401
402# Check for appropriate SWIG version
403swig_version = readCommand(('swig', '-version'), exception='').split()
404# First 3 words should be "SWIG Version x.y.z"
405if len(swig_version) < 3 or \
406        swig_version[0] != 'SWIG' or swig_version[1] != 'Version':
407    print 'Error determining SWIG version.'
408    Exit(1)
409
410min_swig_version = '1.3.28'
411if compareVersions(swig_version[2], min_swig_version) < 0:
412    print 'Error: SWIG version', min_swig_version, 'or newer required.'
413    print '       Installed version:', swig_version[2]
414    Exit(1)
415
416# Set up SWIG flags & scanner
417swig_flags=Split('-c++ -python -modern -templatereduce $_CPPINCFLAGS')
418main.Append(SWIGFLAGS=swig_flags)
419
420# filter out all existing swig scanners, they mess up the dependency
421# stuff for some reason
422scanners = []
423for scanner in main['SCANNERS']:
424    skeys = scanner.skeys
425    if skeys == '.i':
426        continue
427
428    if isinstance(skeys, (list, tuple)) and '.i' in skeys:
429        continue
430
431    scanners.append(scanner)
432
433# add the new swig scanner that we like better
434from SCons.Scanner import ClassicCPP as CPPScanner
435swig_inc_re = '^[ \t]*[%,#][ \t]*(?:include|import)[ \t]*(<|")([^>"]+)(>|")'
436scanners.append(CPPScanner("SwigScan", [ ".i" ], "CPPPATH", swig_inc_re))
437
438# replace the scanners list that has what we want
439main['SCANNERS'] = scanners
440
441# Add a custom Check function to the Configure context so that we can
442# figure out if the compiler adds leading underscores to global
443# variables.  This is needed for the autogenerated asm files that we
444# use for embedding the python code.
445def CheckLeading(context):
446    context.Message("Checking for leading underscore in global variables...")
447    # 1) Define a global variable called x from asm so the C compiler
448    #    won't change the symbol at all.
449    # 2) Declare that variable.
450    # 3) Use the variable
451    #
452    # If the compiler prepends an underscore, this will successfully
453    # link because the external symbol 'x' will be called '_x' which
454    # was defined by the asm statement.  If the compiler does not
455    # prepend an underscore, this will not successfully link because
456    # '_x' will have been defined by assembly, while the C portion of
457    # the code will be trying to use 'x'
458    ret = context.TryLink('''
459        asm(".globl _x; _x: .byte 0");
460        extern int x;
461        int main() { return x; }
462        ''', extension=".c")
463    context.env.Append(LEADING_UNDERSCORE=ret)
464    context.Result(ret)
465    return ret
466
467# Platform-specific configuration.  Note again that we assume that all
468# builds under a given build root run on the same host platform.
469conf = Configure(main,
470                 conf_dir = joinpath(build_root, '.scons_config'),
471                 log_file = joinpath(build_root, 'scons_config.log'),
472                 custom_tests = { 'CheckLeading' : CheckLeading })
473
474# Check for leading underscores.  Don't really need to worry either
475# way so don't need to check the return code.
476conf.CheckLeading()
477
478# Check if we should compile a 64 bit binary on Mac OS X/Darwin
479try:
480    import platform
481    uname = platform.uname()
482    if uname[0] == 'Darwin' and compareVersions(uname[2], '9.0.0') >= 0:
483        if int(readCommand('sysctl -n hw.cpu64bit_capable')[0]):
484            main.Append(CCFLAGS='-arch x86_64')
485            main.Append(CFLAGS='-arch x86_64')
486            main.Append(LINKFLAGS='-arch x86_64')
487            main.Append(ASFLAGS='-arch x86_64')
488except:
489    pass
490
491# Recent versions of scons substitute a "Null" object for Configure()
492# when configuration isn't necessary, e.g., if the "--help" option is
493# present.  Unfortuantely this Null object always returns false,
494# breaking all our configuration checks.  We replace it with our own
495# more optimistic null object that returns True instead.
496if not conf:
497    def NullCheck(*args, **kwargs):
498        return True
499
500    class NullConf:
501        def __init__(self, env):
502            self.env = env
503        def Finish(self):
504            return self.env
505        def __getattr__(self, mname):
506            return NullCheck
507
508    conf = NullConf(main)
509
510# Find Python include and library directories for embedding the
511# interpreter.  For consistency, we will use the same Python
512# installation used to run scons (and thus this script).  If you want
513# to link in an alternate version, see above for instructions on how
514# to invoke scons with a different copy of the Python interpreter.
515from distutils import sysconfig
516
517py_getvar = sysconfig.get_config_var
518
519py_version = 'python' + py_getvar('VERSION')
520
521py_general_include = sysconfig.get_python_inc()
522py_platform_include = sysconfig.get_python_inc(plat_specific=True)
523py_includes = [ py_general_include ]
524if py_platform_include != py_general_include:
525    py_includes.append(py_platform_include)
526
527py_lib_path = [ py_getvar('LIBDIR') ]
528# add the prefix/lib/pythonX.Y/config dir, but only if there is no
529# shared library in prefix/lib/.
530if not py_getvar('Py_ENABLE_SHARED'):
531    py_lib_path.append(py_getvar('LIBPL'))
532
533py_libs = []
534for lib in py_getvar('LIBS').split() + py_getvar('SYSLIBS').split():
535    assert lib.startswith('-l')
536    lib = lib[2:]   
537    if lib not in py_libs:
538        py_libs.append(lib)
539py_libs.append(py_version)
540
541main.Append(CPPPATH=py_includes)
542main.Append(LIBPATH=py_lib_path)
543
544# verify that this stuff works
545if not conf.CheckHeader('Python.h', '<>'):
546    print "Error: can't find Python.h header in", py_includes
547    Exit(1)
548
549for lib in py_libs:
550    if not conf.CheckLib(lib):
551        print "Error: can't find library %s required by python" % lib
552        Exit(1)
553
554# On Solaris you need to use libsocket for socket ops
555if not conf.CheckLibWithHeader(None, 'sys/socket.h', 'C++', 'accept(0,0,0);'):
556   if not conf.CheckLibWithHeader('socket', 'sys/socket.h', 'C++', 'accept(0,0,0);'):
557       print "Can't find library with socket calls (e.g. accept())"
558       Exit(1)
559
560# Check for zlib.  If the check passes, libz will be automatically
561# added to the LIBS environment variable.
562if not conf.CheckLibWithHeader('z', 'zlib.h', 'C++','zlibVersion();'):
563    print 'Error: did not find needed zlib compression library '\
564          'and/or zlib.h header file.'
565    print '       Please install zlib and try again.'
566    Exit(1)
567
568# Check for <fenv.h> (C99 FP environment control)
569have_fenv = conf.CheckHeader('fenv.h', '<>')
570if not have_fenv:
571    print "Warning: Header file <fenv.h> not found."
572    print "         This host has no IEEE FP rounding mode control."
573
574######################################################################
575#
576# Check for mysql.
577#
578mysql_config = WhereIs('mysql_config')
579have_mysql = bool(mysql_config)
580
581# Check MySQL version.
582if have_mysql:
583    mysql_version = readCommand(mysql_config + ' --version')
584    min_mysql_version = '4.1'
585    if compareVersions(mysql_version, min_mysql_version) < 0:
586        print 'Warning: MySQL', min_mysql_version, 'or newer required.'
587        print '         Version', mysql_version, 'detected.'
588        have_mysql = False
589
590# Set up mysql_config commands.
591if have_mysql:
592    mysql_config_include = mysql_config + ' --include'
593    if os.system(mysql_config_include + ' > /dev/null') != 0:
594        # older mysql_config versions don't support --include, use
595        # --cflags instead
596        mysql_config_include = mysql_config + ' --cflags | sed s/\\\'//g'
597    # This seems to work in all versions
598    mysql_config_libs = mysql_config + ' --libs'
599
600######################################################################
601#
602# Finish the configuration
603#
604main = conf.Finish()
605
606######################################################################
607#
608# Collect all non-global variables
609#
610
611# Define the universe of supported ISAs
612all_isa_list = [ ]
613Export('all_isa_list')
614
615# Define the universe of supported CPU models
616all_cpu_list = [ ]
617default_cpus = [ ]
618Export('all_cpu_list', 'default_cpus')
619
620# Sticky variables get saved in the variables file so they persist from
621# one invocation to the next (unless overridden, in which case the new
622# value becomes sticky).
623sticky_vars = Variables(args=ARGUMENTS)
624Export('sticky_vars')
625
626# Sticky variables that should be exported
627export_vars = []
628Export('export_vars')
629
630# Non-sticky variables only apply to the current build.
631nonsticky_vars = Variables(args=ARGUMENTS)
632Export('nonsticky_vars')
633
634# Walk the tree and execute all SConsopts scripts that wil add to the
635# above variables
636for bdir in [ base_dir ] + extras_dir_list:
637    for root, dirs, files in os.walk(bdir):
638        if 'SConsopts' in files:
639            print "Reading", joinpath(root, 'SConsopts')
640            SConscript(joinpath(root, 'SConsopts'))
641
642all_isa_list.sort()
643all_cpu_list.sort()
644default_cpus.sort()
645
646sticky_vars.AddVariables(
647    EnumVariable('TARGET_ISA', 'Target ISA', 'alpha', all_isa_list),
648    BoolVariable('FULL_SYSTEM', 'Full-system support', False),
649    ListVariable('CPU_MODELS', 'CPU models', default_cpus, all_cpu_list),
650    BoolVariable('NO_FAST_ALLOC', 'Disable fast object allocator', False),
651    BoolVariable('FAST_ALLOC_DEBUG', 'Enable fast object allocator debugging',
652                 False),
653    BoolVariable('FAST_ALLOC_STATS', 'Enable fast object allocator statistics',
654                 False),
655    BoolVariable('EFENCE', 'Link with Electric Fence malloc debugger',
656                 False),
657    BoolVariable('SS_COMPATIBLE_FP',
658                 'Make floating-point results compatible with SimpleScalar',
659                 False),
660    BoolVariable('USE_SSE2',
661                 'Compile for SSE2 (-msse2) to get IEEE FP on x86 hosts',
662                 False),
663    BoolVariable('USE_MYSQL', 'Use MySQL for stats output', have_mysql),
664    BoolVariable('USE_FENV', 'Use <fenv.h> IEEE mode control', have_fenv),
665    BoolVariable('USE_CHECKER', 'Use checker for detailed CPU models', False),
666    BoolVariable('CP_ANNOTATE', 'Enable critical path annotation capability', False),
667    )
668
669nonsticky_vars.AddVariables(
670    BoolVariable('update_ref', 'Update test reference outputs', False)
671    )
672
673# These variables get exported to #defines in config/*.hh (see src/SConscript).
674export_vars += ['FULL_SYSTEM', 'USE_FENV', 'USE_MYSQL',
675                'NO_FAST_ALLOC', 'FAST_ALLOC_DEBUG', 'FAST_ALLOC_STATS',
676                'SS_COMPATIBLE_FP', 'USE_CHECKER', 'TARGET_ISA', 'CP_ANNOTATE']
677
678###################################################
679#
680# Define a SCons builder for configuration flag headers.
681#
682###################################################
683
684# This function generates a config header file that #defines the
685# variable symbol to the current variable setting (0 or 1).  The source
686# operands are the name of the variable and a Value node containing the
687# value of the variable.
688def build_config_file(target, source, env):
689    (variable, value) = [s.get_contents() for s in source]
690    f = file(str(target[0]), 'w')
691    print >> f, '#define', variable, value
692    f.close()
693    return None
694
695# Generate the message to be printed when building the config file.
696def build_config_file_string(target, source, env):
697    (variable, value) = [s.get_contents() for s in source]
698    return "Defining %s as %s in %s." % (variable, value, target[0])
699
700# Combine the two functions into a scons Action object.
701config_action = Action(build_config_file, build_config_file_string)
702
703# The emitter munges the source & target node lists to reflect what
704# we're really doing.
705def config_emitter(target, source, env):
706    # extract variable name from Builder arg
707    variable = str(target[0])
708    # True target is config header file
709    target = joinpath('config', variable.lower() + '.hh')
710    val = env[variable]
711    if isinstance(val, bool):
712        # Force value to 0/1
713        val = int(val)
714    elif isinstance(val, str):
715        val = '"' + val + '"'
716
717    # Sources are variable name & value (packaged in SCons Value nodes)
718    return ([target], [Value(variable), Value(val)])
719
720config_builder = Builder(emitter = config_emitter, action = config_action)
721
722main.Append(BUILDERS = { 'ConfigFile' : config_builder })
723
724# libelf build is shared across all configs in the build root.
725main.SConscript('ext/libelf/SConscript',
726                variant_dir = joinpath(build_root, 'libelf'))
727
728# gzstream build is shared across all configs in the build root.
729main.SConscript('ext/gzstream/SConscript',
730                variant_dir = joinpath(build_root, 'gzstream'))
731
732###################################################
733#
734# This function is used to set up a directory with switching headers
735#
736###################################################
737
738main['ALL_ISA_LIST'] = all_isa_list
739def make_switching_dir(dname, switch_headers, env):
740    # Generate the header.  target[0] is the full path of the output
741    # header to generate.  'source' is a dummy variable, since we get the
742    # list of ISAs from env['ALL_ISA_LIST'].
743    def gen_switch_hdr(target, source, env):
744        fname = str(target[0])
745        f = open(fname, 'w')
746        isa = env['TARGET_ISA'].lower()
747        print >>f, '#include "%s/%s/%s"' % (dname, isa, basename(fname))
748        f.close()
749
750    # String to print when generating header
751    def gen_switch_hdr_string(target, source, env):
752        return "Generating switch header " + str(target[0])
753
754    # Build SCons Action object. 'varlist' specifies env vars that this
755    # action depends on; when env['ALL_ISA_LIST'] changes these actions
756    # should get re-executed.
757    switch_hdr_action = Action(gen_switch_hdr, gen_switch_hdr_string,
758                               varlist=['ALL_ISA_LIST'])
759
760    # Instantiate actions for each header
761    for hdr in switch_headers:
762        env.Command(hdr, [], switch_hdr_action)
763Export('make_switching_dir')
764
765###################################################
766#
767# Define build environments for selected configurations.
768#
769###################################################
770
771for variant_path in variant_paths:
772    print "Building in", variant_path
773
774    # Make a copy of the build-root environment to use for this config.
775    env = main.Clone()
776    env['BUILDDIR'] = variant_path
777
778    # variant_dir is the tail component of build path, and is used to
779    # determine the build parameters (e.g., 'ALPHA_SE')
780    (build_root, variant_dir) = splitpath(variant_path)
781
782    # Set env variables according to the build directory config.
783    sticky_vars.files = []
784    # Variables for $BUILD_ROOT/$VARIANT_DIR are stored in
785    # $BUILD_ROOT/variables/$VARIANT_DIR so you can nuke
786    # $BUILD_ROOT/$VARIANT_DIR without losing your variables settings.
787    current_vars_file = joinpath(build_root, 'variables', variant_dir)
788    if isfile(current_vars_file):
789        sticky_vars.files.append(current_vars_file)
790        print "Using saved variables file %s" % current_vars_file
791    else:
792        # Build dir-specific variables file doesn't exist.
793
794        # Make sure the directory is there so we can create it later
795        opt_dir = dirname(current_vars_file)
796        if not isdir(opt_dir):
797            mkdir(opt_dir)
798
799        # Get default build variables from source tree.  Variables are
800        # normally determined by name of $VARIANT_DIR, but can be
801        # overriden by 'default=' arg on command line.
802        default_vars_file = joinpath('build_opts',
803                                     ARGUMENTS.get('default', variant_dir))
804        if isfile(default_vars_file):
805            sticky_vars.files.append(default_vars_file)
806            print "Variables file %s not found,\n  using defaults in %s" \
807                  % (current_vars_file, default_vars_file)
808        else:
809            print "Error: cannot find variables file %s or %s" \
810                  % (current_vars_file, default_vars_file)
811            Exit(1)
812
813    # Apply current variable settings to env
814    sticky_vars.Update(env)
815    nonsticky_vars.Update(env)
816
817    help_text += "\nSticky variables for %s:\n" % variant_dir \
818                 + sticky_vars.GenerateHelpText(env) \
819                 + "\nNon-sticky variables for %s:\n" % variant_dir \
820                 + nonsticky_vars.GenerateHelpText(env)
821
822    # Process variable settings.
823
824    if not have_fenv and env['USE_FENV']:
825        print "Warning: <fenv.h> not available; " \
826              "forcing USE_FENV to False in", variant_dir + "."
827        env['USE_FENV'] = False
828
829    if not env['USE_FENV']:
830        print "Warning: No IEEE FP rounding mode control in", variant_dir + "."
831        print "         FP results may deviate slightly from other platforms."
832
833    if env['EFENCE']:
834        env.Append(LIBS=['efence'])
835
836    if env['USE_MYSQL']:
837        if not have_mysql:
838            print "Warning: MySQL not available; " \
839                  "forcing USE_MYSQL to False in", variant_dir + "."
840            env['USE_MYSQL'] = False
841        else:
842            print "Compiling in", variant_dir, "with MySQL support."
843            env.ParseConfig(mysql_config_libs)
844            env.ParseConfig(mysql_config_include)
845
846    # Save sticky variable settings back to current variables file
847    sticky_vars.Save(current_vars_file, env)
848
849    if env['USE_SSE2']:
850        env.Append(CCFLAGS='-msse2')
851
852    # The src/SConscript file sets up the build rules in 'env' according
853    # to the configured variables.  It returns a list of environments,
854    # one for each variant build (debug, opt, etc.)
855    envList = SConscript('src/SConscript', variant_dir = variant_path,
856                         exports = 'env')
857
858    # Set up the regression tests for each build.
859    for e in envList:
860        SConscript('tests/SConscript',
861                   variant_dir = joinpath(variant_path, 'tests', e.Label),
862                   exports = { 'env' : e }, duplicate = False)
863
864Help(help_text)
865