SConstruct revision 9396:0c0ec9d87746
1360SN/A# -*- mode:python -*-
21458SN/A
3360SN/A# Copyright (c) 2011 Advanced Micro Devices, Inc.
4360SN/A# Copyright (c) 2009 The Hewlett-Packard Development Company
5360SN/A# Copyright (c) 2004-2005 The Regents of The University of Michigan
6360SN/A# All rights reserved.
7360SN/A#
8360SN/A# Redistribution and use in source and binary forms, with or without
9360SN/A# modification, are permitted provided that the following conditions are
10360SN/A# met: redistributions of source code must retain the above copyright
11360SN/A# notice, this list of conditions and the following disclaimer;
12360SN/A# redistributions in binary form must reproduce the above copyright
13360SN/A# notice, this list of conditions and the following disclaimer in the
14360SN/A# documentation and/or other materials provided with the distribution;
15360SN/A# neither the name of the copyright holders nor the names of its
16360SN/A# contributors may be used to endorse or promote products derived from
17360SN/A# this software without specific prior written permission.
18360SN/A#
19360SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20360SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21360SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22360SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23360SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24360SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25360SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26360SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
272665Ssaidi@eecs.umich.edu# 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.
30360SN/A#
31360SN/A# Authors: Steve Reinhardt
321354SN/A#          Nathan Binkert
331354SN/A
34360SN/A###################################################
352764Sstever@eecs.umich.edu#
362764Sstever@eecs.umich.edu# SCons top-level build description (SConstruct) file.
372064SN/A#
38360SN/A# While in this directory ('gem5'), just type 'scons' to build the default
39360SN/A# configuration (see below), or type 'scons build/<CONFIG>/<binary>'
40360SN/A# to build some other configuration (e.g., 'build/ALPHA/gem5.opt' for
41360SN/A# the optimized full-system version).
42360SN/A#
43360SN/A# You can build gem5 in a different directory as long as there is a
441354SN/A# 'build/<CONFIG>' somewhere along the target path.  The build system
45360SN/A# expects that all configs under the same build directory are being
461809SN/A# built for the same host system.
471809SN/A#
481809SN/A# Examples:
493113Sgblack@eecs.umich.edu#
503113Sgblack@eecs.umich.edu#   The following two commands are equivalent.  The '-u' option tells
511999SN/A#   scons to search up the directory tree for this SConstruct file.
52360SN/A#   % cd <path-to-src>/gem5 ; scons build/ALPHA/gem5.debug
533113Sgblack@eecs.umich.edu#   % cd <path-to-src>/gem5/build/ALPHA; scons -u gem5.debug
542474SN/A#
55360SN/A#   The following two commands are equivalent and demonstrate building
562462SN/A#   in a directory outside of the source tree.  The '-C' option tells
571354SN/A#   scons to chdir to the specified directory to find this SConstruct
582474SN/A#   file.
592680Sktlim@umich.edu#   % cd <path-to-src>/gem5 ; scons /local/foo/build/ALPHA/gem5.debug
602474SN/A#   % cd /local/foo/build/ALPHA; scons -C <path-to-src>/gem5 gem5.debug
612474SN/A#
621354SN/A# You can use 'scons -H' to print scons options.  If you're in this
63360SN/A# 'gem5' directory (or use -u or -C to tell scons where to find this
64360SN/A# file), you can use 'scons -h' to print all the gem5-specific build
65360SN/A# options as well.
66360SN/A#
67360SN/A###################################################
68360SN/A
69360SN/A# Check for recent-enough Python and SCons versions.
70360SN/Atry:
71378SN/A    # Really old versions of scons only take two options for the
721450SN/A    # function, so check once without the revision and once with the
733114Sgblack@eecs.umich.edu    # revision, the first instance will fail for stuff other than
74360SN/A    # 0.98, and the second will fail for 0.98.0
75360SN/A    EnsureSConsVersion(0, 98)
76360SN/A    EnsureSConsVersion(0, 98, 1)
77360SN/Aexcept SystemExit, e:
78360SN/A    print """
79360SN/AFor more details, see:
80360SN/A    http://gem5.org/Dependencies
81360SN/A"""
82360SN/A    raise
832680Sktlim@umich.edu
84360SN/A# We ensure the python version early because we have stuff that
85360SN/A# requires python 2.4
86360SN/Atry:
87360SN/A    EnsurePythonVersion(2, 4)
88360SN/Aexcept SystemExit, e:
89360SN/A    print """
90360SN/AYou can use a non-default installation of the Python interpreter by
91360SN/Aeither (1) rearranging your PATH so that scons finds the non-default
92360SN/A'python' first or (2) explicitly invoking an alternative interpreter
93360SN/Aon the scons script.
94360SN/A
953114Sgblack@eecs.umich.eduFor more details, see:
96360SN/A    http://gem5.org/wiki/index.php/Using_a_non-default_Python_installation
97360SN/A"""
98360SN/A    raise
99360SN/A
100360SN/A# Global Python includes
101360SN/Aimport os
102360SN/Aimport re
103360SN/Aimport subprocess
104360SN/Aimport sys
105360SN/A
106360SN/Afrom os import mkdir, environ
107360SN/Afrom os.path import abspath, basename, dirname, expanduser, normpath
108360SN/Afrom os.path import exists,  isdir, isfile
109360SN/Afrom os.path import join as joinpath, split as splitpath
110360SN/A
111360SN/A# SCons includes
112360SN/Aimport SCons
113360SN/Aimport SCons.Node
114360SN/A
115360SN/Aextra_python_paths = [
116360SN/A    Dir('src/python').srcnode().abspath, # gem5 includes
1172400SN/A    Dir('ext/ply').srcnode().abspath, # ply is used by several files
118360SN/A    ]
1192461SN/A
120360SN/Asys.path[1:1] = extra_python_paths
121360SN/A
122360SN/Afrom m5.util import compareVersions, readCommand
123360SN/Afrom m5.util.terminal import get_termcap
124360SN/A
125360SN/Ahelp_texts = {
1262400SN/A    "options" : "",
127360SN/A    "global_vars" : "",
1282461SN/A    "local_vars" : ""
129360SN/A}
130360SN/A
131360SN/AExport("help_texts")
132360SN/A
133360SN/A
134360SN/A# There's a bug in scons in that (1) by default, the help texts from
135360SN/A# AddOption() are supposed to be displayed when you type 'scons -h'
136360SN/A# and (2) you can override the help displayed by 'scons -h' using the
137360SN/A# Help() function, but these two features are incompatible: once
138360SN/A# you've overridden the help text using Help(), there's no way to get
139360SN/A# at the help texts from AddOptions.  See:
140360SN/A#     http://scons.tigris.org/issues/show_bug.cgi?id=2356
141360SN/A#     http://scons.tigris.org/issues/show_bug.cgi?id=2611
142360SN/A# This hack lets us extract the help text from AddOptions and
143360SN/A# re-inject it via Help().  Ideally someday this bug will be fixed and
144360SN/A# we can just use AddOption directly.
145360SN/Adef AddLocalOption(*args, **kwargs):
146360SN/A    col_width = 30
147360SN/A
148360SN/A    help = "  " + ", ".join(args)
149360SN/A    if "help" in kwargs:
150360SN/A        length = len(help)
151360SN/A        if length >= col_width:
152360SN/A            help += "\n" + " " * col_width
153360SN/A        else:
154360SN/A            help += " " * (col_width - length)
155360SN/A        help += kwargs["help"]
156360SN/A    help_texts["options"] += help + "\n"
157360SN/A
158360SN/A    AddOption(*args, **kwargs)
159360SN/A
160360SN/AAddLocalOption('--colors', dest='use_colors', action='store_true',
161502SN/A               help="Add color to abbreviated scons output")
162360SN/AAddLocalOption('--no-colors', dest='use_colors', action='store_false',
163502SN/A               help="Don't add color to abbreviated scons output")
164360SN/AAddLocalOption('--default', dest='default', type='string', action='store',
165360SN/A               help='Override which build_opts file to use for defaults')
166360SN/AAddLocalOption('--ignore-style', dest='ignore_style', action='store_true',
167360SN/A               help='Disable style checking hooks')
168360SN/AAddLocalOption('--no-lto', dest='no_lto', action='store_true',
169360SN/A               help='Disable Link-Time Optimization for fast')
170360SN/AAddLocalOption('--update-ref', dest='update_ref', action='store_true',
171360SN/A               help='Update test reference outputs')
172360SN/AAddLocalOption('--verbose', dest='verbose', action='store_true',
173360SN/A               help='Print full tool command lines')
174360SN/A
175378SN/Atermcap = get_termcap(GetOption('use_colors'))
1761706SN/A
1773114Sgblack@eecs.umich.edu########################################################################
178378SN/A#
179378SN/A# Set up the main build environment.
180378SN/A#
181378SN/A########################################################################
182378SN/Ause_vars = set([ 'AS', 'AR', 'CC', 'CXX', 'HOME', 'LD_LIBRARY_PATH',
1831706SN/A                 'LIBRARY_PATH', 'PATH', 'PKG_CONFIG_PATH', 'PYTHONPATH',
1843114Sgblack@eecs.umich.edu                 'RANLIB', 'SWIG' ])
185360SN/A
186378SN/Ause_env = {}
1871706SN/Afor key,val in os.environ.iteritems():
1883114Sgblack@eecs.umich.edu    if key in use_vars or key.startswith("M5"):
189378SN/A        use_env[key] = val
190378SN/A
1911706SN/Amain = Environment(ENV=use_env)
1923114Sgblack@eecs.umich.edumain.Decider('MD5-timestamp')
193378SN/Amain.root = Dir(".")         # The current directory (where this file lives).
194378SN/Amain.srcdir = Dir("src")     # The source directory
1951706SN/A
1963114Sgblack@eecs.umich.edumain_dict_keys = main.Dictionary().keys()
197378SN/A
198378SN/A# Check that we have a C/C++ compiler
1991706SN/Aif not ('CC' in main_dict_keys and 'CXX' in main_dict_keys):
2003114Sgblack@eecs.umich.edu    print "No C++ compiler installed (package g++ on Ubuntu and RedHat)"
201378SN/A    Exit(1)
202378SN/A
2031706SN/A# Check that swig is present
2043114Sgblack@eecs.umich.eduif not 'SWIG' in main_dict_keys:
205378SN/A    print "swig is not installed (package swig on Ubuntu and RedHat)"
206378SN/A    Exit(1)
2071706SN/A
2083114Sgblack@eecs.umich.edu# add useful python code PYTHONPATH so it can be used by subprocesses
209378SN/A# as well
210378SN/Amain.AppendENVPath('PYTHONPATH', extra_python_paths)
2111706SN/A
2123114Sgblack@eecs.umich.edu########################################################################
213378SN/A#
214378SN/A# Mercurial Stuff.
2151706SN/A#
2163114Sgblack@eecs.umich.edu# If the gem5 directory is a mercurial repository, we should do some
217378SN/A# extra things.
218378SN/A#
2191706SN/A########################################################################
2203114Sgblack@eecs.umich.edu
221360SN/Ahgdir = main.root.Dir(".hg")
222511SN/A
2231706SN/Amercurial_style_message = """
2243114Sgblack@eecs.umich.eduYou're missing the gem5 style hook, which automatically checks your code
225511SN/Aagainst the gem5 style rules on hg commit and qrefresh commands.  This
226511SN/Ascript will now install the hook in your .hg/hgrc file.
2271706SN/APress enter to continue, or ctrl-c to abort: """
2283114Sgblack@eecs.umich.edu
2291706SN/Amercurial_style_hook = """
2301706SN/A# The following lines were automatically added by gem5/SConstruct
2311706SN/A# to provide the gem5 style-checking hooks
2321706SN/A[extensions]
2333114Sgblack@eecs.umich.edustyle = %s/util/style.py
2341706SN/A
2351706SN/A[hooks]
2361706SN/Apretxncommit.style = python:style.check_style
2371706SN/Apre-qrefresh.style = python:style.check_style
2383114Sgblack@eecs.umich.edu# End of SConstruct additions
2391706SN/A
240511SN/A""" % (main.root.abspath)
2411999SN/A
2421999SN/Amercurial_lib_not_found = """
2433114Sgblack@eecs.umich.eduMercurial libraries cannot be found, ignoring style hook.  If
2441999SN/Ayou are a gem5 developer, please fix this and run the style
2451999SN/Ahook. It is important.
2461999SN/A"""
2471999SN/A
2483114Sgblack@eecs.umich.edu# Check for style hook and prompt for installation if it's not there.
2491999SN/A# Skip this if --ignore-style was specified, there's no .hg dir to
2503079Sstever@eecs.umich.edu# install a hook in, or there's no interactive terminal to prompt.
2513079Sstever@eecs.umich.eduif not GetOption('ignore_style') and hgdir.exists() and sys.stdin.isatty():
2523114Sgblack@eecs.umich.edu    style_hook = True
2533079Sstever@eecs.umich.edu    try:
2542093SN/A        from mercurial import ui
2552093SN/A        ui = ui.ui()
2563114Sgblack@eecs.umich.edu        ui.readconfig(hgdir.File('hgrc').abspath)
2572093SN/A        style_hook = ui.config('hooks', 'pretxncommit.style', None) and \
2582687Sksewell@umich.edu                     ui.config('hooks', 'pre-qrefresh.style', None)
2592687Sksewell@umich.edu    except ImportError:
2603114Sgblack@eecs.umich.edu        print mercurial_lib_not_found
2612687Sksewell@umich.edu
2622238SN/A    if not style_hook:
2632238SN/A        print mercurial_style_message,
2643114Sgblack@eecs.umich.edu        # continue unless user does ctrl-c/ctrl-d etc.
2652238SN/A        try:
2662238SN/A            raw_input()
2672238SN/A        except:
2683114Sgblack@eecs.umich.edu            print "Input exception, exiting scons.\n"
2692238SN/A            sys.exit(1)
2702238SN/A        hgrc_path = '%s/.hg/hgrc' % main.root.abspath
2712238SN/A        print "Adding style hook to", hgrc_path, "\n"
2723114Sgblack@eecs.umich.edu        try:
2732238SN/A            hgrc = open(hgrc_path, 'a')
2742238SN/A            hgrc.write(mercurial_style_hook)
2752238SN/A            hgrc.close()
2763114Sgblack@eecs.umich.edu        except:
2772238SN/A            print "Error updating", hgrc_path
2782238SN/A            sys.exit(1)
2792238SN/A
2803114Sgblack@eecs.umich.edu
2812238SN/A###################################################
2822238SN/A#
2832238SN/A# Figure out which configurations to set up based on the path(s) of
2843114Sgblack@eecs.umich.edu# the target(s).
2852238SN/A#
2862238SN/A###################################################
2872238SN/A
2883114Sgblack@eecs.umich.edu# Find default configuration & binary.
2892238SN/ADefault(environ.get('M5_DEFAULT_BINARY', 'build/ALPHA/gem5.debug'))
2902238SN/A
2912238SN/A# helper function: find last occurrence of element in list
2922238SN/Adef rfind(l, elt, offs = -1):
2932238SN/A    for i in range(len(l)+offs, 0, -1):
2942238SN/A        if l[i] == elt:
2953114Sgblack@eecs.umich.edu            return i
2962238SN/A    raise ValueError, "element not found"
2972238SN/A
2982238SN/A# Take a list of paths (or SCons Nodes) and return a list with all
2993114Sgblack@eecs.umich.edu# paths made absolute and ~-expanded.  Paths will be interpreted
3002238SN/A# relative to the launch directory unless a different root is provided
3012238SN/Adef makePathListAbsolute(path_list, root=GetLaunchDir()):
3022238SN/A    return [abspath(joinpath(root, expanduser(str(p))))
3033114Sgblack@eecs.umich.edu            for p in path_list]
3042238SN/A
3052238SN/A# Each target must have 'build' in the interior of the path; the
3062238SN/A# directory below this will determine the build parameters.  For
3073114Sgblack@eecs.umich.edu# example, for target 'foo/bar/build/ALPHA_SE/arch/alpha/blah.do' we
3082238SN/A# recognize that ALPHA_SE specifies the configuration because it
3092238SN/A# follow 'build' in the build path.
3101354SN/A
3111354SN/A# The funky assignment to "[:]" is needed to replace the list contents
3121354SN/A# in place rather than reassign the symbol to a new list, which
3131354SN/A# doesn't work (obviously!).
3141354SN/ABUILD_TARGETS[:] = makePathListAbsolute(BUILD_TARGETS)
3151354SN/A
3161354SN/A# Generate a list of the unique build roots and configs that the
3171354SN/A# collected targets reference.
3181354SN/Avariant_paths = []
3191354SN/Abuild_root = None
3201354SN/Afor t in BUILD_TARGETS:
3211354SN/A    path_dirs = t.split('/')
3221354SN/A    try:
3231354SN/A        build_top = rfind(path_dirs, 'build', -2)
3241609SN/A    except:
3251354SN/A        print "Error: no non-leaf 'build' dir found on target path", t
3261354SN/A        Exit(1)
3271354SN/A    this_build_root = joinpath('/',*path_dirs[:build_top+1])
3281354SN/A    if not build_root:
329360SN/A        build_root = this_build_root
330360SN/A    else:
331360SN/A        if this_build_root != build_root:
332360SN/A            print "Error: build targets not under same build root\n"\
333360SN/A                  "  %s\n  %s" % (build_root, this_build_root)
334360SN/A            Exit(1)
335360SN/A    variant_path = joinpath('/',*path_dirs[:build_top+2])
3363113Sgblack@eecs.umich.edu    if variant_path not in variant_paths:
3373113Sgblack@eecs.umich.edu        variant_paths.append(variant_path)
3383113Sgblack@eecs.umich.edu
3393113Sgblack@eecs.umich.edu# Make sure build_root exists (might not if this is the first build there)
3403113Sgblack@eecs.umich.eduif not isdir(build_root):
3413113Sgblack@eecs.umich.edu    mkdir(build_root)
3423113Sgblack@eecs.umich.edumain['BUILDROOT'] = build_root
3433113Sgblack@eecs.umich.edu
3443113Sgblack@eecs.umich.eduExport('main')
3453113Sgblack@eecs.umich.edu
3463113Sgblack@eecs.umich.edumain.SConsignFile(joinpath(build_root, "sconsign"))
3473113Sgblack@eecs.umich.edu
3483113Sgblack@eecs.umich.edu# Default duplicate option is to use hard links, but this messes up
3493113Sgblack@eecs.umich.edu# when you use emacs to edit a file in the target dir, as emacs moves
3503113Sgblack@eecs.umich.edu# file to file~ then copies to file, breaking the link.  Symbolic
3513113Sgblack@eecs.umich.edu# (soft) links work better.
3523113Sgblack@eecs.umich.edumain.SetOption('duplicate', 'soft-copy')
3533113Sgblack@eecs.umich.edu
3543113Sgblack@eecs.umich.edu#
3553113Sgblack@eecs.umich.edu# Set up global sticky variables... these are common to an entire build
3563113Sgblack@eecs.umich.edu# tree (not specific to a particular build like ALPHA_SE)
3573113Sgblack@eecs.umich.edu#
3583113Sgblack@eecs.umich.edu
3593277Sgblack@eecs.umich.eduglobal_vars_file = joinpath(build_root, 'variables.global')
3603277Sgblack@eecs.umich.edu
3613277Sgblack@eecs.umich.eduglobal_vars = Variables(global_vars_file, args=ARGUMENTS)
3623277Sgblack@eecs.umich.edu
3633277Sgblack@eecs.umich.eduglobal_vars.AddVariables(
3643277Sgblack@eecs.umich.edu    ('CC', 'C compiler', environ.get('CC', main['CC'])),
3653277Sgblack@eecs.umich.edu    ('CXX', 'C++ compiler', environ.get('CXX', main['CXX'])),
3663277Sgblack@eecs.umich.edu    ('SWIG', 'SWIG tool', environ.get('SWIG', main['SWIG'])),
3673113Sgblack@eecs.umich.edu    ('PROTOC', 'protoc tool', environ.get('PROTOC', 'protoc')),
3683113Sgblack@eecs.umich.edu    ('BATCH', 'Use batch pool for build and tests', False),
3693113Sgblack@eecs.umich.edu    ('BATCH_CMD', 'Batch pool submission command name', 'qdo'),
3703113Sgblack@eecs.umich.edu    ('M5_BUILD_CACHE', 'Cache built objects in this directory', False),
3713113Sgblack@eecs.umich.edu    ('EXTRAS', 'Add extra directories to the compilation', '')
3723113Sgblack@eecs.umich.edu    )
3733113Sgblack@eecs.umich.edu
3743114Sgblack@eecs.umich.edu# Update main environment with values from ARGUMENTS & global_vars_file
3753113Sgblack@eecs.umich.eduglobal_vars.Update(main)
3763114Sgblack@eecs.umich.eduhelp_texts["global_vars"] += global_vars.GenerateHelpText(main)
3773113Sgblack@eecs.umich.edu
3783114Sgblack@eecs.umich.edu# Save sticky variable settings back to current variables file
3793113Sgblack@eecs.umich.eduglobal_vars.Save(global_vars_file, main)
3803113Sgblack@eecs.umich.edu
3813113Sgblack@eecs.umich.edu# Parse EXTRAS variable to build list of all directories where we're
3823113Sgblack@eecs.umich.edu# look for sources etc.  This list is exported as extras_dir_list.
3833113Sgblack@eecs.umich.edubase_dir = main.srcdir.abspath
3843113Sgblack@eecs.umich.eduif main['EXTRAS']:
3853113Sgblack@eecs.umich.edu    extras_dir_list = makePathListAbsolute(main['EXTRAS'].split(':'))
3863113Sgblack@eecs.umich.eduelse:
3873113Sgblack@eecs.umich.edu    extras_dir_list = []
3883113Sgblack@eecs.umich.edu
3893113Sgblack@eecs.umich.eduExport('base_dir')
3903113Sgblack@eecs.umich.eduExport('extras_dir_list')
3913113Sgblack@eecs.umich.edu
3923113Sgblack@eecs.umich.edu# the ext directory should be on the #includes path
3933113Sgblack@eecs.umich.edumain.Append(CPPPATH=[Dir('ext')])
3943113Sgblack@eecs.umich.edu
3953113Sgblack@eecs.umich.edudef strip_build_path(path, env):
3963113Sgblack@eecs.umich.edu    path = str(path)
3973113Sgblack@eecs.umich.edu    variant_base = env['BUILDROOT'] + os.path.sep
3983113Sgblack@eecs.umich.edu    if path.startswith(variant_base):
3993113Sgblack@eecs.umich.edu        path = path[len(variant_base):]
4003113Sgblack@eecs.umich.edu    elif path.startswith('build/'):
4013113Sgblack@eecs.umich.edu        path = path[6:]
4023113Sgblack@eecs.umich.edu    return path
4033113Sgblack@eecs.umich.edu
4043113Sgblack@eecs.umich.edu# Generate a string of the form:
4053113Sgblack@eecs.umich.edu#   common/path/prefix/src1, src2 -> tgt1, tgt2
4063113Sgblack@eecs.umich.edu# to print while building.
4073113Sgblack@eecs.umich.educlass Transform(object):
4083113Sgblack@eecs.umich.edu    # all specific color settings should be here and nowhere else
4093113Sgblack@eecs.umich.edu    tool_color = termcap.Normal
4103113Sgblack@eecs.umich.edu    pfx_color = termcap.Yellow
4113113Sgblack@eecs.umich.edu    srcs_color = termcap.Yellow + termcap.Bold
4123113Sgblack@eecs.umich.edu    arrow_color = termcap.Blue + termcap.Bold
4133113Sgblack@eecs.umich.edu    tgts_color = termcap.Yellow + termcap.Bold
4143113Sgblack@eecs.umich.edu
4153113Sgblack@eecs.umich.edu    def __init__(self, tool, max_sources=99):
4163113Sgblack@eecs.umich.edu        self.format = self.tool_color + (" [%8s] " % tool) \
4173113Sgblack@eecs.umich.edu                      + self.pfx_color + "%s" \
4183113Sgblack@eecs.umich.edu                      + self.srcs_color + "%s" \
4193113Sgblack@eecs.umich.edu                      + self.arrow_color + " -> " \
4203113Sgblack@eecs.umich.edu                      + self.tgts_color + "%s" \
4213113Sgblack@eecs.umich.edu                      + termcap.Normal
4223113Sgblack@eecs.umich.edu        self.max_sources = max_sources
4233113Sgblack@eecs.umich.edu
4243113Sgblack@eecs.umich.edu    def __call__(self, target, source, env, for_signature=None):
4253113Sgblack@eecs.umich.edu        # truncate source list according to max_sources param
4263113Sgblack@eecs.umich.edu        source = source[0:self.max_sources]
4273113Sgblack@eecs.umich.edu        def strip(f):
4283113Sgblack@eecs.umich.edu            return strip_build_path(str(f), env)
4293113Sgblack@eecs.umich.edu        if len(source) > 0:
430378SN/A            srcs = map(strip, source)
431378SN/A        else:
432378SN/A            srcs = ['']
433360SN/A        tgts = map(strip, target)
4341450SN/A        # surprisingly, os.path.commonprefix is a dumb char-by-char string
4353114Sgblack@eecs.umich.edu        # operation that has nothing to do with paths.
4362680Sktlim@umich.edu        com_pfx = os.path.commonprefix(srcs + tgts)
437360SN/A        com_pfx_len = len(com_pfx)
4382680Sktlim@umich.edu        if com_pfx:
4392680Sktlim@umich.edu            # do some cleanup and sanity checking on common prefix
440360SN/A            if com_pfx[-1] == ".":
4411969SN/A                # prefix matches all but file extension: ok
442360SN/A                # back up one to change 'foo.cc -> o' to 'foo.cc -> .o'
443360SN/A                com_pfx = com_pfx[0:-1]
444360SN/A            elif com_pfx[-1] == "/":
4451458SN/A                # common prefix is directory path: OK
446360SN/A                pass
447360SN/A            else:
448360SN/A                src0_len = len(srcs[0])
4492553SN/A                tgt0_len = len(tgts[0])
4502553SN/A                if src0_len == com_pfx_len:
4512553SN/A                    # source is a substring of target, OK
4522553SN/A                    pass
4532553SN/A                elif tgt0_len == com_pfx_len:
4542553SN/A                    # target is a substring of source, need to back up to
4552553SN/A                    # avoid empty string on RHS of arrow
4562553SN/A                    sep_idx = com_pfx.rfind(".")
4571458SN/A                    if sep_idx != -1:
458360SN/A                        com_pfx = com_pfx[0:sep_idx]
459360SN/A                    else:
4601706SN/A                        com_pfx = ''
4612680Sktlim@umich.edu                elif src0_len > com_pfx_len and srcs[0][com_pfx_len] == ".":
462360SN/A                    # still splitting at file extension: ok
463360SN/A                    pass
464360SN/A                else:
465378SN/A                    # probably a fluke; ignore it
466360SN/A                    com_pfx = ''
4671450SN/A        # recalculate length in case com_pfx was modified
4683114Sgblack@eecs.umich.edu        com_pfx_len = len(com_pfx)
4692680Sktlim@umich.edu        def fmt(files):
470360SN/A            f = map(lambda s: s[com_pfx_len:], files)
471360SN/A            return ', '.join(f)
472360SN/A        return self.format % (com_pfx, fmt(srcs), fmt(tgts))
4732680Sktlim@umich.edu
4741458SN/AExport('Transform')
475360SN/A
476360SN/A# enable the regression script to use the termcap
477360SN/Amain['TERMCAP'] = termcap
478360SN/A
4791706SN/Aif GetOption('verbose'):
4801458SN/A    def MakeAction(action, string, *args, **kwargs):
481360SN/A        return Action(action, *args, **kwargs)
482360SN/Aelse:
4832680Sktlim@umich.edu    MakeAction = Action
4842680Sktlim@umich.edu    main['CCCOMSTR']        = Transform("CC")
485360SN/A    main['CXXCOMSTR']       = Transform("CXX")
486360SN/A    main['ASCOMSTR']        = Transform("AS")
487360SN/A    main['SWIGCOMSTR']      = Transform("SWIG")
488360SN/A    main['ARCOMSTR']        = Transform("AR", 0)
489360SN/A    main['LINKCOMSTR']      = Transform("LINK", 0)
490360SN/A    main['RANLIBCOMSTR']    = Transform("RANLIB", 0)
491360SN/A    main['M4COMSTR']        = Transform("M4")
492360SN/A    main['SHCCCOMSTR']      = Transform("SHCC")
493360SN/A    main['SHCXXCOMSTR']     = Transform("SHCXX")
494360SN/AExport('MakeAction')
495360SN/A
496360SN/A# Initialize the Link-Time Optimization (LTO) flags
4971706SN/Amain['LTO_CCFLAGS'] = []
498360SN/Amain['LTO_LDFLAGS'] = []
499360SN/A
500360SN/ACXX_version = readCommand([main['CXX'],'--version'], exception=False)
501360SN/ACXX_V = readCommand([main['CXX'],'-V'], exception=False)
502360SN/A
5031706SN/Amain['GCC'] = CXX_version and CXX_version.find('g++') >= 0
5041706SN/Amain['SUNCC'] = CXX_V and CXX_V.find('Sun C++') >= 0
505360SN/Amain['ICC'] = CXX_V and CXX_V.find('Intel') >= 0
506360SN/Amain['CLANG'] = CXX_version and CXX_version.find('clang') >= 0
507360SN/Aif main['GCC'] + main['SUNCC'] + main['ICC'] + main['CLANG'] > 1:
5081970SN/A    print 'Error: How can we have two at the same time?'
509360SN/A    Exit(1)
510360SN/A
511360SN/A# Set up default C++ compiler flags
5121999SN/Aif main['GCC']:
5131999SN/A    main.Append(CCFLAGS=['-pipe'])
5141999SN/A    main.Append(CCFLAGS=['-fno-strict-aliasing'])
5153114Sgblack@eecs.umich.edu    main.Append(CCFLAGS=['-Wall', '-Wno-sign-compare', '-Wundef'])
5162680Sktlim@umich.edu    # Read the GCC version to check for versions with bugs
5171999SN/A    # Note CCVERSION doesn't work here because it is run with the CC
5181999SN/A    # before we override it from the command line
5191999SN/A    gcc_version = readCommand([main['CXX'], '-dumpversion'], exception=False)
5202680Sktlim@umich.edu    main['GCC_VERSION'] = gcc_version
5211999SN/A    if not compareVersions(gcc_version, '4.4.1') or \
5221999SN/A       not compareVersions(gcc_version, '4.4.2'):
5232680Sktlim@umich.edu        print 'Info: Tree vectorizer in GCC 4.4.1 & 4.4.2 is buggy, disabling.'
5241999SN/A        main.Append(CCFLAGS=['-fno-tree-vectorize'])
5251999SN/A    # c++0x support in gcc is useful already from 4.4, see
5261999SN/A    # http://gcc.gnu.org/projects/cxx0x.html for details
5271999SN/A    if compareVersions(gcc_version, '4.4') >= 0:
5281999SN/A        main.Append(CXXFLAGS=['-std=c++0x'])
5291999SN/A
5301999SN/A    # LTO support is only really working properly from 4.6 and beyond
5311999SN/A    if compareVersions(gcc_version, '4.6') >= 0:
5322218SN/A        # Add the appropriate Link-Time Optimization (LTO) flags
5331999SN/A        # unless LTO is explicitly turned off. Note that these flags
5341999SN/A        # are only used by the fast target.
5351999SN/A        if not GetOption('no_lto'):
5361999SN/A            # Pass the LTO flag when compiling to produce GIMPLE
5371999SN/A            # output, we merely create the flags here and only append
5381999SN/A            # them later/
5391999SN/A            main['LTO_CCFLAGS'] = ['-flto=%d' % GetOption('num_jobs')]
5401999SN/A
5413114Sgblack@eecs.umich.edu            # Use the same amount of jobs for LTO as we are running
5422680Sktlim@umich.edu            # scons with, we hardcode the use of the linker plugin
5431999SN/A            # which requires either gold or GNU ld >= 2.21
5442680Sktlim@umich.edu            main['LTO_LDFLAGS'] = ['-flto=%d' % GetOption('num_jobs'),
5451999SN/A                                   '-fuse-linker-plugin']
5461999SN/A
5471999SN/Aelif main['ICC']:
5481999SN/A    pass #Fix me... add warning flags once we clean up icc warnings
5491999SN/Aelif main['SUNCC']:
5502680Sktlim@umich.edu    main.Append(CCFLAGS=['-Qoption ccfe'])
5511999SN/A    main.Append(CCFLAGS=['-features=gcc'])
5521999SN/A    main.Append(CCFLAGS=['-features=extensions'])
5531999SN/A    main.Append(CCFLAGS=['-library=stlport4'])
5541999SN/A    main.Append(CCFLAGS=['-xar'])
5551999SN/A    #main.Append(CCFLAGS=['-instances=semiexplicit'])
5561999SN/Aelif main['CLANG']:
5571999SN/A    clang_version_re = re.compile(".* version (\d+\.\d+)")
5581999SN/A    clang_version_match = clang_version_re.match(CXX_version)
5592218SN/A    if (clang_version_match):
5601999SN/A        clang_version = clang_version_match.groups()[0]
5611999SN/A        if compareVersions(clang_version, "2.9") < 0:
5621999SN/A            print 'Error: clang version 2.9 or newer required.'
5631999SN/A            print '       Installed version:', clang_version
5641999SN/A            Exit(1)
565378SN/A    else:
566360SN/A        print 'Error: Unable to determine clang version.'
5671450SN/A        Exit(1)
5683114Sgblack@eecs.umich.edu
5692680Sktlim@umich.edu    main.Append(CCFLAGS=['-pipe'])
570360SN/A    main.Append(CCFLAGS=['-fno-strict-aliasing'])
571360SN/A    main.Append(CCFLAGS=['-Wall', '-Wno-sign-compare', '-Wundef'])
572360SN/A    main.Append(CCFLAGS=['-Wno-tautological-compare'])
5732680Sktlim@umich.edu    main.Append(CCFLAGS=['-Wno-self-assign'])
5742400SN/A    # Ruby makes frequent use of extraneous parantheses in the printing
575360SN/A    # of if-statements
576360SN/A    main.Append(CCFLAGS=['-Wno-parentheses'])
577360SN/A
578360SN/A    # clang 2.9 does not play well with c++0x as it ships with C++
579360SN/A    # headers that produce errors, this was fixed in 3.0
5802218SN/A    if compareVersions(clang_version, "3") >= 0:
581360SN/A        main.Append(CXXFLAGS=['-std=c++0x'])
5823113Sgblack@eecs.umich.eduelse:
583360SN/A    print termcap.Yellow + termcap.Bold + 'Error' + termcap.Normal,
5841458SN/A    print "Don't know what compiler options to use for your compiler."
585360SN/A    print termcap.Yellow + '       compiler:' + termcap.Normal, main['CXX']
586360SN/A    print termcap.Yellow + '       version:' + termcap.Normal,
587360SN/A    if not CXX_version:
5881999SN/A        print termcap.Yellow + termcap.Bold + "COMMAND NOT FOUND!" +\
5891999SN/A               termcap.Normal
5901999SN/A    else:
5913114Sgblack@eecs.umich.edu        print CXX_version.replace('\n', '<nl>')
5922680Sktlim@umich.edu    print "       If you're trying to use a compiler other than GCC, ICC, SunCC,"
5931999SN/A    print "       or clang, there appears to be something wrong with your"
5942680Sktlim@umich.edu    print "       environment."
5951999SN/A    print "       "
5961999SN/A    print "       If you are trying to use a compiler other than those listed"
5971999SN/A    print "       above you will need to ease fix SConstruct and "
5981999SN/A    print "       src/SConscript to support that compiler."
5991999SN/A    Exit(1)
6002764Sstever@eecs.umich.edu
6012064SN/A# Set up common yacc/bison flags (needed for Ruby)
6022064SN/Amain['YACCFLAGS'] = '-d'
6032064SN/Amain['YACCHXXFILESUFFIX'] = '.hh'
6042064SN/A
6051999SN/A# Do this after we save setting back, or else we'll tack on an
6062064SN/A# extra 'qdo' every time we run scons.
6071999SN/Aif main['BATCH']:
6081999SN/A    main['CC']     = main['BATCH_CMD'] + ' ' + main['CC']
6092218SN/A    main['CXX']    = main['BATCH_CMD'] + ' ' + main['CXX']
6101999SN/A    main['AS']     = main['BATCH_CMD'] + ' ' + main['AS']
6113114Sgblack@eecs.umich.edu    main['AR']     = main['BATCH_CMD'] + ' ' + main['AR']
6123114Sgblack@eecs.umich.edu    main['RANLIB'] = main['BATCH_CMD'] + ' ' + main['RANLIB']
6131999SN/A
6141999SN/Aif sys.platform == 'cygwin':
6151999SN/A    # cygwin has some header file issues...
6161999SN/A    main.Append(CCFLAGS=["-Wno-uninitialized"])
6171999SN/A
618378SN/A# Check for the protobuf compiler
619360SN/Aprotoc_version = readCommand([main['PROTOC'], '--version'],
6201450SN/A                             exception='').split()
6213114Sgblack@eecs.umich.edu
6222680Sktlim@umich.edu# First two words should be "libprotoc x.y.z"
623360SN/Aif len(protoc_version) < 2 or protoc_version[0] != 'libprotoc':
624360SN/A    print termcap.Yellow + termcap.Bold + \
625360SN/A        'Warning: Protocol buffer compiler (protoc) not found.\n' + \
6262680Sktlim@umich.edu        '         Please install protobuf-compiler for tracing support.' + \
6272400SN/A        termcap.Normal
628360SN/A    main['PROTOC'] = False
629360SN/Aelse:
630360SN/A    # Determine the appropriate include path and library path using
631360SN/A    # pkg-config, that means we also need to check for pkg-config
632360SN/A    if not readCommand(['pkg-config', '--version'], exception=''):
6331458SN/A        print 'Error: pkg-config not found. Please install and retry.'
634360SN/A        Exit(1)
6353113Sgblack@eecs.umich.edu
636360SN/A    main.ParseConfig('pkg-config --cflags --libs-only-L protobuf')
6371458SN/A
638360SN/A    # Based on the availability of the compress stream wrappers,
639360SN/A    # require 2.1.0
6401999SN/A    min_protoc_version = '2.1.0'
6411999SN/A    if compareVersions(protoc_version[1], min_protoc_version) < 0:
6421999SN/A        print 'Error: protoc version', min_protoc_version, 'or newer required.'
6433114Sgblack@eecs.umich.edu        print '       Installed version:', protoc_version[1]
6442680Sktlim@umich.edu        Exit(1)
6451999SN/A
6461999SN/A# Check for SWIG
6471999SN/Aif not main.has_key('SWIG'):
6482680Sktlim@umich.edu    print 'Error: SWIG utility not found.'
6492400SN/A    print '       Please install (see http://www.swig.org) and retry.'
6501999SN/A    Exit(1)
6512764Sstever@eecs.umich.edu
6522064SN/A# Check for appropriate SWIG version
6532064SN/Aswig_version = readCommand([main['SWIG'], '-version'], exception='').split()
6542064SN/A# First 3 words should be "SWIG Version x.y.z"
6551999SN/Aif len(swig_version) < 3 or \
6561999SN/A        swig_version[0] != 'SWIG' or swig_version[1] != 'Version':
6572064SN/A    print 'Error determining SWIG version.'
6581999SN/A    Exit(1)
6591999SN/A
6601999SN/Amin_swig_version = '1.3.34'
6611999SN/Aif compareVersions(swig_version[2], min_swig_version) < 0:
6623114Sgblack@eecs.umich.edu    print 'Error: SWIG version', min_swig_version, 'or newer required.'
6631999SN/A    print '       Installed version:', swig_version[2]
6641999SN/A    Exit(1)
6651999SN/A
6661999SN/A# Set up SWIG flags & scanner
667378SN/Aswig_flags=Split('-c++ -python -modern -templatereduce $_CPPINCFLAGS')
668360SN/Amain.Append(SWIGFLAGS=swig_flags)
6691450SN/A
6703114Sgblack@eecs.umich.edu# filter out all existing swig scanners, they mess up the dependency
6712680Sktlim@umich.edu# stuff for some reason
672360SN/Ascanners = []
6732680Sktlim@umich.edufor scanner in main['SCANNERS']:
674360SN/A    skeys = scanner.skeys
6751969SN/A    if skeys == '.i':
676360SN/A        continue
677360SN/A
6781458SN/A    if isinstance(skeys, (list, tuple)) and '.i' in skeys:
679360SN/A        continue
680360SN/A
681360SN/A    scanners.append(scanner)
682360SN/A
683360SN/A# add the new swig scanner that we like better
6841458SN/Afrom SCons.Scanner import ClassicCPP as CPPScanner
685360SN/Aswig_inc_re = '^[ \t]*[%,#][ \t]*(?:include|import)[ \t]*(<|")([^>"]+)(>|")'
6863114Sgblack@eecs.umich.eduscanners.append(CPPScanner("SwigScan", [ ".i" ], "CPPPATH", swig_inc_re))
6873114Sgblack@eecs.umich.edu
6882021SN/A# replace the scanners list that has what we want
6891458SN/Amain['SCANNERS'] = scanners
690360SN/A
691360SN/A# Add a custom Check function to the Configure context so that we can
692360SN/A# figure out if the compiler adds leading underscores to global
6931706SN/A# variables.  This is needed for the autogenerated asm files that we
6941706SN/A# use for embedding the python code.
6951706SN/Adef CheckLeading(context):
6963114Sgblack@eecs.umich.edu    context.Message("Checking for leading underscore in global variables...")
6972680Sktlim@umich.edu    # 1) Define a global variable called x from asm so the C compiler
6981706SN/A    #    won't change the symbol at all.
6991706SN/A    # 2) Declare that variable.
7001706SN/A    # 3) Use the variable
7012680Sktlim@umich.edu    #
7022400SN/A    # If the compiler prepends an underscore, this will successfully
7031706SN/A    # link because the external symbol 'x' will be called '_x' which
7041706SN/A    # was defined by the asm statement.  If the compiler does not
7051706SN/A    # prepend an underscore, this will not successfully link because
7061706SN/A    # '_x' will have been defined by assembly, while the C portion of
7071706SN/A    # the code will be trying to use 'x'
7082218SN/A    ret = context.TryLink('''
7091706SN/A        asm(".globl _x; _x: .byte 0");
7103114Sgblack@eecs.umich.edu        extern int x;
7113114Sgblack@eecs.umich.edu        int main() { return x; }
7121706SN/A        ''', extension=".c")
7131706SN/A    context.env.Append(LEADING_UNDERSCORE=ret)
7141706SN/A    context.Result(ret)
7151706SN/A    return ret
7161706SN/A
7171706SN/A# Test for the presence of C++11 static asserts. If the compiler lacks
7181706SN/A# support for static asserts, base/compiler.hh enables a macro that
7191706SN/A# removes any static asserts in the code.
7203114Sgblack@eecs.umich.edudef CheckStaticAssert(context):
7212680Sktlim@umich.edu    context.Message("Checking for C++11 static_assert support...")
7221706SN/A    ret = context.TryCompile('''
7232680Sktlim@umich.edu        static_assert(1, "This assert is always true");
7241706SN/A        ''', extension=".cc")
7251706SN/A    context.env.Append(HAVE_STATIC_ASSERT=ret)
7261706SN/A    context.Result(ret)
7271706SN/A    return ret
7281706SN/A
7291706SN/A# Platform-specific configuration.  Note again that we assume that all
7301706SN/A# builds under a given build root run on the same host platform.
7311706SN/Aconf = Configure(main,
7322218SN/A                 conf_dir = joinpath(build_root, '.scons_config'),
7331706SN/A                 log_file = joinpath(build_root, 'scons_config.log'),
7343114Sgblack@eecs.umich.edu                 custom_tests = { 'CheckLeading' : CheckLeading,
7353114Sgblack@eecs.umich.edu                                  'CheckStaticAssert' : CheckStaticAssert,
7361706SN/A                                })
7371706SN/A
7381706SN/A# Check for leading underscores.  Don't really need to worry either
7391706SN/A# way so don't need to check the return code.
7401706SN/Aconf.CheckLeading()
7411999SN/A
7421999SN/A# Check for C++11 features we want to use if they exist
7431999SN/Aconf.CheckStaticAssert()
7443114Sgblack@eecs.umich.edu
7452680Sktlim@umich.edu# Check if we should compile a 64 bit binary on Mac OS X/Darwin
7461999SN/Atry:
7472680Sktlim@umich.edu    import platform
7481999SN/A    uname = platform.uname()
7491999SN/A    if uname[0] == 'Darwin' and compareVersions(uname[2], '9.0.0') >= 0:
7501999SN/A        if int(readCommand('sysctl -n hw.cpu64bit_capable')[0]):
7511999SN/A            main.Append(CCFLAGS=['-arch', 'x86_64'])
7521999SN/A            main.Append(CFLAGS=['-arch', 'x86_64'])
7532680Sktlim@umich.edu            main.Append(LINKFLAGS=['-arch', 'x86_64'])
7542680Sktlim@umich.edu            main.Append(ASFLAGS=['-arch', 'x86_64'])
7552680Sktlim@umich.eduexcept:
7561999SN/A    pass
7571999SN/A
7581999SN/A# Recent versions of scons substitute a "Null" object for Configure()
7591999SN/A# when configuration isn't necessary, e.g., if the "--help" option is
7602461SN/A# present.  Unfortuantely this Null object always returns false,
7612461SN/A# breaking all our configuration checks.  We replace it with our own
7622461SN/A# more optimistic null object that returns True instead.
7632091SN/Aif not conf:
7641999SN/A    def NullCheck(*args, **kwargs):
7652461SN/A        return True
7662461SN/A
7671999SN/A    class NullConf:
7681999SN/A        def __init__(self, env):
7691999SN/A            self.env = env
7701999SN/A        def Finish(self):
7711999SN/A            return self.env
7721999SN/A        def __getattr__(self, mname):
7731999SN/A            return NullCheck
7741999SN/A
7751999SN/A    conf = NullConf(main)
7761999SN/A
7772218SN/A# Find Python include and library directories for embedding the
7781999SN/A# interpreter.  For consistency, we will use the same Python
7791999SN/A# installation used to run scons (and thus this script).  If you want
7801999SN/A# to link in an alternate version, see above for instructions on how
7811999SN/A# to invoke scons with a different copy of the Python interpreter.
7821999SN/Afrom distutils import sysconfig
783378SN/A
784378SN/Apy_getvar = sysconfig.get_config_var
785378SN/A
786378SN/Apy_debug = getattr(sys, 'pydebug', False)
787378SN/Apy_version = 'python' + py_getvar('VERSION') + (py_debug and "_d" or "")
788378SN/A
789378SN/Apy_general_include = sysconfig.get_python_inc()
790378SN/Apy_platform_include = sysconfig.get_python_inc(plat_specific=True)
791360SN/Apy_includes = [ py_general_include ]
792378SN/Aif py_platform_include != py_general_include:
793378SN/A    py_includes.append(py_platform_include)
794378SN/A
795360SN/Apy_lib_path = [ py_getvar('LIBDIR') ]
7961450SN/A# add the prefix/lib/pythonX.Y/config dir, but only if there is no
7973114Sgblack@eecs.umich.edu# shared library in prefix/lib/.
798360SN/Aif not py_getvar('Py_ENABLE_SHARED'):
7992680Sktlim@umich.edu    py_lib_path.append(py_getvar('LIBPL'))
8002680Sktlim@umich.edu
8012680Sktlim@umich.edupy_libs = []
8022680Sktlim@umich.edufor lib in py_getvar('LIBS').split() + py_getvar('SYSLIBS').split():
8032680Sktlim@umich.edu    if not lib.startswith('-l'):
8042680Sktlim@umich.edu        # Python requires some special flags to link (e.g. -framework
805360SN/A        # common on OS X systems), assume appending preserves order
8062544SN/A        main.Append(LINKFLAGS=[lib])
8072544SN/A    else:
8082544SN/A        lib = lib[2:]
8092544SN/A        if lib not in py_libs:
8102544SN/A            py_libs.append(lib)
8112544SN/Apy_libs.append(py_version)
812360SN/A
813360SN/Amain.Append(CPPPATH=py_includes)
8142544SN/Amain.Append(LIBPATH=py_lib_path)
8152544SN/A
8162544SN/A# Cache build files in the supplied directory.
8172544SN/Aif main['M5_BUILD_CACHE']:
8182544SN/A    print 'Using build cache located at', main['M5_BUILD_CACHE']
8192544SN/A    CacheDir(main['M5_BUILD_CACHE'])
8202544SN/A
8212544SN/A
8222544SN/A# verify that this stuff works
8232544SN/Aif not conf.CheckHeader('Python.h', '<>'):
8242553SN/A    print "Error: can't find Python.h header in", py_includes
8251969SN/A    print "Install Python headers (package python-dev on Ubuntu and RedHat)"
8262680Sktlim@umich.edu    Exit(1)
827360SN/A
828360SN/Afor lib in py_libs:
8291458SN/A    if not conf.CheckLib(lib):
830360SN/A        print "Error: can't find library %s required by python" % lib
831360SN/A        Exit(1)
832378SN/A
833360SN/A# On Solaris you need to use libsocket for socket ops
8341450SN/Aif not conf.CheckLibWithHeader(None, 'sys/socket.h', 'C++', 'accept(0,0,0);'):
8353114Sgblack@eecs.umich.edu   if not conf.CheckLibWithHeader('socket', 'sys/socket.h', 'C++', 'accept(0,0,0);'):
8362680Sktlim@umich.edu       print "Can't find library with socket calls (e.g. accept())"
837360SN/A       Exit(1)
8382680Sktlim@umich.edu
8392680Sktlim@umich.edu# Check for zlib.  If the check passes, libz will be automatically
840360SN/A# added to the LIBS environment variable.
841360SN/Aif not conf.CheckLibWithHeader('z', 'zlib.h', 'C++','zlibVersion();'):
8422064SN/A    print 'Error: did not find needed zlib compression library '\
8432064SN/A          'and/or zlib.h header file.'
8442064SN/A    print '       Please install zlib and try again.'
8452091SN/A    Exit(1)
8462091SN/A
8472064SN/A# If we have the protobuf compiler, also make sure we have the
848360SN/A# development libraries. If the check passes, libprotobuf will be
8492064SN/A# automatically added to the LIBS environment variable. After
8502064SN/A# this, we can use the HAVE_PROTOBUF flag to determine if we have
8512064SN/A# got both protoc and libprotobuf available.
8522064SN/Amain['HAVE_PROTOBUF'] = main['PROTOC'] and \
8532064SN/A    conf.CheckLibWithHeader('protobuf', 'google/protobuf/message.h',
854360SN/A                            'C++', 'GOOGLE_PROTOBUF_VERIFY_VERSION;')
855360SN/A
8562680Sktlim@umich.edu# If we have the compiler but not the library, treat it as an error.
8571458SN/Aif main['PROTOC'] and not main['HAVE_PROTOBUF']:
858360SN/A    print 'Error: did not find protocol buffer library and/or headers.'
859360SN/A    print '       Please install libprotobuf-dev and try again.'
860378SN/A    Exit(1)
861360SN/A
8621450SN/A# Check for librt.
8633114Sgblack@eecs.umich.eduhave_posix_clock = \
8642680Sktlim@umich.edu    conf.CheckLibWithHeader(None, 'time.h', 'C',
865360SN/A                            'clock_nanosleep(0,0,NULL,NULL);') or \
8662680Sktlim@umich.edu    conf.CheckLibWithHeader('rt', 'time.h', 'C',
867360SN/A                            'clock_nanosleep(0,0,NULL,NULL);')
868360SN/A
869360SN/Aif conf.CheckLib('tcmalloc_minimal'):
8702091SN/A    have_tcmalloc = True
8712091SN/Aelse:
872360SN/A    have_tcmalloc = False
8732680Sktlim@umich.edu    print termcap.Yellow + termcap.Bold + \
874360SN/A          "You can get a 12% performance improvement by installing tcmalloc "\
8751458SN/A          "(libgoogle-perftools-dev package on Ubuntu or RedHat)." + \
876360SN/A          termcap.Normal
877360SN/A
878360SN/Aif not have_posix_clock:
8791999SN/A    print "Can't find library for POSIX clocks."
8801999SN/A
8811999SN/A# Check for <fenv.h> (C99 FP environment control)
8823114Sgblack@eecs.umich.eduhave_fenv = conf.CheckHeader('fenv.h', '<>')
8832680Sktlim@umich.eduif not have_fenv:
8841999SN/A    print "Warning: Header file <fenv.h> not found."
8851999SN/A    print "         This host has no IEEE FP rounding mode control."
8861999SN/A
8872680Sktlim@umich.edu######################################################################
8882400SN/A#
8891999SN/A# Finish the configuration
8902680Sktlim@umich.edu#
8912680Sktlim@umich.edumain = conf.Finish()
8921999SN/A
8931999SN/A######################################################################
8941999SN/A#
8951999SN/A# Collect all non-global variables
8962091SN/A#
8972091SN/A
8981999SN/A# Define the universe of supported ISAs
8991999SN/Aall_isa_list = [ ]
9001999SN/AExport('all_isa_list')
9011999SN/A
9021999SN/Aclass CpuModel(object):
9031999SN/A    '''The CpuModel class encapsulates everything the ISA parser needs to
9041999SN/A    know about a particular CPU model.'''
9051999SN/A
906378SN/A    # Dict of available CPU model objects.  Accessible as CpuModel.dict.
907360SN/A    dict = {}
9081450SN/A    list = []
9093114Sgblack@eecs.umich.edu    defaults = []
9102680Sktlim@umich.edu
911360SN/A    # Constructor.  Automatically adds models to CpuModel.dict.
9122680Sktlim@umich.edu    def __init__(self, name, filename, includes, strings, default=False):
9132680Sktlim@umich.edu        self.name = name           # name of model
914360SN/A        self.filename = filename   # filename for output exec code
9152553SN/A        self.includes = includes   # include files needed in exec file
916360SN/A        # The 'strings' dict holds all the per-CPU symbols we can
917360SN/A        # substitute into templates etc.
9181969SN/A        self.strings = strings
9191969SN/A
920360SN/A        # This cpu is enabled by default
921360SN/A        self.default = default
922360SN/A
9232091SN/A        # Add self to dict
9242091SN/A        if name in CpuModel.dict:
9252091SN/A            raise AttributeError, "CpuModel '%s' already registered" % name
926360SN/A        CpuModel.dict[name] = self
927360SN/A        CpuModel.list.append(name)
928360SN/A
929360SN/AExport('CpuModel')
930360SN/A
931360SN/A# Sticky variables get saved in the variables file so they persist from
932360SN/A# one invocation to the next (unless overridden, in which case the new
933360SN/A# value becomes sticky).
934360SN/Asticky_vars = Variables(args=ARGUMENTS)
935360SN/AExport('sticky_vars')
936360SN/A
937360SN/A# Sticky variables that should be exported
938360SN/Aexport_vars = []
939360SN/AExport('export_vars')
940360SN/A
941360SN/A# For Ruby
942360SN/Aall_protocols = []
9432680Sktlim@umich.eduExport('all_protocols')
944360SN/Aprotocol_dirs = []
9451458SN/AExport('protocol_dirs')
946360SN/Aslicc_includes = []
947360SN/AExport('slicc_includes')
9482553SN/A
9492553SN/A# Walk the tree and execute all SConsopts scripts that wil add to the
9502553SN/A# above variables
9511354SN/Aif not GetOption('verbose'):
952    print "Reading SConsopts"
953for bdir in [ base_dir ] + extras_dir_list:
954    if not isdir(bdir):
955        print "Error: directory '%s' does not exist" % bdir
956        Exit(1)
957    for root, dirs, files in os.walk(bdir):
958        if 'SConsopts' in files:
959            if GetOption('verbose'):
960                print "Reading", joinpath(root, 'SConsopts')
961            SConscript(joinpath(root, 'SConsopts'))
962
963all_isa_list.sort()
964
965sticky_vars.AddVariables(
966    EnumVariable('TARGET_ISA', 'Target ISA', 'alpha', all_isa_list),
967    ListVariable('CPU_MODELS', 'CPU models',
968                 sorted(n for n,m in CpuModel.dict.iteritems() if m.default),
969                 sorted(CpuModel.list)),
970    BoolVariable('EFENCE', 'Link with Electric Fence malloc debugger',
971                 False),
972    BoolVariable('SS_COMPATIBLE_FP',
973                 'Make floating-point results compatible with SimpleScalar',
974                 False),
975    BoolVariable('USE_SSE2',
976                 'Compile for SSE2 (-msse2) to get IEEE FP on x86 hosts',
977                 False),
978    BoolVariable('USE_POSIX_CLOCK', 'Use POSIX Clocks', have_posix_clock),
979    BoolVariable('USE_FENV', 'Use <fenv.h> IEEE mode control', have_fenv),
980    BoolVariable('CP_ANNOTATE', 'Enable critical path annotation capability', False),
981    EnumVariable('PROTOCOL', 'Coherence protocol for Ruby', 'None',
982                  all_protocols),
983    )
984
985# These variables get exported to #defines in config/*.hh (see src/SConscript).
986export_vars += ['USE_FENV', 'SS_COMPATIBLE_FP',
987                'TARGET_ISA', 'CP_ANNOTATE', 'USE_POSIX_CLOCK', 'PROTOCOL',
988                'HAVE_STATIC_ASSERT', 'HAVE_PROTOBUF']
989
990###################################################
991#
992# Define a SCons builder for configuration flag headers.
993#
994###################################################
995
996# This function generates a config header file that #defines the
997# variable symbol to the current variable setting (0 or 1).  The source
998# operands are the name of the variable and a Value node containing the
999# value of the variable.
1000def build_config_file(target, source, env):
1001    (variable, value) = [s.get_contents() for s in source]
1002    f = file(str(target[0]), 'w')
1003    print >> f, '#define', variable, value
1004    f.close()
1005    return None
1006
1007# Combine the two functions into a scons Action object.
1008config_action = MakeAction(build_config_file, Transform("CONFIG H", 2))
1009
1010# The emitter munges the source & target node lists to reflect what
1011# we're really doing.
1012def config_emitter(target, source, env):
1013    # extract variable name from Builder arg
1014    variable = str(target[0])
1015    # True target is config header file
1016    target = joinpath('config', variable.lower() + '.hh')
1017    val = env[variable]
1018    if isinstance(val, bool):
1019        # Force value to 0/1
1020        val = int(val)
1021    elif isinstance(val, str):
1022        val = '"' + val + '"'
1023
1024    # Sources are variable name & value (packaged in SCons Value nodes)
1025    return ([target], [Value(variable), Value(val)])
1026
1027config_builder = Builder(emitter = config_emitter, action = config_action)
1028
1029main.Append(BUILDERS = { 'ConfigFile' : config_builder })
1030
1031# libelf build is shared across all configs in the build root.
1032main.SConscript('ext/libelf/SConscript',
1033                variant_dir = joinpath(build_root, 'libelf'))
1034
1035# gzstream build is shared across all configs in the build root.
1036main.SConscript('ext/gzstream/SConscript',
1037                variant_dir = joinpath(build_root, 'gzstream'))
1038
1039###################################################
1040#
1041# This function is used to set up a directory with switching headers
1042#
1043###################################################
1044
1045main['ALL_ISA_LIST'] = all_isa_list
1046def make_switching_dir(dname, switch_headers, env):
1047    # Generate the header.  target[0] is the full path of the output
1048    # header to generate.  'source' is a dummy variable, since we get the
1049    # list of ISAs from env['ALL_ISA_LIST'].
1050    def gen_switch_hdr(target, source, env):
1051        fname = str(target[0])
1052        f = open(fname, 'w')
1053        isa = env['TARGET_ISA'].lower()
1054        print >>f, '#include "%s/%s/%s"' % (dname, isa, basename(fname))
1055        f.close()
1056
1057    # Build SCons Action object. 'varlist' specifies env vars that this
1058    # action depends on; when env['ALL_ISA_LIST'] changes these actions
1059    # should get re-executed.
1060    switch_hdr_action = MakeAction(gen_switch_hdr,
1061                          Transform("GENERATE"), varlist=['ALL_ISA_LIST'])
1062
1063    # Instantiate actions for each header
1064    for hdr in switch_headers:
1065        env.Command(hdr, [], switch_hdr_action)
1066Export('make_switching_dir')
1067
1068###################################################
1069#
1070# Define build environments for selected configurations.
1071#
1072###################################################
1073
1074for variant_path in variant_paths:
1075    print "Building in", variant_path
1076
1077    # Make a copy of the build-root environment to use for this config.
1078    env = main.Clone()
1079    env['BUILDDIR'] = variant_path
1080
1081    # variant_dir is the tail component of build path, and is used to
1082    # determine the build parameters (e.g., 'ALPHA_SE')
1083    (build_root, variant_dir) = splitpath(variant_path)
1084
1085    # Set env variables according to the build directory config.
1086    sticky_vars.files = []
1087    # Variables for $BUILD_ROOT/$VARIANT_DIR are stored in
1088    # $BUILD_ROOT/variables/$VARIANT_DIR so you can nuke
1089    # $BUILD_ROOT/$VARIANT_DIR without losing your variables settings.
1090    current_vars_file = joinpath(build_root, 'variables', variant_dir)
1091    if isfile(current_vars_file):
1092        sticky_vars.files.append(current_vars_file)
1093        print "Using saved variables file %s" % current_vars_file
1094    else:
1095        # Build dir-specific variables file doesn't exist.
1096
1097        # Make sure the directory is there so we can create it later
1098        opt_dir = dirname(current_vars_file)
1099        if not isdir(opt_dir):
1100            mkdir(opt_dir)
1101
1102        # Get default build variables from source tree.  Variables are
1103        # normally determined by name of $VARIANT_DIR, but can be
1104        # overridden by '--default=' arg on command line.
1105        default = GetOption('default')
1106        opts_dir = joinpath(main.root.abspath, 'build_opts')
1107        if default:
1108            default_vars_files = [joinpath(build_root, 'variables', default),
1109                                  joinpath(opts_dir, default)]
1110        else:
1111            default_vars_files = [joinpath(opts_dir, variant_dir)]
1112        existing_files = filter(isfile, default_vars_files)
1113        if existing_files:
1114            default_vars_file = existing_files[0]
1115            sticky_vars.files.append(default_vars_file)
1116            print "Variables file %s not found,\n  using defaults in %s" \
1117                  % (current_vars_file, default_vars_file)
1118        else:
1119            print "Error: cannot find variables file %s or " \
1120                  "default file(s) %s" \
1121                  % (current_vars_file, ' or '.join(default_vars_files))
1122            Exit(1)
1123
1124    # Apply current variable settings to env
1125    sticky_vars.Update(env)
1126
1127    help_texts["local_vars"] += \
1128        "Build variables for %s:\n" % variant_dir \
1129                 + sticky_vars.GenerateHelpText(env)
1130
1131    # Process variable settings.
1132
1133    if not have_fenv and env['USE_FENV']:
1134        print "Warning: <fenv.h> not available; " \
1135              "forcing USE_FENV to False in", variant_dir + "."
1136        env['USE_FENV'] = False
1137
1138    if not env['USE_FENV']:
1139        print "Warning: No IEEE FP rounding mode control in", variant_dir + "."
1140        print "         FP results may deviate slightly from other platforms."
1141
1142    if env['EFENCE']:
1143        env.Append(LIBS=['efence'])
1144
1145    # Save sticky variable settings back to current variables file
1146    sticky_vars.Save(current_vars_file, env)
1147
1148    if env['USE_SSE2']:
1149        env.Append(CCFLAGS=['-msse2'])
1150
1151    if have_tcmalloc:
1152        env.Append(LIBS=['tcmalloc_minimal'])
1153
1154    # The src/SConscript file sets up the build rules in 'env' according
1155    # to the configured variables.  It returns a list of environments,
1156    # one for each variant build (debug, opt, etc.)
1157    envList = SConscript('src/SConscript', variant_dir = variant_path,
1158                         exports = 'env')
1159
1160    # Set up the regression tests for each build.
1161    for e in envList:
1162        SConscript('tests/SConscript',
1163                   variant_dir = joinpath(variant_path, 'tests', e.Label),
1164                   exports = { 'env' : e }, duplicate = False)
1165
1166# base help text
1167Help('''
1168Usage: scons [scons options] [build variables] [target(s)]
1169
1170Extra scons options:
1171%(options)s
1172
1173Global build variables:
1174%(global_vars)s
1175
1176%(local_vars)s
1177''' % help_texts)
1178