SConstruct revision 12304:299452fa8cc4
1360SN/A# -*- mode:python -*-
21458SN/A
3360SN/A# Copyright (c) 2013, 2015-2017 ARM Limited
4360SN/A# All rights reserved.
5360SN/A#
6360SN/A# The license below extends only to copyright in the software and shall
7360SN/A# not be construed as granting a license to any other intellectual
8360SN/A# property including but not limited to intellectual property relating
9360SN/A# to a hardware implementation of the functionality of the software
10360SN/A# licensed hereunder.  You may use the software subject to the license
11360SN/A# terms below provided that you ensure that this notice is replicated
12360SN/A# unmodified and in its entirety in all distributions of the software,
13360SN/A# modified or unmodified, in source code or in binary form.
14360SN/A#
15360SN/A# Copyright (c) 2011 Advanced Micro Devices, Inc.
16360SN/A# Copyright (c) 2009 The Hewlett-Packard Development Company
17360SN/A# Copyright (c) 2004-2005 The Regents of The University of Michigan
18360SN/A# All rights reserved.
19360SN/A#
20360SN/A# Redistribution and use in source and binary forms, with or without
21360SN/A# modification, are permitted provided that the following conditions are
22360SN/A# met: redistributions of source code must retain the above copyright
23360SN/A# notice, this list of conditions and the following disclaimer;
24360SN/A# redistributions in binary form must reproduce the above copyright
25360SN/A# notice, this list of conditions and the following disclaimer in the
26360SN/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.
30360SN/A#
31360SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
321354SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
331354SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34360SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
352764Sstever@eecs.umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
362764Sstever@eecs.umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
372064SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38360SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39360SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40360SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
41360SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42360SN/A#
43360SN/A# Authors: Steve Reinhardt
441354SN/A#          Nathan Binkert
45360SN/A
461809SN/A###################################################
471809SN/A#
481809SN/A# SCons top-level build description (SConstruct) file.
493113Sgblack@eecs.umich.edu#
503113Sgblack@eecs.umich.edu# While in this directory ('gem5'), just type 'scons' to build the default
511999SN/A# configuration (see below), or type 'scons build/<CONFIG>/<binary>'
52360SN/A# to build some other configuration (e.g., 'build/ALPHA/gem5.opt' for
533113Sgblack@eecs.umich.edu# the optimized full-system version).
542474SN/A#
55360SN/A# You can build gem5 in a different directory as long as there is a
562462SN/A# 'build/<CONFIG>' somewhere along the target path.  The build system
571354SN/A# expects that all configs under the same build directory are being
582474SN/A# built for the same host system.
592680Sktlim@umich.edu#
602474SN/A# Examples:
612474SN/A#
621354SN/A#   The following two commands are equivalent.  The '-u' option tells
63360SN/A#   scons to search up the directory tree for this SConstruct file.
64360SN/A#   % cd <path-to-src>/gem5 ; scons build/ALPHA/gem5.debug
65360SN/A#   % cd <path-to-src>/gem5/build/ALPHA; scons -u gem5.debug
66360SN/A#
67360SN/A#   The following two commands are equivalent and demonstrate building
68360SN/A#   in a directory outside of the source tree.  The '-C' option tells
69360SN/A#   scons to chdir to the specified directory to find this SConstruct
70360SN/A#   file.
71378SN/A#   % cd <path-to-src>/gem5 ; scons /local/foo/build/ALPHA/gem5.debug
721450SN/A#   % cd /local/foo/build/ALPHA; scons -C <path-to-src>/gem5 gem5.debug
733114Sgblack@eecs.umich.edu#
74360SN/A# You can use 'scons -H' to print scons options.  If you're in this
75360SN/A# 'gem5' directory (or use -u or -C to tell scons where to find this
76360SN/A# file), you can use 'scons -h' to print all the gem5-specific build
77360SN/A# options as well.
78360SN/A#
79360SN/A###################################################
80360SN/A
81360SN/A# Global Python includes
82360SN/Aimport itertools
832680Sktlim@umich.eduimport os
84360SN/Aimport re
85360SN/Aimport shutil
86360SN/Aimport subprocess
87360SN/Aimport sys
88360SN/A
89360SN/Afrom os import mkdir, environ
90360SN/Afrom os.path import abspath, basename, dirname, expanduser, normpath
91360SN/Afrom os.path import exists,  isdir, isfile
92360SN/Afrom os.path import join as joinpath, split as splitpath
93360SN/A
94360SN/A# SCons includes
953114Sgblack@eecs.umich.eduimport SCons
96360SN/Aimport SCons.Node
97360SN/A
98360SN/Afrom m5.util import compareVersions, readCommand
99360SN/A
100360SN/Ahelp_texts = {
101360SN/A    "options" : "",
102360SN/A    "global_vars" : "",
103360SN/A    "local_vars" : ""
104360SN/A}
105360SN/A
106360SN/AExport("help_texts")
107360SN/A
108360SN/A
109360SN/A# There's a bug in scons in that (1) by default, the help texts from
110360SN/A# AddOption() are supposed to be displayed when you type 'scons -h'
111360SN/A# and (2) you can override the help displayed by 'scons -h' using the
112360SN/A# Help() function, but these two features are incompatible: once
113360SN/A# you've overridden the help text using Help(), there's no way to get
114360SN/A# at the help texts from AddOptions.  See:
115360SN/A#     http://scons.tigris.org/issues/show_bug.cgi?id=2356
116360SN/A#     http://scons.tigris.org/issues/show_bug.cgi?id=2611
1172400SN/A# This hack lets us extract the help text from AddOptions and
118360SN/A# re-inject it via Help().  Ideally someday this bug will be fixed and
1192461SN/A# we can just use AddOption directly.
120360SN/Adef AddLocalOption(*args, **kwargs):
121360SN/A    col_width = 30
122360SN/A
123360SN/A    help = "  " + ", ".join(args)
124360SN/A    if "help" in kwargs:
125360SN/A        length = len(help)
1262400SN/A        if length >= col_width:
127360SN/A            help += "\n" + " " * col_width
1282461SN/A        else:
129360SN/A            help += " " * (col_width - length)
130360SN/A        help += kwargs["help"]
131360SN/A    help_texts["options"] += help + "\n"
132360SN/A
133360SN/A    AddOption(*args, **kwargs)
134360SN/A
135360SN/AAddLocalOption('--colors', dest='use_colors', action='store_true',
136360SN/A               help="Add color to abbreviated scons output")
137360SN/AAddLocalOption('--no-colors', dest='use_colors', action='store_false',
138360SN/A               help="Don't add color to abbreviated scons output")
139360SN/AAddLocalOption('--with-cxx-config', dest='with_cxx_config',
140360SN/A               action='store_true',
141360SN/A               help="Build with support for C++-based configuration")
142360SN/AAddLocalOption('--default', dest='default', type='string', action='store',
143360SN/A               help='Override which build_opts file to use for defaults')
144360SN/AAddLocalOption('--ignore-style', dest='ignore_style', action='store_true',
145360SN/A               help='Disable style checking hooks')
146360SN/AAddLocalOption('--no-lto', dest='no_lto', action='store_true',
147360SN/A               help='Disable Link-Time Optimization for fast')
148360SN/AAddLocalOption('--force-lto', dest='force_lto', action='store_true',
149360SN/A               help='Use Link-Time Optimization instead of partial linking' +
150360SN/A                    ' when the compiler doesn\'t support using them together.')
151360SN/AAddLocalOption('--update-ref', dest='update_ref', action='store_true',
152360SN/A               help='Update test reference outputs')
153360SN/AAddLocalOption('--verbose', dest='verbose', action='store_true',
154360SN/A               help='Print full tool command lines')
155360SN/AAddLocalOption('--without-python', dest='without_python',
156360SN/A               action='store_true',
157360SN/A               help='Build without Python configuration support')
158360SN/AAddLocalOption('--without-tcmalloc', dest='without_tcmalloc',
159360SN/A               action='store_true',
160360SN/A               help='Disable linking against tcmalloc')
161502SN/AAddLocalOption('--with-ubsan', dest='with_ubsan', action='store_true',
162360SN/A               help='Build with Undefined Behavior Sanitizer if available')
163502SN/AAddLocalOption('--with-asan', dest='with_asan', action='store_true',
164360SN/A               help='Build with Address Sanitizer if available')
165360SN/A
166360SN/Aif GetOption('no_lto') and GetOption('force_lto'):
167360SN/A    print '--no-lto and --force-lto are mutually exclusive'
168360SN/A    Exit(1)
169360SN/A
170360SN/A########################################################################
171360SN/A#
172360SN/A# Set up the main build environment.
173360SN/A#
174360SN/A########################################################################
175378SN/A
1761706SN/Amain = Environment()
1773114Sgblack@eecs.umich.edu
178378SN/Afrom gem5_scons import Transform
179378SN/Afrom gem5_scons.util import get_termcap
180378SN/Atermcap = get_termcap()
181378SN/A
182378SN/Amain_dict_keys = main.Dictionary().keys()
1831706SN/A
1843114Sgblack@eecs.umich.edu# Check that we have a C/C++ compiler
185360SN/Aif not ('CC' in main_dict_keys and 'CXX' in main_dict_keys):
186378SN/A    print "No C++ compiler installed (package g++ on Ubuntu and RedHat)"
1871706SN/A    Exit(1)
1883114Sgblack@eecs.umich.edu
189378SN/A###################################################
190378SN/A#
1911706SN/A# Figure out which configurations to set up based on the path(s) of
1923114Sgblack@eecs.umich.edu# the target(s).
193378SN/A#
194378SN/A###################################################
1951706SN/A
1963114Sgblack@eecs.umich.edu# Find default configuration & binary.
197378SN/ADefault(environ.get('M5_DEFAULT_BINARY', 'build/ALPHA/gem5.debug'))
198378SN/A
1991706SN/A# helper function: find last occurrence of element in list
2003114Sgblack@eecs.umich.edudef rfind(l, elt, offs = -1):
201378SN/A    for i in range(len(l)+offs, 0, -1):
202378SN/A        if l[i] == elt:
2031706SN/A            return i
2043114Sgblack@eecs.umich.edu    raise ValueError, "element not found"
205378SN/A
206378SN/A# Take a list of paths (or SCons Nodes) and return a list with all
2071706SN/A# paths made absolute and ~-expanded.  Paths will be interpreted
2083114Sgblack@eecs.umich.edu# relative to the launch directory unless a different root is provided
209378SN/Adef makePathListAbsolute(path_list, root=GetLaunchDir()):
210378SN/A    return [abspath(joinpath(root, expanduser(str(p))))
2111706SN/A            for p in path_list]
2123114Sgblack@eecs.umich.edu
213378SN/A# Each target must have 'build' in the interior of the path; the
214378SN/A# directory below this will determine the build parameters.  For
2151706SN/A# example, for target 'foo/bar/build/ALPHA_SE/arch/alpha/blah.do' we
2163114Sgblack@eecs.umich.edu# recognize that ALPHA_SE specifies the configuration because it
217378SN/A# follow 'build' in the build path.
218378SN/A
2191706SN/A# The funky assignment to "[:]" is needed to replace the list contents
2203114Sgblack@eecs.umich.edu# in place rather than reassign the symbol to a new list, which
221360SN/A# doesn't work (obviously!).
222511SN/ABUILD_TARGETS[:] = makePathListAbsolute(BUILD_TARGETS)
2231706SN/A
2243114Sgblack@eecs.umich.edu# Generate a list of the unique build roots and configs that the
225511SN/A# collected targets reference.
226511SN/Avariant_paths = []
2271706SN/Abuild_root = None
2283114Sgblack@eecs.umich.edufor t in BUILD_TARGETS:
2291706SN/A    path_dirs = t.split('/')
2301706SN/A    try:
2311706SN/A        build_top = rfind(path_dirs, 'build', -2)
2321706SN/A    except:
2333114Sgblack@eecs.umich.edu        print "Error: no non-leaf 'build' dir found on target path", t
2341706SN/A        Exit(1)
2351706SN/A    this_build_root = joinpath('/',*path_dirs[:build_top+1])
2361706SN/A    if not build_root:
2371706SN/A        build_root = this_build_root
2383114Sgblack@eecs.umich.edu    else:
2391706SN/A        if this_build_root != build_root:
240511SN/A            print "Error: build targets not under same build root\n"\
2411999SN/A                  "  %s\n  %s" % (build_root, this_build_root)
2421999SN/A            Exit(1)
2433114Sgblack@eecs.umich.edu    variant_path = joinpath('/',*path_dirs[:build_top+2])
2441999SN/A    if variant_path not in variant_paths:
2451999SN/A        variant_paths.append(variant_path)
2461999SN/A
2471999SN/A# Make sure build_root exists (might not if this is the first build there)
2483114Sgblack@eecs.umich.eduif not isdir(build_root):
2491999SN/A    mkdir(build_root)
2503079Sstever@eecs.umich.edumain['BUILDROOT'] = build_root
2513079Sstever@eecs.umich.edu
2523114Sgblack@eecs.umich.eduExport('main')
2533079Sstever@eecs.umich.edu
2542093SN/Amain.SConsignFile(joinpath(build_root, "sconsign"))
2552093SN/A
2563114Sgblack@eecs.umich.edu# Default duplicate option is to use hard links, but this messes up
2572093SN/A# when you use emacs to edit a file in the target dir, as emacs moves
2582687Sksewell@umich.edu# file to file~ then copies to file, breaking the link.  Symbolic
2592687Sksewell@umich.edu# (soft) links work better.
2603114Sgblack@eecs.umich.edumain.SetOption('duplicate', 'soft-copy')
2612687Sksewell@umich.edu
2622238SN/A#
2632238SN/A# Set up global sticky variables... these are common to an entire build
2643114Sgblack@eecs.umich.edu# tree (not specific to a particular build like ALPHA_SE)
2652238SN/A#
2662238SN/A
2672238SN/Aglobal_vars_file = joinpath(build_root, 'variables.global')
2683114Sgblack@eecs.umich.edu
2692238SN/Aglobal_vars = Variables(global_vars_file, args=ARGUMENTS)
2702238SN/A
2712238SN/Aglobal_vars.AddVariables(
2723114Sgblack@eecs.umich.edu    ('CC', 'C compiler', environ.get('CC', main['CC'])),
2732238SN/A    ('CXX', 'C++ compiler', environ.get('CXX', main['CXX'])),
2742238SN/A    ('PROTOC', 'protoc tool', environ.get('PROTOC', 'protoc')),
2752238SN/A    ('BATCH', 'Use batch pool for build and tests', False),
2763114Sgblack@eecs.umich.edu    ('BATCH_CMD', 'Batch pool submission command name', 'qdo'),
2772238SN/A    ('M5_BUILD_CACHE', 'Cache built objects in this directory', False),
2782238SN/A    ('EXTRAS', 'Add extra directories to the compilation', '')
2792238SN/A    )
2803114Sgblack@eecs.umich.edu
2812238SN/A# Update main environment with values from ARGUMENTS & global_vars_file
2822238SN/Aglobal_vars.Update(main)
2832238SN/Ahelp_texts["global_vars"] += global_vars.GenerateHelpText(main)
2843114Sgblack@eecs.umich.edu
2852238SN/A# Save sticky variable settings back to current variables file
2862238SN/Aglobal_vars.Save(global_vars_file, main)
2872238SN/A
2883114Sgblack@eecs.umich.edu# Parse EXTRAS variable to build list of all directories where we're
2892238SN/A# look for sources etc.  This list is exported as extras_dir_list.
2902238SN/Abase_dir = main.srcdir.abspath
2912238SN/Aif main['EXTRAS']:
2922238SN/A    extras_dir_list = makePathListAbsolute(main['EXTRAS'].split(':'))
2932238SN/Aelse:
2942238SN/A    extras_dir_list = []
2953114Sgblack@eecs.umich.edu
2962238SN/AExport('base_dir')
2972238SN/AExport('extras_dir_list')
2982238SN/A
2993114Sgblack@eecs.umich.edu# the ext directory should be on the #includes path
3002238SN/Amain.Append(CPPPATH=[Dir('ext')])
3012238SN/A
3022238SN/A# Add shared top-level headers
3033114Sgblack@eecs.umich.edumain.Prepend(CPPPATH=Dir('include'))
3042238SN/A
3052238SN/Aif GetOption('verbose'):
3062238SN/A    def MakeAction(action, string, *args, **kwargs):
3073114Sgblack@eecs.umich.edu        return Action(action, *args, **kwargs)
3082238SN/Aelse:
3092238SN/A    MakeAction = Action
3101354SN/A    main['CCCOMSTR']        = Transform("CC")
3111354SN/A    main['CXXCOMSTR']       = Transform("CXX")
3121354SN/A    main['ASCOMSTR']        = Transform("AS")
3131354SN/A    main['ARCOMSTR']        = Transform("AR", 0)
3141354SN/A    main['LINKCOMSTR']      = Transform("LINK", 0)
3151354SN/A    main['SHLINKCOMSTR']    = Transform("SHLINK", 0)
3161354SN/A    main['RANLIBCOMSTR']    = Transform("RANLIB", 0)
3171354SN/A    main['M4COMSTR']        = Transform("M4")
3181354SN/A    main['SHCCCOMSTR']      = Transform("SHCC")
3191354SN/A    main['SHCXXCOMSTR']     = Transform("SHCXX")
3201354SN/AExport('MakeAction')
3211354SN/A
3221354SN/A# Initialize the Link-Time Optimization (LTO) flags
3231354SN/Amain['LTO_CCFLAGS'] = []
3241609SN/Amain['LTO_LDFLAGS'] = []
3251354SN/A
3261354SN/A# According to the readme, tcmalloc works best if the compiler doesn't
3271354SN/A# assume that we're using the builtin malloc and friends. These flags
3281354SN/A# are compiler-specific, so we need to set them after we detect which
329360SN/A# compiler we're using.
330360SN/Amain['TCMALLOC_CCFLAGS'] = []
331360SN/A
332360SN/ACXX_version = readCommand([main['CXX'],'--version'], exception=False)
333360SN/ACXX_V = readCommand([main['CXX'],'-V'], exception=False)
334360SN/A
335360SN/Amain['GCC'] = CXX_version and CXX_version.find('g++') >= 0
3363113Sgblack@eecs.umich.edumain['CLANG'] = CXX_version and CXX_version.find('clang') >= 0
3373113Sgblack@eecs.umich.eduif main['GCC'] + main['CLANG'] > 1:
3383113Sgblack@eecs.umich.edu    print 'Error: How can we have two at the same time?'
3393113Sgblack@eecs.umich.edu    Exit(1)
3403113Sgblack@eecs.umich.edu
3413113Sgblack@eecs.umich.edu# Set up default C++ compiler flags
3423113Sgblack@eecs.umich.eduif main['GCC'] or main['CLANG']:
3433113Sgblack@eecs.umich.edu    # As gcc and clang share many flags, do the common parts here
3443113Sgblack@eecs.umich.edu    main.Append(CCFLAGS=['-pipe'])
3453113Sgblack@eecs.umich.edu    main.Append(CCFLAGS=['-fno-strict-aliasing'])
3463113Sgblack@eecs.umich.edu    # Enable -Wall and -Wextra and then disable the few warnings that
3473113Sgblack@eecs.umich.edu    # we consistently violate
3483113Sgblack@eecs.umich.edu    main.Append(CCFLAGS=['-Wall', '-Wundef', '-Wextra',
3493113Sgblack@eecs.umich.edu                         '-Wno-sign-compare', '-Wno-unused-parameter'])
3503113Sgblack@eecs.umich.edu    # We always compile using C++11
3513113Sgblack@eecs.umich.edu    main.Append(CXXFLAGS=['-std=c++11'])
3523113Sgblack@eecs.umich.edu    if sys.platform.startswith('freebsd'):
3533113Sgblack@eecs.umich.edu        main.Append(CCFLAGS=['-I/usr/local/include'])
3543113Sgblack@eecs.umich.edu        main.Append(CXXFLAGS=['-I/usr/local/include'])
3553113Sgblack@eecs.umich.edu
3563113Sgblack@eecs.umich.edu    main['FILTER_PSHLINKFLAGS'] = lambda x: str(x).replace(' -shared', '')
3573113Sgblack@eecs.umich.edu    main['PSHLINKFLAGS'] = main.subst('${FILTER_PSHLINKFLAGS(SHLINKFLAGS)}')
3583113Sgblack@eecs.umich.edu    main['PLINKFLAGS'] = main.subst('${LINKFLAGS}')
3593277Sgblack@eecs.umich.edu    shared_partial_flags = ['-r', '-nostdlib']
3603277Sgblack@eecs.umich.edu    main.Append(PSHLINKFLAGS=shared_partial_flags)
3613277Sgblack@eecs.umich.edu    main.Append(PLINKFLAGS=shared_partial_flags)
3623277Sgblack@eecs.umich.eduelse:
3633277Sgblack@eecs.umich.edu    print termcap.Yellow + termcap.Bold + 'Error' + termcap.Normal,
3643277Sgblack@eecs.umich.edu    print "Don't know what compiler options to use for your compiler."
3653277Sgblack@eecs.umich.edu    print termcap.Yellow + '       compiler:' + termcap.Normal, main['CXX']
3663277Sgblack@eecs.umich.edu    print termcap.Yellow + '       version:' + termcap.Normal,
3673113Sgblack@eecs.umich.edu    if not CXX_version:
3683113Sgblack@eecs.umich.edu        print termcap.Yellow + termcap.Bold + "COMMAND NOT FOUND!" +\
3693113Sgblack@eecs.umich.edu               termcap.Normal
3703113Sgblack@eecs.umich.edu    else:
3713113Sgblack@eecs.umich.edu        print CXX_version.replace('\n', '<nl>')
3723113Sgblack@eecs.umich.edu    print "       If you're trying to use a compiler other than GCC"
3733113Sgblack@eecs.umich.edu    print "       or clang, there appears to be something wrong with your"
3743114Sgblack@eecs.umich.edu    print "       environment."
3753113Sgblack@eecs.umich.edu    print "       "
3763114Sgblack@eecs.umich.edu    print "       If you are trying to use a compiler other than those listed"
3773113Sgblack@eecs.umich.edu    print "       above you will need to ease fix SConstruct and "
3783114Sgblack@eecs.umich.edu    print "       src/SConscript to support that compiler."
3793113Sgblack@eecs.umich.edu    Exit(1)
3804061Sgblack@eecs.umich.edu
3814061Sgblack@eecs.umich.eduif main['GCC']:
3824061Sgblack@eecs.umich.edu    # Check for a supported version of gcc. >= 4.8 is chosen for its
3833113Sgblack@eecs.umich.edu    # level of c++11 support. See
3843113Sgblack@eecs.umich.edu    # http://gcc.gnu.org/projects/cxx0x.html for details.
3853113Sgblack@eecs.umich.edu    gcc_version = readCommand([main['CXX'], '-dumpversion'], exception=False)
3863113Sgblack@eecs.umich.edu    if compareVersions(gcc_version, "4.8") < 0:
3873113Sgblack@eecs.umich.edu        print 'Error: gcc version 4.8 or newer required.'
3883113Sgblack@eecs.umich.edu        print '       Installed version:', gcc_version
3893113Sgblack@eecs.umich.edu        Exit(1)
3903113Sgblack@eecs.umich.edu
3913113Sgblack@eecs.umich.edu    main['GCC_VERSION'] = gcc_version
3923113Sgblack@eecs.umich.edu
3933113Sgblack@eecs.umich.edu    if compareVersions(gcc_version, '4.9') >= 0:
3943113Sgblack@eecs.umich.edu        # Incremental linking with LTO is currently broken in gcc versions
3953113Sgblack@eecs.umich.edu        # 4.9 and above. A version where everything works completely hasn't
3963113Sgblack@eecs.umich.edu        # yet been identified.
3973113Sgblack@eecs.umich.edu        #
3983113Sgblack@eecs.umich.edu        # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67548
3993113Sgblack@eecs.umich.edu        main['BROKEN_INCREMENTAL_LTO'] = True
4003113Sgblack@eecs.umich.edu    if compareVersions(gcc_version, '6.0') >= 0:
4013113Sgblack@eecs.umich.edu        # gcc versions 6.0 and greater accept an -flinker-output flag which
4023113Sgblack@eecs.umich.edu        # selects what type of output the linker should generate. This is
4033113Sgblack@eecs.umich.edu        # necessary for incremental lto to work, but is also broken in
4043113Sgblack@eecs.umich.edu        # current versions of gcc. It may not be necessary in future
4053113Sgblack@eecs.umich.edu        # versions. We add it here since it might be, and as a reminder that
4063113Sgblack@eecs.umich.edu        # it exists. It's excluded if lto is being forced.
4073113Sgblack@eecs.umich.edu        #
4083113Sgblack@eecs.umich.edu        # https://gcc.gnu.org/gcc-6/changes.html
4093113Sgblack@eecs.umich.edu        # https://gcc.gnu.org/ml/gcc-patches/2015-11/msg03161.html
4103113Sgblack@eecs.umich.edu        # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69866
4113113Sgblack@eecs.umich.edu        if not GetOption('force_lto'):
4123113Sgblack@eecs.umich.edu            main.Append(PSHLINKFLAGS='-flinker-output=rel')
4133113Sgblack@eecs.umich.edu            main.Append(PLINKFLAGS='-flinker-output=rel')
4143113Sgblack@eecs.umich.edu
4153113Sgblack@eecs.umich.edu    # gcc from version 4.8 and above generates "rep; ret" instructions
4163113Sgblack@eecs.umich.edu    # to avoid performance penalties on certain AMD chips. Older
4173113Sgblack@eecs.umich.edu    # assemblers detect this as an error, "Error: expecting string
4183113Sgblack@eecs.umich.edu    # instruction after `rep'"
4193113Sgblack@eecs.umich.edu    as_version_raw = readCommand([main['AS'], '-v', '/dev/null',
4203113Sgblack@eecs.umich.edu                                  '-o', '/dev/null'],
4213113Sgblack@eecs.umich.edu                                 exception=False).split()
4223113Sgblack@eecs.umich.edu
4233113Sgblack@eecs.umich.edu    # version strings may contain extra distro-specific
4243113Sgblack@eecs.umich.edu    # qualifiers, so play it safe and keep only what comes before
4253113Sgblack@eecs.umich.edu    # the first hyphen
4263113Sgblack@eecs.umich.edu    as_version = as_version_raw[-1].split('-')[0] if as_version_raw else None
4273113Sgblack@eecs.umich.edu
4283113Sgblack@eecs.umich.edu    if not as_version or compareVersions(as_version, "2.23") < 0:
4293113Sgblack@eecs.umich.edu        print termcap.Yellow + termcap.Bold + \
4303113Sgblack@eecs.umich.edu            'Warning: This combination of gcc and binutils have' + \
4313113Sgblack@eecs.umich.edu            ' known incompatibilities.\n' + \
432378SN/A            '         If you encounter build problems, please update ' + \
433378SN/A            'binutils to 2.23.' + \
434378SN/A            termcap.Normal
435360SN/A
4361450SN/A    # Make sure we warn if the user has requested to compile with the
4373114Sgblack@eecs.umich.edu    # Undefined Benahvior Sanitizer and this version of gcc does not
4382680Sktlim@umich.edu    # support it.
439360SN/A    if GetOption('with_ubsan') and \
4402680Sktlim@umich.edu            compareVersions(gcc_version, '4.9') < 0:
4412680Sktlim@umich.edu        print termcap.Yellow + termcap.Bold + \
442360SN/A            'Warning: UBSan is only supported using gcc 4.9 and later.' + \
4431969SN/A            termcap.Normal
444360SN/A
445360SN/A    disable_lto = GetOption('no_lto')
446360SN/A    if not disable_lto and main.get('BROKEN_INCREMENTAL_LTO', False) and \
4471458SN/A            not GetOption('force_lto'):
448360SN/A        print termcap.Yellow + termcap.Bold + \
449360SN/A            'Warning: Your compiler doesn\'t support incremental linking' + \
450360SN/A            ' and lto at the same time, so lto is being disabled. To force' + \
4512553SN/A            ' lto on anyway, use the --force-lto option. That will disable' + \
4522553SN/A            ' partial linking.' + \
4532553SN/A            termcap.Normal
4542553SN/A        disable_lto = True
4552553SN/A
4562553SN/A    # Add the appropriate Link-Time Optimization (LTO) flags
4572553SN/A    # unless LTO is explicitly turned off. Note that these flags
4582553SN/A    # are only used by the fast target.
4591458SN/A    if not disable_lto:
460360SN/A        # Pass the LTO flag when compiling to produce GIMPLE
461360SN/A        # output, we merely create the flags here and only append
4621706SN/A        # them later
4632680Sktlim@umich.edu        main['LTO_CCFLAGS'] = ['-flto=%d' % GetOption('num_jobs')]
464360SN/A
465360SN/A        # Use the same amount of jobs for LTO as we are running
466360SN/A        # scons with
467378SN/A        main['LTO_LDFLAGS'] = ['-flto=%d' % GetOption('num_jobs')]
468360SN/A
4691450SN/A    main.Append(TCMALLOC_CCFLAGS=['-fno-builtin-malloc', '-fno-builtin-calloc',
4703114Sgblack@eecs.umich.edu                                  '-fno-builtin-realloc', '-fno-builtin-free'])
4712680Sktlim@umich.edu
472360SN/A    # add option to check for undeclared overrides
473360SN/A    if compareVersions(gcc_version, "5.0") > 0:
474360SN/A        main.Append(CCFLAGS=['-Wno-error=suggest-override'])
4752680Sktlim@umich.edu
4761458SN/A    # The address sanitizer is available for gcc >= 4.8
477360SN/A    if GetOption('with_asan'):
478360SN/A        if GetOption('with_ubsan') and \
479360SN/A                compareVersions(env['GCC_VERSION'], '4.9') >= 0:
480360SN/A            env.Append(CCFLAGS=['-fsanitize=address,undefined',
4811706SN/A                                '-fno-omit-frame-pointer'],
4821458SN/A                       LINKFLAGS='-fsanitize=address,undefined')
483360SN/A        else:
484360SN/A            env.Append(CCFLAGS=['-fsanitize=address',
4852680Sktlim@umich.edu                                '-fno-omit-frame-pointer'],
4862680Sktlim@umich.edu                       LINKFLAGS='-fsanitize=address')
487360SN/A    # Only gcc >= 4.9 supports UBSan, so check both the version
488360SN/A    # and the command-line option before adding the compiler and
489360SN/A    # linker flags.
490360SN/A    elif GetOption('with_ubsan') and \
491360SN/A            compareVersions(env['GCC_VERSION'], '4.9') >= 0:
492360SN/A        env.Append(CCFLAGS='-fsanitize=undefined')
493360SN/A        env.Append(LINKFLAGS='-fsanitize=undefined')
494360SN/A
495360SN/Aelif main['CLANG']:
496360SN/A    # Check for a supported version of clang, >= 3.1 is needed to
497360SN/A    # support similar features as gcc 4.8. See
498360SN/A    # http://clang.llvm.org/cxx_status.html for details
4991706SN/A    clang_version_re = re.compile(".* version (\d+\.\d+)")
500360SN/A    clang_version_match = clang_version_re.search(CXX_version)
501360SN/A    if (clang_version_match):
502360SN/A        clang_version = clang_version_match.groups()[0]
503360SN/A        if compareVersions(clang_version, "3.1") < 0:
504360SN/A            print 'Error: clang version 3.1 or newer required.'
5053669Sbinkertn@umich.edu            print '       Installed version:', clang_version
5063669Sbinkertn@umich.edu            Exit(1)
5073669Sbinkertn@umich.edu    else:
5081706SN/A        print 'Error: Unable to determine clang version.'
5091706SN/A        Exit(1)
510360SN/A
511360SN/A    # clang has a few additional warnings that we disable, extraneous
512360SN/A    # parantheses are allowed due to Ruby's printing of the AST,
5131970SN/A    # finally self assignments are allowed as the generated CPU code
514360SN/A    # is relying on this
515360SN/A    main.Append(CCFLAGS=['-Wno-parentheses',
516360SN/A                         '-Wno-self-assign',
5171999SN/A                         # Some versions of libstdc++ (4.8?) seem to
5181999SN/A                         # use struct hash and class hash
5191999SN/A                         # interchangeably.
5203114Sgblack@eecs.umich.edu                         '-Wno-mismatched-tags',
5212680Sktlim@umich.edu                         ])
5221999SN/A
5231999SN/A    main.Append(TCMALLOC_CCFLAGS=['-fno-builtin'])
5241999SN/A
5252680Sktlim@umich.edu    # On Mac OS X/Darwin we need to also use libc++ (part of XCode) as
5261999SN/A    # opposed to libstdc++, as the later is dated.
5271999SN/A    if sys.platform == "darwin":
5282680Sktlim@umich.edu        main.Append(CXXFLAGS=['-stdlib=libc++'])
5291999SN/A        main.Append(LIBS=['c++'])
5301999SN/A
5311999SN/A    # On FreeBSD we need libthr.
5321999SN/A    if sys.platform.startswith('freebsd'):
5331999SN/A        main.Append(LIBS=['thr'])
5343669Sbinkertn@umich.edu
5353669Sbinkertn@umich.edu    # We require clang >= 3.1, so there is no need to check any
5363669Sbinkertn@umich.edu    # versions here.
5371999SN/A    if GetOption('with_ubsan'):
5381999SN/A        if GetOption('with_asan'):
5391999SN/A            env.Append(CCFLAGS=['-fsanitize=address,undefined',
5402218SN/A                                '-fno-omit-frame-pointer'],
5411999SN/A                       LINKFLAGS='-fsanitize=address,undefined')
5421999SN/A        else:
5431999SN/A            env.Append(CCFLAGS='-fsanitize=undefined',
5441999SN/A                       LINKFLAGS='-fsanitize=undefined')
5451999SN/A
5461999SN/A    elif GetOption('with_asan'):
5471999SN/A        env.Append(CCFLAGS=['-fsanitize=address',
5481999SN/A                            '-fno-omit-frame-pointer'],
5493114Sgblack@eecs.umich.edu                   LINKFLAGS='-fsanitize=address')
5502680Sktlim@umich.edu
5511999SN/Aelse:
5522680Sktlim@umich.edu    print termcap.Yellow + termcap.Bold + 'Error' + termcap.Normal,
5531999SN/A    print "Don't know what compiler options to use for your compiler."
5541999SN/A    print termcap.Yellow + '       compiler:' + termcap.Normal, main['CXX']
5551999SN/A    print termcap.Yellow + '       version:' + termcap.Normal,
5561999SN/A    if not CXX_version:
5571999SN/A        print termcap.Yellow + termcap.Bold + "COMMAND NOT FOUND!" +\
5582680Sktlim@umich.edu               termcap.Normal
5591999SN/A    else:
5601999SN/A        print CXX_version.replace('\n', '<nl>')
5611999SN/A    print "       If you're trying to use a compiler other than GCC"
5621999SN/A    print "       or clang, there appears to be something wrong with your"
5631999SN/A    print "       environment."
5641999SN/A    print "       "
5651999SN/A    print "       If you are trying to use a compiler other than those listed"
5661999SN/A    print "       above you will need to ease fix SConstruct and "
5672218SN/A    print "       src/SConscript to support that compiler."
5681999SN/A    Exit(1)
5691999SN/A
5701999SN/A# Set up common yacc/bison flags (needed for Ruby)
5711999SN/Amain['YACCFLAGS'] = '-d'
5721999SN/Amain['YACCHXXFILESUFFIX'] = '.hh'
573378SN/A
574360SN/A# Do this after we save setting back, or else we'll tack on an
5751450SN/A# extra 'qdo' every time we run scons.
5763114Sgblack@eecs.umich.eduif main['BATCH']:
5772680Sktlim@umich.edu    main['CC']     = main['BATCH_CMD'] + ' ' + main['CC']
578360SN/A    main['CXX']    = main['BATCH_CMD'] + ' ' + main['CXX']
579360SN/A    main['AS']     = main['BATCH_CMD'] + ' ' + main['AS']
580360SN/A    main['AR']     = main['BATCH_CMD'] + ' ' + main['AR']
5812680Sktlim@umich.edu    main['RANLIB'] = main['BATCH_CMD'] + ' ' + main['RANLIB']
5822400SN/A
583360SN/Aif sys.platform == 'cygwin':
5843669Sbinkertn@umich.edu    # cygwin has some header file issues...
5853669Sbinkertn@umich.edu    main.Append(CCFLAGS=["-Wno-uninitialized"])
5863669Sbinkertn@umich.edu
587360SN/A# Check for the protobuf compiler
588360SN/Aprotoc_version = readCommand([main['PROTOC'], '--version'],
589360SN/A                             exception='').split()
590360SN/A
5912218SN/A# First two words should be "libprotoc x.y.z"
592360SN/Aif len(protoc_version) < 2 or protoc_version[0] != 'libprotoc':
5933113Sgblack@eecs.umich.edu    print termcap.Yellow + termcap.Bold + \
594360SN/A        'Warning: Protocol buffer compiler (protoc) not found.\n' + \
5951458SN/A        '         Please install protobuf-compiler for tracing support.' + \
596360SN/A        termcap.Normal
597360SN/A    main['PROTOC'] = False
598360SN/Aelse:
5991999SN/A    # Based on the availability of the compress stream wrappers,
6001999SN/A    # require 2.1.0
6011999SN/A    min_protoc_version = '2.1.0'
6023114Sgblack@eecs.umich.edu    if compareVersions(protoc_version[1], min_protoc_version) < 0:
6032680Sktlim@umich.edu        print termcap.Yellow + termcap.Bold + \
6041999SN/A            'Warning: protoc version', min_protoc_version, \
6052680Sktlim@umich.edu            'or newer required.\n' + \
6061999SN/A            '         Installed version:', protoc_version[1], \
6071999SN/A            termcap.Normal
6081999SN/A        main['PROTOC'] = False
6091999SN/A    else:
6101999SN/A        # Attempt to determine the appropriate include path and
6112764Sstever@eecs.umich.edu        # library path using pkg-config, that means we also need to
6122064SN/A        # check for pkg-config. Note that it is possible to use
6132064SN/A        # protobuf without the involvement of pkg-config. Later on we
6142064SN/A        # check go a library config check and at that point the test
6152064SN/A        # will fail if libprotobuf cannot be found.
6161999SN/A        if readCommand(['pkg-config', '--version'], exception=''):
6172064SN/A            try:
6181999SN/A                # Attempt to establish what linking flags to add for protobuf
6191999SN/A                # using pkg-config
6202218SN/A                main.ParseConfig('pkg-config --cflags --libs-only-L protobuf')
6211999SN/A            except:
6223114Sgblack@eecs.umich.edu                print termcap.Yellow + termcap.Bold + \
6233114Sgblack@eecs.umich.edu                    'Warning: pkg-config could not get protobuf flags.' + \
6241999SN/A                    termcap.Normal
6251999SN/A
6261999SN/A
6271999SN/A# Check for 'timeout' from GNU coreutils. If present, regressions will
6281999SN/A# be run with a time limit. We require version 8.13 since we rely on
629378SN/A# support for the '--foreground' option.
630360SN/Aif sys.platform.startswith('freebsd'):
6311450SN/A    timeout_lines = readCommand(['gtimeout', '--version'],
6323114Sgblack@eecs.umich.edu                                exception='').splitlines()
6332680Sktlim@umich.eduelse:
634360SN/A    timeout_lines = readCommand(['timeout', '--version'],
635360SN/A                                exception='').splitlines()
636360SN/A# Get the first line and tokenize it
6372680Sktlim@umich.edutimeout_version = timeout_lines[0].split() if timeout_lines else []
6382400SN/Amain['TIMEOUT'] =  timeout_version and \
639360SN/A    compareVersions(timeout_version[-1], '8.13') >= 0
6403669Sbinkertn@umich.edu
6413669Sbinkertn@umich.edu# Add a custom Check function to test for structure members.
6423669Sbinkertn@umich.edudef CheckMember(context, include, decl, member, include_quotes="<>"):
643360SN/A    context.Message("Checking for member %s in %s..." %
644360SN/A                    (member, decl))
645360SN/A    text = """
646360SN/A#include %(header)s
6471458SN/Aint main(){
648360SN/A  %(decl)s test;
6493113Sgblack@eecs.umich.edu  (void)test.%(member)s;
650360SN/A  return 0;
6511458SN/A};
652360SN/A""" % { "header" : include_quotes[0] + include + include_quotes[1],
653360SN/A        "decl" : decl,
6541999SN/A        "member" : member,
6551999SN/A        }
6561999SN/A
6573114Sgblack@eecs.umich.edu    ret = context.TryCompile(text, extension=".cc")
6582680Sktlim@umich.edu    context.Result(ret)
6591999SN/A    return ret
6601999SN/A
6611999SN/A# Platform-specific configuration.  Note again that we assume that all
6622680Sktlim@umich.edu# builds under a given build root run on the same host platform.
6632400SN/Aconf = Configure(main,
6641999SN/A                 conf_dir = joinpath(build_root, '.scons_config'),
6653669Sbinkertn@umich.edu                 log_file = joinpath(build_root, 'scons_config.log'),
6663669Sbinkertn@umich.edu                 custom_tests = {
6673669Sbinkertn@umich.edu        'CheckMember' : CheckMember,
6682764Sstever@eecs.umich.edu        })
6692064SN/A
6702064SN/A# Check if we should compile a 64 bit binary on Mac OS X/Darwin
6712064SN/Atry:
6721999SN/A    import platform
6731999SN/A    uname = platform.uname()
6742064SN/A    if uname[0] == 'Darwin' and compareVersions(uname[2], '9.0.0') >= 0:
6751999SN/A        if int(readCommand('sysctl -n hw.cpu64bit_capable')[0]):
6761999SN/A            main.Append(CCFLAGS=['-arch', 'x86_64'])
6771999SN/A            main.Append(CFLAGS=['-arch', 'x86_64'])
6781999SN/A            main.Append(LINKFLAGS=['-arch', 'x86_64'])
6793114Sgblack@eecs.umich.edu            main.Append(ASFLAGS=['-arch', 'x86_64'])
6801999SN/Aexcept:
6811999SN/A    pass
6821999SN/A
6831999SN/A# Recent versions of scons substitute a "Null" object for Configure()
684378SN/A# when configuration isn't necessary, e.g., if the "--help" option is
685360SN/A# present.  Unfortuantely this Null object always returns false,
6861450SN/A# breaking all our configuration checks.  We replace it with our own
6873114Sgblack@eecs.umich.edu# more optimistic null object that returns True instead.
6882680Sktlim@umich.eduif not conf:
689360SN/A    def NullCheck(*args, **kwargs):
6902680Sktlim@umich.edu        return True
691360SN/A
6921969SN/A    class NullConf:
693360SN/A        def __init__(self, env):
694360SN/A            self.env = env
6951458SN/A        def Finish(self):
696360SN/A            return self.env
697360SN/A        def __getattr__(self, mname):
698360SN/A            return NullCheck
699360SN/A
700360SN/A    conf = NullConf(main)
7011458SN/A
702360SN/A# Cache build files in the supplied directory.
7033114Sgblack@eecs.umich.eduif main['M5_BUILD_CACHE']:
7043114Sgblack@eecs.umich.edu    print 'Using build cache located at', main['M5_BUILD_CACHE']
7052021SN/A    CacheDir(main['M5_BUILD_CACHE'])
7061458SN/A
707360SN/Amain['USE_PYTHON'] = not GetOption('without_python')
708360SN/Aif main['USE_PYTHON']:
709360SN/A    # Find Python include and library directories for embedding the
7101706SN/A    # interpreter. We rely on python-config to resolve the appropriate
7111706SN/A    # includes and linker flags. ParseConfig does not seem to understand
7121706SN/A    # the more exotic linker flags such as -Xlinker and -export-dynamic so
7133114Sgblack@eecs.umich.edu    # we add them explicitly below. If you want to link in an alternate
7142680Sktlim@umich.edu    # version of python, see above for instructions on how to invoke
7151706SN/A    # scons with the appropriate PATH set.
7161706SN/A    #
7171706SN/A    # First we check if python2-config exists, else we use python-config
7182680Sktlim@umich.edu    python_config = readCommand(['which', 'python2-config'],
7192400SN/A                                exception='').strip()
7201706SN/A    if not os.path.exists(python_config):
7213669Sbinkertn@umich.edu        python_config = readCommand(['which', 'python-config'],
7223669Sbinkertn@umich.edu                                    exception='').strip()
7233669Sbinkertn@umich.edu    py_includes = readCommand([python_config, '--includes'],
7241706SN/A                              exception='').split()
7251706SN/A    # Strip the -I from the include folders before adding them to the
7261706SN/A    # CPPPATH
7271706SN/A    main.Append(CPPPATH=map(lambda inc: inc[2:], py_includes))
7282218SN/A
7291706SN/A    # Read the linker flags and split them into libraries and other link
7303114Sgblack@eecs.umich.edu    # flags. The libraries are added later through the call the CheckLib.
7313114Sgblack@eecs.umich.edu    py_ld_flags = readCommand([python_config, '--ldflags'],
7321706SN/A        exception='').split()
7331706SN/A    py_libs = []
7341706SN/A    for lib in py_ld_flags:
7351706SN/A         if not lib.startswith('-l'):
7361706SN/A             main.Append(LINKFLAGS=[lib])
7371706SN/A         else:
7381706SN/A             lib = lib[2:]
7391706SN/A             if lib not in py_libs:
7403114Sgblack@eecs.umich.edu                 py_libs.append(lib)
7412680Sktlim@umich.edu
7421706SN/A    # verify that this stuff works
7432680Sktlim@umich.edu    if not conf.CheckHeader('Python.h', '<>'):
7441706SN/A        print "Error: can't find Python.h header in", py_includes
7451706SN/A        print "Install Python headers (package python-dev on Ubuntu and RedHat)"
7461706SN/A        Exit(1)
7471706SN/A
7481706SN/A    for lib in py_libs:
7491706SN/A        if not conf.CheckLib(lib):
7501706SN/A            print "Error: can't find library %s required by python" % lib
7511706SN/A            Exit(1)
7522218SN/A
7531706SN/A# On Solaris you need to use libsocket for socket ops
7543114Sgblack@eecs.umich.eduif not conf.CheckLibWithHeader(None, 'sys/socket.h', 'C++', 'accept(0,0,0);'):
7553114Sgblack@eecs.umich.edu   if not conf.CheckLibWithHeader('socket', 'sys/socket.h', 'C++', 'accept(0,0,0);'):
7561706SN/A       print "Can't find library with socket calls (e.g. accept())"
7571706SN/A       Exit(1)
7581706SN/A
7591706SN/A# Check for zlib.  If the check passes, libz will be automatically
7601706SN/A# added to the LIBS environment variable.
7611999SN/Aif not conf.CheckLibWithHeader('z', 'zlib.h', 'C++','zlibVersion();'):
7621999SN/A    print 'Error: did not find needed zlib compression library '\
7631999SN/A          'and/or zlib.h header file.'
7643114Sgblack@eecs.umich.edu    print '       Please install zlib and try again.'
7652680Sktlim@umich.edu    Exit(1)
7661999SN/A
7672680Sktlim@umich.edu# If we have the protobuf compiler, also make sure we have the
7681999SN/A# development libraries. If the check passes, libprotobuf will be
7691999SN/A# automatically added to the LIBS environment variable. After
7701999SN/A# this, we can use the HAVE_PROTOBUF flag to determine if we have
7711999SN/A# got both protoc and libprotobuf available.
7721999SN/Amain['HAVE_PROTOBUF'] = main['PROTOC'] and \
7732680Sktlim@umich.edu    conf.CheckLibWithHeader('protobuf', 'google/protobuf/message.h',
7742680Sktlim@umich.edu                            'C++', 'GOOGLE_PROTOBUF_VERIFY_VERSION;')
7752680Sktlim@umich.edu
7761999SN/A# If we have the compiler but not the library, print another warning.
7771999SN/Aif main['PROTOC'] and not main['HAVE_PROTOBUF']:
7781999SN/A    print termcap.Yellow + termcap.Bold + \
7791999SN/A        'Warning: did not find protocol buffer library and/or headers.\n' + \
7802461SN/A    '       Please install libprotobuf-dev for tracing support.' + \
7812461SN/A    termcap.Normal
7822461SN/A
7832091SN/A# Check for librt.
7841999SN/Ahave_posix_clock = \
7852461SN/A    conf.CheckLibWithHeader(None, 'time.h', 'C',
7862461SN/A                            'clock_nanosleep(0,0,NULL,NULL);') or \
7871999SN/A    conf.CheckLibWithHeader('rt', 'time.h', 'C',
7881999SN/A                            'clock_nanosleep(0,0,NULL,NULL);')
7891999SN/A
7901999SN/Ahave_posix_timers = \
7911999SN/A    conf.CheckLibWithHeader([None, 'rt'], [ 'time.h', 'signal.h' ], 'C',
7921999SN/A                            'timer_create(CLOCK_MONOTONIC, NULL, NULL);')
7931999SN/A
7941999SN/Aif not GetOption('without_tcmalloc'):
7951999SN/A    if conf.CheckLib('tcmalloc'):
7961999SN/A        main.Append(CCFLAGS=main['TCMALLOC_CCFLAGS'])
7972218SN/A    elif conf.CheckLib('tcmalloc_minimal'):
7981999SN/A        main.Append(CCFLAGS=main['TCMALLOC_CCFLAGS'])
7991999SN/A    else:
8001999SN/A        print termcap.Yellow + termcap.Bold + \
8011999SN/A              "You can get a 12% performance improvement by "\
8021999SN/A              "installing tcmalloc (libgoogle-perftools-dev package "\
803378SN/A              "on Ubuntu or RedHat)." + termcap.Normal
804378SN/A
805378SN/A
806378SN/A# Detect back trace implementations. The last implementation in the
807378SN/A# list will be used by default.
808378SN/Abacktrace_impls = [ "none" ]
809378SN/A
810378SN/Aif conf.CheckLibWithHeader(None, 'execinfo.h', 'C',
811360SN/A                           'backtrace_symbols_fd((void*)0, 0, 0);'):
812378SN/A    backtrace_impls.append("glibc")
813378SN/Aelif conf.CheckLibWithHeader('execinfo', 'execinfo.h', 'C',
814378SN/A                           'backtrace_symbols_fd((void*)0, 0, 0);'):
815360SN/A    # NetBSD and FreeBSD need libexecinfo.
8161450SN/A    backtrace_impls.append("glibc")
8173114Sgblack@eecs.umich.edu    main.Append(LIBS=['execinfo'])
818360SN/A
8192680Sktlim@umich.eduif backtrace_impls[-1] == "none":
8202680Sktlim@umich.edu    default_backtrace_impl = "none"
8212680Sktlim@umich.edu    print termcap.Yellow + termcap.Bold + \
8222680Sktlim@umich.edu        "No suitable back trace implementation found." + \
8232680Sktlim@umich.edu        termcap.Normal
8242680Sktlim@umich.edu
825360SN/Aif not have_posix_clock:
8262544SN/A    print "Can't find library for POSIX clocks."
8272544SN/A
8282544SN/A# Check for <fenv.h> (C99 FP environment control)
8292544SN/Ahave_fenv = conf.CheckHeader('fenv.h', '<>')
8302544SN/Aif not have_fenv:
8312544SN/A    print "Warning: Header file <fenv.h> not found."
832360SN/A    print "         This host has no IEEE FP rounding mode control."
833360SN/A
8342544SN/A# Check for <png.h> (libpng library needed if wanting to dump
8352544SN/A# frame buffer image in png format)
8362544SN/Ahave_png = conf.CheckHeader('png.h', '<>')
8372544SN/Aif not have_png:
8382544SN/A    print "Warning: Header file <png.h> not found."
8392544SN/A    print "         This host has no libpng library."
8402544SN/A    print "         Disabling support for PNG framebuffers."
8412544SN/A
8422544SN/A# Check if we should enable KVM-based hardware virtualization. The API
8432544SN/A# we rely on exists since version 2.6.36 of the kernel, but somehow
8442553SN/A# the KVM_API_VERSION does not reflect the change. We test for one of
8451969SN/A# the types as a fall back.
8462680Sktlim@umich.eduhave_kvm = conf.CheckHeader('linux/kvm.h', '<>')
847360SN/Aif not have_kvm:
848360SN/A    print "Info: Compatible header file <linux/kvm.h> not found, " \
8491458SN/A        "disabling KVM support."
850360SN/A
851360SN/A# Check if the TUN/TAP driver is available.
852378SN/Ahave_tuntap = conf.CheckHeader('linux/if_tun.h', '<>')
853360SN/Aif not have_tuntap:
8541450SN/A    print "Info: Compatible header file <linux/if_tun.h> not found."
8553114Sgblack@eecs.umich.edu
8562680Sktlim@umich.edu# x86 needs support for xsave. We test for the structure here since we
857360SN/A# won't be able to run new tests by the time we know which ISA we're
8582680Sktlim@umich.edu# targeting.
8592680Sktlim@umich.eduhave_kvm_xsave = conf.CheckTypeSize('struct kvm_xsave',
860360SN/A                                    '#include <linux/kvm.h>') != 0
861360SN/A
8622064SN/A# Check if the requested target ISA is compatible with the host
8632064SN/Adef is_isa_kvm_compatible(isa):
8642064SN/A    try:
8652091SN/A        import platform
8662091SN/A        host_isa = platform.machine()
8672064SN/A    except:
868360SN/A        print "Warning: Failed to determine host ISA."
8692064SN/A        return False
8702064SN/A
8712064SN/A    if not have_posix_timers:
8722064SN/A        print "Warning: Can not enable KVM, host seems to lack support " \
8732064SN/A            "for POSIX timers"
874360SN/A        return False
875360SN/A
8762680Sktlim@umich.edu    if isa == "arm":
8771458SN/A        return host_isa in ( "armv7l", "aarch64" )
878360SN/A    elif isa == "x86":
879360SN/A        if host_isa != "x86_64":
880378SN/A            return False
881360SN/A
8821450SN/A        if not have_kvm_xsave:
8833114Sgblack@eecs.umich.edu            print "KVM on x86 requires xsave support in kernel headers."
8842680Sktlim@umich.edu            return False
885360SN/A
8862680Sktlim@umich.edu        return True
887360SN/A    else:
888360SN/A        return False
889360SN/A
8902091SN/A
8912091SN/A# Check if the exclude_host attribute is available. We want this to
892360SN/A# get accurate instruction counts in KVM.
8932680Sktlim@umich.edumain['HAVE_PERF_ATTR_EXCLUDE_HOST'] = conf.CheckMember(
894360SN/A    'linux/perf_event.h', 'struct perf_event_attr', 'exclude_host')
8951458SN/A
896360SN/A
897360SN/A######################################################################
898360SN/A#
8991999SN/A# Finish the configuration
9001999SN/A#
9011999SN/Amain = conf.Finish()
9023114Sgblack@eecs.umich.edu
9032680Sktlim@umich.edu######################################################################
9041999SN/A#
9051999SN/A# Collect all non-global variables
9061999SN/A#
9072680Sktlim@umich.edu
9082400SN/A# Define the universe of supported ISAs
9091999SN/Aall_isa_list = [ ]
9102680Sktlim@umich.eduall_gpu_isa_list = [ ]
9112680Sktlim@umich.eduExport('all_isa_list')
9121999SN/AExport('all_gpu_isa_list')
9131999SN/A
9141999SN/Aclass CpuModel(object):
9151999SN/A    '''The CpuModel class encapsulates everything the ISA parser needs to
9162091SN/A    know about a particular CPU model.'''
9172091SN/A
9181999SN/A    # Dict of available CPU model objects.  Accessible as CpuModel.dict.
9193669Sbinkertn@umich.edu    dict = {}
9203669Sbinkertn@umich.edu
9213669Sbinkertn@umich.edu    # Constructor.  Automatically adds models to CpuModel.dict.
9223669Sbinkertn@umich.edu    def __init__(self, name, default=False):
9231999SN/A        self.name = name           # name of model
9241999SN/A
9251999SN/A        # This cpu is enabled by default
9261999SN/A        self.default = default
9271999SN/A
9281999SN/A        # Add self to dict
9291999SN/A        if name in CpuModel.dict:
930378SN/A            raise AttributeError, "CpuModel '%s' already registered" % name
931360SN/A        CpuModel.dict[name] = self
9321450SN/A
9333114Sgblack@eecs.umich.eduExport('CpuModel')
9342680Sktlim@umich.edu
935360SN/A# Sticky variables get saved in the variables file so they persist from
9362680Sktlim@umich.edu# one invocation to the next (unless overridden, in which case the new
9372680Sktlim@umich.edu# value becomes sticky).
938360SN/Asticky_vars = Variables(args=ARGUMENTS)
9393670Sbinkertn@umich.eduExport('sticky_vars')
9403670Sbinkertn@umich.edu
941360SN/A# Sticky variables that should be exported
942360SN/Aexport_vars = []
943360SN/AExport('export_vars')
944360SN/A
945360SN/A# For Ruby
946360SN/Aall_protocols = []
947360SN/AExport('all_protocols')
948360SN/Aprotocol_dirs = []
949360SN/AExport('protocol_dirs')
950360SN/Aslicc_includes = []
951360SN/AExport('slicc_includes')
952360SN/A
953360SN/A# Walk the tree and execute all SConsopts scripts that wil add to the
954360SN/A# above variables
955360SN/Aif GetOption('verbose'):
956360SN/A    print "Reading SConsopts"
957360SN/Afor bdir in [ base_dir ] + extras_dir_list:
9583670Sbinkertn@umich.edu    if not isdir(bdir):
9593670Sbinkertn@umich.edu        print "Error: directory '%s' does not exist" % bdir
9603670Sbinkertn@umich.edu        Exit(1)
9613670Sbinkertn@umich.edu    for root, dirs, files in os.walk(bdir):
9623670Sbinkertn@umich.edu        if 'SConsopts' in files:
9633670Sbinkertn@umich.edu            if GetOption('verbose'):
9643670Sbinkertn@umich.edu                print "Reading", joinpath(root, 'SConsopts')
9653670Sbinkertn@umich.edu            SConscript(joinpath(root, 'SConsopts'))
9663670Sbinkertn@umich.edu
9673670Sbinkertn@umich.eduall_isa_list.sort()
9683670Sbinkertn@umich.eduall_gpu_isa_list.sort()
9693670Sbinkertn@umich.edu
9703670Sbinkertn@umich.edusticky_vars.AddVariables(
9713670Sbinkertn@umich.edu    EnumVariable('TARGET_ISA', 'Target ISA', 'alpha', all_isa_list),
9723670Sbinkertn@umich.edu    EnumVariable('TARGET_GPU_ISA', 'Target GPU ISA', 'hsail', all_gpu_isa_list),
9733670Sbinkertn@umich.edu    ListVariable('CPU_MODELS', 'CPU models',
9743670Sbinkertn@umich.edu                 sorted(n for n,m in CpuModel.dict.iteritems() if m.default),
9753670Sbinkertn@umich.edu                 sorted(CpuModel.dict.keys())),
9762680Sktlim@umich.edu    BoolVariable('EFENCE', 'Link with Electric Fence malloc debugger',
977360SN/A                 False),
9781458SN/A    BoolVariable('SS_COMPATIBLE_FP',
979360SN/A                 'Make floating-point results compatible with SimpleScalar',
980360SN/A                 False),
9812553SN/A    BoolVariable('USE_SSE2',
9822553SN/A                 'Compile for SSE2 (-msse2) to get IEEE FP on x86 hosts',
9832553SN/A                 False),
9841354SN/A    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