SConstruct revision 9419
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###################################################
683918Ssaidi@eecs.umich.edu
694202Sbinkertn@umich.edu# Check for recent-enough Python and SCons versions.
704678Snate@binkert.orgtry:
71955SN/A    # 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:
782656Sstever@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:
892653Sstever@eecs.umich.edu    print """
901852SN/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
93955SN/Aon the scons script.
943717Sstever@eecs.umich.edu
953716Sstever@eecs.umich.eduFor more details, see:
96955SN/A    http://gem5.org/wiki/index.php/Using_a_non-default_Python_installation
971533SN/A"""
983716Sstever@eecs.umich.edu    raise
991533SN/A
1004678Snate@binkert.org# Global Python includes
1014678Snate@binkert.orgimport os
1024678Snate@binkert.orgimport re
1034678Snate@binkert.orgimport subprocess
1044678Snate@binkert.orgimport sys
1054678Snate@binkert.org
1064678Snate@binkert.orgfrom os import mkdir, environ
1074678Snate@binkert.orgfrom os.path import abspath, basename, dirname, expanduser, normpath
1084678Snate@binkert.orgfrom os.path import exists,  isdir, isfile
1094678Snate@binkert.orgfrom os.path import join as joinpath, split as splitpath
1104678Snate@binkert.org
1114678Snate@binkert.org# SCons includes
1124678Snate@binkert.orgimport SCons
1134678Snate@binkert.orgimport SCons.Node
1144678Snate@binkert.org
1154678Snate@binkert.orgextra_python_paths = [
1164678Snate@binkert.org    Dir('src/python').srcnode().abspath, # gem5 includes
1174678Snate@binkert.org    Dir('ext/ply').srcnode().abspath, # ply is used by several files
1184678Snate@binkert.org    ]
1194678Snate@binkert.org
1204678Snate@binkert.orgsys.path[1:1] = extra_python_paths
1214678Snate@binkert.org
1224678Snate@binkert.orgfrom m5.util import compareVersions, readCommand
1234678Snate@binkert.orgfrom m5.util.terminal import get_termcap
1244678Snate@binkert.org
1254678Snate@binkert.orghelp_texts = {
1264678Snate@binkert.org    "options" : "",
1274678Snate@binkert.org    "global_vars" : "",
128955SN/A    "local_vars" : ""
129955SN/A}
1302632Sstever@eecs.umich.edu
1312632Sstever@eecs.umich.eduExport("help_texts")
132955SN/A
133955SN/A
134955SN/A# There's a bug in scons in that (1) by default, the help texts from
135955SN/A# AddOption() are supposed to be displayed when you type 'scons -h'
1362632Sstever@eecs.umich.edu# and (2) you can override the help displayed by 'scons -h' using the
137955SN/A# Help() function, but these two features are incompatible: once
1382632Sstever@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.
1453053Sstever@eecs.umich.edudef AddLocalOption(*args, **kwargs):
1463053Sstever@eecs.umich.edu    col_width = 30
1473053Sstever@eecs.umich.edu
1483053Sstever@eecs.umich.edu    help = "  " + ", ".join(args)
1493053Sstever@eecs.umich.edu    if "help" in kwargs:
1503053Sstever@eecs.umich.edu        length = len(help)
1513053Sstever@eecs.umich.edu        if length >= col_width:
1523053Sstever@eecs.umich.edu            help += "\n" + " " * col_width
1533053Sstever@eecs.umich.edu        else:
1543053Sstever@eecs.umich.edu            help += " " * (col_width - length)
1553053Sstever@eecs.umich.edu        help += kwargs["help"]
1563053Sstever@eecs.umich.edu    help_texts["options"] += help + "\n"
1573053Sstever@eecs.umich.edu
1583053Sstever@eecs.umich.edu    AddOption(*args, **kwargs)
1593053Sstever@eecs.umich.edu
1603053Sstever@eecs.umich.eduAddLocalOption('--colors', dest='use_colors', action='store_true',
1612632Sstever@eecs.umich.edu               help="Add color to abbreviated scons output")
1622632Sstever@eecs.umich.eduAddLocalOption('--no-colors', dest='use_colors', action='store_false',
1632632Sstever@eecs.umich.edu               help="Don't add color to abbreviated scons output")
1642632Sstever@eecs.umich.eduAddLocalOption('--default', dest='default', type='string', action='store',
1652632Sstever@eecs.umich.edu               help='Override which build_opts file to use for defaults')
1662632Sstever@eecs.umich.eduAddLocalOption('--ignore-style', dest='ignore_style', action='store_true',
1673718Sstever@eecs.umich.edu               help='Disable style checking hooks')
1683718Sstever@eecs.umich.eduAddLocalOption('--no-lto', dest='no_lto', action='store_true',
1693718Sstever@eecs.umich.edu               help='Disable Link-Time Optimization for fast')
1703718Sstever@eecs.umich.eduAddLocalOption('--update-ref', dest='update_ref', action='store_true',
1713718Sstever@eecs.umich.edu               help='Update test reference outputs')
1723718Sstever@eecs.umich.eduAddLocalOption('--verbose', dest='verbose', action='store_true',
1733718Sstever@eecs.umich.edu               help='Print full tool command lines')
1743718Sstever@eecs.umich.edu
1753718Sstever@eecs.umich.edutermcap = get_termcap(GetOption('use_colors'))
1763718Sstever@eecs.umich.edu
1773718Sstever@eecs.umich.edu########################################################################
1783718Sstever@eecs.umich.edu#
1793718Sstever@eecs.umich.edu# Set up the main build environment.
1802634Sstever@eecs.umich.edu#
1812634Sstever@eecs.umich.edu########################################################################
1822632Sstever@eecs.umich.eduuse_vars = set([ 'AS', 'AR', 'CC', 'CXX', 'HOME', 'LD_LIBRARY_PATH',
1832638Sstever@eecs.umich.edu                 'LIBRARY_PATH', 'PATH', 'PKG_CONFIG_PATH', 'PYTHONPATH',
1842632Sstever@eecs.umich.edu                 'RANLIB', 'SWIG' ])
1852632Sstever@eecs.umich.edu
1862632Sstever@eecs.umich.eduuse_prefixes = [
1872632Sstever@eecs.umich.edu    "M5",           # M5 configuration (e.g., path to kernels)
1882632Sstever@eecs.umich.edu    "DISTCC_",      # distcc (distributed compiler wrapper) configuration
1892632Sstever@eecs.umich.edu    "CCACHE_",      # ccache (caching compiler wrapper) configuration
1901858SN/A    "CCC_",         # clang static analyzer configuration
1913716Sstever@eecs.umich.edu    ]
1922638Sstever@eecs.umich.edu
1932638Sstever@eecs.umich.eduuse_env = {}
1942638Sstever@eecs.umich.edufor key,val in os.environ.iteritems():
1952638Sstever@eecs.umich.edu    if key in use_vars or \
1962638Sstever@eecs.umich.edu            any([key.startswith(prefix) for prefix in use_prefixes]):
1972638Sstever@eecs.umich.edu        use_env[key] = val
1982638Sstever@eecs.umich.edu
1993716Sstever@eecs.umich.edumain = Environment(ENV=use_env)
2002634Sstever@eecs.umich.edumain.Decider('MD5-timestamp')
2012634Sstever@eecs.umich.edumain.root = Dir(".")         # The current directory (where this file lives).
202955SN/Amain.srcdir = Dir("src")     # The source directory
203955SN/A
204955SN/Amain_dict_keys = main.Dictionary().keys()
205955SN/A
206955SN/A# Check that we have a C/C++ compiler
207955SN/Aif not ('CC' in main_dict_keys and 'CXX' in main_dict_keys):
208955SN/A    print "No C++ compiler installed (package g++ on Ubuntu and RedHat)"
209955SN/A    Exit(1)
2101858SN/A
2111858SN/A# Check that swig is present
2122632Sstever@eecs.umich.eduif not 'SWIG' in main_dict_keys:
213955SN/A    print "swig is not installed (package swig on Ubuntu and RedHat)"
2143643Ssaidi@eecs.umich.edu    Exit(1)
2153643Ssaidi@eecs.umich.edu
2163643Ssaidi@eecs.umich.edu# add useful python code PYTHONPATH so it can be used by subprocesses
2173643Ssaidi@eecs.umich.edu# as well
2183643Ssaidi@eecs.umich.edumain.AppendENVPath('PYTHONPATH', extra_python_paths)
2193643Ssaidi@eecs.umich.edu
2203643Ssaidi@eecs.umich.edu########################################################################
2213643Ssaidi@eecs.umich.edu#
2224494Ssaidi@eecs.umich.edu# Mercurial Stuff.
2234494Ssaidi@eecs.umich.edu#
2243716Sstever@eecs.umich.edu# If the gem5 directory is a mercurial repository, we should do some
2251105SN/A# extra things.
2262667Sstever@eecs.umich.edu#
2272667Sstever@eecs.umich.edu########################################################################
2282667Sstever@eecs.umich.edu
2292667Sstever@eecs.umich.eduhgdir = main.root.Dir(".hg")
2302667Sstever@eecs.umich.edu
2312667Sstever@eecs.umich.edumercurial_style_message = """
2321869SN/AYou're missing the gem5 style hook, which automatically checks your code
2331869SN/Aagainst the gem5 style rules on hg commit and qrefresh commands.  This
2341869SN/Ascript will now install the hook in your .hg/hgrc file.
2351869SN/APress enter to continue, or ctrl-c to abort: """
2361869SN/A
2371065SN/Amercurial_style_hook = """
2382632Sstever@eecs.umich.edu# The following lines were automatically added by gem5/SConstruct
2392632Sstever@eecs.umich.edu# to provide the gem5 style-checking hooks
2403918Ssaidi@eecs.umich.edu[extensions]
2413918Ssaidi@eecs.umich.edustyle = %s/util/style.py
2423940Ssaidi@eecs.umich.edu
2433918Ssaidi@eecs.umich.edu[hooks]
2443918Ssaidi@eecs.umich.edupretxncommit.style = python:style.check_style
2453918Ssaidi@eecs.umich.edupre-qrefresh.style = python:style.check_style
2463918Ssaidi@eecs.umich.edu# End of SConstruct additions
2473918Ssaidi@eecs.umich.edu
2483918Ssaidi@eecs.umich.edu""" % (main.root.abspath)
2493940Ssaidi@eecs.umich.edu
2503940Ssaidi@eecs.umich.edumercurial_lib_not_found = """
2513940Ssaidi@eecs.umich.eduMercurial libraries cannot be found, ignoring style hook.  If
2523942Ssaidi@eecs.umich.eduyou are a gem5 developer, please fix this and run the style
2533940Ssaidi@eecs.umich.eduhook. It is important.
2543918Ssaidi@eecs.umich.edu"""
2553918Ssaidi@eecs.umich.edu
256955SN/A# Check for style hook and prompt for installation if it's not there.
2571858SN/A# Skip this if --ignore-style was specified, there's no .hg dir to
2583918Ssaidi@eecs.umich.edu# install a hook in, or there's no interactive terminal to prompt.
2593918Ssaidi@eecs.umich.eduif not GetOption('ignore_style') and hgdir.exists() and sys.stdin.isatty():
2603918Ssaidi@eecs.umich.edu    style_hook = True
2613918Ssaidi@eecs.umich.edu    try:
2623940Ssaidi@eecs.umich.edu        from mercurial import ui
2633940Ssaidi@eecs.umich.edu        ui = ui.ui()
2643918Ssaidi@eecs.umich.edu        ui.readconfig(hgdir.File('hgrc').abspath)
2653918Ssaidi@eecs.umich.edu        style_hook = ui.config('hooks', 'pretxncommit.style', None) and \
2663918Ssaidi@eecs.umich.edu                     ui.config('hooks', 'pre-qrefresh.style', None)
2673918Ssaidi@eecs.umich.edu    except ImportError:
2683918Ssaidi@eecs.umich.edu        print mercurial_lib_not_found
2693918Ssaidi@eecs.umich.edu
2703918Ssaidi@eecs.umich.edu    if not style_hook:
2713918Ssaidi@eecs.umich.edu        print mercurial_style_message,
2723918Ssaidi@eecs.umich.edu        # continue unless user does ctrl-c/ctrl-d etc.
2733940Ssaidi@eecs.umich.edu        try:
2743918Ssaidi@eecs.umich.edu            raw_input()
2753918Ssaidi@eecs.umich.edu        except:
2761851SN/A            print "Input exception, exiting scons.\n"
2771851SN/A            sys.exit(1)
2781858SN/A        hgrc_path = '%s/.hg/hgrc' % main.root.abspath
2792632Sstever@eecs.umich.edu        print "Adding style hook to", hgrc_path, "\n"
280955SN/A        try:
2813053Sstever@eecs.umich.edu            hgrc = open(hgrc_path, 'a')
2823053Sstever@eecs.umich.edu            hgrc.write(mercurial_style_hook)
2833053Sstever@eecs.umich.edu            hgrc.close()
2843053Sstever@eecs.umich.edu        except:
2853053Sstever@eecs.umich.edu            print "Error updating", hgrc_path
2863053Sstever@eecs.umich.edu            sys.exit(1)
2873053Sstever@eecs.umich.edu
2883053Sstever@eecs.umich.edu
2893053Sstever@eecs.umich.edu###################################################
2904742Sstever@eecs.umich.edu#
2914742Sstever@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
2963053Sstever@eecs.umich.edu# Find default configuration & binary.
2973053Sstever@eecs.umich.eduDefault(environ.get('M5_DEFAULT_BINARY', 'build/ALPHA/gem5.debug'))
2983053Sstever@eecs.umich.edu
2993053Sstever@eecs.umich.edu# helper function: find last occurrence of element in list
3003053Sstever@eecs.umich.edudef rfind(l, elt, offs = -1):
3012667Sstever@eecs.umich.edu    for i in range(len(l)+offs, 0, -1):
3024554Sbinkertn@umich.edu        if l[i] == elt:
3034554Sbinkertn@umich.edu            return i
3042667Sstever@eecs.umich.edu    raise ValueError, "element not found"
3054554Sbinkertn@umich.edu
3064554Sbinkertn@umich.edu# Take a list of paths (or SCons Nodes) and return a list with all
3074554Sbinkertn@umich.edu# paths made absolute and ~-expanded.  Paths will be interpreted
3084554Sbinkertn@umich.edu# relative to the launch directory unless a different root is provided
3094554Sbinkertn@umich.edudef makePathListAbsolute(path_list, root=GetLaunchDir()):
3104554Sbinkertn@umich.edu    return [abspath(joinpath(root, expanduser(str(p))))
3114554Sbinkertn@umich.edu            for p in path_list]
3124554Sbinkertn@umich.edu
3134554Sbinkertn@umich.edu# Each target must have 'build' in the interior of the path; the
3144554Sbinkertn@umich.edu# directory below this will determine the build parameters.  For
3152667Sstever@eecs.umich.edu# example, for target 'foo/bar/build/ALPHA_SE/arch/alpha/blah.do' we
3164554Sbinkertn@umich.edu# recognize that ALPHA_SE specifies the configuration because it
3174554Sbinkertn@umich.edu# follow 'build' in the build path.
3184554Sbinkertn@umich.edu
3194554Sbinkertn@umich.edu# The funky assignment to "[:]" is needed to replace the list contents
3202667Sstever@eecs.umich.edu# in place rather than reassign the symbol to a new list, which
3214554Sbinkertn@umich.edu# doesn't work (obviously!).
3222667Sstever@eecs.umich.eduBUILD_TARGETS[:] = makePathListAbsolute(BUILD_TARGETS)
3234554Sbinkertn@umich.edu
3244554Sbinkertn@umich.edu# Generate a list of the unique build roots and configs that the
3252667Sstever@eecs.umich.edu# collected targets reference.
3262638Sstever@eecs.umich.eduvariant_paths = []
3272638Sstever@eecs.umich.edubuild_root = None
3282638Sstever@eecs.umich.edufor t in BUILD_TARGETS:
3293716Sstever@eecs.umich.edu    path_dirs = t.split('/')
3303716Sstever@eecs.umich.edu    try:
3311858SN/A        build_top = rfind(path_dirs, 'build', -2)
3323118Sstever@eecs.umich.edu    except:
3333118Sstever@eecs.umich.edu        print "Error: no non-leaf 'build' dir found on target path", t
3343118Sstever@eecs.umich.edu        Exit(1)
3353118Sstever@eecs.umich.edu    this_build_root = joinpath('/',*path_dirs[:build_top+1])
3363118Sstever@eecs.umich.edu    if not build_root:
3373118Sstever@eecs.umich.edu        build_root = this_build_root
3383118Sstever@eecs.umich.edu    else:
3393118Sstever@eecs.umich.edu        if this_build_root != build_root:
3403118Sstever@eecs.umich.edu            print "Error: build targets not under same build root\n"\
3413118Sstever@eecs.umich.edu                  "  %s\n  %s" % (build_root, this_build_root)
3423118Sstever@eecs.umich.edu            Exit(1)
3433716Sstever@eecs.umich.edu    variant_path = joinpath('/',*path_dirs[:build_top+2])
3443118Sstever@eecs.umich.edu    if variant_path not in variant_paths:
3453118Sstever@eecs.umich.edu        variant_paths.append(variant_path)
3463118Sstever@eecs.umich.edu
3473118Sstever@eecs.umich.edu# Make sure build_root exists (might not if this is the first build there)
3483118Sstever@eecs.umich.eduif not isdir(build_root):
3493118Sstever@eecs.umich.edu    mkdir(build_root)
3503118Sstever@eecs.umich.edumain['BUILDROOT'] = build_root
3513118Sstever@eecs.umich.edu
3523118Sstever@eecs.umich.eduExport('main')
3533716Sstever@eecs.umich.edu
3543118Sstever@eecs.umich.edumain.SConsignFile(joinpath(build_root, "sconsign"))
3553118Sstever@eecs.umich.edu
3563118Sstever@eecs.umich.edu# Default duplicate option is to use hard links, but this messes up
3573118Sstever@eecs.umich.edu# when you use emacs to edit a file in the target dir, as emacs moves
3583118Sstever@eecs.umich.edu# file to file~ then copies to file, breaking the link.  Symbolic
3593118Sstever@eecs.umich.edu# (soft) links work better.
3603118Sstever@eecs.umich.edumain.SetOption('duplicate', 'soft-copy')
3613118Sstever@eecs.umich.edu
3623118Sstever@eecs.umich.edu#
3633118Sstever@eecs.umich.edu# Set up global sticky variables... these are common to an entire build
3643483Ssaidi@eecs.umich.edu# tree (not specific to a particular build like ALPHA_SE)
3653494Ssaidi@eecs.umich.edu#
3663494Ssaidi@eecs.umich.edu
3673483Ssaidi@eecs.umich.eduglobal_vars_file = joinpath(build_root, 'variables.global')
3683483Ssaidi@eecs.umich.edu
3693483Ssaidi@eecs.umich.eduglobal_vars = Variables(global_vars_file, args=ARGUMENTS)
3703053Sstever@eecs.umich.edu
3713053Sstever@eecs.umich.eduglobal_vars.AddVariables(
3723918Ssaidi@eecs.umich.edu    ('CC', 'C compiler', environ.get('CC', main['CC'])),
3733053Sstever@eecs.umich.edu    ('CXX', 'C++ compiler', environ.get('CXX', main['CXX'])),
3743053Sstever@eecs.umich.edu    ('SWIG', 'SWIG tool', environ.get('SWIG', main['SWIG'])),
3753053Sstever@eecs.umich.edu    ('PROTOC', 'protoc tool', environ.get('PROTOC', 'protoc')),
3763053Sstever@eecs.umich.edu    ('BATCH', 'Use batch pool for build and tests', False),
3773053Sstever@eecs.umich.edu    ('BATCH_CMD', 'Batch pool submission command name', 'qdo'),
3781858SN/A    ('M5_BUILD_CACHE', 'Cache built objects in this directory', False),
3791858SN/A    ('EXTRAS', 'Add extra directories to the compilation', '')
3801858SN/A    )
3811858SN/A
3821858SN/A# Update main environment with values from ARGUMENTS & global_vars_file
3831858SN/Aglobal_vars.Update(main)
3841859SN/Ahelp_texts["global_vars"] += global_vars.GenerateHelpText(main)
3851858SN/A
3861858SN/A# Save sticky variable settings back to current variables file
3871858SN/Aglobal_vars.Save(global_vars_file, main)
3881859SN/A
3891859SN/A# Parse EXTRAS variable to build list of all directories where we're
3901862SN/A# look for sources etc.  This list is exported as extras_dir_list.
3913053Sstever@eecs.umich.edubase_dir = main.srcdir.abspath
3923053Sstever@eecs.umich.eduif main['EXTRAS']:
3933053Sstever@eecs.umich.edu    extras_dir_list = makePathListAbsolute(main['EXTRAS'].split(':'))
3943053Sstever@eecs.umich.eduelse:
3951859SN/A    extras_dir_list = []
3961859SN/A
3971859SN/AExport('base_dir')
3981859SN/AExport('extras_dir_list')
3991859SN/A
4001859SN/A# the ext directory should be on the #includes path
4011859SN/Amain.Append(CPPPATH=[Dir('ext')])
4021859SN/A
4031862SN/Adef strip_build_path(path, env):
4041859SN/A    path = str(path)
4051859SN/A    variant_base = env['BUILDROOT'] + os.path.sep
4061859SN/A    if path.startswith(variant_base):
4071858SN/A        path = path[len(variant_base):]
4081858SN/A    elif path.startswith('build/'):
4092139SN/A        path = path[6:]
4104202Sbinkertn@umich.edu    return path
4114202Sbinkertn@umich.edu
4122139SN/A# Generate a string of the form:
4132155SN/A#   common/path/prefix/src1, src2 -> tgt1, tgt2
4144202Sbinkertn@umich.edu# to print while building.
4154202Sbinkertn@umich.educlass Transform(object):
4164202Sbinkertn@umich.edu    # all specific color settings should be here and nowhere else
4172155SN/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
4211869SN/A    tgts_color = termcap.Yellow + termcap.Bold
4224202Sbinkertn@umich.edu
4234202Sbinkertn@umich.edu    def __init__(self, tool, max_sources=99):
4244202Sbinkertn@umich.edu        self.format = self.tool_color + (" [%8s] " % tool) \
4254202Sbinkertn@umich.edu                      + self.pfx_color + "%s" \
4264202Sbinkertn@umich.edu                      + self.srcs_color + "%s" \
4274202Sbinkertn@umich.edu                      + self.arrow_color + " -> " \
4284202Sbinkertn@umich.edu                      + self.tgts_color + "%s" \
4294202Sbinkertn@umich.edu                      + termcap.Normal
4304202Sbinkertn@umich.edu        self.max_sources = max_sources
4314202Sbinkertn@umich.edu
4324202Sbinkertn@umich.edu    def __call__(self, target, source, env, for_signature=None):
4334202Sbinkertn@umich.edu        # truncate source list according to max_sources param
4344202Sbinkertn@umich.edu        source = source[0:self.max_sources]
4354202Sbinkertn@umich.edu        def strip(f):
4364202Sbinkertn@umich.edu            return strip_build_path(str(f), env)
4374202Sbinkertn@umich.edu        if len(source) > 0:
4384773Snate@binkert.org            srcs = map(strip, source)
4394773Snate@binkert.org        else:
4404773Snate@binkert.org            srcs = ['']
4414773Snate@binkert.org        tgts = map(strip, target)
4424773Snate@binkert.org        # surprisingly, os.path.commonprefix is a dumb char-by-char string
4434773Snate@binkert.org        # operation that has nothing to do with paths.
4444773Snate@binkert.org        com_pfx = os.path.commonprefix(srcs + tgts)
4451869SN/A        com_pfx_len = len(com_pfx)
4464202Sbinkertn@umich.edu        if com_pfx:
4471869SN/A            # do some cleanup and sanity checking on common prefix
4482508SN/A            if com_pfx[-1] == ".":
4492508SN/A                # prefix matches all but file extension: ok
4502508SN/A                # back up one to change 'foo.cc -> o' to 'foo.cc -> .o'
4512508SN/A                com_pfx = com_pfx[0:-1]
4524202Sbinkertn@umich.edu            elif com_pfx[-1] == "/":
4531869SN/A                # common prefix is directory path: OK
4541869SN/A                pass
4551869SN/A            else:
4561869SN/A                src0_len = len(srcs[0])
4571869SN/A                tgt0_len = len(tgts[0])
4581869SN/A                if src0_len == com_pfx_len:
4591965SN/A                    # source is a substring of target, OK
4601965SN/A                    pass
4611965SN/A                elif tgt0_len == com_pfx_len:
4621869SN/A                    # target is a substring of source, need to back up to
4631869SN/A                    # avoid empty string on RHS of arrow
4642733Sktlim@umich.edu                    sep_idx = com_pfx.rfind(".")
4651869SN/A                    if sep_idx != -1:
4661884SN/A                        com_pfx = com_pfx[0:sep_idx]
4671884SN/A                    else:
4683356Sbinkertn@umich.edu                        com_pfx = ''
4693356Sbinkertn@umich.edu                elif src0_len > com_pfx_len and srcs[0][com_pfx_len] == ".":
4703356Sbinkertn@umich.edu                    # still splitting at file extension: ok
4714773Snate@binkert.org                    pass
4724773Snate@binkert.org                else:
4734773Snate@binkert.org                    # probably a fluke; ignore it
4741869SN/A                    com_pfx = ''
4751858SN/A        # recalculate length in case com_pfx was modified
4761869SN/A        com_pfx_len = len(com_pfx)
4771869SN/A        def fmt(files):
4781869SN/A            f = map(lambda s: s[com_pfx_len:], files)
4791858SN/A            return ', '.join(f)
4802761Sstever@eecs.umich.edu        return self.format % (com_pfx, fmt(srcs), fmt(tgts))
4811869SN/A
4822733Sktlim@umich.eduExport('Transform')
4833584Ssaidi@eecs.umich.edu
4841869SN/A# enable the regression script to use the termcap
4851869SN/Amain['TERMCAP'] = termcap
4861869SN/A
4871869SN/Aif GetOption('verbose'):
4881869SN/A    def MakeAction(action, string, *args, **kwargs):
4891869SN/A        return Action(action, *args, **kwargs)
4901858SN/Aelse:
491955SN/A    MakeAction = Action
492955SN/A    main['CCCOMSTR']        = Transform("CC")
4931869SN/A    main['CXXCOMSTR']       = Transform("CXX")
4941869SN/A    main['ASCOMSTR']        = Transform("AS")
4951869SN/A    main['SWIGCOMSTR']      = Transform("SWIG")
4961869SN/A    main['ARCOMSTR']        = Transform("AR", 0)
4971869SN/A    main['LINKCOMSTR']      = Transform("LINK", 0)
4981869SN/A    main['RANLIBCOMSTR']    = Transform("RANLIB", 0)
4991869SN/A    main['M4COMSTR']        = Transform("M4")
5001869SN/A    main['SHCCCOMSTR']      = Transform("SHCC")
5011869SN/A    main['SHCXXCOMSTR']     = Transform("SHCXX")
5021869SN/AExport('MakeAction')
5031869SN/A
5041869SN/A# Initialize the Link-Time Optimization (LTO) flags
5051869SN/Amain['LTO_CCFLAGS'] = []
5061869SN/Amain['LTO_LDFLAGS'] = []
5071869SN/A
5081869SN/ACXX_version = readCommand([main['CXX'],'--version'], exception=False)
5091869SN/ACXX_V = readCommand([main['CXX'],'-V'], exception=False)
5101869SN/A
5111869SN/Amain['GCC'] = CXX_version and CXX_version.find('g++') >= 0
5121869SN/Amain['CLANG'] = CXX_version and CXX_version.find('clang') >= 0
5131869SN/Aif main['GCC'] + main['CLANG'] > 1:
5141869SN/A    print 'Error: How can we have two at the same time?'
5151869SN/A    Exit(1)
5161869SN/A
5171869SN/A# Set up default C++ compiler flags
5181869SN/Aif main['GCC']:
5191869SN/A    main.Append(CCFLAGS=['-pipe'])
5201869SN/A    main.Append(CCFLAGS=['-fno-strict-aliasing'])
5211869SN/A    main.Append(CCFLAGS=['-Wall', '-Wno-sign-compare', '-Wundef'])
5223716Sstever@eecs.umich.edu    # Read the GCC version to check for versions with bugs
5233356Sbinkertn@umich.edu    # Note CCVERSION doesn't work here because it is run with the CC
5243356Sbinkertn@umich.edu    # before we override it from the command line
5253356Sbinkertn@umich.edu    gcc_version = readCommand([main['CXX'], '-dumpversion'], exception=False)
5263356Sbinkertn@umich.edu    main['GCC_VERSION'] = gcc_version
5273356Sbinkertn@umich.edu    if not compareVersions(gcc_version, '4.4.1') or \
5283356Sbinkertn@umich.edu       not compareVersions(gcc_version, '4.4.2'):
5293356Sbinkertn@umich.edu        print 'Info: Tree vectorizer in GCC 4.4.1 & 4.4.2 is buggy, disabling.'
5301869SN/A        main.Append(CCFLAGS=['-fno-tree-vectorize'])
5311869SN/A    # c++0x support in gcc is useful already from 4.4, see
5321869SN/A    # http://gcc.gnu.org/projects/cxx0x.html for details
5331869SN/A    if compareVersions(gcc_version, '4.4') >= 0:
5341869SN/A        main.Append(CXXFLAGS=['-std=c++0x'])
5351869SN/A
5361869SN/A    # LTO support is only really working properly from 4.6 and beyond
5372655Sstever@eecs.umich.edu    if compareVersions(gcc_version, '4.6') >= 0:
5382655Sstever@eecs.umich.edu        # Add the appropriate Link-Time Optimization (LTO) flags
5392655Sstever@eecs.umich.edu        # unless LTO is explicitly turned off. Note that these flags
5402655Sstever@eecs.umich.edu        # are only used by the fast target.
5412655Sstever@eecs.umich.edu        if not GetOption('no_lto'):
5422655Sstever@eecs.umich.edu            # Pass the LTO flag when compiling to produce GIMPLE
5432655Sstever@eecs.umich.edu            # output, we merely create the flags here and only append
5442655Sstever@eecs.umich.edu            # them later/
5452655Sstever@eecs.umich.edu            main['LTO_CCFLAGS'] = ['-flto=%d' % GetOption('num_jobs')]
5462655Sstever@eecs.umich.edu
5472655Sstever@eecs.umich.edu            # Use the same amount of jobs for LTO as we are running
5482655Sstever@eecs.umich.edu            # scons with, we hardcode the use of the linker plugin
5492655Sstever@eecs.umich.edu            # which requires either gold or GNU ld >= 2.21
5502655Sstever@eecs.umich.edu            main['LTO_LDFLAGS'] = ['-flto=%d' % GetOption('num_jobs'),
5512655Sstever@eecs.umich.edu                                   '-fuse-linker-plugin']
5522655Sstever@eecs.umich.edu
5532655Sstever@eecs.umich.eduelif main['CLANG']:
5542655Sstever@eecs.umich.edu    clang_version_re = re.compile(".* version (\d+\.\d+)")
5552655Sstever@eecs.umich.edu    clang_version_match = clang_version_re.match(CXX_version)
5562655Sstever@eecs.umich.edu    if (clang_version_match):
5572655Sstever@eecs.umich.edu        clang_version = clang_version_match.groups()[0]
5582655Sstever@eecs.umich.edu        if compareVersions(clang_version, "2.9") < 0:
5592655Sstever@eecs.umich.edu            print 'Error: clang version 2.9 or newer required.'
5602655Sstever@eecs.umich.edu            print '       Installed version:', clang_version
5612655Sstever@eecs.umich.edu            Exit(1)
5622655Sstever@eecs.umich.edu    else:
5632634Sstever@eecs.umich.edu        print 'Error: Unable to determine clang version.'
5642634Sstever@eecs.umich.edu        Exit(1)
5652634Sstever@eecs.umich.edu
5662634Sstever@eecs.umich.edu    main.Append(CCFLAGS=['-pipe'])
5672634Sstever@eecs.umich.edu    main.Append(CCFLAGS=['-fno-strict-aliasing'])
5682634Sstever@eecs.umich.edu    main.Append(CCFLAGS=['-Wall', '-Wno-sign-compare', '-Wundef'])
5692638Sstever@eecs.umich.edu    main.Append(CCFLAGS=['-Wno-tautological-compare'])
5702638Sstever@eecs.umich.edu    main.Append(CCFLAGS=['-Wno-self-assign'])
5713716Sstever@eecs.umich.edu    # Ruby makes frequent use of extraneous parantheses in the printing
5722638Sstever@eecs.umich.edu    # of if-statements
5732638Sstever@eecs.umich.edu    main.Append(CCFLAGS=['-Wno-parentheses'])
5741869SN/A
5751869SN/A    # clang 2.9 does not play well with c++0x as it ships with C++
5763546Sgblack@eecs.umich.edu    # headers that produce errors, this was fixed in 3.0
5773546Sgblack@eecs.umich.edu    if compareVersions(clang_version, "3") >= 0:
5783546Sgblack@eecs.umich.edu        main.Append(CXXFLAGS=['-std=c++0x'])
5793546Sgblack@eecs.umich.eduelse:
5804202Sbinkertn@umich.edu    print termcap.Yellow + termcap.Bold + 'Error' + termcap.Normal,
5813546Sgblack@eecs.umich.edu    print "Don't know what compiler options to use for your compiler."
5823546Sgblack@eecs.umich.edu    print termcap.Yellow + '       compiler:' + termcap.Normal, main['CXX']
5833546Sgblack@eecs.umich.edu    print termcap.Yellow + '       version:' + termcap.Normal,
5843546Sgblack@eecs.umich.edu    if not CXX_version:
5853546Sgblack@eecs.umich.edu        print termcap.Yellow + termcap.Bold + "COMMAND NOT FOUND!" +\
5863546Sgblack@eecs.umich.edu               termcap.Normal
5873546Sgblack@eecs.umich.edu    else:
5883546Sgblack@eecs.umich.edu        print CXX_version.replace('\n', '<nl>')
5893546Sgblack@eecs.umich.edu    print "       If you're trying to use a compiler other than GCC"
5903546Sgblack@eecs.umich.edu    print "       or clang, there appears to be something wrong with your"
5914202Sbinkertn@umich.edu    print "       environment."
5923546Sgblack@eecs.umich.edu    print "       "
5933546Sgblack@eecs.umich.edu    print "       If you are trying to use a compiler other than those listed"
5943546Sgblack@eecs.umich.edu    print "       above you will need to ease fix SConstruct and "
5953546Sgblack@eecs.umich.edu    print "       src/SConscript to support that compiler."
5963546Sgblack@eecs.umich.edu    Exit(1)
5973546Sgblack@eecs.umich.edu
5983546Sgblack@eecs.umich.edu# Set up common yacc/bison flags (needed for Ruby)
5993546Sgblack@eecs.umich.edumain['YACCFLAGS'] = '-d'
6003546Sgblack@eecs.umich.edumain['YACCHXXFILESUFFIX'] = '.hh'
6013546Sgblack@eecs.umich.edu
6023546Sgblack@eecs.umich.edu# Do this after we save setting back, or else we'll tack on an
6033546Sgblack@eecs.umich.edu# extra 'qdo' every time we run scons.
6043546Sgblack@eecs.umich.eduif main['BATCH']:
6053546Sgblack@eecs.umich.edu    main['CC']     = main['BATCH_CMD'] + ' ' + main['CC']
6063546Sgblack@eecs.umich.edu    main['CXX']    = main['BATCH_CMD'] + ' ' + main['CXX']
6073546Sgblack@eecs.umich.edu    main['AS']     = main['BATCH_CMD'] + ' ' + main['AS']
6083546Sgblack@eecs.umich.edu    main['AR']     = main['BATCH_CMD'] + ' ' + main['AR']
6093546Sgblack@eecs.umich.edu    main['RANLIB'] = main['BATCH_CMD'] + ' ' + main['RANLIB']
6103546Sgblack@eecs.umich.edu
6113546Sgblack@eecs.umich.eduif sys.platform == 'cygwin':
6124202Sbinkertn@umich.edu    # cygwin has some header file issues...
6133546Sgblack@eecs.umich.edu    main.Append(CCFLAGS=["-Wno-uninitialized"])
6143546Sgblack@eecs.umich.edu
6153546Sgblack@eecs.umich.edu# Check for the protobuf compiler
616955SN/Aprotoc_version = readCommand([main['PROTOC'], '--version'],
617955SN/A                             exception='').split()
618955SN/A
619955SN/A# First two words should be "libprotoc x.y.z"
6201858SN/Aif len(protoc_version) < 2 or protoc_version[0] != 'libprotoc':
6211858SN/A    print termcap.Yellow + termcap.Bold + \
6221858SN/A        'Warning: Protocol buffer compiler (protoc) not found.\n' + \
6232632Sstever@eecs.umich.edu        '         Please install protobuf-compiler for tracing support.' + \
6242632Sstever@eecs.umich.edu        termcap.Normal
6254773Snate@binkert.org    main['PROTOC'] = False
6264773Snate@binkert.orgelse:
6272632Sstever@eecs.umich.edu    # Determine the appropriate include path and library path using
6282632Sstever@eecs.umich.edu    # pkg-config, that means we also need to check for pkg-config
6292632Sstever@eecs.umich.edu    if not readCommand(['pkg-config', '--version'], exception=''):
6302634Sstever@eecs.umich.edu        print 'Error: pkg-config not found. Please install and retry.'
6312638Sstever@eecs.umich.edu        Exit(1)
6322023SN/A
6332632Sstever@eecs.umich.edu    main.ParseConfig('pkg-config --cflags --libs-only-L protobuf')
6342632Sstever@eecs.umich.edu
6352632Sstever@eecs.umich.edu    # Based on the availability of the compress stream wrappers,
6362632Sstever@eecs.umich.edu    # require 2.1.0
6372632Sstever@eecs.umich.edu    min_protoc_version = '2.1.0'
6383716Sstever@eecs.umich.edu    if compareVersions(protoc_version[1], min_protoc_version) < 0:
6392632Sstever@eecs.umich.edu        print 'Error: protoc version', min_protoc_version, 'or newer required.'
6402632Sstever@eecs.umich.edu        print '       Installed version:', protoc_version[1]
6412632Sstever@eecs.umich.edu        Exit(1)
6422632Sstever@eecs.umich.edu
6432632Sstever@eecs.umich.edu# Check for SWIG
6442023SN/Aif not main.has_key('SWIG'):
6452632Sstever@eecs.umich.edu    print 'Error: SWIG utility not found.'
6462632Sstever@eecs.umich.edu    print '       Please install (see http://www.swig.org) and retry.'
6471889SN/A    Exit(1)
6481889SN/A
6492632Sstever@eecs.umich.edu# Check for appropriate SWIG version
6502632Sstever@eecs.umich.eduswig_version = readCommand([main['SWIG'], '-version'], exception='').split()
6512632Sstever@eecs.umich.edu# First 3 words should be "SWIG Version x.y.z"
6522632Sstever@eecs.umich.eduif len(swig_version) < 3 or \
6533716Sstever@eecs.umich.edu        swig_version[0] != 'SWIG' or swig_version[1] != 'Version':
6543716Sstever@eecs.umich.edu    print 'Error determining SWIG version.'
6552632Sstever@eecs.umich.edu    Exit(1)
6562632Sstever@eecs.umich.edu
6572632Sstever@eecs.umich.edumin_swig_version = '1.3.34'
6582632Sstever@eecs.umich.eduif compareVersions(swig_version[2], min_swig_version) < 0:
6592632Sstever@eecs.umich.edu    print 'Error: SWIG version', min_swig_version, 'or newer required.'
6602632Sstever@eecs.umich.edu    print '       Installed version:', swig_version[2]
6612632Sstever@eecs.umich.edu    Exit(1)
6622632Sstever@eecs.umich.edu
6631888SN/A# Set up SWIG flags & scanner
6641888SN/Aswig_flags=Split('-c++ -python -modern -templatereduce $_CPPINCFLAGS')
6651869SN/Amain.Append(SWIGFLAGS=swig_flags)
6661869SN/A
6671858SN/A# filter out all existing swig scanners, they mess up the dependency
6682598SN/A# stuff for some reason
6692598SN/Ascanners = []
6702598SN/Afor scanner in main['SCANNERS']:
6712598SN/A    skeys = scanner.skeys
6722598SN/A    if skeys == '.i':
6731858SN/A        continue
6741858SN/A
6751858SN/A    if isinstance(skeys, (list, tuple)) and '.i' in skeys:
6761858SN/A        continue
6771858SN/A
6781858SN/A    scanners.append(scanner)
6791858SN/A
6801858SN/A# add the new swig scanner that we like better
6811858SN/Afrom SCons.Scanner import ClassicCPP as CPPScanner
6821871SN/Aswig_inc_re = '^[ \t]*[%,#][ \t]*(?:include|import)[ \t]*(<|")([^>"]+)(>|")'
6831858SN/Ascanners.append(CPPScanner("SwigScan", [ ".i" ], "CPPPATH", swig_inc_re))
6841858SN/A
6851858SN/A# replace the scanners list that has what we want
6861858SN/Amain['SCANNERS'] = scanners
6871858SN/A
6881858SN/A# Add a custom Check function to the Configure context so that we can
6891858SN/A# figure out if the compiler adds leading underscores to global
6901858SN/A# variables.  This is needed for the autogenerated asm files that we
6911858SN/A# use for embedding the python code.
6921858SN/Adef CheckLeading(context):
6931858SN/A    context.Message("Checking for leading underscore in global variables...")
6941859SN/A    # 1) Define a global variable called x from asm so the C compiler
6951859SN/A    #    won't change the symbol at all.
6961869SN/A    # 2) Declare that variable.
6971888SN/A    # 3) Use the variable
6982632Sstever@eecs.umich.edu    #
6991869SN/A    # If the compiler prepends an underscore, this will successfully
7001884SN/A    # link because the external symbol 'x' will be called '_x' which
7011884SN/A    # was defined by the asm statement.  If the compiler does not
7021884SN/A    # prepend an underscore, this will not successfully link because
7031884SN/A    # '_x' will have been defined by assembly, while the C portion of
7041884SN/A    # the code will be trying to use 'x'
7051884SN/A    ret = context.TryLink('''
7061965SN/A        asm(".globl _x; _x: .byte 0");
7071965SN/A        extern int x;
7081965SN/A        int main() { return x; }
7092761Sstever@eecs.umich.edu        ''', extension=".c")
7101869SN/A    context.env.Append(LEADING_UNDERSCORE=ret)
7111869SN/A    context.Result(ret)
7122632Sstever@eecs.umich.edu    return ret
7132667Sstever@eecs.umich.edu
7141869SN/A# Test for the presence of C++11 static asserts. If the compiler lacks
7151869SN/A# support for static asserts, base/compiler.hh enables a macro that
7162929Sktlim@umich.edu# removes any static asserts in the code.
7172929Sktlim@umich.edudef CheckStaticAssert(context):
7183716Sstever@eecs.umich.edu    context.Message("Checking for C++11 static_assert support...")
7192929Sktlim@umich.edu    ret = context.TryCompile('''
720955SN/A        static_assert(1, "This assert is always true");
7212598SN/A        ''', extension=".cc")
7222598SN/A    context.env.Append(HAVE_STATIC_ASSERT=ret)
7233546Sgblack@eecs.umich.edu    context.Result(ret)
724955SN/A    return ret
725955SN/A
726955SN/A# Platform-specific configuration.  Note again that we assume that all
7271530SN/A# builds under a given build root run on the same host platform.
728955SN/Aconf = Configure(main,
729955SN/A                 conf_dir = joinpath(build_root, '.scons_config'),
730955SN/A                 log_file = joinpath(build_root, 'scons_config.log'),
731                 custom_tests = { 'CheckLeading' : CheckLeading,
732                                  'CheckStaticAssert' : CheckStaticAssert,
733                                })
734
735# Check for leading underscores.  Don't really need to worry either
736# way so don't need to check the return code.
737conf.CheckLeading()
738
739# Check for C++11 features we want to use if they exist
740conf.CheckStaticAssert()
741
742# Check if we should compile a 64 bit binary on Mac OS X/Darwin
743try:
744    import platform
745    uname = platform.uname()
746    if uname[0] == 'Darwin' and compareVersions(uname[2], '9.0.0') >= 0:
747        if int(readCommand('sysctl -n hw.cpu64bit_capable')[0]):
748            main.Append(CCFLAGS=['-arch', 'x86_64'])
749            main.Append(CFLAGS=['-arch', 'x86_64'])
750            main.Append(LINKFLAGS=['-arch', 'x86_64'])
751            main.Append(ASFLAGS=['-arch', 'x86_64'])
752except:
753    pass
754
755# Recent versions of scons substitute a "Null" object for Configure()
756# when configuration isn't necessary, e.g., if the "--help" option is
757# present.  Unfortuantely this Null object always returns false,
758# breaking all our configuration checks.  We replace it with our own
759# more optimistic null object that returns True instead.
760if not conf:
761    def NullCheck(*args, **kwargs):
762        return True
763
764    class NullConf:
765        def __init__(self, env):
766            self.env = env
767        def Finish(self):
768            return self.env
769        def __getattr__(self, mname):
770            return NullCheck
771
772    conf = NullConf(main)
773
774# Find Python include and library directories for embedding the
775# interpreter.  For consistency, we will use the same Python
776# installation used to run scons (and thus this script).  If you want
777# to link in an alternate version, see above for instructions on how
778# to invoke scons with a different copy of the Python interpreter.
779from distutils import sysconfig
780
781py_getvar = sysconfig.get_config_var
782
783py_debug = getattr(sys, 'pydebug', False)
784py_version = 'python' + py_getvar('VERSION') + (py_debug and "_d" or "")
785
786py_general_include = sysconfig.get_python_inc()
787py_platform_include = sysconfig.get_python_inc(plat_specific=True)
788py_includes = [ py_general_include ]
789if py_platform_include != py_general_include:
790    py_includes.append(py_platform_include)
791
792py_lib_path = [ py_getvar('LIBDIR') ]
793# add the prefix/lib/pythonX.Y/config dir, but only if there is no
794# shared library in prefix/lib/.
795if not py_getvar('Py_ENABLE_SHARED'):
796    py_lib_path.append(py_getvar('LIBPL'))
797
798py_libs = []
799for lib in py_getvar('LIBS').split() + py_getvar('SYSLIBS').split():
800    if not lib.startswith('-l'):
801        # Python requires some special flags to link (e.g. -framework
802        # common on OS X systems), assume appending preserves order
803        main.Append(LINKFLAGS=[lib])
804    else:
805        lib = lib[2:]
806        if lib not in py_libs:
807            py_libs.append(lib)
808py_libs.append(py_version)
809
810main.Append(CPPPATH=py_includes)
811main.Append(LIBPATH=py_lib_path)
812
813# Cache build files in the supplied directory.
814if main['M5_BUILD_CACHE']:
815    print 'Using build cache located at', main['M5_BUILD_CACHE']
816    CacheDir(main['M5_BUILD_CACHE'])
817
818
819# verify that this stuff works
820if not conf.CheckHeader('Python.h', '<>'):
821    print "Error: can't find Python.h header in", py_includes
822    print "Install Python headers (package python-dev on Ubuntu and RedHat)"
823    Exit(1)
824
825for lib in py_libs:
826    if not conf.CheckLib(lib):
827        print "Error: can't find library %s required by python" % lib
828        Exit(1)
829
830# On Solaris you need to use libsocket for socket ops
831if not conf.CheckLibWithHeader(None, 'sys/socket.h', 'C++', 'accept(0,0,0);'):
832   if not conf.CheckLibWithHeader('socket', 'sys/socket.h', 'C++', 'accept(0,0,0);'):
833       print "Can't find library with socket calls (e.g. accept())"
834       Exit(1)
835
836# Check for zlib.  If the check passes, libz will be automatically
837# added to the LIBS environment variable.
838if not conf.CheckLibWithHeader('z', 'zlib.h', 'C++','zlibVersion();'):
839    print 'Error: did not find needed zlib compression library '\
840          'and/or zlib.h header file.'
841    print '       Please install zlib and try again.'
842    Exit(1)
843
844# If we have the protobuf compiler, also make sure we have the
845# development libraries. If the check passes, libprotobuf will be
846# automatically added to the LIBS environment variable. After
847# this, we can use the HAVE_PROTOBUF flag to determine if we have
848# got both protoc and libprotobuf available.
849main['HAVE_PROTOBUF'] = main['PROTOC'] and \
850    conf.CheckLibWithHeader('protobuf', 'google/protobuf/message.h',
851                            'C++', 'GOOGLE_PROTOBUF_VERIFY_VERSION;')
852
853# If we have the compiler but not the library, treat it as an error.
854if main['PROTOC'] and not main['HAVE_PROTOBUF']:
855    print 'Error: did not find protocol buffer library and/or headers.'
856    print '       Please install libprotobuf-dev and try again.'
857    Exit(1)
858
859# Check for librt.
860have_posix_clock = \
861    conf.CheckLibWithHeader(None, 'time.h', 'C',
862                            'clock_nanosleep(0,0,NULL,NULL);') or \
863    conf.CheckLibWithHeader('rt', 'time.h', 'C',
864                            'clock_nanosleep(0,0,NULL,NULL);')
865
866if conf.CheckLib('tcmalloc_minimal'):
867    have_tcmalloc = True
868else:
869    have_tcmalloc = False
870    print termcap.Yellow + termcap.Bold + \
871          "You can get a 12% performance improvement by installing tcmalloc "\
872          "(libgoogle-perftools-dev package on Ubuntu or RedHat)." + \
873          termcap.Normal
874
875if not have_posix_clock:
876    print "Can't find library for POSIX clocks."
877
878# Check for <fenv.h> (C99 FP environment control)
879have_fenv = conf.CheckHeader('fenv.h', '<>')
880if not have_fenv:
881    print "Warning: Header file <fenv.h> not found."
882    print "         This host has no IEEE FP rounding mode control."
883
884######################################################################
885#
886# Finish the configuration
887#
888main = conf.Finish()
889
890######################################################################
891#
892# Collect all non-global variables
893#
894
895# Define the universe of supported ISAs
896all_isa_list = [ ]
897Export('all_isa_list')
898
899class CpuModel(object):
900    '''The CpuModel class encapsulates everything the ISA parser needs to
901    know about a particular CPU model.'''
902
903    # Dict of available CPU model objects.  Accessible as CpuModel.dict.
904    dict = {}
905    list = []
906    defaults = []
907
908    # Constructor.  Automatically adds models to CpuModel.dict.
909    def __init__(self, name, filename, includes, strings, default=False):
910        self.name = name           # name of model
911        self.filename = filename   # filename for output exec code
912        self.includes = includes   # include files needed in exec file
913        # The 'strings' dict holds all the per-CPU symbols we can
914        # substitute into templates etc.
915        self.strings = strings
916
917        # This cpu is enabled by default
918        self.default = default
919
920        # Add self to dict
921        if name in CpuModel.dict:
922            raise AttributeError, "CpuModel '%s' already registered" % name
923        CpuModel.dict[name] = self
924        CpuModel.list.append(name)
925
926Export('CpuModel')
927
928# Sticky variables get saved in the variables file so they persist from
929# one invocation to the next (unless overridden, in which case the new
930# value becomes sticky).
931sticky_vars = Variables(args=ARGUMENTS)
932Export('sticky_vars')
933
934# Sticky variables that should be exported
935export_vars = []
936Export('export_vars')
937
938# For Ruby
939all_protocols = []
940Export('all_protocols')
941protocol_dirs = []
942Export('protocol_dirs')
943slicc_includes = []
944Export('slicc_includes')
945
946# Walk the tree and execute all SConsopts scripts that wil add to the
947# above variables
948if not GetOption('verbose'):
949    print "Reading SConsopts"
950for bdir in [ base_dir ] + extras_dir_list:
951    if not isdir(bdir):
952        print "Error: directory '%s' does not exist" % bdir
953        Exit(1)
954    for root, dirs, files in os.walk(bdir):
955        if 'SConsopts' in files:
956            if GetOption('verbose'):
957                print "Reading", joinpath(root, 'SConsopts')
958            SConscript(joinpath(root, 'SConsopts'))
959
960all_isa_list.sort()
961
962sticky_vars.AddVariables(
963    EnumVariable('TARGET_ISA', 'Target ISA', 'alpha', all_isa_list),
964    ListVariable('CPU_MODELS', 'CPU models',
965                 sorted(n for n,m in CpuModel.dict.iteritems() if m.default),
966                 sorted(CpuModel.list)),
967    BoolVariable('EFENCE', 'Link with Electric Fence malloc debugger',
968                 False),
969    BoolVariable('SS_COMPATIBLE_FP',
970                 'Make floating-point results compatible with SimpleScalar',
971                 False),
972    BoolVariable('USE_SSE2',
973                 'Compile for SSE2 (-msse2) to get IEEE FP on x86 hosts',
974                 False),
975    BoolVariable('USE_POSIX_CLOCK', 'Use POSIX Clocks', have_posix_clock),
976    BoolVariable('USE_FENV', 'Use <fenv.h> IEEE mode control', have_fenv),
977    BoolVariable('CP_ANNOTATE', 'Enable critical path annotation capability', False),
978    EnumVariable('PROTOCOL', 'Coherence protocol for Ruby', 'None',
979                  all_protocols),
980    )
981
982# These variables get exported to #defines in config/*.hh (see src/SConscript).
983export_vars += ['USE_FENV', 'SS_COMPATIBLE_FP',
984                'TARGET_ISA', 'CP_ANNOTATE', 'USE_POSIX_CLOCK', 'PROTOCOL',
985                'HAVE_STATIC_ASSERT', 'HAVE_PROTOBUF']
986
987###################################################
988#
989# Define a SCons builder for configuration flag headers.
990#
991###################################################
992
993# This function generates a config header file that #defines the
994# variable symbol to the current variable setting (0 or 1).  The source
995# operands are the name of the variable and a Value node containing the
996# value of the variable.
997def build_config_file(target, source, env):
998    (variable, value) = [s.get_contents() for s in source]
999    f = file(str(target[0]), 'w')
1000    print >> f, '#define', variable, value
1001    f.close()
1002    return None
1003
1004# Combine the two functions into a scons Action object.
1005config_action = MakeAction(build_config_file, Transform("CONFIG H", 2))
1006
1007# The emitter munges the source & target node lists to reflect what
1008# we're really doing.
1009def config_emitter(target, source, env):
1010    # extract variable name from Builder arg
1011    variable = str(target[0])
1012    # True target is config header file
1013    target = joinpath('config', variable.lower() + '.hh')
1014    val = env[variable]
1015    if isinstance(val, bool):
1016        # Force value to 0/1
1017        val = int(val)
1018    elif isinstance(val, str):
1019        val = '"' + val + '"'
1020
1021    # Sources are variable name & value (packaged in SCons Value nodes)
1022    return ([target], [Value(variable), Value(val)])
1023
1024config_builder = Builder(emitter = config_emitter, action = config_action)
1025
1026main.Append(BUILDERS = { 'ConfigFile' : config_builder })
1027
1028# libelf build is shared across all configs in the build root.
1029main.SConscript('ext/libelf/SConscript',
1030                variant_dir = joinpath(build_root, 'libelf'))
1031
1032# gzstream build is shared across all configs in the build root.
1033main.SConscript('ext/gzstream/SConscript',
1034                variant_dir = joinpath(build_root, 'gzstream'))
1035
1036###################################################
1037#
1038# This function is used to set up a directory with switching headers
1039#
1040###################################################
1041
1042main['ALL_ISA_LIST'] = all_isa_list
1043def make_switching_dir(dname, switch_headers, env):
1044    # Generate the header.  target[0] is the full path of the output
1045    # header to generate.  'source' is a dummy variable, since we get the
1046    # list of ISAs from env['ALL_ISA_LIST'].
1047    def gen_switch_hdr(target, source, env):
1048        fname = str(target[0])
1049        f = open(fname, 'w')
1050        isa = env['TARGET_ISA'].lower()
1051        print >>f, '#include "%s/%s/%s"' % (dname, isa, basename(fname))
1052        f.close()
1053
1054    # Build SCons Action object. 'varlist' specifies env vars that this
1055    # action depends on; when env['ALL_ISA_LIST'] changes these actions
1056    # should get re-executed.
1057    switch_hdr_action = MakeAction(gen_switch_hdr,
1058                          Transform("GENERATE"), varlist=['ALL_ISA_LIST'])
1059
1060    # Instantiate actions for each header
1061    for hdr in switch_headers:
1062        env.Command(hdr, [], switch_hdr_action)
1063Export('make_switching_dir')
1064
1065###################################################
1066#
1067# Define build environments for selected configurations.
1068#
1069###################################################
1070
1071for variant_path in variant_paths:
1072    print "Building in", variant_path
1073
1074    # Make a copy of the build-root environment to use for this config.
1075    env = main.Clone()
1076    env['BUILDDIR'] = variant_path
1077
1078    # variant_dir is the tail component of build path, and is used to
1079    # determine the build parameters (e.g., 'ALPHA_SE')
1080    (build_root, variant_dir) = splitpath(variant_path)
1081
1082    # Set env variables according to the build directory config.
1083    sticky_vars.files = []
1084    # Variables for $BUILD_ROOT/$VARIANT_DIR are stored in
1085    # $BUILD_ROOT/variables/$VARIANT_DIR so you can nuke
1086    # $BUILD_ROOT/$VARIANT_DIR without losing your variables settings.
1087    current_vars_file = joinpath(build_root, 'variables', variant_dir)
1088    if isfile(current_vars_file):
1089        sticky_vars.files.append(current_vars_file)
1090        print "Using saved variables file %s" % current_vars_file
1091    else:
1092        # Build dir-specific variables file doesn't exist.
1093
1094        # Make sure the directory is there so we can create it later
1095        opt_dir = dirname(current_vars_file)
1096        if not isdir(opt_dir):
1097            mkdir(opt_dir)
1098
1099        # Get default build variables from source tree.  Variables are
1100        # normally determined by name of $VARIANT_DIR, but can be
1101        # overridden by '--default=' arg on command line.
1102        default = GetOption('default')
1103        opts_dir = joinpath(main.root.abspath, 'build_opts')
1104        if default:
1105            default_vars_files = [joinpath(build_root, 'variables', default),
1106                                  joinpath(opts_dir, default)]
1107        else:
1108            default_vars_files = [joinpath(opts_dir, variant_dir)]
1109        existing_files = filter(isfile, default_vars_files)
1110        if existing_files:
1111            default_vars_file = existing_files[0]
1112            sticky_vars.files.append(default_vars_file)
1113            print "Variables file %s not found,\n  using defaults in %s" \
1114                  % (current_vars_file, default_vars_file)
1115        else:
1116            print "Error: cannot find variables file %s or " \
1117                  "default file(s) %s" \
1118                  % (current_vars_file, ' or '.join(default_vars_files))
1119            Exit(1)
1120
1121    # Apply current variable settings to env
1122    sticky_vars.Update(env)
1123
1124    help_texts["local_vars"] += \
1125        "Build variables for %s:\n" % variant_dir \
1126                 + sticky_vars.GenerateHelpText(env)
1127
1128    # Process variable settings.
1129
1130    if not have_fenv and env['USE_FENV']:
1131        print "Warning: <fenv.h> not available; " \
1132              "forcing USE_FENV to False in", variant_dir + "."
1133        env['USE_FENV'] = False
1134
1135    if not env['USE_FENV']:
1136        print "Warning: No IEEE FP rounding mode control in", variant_dir + "."
1137        print "         FP results may deviate slightly from other platforms."
1138
1139    if env['EFENCE']:
1140        env.Append(LIBS=['efence'])
1141
1142    # Save sticky variable settings back to current variables file
1143    sticky_vars.Save(current_vars_file, env)
1144
1145    if env['USE_SSE2']:
1146        env.Append(CCFLAGS=['-msse2'])
1147
1148    if have_tcmalloc:
1149        env.Append(LIBS=['tcmalloc_minimal'])
1150
1151    # The src/SConscript file sets up the build rules in 'env' according
1152    # to the configured variables.  It returns a list of environments,
1153    # one for each variant build (debug, opt, etc.)
1154    envList = SConscript('src/SConscript', variant_dir = variant_path,
1155                         exports = 'env')
1156
1157    # Set up the regression tests for each build.
1158    for e in envList:
1159        SConscript('tests/SConscript',
1160                   variant_dir = joinpath(variant_path, 'tests', e.Label),
1161                   exports = { 'env' : e }, duplicate = False)
1162
1163# base help text
1164Help('''
1165Usage: scons [scons options] [build variables] [target(s)]
1166
1167Extra scons options:
1168%(options)s
1169
1170Global build variables:
1171%(global_vars)s
1172
1173%(local_vars)s
1174''' % help_texts)
1175