SConstruct revision 12245:ad6fa75d2aba
112854Sgabeblack@google.com# -*- mode:python -*-
212854Sgabeblack@google.com
312854Sgabeblack@google.com# Copyright (c) 2013, 2015-2017 ARM Limited
412854Sgabeblack@google.com# All rights reserved.
512854Sgabeblack@google.com#
612854Sgabeblack@google.com# The license below extends only to copyright in the software and shall
712854Sgabeblack@google.com# not be construed as granting a license to any other intellectual
812854Sgabeblack@google.com# property including but not limited to intellectual property relating
912854Sgabeblack@google.com# to a hardware implementation of the functionality of the software
1012854Sgabeblack@google.com# licensed hereunder.  You may use the software subject to the license
1112854Sgabeblack@google.com# terms below provided that you ensure that this notice is replicated
1212854Sgabeblack@google.com# unmodified and in its entirety in all distributions of the software,
1312854Sgabeblack@google.com# modified or unmodified, in source code or in binary form.
1412854Sgabeblack@google.com#
1512854Sgabeblack@google.com# Copyright (c) 2011 Advanced Micro Devices, Inc.
1612854Sgabeblack@google.com# Copyright (c) 2009 The Hewlett-Packard Development Company
1712854Sgabeblack@google.com# Copyright (c) 2004-2005 The Regents of The University of Michigan
1812854Sgabeblack@google.com# All rights reserved.
1912854Sgabeblack@google.com#
2012854Sgabeblack@google.com# Redistribution and use in source and binary forms, with or without
2112854Sgabeblack@google.com# modification, are permitted provided that the following conditions are
2212854Sgabeblack@google.com# met: redistributions of source code must retain the above copyright
2312854Sgabeblack@google.com# notice, this list of conditions and the following disclaimer;
2412854Sgabeblack@google.com# redistributions in binary form must reproduce the above copyright
2512854Sgabeblack@google.com# notice, this list of conditions and the following disclaimer in the
2612854Sgabeblack@google.com# documentation and/or other materials provided with the distribution;
2712854Sgabeblack@google.com# neither the name of the copyright holders nor the names of its
2812854Sgabeblack@google.com# contributors may be used to endorse or promote products derived from
2912854Sgabeblack@google.com# this software without specific prior written permission.
3012854Sgabeblack@google.com#
3112854Sgabeblack@google.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3212854Sgabeblack@google.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3312854Sgabeblack@google.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3412854Sgabeblack@google.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3512854Sgabeblack@google.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3612854Sgabeblack@google.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3712854Sgabeblack@google.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3812854Sgabeblack@google.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3912854Sgabeblack@google.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
4012854Sgabeblack@google.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
4112854Sgabeblack@google.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4212854Sgabeblack@google.com#
4312854Sgabeblack@google.com# Authors: Steve Reinhardt
4412854Sgabeblack@google.com#          Nathan Binkert
4512854Sgabeblack@google.com
4612854Sgabeblack@google.com###################################################
4712854Sgabeblack@google.com#
4812854Sgabeblack@google.com# SCons top-level build description (SConstruct) file.
4912854Sgabeblack@google.com#
5012854Sgabeblack@google.com# While in this directory ('gem5'), just type 'scons' to build the default
5112854Sgabeblack@google.com# configuration (see below), or type 'scons build/<CONFIG>/<binary>'
5212854Sgabeblack@google.com# to build some other configuration (e.g., 'build/ALPHA/gem5.opt' for
5312854Sgabeblack@google.com# the optimized full-system version).
5412854Sgabeblack@google.com#
5512854Sgabeblack@google.com# You can build gem5 in a different directory as long as there is a
5612854Sgabeblack@google.com# 'build/<CONFIG>' somewhere along the target path.  The build system
5712854Sgabeblack@google.com# expects that all configs under the same build directory are being
5812854Sgabeblack@google.com# built for the same host system.
5912854Sgabeblack@google.com#
6012854Sgabeblack@google.com# Examples:
6112854Sgabeblack@google.com#
6212854Sgabeblack@google.com#   The following two commands are equivalent.  The '-u' option tells
6312854Sgabeblack@google.com#   scons to search up the directory tree for this SConstruct file.
6412854Sgabeblack@google.com#   % cd <path-to-src>/gem5 ; scons build/ALPHA/gem5.debug
6512854Sgabeblack@google.com#   % cd <path-to-src>/gem5/build/ALPHA; scons -u gem5.debug
6612854Sgabeblack@google.com#
6712854Sgabeblack@google.com#   The following two commands are equivalent and demonstrate building
6812854Sgabeblack@google.com#   in a directory outside of the source tree.  The '-C' option tells
6912854Sgabeblack@google.com#   scons to chdir to the specified directory to find this SConstruct
7012854Sgabeblack@google.com#   file.
7112854Sgabeblack@google.com#   % cd <path-to-src>/gem5 ; scons /local/foo/build/ALPHA/gem5.debug
7212854Sgabeblack@google.com#   % cd /local/foo/build/ALPHA; scons -C <path-to-src>/gem5 gem5.debug
7312854Sgabeblack@google.com#
7412854Sgabeblack@google.com# You can use 'scons -H' to print scons options.  If you're in this
7512854Sgabeblack@google.com# 'gem5' directory (or use -u or -C to tell scons where to find this
7612854Sgabeblack@google.com# file), you can use 'scons -h' to print all the gem5-specific build
7712854Sgabeblack@google.com# options as well.
7812854Sgabeblack@google.com#
7912854Sgabeblack@google.com###################################################
8012854Sgabeblack@google.com
8112854Sgabeblack@google.com# Global Python includes
8212854Sgabeblack@google.comimport itertools
8312854Sgabeblack@google.comimport os
8412854Sgabeblack@google.comimport re
8512854Sgabeblack@google.comimport shutil
8612854Sgabeblack@google.comimport subprocess
8712854Sgabeblack@google.comimport sys
8812854Sgabeblack@google.com
8912854Sgabeblack@google.comfrom os import mkdir, environ
9012854Sgabeblack@google.comfrom os.path import abspath, basename, dirname, expanduser, normpath
9112854Sgabeblack@google.comfrom os.path import exists,  isdir, isfile
9212854Sgabeblack@google.comfrom os.path import join as joinpath, split as splitpath
9312854Sgabeblack@google.com
9412854Sgabeblack@google.com# SCons includes
9512854Sgabeblack@google.comimport SCons
9612854Sgabeblack@google.comimport SCons.Node
9712854Sgabeblack@google.com
9812854Sgabeblack@google.comfrom m5.util import compareVersions, readCommand
9912854Sgabeblack@google.comfrom m5.util.terminal import get_termcap
10012854Sgabeblack@google.com
10112854Sgabeblack@google.comhelp_texts = {
10212854Sgabeblack@google.com    "options" : "",
10312854Sgabeblack@google.com    "global_vars" : "",
10412854Sgabeblack@google.com    "local_vars" : ""
10512854Sgabeblack@google.com}
10612854Sgabeblack@google.com
10712854Sgabeblack@google.comExport("help_texts")
10812854Sgabeblack@google.com
10912854Sgabeblack@google.com
11012854Sgabeblack@google.com# There's a bug in scons in that (1) by default, the help texts from
11112854Sgabeblack@google.com# AddOption() are supposed to be displayed when you type 'scons -h'
11212854Sgabeblack@google.com# and (2) you can override the help displayed by 'scons -h' using the
11312854Sgabeblack@google.com# Help() function, but these two features are incompatible: once
11412854Sgabeblack@google.com# you've overridden the help text using Help(), there's no way to get
11512854Sgabeblack@google.com# at the help texts from AddOptions.  See:
11612854Sgabeblack@google.com#     http://scons.tigris.org/issues/show_bug.cgi?id=2356
11712854Sgabeblack@google.com#     http://scons.tigris.org/issues/show_bug.cgi?id=2611
11812854Sgabeblack@google.com# This hack lets us extract the help text from AddOptions and
11912854Sgabeblack@google.com# re-inject it via Help().  Ideally someday this bug will be fixed and
12012854Sgabeblack@google.com# we can just use AddOption directly.
12112854Sgabeblack@google.comdef AddLocalOption(*args, **kwargs):
12212854Sgabeblack@google.com    col_width = 30
12312854Sgabeblack@google.com
12412854Sgabeblack@google.com    help = "  " + ", ".join(args)
12512854Sgabeblack@google.com    if "help" in kwargs:
12613138Sgabeblack@google.com        length = len(help)
12712854Sgabeblack@google.com        if length >= col_width:
12812854Sgabeblack@google.com            help += "\n" + " " * col_width
12912854Sgabeblack@google.com        else:
13012854Sgabeblack@google.com            help += " " * (col_width - length)
13112854Sgabeblack@google.com        help += kwargs["help"]
13212854Sgabeblack@google.com    help_texts["options"] += help + "\n"
13312854Sgabeblack@google.com
13412854Sgabeblack@google.com    AddOption(*args, **kwargs)
13513160Sgabeblack@google.com
13612854Sgabeblack@google.comAddLocalOption('--colors', dest='use_colors', action='store_true',
13712854Sgabeblack@google.com               help="Add color to abbreviated scons output")
13813138Sgabeblack@google.comAddLocalOption('--no-colors', dest='use_colors', action='store_false',
13912854Sgabeblack@google.com               help="Don't add color to abbreviated scons output")
14012854Sgabeblack@google.comAddLocalOption('--with-cxx-config', dest='with_cxx_config',
14112854Sgabeblack@google.com               action='store_true',
14212854Sgabeblack@google.com               help="Build with support for C++-based configuration")
14312854Sgabeblack@google.comAddLocalOption('--default', dest='default', type='string', action='store',
14412854Sgabeblack@google.com               help='Override which build_opts file to use for defaults')
14512854Sgabeblack@google.comAddLocalOption('--ignore-style', dest='ignore_style', action='store_true',
14612854Sgabeblack@google.com               help='Disable style checking hooks')
14712854Sgabeblack@google.comAddLocalOption('--no-lto', dest='no_lto', action='store_true',
14812854Sgabeblack@google.com               help='Disable Link-Time Optimization for fast')
14912854Sgabeblack@google.comAddLocalOption('--force-lto', dest='force_lto', action='store_true',
15012854Sgabeblack@google.com               help='Use Link-Time Optimization instead of partial linking' +
15112854Sgabeblack@google.com                    ' when the compiler doesn\'t support using them together.')
15212854Sgabeblack@google.comAddLocalOption('--update-ref', dest='update_ref', action='store_true',
15312854Sgabeblack@google.com               help='Update test reference outputs')
15412854Sgabeblack@google.comAddLocalOption('--verbose', dest='verbose', action='store_true',
15512854Sgabeblack@google.com               help='Print full tool command lines')
15612854Sgabeblack@google.comAddLocalOption('--without-python', dest='without_python',
15712854Sgabeblack@google.com               action='store_true',
15812854Sgabeblack@google.com               help='Build without Python configuration support')
15912854Sgabeblack@google.comAddLocalOption('--without-tcmalloc', dest='without_tcmalloc',
16012854Sgabeblack@google.com               action='store_true',
16112854Sgabeblack@google.com               help='Disable linking against tcmalloc')
16212854Sgabeblack@google.comAddLocalOption('--with-ubsan', dest='with_ubsan', action='store_true',
16312854Sgabeblack@google.com               help='Build with Undefined Behavior Sanitizer if available')
16412854Sgabeblack@google.comAddLocalOption('--with-asan', dest='with_asan', action='store_true',
16512854Sgabeblack@google.com               help='Build with Address Sanitizer if available')
16612854Sgabeblack@google.com
16712854Sgabeblack@google.comif GetOption('no_lto') and GetOption('force_lto'):
16812854Sgabeblack@google.com    print '--no-lto and --force-lto are mutually exclusive'
16912854Sgabeblack@google.com    Exit(1)
17012854Sgabeblack@google.com
17112854Sgabeblack@google.comtermcap = get_termcap(GetOption('use_colors'))
17212854Sgabeblack@google.com
17312854Sgabeblack@google.com########################################################################
17412854Sgabeblack@google.com#
17512854Sgabeblack@google.com# Set up the main build environment.
17612854Sgabeblack@google.com#
17712854Sgabeblack@google.com########################################################################
17812854Sgabeblack@google.com
17912854Sgabeblack@google.commain = Environment()
18012854Sgabeblack@google.com
18112854Sgabeblack@google.commain_dict_keys = main.Dictionary().keys()
18212854Sgabeblack@google.com
18312854Sgabeblack@google.com# Check that we have a C/C++ compiler
18412854Sgabeblack@google.comif not ('CC' in main_dict_keys and 'CXX' in main_dict_keys):
18512854Sgabeblack@google.com    print "No C++ compiler installed (package g++ on Ubuntu and RedHat)"
18612854Sgabeblack@google.com    Exit(1)
18712854Sgabeblack@google.com
18812854Sgabeblack@google.com###################################################
18912854Sgabeblack@google.com#
19012854Sgabeblack@google.com# Figure out which configurations to set up based on the path(s) of
19112854Sgabeblack@google.com# the target(s).
19212854Sgabeblack@google.com#
19312854Sgabeblack@google.com###################################################
19412854Sgabeblack@google.com
19512854Sgabeblack@google.com# Find default configuration & binary.
19612854Sgabeblack@google.comDefault(environ.get('M5_DEFAULT_BINARY', 'build/ALPHA/gem5.debug'))
19712854Sgabeblack@google.com
19812854Sgabeblack@google.com# helper function: find last occurrence of element in list
19912854Sgabeblack@google.comdef rfind(l, elt, offs = -1):
20012854Sgabeblack@google.com    for i in range(len(l)+offs, 0, -1):
20112854Sgabeblack@google.com        if l[i] == elt:
20212854Sgabeblack@google.com            return i
20312854Sgabeblack@google.com    raise ValueError, "element not found"
20412854Sgabeblack@google.com
20512854Sgabeblack@google.com# Take a list of paths (or SCons Nodes) and return a list with all
20612854Sgabeblack@google.com# paths made absolute and ~-expanded.  Paths will be interpreted
20712854Sgabeblack@google.com# relative to the launch directory unless a different root is provided
20812854Sgabeblack@google.comdef makePathListAbsolute(path_list, root=GetLaunchDir()):
20912854Sgabeblack@google.com    return [abspath(joinpath(root, expanduser(str(p))))
21012854Sgabeblack@google.com            for p in path_list]
21112854Sgabeblack@google.com
21212854Sgabeblack@google.com# Each target must have 'build' in the interior of the path; the
21312854Sgabeblack@google.com# directory below this will determine the build parameters.  For
21412854Sgabeblack@google.com# example, for target 'foo/bar/build/ALPHA_SE/arch/alpha/blah.do' we
21512854Sgabeblack@google.com# recognize that ALPHA_SE specifies the configuration because it
21612854Sgabeblack@google.com# follow 'build' in the build path.
21712854Sgabeblack@google.com
21812854Sgabeblack@google.com# The funky assignment to "[:]" is needed to replace the list contents
21912854Sgabeblack@google.com# in place rather than reassign the symbol to a new list, which
22012854Sgabeblack@google.com# doesn't work (obviously!).
22112854Sgabeblack@google.comBUILD_TARGETS[:] = makePathListAbsolute(BUILD_TARGETS)
22212854Sgabeblack@google.com
22312854Sgabeblack@google.com# Generate a list of the unique build roots and configs that the
22412854Sgabeblack@google.com# collected targets reference.
22512854Sgabeblack@google.comvariant_paths = []
22612854Sgabeblack@google.combuild_root = None
22712854Sgabeblack@google.comfor t in BUILD_TARGETS:
22812854Sgabeblack@google.com    path_dirs = t.split('/')
22912854Sgabeblack@google.com    try:
23012854Sgabeblack@google.com        build_top = rfind(path_dirs, 'build', -2)
23112854Sgabeblack@google.com    except:
23212854Sgabeblack@google.com        print "Error: no non-leaf 'build' dir found on target path", t
23312854Sgabeblack@google.com        Exit(1)
23412854Sgabeblack@google.com    this_build_root = joinpath('/',*path_dirs[:build_top+1])
23512854Sgabeblack@google.com    if not build_root:
23612854Sgabeblack@google.com        build_root = this_build_root
23712854Sgabeblack@google.com    else:
23812854Sgabeblack@google.com        if this_build_root != build_root:
23912854Sgabeblack@google.com            print "Error: build targets not under same build root\n"\
24012854Sgabeblack@google.com                  "  %s\n  %s" % (build_root, this_build_root)
24112854Sgabeblack@google.com            Exit(1)
24212854Sgabeblack@google.com    variant_path = joinpath('/',*path_dirs[:build_top+2])
24312854Sgabeblack@google.com    if variant_path not in variant_paths:
24412854Sgabeblack@google.com        variant_paths.append(variant_path)
24512854Sgabeblack@google.com
24612854Sgabeblack@google.com# Make sure build_root exists (might not if this is the first build there)
24712854Sgabeblack@google.comif not isdir(build_root):
24812854Sgabeblack@google.com    mkdir(build_root)
24912854Sgabeblack@google.commain['BUILDROOT'] = build_root
25012854Sgabeblack@google.com
25112854Sgabeblack@google.comExport('main')
25212854Sgabeblack@google.com
25312854Sgabeblack@google.commain.SConsignFile(joinpath(build_root, "sconsign"))
25412854Sgabeblack@google.com
25512854Sgabeblack@google.com# Default duplicate option is to use hard links, but this messes up
25612854Sgabeblack@google.com# when you use emacs to edit a file in the target dir, as emacs moves
25712854Sgabeblack@google.com# file to file~ then copies to file, breaking the link.  Symbolic
25812854Sgabeblack@google.com# (soft) links work better.
25912854Sgabeblack@google.commain.SetOption('duplicate', 'soft-copy')
26012854Sgabeblack@google.com
26112854Sgabeblack@google.com#
26212854Sgabeblack@google.com# Set up global sticky variables... these are common to an entire build
26312854Sgabeblack@google.com# tree (not specific to a particular build like ALPHA_SE)
26412854Sgabeblack@google.com#
26512854Sgabeblack@google.com
26612854Sgabeblack@google.comglobal_vars_file = joinpath(build_root, 'variables.global')
26712854Sgabeblack@google.com
26812854Sgabeblack@google.comglobal_vars = Variables(global_vars_file, args=ARGUMENTS)
26912854Sgabeblack@google.com
27012854Sgabeblack@google.comglobal_vars.AddVariables(
27112854Sgabeblack@google.com    ('CC', 'C compiler', environ.get('CC', main['CC'])),
27212854Sgabeblack@google.com    ('CXX', 'C++ compiler', environ.get('CXX', main['CXX'])),
27312854Sgabeblack@google.com    ('PROTOC', 'protoc tool', environ.get('PROTOC', 'protoc')),
27412854Sgabeblack@google.com    ('BATCH', 'Use batch pool for build and tests', False),
27512854Sgabeblack@google.com    ('BATCH_CMD', 'Batch pool submission command name', 'qdo'),
27612854Sgabeblack@google.com    ('M5_BUILD_CACHE', 'Cache built objects in this directory', False),
27712854Sgabeblack@google.com    ('EXTRAS', 'Add extra directories to the compilation', '')
27812854Sgabeblack@google.com    )
27912854Sgabeblack@google.com
28012854Sgabeblack@google.com# Update main environment with values from ARGUMENTS & global_vars_file
28112854Sgabeblack@google.comglobal_vars.Update(main)
28212854Sgabeblack@google.comhelp_texts["global_vars"] += global_vars.GenerateHelpText(main)
28312854Sgabeblack@google.com
28412854Sgabeblack@google.com# Save sticky variable settings back to current variables file
28512854Sgabeblack@google.comglobal_vars.Save(global_vars_file, main)
28612854Sgabeblack@google.com
28712854Sgabeblack@google.com# Parse EXTRAS variable to build list of all directories where we're
28812854Sgabeblack@google.com# look for sources etc.  This list is exported as extras_dir_list.
28912854Sgabeblack@google.combase_dir = main.srcdir.abspath
29012854Sgabeblack@google.comif main['EXTRAS']:
29112854Sgabeblack@google.com    extras_dir_list = makePathListAbsolute(main['EXTRAS'].split(':'))
29212854Sgabeblack@google.comelse:
29312854Sgabeblack@google.com    extras_dir_list = []
29412854Sgabeblack@google.com
29512854Sgabeblack@google.comExport('base_dir')
29612854Sgabeblack@google.comExport('extras_dir_list')
29712854Sgabeblack@google.com
29812854Sgabeblack@google.com# the ext directory should be on the #includes path
29912854Sgabeblack@google.commain.Append(CPPPATH=[Dir('ext')])
30012854Sgabeblack@google.com
30112854Sgabeblack@google.com# Add shared top-level headers
30212854Sgabeblack@google.commain.Prepend(CPPPATH=Dir('include'))
30312854Sgabeblack@google.com
30412854Sgabeblack@google.comdef strip_build_path(path, env):
30512854Sgabeblack@google.com    path = str(path)
30612854Sgabeblack@google.com    variant_base = env['BUILDROOT'] + os.path.sep
30712854Sgabeblack@google.com    if path.startswith(variant_base):
30812854Sgabeblack@google.com        path = path[len(variant_base):]
30912854Sgabeblack@google.com    elif path.startswith('build/'):
31012854Sgabeblack@google.com        path = path[6:]
31112854Sgabeblack@google.com    return path
31212854Sgabeblack@google.com
31312854Sgabeblack@google.com# Generate a string of the form:
31412854Sgabeblack@google.com#   common/path/prefix/src1, src2 -> tgt1, tgt2
31512854Sgabeblack@google.com# to print while building.
31612854Sgabeblack@google.comclass Transform(object):
31712854Sgabeblack@google.com    # all specific color settings should be here and nowhere else
31812854Sgabeblack@google.com    tool_color = termcap.Normal
31912854Sgabeblack@google.com    pfx_color = termcap.Yellow
32012854Sgabeblack@google.com    srcs_color = termcap.Yellow + termcap.Bold
32112854Sgabeblack@google.com    arrow_color = termcap.Blue + termcap.Bold
32212854Sgabeblack@google.com    tgts_color = termcap.Yellow + termcap.Bold
32312854Sgabeblack@google.com
32412854Sgabeblack@google.com    def __init__(self, tool, max_sources=99):
32512854Sgabeblack@google.com        self.format = self.tool_color + (" [%8s] " % tool) \
32612854Sgabeblack@google.com                      + self.pfx_color + "%s" \
32712854Sgabeblack@google.com                      + self.srcs_color + "%s" \
32812854Sgabeblack@google.com                      + self.arrow_color + " -> " \
32912854Sgabeblack@google.com                      + self.tgts_color + "%s" \
33012854Sgabeblack@google.com                      + termcap.Normal
33112854Sgabeblack@google.com        self.max_sources = max_sources
33212854Sgabeblack@google.com
33312854Sgabeblack@google.com    def __call__(self, target, source, env, for_signature=None):
33412854Sgabeblack@google.com        # truncate source list according to max_sources param
33512854Sgabeblack@google.com        source = source[0:self.max_sources]
33612854Sgabeblack@google.com        def strip(f):
33712854Sgabeblack@google.com            return strip_build_path(str(f), env)
33812854Sgabeblack@google.com        if len(source) > 0:
33912854Sgabeblack@google.com            srcs = map(strip, source)
34012854Sgabeblack@google.com        else:
34112854Sgabeblack@google.com            srcs = ['']
34212854Sgabeblack@google.com        tgts = map(strip, target)
34312854Sgabeblack@google.com        # surprisingly, os.path.commonprefix is a dumb char-by-char string
34412854Sgabeblack@google.com        # operation that has nothing to do with paths.
34512854Sgabeblack@google.com        com_pfx = os.path.commonprefix(srcs + tgts)
34612854Sgabeblack@google.com        com_pfx_len = len(com_pfx)
34712854Sgabeblack@google.com        if com_pfx:
34812854Sgabeblack@google.com            # do some cleanup and sanity checking on common prefix
34912854Sgabeblack@google.com            if com_pfx[-1] == ".":
35012854Sgabeblack@google.com                # prefix matches all but file extension: ok
35112854Sgabeblack@google.com                # back up one to change 'foo.cc -> o' to 'foo.cc -> .o'
35212854Sgabeblack@google.com                com_pfx = com_pfx[0:-1]
35312854Sgabeblack@google.com            elif com_pfx[-1] == "/":
35412854Sgabeblack@google.com                # common prefix is directory path: OK
35512854Sgabeblack@google.com                pass
35612854Sgabeblack@google.com            else:
35712854Sgabeblack@google.com                src0_len = len(srcs[0])
35812854Sgabeblack@google.com                tgt0_len = len(tgts[0])
35912854Sgabeblack@google.com                if src0_len == com_pfx_len:
36012854Sgabeblack@google.com                    # source is a substring of target, OK
36112854Sgabeblack@google.com                    pass
36212854Sgabeblack@google.com                elif tgt0_len == com_pfx_len:
36312854Sgabeblack@google.com                    # target is a substring of source, need to back up to
36412854Sgabeblack@google.com                    # avoid empty string on RHS of arrow
36512854Sgabeblack@google.com                    sep_idx = com_pfx.rfind(".")
36612854Sgabeblack@google.com                    if sep_idx != -1:
36712854Sgabeblack@google.com                        com_pfx = com_pfx[0:sep_idx]
36812854Sgabeblack@google.com                    else:
36912854Sgabeblack@google.com                        com_pfx = ''
37012854Sgabeblack@google.com                elif src0_len > com_pfx_len and srcs[0][com_pfx_len] == ".":
37112854Sgabeblack@google.com                    # still splitting at file extension: ok
37212854Sgabeblack@google.com                    pass
37312854Sgabeblack@google.com                else:
37412854Sgabeblack@google.com                    # probably a fluke; ignore it
37512854Sgabeblack@google.com                    com_pfx = ''
37612854Sgabeblack@google.com        # recalculate length in case com_pfx was modified
37712854Sgabeblack@google.com        com_pfx_len = len(com_pfx)
37812854Sgabeblack@google.com        def fmt(files):
37912854Sgabeblack@google.com            f = map(lambda s: s[com_pfx_len:], files)
38012854Sgabeblack@google.com            return ', '.join(f)
38112854Sgabeblack@google.com        return self.format % (com_pfx, fmt(srcs), fmt(tgts))
38212854Sgabeblack@google.com
38312854Sgabeblack@google.comExport('Transform')
38412854Sgabeblack@google.com
38512854Sgabeblack@google.com# enable the regression script to use the termcap
38612854Sgabeblack@google.commain['TERMCAP'] = termcap
38712854Sgabeblack@google.com
38812854Sgabeblack@google.comif GetOption('verbose'):
38912854Sgabeblack@google.com    def MakeAction(action, string, *args, **kwargs):
39012854Sgabeblack@google.com        return Action(action, *args, **kwargs)
39112854Sgabeblack@google.comelse:
39212854Sgabeblack@google.com    MakeAction = Action
39312854Sgabeblack@google.com    main['CCCOMSTR']        = Transform("CC")
39412854Sgabeblack@google.com    main['CXXCOMSTR']       = Transform("CXX")
39512854Sgabeblack@google.com    main['ASCOMSTR']        = Transform("AS")
39612854Sgabeblack@google.com    main['ARCOMSTR']        = Transform("AR", 0)
39712854Sgabeblack@google.com    main['LINKCOMSTR']      = Transform("LINK", 0)
39812854Sgabeblack@google.com    main['SHLINKCOMSTR']    = Transform("SHLINK", 0)
39912854Sgabeblack@google.com    main['RANLIBCOMSTR']    = Transform("RANLIB", 0)
40012854Sgabeblack@google.com    main['M4COMSTR']        = Transform("M4")
40112854Sgabeblack@google.com    main['SHCCCOMSTR']      = Transform("SHCC")
40212854Sgabeblack@google.com    main['SHCXXCOMSTR']     = Transform("SHCXX")
40312854Sgabeblack@google.comExport('MakeAction')
40412854Sgabeblack@google.com
40512854Sgabeblack@google.com# Initialize the Link-Time Optimization (LTO) flags
40612854Sgabeblack@google.commain['LTO_CCFLAGS'] = []
40712854Sgabeblack@google.commain['LTO_LDFLAGS'] = []
40812854Sgabeblack@google.com
40912854Sgabeblack@google.com# According to the readme, tcmalloc works best if the compiler doesn't
41012854Sgabeblack@google.com# assume that we're using the builtin malloc and friends. These flags
41112854Sgabeblack@google.com# are compiler-specific, so we need to set them after we detect which
41212854Sgabeblack@google.com# compiler we're using.
41312854Sgabeblack@google.commain['TCMALLOC_CCFLAGS'] = []
41412854Sgabeblack@google.com
41512854Sgabeblack@google.comCXX_version = readCommand([main['CXX'],'--version'], exception=False)
41612854Sgabeblack@google.comCXX_V = readCommand([main['CXX'],'-V'], exception=False)
41712854Sgabeblack@google.com
41812854Sgabeblack@google.commain['GCC'] = CXX_version and CXX_version.find('g++') >= 0
41912854Sgabeblack@google.commain['CLANG'] = CXX_version and CXX_version.find('clang') >= 0
42012854Sgabeblack@google.comif main['GCC'] + main['CLANG'] > 1:
42112854Sgabeblack@google.com    print 'Error: How can we have two at the same time?'
42212854Sgabeblack@google.com    Exit(1)
42312854Sgabeblack@google.com
42412854Sgabeblack@google.com# Set up default C++ compiler flags
42512854Sgabeblack@google.comif main['GCC'] or main['CLANG']:
42612854Sgabeblack@google.com    # As gcc and clang share many flags, do the common parts here
42712854Sgabeblack@google.com    main.Append(CCFLAGS=['-pipe'])
42812854Sgabeblack@google.com    main.Append(CCFLAGS=['-fno-strict-aliasing'])
42912854Sgabeblack@google.com    # Enable -Wall and -Wextra and then disable the few warnings that
43012854Sgabeblack@google.com    # we consistently violate
43112854Sgabeblack@google.com    main.Append(CCFLAGS=['-Wall', '-Wundef', '-Wextra',
43212854Sgabeblack@google.com                         '-Wno-sign-compare', '-Wno-unused-parameter'])
43312854Sgabeblack@google.com    # We always compile using C++11
43412854Sgabeblack@google.com    main.Append(CXXFLAGS=['-std=c++11'])
43512854Sgabeblack@google.com    if sys.platform.startswith('freebsd'):
43612854Sgabeblack@google.com        main.Append(CCFLAGS=['-I/usr/local/include'])
43712854Sgabeblack@google.com        main.Append(CXXFLAGS=['-I/usr/local/include'])
43812854Sgabeblack@google.com
43912854Sgabeblack@google.com    main['FILTER_PSHLINKFLAGS'] = lambda x: str(x).replace(' -shared', '')
44012854Sgabeblack@google.com    main['PSHLINKFLAGS'] = main.subst('${FILTER_PSHLINKFLAGS(SHLINKFLAGS)}')
44112854Sgabeblack@google.com    main['PLINKFLAGS'] = main.subst('${LINKFLAGS}')
44212854Sgabeblack@google.com    shared_partial_flags = ['-r', '-nostdlib']
44312854Sgabeblack@google.com    main.Append(PSHLINKFLAGS=shared_partial_flags)
44412854Sgabeblack@google.com    main.Append(PLINKFLAGS=shared_partial_flags)
44512854Sgabeblack@google.comelse:
44612854Sgabeblack@google.com    print termcap.Yellow + termcap.Bold + 'Error' + termcap.Normal,
44712854Sgabeblack@google.com    print "Don't know what compiler options to use for your compiler."
44812854Sgabeblack@google.com    print termcap.Yellow + '       compiler:' + termcap.Normal, main['CXX']
44912854Sgabeblack@google.com    print termcap.Yellow + '       version:' + termcap.Normal,
45012854Sgabeblack@google.com    if not CXX_version:
45112854Sgabeblack@google.com        print termcap.Yellow + termcap.Bold + "COMMAND NOT FOUND!" +\
45212854Sgabeblack@google.com               termcap.Normal
45312854Sgabeblack@google.com    else:
45412854Sgabeblack@google.com        print CXX_version.replace('\n', '<nl>')
45512854Sgabeblack@google.com    print "       If you're trying to use a compiler other than GCC"
45612854Sgabeblack@google.com    print "       or clang, there appears to be something wrong with your"
45712854Sgabeblack@google.com    print "       environment."
45812854Sgabeblack@google.com    print "       "
45912854Sgabeblack@google.com    print "       If you are trying to use a compiler other than those listed"
46012854Sgabeblack@google.com    print "       above you will need to ease fix SConstruct and "
46112854Sgabeblack@google.com    print "       src/SConscript to support that compiler."
46212854Sgabeblack@google.com    Exit(1)
46312854Sgabeblack@google.com
46412854Sgabeblack@google.comif main['GCC']:
46512854Sgabeblack@google.com    # Check for a supported version of gcc. >= 4.8 is chosen for its
46612854Sgabeblack@google.com    # level of c++11 support. See
46712854Sgabeblack@google.com    # http://gcc.gnu.org/projects/cxx0x.html for details.
46812854Sgabeblack@google.com    gcc_version = readCommand([main['CXX'], '-dumpversion'], exception=False)
46912854Sgabeblack@google.com    if compareVersions(gcc_version, "4.8") < 0:
47012854Sgabeblack@google.com        print 'Error: gcc version 4.8 or newer required.'
47112854Sgabeblack@google.com        print '       Installed version:', gcc_version
47212854Sgabeblack@google.com        Exit(1)
47312854Sgabeblack@google.com
47412854Sgabeblack@google.com    main['GCC_VERSION'] = gcc_version
47512854Sgabeblack@google.com
47612854Sgabeblack@google.com    if compareVersions(gcc_version, '4.9') >= 0:
47712854Sgabeblack@google.com        # Incremental linking with LTO is currently broken in gcc versions
47812854Sgabeblack@google.com        # 4.9 and above. A version where everything works completely hasn't
47912854Sgabeblack@google.com        # yet been identified.
48012854Sgabeblack@google.com        #
48112854Sgabeblack@google.com        # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67548
48212854Sgabeblack@google.com        main['BROKEN_INCREMENTAL_LTO'] = True
48312854Sgabeblack@google.com    if compareVersions(gcc_version, '6.0') >= 0:
48412854Sgabeblack@google.com        # gcc versions 6.0 and greater accept an -flinker-output flag which
48512854Sgabeblack@google.com        # selects what type of output the linker should generate. This is
48612854Sgabeblack@google.com        # necessary for incremental lto to work, but is also broken in
48712854Sgabeblack@google.com        # current versions of gcc. It may not be necessary in future
48812854Sgabeblack@google.com        # versions. We add it here since it might be, and as a reminder that
48912854Sgabeblack@google.com        # it exists. It's excluded if lto is being forced.
49012854Sgabeblack@google.com        #
49112854Sgabeblack@google.com        # https://gcc.gnu.org/gcc-6/changes.html
49212854Sgabeblack@google.com        # https://gcc.gnu.org/ml/gcc-patches/2015-11/msg03161.html
49312854Sgabeblack@google.com        # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69866
49412854Sgabeblack@google.com        if not GetOption('force_lto'):
49512854Sgabeblack@google.com            main.Append(PSHLINKFLAGS='-flinker-output=rel')
49612854Sgabeblack@google.com            main.Append(PLINKFLAGS='-flinker-output=rel')
49712854Sgabeblack@google.com
49812854Sgabeblack@google.com    # gcc from version 4.8 and above generates "rep; ret" instructions
49912854Sgabeblack@google.com    # to avoid performance penalties on certain AMD chips. Older
50012854Sgabeblack@google.com    # assemblers detect this as an error, "Error: expecting string
50112854Sgabeblack@google.com    # instruction after `rep'"
50212854Sgabeblack@google.com    as_version_raw = readCommand([main['AS'], '-v', '/dev/null',
50312854Sgabeblack@google.com                                  '-o', '/dev/null'],
50412854Sgabeblack@google.com                                 exception=False).split()
50512854Sgabeblack@google.com
50612854Sgabeblack@google.com    # version strings may contain extra distro-specific
50712854Sgabeblack@google.com    # qualifiers, so play it safe and keep only what comes before
50812854Sgabeblack@google.com    # the first hyphen
50912854Sgabeblack@google.com    as_version = as_version_raw[-1].split('-')[0] if as_version_raw else None
51012854Sgabeblack@google.com
51112854Sgabeblack@google.com    if not as_version or compareVersions(as_version, "2.23") < 0:
51212854Sgabeblack@google.com        print termcap.Yellow + termcap.Bold + \
51312854Sgabeblack@google.com            'Warning: This combination of gcc and binutils have' + \
51412854Sgabeblack@google.com            ' known incompatibilities.\n' + \
51512854Sgabeblack@google.com            '         If you encounter build problems, please update ' + \
51612854Sgabeblack@google.com            'binutils to 2.23.' + \
51712854Sgabeblack@google.com            termcap.Normal
51812854Sgabeblack@google.com
51912854Sgabeblack@google.com    # Make sure we warn if the user has requested to compile with the
52012854Sgabeblack@google.com    # Undefined Benahvior Sanitizer and this version of gcc does not
52112854Sgabeblack@google.com    # support it.
52212854Sgabeblack@google.com    if GetOption('with_ubsan') and \
52312854Sgabeblack@google.com            compareVersions(gcc_version, '4.9') < 0:
52412854Sgabeblack@google.com        print termcap.Yellow + termcap.Bold + \
52512854Sgabeblack@google.com            'Warning: UBSan is only supported using gcc 4.9 and later.' + \
52612854Sgabeblack@google.com            termcap.Normal
52712854Sgabeblack@google.com
52812854Sgabeblack@google.com    disable_lto = GetOption('no_lto')
52912854Sgabeblack@google.com    if not disable_lto and main.get('BROKEN_INCREMENTAL_LTO', False) and \
53012854Sgabeblack@google.com            not GetOption('force_lto'):
53112854Sgabeblack@google.com        print termcap.Yellow + termcap.Bold + \
53212854Sgabeblack@google.com            'Warning: Your compiler doesn\'t support incremental linking' + \
53312854Sgabeblack@google.com            ' and lto at the same time, so lto is being disabled. To force' + \
53412854Sgabeblack@google.com            ' lto on anyway, use the --force-lto option. That will disable' + \
53512854Sgabeblack@google.com            ' partial linking.' + \
53612854Sgabeblack@google.com            termcap.Normal
53712854Sgabeblack@google.com        disable_lto = True
53812854Sgabeblack@google.com
53912854Sgabeblack@google.com    # Add the appropriate Link-Time Optimization (LTO) flags
54012854Sgabeblack@google.com    # unless LTO is explicitly turned off. Note that these flags
54112854Sgabeblack@google.com    # are only used by the fast target.
54212854Sgabeblack@google.com    if not disable_lto:
54312854Sgabeblack@google.com        # Pass the LTO flag when compiling to produce GIMPLE
54412854Sgabeblack@google.com        # output, we merely create the flags here and only append
54512854Sgabeblack@google.com        # them later
54612854Sgabeblack@google.com        main['LTO_CCFLAGS'] = ['-flto=%d' % GetOption('num_jobs')]
54712854Sgabeblack@google.com
54812854Sgabeblack@google.com        # Use the same amount of jobs for LTO as we are running
54912854Sgabeblack@google.com        # scons with
55012854Sgabeblack@google.com        main['LTO_LDFLAGS'] = ['-flto=%d' % GetOption('num_jobs')]
55112854Sgabeblack@google.com
55212854Sgabeblack@google.com    main.Append(TCMALLOC_CCFLAGS=['-fno-builtin-malloc', '-fno-builtin-calloc',
55312854Sgabeblack@google.com                                  '-fno-builtin-realloc', '-fno-builtin-free'])
55412854Sgabeblack@google.com
55512854Sgabeblack@google.com    # add option to check for undeclared overrides
55612854Sgabeblack@google.com    if compareVersions(gcc_version, "5.0") > 0:
55712854Sgabeblack@google.com        main.Append(CCFLAGS=['-Wno-error=suggest-override'])
55812854Sgabeblack@google.com
55912854Sgabeblack@google.comelif main['CLANG']:
56012854Sgabeblack@google.com    # Check for a supported version of clang, >= 3.1 is needed to
56112854Sgabeblack@google.com    # support similar features as gcc 4.8. See
56212854Sgabeblack@google.com    # http://clang.llvm.org/cxx_status.html for details
56312854Sgabeblack@google.com    clang_version_re = re.compile(".* version (\d+\.\d+)")
56412854Sgabeblack@google.com    clang_version_match = clang_version_re.search(CXX_version)
56512854Sgabeblack@google.com    if (clang_version_match):
56612854Sgabeblack@google.com        clang_version = clang_version_match.groups()[0]
56712854Sgabeblack@google.com        if compareVersions(clang_version, "3.1") < 0:
56812854Sgabeblack@google.com            print 'Error: clang version 3.1 or newer required.'
56912854Sgabeblack@google.com            print '       Installed version:', clang_version
57012854Sgabeblack@google.com            Exit(1)
57112854Sgabeblack@google.com    else:
57212854Sgabeblack@google.com        print 'Error: Unable to determine clang version.'
57312854Sgabeblack@google.com        Exit(1)
57412854Sgabeblack@google.com
57512854Sgabeblack@google.com    # clang has a few additional warnings that we disable, extraneous
57612854Sgabeblack@google.com    # parantheses are allowed due to Ruby's printing of the AST,
57712854Sgabeblack@google.com    # finally self assignments are allowed as the generated CPU code
57812854Sgabeblack@google.com    # is relying on this
57912854Sgabeblack@google.com    main.Append(CCFLAGS=['-Wno-parentheses',
58012854Sgabeblack@google.com                         '-Wno-self-assign',
58112854Sgabeblack@google.com                         # Some versions of libstdc++ (4.8?) seem to
58212854Sgabeblack@google.com                         # use struct hash and class hash
58312854Sgabeblack@google.com                         # interchangeably.
58412854Sgabeblack@google.com                         '-Wno-mismatched-tags',
58512854Sgabeblack@google.com                         ])
58612854Sgabeblack@google.com
58712854Sgabeblack@google.com    main.Append(TCMALLOC_CCFLAGS=['-fno-builtin'])
58812854Sgabeblack@google.com
58912854Sgabeblack@google.com    # On Mac OS X/Darwin we need to also use libc++ (part of XCode) as
59012854Sgabeblack@google.com    # opposed to libstdc++, as the later is dated.
59112854Sgabeblack@google.com    if sys.platform == "darwin":
59212854Sgabeblack@google.com        main.Append(CXXFLAGS=['-stdlib=libc++'])
59312854Sgabeblack@google.com        main.Append(LIBS=['c++'])
59412854Sgabeblack@google.com
59512854Sgabeblack@google.com    # On FreeBSD we need libthr.
59612854Sgabeblack@google.com    if sys.platform.startswith('freebsd'):
59712854Sgabeblack@google.com        main.Append(LIBS=['thr'])
59812854Sgabeblack@google.com
59912854Sgabeblack@google.comelse:
60012854Sgabeblack@google.com    print termcap.Yellow + termcap.Bold + 'Error' + termcap.Normal,
60112854Sgabeblack@google.com    print "Don't know what compiler options to use for your compiler."
60212854Sgabeblack@google.com    print termcap.Yellow + '       compiler:' + termcap.Normal, main['CXX']
60312854Sgabeblack@google.com    print termcap.Yellow + '       version:' + termcap.Normal,
60412854Sgabeblack@google.com    if not CXX_version:
60512854Sgabeblack@google.com        print termcap.Yellow + termcap.Bold + "COMMAND NOT FOUND!" +\
60612854Sgabeblack@google.com               termcap.Normal
60712854Sgabeblack@google.com    else:
60812854Sgabeblack@google.com        print CXX_version.replace('\n', '<nl>')
60912854Sgabeblack@google.com    print "       If you're trying to use a compiler other than GCC"
61012854Sgabeblack@google.com    print "       or clang, there appears to be something wrong with your"
61112854Sgabeblack@google.com    print "       environment."
61212854Sgabeblack@google.com    print "       "
61312854Sgabeblack@google.com    print "       If you are trying to use a compiler other than those listed"
61412854Sgabeblack@google.com    print "       above you will need to ease fix SConstruct and "
61512854Sgabeblack@google.com    print "       src/SConscript to support that compiler."
61612854Sgabeblack@google.com    Exit(1)
61712854Sgabeblack@google.com
61812854Sgabeblack@google.com# Set up common yacc/bison flags (needed for Ruby)
61912854Sgabeblack@google.commain['YACCFLAGS'] = '-d'
62012854Sgabeblack@google.commain['YACCHXXFILESUFFIX'] = '.hh'
62112854Sgabeblack@google.com
62212854Sgabeblack@google.com# Do this after we save setting back, or else we'll tack on an
62312854Sgabeblack@google.com# extra 'qdo' every time we run scons.
62412854Sgabeblack@google.comif main['BATCH']:
62512854Sgabeblack@google.com    main['CC']     = main['BATCH_CMD'] + ' ' + main['CC']
62612854Sgabeblack@google.com    main['CXX']    = main['BATCH_CMD'] + ' ' + main['CXX']
62712854Sgabeblack@google.com    main['AS']     = main['BATCH_CMD'] + ' ' + main['AS']
62812854Sgabeblack@google.com    main['AR']     = main['BATCH_CMD'] + ' ' + main['AR']
62912854Sgabeblack@google.com    main['RANLIB'] = main['BATCH_CMD'] + ' ' + main['RANLIB']
63012854Sgabeblack@google.com
63112854Sgabeblack@google.comif sys.platform == 'cygwin':
63212854Sgabeblack@google.com    # cygwin has some header file issues...
63312854Sgabeblack@google.com    main.Append(CCFLAGS=["-Wno-uninitialized"])
63412854Sgabeblack@google.com
63512854Sgabeblack@google.com# Check for the protobuf compiler
63612854Sgabeblack@google.comprotoc_version = readCommand([main['PROTOC'], '--version'],
63712854Sgabeblack@google.com                             exception='').split()
63812854Sgabeblack@google.com
63912854Sgabeblack@google.com# First two words should be "libprotoc x.y.z"
64012854Sgabeblack@google.comif len(protoc_version) < 2 or protoc_version[0] != 'libprotoc':
64112854Sgabeblack@google.com    print termcap.Yellow + termcap.Bold + \
64212854Sgabeblack@google.com        'Warning: Protocol buffer compiler (protoc) not found.\n' + \
64312854Sgabeblack@google.com        '         Please install protobuf-compiler for tracing support.' + \
64412854Sgabeblack@google.com        termcap.Normal
64512854Sgabeblack@google.com    main['PROTOC'] = False
64612854Sgabeblack@google.comelse:
64712854Sgabeblack@google.com    # Based on the availability of the compress stream wrappers,
64812854Sgabeblack@google.com    # require 2.1.0
64912854Sgabeblack@google.com    min_protoc_version = '2.1.0'
65012854Sgabeblack@google.com    if compareVersions(protoc_version[1], min_protoc_version) < 0:
65112854Sgabeblack@google.com        print termcap.Yellow + termcap.Bold + \
65212854Sgabeblack@google.com            'Warning: protoc version', min_protoc_version, \
65312854Sgabeblack@google.com            'or newer required.\n' + \
65412854Sgabeblack@google.com            '         Installed version:', protoc_version[1], \
65512854Sgabeblack@google.com            termcap.Normal
65612854Sgabeblack@google.com        main['PROTOC'] = False
65712854Sgabeblack@google.com    else:
65812854Sgabeblack@google.com        # Attempt to determine the appropriate include path and
65912854Sgabeblack@google.com        # library path using pkg-config, that means we also need to
66012854Sgabeblack@google.com        # check for pkg-config. Note that it is possible to use
66112854Sgabeblack@google.com        # protobuf without the involvement of pkg-config. Later on we
66212854Sgabeblack@google.com        # check go a library config check and at that point the test
66312854Sgabeblack@google.com        # will fail if libprotobuf cannot be found.
66412854Sgabeblack@google.com        if readCommand(['pkg-config', '--version'], exception=''):
66512854Sgabeblack@google.com            try:
66612854Sgabeblack@google.com                # Attempt to establish what linking flags to add for protobuf
66712854Sgabeblack@google.com                # using pkg-config
66812854Sgabeblack@google.com                main.ParseConfig('pkg-config --cflags --libs-only-L protobuf')
66912854Sgabeblack@google.com            except:
67012854Sgabeblack@google.com                print termcap.Yellow + termcap.Bold + \
67112854Sgabeblack@google.com                    'Warning: pkg-config could not get protobuf flags.' + \
67212854Sgabeblack@google.com                    termcap.Normal
67312854Sgabeblack@google.com
67412854Sgabeblack@google.com
67512854Sgabeblack@google.com# Check for 'timeout' from GNU coreutils. If present, regressions will
67612854Sgabeblack@google.com# be run with a time limit. We require version 8.13 since we rely on
67712854Sgabeblack@google.com# support for the '--foreground' option.
67812854Sgabeblack@google.comif sys.platform.startswith('freebsd'):
67912854Sgabeblack@google.com    timeout_lines = readCommand(['gtimeout', '--version'],
68012854Sgabeblack@google.com                                exception='').splitlines()
68112854Sgabeblack@google.comelse:
68212854Sgabeblack@google.com    timeout_lines = readCommand(['timeout', '--version'],
68312854Sgabeblack@google.com                                exception='').splitlines()
68412854Sgabeblack@google.com# Get the first line and tokenize it
68512854Sgabeblack@google.comtimeout_version = timeout_lines[0].split() if timeout_lines else []
68612854Sgabeblack@google.commain['TIMEOUT'] =  timeout_version and \
68712854Sgabeblack@google.com    compareVersions(timeout_version[-1], '8.13') >= 0
68812854Sgabeblack@google.com
68912854Sgabeblack@google.com# Add a custom Check function to test for structure members.
69012854Sgabeblack@google.comdef CheckMember(context, include, decl, member, include_quotes="<>"):
69112854Sgabeblack@google.com    context.Message("Checking for member %s in %s..." %
69212854Sgabeblack@google.com                    (member, decl))
69312854Sgabeblack@google.com    text = """
69412854Sgabeblack@google.com#include %(header)s
69512854Sgabeblack@google.comint main(){
69612854Sgabeblack@google.com  %(decl)s test;
69712854Sgabeblack@google.com  (void)test.%(member)s;
69812854Sgabeblack@google.com  return 0;
69912854Sgabeblack@google.com};
70012854Sgabeblack@google.com""" % { "header" : include_quotes[0] + include + include_quotes[1],
70112854Sgabeblack@google.com        "decl" : decl,
70212854Sgabeblack@google.com        "member" : member,
70312854Sgabeblack@google.com        }
70412854Sgabeblack@google.com
70512854Sgabeblack@google.com    ret = context.TryCompile(text, extension=".cc")
70612854Sgabeblack@google.com    context.Result(ret)
70712854Sgabeblack@google.com    return ret
70812854Sgabeblack@google.com
70912854Sgabeblack@google.com# Platform-specific configuration.  Note again that we assume that all
71012854Sgabeblack@google.com# builds under a given build root run on the same host platform.
71112854Sgabeblack@google.comconf = Configure(main,
71212854Sgabeblack@google.com                 conf_dir = joinpath(build_root, '.scons_config'),
71312854Sgabeblack@google.com                 log_file = joinpath(build_root, 'scons_config.log'),
71412854Sgabeblack@google.com                 custom_tests = {
71512854Sgabeblack@google.com        'CheckMember' : CheckMember,
71612854Sgabeblack@google.com        })
71712854Sgabeblack@google.com
71812854Sgabeblack@google.com# Check if we should compile a 64 bit binary on Mac OS X/Darwin
71912854Sgabeblack@google.comtry:
72012854Sgabeblack@google.com    import platform
72112854Sgabeblack@google.com    uname = platform.uname()
72212854Sgabeblack@google.com    if uname[0] == 'Darwin' and compareVersions(uname[2], '9.0.0') >= 0:
72312854Sgabeblack@google.com        if int(readCommand('sysctl -n hw.cpu64bit_capable')[0]):
72412854Sgabeblack@google.com            main.Append(CCFLAGS=['-arch', 'x86_64'])
72512854Sgabeblack@google.com            main.Append(CFLAGS=['-arch', 'x86_64'])
72612854Sgabeblack@google.com            main.Append(LINKFLAGS=['-arch', 'x86_64'])
72712854Sgabeblack@google.com            main.Append(ASFLAGS=['-arch', 'x86_64'])
72812854Sgabeblack@google.comexcept:
72912854Sgabeblack@google.com    pass
73012854Sgabeblack@google.com
73112854Sgabeblack@google.com# Recent versions of scons substitute a "Null" object for Configure()
73212854Sgabeblack@google.com# when configuration isn't necessary, e.g., if the "--help" option is
73312854Sgabeblack@google.com# present.  Unfortuantely this Null object always returns false,
73412854Sgabeblack@google.com# breaking all our configuration checks.  We replace it with our own
73512854Sgabeblack@google.com# more optimistic null object that returns True instead.
73612854Sgabeblack@google.comif not conf:
73712854Sgabeblack@google.com    def NullCheck(*args, **kwargs):
73812854Sgabeblack@google.com        return True
73912854Sgabeblack@google.com
74012854Sgabeblack@google.com    class NullConf:
74112854Sgabeblack@google.com        def __init__(self, env):
74212854Sgabeblack@google.com            self.env = env
74312854Sgabeblack@google.com        def Finish(self):
74412854Sgabeblack@google.com            return self.env
74512854Sgabeblack@google.com        def __getattr__(self, mname):
74612854Sgabeblack@google.com            return NullCheck
74712854Sgabeblack@google.com
74812854Sgabeblack@google.com    conf = NullConf(main)
74912854Sgabeblack@google.com
75012854Sgabeblack@google.com# Cache build files in the supplied directory.
75112854Sgabeblack@google.comif main['M5_BUILD_CACHE']:
75212854Sgabeblack@google.com    print 'Using build cache located at', main['M5_BUILD_CACHE']
75312854Sgabeblack@google.com    CacheDir(main['M5_BUILD_CACHE'])
75412854Sgabeblack@google.com
75512854Sgabeblack@google.commain['USE_PYTHON'] = not GetOption('without_python')
75612854Sgabeblack@google.comif main['USE_PYTHON']:
75712854Sgabeblack@google.com    # Find Python include and library directories for embedding the
75812854Sgabeblack@google.com    # interpreter. We rely on python-config to resolve the appropriate
75912854Sgabeblack@google.com    # includes and linker flags. ParseConfig does not seem to understand
76012854Sgabeblack@google.com    # the more exotic linker flags such as -Xlinker and -export-dynamic so
76112854Sgabeblack@google.com    # we add them explicitly below. If you want to link in an alternate
76212854Sgabeblack@google.com    # version of python, see above for instructions on how to invoke
76312854Sgabeblack@google.com    # scons with the appropriate PATH set.
76412854Sgabeblack@google.com    #
76512854Sgabeblack@google.com    # First we check if python2-config exists, else we use python-config
76612854Sgabeblack@google.com    python_config = readCommand(['which', 'python2-config'],
76712854Sgabeblack@google.com                                exception='').strip()
76812854Sgabeblack@google.com    if not os.path.exists(python_config):
76912854Sgabeblack@google.com        python_config = readCommand(['which', 'python-config'],
77012854Sgabeblack@google.com                                    exception='').strip()
77112854Sgabeblack@google.com    py_includes = readCommand([python_config, '--includes'],
77212854Sgabeblack@google.com                              exception='').split()
77312854Sgabeblack@google.com    # Strip the -I from the include folders before adding them to the
77412854Sgabeblack@google.com    # CPPPATH
77512854Sgabeblack@google.com    main.Append(CPPPATH=map(lambda inc: inc[2:], py_includes))
77612854Sgabeblack@google.com
77712854Sgabeblack@google.com    # Read the linker flags and split them into libraries and other link
77812854Sgabeblack@google.com    # flags. The libraries are added later through the call the CheckLib.
77912854Sgabeblack@google.com    py_ld_flags = readCommand([python_config, '--ldflags'],
78012854Sgabeblack@google.com        exception='').split()
78112854Sgabeblack@google.com    py_libs = []
78212854Sgabeblack@google.com    for lib in py_ld_flags:
78312854Sgabeblack@google.com         if not lib.startswith('-l'):
78412854Sgabeblack@google.com             main.Append(LINKFLAGS=[lib])
78512854Sgabeblack@google.com         else:
78612854Sgabeblack@google.com             lib = lib[2:]
78712854Sgabeblack@google.com             if lib not in py_libs:
78812854Sgabeblack@google.com                 py_libs.append(lib)
78912854Sgabeblack@google.com
79012854Sgabeblack@google.com    # verify that this stuff works
79112854Sgabeblack@google.com    if not conf.CheckHeader('Python.h', '<>'):
79212854Sgabeblack@google.com        print "Error: can't find Python.h header in", py_includes
79312854Sgabeblack@google.com        print "Install Python headers (package python-dev on Ubuntu and RedHat)"
79412854Sgabeblack@google.com        Exit(1)
79512854Sgabeblack@google.com
79612854Sgabeblack@google.com    for lib in py_libs:
79712854Sgabeblack@google.com        if not conf.CheckLib(lib):
79812854Sgabeblack@google.com            print "Error: can't find library %s required by python" % lib
79912854Sgabeblack@google.com            Exit(1)
80012854Sgabeblack@google.com
80112854Sgabeblack@google.com# On Solaris you need to use libsocket for socket ops
80212854Sgabeblack@google.comif not conf.CheckLibWithHeader(None, 'sys/socket.h', 'C++', 'accept(0,0,0);'):
80312854Sgabeblack@google.com   if not conf.CheckLibWithHeader('socket', 'sys/socket.h', 'C++', 'accept(0,0,0);'):
80412854Sgabeblack@google.com       print "Can't find library with socket calls (e.g. accept())"
80512854Sgabeblack@google.com       Exit(1)
80612854Sgabeblack@google.com
80712854Sgabeblack@google.com# Check for zlib.  If the check passes, libz will be automatically
80812854Sgabeblack@google.com# added to the LIBS environment variable.
80912854Sgabeblack@google.comif not conf.CheckLibWithHeader('z', 'zlib.h', 'C++','zlibVersion();'):
81012854Sgabeblack@google.com    print 'Error: did not find needed zlib compression library '\
81112854Sgabeblack@google.com          'and/or zlib.h header file.'
81212854Sgabeblack@google.com    print '       Please install zlib and try again.'
81312854Sgabeblack@google.com    Exit(1)
81412854Sgabeblack@google.com
81512854Sgabeblack@google.com# If we have the protobuf compiler, also make sure we have the
81612854Sgabeblack@google.com# development libraries. If the check passes, libprotobuf will be
81712854Sgabeblack@google.com# automatically added to the LIBS environment variable. After
81812854Sgabeblack@google.com# this, we can use the HAVE_PROTOBUF flag to determine if we have
81912854Sgabeblack@google.com# got both protoc and libprotobuf available.
82012854Sgabeblack@google.commain['HAVE_PROTOBUF'] = main['PROTOC'] and \
82112854Sgabeblack@google.com    conf.CheckLibWithHeader('protobuf', 'google/protobuf/message.h',
82212854Sgabeblack@google.com                            'C++', 'GOOGLE_PROTOBUF_VERIFY_VERSION;')
82312854Sgabeblack@google.com
82412854Sgabeblack@google.com# If we have the compiler but not the library, print another warning.
82512854Sgabeblack@google.comif main['PROTOC'] and not main['HAVE_PROTOBUF']:
82612854Sgabeblack@google.com    print termcap.Yellow + termcap.Bold + \
82712854Sgabeblack@google.com        'Warning: did not find protocol buffer library and/or headers.\n' + \
82812854Sgabeblack@google.com    '       Please install libprotobuf-dev for tracing support.' + \
82912854Sgabeblack@google.com    termcap.Normal
83012854Sgabeblack@google.com
83112854Sgabeblack@google.com# Check for librt.
83212854Sgabeblack@google.comhave_posix_clock = \
83312854Sgabeblack@google.com    conf.CheckLibWithHeader(None, 'time.h', 'C',
83412854Sgabeblack@google.com                            'clock_nanosleep(0,0,NULL,NULL);') or \
83512854Sgabeblack@google.com    conf.CheckLibWithHeader('rt', 'time.h', 'C',
83612854Sgabeblack@google.com                            'clock_nanosleep(0,0,NULL,NULL);')
83712854Sgabeblack@google.com
83812854Sgabeblack@google.comhave_posix_timers = \
83912854Sgabeblack@google.com    conf.CheckLibWithHeader([None, 'rt'], [ 'time.h', 'signal.h' ], 'C',
84012854Sgabeblack@google.com                            'timer_create(CLOCK_MONOTONIC, NULL, NULL);')
84112854Sgabeblack@google.com
84212854Sgabeblack@google.comif not GetOption('without_tcmalloc'):
84312854Sgabeblack@google.com    if conf.CheckLib('tcmalloc'):
84412854Sgabeblack@google.com        main.Append(CCFLAGS=main['TCMALLOC_CCFLAGS'])
84512854Sgabeblack@google.com    elif conf.CheckLib('tcmalloc_minimal'):
84612854Sgabeblack@google.com        main.Append(CCFLAGS=main['TCMALLOC_CCFLAGS'])
84712854Sgabeblack@google.com    else:
84812854Sgabeblack@google.com        print termcap.Yellow + termcap.Bold + \
84912854Sgabeblack@google.com              "You can get a 12% performance improvement by "\
85012854Sgabeblack@google.com              "installing tcmalloc (libgoogle-perftools-dev package "\
85112854Sgabeblack@google.com              "on Ubuntu or RedHat)." + termcap.Normal
85212854Sgabeblack@google.com
85312854Sgabeblack@google.com
85412854Sgabeblack@google.com# Detect back trace implementations. The last implementation in the
85512854Sgabeblack@google.com# list will be used by default.
85612854Sgabeblack@google.combacktrace_impls = [ "none" ]
85712854Sgabeblack@google.com
85812854Sgabeblack@google.comif conf.CheckLibWithHeader(None, 'execinfo.h', 'C',
85912854Sgabeblack@google.com                           'backtrace_symbols_fd((void*)0, 0, 0);'):
86012854Sgabeblack@google.com    backtrace_impls.append("glibc")
86112854Sgabeblack@google.comelif conf.CheckLibWithHeader('execinfo', 'execinfo.h', 'C',
86212854Sgabeblack@google.com                           'backtrace_symbols_fd((void*)0, 0, 0);'):
86312854Sgabeblack@google.com    # NetBSD and FreeBSD need libexecinfo.
86412854Sgabeblack@google.com    backtrace_impls.append("glibc")
86512854Sgabeblack@google.com    main.Append(LIBS=['execinfo'])
86612854Sgabeblack@google.com
86712854Sgabeblack@google.comif backtrace_impls[-1] == "none":
86812854Sgabeblack@google.com    default_backtrace_impl = "none"
86912854Sgabeblack@google.com    print termcap.Yellow + termcap.Bold + \
87012854Sgabeblack@google.com        "No suitable back trace implementation found." + \
87112854Sgabeblack@google.com        termcap.Normal
87212854Sgabeblack@google.com
87312854Sgabeblack@google.comif not have_posix_clock:
87412854Sgabeblack@google.com    print "Can't find library for POSIX clocks."
87512854Sgabeblack@google.com
87612854Sgabeblack@google.com# Check for <fenv.h> (C99 FP environment control)
87712854Sgabeblack@google.comhave_fenv = conf.CheckHeader('fenv.h', '<>')
87812854Sgabeblack@google.comif not have_fenv:
87912854Sgabeblack@google.com    print "Warning: Header file <fenv.h> not found."
88012854Sgabeblack@google.com    print "         This host has no IEEE FP rounding mode control."
88112854Sgabeblack@google.com
88212854Sgabeblack@google.com# Check for <png.h> (libpng library needed if wanting to dump
88312854Sgabeblack@google.com# frame buffer image in png format)
88412854Sgabeblack@google.comhave_png = conf.CheckHeader('png.h', '<>')
88512854Sgabeblack@google.comif not have_png:
88612854Sgabeblack@google.com    print "Warning: Header file <png.h> not found."
88712854Sgabeblack@google.com    print "         This host has no libpng library."
88812854Sgabeblack@google.com    print "         Disabling support for PNG framebuffers."
88912854Sgabeblack@google.com
89012854Sgabeblack@google.com# Check if we should enable KVM-based hardware virtualization. The API
89112854Sgabeblack@google.com# we rely on exists since version 2.6.36 of the kernel, but somehow
89212854Sgabeblack@google.com# the KVM_API_VERSION does not reflect the change. We test for one of
89312854Sgabeblack@google.com# the types as a fall back.
89412854Sgabeblack@google.comhave_kvm = conf.CheckHeader('linux/kvm.h', '<>')
89512854Sgabeblack@google.comif not have_kvm:
89612854Sgabeblack@google.com    print "Info: Compatible header file <linux/kvm.h> not found, " \
89712854Sgabeblack@google.com        "disabling KVM support."
89812854Sgabeblack@google.com
89912854Sgabeblack@google.com# Check if the TUN/TAP driver is available.
90012854Sgabeblack@google.comhave_tuntap = conf.CheckHeader('linux/if_tun.h', '<>')
90112854Sgabeblack@google.comif not have_tuntap:
90212854Sgabeblack@google.com    print "Info: Compatible header file <linux/if_tun.h> not found."
90312854Sgabeblack@google.com
90412854Sgabeblack@google.com# x86 needs support for xsave. We test for the structure here since we
90512854Sgabeblack@google.com# won't be able to run new tests by the time we know which ISA we're
90612854Sgabeblack@google.com# targeting.
90712854Sgabeblack@google.comhave_kvm_xsave = conf.CheckTypeSize('struct kvm_xsave',
90812854Sgabeblack@google.com                                    '#include <linux/kvm.h>') != 0
90912854Sgabeblack@google.com
91012854Sgabeblack@google.com# Check if the requested target ISA is compatible with the host
91112854Sgabeblack@google.comdef is_isa_kvm_compatible(isa):
91212854Sgabeblack@google.com    try:
91312854Sgabeblack@google.com        import platform
91412854Sgabeblack@google.com        host_isa = platform.machine()
91512854Sgabeblack@google.com    except:
91612854Sgabeblack@google.com        print "Warning: Failed to determine host ISA."
91712854Sgabeblack@google.com        return False
91812854Sgabeblack@google.com
91912854Sgabeblack@google.com    if not have_posix_timers:
92012854Sgabeblack@google.com        print "Warning: Can not enable KVM, host seems to lack support " \
92112854Sgabeblack@google.com            "for POSIX timers"
92212854Sgabeblack@google.com        return False
92312854Sgabeblack@google.com
92412854Sgabeblack@google.com    if isa == "arm":
92512854Sgabeblack@google.com        return host_isa in ( "armv7l", "aarch64" )
92612854Sgabeblack@google.com    elif isa == "x86":
92712854Sgabeblack@google.com        if host_isa != "x86_64":
92812854Sgabeblack@google.com            return False
92912854Sgabeblack@google.com
93012854Sgabeblack@google.com        if not have_kvm_xsave:
93112854Sgabeblack@google.com            print "KVM on x86 requires xsave support in kernel headers."
93212854Sgabeblack@google.com            return False
93312854Sgabeblack@google.com
93412854Sgabeblack@google.com        return True
93512854Sgabeblack@google.com    else:
93612854Sgabeblack@google.com        return False
93712854Sgabeblack@google.com
93812854Sgabeblack@google.com
93912854Sgabeblack@google.com# Check if the exclude_host attribute is available. We want this to
94012854Sgabeblack@google.com# get accurate instruction counts in KVM.
94112854Sgabeblack@google.commain['HAVE_PERF_ATTR_EXCLUDE_HOST'] = conf.CheckMember(
94212854Sgabeblack@google.com    'linux/perf_event.h', 'struct perf_event_attr', 'exclude_host')
94312854Sgabeblack@google.com
94412854Sgabeblack@google.com
94512854Sgabeblack@google.com######################################################################
94612854Sgabeblack@google.com#
94712854Sgabeblack@google.com# Finish the configuration
94812854Sgabeblack@google.com#
94912854Sgabeblack@google.commain = conf.Finish()
95012854Sgabeblack@google.com
95112854Sgabeblack@google.com######################################################################
95212854Sgabeblack@google.com#
95312854Sgabeblack@google.com# Collect all non-global variables
95412854Sgabeblack@google.com#
95512854Sgabeblack@google.com
95612854Sgabeblack@google.com# Define the universe of supported ISAs
95712854Sgabeblack@google.comall_isa_list = [ ]
95812854Sgabeblack@google.comall_gpu_isa_list = [ ]
95912854Sgabeblack@google.comExport('all_isa_list')
96012854Sgabeblack@google.comExport('all_gpu_isa_list')
96112854Sgabeblack@google.com
96212854Sgabeblack@google.comclass CpuModel(object):
96312854Sgabeblack@google.com    '''The CpuModel class encapsulates everything the ISA parser needs to
96412854Sgabeblack@google.com    know about a particular CPU model.'''
96512854Sgabeblack@google.com
96612854Sgabeblack@google.com    # Dict of available CPU model objects.  Accessible as CpuModel.dict.
96712854Sgabeblack@google.com    dict = {}
96812854Sgabeblack@google.com
96912854Sgabeblack@google.com    # Constructor.  Automatically adds models to CpuModel.dict.
97012854Sgabeblack@google.com    def __init__(self, name, default=False):
97112854Sgabeblack@google.com        self.name = name           # name of model
97212854Sgabeblack@google.com
97312854Sgabeblack@google.com        # This cpu is enabled by default
97412854Sgabeblack@google.com        self.default = default
97512854Sgabeblack@google.com
97612854Sgabeblack@google.com        # Add self to dict
97712854Sgabeblack@google.com        if name in CpuModel.dict:
97812854Sgabeblack@google.com            raise AttributeError, "CpuModel '%s' already registered" % name
97912854Sgabeblack@google.com        CpuModel.dict[name] = self
98012854Sgabeblack@google.com
98112854Sgabeblack@google.comExport('CpuModel')
98212854Sgabeblack@google.com
98312854Sgabeblack@google.com# Sticky variables get saved in the variables file so they persist from
98412854Sgabeblack@google.com# one invocation to the next (unless overridden, in which case the new
98512854Sgabeblack@google.com# value becomes sticky).
98612854Sgabeblack@google.comsticky_vars = Variables(args=ARGUMENTS)
98712854Sgabeblack@google.comExport('sticky_vars')
98812854Sgabeblack@google.com
98912854Sgabeblack@google.com# Sticky variables that should be exported
99012854Sgabeblack@google.comexport_vars = []
99112854Sgabeblack@google.comExport('export_vars')
99212854Sgabeblack@google.com
99312854Sgabeblack@google.com# For Ruby
99412854Sgabeblack@google.comall_protocols = []
99512854Sgabeblack@google.comExport('all_protocols')
99612854Sgabeblack@google.comprotocol_dirs = []
99712854Sgabeblack@google.comExport('protocol_dirs')
99812854Sgabeblack@google.comslicc_includes = []
99912854Sgabeblack@google.comExport('slicc_includes')
100012854Sgabeblack@google.com
100112854Sgabeblack@google.com# Walk the tree and execute all SConsopts scripts that wil add to the
100212854Sgabeblack@google.com# above variables
100312854Sgabeblack@google.comif GetOption('verbose'):
100412854Sgabeblack@google.com    print "Reading SConsopts"
100512854Sgabeblack@google.comfor bdir in [ base_dir ] + extras_dir_list:
100612854Sgabeblack@google.com    if not isdir(bdir):
100712854Sgabeblack@google.com        print "Error: directory '%s' does not exist" % bdir
100812854Sgabeblack@google.com        Exit(1)
100912854Sgabeblack@google.com    for root, dirs, files in os.walk(bdir):
101012854Sgabeblack@google.com        if 'SConsopts' in files:
101112854Sgabeblack@google.com            if GetOption('verbose'):
101212854Sgabeblack@google.com                print "Reading", joinpath(root, 'SConsopts')
101312854Sgabeblack@google.com            SConscript(joinpath(root, 'SConsopts'))
101412854Sgabeblack@google.com
101512854Sgabeblack@google.comall_isa_list.sort()
101612854Sgabeblack@google.comall_gpu_isa_list.sort()
101712854Sgabeblack@google.com
101812854Sgabeblack@google.comsticky_vars.AddVariables(
101912854Sgabeblack@google.com    EnumVariable('TARGET_ISA', 'Target ISA', 'alpha', all_isa_list),
102012854Sgabeblack@google.com    EnumVariable('TARGET_GPU_ISA', 'Target GPU ISA', 'hsail', all_gpu_isa_list),
102112854Sgabeblack@google.com    ListVariable('CPU_MODELS', 'CPU models',
102212854Sgabeblack@google.com                 sorted(n for n,m in CpuModel.dict.iteritems() if m.default),
102312854Sgabeblack@google.com                 sorted(CpuModel.dict.keys())),
102412854Sgabeblack@google.com    BoolVariable('EFENCE', 'Link with Electric Fence malloc debugger',
102512854Sgabeblack@google.com                 False),
102612854Sgabeblack@google.com    BoolVariable('SS_COMPATIBLE_FP',
102712854Sgabeblack@google.com                 'Make floating-point results compatible with SimpleScalar',
102812854Sgabeblack@google.com                 False),
102912854Sgabeblack@google.com    BoolVariable('USE_SSE2',
103012854Sgabeblack@google.com                 'Compile for SSE2 (-msse2) to get IEEE FP on x86 hosts',
103112854Sgabeblack@google.com                 False),
103212854Sgabeblack@google.com    BoolVariable('USE_POSIX_CLOCK', 'Use POSIX Clocks', have_posix_clock),
103312854Sgabeblack@google.com    BoolVariable('USE_FENV', 'Use <fenv.h> IEEE mode control', have_fenv),
103412854Sgabeblack@google.com    BoolVariable('USE_PNG',  'Enable support for PNG images', have_png),
103512854Sgabeblack@google.com    BoolVariable('CP_ANNOTATE', 'Enable critical path annotation capability',
103612854Sgabeblack@google.com                 False),
103712854Sgabeblack@google.com    BoolVariable('USE_KVM', 'Enable hardware virtualized (KVM) CPU models',
103812854Sgabeblack@google.com                 have_kvm),
103912854Sgabeblack@google.com    BoolVariable('USE_TUNTAP',
104012854Sgabeblack@google.com                 'Enable using a tap device to bridge to the host network',
104112854Sgabeblack@google.com                 have_tuntap),
104212854Sgabeblack@google.com    BoolVariable('BUILD_GPU', 'Build the compute-GPU model', False),
104312854Sgabeblack@google.com    EnumVariable('PROTOCOL', 'Coherence protocol for Ruby', 'None',
104412854Sgabeblack@google.com                  all_protocols),
104512854Sgabeblack@google.com    EnumVariable('BACKTRACE_IMPL', 'Post-mortem dump implementation',
104612854Sgabeblack@google.com                 backtrace_impls[-1], backtrace_impls)
104712854Sgabeblack@google.com    )
104812854Sgabeblack@google.com
104912854Sgabeblack@google.com# These variables get exported to #defines in config/*.hh (see src/SConscript).
105012854Sgabeblack@google.comexport_vars += ['USE_FENV', 'SS_COMPATIBLE_FP', 'TARGET_ISA', 'TARGET_GPU_ISA',
105112854Sgabeblack@google.com                'CP_ANNOTATE', 'USE_POSIX_CLOCK', 'USE_KVM', 'USE_TUNTAP',
105212854Sgabeblack@google.com                'PROTOCOL', 'HAVE_PROTOBUF', 'HAVE_PERF_ATTR_EXCLUDE_HOST',
105312854Sgabeblack@google.com                'USE_PNG']
105412854Sgabeblack@google.com
105512854Sgabeblack@google.com###################################################
105612854Sgabeblack@google.com#
105712854Sgabeblack@google.com# Define a SCons builder for configuration flag headers.
105812854Sgabeblack@google.com#
105912854Sgabeblack@google.com###################################################
106012854Sgabeblack@google.com
106112854Sgabeblack@google.com# This function generates a config header file that #defines the
106212854Sgabeblack@google.com# variable symbol to the current variable setting (0 or 1).  The source
106312854Sgabeblack@google.com# operands are the name of the variable and a Value node containing the
106412854Sgabeblack@google.com# value of the variable.
106512854Sgabeblack@google.comdef build_config_file(target, source, env):
106612854Sgabeblack@google.com    (variable, value) = [s.get_contents() for s in source]
106712854Sgabeblack@google.com    f = file(str(target[0]), 'w')
106812854Sgabeblack@google.com    print >> f, '#define', variable, value
106912854Sgabeblack@google.com    f.close()
107012854Sgabeblack@google.com    return None
107112854Sgabeblack@google.com
107212854Sgabeblack@google.com# Combine the two functions into a scons Action object.
107312854Sgabeblack@google.comconfig_action = MakeAction(build_config_file, Transform("CONFIG H", 2))
107412854Sgabeblack@google.com
107512854Sgabeblack@google.com# The emitter munges the source & target node lists to reflect what
107612854Sgabeblack@google.com# we're really doing.
107712854Sgabeblack@google.comdef config_emitter(target, source, env):
107812854Sgabeblack@google.com    # extract variable name from Builder arg
107912854Sgabeblack@google.com    variable = str(target[0])
108012854Sgabeblack@google.com    # True target is config header file
108112854Sgabeblack@google.com    target = joinpath('config', variable.lower() + '.hh')
108212854Sgabeblack@google.com    val = env[variable]
108312854Sgabeblack@google.com    if isinstance(val, bool):
108412854Sgabeblack@google.com        # Force value to 0/1
108512854Sgabeblack@google.com        val = int(val)
108612854Sgabeblack@google.com    elif isinstance(val, str):
108712854Sgabeblack@google.com        val = '"' + val + '"'
108812854Sgabeblack@google.com
108912854Sgabeblack@google.com    # Sources are variable name & value (packaged in SCons Value nodes)
109012854Sgabeblack@google.com    return ([target], [Value(variable), Value(val)])
109112854Sgabeblack@google.com
109212854Sgabeblack@google.comconfig_builder = Builder(emitter = config_emitter, action = config_action)
109312854Sgabeblack@google.com
109412854Sgabeblack@google.commain.Append(BUILDERS = { 'ConfigFile' : config_builder })
109512854Sgabeblack@google.com
109612854Sgabeblack@google.com###################################################
109712854Sgabeblack@google.com#
109812854Sgabeblack@google.com# Builders for static and shared partially linked object files.
109912854Sgabeblack@google.com#
110012854Sgabeblack@google.com###################################################
110112854Sgabeblack@google.com
110212854Sgabeblack@google.compartial_static_builder = Builder(action=SCons.Defaults.LinkAction,
110312854Sgabeblack@google.com                                 src_suffix='$OBJSUFFIX',
110412854Sgabeblack@google.com                                 src_builder=['StaticObject', 'Object'],
110512854Sgabeblack@google.com                                 LINKFLAGS='$PLINKFLAGS',
110612854Sgabeblack@google.com                                 LIBS='')
110712854Sgabeblack@google.com
110812854Sgabeblack@google.comdef partial_shared_emitter(target, source, env):
110912854Sgabeblack@google.com    for tgt in target:
111012854Sgabeblack@google.com        tgt.attributes.shared = 1
111112854Sgabeblack@google.com    return (target, source)
111212854Sgabeblack@google.compartial_shared_builder = Builder(action=SCons.Defaults.ShLinkAction,
111312854Sgabeblack@google.com                                 emitter=partial_shared_emitter,
111412854Sgabeblack@google.com                                 src_suffix='$SHOBJSUFFIX',
111512854Sgabeblack@google.com                                 src_builder='SharedObject',
111612854Sgabeblack@google.com                                 SHLINKFLAGS='$PSHLINKFLAGS',
111712854Sgabeblack@google.com                                 LIBS='')
111812854Sgabeblack@google.com
111912854Sgabeblack@google.commain.Append(BUILDERS = { 'PartialShared' : partial_shared_builder,
112012854Sgabeblack@google.com                         'PartialStatic' : partial_static_builder })
112112854Sgabeblack@google.com
112212854Sgabeblack@google.com# builds in ext are shared across all configs in the build root.
112312854Sgabeblack@google.comext_dir = abspath(joinpath(str(main.root), 'ext'))
112412854Sgabeblack@google.comext_build_dirs = []
112512854Sgabeblack@google.comfor root, dirs, files in os.walk(ext_dir):
112612854Sgabeblack@google.com    if 'SConscript' in files:
112712854Sgabeblack@google.com        build_dir = os.path.relpath(root, ext_dir)
112812854Sgabeblack@google.com        ext_build_dirs.append(build_dir)
112912854Sgabeblack@google.com        main.SConscript(joinpath(root, 'SConscript'),
113012854Sgabeblack@google.com                        variant_dir=joinpath(build_root, build_dir))
113112854Sgabeblack@google.com
113212854Sgabeblack@google.commain.Prepend(CPPPATH=Dir('ext/pybind11/include/'))
113312854Sgabeblack@google.com
113412854Sgabeblack@google.com###################################################
113512854Sgabeblack@google.com#
113612854Sgabeblack@google.com# This builder and wrapper method are used to set up a directory with
113712854Sgabeblack@google.com# switching headers. Those are headers which are in a generic location and
113812854Sgabeblack@google.com# that include more specific headers from a directory chosen at build time
113912854Sgabeblack@google.com# based on the current build settings.
114012854Sgabeblack@google.com#
114112854Sgabeblack@google.com###################################################
114212854Sgabeblack@google.com
114312854Sgabeblack@google.comdef build_switching_header(target, source, env):
114412854Sgabeblack@google.com    path = str(target[0])
114512854Sgabeblack@google.com    subdir = str(source[0])
114612854Sgabeblack@google.com    dp, fp = os.path.split(path)
114712854Sgabeblack@google.com    dp = os.path.relpath(os.path.realpath(dp),
114812854Sgabeblack@google.com                         os.path.realpath(env['BUILDDIR']))
114912854Sgabeblack@google.com    with open(path, 'w') as hdr:
115012854Sgabeblack@google.com        print >>hdr, '#include "%s/%s/%s"' % (dp, subdir, fp)
115112854Sgabeblack@google.com
115212854Sgabeblack@google.comswitching_header_action = MakeAction(build_switching_header,
115312854Sgabeblack@google.com                                     Transform('GENERATE'))
115412854Sgabeblack@google.com
115512854Sgabeblack@google.comswitching_header_builder = Builder(action=switching_header_action,
115612854Sgabeblack@google.com                                   source_factory=Value,
115712854Sgabeblack@google.com                                   single_source=True)
115812854Sgabeblack@google.com
115912854Sgabeblack@google.commain.Append(BUILDERS = { 'SwitchingHeader': switching_header_builder })
116012854Sgabeblack@google.com
116112854Sgabeblack@google.comdef switching_headers(self, headers, source):
116212854Sgabeblack@google.com    for header in headers:
116312854Sgabeblack@google.com        self.SwitchingHeader(header, source)
116412854Sgabeblack@google.com
116512854Sgabeblack@google.commain.AddMethod(switching_headers, 'SwitchingHeaders')
116612854Sgabeblack@google.com
116712854Sgabeblack@google.com###################################################
116812854Sgabeblack@google.com#
116912854Sgabeblack@google.com# Define build environments for selected configurations.
117012854Sgabeblack@google.com#
117112854Sgabeblack@google.com###################################################
117212854Sgabeblack@google.com
117312854Sgabeblack@google.comfor variant_path in variant_paths:
117412854Sgabeblack@google.com    if not GetOption('silent'):
117512854Sgabeblack@google.com        print "Building in", variant_path
117612854Sgabeblack@google.com
117712854Sgabeblack@google.com    # Make a copy of the build-root environment to use for this config.
117812854Sgabeblack@google.com    env = main.Clone()
117912854Sgabeblack@google.com    env['BUILDDIR'] = variant_path
118012854Sgabeblack@google.com
118112854Sgabeblack@google.com    # variant_dir is the tail component of build path, and is used to
118212854Sgabeblack@google.com    # determine the build parameters (e.g., 'ALPHA_SE')
118312854Sgabeblack@google.com    (build_root, variant_dir) = splitpath(variant_path)
118412854Sgabeblack@google.com
118512854Sgabeblack@google.com    # Set env variables according to the build directory config.
118612854Sgabeblack@google.com    sticky_vars.files = []
118712854Sgabeblack@google.com    # Variables for $BUILD_ROOT/$VARIANT_DIR are stored in
118812854Sgabeblack@google.com    # $BUILD_ROOT/variables/$VARIANT_DIR so you can nuke
118912854Sgabeblack@google.com    # $BUILD_ROOT/$VARIANT_DIR without losing your variables settings.
119012854Sgabeblack@google.com    current_vars_file = joinpath(build_root, 'variables', variant_dir)
119112854Sgabeblack@google.com    if isfile(current_vars_file):
119212854Sgabeblack@google.com        sticky_vars.files.append(current_vars_file)
119312854Sgabeblack@google.com        if not GetOption('silent'):
119412854Sgabeblack@google.com            print "Using saved variables file %s" % current_vars_file
119512854Sgabeblack@google.com    elif variant_dir in ext_build_dirs:
119612854Sgabeblack@google.com        # Things in ext are built without a variant directory.
119712854Sgabeblack@google.com        continue
119812854Sgabeblack@google.com    else:
119912854Sgabeblack@google.com        # Build dir-specific variables file doesn't exist.
120012854Sgabeblack@google.com
120112854Sgabeblack@google.com        # Make sure the directory is there so we can create it later
120212854Sgabeblack@google.com        opt_dir = dirname(current_vars_file)
120312854Sgabeblack@google.com        if not isdir(opt_dir):
120412854Sgabeblack@google.com            mkdir(opt_dir)
120512854Sgabeblack@google.com
120612854Sgabeblack@google.com        # Get default build variables from source tree.  Variables are
120712854Sgabeblack@google.com        # normally determined by name of $VARIANT_DIR, but can be
120812854Sgabeblack@google.com        # overridden by '--default=' arg on command line.
120912854Sgabeblack@google.com        default = GetOption('default')
121012854Sgabeblack@google.com        opts_dir = joinpath(main.root.abspath, 'build_opts')
121112854Sgabeblack@google.com        if default:
121212854Sgabeblack@google.com            default_vars_files = [joinpath(build_root, 'variables', default),
121312854Sgabeblack@google.com                                  joinpath(opts_dir, default)]
121412854Sgabeblack@google.com        else:
121512854Sgabeblack@google.com            default_vars_files = [joinpath(opts_dir, variant_dir)]
121612854Sgabeblack@google.com        existing_files = filter(isfile, default_vars_files)
121712854Sgabeblack@google.com        if existing_files:
121812854Sgabeblack@google.com            default_vars_file = existing_files[0]
121912854Sgabeblack@google.com            sticky_vars.files.append(default_vars_file)
122012854Sgabeblack@google.com            print "Variables file %s not found,\n  using defaults in %s" \
122112854Sgabeblack@google.com                  % (current_vars_file, default_vars_file)
122212854Sgabeblack@google.com        else:
122312854Sgabeblack@google.com            print "Error: cannot find variables file %s or " \
122412854Sgabeblack@google.com                  "default file(s) %s" \
122512854Sgabeblack@google.com                  % (current_vars_file, ' or '.join(default_vars_files))
122612854Sgabeblack@google.com            Exit(1)
122712854Sgabeblack@google.com
122812854Sgabeblack@google.com    # Apply current variable settings to env
122912854Sgabeblack@google.com    sticky_vars.Update(env)
123012854Sgabeblack@google.com
123112854Sgabeblack@google.com    help_texts["local_vars"] += \
123212854Sgabeblack@google.com        "Build variables for %s:\n" % variant_dir \
123312854Sgabeblack@google.com                 + sticky_vars.GenerateHelpText(env)
123412854Sgabeblack@google.com
123512854Sgabeblack@google.com    # Process variable settings.
123612854Sgabeblack@google.com
123712854Sgabeblack@google.com    if not have_fenv and env['USE_FENV']:
123812854Sgabeblack@google.com        print "Warning: <fenv.h> not available; " \
123912854Sgabeblack@google.com              "forcing USE_FENV to False in", variant_dir + "."
124012854Sgabeblack@google.com        env['USE_FENV'] = False
124112854Sgabeblack@google.com
124212854Sgabeblack@google.com    if not env['USE_FENV']:
124312854Sgabeblack@google.com        print "Warning: No IEEE FP rounding mode control in", variant_dir + "."
124412854Sgabeblack@google.com        print "         FP results may deviate slightly from other platforms."
124512854Sgabeblack@google.com
124612854Sgabeblack@google.com    if not have_png and env['USE_PNG']:
124712854Sgabeblack@google.com        print "Warning: <png.h> not available; " \
124812854Sgabeblack@google.com              "forcing USE_PNG to False in", variant_dir + "."
124912854Sgabeblack@google.com        env['USE_PNG'] = False
125012854Sgabeblack@google.com
125112854Sgabeblack@google.com    if env['USE_PNG']:
125212854Sgabeblack@google.com        env.Append(LIBS=['png'])
125312854Sgabeblack@google.com
125412854Sgabeblack@google.com    if env['EFENCE']:
125512854Sgabeblack@google.com        env.Append(LIBS=['efence'])
125612854Sgabeblack@google.com
125712854Sgabeblack@google.com    if env['USE_KVM']:
125812854Sgabeblack@google.com        if not have_kvm:
125912854Sgabeblack@google.com            print "Warning: Can not enable KVM, host seems to lack KVM support"
126012854Sgabeblack@google.com            env['USE_KVM'] = False
126112854Sgabeblack@google.com        elif not is_isa_kvm_compatible(env['TARGET_ISA']):
126212854Sgabeblack@google.com            print "Info: KVM support disabled due to unsupported host and " \
126312854Sgabeblack@google.com                "target ISA combination"
126412854Sgabeblack@google.com            env['USE_KVM'] = False
126512854Sgabeblack@google.com
126612854Sgabeblack@google.com    if env['USE_TUNTAP']:
126712854Sgabeblack@google.com        if not have_tuntap:
126812854Sgabeblack@google.com            print "Warning: Can't connect EtherTap with a tap device."
126912854Sgabeblack@google.com            env['USE_TUNTAP'] = False
127012854Sgabeblack@google.com
127112854Sgabeblack@google.com    if env['BUILD_GPU']:
127212854Sgabeblack@google.com        env.Append(CPPDEFINES=['BUILD_GPU'])
127312854Sgabeblack@google.com
127412854Sgabeblack@google.com    # Warn about missing optional functionality
127512854Sgabeblack@google.com    if env['USE_KVM']:
127612854Sgabeblack@google.com        if not main['HAVE_PERF_ATTR_EXCLUDE_HOST']:
127712854Sgabeblack@google.com            print "Warning: perf_event headers lack support for the " \
127812854Sgabeblack@google.com                "exclude_host attribute. KVM instruction counts will " \
127912854Sgabeblack@google.com                "be inaccurate."
128012854Sgabeblack@google.com
128112854Sgabeblack@google.com    # Save sticky variable settings back to current variables file
128212854Sgabeblack@google.com    sticky_vars.Save(current_vars_file, env)
128312854Sgabeblack@google.com
128412854Sgabeblack@google.com    if env['USE_SSE2']:
128512854Sgabeblack@google.com        env.Append(CCFLAGS=['-msse2'])
128612854Sgabeblack@google.com
128712854Sgabeblack@google.com    # The src/SConscript file sets up the build rules in 'env' according
128812854Sgabeblack@google.com    # to the configured variables.  It returns a list of environments,
128912854Sgabeblack@google.com    # one for each variant build (debug, opt, etc.)
129012854Sgabeblack@google.com    SConscript('src/SConscript', variant_dir = variant_path, exports = 'env')
129112854Sgabeblack@google.com
129212854Sgabeblack@google.com# base help text
129312854Sgabeblack@google.comHelp('''
129412854Sgabeblack@google.comUsage: scons [scons options] [build variables] [target(s)]
129512854Sgabeblack@google.com
129612854Sgabeblack@google.comExtra scons options:
129712854Sgabeblack@google.com%(options)s
129812854Sgabeblack@google.com
129912854Sgabeblack@google.comGlobal build variables:
130012854Sgabeblack@google.com%(global_vars)s
130112854Sgabeblack@google.com
130212854Sgabeblack@google.com%(local_vars)s
130312854Sgabeblack@google.com''' % help_texts)
130412854Sgabeblack@google.com