SConstruct revision 12304:299452fa8cc4
12568SN/A# -*- mode:python -*-
22568SN/A
32568SN/A# Copyright (c) 2013, 2015-2017 ARM Limited
42568SN/A# All rights reserved.
52568SN/A#
62568SN/A# The license below extends only to copyright in the software and shall
72568SN/A# not be construed as granting a license to any other intellectual
82568SN/A# property including but not limited to intellectual property relating
92568SN/A# to a hardware implementation of the functionality of the software
102568SN/A# licensed hereunder.  You may use the software subject to the license
112568SN/A# terms below provided that you ensure that this notice is replicated
122568SN/A# unmodified and in its entirety in all distributions of the software,
132568SN/A# modified or unmodified, in source code or in binary form.
142568SN/A#
152568SN/A# Copyright (c) 2011 Advanced Micro Devices, Inc.
162568SN/A# Copyright (c) 2009 The Hewlett-Packard Development Company
172568SN/A# Copyright (c) 2004-2005 The Regents of The University of Michigan
182568SN/A# All rights reserved.
192568SN/A#
202568SN/A# Redistribution and use in source and binary forms, with or without
212568SN/A# modification, are permitted provided that the following conditions are
222568SN/A# met: redistributions of source code must retain the above copyright
232568SN/A# notice, this list of conditions and the following disclaimer;
242568SN/A# redistributions in binary form must reproduce the above copyright
252568SN/A# notice, this list of conditions and the following disclaimer in the
262568SN/A# documentation and/or other materials provided with the distribution;
272665Ssaidi@eecs.umich.edu# neither the name of the copyright holders nor the names of its
282665Ssaidi@eecs.umich.edu# contributors may be used to endorse or promote products derived from
292665Ssaidi@eecs.umich.edu# this software without specific prior written permission.
302568SN/A#
312568SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
322568SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
332982Sstever@eecs.umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
342982Sstever@eecs.umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
352568SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
362568SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
372568SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
382568SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
392568SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
402568SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
412568SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
422568SN/A#
432568SN/A# Authors: Steve Reinhardt
442568SN/A#          Nathan Binkert
452568SN/A
462568SN/A###################################################
472568SN/A#
482568SN/A# SCons top-level build description (SConstruct) file.
492568SN/A#
502568SN/A# While in this directory ('gem5'), just type 'scons' to build the default
512568SN/A# configuration (see below), or type 'scons build/<CONFIG>/<binary>'
522568SN/A# to build some other configuration (e.g., 'build/ALPHA/gem5.opt' for
532982Sstever@eecs.umich.edu# the optimized full-system version).
542568SN/A#
552568SN/A# You can build gem5 in a different directory as long as there is a
562568SN/A# 'build/<CONFIG>' somewhere along the target path.  The build system
572643Sstever@eecs.umich.edu# expects that all configs under the same build directory are being
582568SN/A# built for the same host system.
592568SN/A#
602643Sstever@eecs.umich.edu# Examples:
612643Sstever@eecs.umich.edu#
622643Sstever@eecs.umich.edu#   The following two commands are equivalent.  The '-u' option tells
632643Sstever@eecs.umich.edu#   scons to search up the directory tree for this SConstruct file.
642643Sstever@eecs.umich.edu#   % cd <path-to-src>/gem5 ; scons build/ALPHA/gem5.debug
652643Sstever@eecs.umich.edu#   % cd <path-to-src>/gem5/build/ALPHA; scons -u gem5.debug
662643Sstever@eecs.umich.edu#
672643Sstever@eecs.umich.edu#   The following two commands are equivalent and demonstrate building
682643Sstever@eecs.umich.edu#   in a directory outside of the source tree.  The '-C' option tells
694432Ssaidi@eecs.umich.edu#   scons to chdir to the specified directory to find this SConstruct
704432Ssaidi@eecs.umich.edu#   file.
712643Sstever@eecs.umich.edu#   % cd <path-to-src>/gem5 ; scons /local/foo/build/ALPHA/gem5.debug
722643Sstever@eecs.umich.edu#   % cd /local/foo/build/ALPHA; scons -C <path-to-src>/gem5 gem5.debug
732643Sstever@eecs.umich.edu#
742643Sstever@eecs.umich.edu# You can use 'scons -H' to print scons options.  If you're in this
753349Sbinkertn@umich.edu# 'gem5' directory (or use -u or -C to tell scons where to find this
762643Sstever@eecs.umich.edu# file), you can use 'scons -h' to print all the gem5-specific build
772643Sstever@eecs.umich.edu# options as well.
782643Sstever@eecs.umich.edu#
792643Sstever@eecs.umich.edu###################################################
804432Ssaidi@eecs.umich.edu
814432Ssaidi@eecs.umich.edu# Global Python includes
824433Ssaidi@eecs.umich.eduimport itertools
834432Ssaidi@eecs.umich.eduimport os
844433Ssaidi@eecs.umich.eduimport re
852643Sstever@eecs.umich.eduimport shutil
862643Sstever@eecs.umich.eduimport subprocess
874433Ssaidi@eecs.umich.eduimport sys
884433Ssaidi@eecs.umich.edu
894433Ssaidi@eecs.umich.edufrom os import mkdir, environ
902643Sstever@eecs.umich.edufrom os.path import abspath, basename, dirname, expanduser, normpath
914433Ssaidi@eecs.umich.edufrom os.path import exists,  isdir, isfile
922657Ssaidi@eecs.umich.edufrom os.path import join as joinpath, split as splitpath
932643Sstever@eecs.umich.edu
942643Sstever@eecs.umich.edu# SCons includes
953349Sbinkertn@umich.eduimport SCons
962643Sstever@eecs.umich.eduimport SCons.Node
972643Sstever@eecs.umich.edu
982643Sstever@eecs.umich.edufrom m5.util import compareVersions, readCommand
992643Sstever@eecs.umich.edu
1004432Ssaidi@eecs.umich.eduhelp_texts = {
1014432Ssaidi@eecs.umich.edu    "options" : "",
1022643Sstever@eecs.umich.edu    "global_vars" : "",
1034432Ssaidi@eecs.umich.edu    "local_vars" : ""
1044432Ssaidi@eecs.umich.edu}
1054432Ssaidi@eecs.umich.edu
1064432Ssaidi@eecs.umich.eduExport("help_texts")
1074432Ssaidi@eecs.umich.edu
1084432Ssaidi@eecs.umich.edu
1094432Ssaidi@eecs.umich.edu# There's a bug in scons in that (1) by default, the help texts from
1104432Ssaidi@eecs.umich.edu# AddOption() are supposed to be displayed when you type 'scons -h'
1114432Ssaidi@eecs.umich.edu# and (2) you can override the help displayed by 'scons -h' using the
1124432Ssaidi@eecs.umich.edu# Help() function, but these two features are incompatible: once
1134432Ssaidi@eecs.umich.edu# you've overridden the help text using Help(), there's no way to get
1144432Ssaidi@eecs.umich.edu# at the help texts from AddOptions.  See:
1154432Ssaidi@eecs.umich.edu#     http://scons.tigris.org/issues/show_bug.cgi?id=2356
1164432Ssaidi@eecs.umich.edu#     http://scons.tigris.org/issues/show_bug.cgi?id=2611
1174432Ssaidi@eecs.umich.edu# This hack lets us extract the help text from AddOptions and
1184432Ssaidi@eecs.umich.edu# re-inject it via Help().  Ideally someday this bug will be fixed and
1194432Ssaidi@eecs.umich.edu# we can just use AddOption directly.
1204432Ssaidi@eecs.umich.edudef AddLocalOption(*args, **kwargs):
1214432Ssaidi@eecs.umich.edu    col_width = 30
1224432Ssaidi@eecs.umich.edu
1234432Ssaidi@eecs.umich.edu    help = "  " + ", ".join(args)
1244432Ssaidi@eecs.umich.edu    if "help" in kwargs:
1254432Ssaidi@eecs.umich.edu        length = len(help)
1264432Ssaidi@eecs.umich.edu        if length >= col_width:
1274432Ssaidi@eecs.umich.edu            help += "\n" + " " * col_width
1284432Ssaidi@eecs.umich.edu        else:
1294432Ssaidi@eecs.umich.edu            help += " " * (col_width - length)
1304432Ssaidi@eecs.umich.edu        help += kwargs["help"]
1314432Ssaidi@eecs.umich.edu    help_texts["options"] += help + "\n"
1324432Ssaidi@eecs.umich.edu
1334432Ssaidi@eecs.umich.edu    AddOption(*args, **kwargs)
1344432Ssaidi@eecs.umich.edu
1354432Ssaidi@eecs.umich.eduAddLocalOption('--colors', dest='use_colors', action='store_true',
1364432Ssaidi@eecs.umich.edu               help="Add color to abbreviated scons output")
1374432Ssaidi@eecs.umich.eduAddLocalOption('--no-colors', dest='use_colors', action='store_false',
1384432Ssaidi@eecs.umich.edu               help="Don't add color to abbreviated scons output")
1394432Ssaidi@eecs.umich.eduAddLocalOption('--with-cxx-config', dest='with_cxx_config',
1402643Sstever@eecs.umich.edu               action='store_true',
1412643Sstever@eecs.umich.edu               help="Build with support for C++-based configuration")
1422643Sstever@eecs.umich.eduAddLocalOption('--default', dest='default', type='string', action='store',
1432643Sstever@eecs.umich.edu               help='Override which build_opts file to use for defaults')
1442643Sstever@eecs.umich.eduAddLocalOption('--ignore-style', dest='ignore_style', action='store_true',
1452643Sstever@eecs.umich.edu               help='Disable style checking hooks')
1462643Sstever@eecs.umich.eduAddLocalOption('--no-lto', dest='no_lto', action='store_true',
1472643Sstever@eecs.umich.edu               help='Disable Link-Time Optimization for fast')
1482643Sstever@eecs.umich.eduAddLocalOption('--force-lto', dest='force_lto', action='store_true',
1492643Sstever@eecs.umich.edu               help='Use Link-Time Optimization instead of partial linking' +
1504433Ssaidi@eecs.umich.edu                    ' when the compiler doesn\'t support using them together.')
1512643Sstever@eecs.umich.eduAddLocalOption('--update-ref', dest='update_ref', action='store_true',
1522643Sstever@eecs.umich.edu               help='Update test reference outputs')
1532643Sstever@eecs.umich.eduAddLocalOption('--verbose', dest='verbose', action='store_true',
1542643Sstever@eecs.umich.edu               help='Print full tool command lines')
1552643Sstever@eecs.umich.eduAddLocalOption('--without-python', dest='without_python',
1562643Sstever@eecs.umich.edu               action='store_true',
1572643Sstever@eecs.umich.edu               help='Build without Python configuration support')
1584433Ssaidi@eecs.umich.eduAddLocalOption('--without-tcmalloc', dest='without_tcmalloc',
1592643Sstever@eecs.umich.edu               action='store_true',
1604433Ssaidi@eecs.umich.edu               help='Disable linking against tcmalloc')
1612643Sstever@eecs.umich.eduAddLocalOption('--with-ubsan', dest='with_ubsan', action='store_true',
1622643Sstever@eecs.umich.edu               help='Build with Undefined Behavior Sanitizer if available')
1632643Sstever@eecs.umich.eduAddLocalOption('--with-asan', dest='with_asan', action='store_true',
1644433Ssaidi@eecs.umich.edu               help='Build with Address Sanitizer if available')
1654433Ssaidi@eecs.umich.edu
1662643Sstever@eecs.umich.eduif GetOption('no_lto') and GetOption('force_lto'):
1672643Sstever@eecs.umich.edu    print '--no-lto and --force-lto are mutually exclusive'
1682643Sstever@eecs.umich.edu    Exit(1)
1692643Sstever@eecs.umich.edu
1702643Sstever@eecs.umich.edu########################################################################
1712643Sstever@eecs.umich.edu#
1722643Sstever@eecs.umich.edu# Set up the main build environment.
1732643Sstever@eecs.umich.edu#
1742643Sstever@eecs.umich.edu########################################################################
1752643Sstever@eecs.umich.edu
1762643Sstever@eecs.umich.edumain = Environment()
1772643Sstever@eecs.umich.edu
1782643Sstever@eecs.umich.edufrom gem5_scons import Transform
1792643Sstever@eecs.umich.edufrom gem5_scons.util import get_termcap
1802643Sstever@eecs.umich.edutermcap = get_termcap()
1812643Sstever@eecs.umich.edu
1822643Sstever@eecs.umich.edumain_dict_keys = main.Dictionary().keys()
1832643Sstever@eecs.umich.edu
1842643Sstever@eecs.umich.edu# Check that we have a C/C++ compiler
1852643Sstever@eecs.umich.eduif not ('CC' in main_dict_keys and 'CXX' in main_dict_keys):
1862643Sstever@eecs.umich.edu    print "No C++ compiler installed (package g++ on Ubuntu and RedHat)"
1872568SN/A    Exit(1)
1882568SN/A
1892568SN/A###################################################
1902568SN/A#
1912643Sstever@eecs.umich.edu# Figure out which configurations to set up based on the path(s) of
1922643Sstever@eecs.umich.edu# the target(s).
1934432Ssaidi@eecs.umich.edu#
1942568SN/A###################################################
1952568SN/A
1962568SN/A# Find default configuration & binary.
1972643Sstever@eecs.umich.eduDefault(environ.get('M5_DEFAULT_BINARY', 'build/ALPHA/gem5.debug'))
1982643Sstever@eecs.umich.edu
1993349Sbinkertn@umich.edu# helper function: find last occurrence of element in list
2002568SN/Adef rfind(l, elt, offs = -1):
2012643Sstever@eecs.umich.edu    for i in range(len(l)+offs, 0, -1):
2022568SN/A        if l[i] == elt:
2032657Ssaidi@eecs.umich.edu            return i
2042568SN/A    raise ValueError, "element not found"
2052643Sstever@eecs.umich.edu
2062568SN/A# Take a list of paths (or SCons Nodes) and return a list with all
2073349Sbinkertn@umich.edu# paths made absolute and ~-expanded.  Paths will be interpreted
2082568SN/A# relative to the launch directory unless a different root is provided
2092643Sstever@eecs.umich.edudef makePathListAbsolute(path_list, root=GetLaunchDir()):
2102568SN/A    return [abspath(joinpath(root, expanduser(str(p))))
2113349Sbinkertn@umich.edu            for p in path_list]
2122568SN/A
2132643Sstever@eecs.umich.edu# Each target must have 'build' in the interior of the path; the
2142568SN/A# directory below this will determine the build parameters.  For
2152643Sstever@eecs.umich.edu# example, for target 'foo/bar/build/ALPHA_SE/arch/alpha/blah.do' we
2162568SN/A# recognize that ALPHA_SE specifies the configuration because it
2172643Sstever@eecs.umich.edu# follow 'build' in the build path.
2182568SN/A
2192643Sstever@eecs.umich.edu# The funky assignment to "[:]" is needed to replace the list contents
2202643Sstever@eecs.umich.edu# in place rather than reassign the symbol to a new list, which
2212568SN/A# doesn't work (obviously!).
2222568SN/ABUILD_TARGETS[:] = makePathListAbsolute(BUILD_TARGETS)
2232643Sstever@eecs.umich.edu
2242568SN/A# Generate a list of the unique build roots and configs that the
2252568SN/A# collected targets reference.
2262568SN/Avariant_paths = []
2272568SN/Abuild_root = None
2282568SN/Afor t in BUILD_TARGETS:
2292568SN/A    path_dirs = t.split('/')
2302568SN/A    try:
2312738Sstever@eecs.umich.edu        build_top = rfind(path_dirs, 'build', -2)
2322568SN/A    except:
2332568SN/A        print "Error: no non-leaf 'build' dir found on target path", t
2342568SN/A        Exit(1)
2354432Ssaidi@eecs.umich.edu    this_build_root = joinpath('/',*path_dirs[:build_top+1])
2364432Ssaidi@eecs.umich.edu    if not build_root:
2372568SN/A        build_root = this_build_root
2382568SN/A    else:
2392568SN/A        if this_build_root != build_root:
240            print "Error: build targets not under same build root\n"\
241                  "  %s\n  %s" % (build_root, this_build_root)
242            Exit(1)
243    variant_path = joinpath('/',*path_dirs[:build_top+2])
244    if variant_path not in variant_paths:
245        variant_paths.append(variant_path)
246
247# Make sure build_root exists (might not if this is the first build there)
248if not isdir(build_root):
249    mkdir(build_root)
250main['BUILDROOT'] = build_root
251
252Export('main')
253
254main.SConsignFile(joinpath(build_root, "sconsign"))
255
256# Default duplicate option is to use hard links, but this messes up
257# when you use emacs to edit a file in the target dir, as emacs moves
258# file to file~ then copies to file, breaking the link.  Symbolic
259# (soft) links work better.
260main.SetOption('duplicate', 'soft-copy')
261
262#
263# Set up global sticky variables... these are common to an entire build
264# tree (not specific to a particular build like ALPHA_SE)
265#
266
267global_vars_file = joinpath(build_root, 'variables.global')
268
269global_vars = Variables(global_vars_file, args=ARGUMENTS)
270
271global_vars.AddVariables(
272    ('CC', 'C compiler', environ.get('CC', main['CC'])),
273    ('CXX', 'C++ compiler', environ.get('CXX', main['CXX'])),
274    ('PROTOC', 'protoc tool', environ.get('PROTOC', 'protoc')),
275    ('BATCH', 'Use batch pool for build and tests', False),
276    ('BATCH_CMD', 'Batch pool submission command name', 'qdo'),
277    ('M5_BUILD_CACHE', 'Cache built objects in this directory', False),
278    ('EXTRAS', 'Add extra directories to the compilation', '')
279    )
280
281# Update main environment with values from ARGUMENTS & global_vars_file
282global_vars.Update(main)
283help_texts["global_vars"] += global_vars.GenerateHelpText(main)
284
285# Save sticky variable settings back to current variables file
286global_vars.Save(global_vars_file, main)
287
288# Parse EXTRAS variable to build list of all directories where we're
289# look for sources etc.  This list is exported as extras_dir_list.
290base_dir = main.srcdir.abspath
291if main['EXTRAS']:
292    extras_dir_list = makePathListAbsolute(main['EXTRAS'].split(':'))
293else:
294    extras_dir_list = []
295
296Export('base_dir')
297Export('extras_dir_list')
298
299# the ext directory should be on the #includes path
300main.Append(CPPPATH=[Dir('ext')])
301
302# Add shared top-level headers
303main.Prepend(CPPPATH=Dir('include'))
304
305if GetOption('verbose'):
306    def MakeAction(action, string, *args, **kwargs):
307        return Action(action, *args, **kwargs)
308else:
309    MakeAction = Action
310    main['CCCOMSTR']        = Transform("CC")
311    main['CXXCOMSTR']       = Transform("CXX")
312    main['ASCOMSTR']        = Transform("AS")
313    main['ARCOMSTR']        = Transform("AR", 0)
314    main['LINKCOMSTR']      = Transform("LINK", 0)
315    main['SHLINKCOMSTR']    = Transform("SHLINK", 0)
316    main['RANLIBCOMSTR']    = Transform("RANLIB", 0)
317    main['M4COMSTR']        = Transform("M4")
318    main['SHCCCOMSTR']      = Transform("SHCC")
319    main['SHCXXCOMSTR']     = Transform("SHCXX")
320Export('MakeAction')
321
322# Initialize the Link-Time Optimization (LTO) flags
323main['LTO_CCFLAGS'] = []
324main['LTO_LDFLAGS'] = []
325
326# According to the readme, tcmalloc works best if the compiler doesn't
327# assume that we're using the builtin malloc and friends. These flags
328# are compiler-specific, so we need to set them after we detect which
329# compiler we're using.
330main['TCMALLOC_CCFLAGS'] = []
331
332CXX_version = readCommand([main['CXX'],'--version'], exception=False)
333CXX_V = readCommand([main['CXX'],'-V'], exception=False)
334
335main['GCC'] = CXX_version and CXX_version.find('g++') >= 0
336main['CLANG'] = CXX_version and CXX_version.find('clang') >= 0
337if main['GCC'] + main['CLANG'] > 1:
338    print 'Error: How can we have two at the same time?'
339    Exit(1)
340
341# Set up default C++ compiler flags
342if main['GCC'] or main['CLANG']:
343    # As gcc and clang share many flags, do the common parts here
344    main.Append(CCFLAGS=['-pipe'])
345    main.Append(CCFLAGS=['-fno-strict-aliasing'])
346    # Enable -Wall and -Wextra and then disable the few warnings that
347    # we consistently violate
348    main.Append(CCFLAGS=['-Wall', '-Wundef', '-Wextra',
349                         '-Wno-sign-compare', '-Wno-unused-parameter'])
350    # We always compile using C++11
351    main.Append(CXXFLAGS=['-std=c++11'])
352    if sys.platform.startswith('freebsd'):
353        main.Append(CCFLAGS=['-I/usr/local/include'])
354        main.Append(CXXFLAGS=['-I/usr/local/include'])
355
356    main['FILTER_PSHLINKFLAGS'] = lambda x: str(x).replace(' -shared', '')
357    main['PSHLINKFLAGS'] = main.subst('${FILTER_PSHLINKFLAGS(SHLINKFLAGS)}')
358    main['PLINKFLAGS'] = main.subst('${LINKFLAGS}')
359    shared_partial_flags = ['-r', '-nostdlib']
360    main.Append(PSHLINKFLAGS=shared_partial_flags)
361    main.Append(PLINKFLAGS=shared_partial_flags)
362else:
363    print termcap.Yellow + termcap.Bold + 'Error' + termcap.Normal,
364    print "Don't know what compiler options to use for your compiler."
365    print termcap.Yellow + '       compiler:' + termcap.Normal, main['CXX']
366    print termcap.Yellow + '       version:' + termcap.Normal,
367    if not CXX_version:
368        print termcap.Yellow + termcap.Bold + "COMMAND NOT FOUND!" +\
369               termcap.Normal
370    else:
371        print CXX_version.replace('\n', '<nl>')
372    print "       If you're trying to use a compiler other than GCC"
373    print "       or clang, there appears to be something wrong with your"
374    print "       environment."
375    print "       "
376    print "       If you are trying to use a compiler other than those listed"
377    print "       above you will need to ease fix SConstruct and "
378    print "       src/SConscript to support that compiler."
379    Exit(1)
380
381if main['GCC']:
382    # Check for a supported version of gcc. >= 4.8 is chosen for its
383    # level of c++11 support. See
384    # http://gcc.gnu.org/projects/cxx0x.html for details.
385    gcc_version = readCommand([main['CXX'], '-dumpversion'], exception=False)
386    if compareVersions(gcc_version, "4.8") < 0:
387        print 'Error: gcc version 4.8 or newer required.'
388        print '       Installed version:', gcc_version
389        Exit(1)
390
391    main['GCC_VERSION'] = gcc_version
392
393    if compareVersions(gcc_version, '4.9') >= 0:
394        # Incremental linking with LTO is currently broken in gcc versions
395        # 4.9 and above. A version where everything works completely hasn't
396        # yet been identified.
397        #
398        # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67548
399        main['BROKEN_INCREMENTAL_LTO'] = True
400    if compareVersions(gcc_version, '6.0') >= 0:
401        # gcc versions 6.0 and greater accept an -flinker-output flag which
402        # selects what type of output the linker should generate. This is
403        # necessary for incremental lto to work, but is also broken in
404        # current versions of gcc. It may not be necessary in future
405        # versions. We add it here since it might be, and as a reminder that
406        # it exists. It's excluded if lto is being forced.
407        #
408        # https://gcc.gnu.org/gcc-6/changes.html
409        # https://gcc.gnu.org/ml/gcc-patches/2015-11/msg03161.html
410        # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69866
411        if not GetOption('force_lto'):
412            main.Append(PSHLINKFLAGS='-flinker-output=rel')
413            main.Append(PLINKFLAGS='-flinker-output=rel')
414
415    # gcc from version 4.8 and above generates "rep; ret" instructions
416    # to avoid performance penalties on certain AMD chips. Older
417    # assemblers detect this as an error, "Error: expecting string
418    # instruction after `rep'"
419    as_version_raw = readCommand([main['AS'], '-v', '/dev/null',
420                                  '-o', '/dev/null'],
421                                 exception=False).split()
422
423    # version strings may contain extra distro-specific
424    # qualifiers, so play it safe and keep only what comes before
425    # the first hyphen
426    as_version = as_version_raw[-1].split('-')[0] if as_version_raw else None
427
428    if not as_version or compareVersions(as_version, "2.23") < 0:
429        print termcap.Yellow + termcap.Bold + \
430            'Warning: This combination of gcc and binutils have' + \
431            ' known incompatibilities.\n' + \
432            '         If you encounter build problems, please update ' + \
433            'binutils to 2.23.' + \
434            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
476    # The address sanitizer is available for gcc >= 4.8
477    if GetOption('with_asan'):
478        if GetOption('with_ubsan') and \
479                compareVersions(env['GCC_VERSION'], '4.9') >= 0:
480            env.Append(CCFLAGS=['-fsanitize=address,undefined',
481                                '-fno-omit-frame-pointer'],
482                       LINKFLAGS='-fsanitize=address,undefined')
483        else:
484            env.Append(CCFLAGS=['-fsanitize=address',
485                                '-fno-omit-frame-pointer'],
486                       LINKFLAGS='-fsanitize=address')
487    # Only gcc >= 4.9 supports UBSan, so check both the version
488    # and the command-line option before adding the compiler and
489    # linker flags.
490    elif GetOption('with_ubsan') and \
491            compareVersions(env['GCC_VERSION'], '4.9') >= 0:
492        env.Append(CCFLAGS='-fsanitize=undefined')
493        env.Append(LINKFLAGS='-fsanitize=undefined')
494
495elif main['CLANG']:
496    # Check for a supported version of clang, >= 3.1 is needed to
497    # support similar features as gcc 4.8. See
498    # http://clang.llvm.org/cxx_status.html for details
499    clang_version_re = re.compile(".* version (\d+\.\d+)")
500    clang_version_match = clang_version_re.search(CXX_version)
501    if (clang_version_match):
502        clang_version = clang_version_match.groups()[0]
503        if compareVersions(clang_version, "3.1") < 0:
504            print 'Error: clang version 3.1 or newer required.'
505            print '       Installed version:', clang_version
506            Exit(1)
507    else:
508        print 'Error: Unable to determine clang version.'
509        Exit(1)
510
511    # clang has a few additional warnings that we disable, extraneous
512    # parantheses are allowed due to Ruby's printing of the AST,
513    # finally self assignments are allowed as the generated CPU code
514    # is relying on this
515    main.Append(CCFLAGS=['-Wno-parentheses',
516                         '-Wno-self-assign',
517                         # Some versions of libstdc++ (4.8?) seem to
518                         # use struct hash and class hash
519                         # interchangeably.
520                         '-Wno-mismatched-tags',
521                         ])
522
523    main.Append(TCMALLOC_CCFLAGS=['-fno-builtin'])
524
525    # On Mac OS X/Darwin we need to also use libc++ (part of XCode) as
526    # opposed to libstdc++, as the later is dated.
527    if sys.platform == "darwin":
528        main.Append(CXXFLAGS=['-stdlib=libc++'])
529        main.Append(LIBS=['c++'])
530
531    # On FreeBSD we need libthr.
532    if sys.platform.startswith('freebsd'):
533        main.Append(LIBS=['thr'])
534
535    # We require clang >= 3.1, so there is no need to check any
536    # versions here.
537    if GetOption('with_ubsan'):
538        if GetOption('with_asan'):
539            env.Append(CCFLAGS=['-fsanitize=address,undefined',
540                                '-fno-omit-frame-pointer'],
541                       LINKFLAGS='-fsanitize=address,undefined')
542        else:
543            env.Append(CCFLAGS='-fsanitize=undefined',
544                       LINKFLAGS='-fsanitize=undefined')
545
546    elif GetOption('with_asan'):
547        env.Append(CCFLAGS=['-fsanitize=address',
548                            '-fno-omit-frame-pointer'],
549                   LINKFLAGS='-fsanitize=address')
550
551else:
552    print termcap.Yellow + termcap.Bold + 'Error' + termcap.Normal,
553    print "Don't know what compiler options to use for your compiler."
554    print termcap.Yellow + '       compiler:' + termcap.Normal, main['CXX']
555    print termcap.Yellow + '       version:' + termcap.Normal,
556    if not CXX_version:
557        print termcap.Yellow + termcap.Bold + "COMMAND NOT FOUND!" +\
558               termcap.Normal
559    else:
560        print CXX_version.replace('\n', '<nl>')
561    print "       If you're trying to use a compiler other than GCC"
562    print "       or clang, there appears to be something wrong with your"
563    print "       environment."
564    print "       "
565    print "       If you are trying to use a compiler other than those listed"
566    print "       above you will need to ease fix SConstruct and "
567    print "       src/SConscript to support that compiler."
568    Exit(1)
569
570# Set up common yacc/bison flags (needed for Ruby)
571main['YACCFLAGS'] = '-d'
572main['YACCHXXFILESUFFIX'] = '.hh'
573
574# Do this after we save setting back, or else we'll tack on an
575# extra 'qdo' every time we run scons.
576if main['BATCH']:
577    main['CC']     = main['BATCH_CMD'] + ' ' + main['CC']
578    main['CXX']    = main['BATCH_CMD'] + ' ' + main['CXX']
579    main['AS']     = main['BATCH_CMD'] + ' ' + main['AS']
580    main['AR']     = main['BATCH_CMD'] + ' ' + main['AR']
581    main['RANLIB'] = main['BATCH_CMD'] + ' ' + main['RANLIB']
582
583if sys.platform == 'cygwin':
584    # cygwin has some header file issues...
585    main.Append(CCFLAGS=["-Wno-uninitialized"])
586
587# Check for the protobuf compiler
588protoc_version = readCommand([main['PROTOC'], '--version'],
589                             exception='').split()
590
591# First two words should be "libprotoc x.y.z"
592if len(protoc_version) < 2 or protoc_version[0] != 'libprotoc':
593    print termcap.Yellow + termcap.Bold + \
594        'Warning: Protocol buffer compiler (protoc) not found.\n' + \
595        '         Please install protobuf-compiler for tracing support.' + \
596        termcap.Normal
597    main['PROTOC'] = False
598else:
599    # Based on the availability of the compress stream wrappers,
600    # require 2.1.0
601    min_protoc_version = '2.1.0'
602    if compareVersions(protoc_version[1], min_protoc_version) < 0:
603        print termcap.Yellow + termcap.Bold + \
604            'Warning: protoc version', min_protoc_version, \
605            'or newer required.\n' + \
606            '         Installed version:', protoc_version[1], \
607            termcap.Normal
608        main['PROTOC'] = False
609    else:
610        # Attempt to determine the appropriate include path and
611        # library path using pkg-config, that means we also need to
612        # check for pkg-config. Note that it is possible to use
613        # protobuf without the involvement of pkg-config. Later on we
614        # check go a library config check and at that point the test
615        # will fail if libprotobuf cannot be found.
616        if readCommand(['pkg-config', '--version'], exception=''):
617            try:
618                # Attempt to establish what linking flags to add for protobuf
619                # using pkg-config
620                main.ParseConfig('pkg-config --cflags --libs-only-L protobuf')
621            except:
622                print termcap.Yellow + termcap.Bold + \
623                    'Warning: pkg-config could not get protobuf flags.' + \
624                    termcap.Normal
625
626
627# Check for 'timeout' from GNU coreutils. If present, regressions will
628# be run with a time limit. We require version 8.13 since we rely on
629# support for the '--foreground' option.
630if sys.platform.startswith('freebsd'):
631    timeout_lines = readCommand(['gtimeout', '--version'],
632                                exception='').splitlines()
633else:
634    timeout_lines = readCommand(['timeout', '--version'],
635                                exception='').splitlines()
636# Get the first line and tokenize it
637timeout_version = timeout_lines[0].split() if timeout_lines else []
638main['TIMEOUT'] =  timeout_version and \
639    compareVersions(timeout_version[-1], '8.13') >= 0
640
641# Add a custom Check function to test for structure members.
642def CheckMember(context, include, decl, member, include_quotes="<>"):
643    context.Message("Checking for member %s in %s..." %
644                    (member, decl))
645    text = """
646#include %(header)s
647int main(){
648  %(decl)s test;
649  (void)test.%(member)s;
650  return 0;
651};
652""" % { "header" : include_quotes[0] + include + include_quotes[1],
653        "decl" : decl,
654        "member" : member,
655        }
656
657    ret = context.TryCompile(text, extension=".cc")
658    context.Result(ret)
659    return ret
660
661# Platform-specific configuration.  Note again that we assume that all
662# builds under a given build root run on the same host platform.
663conf = Configure(main,
664                 conf_dir = joinpath(build_root, '.scons_config'),
665                 log_file = joinpath(build_root, 'scons_config.log'),
666                 custom_tests = {
667        'CheckMember' : CheckMember,
668        })
669
670# Check if we should compile a 64 bit binary on Mac OS X/Darwin
671try:
672    import platform
673    uname = platform.uname()
674    if uname[0] == 'Darwin' and compareVersions(uname[2], '9.0.0') >= 0:
675        if int(readCommand('sysctl -n hw.cpu64bit_capable')[0]):
676            main.Append(CCFLAGS=['-arch', 'x86_64'])
677            main.Append(CFLAGS=['-arch', 'x86_64'])
678            main.Append(LINKFLAGS=['-arch', 'x86_64'])
679            main.Append(ASFLAGS=['-arch', 'x86_64'])
680except:
681    pass
682
683# Recent versions of scons substitute a "Null" object for Configure()
684# when configuration isn't necessary, e.g., if the "--help" option is
685# present.  Unfortuantely this Null object always returns false,
686# breaking all our configuration checks.  We replace it with our own
687# more optimistic null object that returns True instead.
688if not conf:
689    def NullCheck(*args, **kwargs):
690        return True
691
692    class NullConf:
693        def __init__(self, env):
694            self.env = env
695        def Finish(self):
696            return self.env
697        def __getattr__(self, mname):
698            return NullCheck
699
700    conf = NullConf(main)
701
702# Cache build files in the supplied directory.
703if main['M5_BUILD_CACHE']:
704    print 'Using build cache located at', main['M5_BUILD_CACHE']
705    CacheDir(main['M5_BUILD_CACHE'])
706
707main['USE_PYTHON'] = not GetOption('without_python')
708if main['USE_PYTHON']:
709    # Find Python include and library directories for embedding the
710    # interpreter. We rely on python-config to resolve the appropriate
711    # includes and linker flags. ParseConfig does not seem to understand
712    # the more exotic linker flags such as -Xlinker and -export-dynamic so
713    # we add them explicitly below. If you want to link in an alternate
714    # version of python, see above for instructions on how to invoke
715    # scons with the appropriate PATH set.
716    #
717    # First we check if python2-config exists, else we use python-config
718    python_config = readCommand(['which', 'python2-config'],
719                                exception='').strip()
720    if not os.path.exists(python_config):
721        python_config = readCommand(['which', 'python-config'],
722                                    exception='').strip()
723    py_includes = readCommand([python_config, '--includes'],
724                              exception='').split()
725    # Strip the -I from the include folders before adding them to the
726    # CPPPATH
727    main.Append(CPPPATH=map(lambda inc: inc[2:], py_includes))
728
729    # Read the linker flags and split them into libraries and other link
730    # flags. The libraries are added later through the call the CheckLib.
731    py_ld_flags = readCommand([python_config, '--ldflags'],
732        exception='').split()
733    py_libs = []
734    for lib in py_ld_flags:
735         if not lib.startswith('-l'):
736             main.Append(LINKFLAGS=[lib])
737         else:
738             lib = lib[2:]
739             if lib not in py_libs:
740                 py_libs.append(lib)
741
742    # verify that this stuff works
743    if not conf.CheckHeader('Python.h', '<>'):
744        print "Error: can't find Python.h header in", py_includes
745        print "Install Python headers (package python-dev on Ubuntu and RedHat)"
746        Exit(1)
747
748    for lib in py_libs:
749        if not conf.CheckLib(lib):
750            print "Error: can't find library %s required by python" % lib
751            Exit(1)
752
753# On Solaris you need to use libsocket for socket ops
754if not conf.CheckLibWithHeader(None, 'sys/socket.h', 'C++', 'accept(0,0,0);'):
755   if not conf.CheckLibWithHeader('socket', 'sys/socket.h', 'C++', 'accept(0,0,0);'):
756       print "Can't find library with socket calls (e.g. accept())"
757       Exit(1)
758
759# Check for zlib.  If the check passes, libz will be automatically
760# added to the LIBS environment variable.
761if not conf.CheckLibWithHeader('z', 'zlib.h', 'C++','zlibVersion();'):
762    print 'Error: did not find needed zlib compression library '\
763          'and/or zlib.h header file.'
764    print '       Please install zlib and try again.'
765    Exit(1)
766
767# If we have the protobuf compiler, also make sure we have the
768# development libraries. If the check passes, libprotobuf will be
769# automatically added to the LIBS environment variable. After
770# this, we can use the HAVE_PROTOBUF flag to determine if we have
771# got both protoc and libprotobuf available.
772main['HAVE_PROTOBUF'] = main['PROTOC'] and \
773    conf.CheckLibWithHeader('protobuf', 'google/protobuf/message.h',
774                            'C++', 'GOOGLE_PROTOBUF_VERIFY_VERSION;')
775
776# If we have the compiler but not the library, print another warning.
777if main['PROTOC'] and not main['HAVE_PROTOBUF']:
778    print termcap.Yellow + termcap.Bold + \
779        'Warning: did not find protocol buffer library and/or headers.\n' + \
780    '       Please install libprotobuf-dev for tracing support.' + \
781    termcap.Normal
782
783# Check for librt.
784have_posix_clock = \
785    conf.CheckLibWithHeader(None, 'time.h', 'C',
786                            'clock_nanosleep(0,0,NULL,NULL);') or \
787    conf.CheckLibWithHeader('rt', 'time.h', 'C',
788                            'clock_nanosleep(0,0,NULL,NULL);')
789
790have_posix_timers = \
791    conf.CheckLibWithHeader([None, 'rt'], [ 'time.h', 'signal.h' ], 'C',
792                            'timer_create(CLOCK_MONOTONIC, NULL, NULL);')
793
794if not GetOption('without_tcmalloc'):
795    if conf.CheckLib('tcmalloc'):
796        main.Append(CCFLAGS=main['TCMALLOC_CCFLAGS'])
797    elif conf.CheckLib('tcmalloc_minimal'):
798        main.Append(CCFLAGS=main['TCMALLOC_CCFLAGS'])
799    else:
800        print termcap.Yellow + termcap.Bold + \
801              "You can get a 12% performance improvement by "\
802              "installing tcmalloc (libgoogle-perftools-dev package "\
803              "on Ubuntu or RedHat)." + termcap.Normal
804
805
806# Detect back trace implementations. The last implementation in the
807# list will be used by default.
808backtrace_impls = [ "none" ]
809
810if conf.CheckLibWithHeader(None, 'execinfo.h', 'C',
811                           'backtrace_symbols_fd((void*)0, 0, 0);'):
812    backtrace_impls.append("glibc")
813elif conf.CheckLibWithHeader('execinfo', 'execinfo.h', 'C',
814                           'backtrace_symbols_fd((void*)0, 0, 0);'):
815    # NetBSD and FreeBSD need libexecinfo.
816    backtrace_impls.append("glibc")
817    main.Append(LIBS=['execinfo'])
818
819if backtrace_impls[-1] == "none":
820    default_backtrace_impl = "none"
821    print termcap.Yellow + termcap.Bold + \
822        "No suitable back trace implementation found." + \
823        termcap.Normal
824
825if not have_posix_clock:
826    print "Can't find library for POSIX clocks."
827
828# Check for <fenv.h> (C99 FP environment control)
829have_fenv = conf.CheckHeader('fenv.h', '<>')
830if not have_fenv:
831    print "Warning: Header file <fenv.h> not found."
832    print "         This host has no IEEE FP rounding mode control."
833
834# Check for <png.h> (libpng library needed if wanting to dump
835# frame buffer image in png format)
836have_png = conf.CheckHeader('png.h', '<>')
837if not have_png:
838    print "Warning: Header file <png.h> not found."
839    print "         This host has no libpng library."
840    print "         Disabling support for PNG framebuffers."
841
842# Check if we should enable KVM-based hardware virtualization. The API
843# we rely on exists since version 2.6.36 of the kernel, but somehow
844# the KVM_API_VERSION does not reflect the change. We test for one of
845# the types as a fall back.
846have_kvm = conf.CheckHeader('linux/kvm.h', '<>')
847if not have_kvm:
848    print "Info: Compatible header file <linux/kvm.h> not found, " \
849        "disabling KVM support."
850
851# Check if the TUN/TAP driver is available.
852have_tuntap = conf.CheckHeader('linux/if_tun.h', '<>')
853if not have_tuntap:
854    print "Info: Compatible header file <linux/if_tun.h> not found."
855
856# x86 needs support for xsave. We test for the structure here since we
857# won't be able to run new tests by the time we know which ISA we're
858# targeting.
859have_kvm_xsave = conf.CheckTypeSize('struct kvm_xsave',
860                                    '#include <linux/kvm.h>') != 0
861
862# Check if the requested target ISA is compatible with the host
863def is_isa_kvm_compatible(isa):
864    try:
865        import platform
866        host_isa = platform.machine()
867    except:
868        print "Warning: Failed to determine host ISA."
869        return False
870
871    if not have_posix_timers:
872        print "Warning: Can not enable KVM, host seems to lack support " \
873            "for POSIX timers"
874        return False
875
876    if isa == "arm":
877        return host_isa in ( "armv7l", "aarch64" )
878    elif isa == "x86":
879        if host_isa != "x86_64":
880            return False
881
882        if not have_kvm_xsave:
883            print "KVM on x86 requires xsave support in kernel headers."
884            return False
885
886        return True
887    else:
888        return False
889
890
891# Check if the exclude_host attribute is available. We want this to
892# get accurate instruction counts in KVM.
893main['HAVE_PERF_ATTR_EXCLUDE_HOST'] = conf.CheckMember(
894    'linux/perf_event.h', 'struct perf_event_attr', 'exclude_host')
895
896
897######################################################################
898#
899# Finish the configuration
900#
901main = conf.Finish()
902
903######################################################################
904#
905# Collect all non-global variables
906#
907
908# Define the universe of supported ISAs
909all_isa_list = [ ]
910all_gpu_isa_list = [ ]
911Export('all_isa_list')
912Export('all_gpu_isa_list')
913
914class CpuModel(object):
915    '''The CpuModel class encapsulates everything the ISA parser needs to
916    know about a particular CPU model.'''
917
918    # Dict of available CPU model objects.  Accessible as CpuModel.dict.
919    dict = {}
920
921    # Constructor.  Automatically adds models to CpuModel.dict.
922    def __init__(self, name, default=False):
923        self.name = name           # name of model
924
925        # This cpu is enabled by default
926        self.default = default
927
928        # Add self to dict
929        if name in CpuModel.dict:
930            raise AttributeError, "CpuModel '%s' already registered" % name
931        CpuModel.dict[name] = self
932
933Export('CpuModel')
934
935# Sticky variables get saved in the variables file so they persist from
936# one invocation to the next (unless overridden, in which case the new
937# value becomes sticky).
938sticky_vars = Variables(args=ARGUMENTS)
939Export('sticky_vars')
940
941# Sticky variables that should be exported
942export_vars = []
943Export('export_vars')
944
945# For Ruby
946all_protocols = []
947Export('all_protocols')
948protocol_dirs = []
949Export('protocol_dirs')
950slicc_includes = []
951Export('slicc_includes')
952
953# Walk the tree and execute all SConsopts scripts that wil add to the
954# above variables
955if GetOption('verbose'):
956    print "Reading SConsopts"
957for bdir in [ base_dir ] + extras_dir_list:
958    if not isdir(bdir):
959        print "Error: directory '%s' does not exist" % bdir
960        Exit(1)
961    for root, dirs, files in os.walk(bdir):
962        if 'SConsopts' in files:
963            if GetOption('verbose'):
964                print "Reading", joinpath(root, 'SConsopts')
965            SConscript(joinpath(root, 'SConsopts'))
966
967all_isa_list.sort()
968all_gpu_isa_list.sort()
969
970sticky_vars.AddVariables(
971    EnumVariable('TARGET_ISA', 'Target ISA', 'alpha', all_isa_list),
972    EnumVariable('TARGET_GPU_ISA', 'Target GPU ISA', 'hsail', all_gpu_isa_list),
973    ListVariable('CPU_MODELS', 'CPU models',
974                 sorted(n for n,m in CpuModel.dict.iteritems() if m.default),
975                 sorted(CpuModel.dict.keys())),
976    BoolVariable('EFENCE', 'Link with Electric Fence malloc debugger',
977                 False),
978    BoolVariable('SS_COMPATIBLE_FP',
979                 'Make floating-point results compatible with SimpleScalar',
980                 False),
981    BoolVariable('USE_SSE2',
982                 'Compile for SSE2 (-msse2) to get IEEE FP on x86 hosts',
983                 False),
984    BoolVariable('USE_POSIX_CLOCK', 'Use POSIX Clocks', have_posix_clock),
985    BoolVariable('USE_FENV', 'Use <fenv.h> IEEE mode control', have_fenv),
986    BoolVariable('USE_PNG',  'Enable support for PNG images', have_png),
987    BoolVariable('CP_ANNOTATE', 'Enable critical path annotation capability',
988                 False),
989    BoolVariable('USE_KVM', 'Enable hardware virtualized (KVM) CPU models',
990                 have_kvm),
991    BoolVariable('USE_TUNTAP',
992                 'Enable using a tap device to bridge to the host network',
993                 have_tuntap),
994    BoolVariable('BUILD_GPU', 'Build the compute-GPU model', False),
995    EnumVariable('PROTOCOL', 'Coherence protocol for Ruby', 'None',
996                  all_protocols),
997    EnumVariable('BACKTRACE_IMPL', 'Post-mortem dump implementation',
998                 backtrace_impls[-1], backtrace_impls)
999    )
1000
1001# These variables get exported to #defines in config/*.hh (see src/SConscript).
1002export_vars += ['USE_FENV', 'SS_COMPATIBLE_FP', 'TARGET_ISA', 'TARGET_GPU_ISA',
1003                'CP_ANNOTATE', 'USE_POSIX_CLOCK', 'USE_KVM', 'USE_TUNTAP',
1004                'PROTOCOL', 'HAVE_PROTOBUF', 'HAVE_PERF_ATTR_EXCLUDE_HOST',
1005                'USE_PNG']
1006
1007###################################################
1008#
1009# Define a SCons builder for configuration flag headers.
1010#
1011###################################################
1012
1013# This function generates a config header file that #defines the
1014# variable symbol to the current variable setting (0 or 1).  The source
1015# operands are the name of the variable and a Value node containing the
1016# value of the variable.
1017def build_config_file(target, source, env):
1018    (variable, value) = [s.get_contents() for s in source]
1019    f = file(str(target[0]), 'w')
1020    print >> f, '#define', variable, value
1021    f.close()
1022    return None
1023
1024# Combine the two functions into a scons Action object.
1025config_action = MakeAction(build_config_file, Transform("CONFIG H", 2))
1026
1027# The emitter munges the source & target node lists to reflect what
1028# we're really doing.
1029def config_emitter(target, source, env):
1030    # extract variable name from Builder arg
1031    variable = str(target[0])
1032    # True target is config header file
1033    target = joinpath('config', variable.lower() + '.hh')
1034    val = env[variable]
1035    if isinstance(val, bool):
1036        # Force value to 0/1
1037        val = int(val)
1038    elif isinstance(val, str):
1039        val = '"' + val + '"'
1040
1041    # Sources are variable name & value (packaged in SCons Value nodes)
1042    return ([target], [Value(variable), Value(val)])
1043
1044config_builder = Builder(emitter = config_emitter, action = config_action)
1045
1046main.Append(BUILDERS = { 'ConfigFile' : config_builder })
1047
1048###################################################
1049#
1050# Builders for static and shared partially linked object files.
1051#
1052###################################################
1053
1054partial_static_builder = Builder(action=SCons.Defaults.LinkAction,
1055                                 src_suffix='$OBJSUFFIX',
1056                                 src_builder=['StaticObject', 'Object'],
1057                                 LINKFLAGS='$PLINKFLAGS',
1058                                 LIBS='')
1059
1060def partial_shared_emitter(target, source, env):
1061    for tgt in target:
1062        tgt.attributes.shared = 1
1063    return (target, source)
1064partial_shared_builder = Builder(action=SCons.Defaults.ShLinkAction,
1065                                 emitter=partial_shared_emitter,
1066                                 src_suffix='$SHOBJSUFFIX',
1067                                 src_builder='SharedObject',
1068                                 SHLINKFLAGS='$PSHLINKFLAGS',
1069                                 LIBS='')
1070
1071main.Append(BUILDERS = { 'PartialShared' : partial_shared_builder,
1072                         'PartialStatic' : partial_static_builder })
1073
1074# builds in ext are shared across all configs in the build root.
1075ext_dir = abspath(joinpath(str(main.root), 'ext'))
1076ext_build_dirs = []
1077for root, dirs, files in os.walk(ext_dir):
1078    if 'SConscript' in files:
1079        build_dir = os.path.relpath(root, ext_dir)
1080        ext_build_dirs.append(build_dir)
1081        main.SConscript(joinpath(root, 'SConscript'),
1082                        variant_dir=joinpath(build_root, build_dir))
1083
1084main.Prepend(CPPPATH=Dir('ext/pybind11/include/'))
1085
1086###################################################
1087#
1088# This builder and wrapper method are used to set up a directory with
1089# switching headers. Those are headers which are in a generic location and
1090# that include more specific headers from a directory chosen at build time
1091# based on the current build settings.
1092#
1093###################################################
1094
1095def build_switching_header(target, source, env):
1096    path = str(target[0])
1097    subdir = str(source[0])
1098    dp, fp = os.path.split(path)
1099    dp = os.path.relpath(os.path.realpath(dp),
1100                         os.path.realpath(env['BUILDDIR']))
1101    with open(path, 'w') as hdr:
1102        print >>hdr, '#include "%s/%s/%s"' % (dp, subdir, fp)
1103
1104switching_header_action = MakeAction(build_switching_header,
1105                                     Transform('GENERATE'))
1106
1107switching_header_builder = Builder(action=switching_header_action,
1108                                   source_factory=Value,
1109                                   single_source=True)
1110
1111main.Append(BUILDERS = { 'SwitchingHeader': switching_header_builder })
1112
1113def switching_headers(self, headers, source):
1114    for header in headers:
1115        self.SwitchingHeader(header, source)
1116
1117main.AddMethod(switching_headers, 'SwitchingHeaders')
1118
1119###################################################
1120#
1121# Define build environments for selected configurations.
1122#
1123###################################################
1124
1125for variant_path in variant_paths:
1126    if not GetOption('silent'):
1127        print "Building in", variant_path
1128
1129    # Make a copy of the build-root environment to use for this config.
1130    env = main.Clone()
1131    env['BUILDDIR'] = variant_path
1132
1133    # variant_dir is the tail component of build path, and is used to
1134    # determine the build parameters (e.g., 'ALPHA_SE')
1135    (build_root, variant_dir) = splitpath(variant_path)
1136
1137    # Set env variables according to the build directory config.
1138    sticky_vars.files = []
1139    # Variables for $BUILD_ROOT/$VARIANT_DIR are stored in
1140    # $BUILD_ROOT/variables/$VARIANT_DIR so you can nuke
1141    # $BUILD_ROOT/$VARIANT_DIR without losing your variables settings.
1142    current_vars_file = joinpath(build_root, 'variables', variant_dir)
1143    if isfile(current_vars_file):
1144        sticky_vars.files.append(current_vars_file)
1145        if not GetOption('silent'):
1146            print "Using saved variables file %s" % current_vars_file
1147    elif variant_dir in ext_build_dirs:
1148        # Things in ext are built without a variant directory.
1149        continue
1150    else:
1151        # Build dir-specific variables file doesn't exist.
1152
1153        # Make sure the directory is there so we can create it later
1154        opt_dir = dirname(current_vars_file)
1155        if not isdir(opt_dir):
1156            mkdir(opt_dir)
1157
1158        # Get default build variables from source tree.  Variables are
1159        # normally determined by name of $VARIANT_DIR, but can be
1160        # overridden by '--default=' arg on command line.
1161        default = GetOption('default')
1162        opts_dir = joinpath(main.root.abspath, 'build_opts')
1163        if default:
1164            default_vars_files = [joinpath(build_root, 'variables', default),
1165                                  joinpath(opts_dir, default)]
1166        else:
1167            default_vars_files = [joinpath(opts_dir, variant_dir)]
1168        existing_files = filter(isfile, default_vars_files)
1169        if existing_files:
1170            default_vars_file = existing_files[0]
1171            sticky_vars.files.append(default_vars_file)
1172            print "Variables file %s not found,\n  using defaults in %s" \
1173                  % (current_vars_file, default_vars_file)
1174        else:
1175            print "Error: cannot find variables file %s or " \
1176                  "default file(s) %s" \
1177                  % (current_vars_file, ' or '.join(default_vars_files))
1178            Exit(1)
1179
1180    # Apply current variable settings to env
1181    sticky_vars.Update(env)
1182
1183    help_texts["local_vars"] += \
1184        "Build variables for %s:\n" % variant_dir \
1185                 + sticky_vars.GenerateHelpText(env)
1186
1187    # Process variable settings.
1188
1189    if not have_fenv and env['USE_FENV']:
1190        print "Warning: <fenv.h> not available; " \
1191              "forcing USE_FENV to False in", variant_dir + "."
1192        env['USE_FENV'] = False
1193
1194    if not env['USE_FENV']:
1195        print "Warning: No IEEE FP rounding mode control in", variant_dir + "."
1196        print "         FP results may deviate slightly from other platforms."
1197
1198    if not have_png and env['USE_PNG']:
1199        print "Warning: <png.h> not available; " \
1200              "forcing USE_PNG to False in", variant_dir + "."
1201        env['USE_PNG'] = False
1202
1203    if env['USE_PNG']:
1204        env.Append(LIBS=['png'])
1205
1206    if env['EFENCE']:
1207        env.Append(LIBS=['efence'])
1208
1209    if env['USE_KVM']:
1210        if not have_kvm:
1211            print "Warning: Can not enable KVM, host seems to lack KVM support"
1212            env['USE_KVM'] = False
1213        elif not is_isa_kvm_compatible(env['TARGET_ISA']):
1214            print "Info: KVM support disabled due to unsupported host and " \
1215                "target ISA combination"
1216            env['USE_KVM'] = False
1217
1218    if env['USE_TUNTAP']:
1219        if not have_tuntap:
1220            print "Warning: Can't connect EtherTap with a tap device."
1221            env['USE_TUNTAP'] = False
1222
1223    if env['BUILD_GPU']:
1224        env.Append(CPPDEFINES=['BUILD_GPU'])
1225
1226    # Warn about missing optional functionality
1227    if env['USE_KVM']:
1228        if not main['HAVE_PERF_ATTR_EXCLUDE_HOST']:
1229            print "Warning: perf_event headers lack support for the " \
1230                "exclude_host attribute. KVM instruction counts will " \
1231                "be inaccurate."
1232
1233    # Save sticky variable settings back to current variables file
1234    sticky_vars.Save(current_vars_file, env)
1235
1236    if env['USE_SSE2']:
1237        env.Append(CCFLAGS=['-msse2'])
1238
1239    # The src/SConscript file sets up the build rules in 'env' according
1240    # to the configured variables.  It returns a list of environments,
1241    # one for each variant build (debug, opt, etc.)
1242    SConscript('src/SConscript', variant_dir = variant_path, exports = 'env')
1243
1244# base help text
1245Help('''
1246Usage: scons [scons options] [build variables] [target(s)]
1247
1248Extra scons options:
1249%(options)s
1250
1251Global build variables:
1252%(global_vars)s
1253
1254%(local_vars)s
1255''' % help_texts)
1256