SConstruct revision 10196
1955SN/A# -*- mode:python -*-
2955SN/A
37816Ssteve.reinhardt@amd.com# Copyright (c) 2013 ARM Limited
45871Snate@binkert.org# All rights reserved.
51762SN/A#
6955SN/A# The license below extends only to copyright in the software and shall
7955SN/A# not be construed as granting a license to any other intellectual
8955SN/A# property including but not limited to intellectual property relating
9955SN/A# to a hardware implementation of the functionality of the software
10955SN/A# licensed hereunder.  You may use the software subject to the license
11955SN/A# terms below provided that you ensure that this notice is replicated
12955SN/A# unmodified and in its entirety in all distributions of the software,
13955SN/A# modified or unmodified, in source code or in binary form.
14955SN/A#
15955SN/A# Copyright (c) 2011 Advanced Micro Devices, Inc.
16955SN/A# Copyright (c) 2009 The Hewlett-Packard Development Company
17955SN/A# Copyright (c) 2004-2005 The Regents of The University of Michigan
18955SN/A# All rights reserved.
19955SN/A#
20955SN/A# Redistribution and use in source and binary forms, with or without
21955SN/A# modification, are permitted provided that the following conditions are
22955SN/A# met: redistributions of source code must retain the above copyright
23955SN/A# notice, this list of conditions and the following disclaimer;
24955SN/A# redistributions in binary form must reproduce the above copyright
25955SN/A# notice, this list of conditions and the following disclaimer in the
26955SN/A# documentation and/or other materials provided with the distribution;
27955SN/A# neither the name of the copyright holders nor the names of its
28955SN/A# contributors may be used to endorse or promote products derived from
29955SN/A# this software without specific prior written permission.
302665Ssaidi@eecs.umich.edu#
312665Ssaidi@eecs.umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
325863Snate@binkert.org# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33955SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34955SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35955SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36955SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37955SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
388878Ssteve.reinhardt@amd.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
392632Sstever@eecs.umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
408878Ssteve.reinhardt@amd.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
412632Sstever@eecs.umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42955SN/A#
438878Ssteve.reinhardt@amd.com# Authors: Steve Reinhardt
442632Sstever@eecs.umich.edu#          Nathan Binkert
452761Sstever@eecs.umich.edu
462632Sstever@eecs.umich.edu###################################################
472632Sstever@eecs.umich.edu#
482632Sstever@eecs.umich.edu# SCons top-level build description (SConstruct) file.
492761Sstever@eecs.umich.edu#
502761Sstever@eecs.umich.edu# While in this directory ('gem5'), just type 'scons' to build the default
512761Sstever@eecs.umich.edu# configuration (see below), or type 'scons build/<CONFIG>/<binary>'
528878Ssteve.reinhardt@amd.com# to build some other configuration (e.g., 'build/ALPHA/gem5.opt' for
538878Ssteve.reinhardt@amd.com# the optimized full-system version).
542761Sstever@eecs.umich.edu#
552761Sstever@eecs.umich.edu# You can build gem5 in a different directory as long as there is a
562761Sstever@eecs.umich.edu# 'build/<CONFIG>' somewhere along the target path.  The build system
572761Sstever@eecs.umich.edu# expects that all configs under the same build directory are being
582761Sstever@eecs.umich.edu# built for the same host system.
598878Ssteve.reinhardt@amd.com#
608878Ssteve.reinhardt@amd.com# Examples:
612632Sstever@eecs.umich.edu#
622632Sstever@eecs.umich.edu#   The following two commands are equivalent.  The '-u' option tells
638878Ssteve.reinhardt@amd.com#   scons to search up the directory tree for this SConstruct file.
648878Ssteve.reinhardt@amd.com#   % cd <path-to-src>/gem5 ; scons build/ALPHA/gem5.debug
652632Sstever@eecs.umich.edu#   % cd <path-to-src>/gem5/build/ALPHA; scons -u gem5.debug
66955SN/A#
67955SN/A#   The following two commands are equivalent and demonstrate building
68955SN/A#   in a directory outside of the source tree.  The '-C' option tells
695863Snate@binkert.org#   scons to chdir to the specified directory to find this SConstruct
705863Snate@binkert.org#   file.
715863Snate@binkert.org#   % cd <path-to-src>/gem5 ; scons /local/foo/build/ALPHA/gem5.debug
725863Snate@binkert.org#   % cd /local/foo/build/ALPHA; scons -C <path-to-src>/gem5 gem5.debug
735863Snate@binkert.org#
745863Snate@binkert.org# You can use 'scons -H' to print scons options.  If you're in this
755863Snate@binkert.org# 'gem5' directory (or use -u or -C to tell scons where to find this
765863Snate@binkert.org# file), you can use 'scons -h' to print all the gem5-specific build
775863Snate@binkert.org# options as well.
785863Snate@binkert.org#
795863Snate@binkert.org###################################################
808878Ssteve.reinhardt@amd.com
815863Snate@binkert.org# Check for recent-enough Python and SCons versions.
825863Snate@binkert.orgtry:
835863Snate@binkert.org    # Really old versions of scons only take two options for the
845863Snate@binkert.org    # function, so check once without the revision and once with the
855863Snate@binkert.org    # revision, the first instance will fail for stuff other than
865863Snate@binkert.org    # 0.98, and the second will fail for 0.98.0
875863Snate@binkert.org    EnsureSConsVersion(0, 98)
885863Snate@binkert.org    EnsureSConsVersion(0, 98, 1)
895863Snate@binkert.orgexcept SystemExit, e:
905863Snate@binkert.org    print """
915863Snate@binkert.orgFor more details, see:
925863Snate@binkert.org    http://gem5.org/Dependencies
935863Snate@binkert.org"""
945863Snate@binkert.org    raise
955863Snate@binkert.org
968878Ssteve.reinhardt@amd.com# We ensure the python version early because because python-config
975863Snate@binkert.org# requires python 2.5
985863Snate@binkert.orgtry:
995863Snate@binkert.org    EnsurePythonVersion(2, 5)
1006654Snate@binkert.orgexcept SystemExit, e:
101955SN/A    print """
1025396Ssaidi@eecs.umich.eduYou can use a non-default installation of the Python interpreter by
1035863Snate@binkert.orgrearranging your PATH so that scons finds the non-default 'python' and
1045863Snate@binkert.org'python-config' first.
1054202Sbinkertn@umich.edu
1065863Snate@binkert.orgFor more details, see:
1075863Snate@binkert.org    http://gem5.org/wiki/index.php/Using_a_non-default_Python_installation
1085863Snate@binkert.org"""
1095863Snate@binkert.org    raise
110955SN/A
1116654Snate@binkert.org# Global Python includes
1125273Sstever@gmail.comimport itertools
1135871Snate@binkert.orgimport os
1145273Sstever@gmail.comimport re
1156655Snate@binkert.orgimport subprocess
1168878Ssteve.reinhardt@amd.comimport sys
1176655Snate@binkert.org
1186655Snate@binkert.orgfrom os import mkdir, environ
1196655Snate@binkert.orgfrom os.path import abspath, basename, dirname, expanduser, normpath
1206655Snate@binkert.orgfrom os.path import exists,  isdir, isfile
1215871Snate@binkert.orgfrom os.path import join as joinpath, split as splitpath
1226654Snate@binkert.org
1235396Ssaidi@eecs.umich.edu# SCons includes
1248120Sgblack@eecs.umich.eduimport SCons
1258120Sgblack@eecs.umich.eduimport SCons.Node
1268120Sgblack@eecs.umich.edu
1278120Sgblack@eecs.umich.eduextra_python_paths = [
1288120Sgblack@eecs.umich.edu    Dir('src/python').srcnode().abspath, # gem5 includes
1298120Sgblack@eecs.umich.edu    Dir('ext/ply').srcnode().abspath, # ply is used by several files
1308120Sgblack@eecs.umich.edu    ]
1318120Sgblack@eecs.umich.edu
1328120Sgblack@eecs.umich.edusys.path[1:1] = extra_python_paths
1338120Sgblack@eecs.umich.edu
1348120Sgblack@eecs.umich.edufrom m5.util import compareVersions, readCommand
1358120Sgblack@eecs.umich.edufrom m5.util.terminal import get_termcap
1368120Sgblack@eecs.umich.edu
1378120Sgblack@eecs.umich.eduhelp_texts = {
1388120Sgblack@eecs.umich.edu    "options" : "",
1398120Sgblack@eecs.umich.edu    "global_vars" : "",
1408120Sgblack@eecs.umich.edu    "local_vars" : ""
1418120Sgblack@eecs.umich.edu}
1428120Sgblack@eecs.umich.edu
1438120Sgblack@eecs.umich.eduExport("help_texts")
1448120Sgblack@eecs.umich.edu
1458120Sgblack@eecs.umich.edu
1468120Sgblack@eecs.umich.edu# There's a bug in scons in that (1) by default, the help texts from
1478120Sgblack@eecs.umich.edu# AddOption() are supposed to be displayed when you type 'scons -h'
1488120Sgblack@eecs.umich.edu# and (2) you can override the help displayed by 'scons -h' using the
1498120Sgblack@eecs.umich.edu# Help() function, but these two features are incompatible: once
1508120Sgblack@eecs.umich.edu# you've overridden the help text using Help(), there's no way to get
1518120Sgblack@eecs.umich.edu# at the help texts from AddOptions.  See:
1528120Sgblack@eecs.umich.edu#     http://scons.tigris.org/issues/show_bug.cgi?id=2356
1538120Sgblack@eecs.umich.edu#     http://scons.tigris.org/issues/show_bug.cgi?id=2611
1548120Sgblack@eecs.umich.edu# This hack lets us extract the help text from AddOptions and
1558120Sgblack@eecs.umich.edu# re-inject it via Help().  Ideally someday this bug will be fixed and
1568120Sgblack@eecs.umich.edu# we can just use AddOption directly.
1578120Sgblack@eecs.umich.edudef AddLocalOption(*args, **kwargs):
1588120Sgblack@eecs.umich.edu    col_width = 30
1598120Sgblack@eecs.umich.edu
1607816Ssteve.reinhardt@amd.com    help = "  " + ", ".join(args)
1617816Ssteve.reinhardt@amd.com    if "help" in kwargs:
1627816Ssteve.reinhardt@amd.com        length = len(help)
1637816Ssteve.reinhardt@amd.com        if length >= col_width:
1647816Ssteve.reinhardt@amd.com            help += "\n" + " " * col_width
1657816Ssteve.reinhardt@amd.com        else:
1667816Ssteve.reinhardt@amd.com            help += " " * (col_width - length)
1677816Ssteve.reinhardt@amd.com        help += kwargs["help"]
1687816Ssteve.reinhardt@amd.com    help_texts["options"] += help + "\n"
1695871Snate@binkert.org
1705871Snate@binkert.org    AddOption(*args, **kwargs)
1716121Snate@binkert.org
1725871Snate@binkert.orgAddLocalOption('--colors', dest='use_colors', action='store_true',
1735871Snate@binkert.org               help="Add color to abbreviated scons output")
1746003Snate@binkert.orgAddLocalOption('--no-colors', dest='use_colors', action='store_false',
1756655Snate@binkert.org               help="Don't add color to abbreviated scons output")
176955SN/AAddLocalOption('--default', dest='default', type='string', action='store',
1775871Snate@binkert.org               help='Override which build_opts file to use for defaults')
1785871Snate@binkert.orgAddLocalOption('--ignore-style', dest='ignore_style', action='store_true',
1795871Snate@binkert.org               help='Disable style checking hooks')
1805871Snate@binkert.orgAddLocalOption('--no-lto', dest='no_lto', action='store_true',
181955SN/A               help='Disable Link-Time Optimization for fast')
1826121Snate@binkert.orgAddLocalOption('--update-ref', dest='update_ref', action='store_true',
1836121Snate@binkert.org               help='Update test reference outputs')
1846121Snate@binkert.orgAddLocalOption('--verbose', dest='verbose', action='store_true',
1851533SN/A               help='Print full tool command lines')
1866655Snate@binkert.org
1876655Snate@binkert.orgtermcap = get_termcap(GetOption('use_colors'))
1886655Snate@binkert.org
1896655Snate@binkert.org########################################################################
1905871Snate@binkert.org#
1915871Snate@binkert.org# Set up the main build environment.
1925863Snate@binkert.org#
1935871Snate@binkert.org########################################################################
1948878Ssteve.reinhardt@amd.com
1955871Snate@binkert.org# export TERM so that clang reports errors in color
1965871Snate@binkert.orguse_vars = set([ 'AS', 'AR', 'CC', 'CXX', 'HOME', 'LD_LIBRARY_PATH',
1975871Snate@binkert.org                 'LIBRARY_PATH', 'PATH', 'PKG_CONFIG_PATH', 'PROTOC',
1985863Snate@binkert.org                 'PYTHONPATH', 'RANLIB', 'SWIG', 'TERM' ])
1996121Snate@binkert.org
2005863Snate@binkert.orguse_prefixes = [
2015871Snate@binkert.org    "M5",           # M5 configuration (e.g., path to kernels)
2028336Ssteve.reinhardt@amd.com    "DISTCC_",      # distcc (distributed compiler wrapper) configuration
2038336Ssteve.reinhardt@amd.com    "CCACHE_",      # ccache (caching compiler wrapper) configuration
2048336Ssteve.reinhardt@amd.com    "CCC_",         # clang static analyzer configuration
2058336Ssteve.reinhardt@amd.com    ]
2064678Snate@binkert.org
2078336Ssteve.reinhardt@amd.comuse_env = {}
2088336Ssteve.reinhardt@amd.comfor key,val in os.environ.iteritems():
2098336Ssteve.reinhardt@amd.com    if key in use_vars or \
2104678Snate@binkert.org            any([key.startswith(prefix) for prefix in use_prefixes]):
2114678Snate@binkert.org        use_env[key] = val
2124678Snate@binkert.org
2134678Snate@binkert.orgmain = Environment(ENV=use_env)
2147827Snate@binkert.orgmain.Decider('MD5-timestamp')
2157827Snate@binkert.orgmain.root = Dir(".")         # The current directory (where this file lives).
2168336Ssteve.reinhardt@amd.commain.srcdir = Dir("src")     # The source directory
2174678Snate@binkert.org
2188336Ssteve.reinhardt@amd.commain_dict_keys = main.Dictionary().keys()
2198336Ssteve.reinhardt@amd.com
2208336Ssteve.reinhardt@amd.com# Check that we have a C/C++ compiler
2218336Ssteve.reinhardt@amd.comif not ('CC' in main_dict_keys and 'CXX' in main_dict_keys):
2228336Ssteve.reinhardt@amd.com    print "No C++ compiler installed (package g++ on Ubuntu and RedHat)"
2238336Ssteve.reinhardt@amd.com    Exit(1)
2245871Snate@binkert.org
2255871Snate@binkert.org# Check that swig is present
2268336Ssteve.reinhardt@amd.comif not 'SWIG' in main_dict_keys:
2278336Ssteve.reinhardt@amd.com    print "swig is not installed (package swig on Ubuntu and RedHat)"
2288336Ssteve.reinhardt@amd.com    Exit(1)
2298336Ssteve.reinhardt@amd.com
2308336Ssteve.reinhardt@amd.com# add useful python code PYTHONPATH so it can be used by subprocesses
2315871Snate@binkert.org# as well
2328336Ssteve.reinhardt@amd.commain.AppendENVPath('PYTHONPATH', extra_python_paths)
2338336Ssteve.reinhardt@amd.com
2348336Ssteve.reinhardt@amd.com########################################################################
2358336Ssteve.reinhardt@amd.com#
2368336Ssteve.reinhardt@amd.com# Mercurial Stuff.
2374678Snate@binkert.org#
2385871Snate@binkert.org# If the gem5 directory is a mercurial repository, we should do some
2394678Snate@binkert.org# extra things.
2408336Ssteve.reinhardt@amd.com#
2418336Ssteve.reinhardt@amd.com########################################################################
2428336Ssteve.reinhardt@amd.com
2438336Ssteve.reinhardt@amd.comhgdir = main.root.Dir(".hg")
2448336Ssteve.reinhardt@amd.com
2458336Ssteve.reinhardt@amd.commercurial_style_message = """
2468336Ssteve.reinhardt@amd.comYou're missing the gem5 style hook, which automatically checks your code
2478336Ssteve.reinhardt@amd.comagainst the gem5 style rules on hg commit and qrefresh commands.  This
2488336Ssteve.reinhardt@amd.comscript will now install the hook in your .hg/hgrc file.
2498336Ssteve.reinhardt@amd.comPress enter to continue, or ctrl-c to abort: """
2508336Ssteve.reinhardt@amd.com
2518336Ssteve.reinhardt@amd.commercurial_style_hook = """
2528336Ssteve.reinhardt@amd.com# The following lines were automatically added by gem5/SConstruct
2538336Ssteve.reinhardt@amd.com# to provide the gem5 style-checking hooks
2548336Ssteve.reinhardt@amd.com[extensions]
2558336Ssteve.reinhardt@amd.comstyle = %s/util/style.py
2568336Ssteve.reinhardt@amd.com
2575871Snate@binkert.org[hooks]
2586121Snate@binkert.orgpretxncommit.style = python:style.check_style
259955SN/Apre-qrefresh.style = python:style.check_style
260955SN/A# End of SConstruct additions
2612632Sstever@eecs.umich.edu
2622632Sstever@eecs.umich.edu""" % (main.root.abspath)
263955SN/A
264955SN/Amercurial_lib_not_found = """
265955SN/AMercurial libraries cannot be found, ignoring style hook.  If
266955SN/Ayou are a gem5 developer, please fix this and run the style
2678878Ssteve.reinhardt@amd.comhook. It is important.
268955SN/A"""
2692632Sstever@eecs.umich.edu
2702632Sstever@eecs.umich.edu# Check for style hook and prompt for installation if it's not there.
2712632Sstever@eecs.umich.edu# Skip this if --ignore-style was specified, there's no .hg dir to
2722632Sstever@eecs.umich.edu# install a hook in, or there's no interactive terminal to prompt.
2732632Sstever@eecs.umich.eduif not GetOption('ignore_style') and hgdir.exists() and sys.stdin.isatty():
2742632Sstever@eecs.umich.edu    style_hook = True
2752632Sstever@eecs.umich.edu    try:
2768268Ssteve.reinhardt@amd.com        from mercurial import ui
2778268Ssteve.reinhardt@amd.com        ui = ui.ui()
2788268Ssteve.reinhardt@amd.com        ui.readconfig(hgdir.File('hgrc').abspath)
2798268Ssteve.reinhardt@amd.com        style_hook = ui.config('hooks', 'pretxncommit.style', None) and \
2808268Ssteve.reinhardt@amd.com                     ui.config('hooks', 'pre-qrefresh.style', None)
2818268Ssteve.reinhardt@amd.com    except ImportError:
2828268Ssteve.reinhardt@amd.com        print mercurial_lib_not_found
2832632Sstever@eecs.umich.edu
2842632Sstever@eecs.umich.edu    if not style_hook:
2852632Sstever@eecs.umich.edu        print mercurial_style_message,
2862632Sstever@eecs.umich.edu        # continue unless user does ctrl-c/ctrl-d etc.
2878268Ssteve.reinhardt@amd.com        try:
2882632Sstever@eecs.umich.edu            raw_input()
2898268Ssteve.reinhardt@amd.com        except:
2908268Ssteve.reinhardt@amd.com            print "Input exception, exiting scons.\n"
2918268Ssteve.reinhardt@amd.com            sys.exit(1)
2928268Ssteve.reinhardt@amd.com        hgrc_path = '%s/.hg/hgrc' % main.root.abspath
2933718Sstever@eecs.umich.edu        print "Adding style hook to", hgrc_path, "\n"
2942634Sstever@eecs.umich.edu        try:
2952634Sstever@eecs.umich.edu            hgrc = open(hgrc_path, 'a')
2965863Snate@binkert.org            hgrc.write(mercurial_style_hook)
2972638Sstever@eecs.umich.edu            hgrc.close()
2988268Ssteve.reinhardt@amd.com        except:
2992632Sstever@eecs.umich.edu            print "Error updating", hgrc_path
3002632Sstever@eecs.umich.edu            sys.exit(1)
3012632Sstever@eecs.umich.edu
3022632Sstever@eecs.umich.edu
3032632Sstever@eecs.umich.edu###################################################
3041858SN/A#
3053716Sstever@eecs.umich.edu# Figure out which configurations to set up based on the path(s) of
3062638Sstever@eecs.umich.edu# the target(s).
3072638Sstever@eecs.umich.edu#
3082638Sstever@eecs.umich.edu###################################################
3092638Sstever@eecs.umich.edu
3102638Sstever@eecs.umich.edu# Find default configuration & binary.
3112638Sstever@eecs.umich.eduDefault(environ.get('M5_DEFAULT_BINARY', 'build/ALPHA/gem5.debug'))
3122638Sstever@eecs.umich.edu
3135863Snate@binkert.org# helper function: find last occurrence of element in list
3145863Snate@binkert.orgdef rfind(l, elt, offs = -1):
3155863Snate@binkert.org    for i in range(len(l)+offs, 0, -1):
316955SN/A        if l[i] == elt:
3175341Sstever@gmail.com            return i
3185341Sstever@gmail.com    raise ValueError, "element not found"
3195863Snate@binkert.org
3207756SAli.Saidi@ARM.com# Take a list of paths (or SCons Nodes) and return a list with all
3215341Sstever@gmail.com# paths made absolute and ~-expanded.  Paths will be interpreted
3226121Snate@binkert.org# relative to the launch directory unless a different root is provided
3234494Ssaidi@eecs.umich.edudef makePathListAbsolute(path_list, root=GetLaunchDir()):
3246121Snate@binkert.org    return [abspath(joinpath(root, expanduser(str(p))))
3251105SN/A            for p in path_list]
3262667Sstever@eecs.umich.edu
3272667Sstever@eecs.umich.edu# Each target must have 'build' in the interior of the path; the
3282667Sstever@eecs.umich.edu# directory below this will determine the build parameters.  For
3292667Sstever@eecs.umich.edu# example, for target 'foo/bar/build/ALPHA_SE/arch/alpha/blah.do' we
3306121Snate@binkert.org# recognize that ALPHA_SE specifies the configuration because it
3312667Sstever@eecs.umich.edu# follow 'build' in the build path.
3325341Sstever@gmail.com
3335863Snate@binkert.org# The funky assignment to "[:]" is needed to replace the list contents
3345341Sstever@gmail.com# in place rather than reassign the symbol to a new list, which
3355341Sstever@gmail.com# doesn't work (obviously!).
3365341Sstever@gmail.comBUILD_TARGETS[:] = makePathListAbsolute(BUILD_TARGETS)
3378120Sgblack@eecs.umich.edu
3385341Sstever@gmail.com# Generate a list of the unique build roots and configs that the
3398120Sgblack@eecs.umich.edu# collected targets reference.
3405341Sstever@gmail.comvariant_paths = []
3418120Sgblack@eecs.umich.edubuild_root = None
3426121Snate@binkert.orgfor t in BUILD_TARGETS:
3436121Snate@binkert.org    path_dirs = t.split('/')
3445397Ssaidi@eecs.umich.edu    try:
3455397Ssaidi@eecs.umich.edu        build_top = rfind(path_dirs, 'build', -2)
3467727SAli.Saidi@ARM.com    except:
3478268Ssteve.reinhardt@amd.com        print "Error: no non-leaf 'build' dir found on target path", t
3486168Snate@binkert.org        Exit(1)
3495341Sstever@gmail.com    this_build_root = joinpath('/',*path_dirs[:build_top+1])
3508120Sgblack@eecs.umich.edu    if not build_root:
3518120Sgblack@eecs.umich.edu        build_root = this_build_root
3528120Sgblack@eecs.umich.edu    else:
3536814Sgblack@eecs.umich.edu        if this_build_root != build_root:
3545863Snate@binkert.org            print "Error: build targets not under same build root\n"\
3558120Sgblack@eecs.umich.edu                  "  %s\n  %s" % (build_root, this_build_root)
3565341Sstever@gmail.com            Exit(1)
3575863Snate@binkert.org    variant_path = joinpath('/',*path_dirs[:build_top+2])
3588268Ssteve.reinhardt@amd.com    if variant_path not in variant_paths:
3596121Snate@binkert.org        variant_paths.append(variant_path)
3606121Snate@binkert.org
3618268Ssteve.reinhardt@amd.com# Make sure build_root exists (might not if this is the first build there)
3625742Snate@binkert.orgif not isdir(build_root):
3635742Snate@binkert.org    mkdir(build_root)
3645341Sstever@gmail.commain['BUILDROOT'] = build_root
3655742Snate@binkert.org
3665742Snate@binkert.orgExport('main')
3675341Sstever@gmail.com
3686017Snate@binkert.orgmain.SConsignFile(joinpath(build_root, "sconsign"))
3696121Snate@binkert.org
3706017Snate@binkert.org# Default duplicate option is to use hard links, but this messes up
3717816Ssteve.reinhardt@amd.com# when you use emacs to edit a file in the target dir, as emacs moves
3727756SAli.Saidi@ARM.com# file to file~ then copies to file, breaking the link.  Symbolic
3737756SAli.Saidi@ARM.com# (soft) links work better.
3747756SAli.Saidi@ARM.commain.SetOption('duplicate', 'soft-copy')
3757756SAli.Saidi@ARM.com
3767756SAli.Saidi@ARM.com#
3777756SAli.Saidi@ARM.com# Set up global sticky variables... these are common to an entire build
3787756SAli.Saidi@ARM.com# tree (not specific to a particular build like ALPHA_SE)
3797756SAli.Saidi@ARM.com#
3807816Ssteve.reinhardt@amd.com
3817816Ssteve.reinhardt@amd.comglobal_vars_file = joinpath(build_root, 'variables.global')
3827816Ssteve.reinhardt@amd.com
3837816Ssteve.reinhardt@amd.comglobal_vars = Variables(global_vars_file, args=ARGUMENTS)
3847816Ssteve.reinhardt@amd.com
3857816Ssteve.reinhardt@amd.comglobal_vars.AddVariables(
3867816Ssteve.reinhardt@amd.com    ('CC', 'C compiler', environ.get('CC', main['CC'])),
3877816Ssteve.reinhardt@amd.com    ('CXX', 'C++ compiler', environ.get('CXX', main['CXX'])),
3887816Ssteve.reinhardt@amd.com    ('SWIG', 'SWIG tool', environ.get('SWIG', main['SWIG'])),
3897816Ssteve.reinhardt@amd.com    ('PROTOC', 'protoc tool', environ.get('PROTOC', 'protoc')),
3907756SAli.Saidi@ARM.com    ('BATCH', 'Use batch pool for build and tests', False),
3917816Ssteve.reinhardt@amd.com    ('BATCH_CMD', 'Batch pool submission command name', 'qdo'),
3927816Ssteve.reinhardt@amd.com    ('M5_BUILD_CACHE', 'Cache built objects in this directory', False),
3937816Ssteve.reinhardt@amd.com    ('EXTRAS', 'Add extra directories to the compilation', '')
3947816Ssteve.reinhardt@amd.com    )
3957816Ssteve.reinhardt@amd.com
3967816Ssteve.reinhardt@amd.com# Update main environment with values from ARGUMENTS & global_vars_file
3977816Ssteve.reinhardt@amd.comglobal_vars.Update(main)
3987816Ssteve.reinhardt@amd.comhelp_texts["global_vars"] += global_vars.GenerateHelpText(main)
3997816Ssteve.reinhardt@amd.com
4007816Ssteve.reinhardt@amd.com# Save sticky variable settings back to current variables file
4017816Ssteve.reinhardt@amd.comglobal_vars.Save(global_vars_file, main)
4027816Ssteve.reinhardt@amd.com
4037816Ssteve.reinhardt@amd.com# Parse EXTRAS variable to build list of all directories where we're
4047816Ssteve.reinhardt@amd.com# look for sources etc.  This list is exported as extras_dir_list.
4057816Ssteve.reinhardt@amd.combase_dir = main.srcdir.abspath
4067816Ssteve.reinhardt@amd.comif main['EXTRAS']:
4077816Ssteve.reinhardt@amd.com    extras_dir_list = makePathListAbsolute(main['EXTRAS'].split(':'))
4087816Ssteve.reinhardt@amd.comelse:
4097816Ssteve.reinhardt@amd.com    extras_dir_list = []
4107816Ssteve.reinhardt@amd.com
4117816Ssteve.reinhardt@amd.comExport('base_dir')
4127816Ssteve.reinhardt@amd.comExport('extras_dir_list')
4137816Ssteve.reinhardt@amd.com
4147816Ssteve.reinhardt@amd.com# the ext directory should be on the #includes path
4157816Ssteve.reinhardt@amd.commain.Append(CPPPATH=[Dir('ext')])
4167816Ssteve.reinhardt@amd.com
4177816Ssteve.reinhardt@amd.comdef strip_build_path(path, env):
4187816Ssteve.reinhardt@amd.com    path = str(path)
4197816Ssteve.reinhardt@amd.com    variant_base = env['BUILDROOT'] + os.path.sep
4207816Ssteve.reinhardt@amd.com    if path.startswith(variant_base):
4217816Ssteve.reinhardt@amd.com        path = path[len(variant_base):]
4227816Ssteve.reinhardt@amd.com    elif path.startswith('build/'):
4237816Ssteve.reinhardt@amd.com        path = path[6:]
4247816Ssteve.reinhardt@amd.com    return path
4257816Ssteve.reinhardt@amd.com
4267816Ssteve.reinhardt@amd.com# Generate a string of the form:
4277816Ssteve.reinhardt@amd.com#   common/path/prefix/src1, src2 -> tgt1, tgt2
4287816Ssteve.reinhardt@amd.com# to print while building.
4297816Ssteve.reinhardt@amd.comclass Transform(object):
4307816Ssteve.reinhardt@amd.com    # all specific color settings should be here and nowhere else
4317816Ssteve.reinhardt@amd.com    tool_color = termcap.Normal
4327816Ssteve.reinhardt@amd.com    pfx_color = termcap.Yellow
4337816Ssteve.reinhardt@amd.com    srcs_color = termcap.Yellow + termcap.Bold
4347816Ssteve.reinhardt@amd.com    arrow_color = termcap.Blue + termcap.Bold
4357816Ssteve.reinhardt@amd.com    tgts_color = termcap.Yellow + termcap.Bold
4367816Ssteve.reinhardt@amd.com
4377816Ssteve.reinhardt@amd.com    def __init__(self, tool, max_sources=99):
4387816Ssteve.reinhardt@amd.com        self.format = self.tool_color + (" [%8s] " % tool) \
4397816Ssteve.reinhardt@amd.com                      + self.pfx_color + "%s" \
4407816Ssteve.reinhardt@amd.com                      + self.srcs_color + "%s" \
4417816Ssteve.reinhardt@amd.com                      + self.arrow_color + " -> " \
4427816Ssteve.reinhardt@amd.com                      + self.tgts_color + "%s" \
4437816Ssteve.reinhardt@amd.com                      + termcap.Normal
4447816Ssteve.reinhardt@amd.com        self.max_sources = max_sources
4457816Ssteve.reinhardt@amd.com
4467816Ssteve.reinhardt@amd.com    def __call__(self, target, source, env, for_signature=None):
4477816Ssteve.reinhardt@amd.com        # truncate source list according to max_sources param
4487816Ssteve.reinhardt@amd.com        source = source[0:self.max_sources]
4497816Ssteve.reinhardt@amd.com        def strip(f):
4507816Ssteve.reinhardt@amd.com            return strip_build_path(str(f), env)
4517816Ssteve.reinhardt@amd.com        if len(source) > 0:
4527756SAli.Saidi@ARM.com            srcs = map(strip, source)
4538120Sgblack@eecs.umich.edu        else:
4547756SAli.Saidi@ARM.com            srcs = ['']
4557756SAli.Saidi@ARM.com        tgts = map(strip, target)
4567756SAli.Saidi@ARM.com        # surprisingly, os.path.commonprefix is a dumb char-by-char string
4577756SAli.Saidi@ARM.com        # operation that has nothing to do with paths.
4587816Ssteve.reinhardt@amd.com        com_pfx = os.path.commonprefix(srcs + tgts)
4597816Ssteve.reinhardt@amd.com        com_pfx_len = len(com_pfx)
4607816Ssteve.reinhardt@amd.com        if com_pfx:
4617816Ssteve.reinhardt@amd.com            # do some cleanup and sanity checking on common prefix
4627816Ssteve.reinhardt@amd.com            if com_pfx[-1] == ".":
4637816Ssteve.reinhardt@amd.com                # prefix matches all but file extension: ok
4647816Ssteve.reinhardt@amd.com                # back up one to change 'foo.cc -> o' to 'foo.cc -> .o'
4657816Ssteve.reinhardt@amd.com                com_pfx = com_pfx[0:-1]
4667816Ssteve.reinhardt@amd.com            elif com_pfx[-1] == "/":
4677816Ssteve.reinhardt@amd.com                # common prefix is directory path: OK
4687756SAli.Saidi@ARM.com                pass
4697756SAli.Saidi@ARM.com            else:
4706654Snate@binkert.org                src0_len = len(srcs[0])
4716654Snate@binkert.org                tgt0_len = len(tgts[0])
4725871Snate@binkert.org                if src0_len == com_pfx_len:
4736121Snate@binkert.org                    # source is a substring of target, OK
4746121Snate@binkert.org                    pass
4756121Snate@binkert.org                elif tgt0_len == com_pfx_len:
4768737Skoansin.tan@gmail.com                    # target is a substring of source, need to back up to
4778737Skoansin.tan@gmail.com                    # avoid empty string on RHS of arrow
4783940Ssaidi@eecs.umich.edu                    sep_idx = com_pfx.rfind(".")
4793918Ssaidi@eecs.umich.edu                    if sep_idx != -1:
4803918Ssaidi@eecs.umich.edu                        com_pfx = com_pfx[0:sep_idx]
4811858SN/A                    else:
4826121Snate@binkert.org                        com_pfx = ''
4837739Sgblack@eecs.umich.edu                elif src0_len > com_pfx_len and srcs[0][com_pfx_len] == ".":
4847739Sgblack@eecs.umich.edu                    # still splitting at file extension: ok
4856143Snate@binkert.org                    pass
4867739Sgblack@eecs.umich.edu                else:
4877618SAli.Saidi@arm.com                    # probably a fluke; ignore it
4887618SAli.Saidi@arm.com                    com_pfx = ''
4897618SAli.Saidi@arm.com        # recalculate length in case com_pfx was modified
4907618SAli.Saidi@arm.com        com_pfx_len = len(com_pfx)
4918614Sgblack@eecs.umich.edu        def fmt(files):
4927618SAli.Saidi@arm.com            f = map(lambda s: s[com_pfx_len:], files)
4937618SAli.Saidi@arm.com            return ', '.join(f)
4947618SAli.Saidi@arm.com        return self.format % (com_pfx, fmt(srcs), fmt(tgts))
4957739Sgblack@eecs.umich.edu
4966121Snate@binkert.orgExport('Transform')
4973940Ssaidi@eecs.umich.edu
4986121Snate@binkert.org# enable the regression script to use the termcap
4997739Sgblack@eecs.umich.edumain['TERMCAP'] = termcap
5007739Sgblack@eecs.umich.edu
5017739Sgblack@eecs.umich.eduif GetOption('verbose'):
5027739Sgblack@eecs.umich.edu    def MakeAction(action, string, *args, **kwargs):
5037739Sgblack@eecs.umich.edu        return Action(action, *args, **kwargs)
5047739Sgblack@eecs.umich.eduelse:
5058737Skoansin.tan@gmail.com    MakeAction = Action
5068737Skoansin.tan@gmail.com    main['CCCOMSTR']        = Transform("CC")
5078737Skoansin.tan@gmail.com    main['CXXCOMSTR']       = Transform("CXX")
5088737Skoansin.tan@gmail.com    main['ASCOMSTR']        = Transform("AS")
5098737Skoansin.tan@gmail.com    main['SWIGCOMSTR']      = Transform("SWIG")
5108737Skoansin.tan@gmail.com    main['ARCOMSTR']        = Transform("AR", 0)
5118737Skoansin.tan@gmail.com    main['LINKCOMSTR']      = Transform("LINK", 0)
5128737Skoansin.tan@gmail.com    main['RANLIBCOMSTR']    = Transform("RANLIB", 0)
5138737Skoansin.tan@gmail.com    main['M4COMSTR']        = Transform("M4")
5148737Skoansin.tan@gmail.com    main['SHCCCOMSTR']      = Transform("SHCC")
5158737Skoansin.tan@gmail.com    main['SHCXXCOMSTR']     = Transform("SHCXX")
5168737Skoansin.tan@gmail.comExport('MakeAction')
5178737Skoansin.tan@gmail.com
5188737Skoansin.tan@gmail.com# Initialize the Link-Time Optimization (LTO) flags
5198737Skoansin.tan@gmail.commain['LTO_CCFLAGS'] = []
5208737Skoansin.tan@gmail.commain['LTO_LDFLAGS'] = []
5218737Skoansin.tan@gmail.com
5228737Skoansin.tan@gmail.com# According to the readme, tcmalloc works best if the compiler doesn't
5233918Ssaidi@eecs.umich.edu# assume that we're using the builtin malloc and friends. These flags
5243918Ssaidi@eecs.umich.edu# are compiler-specific, so we need to set them after we detect which
5253940Ssaidi@eecs.umich.edu# compiler we're using.
5263918Ssaidi@eecs.umich.edumain['TCMALLOC_CCFLAGS'] = []
5273918Ssaidi@eecs.umich.edu
5286157Snate@binkert.orgCXX_version = readCommand([main['CXX'],'--version'], exception=False)
5296157Snate@binkert.orgCXX_V = readCommand([main['CXX'],'-V'], exception=False)
5306157Snate@binkert.org
5316157Snate@binkert.orgmain['GCC'] = CXX_version and CXX_version.find('g++') >= 0
5325397Ssaidi@eecs.umich.edumain['CLANG'] = CXX_version and CXX_version.find('clang') >= 0
5335397Ssaidi@eecs.umich.eduif main['GCC'] + main['CLANG'] > 1:
5346121Snate@binkert.org    print 'Error: How can we have two at the same time?'
5356121Snate@binkert.org    Exit(1)
5366121Snate@binkert.org
5376121Snate@binkert.org# Set up default C++ compiler flags
5386121Snate@binkert.orgif main['GCC'] or main['CLANG']:
5396121Snate@binkert.org    # As gcc and clang share many flags, do the common parts here
5405397Ssaidi@eecs.umich.edu    main.Append(CCFLAGS=['-pipe'])
5411851SN/A    main.Append(CCFLAGS=['-fno-strict-aliasing'])
5421851SN/A    # Enable -Wall and then disable the few warnings that we
5437739Sgblack@eecs.umich.edu    # consistently violate
544955SN/A    main.Append(CCFLAGS=['-Wall', '-Wno-sign-compare', '-Wundef'])
5453053Sstever@eecs.umich.edu    # We always compile using C++11, but only gcc >= 4.7 and clang 3.1
5466121Snate@binkert.org    # actually use that name, so we stick with c++0x
5473053Sstever@eecs.umich.edu    main.Append(CXXFLAGS=['-std=c++0x'])
5483053Sstever@eecs.umich.edu    # Add selected sanity checks from -Wextra
5493053Sstever@eecs.umich.edu    main.Append(CXXFLAGS=['-Wmissing-field-initializers',
5503053Sstever@eecs.umich.edu                          '-Woverloaded-virtual'])
5513053Sstever@eecs.umich.eduelse:
5526654Snate@binkert.org    print termcap.Yellow + termcap.Bold + 'Error' + termcap.Normal,
5533053Sstever@eecs.umich.edu    print "Don't know what compiler options to use for your compiler."
5544742Sstever@eecs.umich.edu    print termcap.Yellow + '       compiler:' + termcap.Normal, main['CXX']
5554742Sstever@eecs.umich.edu    print termcap.Yellow + '       version:' + termcap.Normal,
5563053Sstever@eecs.umich.edu    if not CXX_version:
5573053Sstever@eecs.umich.edu        print termcap.Yellow + termcap.Bold + "COMMAND NOT FOUND!" +\
5583053Sstever@eecs.umich.edu               termcap.Normal
5593053Sstever@eecs.umich.edu    else:
5606654Snate@binkert.org        print CXX_version.replace('\n', '<nl>')
5613053Sstever@eecs.umich.edu    print "       If you're trying to use a compiler other than GCC"
5623053Sstever@eecs.umich.edu    print "       or clang, there appears to be something wrong with your"
5633053Sstever@eecs.umich.edu    print "       environment."
5643053Sstever@eecs.umich.edu    print "       "
5652667Sstever@eecs.umich.edu    print "       If you are trying to use a compiler other than those listed"
5664554Sbinkertn@umich.edu    print "       above you will need to ease fix SConstruct and "
5676121Snate@binkert.org    print "       src/SConscript to support that compiler."
5682667Sstever@eecs.umich.edu    Exit(1)
5694554Sbinkertn@umich.edu
5704554Sbinkertn@umich.eduif main['GCC']:
5714554Sbinkertn@umich.edu    # Check for a supported version of gcc, >= 4.4 is needed for c++0x
5726121Snate@binkert.org    # support. See http://gcc.gnu.org/projects/cxx0x.html for details
5734554Sbinkertn@umich.edu    gcc_version = readCommand([main['CXX'], '-dumpversion'], exception=False)
5744554Sbinkertn@umich.edu    if compareVersions(gcc_version, "4.4") < 0:
5754554Sbinkertn@umich.edu        print 'Error: gcc version 4.4 or newer required.'
5764781Snate@binkert.org        print '       Installed version:', gcc_version
5774554Sbinkertn@umich.edu        Exit(1)
5784554Sbinkertn@umich.edu
5792667Sstever@eecs.umich.edu    main['GCC_VERSION'] = gcc_version
5804554Sbinkertn@umich.edu
5814554Sbinkertn@umich.edu    # Check for versions with bugs
5824554Sbinkertn@umich.edu    if not compareVersions(gcc_version, '4.4.1') or \
5834554Sbinkertn@umich.edu       not compareVersions(gcc_version, '4.4.2'):
5842667Sstever@eecs.umich.edu        print 'Info: Tree vectorizer in GCC 4.4.1 & 4.4.2 is buggy, disabling.'
5854554Sbinkertn@umich.edu        main.Append(CCFLAGS=['-fno-tree-vectorize'])
5862667Sstever@eecs.umich.edu
5874554Sbinkertn@umich.edu    # LTO support is only really working properly from 4.6 and beyond
5886121Snate@binkert.org    if compareVersions(gcc_version, '4.6') >= 0:
5892667Sstever@eecs.umich.edu        # Add the appropriate Link-Time Optimization (LTO) flags
5905522Snate@binkert.org        # unless LTO is explicitly turned off. Note that these flags
5915522Snate@binkert.org        # are only used by the fast target.
5925522Snate@binkert.org        if not GetOption('no_lto'):
5935522Snate@binkert.org            # Pass the LTO flag when compiling to produce GIMPLE
5945522Snate@binkert.org            # output, we merely create the flags here and only append
5955522Snate@binkert.org            # them later/
5965522Snate@binkert.org            main['LTO_CCFLAGS'] = ['-flto=%d' % GetOption('num_jobs')]
5975522Snate@binkert.org
5985522Snate@binkert.org            # Use the same amount of jobs for LTO as we are running
5995522Snate@binkert.org            # scons with, we hardcode the use of the linker plugin
6005522Snate@binkert.org            # which requires either gold or GNU ld >= 2.21
6015522Snate@binkert.org            main['LTO_LDFLAGS'] = ['-flto=%d' % GetOption('num_jobs'),
6025522Snate@binkert.org                                   '-fuse-linker-plugin']
6035522Snate@binkert.org
6045522Snate@binkert.org    main.Append(TCMALLOC_CCFLAGS=['-fno-builtin-malloc', '-fno-builtin-calloc',
6055522Snate@binkert.org                                  '-fno-builtin-realloc', '-fno-builtin-free'])
6065522Snate@binkert.org
6075522Snate@binkert.orgelif main['CLANG']:
6085522Snate@binkert.org    # Check for a supported version of clang, >= 2.9 is needed to
6095522Snate@binkert.org    # support similar features as gcc 4.4. See
6105522Snate@binkert.org    # http://clang.llvm.org/cxx_status.html for details
6115522Snate@binkert.org    clang_version_re = re.compile(".* version (\d+\.\d+)")
6125522Snate@binkert.org    clang_version_match = clang_version_re.search(CXX_version)
6135522Snate@binkert.org    if (clang_version_match):
6145522Snate@binkert.org        clang_version = clang_version_match.groups()[0]
6155522Snate@binkert.org        if compareVersions(clang_version, "2.9") < 0:
6162638Sstever@eecs.umich.edu            print 'Error: clang version 2.9 or newer required.'
6172638Sstever@eecs.umich.edu            print '       Installed version:', clang_version
6186121Snate@binkert.org            Exit(1)
6193716Sstever@eecs.umich.edu    else:
6205522Snate@binkert.org        print 'Error: Unable to determine clang version.'
6215522Snate@binkert.org        Exit(1)
6225522Snate@binkert.org
6235522Snate@binkert.org    # clang has a few additional warnings that we disable,
6245522Snate@binkert.org    # tautological comparisons are allowed due to unsigned integers
6255522Snate@binkert.org    # being compared to constants that happen to be 0, and extraneous
6261858SN/A    # parantheses are allowed due to Ruby's printing of the AST,
6275227Ssaidi@eecs.umich.edu    # finally self assignments are allowed as the generated CPU code
6285227Ssaidi@eecs.umich.edu    # is relying on this
6295227Ssaidi@eecs.umich.edu    main.Append(CCFLAGS=['-Wno-tautological-compare',
6305227Ssaidi@eecs.umich.edu                         '-Wno-parentheses',
6316654Snate@binkert.org                         '-Wno-self-assign'])
6326654Snate@binkert.org
6337769SAli.Saidi@ARM.com    main.Append(TCMALLOC_CCFLAGS=['-fno-builtin'])
6347769SAli.Saidi@ARM.com
6357769SAli.Saidi@ARM.com    # On Mac OS X/Darwin we need to also use libc++ (part of XCode) as
6367769SAli.Saidi@ARM.com    # opposed to libstdc++, as the later is dated.
6375227Ssaidi@eecs.umich.edu    if sys.platform == "darwin":
6385227Ssaidi@eecs.umich.edu        main.Append(CXXFLAGS=['-stdlib=libc++'])
6395227Ssaidi@eecs.umich.edu        main.Append(LIBS=['c++'])
6405204Sstever@gmail.com
6415204Sstever@gmail.comelse:
6425204Sstever@gmail.com    print termcap.Yellow + termcap.Bold + 'Error' + termcap.Normal,
6435204Sstever@gmail.com    print "Don't know what compiler options to use for your compiler."
6445204Sstever@gmail.com    print termcap.Yellow + '       compiler:' + termcap.Normal, main['CXX']
6455204Sstever@gmail.com    print termcap.Yellow + '       version:' + termcap.Normal,
6465204Sstever@gmail.com    if not CXX_version:
6475204Sstever@gmail.com        print termcap.Yellow + termcap.Bold + "COMMAND NOT FOUND!" +\
6485204Sstever@gmail.com               termcap.Normal
6495204Sstever@gmail.com    else:
6505204Sstever@gmail.com        print CXX_version.replace('\n', '<nl>')
6515204Sstever@gmail.com    print "       If you're trying to use a compiler other than GCC"
6525204Sstever@gmail.com    print "       or clang, there appears to be something wrong with your"
6535204Sstever@gmail.com    print "       environment."
6545204Sstever@gmail.com    print "       "
6555204Sstever@gmail.com    print "       If you are trying to use a compiler other than those listed"
6565204Sstever@gmail.com    print "       above you will need to ease fix SConstruct and "
6576121Snate@binkert.org    print "       src/SConscript to support that compiler."
6585204Sstever@gmail.com    Exit(1)
6593118Sstever@eecs.umich.edu
6603118Sstever@eecs.umich.edu# Set up common yacc/bison flags (needed for Ruby)
6613118Sstever@eecs.umich.edumain['YACCFLAGS'] = '-d'
6623118Sstever@eecs.umich.edumain['YACCHXXFILESUFFIX'] = '.hh'
6633118Sstever@eecs.umich.edu
6645863Snate@binkert.org# Do this after we save setting back, or else we'll tack on an
6653118Sstever@eecs.umich.edu# extra 'qdo' every time we run scons.
6665863Snate@binkert.orgif main['BATCH']:
6673118Sstever@eecs.umich.edu    main['CC']     = main['BATCH_CMD'] + ' ' + main['CC']
6687457Snate@binkert.org    main['CXX']    = main['BATCH_CMD'] + ' ' + main['CXX']
6697457Snate@binkert.org    main['AS']     = main['BATCH_CMD'] + ' ' + main['AS']
6705863Snate@binkert.org    main['AR']     = main['BATCH_CMD'] + ' ' + main['AR']
6715863Snate@binkert.org    main['RANLIB'] = main['BATCH_CMD'] + ' ' + main['RANLIB']
6725863Snate@binkert.org
6735863Snate@binkert.orgif sys.platform == 'cygwin':
6745863Snate@binkert.org    # cygwin has some header file issues...
6755863Snate@binkert.org    main.Append(CCFLAGS=["-Wno-uninitialized"])
6765863Snate@binkert.org
6776003Snate@binkert.org# Check for the protobuf compiler
6785863Snate@binkert.orgprotoc_version = readCommand([main['PROTOC'], '--version'],
6795863Snate@binkert.org                             exception='').split()
6805863Snate@binkert.org
6816120Snate@binkert.org# First two words should be "libprotoc x.y.z"
6825863Snate@binkert.orgif len(protoc_version) < 2 or protoc_version[0] != 'libprotoc':
6835863Snate@binkert.org    print termcap.Yellow + termcap.Bold + \
6845863Snate@binkert.org        'Warning: Protocol buffer compiler (protoc) not found.\n' + \
6858655Sandreas.hansson@arm.com        '         Please install protobuf-compiler for tracing support.' + \
6868655Sandreas.hansson@arm.com        termcap.Normal
6878655Sandreas.hansson@arm.com    main['PROTOC'] = False
6888655Sandreas.hansson@arm.comelse:
6898655Sandreas.hansson@arm.com    # Based on the availability of the compress stream wrappers,
6908655Sandreas.hansson@arm.com    # require 2.1.0
6918655Sandreas.hansson@arm.com    min_protoc_version = '2.1.0'
6928655Sandreas.hansson@arm.com    if compareVersions(protoc_version[1], min_protoc_version) < 0:
6936120Snate@binkert.org        print termcap.Yellow + termcap.Bold + \
6945863Snate@binkert.org            'Warning: protoc version', min_protoc_version, \
6956121Snate@binkert.org            'or newer required.\n' + \
6966121Snate@binkert.org            '         Installed version:', protoc_version[1], \
6975863Snate@binkert.org            termcap.Normal
6987727SAli.Saidi@ARM.com        main['PROTOC'] = False
6997727SAli.Saidi@ARM.com    else:
7007727SAli.Saidi@ARM.com        # Attempt to determine the appropriate include path and
7017727SAli.Saidi@ARM.com        # library path using pkg-config, that means we also need to
7027727SAli.Saidi@ARM.com        # check for pkg-config. Note that it is possible to use
7037727SAli.Saidi@ARM.com        # protobuf without the involvement of pkg-config. Later on we
7045863Snate@binkert.org        # check go a library config check and at that point the test
7053118Sstever@eecs.umich.edu        # will fail if libprotobuf cannot be found.
7065863Snate@binkert.org        if readCommand(['pkg-config', '--version'], exception=''):
7073118Sstever@eecs.umich.edu            try:
7083118Sstever@eecs.umich.edu                # Attempt to establish what linking flags to add for protobuf
7095863Snate@binkert.org                # using pkg-config
7105863Snate@binkert.org                main.ParseConfig('pkg-config --cflags --libs-only-L protobuf')
7115863Snate@binkert.org            except:
7125863Snate@binkert.org                print termcap.Yellow + termcap.Bold + \
7133118Sstever@eecs.umich.edu                    'Warning: pkg-config could not get protobuf flags.' + \
7143483Ssaidi@eecs.umich.edu                    termcap.Normal
7153494Ssaidi@eecs.umich.edu
7163494Ssaidi@eecs.umich.edu# Check for SWIG
7173483Ssaidi@eecs.umich.eduif not main.has_key('SWIG'):
7183483Ssaidi@eecs.umich.edu    print 'Error: SWIG utility not found.'
7193483Ssaidi@eecs.umich.edu    print '       Please install (see http://www.swig.org) and retry.'
7203053Sstever@eecs.umich.edu    Exit(1)
7213053Sstever@eecs.umich.edu
7223918Ssaidi@eecs.umich.edu# Check for appropriate SWIG version
7233053Sstever@eecs.umich.eduswig_version = readCommand([main['SWIG'], '-version'], exception='').split()
7243053Sstever@eecs.umich.edu# First 3 words should be "SWIG Version x.y.z"
7253053Sstever@eecs.umich.eduif len(swig_version) < 3 or \
7263053Sstever@eecs.umich.edu        swig_version[0] != 'SWIG' or swig_version[1] != 'Version':
7273053Sstever@eecs.umich.edu    print 'Error determining SWIG version.'
7287840Snate@binkert.org    Exit(1)
7297865Sgblack@eecs.umich.edu
7307865Sgblack@eecs.umich.edumin_swig_version = '2.0.4'
7317865Sgblack@eecs.umich.eduif compareVersions(swig_version[2], min_swig_version) < 0:
7327865Sgblack@eecs.umich.edu    print 'Error: SWIG version', min_swig_version, 'or newer required.'
7337865Sgblack@eecs.umich.edu    print '       Installed version:', swig_version[2]
7347840Snate@binkert.org    Exit(1)
7357840Snate@binkert.org
7367840Snate@binkert.org# Set up SWIG flags & scanner
7377840Snate@binkert.orgswig_flags=Split('-c++ -python -modern -templatereduce $_CPPINCFLAGS')
7381858SN/Amain.Append(SWIGFLAGS=swig_flags)
7391858SN/A
7401858SN/A# filter out all existing swig scanners, they mess up the dependency
7411858SN/A# stuff for some reason
7421858SN/Ascanners = []
7431858SN/Afor scanner in main['SCANNERS']:
7445863Snate@binkert.org    skeys = scanner.skeys
7455863Snate@binkert.org    if skeys == '.i':
7465863Snate@binkert.org        continue
7475863Snate@binkert.org
7486121Snate@binkert.org    if isinstance(skeys, (list, tuple)) and '.i' in skeys:
7491858SN/A        continue
7505863Snate@binkert.org
7515863Snate@binkert.org    scanners.append(scanner)
7525863Snate@binkert.org
7535863Snate@binkert.org# add the new swig scanner that we like better
7545863Snate@binkert.orgfrom SCons.Scanner import ClassicCPP as CPPScanner
7552139SN/Aswig_inc_re = '^[ \t]*[%,#][ \t]*(?:include|import)[ \t]*(<|")([^>"]+)(>|")'
7564202Sbinkertn@umich.eduscanners.append(CPPScanner("SwigScan", [ ".i" ], "CPPPATH", swig_inc_re))
7574202Sbinkertn@umich.edu
7582139SN/A# replace the scanners list that has what we want
7596994Snate@binkert.orgmain['SCANNERS'] = scanners
7606994Snate@binkert.org
7616994Snate@binkert.org# Add a custom Check function to the Configure context so that we can
7626994Snate@binkert.org# figure out if the compiler adds leading underscores to global
7636994Snate@binkert.org# variables.  This is needed for the autogenerated asm files that we
7646994Snate@binkert.org# use for embedding the python code.
7656994Snate@binkert.orgdef CheckLeading(context):
7666994Snate@binkert.org    context.Message("Checking for leading underscore in global variables...")
7676994Snate@binkert.org    # 1) Define a global variable called x from asm so the C compiler
7686994Snate@binkert.org    #    won't change the symbol at all.
7696994Snate@binkert.org    # 2) Declare that variable.
7706994Snate@binkert.org    # 3) Use the variable
7716994Snate@binkert.org    #
7726994Snate@binkert.org    # If the compiler prepends an underscore, this will successfully
7736994Snate@binkert.org    # link because the external symbol 'x' will be called '_x' which
7746994Snate@binkert.org    # was defined by the asm statement.  If the compiler does not
7756994Snate@binkert.org    # prepend an underscore, this will not successfully link because
7766994Snate@binkert.org    # '_x' will have been defined by assembly, while the C portion of
7776994Snate@binkert.org    # the code will be trying to use 'x'
7786994Snate@binkert.org    ret = context.TryLink('''
7796994Snate@binkert.org        asm(".globl _x; _x: .byte 0");
7806994Snate@binkert.org        extern int x;
7816994Snate@binkert.org        int main() { return x; }
7826994Snate@binkert.org        ''', extension=".c")
7836994Snate@binkert.org    context.env.Append(LEADING_UNDERSCORE=ret)
7846994Snate@binkert.org    context.Result(ret)
7856994Snate@binkert.org    return ret
7866994Snate@binkert.org
7872155SN/A# Add a custom Check function to test for structure members.
7885863Snate@binkert.orgdef CheckMember(context, include, decl, member, include_quotes="<>"):
7891869SN/A    context.Message("Checking for member %s in %s..." %
7901869SN/A                    (member, decl))
7915863Snate@binkert.org    text = """
7925863Snate@binkert.org#include %(header)s
7934202Sbinkertn@umich.eduint main(){
7946108Snate@binkert.org  %(decl)s test;
7956108Snate@binkert.org  (void)test.%(member)s;
7966108Snate@binkert.org  return 0;
7976108Snate@binkert.org};
7984202Sbinkertn@umich.edu""" % { "header" : include_quotes[0] + include + include_quotes[1],
7995863Snate@binkert.org        "decl" : decl,
8008474Sgblack@eecs.umich.edu        "member" : member,
8018474Sgblack@eecs.umich.edu        }
8025742Snate@binkert.org
8038268Ssteve.reinhardt@amd.com    ret = context.TryCompile(text, extension=".cc")
8048268Ssteve.reinhardt@amd.com    context.Result(ret)
8058268Ssteve.reinhardt@amd.com    return ret
8065742Snate@binkert.org
8075341Sstever@gmail.com# Platform-specific configuration.  Note again that we assume that all
8088474Sgblack@eecs.umich.edu# builds under a given build root run on the same host platform.
8098474Sgblack@eecs.umich.educonf = Configure(main,
8105342Sstever@gmail.com                 conf_dir = joinpath(build_root, '.scons_config'),
8114202Sbinkertn@umich.edu                 log_file = joinpath(build_root, 'scons_config.log'),
8124202Sbinkertn@umich.edu                 custom_tests = {
8134202Sbinkertn@umich.edu        'CheckLeading' : CheckLeading,
8145863Snate@binkert.org        'CheckMember' : CheckMember,
8155863Snate@binkert.org        })
8166994Snate@binkert.org
8176994Snate@binkert.org# Check for leading underscores.  Don't really need to worry either
8186994Snate@binkert.org# way so don't need to check the return code.
8195863Snate@binkert.orgconf.CheckLeading()
8208152Ssteve.reinhardt@amd.com
8218878Ssteve.reinhardt@amd.com# Check if we should compile a 64 bit binary on Mac OS X/Darwin
8225863Snate@binkert.orgtry:
8235863Snate@binkert.org    import platform
8245863Snate@binkert.org    uname = platform.uname()
8255863Snate@binkert.org    if uname[0] == 'Darwin' and compareVersions(uname[2], '9.0.0') >= 0:
8265863Snate@binkert.org        if int(readCommand('sysctl -n hw.cpu64bit_capable')[0]):
8275863Snate@binkert.org            main.Append(CCFLAGS=['-arch', 'x86_64'])
8285863Snate@binkert.org            main.Append(CFLAGS=['-arch', 'x86_64'])
8295863Snate@binkert.org            main.Append(LINKFLAGS=['-arch', 'x86_64'])
8305863Snate@binkert.org            main.Append(ASFLAGS=['-arch', 'x86_64'])
8315863Snate@binkert.orgexcept:
8327840Snate@binkert.org    pass
8335863Snate@binkert.org
8345863Snate@binkert.org# Recent versions of scons substitute a "Null" object for Configure()
8355952Ssaidi@eecs.umich.edu# when configuration isn't necessary, e.g., if the "--help" option is
8361869SN/A# present.  Unfortuantely this Null object always returns false,
8371858SN/A# breaking all our configuration checks.  We replace it with our own
8385863Snate@binkert.org# more optimistic null object that returns True instead.
8398805Sgblack@eecs.umich.eduif not conf:
8408805Sgblack@eecs.umich.edu    def NullCheck(*args, **kwargs):
8418805Sgblack@eecs.umich.edu        return True
8421858SN/A
843955SN/A    class NullConf:
844955SN/A        def __init__(self, env):
8451869SN/A            self.env = env
8461869SN/A        def Finish(self):
8471869SN/A            return self.env
8481869SN/A        def __getattr__(self, mname):
8491869SN/A            return NullCheck
8505863Snate@binkert.org
8515863Snate@binkert.org    conf = NullConf(main)
8525863Snate@binkert.org
8531869SN/A# Cache build files in the supplied directory.
8545863Snate@binkert.orgif main['M5_BUILD_CACHE']:
8551869SN/A    print 'Using build cache located at', main['M5_BUILD_CACHE']
8565863Snate@binkert.org    CacheDir(main['M5_BUILD_CACHE'])
8571869SN/A
8581869SN/A# Find Python include and library directories for embedding the
8591869SN/A# interpreter. We rely on python-config to resolve the appropriate
8601869SN/A# includes and linker flags. ParseConfig does not seem to understand
8618483Sgblack@eecs.umich.edu# the more exotic linker flags such as -Xlinker and -export-dynamic so
8621869SN/A# we add them explicitly below. If you want to link in an alternate
8631869SN/A# version of python, see above for instructions on how to invoke
8641869SN/A# scons with the appropriate PATH set.
8651869SN/A#
8665863Snate@binkert.org# First we check if python2-config exists, else we use python-config
8675863Snate@binkert.orgpython_config = readCommand(['which', 'python2-config'], exception='').strip()
8681869SN/Aif not os.path.exists(python_config):
8695863Snate@binkert.org    python_config = readCommand(['which', 'python-config'],
8705863Snate@binkert.org                                exception='').strip()
8713356Sbinkertn@umich.edupy_includes = readCommand([python_config, '--includes'],
8723356Sbinkertn@umich.edu                          exception='').split()
8733356Sbinkertn@umich.edu# Strip the -I from the include folders before adding them to the
8743356Sbinkertn@umich.edu# CPPPATH
8753356Sbinkertn@umich.edumain.Append(CPPPATH=map(lambda inc: inc[2:], py_includes))
8764781Snate@binkert.org
8775863Snate@binkert.org# Read the linker flags and split them into libraries and other link
8785863Snate@binkert.org# flags. The libraries are added later through the call the CheckLib.
8791869SN/Apy_ld_flags = readCommand([python_config, '--ldflags'], exception='').split()
8801869SN/Apy_libs = []
8811869SN/Afor lib in py_ld_flags:
8826121Snate@binkert.org     if not lib.startswith('-l'):
8831869SN/A         main.Append(LINKFLAGS=[lib])
8842638Sstever@eecs.umich.edu     else:
8856121Snate@binkert.org         lib = lib[2:]
8866121Snate@binkert.org         if lib not in py_libs:
8872638Sstever@eecs.umich.edu             py_libs.append(lib)
8885749Scws3k@cs.virginia.edu
8896121Snate@binkert.org# verify that this stuff works
8906121Snate@binkert.orgif not conf.CheckHeader('Python.h', '<>'):
8915749Scws3k@cs.virginia.edu    print "Error: can't find Python.h header in", py_includes
8921869SN/A    print "Install Python headers (package python-dev on Ubuntu and RedHat)"
8931869SN/A    Exit(1)
8943546Sgblack@eecs.umich.edu
8953546Sgblack@eecs.umich.edufor lib in py_libs:
8963546Sgblack@eecs.umich.edu    if not conf.CheckLib(lib):
8973546Sgblack@eecs.umich.edu        print "Error: can't find library %s required by python" % lib
8986121Snate@binkert.org        Exit(1)
8995863Snate@binkert.org
9003546Sgblack@eecs.umich.edu# On Solaris you need to use libsocket for socket ops
9013546Sgblack@eecs.umich.eduif not conf.CheckLibWithHeader(None, 'sys/socket.h', 'C++', 'accept(0,0,0);'):
9023546Sgblack@eecs.umich.edu   if not conf.CheckLibWithHeader('socket', 'sys/socket.h', 'C++', 'accept(0,0,0);'):
9033546Sgblack@eecs.umich.edu       print "Can't find library with socket calls (e.g. accept())"
9044781Snate@binkert.org       Exit(1)
9054781Snate@binkert.org
9066658Snate@binkert.org# Check for zlib.  If the check passes, libz will be automatically
9076658Snate@binkert.org# added to the LIBS environment variable.
9084781Snate@binkert.orgif not conf.CheckLibWithHeader('z', 'zlib.h', 'C++','zlibVersion();'):
9093546Sgblack@eecs.umich.edu    print 'Error: did not find needed zlib compression library '\
9103546Sgblack@eecs.umich.edu          'and/or zlib.h header file.'
9113546Sgblack@eecs.umich.edu    print '       Please install zlib and try again.'
9123546Sgblack@eecs.umich.edu    Exit(1)
9137756SAli.Saidi@ARM.com
9147816Ssteve.reinhardt@amd.com# If we have the protobuf compiler, also make sure we have the
9153546Sgblack@eecs.umich.edu# development libraries. If the check passes, libprotobuf will be
9163546Sgblack@eecs.umich.edu# automatically added to the LIBS environment variable. After
9173546Sgblack@eecs.umich.edu# this, we can use the HAVE_PROTOBUF flag to determine if we have
9183546Sgblack@eecs.umich.edu# got both protoc and libprotobuf available.
9194202Sbinkertn@umich.edumain['HAVE_PROTOBUF'] = main['PROTOC'] and \
9203546Sgblack@eecs.umich.edu    conf.CheckLibWithHeader('protobuf', 'google/protobuf/message.h',
9213546Sgblack@eecs.umich.edu                            'C++', 'GOOGLE_PROTOBUF_VERIFY_VERSION;')
9223546Sgblack@eecs.umich.edu
923955SN/A# If we have the compiler but not the library, print another warning.
924955SN/Aif main['PROTOC'] and not main['HAVE_PROTOBUF']:
925955SN/A    print termcap.Yellow + termcap.Bold + \
926955SN/A        'Warning: did not find protocol buffer library and/or headers.\n' + \
9275863Snate@binkert.org    '       Please install libprotobuf-dev for tracing support.' + \
9285863Snate@binkert.org    termcap.Normal
9295343Sstever@gmail.com
9305343Sstever@gmail.com# Check for librt.
9316121Snate@binkert.orghave_posix_clock = \
9325863Snate@binkert.org    conf.CheckLibWithHeader(None, 'time.h', 'C',
9334773Snate@binkert.org                            'clock_nanosleep(0,0,NULL,NULL);') or \
9345863Snate@binkert.org    conf.CheckLibWithHeader('rt', 'time.h', 'C',
9352632Sstever@eecs.umich.edu                            'clock_nanosleep(0,0,NULL,NULL);')
9365863Snate@binkert.org
9372023SN/Ahave_posix_timers = \
9385863Snate@binkert.org    conf.CheckLibWithHeader([None, 'rt'], [ 'time.h', 'signal.h' ], 'C',
9395863Snate@binkert.org                            'timer_create(CLOCK_MONOTONIC, NULL, NULL);')
9405863Snate@binkert.org
9415863Snate@binkert.orgif conf.CheckLib('tcmalloc'):
9425863Snate@binkert.org    main.Append(CCFLAGS=main['TCMALLOC_CCFLAGS'])
9435863Snate@binkert.orgelif conf.CheckLib('tcmalloc_minimal'):
9445863Snate@binkert.org    main.Append(CCFLAGS=main['TCMALLOC_CCFLAGS'])
9455863Snate@binkert.orgelse:
9465863Snate@binkert.org    print termcap.Yellow + termcap.Bold + \
9472632Sstever@eecs.umich.edu          "You can get a 12% performance improvement by installing tcmalloc "\
9485863Snate@binkert.org          "(libgoogle-perftools-dev package on Ubuntu or RedHat)." + \
9492023SN/A          termcap.Normal
9502632Sstever@eecs.umich.edu
9515863Snate@binkert.orgif not have_posix_clock:
9525342Sstever@gmail.com    print "Can't find library for POSIX clocks."
9535863Snate@binkert.org
9542632Sstever@eecs.umich.edu# Check for <fenv.h> (C99 FP environment control)
9555863Snate@binkert.orghave_fenv = conf.CheckHeader('fenv.h', '<>')
9565863Snate@binkert.orgif not have_fenv:
9578267Ssteve.reinhardt@amd.com    print "Warning: Header file <fenv.h> not found."
9588120Sgblack@eecs.umich.edu    print "         This host has no IEEE FP rounding mode control."
9598267Ssteve.reinhardt@amd.com
9608267Ssteve.reinhardt@amd.com# Check if we should enable KVM-based hardware virtualization. The API
9618267Ssteve.reinhardt@amd.com# we rely on exists since version 2.6.36 of the kernel, but somehow
9628267Ssteve.reinhardt@amd.com# the KVM_API_VERSION does not reflect the change. We test for one of
9638267Ssteve.reinhardt@amd.com# the types as a fall back.
9648267Ssteve.reinhardt@amd.comhave_kvm = conf.CheckHeader('linux/kvm.h', '<>') and \
9658267Ssteve.reinhardt@amd.com    conf.CheckTypeSize('struct kvm_xsave', '#include <linux/kvm.h>') != 0
9668267Ssteve.reinhardt@amd.comif not have_kvm:
9678267Ssteve.reinhardt@amd.com    print "Info: Compatible header file <linux/kvm.h> not found, " \
9685863Snate@binkert.org        "disabling KVM support."
9695863Snate@binkert.org
9705863Snate@binkert.org# Check if the requested target ISA is compatible with the host
9712632Sstever@eecs.umich.edudef is_isa_kvm_compatible(isa):
9728267Ssteve.reinhardt@amd.com    isa_comp_table = {
9738267Ssteve.reinhardt@amd.com        "arm" : ( "armv7l" ),
9748267Ssteve.reinhardt@amd.com        "x86" : ( "x86_64" ),
9752632Sstever@eecs.umich.edu        }
9761888SN/A    try:
9775863Snate@binkert.org        import platform
9785863Snate@binkert.org        host_isa = platform.machine()
9791858SN/A    except:
9808120Sgblack@eecs.umich.edu        print "Warning: Failed to determine host ISA."
9818120Sgblack@eecs.umich.edu        return False
9827756SAli.Saidi@ARM.com
9832598SN/A    return host_isa in isa_comp_table.get(isa, [])
9845863Snate@binkert.org
9851858SN/A
9861858SN/A# Check if the exclude_host attribute is available. We want this to
9871858SN/A# get accurate instruction counts in KVM.
9885863Snate@binkert.orgmain['HAVE_PERF_ATTR_EXCLUDE_HOST'] = conf.CheckMember(
9891858SN/A    'linux/perf_event.h', 'struct perf_event_attr', 'exclude_host')
9901858SN/A
9911858SN/A
9925863Snate@binkert.org######################################################################
9931871SN/A#
9941858SN/A# Finish the configuration
9951858SN/A#
9961858SN/Amain = conf.Finish()
9971858SN/A
9985863Snate@binkert.org######################################################################
9995863Snate@binkert.org#
10001869SN/A# Collect all non-global variables
10011965SN/A#
10027739Sgblack@eecs.umich.edu
10031965SN/A# Define the universe of supported ISAs
10042761Sstever@eecs.umich.eduall_isa_list = [ ]
10055863Snate@binkert.orgExport('all_isa_list')
10061869SN/A
10075863Snate@binkert.orgclass CpuModel(object):
10082667Sstever@eecs.umich.edu    '''The CpuModel class encapsulates everything the ISA parser needs to
10091869SN/A    know about a particular CPU model.'''
10101869SN/A
10112929Sktlim@umich.edu    # Dict of available CPU model objects.  Accessible as CpuModel.dict.
10122929Sktlim@umich.edu    dict = {}
10135863Snate@binkert.org    list = []
10142929Sktlim@umich.edu    defaults = []
1015955SN/A
10168120Sgblack@eecs.umich.edu    # Constructor.  Automatically adds models to CpuModel.dict.
10178120Sgblack@eecs.umich.edu    def __init__(self, name, filename, includes, strings, default=False):
10188120Sgblack@eecs.umich.edu        self.name = name           # name of model
10198120Sgblack@eecs.umich.edu        self.filename = filename   # filename for output exec code
10208120Sgblack@eecs.umich.edu        self.includes = includes   # include files needed in exec file
10218120Sgblack@eecs.umich.edu        # The 'strings' dict holds all the per-CPU symbols we can
10228120Sgblack@eecs.umich.edu        # substitute into templates etc.
10238120Sgblack@eecs.umich.edu        self.strings = strings
10248120Sgblack@eecs.umich.edu
10258120Sgblack@eecs.umich.edu        # This cpu is enabled by default
10268120Sgblack@eecs.umich.edu        self.default = default
10278120Sgblack@eecs.umich.edu
1028        # Add self to dict
1029        if name in CpuModel.dict:
1030            raise AttributeError, "CpuModel '%s' already registered" % name
1031        CpuModel.dict[name] = self
1032        CpuModel.list.append(name)
1033
1034Export('CpuModel')
1035
1036# Sticky variables get saved in the variables file so they persist from
1037# one invocation to the next (unless overridden, in which case the new
1038# value becomes sticky).
1039sticky_vars = Variables(args=ARGUMENTS)
1040Export('sticky_vars')
1041
1042# Sticky variables that should be exported
1043export_vars = []
1044Export('export_vars')
1045
1046# For Ruby
1047all_protocols = []
1048Export('all_protocols')
1049protocol_dirs = []
1050Export('protocol_dirs')
1051slicc_includes = []
1052Export('slicc_includes')
1053
1054# Walk the tree and execute all SConsopts scripts that wil add to the
1055# above variables
1056if GetOption('verbose'):
1057    print "Reading SConsopts"
1058for bdir in [ base_dir ] + extras_dir_list:
1059    if not isdir(bdir):
1060        print "Error: directory '%s' does not exist" % bdir
1061        Exit(1)
1062    for root, dirs, files in os.walk(bdir):
1063        if 'SConsopts' in files:
1064            if GetOption('verbose'):
1065                print "Reading", joinpath(root, 'SConsopts')
1066            SConscript(joinpath(root, 'SConsopts'))
1067
1068all_isa_list.sort()
1069
1070sticky_vars.AddVariables(
1071    EnumVariable('TARGET_ISA', 'Target ISA', 'alpha', all_isa_list),
1072    ListVariable('CPU_MODELS', 'CPU models',
1073                 sorted(n for n,m in CpuModel.dict.iteritems() if m.default),
1074                 sorted(CpuModel.list)),
1075    BoolVariable('EFENCE', 'Link with Electric Fence malloc debugger',
1076                 False),
1077    BoolVariable('SS_COMPATIBLE_FP',
1078                 'Make floating-point results compatible with SimpleScalar',
1079                 False),
1080    BoolVariable('USE_SSE2',
1081                 'Compile for SSE2 (-msse2) to get IEEE FP on x86 hosts',
1082                 False),
1083    BoolVariable('USE_POSIX_CLOCK', 'Use POSIX Clocks', have_posix_clock),
1084    BoolVariable('USE_FENV', 'Use <fenv.h> IEEE mode control', have_fenv),
1085    BoolVariable('CP_ANNOTATE', 'Enable critical path annotation capability', False),
1086    BoolVariable('USE_KVM', 'Enable hardware virtualized (KVM) CPU models', have_kvm),
1087    EnumVariable('PROTOCOL', 'Coherence protocol for Ruby', 'None',
1088                  all_protocols),
1089    )
1090
1091# These variables get exported to #defines in config/*.hh (see src/SConscript).
1092export_vars += ['USE_FENV', 'SS_COMPATIBLE_FP', 'TARGET_ISA', 'CP_ANNOTATE',
1093                'USE_POSIX_CLOCK', 'PROTOCOL', 'HAVE_PROTOBUF',
1094                'HAVE_PERF_ATTR_EXCLUDE_HOST']
1095
1096###################################################
1097#
1098# Define a SCons builder for configuration flag headers.
1099#
1100###################################################
1101
1102# This function generates a config header file that #defines the
1103# variable symbol to the current variable setting (0 or 1).  The source
1104# operands are the name of the variable and a Value node containing the
1105# value of the variable.
1106def build_config_file(target, source, env):
1107    (variable, value) = [s.get_contents() for s in source]
1108    f = file(str(target[0]), 'w')
1109    print >> f, '#define', variable, value
1110    f.close()
1111    return None
1112
1113# Combine the two functions into a scons Action object.
1114config_action = MakeAction(build_config_file, Transform("CONFIG H", 2))
1115
1116# The emitter munges the source & target node lists to reflect what
1117# we're really doing.
1118def config_emitter(target, source, env):
1119    # extract variable name from Builder arg
1120    variable = str(target[0])
1121    # True target is config header file
1122    target = joinpath('config', variable.lower() + '.hh')
1123    val = env[variable]
1124    if isinstance(val, bool):
1125        # Force value to 0/1
1126        val = int(val)
1127    elif isinstance(val, str):
1128        val = '"' + val + '"'
1129
1130    # Sources are variable name & value (packaged in SCons Value nodes)
1131    return ([target], [Value(variable), Value(val)])
1132
1133config_builder = Builder(emitter = config_emitter, action = config_action)
1134
1135main.Append(BUILDERS = { 'ConfigFile' : config_builder })
1136
1137# libelf build is shared across all configs in the build root.
1138main.SConscript('ext/libelf/SConscript',
1139                variant_dir = joinpath(build_root, 'libelf'))
1140
1141# gzstream build is shared across all configs in the build root.
1142main.SConscript('ext/gzstream/SConscript',
1143                variant_dir = joinpath(build_root, 'gzstream'))
1144
1145# libfdt build is shared across all configs in the build root.
1146main.SConscript('ext/libfdt/SConscript',
1147                variant_dir = joinpath(build_root, 'libfdt'))
1148
1149# fputils build is shared across all configs in the build root.
1150main.SConscript('ext/fputils/SConscript',
1151                variant_dir = joinpath(build_root, 'fputils'))
1152
1153# DRAMSim2 build is shared across all configs in the build root.
1154main.SConscript('ext/dramsim2/SConscript',
1155                variant_dir = joinpath(build_root, 'dramsim2'))
1156
1157###################################################
1158#
1159# This function is used to set up a directory with switching headers
1160#
1161###################################################
1162
1163main['ALL_ISA_LIST'] = all_isa_list
1164all_isa_deps = {}
1165def make_switching_dir(dname, switch_headers, env):
1166    # Generate the header.  target[0] is the full path of the output
1167    # header to generate.  'source' is a dummy variable, since we get the
1168    # list of ISAs from env['ALL_ISA_LIST'].
1169    def gen_switch_hdr(target, source, env):
1170        fname = str(target[0])
1171        isa = env['TARGET_ISA'].lower()
1172        try:
1173            f = open(fname, 'w')
1174            print >>f, '#include "%s/%s/%s"' % (dname, isa, basename(fname))
1175            f.close()
1176        except IOError:
1177            print "Failed to create %s" % fname
1178            raise
1179
1180    # Build SCons Action object. 'varlist' specifies env vars that this
1181    # action depends on; when env['ALL_ISA_LIST'] changes these actions
1182    # should get re-executed.
1183    switch_hdr_action = MakeAction(gen_switch_hdr,
1184                          Transform("GENERATE"), varlist=['ALL_ISA_LIST'])
1185
1186    # Instantiate actions for each header
1187    for hdr in switch_headers:
1188        env.Command(hdr, [], switch_hdr_action)
1189
1190    isa_target = Dir('.').up().name.lower().replace('_', '-')
1191    env['PHONY_BASE'] = '#'+isa_target
1192    all_isa_deps[isa_target] = None
1193
1194Export('make_switching_dir')
1195
1196# all-isas -> all-deps -> all-environs -> all_targets
1197main.Alias('#all-isas', [])
1198main.Alias('#all-deps', '#all-isas')
1199
1200# Dummy target to ensure all environments are created before telling
1201# SCons what to actually make (the command line arguments).  We attach
1202# them to the dependence graph after the environments are complete.
1203ORIG_BUILD_TARGETS = list(BUILD_TARGETS) # force a copy; gets closure to work.
1204def environsComplete(target, source, env):
1205    for t in ORIG_BUILD_TARGETS:
1206        main.Depends('#all-targets', t)
1207
1208# Each build/* switching_dir attaches its *-environs target to #all-environs.
1209main.Append(BUILDERS = {'CompleteEnvirons' :
1210                        Builder(action=MakeAction(environsComplete, None))})
1211main.CompleteEnvirons('#all-environs', [])
1212
1213def doNothing(**ignored): pass
1214main.Append(BUILDERS = {'Dummy': Builder(action=MakeAction(doNothing, None))})
1215
1216# The final target to which all the original targets ultimately get attached.
1217main.Dummy('#all-targets', '#all-environs')
1218BUILD_TARGETS[:] = ['#all-targets']
1219
1220###################################################
1221#
1222# Define build environments for selected configurations.
1223#
1224###################################################
1225
1226for variant_path in variant_paths:
1227    if not GetOption('silent'):
1228        print "Building in", variant_path
1229
1230    # Make a copy of the build-root environment to use for this config.
1231    env = main.Clone()
1232    env['BUILDDIR'] = variant_path
1233
1234    # variant_dir is the tail component of build path, and is used to
1235    # determine the build parameters (e.g., 'ALPHA_SE')
1236    (build_root, variant_dir) = splitpath(variant_path)
1237
1238    # Set env variables according to the build directory config.
1239    sticky_vars.files = []
1240    # Variables for $BUILD_ROOT/$VARIANT_DIR are stored in
1241    # $BUILD_ROOT/variables/$VARIANT_DIR so you can nuke
1242    # $BUILD_ROOT/$VARIANT_DIR without losing your variables settings.
1243    current_vars_file = joinpath(build_root, 'variables', variant_dir)
1244    if isfile(current_vars_file):
1245        sticky_vars.files.append(current_vars_file)
1246        if not GetOption('silent'):
1247            print "Using saved variables file %s" % current_vars_file
1248    else:
1249        # Build dir-specific variables file doesn't exist.
1250
1251        # Make sure the directory is there so we can create it later
1252        opt_dir = dirname(current_vars_file)
1253        if not isdir(opt_dir):
1254            mkdir(opt_dir)
1255
1256        # Get default build variables from source tree.  Variables are
1257        # normally determined by name of $VARIANT_DIR, but can be
1258        # overridden by '--default=' arg on command line.
1259        default = GetOption('default')
1260        opts_dir = joinpath(main.root.abspath, 'build_opts')
1261        if default:
1262            default_vars_files = [joinpath(build_root, 'variables', default),
1263                                  joinpath(opts_dir, default)]
1264        else:
1265            default_vars_files = [joinpath(opts_dir, variant_dir)]
1266        existing_files = filter(isfile, default_vars_files)
1267        if existing_files:
1268            default_vars_file = existing_files[0]
1269            sticky_vars.files.append(default_vars_file)
1270            print "Variables file %s not found,\n  using defaults in %s" \
1271                  % (current_vars_file, default_vars_file)
1272        else:
1273            print "Error: cannot find variables file %s or " \
1274                  "default file(s) %s" \
1275                  % (current_vars_file, ' or '.join(default_vars_files))
1276            Exit(1)
1277
1278    # Apply current variable settings to env
1279    sticky_vars.Update(env)
1280
1281    help_texts["local_vars"] += \
1282        "Build variables for %s:\n" % variant_dir \
1283                 + sticky_vars.GenerateHelpText(env)
1284
1285    # Process variable settings.
1286
1287    if not have_fenv and env['USE_FENV']:
1288        print "Warning: <fenv.h> not available; " \
1289              "forcing USE_FENV to False in", variant_dir + "."
1290        env['USE_FENV'] = False
1291
1292    if not env['USE_FENV']:
1293        print "Warning: No IEEE FP rounding mode control in", variant_dir + "."
1294        print "         FP results may deviate slightly from other platforms."
1295
1296    if env['EFENCE']:
1297        env.Append(LIBS=['efence'])
1298
1299    if env['USE_KVM']:
1300        if not have_kvm:
1301            print "Warning: Can not enable KVM, host seems to lack KVM support"
1302            env['USE_KVM'] = False
1303        elif not have_posix_timers:
1304            print "Warning: Can not enable KVM, host seems to lack support " \
1305                "for POSIX timers"
1306            env['USE_KVM'] = False
1307        elif not is_isa_kvm_compatible(env['TARGET_ISA']):
1308            print "Info: KVM support disabled due to unsupported host and " \
1309                "target ISA combination"
1310            env['USE_KVM'] = False
1311
1312    # Warn about missing optional functionality
1313    if env['USE_KVM']:
1314        if not main['HAVE_PERF_ATTR_EXCLUDE_HOST']:
1315            print "Warning: perf_event headers lack support for the " \
1316                "exclude_host attribute. KVM instruction counts will " \
1317                "be inaccurate."
1318
1319    # Save sticky variable settings back to current variables file
1320    sticky_vars.Save(current_vars_file, env)
1321
1322    if env['USE_SSE2']:
1323        env.Append(CCFLAGS=['-msse2'])
1324
1325    # The src/SConscript file sets up the build rules in 'env' according
1326    # to the configured variables.  It returns a list of environments,
1327    # one for each variant build (debug, opt, etc.)
1328    SConscript('src/SConscript', variant_dir = variant_path, exports = 'env')
1329
1330def pairwise(iterable):
1331    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
1332    a, b = itertools.tee(iterable)
1333    b.next()
1334    return itertools.izip(a, b)
1335
1336# Create false dependencies so SCons will parse ISAs, establish
1337# dependencies, and setup the build Environments serially. Either
1338# SCons (likely) and/or our SConscripts (possibly) cannot cope with -j
1339# greater than 1. It appears to be standard race condition stuff; it
1340# doesn't always fail, but usually, and the behaviors are different.
1341# Every time I tried to remove this, builds would fail in some
1342# creative new way. So, don't do that. You'll want to, though, because
1343# tests/SConscript takes a long time to make its Environments.
1344for t1, t2 in pairwise(sorted(all_isa_deps.iterkeys())):
1345    main.Depends('#%s-deps'     % t2, '#%s-deps'     % t1)
1346    main.Depends('#%s-environs' % t2, '#%s-environs' % t1)
1347
1348# base help text
1349Help('''
1350Usage: scons [scons options] [build variables] [target(s)]
1351
1352Extra scons options:
1353%(options)s
1354
1355Global build variables:
1356%(global_vars)s
1357
1358%(local_vars)s
1359''' % help_texts)
1360