SConstruct revision 13541:a23fca6305e1
19651SAndreas.Sandberg@ARM.com# -*- mode:python -*-
210858Sandreas.sandberg@arm.com
39651SAndreas.Sandberg@ARM.com# Copyright (c) 2013, 2015-2017 ARM Limited
49651SAndreas.Sandberg@ARM.com# All rights reserved.
59651SAndreas.Sandberg@ARM.com#
69651SAndreas.Sandberg@ARM.com# The license below extends only to copyright in the software and shall
79651SAndreas.Sandberg@ARM.com# not be construed as granting a license to any other intellectual
89651SAndreas.Sandberg@ARM.com# property including but not limited to intellectual property relating
99651SAndreas.Sandberg@ARM.com# to a hardware implementation of the functionality of the software
109651SAndreas.Sandberg@ARM.com# licensed hereunder.  You may use the software subject to the license
119651SAndreas.Sandberg@ARM.com# terms below provided that you ensure that this notice is replicated
129651SAndreas.Sandberg@ARM.com# unmodified and in its entirety in all distributions of the software,
139651SAndreas.Sandberg@ARM.com# modified or unmodified, in source code or in binary form.
149651SAndreas.Sandberg@ARM.com#
159651SAndreas.Sandberg@ARM.com# Copyright (c) 2011 Advanced Micro Devices, Inc.
169651SAndreas.Sandberg@ARM.com# Copyright (c) 2009 The Hewlett-Packard Development Company
179651SAndreas.Sandberg@ARM.com# Copyright (c) 2004-2005 The Regents of The University of Michigan
189651SAndreas.Sandberg@ARM.com# All rights reserved.
199651SAndreas.Sandberg@ARM.com#
209651SAndreas.Sandberg@ARM.com# Redistribution and use in source and binary forms, with or without
219651SAndreas.Sandberg@ARM.com# modification, are permitted provided that the following conditions are
229651SAndreas.Sandberg@ARM.com# met: redistributions of source code must retain the above copyright
239651SAndreas.Sandberg@ARM.com# notice, this list of conditions and the following disclaimer;
249651SAndreas.Sandberg@ARM.com# redistributions in binary form must reproduce the above copyright
259651SAndreas.Sandberg@ARM.com# notice, this list of conditions and the following disclaimer in the
269651SAndreas.Sandberg@ARM.com# documentation and/or other materials provided with the distribution;
279651SAndreas.Sandberg@ARM.com# neither the name of the copyright holders nor the names of its
289651SAndreas.Sandberg@ARM.com# contributors may be used to endorse or promote products derived from
299651SAndreas.Sandberg@ARM.com# this software without specific prior written permission.
309651SAndreas.Sandberg@ARM.com#
319651SAndreas.Sandberg@ARM.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
329651SAndreas.Sandberg@ARM.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
339651SAndreas.Sandberg@ARM.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
349651SAndreas.Sandberg@ARM.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
359651SAndreas.Sandberg@ARM.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
369651SAndreas.Sandberg@ARM.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
379651SAndreas.Sandberg@ARM.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
389651SAndreas.Sandberg@ARM.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
399651SAndreas.Sandberg@ARM.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
409651SAndreas.Sandberg@ARM.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
419651SAndreas.Sandberg@ARM.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
429651SAndreas.Sandberg@ARM.com#
439651SAndreas.Sandberg@ARM.com# Authors: Steve Reinhardt
449651SAndreas.Sandberg@ARM.com#          Nathan Binkert
459651SAndreas.Sandberg@ARM.com
469651SAndreas.Sandberg@ARM.com###################################################
479651SAndreas.Sandberg@ARM.com#
489651SAndreas.Sandberg@ARM.com# SCons top-level build description (SConstruct) file.
499760Sandreas@sandberg.pp.se#
509651SAndreas.Sandberg@ARM.com# While in this directory ('gem5'), just type 'scons' to build the default
519651SAndreas.Sandberg@ARM.com# configuration (see below), or type 'scons build/<CONFIG>/<binary>'
529683Sandreas@sandberg.pp.se# to build some other configuration (e.g., 'build/ALPHA/gem5.opt' for
539753Sandreas@sandberg.pp.se# the optimized full-system version).
549651SAndreas.Sandberg@ARM.com#
559651SAndreas.Sandberg@ARM.com# You can build gem5 in a different directory as long as there is a
569651SAndreas.Sandberg@ARM.com# 'build/<CONFIG>' somewhere along the target path.  The build system
579651SAndreas.Sandberg@ARM.com# expects that all configs under the same build directory are being
589651SAndreas.Sandberg@ARM.com# built for the same host system.
599651SAndreas.Sandberg@ARM.com#
609651SAndreas.Sandberg@ARM.com# Examples:
619753Sandreas@sandberg.pp.se#
629753Sandreas@sandberg.pp.se#   The following two commands are equivalent.  The '-u' option tells
639651SAndreas.Sandberg@ARM.com#   scons to search up the directory tree for this SConstruct file.
649651SAndreas.Sandberg@ARM.com#   % cd <path-to-src>/gem5 ; scons build/ALPHA/gem5.debug
659651SAndreas.Sandberg@ARM.com#   % cd <path-to-src>/gem5/build/ALPHA; scons -u gem5.debug
669651SAndreas.Sandberg@ARM.com#
679651SAndreas.Sandberg@ARM.com#   The following two commands are equivalent and demonstrate building
689651SAndreas.Sandberg@ARM.com#   in a directory outside of the source tree.  The '-C' option tells
699651SAndreas.Sandberg@ARM.com#   scons to chdir to the specified directory to find this SConstruct
709651SAndreas.Sandberg@ARM.com#   file.
719651SAndreas.Sandberg@ARM.com#   % cd <path-to-src>/gem5 ; scons /local/foo/build/ALPHA/gem5.debug
7211399Sandreas.sandberg@arm.com#   % cd /local/foo/build/ALPHA; scons -C <path-to-src>/gem5 gem5.debug
739652SAndreas.Sandberg@ARM.com#
749652SAndreas.Sandberg@ARM.com# You can use 'scons -H' to print scons options.  If you're in this
759651SAndreas.Sandberg@ARM.com# 'gem5' directory (or use -u or -C to tell scons where to find this
769651SAndreas.Sandberg@ARM.com# file), you can use 'scons -h' to print all the gem5-specific build
779651SAndreas.Sandberg@ARM.com# options as well.
789651SAndreas.Sandberg@ARM.com#
799892Sandreas@sandberg.pp.se###################################################
809655SAndreas.Sandberg@ARM.com
819752Sandreas@sandberg.pp.sefrom __future__ import print_function
829752Sandreas@sandberg.pp.se
839651SAndreas.Sandberg@ARM.com# Global Python includes
849651SAndreas.Sandberg@ARM.comimport itertools
859651SAndreas.Sandberg@ARM.comimport os
869651SAndreas.Sandberg@ARM.comimport re
879651SAndreas.Sandberg@ARM.comimport shutil
8810553Salexandru.dutu@amd.comimport subprocess
8910553Salexandru.dutu@amd.comimport sys
9010553Salexandru.dutu@amd.com
9110553Salexandru.dutu@amd.comfrom os import mkdir, environ
9210553Salexandru.dutu@amd.comfrom os.path import abspath, basename, dirname, expanduser, normpath
9310553Salexandru.dutu@amd.comfrom os.path import exists,  isdir, isfile
9410553Salexandru.dutu@amd.comfrom os.path import join as joinpath, split as splitpath
9510553Salexandru.dutu@amd.comfrom re import match
969651SAndreas.Sandberg@ARM.com
979651SAndreas.Sandberg@ARM.com# SCons includes
989651SAndreas.Sandberg@ARM.comimport SCons
999651SAndreas.Sandberg@ARM.comimport SCons.Node
1009651SAndreas.Sandberg@ARM.com
1019651SAndreas.Sandberg@ARM.comfrom m5.util import compareVersions, readCommand
1029651SAndreas.Sandberg@ARM.com
1039651SAndreas.Sandberg@ARM.comhelp_texts = {
1049651SAndreas.Sandberg@ARM.com    "options" : "",
1059651SAndreas.Sandberg@ARM.com    "global_vars" : "",
1069651SAndreas.Sandberg@ARM.com    "local_vars" : ""
1079651SAndreas.Sandberg@ARM.com}
1089651SAndreas.Sandberg@ARM.com
1099651SAndreas.Sandberg@ARM.comExport("help_texts")
1109651SAndreas.Sandberg@ARM.com
1119651SAndreas.Sandberg@ARM.com
1129651SAndreas.Sandberg@ARM.com# There's a bug in scons in that (1) by default, the help texts from
1139651SAndreas.Sandberg@ARM.com# AddOption() are supposed to be displayed when you type 'scons -h'
1149651SAndreas.Sandberg@ARM.com# and (2) you can override the help displayed by 'scons -h' using the
1159651SAndreas.Sandberg@ARM.com# Help() function, but these two features are incompatible: once
1169651SAndreas.Sandberg@ARM.com# you've overridden the help text using Help(), there's no way to get
1179651SAndreas.Sandberg@ARM.com# at the help texts from AddOptions.  See:
1189651SAndreas.Sandberg@ARM.com#     http://scons.tigris.org/issues/show_bug.cgi?id=2356
1199651SAndreas.Sandberg@ARM.com#     http://scons.tigris.org/issues/show_bug.cgi?id=2611
1209651SAndreas.Sandberg@ARM.com# This hack lets us extract the help text from AddOptions and
1219651SAndreas.Sandberg@ARM.com# re-inject it via Help().  Ideally someday this bug will be fixed and
1229651SAndreas.Sandberg@ARM.com# we can just use AddOption directly.
1239651SAndreas.Sandberg@ARM.comdef AddLocalOption(*args, **kwargs):
1249651SAndreas.Sandberg@ARM.com    col_width = 30
1259651SAndreas.Sandberg@ARM.com
1269690Sandreas@sandberg.pp.se    help = "  " + ", ".join(args)
1279690Sandreas@sandberg.pp.se    if "help" in kwargs:
1289690Sandreas@sandberg.pp.se        length = len(help)
12911363Sandreas@sandberg.pp.se        if length >= col_width:
1309651SAndreas.Sandberg@ARM.com            help += "\n" + " " * col_width
1319651SAndreas.Sandberg@ARM.com        else:
1329651SAndreas.Sandberg@ARM.com            help += " " * (col_width - length)
1339651SAndreas.Sandberg@ARM.com        help += kwargs["help"]
1349651SAndreas.Sandberg@ARM.com    help_texts["options"] += help + "\n"
1359651SAndreas.Sandberg@ARM.com
1369651SAndreas.Sandberg@ARM.com    AddOption(*args, **kwargs)
1379651SAndreas.Sandberg@ARM.com
1389651SAndreas.Sandberg@ARM.comAddLocalOption('--colors', dest='use_colors', action='store_true',
1399651SAndreas.Sandberg@ARM.com               help="Add color to abbreviated scons output")
1409651SAndreas.Sandberg@ARM.comAddLocalOption('--no-colors', dest='use_colors', action='store_false',
1419651SAndreas.Sandberg@ARM.com               help="Don't add color to abbreviated scons output")
1429651SAndreas.Sandberg@ARM.comAddLocalOption('--with-cxx-config', dest='with_cxx_config',
1439651SAndreas.Sandberg@ARM.com               action='store_true',
1449651SAndreas.Sandberg@ARM.com               help="Build with support for C++-based configuration")
1459651SAndreas.Sandberg@ARM.comAddLocalOption('--default', dest='default', type='string', action='store',
1469651SAndreas.Sandberg@ARM.com               help='Override which build_opts file to use for defaults')
1479651SAndreas.Sandberg@ARM.comAddLocalOption('--ignore-style', dest='ignore_style', action='store_true',
1489651SAndreas.Sandberg@ARM.com               help='Disable style checking hooks')
1499651SAndreas.Sandberg@ARM.comAddLocalOption('--gold-linker', dest='gold_linker', action='store_true',
1509651SAndreas.Sandberg@ARM.com               help='Use the gold linker')
1519651SAndreas.Sandberg@ARM.comAddLocalOption('--no-lto', dest='no_lto', action='store_true',
1529651SAndreas.Sandberg@ARM.com               help='Disable Link-Time Optimization for fast')
1539651SAndreas.Sandberg@ARM.comAddLocalOption('--force-lto', dest='force_lto', action='store_true',
1549651SAndreas.Sandberg@ARM.com               help='Use Link-Time Optimization instead of partial linking' +
1559690Sandreas@sandberg.pp.se                    ' when the compiler doesn\'t support using them together.')
1569690Sandreas@sandberg.pp.seAddLocalOption('--update-ref', dest='update_ref', action='store_true',
1579690Sandreas@sandberg.pp.se               help='Update test reference outputs')
1589651SAndreas.Sandberg@ARM.comAddLocalOption('--verbose', dest='verbose', action='store_true',
1599651SAndreas.Sandberg@ARM.com               help='Print full tool command lines')
1609651SAndreas.Sandberg@ARM.comAddLocalOption('--without-python', dest='without_python',
1619651SAndreas.Sandberg@ARM.com               action='store_true',
1629651SAndreas.Sandberg@ARM.com               help='Build without Python configuration support')
1639651SAndreas.Sandberg@ARM.comAddLocalOption('--without-tcmalloc', dest='without_tcmalloc',
1649732Sandreas@sandberg.pp.se               action='store_true',
1659732Sandreas@sandberg.pp.se               help='Disable linking against tcmalloc')
16610073Sandreas@sandberg.pp.seAddLocalOption('--with-ubsan', dest='with_ubsan', action='store_true',
16710073Sandreas@sandberg.pp.se               help='Build with Undefined Behavior Sanitizer if available')
16810073Sandreas@sandberg.pp.seAddLocalOption('--with-asan', dest='with_asan', action='store_true',
16910073Sandreas@sandberg.pp.se               help='Build with Address Sanitizer if available')
17010073Sandreas@sandberg.pp.se
17110073Sandreas@sandberg.pp.seif GetOption('no_lto') and GetOption('force_lto'):
17210073Sandreas@sandberg.pp.se    print('--no-lto and --force-lto are mutually exclusive')
17311629Smichael.lebeane@amd.com    Exit(1)
17411629Smichael.lebeane@amd.com
17511629Smichael.lebeane@amd.com########################################################################
17611629Smichael.lebeane@amd.com#
17711629Smichael.lebeane@amd.com# Set up the main build environment.
17811629Smichael.lebeane@amd.com#
17911629Smichael.lebeane@amd.com########################################################################
18011629Smichael.lebeane@amd.com
18111629Smichael.lebeane@amd.commain = Environment()
18211629Smichael.lebeane@amd.com
18311629Smichael.lebeane@amd.comfrom gem5_scons import Transform
18411629Smichael.lebeane@amd.comfrom gem5_scons.util import get_termcap
18511629Smichael.lebeane@amd.comtermcap = get_termcap()
18611629Smichael.lebeane@amd.com
18711629Smichael.lebeane@amd.commain_dict_keys = main.Dictionary().keys()
18811629Smichael.lebeane@amd.com
18911629Smichael.lebeane@amd.com# Check that we have a C/C++ compiler
19011629Smichael.lebeane@amd.comif not ('CC' in main_dict_keys and 'CXX' in main_dict_keys):
19111629Smichael.lebeane@amd.com    print("No C++ compiler installed (package g++ on Ubuntu and RedHat)")
19211629Smichael.lebeane@amd.com    Exit(1)
19311629Smichael.lebeane@amd.com
19411629Smichael.lebeane@amd.com###################################################
19511629Smichael.lebeane@amd.com#
19611629Smichael.lebeane@amd.com# Figure out which configurations to set up based on the path(s) of
19711629Smichael.lebeane@amd.com# the target(s).
19811629Smichael.lebeane@amd.com#
19911629Smichael.lebeane@amd.com###################################################
20011629Smichael.lebeane@amd.com
20111629Smichael.lebeane@amd.com# Find default configuration & binary.
20211629Smichael.lebeane@amd.comDefault(environ.get('M5_DEFAULT_BINARY', 'build/ALPHA/gem5.debug'))
20311629Smichael.lebeane@amd.com
20411629Smichael.lebeane@amd.com# helper function: find last occurrence of element in list
20511629Smichael.lebeane@amd.comdef rfind(l, elt, offs = -1):
20611629Smichael.lebeane@amd.com    for i in range(len(l)+offs, 0, -1):
20711629Smichael.lebeane@amd.com        if l[i] == elt:
20811629Smichael.lebeane@amd.com            return i
20911629Smichael.lebeane@amd.com    raise ValueError, "element not found"
21011629Smichael.lebeane@amd.com
21111629Smichael.lebeane@amd.com# Take a list of paths (or SCons Nodes) and return a list with all
21211629Smichael.lebeane@amd.com# paths made absolute and ~-expanded.  Paths will be interpreted
21311629Smichael.lebeane@amd.com# relative to the launch directory unless a different root is provided
21411629Smichael.lebeane@amd.comdef makePathListAbsolute(path_list, root=GetLaunchDir()):
21511629Smichael.lebeane@amd.com    return [abspath(joinpath(root, expanduser(str(p))))
21611629Smichael.lebeane@amd.com            for p in path_list]
21711629Smichael.lebeane@amd.com
21811629Smichael.lebeane@amd.com# Each target must have 'build' in the interior of the path; the
21911629Smichael.lebeane@amd.com# directory below this will determine the build parameters.  For
22011629Smichael.lebeane@amd.com# example, for target 'foo/bar/build/ALPHA_SE/arch/alpha/blah.do' we
22111629Smichael.lebeane@amd.com# recognize that ALPHA_SE specifies the configuration because it
22211629Smichael.lebeane@amd.com# follow 'build' in the build path.
22311629Smichael.lebeane@amd.com
22411629Smichael.lebeane@amd.com# The funky assignment to "[:]" is needed to replace the list contents
22511629Smichael.lebeane@amd.com# in place rather than reassign the symbol to a new list, which
22611629Smichael.lebeane@amd.com# doesn't work (obviously!).
22711629Smichael.lebeane@amd.comBUILD_TARGETS[:] = makePathListAbsolute(BUILD_TARGETS)
22811629Smichael.lebeane@amd.com
22911629Smichael.lebeane@amd.com# Generate a list of the unique build roots and configs that the
23011629Smichael.lebeane@amd.com# collected targets reference.
23111629Smichael.lebeane@amd.comvariant_paths = []
23211629Smichael.lebeane@amd.combuild_root = None
23311629Smichael.lebeane@amd.comfor t in BUILD_TARGETS:
23411629Smichael.lebeane@amd.com    path_dirs = t.split('/')
23511629Smichael.lebeane@amd.com    try:
23611629Smichael.lebeane@amd.com        build_top = rfind(path_dirs, 'build', -2)
23711629Smichael.lebeane@amd.com    except:
23811629Smichael.lebeane@amd.com        print("Error: no non-leaf 'build' dir found on target path", t)
23911629Smichael.lebeane@amd.com        Exit(1)
24011629Smichael.lebeane@amd.com    this_build_root = joinpath('/',*path_dirs[:build_top+1])
24111629Smichael.lebeane@amd.com    if not build_root:
24211629Smichael.lebeane@amd.com        build_root = this_build_root
24310073Sandreas@sandberg.pp.se    else:
24410073Sandreas@sandberg.pp.se        if this_build_root != build_root:
24510073Sandreas@sandberg.pp.se            print("Error: build targets not under same build root\n"
24610073Sandreas@sandberg.pp.se                  "  %s\n  %s" % (build_root, this_build_root))
24710073Sandreas@sandberg.pp.se            Exit(1)
24810073Sandreas@sandberg.pp.se    variant_path = joinpath('/',*path_dirs[:build_top+2])
24910073Sandreas@sandberg.pp.se    if variant_path not in variant_paths:
25010073Sandreas@sandberg.pp.se        variant_paths.append(variant_path)
25110073Sandreas@sandberg.pp.se
25210073Sandreas@sandberg.pp.se# Make sure build_root exists (might not if this is the first build there)
25310114Sandreas@sandberg.pp.seif not isdir(build_root):
25410114Sandreas@sandberg.pp.se    mkdir(build_root)
25510098Sandreas@sandberg.pp.semain['BUILDROOT'] = build_root
25610098Sandreas@sandberg.pp.se
25710098Sandreas@sandberg.pp.seExport('main')
25810098Sandreas@sandberg.pp.se
25910073Sandreas@sandberg.pp.semain.SConsignFile(joinpath(build_root, "sconsign"))
26010073Sandreas@sandberg.pp.se
26110073Sandreas@sandberg.pp.se# Default duplicate option is to use hard links, but this messes up
26210073Sandreas@sandberg.pp.se# when you use emacs to edit a file in the target dir, as emacs moves
26310114Sandreas@sandberg.pp.se# file to file~ then copies to file, breaking the link.  Symbolic
26410073Sandreas@sandberg.pp.se# (soft) links work better.
26510073Sandreas@sandberg.pp.semain.SetOption('duplicate', 'soft-copy')
26610073Sandreas@sandberg.pp.se
26710114Sandreas@sandberg.pp.se#
26810073Sandreas@sandberg.pp.se# Set up global sticky variables... these are common to an entire build
26910073Sandreas@sandberg.pp.se# tree (not specific to a particular build like ALPHA_SE)
27010073Sandreas@sandberg.pp.se#
2719651SAndreas.Sandberg@ARM.com
2729651SAndreas.Sandberg@ARM.comglobal_vars_file = joinpath(build_root, 'variables.global')
2739651SAndreas.Sandberg@ARM.com
2749651SAndreas.Sandberg@ARM.comglobal_vars = Variables(global_vars_file, args=ARGUMENTS)
2759651SAndreas.Sandberg@ARM.com
2769651SAndreas.Sandberg@ARM.comglobal_vars.AddVariables(
2779651SAndreas.Sandberg@ARM.com    ('CC', 'C compiler', environ.get('CC', main['CC'])),
2789651SAndreas.Sandberg@ARM.com    ('CXX', 'C++ compiler', environ.get('CXX', main['CXX'])),
2799651SAndreas.Sandberg@ARM.com    ('PROTOC', 'protoc tool', environ.get('PROTOC', 'protoc')),
2809684Sandreas@sandberg.pp.se    ('BATCH', 'Use batch pool for build and tests', False),
2819684Sandreas@sandberg.pp.se    ('BATCH_CMD', 'Batch pool submission command name', 'qdo'),
2829684Sandreas@sandberg.pp.se    ('M5_BUILD_CACHE', 'Cache built objects in this directory', False),
2839684Sandreas@sandberg.pp.se    ('EXTRAS', 'Add extra directories to the compilation', '')
2849684Sandreas@sandberg.pp.se    )
2859651SAndreas.Sandberg@ARM.com
2869651SAndreas.Sandberg@ARM.com# Update main environment with values from ARGUMENTS & global_vars_file
2879651SAndreas.Sandberg@ARM.comglobal_vars.Update(main)
2889651SAndreas.Sandberg@ARM.comhelp_texts["global_vars"] += global_vars.GenerateHelpText(main)
2899651SAndreas.Sandberg@ARM.com
2909755Sandreas@sandberg.pp.se# Save sticky variable settings back to current variables file
2919755Sandreas@sandberg.pp.seglobal_vars.Save(global_vars_file, main)
2929755Sandreas@sandberg.pp.se
2939755Sandreas@sandberg.pp.se# Parse EXTRAS variable to build list of all directories where we're
2949755Sandreas@sandberg.pp.se# look for sources etc.  This list is exported as extras_dir_list.
2959755Sandreas@sandberg.pp.sebase_dir = main.srcdir.abspath
2969755Sandreas@sandberg.pp.seif main['EXTRAS']:
2979755Sandreas@sandberg.pp.se    extras_dir_list = makePathListAbsolute(main['EXTRAS'].split(':'))
2989755Sandreas@sandberg.pp.seelse:
2999755Sandreas@sandberg.pp.se    extras_dir_list = []
3009651SAndreas.Sandberg@ARM.com
3019651SAndreas.Sandberg@ARM.comExport('base_dir')
3029651SAndreas.Sandberg@ARM.comExport('extras_dir_list')
3039651SAndreas.Sandberg@ARM.com
3049651SAndreas.Sandberg@ARM.com# the ext directory should be on the #includes path
3059651SAndreas.Sandberg@ARM.commain.Append(CPPPATH=[Dir('ext')])
3069651SAndreas.Sandberg@ARM.com
3079651SAndreas.Sandberg@ARM.com# Add shared top-level headers
3089651SAndreas.Sandberg@ARM.commain.Prepend(CPPPATH=Dir('include'))
3099651SAndreas.Sandberg@ARM.com
3109651SAndreas.Sandberg@ARM.comif GetOption('verbose'):
3119651SAndreas.Sandberg@ARM.com    def MakeAction(action, string, *args, **kwargs):
3129651SAndreas.Sandberg@ARM.com        return Action(action, *args, **kwargs)
3139651SAndreas.Sandberg@ARM.comelse:
3149651SAndreas.Sandberg@ARM.com    MakeAction = Action
3159651SAndreas.Sandberg@ARM.com    main['CCCOMSTR']        = Transform("CC")
3169651SAndreas.Sandberg@ARM.com    main['CXXCOMSTR']       = Transform("CXX")
3179651SAndreas.Sandberg@ARM.com    main['ASCOMSTR']        = Transform("AS")
3189651SAndreas.Sandberg@ARM.com    main['ARCOMSTR']        = Transform("AR", 0)
3199651SAndreas.Sandberg@ARM.com    main['LINKCOMSTR']      = Transform("LINK", 0)
3209651SAndreas.Sandberg@ARM.com    main['SHLINKCOMSTR']    = Transform("SHLINK", 0)
3219651SAndreas.Sandberg@ARM.com    main['RANLIBCOMSTR']    = Transform("RANLIB", 0)
3229651SAndreas.Sandberg@ARM.com    main['M4COMSTR']        = Transform("M4")
3239651SAndreas.Sandberg@ARM.com    main['SHCCCOMSTR']      = Transform("SHCC")
3249651SAndreas.Sandberg@ARM.com    main['SHCXXCOMSTR']     = Transform("SHCXX")
3259651SAndreas.Sandberg@ARM.comExport('MakeAction')
3269651SAndreas.Sandberg@ARM.com
3279651SAndreas.Sandberg@ARM.com# Initialize the Link-Time Optimization (LTO) flags
3289651SAndreas.Sandberg@ARM.commain['LTO_CCFLAGS'] = []
3299651SAndreas.Sandberg@ARM.commain['LTO_LDFLAGS'] = []
3309651SAndreas.Sandberg@ARM.com
3319651SAndreas.Sandberg@ARM.com# According to the readme, tcmalloc works best if the compiler doesn't
33210905Sandreas.sandberg@arm.com# assume that we're using the builtin malloc and friends. These flags
3339651SAndreas.Sandberg@ARM.com# are compiler-specific, so we need to set them after we detect which
3349683Sandreas@sandberg.pp.se# compiler we're using.
3359683Sandreas@sandberg.pp.semain['TCMALLOC_CCFLAGS'] = []
3369683Sandreas@sandberg.pp.se
3379683Sandreas@sandberg.pp.seCXX_version = readCommand([main['CXX'],'--version'], exception=False)
3389683Sandreas@sandberg.pp.seCXX_V = readCommand([main['CXX'],'-V'], exception=False)
3399651SAndreas.Sandberg@ARM.com
3409651SAndreas.Sandberg@ARM.commain['GCC'] = CXX_version and CXX_version.find('g++') >= 0
34110905Sandreas.sandberg@arm.commain['CLANG'] = CXX_version and CXX_version.find('clang') >= 0
3429651SAndreas.Sandberg@ARM.comif main['GCC'] + main['CLANG'] > 1:
3439651SAndreas.Sandberg@ARM.com    print('Error: How can we have two at the same time?')
3449651SAndreas.Sandberg@ARM.com    Exit(1)
34510905Sandreas.sandberg@arm.com
3469651SAndreas.Sandberg@ARM.com# Set up default C++ compiler flags
3479683Sandreas@sandberg.pp.seif main['GCC'] or main['CLANG']:
3489683Sandreas@sandberg.pp.se    # As gcc and clang share many flags, do the common parts here
3499651SAndreas.Sandberg@ARM.com    main.Append(CCFLAGS=['-pipe'])
3509651SAndreas.Sandberg@ARM.com    main.Append(CCFLAGS=['-fno-strict-aliasing'])
35110905Sandreas.sandberg@arm.com    # Enable -Wall and -Wextra and then disable the few warnings that
3529652SAndreas.Sandberg@ARM.com    # we consistently violate
3539651SAndreas.Sandberg@ARM.com    main.Append(CCFLAGS=['-Wall', '-Wundef', '-Wextra',
3549651SAndreas.Sandberg@ARM.com                         '-Wno-sign-compare', '-Wno-unused-parameter'])
35510913Sandreas.sandberg@arm.com    # We always compile using C++11
35610913Sandreas.sandberg@arm.com    main.Append(CXXFLAGS=['-std=c++11'])
3579651SAndreas.Sandberg@ARM.com    if sys.platform.startswith('freebsd'):
3589651SAndreas.Sandberg@ARM.com        main.Append(CCFLAGS=['-I/usr/local/include'])
35910913Sandreas.sandberg@arm.com        main.Append(CXXFLAGS=['-I/usr/local/include'])
3609651SAndreas.Sandberg@ARM.com
3619753Sandreas@sandberg.pp.se    main['FILTER_PSHLINKFLAGS'] = lambda x: str(x).replace(' -shared', '')
3629753Sandreas@sandberg.pp.se    main['PSHLINKFLAGS'] = main.subst('${FILTER_PSHLINKFLAGS(SHLINKFLAGS)}')
3639753Sandreas@sandberg.pp.se    if GetOption('gold_linker'):
3649753Sandreas@sandberg.pp.se        main.Append(LINKFLAGS='-fuse-ld=gold')
3659753Sandreas@sandberg.pp.se    main['PLINKFLAGS'] = main.subst('${LINKFLAGS}')
3669753Sandreas@sandberg.pp.se    shared_partial_flags = ['-r', '-nostdlib']
3679753Sandreas@sandberg.pp.se    main.Append(PSHLINKFLAGS=shared_partial_flags)
3689753Sandreas@sandberg.pp.se    main.Append(PLINKFLAGS=shared_partial_flags)
36910913Sandreas.sandberg@arm.com
37010913Sandreas.sandberg@arm.com    # Treat warnings as errors but white list some warnings that we
3719651SAndreas.Sandberg@ARM.com    # want to allow (e.g., deprecation warnings).
3729753Sandreas@sandberg.pp.se    main.Append(CCFLAGS=['-Werror',
3739753Sandreas@sandberg.pp.se                         '-Wno-error=deprecated-declarations',
3749753Sandreas@sandberg.pp.se                         '-Wno-error=deprecated',
3759753Sandreas@sandberg.pp.se                        ])
3769753Sandreas@sandberg.pp.seelse:
3779753Sandreas@sandberg.pp.se    print(termcap.Yellow + termcap.Bold + 'Error' + termcap.Normal, end=' ')
3789753Sandreas@sandberg.pp.se    print("Don't know what compiler options to use for your compiler.")
3799651SAndreas.Sandberg@ARM.com    print(termcap.Yellow + '       compiler:' + termcap.Normal, main['CXX'])
3809753Sandreas@sandberg.pp.se    print(termcap.Yellow + '       version:' + termcap.Normal, end = ' ')
3819753Sandreas@sandberg.pp.se    if not CXX_version:
3829753Sandreas@sandberg.pp.se        print(termcap.Yellow + termcap.Bold + "COMMAND NOT FOUND!" +
3839753Sandreas@sandberg.pp.se              termcap.Normal)
3849753Sandreas@sandberg.pp.se    else:
3859753Sandreas@sandberg.pp.se        print(CXX_version.replace('\n', '<nl>'))
3869753Sandreas@sandberg.pp.se    print("       If you're trying to use a compiler other than GCC")
3879753Sandreas@sandberg.pp.se    print("       or clang, there appears to be something wrong with your")
3889753Sandreas@sandberg.pp.se    print("       environment.")
38910913Sandreas.sandberg@arm.com    print("       ")
3909753Sandreas@sandberg.pp.se    print("       If you are trying to use a compiler other than those listed")
3919753Sandreas@sandberg.pp.se    print("       above you will need to ease fix SConstruct and ")
3929753Sandreas@sandberg.pp.se    print("       src/SConscript to support that compiler.")
3939753Sandreas@sandberg.pp.se    Exit(1)
3949753Sandreas@sandberg.pp.se
3959753Sandreas@sandberg.pp.seif main['GCC']:
3969753Sandreas@sandberg.pp.se    # Check for a supported version of gcc. >= 4.8 is chosen for its
3979753Sandreas@sandberg.pp.se    # level of c++11 support. See
3989753Sandreas@sandberg.pp.se    # http://gcc.gnu.org/projects/cxx0x.html for details.
3999753Sandreas@sandberg.pp.se    gcc_version = readCommand([main['CXX'], '-dumpversion'], exception=False)
40010913Sandreas.sandberg@arm.com    if compareVersions(gcc_version, "4.8") < 0:
4019753Sandreas@sandberg.pp.se        print('Error: gcc version 4.8 or newer required.')
40211629Smichael.lebeane@amd.com        print('       Installed version: ', gcc_version)
40311629Smichael.lebeane@amd.com        Exit(1)
40411629Smichael.lebeane@amd.com
40511629Smichael.lebeane@amd.com    main['GCC_VERSION'] = gcc_version
40611629Smichael.lebeane@amd.com
40711629Smichael.lebeane@amd.com    if compareVersions(gcc_version, '4.9') >= 0:
4089753Sandreas@sandberg.pp.se        # Incremental linking with LTO is currently broken in gcc versions
4099753Sandreas@sandberg.pp.se        # 4.9 and above. A version where everything works completely hasn't
4109753Sandreas@sandberg.pp.se        # yet been identified.
41110913Sandreas.sandberg@arm.com        #
4129753Sandreas@sandberg.pp.se        # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67548
4139753Sandreas@sandberg.pp.se        main['BROKEN_INCREMENTAL_LTO'] = True
4149753Sandreas@sandberg.pp.se    if compareVersions(gcc_version, '6.0') >= 0:
41510913Sandreas.sandberg@arm.com        # gcc versions 6.0 and greater accept an -flinker-output flag which
4169753Sandreas@sandberg.pp.se        # selects what type of output the linker should generate. This is
4179651SAndreas.Sandberg@ARM.com        # necessary for incremental lto to work, but is also broken in
4189651SAndreas.Sandberg@ARM.com        # current versions of gcc. It may not be necessary in future
4199651SAndreas.Sandberg@ARM.com        # versions. We add it here since it might be, and as a reminder that
4209651SAndreas.Sandberg@ARM.com        # it exists. It's excluded if lto is being forced.
4219651SAndreas.Sandberg@ARM.com        #
4229651SAndreas.Sandberg@ARM.com        # https://gcc.gnu.org/gcc-6/changes.html
4239651SAndreas.Sandberg@ARM.com        # https://gcc.gnu.org/ml/gcc-patches/2015-11/msg03161.html
4249651SAndreas.Sandberg@ARM.com        # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69866
4259651SAndreas.Sandberg@ARM.com        if not GetOption('force_lto'):
4269651SAndreas.Sandberg@ARM.com            main.Append(PSHLINKFLAGS='-flinker-output=rel')
4279651SAndreas.Sandberg@ARM.com            main.Append(PLINKFLAGS='-flinker-output=rel')
4289651SAndreas.Sandberg@ARM.com
4299651SAndreas.Sandberg@ARM.com    # Make sure we warn if the user has requested to compile with the
4309651SAndreas.Sandberg@ARM.com    # Undefined Benahvior Sanitizer and this version of gcc does not
4319651SAndreas.Sandberg@ARM.com    # support it.
4329651SAndreas.Sandberg@ARM.com    if GetOption('with_ubsan') and \
4339651SAndreas.Sandberg@ARM.com            compareVersions(gcc_version, '4.9') < 0:
4349651SAndreas.Sandberg@ARM.com        print(termcap.Yellow + termcap.Bold +
4359651SAndreas.Sandberg@ARM.com            'Warning: UBSan is only supported using gcc 4.9 and later.' +
4369651SAndreas.Sandberg@ARM.com            termcap.Normal)
4379651SAndreas.Sandberg@ARM.com
4389651SAndreas.Sandberg@ARM.com    disable_lto = GetOption('no_lto')
4399651SAndreas.Sandberg@ARM.com    if not disable_lto and main.get('BROKEN_INCREMENTAL_LTO', False) and \
4409651SAndreas.Sandberg@ARM.com            not GetOption('force_lto'):
4419651SAndreas.Sandberg@ARM.com        print(termcap.Yellow + termcap.Bold +
4429651SAndreas.Sandberg@ARM.com            'Warning: Your compiler doesn\'t support incremental linking' +
44311363Sandreas@sandberg.pp.se            ' and lto at the same time, so lto is being disabled. To force' +
44411363Sandreas@sandberg.pp.se            ' lto on anyway, use the --force-lto option. That will disable' +
44511363Sandreas@sandberg.pp.se            ' partial linking.' +
44611363Sandreas@sandberg.pp.se            termcap.Normal)
44711363Sandreas@sandberg.pp.se        disable_lto = True
44811363Sandreas@sandberg.pp.se
44911363Sandreas@sandberg.pp.se    # Add the appropriate Link-Time Optimization (LTO) flags
45011363Sandreas@sandberg.pp.se    # unless LTO is explicitly turned off. Note that these flags
45111363Sandreas@sandberg.pp.se    # are only used by the fast target.
45211363Sandreas@sandberg.pp.se    if not disable_lto:
45311363Sandreas@sandberg.pp.se        # Pass the LTO flag when compiling to produce GIMPLE
45411363Sandreas@sandberg.pp.se        # output, we merely create the flags here and only append
45511363Sandreas@sandberg.pp.se        # them later
45611363Sandreas@sandberg.pp.se        main['LTO_CCFLAGS'] = ['-flto=%d' % GetOption('num_jobs')]
45711363Sandreas@sandberg.pp.se
45811363Sandreas@sandberg.pp.se        # Use the same amount of jobs for LTO as we are running
45911363Sandreas@sandberg.pp.se        # scons with
46011363Sandreas@sandberg.pp.se        main['LTO_LDFLAGS'] = ['-flto=%d' % GetOption('num_jobs')]
46111363Sandreas@sandberg.pp.se
46211363Sandreas@sandberg.pp.se    main.Append(TCMALLOC_CCFLAGS=['-fno-builtin-malloc', '-fno-builtin-calloc',
46311363Sandreas@sandberg.pp.se                                  '-fno-builtin-realloc', '-fno-builtin-free'])
46411363Sandreas@sandberg.pp.se
46511363Sandreas@sandberg.pp.se    # The address sanitizer is available for gcc >= 4.8
4669651SAndreas.Sandberg@ARM.com    if GetOption('with_asan'):
4679651SAndreas.Sandberg@ARM.com        if GetOption('with_ubsan') and \
4689652SAndreas.Sandberg@ARM.com                compareVersions(main['GCC_VERSION'], '4.9') >= 0:
4699652SAndreas.Sandberg@ARM.com            main.Append(CCFLAGS=['-fsanitize=address,undefined',
4709651SAndreas.Sandberg@ARM.com                                 '-fno-omit-frame-pointer'],
4719651SAndreas.Sandberg@ARM.com                        LINKFLAGS='-fsanitize=address,undefined')
4729651SAndreas.Sandberg@ARM.com        else:
4739651SAndreas.Sandberg@ARM.com            main.Append(CCFLAGS=['-fsanitize=address',
4749651SAndreas.Sandberg@ARM.com                                 '-fno-omit-frame-pointer'],
4759651SAndreas.Sandberg@ARM.com                        LINKFLAGS='-fsanitize=address')
4769651SAndreas.Sandberg@ARM.com    # Only gcc >= 4.9 supports UBSan, so check both the version
4779651SAndreas.Sandberg@ARM.com    # and the command-line option before adding the compiler and
4789651SAndreas.Sandberg@ARM.com    # linker flags.
4799651SAndreas.Sandberg@ARM.com    elif GetOption('with_ubsan') and \
4809651SAndreas.Sandberg@ARM.com            compareVersions(main['GCC_VERSION'], '4.9') >= 0:
4819651SAndreas.Sandberg@ARM.com        main.Append(CCFLAGS='-fsanitize=undefined')
4829651SAndreas.Sandberg@ARM.com        main.Append(LINKFLAGS='-fsanitize=undefined')
4839651SAndreas.Sandberg@ARM.com
4849651SAndreas.Sandberg@ARM.comelif main['CLANG']:
4859651SAndreas.Sandberg@ARM.com    # Check for a supported version of clang, >= 3.1 is needed to
4869651SAndreas.Sandberg@ARM.com    # support similar features as gcc 4.8. See
4879651SAndreas.Sandberg@ARM.com    # http://clang.llvm.org/cxx_status.html for details
4889651SAndreas.Sandberg@ARM.com    clang_version_re = re.compile(".* version (\d+\.\d+)")
4899651SAndreas.Sandberg@ARM.com    clang_version_match = clang_version_re.search(CXX_version)
4909651SAndreas.Sandberg@ARM.com    if (clang_version_match):
4919651SAndreas.Sandberg@ARM.com        clang_version = clang_version_match.groups()[0]
4929651SAndreas.Sandberg@ARM.com        if compareVersions(clang_version, "3.1") < 0:
4939753Sandreas@sandberg.pp.se            print('Error: clang version 3.1 or newer required.')
4949753Sandreas@sandberg.pp.se            print('       Installed version:', clang_version)
4959753Sandreas@sandberg.pp.se            Exit(1)
4969753Sandreas@sandberg.pp.se    else:
4979753Sandreas@sandberg.pp.se        print('Error: Unable to determine clang version.')
4989753Sandreas@sandberg.pp.se        Exit(1)
4999651SAndreas.Sandberg@ARM.com
5009651SAndreas.Sandberg@ARM.com    # clang has a few additional warnings that we disable, extraneous
5019651SAndreas.Sandberg@ARM.com    # parantheses are allowed due to Ruby's printing of the AST,
5029651SAndreas.Sandberg@ARM.com    # finally self assignments are allowed as the generated CPU code
5039651SAndreas.Sandberg@ARM.com    # is relying on this
50411629Smichael.lebeane@amd.com    main.Append(CCFLAGS=['-Wno-parentheses',
5059651SAndreas.Sandberg@ARM.com                         '-Wno-self-assign',
50611629Smichael.lebeane@amd.com                         # Some versions of libstdc++ (4.8?) seem to
5079651SAndreas.Sandberg@ARM.com                         # use struct hash and class hash
5089651SAndreas.Sandberg@ARM.com                         # interchangeably.
5099651SAndreas.Sandberg@ARM.com                         '-Wno-mismatched-tags',
5109651SAndreas.Sandberg@ARM.com                         ])
51111151Smitch.hayenga@arm.com
5129651SAndreas.Sandberg@ARM.com    main.Append(TCMALLOC_CCFLAGS=['-fno-builtin'])
5139651SAndreas.Sandberg@ARM.com
51410157Sandreas@sandberg.pp.se    # On Mac OS X/Darwin we need to also use libc++ (part of XCode) as
51510157Sandreas@sandberg.pp.se    # opposed to libstdc++, as the later is dated.
51610157Sandreas@sandberg.pp.se    if sys.platform == "darwin":
51710157Sandreas@sandberg.pp.se        main.Append(CXXFLAGS=['-stdlib=libc++'])
51810157Sandreas@sandberg.pp.se        main.Append(LIBS=['c++'])
51910157Sandreas@sandberg.pp.se
52010157Sandreas@sandberg.pp.se    # On FreeBSD we need libthr.
5219651SAndreas.Sandberg@ARM.com    if sys.platform.startswith('freebsd'):
5229651SAndreas.Sandberg@ARM.com        main.Append(LIBS=['thr'])
5239651SAndreas.Sandberg@ARM.com
5249651SAndreas.Sandberg@ARM.com    # We require clang >= 3.1, so there is no need to check any
5259651SAndreas.Sandberg@ARM.com    # versions here.
5269651SAndreas.Sandberg@ARM.com    if GetOption('with_ubsan'):
5279651SAndreas.Sandberg@ARM.com        if GetOption('with_asan'):
5289651SAndreas.Sandberg@ARM.com            main.Append(CCFLAGS=['-fsanitize=address,undefined',
52910407Smitch.hayenga@arm.com                                 '-fno-omit-frame-pointer'],
5309651SAndreas.Sandberg@ARM.com                       LINKFLAGS='-fsanitize=address,undefined')
53110407Smitch.hayenga@arm.com        else:
5329651SAndreas.Sandberg@ARM.com            main.Append(CCFLAGS='-fsanitize=undefined',
5339651SAndreas.Sandberg@ARM.com                        LINKFLAGS='-fsanitize=undefined')
5349651SAndreas.Sandberg@ARM.com
5359651SAndreas.Sandberg@ARM.com    elif GetOption('with_asan'):
5369651SAndreas.Sandberg@ARM.com        main.Append(CCFLAGS=['-fsanitize=address',
5379651SAndreas.Sandberg@ARM.com                             '-fno-omit-frame-pointer'],
5389651SAndreas.Sandberg@ARM.com                   LINKFLAGS='-fsanitize=address')
5399754Sandreas@sandberg.pp.se
5409651SAndreas.Sandberg@ARM.comelse:
54110407Smitch.hayenga@arm.com    print(termcap.Yellow + termcap.Bold + 'Error' + termcap.Normal, end=' ')
5429651SAndreas.Sandberg@ARM.com    print("Don't know what compiler options to use for your compiler.")
5439651SAndreas.Sandberg@ARM.com    print(termcap.Yellow + '       compiler:' + termcap.Normal, main['CXX'])
5449651SAndreas.Sandberg@ARM.com    print(termcap.Yellow + '       version:' + termcap.Normal, end=' ')
5459651SAndreas.Sandberg@ARM.com    if not CXX_version:
5469651SAndreas.Sandberg@ARM.com        print(termcap.Yellow + termcap.Bold + "COMMAND NOT FOUND!" +
5479651SAndreas.Sandberg@ARM.com              termcap.Normal)
5489651SAndreas.Sandberg@ARM.com    else:
5499651SAndreas.Sandberg@ARM.com        print(CXX_version.replace('\n', '<nl>'))
5509651SAndreas.Sandberg@ARM.com    print("       If you're trying to use a compiler other than GCC")
5519651SAndreas.Sandberg@ARM.com    print("       or clang, there appears to be something wrong with your")
5529651SAndreas.Sandberg@ARM.com    print("       environment.")
5539651SAndreas.Sandberg@ARM.com    print("       ")
5549651SAndreas.Sandberg@ARM.com    print("       If you are trying to use a compiler other than those listed")
5559651SAndreas.Sandberg@ARM.com    print("       above you will need to ease fix SConstruct and ")
5569651SAndreas.Sandberg@ARM.com    print("       src/SConscript to support that compiler.")
55710553Salexandru.dutu@amd.com    Exit(1)
5589651SAndreas.Sandberg@ARM.com
5599651SAndreas.Sandberg@ARM.com# Set up common yacc/bison flags (needed for Ruby)
5609651SAndreas.Sandberg@ARM.commain['YACCFLAGS'] = '-d'
5619651SAndreas.Sandberg@ARM.commain['YACCHXXFILESUFFIX'] = '.hh'
5629651SAndreas.Sandberg@ARM.com
5639651SAndreas.Sandberg@ARM.com# Do this after we save setting back, or else we'll tack on an
5649651SAndreas.Sandberg@ARM.com# extra 'qdo' every time we run scons.
5659651SAndreas.Sandberg@ARM.comif main['BATCH']:
5669651SAndreas.Sandberg@ARM.com    main['CC']     = main['BATCH_CMD'] + ' ' + main['CC']
5679651SAndreas.Sandberg@ARM.com    main['CXX']    = main['BATCH_CMD'] + ' ' + main['CXX']
5689651SAndreas.Sandberg@ARM.com    main['AS']     = main['BATCH_CMD'] + ' ' + main['AS']
5699651SAndreas.Sandberg@ARM.com    main['AR']     = main['BATCH_CMD'] + ' ' + main['AR']
5709651SAndreas.Sandberg@ARM.com    main['RANLIB'] = main['BATCH_CMD'] + ' ' + main['RANLIB']
5719651SAndreas.Sandberg@ARM.com
5729651SAndreas.Sandberg@ARM.comif sys.platform == 'cygwin':
5739651SAndreas.Sandberg@ARM.com    # cygwin has some header file issues...
5749651SAndreas.Sandberg@ARM.com    main.Append(CCFLAGS=["-Wno-uninitialized"])
5759651SAndreas.Sandberg@ARM.com
5769651SAndreas.Sandberg@ARM.com# Check for the protobuf compiler
5779651SAndreas.Sandberg@ARM.comprotoc_version = readCommand([main['PROTOC'], '--version'],
5789651SAndreas.Sandberg@ARM.com                             exception='').split()
5799651SAndreas.Sandberg@ARM.com
5809651SAndreas.Sandberg@ARM.com# First two words should be "libprotoc x.y.z"
5819651SAndreas.Sandberg@ARM.comif len(protoc_version) < 2 or protoc_version[0] != 'libprotoc':
5829651SAndreas.Sandberg@ARM.com    print(termcap.Yellow + termcap.Bold +
5839652SAndreas.Sandberg@ARM.com        'Warning: Protocol buffer compiler (protoc) not found.\n' +
5849652SAndreas.Sandberg@ARM.com        '         Please install protobuf-compiler for tracing support.' +
5859652SAndreas.Sandberg@ARM.com        termcap.Normal)
5869652SAndreas.Sandberg@ARM.com    main['PROTOC'] = False
5879652SAndreas.Sandberg@ARM.comelse:
5889652SAndreas.Sandberg@ARM.com    # Based on the availability of the compress stream wrappers,
5899652SAndreas.Sandberg@ARM.com    # require 2.1.0
5909652SAndreas.Sandberg@ARM.com    min_protoc_version = '2.1.0'
5919652SAndreas.Sandberg@ARM.com    if compareVersions(protoc_version[1], min_protoc_version) < 0:
5929651SAndreas.Sandberg@ARM.com        print(termcap.Yellow + termcap.Bold +
5939651SAndreas.Sandberg@ARM.com            'Warning: protoc version', min_protoc_version,
5949651SAndreas.Sandberg@ARM.com            'or newer required.\n' +
5959752Sandreas@sandberg.pp.se            '         Installed version:', protoc_version[1],
5969651SAndreas.Sandberg@ARM.com            termcap.Normal)
5979651SAndreas.Sandberg@ARM.com        main['PROTOC'] = False
5989651SAndreas.Sandberg@ARM.com    else:
5999651SAndreas.Sandberg@ARM.com        # Attempt to determine the appropriate include path and
6009651SAndreas.Sandberg@ARM.com        # library path using pkg-config, that means we also need to
6019651SAndreas.Sandberg@ARM.com        # check for pkg-config. Note that it is possible to use
6029752Sandreas@sandberg.pp.se        # protobuf without the involvement of pkg-config. Later on we
6039651SAndreas.Sandberg@ARM.com        # check go a library config check and at that point the test
6049651SAndreas.Sandberg@ARM.com        # will fail if libprotobuf cannot be found.
6059651SAndreas.Sandberg@ARM.com        if readCommand(['pkg-config', '--version'], exception=''):
60610905Sandreas.sandberg@arm.com            try:
6079651SAndreas.Sandberg@ARM.com                # Attempt to establish what linking flags to add for protobuf
6089651SAndreas.Sandberg@ARM.com                # using pkg-config
6099651SAndreas.Sandberg@ARM.com                main.ParseConfig('pkg-config --cflags --libs-only-L protobuf')
6109651SAndreas.Sandberg@ARM.com            except:
6119651SAndreas.Sandberg@ARM.com                print(termcap.Yellow + termcap.Bold +
6129651SAndreas.Sandberg@ARM.com                    'Warning: pkg-config could not get protobuf flags.' +
6139651SAndreas.Sandberg@ARM.com                    termcap.Normal)
6149753Sandreas@sandberg.pp.se
61511629Smichael.lebeane@amd.com
6169651SAndreas.Sandberg@ARM.com# Check for 'timeout' from GNU coreutils. If present, regressions will
6179651SAndreas.Sandberg@ARM.com# be run with a time limit. We require version 8.13 since we rely on
6189753Sandreas@sandberg.pp.se# support for the '--foreground' option.
6199753Sandreas@sandberg.pp.seif sys.platform.startswith('freebsd'):
6209753Sandreas@sandberg.pp.se    timeout_lines = readCommand(['gtimeout', '--version'],
6219753Sandreas@sandberg.pp.se                                exception='').splitlines()
6229753Sandreas@sandberg.pp.seelse:
6239753Sandreas@sandberg.pp.se    timeout_lines = readCommand(['timeout', '--version'],
6249651SAndreas.Sandberg@ARM.com                                exception='').splitlines()
6259651SAndreas.Sandberg@ARM.com# Get the first line and tokenize it
6269753Sandreas@sandberg.pp.setimeout_version = timeout_lines[0].split() if timeout_lines else []
6279753Sandreas@sandberg.pp.semain['TIMEOUT'] =  timeout_version and \
62810858Sandreas.sandberg@arm.com    compareVersions(timeout_version[-1], '8.13') >= 0
62910858Sandreas.sandberg@arm.com
63010858Sandreas.sandberg@arm.com# Add a custom Check function to test for structure members.
63110858Sandreas.sandberg@arm.comdef CheckMember(context, include, decl, member, include_quotes="<>"):
63210858Sandreas.sandberg@arm.com    context.Message("Checking for member %s in %s..." %
63310858Sandreas.sandberg@arm.com                    (member, decl))
63410858Sandreas.sandberg@arm.com    text = """
63510858Sandreas.sandberg@arm.com#include %(header)s
6369753Sandreas@sandberg.pp.seint main(){
63711399Sandreas.sandberg@arm.com  %(decl)s test;
63811399Sandreas.sandberg@arm.com  (void)test.%(member)s;
63911399Sandreas.sandberg@arm.com  return 0;
6409753Sandreas@sandberg.pp.se};
6419753Sandreas@sandberg.pp.se""" % { "header" : include_quotes[0] + include + include_quotes[1],
6429753Sandreas@sandberg.pp.se        "decl" : decl,
6439892Sandreas@sandberg.pp.se        "member" : member,
64410858Sandreas.sandberg@arm.com        }
64510858Sandreas.sandberg@arm.com
64610858Sandreas.sandberg@arm.com    ret = context.TryCompile(text, extension=".cc")
64710858Sandreas.sandberg@arm.com    context.Result(ret)
6489892Sandreas@sandberg.pp.se    return ret
6499753Sandreas@sandberg.pp.se
65010913Sandreas.sandberg@arm.com# Platform-specific configuration.  Note again that we assume that all
6519753Sandreas@sandberg.pp.se# builds under a given build root run on the same host platform.
6529753Sandreas@sandberg.pp.seconf = Configure(main,
6539753Sandreas@sandberg.pp.se                 conf_dir = joinpath(build_root, '.scons_config'),
6549753Sandreas@sandberg.pp.se                 log_file = joinpath(build_root, 'scons_config.log'),
6559753Sandreas@sandberg.pp.se                 custom_tests = {
6569753Sandreas@sandberg.pp.se        'CheckMember' : CheckMember,
6579753Sandreas@sandberg.pp.se        })
6589753Sandreas@sandberg.pp.se
6599753Sandreas@sandberg.pp.se# Check if we should compile a 64 bit binary on Mac OS X/Darwin
66010112Sandreas@sandberg.pp.setry:
66110112Sandreas@sandberg.pp.se    import platform
66210112Sandreas@sandberg.pp.se    uname = platform.uname()
66310112Sandreas@sandberg.pp.se    if uname[0] == 'Darwin' and compareVersions(uname[2], '9.0.0') >= 0:
66410112Sandreas@sandberg.pp.se        if int(readCommand('sysctl -n hw.cpu64bit_capable')[0]):
66510112Sandreas@sandberg.pp.se            main.Append(CCFLAGS=['-arch', 'x86_64'])
6669753Sandreas@sandberg.pp.se            main.Append(CFLAGS=['-arch', 'x86_64'])
6679753Sandreas@sandberg.pp.se            main.Append(LINKFLAGS=['-arch', 'x86_64'])
6689753Sandreas@sandberg.pp.se            main.Append(ASFLAGS=['-arch', 'x86_64'])
6699753Sandreas@sandberg.pp.seexcept:
6709753Sandreas@sandberg.pp.se    pass
67111399Sandreas.sandberg@arm.com
67211399Sandreas.sandberg@arm.com# Recent versions of scons substitute a "Null" object for Configure()
67311399Sandreas.sandberg@arm.com# when configuration isn't necessary, e.g., if the "--help" option is
6749753Sandreas@sandberg.pp.se# present.  Unfortuantely this Null object always returns false,
6759753Sandreas@sandberg.pp.se# breaking all our configuration checks.  We replace it with our own
6769755Sandreas@sandberg.pp.se# more optimistic null object that returns True instead.
6779753Sandreas@sandberg.pp.seif not conf:
6789755Sandreas@sandberg.pp.se    def NullCheck(*args, **kwargs):
6799755Sandreas@sandberg.pp.se        return True
6809753Sandreas@sandberg.pp.se
6819755Sandreas@sandberg.pp.se    class NullConf:
6829753Sandreas@sandberg.pp.se        def __init__(self, env):
6839904Sandreas@sandberg.pp.se            self.env = env
6849904Sandreas@sandberg.pp.se        def Finish(self):
6859904Sandreas@sandberg.pp.se            return self.env
6869904Sandreas@sandberg.pp.se        def __getattr__(self, mname):
6879904Sandreas@sandberg.pp.se            return NullCheck
6889904Sandreas@sandberg.pp.se
6899753Sandreas@sandberg.pp.se    conf = NullConf(main)
6909753Sandreas@sandberg.pp.se
6919753Sandreas@sandberg.pp.se# Cache build files in the supplied directory.
6929753Sandreas@sandberg.pp.seif main['M5_BUILD_CACHE']:
6939651SAndreas.Sandberg@ARM.com    print('Using build cache located at', main['M5_BUILD_CACHE'])
6949753Sandreas@sandberg.pp.se    CacheDir(main['M5_BUILD_CACHE'])
6959753Sandreas@sandberg.pp.se
6969651SAndreas.Sandberg@ARM.commain['USE_PYTHON'] = not GetOption('without_python')
6979753Sandreas@sandberg.pp.seif main['USE_PYTHON']:
6989753Sandreas@sandberg.pp.se    # Find Python include and library directories for embedding the
69911629Smichael.lebeane@amd.com    # interpreter. We rely on python-config to resolve the appropriate
7009753Sandreas@sandberg.pp.se    # includes and linker flags. ParseConfig does not seem to understand
7019753Sandreas@sandberg.pp.se    # the more exotic linker flags such as -Xlinker and -export-dynamic so
7029753Sandreas@sandberg.pp.se    # we add them explicitly below. If you want to link in an alternate
7039753Sandreas@sandberg.pp.se    # version of python, see above for instructions on how to invoke
7049753Sandreas@sandberg.pp.se    # scons with the appropriate PATH set.
7059753Sandreas@sandberg.pp.se    #
7069753Sandreas@sandberg.pp.se    # First we check if python2-config exists, else we use python-config
7079753Sandreas@sandberg.pp.se    python_config = readCommand(['which', 'python2-config'],
70811629Smichael.lebeane@amd.com                                exception='').strip()
70911629Smichael.lebeane@amd.com    if not os.path.exists(python_config):
71011629Smichael.lebeane@amd.com        python_config = readCommand(['which', 'python-config'],
7119753Sandreas@sandberg.pp.se                                    exception='').strip()
7129753Sandreas@sandberg.pp.se    py_includes = readCommand([python_config, '--includes'],
7139753Sandreas@sandberg.pp.se                              exception='').split()
7149753Sandreas@sandberg.pp.se    py_includes = filter(lambda s: match(r'.*\/include\/.*',s), py_includes)
7159651SAndreas.Sandberg@ARM.com    # Strip the -I from the include folders before adding them to the
7169651SAndreas.Sandberg@ARM.com    # CPPPATH
7179735Sandreas@sandberg.pp.se    py_includes = map(lambda s: s[2:] if s.startswith('-I') else s, py_includes)
7189735Sandreas@sandberg.pp.se    main.Append(CPPPATH=py_includes)
7199735Sandreas@sandberg.pp.se
7209735Sandreas@sandberg.pp.se    # Read the linker flags and split them into libraries and other link
7219735Sandreas@sandberg.pp.se    # flags. The libraries are added later through the call the CheckLib.
7229735Sandreas@sandberg.pp.se    py_ld_flags = readCommand([python_config, '--ldflags'],
7239651SAndreas.Sandberg@ARM.com        exception='').split()
7249651SAndreas.Sandberg@ARM.com    py_libs = []
7259651SAndreas.Sandberg@ARM.com    for lib in py_ld_flags:
7269753Sandreas@sandberg.pp.se         if not lib.startswith('-l'):
72711363Sandreas@sandberg.pp.se             main.Append(LINKFLAGS=[lib])
72811363Sandreas@sandberg.pp.se         else:
72911363Sandreas@sandberg.pp.se             lib = lib[2:]
7309651SAndreas.Sandberg@ARM.com             if lib not in py_libs:
7319655SAndreas.Sandberg@ARM.com                 py_libs.append(lib)
7329753Sandreas@sandberg.pp.se
7339753Sandreas@sandberg.pp.se    # verify that this stuff works
7349753Sandreas@sandberg.pp.se    if not conf.CheckHeader('Python.h', '<>'):
7359753Sandreas@sandberg.pp.se        print("Error: Check failed for Python.h header in", py_includes)
7369753Sandreas@sandberg.pp.se        print("Two possible reasons:")
7379735Sandreas@sandberg.pp.se        print("1. Python headers are not installed (You can install the "
7389755Sandreas@sandberg.pp.se              "package python-dev on Ubuntu and RedHat)")
7399755Sandreas@sandberg.pp.se        print("2. SCons is using a wrong C compiler. This can happen if "
74010114Sandreas@sandberg.pp.se              "CC has the wrong value.")
74110114Sandreas@sandberg.pp.se        print("CC = %s" % main['CC'])
74210114Sandreas@sandberg.pp.se        Exit(1)
7439753Sandreas@sandberg.pp.se
74410114Sandreas@sandberg.pp.se    for lib in py_libs:
7459655SAndreas.Sandberg@ARM.com        if not conf.CheckLib(lib):
74610114Sandreas@sandberg.pp.se            print("Error: can't find library %s required by python" % lib)
74710114Sandreas@sandberg.pp.se            Exit(1)
74810114Sandreas@sandberg.pp.se
7499753Sandreas@sandberg.pp.se# On Solaris you need to use libsocket for socket ops
75010114Sandreas@sandberg.pp.seif not conf.CheckLibWithHeader(None, 'sys/socket.h', 'C++', 'accept(0,0,0);'):
75110114Sandreas@sandberg.pp.se   if not conf.CheckLibWithHeader('socket', 'sys/socket.h', 'C++', 'accept(0,0,0);'):
7529753Sandreas@sandberg.pp.se       print("Can't find library with socket calls (e.g. accept())")
7539753Sandreas@sandberg.pp.se       Exit(1)
7549753Sandreas@sandberg.pp.se
7559753Sandreas@sandberg.pp.se# Check for zlib.  If the check passes, libz will be automatically
7569753Sandreas@sandberg.pp.se# added to the LIBS environment variable.
7579753Sandreas@sandberg.pp.seif not conf.CheckLibWithHeader('z', 'zlib.h', 'C++','zlibVersion();'):
7589753Sandreas@sandberg.pp.se    print('Error: did not find needed zlib compression library '
75910157Sandreas@sandberg.pp.se          'and/or zlib.h header file.')
76010157Sandreas@sandberg.pp.se    print('       Please install zlib and try again.')
76110157Sandreas@sandberg.pp.se    Exit(1)
76210157Sandreas@sandberg.pp.se
76310157Sandreas@sandberg.pp.se# If we have the protobuf compiler, also make sure we have the
76410157Sandreas@sandberg.pp.se# development libraries. If the check passes, libprotobuf will be
76510157Sandreas@sandberg.pp.se# automatically added to the LIBS environment variable. After
76610157Sandreas@sandberg.pp.se# this, we can use the HAVE_PROTOBUF flag to determine if we have
7679753Sandreas@sandberg.pp.se# got both protoc and libprotobuf available.
7689753Sandreas@sandberg.pp.semain['HAVE_PROTOBUF'] = main['PROTOC'] and \
7699753Sandreas@sandberg.pp.se    conf.CheckLibWithHeader('protobuf', 'google/protobuf/message.h',
7709753Sandreas@sandberg.pp.se                            'C++', 'GOOGLE_PROTOBUF_VERIFY_VERSION;')
7719753Sandreas@sandberg.pp.se
7729753Sandreas@sandberg.pp.se# Valgrind gets much less confused if you tell it when you're using
7739753Sandreas@sandberg.pp.se# alternative stacks.
7749753Sandreas@sandberg.pp.semain['HAVE_VALGRIND'] = conf.CheckCHeader('valgrind/valgrind.h')
7759753Sandreas@sandberg.pp.se
7769753Sandreas@sandberg.pp.se# If we have the compiler but not the library, print another warning.
7779753Sandreas@sandberg.pp.seif main['PROTOC'] and not main['HAVE_PROTOBUF']:
7789753Sandreas@sandberg.pp.se    print(termcap.Yellow + termcap.Bold +
7799753Sandreas@sandberg.pp.se        'Warning: did not find protocol buffer library and/or headers.\n' +
7809753Sandreas@sandberg.pp.se    '       Please install libprotobuf-dev for tracing support.' +
7819753Sandreas@sandberg.pp.se    termcap.Normal)
7829753Sandreas@sandberg.pp.se
7839753Sandreas@sandberg.pp.se# Check for librt.
7849753Sandreas@sandberg.pp.sehave_posix_clock = \
7859753Sandreas@sandberg.pp.se    conf.CheckLibWithHeader(None, 'time.h', 'C',
7869753Sandreas@sandberg.pp.se                            'clock_nanosleep(0,0,NULL,NULL);') or \
7879753Sandreas@sandberg.pp.se    conf.CheckLibWithHeader('rt', 'time.h', 'C',
7889753Sandreas@sandberg.pp.se                            'clock_nanosleep(0,0,NULL,NULL);')
7899753Sandreas@sandberg.pp.se
7909753Sandreas@sandberg.pp.sehave_posix_timers = \
7919753Sandreas@sandberg.pp.se    conf.CheckLibWithHeader([None, 'rt'], [ 'time.h', 'signal.h' ], 'C',
79210114Sandreas@sandberg.pp.se                            'timer_create(CLOCK_MONOTONIC, NULL, NULL);')
7939753Sandreas@sandberg.pp.se
7949753Sandreas@sandberg.pp.seif not GetOption('without_tcmalloc'):
7959753Sandreas@sandberg.pp.se    if conf.CheckLib('tcmalloc'):
7969753Sandreas@sandberg.pp.se        main.Append(CCFLAGS=main['TCMALLOC_CCFLAGS'])
79710114Sandreas@sandberg.pp.se    elif conf.CheckLib('tcmalloc_minimal'):
7989753Sandreas@sandberg.pp.se        main.Append(CCFLAGS=main['TCMALLOC_CCFLAGS'])
7999753Sandreas@sandberg.pp.se    else:
8009753Sandreas@sandberg.pp.se        print(termcap.Yellow + termcap.Bold +
8019753Sandreas@sandberg.pp.se              "You can get a 12% performance improvement by "
8029753Sandreas@sandberg.pp.se              "installing tcmalloc (libgoogle-perftools-dev package "
8039753Sandreas@sandberg.pp.se              "on Ubuntu or RedHat)." + termcap.Normal)
8049753Sandreas@sandberg.pp.se
8059753Sandreas@sandberg.pp.se
8069753Sandreas@sandberg.pp.se# Detect back trace implementations. The last implementation in the
8079753Sandreas@sandberg.pp.se# list will be used by default.
8089753Sandreas@sandberg.pp.sebacktrace_impls = [ "none" ]
8099753Sandreas@sandberg.pp.se
8109753Sandreas@sandberg.pp.sebacktrace_checker = 'char temp;' + \
8119753Sandreas@sandberg.pp.se    ' backtrace_symbols_fd((void*)&temp, 0, 0);'
8129753Sandreas@sandberg.pp.seif conf.CheckLibWithHeader(None, 'execinfo.h', 'C', backtrace_checker):
8139753Sandreas@sandberg.pp.se    backtrace_impls.append("glibc")
8149651SAndreas.Sandberg@ARM.comelif conf.CheckLibWithHeader('execinfo', 'execinfo.h', 'C',
8159655SAndreas.Sandberg@ARM.com                             backtrace_checker):
8169651SAndreas.Sandberg@ARM.com    # NetBSD and FreeBSD need libexecinfo.
8179651SAndreas.Sandberg@ARM.com    backtrace_impls.append("glibc")
8189651SAndreas.Sandberg@ARM.com    main.Append(LIBS=['execinfo'])
8199651SAndreas.Sandberg@ARM.com
8209651SAndreas.Sandberg@ARM.comif backtrace_impls[-1] == "none":
8219651SAndreas.Sandberg@ARM.com    default_backtrace_impl = "none"
8229651SAndreas.Sandberg@ARM.com    print(termcap.Yellow + termcap.Bold +
8239651SAndreas.Sandberg@ARM.com        "No suitable back trace implementation found." +
8249651SAndreas.Sandberg@ARM.com        termcap.Normal)
8259651SAndreas.Sandberg@ARM.com
8269651SAndreas.Sandberg@ARM.comif not have_posix_clock:
8279651SAndreas.Sandberg@ARM.com    print("Can't find library for POSIX clocks.")
8289651SAndreas.Sandberg@ARM.com
8299651SAndreas.Sandberg@ARM.com# Check for <fenv.h> (C99 FP environment control)
8309651SAndreas.Sandberg@ARM.comhave_fenv = conf.CheckHeader('fenv.h', '<>')
8319651SAndreas.Sandberg@ARM.comif not have_fenv:
8329651SAndreas.Sandberg@ARM.com    print("Warning: Header file <fenv.h> not found.")
8339651SAndreas.Sandberg@ARM.com    print("         This host has no IEEE FP rounding mode control.")
8349651SAndreas.Sandberg@ARM.com
8359651SAndreas.Sandberg@ARM.com# Check for <png.h> (libpng library needed if wanting to dump
8369651SAndreas.Sandberg@ARM.com# frame buffer image in png format)
8379651SAndreas.Sandberg@ARM.comhave_png = conf.CheckHeader('png.h', '<>')
8389651SAndreas.Sandberg@ARM.comif not have_png:
8399651SAndreas.Sandberg@ARM.com    print("Warning: Header file <png.h> not found.")
8409651SAndreas.Sandberg@ARM.com    print("         This host has no libpng library.")
8419651SAndreas.Sandberg@ARM.com    print("         Disabling support for PNG framebuffers.")
8429651SAndreas.Sandberg@ARM.com
8439651SAndreas.Sandberg@ARM.com# Check if we should enable KVM-based hardware virtualization. The API
8449651SAndreas.Sandberg@ARM.com# we rely on exists since version 2.6.36 of the kernel, but somehow
8459651SAndreas.Sandberg@ARM.com# the KVM_API_VERSION does not reflect the change. We test for one of
8469651SAndreas.Sandberg@ARM.com# the types as a fall back.
8479651SAndreas.Sandberg@ARM.comhave_kvm = conf.CheckHeader('linux/kvm.h', '<>')
8489651SAndreas.Sandberg@ARM.comif not have_kvm:
8499651SAndreas.Sandberg@ARM.com    print("Info: Compatible header file <linux/kvm.h> not found, "
8509651SAndreas.Sandberg@ARM.com          "disabling KVM support.")
8519651SAndreas.Sandberg@ARM.com
8529651SAndreas.Sandberg@ARM.com# Check if the TUN/TAP driver is available.
8539651SAndreas.Sandberg@ARM.comhave_tuntap = conf.CheckHeader('linux/if_tun.h', '<>')
8549651SAndreas.Sandberg@ARM.comif not have_tuntap:
8559651SAndreas.Sandberg@ARM.com    print("Info: Compatible header file <linux/if_tun.h> not found.")
8569651SAndreas.Sandberg@ARM.com
8579651SAndreas.Sandberg@ARM.com# x86 needs support for xsave. We test for the structure here since we
8589651SAndreas.Sandberg@ARM.com# won't be able to run new tests by the time we know which ISA we're
8599651SAndreas.Sandberg@ARM.com# targeting.
8609651SAndreas.Sandberg@ARM.comhave_kvm_xsave = conf.CheckTypeSize('struct kvm_xsave',
8619651SAndreas.Sandberg@ARM.com                                    '#include <linux/kvm.h>') != 0
8629651SAndreas.Sandberg@ARM.com
8639651SAndreas.Sandberg@ARM.com# Check if the requested target ISA is compatible with the host
8649651SAndreas.Sandberg@ARM.comdef is_isa_kvm_compatible(isa):
8659651SAndreas.Sandberg@ARM.com    try:
8669651SAndreas.Sandberg@ARM.com        import platform
8679651SAndreas.Sandberg@ARM.com        host_isa = platform.machine()
8689651SAndreas.Sandberg@ARM.com    except:
8699651SAndreas.Sandberg@ARM.com        print("Warning: Failed to determine host ISA.")
8709651SAndreas.Sandberg@ARM.com        return False
8719651SAndreas.Sandberg@ARM.com
8729651SAndreas.Sandberg@ARM.com    if not have_posix_timers:
8739651SAndreas.Sandberg@ARM.com        print("Warning: Can not enable KVM, host seems to lack support "
8749651SAndreas.Sandberg@ARM.com              "for POSIX timers")
8759651SAndreas.Sandberg@ARM.com        return False
8769651SAndreas.Sandberg@ARM.com
8779651SAndreas.Sandberg@ARM.com    if isa == "arm":
8789651SAndreas.Sandberg@ARM.com        return host_isa in ( "armv7l", "aarch64" )
8799651SAndreas.Sandberg@ARM.com    elif isa == "x86":
8809651SAndreas.Sandberg@ARM.com        if host_isa != "x86_64":
8819651SAndreas.Sandberg@ARM.com            return False
8829651SAndreas.Sandberg@ARM.com
8839651SAndreas.Sandberg@ARM.com        if not have_kvm_xsave:
8849651SAndreas.Sandberg@ARM.com            print("KVM on x86 requires xsave support in kernel headers.")
8859651SAndreas.Sandberg@ARM.com            return False
8869651SAndreas.Sandberg@ARM.com
8879651SAndreas.Sandberg@ARM.com        return True
8889651SAndreas.Sandberg@ARM.com    else:
8899651SAndreas.Sandberg@ARM.com        return False
8909651SAndreas.Sandberg@ARM.com
8919651SAndreas.Sandberg@ARM.com
8929651SAndreas.Sandberg@ARM.com# Check if the exclude_host attribute is available. We want this to
8939651SAndreas.Sandberg@ARM.com# get accurate instruction counts in KVM.
8949651SAndreas.Sandberg@ARM.commain['HAVE_PERF_ATTR_EXCLUDE_HOST'] = conf.CheckMember(
8959651SAndreas.Sandberg@ARM.com    'linux/perf_event.h', 'struct perf_event_attr', 'exclude_host')
8969651SAndreas.Sandberg@ARM.com
8979651SAndreas.Sandberg@ARM.com
8989651SAndreas.Sandberg@ARM.com######################################################################
8999651SAndreas.Sandberg@ARM.com#
9009651SAndreas.Sandberg@ARM.com# Finish the configuration
9019651SAndreas.Sandberg@ARM.com#
9029651SAndreas.Sandberg@ARM.commain = conf.Finish()
9039651SAndreas.Sandberg@ARM.com
9049651SAndreas.Sandberg@ARM.com######################################################################
9059651SAndreas.Sandberg@ARM.com#
9069651SAndreas.Sandberg@ARM.com# Collect all non-global variables
9079651SAndreas.Sandberg@ARM.com#
9089651SAndreas.Sandberg@ARM.com
9099651SAndreas.Sandberg@ARM.com# Define the universe of supported ISAs
9109651SAndreas.Sandberg@ARM.comall_isa_list = [ ]
9119651SAndreas.Sandberg@ARM.comall_gpu_isa_list = [ ]
9129651SAndreas.Sandberg@ARM.comExport('all_isa_list')
9139651SAndreas.Sandberg@ARM.comExport('all_gpu_isa_list')
9149651SAndreas.Sandberg@ARM.com
9159651SAndreas.Sandberg@ARM.comclass CpuModel(object):
9169651SAndreas.Sandberg@ARM.com    '''The CpuModel class encapsulates everything the ISA parser needs to
9179651SAndreas.Sandberg@ARM.com    know about a particular CPU model.'''
9189651SAndreas.Sandberg@ARM.com
9199651SAndreas.Sandberg@ARM.com    # Dict of available CPU model objects.  Accessible as CpuModel.dict.
9209651SAndreas.Sandberg@ARM.com    dict = {}
9219651SAndreas.Sandberg@ARM.com
9229651SAndreas.Sandberg@ARM.com    # Constructor.  Automatically adds models to CpuModel.dict.
9239651SAndreas.Sandberg@ARM.com    def __init__(self, name, default=False):
9249651SAndreas.Sandberg@ARM.com        self.name = name           # name of model
9259651SAndreas.Sandberg@ARM.com
9269651SAndreas.Sandberg@ARM.com        # This cpu is enabled by default
9279651SAndreas.Sandberg@ARM.com        self.default = default
9289651SAndreas.Sandberg@ARM.com
92910843Sandreas.sandberg@arm.com        # Add self to dict
93010843Sandreas.sandberg@arm.com        if name in CpuModel.dict:
93110843Sandreas.sandberg@arm.com            raise AttributeError, "CpuModel '%s' already registered" % name
93210843Sandreas.sandberg@arm.com        CpuModel.dict[name] = self
93310843Sandreas.sandberg@arm.com
93410843Sandreas.sandberg@arm.comExport('CpuModel')
93510843Sandreas.sandberg@arm.com
93610843Sandreas.sandberg@arm.com# Sticky variables get saved in the variables file so they persist from
9379651SAndreas.Sandberg@ARM.com# one invocation to the next (unless overridden, in which case the new
9389651SAndreas.Sandberg@ARM.com# value becomes sticky).
9399651SAndreas.Sandberg@ARM.comsticky_vars = Variables(args=ARGUMENTS)
9409651SAndreas.Sandberg@ARM.comExport('sticky_vars')
9419651SAndreas.Sandberg@ARM.com
9429651SAndreas.Sandberg@ARM.com# Sticky variables that should be exported
9439651SAndreas.Sandberg@ARM.comexport_vars = []
9449651SAndreas.Sandberg@ARM.comExport('export_vars')
9459651SAndreas.Sandberg@ARM.com
9469651SAndreas.Sandberg@ARM.com# For Ruby
9479651SAndreas.Sandberg@ARM.comall_protocols = []
9489651SAndreas.Sandberg@ARM.comExport('all_protocols')
9499651SAndreas.Sandberg@ARM.comprotocol_dirs = []
9509651SAndreas.Sandberg@ARM.comExport('protocol_dirs')
9519651SAndreas.Sandberg@ARM.comslicc_includes = []
9529651SAndreas.Sandberg@ARM.comExport('slicc_includes')
9539651SAndreas.Sandberg@ARM.com
9549651SAndreas.Sandberg@ARM.com# Walk the tree and execute all SConsopts scripts that wil add to the
9559651SAndreas.Sandberg@ARM.com# above variables
9569651SAndreas.Sandberg@ARM.comif GetOption('verbose'):
9579651SAndreas.Sandberg@ARM.com    print("Reading SConsopts")
9589651SAndreas.Sandberg@ARM.comfor bdir in [ base_dir ] + extras_dir_list:
9599651SAndreas.Sandberg@ARM.com    if not isdir(bdir):
9609651SAndreas.Sandberg@ARM.com        print("Error: directory '%s' does not exist" % bdir)
9619652SAndreas.Sandberg@ARM.com        Exit(1)
9629652SAndreas.Sandberg@ARM.com    for root, dirs, files in os.walk(bdir):
9639652SAndreas.Sandberg@ARM.com        if 'SConsopts' in files:
9649652SAndreas.Sandberg@ARM.com            if GetOption('verbose'):
9659652SAndreas.Sandberg@ARM.com                print("Reading", joinpath(root, 'SConsopts'))
9669652SAndreas.Sandberg@ARM.com            SConscript(joinpath(root, 'SConsopts'))
9679652SAndreas.Sandberg@ARM.com
9689652SAndreas.Sandberg@ARM.comall_isa_list.sort()
9699652SAndreas.Sandberg@ARM.comall_gpu_isa_list.sort()
9709652SAndreas.Sandberg@ARM.com
9719652SAndreas.Sandberg@ARM.comsticky_vars.AddVariables(
9729652SAndreas.Sandberg@ARM.com    EnumVariable('TARGET_ISA', 'Target ISA', 'alpha', all_isa_list),
9739652SAndreas.Sandberg@ARM.com    EnumVariable('TARGET_GPU_ISA', 'Target GPU ISA', 'hsail', all_gpu_isa_list),
9749652SAndreas.Sandberg@ARM.com    ListVariable('CPU_MODELS', 'CPU models',
9759652SAndreas.Sandberg@ARM.com                 sorted(n for n,m in CpuModel.dict.iteritems() if m.default),
9769652SAndreas.Sandberg@ARM.com                 sorted(CpuModel.dict.keys())),
9779652SAndreas.Sandberg@ARM.com    BoolVariable('EFENCE', 'Link with Electric Fence malloc debugger',
9789652SAndreas.Sandberg@ARM.com                 False),
9799652SAndreas.Sandberg@ARM.com    BoolVariable('SS_COMPATIBLE_FP',
9809652SAndreas.Sandberg@ARM.com                 'Make floating-point results compatible with SimpleScalar',
9819652SAndreas.Sandberg@ARM.com                 False),
9829652SAndreas.Sandberg@ARM.com    BoolVariable('USE_SSE2',
9839652SAndreas.Sandberg@ARM.com                 'Compile for SSE2 (-msse2) to get IEEE FP on x86 hosts',
9849652SAndreas.Sandberg@ARM.com                 False),
9859651SAndreas.Sandberg@ARM.com    BoolVariable('USE_POSIX_CLOCK', 'Use POSIX Clocks', have_posix_clock),
9869651SAndreas.Sandberg@ARM.com    BoolVariable('USE_FENV', 'Use <fenv.h> IEEE mode control', have_fenv),
9879651SAndreas.Sandberg@ARM.com    BoolVariable('USE_PNG',  'Enable support for PNG images', have_png),
9889651SAndreas.Sandberg@ARM.com    BoolVariable('CP_ANNOTATE', 'Enable critical path annotation capability',
9899753Sandreas@sandberg.pp.se                 False),
9909651SAndreas.Sandberg@ARM.com    BoolVariable('USE_KVM', 'Enable hardware virtualized (KVM) CPU models',
9919753Sandreas@sandberg.pp.se                 have_kvm),
9929753Sandreas@sandberg.pp.se    BoolVariable('USE_TUNTAP',
9939753Sandreas@sandberg.pp.se                 'Enable using a tap device to bridge to the host network',
9949651SAndreas.Sandberg@ARM.com                 have_tuntap),
9959651SAndreas.Sandberg@ARM.com    BoolVariable('BUILD_GPU', 'Build the compute-GPU model', False),
9969651SAndreas.Sandberg@ARM.com    EnumVariable('PROTOCOL', 'Coherence protocol for Ruby', 'None',
9979651SAndreas.Sandberg@ARM.com                  all_protocols),
9989651SAndreas.Sandberg@ARM.com    EnumVariable('BACKTRACE_IMPL', 'Post-mortem dump implementation',
9999651SAndreas.Sandberg@ARM.com                 backtrace_impls[-1], backtrace_impls)
10009651SAndreas.Sandberg@ARM.com    )
10019651SAndreas.Sandberg@ARM.com
100211629Smichael.lebeane@amd.com# These variables get exported to #defines in config/*.hh (see src/SConscript).
10039651SAndreas.Sandberg@ARM.comexport_vars += ['USE_FENV', 'SS_COMPATIBLE_FP', 'TARGET_ISA', 'TARGET_GPU_ISA',
100411629Smichael.lebeane@amd.com                'CP_ANNOTATE', 'USE_POSIX_CLOCK', 'USE_KVM', 'USE_TUNTAP',
100511629Smichael.lebeane@amd.com                'PROTOCOL', 'HAVE_PROTOBUF', 'HAVE_VALGRIND',
100611629Smichael.lebeane@amd.com                'HAVE_PERF_ATTR_EXCLUDE_HOST', 'USE_PNG']
100711629Smichael.lebeane@amd.com
10089651SAndreas.Sandberg@ARM.com###################################################
10099651SAndreas.Sandberg@ARM.com#
10109651SAndreas.Sandberg@ARM.com# Define a SCons builder for configuration flag headers.
10119651SAndreas.Sandberg@ARM.com#
10129651SAndreas.Sandberg@ARM.com###################################################
10139651SAndreas.Sandberg@ARM.com
10149651SAndreas.Sandberg@ARM.com# This function generates a config header file that #defines the
10159651SAndreas.Sandberg@ARM.com# variable symbol to the current variable setting (0 or 1).  The source
10169651SAndreas.Sandberg@ARM.com# operands are the name of the variable and a Value node containing the
10179651SAndreas.Sandberg@ARM.com# value of the variable.
10189651SAndreas.Sandberg@ARM.comdef build_config_file(target, source, env):
10199651SAndreas.Sandberg@ARM.com    (variable, value) = [s.get_contents() for s in source]
10209651SAndreas.Sandberg@ARM.com    f = file(str(target[0]), 'w')
10219651SAndreas.Sandberg@ARM.com    print('#define', variable, value, file=f)
10229651SAndreas.Sandberg@ARM.com    f.close()
10239651SAndreas.Sandberg@ARM.com    return None
10249651SAndreas.Sandberg@ARM.com
102511629Smichael.lebeane@amd.com# Combine the two functions into a scons Action object.
10269651SAndreas.Sandberg@ARM.comconfig_action = MakeAction(build_config_file, Transform("CONFIG H", 2))
10279651SAndreas.Sandberg@ARM.com
10289651SAndreas.Sandberg@ARM.com# The emitter munges the source & target node lists to reflect what
10299651SAndreas.Sandberg@ARM.com# we're really doing.
10309651SAndreas.Sandberg@ARM.comdef config_emitter(target, source, env):
10319651SAndreas.Sandberg@ARM.com    # extract variable name from Builder arg
103211629Smichael.lebeane@amd.com    variable = str(target[0])
103311629Smichael.lebeane@amd.com    # True target is config header file
103411629Smichael.lebeane@amd.com    target = joinpath('config', variable.lower() + '.hh')
103511629Smichael.lebeane@amd.com    val = env[variable]
103611629Smichael.lebeane@amd.com    if isinstance(val, bool):
103711629Smichael.lebeane@amd.com        # Force value to 0/1
103811629Smichael.lebeane@amd.com        val = int(val)
103911629Smichael.lebeane@amd.com    elif isinstance(val, str):
10409651SAndreas.Sandberg@ARM.com        val = '"' + val + '"'
10419651SAndreas.Sandberg@ARM.com
10429651SAndreas.Sandberg@ARM.com    # Sources are variable name & value (packaged in SCons Value nodes)
10439651SAndreas.Sandberg@ARM.com    return ([target], [Value(variable), Value(val)])
10449651SAndreas.Sandberg@ARM.com
10459651SAndreas.Sandberg@ARM.comconfig_builder = Builder(emitter = config_emitter, action = config_action)
10469651SAndreas.Sandberg@ARM.com
10479651SAndreas.Sandberg@ARM.commain.Append(BUILDERS = { 'ConfigFile' : config_builder })
10489651SAndreas.Sandberg@ARM.com
10499651SAndreas.Sandberg@ARM.com###################################################
10509651SAndreas.Sandberg@ARM.com#
10519651SAndreas.Sandberg@ARM.com# Builders for static and shared partially linked object files.
10529651SAndreas.Sandberg@ARM.com#
10539651SAndreas.Sandberg@ARM.com###################################################
10549651SAndreas.Sandberg@ARM.com
10559651SAndreas.Sandberg@ARM.compartial_static_builder = Builder(action=SCons.Defaults.LinkAction,
10569651SAndreas.Sandberg@ARM.com                                 src_suffix='$OBJSUFFIX',
10579689Sandreas@sandberg.pp.se                                 src_builder=['StaticObject', 'Object'],
10589651SAndreas.Sandberg@ARM.com                                 LINKFLAGS='$PLINKFLAGS',
10599651SAndreas.Sandberg@ARM.com                                 LIBS='')
10609651SAndreas.Sandberg@ARM.com
10619651SAndreas.Sandberg@ARM.comdef partial_shared_emitter(target, source, env):
10629651SAndreas.Sandberg@ARM.com    for tgt in target:
10639651SAndreas.Sandberg@ARM.com        tgt.attributes.shared = 1
10649651SAndreas.Sandberg@ARM.com    return (target, source)
10659651SAndreas.Sandberg@ARM.compartial_shared_builder = Builder(action=SCons.Defaults.ShLinkAction,
10669651SAndreas.Sandberg@ARM.com                                 emitter=partial_shared_emitter,
10679651SAndreas.Sandberg@ARM.com                                 src_suffix='$SHOBJSUFFIX',
10689651SAndreas.Sandberg@ARM.com                                 src_builder='SharedObject',
10699651SAndreas.Sandberg@ARM.com                                 SHLINKFLAGS='$PSHLINKFLAGS',
10709651SAndreas.Sandberg@ARM.com                                 LIBS='')
10719651SAndreas.Sandberg@ARM.com
10729651SAndreas.Sandberg@ARM.commain.Append(BUILDERS = { 'PartialShared' : partial_shared_builder,
10739651SAndreas.Sandberg@ARM.com                         'PartialStatic' : partial_static_builder })
10749651SAndreas.Sandberg@ARM.com
10759651SAndreas.Sandberg@ARM.com# builds in ext are shared across all configs in the build root.
10769651SAndreas.Sandberg@ARM.comext_dir = abspath(joinpath(str(main.root), 'ext'))
10779651SAndreas.Sandberg@ARM.comext_build_dirs = []
10789651SAndreas.Sandberg@ARM.comfor root, dirs, files in os.walk(ext_dir):
10799651SAndreas.Sandberg@ARM.com    if 'SConscript' in files:
10809651SAndreas.Sandberg@ARM.com        build_dir = os.path.relpath(root, ext_dir)
10819651SAndreas.Sandberg@ARM.com        ext_build_dirs.append(build_dir)
10829651SAndreas.Sandberg@ARM.com        main.SConscript(joinpath(root, 'SConscript'),
10839651SAndreas.Sandberg@ARM.com                        variant_dir=joinpath(build_root, build_dir))
10849651SAndreas.Sandberg@ARM.com
10859651SAndreas.Sandberg@ARM.commain.Prepend(CPPPATH=Dir('ext/pybind11/include/'))
10869651SAndreas.Sandberg@ARM.com
10879689Sandreas@sandberg.pp.se###################################################
10889651SAndreas.Sandberg@ARM.com#
10899651SAndreas.Sandberg@ARM.com# This builder and wrapper method are used to set up a directory with
10909651SAndreas.Sandberg@ARM.com# switching headers. Those are headers which are in a generic location and
10919651SAndreas.Sandberg@ARM.com# that include more specific headers from a directory chosen at build time
10929651SAndreas.Sandberg@ARM.com# based on the current build settings.
10939651SAndreas.Sandberg@ARM.com#
10949651SAndreas.Sandberg@ARM.com###################################################
10959689Sandreas@sandberg.pp.se
10969651SAndreas.Sandberg@ARM.comdef build_switching_header(target, source, env):
10979651SAndreas.Sandberg@ARM.com    path = str(target[0])
10989651SAndreas.Sandberg@ARM.com    subdir = str(source[0])
10999651SAndreas.Sandberg@ARM.com    dp, fp = os.path.split(path)
11009651SAndreas.Sandberg@ARM.com    dp = os.path.relpath(os.path.realpath(dp),
11019651SAndreas.Sandberg@ARM.com                         os.path.realpath(env['BUILDDIR']))
11029651SAndreas.Sandberg@ARM.com    with open(path, 'w') as hdr:
11039651SAndreas.Sandberg@ARM.com        print('#include "%s/%s/%s"' % (dp, subdir, fp), file=hdr)
11049689Sandreas@sandberg.pp.se
11059651SAndreas.Sandberg@ARM.comswitching_header_action = MakeAction(build_switching_header,
11069651SAndreas.Sandberg@ARM.com                                     Transform('GENERATE'))
11079651SAndreas.Sandberg@ARM.com
11089651SAndreas.Sandberg@ARM.comswitching_header_builder = Builder(action=switching_header_action,
11099651SAndreas.Sandberg@ARM.com                                   source_factory=Value,
11109651SAndreas.Sandberg@ARM.com                                   single_source=True)
11119651SAndreas.Sandberg@ARM.com
11129760Sandreas@sandberg.pp.semain.Append(BUILDERS = { 'SwitchingHeader': switching_header_builder })
11139760Sandreas@sandberg.pp.se
11149760Sandreas@sandberg.pp.sedef switching_headers(self, headers, source):
111511629Smichael.lebeane@amd.com    for header in headers:
111611629Smichael.lebeane@amd.com        self.SwitchingHeader(header, source)
111711629Smichael.lebeane@amd.com
11189760Sandreas@sandberg.pp.semain.AddMethod(switching_headers, 'SwitchingHeaders')
11199760Sandreas@sandberg.pp.se
11209760Sandreas@sandberg.pp.se###################################################
11219760Sandreas@sandberg.pp.se#
11229760Sandreas@sandberg.pp.se# Define build environments for selected configurations.
112311629Smichael.lebeane@amd.com#
11249760Sandreas@sandberg.pp.se###################################################
11259760Sandreas@sandberg.pp.se
11269760Sandreas@sandberg.pp.sefor variant_path in variant_paths:
11279651SAndreas.Sandberg@ARM.com    if not GetOption('silent'):
11289651SAndreas.Sandberg@ARM.com        print("Building in", variant_path)
112911629Smichael.lebeane@amd.com
113011629Smichael.lebeane@amd.com    # Make a copy of the build-root environment to use for this config.
11319760Sandreas@sandberg.pp.se    env = main.Clone()
113211629Smichael.lebeane@amd.com    env['BUILDDIR'] = variant_path
113310157Sandreas@sandberg.pp.se
113410157Sandreas@sandberg.pp.se    # variant_dir is the tail component of build path, and is used to
113510157Sandreas@sandberg.pp.se    # determine the build parameters (e.g., 'ALPHA_SE')
11369882Sandreas@sandberg.pp.se    (build_root, variant_dir) = splitpath(variant_path)
113711629Smichael.lebeane@amd.com
113811629Smichael.lebeane@amd.com    # Set env variables according to the build directory config.
113910553Salexandru.dutu@amd.com    sticky_vars.files = []
114011629Smichael.lebeane@amd.com    # Variables for $BUILD_ROOT/$VARIANT_DIR are stored in
114111629Smichael.lebeane@amd.com    # $BUILD_ROOT/variables/$VARIANT_DIR so you can nuke
11429925Sandreas@sandberg.pp.se    # $BUILD_ROOT/$VARIANT_DIR without losing your variables settings.
11439760Sandreas@sandberg.pp.se    current_vars_file = joinpath(build_root, 'variables', variant_dir)
114410157Sandreas@sandberg.pp.se    if isfile(current_vars_file):
114510157Sandreas@sandberg.pp.se        sticky_vars.files.append(current_vars_file)
114610157Sandreas@sandberg.pp.se        if not GetOption('silent'):
114710157Sandreas@sandberg.pp.se            print("Using saved variables file %s" % current_vars_file)
114810157Sandreas@sandberg.pp.se    elif variant_dir in ext_build_dirs:
114911629Smichael.lebeane@amd.com        # Things in ext are built without a variant directory.
11509760Sandreas@sandberg.pp.se        continue
11519651SAndreas.Sandberg@ARM.com    else:
11529651SAndreas.Sandberg@ARM.com        # Build dir-specific variables file doesn't exist.
11539753Sandreas@sandberg.pp.se
11549753Sandreas@sandberg.pp.se        # Make sure the directory is there so we can create it later
11559753Sandreas@sandberg.pp.se        opt_dir = dirname(current_vars_file)
11569753Sandreas@sandberg.pp.se        if not isdir(opt_dir):
11579753Sandreas@sandberg.pp.se            mkdir(opt_dir)
11589753Sandreas@sandberg.pp.se
11599753Sandreas@sandberg.pp.se        # Get default build variables from source tree.  Variables are
11609753Sandreas@sandberg.pp.se        # normally determined by name of $VARIANT_DIR, but can be
11619753Sandreas@sandberg.pp.se        # overridden by '--default=' arg on command line.
11629753Sandreas@sandberg.pp.se        default = GetOption('default')
11639753Sandreas@sandberg.pp.se        opts_dir = joinpath(main.root.abspath, 'build_opts')
11649753Sandreas@sandberg.pp.se        if default:
11659753Sandreas@sandberg.pp.se            default_vars_files = [joinpath(build_root, 'variables', default),
11669753Sandreas@sandberg.pp.se                                  joinpath(opts_dir, default)]
11679753Sandreas@sandberg.pp.se        else:
11689753Sandreas@sandberg.pp.se            default_vars_files = [joinpath(opts_dir, variant_dir)]
11699753Sandreas@sandberg.pp.se        existing_files = filter(isfile, default_vars_files)
11709753Sandreas@sandberg.pp.se        if existing_files:
11719753Sandreas@sandberg.pp.se            default_vars_file = existing_files[0]
11729753Sandreas@sandberg.pp.se            sticky_vars.files.append(default_vars_file)
11739753Sandreas@sandberg.pp.se            print("Variables file %s not found,\n  using defaults in %s"
11749651SAndreas.Sandberg@ARM.com                  % (current_vars_file, default_vars_file))
11759651SAndreas.Sandberg@ARM.com        else:
11769651SAndreas.Sandberg@ARM.com            print("Error: cannot find variables file %s or "
11779651SAndreas.Sandberg@ARM.com                  "default file(s) %s"
11789651SAndreas.Sandberg@ARM.com                  % (current_vars_file, ' or '.join(default_vars_files)))
11799651SAndreas.Sandberg@ARM.com            Exit(1)
11809651SAndreas.Sandberg@ARM.com
11819651SAndreas.Sandberg@ARM.com    # Apply current variable settings to env
11829651SAndreas.Sandberg@ARM.com    sticky_vars.Update(env)
11839651SAndreas.Sandberg@ARM.com
11849651SAndreas.Sandberg@ARM.com    help_texts["local_vars"] += \
11859651SAndreas.Sandberg@ARM.com        "Build variables for %s:\n" % variant_dir \
11869651SAndreas.Sandberg@ARM.com                 + sticky_vars.GenerateHelpText(env)
11879651SAndreas.Sandberg@ARM.com
11889651SAndreas.Sandberg@ARM.com    # Process variable settings.
11899651SAndreas.Sandberg@ARM.com
11909651SAndreas.Sandberg@ARM.com    if not have_fenv and env['USE_FENV']:
11919651SAndreas.Sandberg@ARM.com        print("Warning: <fenv.h> not available; "
11929651SAndreas.Sandberg@ARM.com              "forcing USE_FENV to False in", variant_dir + ".")
11939651SAndreas.Sandberg@ARM.com        env['USE_FENV'] = False
11949651SAndreas.Sandberg@ARM.com
11959651SAndreas.Sandberg@ARM.com    if not env['USE_FENV']:
11969651SAndreas.Sandberg@ARM.com        print("Warning: No IEEE FP rounding mode control in",
11979651SAndreas.Sandberg@ARM.com              variant_dir + ".")
11989651SAndreas.Sandberg@ARM.com        print("         FP results may deviate slightly from other platforms.")
11999651SAndreas.Sandberg@ARM.com
12009651SAndreas.Sandberg@ARM.com    if not have_png and env['USE_PNG']:
12019651SAndreas.Sandberg@ARM.com        print("Warning: <png.h> not available; "
12029651SAndreas.Sandberg@ARM.com              "forcing USE_PNG to False in", variant_dir + ".")
12039651SAndreas.Sandberg@ARM.com        env['USE_PNG'] = False
12049651SAndreas.Sandberg@ARM.com
12059651SAndreas.Sandberg@ARM.com    if env['USE_PNG']:
12069651SAndreas.Sandberg@ARM.com        env.Append(LIBS=['png'])
12079651SAndreas.Sandberg@ARM.com
12089651SAndreas.Sandberg@ARM.com    if env['EFENCE']:
12099651SAndreas.Sandberg@ARM.com        env.Append(LIBS=['efence'])
12109892Sandreas@sandberg.pp.se
121110114Sandreas@sandberg.pp.se    if env['USE_KVM']:
121210073Sandreas@sandberg.pp.se        if not have_kvm:
121310114Sandreas@sandberg.pp.se            print("Warning: Can not enable KVM, host seems to "
121410073Sandreas@sandberg.pp.se                  "lack KVM support")
121510073Sandreas@sandberg.pp.se            env['USE_KVM'] = False
121610073Sandreas@sandberg.pp.se        elif not is_isa_kvm_compatible(env['TARGET_ISA']):
121710073Sandreas@sandberg.pp.se            print("Info: KVM support disabled due to unsupported host and "
121810073Sandreas@sandberg.pp.se                  "target ISA combination")
12199892Sandreas@sandberg.pp.se            env['USE_KVM'] = False
12209892Sandreas@sandberg.pp.se
122110114Sandreas@sandberg.pp.se    if env['USE_TUNTAP']:
12229892Sandreas@sandberg.pp.se        if not have_tuntap:
12239892Sandreas@sandberg.pp.se            print("Warning: Can't connect EtherTap with a tap device.")
12249892Sandreas@sandberg.pp.se            env['USE_TUNTAP'] = False
12259651SAndreas.Sandberg@ARM.com
12269651SAndreas.Sandberg@ARM.com    if env['BUILD_GPU']:
12279651SAndreas.Sandberg@ARM.com        env.Append(CPPDEFINES=['BUILD_GPU'])
12289651SAndreas.Sandberg@ARM.com
12299651SAndreas.Sandberg@ARM.com    # Warn about missing optional functionality
12309651SAndreas.Sandberg@ARM.com    if env['USE_KVM']:
123110114Sandreas@sandberg.pp.se        if not main['HAVE_PERF_ATTR_EXCLUDE_HOST']:
12329651SAndreas.Sandberg@ARM.com            print("Warning: perf_event headers lack support for the "
123310114Sandreas@sandberg.pp.se                  "exclude_host attribute. KVM instruction counts will "
12349892Sandreas@sandberg.pp.se                  "be inaccurate.")
12359892Sandreas@sandberg.pp.se
12369753Sandreas@sandberg.pp.se    # Save sticky variable settings back to current variables file
123710073Sandreas@sandberg.pp.se    sticky_vars.Save(current_vars_file, env)
12389753Sandreas@sandberg.pp.se
12399753Sandreas@sandberg.pp.se    if env['USE_SSE2']:
12409753Sandreas@sandberg.pp.se        env.Append(CCFLAGS=['-msse2'])
124110114Sandreas@sandberg.pp.se
124210114Sandreas@sandberg.pp.se    # The src/SConscript file sets up the build rules in 'env' according
124310114Sandreas@sandberg.pp.se    # to the configured variables.  It returns a list of environments,
124410114Sandreas@sandberg.pp.se    # one for each variant build (debug, opt, etc.)
12459753Sandreas@sandberg.pp.se    SConscript('src/SConscript', variant_dir = variant_path, exports = 'env')
12469753Sandreas@sandberg.pp.se
12479892Sandreas@sandberg.pp.se# base help text
12489753Sandreas@sandberg.pp.seHelp('''
124910114Sandreas@sandberg.pp.seUsage: scons [scons options] [build variables] [target(s)]
125010073Sandreas@sandberg.pp.se
12519892Sandreas@sandberg.pp.seExtra scons options:
12529753Sandreas@sandberg.pp.se%(options)s
12539753Sandreas@sandberg.pp.se
12549753Sandreas@sandberg.pp.seGlobal build variables:
12559753Sandreas@sandberg.pp.se%(global_vars)s
12569753Sandreas@sandberg.pp.se
12579753Sandreas@sandberg.pp.se%(local_vars)s
12589753Sandreas@sandberg.pp.se''' % help_texts)
12599753Sandreas@sandberg.pp.se