SConstruct revision 9419
11689SN/A# -*- mode:python -*-
22329SN/A
31689SN/A# Copyright (c) 2011 Advanced Micro Devices, Inc.
41689SN/A# Copyright (c) 2009 The Hewlett-Packard Development Company
51689SN/A# Copyright (c) 2004-2005 The Regents of The University of Michigan
61689SN/A# All rights reserved.
71689SN/A#
81689SN/A# Redistribution and use in source and binary forms, with or without
91689SN/A# modification, are permitted provided that the following conditions are
101689SN/A# met: redistributions of source code must retain the above copyright
111689SN/A# notice, this list of conditions and the following disclaimer;
121689SN/A# redistributions in binary form must reproduce the above copyright
131689SN/A# notice, this list of conditions and the following disclaimer in the
141689SN/A# documentation and/or other materials provided with the distribution;
151689SN/A# neither the name of the copyright holders nor the names of its
161689SN/A# contributors may be used to endorse or promote products derived from
171689SN/A# this software without specific prior written permission.
181689SN/A#
191689SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
201689SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
211689SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
221689SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
231689SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
241689SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
251689SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
261689SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
272665Ssaidi@eecs.umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
282665Ssaidi@eecs.umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
292935Sksewell@umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
301689SN/A#
311689SN/A# Authors: Steve Reinhardt
321060SN/A#          Nathan Binkert
331060SN/A
341858SN/A###################################################
351717SN/A#
361060SN/A# SCons top-level build description (SConstruct) file.
371061SN/A#
382292SN/A# While in this directory ('gem5'), just type 'scons' to build the default
392292SN/A# configuration (see below), or type 'scons build/<CONFIG>/<binary>'
402292SN/A# to build some other configuration (e.g., 'build/ALPHA/gem5.opt' for
412292SN/A# the optimized full-system version).
422292SN/A#
432292SN/A# You can build gem5 in a different directory as long as there is a
442292SN/A# 'build/<CONFIG>' somewhere along the target path.  The build system
451060SN/A# expects that all configs under the same build directory are being
462292SN/A# built for the same host system.
472292SN/A#
482292SN/A# Examples:
492292SN/A#
502292SN/A#   The following two commands are equivalent.  The '-u' option tells
512292SN/A#   scons to search up the directory tree for this SConstruct file.
522292SN/A#   % cd <path-to-src>/gem5 ; scons build/ALPHA/gem5.debug
532292SN/A#   % cd <path-to-src>/gem5/build/ALPHA; scons -u gem5.debug
542292SN/A#
552292SN/A#   The following two commands are equivalent and demonstrate building
562292SN/A#   in a directory outside of the source tree.  The '-C' option tells
572301SN/A#   scons to chdir to the specified directory to find this SConstruct
582292SN/A#   file.
592292SN/A#   % cd <path-to-src>/gem5 ; scons /local/foo/build/ALPHA/gem5.debug
602292SN/A#   % cd /local/foo/build/ALPHA; scons -C <path-to-src>/gem5 gem5.debug
612292SN/A#
622292SN/A# You can use 'scons -H' to print scons options.  If you're in this
632292SN/A# 'gem5' directory (or use -u or -C to tell scons where to find this
642292SN/A# file), you can use 'scons -h' to print all the gem5-specific build
652292SN/A# options as well.
662292SN/A#
672292SN/A###################################################
682292SN/A
692292SN/A# Check for recent-enough Python and SCons versions.
702292SN/Atry:
712292SN/A    # Really old versions of scons only take two options for the
722292SN/A    # function, so check once without the revision and once with the
732292SN/A    # revision, the first instance will fail for stuff other than
742292SN/A    # 0.98, and the second will fail for 0.98.0
751060SN/A    EnsureSConsVersion(0, 98)
761060SN/A    EnsureSConsVersion(0, 98, 1)
771061SN/Aexcept SystemExit, e:
781060SN/A    print """
792292SN/AFor more details, see:
801062SN/A    http://gem5.org/Dependencies
811062SN/A"""
822301SN/A    raise
831062SN/A
841062SN/A# We ensure the python version early because we have stuff that
851062SN/A# requires python 2.4
862301SN/Atry:
871062SN/A    EnsurePythonVersion(2, 4)
881062SN/Aexcept SystemExit, e:
891062SN/A    print """
902301SN/AYou can use a non-default installation of the Python interpreter by
911062SN/Aeither (1) rearranging your PATH so that scons finds the non-default
921062SN/A'python' first or (2) explicitly invoking an alternative interpreter
932301SN/Aon the scons script.
942301SN/A
952301SN/AFor more details, see:
962301SN/A    http://gem5.org/wiki/index.php/Using_a_non-default_Python_installation
972292SN/A"""
982301SN/A    raise
992292SN/A
1002292SN/A# Global Python includes
1011062SN/Aimport os
1022301SN/Aimport re
1031062SN/Aimport subprocess
1041062SN/Aimport sys
1051062SN/A
1062301SN/Afrom os import mkdir, environ
1071062SN/Afrom os.path import abspath, basename, dirname, expanduser, normpath
1081062SN/Afrom os.path import exists,  isdir, isfile
1091062SN/Afrom os.path import join as joinpath, split as splitpath
1102301SN/A
1111062SN/A# SCons includes
1121062SN/Aimport SCons
1131062SN/Aimport SCons.Node
1142301SN/A
1152292SN/Aextra_python_paths = [
1161062SN/A    Dir('src/python').srcnode().abspath, # gem5 includes
1171062SN/A    Dir('ext/ply').srcnode().abspath, # ply is used by several files
1182301SN/A    ]
1192292SN/A
1201062SN/Asys.path[1:1] = extra_python_paths
1212292SN/A
1222301SN/Afrom m5.util import compareVersions, readCommand
1232292SN/Afrom m5.util.terminal import get_termcap
1242292SN/A
1251062SN/Ahelp_texts = {
1262301SN/A    "options" : "",
1271062SN/A    "global_vars" : "",
1281062SN/A    "local_vars" : ""
1291062SN/A}
1302301SN/A
1311062SN/AExport("help_texts")
1321062SN/A
1331062SN/A
1342301SN/A# There's a bug in scons in that (1) by default, the help texts from
1351062SN/A# AddOption() are supposed to be displayed when you type 'scons -h'
1361062SN/A# and (2) you can override the help displayed by 'scons -h' using the
1371062SN/A# Help() function, but these two features are incompatible: once
1382301SN/A# you've overridden the help text using Help(), there's no way to get
1391062SN/A# at the help texts from AddOptions.  See:
1401062SN/A#     http://scons.tigris.org/issues/show_bug.cgi?id=2356
1411062SN/A#     http://scons.tigris.org/issues/show_bug.cgi?id=2611
1422301SN/A# This hack lets us extract the help text from AddOptions and
1431062SN/A# re-inject it via Help().  Ideally someday this bug will be fixed and
1441062SN/A# we can just use AddOption directly.
1452301SN/Adef AddLocalOption(*args, **kwargs):
1462301SN/A    col_width = 30
1472301SN/A
1482301SN/A    help = "  " + ", ".join(args)
1492301SN/A    if "help" in kwargs:
1502301SN/A        length = len(help)
1512301SN/A        if length >= col_width:
1522301SN/A            help += "\n" + " " * col_width
1532301SN/A        else:
1542301SN/A            help += " " * (col_width - length)
1552307SN/A        help += kwargs["help"]
1562307SN/A    help_texts["options"] += help + "\n"
1572307SN/A
1582307SN/A    AddOption(*args, **kwargs)
1592307SN/A
1601062SN/AAddLocalOption('--colors', dest='use_colors', action='store_true',
1611062SN/A               help="Add color to abbreviated scons output")
1621062SN/AAddLocalOption('--no-colors', dest='use_colors', action='store_false',
1631062SN/A               help="Don't add color to abbreviated scons output")
1642733Sktlim@umich.eduAddLocalOption('--default', dest='default', type='string', action='store',
1651060SN/A               help='Override which build_opts file to use for defaults')
1662292SN/AAddLocalOption('--ignore-style', dest='ignore_style', action='store_true',
1671060SN/A               help='Disable style checking hooks')
1681060SN/AAddLocalOption('--no-lto', dest='no_lto', action='store_true',
1691060SN/A               help='Disable Link-Time Optimization for fast')
1701061SN/AAddLocalOption('--update-ref', dest='update_ref', action='store_true',
1711060SN/A               help='Update test reference outputs')
1722292SN/AAddLocalOption('--verbose', dest='verbose', action='store_true',
1731060SN/A               help='Print full tool command lines')
1742292SN/A
1751060SN/Atermcap = get_termcap(GetOption('use_colors'))
1761060SN/A
1771060SN/A########################################################################
1781060SN/A#
1791060SN/A# Set up the main build environment.
1801060SN/A#
1811060SN/A########################################################################
1821060SN/Ause_vars = set([ 'AS', 'AR', 'CC', 'CXX', 'HOME', 'LD_LIBRARY_PATH',
1831060SN/A                 'LIBRARY_PATH', 'PATH', 'PKG_CONFIG_PATH', 'PYTHONPATH',
1841060SN/A                 'RANLIB', 'SWIG' ])
1851060SN/A
1861060SN/Ause_prefixes = [
1871061SN/A    "M5",           # M5 configuration (e.g., path to kernels)
1881060SN/A    "DISTCC_",      # distcc (distributed compiler wrapper) configuration
1892292SN/A    "CCACHE_",      # ccache (caching compiler wrapper) configuration
1901060SN/A    "CCC_",         # clang static analyzer configuration
1912292SN/A    ]
1921060SN/A
1931060SN/Ause_env = {}
1941060SN/Afor key,val in os.environ.iteritems():
1951060SN/A    if key in use_vars or \
1961060SN/A            any([key.startswith(prefix) for prefix in use_prefixes]):
1971060SN/A        use_env[key] = val
1981061SN/A
1991060SN/Amain = Environment(ENV=use_env)
2002292SN/Amain.Decider('MD5-timestamp')
2011060SN/Amain.root = Dir(".")         # The current directory (where this file lives).
2022292SN/Amain.srcdir = Dir("src")     # The source directory
2031060SN/A
2041060SN/Amain_dict_keys = main.Dictionary().keys()
2051060SN/A
2061060SN/A# Check that we have a C/C++ compiler
2071060SN/Aif not ('CC' in main_dict_keys and 'CXX' in main_dict_keys):
2081060SN/A    print "No C++ compiler installed (package g++ on Ubuntu and RedHat)"
2091061SN/A    Exit(1)
2101060SN/A
2112292SN/A# Check that swig is present
2121060SN/Aif not 'SWIG' in main_dict_keys:
2132329SN/A    print "swig is not installed (package swig on Ubuntu and RedHat)"
2142292SN/A    Exit(1)
2152292SN/A
2162292SN/A# add useful python code PYTHONPATH so it can be used by subprocesses
2172292SN/A# as well
2182292SN/Amain.AppendENVPath('PYTHONPATH', extra_python_paths)
2192292SN/A
2201060SN/A########################################################################
2211060SN/A#
2222292SN/A# Mercurial Stuff.
2232292SN/A#
2242980Sgblack@eecs.umich.edu# If the gem5 directory is a mercurial repository, we should do some
2252292SN/A# extra things.
2262292SN/A#
2272292SN/A########################################################################
2282292SN/A
2292292SN/Ahgdir = main.root.Dir(".hg")
2302292SN/A
2311061SN/Amercurial_style_message = """
2321060SN/AYou're missing the gem5 style hook, which automatically checks your code
2332292SN/Aagainst the gem5 style rules on hg commit and qrefresh commands.  This
2341060SN/Ascript will now install the hook in your .hg/hgrc file.
2352292SN/APress enter to continue, or ctrl-c to abort: """
2361060SN/A
2372292SN/Amercurial_style_hook = """
2382292SN/A# The following lines were automatically added by gem5/SConstruct
2391060SN/A# to provide the gem5 style-checking hooks
2401060SN/A[extensions]
2411060SN/Astyle = %s/util/style.py
2421061SN/A
2431060SN/A[hooks]
2442292SN/Apretxncommit.style = python:style.check_style
2451060SN/Apre-qrefresh.style = python:style.check_style
2462292SN/A# End of SConstruct additions
2472292SN/A
2482292SN/A""" % (main.root.abspath)
2491060SN/A
2502292SN/Amercurial_lib_not_found = """
2512292SN/AMercurial libraries cannot be found, ignoring style hook.  If
2522292SN/Ayou are a gem5 developer, please fix this and run the style
2532292SN/Ahook. It is important.
2542292SN/A"""
2552292SN/A
2561060SN/A# Check for style hook and prompt for installation if it's not there.
2571060SN/A# Skip this if --ignore-style was specified, there's no .hg dir to
2581061SN/A# install a hook in, or there's no interactive terminal to prompt.
2592863Sktlim@umich.eduif not GetOption('ignore_style') and hgdir.exists() and sys.stdin.isatty():
2602843Sktlim@umich.edu    style_hook = True
2611060SN/A    try:
2622348SN/A        from mercurial import ui
2632843Sktlim@umich.edu        ui = ui.ui()
2642863Sktlim@umich.edu        ui.readconfig(hgdir.File('hgrc').abspath)
2652316SN/A        style_hook = ui.config('hooks', 'pretxncommit.style', None) and \
2661060SN/A                     ui.config('hooks', 'pre-qrefresh.style', None)
2672316SN/A    except ImportError:
2682316SN/A        print mercurial_lib_not_found
2692843Sktlim@umich.edu
2702316SN/A    if not style_hook:
2712348SN/A        print mercurial_style_message,
2722307SN/A        # continue unless user does ctrl-c/ctrl-d etc.
2732980Sgblack@eecs.umich.edu        try:
2742980Sgblack@eecs.umich.edu            raw_input()
2752307SN/A        except:
2762307SN/A            print "Input exception, exiting scons.\n"
2772307SN/A            sys.exit(1)
2782307SN/A        hgrc_path = '%s/.hg/hgrc' % main.root.abspath
2792307SN/A        print "Adding style hook to", hgrc_path, "\n"
2802307SN/A        try:
2812307SN/A            hgrc = open(hgrc_path, 'a')
2822307SN/A            hgrc.write(mercurial_style_hook)
2832307SN/A            hgrc.close()
2842307SN/A        except:
2852307SN/A            print "Error updating", hgrc_path
2862307SN/A            sys.exit(1)
2872307SN/A
2882307SN/A
2892307SN/A###################################################
2902307SN/A#
2912307SN/A# Figure out which configurations to set up based on the path(s) of
2922307SN/A# the target(s).
2931060SN/A#
2941060SN/A###################################################
2951060SN/A
2961061SN/A# Find default configuration & binary.
2971060SN/ADefault(environ.get('M5_DEFAULT_BINARY', 'build/ALPHA/gem5.debug'))
2982307SN/A
2991060SN/A# helper function: find last occurrence of element in list
3002307SN/Adef rfind(l, elt, offs = -1):
3012307SN/A    for i in range(len(l)+offs, 0, -1):
3021060SN/A        if l[i] == elt:
3032329SN/A            return i
3042307SN/A    raise ValueError, "element not found"
3052307SN/A
3061060SN/A# Take a list of paths (or SCons Nodes) and return a list with all
3072307SN/A# paths made absolute and ~-expanded.  Paths will be interpreted
3082307SN/A# relative to the launch directory unless a different root is provided
3092307SN/Adef makePathListAbsolute(path_list, root=GetLaunchDir()):
3102307SN/A    return [abspath(joinpath(root, expanduser(str(p))))
3112307SN/A            for p in path_list]
3122307SN/A
3132307SN/A# Each target must have 'build' in the interior of the path; the
3142307SN/A# directory below this will determine the build parameters.  For
3152307SN/A# example, for target 'foo/bar/build/ALPHA_SE/arch/alpha/blah.do' we
3162307SN/A# recognize that ALPHA_SE specifies the configuration because it
3172307SN/A# follow 'build' in the build path.
3182307SN/A
3192307SN/A# The funky assignment to "[:]" is needed to replace the list contents
3202307SN/A# in place rather than reassign the symbol to a new list, which
3212935Sksewell@umich.edu# doesn't work (obviously!).
3221858SN/ABUILD_TARGETS[:] = makePathListAbsolute(BUILD_TARGETS)
3232292SN/A
3241858SN/A# Generate a list of the unique build roots and configs that the
3252292SN/A# collected targets reference.
3262292SN/Avariant_paths = []
3272292SN/Abuild_root = None
3282292SN/Afor t in BUILD_TARGETS:
3292292SN/A    path_dirs = t.split('/')
3302301SN/A    try:
3312698Sktlim@umich.edu        build_top = rfind(path_dirs, 'build', -2)
3322292SN/A    except:
3332698Sktlim@umich.edu        print "Error: no non-leaf 'build' dir found on target path", t
3342301SN/A        Exit(1)
3352292SN/A    this_build_root = joinpath('/',*path_dirs[:build_top+1])
3362292SN/A    if not build_root:
3372292SN/A        build_root = this_build_root
3382292SN/A    else:
3392292SN/A        if this_build_root != build_root:
3402329SN/A            print "Error: build targets not under same build root\n"\
3412292SN/A                  "  %s\n  %s" % (build_root, this_build_root)
3422292SN/A            Exit(1)
3432292SN/A    variant_path = joinpath('/',*path_dirs[:build_top+2])
3442935Sksewell@umich.edu    if variant_path not in variant_paths:
3452935Sksewell@umich.edu        variant_paths.append(variant_path)
3462731Sktlim@umich.edu
3472292SN/A# Make sure build_root exists (might not if this is the first build there)
3482292SN/Aif not isdir(build_root):
3492292SN/A    mkdir(build_root)
3502935Sksewell@umich.edumain['BUILDROOT'] = build_root
3512292SN/A
3522292SN/AExport('main')
3532935Sksewell@umich.edu
3542935Sksewell@umich.edumain.SConsignFile(joinpath(build_root, "sconsign"))
3552935Sksewell@umich.edu
3562935Sksewell@umich.edu# Default duplicate option is to use hard links, but this messes up
3572935Sksewell@umich.edu# when you use emacs to edit a file in the target dir, as emacs moves
3583093Sksewell@umich.edu# file to file~ then copies to file, breaking the link.  Symbolic
3592935Sksewell@umich.edu# (soft) links work better.
3602935Sksewell@umich.edumain.SetOption('duplicate', 'soft-copy')
3612935Sksewell@umich.edu
3622935Sksewell@umich.edu#
3632935Sksewell@umich.edu# Set up global sticky variables... these are common to an entire build
3642935Sksewell@umich.edu# tree (not specific to a particular build like ALPHA_SE)
3652935Sksewell@umich.edu#
3662935Sksewell@umich.edu
3672935Sksewell@umich.eduglobal_vars_file = joinpath(build_root, 'variables.global')
3682935Sksewell@umich.edu
3692935Sksewell@umich.eduglobal_vars = Variables(global_vars_file, args=ARGUMENTS)
3703093Sksewell@umich.edu
3713093Sksewell@umich.eduglobal_vars.AddVariables(
3722935Sksewell@umich.edu    ('CC', 'C compiler', environ.get('CC', main['CC'])),
3732292SN/A    ('CXX', 'C++ compiler', environ.get('CXX', main['CXX'])),
3742292SN/A    ('SWIG', 'SWIG tool', environ.get('SWIG', main['SWIG'])),
3752935Sksewell@umich.edu    ('PROTOC', 'protoc tool', environ.get('PROTOC', 'protoc')),
3762935Sksewell@umich.edu    ('BATCH', 'Use batch pool for build and tests', False),
3773093Sksewell@umich.edu    ('BATCH_CMD', 'Batch pool submission command name', 'qdo'),
3782935Sksewell@umich.edu    ('M5_BUILD_CACHE', 'Cache built objects in this directory', False),
3792935Sksewell@umich.edu    ('EXTRAS', 'Add extra directories to the compilation', '')
3802935Sksewell@umich.edu    )
3812935Sksewell@umich.edu
3822935Sksewell@umich.edu# Update main environment with values from ARGUMENTS & global_vars_file
3832935Sksewell@umich.eduglobal_vars.Update(main)
3842935Sksewell@umich.eduhelp_texts["global_vars"] += global_vars.GenerateHelpText(main)
3852935Sksewell@umich.edu
3862935Sksewell@umich.edu# Save sticky variable settings back to current variables file
3872935Sksewell@umich.eduglobal_vars.Save(global_vars_file, main)
3882935Sksewell@umich.edu
3893093Sksewell@umich.edu# Parse EXTRAS variable to build list of all directories where we're
3903093Sksewell@umich.edu# look for sources etc.  This list is exported as extras_dir_list.
3912935Sksewell@umich.edubase_dir = main.srcdir.abspath
3922935Sksewell@umich.eduif main['EXTRAS']:
3932292SN/A    extras_dir_list = makePathListAbsolute(main['EXTRAS'].split(':'))
3942292SN/Aelse:
3952292SN/A    extras_dir_list = []
3962292SN/A
3972292SN/AExport('base_dir')
3982292SN/AExport('extras_dir_list')
3992292SN/A
4002292SN/A# the ext directory should be on the #includes path
4012292SN/Amain.Append(CPPPATH=[Dir('ext')])
4022292SN/A
4032292SN/Adef strip_build_path(path, env):
4042292SN/A    path = str(path)
4052292SN/A    variant_base = env['BUILDROOT'] + os.path.sep
4062292SN/A    if path.startswith(variant_base):
4072292SN/A        path = path[len(variant_base):]
4082292SN/A    elif path.startswith('build/'):
4092980Sgblack@eecs.umich.edu        path = path[6:]
4102292SN/A    return path
4112292SN/A
4122292SN/A# Generate a string of the form:
4132292SN/A#   common/path/prefix/src1, src2 -> tgt1, tgt2
4142292SN/A# to print while building.
4152292SN/Aclass Transform(object):
4162292SN/A    # all specific color settings should be here and nowhere else
4172292SN/A    tool_color = termcap.Normal
4182292SN/A    pfx_color = termcap.Yellow
4192292SN/A    srcs_color = termcap.Yellow + termcap.Bold
4202292SN/A    arrow_color = termcap.Blue + termcap.Bold
4212292SN/A    tgts_color = termcap.Yellow + termcap.Bold
4222292SN/A
4232292SN/A    def __init__(self, tool, max_sources=99):
4242292SN/A        self.format = self.tool_color + (" [%8s] " % tool) \
4252292SN/A                      + self.pfx_color + "%s" \
4262292SN/A                      + self.srcs_color + "%s" \
4272292SN/A                      + self.arrow_color + " -> " \
4282292SN/A                      + self.tgts_color + "%s" \
4292292SN/A                      + termcap.Normal
4302292SN/A        self.max_sources = max_sources
4312292SN/A
4322292SN/A    def __call__(self, target, source, env, for_signature=None):
4332292SN/A        # truncate source list according to max_sources param
4342292SN/A        source = source[0:self.max_sources]
4352292SN/A        def strip(f):
4362292SN/A            return strip_build_path(str(f), env)
4372292SN/A        if len(source) > 0:
4382292SN/A            srcs = map(strip, source)
4392292SN/A        else:
4402292SN/A            srcs = ['']
4412292SN/A        tgts = map(strip, target)
4422292SN/A        # surprisingly, os.path.commonprefix is a dumb char-by-char string
4432292SN/A        # operation that has nothing to do with paths.
4442292SN/A        com_pfx = os.path.commonprefix(srcs + tgts)
4452292SN/A        com_pfx_len = len(com_pfx)
4462292SN/A        if com_pfx:
4472292SN/A            # do some cleanup and sanity checking on common prefix
4482292SN/A            if com_pfx[-1] == ".":
4492292SN/A                # prefix matches all but file extension: ok
4502292SN/A                # back up one to change 'foo.cc -> o' to 'foo.cc -> .o'
4512292SN/A                com_pfx = com_pfx[0:-1]
4522292SN/A            elif com_pfx[-1] == "/":
4532292SN/A                # common prefix is directory path: OK
4542292SN/A                pass
4552292SN/A            else:
4562292SN/A                src0_len = len(srcs[0])
4572292SN/A                tgt0_len = len(tgts[0])
4582292SN/A                if src0_len == com_pfx_len:
4592292SN/A                    # source is a substring of target, OK
4602292SN/A                    pass
4612292SN/A                elif tgt0_len == com_pfx_len:
4622292SN/A                    # target is a substring of source, need to back up to
4632292SN/A                    # avoid empty string on RHS of arrow
4642292SN/A                    sep_idx = com_pfx.rfind(".")
4652292SN/A                    if sep_idx != -1:
4662292SN/A                        com_pfx = com_pfx[0:sep_idx]
4672292SN/A                    else:
4682292SN/A                        com_pfx = ''
4692292SN/A                elif src0_len > com_pfx_len and srcs[0][com_pfx_len] == ".":
4702301SN/A                    # still splitting at file extension: ok
4712301SN/A                    pass
4722292SN/A                else:
4732292SN/A                    # probably a fluke; ignore it
4742292SN/A                    com_pfx = ''
4752292SN/A        # recalculate length in case com_pfx was modified
4762292SN/A        com_pfx_len = len(com_pfx)
4772292SN/A        def fmt(files):
4782292SN/A            f = map(lambda s: s[com_pfx_len:], files)
4792292SN/A            return ', '.join(f)
4802292SN/A        return self.format % (com_pfx, fmt(srcs), fmt(tgts))
4812292SN/A
4822292SN/AExport('Transform')
4832292SN/A
4842292SN/A# enable the regression script to use the termcap
4852292SN/Amain['TERMCAP'] = termcap
4862292SN/A
4872292SN/Aif GetOption('verbose'):
4882292SN/A    def MakeAction(action, string, *args, **kwargs):
4892292SN/A        return Action(action, *args, **kwargs)
4902292SN/Aelse:
4912292SN/A    MakeAction = Action
4921858SN/A    main['CCCOMSTR']        = Transform("CC")
4931858SN/A    main['CXXCOMSTR']       = Transform("CXX")
4941858SN/A    main['ASCOMSTR']        = Transform("AS")
4951858SN/A    main['SWIGCOMSTR']      = Transform("SWIG")
4961858SN/A    main['ARCOMSTR']        = Transform("AR", 0)
4972292SN/A    main['LINKCOMSTR']      = Transform("LINK", 0)
4981858SN/A    main['RANLIBCOMSTR']    = Transform("RANLIB", 0)
4992292SN/A    main['M4COMSTR']        = Transform("M4")
5002292SN/A    main['SHCCCOMSTR']      = Transform("SHCC")
5012292SN/A    main['SHCXXCOMSTR']     = Transform("SHCXX")
5022292SN/AExport('MakeAction')
5031858SN/A
5042292SN/A# Initialize the Link-Time Optimization (LTO) flags
5052292SN/Amain['LTO_CCFLAGS'] = []
5062292SN/Amain['LTO_LDFLAGS'] = []
5072292SN/A
5082292SN/ACXX_version = readCommand([main['CXX'],'--version'], exception=False)
5092292SN/ACXX_V = readCommand([main['CXX'],'-V'], exception=False)
5102292SN/A
5112292SN/Amain['GCC'] = CXX_version and CXX_version.find('g++') >= 0
5122292SN/Amain['CLANG'] = CXX_version and CXX_version.find('clang') >= 0
5132292SN/Aif main['GCC'] + main['CLANG'] > 1:
5142292SN/A    print 'Error: How can we have two at the same time?'
5152292SN/A    Exit(1)
5162292SN/A
5171858SN/A# Set up default C++ compiler flags
5182292SN/Aif main['GCC']:
5192292SN/A    main.Append(CCFLAGS=['-pipe'])
5202292SN/A    main.Append(CCFLAGS=['-fno-strict-aliasing'])
5212292SN/A    main.Append(CCFLAGS=['-Wall', '-Wno-sign-compare', '-Wundef'])
5222292SN/A    # Read the GCC version to check for versions with bugs
5232292SN/A    # Note CCVERSION doesn't work here because it is run with the CC
5242292SN/A    # before we override it from the command line
5252292SN/A    gcc_version = readCommand([main['CXX'], '-dumpversion'], exception=False)
5262292SN/A    main['GCC_VERSION'] = gcc_version
5272292SN/A    if not compareVersions(gcc_version, '4.4.1') or \
5282292SN/A       not compareVersions(gcc_version, '4.4.2'):
5292292SN/A        print 'Info: Tree vectorizer in GCC 4.4.1 & 4.4.2 is buggy, disabling.'
5302292SN/A        main.Append(CCFLAGS=['-fno-tree-vectorize'])
5312292SN/A    # c++0x support in gcc is useful already from 4.4, see
5322292SN/A    # http://gcc.gnu.org/projects/cxx0x.html for details
5332292SN/A    if compareVersions(gcc_version, '4.4') >= 0:
5342292SN/A        main.Append(CXXFLAGS=['-std=c++0x'])
5352292SN/A
5362292SN/A    # LTO support is only really working properly from 4.6 and beyond
5372292SN/A    if compareVersions(gcc_version, '4.6') >= 0:
5382292SN/A        # Add the appropriate Link-Time Optimization (LTO) flags
5392292SN/A        # unless LTO is explicitly turned off. Note that these flags
5402292SN/A        # are only used by the fast target.
5412292SN/A        if not GetOption('no_lto'):
5422292SN/A            # Pass the LTO flag when compiling to produce GIMPLE
5432292SN/A            # output, we merely create the flags here and only append
5442292SN/A            # them later/
5452292SN/A            main['LTO_CCFLAGS'] = ['-flto=%d' % GetOption('num_jobs')]
5462292SN/A
5472292SN/A            # Use the same amount of jobs for LTO as we are running
5482292SN/A            # scons with, we hardcode the use of the linker plugin
5492292SN/A            # which requires either gold or GNU ld >= 2.21
5502292SN/A            main['LTO_LDFLAGS'] = ['-flto=%d' % GetOption('num_jobs'),
5512292SN/A                                   '-fuse-linker-plugin']
5522292SN/A
5532292SN/Aelif main['CLANG']:
5542292SN/A    clang_version_re = re.compile(".* version (\d+\.\d+)")
5552292SN/A    clang_version_match = clang_version_re.match(CXX_version)
5562292SN/A    if (clang_version_match):
5572292SN/A        clang_version = clang_version_match.groups()[0]
5582292SN/A        if compareVersions(clang_version, "2.9") < 0:
5592292SN/A            print 'Error: clang version 2.9 or newer required.'
5602292SN/A            print '       Installed version:', clang_version
5612292SN/A            Exit(1)
5622292SN/A    else:
5632292SN/A        print 'Error: Unable to determine clang version.'
5642292SN/A        Exit(1)
5652292SN/A
5662292SN/A    main.Append(CCFLAGS=['-pipe'])
5672292SN/A    main.Append(CCFLAGS=['-fno-strict-aliasing'])
5682292SN/A    main.Append(CCFLAGS=['-Wall', '-Wno-sign-compare', '-Wundef'])
5692292SN/A    main.Append(CCFLAGS=['-Wno-tautological-compare'])
5702292SN/A    main.Append(CCFLAGS=['-Wno-self-assign'])
5712292SN/A    # Ruby makes frequent use of extraneous parantheses in the printing
5722292SN/A    # of if-statements
5732292SN/A    main.Append(CCFLAGS=['-Wno-parentheses'])
5742292SN/A
5752292SN/A    # clang 2.9 does not play well with c++0x as it ships with C++
5762292SN/A    # headers that produce errors, this was fixed in 3.0
5772292SN/A    if compareVersions(clang_version, "3") >= 0:
5782292SN/A        main.Append(CXXFLAGS=['-std=c++0x'])
5792292SN/Aelse:
5802292SN/A    print termcap.Yellow + termcap.Bold + 'Error' + termcap.Normal,
5812292SN/A    print "Don't know what compiler options to use for your compiler."
5822292SN/A    print termcap.Yellow + '       compiler:' + termcap.Normal, main['CXX']
5832292SN/A    print termcap.Yellow + '       version:' + termcap.Normal,
5842292SN/A    if not CXX_version:
5852292SN/A        print termcap.Yellow + termcap.Bold + "COMMAND NOT FOUND!" +\
5862292SN/A               termcap.Normal
5872292SN/A    else:
5882292SN/A        print CXX_version.replace('\n', '<nl>')
5892292SN/A    print "       If you're trying to use a compiler other than GCC"
5902292SN/A    print "       or clang, there appears to be something wrong with your"
5912292SN/A    print "       environment."
5922292SN/A    print "       "
5932292SN/A    print "       If you are trying to use a compiler other than those listed"
5942292SN/A    print "       above you will need to ease fix SConstruct and "
5952292SN/A    print "       src/SConscript to support that compiler."
5962292SN/A    Exit(1)
5972292SN/A
5982292SN/A# Set up common yacc/bison flags (needed for Ruby)
5992292SN/Amain['YACCFLAGS'] = '-d'
6002292SN/Amain['YACCHXXFILESUFFIX'] = '.hh'
6012292SN/A
6022292SN/A# Do this after we save setting back, or else we'll tack on an
6032292SN/A# extra 'qdo' every time we run scons.
6042292SN/Aif main['BATCH']:
6052292SN/A    main['CC']     = main['BATCH_CMD'] + ' ' + main['CC']
6062292SN/A    main['CXX']    = main['BATCH_CMD'] + ' ' + main['CXX']
6072292SN/A    main['AS']     = main['BATCH_CMD'] + ' ' + main['AS']
6082292SN/A    main['AR']     = main['BATCH_CMD'] + ' ' + main['AR']
6092292SN/A    main['RANLIB'] = main['BATCH_CMD'] + ' ' + main['RANLIB']
6102292SN/A
6112935Sksewell@umich.eduif sys.platform == 'cygwin':
6122292SN/A    # cygwin has some header file issues...
6132292SN/A    main.Append(CCFLAGS=["-Wno-uninitialized"])
6142292SN/A
6152292SN/A# Check for the protobuf compiler
6162292SN/Aprotoc_version = readCommand([main['PROTOC'], '--version'],
6172292SN/A                             exception='').split()
6182292SN/A
6192292SN/A# First two words should be "libprotoc x.y.z"
6202292SN/Aif len(protoc_version) < 2 or protoc_version[0] != 'libprotoc':
6212292SN/A    print termcap.Yellow + termcap.Bold + \
6222292SN/A        'Warning: Protocol buffer compiler (protoc) not found.\n' + \
6232292SN/A        '         Please install protobuf-compiler for tracing support.' + \
6242292SN/A        termcap.Normal
6252292SN/A    main['PROTOC'] = False
6262292SN/Aelse:
6272292SN/A    # Determine the appropriate include path and library path using
6282292SN/A    # pkg-config, that means we also need to check for pkg-config
6292336SN/A    if not readCommand(['pkg-config', '--version'], exception=''):
6302336SN/A        print 'Error: pkg-config not found. Please install and retry.'
6312336SN/A        Exit(1)
6322336SN/A
6332336SN/A    main.ParseConfig('pkg-config --cflags --libs-only-L protobuf')
6342336SN/A
6352336SN/A    # Based on the availability of the compress stream wrappers,
6362336SN/A    # require 2.1.0
6372292SN/A    min_protoc_version = '2.1.0'
6382292SN/A    if compareVersions(protoc_version[1], min_protoc_version) < 0:
6392301SN/A        print 'Error: protoc version', min_protoc_version, 'or newer required.'
6402301SN/A        print '       Installed version:', protoc_version[1]
6412292SN/A        Exit(1)
6422301SN/A
6432301SN/A# Check for SWIG
6442301SN/Aif not main.has_key('SWIG'):
6452292SN/A    print 'Error: SWIG utility not found.'
6462301SN/A    print '       Please install (see http://www.swig.org) and retry.'
6472292SN/A    Exit(1)
6482301SN/A
6492292SN/A# Check for appropriate SWIG version
6502301SN/Aswig_version = readCommand([main['SWIG'], '-version'], exception='').split()
6512292SN/A# First 3 words should be "SWIG Version x.y.z"
6522292SN/Aif len(swig_version) < 3 or \
6532292SN/A        swig_version[0] != 'SWIG' or swig_version[1] != 'Version':
6542292SN/A    print 'Error determining SWIG version.'
6552336SN/A    Exit(1)
6562336SN/A
6572292SN/Amin_swig_version = '1.3.34'
6582292SN/Aif compareVersions(swig_version[2], min_swig_version) < 0:
6592307SN/A    print 'Error: SWIG version', min_swig_version, 'or newer required.'
6602307SN/A    print '       Installed version:', swig_version[2]
6612292SN/A    Exit(1)
6622292SN/A
6632292SN/A# Set up SWIG flags & scanner
6642292SN/Aswig_flags=Split('-c++ -python -modern -templatereduce $_CPPINCFLAGS')
6652292SN/Amain.Append(SWIGFLAGS=swig_flags)
6662292SN/A
6672292SN/A# filter out all existing swig scanners, they mess up the dependency
6682292SN/A# stuff for some reason
6692292SN/Ascanners = []
6702292SN/Afor scanner in main['SCANNERS']:
6712292SN/A    skeys = scanner.skeys
6722292SN/A    if skeys == '.i':
6732292SN/A        continue
6742292SN/A
6752292SN/A    if isinstance(skeys, (list, tuple)) and '.i' in skeys:
6762292SN/A        continue
6772292SN/A
6782292SN/A    scanners.append(scanner)
6792292SN/A
6802292SN/A# add the new swig scanner that we like better
6812292SN/Afrom SCons.Scanner import ClassicCPP as CPPScanner
6822292SN/Aswig_inc_re = '^[ \t]*[%,#][ \t]*(?:include|import)[ \t]*(<|")([^>"]+)(>|")'
6832292SN/Ascanners.append(CPPScanner("SwigScan", [ ".i" ], "CPPPATH", swig_inc_re))
6842292SN/A
6852292SN/A# replace the scanners list that has what we want
6862292SN/Amain['SCANNERS'] = scanners
6872292SN/A
6882292SN/A# Add a custom Check function to the Configure context so that we can
6892292SN/A# figure out if the compiler adds leading underscores to global
6902292SN/A# variables.  This is needed for the autogenerated asm files that we
6912292SN/A# use for embedding the python code.
6922292SN/Adef CheckLeading(context):
6932292SN/A    context.Message("Checking for leading underscore in global variables...")
6942292SN/A    # 1) Define a global variable called x from asm so the C compiler
6952292SN/A    #    won't change the symbol at all.
6962307SN/A    # 2) Declare that variable.
6972292SN/A    # 3) Use the variable
6982292SN/A    #
6992292SN/A    # If the compiler prepends an underscore, this will successfully
7002292SN/A    # link because the external symbol 'x' will be called '_x' which
7012292SN/A    # was defined by the asm statement.  If the compiler does not
7022292SN/A    # prepend an underscore, this will not successfully link because
7032292SN/A    # '_x' will have been defined by assembly, while the C portion of
7042292SN/A    # the code will be trying to use 'x'
7052292SN/A    ret = context.TryLink('''
7062292SN/A        asm(".globl _x; _x: .byte 0");
7072292SN/A        extern int x;
7082292SN/A        int main() { return x; }
7092292SN/A        ''', extension=".c")
7102292SN/A    context.env.Append(LEADING_UNDERSCORE=ret)
7112292SN/A    context.Result(ret)
7122292SN/A    return ret
7132292SN/A
7142292SN/A# Test for the presence of C++11 static asserts. If the compiler lacks
7152292SN/A# support for static asserts, base/compiler.hh enables a macro that
7162292SN/A# removes any static asserts in the code.
7172292SN/Adef CheckStaticAssert(context):
7182292SN/A    context.Message("Checking for C++11 static_assert support...")
7192292SN/A    ret = context.TryCompile('''
7202292SN/A        static_assert(1, "This assert is always true");
7212292SN/A        ''', extension=".cc")
7222292SN/A    context.env.Append(HAVE_STATIC_ASSERT=ret)
7232292SN/A    context.Result(ret)
7242292SN/A    return ret
7252292SN/A
7262292SN/A# Platform-specific configuration.  Note again that we assume that all
7272292SN/A# builds under a given build root run on the same host platform.
7282292SN/Aconf = Configure(main,
7292292SN/A                 conf_dir = joinpath(build_root, '.scons_config'),
7302292SN/A                 log_file = joinpath(build_root, 'scons_config.log'),
7312307SN/A                 custom_tests = { 'CheckLeading' : CheckLeading,
7322307SN/A                                  'CheckStaticAssert' : CheckStaticAssert,
7332292SN/A                                })
7342292SN/A
7352292SN/A# Check for leading underscores.  Don't really need to worry either
7362292SN/A# way so don't need to check the return code.
7372292SN/Aconf.CheckLeading()
7382292SN/A
7392292SN/A# Check for C++11 features we want to use if they exist
7402292SN/Aconf.CheckStaticAssert()
7412292SN/A
7422292SN/A# Check if we should compile a 64 bit binary on Mac OS X/Darwin
7432292SN/Atry:
7442292SN/A    import platform
7452329SN/A    uname = platform.uname()
7463093Sksewell@umich.edu    if uname[0] == 'Darwin' and compareVersions(uname[2], '9.0.0') >= 0:
7472292SN/A        if int(readCommand('sysctl -n hw.cpu64bit_capable')[0]):
7482292SN/A            main.Append(CCFLAGS=['-arch', 'x86_64'])
7492329SN/A            main.Append(CFLAGS=['-arch', 'x86_64'])
7502935Sksewell@umich.edu            main.Append(LINKFLAGS=['-arch', 'x86_64'])
7512292SN/A            main.Append(ASFLAGS=['-arch', 'x86_64'])
7522292SN/Aexcept:
7532292SN/A    pass
7542292SN/A
7552292SN/A# Recent versions of scons substitute a "Null" object for Configure()
7562292SN/A# when configuration isn't necessary, e.g., if the "--help" option is
7572292SN/A# present.  Unfortuantely this Null object always returns false,
7582292SN/A# breaking all our configuration checks.  We replace it with our own
7592292SN/A# more optimistic null object that returns True instead.
7602292SN/Aif not conf:
7612980Sgblack@eecs.umich.edu    def NullCheck(*args, **kwargs):
7622292SN/A        return True
7632292SN/A
7642292SN/A    class NullConf:
7652292SN/A        def __init__(self, env):
7662292SN/A            self.env = env
7672292SN/A        def Finish(self):
7682292SN/A            return self.env
7692292SN/A        def __getattr__(self, mname):
7702292SN/A            return NullCheck
7712292SN/A
7722292SN/A    conf = NullConf(main)
7732292SN/A
7742292SN/A# Find Python include and library directories for embedding the
7752292SN/A# interpreter.  For consistency, we will use the same Python
7762292SN/A# installation used to run scons (and thus this script).  If you want
7772980Sgblack@eecs.umich.edu# to link in an alternate version, see above for instructions on how
7782292SN/A# to invoke scons with a different copy of the Python interpreter.
7792292SN/Afrom distutils import sysconfig
7802292SN/A
7812292SN/Apy_getvar = sysconfig.get_config_var
7822292SN/A
7832292SN/Apy_debug = getattr(sys, 'pydebug', False)
7842292SN/Apy_version = 'python' + py_getvar('VERSION') + (py_debug and "_d" or "")
7852292SN/A
7862292SN/Apy_general_include = sysconfig.get_python_inc()
7872292SN/Apy_platform_include = sysconfig.get_python_inc(plat_specific=True)
7882292SN/Apy_includes = [ py_general_include ]
7892292SN/Aif py_platform_include != py_general_include:
7902292SN/A    py_includes.append(py_platform_include)
7912292SN/A
7922292SN/Apy_lib_path = [ py_getvar('LIBDIR') ]
7932292SN/A# add the prefix/lib/pythonX.Y/config dir, but only if there is no
7942292SN/A# shared library in prefix/lib/.
7952292SN/Aif not py_getvar('Py_ENABLE_SHARED'):
7962292SN/A    py_lib_path.append(py_getvar('LIBPL'))
7972733Sktlim@umich.edu
7982292SN/Apy_libs = []
7992292SN/Afor lib in py_getvar('LIBS').split() + py_getvar('SYSLIBS').split():
8002292SN/A    if not lib.startswith('-l'):
8012292SN/A        # Python requires some special flags to link (e.g. -framework
8022292SN/A        # common on OS X systems), assume appending preserves order
8032292SN/A        main.Append(LINKFLAGS=[lib])
8042292SN/A    else:
8052292SN/A        lib = lib[2:]
8062733Sktlim@umich.edu        if lib not in py_libs:
8072292SN/A            py_libs.append(lib)
8082292SN/Apy_libs.append(py_version)
8092292SN/A
8102292SN/Amain.Append(CPPPATH=py_includes)
8112292SN/Amain.Append(LIBPATH=py_lib_path)
8122292SN/A
8132292SN/A# Cache build files in the supplied directory.
8142292SN/Aif main['M5_BUILD_CACHE']:
8152292SN/A    print 'Using build cache located at', main['M5_BUILD_CACHE']
8162292SN/A    CacheDir(main['M5_BUILD_CACHE'])
8172292SN/A
8182292SN/A
8192292SN/A# verify that this stuff works
8202292SN/Aif not conf.CheckHeader('Python.h', '<>'):
8212292SN/A    print "Error: can't find Python.h header in", py_includes
8222292SN/A    print "Install Python headers (package python-dev on Ubuntu and RedHat)"
8232292SN/A    Exit(1)
8242292SN/A
8252292SN/Afor lib in py_libs:
8262292SN/A    if not conf.CheckLib(lib):
8272292SN/A        print "Error: can't find library %s required by python" % lib
8282292SN/A        Exit(1)
8292292SN/A
8302329SN/A# On Solaris you need to use libsocket for socket ops
8312329SN/Aif not conf.CheckLibWithHeader(None, 'sys/socket.h', 'C++', 'accept(0,0,0);'):
8322301SN/A   if not conf.CheckLibWithHeader('socket', 'sys/socket.h', 'C++', 'accept(0,0,0);'):
8332292SN/A       print "Can't find library with socket calls (e.g. accept())"
8342292SN/A       Exit(1)
8352292SN/A
8362292SN/A# Check for zlib.  If the check passes, libz will be automatically
8372292SN/A# added to the LIBS environment variable.
8382292SN/Aif not conf.CheckLibWithHeader('z', 'zlib.h', 'C++','zlibVersion();'):
8392292SN/A    print 'Error: did not find needed zlib compression library '\
8402292SN/A          'and/or zlib.h header file.'
8412292SN/A    print '       Please install zlib and try again.'
8422292SN/A    Exit(1)
8432292SN/A
8442292SN/A# If we have the protobuf compiler, also make sure we have the
8452292SN/A# development libraries. If the check passes, libprotobuf will be
8462292SN/A# automatically added to the LIBS environment variable. After
8472292SN/A# this, we can use the HAVE_PROTOBUF flag to determine if we have
8482292SN/A# got both protoc and libprotobuf available.
8492301SN/Amain['HAVE_PROTOBUF'] = main['PROTOC'] and \
8502292SN/A    conf.CheckLibWithHeader('protobuf', 'google/protobuf/message.h',
8512292SN/A                            'C++', 'GOOGLE_PROTOBUF_VERIFY_VERSION;')
8522292SN/A
8532292SN/A# If we have the compiler but not the library, treat it as an error.
8542292SN/Aif main['PROTOC'] and not main['HAVE_PROTOBUF']:
8552292SN/A    print 'Error: did not find protocol buffer library and/or headers.'
8562292SN/A    print '       Please install libprotobuf-dev and try again.'
8572292SN/A    Exit(1)
8582292SN/A
8592292SN/A# Check for librt.
8602292SN/Ahave_posix_clock = \
8612292SN/A    conf.CheckLibWithHeader(None, 'time.h', 'C',
8622292SN/A                            'clock_nanosleep(0,0,NULL,NULL);') or \
8632292SN/A    conf.CheckLibWithHeader('rt', 'time.h', 'C',
8642292SN/A                            'clock_nanosleep(0,0,NULL,NULL);')
8652935Sksewell@umich.edu
8662292SN/Aif conf.CheckLib('tcmalloc_minimal'):
8672980Sgblack@eecs.umich.edu    have_tcmalloc = True
8682980Sgblack@eecs.umich.eduelse:
8692292SN/A    have_tcmalloc = False
8701060SN/A    print termcap.Yellow + termcap.Bold + \
8711060SN/A          "You can get a 12% performance improvement by installing tcmalloc "\
8722292SN/A          "(libgoogle-perftools-dev package on Ubuntu or RedHat)." + \
8731060SN/A          termcap.Normal
8741060SN/A
8751060SN/Aif not have_posix_clock:
8761060SN/A    print "Can't find library for POSIX clocks."
8771060SN/A
8782292SN/A# Check for <fenv.h> (C99 FP environment control)
8792292SN/Ahave_fenv = conf.CheckHeader('fenv.h', '<>')
8802292SN/Aif not have_fenv:
8811062SN/A    print "Warning: Header file <fenv.h> not found."
8822292SN/A    print "         This host has no IEEE FP rounding mode control."
8832292SN/A
8841060SN/A######################################################################
8852292SN/A#
8862292SN/A# Finish the configuration
8872292SN/A#
8881060SN/Amain = conf.Finish()
8892292SN/A
8902292SN/A######################################################################
8911062SN/A#
8922292SN/A# Collect all non-global variables
8931061SN/A#
8941062SN/A
8951060SN/A# Define the universe of supported ISAs
8961060SN/Aall_isa_list = [ ]
8971060SN/AExport('all_isa_list')
8981060SN/A
8991060SN/Aclass CpuModel(object):
9002292SN/A    '''The CpuModel class encapsulates everything the ISA parser needs to
9011060SN/A    know about a particular CPU model.'''
9022292SN/A
9032292SN/A    # Dict of available CPU model objects.  Accessible as CpuModel.dict.
9042292SN/A    dict = {}
9052292SN/A    list = []
9062980Sgblack@eecs.umich.edu    defaults = []
9072980Sgblack@eecs.umich.edu
9081060SN/A    # Constructor.  Automatically adds models to CpuModel.dict.
9091061SN/A    def __init__(self, name, filename, includes, strings, default=False):
9101060SN/A        self.name = name           # name of model
9112292SN/A        self.filename = filename   # filename for output exec code
9122292SN/A        self.includes = includes   # include files needed in exec file
9132292SN/A        # The 'strings' dict holds all the per-CPU symbols we can
9142292SN/A        # substitute into templates etc.
9152292SN/A        self.strings = strings
9162292SN/A
9171060SN/A        # This cpu is enabled by default
9181060SN/A        self.default = default
9191060SN/A
9202292SN/A        # Add self to dict
9212292SN/A        if name in CpuModel.dict:
9222292SN/A            raise AttributeError, "CpuModel '%s' already registered" % name
9232292SN/A        CpuModel.dict[name] = self
9242292SN/A        CpuModel.list.append(name)
9252292SN/A
9262292SN/AExport('CpuModel')
9271060SN/A
9282329SN/A# Sticky variables get saved in the variables file so they persist from
9292329SN/A# one invocation to the next (unless overridden, in which case the new
9302292SN/A# value becomes sticky).
9311061SN/Asticky_vars = Variables(args=ARGUMENTS)
9322292SN/AExport('sticky_vars')
9332292SN/A
9341061SN/A# Sticky variables that should be exported
9352292SN/Aexport_vars = []
9361060SN/AExport('export_vars')
9371060SN/A
9381060SN/A# For Ruby
9391061SN/Aall_protocols = []
9401061SN/AExport('all_protocols')
9412292SN/Aprotocol_dirs = []
9421061SN/AExport('protocol_dirs')
9432292SN/Aslicc_includes = []
9442292SN/AExport('slicc_includes')
9451061SN/A
9461061SN/A# Walk the tree and execute all SConsopts scripts that wil add to the
9471061SN/A# above variables
9481061SN/Aif not GetOption('verbose'):
9491061SN/A    print "Reading SConsopts"
9502292SN/Afor bdir in [ base_dir ] + extras_dir_list:
9511061SN/A    if not isdir(bdir):
9521061SN/A        print "Error: directory '%s' does not exist" % bdir
9531061SN/A        Exit(1)
9541061SN/A    for root, dirs, files in os.walk(bdir):
9552292SN/A        if 'SConsopts' in files:
9561061SN/A            if GetOption('verbose'):
9572292SN/A                print "Reading", joinpath(root, 'SConsopts')
9582292SN/A            SConscript(joinpath(root, 'SConsopts'))
9592292SN/A
9601061SN/Aall_isa_list.sort()
9611061SN/A
9621061SN/Asticky_vars.AddVariables(
9632292SN/A    EnumVariable('TARGET_ISA', 'Target ISA', 'alpha', all_isa_list),
9642292SN/A    ListVariable('CPU_MODELS', 'CPU models',
9652292SN/A                 sorted(n for n,m in CpuModel.dict.iteritems() if m.default),
9661061SN/A                 sorted(CpuModel.list)),
9671061SN/A    BoolVariable('EFENCE', 'Link with Electric Fence malloc debugger',
9681061SN/A                 False),
9691062SN/A    BoolVariable('SS_COMPATIBLE_FP',
9701062SN/A                 'Make floating-point results compatible with SimpleScalar',
9711061SN/A                 False),
9721061SN/A    BoolVariable('USE_SSE2',
9731061SN/A                 'Compile for SSE2 (-msse2) to get IEEE FP on x86 hosts',
9741061SN/A                 False),
9751061SN/A    BoolVariable('USE_POSIX_CLOCK', 'Use POSIX Clocks', have_posix_clock),
9762292SN/A    BoolVariable('USE_FENV', 'Use <fenv.h> IEEE mode control', have_fenv),
9771061SN/A    BoolVariable('CP_ANNOTATE', 'Enable critical path annotation capability', False),
9782292SN/A    EnumVariable('PROTOCOL', 'Coherence protocol for Ruby', 'None',
9791061SN/A                  all_protocols),
9801061SN/A    )
9811061SN/A
9822292SN/A# These variables get exported to #defines in config/*.hh (see src/SConscript).
9832292SN/Aexport_vars += ['USE_FENV', 'SS_COMPATIBLE_FP',
9842292SN/A                'TARGET_ISA', 'CP_ANNOTATE', 'USE_POSIX_CLOCK', 'PROTOCOL',
9851061SN/A                'HAVE_STATIC_ASSERT', 'HAVE_PROTOBUF']
9862292SN/A
9872292SN/A###################################################
9882292SN/A#
9891061SN/A# Define a SCons builder for configuration flag headers.
9902292SN/A#
9912292SN/A###################################################
9921062SN/A
9932292SN/A# This function generates a config header file that #defines the
9942292SN/A# variable symbol to the current variable setting (0 or 1).  The source
9952292SN/A# operands are the name of the variable and a Value node containing the
9961062SN/A# value of the variable.
9972292SN/Adef build_config_file(target, source, env):
9982292SN/A    (variable, value) = [s.get_contents() for s in source]
9992292SN/A    f = file(str(target[0]), 'w')
10002292SN/A    print >> f, '#define', variable, value
10011062SN/A    f.close()
10022292SN/A    return None
10031062SN/A
10042935Sksewell@umich.edu# Combine the two functions into a scons Action object.
10052935Sksewell@umich.educonfig_action = MakeAction(build_config_file, Transform("CONFIG H", 2))
10062935Sksewell@umich.edu
10072292SN/A# The emitter munges the source & target node lists to reflect what
10081062SN/A# we're really doing.
10092292SN/Adef config_emitter(target, source, env):
10102292SN/A    # extract variable name from Builder arg
10112292SN/A    variable = str(target[0])
10122292SN/A    # True target is config header file
10132292SN/A    target = joinpath('config', variable.lower() + '.hh')
10142292SN/A    val = env[variable]
10152292SN/A    if isinstance(val, bool):
10162292SN/A        # Force value to 0/1
10171062SN/A        val = int(val)
10182292SN/A    elif isinstance(val, str):
10191061SN/A        val = '"' + val + '"'
10201061SN/A
10211061SN/A    # Sources are variable name & value (packaged in SCons Value nodes)
10221061SN/A    return ([target], [Value(variable), Value(val)])
10231061SN/A
10242292SN/Aconfig_builder = Builder(emitter = config_emitter, action = config_action)
10251061SN/A
10262292SN/Amain.Append(BUILDERS = { 'ConfigFile' : config_builder })
10272292SN/A
10282292SN/A# libelf build is shared across all configs in the build root.
10292292SN/Amain.SConscript('ext/libelf/SConscript',
10302292SN/A                variant_dir = joinpath(build_root, 'libelf'))
10312292SN/A
10321061SN/A# gzstream build is shared across all configs in the build root.
10331061SN/Amain.SConscript('ext/gzstream/SConscript',
10341061SN/A                variant_dir = joinpath(build_root, 'gzstream'))
10351061SN/A
10362292SN/A###################################################
10371061SN/A#
10382292SN/A# This function is used to set up a directory with switching headers
10392292SN/A#
10402292SN/A###################################################
10412292SN/A
10422292SN/Amain['ALL_ISA_LIST'] = all_isa_list
10432292SN/Adef make_switching_dir(dname, switch_headers, env):
10442292SN/A    # Generate the header.  target[0] is the full path of the output
10452292SN/A    # header to generate.  'source' is a dummy variable, since we get the
10462292SN/A    # list of ISAs from env['ALL_ISA_LIST'].
10472292SN/A    def gen_switch_hdr(target, source, env):
10482292SN/A        fname = str(target[0])
10492292SN/A        f = open(fname, 'w')
10502292SN/A        isa = env['TARGET_ISA'].lower()
10512292SN/A        print >>f, '#include "%s/%s/%s"' % (dname, isa, basename(fname))
10522292SN/A        f.close()
10532292SN/A
10542292SN/A    # Build SCons Action object. 'varlist' specifies env vars that this
10552292SN/A    # action depends on; when env['ALL_ISA_LIST'] changes these actions
10562292SN/A    # should get re-executed.
10572292SN/A    switch_hdr_action = MakeAction(gen_switch_hdr,
10582292SN/A                          Transform("GENERATE"), varlist=['ALL_ISA_LIST'])
10592292SN/A
10602292SN/A    # Instantiate actions for each header
10612292SN/A    for hdr in switch_headers:
10622292SN/A        env.Command(hdr, [], switch_hdr_action)
10632292SN/AExport('make_switching_dir')
10642292SN/A
10652731Sktlim@umich.edu###################################################
10662292SN/A#
10672292SN/A# Define build environments for selected configurations.
10682292SN/A#
10692292SN/A###################################################
10702292SN/A
10712292SN/Afor variant_path in variant_paths:
10722292SN/A    print "Building in", variant_path
10732292SN/A
10742292SN/A    # Make a copy of the build-root environment to use for this config.
10752292SN/A    env = main.Clone()
10762292SN/A    env['BUILDDIR'] = variant_path
10772292SN/A
10782292SN/A    # variant_dir is the tail component of build path, and is used to
10792292SN/A    # determine the build parameters (e.g., 'ALPHA_SE')
10802292SN/A    (build_root, variant_dir) = splitpath(variant_path)
10812292SN/A
10822292SN/A    # Set env variables according to the build directory config.
10832292SN/A    sticky_vars.files = []
10842292SN/A    # Variables for $BUILD_ROOT/$VARIANT_DIR are stored in
10852292SN/A    # $BUILD_ROOT/variables/$VARIANT_DIR so you can nuke
10862292SN/A    # $BUILD_ROOT/$VARIANT_DIR without losing your variables settings.
10872292SN/A    current_vars_file = joinpath(build_root, 'variables', variant_dir)
10882292SN/A    if isfile(current_vars_file):
10892292SN/A        sticky_vars.files.append(current_vars_file)
10902292SN/A        print "Using saved variables file %s" % current_vars_file
10912292SN/A    else:
10922292SN/A        # Build dir-specific variables file doesn't exist.
10932292SN/A
10942292SN/A        # Make sure the directory is there so we can create it later
10952292SN/A        opt_dir = dirname(current_vars_file)
10962292SN/A        if not isdir(opt_dir):
10972292SN/A            mkdir(opt_dir)
10982292SN/A
10992292SN/A        # Get default build variables from source tree.  Variables are
11002292SN/A        # normally determined by name of $VARIANT_DIR, but can be
11012292SN/A        # overridden by '--default=' arg on command line.
11022292SN/A        default = GetOption('default')
11032292SN/A        opts_dir = joinpath(main.root.abspath, 'build_opts')
11042292SN/A        if default:
11052292SN/A            default_vars_files = [joinpath(build_root, 'variables', default),
11062292SN/A                                  joinpath(opts_dir, default)]
11072292SN/A        else:
11082292SN/A            default_vars_files = [joinpath(opts_dir, variant_dir)]
11092292SN/A        existing_files = filter(isfile, default_vars_files)
11102292SN/A        if existing_files:
11112292SN/A            default_vars_file = existing_files[0]
11122292SN/A            sticky_vars.files.append(default_vars_file)
11132292SN/A            print "Variables file %s not found,\n  using defaults in %s" \
11142292SN/A                  % (current_vars_file, default_vars_file)
11152292SN/A        else:
11162292SN/A            print "Error: cannot find variables file %s or " \
11172292SN/A                  "default file(s) %s" \
11182292SN/A                  % (current_vars_file, ' or '.join(default_vars_files))
11192301SN/A            Exit(1)
11202292SN/A
11212301SN/A    # Apply current variable settings to env
11222292SN/A    sticky_vars.Update(env)
11232292SN/A
11242292SN/A    help_texts["local_vars"] += \
11252292SN/A        "Build variables for %s:\n" % variant_dir \
11262292SN/A                 + sticky_vars.GenerateHelpText(env)
11272292SN/A
11282292SN/A    # Process variable settings.
11292292SN/A
11302292SN/A    if not have_fenv and env['USE_FENV']:
11312292SN/A        print "Warning: <fenv.h> not available; " \
11322292SN/A              "forcing USE_FENV to False in", variant_dir + "."
11332292SN/A        env['USE_FENV'] = False
11342292SN/A
11352292SN/A    if not env['USE_FENV']:
11362292SN/A        print "Warning: No IEEE FP rounding mode control in", variant_dir + "."
11372292SN/A        print "         FP results may deviate slightly from other platforms."
11382292SN/A
11392292SN/A    if env['EFENCE']:
11402292SN/A        env.Append(LIBS=['efence'])
11412292SN/A
11422292SN/A    # Save sticky variable settings back to current variables file
11432292SN/A    sticky_vars.Save(current_vars_file, env)
11442292SN/A
11452292SN/A    if env['USE_SSE2']:
11462292SN/A        env.Append(CCFLAGS=['-msse2'])
11472292SN/A
11482292SN/A    if have_tcmalloc:
11492292SN/A        env.Append(LIBS=['tcmalloc_minimal'])
11502292SN/A
11512292SN/A    # The src/SConscript file sets up the build rules in 'env' according
11522292SN/A    # to the configured variables.  It returns a list of environments,
11532292SN/A    # one for each variant build (debug, opt, etc.)
11542292SN/A    envList = SConscript('src/SConscript', variant_dir = variant_path,
11552292SN/A                         exports = 'env')
11562292SN/A
11572292SN/A    # Set up the regression tests for each build.
11582292SN/A    for e in envList:
11592292SN/A        SConscript('tests/SConscript',
11602292SN/A                   variant_dir = joinpath(variant_path, 'tests', e.Label),
11612292SN/A                   exports = { 'env' : e }, duplicate = False)
11622292SN/A
11632292SN/A# base help text
11642292SN/AHelp('''
11652292SN/AUsage: scons [scons options] [build variables] [target(s)]
11662292SN/A
11672292SN/AExtra scons options:
11682292SN/A%(options)s
11692292SN/A
11702292SN/AGlobal build variables:
11712292SN/A%(global_vars)s
11722292SN/A
11732292SN/A%(local_vars)s
11742292SN/A''' % help_texts)
11752301SN/A