SConstruct revision 9651
1955SN/A# -*- mode:python -*-
2955SN/A
31762SN/A# Copyright (c) 2011 Advanced Micro Devices, Inc.
4955SN/A# Copyright (c) 2009 The Hewlett-Packard Development Company
5955SN/A# Copyright (c) 2004-2005 The Regents of The University of Michigan
6955SN/A# All rights reserved.
7955SN/A#
8955SN/A# Redistribution and use in source and binary forms, with or without
9955SN/A# modification, are permitted provided that the following conditions are
10955SN/A# met: redistributions of source code must retain the above copyright
11955SN/A# notice, this list of conditions and the following disclaimer;
12955SN/A# redistributions in binary form must reproduce the above copyright
13955SN/A# notice, this list of conditions and the following disclaimer in the
14955SN/A# documentation and/or other materials provided with the distribution;
15955SN/A# neither the name of the copyright holders nor the names of its
16955SN/A# contributors may be used to endorse or promote products derived from
17955SN/A# this software without specific prior written permission.
18955SN/A#
19955SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20955SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21955SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22955SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23955SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24955SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25955SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26955SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27955SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
282665Ssaidi@eecs.umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
292665Ssaidi@eecs.umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30955SN/A#
31955SN/A# Authors: Steve Reinhardt
32955SN/A#          Nathan Binkert
33955SN/A
34955SN/A###################################################
352632Sstever@eecs.umich.edu#
362632Sstever@eecs.umich.edu# SCons top-level build description (SConstruct) file.
372632Sstever@eecs.umich.edu#
382632Sstever@eecs.umich.edu# While in this directory ('gem5'), just type 'scons' to build the default
39955SN/A# configuration (see below), or type 'scons build/<CONFIG>/<binary>'
402632Sstever@eecs.umich.edu# to build some other configuration (e.g., 'build/ALPHA/gem5.opt' for
412632Sstever@eecs.umich.edu# the optimized full-system version).
422761Sstever@eecs.umich.edu#
432632Sstever@eecs.umich.edu# You can build gem5 in a different directory as long as there is a
442632Sstever@eecs.umich.edu# 'build/<CONFIG>' somewhere along the target path.  The build system
452632Sstever@eecs.umich.edu# expects that all configs under the same build directory are being
462761Sstever@eecs.umich.edu# built for the same host system.
472761Sstever@eecs.umich.edu#
482761Sstever@eecs.umich.edu# Examples:
492632Sstever@eecs.umich.edu#
502632Sstever@eecs.umich.edu#   The following two commands are equivalent.  The '-u' option tells
512761Sstever@eecs.umich.edu#   scons to search up the directory tree for this SConstruct file.
522761Sstever@eecs.umich.edu#   % cd <path-to-src>/gem5 ; scons build/ALPHA/gem5.debug
532761Sstever@eecs.umich.edu#   % cd <path-to-src>/gem5/build/ALPHA; scons -u gem5.debug
542761Sstever@eecs.umich.edu#
552761Sstever@eecs.umich.edu#   The following two commands are equivalent and demonstrate building
562632Sstever@eecs.umich.edu#   in a directory outside of the source tree.  The '-C' option tells
572632Sstever@eecs.umich.edu#   scons to chdir to the specified directory to find this SConstruct
582632Sstever@eecs.umich.edu#   file.
592632Sstever@eecs.umich.edu#   % cd <path-to-src>/gem5 ; scons /local/foo/build/ALPHA/gem5.debug
602632Sstever@eecs.umich.edu#   % cd /local/foo/build/ALPHA; scons -C <path-to-src>/gem5 gem5.debug
612632Sstever@eecs.umich.edu#
622632Sstever@eecs.umich.edu# You can use 'scons -H' to print scons options.  If you're in this
63955SN/A# 'gem5' directory (or use -u or -C to tell scons where to find this
64955SN/A# file), you can use 'scons -h' to print all the gem5-specific build
65955SN/A# options as well.
66955SN/A#
67955SN/A###################################################
68955SN/A
693716Sstever@eecs.umich.edu# Check for recent-enough Python and SCons versions.
70955SN/Atry:
712656Sstever@eecs.umich.edu    # Really old versions of scons only take two options for the
722656Sstever@eecs.umich.edu    # function, so check once without the revision and once with the
732656Sstever@eecs.umich.edu    # revision, the first instance will fail for stuff other than
742656Sstever@eecs.umich.edu    # 0.98, and the second will fail for 0.98.0
752656Sstever@eecs.umich.edu    EnsureSConsVersion(0, 98)
762656Sstever@eecs.umich.edu    EnsureSConsVersion(0, 98, 1)
772656Sstever@eecs.umich.eduexcept SystemExit, e:
782653Sstever@eecs.umich.edu    print """
792653Sstever@eecs.umich.eduFor more details, see:
802653Sstever@eecs.umich.edu    http://gem5.org/Dependencies
812653Sstever@eecs.umich.edu"""
822653Sstever@eecs.umich.edu    raise
832653Sstever@eecs.umich.edu
842653Sstever@eecs.umich.edu# We ensure the python version early because we have stuff that
852653Sstever@eecs.umich.edu# requires python 2.4
862653Sstever@eecs.umich.edutry:
872653Sstever@eecs.umich.edu    EnsurePythonVersion(2, 4)
882653Sstever@eecs.umich.eduexcept SystemExit, e:
891852SN/A    print """
90955SN/AYou can use a non-default installation of the Python interpreter by
91955SN/Aeither (1) rearranging your PATH so that scons finds the non-default
92955SN/A'python' first or (2) explicitly invoking an alternative interpreter
932632Sstever@eecs.umich.eduon the scons script.
943716Sstever@eecs.umich.edu
95955SN/AFor more details, see:
961533SN/A    http://gem5.org/wiki/index.php/Using_a_non-default_Python_installation
973716Sstever@eecs.umich.edu"""
981533SN/A    raise
99955SN/A
100955SN/A# Global Python includes
1012632Sstever@eecs.umich.eduimport os
1022632Sstever@eecs.umich.eduimport re
103955SN/Aimport subprocess
104955SN/Aimport sys
105955SN/A
106955SN/Afrom os import mkdir, environ
1072632Sstever@eecs.umich.edufrom os.path import abspath, basename, dirname, expanduser, normpath
108955SN/Afrom os.path import exists,  isdir, isfile
1092632Sstever@eecs.umich.edufrom os.path import join as joinpath, split as splitpath
110955SN/A
111955SN/A# SCons includes
1122632Sstever@eecs.umich.eduimport SCons
1133716Sstever@eecs.umich.eduimport SCons.Node
1142632Sstever@eecs.umich.edu
1152632Sstever@eecs.umich.eduextra_python_paths = [
1162632Sstever@eecs.umich.edu    Dir('src/python').srcnode().abspath, # gem5 includes
1172632Sstever@eecs.umich.edu    Dir('ext/ply').srcnode().abspath, # ply is used by several files
1182632Sstever@eecs.umich.edu    ]
1192632Sstever@eecs.umich.edu
1202632Sstever@eecs.umich.edusys.path[1:1] = extra_python_paths
1212632Sstever@eecs.umich.edu
1222632Sstever@eecs.umich.edufrom m5.util import compareVersions, readCommand
1233053Sstever@eecs.umich.edufrom m5.util.terminal import get_termcap
1243053Sstever@eecs.umich.edu
1253053Sstever@eecs.umich.eduhelp_texts = {
1263053Sstever@eecs.umich.edu    "options" : "",
1273053Sstever@eecs.umich.edu    "global_vars" : "",
1283053Sstever@eecs.umich.edu    "local_vars" : ""
1293053Sstever@eecs.umich.edu}
1303053Sstever@eecs.umich.edu
1313053Sstever@eecs.umich.eduExport("help_texts")
1323053Sstever@eecs.umich.edu
1333053Sstever@eecs.umich.edu
1343053Sstever@eecs.umich.edu# There's a bug in scons in that (1) by default, the help texts from
1353053Sstever@eecs.umich.edu# AddOption() are supposed to be displayed when you type 'scons -h'
1363053Sstever@eecs.umich.edu# and (2) you can override the help displayed by 'scons -h' using the
1373053Sstever@eecs.umich.edu# Help() function, but these two features are incompatible: once
1383053Sstever@eecs.umich.edu# you've overridden the help text using Help(), there's no way to get
1392632Sstever@eecs.umich.edu# at the help texts from AddOptions.  See:
1402632Sstever@eecs.umich.edu#     http://scons.tigris.org/issues/show_bug.cgi?id=2356
1412632Sstever@eecs.umich.edu#     http://scons.tigris.org/issues/show_bug.cgi?id=2611
1422632Sstever@eecs.umich.edu# This hack lets us extract the help text from AddOptions and
1432632Sstever@eecs.umich.edu# re-inject it via Help().  Ideally someday this bug will be fixed and
1442632Sstever@eecs.umich.edu# we can just use AddOption directly.
1452634Sstever@eecs.umich.edudef AddLocalOption(*args, **kwargs):
1462634Sstever@eecs.umich.edu    col_width = 30
1472632Sstever@eecs.umich.edu
1482638Sstever@eecs.umich.edu    help = "  " + ", ".join(args)
1492632Sstever@eecs.umich.edu    if "help" in kwargs:
1502632Sstever@eecs.umich.edu        length = len(help)
1512632Sstever@eecs.umich.edu        if length >= col_width:
1522632Sstever@eecs.umich.edu            help += "\n" + " " * col_width
1532632Sstever@eecs.umich.edu        else:
1542632Sstever@eecs.umich.edu            help += " " * (col_width - length)
1551858SN/A        help += kwargs["help"]
1563716Sstever@eecs.umich.edu    help_texts["options"] += help + "\n"
1572638Sstever@eecs.umich.edu
1582638Sstever@eecs.umich.edu    AddOption(*args, **kwargs)
1592638Sstever@eecs.umich.edu
1602638Sstever@eecs.umich.eduAddLocalOption('--colors', dest='use_colors', action='store_true',
1612638Sstever@eecs.umich.edu               help="Add color to abbreviated scons output")
1622638Sstever@eecs.umich.eduAddLocalOption('--no-colors', dest='use_colors', action='store_false',
1632638Sstever@eecs.umich.edu               help="Don't add color to abbreviated scons output")
1643716Sstever@eecs.umich.eduAddLocalOption('--default', dest='default', type='string', action='store',
1652634Sstever@eecs.umich.edu               help='Override which build_opts file to use for defaults')
1662634Sstever@eecs.umich.eduAddLocalOption('--ignore-style', dest='ignore_style', action='store_true',
167955SN/A               help='Disable style checking hooks')
168955SN/AAddLocalOption('--no-lto', dest='no_lto', action='store_true',
169955SN/A               help='Disable Link-Time Optimization for fast')
170955SN/AAddLocalOption('--update-ref', dest='update_ref', action='store_true',
171955SN/A               help='Update test reference outputs')
172955SN/AAddLocalOption('--verbose', dest='verbose', action='store_true',
173955SN/A               help='Print full tool command lines')
174955SN/A
1751858SN/Atermcap = get_termcap(GetOption('use_colors'))
1761858SN/A
1772632Sstever@eecs.umich.edu########################################################################
178955SN/A#
1793643Ssaidi@eecs.umich.edu# Set up the main build environment.
1803643Ssaidi@eecs.umich.edu#
1813643Ssaidi@eecs.umich.edu########################################################################
1823643Ssaidi@eecs.umich.eduuse_vars = set([ 'AS', 'AR', 'CC', 'CXX', 'HOME', 'LD_LIBRARY_PATH',
1833643Ssaidi@eecs.umich.edu                 'LIBRARY_PATH', 'PATH', 'PKG_CONFIG_PATH', 'PYTHONPATH',
1843643Ssaidi@eecs.umich.edu                 'RANLIB', 'SWIG' ])
1853643Ssaidi@eecs.umich.edu
1863643Ssaidi@eecs.umich.eduuse_prefixes = [
1873716Sstever@eecs.umich.edu    "M5",           # M5 configuration (e.g., path to kernels)
1881105SN/A    "DISTCC_",      # distcc (distributed compiler wrapper) configuration
1892667Sstever@eecs.umich.edu    "CCACHE_",      # ccache (caching compiler wrapper) configuration
1902667Sstever@eecs.umich.edu    "CCC_",         # clang static analyzer configuration
1912667Sstever@eecs.umich.edu    ]
1922667Sstever@eecs.umich.edu
1932667Sstever@eecs.umich.eduuse_env = {}
1942667Sstever@eecs.umich.edufor key,val in os.environ.iteritems():
1951869SN/A    if key in use_vars or \
1961869SN/A            any([key.startswith(prefix) for prefix in use_prefixes]):
1971869SN/A        use_env[key] = val
1981869SN/A
1991869SN/Amain = Environment(ENV=use_env)
2001065SN/Amain.Decider('MD5-timestamp')
2012632Sstever@eecs.umich.edumain.root = Dir(".")         # The current directory (where this file lives).
2022632Sstever@eecs.umich.edumain.srcdir = Dir("src")     # The source directory
203955SN/A
2041858SN/Amain_dict_keys = main.Dictionary().keys()
2051858SN/A
2061858SN/A# Check that we have a C/C++ compiler
2071858SN/Aif not ('CC' in main_dict_keys and 'CXX' in main_dict_keys):
2081851SN/A    print "No C++ compiler installed (package g++ on Ubuntu and RedHat)"
2091851SN/A    Exit(1)
2101858SN/A
2112632Sstever@eecs.umich.edu# Check that swig is present
212955SN/Aif not 'SWIG' in main_dict_keys:
2133053Sstever@eecs.umich.edu    print "swig is not installed (package swig on Ubuntu and RedHat)"
2143053Sstever@eecs.umich.edu    Exit(1)
2153053Sstever@eecs.umich.edu
2163053Sstever@eecs.umich.edu# add useful python code PYTHONPATH so it can be used by subprocesses
2173053Sstever@eecs.umich.edu# as well
2183053Sstever@eecs.umich.edumain.AppendENVPath('PYTHONPATH', extra_python_paths)
2193053Sstever@eecs.umich.edu
2203053Sstever@eecs.umich.edu########################################################################
2213053Sstever@eecs.umich.edu#
2223053Sstever@eecs.umich.edu# Mercurial Stuff.
2233053Sstever@eecs.umich.edu#
2243053Sstever@eecs.umich.edu# If the gem5 directory is a mercurial repository, we should do some
2253053Sstever@eecs.umich.edu# extra things.
2263053Sstever@eecs.umich.edu#
2273053Sstever@eecs.umich.edu########################################################################
2283053Sstever@eecs.umich.edu
2293053Sstever@eecs.umich.eduhgdir = main.root.Dir(".hg")
2303053Sstever@eecs.umich.edu
2313053Sstever@eecs.umich.edumercurial_style_message = """
2322667Sstever@eecs.umich.eduYou're missing the gem5 style hook, which automatically checks your code
2332667Sstever@eecs.umich.eduagainst the gem5 style rules on hg commit and qrefresh commands.  This
2342667Sstever@eecs.umich.eduscript will now install the hook in your .hg/hgrc file.
2352667Sstever@eecs.umich.eduPress enter to continue, or ctrl-c to abort: """
2362667Sstever@eecs.umich.edu
2372667Sstever@eecs.umich.edumercurial_style_hook = """
2382667Sstever@eecs.umich.edu# The following lines were automatically added by gem5/SConstruct
2392667Sstever@eecs.umich.edu# to provide the gem5 style-checking hooks
2402667Sstever@eecs.umich.edu[extensions]
2412667Sstever@eecs.umich.edustyle = %s/util/style.py
2422667Sstever@eecs.umich.edu
2432667Sstever@eecs.umich.edu[hooks]
2442638Sstever@eecs.umich.edupretxncommit.style = python:style.check_style
2452638Sstever@eecs.umich.edupre-qrefresh.style = python:style.check_style
2462638Sstever@eecs.umich.edu# End of SConstruct additions
2473716Sstever@eecs.umich.edu
2483716Sstever@eecs.umich.edu""" % (main.root.abspath)
2491858SN/A
2503118Sstever@eecs.umich.edumercurial_lib_not_found = """
2513118Sstever@eecs.umich.eduMercurial libraries cannot be found, ignoring style hook.  If
2523118Sstever@eecs.umich.eduyou are a gem5 developer, please fix this and run the style
2533118Sstever@eecs.umich.eduhook. It is important.
2543118Sstever@eecs.umich.edu"""
2553118Sstever@eecs.umich.edu
2563118Sstever@eecs.umich.edu# Check for style hook and prompt for installation if it's not there.
2573118Sstever@eecs.umich.edu# Skip this if --ignore-style was specified, there's no .hg dir to
2583118Sstever@eecs.umich.edu# install a hook in, or there's no interactive terminal to prompt.
2593118Sstever@eecs.umich.eduif not GetOption('ignore_style') and hgdir.exists() and sys.stdin.isatty():
2603118Sstever@eecs.umich.edu    style_hook = True
2613716Sstever@eecs.umich.edu    try:
2623118Sstever@eecs.umich.edu        from mercurial import ui
2633118Sstever@eecs.umich.edu        ui = ui.ui()
2643118Sstever@eecs.umich.edu        ui.readconfig(hgdir.File('hgrc').abspath)
2653118Sstever@eecs.umich.edu        style_hook = ui.config('hooks', 'pretxncommit.style', None) and \
2663118Sstever@eecs.umich.edu                     ui.config('hooks', 'pre-qrefresh.style', None)
2673118Sstever@eecs.umich.edu    except ImportError:
2683118Sstever@eecs.umich.edu        print mercurial_lib_not_found
2693118Sstever@eecs.umich.edu
2703118Sstever@eecs.umich.edu    if not style_hook:
2713716Sstever@eecs.umich.edu        print mercurial_style_message,
2723118Sstever@eecs.umich.edu        # continue unless user does ctrl-c/ctrl-d etc.
2733118Sstever@eecs.umich.edu        try:
2743118Sstever@eecs.umich.edu            raw_input()
2753118Sstever@eecs.umich.edu        except:
2763118Sstever@eecs.umich.edu            print "Input exception, exiting scons.\n"
2773118Sstever@eecs.umich.edu            sys.exit(1)
2783118Sstever@eecs.umich.edu        hgrc_path = '%s/.hg/hgrc' % main.root.abspath
2793118Sstever@eecs.umich.edu        print "Adding style hook to", hgrc_path, "\n"
2803118Sstever@eecs.umich.edu        try:
2813118Sstever@eecs.umich.edu            hgrc = open(hgrc_path, 'a')
2823483Ssaidi@eecs.umich.edu            hgrc.write(mercurial_style_hook)
2833494Ssaidi@eecs.umich.edu            hgrc.close()
2843494Ssaidi@eecs.umich.edu        except:
2853483Ssaidi@eecs.umich.edu            print "Error updating", hgrc_path
2863483Ssaidi@eecs.umich.edu            sys.exit(1)
2873483Ssaidi@eecs.umich.edu
2883053Sstever@eecs.umich.edu
2893053Sstever@eecs.umich.edu###################################################
2903053Sstever@eecs.umich.edu#
2913053Sstever@eecs.umich.edu# Figure out which configurations to set up based on the path(s) of
2923053Sstever@eecs.umich.edu# the target(s).
2933053Sstever@eecs.umich.edu#
2943053Sstever@eecs.umich.edu###################################################
2953053Sstever@eecs.umich.edu
2961858SN/A# Find default configuration & binary.
2971858SN/ADefault(environ.get('M5_DEFAULT_BINARY', 'build/ALPHA/gem5.debug'))
2981858SN/A
2991858SN/A# helper function: find last occurrence of element in list
3001858SN/Adef rfind(l, elt, offs = -1):
3011858SN/A    for i in range(len(l)+offs, 0, -1):
3021859SN/A        if l[i] == elt:
3031858SN/A            return i
3041858SN/A    raise ValueError, "element not found"
3051858SN/A
3061859SN/A# Take a list of paths (or SCons Nodes) and return a list with all
3071859SN/A# paths made absolute and ~-expanded.  Paths will be interpreted
3081862SN/A# relative to the launch directory unless a different root is provided
3093053Sstever@eecs.umich.edudef makePathListAbsolute(path_list, root=GetLaunchDir()):
3103053Sstever@eecs.umich.edu    return [abspath(joinpath(root, expanduser(str(p))))
3113053Sstever@eecs.umich.edu            for p in path_list]
3123053Sstever@eecs.umich.edu
3131859SN/A# Each target must have 'build' in the interior of the path; the
3141859SN/A# directory below this will determine the build parameters.  For
3151859SN/A# example, for target 'foo/bar/build/ALPHA_SE/arch/alpha/blah.do' we
3161859SN/A# recognize that ALPHA_SE specifies the configuration because it
3171859SN/A# follow 'build' in the build path.
3181859SN/A
3191859SN/A# The funky assignment to "[:]" is needed to replace the list contents
3201859SN/A# in place rather than reassign the symbol to a new list, which
3211862SN/A# doesn't work (obviously!).
3221859SN/ABUILD_TARGETS[:] = makePathListAbsolute(BUILD_TARGETS)
3231859SN/A
3241859SN/A# Generate a list of the unique build roots and configs that the
3251858SN/A# collected targets reference.
3261858SN/Avariant_paths = []
3272139SN/Abuild_root = None
3282139SN/Afor t in BUILD_TARGETS:
3292139SN/A    path_dirs = t.split('/')
3302155SN/A    try:
3312623SN/A        build_top = rfind(path_dirs, 'build', -2)
3323583Sbinkertn@umich.edu    except:
3333583Sbinkertn@umich.edu        print "Error: no non-leaf 'build' dir found on target path", t
3343716Sstever@eecs.umich.edu        Exit(1)
3353583Sbinkertn@umich.edu    this_build_root = joinpath('/',*path_dirs[:build_top+1])
3362155SN/A    if not build_root:
3371869SN/A        build_root = this_build_root
3381869SN/A    else:
3391869SN/A        if this_build_root != build_root:
3401869SN/A            print "Error: build targets not under same build root\n"\
3411869SN/A                  "  %s\n  %s" % (build_root, this_build_root)
3422139SN/A            Exit(1)
3431869SN/A    variant_path = joinpath('/',*path_dirs[:build_top+2])
3442508SN/A    if variant_path not in variant_paths:
3452508SN/A        variant_paths.append(variant_path)
3462508SN/A
3472508SN/A# Make sure build_root exists (might not if this is the first build there)
3483685Sktlim@umich.eduif not isdir(build_root):
3492635Sstever@eecs.umich.edu    mkdir(build_root)
3501869SN/Amain['BUILDROOT'] = build_root
3511869SN/A
3521869SN/AExport('main')
3531869SN/A
3541869SN/Amain.SConsignFile(joinpath(build_root, "sconsign"))
3551869SN/A
3561869SN/A# Default duplicate option is to use hard links, but this messes up
3571869SN/A# when you use emacs to edit a file in the target dir, as emacs moves
3581965SN/A# file to file~ then copies to file, breaking the link.  Symbolic
3591965SN/A# (soft) links work better.
3601965SN/Amain.SetOption('duplicate', 'soft-copy')
3611869SN/A
3621869SN/A#
3632733Sktlim@umich.edu# Set up global sticky variables... these are common to an entire build
3641869SN/A# tree (not specific to a particular build like ALPHA_SE)
3651884SN/A#
3661884SN/A
3673356Sbinkertn@umich.eduglobal_vars_file = joinpath(build_root, 'variables.global')
3683356Sbinkertn@umich.edu
3693356Sbinkertn@umich.eduglobal_vars = Variables(global_vars_file, args=ARGUMENTS)
3703356Sbinkertn@umich.edu
3711869SN/Aglobal_vars.AddVariables(
3721858SN/A    ('CC', 'C compiler', environ.get('CC', main['CC'])),
3731869SN/A    ('CXX', 'C++ compiler', environ.get('CXX', main['CXX'])),
3741869SN/A    ('SWIG', 'SWIG tool', environ.get('SWIG', main['SWIG'])),
3751869SN/A    ('PROTOC', 'protoc tool', environ.get('PROTOC', 'protoc')),
3761869SN/A    ('BATCH', 'Use batch pool for build and tests', False),
3771869SN/A    ('BATCH_CMD', 'Batch pool submission command name', 'qdo'),
3781858SN/A    ('M5_BUILD_CACHE', 'Cache built objects in this directory', False),
3792761Sstever@eecs.umich.edu    ('EXTRAS', 'Add extra directories to the compilation', '')
3801869SN/A    )
3812733Sktlim@umich.edu
3823584Ssaidi@eecs.umich.edu# Update main environment with values from ARGUMENTS & global_vars_file
3831869SN/Aglobal_vars.Update(main)
3841869SN/Ahelp_texts["global_vars"] += global_vars.GenerateHelpText(main)
3851869SN/A
3861869SN/A# Save sticky variable settings back to current variables file
3871869SN/Aglobal_vars.Save(global_vars_file, main)
3881869SN/A
3891858SN/A# Parse EXTRAS variable to build list of all directories where we're
390955SN/A# look for sources etc.  This list is exported as extras_dir_list.
391955SN/Abase_dir = main.srcdir.abspath
3921869SN/Aif main['EXTRAS']:
3931869SN/A    extras_dir_list = makePathListAbsolute(main['EXTRAS'].split(':'))
3941869SN/Aelse:
3951869SN/A    extras_dir_list = []
3961869SN/A
3971869SN/AExport('base_dir')
3981869SN/AExport('extras_dir_list')
3991869SN/A
4001869SN/A# the ext directory should be on the #includes path
4011869SN/Amain.Append(CPPPATH=[Dir('ext')])
4021869SN/A
4031869SN/Adef strip_build_path(path, env):
4041869SN/A    path = str(path)
4051869SN/A    variant_base = env['BUILDROOT'] + os.path.sep
4061869SN/A    if path.startswith(variant_base):
4071869SN/A        path = path[len(variant_base):]
4081869SN/A    elif path.startswith('build/'):
4091869SN/A        path = path[6:]
4101869SN/A    return path
4111869SN/A
4121869SN/A# Generate a string of the form:
4131869SN/A#   common/path/prefix/src1, src2 -> tgt1, tgt2
4141869SN/A# to print while building.
4151869SN/Aclass Transform(object):
4161869SN/A    # all specific color settings should be here and nowhere else
4171869SN/A    tool_color = termcap.Normal
4181869SN/A    pfx_color = termcap.Yellow
4191869SN/A    srcs_color = termcap.Yellow + termcap.Bold
4201869SN/A    arrow_color = termcap.Blue + termcap.Bold
4213716Sstever@eecs.umich.edu    tgts_color = termcap.Yellow + termcap.Bold
4223356Sbinkertn@umich.edu
4233356Sbinkertn@umich.edu    def __init__(self, tool, max_sources=99):
4243356Sbinkertn@umich.edu        self.format = self.tool_color + (" [%8s] " % tool) \
4253356Sbinkertn@umich.edu                      + self.pfx_color + "%s" \
4263356Sbinkertn@umich.edu                      + self.srcs_color + "%s" \
4273356Sbinkertn@umich.edu                      + self.arrow_color + " -> " \
4283356Sbinkertn@umich.edu                      + self.tgts_color + "%s" \
4291869SN/A                      + termcap.Normal
4301869SN/A        self.max_sources = max_sources
4311869SN/A
4321869SN/A    def __call__(self, target, source, env, for_signature=None):
4331869SN/A        # truncate source list according to max_sources param
4341869SN/A        source = source[0:self.max_sources]
4351869SN/A        def strip(f):
4362655Sstever@eecs.umich.edu            return strip_build_path(str(f), env)
4372655Sstever@eecs.umich.edu        if len(source) > 0:
4382655Sstever@eecs.umich.edu            srcs = map(strip, source)
4392655Sstever@eecs.umich.edu        else:
4402655Sstever@eecs.umich.edu            srcs = ['']
4412655Sstever@eecs.umich.edu        tgts = map(strip, target)
4422655Sstever@eecs.umich.edu        # surprisingly, os.path.commonprefix is a dumb char-by-char string
4432655Sstever@eecs.umich.edu        # operation that has nothing to do with paths.
4442655Sstever@eecs.umich.edu        com_pfx = os.path.commonprefix(srcs + tgts)
4452655Sstever@eecs.umich.edu        com_pfx_len = len(com_pfx)
4462655Sstever@eecs.umich.edu        if com_pfx:
4472655Sstever@eecs.umich.edu            # do some cleanup and sanity checking on common prefix
4482655Sstever@eecs.umich.edu            if com_pfx[-1] == ".":
4492655Sstever@eecs.umich.edu                # prefix matches all but file extension: ok
4502655Sstever@eecs.umich.edu                # back up one to change 'foo.cc -> o' to 'foo.cc -> .o'
4512655Sstever@eecs.umich.edu                com_pfx = com_pfx[0:-1]
4522655Sstever@eecs.umich.edu            elif com_pfx[-1] == "/":
4532655Sstever@eecs.umich.edu                # common prefix is directory path: OK
4542655Sstever@eecs.umich.edu                pass
4552655Sstever@eecs.umich.edu            else:
4562655Sstever@eecs.umich.edu                src0_len = len(srcs[0])
4572655Sstever@eecs.umich.edu                tgt0_len = len(tgts[0])
4582655Sstever@eecs.umich.edu                if src0_len == com_pfx_len:
4592655Sstever@eecs.umich.edu                    # source is a substring of target, OK
4602655Sstever@eecs.umich.edu                    pass
4612655Sstever@eecs.umich.edu                elif tgt0_len == com_pfx_len:
4622634Sstever@eecs.umich.edu                    # target is a substring of source, need to back up to
4632634Sstever@eecs.umich.edu                    # avoid empty string on RHS of arrow
4642634Sstever@eecs.umich.edu                    sep_idx = com_pfx.rfind(".")
4652634Sstever@eecs.umich.edu                    if sep_idx != -1:
4662634Sstever@eecs.umich.edu                        com_pfx = com_pfx[0:sep_idx]
4672634Sstever@eecs.umich.edu                    else:
4682638Sstever@eecs.umich.edu                        com_pfx = ''
4692638Sstever@eecs.umich.edu                elif src0_len > com_pfx_len and srcs[0][com_pfx_len] == ".":
4703716Sstever@eecs.umich.edu                    # still splitting at file extension: ok
4712638Sstever@eecs.umich.edu                    pass
4722638Sstever@eecs.umich.edu                else:
4731869SN/A                    # probably a fluke; ignore it
4741869SN/A                    com_pfx = ''
4753546Sgblack@eecs.umich.edu        # recalculate length in case com_pfx was modified
4763546Sgblack@eecs.umich.edu        com_pfx_len = len(com_pfx)
4773546Sgblack@eecs.umich.edu        def fmt(files):
4783546Sgblack@eecs.umich.edu            f = map(lambda s: s[com_pfx_len:], files)
4793546Sgblack@eecs.umich.edu            return ', '.join(f)
4803546Sgblack@eecs.umich.edu        return self.format % (com_pfx, fmt(srcs), fmt(tgts))
4813546Sgblack@eecs.umich.edu
4823546Sgblack@eecs.umich.eduExport('Transform')
4833546Sgblack@eecs.umich.edu
4843546Sgblack@eecs.umich.edu# enable the regression script to use the termcap
4853546Sgblack@eecs.umich.edumain['TERMCAP'] = termcap
4863546Sgblack@eecs.umich.edu
4873546Sgblack@eecs.umich.eduif GetOption('verbose'):
4883546Sgblack@eecs.umich.edu    def MakeAction(action, string, *args, **kwargs):
4893546Sgblack@eecs.umich.edu        return Action(action, *args, **kwargs)
4903546Sgblack@eecs.umich.eduelse:
4913546Sgblack@eecs.umich.edu    MakeAction = Action
4923546Sgblack@eecs.umich.edu    main['CCCOMSTR']        = Transform("CC")
4933546Sgblack@eecs.umich.edu    main['CXXCOMSTR']       = Transform("CXX")
4943546Sgblack@eecs.umich.edu    main['ASCOMSTR']        = Transform("AS")
4953546Sgblack@eecs.umich.edu    main['SWIGCOMSTR']      = Transform("SWIG")
4963546Sgblack@eecs.umich.edu    main['ARCOMSTR']        = Transform("AR", 0)
4973546Sgblack@eecs.umich.edu    main['LINKCOMSTR']      = Transform("LINK", 0)
4983546Sgblack@eecs.umich.edu    main['RANLIBCOMSTR']    = Transform("RANLIB", 0)
4993546Sgblack@eecs.umich.edu    main['M4COMSTR']        = Transform("M4")
5003546Sgblack@eecs.umich.edu    main['SHCCCOMSTR']      = Transform("SHCC")
5013546Sgblack@eecs.umich.edu    main['SHCXXCOMSTR']     = Transform("SHCXX")
5023546Sgblack@eecs.umich.eduExport('MakeAction')
5033546Sgblack@eecs.umich.edu
5043546Sgblack@eecs.umich.edu# Initialize the Link-Time Optimization (LTO) flags
5053546Sgblack@eecs.umich.edumain['LTO_CCFLAGS'] = []
5063546Sgblack@eecs.umich.edumain['LTO_LDFLAGS'] = []
5073546Sgblack@eecs.umich.edu
5083546Sgblack@eecs.umich.edu# According to the readme, tcmalloc works best if the compiler doesn't
5093546Sgblack@eecs.umich.edu# assume that we're using the builtin malloc and friends. These flags
5103546Sgblack@eecs.umich.edu# are compiler-specific, so we need to set them after we detect which
5113546Sgblack@eecs.umich.edu# compiler we're using.
5123546Sgblack@eecs.umich.edumain['TCMALLOC_CCFLAGS'] = []
5133546Sgblack@eecs.umich.edu
5143546Sgblack@eecs.umich.eduCXX_version = readCommand([main['CXX'],'--version'], exception=False)
515955SN/ACXX_V = readCommand([main['CXX'],'-V'], exception=False)
516955SN/A
517955SN/Amain['GCC'] = CXX_version and CXX_version.find('g++') >= 0
518955SN/Amain['CLANG'] = CXX_version and CXX_version.find('clang') >= 0
5191858SN/Aif main['GCC'] + main['CLANG'] > 1:
5201858SN/A    print 'Error: How can we have two at the same time?'
5211858SN/A    Exit(1)
5222632Sstever@eecs.umich.edu
5232632Sstever@eecs.umich.edu# Set up default C++ compiler flags
5242632Sstever@eecs.umich.eduif main['GCC'] or main['CLANG']:
5252632Sstever@eecs.umich.edu    # As gcc and clang share many flags, do the common parts here
5262632Sstever@eecs.umich.edu    main.Append(CCFLAGS=['-pipe'])
5272634Sstever@eecs.umich.edu    main.Append(CCFLAGS=['-fno-strict-aliasing'])
5282638Sstever@eecs.umich.edu    # Enable -Wall and then disable the few warnings that we
5292023SN/A    # consistently violate
5302632Sstever@eecs.umich.edu    main.Append(CCFLAGS=['-Wall', '-Wno-sign-compare', '-Wundef'])
5312632Sstever@eecs.umich.edu    # We always compile using C++11, but only gcc >= 4.7 and clang 3.1
5322632Sstever@eecs.umich.edu    # actually use that name, so we stick with c++0x
5332632Sstever@eecs.umich.edu    main.Append(CXXFLAGS=['-std=c++0x'])
5342632Sstever@eecs.umich.edu    # Add selected sanity checks from -Wextra
5353716Sstever@eecs.umich.edu    main.Append(CXXFLAGS=['-Wmissing-field-initializers',
5362632Sstever@eecs.umich.edu                          '-Woverloaded-virtual'])
5372632Sstever@eecs.umich.eduelse:
5382632Sstever@eecs.umich.edu    print termcap.Yellow + termcap.Bold + 'Error' + termcap.Normal,
5392632Sstever@eecs.umich.edu    print "Don't know what compiler options to use for your compiler."
5402632Sstever@eecs.umich.edu    print termcap.Yellow + '       compiler:' + termcap.Normal, main['CXX']
5412023SN/A    print termcap.Yellow + '       version:' + termcap.Normal,
5422632Sstever@eecs.umich.edu    if not CXX_version:
5432632Sstever@eecs.umich.edu        print termcap.Yellow + termcap.Bold + "COMMAND NOT FOUND!" +\
5441889SN/A               termcap.Normal
5451889SN/A    else:
5462632Sstever@eecs.umich.edu        print CXX_version.replace('\n', '<nl>')
5472632Sstever@eecs.umich.edu    print "       If you're trying to use a compiler other than GCC"
5482632Sstever@eecs.umich.edu    print "       or clang, there appears to be something wrong with your"
5492632Sstever@eecs.umich.edu    print "       environment."
5503716Sstever@eecs.umich.edu    print "       "
5513716Sstever@eecs.umich.edu    print "       If you are trying to use a compiler other than those listed"
5522632Sstever@eecs.umich.edu    print "       above you will need to ease fix SConstruct and "
5532632Sstever@eecs.umich.edu    print "       src/SConscript to support that compiler."
5542632Sstever@eecs.umich.edu    Exit(1)
5552632Sstever@eecs.umich.edu
5562632Sstever@eecs.umich.eduif main['GCC']:
5572632Sstever@eecs.umich.edu    # Check for a supported version of gcc, >= 4.4 is needed for c++0x
5582632Sstever@eecs.umich.edu    # support. See http://gcc.gnu.org/projects/cxx0x.html for details
5592632Sstever@eecs.umich.edu    gcc_version = readCommand([main['CXX'], '-dumpversion'], exception=False)
5601888SN/A    if compareVersions(gcc_version, "4.4") < 0:
5611888SN/A        print 'Error: gcc version 4.4 or newer required.'
5621869SN/A        print '       Installed version:', gcc_version
5631869SN/A        Exit(1)
5641858SN/A
5652598SN/A    main['GCC_VERSION'] = gcc_version
5662598SN/A
5672598SN/A    # Check for versions with bugs
5682598SN/A    if not compareVersions(gcc_version, '4.4.1') or \
5692598SN/A       not compareVersions(gcc_version, '4.4.2'):
5701858SN/A        print 'Info: Tree vectorizer in GCC 4.4.1 & 4.4.2 is buggy, disabling.'
5711858SN/A        main.Append(CCFLAGS=['-fno-tree-vectorize'])
5721858SN/A
5731858SN/A    # LTO support is only really working properly from 4.6 and beyond
5741858SN/A    if compareVersions(gcc_version, '4.6') >= 0:
5751858SN/A        # Add the appropriate Link-Time Optimization (LTO) flags
5761858SN/A        # unless LTO is explicitly turned off. Note that these flags
5771858SN/A        # are only used by the fast target.
5781858SN/A        if not GetOption('no_lto'):
5791871SN/A            # Pass the LTO flag when compiling to produce GIMPLE
5801858SN/A            # output, we merely create the flags here and only append
5811858SN/A            # them later/
5821858SN/A            main['LTO_CCFLAGS'] = ['-flto=%d' % GetOption('num_jobs')]
5831858SN/A
5841858SN/A            # Use the same amount of jobs for LTO as we are running
5851858SN/A            # scons with, we hardcode the use of the linker plugin
5861858SN/A            # which requires either gold or GNU ld >= 2.21
5871858SN/A            main['LTO_LDFLAGS'] = ['-flto=%d' % GetOption('num_jobs'),
5881858SN/A                                   '-fuse-linker-plugin']
5891858SN/A
5901858SN/A    main.Append(TCMALLOC_CCFLAGS=['-fno-builtin-malloc', '-fno-builtin-calloc',
5911859SN/A                                  '-fno-builtin-realloc', '-fno-builtin-free'])
5921859SN/A
5931869SN/Aelif main['CLANG']:
5941888SN/A    # Check for a supported version of clang, >= 2.9 is needed to
5952632Sstever@eecs.umich.edu    # support similar features as gcc 4.4. See
5961869SN/A    # http://clang.llvm.org/cxx_status.html for details
5971884SN/A    clang_version_re = re.compile(".* version (\d+\.\d+)")
5981884SN/A    clang_version_match = clang_version_re.match(CXX_version)
5991884SN/A    if (clang_version_match):
6001884SN/A        clang_version = clang_version_match.groups()[0]
6011884SN/A        if compareVersions(clang_version, "2.9") < 0:
6021884SN/A            print 'Error: clang version 2.9 or newer required.'
6031965SN/A            print '       Installed version:', clang_version
6041965SN/A            Exit(1)
6051965SN/A    else:
6062761Sstever@eecs.umich.edu        print 'Error: Unable to determine clang version.'
6071869SN/A        Exit(1)
6081869SN/A
6092632Sstever@eecs.umich.edu    # clang has a few additional warnings that we disable,
6102667Sstever@eecs.umich.edu    # tautological comparisons are allowed due to unsigned integers
6111869SN/A    # being compared to constants that happen to be 0, and extraneous
6121869SN/A    # parantheses are allowed due to Ruby's printing of the AST,
6132929Sktlim@umich.edu    # finally self assignments are allowed as the generated CPU code
6142929Sktlim@umich.edu    # is relying on this
6153716Sstever@eecs.umich.edu    main.Append(CCFLAGS=['-Wno-tautological-compare',
6162929Sktlim@umich.edu                         '-Wno-parentheses',
617955SN/A                         '-Wno-self-assign'])
6182598SN/A
6192598SN/A    main.Append(TCMALLOC_CCFLAGS=['-fno-builtin'])
6203546Sgblack@eecs.umich.edu
621955SN/A    # On Mac OS X/Darwin we need to also use libc++ (part of XCode) as
622955SN/A    # opposed to libstdc++ to make the transition from TR1 to
623955SN/A    # C++11. See http://libcxx.llvm.org. However, clang has chosen a
6241530SN/A    # strict implementation of the C++11 standard, and does not allow
625955SN/A    # incomplete types in template arguments (besides unique_ptr and
626955SN/A    # shared_ptr), and the libc++ STL containers create problems in
627955SN/A    # combination with the current gem5 code. For now, we stick with
628    # libstdc++ and use the TR1 namespace.
629    # if sys.platform == "darwin":
630    #     main.Append(CXXFLAGS=['-stdlib=libc++'])
631
632else:
633    print termcap.Yellow + termcap.Bold + 'Error' + termcap.Normal,
634    print "Don't know what compiler options to use for your compiler."
635    print termcap.Yellow + '       compiler:' + termcap.Normal, main['CXX']
636    print termcap.Yellow + '       version:' + termcap.Normal,
637    if not CXX_version:
638        print termcap.Yellow + termcap.Bold + "COMMAND NOT FOUND!" +\
639               termcap.Normal
640    else:
641        print CXX_version.replace('\n', '<nl>')
642    print "       If you're trying to use a compiler other than GCC"
643    print "       or clang, there appears to be something wrong with your"
644    print "       environment."
645    print "       "
646    print "       If you are trying to use a compiler other than those listed"
647    print "       above you will need to ease fix SConstruct and "
648    print "       src/SConscript to support that compiler."
649    Exit(1)
650
651# Set up common yacc/bison flags (needed for Ruby)
652main['YACCFLAGS'] = '-d'
653main['YACCHXXFILESUFFIX'] = '.hh'
654
655# Do this after we save setting back, or else we'll tack on an
656# extra 'qdo' every time we run scons.
657if main['BATCH']:
658    main['CC']     = main['BATCH_CMD'] + ' ' + main['CC']
659    main['CXX']    = main['BATCH_CMD'] + ' ' + main['CXX']
660    main['AS']     = main['BATCH_CMD'] + ' ' + main['AS']
661    main['AR']     = main['BATCH_CMD'] + ' ' + main['AR']
662    main['RANLIB'] = main['BATCH_CMD'] + ' ' + main['RANLIB']
663
664if sys.platform == 'cygwin':
665    # cygwin has some header file issues...
666    main.Append(CCFLAGS=["-Wno-uninitialized"])
667
668# Check for the protobuf compiler
669protoc_version = readCommand([main['PROTOC'], '--version'],
670                             exception='').split()
671
672# First two words should be "libprotoc x.y.z"
673if len(protoc_version) < 2 or protoc_version[0] != 'libprotoc':
674    print termcap.Yellow + termcap.Bold + \
675        'Warning: Protocol buffer compiler (protoc) not found.\n' + \
676        '         Please install protobuf-compiler for tracing support.' + \
677        termcap.Normal
678    main['PROTOC'] = False
679else:
680    # Based on the availability of the compress stream wrappers,
681    # require 2.1.0
682    min_protoc_version = '2.1.0'
683    if compareVersions(protoc_version[1], min_protoc_version) < 0:
684        print termcap.Yellow + termcap.Bold + \
685            'Warning: protoc version', min_protoc_version, \
686            'or newer required.\n' + \
687            '         Installed version:', protoc_version[1], \
688            termcap.Normal
689        main['PROTOC'] = False
690    else:
691        # Attempt to determine the appropriate include path and
692        # library path using pkg-config, that means we also need to
693        # check for pkg-config. Note that it is possible to use
694        # protobuf without the involvement of pkg-config. Later on we
695        # check go a library config check and at that point the test
696        # will fail if libprotobuf cannot be found.
697        if readCommand(['pkg-config', '--version'], exception=''):
698            try:
699                # Attempt to establish what linking flags to add for protobuf
700                # using pkg-config
701                main.ParseConfig('pkg-config --cflags --libs-only-L protobuf')
702            except:
703                print termcap.Yellow + termcap.Bold + \
704                    'Warning: pkg-config could not get protobuf flags.' + \
705                    termcap.Normal
706
707# Check for SWIG
708if not main.has_key('SWIG'):
709    print 'Error: SWIG utility not found.'
710    print '       Please install (see http://www.swig.org) and retry.'
711    Exit(1)
712
713# Check for appropriate SWIG version
714swig_version = readCommand([main['SWIG'], '-version'], exception='').split()
715# First 3 words should be "SWIG Version x.y.z"
716if len(swig_version) < 3 or \
717        swig_version[0] != 'SWIG' or swig_version[1] != 'Version':
718    print 'Error determining SWIG version.'
719    Exit(1)
720
721min_swig_version = '1.3.34'
722if compareVersions(swig_version[2], min_swig_version) < 0:
723    print 'Error: SWIG version', min_swig_version, 'or newer required.'
724    print '       Installed version:', swig_version[2]
725    Exit(1)
726
727if swig_version[2] == "2.0.9":
728    print '\n' + termcap.Yellow + termcap.Bold + \
729        'Warning: SWIG version 2.0.9 sometimes generates broken code.\n' + \
730        termcap.Normal + \
731        'This problem only affects some platforms and some Python\n' + \
732        'versions. See the following SWIG bug report for details:\n' + \
733        'http://sourceforge.net/p/swig/bugs/1297/\n'
734
735
736# Set up SWIG flags & scanner
737swig_flags=Split('-c++ -python -modern -templatereduce $_CPPINCFLAGS')
738main.Append(SWIGFLAGS=swig_flags)
739
740# filter out all existing swig scanners, they mess up the dependency
741# stuff for some reason
742scanners = []
743for scanner in main['SCANNERS']:
744    skeys = scanner.skeys
745    if skeys == '.i':
746        continue
747
748    if isinstance(skeys, (list, tuple)) and '.i' in skeys:
749        continue
750
751    scanners.append(scanner)
752
753# add the new swig scanner that we like better
754from SCons.Scanner import ClassicCPP as CPPScanner
755swig_inc_re = '^[ \t]*[%,#][ \t]*(?:include|import)[ \t]*(<|")([^>"]+)(>|")'
756scanners.append(CPPScanner("SwigScan", [ ".i" ], "CPPPATH", swig_inc_re))
757
758# replace the scanners list that has what we want
759main['SCANNERS'] = scanners
760
761# Add a custom Check function to the Configure context so that we can
762# figure out if the compiler adds leading underscores to global
763# variables.  This is needed for the autogenerated asm files that we
764# use for embedding the python code.
765def CheckLeading(context):
766    context.Message("Checking for leading underscore in global variables...")
767    # 1) Define a global variable called x from asm so the C compiler
768    #    won't change the symbol at all.
769    # 2) Declare that variable.
770    # 3) Use the variable
771    #
772    # If the compiler prepends an underscore, this will successfully
773    # link because the external symbol 'x' will be called '_x' which
774    # was defined by the asm statement.  If the compiler does not
775    # prepend an underscore, this will not successfully link because
776    # '_x' will have been defined by assembly, while the C portion of
777    # the code will be trying to use 'x'
778    ret = context.TryLink('''
779        asm(".globl _x; _x: .byte 0");
780        extern int x;
781        int main() { return x; }
782        ''', extension=".c")
783    context.env.Append(LEADING_UNDERSCORE=ret)
784    context.Result(ret)
785    return ret
786
787# Platform-specific configuration.  Note again that we assume that all
788# builds under a given build root run on the same host platform.
789conf = Configure(main,
790                 conf_dir = joinpath(build_root, '.scons_config'),
791                 log_file = joinpath(build_root, 'scons_config.log'),
792                 custom_tests = { 'CheckLeading' : CheckLeading })
793
794# Check for leading underscores.  Don't really need to worry either
795# way so don't need to check the return code.
796conf.CheckLeading()
797
798# Check if we should compile a 64 bit binary on Mac OS X/Darwin
799try:
800    import platform
801    uname = platform.uname()
802    if uname[0] == 'Darwin' and compareVersions(uname[2], '9.0.0') >= 0:
803        if int(readCommand('sysctl -n hw.cpu64bit_capable')[0]):
804            main.Append(CCFLAGS=['-arch', 'x86_64'])
805            main.Append(CFLAGS=['-arch', 'x86_64'])
806            main.Append(LINKFLAGS=['-arch', 'x86_64'])
807            main.Append(ASFLAGS=['-arch', 'x86_64'])
808except:
809    pass
810
811# Recent versions of scons substitute a "Null" object for Configure()
812# when configuration isn't necessary, e.g., if the "--help" option is
813# present.  Unfortuantely this Null object always returns false,
814# breaking all our configuration checks.  We replace it with our own
815# more optimistic null object that returns True instead.
816if not conf:
817    def NullCheck(*args, **kwargs):
818        return True
819
820    class NullConf:
821        def __init__(self, env):
822            self.env = env
823        def Finish(self):
824            return self.env
825        def __getattr__(self, mname):
826            return NullCheck
827
828    conf = NullConf(main)
829
830# Find Python include and library directories for embedding the
831# interpreter.  For consistency, we will use the same Python
832# installation used to run scons (and thus this script).  If you want
833# to link in an alternate version, see above for instructions on how
834# to invoke scons with a different copy of the Python interpreter.
835from distutils import sysconfig
836
837py_getvar = sysconfig.get_config_var
838
839py_debug = getattr(sys, 'pydebug', False)
840py_version = 'python' + py_getvar('VERSION') + (py_debug and "_d" or "")
841
842py_general_include = sysconfig.get_python_inc()
843py_platform_include = sysconfig.get_python_inc(plat_specific=True)
844py_includes = [ py_general_include ]
845if py_platform_include != py_general_include:
846    py_includes.append(py_platform_include)
847
848py_lib_path = [ py_getvar('LIBDIR') ]
849# add the prefix/lib/pythonX.Y/config dir, but only if there is no
850# shared library in prefix/lib/.
851if not py_getvar('Py_ENABLE_SHARED'):
852    py_lib_path.append(py_getvar('LIBPL'))
853    # Python requires the flags in LINKFORSHARED to be added the
854    # linker flags when linking with a statically with Python. Failing
855    # to do so can lead to errors from the Python's dynamic module
856    # loader at start up.
857    main.Append(LINKFLAGS=[py_getvar('LINKFORSHARED').split()])
858
859py_libs = []
860for lib in py_getvar('LIBS').split() + py_getvar('SYSLIBS').split():
861    if not lib.startswith('-l'):
862        # Python requires some special flags to link (e.g. -framework
863        # common on OS X systems), assume appending preserves order
864        main.Append(LINKFLAGS=[lib])
865    else:
866        lib = lib[2:]
867        if lib not in py_libs:
868            py_libs.append(lib)
869py_libs.append(py_version)
870
871main.Append(CPPPATH=py_includes)
872main.Append(LIBPATH=py_lib_path)
873
874# Cache build files in the supplied directory.
875if main['M5_BUILD_CACHE']:
876    print 'Using build cache located at', main['M5_BUILD_CACHE']
877    CacheDir(main['M5_BUILD_CACHE'])
878
879
880# verify that this stuff works
881if not conf.CheckHeader('Python.h', '<>'):
882    print "Error: can't find Python.h header in", py_includes
883    print "Install Python headers (package python-dev on Ubuntu and RedHat)"
884    Exit(1)
885
886for lib in py_libs:
887    if not conf.CheckLib(lib):
888        print "Error: can't find library %s required by python" % lib
889        Exit(1)
890
891# On Solaris you need to use libsocket for socket ops
892if not conf.CheckLibWithHeader(None, 'sys/socket.h', 'C++', 'accept(0,0,0);'):
893   if not conf.CheckLibWithHeader('socket', 'sys/socket.h', 'C++', 'accept(0,0,0);'):
894       print "Can't find library with socket calls (e.g. accept())"
895       Exit(1)
896
897# Check for zlib.  If the check passes, libz will be automatically
898# added to the LIBS environment variable.
899if not conf.CheckLibWithHeader('z', 'zlib.h', 'C++','zlibVersion();'):
900    print 'Error: did not find needed zlib compression library '\
901          'and/or zlib.h header file.'
902    print '       Please install zlib and try again.'
903    Exit(1)
904
905# If we have the protobuf compiler, also make sure we have the
906# development libraries. If the check passes, libprotobuf will be
907# automatically added to the LIBS environment variable. After
908# this, we can use the HAVE_PROTOBUF flag to determine if we have
909# got both protoc and libprotobuf available.
910main['HAVE_PROTOBUF'] = main['PROTOC'] and \
911    conf.CheckLibWithHeader('protobuf', 'google/protobuf/message.h',
912                            'C++', 'GOOGLE_PROTOBUF_VERIFY_VERSION;')
913
914# If we have the compiler but not the library, print another warning.
915if main['PROTOC'] and not main['HAVE_PROTOBUF']:
916    print termcap.Yellow + termcap.Bold + \
917        'Warning: did not find protocol buffer library and/or headers.\n' + \
918    '       Please install libprotobuf-dev for tracing support.' + \
919    termcap.Normal
920
921# Check for librt.
922have_posix_clock = \
923    conf.CheckLibWithHeader(None, 'time.h', 'C',
924                            'clock_nanosleep(0,0,NULL,NULL);') or \
925    conf.CheckLibWithHeader('rt', 'time.h', 'C',
926                            'clock_nanosleep(0,0,NULL,NULL);')
927
928if conf.CheckLib('tcmalloc'):
929    main.Append(CCFLAGS=main['TCMALLOC_CCFLAGS'])
930elif conf.CheckLib('tcmalloc_minimal'):
931    main.Append(CCFLAGS=main['TCMALLOC_CCFLAGS'])
932else:
933    print termcap.Yellow + termcap.Bold + \
934          "You can get a 12% performance improvement by installing tcmalloc "\
935          "(libgoogle-perftools-dev package on Ubuntu or RedHat)." + \
936          termcap.Normal
937
938if not have_posix_clock:
939    print "Can't find library for POSIX clocks."
940
941# Check for <fenv.h> (C99 FP environment control)
942have_fenv = conf.CheckHeader('fenv.h', '<>')
943if not have_fenv:
944    print "Warning: Header file <fenv.h> not found."
945    print "         This host has no IEEE FP rounding mode control."
946
947# Check if we should enable KVM-based hardware virtualization
948have_kvm = conf.CheckHeader('linux/kvm.h', '<>')
949if not have_kvm:
950    print "Info: Header file <linux/kvm.h> not found, " \
951        "disabling KVM support."
952
953# Check if the requested target ISA is compatible with the host
954def is_isa_kvm_compatible(isa):
955    isa_comp_table = {
956        }
957    try:
958        import platform
959        host_isa = platform.machine()
960    except:
961        print "Warning: Failed to determine host ISA."
962        return False
963
964    return host_isa in isa_comp_table.get(isa, [])
965
966
967######################################################################
968#
969# Finish the configuration
970#
971main = conf.Finish()
972
973######################################################################
974#
975# Collect all non-global variables
976#
977
978# Define the universe of supported ISAs
979all_isa_list = [ ]
980Export('all_isa_list')
981
982class CpuModel(object):
983    '''The CpuModel class encapsulates everything the ISA parser needs to
984    know about a particular CPU model.'''
985
986    # Dict of available CPU model objects.  Accessible as CpuModel.dict.
987    dict = {}
988    list = []
989    defaults = []
990
991    # Constructor.  Automatically adds models to CpuModel.dict.
992    def __init__(self, name, filename, includes, strings, default=False):
993        self.name = name           # name of model
994        self.filename = filename   # filename for output exec code
995        self.includes = includes   # include files needed in exec file
996        # The 'strings' dict holds all the per-CPU symbols we can
997        # substitute into templates etc.
998        self.strings = strings
999
1000        # This cpu is enabled by default
1001        self.default = default
1002
1003        # Add self to dict
1004        if name in CpuModel.dict:
1005            raise AttributeError, "CpuModel '%s' already registered" % name
1006        CpuModel.dict[name] = self
1007        CpuModel.list.append(name)
1008
1009Export('CpuModel')
1010
1011# Sticky variables get saved in the variables file so they persist from
1012# one invocation to the next (unless overridden, in which case the new
1013# value becomes sticky).
1014sticky_vars = Variables(args=ARGUMENTS)
1015Export('sticky_vars')
1016
1017# Sticky variables that should be exported
1018export_vars = []
1019Export('export_vars')
1020
1021# For Ruby
1022all_protocols = []
1023Export('all_protocols')
1024protocol_dirs = []
1025Export('protocol_dirs')
1026slicc_includes = []
1027Export('slicc_includes')
1028
1029# Walk the tree and execute all SConsopts scripts that wil add to the
1030# above variables
1031if not GetOption('verbose'):
1032    print "Reading SConsopts"
1033for bdir in [ base_dir ] + extras_dir_list:
1034    if not isdir(bdir):
1035        print "Error: directory '%s' does not exist" % bdir
1036        Exit(1)
1037    for root, dirs, files in os.walk(bdir):
1038        if 'SConsopts' in files:
1039            if GetOption('verbose'):
1040                print "Reading", joinpath(root, 'SConsopts')
1041            SConscript(joinpath(root, 'SConsopts'))
1042
1043all_isa_list.sort()
1044
1045sticky_vars.AddVariables(
1046    EnumVariable('TARGET_ISA', 'Target ISA', 'alpha', all_isa_list),
1047    ListVariable('CPU_MODELS', 'CPU models',
1048                 sorted(n for n,m in CpuModel.dict.iteritems() if m.default),
1049                 sorted(CpuModel.list)),
1050    BoolVariable('EFENCE', 'Link with Electric Fence malloc debugger',
1051                 False),
1052    BoolVariable('SS_COMPATIBLE_FP',
1053                 'Make floating-point results compatible with SimpleScalar',
1054                 False),
1055    BoolVariable('USE_SSE2',
1056                 'Compile for SSE2 (-msse2) to get IEEE FP on x86 hosts',
1057                 False),
1058    BoolVariable('USE_POSIX_CLOCK', 'Use POSIX Clocks', have_posix_clock),
1059    BoolVariable('USE_FENV', 'Use <fenv.h> IEEE mode control', have_fenv),
1060    BoolVariable('CP_ANNOTATE', 'Enable critical path annotation capability', False),
1061    BoolVariable('USE_KVM', 'Enable hardware virtualized (KVM) CPU models', have_kvm),
1062    EnumVariable('PROTOCOL', 'Coherence protocol for Ruby', 'None',
1063                  all_protocols),
1064    )
1065
1066# These variables get exported to #defines in config/*.hh (see src/SConscript).
1067export_vars += ['USE_FENV', 'SS_COMPATIBLE_FP', 'TARGET_ISA', 'CP_ANNOTATE',
1068                'USE_POSIX_CLOCK', 'PROTOCOL', 'HAVE_PROTOBUF']
1069
1070###################################################
1071#
1072# Define a SCons builder for configuration flag headers.
1073#
1074###################################################
1075
1076# This function generates a config header file that #defines the
1077# variable symbol to the current variable setting (0 or 1).  The source
1078# operands are the name of the variable and a Value node containing the
1079# value of the variable.
1080def build_config_file(target, source, env):
1081    (variable, value) = [s.get_contents() for s in source]
1082    f = file(str(target[0]), 'w')
1083    print >> f, '#define', variable, value
1084    f.close()
1085    return None
1086
1087# Combine the two functions into a scons Action object.
1088config_action = MakeAction(build_config_file, Transform("CONFIG H", 2))
1089
1090# The emitter munges the source & target node lists to reflect what
1091# we're really doing.
1092def config_emitter(target, source, env):
1093    # extract variable name from Builder arg
1094    variable = str(target[0])
1095    # True target is config header file
1096    target = joinpath('config', variable.lower() + '.hh')
1097    val = env[variable]
1098    if isinstance(val, bool):
1099        # Force value to 0/1
1100        val = int(val)
1101    elif isinstance(val, str):
1102        val = '"' + val + '"'
1103
1104    # Sources are variable name & value (packaged in SCons Value nodes)
1105    return ([target], [Value(variable), Value(val)])
1106
1107config_builder = Builder(emitter = config_emitter, action = config_action)
1108
1109main.Append(BUILDERS = { 'ConfigFile' : config_builder })
1110
1111# libelf build is shared across all configs in the build root.
1112main.SConscript('ext/libelf/SConscript',
1113                variant_dir = joinpath(build_root, 'libelf'))
1114
1115# gzstream build is shared across all configs in the build root.
1116main.SConscript('ext/gzstream/SConscript',
1117                variant_dir = joinpath(build_root, 'gzstream'))
1118
1119# libfdt build is shared across all configs in the build root.
1120main.SConscript('ext/libfdt/SConscript',
1121                variant_dir = joinpath(build_root, 'libfdt'))
1122
1123###################################################
1124#
1125# This function is used to set up a directory with switching headers
1126#
1127###################################################
1128
1129main['ALL_ISA_LIST'] = all_isa_list
1130def make_switching_dir(dname, switch_headers, env):
1131    # Generate the header.  target[0] is the full path of the output
1132    # header to generate.  'source' is a dummy variable, since we get the
1133    # list of ISAs from env['ALL_ISA_LIST'].
1134    def gen_switch_hdr(target, source, env):
1135        fname = str(target[0])
1136        f = open(fname, 'w')
1137        isa = env['TARGET_ISA'].lower()
1138        print >>f, '#include "%s/%s/%s"' % (dname, isa, basename(fname))
1139        f.close()
1140
1141    # Build SCons Action object. 'varlist' specifies env vars that this
1142    # action depends on; when env['ALL_ISA_LIST'] changes these actions
1143    # should get re-executed.
1144    switch_hdr_action = MakeAction(gen_switch_hdr,
1145                          Transform("GENERATE"), varlist=['ALL_ISA_LIST'])
1146
1147    # Instantiate actions for each header
1148    for hdr in switch_headers:
1149        env.Command(hdr, [], switch_hdr_action)
1150Export('make_switching_dir')
1151
1152###################################################
1153#
1154# Define build environments for selected configurations.
1155#
1156###################################################
1157
1158for variant_path in variant_paths:
1159    print "Building in", variant_path
1160
1161    # Make a copy of the build-root environment to use for this config.
1162    env = main.Clone()
1163    env['BUILDDIR'] = variant_path
1164
1165    # variant_dir is the tail component of build path, and is used to
1166    # determine the build parameters (e.g., 'ALPHA_SE')
1167    (build_root, variant_dir) = splitpath(variant_path)
1168
1169    # Set env variables according to the build directory config.
1170    sticky_vars.files = []
1171    # Variables for $BUILD_ROOT/$VARIANT_DIR are stored in
1172    # $BUILD_ROOT/variables/$VARIANT_DIR so you can nuke
1173    # $BUILD_ROOT/$VARIANT_DIR without losing your variables settings.
1174    current_vars_file = joinpath(build_root, 'variables', variant_dir)
1175    if isfile(current_vars_file):
1176        sticky_vars.files.append(current_vars_file)
1177        print "Using saved variables file %s" % current_vars_file
1178    else:
1179        # Build dir-specific variables file doesn't exist.
1180
1181        # Make sure the directory is there so we can create it later
1182        opt_dir = dirname(current_vars_file)
1183        if not isdir(opt_dir):
1184            mkdir(opt_dir)
1185
1186        # Get default build variables from source tree.  Variables are
1187        # normally determined by name of $VARIANT_DIR, but can be
1188        # overridden by '--default=' arg on command line.
1189        default = GetOption('default')
1190        opts_dir = joinpath(main.root.abspath, 'build_opts')
1191        if default:
1192            default_vars_files = [joinpath(build_root, 'variables', default),
1193                                  joinpath(opts_dir, default)]
1194        else:
1195            default_vars_files = [joinpath(opts_dir, variant_dir)]
1196        existing_files = filter(isfile, default_vars_files)
1197        if existing_files:
1198            default_vars_file = existing_files[0]
1199            sticky_vars.files.append(default_vars_file)
1200            print "Variables file %s not found,\n  using defaults in %s" \
1201                  % (current_vars_file, default_vars_file)
1202        else:
1203            print "Error: cannot find variables file %s or " \
1204                  "default file(s) %s" \
1205                  % (current_vars_file, ' or '.join(default_vars_files))
1206            Exit(1)
1207
1208    # Apply current variable settings to env
1209    sticky_vars.Update(env)
1210
1211    help_texts["local_vars"] += \
1212        "Build variables for %s:\n" % variant_dir \
1213                 + sticky_vars.GenerateHelpText(env)
1214
1215    # Process variable settings.
1216
1217    if not have_fenv and env['USE_FENV']:
1218        print "Warning: <fenv.h> not available; " \
1219              "forcing USE_FENV to False in", variant_dir + "."
1220        env['USE_FENV'] = False
1221
1222    if not env['USE_FENV']:
1223        print "Warning: No IEEE FP rounding mode control in", variant_dir + "."
1224        print "         FP results may deviate slightly from other platforms."
1225
1226    if env['EFENCE']:
1227        env.Append(LIBS=['efence'])
1228
1229    if env['USE_KVM']:
1230        if not have_kvm:
1231            print "Warning: Can not enable KVM, host seems to lack KVM support"
1232            env['USE_KVM'] = False
1233        elif not is_isa_kvm_compatible(env['TARGET_ISA']):
1234            print "Info: KVM support disabled due to unsupported host and " \
1235                "target ISA combination"
1236            env['USE_KVM'] = False
1237
1238    # Save sticky variable settings back to current variables file
1239    sticky_vars.Save(current_vars_file, env)
1240
1241    if env['USE_SSE2']:
1242        env.Append(CCFLAGS=['-msse2'])
1243
1244    # The src/SConscript file sets up the build rules in 'env' according
1245    # to the configured variables.  It returns a list of environments,
1246    # one for each variant build (debug, opt, etc.)
1247    envList = SConscript('src/SConscript', variant_dir = variant_path,
1248                         exports = 'env')
1249
1250    # Set up the regression tests for each build.
1251    for e in envList:
1252        SConscript('tests/SConscript',
1253                   variant_dir = joinpath(variant_path, 'tests', e.Label),
1254                   exports = { 'env' : e }, duplicate = False)
1255
1256# base help text
1257Help('''
1258Usage: scons [scons options] [build variables] [target(s)]
1259
1260Extra scons options:
1261%(options)s
1262
1263Global build variables:
1264%(global_vars)s
1265
1266%(local_vars)s
1267''' % help_texts)
1268