SConstruct revision 12246
11758Ssaidi@eecs.umich.edu# -*- mode:python -*-
21758Ssaidi@eecs.umich.edu
31758Ssaidi@eecs.umich.edu# Copyright (c) 2013, 2015-2017 ARM Limited
41758Ssaidi@eecs.umich.edu# All rights reserved.
51758Ssaidi@eecs.umich.edu#
61758Ssaidi@eecs.umich.edu# The license below extends only to copyright in the software and shall
71758Ssaidi@eecs.umich.edu# not be construed as granting a license to any other intellectual
81758Ssaidi@eecs.umich.edu# property including but not limited to intellectual property relating
91758Ssaidi@eecs.umich.edu# to a hardware implementation of the functionality of the software
101758Ssaidi@eecs.umich.edu# licensed hereunder.  You may use the software subject to the license
111758Ssaidi@eecs.umich.edu# terms below provided that you ensure that this notice is replicated
121758Ssaidi@eecs.umich.edu# unmodified and in its entirety in all distributions of the software,
131758Ssaidi@eecs.umich.edu# modified or unmodified, in source code or in binary form.
141758Ssaidi@eecs.umich.edu#
151758Ssaidi@eecs.umich.edu# Copyright (c) 2011 Advanced Micro Devices, Inc.
161758Ssaidi@eecs.umich.edu# Copyright (c) 2009 The Hewlett-Packard Development Company
171758Ssaidi@eecs.umich.edu# Copyright (c) 2004-2005 The Regents of The University of Michigan
181758Ssaidi@eecs.umich.edu# All rights reserved.
191758Ssaidi@eecs.umich.edu#
201758Ssaidi@eecs.umich.edu# Redistribution and use in source and binary forms, with or without
211758Ssaidi@eecs.umich.edu# modification, are permitted provided that the following conditions are
221758Ssaidi@eecs.umich.edu# met: redistributions of source code must retain the above copyright
231758Ssaidi@eecs.umich.edu# notice, this list of conditions and the following disclaimer;
241758Ssaidi@eecs.umich.edu# redistributions in binary form must reproduce the above copyright
251758Ssaidi@eecs.umich.edu# notice, this list of conditions and the following disclaimer in the
261758Ssaidi@eecs.umich.edu# documentation and/or other materials provided with the distribution;
271049Sbinkertn@umich.edu# neither the name of the copyright holders nor the names of its
281049Sbinkertn@umich.edu# contributors may be used to endorse or promote products derived from
291049Sbinkertn@umich.edu# this software without specific prior written permission.
301049Sbinkertn@umich.edu#
311049Sbinkertn@umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
321049Sbinkertn@umich.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
331049Sbinkertn@umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
341049Sbinkertn@umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
351049Sbinkertn@umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
361049Sbinkertn@umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
371049Sbinkertn@umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
381049Sbinkertn@umich.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
391049Sbinkertn@umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
401049Sbinkertn@umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
411049Sbinkertn@umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
421049Sbinkertn@umich.edu#
431049Sbinkertn@umich.edu# Authors: Steve Reinhardt
441049Sbinkertn@umich.edu#          Nathan Binkert
451049Sbinkertn@umich.edu
461049Sbinkertn@umich.edu###################################################
471049Sbinkertn@umich.edu#
481049Sbinkertn@umich.edu# SCons top-level build description (SConstruct) file.
491049Sbinkertn@umich.edu#
501049Sbinkertn@umich.edu# While in this directory ('gem5'), just type 'scons' to build the default
511049Sbinkertn@umich.edu# configuration (see below), or type 'scons build/<CONFIG>/<binary>'
521049Sbinkertn@umich.edu# to build some other configuration (e.g., 'build/ALPHA/gem5.opt' for
531049Sbinkertn@umich.edu# the optimized full-system version).
541049Sbinkertn@umich.edu#
551049Sbinkertn@umich.edu# You can build gem5 in a different directory as long as there is a
561049Sbinkertn@umich.edu# 'build/<CONFIG>' somewhere along the target path.  The build system
571049Sbinkertn@umich.edu# expects that all configs under the same build directory are being
581049Sbinkertn@umich.edu# built for the same host system.
591049Sbinkertn@umich.edu#
601049Sbinkertn@umich.edu# Examples:
611049Sbinkertn@umich.edu#
621049Sbinkertn@umich.edu#   The following two commands are equivalent.  The '-u' option tells
631049Sbinkertn@umich.edu#   scons to search up the directory tree for this SConstruct file.
641049Sbinkertn@umich.edu#   % cd <path-to-src>/gem5 ; scons build/ALPHA/gem5.debug
651049Sbinkertn@umich.edu#   % cd <path-to-src>/gem5/build/ALPHA; scons -u gem5.debug
661049Sbinkertn@umich.edu#
671049Sbinkertn@umich.edu#   The following two commands are equivalent and demonstrate building
681049Sbinkertn@umich.edu#   in a directory outside of the source tree.  The '-C' option tells
691049Sbinkertn@umich.edu#   scons to chdir to the specified directory to find this SConstruct
701049Sbinkertn@umich.edu#   file.
711049Sbinkertn@umich.edu#   % cd <path-to-src>/gem5 ; scons /local/foo/build/ALPHA/gem5.debug
721049Sbinkertn@umich.edu#   % cd /local/foo/build/ALPHA; scons -C <path-to-src>/gem5 gem5.debug
731049Sbinkertn@umich.edu#
741049Sbinkertn@umich.edu# You can use 'scons -H' to print scons options.  If you're in this
751049Sbinkertn@umich.edu# 'gem5' directory (or use -u or -C to tell scons where to find this
761049Sbinkertn@umich.edu# file), you can use 'scons -h' to print all the gem5-specific build
771049Sbinkertn@umich.edu# options as well.
781049Sbinkertn@umich.edu#
791049Sbinkertn@umich.edu###################################################
801049Sbinkertn@umich.edu
811049Sbinkertn@umich.edu# Global Python includes
821049Sbinkertn@umich.eduimport itertools
831049Sbinkertn@umich.eduimport os
841049Sbinkertn@umich.eduimport re
851049Sbinkertn@umich.eduimport shutil
861049Sbinkertn@umich.eduimport subprocess
871049Sbinkertn@umich.eduimport sys
881049Sbinkertn@umich.edu
891049Sbinkertn@umich.edufrom os import mkdir, environ
901049Sbinkertn@umich.edufrom os.path import abspath, basename, dirname, expanduser, normpath
911049Sbinkertn@umich.edufrom os.path import exists,  isdir, isfile
921049Sbinkertn@umich.edufrom os.path import join as joinpath, split as splitpath
931049Sbinkertn@umich.edu
941049Sbinkertn@umich.edu# SCons includes
951049Sbinkertn@umich.eduimport SCons
961049Sbinkertn@umich.eduimport SCons.Node
971049Sbinkertn@umich.edu
981049Sbinkertn@umich.edufrom m5.util import compareVersions, readCommand
991049Sbinkertn@umich.edu
1001049Sbinkertn@umich.eduhelp_texts = {
1011049Sbinkertn@umich.edu    "options" : "",
1021049Sbinkertn@umich.edu    "global_vars" : "",
1031049Sbinkertn@umich.edu    "local_vars" : ""
1041881Sbinkertn@umich.edu}
1051049Sbinkertn@umich.edu
1061929Sbinkertn@umich.eduExport("help_texts")
1071929Sbinkertn@umich.edu
1081929Sbinkertn@umich.edu
1091929Sbinkertn@umich.edu# There's a bug in scons in that (1) by default, the help texts from
1101929Sbinkertn@umich.edu# AddOption() are supposed to be displayed when you type 'scons -h'
1111929Sbinkertn@umich.edu# and (2) you can override the help displayed by 'scons -h' using the
1121929Sbinkertn@umich.edu# Help() function, but these two features are incompatible: once
1131929Sbinkertn@umich.edu# you've overridden the help text using Help(), there's no way to get
1141929Sbinkertn@umich.edu# at the help texts from AddOptions.  See:
1151929Sbinkertn@umich.edu#     http://scons.tigris.org/issues/show_bug.cgi?id=2356
1161929Sbinkertn@umich.edu#     http://scons.tigris.org/issues/show_bug.cgi?id=2611
1171929Sbinkertn@umich.edu# This hack lets us extract the help text from AddOptions and
1181929Sbinkertn@umich.edu# re-inject it via Help().  Ideally someday this bug will be fixed and
1191929Sbinkertn@umich.edu# we can just use AddOption directly.
1201049Sbinkertn@umich.edudef AddLocalOption(*args, **kwargs):
1211049Sbinkertn@umich.edu    col_width = 30
1221049Sbinkertn@umich.edu
1231049Sbinkertn@umich.edu    help = "  " + ", ".join(args)
1241049Sbinkertn@umich.edu    if "help" in kwargs:
1251049Sbinkertn@umich.edu        length = len(help)
1261049Sbinkertn@umich.edu        if length >= col_width:
1271049Sbinkertn@umich.edu            help += "\n" + " " * col_width
1281049Sbinkertn@umich.edu        else:
1291049Sbinkertn@umich.edu            help += " " * (col_width - length)
1301049Sbinkertn@umich.edu        help += kwargs["help"]
1311049Sbinkertn@umich.edu    help_texts["options"] += help + "\n"
1321049Sbinkertn@umich.edu
1331049Sbinkertn@umich.edu    AddOption(*args, **kwargs)
1341049Sbinkertn@umich.edu
1351049Sbinkertn@umich.eduAddLocalOption('--colors', dest='use_colors', action='store_true',
1361049Sbinkertn@umich.edu               help="Add color to abbreviated scons output")
1371049Sbinkertn@umich.eduAddLocalOption('--no-colors', dest='use_colors', action='store_false',
1381049Sbinkertn@umich.edu               help="Don't add color to abbreviated scons output")
1391049Sbinkertn@umich.eduAddLocalOption('--with-cxx-config', dest='with_cxx_config',
1401049Sbinkertn@umich.edu               action='store_true',
1411049Sbinkertn@umich.edu               help="Build with support for C++-based configuration")
1421049Sbinkertn@umich.eduAddLocalOption('--default', dest='default', type='string', action='store',
1431049Sbinkertn@umich.edu               help='Override which build_opts file to use for defaults')
1441049Sbinkertn@umich.eduAddLocalOption('--ignore-style', dest='ignore_style', action='store_true',
1451049Sbinkertn@umich.edu               help='Disable style checking hooks')
1461049Sbinkertn@umich.eduAddLocalOption('--no-lto', dest='no_lto', action='store_true',
1471929Sbinkertn@umich.edu               help='Disable Link-Time Optimization for fast')
1481929Sbinkertn@umich.eduAddLocalOption('--force-lto', dest='force_lto', action='store_true',
1491929Sbinkertn@umich.edu               help='Use Link-Time Optimization instead of partial linking' +
1502014Sbinkertn@umich.edu                    ' when the compiler doesn\'t support using them together.')
1512014Sbinkertn@umich.eduAddLocalOption('--update-ref', dest='update_ref', action='store_true',
1521929Sbinkertn@umich.edu               help='Update test reference outputs')
1531929Sbinkertn@umich.eduAddLocalOption('--verbose', dest='verbose', action='store_true',
1541929Sbinkertn@umich.edu               help='Print full tool command lines')
1552014Sbinkertn@umich.eduAddLocalOption('--without-python', dest='without_python',
1562183Sbinkertn@umich.edu               action='store_true',
1572014Sbinkertn@umich.edu               help='Build without Python configuration support')
1582014Sbinkertn@umich.eduAddLocalOption('--without-tcmalloc', dest='without_tcmalloc',
1592014Sbinkertn@umich.edu               action='store_true',
1602014Sbinkertn@umich.edu               help='Disable linking against tcmalloc')
1612014Sbinkertn@umich.eduAddLocalOption('--with-ubsan', dest='with_ubsan', action='store_true',
1622014Sbinkertn@umich.edu               help='Build with Undefined Behavior Sanitizer if available')
1632014Sbinkertn@umich.eduAddLocalOption('--with-asan', dest='with_asan', action='store_true',
1642014Sbinkertn@umich.edu               help='Build with Address Sanitizer if available')
1652014Sbinkertn@umich.edu
1662014Sbinkertn@umich.eduif GetOption('no_lto') and GetOption('force_lto'):
1672014Sbinkertn@umich.edu    print '--no-lto and --force-lto are mutually exclusive'
1681929Sbinkertn@umich.edu    Exit(1)
1691929Sbinkertn@umich.edu
1701049Sbinkertn@umich.edu########################################################################
1711049Sbinkertn@umich.edu#
1721049Sbinkertn@umich.edu# Set up the main build environment.
1731049Sbinkertn@umich.edu#
1741049Sbinkertn@umich.edu########################################################################
1751049Sbinkertn@umich.edu
1761049Sbinkertn@umich.edumain = Environment()
1771049Sbinkertn@umich.edu
1781049Sbinkertn@umich.edufrom gem5_scons import Transform
1791049Sbinkertn@umich.edufrom gem5_scons.util import get_termcap
1801049Sbinkertn@umich.edutermcap = get_termcap()
1811049Sbinkertn@umich.edu
1821049Sbinkertn@umich.edumain_dict_keys = main.Dictionary().keys()
1831049Sbinkertn@umich.edu
1841049Sbinkertn@umich.edu# Check that we have a C/C++ compiler
1851049Sbinkertn@umich.eduif not ('CC' in main_dict_keys and 'CXX' in main_dict_keys):
1861049Sbinkertn@umich.edu    print "No C++ compiler installed (package g++ on Ubuntu and RedHat)"
1871049Sbinkertn@umich.edu    Exit(1)
1881049Sbinkertn@umich.edu
1891049Sbinkertn@umich.edu###################################################
1901049Sbinkertn@umich.edu#
1911049Sbinkertn@umich.edu# Figure out which configurations to set up based on the path(s) of
1921049Sbinkertn@umich.edu# the target(s).
1931049Sbinkertn@umich.edu#
1941049Sbinkertn@umich.edu###################################################
1951049Sbinkertn@umich.edu
1961049Sbinkertn@umich.edu# Find default configuration & binary.
1971049Sbinkertn@umich.eduDefault(environ.get('M5_DEFAULT_BINARY', 'build/ALPHA/gem5.debug'))
1981049Sbinkertn@umich.edu
1991049Sbinkertn@umich.edu# helper function: find last occurrence of element in list
2001049Sbinkertn@umich.edudef rfind(l, elt, offs = -1):
2011049Sbinkertn@umich.edu    for i in range(len(l)+offs, 0, -1):
2021049Sbinkertn@umich.edu        if l[i] == elt:
2031049Sbinkertn@umich.edu            return i
2041049Sbinkertn@umich.edu    raise ValueError, "element not found"
2051049Sbinkertn@umich.edu
2061049Sbinkertn@umich.edu# Take a list of paths (or SCons Nodes) and return a list with all
2071049Sbinkertn@umich.edu# paths made absolute and ~-expanded.  Paths will be interpreted
2081049Sbinkertn@umich.edu# relative to the launch directory unless a different root is provided
2091049Sbinkertn@umich.edudef makePathListAbsolute(path_list, root=GetLaunchDir()):
2101049Sbinkertn@umich.edu    return [abspath(joinpath(root, expanduser(str(p))))
2111049Sbinkertn@umich.edu            for p in path_list]
2121049Sbinkertn@umich.edu
2131049Sbinkertn@umich.edu# Each target must have 'build' in the interior of the path; the
2141049Sbinkertn@umich.edu# directory below this will determine the build parameters.  For
2151049Sbinkertn@umich.edu# example, for target 'foo/bar/build/ALPHA_SE/arch/alpha/blah.do' we
2161049Sbinkertn@umich.edu# recognize that ALPHA_SE specifies the configuration because it
2171049Sbinkertn@umich.edu# follow 'build' in the build path.
2181049Sbinkertn@umich.edu
2191049Sbinkertn@umich.edu# The funky assignment to "[:]" is needed to replace the list contents
2201049Sbinkertn@umich.edu# in place rather than reassign the symbol to a new list, which
2211049Sbinkertn@umich.edu# doesn't work (obviously!).
2221049Sbinkertn@umich.eduBUILD_TARGETS[:] = makePathListAbsolute(BUILD_TARGETS)
2231049Sbinkertn@umich.edu
2241049Sbinkertn@umich.edu# Generate a list of the unique build roots and configs that the
2251049Sbinkertn@umich.edu# collected targets reference.
2261306Sbinkertn@umich.eduvariant_paths = []
2271049Sbinkertn@umich.edubuild_root = None
2281049Sbinkertn@umich.edufor t in BUILD_TARGETS:
2291049Sbinkertn@umich.edu    path_dirs = t.split('/')
2301049Sbinkertn@umich.edu    try:
2311049Sbinkertn@umich.edu        build_top = rfind(path_dirs, 'build', -2)
2321929Sbinkertn@umich.edu    except:
2331049Sbinkertn@umich.edu        print "Error: no non-leaf 'build' dir found on target path", t
2341049Sbinkertn@umich.edu        Exit(1)
2351049Sbinkertn@umich.edu    this_build_root = joinpath('/',*path_dirs[:build_top+1])
2361049Sbinkertn@umich.edu    if not build_root:
2371049Sbinkertn@umich.edu        build_root = this_build_root
2381049Sbinkertn@umich.edu    else:
2391049Sbinkertn@umich.edu        if this_build_root != build_root:
2401049Sbinkertn@umich.edu            print "Error: build targets not under same build root\n"\
2411049Sbinkertn@umich.edu                  "  %s\n  %s" % (build_root, this_build_root)
2421049Sbinkertn@umich.edu            Exit(1)
2431049Sbinkertn@umich.edu    variant_path = joinpath('/',*path_dirs[:build_top+2])
2441049Sbinkertn@umich.edu    if variant_path not in variant_paths:
2451049Sbinkertn@umich.edu        variant_paths.append(variant_path)
2461049Sbinkertn@umich.edu
2471049Sbinkertn@umich.edu# Make sure build_root exists (might not if this is the first build there)
2481049Sbinkertn@umich.eduif not isdir(build_root):
2491049Sbinkertn@umich.edu    mkdir(build_root)
2501309Ssaidi@eecs.umich.edumain['BUILDROOT'] = build_root
2511049Sbinkertn@umich.edu
2521049Sbinkertn@umich.eduExport('main')
2531309Ssaidi@eecs.umich.edu
2541309Ssaidi@eecs.umich.edumain.SConsignFile(joinpath(build_root, "sconsign"))
2551309Ssaidi@eecs.umich.edu
2561309Ssaidi@eecs.umich.edu# Default duplicate option is to use hard links, but this messes up
2571309Ssaidi@eecs.umich.edu# when you use emacs to edit a file in the target dir, as emacs moves
2581309Ssaidi@eecs.umich.edu# file to file~ then copies to file, breaking the link.  Symbolic
2591309Ssaidi@eecs.umich.edu# (soft) links work better.
2601309Ssaidi@eecs.umich.edumain.SetOption('duplicate', 'soft-copy')
2611309Ssaidi@eecs.umich.edu
2621309Ssaidi@eecs.umich.edu#
2631309Ssaidi@eecs.umich.edu# Set up global sticky variables... these are common to an entire build
2641049Sbinkertn@umich.edu# tree (not specific to a particular build like ALPHA_SE)
2651049Sbinkertn@umich.edu#
2661049Sbinkertn@umich.edu
2671049Sbinkertn@umich.eduglobal_vars_file = joinpath(build_root, 'variables.global')
2681309Ssaidi@eecs.umich.edu
2691309Ssaidi@eecs.umich.eduglobal_vars = Variables(global_vars_file, args=ARGUMENTS)
2701309Ssaidi@eecs.umich.edu
2711309Ssaidi@eecs.umich.eduglobal_vars.AddVariables(
2721309Ssaidi@eecs.umich.edu    ('CC', 'C compiler', environ.get('CC', main['CC'])),
2731309Ssaidi@eecs.umich.edu    ('CXX', 'C++ compiler', environ.get('CXX', main['CXX'])),
2741309Ssaidi@eecs.umich.edu    ('PROTOC', 'protoc tool', environ.get('PROTOC', 'protoc')),
2751309Ssaidi@eecs.umich.edu    ('BATCH', 'Use batch pool for build and tests', False),
2761309Ssaidi@eecs.umich.edu    ('BATCH_CMD', 'Batch pool submission command name', 'qdo'),
2771309Ssaidi@eecs.umich.edu    ('M5_BUILD_CACHE', 'Cache built objects in this directory', False),
2781309Ssaidi@eecs.umich.edu    ('EXTRAS', 'Add extra directories to the compilation', '')
2791309Ssaidi@eecs.umich.edu    )
2801309Ssaidi@eecs.umich.edu
2811309Ssaidi@eecs.umich.edu# Update main environment with values from ARGUMENTS & global_vars_file
2821309Ssaidi@eecs.umich.eduglobal_vars.Update(main)
2831309Ssaidi@eecs.umich.eduhelp_texts["global_vars"] += global_vars.GenerateHelpText(main)
2841309Ssaidi@eecs.umich.edu
2851309Ssaidi@eecs.umich.edu# Save sticky variable settings back to current variables file
2861309Ssaidi@eecs.umich.eduglobal_vars.Save(global_vars_file, main)
2871049Sbinkertn@umich.edu
2881049Sbinkertn@umich.edu# Parse EXTRAS variable to build list of all directories where we're
2891049Sbinkertn@umich.edu# look for sources etc.  This list is exported as extras_dir_list.
2901049Sbinkertn@umich.edubase_dir = main.srcdir.abspath
2911049Sbinkertn@umich.eduif main['EXTRAS']:
2921049Sbinkertn@umich.edu    extras_dir_list = makePathListAbsolute(main['EXTRAS'].split(':'))
2931049Sbinkertn@umich.eduelse:
2941049Sbinkertn@umich.edu    extras_dir_list = []
2951049Sbinkertn@umich.edu
2961049Sbinkertn@umich.eduExport('base_dir')
2971049Sbinkertn@umich.eduExport('extras_dir_list')
2981049Sbinkertn@umich.edu
2991049Sbinkertn@umich.edu# the ext directory should be on the #includes path
3001049Sbinkertn@umich.edumain.Append(CPPPATH=[Dir('ext')])
3011049Sbinkertn@umich.edu
3021049Sbinkertn@umich.edu# Add shared top-level headers
3031049Sbinkertn@umich.edumain.Prepend(CPPPATH=Dir('include'))
3041049Sbinkertn@umich.edu
3051049Sbinkertn@umich.eduif GetOption('verbose'):
3061049Sbinkertn@umich.edu    def MakeAction(action, string, *args, **kwargs):
3071049Sbinkertn@umich.edu        return Action(action, *args, **kwargs)
3081049Sbinkertn@umich.eduelse:
3091049Sbinkertn@umich.edu    MakeAction = Action
3101049Sbinkertn@umich.edu    main['CCCOMSTR']        = Transform("CC")
3111049Sbinkertn@umich.edu    main['CXXCOMSTR']       = Transform("CXX")
3121049Sbinkertn@umich.edu    main['ASCOMSTR']        = Transform("AS")
3131049Sbinkertn@umich.edu    main['ARCOMSTR']        = Transform("AR", 0)
3141049Sbinkertn@umich.edu    main['LINKCOMSTR']      = Transform("LINK", 0)
3151049Sbinkertn@umich.edu    main['SHLINKCOMSTR']    = Transform("SHLINK", 0)
3161049Sbinkertn@umich.edu    main['RANLIBCOMSTR']    = Transform("RANLIB", 0)
3171049Sbinkertn@umich.edu    main['M4COMSTR']        = Transform("M4")
3181049Sbinkertn@umich.edu    main['SHCCCOMSTR']      = Transform("SHCC")
3191049Sbinkertn@umich.edu    main['SHCXXCOMSTR']     = Transform("SHCXX")
3201049Sbinkertn@umich.eduExport('MakeAction')
3211049Sbinkertn@umich.edu
3221049Sbinkertn@umich.edu# Initialize the Link-Time Optimization (LTO) flags
3231049Sbinkertn@umich.edumain['LTO_CCFLAGS'] = []
3241049Sbinkertn@umich.edumain['LTO_LDFLAGS'] = []
3251049Sbinkertn@umich.edu
3261049Sbinkertn@umich.edu# According to the readme, tcmalloc works best if the compiler doesn't
3271049Sbinkertn@umich.edu# assume that we're using the builtin malloc and friends. These flags
3281049Sbinkertn@umich.edu# are compiler-specific, so we need to set them after we detect which
3291049Sbinkertn@umich.edu# compiler we're using.
3301049Sbinkertn@umich.edumain['TCMALLOC_CCFLAGS'] = []
3311049Sbinkertn@umich.edu
3321049Sbinkertn@umich.eduCXX_version = readCommand([main['CXX'],'--version'], exception=False)
3331049Sbinkertn@umich.eduCXX_V = readCommand([main['CXX'],'-V'], exception=False)
3341049Sbinkertn@umich.edu
3351049Sbinkertn@umich.edumain['GCC'] = CXX_version and CXX_version.find('g++') >= 0
3361049Sbinkertn@umich.edumain['CLANG'] = CXX_version and CXX_version.find('clang') >= 0
3371049Sbinkertn@umich.eduif main['GCC'] + main['CLANG'] > 1:
3381049Sbinkertn@umich.edu    print 'Error: How can we have two at the same time?'
3391049Sbinkertn@umich.edu    Exit(1)
3401049Sbinkertn@umich.edu
3411049Sbinkertn@umich.edu# Set up default C++ compiler flags
3421049Sbinkertn@umich.eduif main['GCC'] or main['CLANG']:
3431049Sbinkertn@umich.edu    # As gcc and clang share many flags, do the common parts here
3442343Sbinkertn@umich.edu    main.Append(CCFLAGS=['-pipe'])
3451049Sbinkertn@umich.edu    main.Append(CCFLAGS=['-fno-strict-aliasing'])
3461049Sbinkertn@umich.edu    # Enable -Wall and -Wextra and then disable the few warnings that
3471049Sbinkertn@umich.edu    # we consistently violate
3481049Sbinkertn@umich.edu    main.Append(CCFLAGS=['-Wall', '-Wundef', '-Wextra',
3491049Sbinkertn@umich.edu                         '-Wno-sign-compare', '-Wno-unused-parameter'])
3501049Sbinkertn@umich.edu    # We always compile using C++11
3511049Sbinkertn@umich.edu    main.Append(CXXFLAGS=['-std=c++11'])
3521049Sbinkertn@umich.edu    if sys.platform.startswith('freebsd'):
3531049Sbinkertn@umich.edu        main.Append(CCFLAGS=['-I/usr/local/include'])
3541049Sbinkertn@umich.edu        main.Append(CXXFLAGS=['-I/usr/local/include'])
3551049Sbinkertn@umich.edu
3561049Sbinkertn@umich.edu    main['FILTER_PSHLINKFLAGS'] = lambda x: str(x).replace(' -shared', '')
3571049Sbinkertn@umich.edu    main['PSHLINKFLAGS'] = main.subst('${FILTER_PSHLINKFLAGS(SHLINKFLAGS)}')
3581049Sbinkertn@umich.edu    main['PLINKFLAGS'] = main.subst('${LINKFLAGS}')
3591049Sbinkertn@umich.edu    shared_partial_flags = ['-r', '-nostdlib']
3601049Sbinkertn@umich.edu    main.Append(PSHLINKFLAGS=shared_partial_flags)
3611049Sbinkertn@umich.edu    main.Append(PLINKFLAGS=shared_partial_flags)
3621049Sbinkertn@umich.eduelse:
3631049Sbinkertn@umich.edu    print termcap.Yellow + termcap.Bold + 'Error' + termcap.Normal,
3641049Sbinkertn@umich.edu    print "Don't know what compiler options to use for your compiler."
3651049Sbinkertn@umich.edu    print termcap.Yellow + '       compiler:' + termcap.Normal, main['CXX']
3661049Sbinkertn@umich.edu    print termcap.Yellow + '       version:' + termcap.Normal,
3671049Sbinkertn@umich.edu    if not CXX_version:
3681049Sbinkertn@umich.edu        print termcap.Yellow + termcap.Bold + "COMMAND NOT FOUND!" +\
3691049Sbinkertn@umich.edu               termcap.Normal
3701049Sbinkertn@umich.edu    else:
3711049Sbinkertn@umich.edu        print CXX_version.replace('\n', '<nl>')
3721049Sbinkertn@umich.edu    print "       If you're trying to use a compiler other than GCC"
3731049Sbinkertn@umich.edu    print "       or clang, there appears to be something wrong with your"
3741049Sbinkertn@umich.edu    print "       environment."
3751049Sbinkertn@umich.edu    print "       "
3762343Sbinkertn@umich.edu    print "       If you are trying to use a compiler other than those listed"
3772343Sbinkertn@umich.edu    print "       above you will need to ease fix SConstruct and "
3782343Sbinkertn@umich.edu    print "       src/SConscript to support that compiler."
3791049Sbinkertn@umich.edu    Exit(1)
3801049Sbinkertn@umich.edu
3812343Sbinkertn@umich.eduif main['GCC']:
3822343Sbinkertn@umich.edu    # Check for a supported version of gcc. >= 4.8 is chosen for its
3832343Sbinkertn@umich.edu    # level of c++11 support. See
3841049Sbinkertn@umich.edu    # http://gcc.gnu.org/projects/cxx0x.html for details.
3851049Sbinkertn@umich.edu    gcc_version = readCommand([main['CXX'], '-dumpversion'], exception=False)
3862343Sbinkertn@umich.edu    if compareVersions(gcc_version, "4.8") < 0:
3872343Sbinkertn@umich.edu        print 'Error: gcc version 4.8 or newer required.'
3882343Sbinkertn@umich.edu        print '       Installed version:', gcc_version
3892343Sbinkertn@umich.edu        Exit(1)
3901049Sbinkertn@umich.edu
3911049Sbinkertn@umich.edu    main['GCC_VERSION'] = gcc_version
3921929Sbinkertn@umich.edu
3931929Sbinkertn@umich.edu    if compareVersions(gcc_version, '4.9') >= 0:
3941049Sbinkertn@umich.edu        # Incremental linking with LTO is currently broken in gcc versions
3951049Sbinkertn@umich.edu        # 4.9 and above. A version where everything works completely hasn't
3961049Sbinkertn@umich.edu        # yet been identified.
3971929Sbinkertn@umich.edu        #
3981049Sbinkertn@umich.edu        # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67548
3991929Sbinkertn@umich.edu        main['BROKEN_INCREMENTAL_LTO'] = True
4001049Sbinkertn@umich.edu    if compareVersions(gcc_version, '6.0') >= 0:
4011929Sbinkertn@umich.edu        # gcc versions 6.0 and greater accept an -flinker-output flag which
4021049Sbinkertn@umich.edu        # selects what type of output the linker should generate. This is
4031049Sbinkertn@umich.edu        # necessary for incremental lto to work, but is also broken in
4041049Sbinkertn@umich.edu        # current versions of gcc. It may not be necessary in future
4052343Sbinkertn@umich.edu        # versions. We add it here since it might be, and as a reminder that
4061049Sbinkertn@umich.edu        # it exists. It's excluded if lto is being forced.
4071049Sbinkertn@umich.edu        #
4082343Sbinkertn@umich.edu        # https://gcc.gnu.org/gcc-6/changes.html
4091049Sbinkertn@umich.edu        # https://gcc.gnu.org/ml/gcc-patches/2015-11/msg03161.html
4101049Sbinkertn@umich.edu        # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69866
4111049Sbinkertn@umich.edu        if not GetOption('force_lto'):
4121929Sbinkertn@umich.edu            main.Append(PSHLINKFLAGS='-flinker-output=rel')
4131929Sbinkertn@umich.edu            main.Append(PLINKFLAGS='-flinker-output=rel')
4141049Sbinkertn@umich.edu
4151049Sbinkertn@umich.edu    # gcc from version 4.8 and above generates "rep; ret" instructions
4161049Sbinkertn@umich.edu    # to avoid performance penalties on certain AMD chips. Older
4171049Sbinkertn@umich.edu    # assemblers detect this as an error, "Error: expecting string
4181049Sbinkertn@umich.edu    # instruction after `rep'"
4191049Sbinkertn@umich.edu    as_version_raw = readCommand([main['AS'], '-v', '/dev/null',
4201049Sbinkertn@umich.edu                                  '-o', '/dev/null'],
4211929Sbinkertn@umich.edu                                 exception=False).split()
4221929Sbinkertn@umich.edu
4231049Sbinkertn@umich.edu    # version strings may contain extra distro-specific
4241929Sbinkertn@umich.edu    # qualifiers, so play it safe and keep only what comes before
4251929Sbinkertn@umich.edu    # the first hyphen
4261929Sbinkertn@umich.edu    as_version = as_version_raw[-1].split('-')[0] if as_version_raw else None
4271929Sbinkertn@umich.edu
4281929Sbinkertn@umich.edu    if not as_version or compareVersions(as_version, "2.23") < 0:
4291929Sbinkertn@umich.edu        print termcap.Yellow + termcap.Bold + \
4301929Sbinkertn@umich.edu            'Warning: This combination of gcc and binutils have' + \
4311929Sbinkertn@umich.edu            ' known incompatibilities.\n' + \
4321881Sbinkertn@umich.edu            '         If you encounter build problems, please update ' + \
4331881Sbinkertn@umich.edu            'binutils to 2.23.' + \
4341881Sbinkertn@umich.edu            termcap.Normal
435
436    # Make sure we warn if the user has requested to compile with the
437    # Undefined Benahvior Sanitizer and this version of gcc does not
438    # support it.
439    if GetOption('with_ubsan') and \
440            compareVersions(gcc_version, '4.9') < 0:
441        print termcap.Yellow + termcap.Bold + \
442            'Warning: UBSan is only supported using gcc 4.9 and later.' + \
443            termcap.Normal
444
445    disable_lto = GetOption('no_lto')
446    if not disable_lto and main.get('BROKEN_INCREMENTAL_LTO', False) and \
447            not GetOption('force_lto'):
448        print termcap.Yellow + termcap.Bold + \
449            'Warning: Your compiler doesn\'t support incremental linking' + \
450            ' and lto at the same time, so lto is being disabled. To force' + \
451            ' lto on anyway, use the --force-lto option. That will disable' + \
452            ' partial linking.' + \
453            termcap.Normal
454        disable_lto = True
455
456    # Add the appropriate Link-Time Optimization (LTO) flags
457    # unless LTO is explicitly turned off. Note that these flags
458    # are only used by the fast target.
459    if not disable_lto:
460        # Pass the LTO flag when compiling to produce GIMPLE
461        # output, we merely create the flags here and only append
462        # them later
463        main['LTO_CCFLAGS'] = ['-flto=%d' % GetOption('num_jobs')]
464
465        # Use the same amount of jobs for LTO as we are running
466        # scons with
467        main['LTO_LDFLAGS'] = ['-flto=%d' % GetOption('num_jobs')]
468
469    main.Append(TCMALLOC_CCFLAGS=['-fno-builtin-malloc', '-fno-builtin-calloc',
470                                  '-fno-builtin-realloc', '-fno-builtin-free'])
471
472    # add option to check for undeclared overrides
473    if compareVersions(gcc_version, "5.0") > 0:
474        main.Append(CCFLAGS=['-Wno-error=suggest-override'])
475
476elif main['CLANG']:
477    # Check for a supported version of clang, >= 3.1 is needed to
478    # support similar features as gcc 4.8. See
479    # http://clang.llvm.org/cxx_status.html for details
480    clang_version_re = re.compile(".* version (\d+\.\d+)")
481    clang_version_match = clang_version_re.search(CXX_version)
482    if (clang_version_match):
483        clang_version = clang_version_match.groups()[0]
484        if compareVersions(clang_version, "3.1") < 0:
485            print 'Error: clang version 3.1 or newer required.'
486            print '       Installed version:', clang_version
487            Exit(1)
488    else:
489        print 'Error: Unable to determine clang version.'
490        Exit(1)
491
492    # clang has a few additional warnings that we disable, extraneous
493    # parantheses are allowed due to Ruby's printing of the AST,
494    # finally self assignments are allowed as the generated CPU code
495    # is relying on this
496    main.Append(CCFLAGS=['-Wno-parentheses',
497                         '-Wno-self-assign',
498                         # Some versions of libstdc++ (4.8?) seem to
499                         # use struct hash and class hash
500                         # interchangeably.
501                         '-Wno-mismatched-tags',
502                         ])
503
504    main.Append(TCMALLOC_CCFLAGS=['-fno-builtin'])
505
506    # On Mac OS X/Darwin we need to also use libc++ (part of XCode) as
507    # opposed to libstdc++, as the later is dated.
508    if sys.platform == "darwin":
509        main.Append(CXXFLAGS=['-stdlib=libc++'])
510        main.Append(LIBS=['c++'])
511
512    # On FreeBSD we need libthr.
513    if sys.platform.startswith('freebsd'):
514        main.Append(LIBS=['thr'])
515
516else:
517    print termcap.Yellow + termcap.Bold + 'Error' + termcap.Normal,
518    print "Don't know what compiler options to use for your compiler."
519    print termcap.Yellow + '       compiler:' + termcap.Normal, main['CXX']
520    print termcap.Yellow + '       version:' + termcap.Normal,
521    if not CXX_version:
522        print termcap.Yellow + termcap.Bold + "COMMAND NOT FOUND!" +\
523               termcap.Normal
524    else:
525        print CXX_version.replace('\n', '<nl>')
526    print "       If you're trying to use a compiler other than GCC"
527    print "       or clang, there appears to be something wrong with your"
528    print "       environment."
529    print "       "
530    print "       If you are trying to use a compiler other than those listed"
531    print "       above you will need to ease fix SConstruct and "
532    print "       src/SConscript to support that compiler."
533    Exit(1)
534
535# Set up common yacc/bison flags (needed for Ruby)
536main['YACCFLAGS'] = '-d'
537main['YACCHXXFILESUFFIX'] = '.hh'
538
539# Do this after we save setting back, or else we'll tack on an
540# extra 'qdo' every time we run scons.
541if main['BATCH']:
542    main['CC']     = main['BATCH_CMD'] + ' ' + main['CC']
543    main['CXX']    = main['BATCH_CMD'] + ' ' + main['CXX']
544    main['AS']     = main['BATCH_CMD'] + ' ' + main['AS']
545    main['AR']     = main['BATCH_CMD'] + ' ' + main['AR']
546    main['RANLIB'] = main['BATCH_CMD'] + ' ' + main['RANLIB']
547
548if sys.platform == 'cygwin':
549    # cygwin has some header file issues...
550    main.Append(CCFLAGS=["-Wno-uninitialized"])
551
552# Check for the protobuf compiler
553protoc_version = readCommand([main['PROTOC'], '--version'],
554                             exception='').split()
555
556# First two words should be "libprotoc x.y.z"
557if len(protoc_version) < 2 or protoc_version[0] != 'libprotoc':
558    print termcap.Yellow + termcap.Bold + \
559        'Warning: Protocol buffer compiler (protoc) not found.\n' + \
560        '         Please install protobuf-compiler for tracing support.' + \
561        termcap.Normal
562    main['PROTOC'] = False
563else:
564    # Based on the availability of the compress stream wrappers,
565    # require 2.1.0
566    min_protoc_version = '2.1.0'
567    if compareVersions(protoc_version[1], min_protoc_version) < 0:
568        print termcap.Yellow + termcap.Bold + \
569            'Warning: protoc version', min_protoc_version, \
570            'or newer required.\n' + \
571            '         Installed version:', protoc_version[1], \
572            termcap.Normal
573        main['PROTOC'] = False
574    else:
575        # Attempt to determine the appropriate include path and
576        # library path using pkg-config, that means we also need to
577        # check for pkg-config. Note that it is possible to use
578        # protobuf without the involvement of pkg-config. Later on we
579        # check go a library config check and at that point the test
580        # will fail if libprotobuf cannot be found.
581        if readCommand(['pkg-config', '--version'], exception=''):
582            try:
583                # Attempt to establish what linking flags to add for protobuf
584                # using pkg-config
585                main.ParseConfig('pkg-config --cflags --libs-only-L protobuf')
586            except:
587                print termcap.Yellow + termcap.Bold + \
588                    'Warning: pkg-config could not get protobuf flags.' + \
589                    termcap.Normal
590
591
592# Check for 'timeout' from GNU coreutils. If present, regressions will
593# be run with a time limit. We require version 8.13 since we rely on
594# support for the '--foreground' option.
595if sys.platform.startswith('freebsd'):
596    timeout_lines = readCommand(['gtimeout', '--version'],
597                                exception='').splitlines()
598else:
599    timeout_lines = readCommand(['timeout', '--version'],
600                                exception='').splitlines()
601# Get the first line and tokenize it
602timeout_version = timeout_lines[0].split() if timeout_lines else []
603main['TIMEOUT'] =  timeout_version and \
604    compareVersions(timeout_version[-1], '8.13') >= 0
605
606# Add a custom Check function to test for structure members.
607def CheckMember(context, include, decl, member, include_quotes="<>"):
608    context.Message("Checking for member %s in %s..." %
609                    (member, decl))
610    text = """
611#include %(header)s
612int main(){
613  %(decl)s test;
614  (void)test.%(member)s;
615  return 0;
616};
617""" % { "header" : include_quotes[0] + include + include_quotes[1],
618        "decl" : decl,
619        "member" : member,
620        }
621
622    ret = context.TryCompile(text, extension=".cc")
623    context.Result(ret)
624    return ret
625
626# Platform-specific configuration.  Note again that we assume that all
627# builds under a given build root run on the same host platform.
628conf = Configure(main,
629                 conf_dir = joinpath(build_root, '.scons_config'),
630                 log_file = joinpath(build_root, 'scons_config.log'),
631                 custom_tests = {
632        'CheckMember' : CheckMember,
633        })
634
635# Check if we should compile a 64 bit binary on Mac OS X/Darwin
636try:
637    import platform
638    uname = platform.uname()
639    if uname[0] == 'Darwin' and compareVersions(uname[2], '9.0.0') >= 0:
640        if int(readCommand('sysctl -n hw.cpu64bit_capable')[0]):
641            main.Append(CCFLAGS=['-arch', 'x86_64'])
642            main.Append(CFLAGS=['-arch', 'x86_64'])
643            main.Append(LINKFLAGS=['-arch', 'x86_64'])
644            main.Append(ASFLAGS=['-arch', 'x86_64'])
645except:
646    pass
647
648# Recent versions of scons substitute a "Null" object for Configure()
649# when configuration isn't necessary, e.g., if the "--help" option is
650# present.  Unfortuantely this Null object always returns false,
651# breaking all our configuration checks.  We replace it with our own
652# more optimistic null object that returns True instead.
653if not conf:
654    def NullCheck(*args, **kwargs):
655        return True
656
657    class NullConf:
658        def __init__(self, env):
659            self.env = env
660        def Finish(self):
661            return self.env
662        def __getattr__(self, mname):
663            return NullCheck
664
665    conf = NullConf(main)
666
667# Cache build files in the supplied directory.
668if main['M5_BUILD_CACHE']:
669    print 'Using build cache located at', main['M5_BUILD_CACHE']
670    CacheDir(main['M5_BUILD_CACHE'])
671
672main['USE_PYTHON'] = not GetOption('without_python')
673if main['USE_PYTHON']:
674    # Find Python include and library directories for embedding the
675    # interpreter. We rely on python-config to resolve the appropriate
676    # includes and linker flags. ParseConfig does not seem to understand
677    # the more exotic linker flags such as -Xlinker and -export-dynamic so
678    # we add them explicitly below. If you want to link in an alternate
679    # version of python, see above for instructions on how to invoke
680    # scons with the appropriate PATH set.
681    #
682    # First we check if python2-config exists, else we use python-config
683    python_config = readCommand(['which', 'python2-config'],
684                                exception='').strip()
685    if not os.path.exists(python_config):
686        python_config = readCommand(['which', 'python-config'],
687                                    exception='').strip()
688    py_includes = readCommand([python_config, '--includes'],
689                              exception='').split()
690    # Strip the -I from the include folders before adding them to the
691    # CPPPATH
692    main.Append(CPPPATH=map(lambda inc: inc[2:], py_includes))
693
694    # Read the linker flags and split them into libraries and other link
695    # flags. The libraries are added later through the call the CheckLib.
696    py_ld_flags = readCommand([python_config, '--ldflags'],
697        exception='').split()
698    py_libs = []
699    for lib in py_ld_flags:
700         if not lib.startswith('-l'):
701             main.Append(LINKFLAGS=[lib])
702         else:
703             lib = lib[2:]
704             if lib not in py_libs:
705                 py_libs.append(lib)
706
707    # verify that this stuff works
708    if not conf.CheckHeader('Python.h', '<>'):
709        print "Error: can't find Python.h header in", py_includes
710        print "Install Python headers (package python-dev on Ubuntu and RedHat)"
711        Exit(1)
712
713    for lib in py_libs:
714        if not conf.CheckLib(lib):
715            print "Error: can't find library %s required by python" % lib
716            Exit(1)
717
718# On Solaris you need to use libsocket for socket ops
719if not conf.CheckLibWithHeader(None, 'sys/socket.h', 'C++', 'accept(0,0,0);'):
720   if not conf.CheckLibWithHeader('socket', 'sys/socket.h', 'C++', 'accept(0,0,0);'):
721       print "Can't find library with socket calls (e.g. accept())"
722       Exit(1)
723
724# Check for zlib.  If the check passes, libz will be automatically
725# added to the LIBS environment variable.
726if not conf.CheckLibWithHeader('z', 'zlib.h', 'C++','zlibVersion();'):
727    print 'Error: did not find needed zlib compression library '\
728          'and/or zlib.h header file.'
729    print '       Please install zlib and try again.'
730    Exit(1)
731
732# If we have the protobuf compiler, also make sure we have the
733# development libraries. If the check passes, libprotobuf will be
734# automatically added to the LIBS environment variable. After
735# this, we can use the HAVE_PROTOBUF flag to determine if we have
736# got both protoc and libprotobuf available.
737main['HAVE_PROTOBUF'] = main['PROTOC'] and \
738    conf.CheckLibWithHeader('protobuf', 'google/protobuf/message.h',
739                            'C++', 'GOOGLE_PROTOBUF_VERIFY_VERSION;')
740
741# If we have the compiler but not the library, print another warning.
742if main['PROTOC'] and not main['HAVE_PROTOBUF']:
743    print termcap.Yellow + termcap.Bold + \
744        'Warning: did not find protocol buffer library and/or headers.\n' + \
745    '       Please install libprotobuf-dev for tracing support.' + \
746    termcap.Normal
747
748# Check for librt.
749have_posix_clock = \
750    conf.CheckLibWithHeader(None, 'time.h', 'C',
751                            'clock_nanosleep(0,0,NULL,NULL);') or \
752    conf.CheckLibWithHeader('rt', 'time.h', 'C',
753                            'clock_nanosleep(0,0,NULL,NULL);')
754
755have_posix_timers = \
756    conf.CheckLibWithHeader([None, 'rt'], [ 'time.h', 'signal.h' ], 'C',
757                            'timer_create(CLOCK_MONOTONIC, NULL, NULL);')
758
759if not GetOption('without_tcmalloc'):
760    if conf.CheckLib('tcmalloc'):
761        main.Append(CCFLAGS=main['TCMALLOC_CCFLAGS'])
762    elif conf.CheckLib('tcmalloc_minimal'):
763        main.Append(CCFLAGS=main['TCMALLOC_CCFLAGS'])
764    else:
765        print termcap.Yellow + termcap.Bold + \
766              "You can get a 12% performance improvement by "\
767              "installing tcmalloc (libgoogle-perftools-dev package "\
768              "on Ubuntu or RedHat)." + termcap.Normal
769
770
771# Detect back trace implementations. The last implementation in the
772# list will be used by default.
773backtrace_impls = [ "none" ]
774
775if conf.CheckLibWithHeader(None, 'execinfo.h', 'C',
776                           'backtrace_symbols_fd((void*)0, 0, 0);'):
777    backtrace_impls.append("glibc")
778elif conf.CheckLibWithHeader('execinfo', 'execinfo.h', 'C',
779                           'backtrace_symbols_fd((void*)0, 0, 0);'):
780    # NetBSD and FreeBSD need libexecinfo.
781    backtrace_impls.append("glibc")
782    main.Append(LIBS=['execinfo'])
783
784if backtrace_impls[-1] == "none":
785    default_backtrace_impl = "none"
786    print termcap.Yellow + termcap.Bold + \
787        "No suitable back trace implementation found." + \
788        termcap.Normal
789
790if not have_posix_clock:
791    print "Can't find library for POSIX clocks."
792
793# Check for <fenv.h> (C99 FP environment control)
794have_fenv = conf.CheckHeader('fenv.h', '<>')
795if not have_fenv:
796    print "Warning: Header file <fenv.h> not found."
797    print "         This host has no IEEE FP rounding mode control."
798
799# Check for <png.h> (libpng library needed if wanting to dump
800# frame buffer image in png format)
801have_png = conf.CheckHeader('png.h', '<>')
802if not have_png:
803    print "Warning: Header file <png.h> not found."
804    print "         This host has no libpng library."
805    print "         Disabling support for PNG framebuffers."
806
807# Check if we should enable KVM-based hardware virtualization. The API
808# we rely on exists since version 2.6.36 of the kernel, but somehow
809# the KVM_API_VERSION does not reflect the change. We test for one of
810# the types as a fall back.
811have_kvm = conf.CheckHeader('linux/kvm.h', '<>')
812if not have_kvm:
813    print "Info: Compatible header file <linux/kvm.h> not found, " \
814        "disabling KVM support."
815
816# Check if the TUN/TAP driver is available.
817have_tuntap = conf.CheckHeader('linux/if_tun.h', '<>')
818if not have_tuntap:
819    print "Info: Compatible header file <linux/if_tun.h> not found."
820
821# x86 needs support for xsave. We test for the structure here since we
822# won't be able to run new tests by the time we know which ISA we're
823# targeting.
824have_kvm_xsave = conf.CheckTypeSize('struct kvm_xsave',
825                                    '#include <linux/kvm.h>') != 0
826
827# Check if the requested target ISA is compatible with the host
828def is_isa_kvm_compatible(isa):
829    try:
830        import platform
831        host_isa = platform.machine()
832    except:
833        print "Warning: Failed to determine host ISA."
834        return False
835
836    if not have_posix_timers:
837        print "Warning: Can not enable KVM, host seems to lack support " \
838            "for POSIX timers"
839        return False
840
841    if isa == "arm":
842        return host_isa in ( "armv7l", "aarch64" )
843    elif isa == "x86":
844        if host_isa != "x86_64":
845            return False
846
847        if not have_kvm_xsave:
848            print "KVM on x86 requires xsave support in kernel headers."
849            return False
850
851        return True
852    else:
853        return False
854
855
856# Check if the exclude_host attribute is available. We want this to
857# get accurate instruction counts in KVM.
858main['HAVE_PERF_ATTR_EXCLUDE_HOST'] = conf.CheckMember(
859    'linux/perf_event.h', 'struct perf_event_attr', 'exclude_host')
860
861
862######################################################################
863#
864# Finish the configuration
865#
866main = conf.Finish()
867
868######################################################################
869#
870# Collect all non-global variables
871#
872
873# Define the universe of supported ISAs
874all_isa_list = [ ]
875all_gpu_isa_list = [ ]
876Export('all_isa_list')
877Export('all_gpu_isa_list')
878
879class CpuModel(object):
880    '''The CpuModel class encapsulates everything the ISA parser needs to
881    know about a particular CPU model.'''
882
883    # Dict of available CPU model objects.  Accessible as CpuModel.dict.
884    dict = {}
885
886    # Constructor.  Automatically adds models to CpuModel.dict.
887    def __init__(self, name, default=False):
888        self.name = name           # name of model
889
890        # This cpu is enabled by default
891        self.default = default
892
893        # Add self to dict
894        if name in CpuModel.dict:
895            raise AttributeError, "CpuModel '%s' already registered" % name
896        CpuModel.dict[name] = self
897
898Export('CpuModel')
899
900# Sticky variables get saved in the variables file so they persist from
901# one invocation to the next (unless overridden, in which case the new
902# value becomes sticky).
903sticky_vars = Variables(args=ARGUMENTS)
904Export('sticky_vars')
905
906# Sticky variables that should be exported
907export_vars = []
908Export('export_vars')
909
910# For Ruby
911all_protocols = []
912Export('all_protocols')
913protocol_dirs = []
914Export('protocol_dirs')
915slicc_includes = []
916Export('slicc_includes')
917
918# Walk the tree and execute all SConsopts scripts that wil add to the
919# above variables
920if GetOption('verbose'):
921    print "Reading SConsopts"
922for bdir in [ base_dir ] + extras_dir_list:
923    if not isdir(bdir):
924        print "Error: directory '%s' does not exist" % bdir
925        Exit(1)
926    for root, dirs, files in os.walk(bdir):
927        if 'SConsopts' in files:
928            if GetOption('verbose'):
929                print "Reading", joinpath(root, 'SConsopts')
930            SConscript(joinpath(root, 'SConsopts'))
931
932all_isa_list.sort()
933all_gpu_isa_list.sort()
934
935sticky_vars.AddVariables(
936    EnumVariable('TARGET_ISA', 'Target ISA', 'alpha', all_isa_list),
937    EnumVariable('TARGET_GPU_ISA', 'Target GPU ISA', 'hsail', all_gpu_isa_list),
938    ListVariable('CPU_MODELS', 'CPU models',
939                 sorted(n for n,m in CpuModel.dict.iteritems() if m.default),
940                 sorted(CpuModel.dict.keys())),
941    BoolVariable('EFENCE', 'Link with Electric Fence malloc debugger',
942                 False),
943    BoolVariable('SS_COMPATIBLE_FP',
944                 'Make floating-point results compatible with SimpleScalar',
945                 False),
946    BoolVariable('USE_SSE2',
947                 'Compile for SSE2 (-msse2) to get IEEE FP on x86 hosts',
948                 False),
949    BoolVariable('USE_POSIX_CLOCK', 'Use POSIX Clocks', have_posix_clock),
950    BoolVariable('USE_FENV', 'Use <fenv.h> IEEE mode control', have_fenv),
951    BoolVariable('USE_PNG',  'Enable support for PNG images', have_png),
952    BoolVariable('CP_ANNOTATE', 'Enable critical path annotation capability',
953                 False),
954    BoolVariable('USE_KVM', 'Enable hardware virtualized (KVM) CPU models',
955                 have_kvm),
956    BoolVariable('USE_TUNTAP',
957                 'Enable using a tap device to bridge to the host network',
958                 have_tuntap),
959    BoolVariable('BUILD_GPU', 'Build the compute-GPU model', False),
960    EnumVariable('PROTOCOL', 'Coherence protocol for Ruby', 'None',
961                  all_protocols),
962    EnumVariable('BACKTRACE_IMPL', 'Post-mortem dump implementation',
963                 backtrace_impls[-1], backtrace_impls)
964    )
965
966# These variables get exported to #defines in config/*.hh (see src/SConscript).
967export_vars += ['USE_FENV', 'SS_COMPATIBLE_FP', 'TARGET_ISA', 'TARGET_GPU_ISA',
968                'CP_ANNOTATE', 'USE_POSIX_CLOCK', 'USE_KVM', 'USE_TUNTAP',
969                'PROTOCOL', 'HAVE_PROTOBUF', 'HAVE_PERF_ATTR_EXCLUDE_HOST',
970                'USE_PNG']
971
972###################################################
973#
974# Define a SCons builder for configuration flag headers.
975#
976###################################################
977
978# This function generates a config header file that #defines the
979# variable symbol to the current variable setting (0 or 1).  The source
980# operands are the name of the variable and a Value node containing the
981# value of the variable.
982def build_config_file(target, source, env):
983    (variable, value) = [s.get_contents() for s in source]
984    f = file(str(target[0]), 'w')
985    print >> f, '#define', variable, value
986    f.close()
987    return None
988
989# Combine the two functions into a scons Action object.
990config_action = MakeAction(build_config_file, Transform("CONFIG H", 2))
991
992# The emitter munges the source & target node lists to reflect what
993# we're really doing.
994def config_emitter(target, source, env):
995    # extract variable name from Builder arg
996    variable = str(target[0])
997    # True target is config header file
998    target = joinpath('config', variable.lower() + '.hh')
999    val = env[variable]
1000    if isinstance(val, bool):
1001        # Force value to 0/1
1002        val = int(val)
1003    elif isinstance(val, str):
1004        val = '"' + val + '"'
1005
1006    # Sources are variable name & value (packaged in SCons Value nodes)
1007    return ([target], [Value(variable), Value(val)])
1008
1009config_builder = Builder(emitter = config_emitter, action = config_action)
1010
1011main.Append(BUILDERS = { 'ConfigFile' : config_builder })
1012
1013###################################################
1014#
1015# Builders for static and shared partially linked object files.
1016#
1017###################################################
1018
1019partial_static_builder = Builder(action=SCons.Defaults.LinkAction,
1020                                 src_suffix='$OBJSUFFIX',
1021                                 src_builder=['StaticObject', 'Object'],
1022                                 LINKFLAGS='$PLINKFLAGS',
1023                                 LIBS='')
1024
1025def partial_shared_emitter(target, source, env):
1026    for tgt in target:
1027        tgt.attributes.shared = 1
1028    return (target, source)
1029partial_shared_builder = Builder(action=SCons.Defaults.ShLinkAction,
1030                                 emitter=partial_shared_emitter,
1031                                 src_suffix='$SHOBJSUFFIX',
1032                                 src_builder='SharedObject',
1033                                 SHLINKFLAGS='$PSHLINKFLAGS',
1034                                 LIBS='')
1035
1036main.Append(BUILDERS = { 'PartialShared' : partial_shared_builder,
1037                         'PartialStatic' : partial_static_builder })
1038
1039# builds in ext are shared across all configs in the build root.
1040ext_dir = abspath(joinpath(str(main.root), 'ext'))
1041ext_build_dirs = []
1042for root, dirs, files in os.walk(ext_dir):
1043    if 'SConscript' in files:
1044        build_dir = os.path.relpath(root, ext_dir)
1045        ext_build_dirs.append(build_dir)
1046        main.SConscript(joinpath(root, 'SConscript'),
1047                        variant_dir=joinpath(build_root, build_dir))
1048
1049main.Prepend(CPPPATH=Dir('ext/pybind11/include/'))
1050
1051###################################################
1052#
1053# This builder and wrapper method are used to set up a directory with
1054# switching headers. Those are headers which are in a generic location and
1055# that include more specific headers from a directory chosen at build time
1056# based on the current build settings.
1057#
1058###################################################
1059
1060def build_switching_header(target, source, env):
1061    path = str(target[0])
1062    subdir = str(source[0])
1063    dp, fp = os.path.split(path)
1064    dp = os.path.relpath(os.path.realpath(dp),
1065                         os.path.realpath(env['BUILDDIR']))
1066    with open(path, 'w') as hdr:
1067        print >>hdr, '#include "%s/%s/%s"' % (dp, subdir, fp)
1068
1069switching_header_action = MakeAction(build_switching_header,
1070                                     Transform('GENERATE'))
1071
1072switching_header_builder = Builder(action=switching_header_action,
1073                                   source_factory=Value,
1074                                   single_source=True)
1075
1076main.Append(BUILDERS = { 'SwitchingHeader': switching_header_builder })
1077
1078def switching_headers(self, headers, source):
1079    for header in headers:
1080        self.SwitchingHeader(header, source)
1081
1082main.AddMethod(switching_headers, 'SwitchingHeaders')
1083
1084###################################################
1085#
1086# Define build environments for selected configurations.
1087#
1088###################################################
1089
1090for variant_path in variant_paths:
1091    if not GetOption('silent'):
1092        print "Building in", variant_path
1093
1094    # Make a copy of the build-root environment to use for this config.
1095    env = main.Clone()
1096    env['BUILDDIR'] = variant_path
1097
1098    # variant_dir is the tail component of build path, and is used to
1099    # determine the build parameters (e.g., 'ALPHA_SE')
1100    (build_root, variant_dir) = splitpath(variant_path)
1101
1102    # Set env variables according to the build directory config.
1103    sticky_vars.files = []
1104    # Variables for $BUILD_ROOT/$VARIANT_DIR are stored in
1105    # $BUILD_ROOT/variables/$VARIANT_DIR so you can nuke
1106    # $BUILD_ROOT/$VARIANT_DIR without losing your variables settings.
1107    current_vars_file = joinpath(build_root, 'variables', variant_dir)
1108    if isfile(current_vars_file):
1109        sticky_vars.files.append(current_vars_file)
1110        if not GetOption('silent'):
1111            print "Using saved variables file %s" % current_vars_file
1112    elif variant_dir in ext_build_dirs:
1113        # Things in ext are built without a variant directory.
1114        continue
1115    else:
1116        # Build dir-specific variables file doesn't exist.
1117
1118        # Make sure the directory is there so we can create it later
1119        opt_dir = dirname(current_vars_file)
1120        if not isdir(opt_dir):
1121            mkdir(opt_dir)
1122
1123        # Get default build variables from source tree.  Variables are
1124        # normally determined by name of $VARIANT_DIR, but can be
1125        # overridden by '--default=' arg on command line.
1126        default = GetOption('default')
1127        opts_dir = joinpath(main.root.abspath, 'build_opts')
1128        if default:
1129            default_vars_files = [joinpath(build_root, 'variables', default),
1130                                  joinpath(opts_dir, default)]
1131        else:
1132            default_vars_files = [joinpath(opts_dir, variant_dir)]
1133        existing_files = filter(isfile, default_vars_files)
1134        if existing_files:
1135            default_vars_file = existing_files[0]
1136            sticky_vars.files.append(default_vars_file)
1137            print "Variables file %s not found,\n  using defaults in %s" \
1138                  % (current_vars_file, default_vars_file)
1139        else:
1140            print "Error: cannot find variables file %s or " \
1141                  "default file(s) %s" \
1142                  % (current_vars_file, ' or '.join(default_vars_files))
1143            Exit(1)
1144
1145    # Apply current variable settings to env
1146    sticky_vars.Update(env)
1147
1148    help_texts["local_vars"] += \
1149        "Build variables for %s:\n" % variant_dir \
1150                 + sticky_vars.GenerateHelpText(env)
1151
1152    # Process variable settings.
1153
1154    if not have_fenv and env['USE_FENV']:
1155        print "Warning: <fenv.h> not available; " \
1156              "forcing USE_FENV to False in", variant_dir + "."
1157        env['USE_FENV'] = False
1158
1159    if not env['USE_FENV']:
1160        print "Warning: No IEEE FP rounding mode control in", variant_dir + "."
1161        print "         FP results may deviate slightly from other platforms."
1162
1163    if not have_png and env['USE_PNG']:
1164        print "Warning: <png.h> not available; " \
1165              "forcing USE_PNG to False in", variant_dir + "."
1166        env['USE_PNG'] = False
1167
1168    if env['USE_PNG']:
1169        env.Append(LIBS=['png'])
1170
1171    if env['EFENCE']:
1172        env.Append(LIBS=['efence'])
1173
1174    if env['USE_KVM']:
1175        if not have_kvm:
1176            print "Warning: Can not enable KVM, host seems to lack KVM support"
1177            env['USE_KVM'] = False
1178        elif not is_isa_kvm_compatible(env['TARGET_ISA']):
1179            print "Info: KVM support disabled due to unsupported host and " \
1180                "target ISA combination"
1181            env['USE_KVM'] = False
1182
1183    if env['USE_TUNTAP']:
1184        if not have_tuntap:
1185            print "Warning: Can't connect EtherTap with a tap device."
1186            env['USE_TUNTAP'] = False
1187
1188    if env['BUILD_GPU']:
1189        env.Append(CPPDEFINES=['BUILD_GPU'])
1190
1191    # Warn about missing optional functionality
1192    if env['USE_KVM']:
1193        if not main['HAVE_PERF_ATTR_EXCLUDE_HOST']:
1194            print "Warning: perf_event headers lack support for the " \
1195                "exclude_host attribute. KVM instruction counts will " \
1196                "be inaccurate."
1197
1198    # Save sticky variable settings back to current variables file
1199    sticky_vars.Save(current_vars_file, env)
1200
1201    if env['USE_SSE2']:
1202        env.Append(CCFLAGS=['-msse2'])
1203
1204    # The src/SConscript file sets up the build rules in 'env' according
1205    # to the configured variables.  It returns a list of environments,
1206    # one for each variant build (debug, opt, etc.)
1207    SConscript('src/SConscript', variant_dir = variant_path, exports = 'env')
1208
1209# base help text
1210Help('''
1211Usage: scons [scons options] [build variables] [target(s)]
1212
1213Extra scons options:
1214%(options)s
1215
1216Global build variables:
1217%(global_vars)s
1218
1219%(local_vars)s
1220''' % help_texts)
1221