SConstruct revision 6108
1955SN/A# -*- mode:python -*-
2955SN/A
35871Snate@binkert.org# Copyright (c) 2009 The Hewlett-Packard Development Company
41762SN/A# Copyright (c) 2004-2005 The Regents of The University of Michigan
5955SN/A# All rights reserved.
6955SN/A#
7955SN/A# Redistribution and use in source and binary forms, with or without
8955SN/A# modification, are permitted provided that the following conditions are
9955SN/A# met: redistributions of source code must retain the above copyright
10955SN/A# notice, this list of conditions and the following disclaimer;
11955SN/A# redistributions in binary form must reproduce the above copyright
12955SN/A# notice, this list of conditions and the following disclaimer in the
13955SN/A# documentation and/or other materials provided with the distribution;
14955SN/A# neither the name of the copyright holders nor the names of its
15955SN/A# contributors may be used to endorse or promote products derived from
16955SN/A# this software without specific prior written permission.
17955SN/A#
18955SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19955SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20955SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21955SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22955SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23955SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24955SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25955SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26955SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27955SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28955SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
292665Ssaidi@eecs.umich.edu#
302665Ssaidi@eecs.umich.edu# Authors: Steve Reinhardt
315863Snate@binkert.org#          Nathan Binkert
32955SN/A
33955SN/A###################################################
34955SN/A#
35955SN/A# SCons top-level build description (SConstruct) file.
36955SN/A#
372632Sstever@eecs.umich.edu# While in this directory ('m5'), just type 'scons' to build the default
382632Sstever@eecs.umich.edu# configuration (see below), or type 'scons build/<CONFIG>/<binary>'
392632Sstever@eecs.umich.edu# to build some other configuration (e.g., 'build/ALPHA_FS/m5.opt' for
402632Sstever@eecs.umich.edu# the optimized full-system version).
41955SN/A#
422632Sstever@eecs.umich.edu# You can build M5 in a different directory as long as there is a
432632Sstever@eecs.umich.edu# 'build/<CONFIG>' somewhere along the target path.  The build system
442761Sstever@eecs.umich.edu# expects that all configs under the same build directory are being
452632Sstever@eecs.umich.edu# built for the same host system.
462632Sstever@eecs.umich.edu#
472632Sstever@eecs.umich.edu# Examples:
482761Sstever@eecs.umich.edu#
492761Sstever@eecs.umich.edu#   The following two commands are equivalent.  The '-u' option tells
502761Sstever@eecs.umich.edu#   scons to search up the directory tree for this SConstruct file.
512632Sstever@eecs.umich.edu#   % cd <path-to-src>/m5 ; scons build/ALPHA_FS/m5.debug
522632Sstever@eecs.umich.edu#   % cd <path-to-src>/m5/build/ALPHA_FS; scons -u m5.debug
532761Sstever@eecs.umich.edu#
542761Sstever@eecs.umich.edu#   The following two commands are equivalent and demonstrate building
552761Sstever@eecs.umich.edu#   in a directory outside of the source tree.  The '-C' option tells
562761Sstever@eecs.umich.edu#   scons to chdir to the specified directory to find this SConstruct
572761Sstever@eecs.umich.edu#   file.
582632Sstever@eecs.umich.edu#   % cd <path-to-src>/m5 ; scons /local/foo/build/ALPHA_FS/m5.debug
592632Sstever@eecs.umich.edu#   % cd /local/foo/build/ALPHA_FS; scons -C <path-to-src>/m5 m5.debug
602632Sstever@eecs.umich.edu#
612632Sstever@eecs.umich.edu# You can use 'scons -H' to print scons options.  If you're in this
622632Sstever@eecs.umich.edu# 'm5' directory (or use -u or -C to tell scons where to find this
632632Sstever@eecs.umich.edu# file), you can use 'scons -h' to print all the M5-specific build
642632Sstever@eecs.umich.edu# options as well.
65955SN/A#
66955SN/A###################################################
67955SN/A
685863Snate@binkert.org# Check for recent-enough Python and SCons versions.
695863Snate@binkert.orgtry:
705863Snate@binkert.org    # Really old versions of scons only take two options for the
715863Snate@binkert.org    # function, so check once without the revision and once with the
725863Snate@binkert.org    # revision, the first instance will fail for stuff other than
735863Snate@binkert.org    # 0.98, and the second will fail for 0.98.0
745863Snate@binkert.org    EnsureSConsVersion(0, 98)
755863Snate@binkert.org    EnsureSConsVersion(0, 98, 1)
765863Snate@binkert.orgexcept SystemExit, e:
775863Snate@binkert.org    print """
785863Snate@binkert.orgFor more details, see:
795863Snate@binkert.org    http://m5sim.org/wiki/index.php/Compiling_M5
805863Snate@binkert.org"""
815863Snate@binkert.org    raise
825863Snate@binkert.org
835863Snate@binkert.org# We ensure the python version early because we have stuff that
845863Snate@binkert.org# requires python 2.4
855863Snate@binkert.orgtry:
865863Snate@binkert.org    EnsurePythonVersion(2, 4)
875863Snate@binkert.orgexcept SystemExit, e:
885863Snate@binkert.org    print """
895863Snate@binkert.orgYou can use a non-default installation of the Python interpreter by
905863Snate@binkert.orgeither (1) rearranging your PATH so that scons finds the non-default
915863Snate@binkert.org'python' first or (2) explicitly invoking an alternative interpreter
925863Snate@binkert.orgon the scons script.
935863Snate@binkert.org
945863Snate@binkert.orgFor more details, see:
955863Snate@binkert.org    http://m5sim.org/wiki/index.php/Using_a_non-default_Python_installation
965863Snate@binkert.org"""
975863Snate@binkert.org    raise
985863Snate@binkert.org
99955SN/Aimport os
1005396Ssaidi@eecs.umich.eduimport re
1015863Snate@binkert.orgimport subprocess
1025863Snate@binkert.orgimport sys
1034202Sbinkertn@umich.edu
1045863Snate@binkert.orgfrom os import mkdir, environ
1055863Snate@binkert.orgfrom os.path import abspath, basename, dirname, expanduser, normpath
1065863Snate@binkert.orgfrom os.path import exists,  isdir, isfile
1075863Snate@binkert.orgfrom os.path import join as joinpath, split as splitpath
108955SN/A
1095273Sstever@gmail.comimport SCons
1105871Snate@binkert.orgimport SCons.Node
1115273Sstever@gmail.com
1125871Snate@binkert.orgdef read_command(cmd, **kwargs):
1135863Snate@binkert.org    """run the command cmd, read the results and return them
1145863Snate@binkert.org    this is sorta like `cmd` in shell"""
1155863Snate@binkert.org    from subprocess import Popen, PIPE, STDOUT
1165871Snate@binkert.org
1175872Snate@binkert.org    if isinstance(cmd, str):
1185872Snate@binkert.org        cmd = cmd.split()
1195872Snate@binkert.org
1205871Snate@binkert.org    no_exception = 'exception' in kwargs
1215871Snate@binkert.org    exception = kwargs.pop('exception', None)
1225871Snate@binkert.org    
1235871Snate@binkert.org    kwargs.setdefault('shell', False)
1245871Snate@binkert.org    kwargs.setdefault('stdout', PIPE)
1255871Snate@binkert.org    kwargs.setdefault('stderr', STDOUT)
1265871Snate@binkert.org    kwargs.setdefault('close_fds', True)
1275871Snate@binkert.org    try:
1285871Snate@binkert.org        subp = Popen(cmd, **kwargs)
1295871Snate@binkert.org    except Exception, e:
1305871Snate@binkert.org        if no_exception:
1315871Snate@binkert.org            return exception
1325871Snate@binkert.org        raise
1335871Snate@binkert.org
1345863Snate@binkert.org    return subp.communicate()[0]
1355227Ssaidi@eecs.umich.edu
1365396Ssaidi@eecs.umich.edu# helper function: compare arrays or strings of version numbers.
1375396Ssaidi@eecs.umich.edu# E.g., compare_version((1,3,25), (1,4,1)')
1385396Ssaidi@eecs.umich.edu# returns -1, 0, 1 if v1 is <, ==, > v2
1395396Ssaidi@eecs.umich.edudef compare_versions(v1, v2):
1405396Ssaidi@eecs.umich.edu    def make_version_list(v):
1415396Ssaidi@eecs.umich.edu        if isinstance(v, (list,tuple)):
1425396Ssaidi@eecs.umich.edu            return v
1435396Ssaidi@eecs.umich.edu        elif isinstance(v, str):
1445588Ssaidi@eecs.umich.edu            return map(lambda x: int(re.match('\d+', x).group()), v.split('.'))
1455396Ssaidi@eecs.umich.edu        else:
1465396Ssaidi@eecs.umich.edu            raise TypeError
1475396Ssaidi@eecs.umich.edu
1485396Ssaidi@eecs.umich.edu    v1 = make_version_list(v1)
1495396Ssaidi@eecs.umich.edu    v2 = make_version_list(v2)
1505396Ssaidi@eecs.umich.edu    # Compare corresponding elements of lists
1515396Ssaidi@eecs.umich.edu    for n1,n2 in zip(v1, v2):
1525396Ssaidi@eecs.umich.edu        if n1 < n2: return -1
1535396Ssaidi@eecs.umich.edu        if n1 > n2: return  1
1545396Ssaidi@eecs.umich.edu    # all corresponding values are equal... see if one has extra values
1555396Ssaidi@eecs.umich.edu    if len(v1) < len(v2): return -1
1565396Ssaidi@eecs.umich.edu    if len(v1) > len(v2): return  1
1575396Ssaidi@eecs.umich.edu    return 0
1585396Ssaidi@eecs.umich.edu
1595871Snate@binkert.org########################################################################
1605871Snate@binkert.org#
1615871Snate@binkert.org# Set up the base build environment.
1625871Snate@binkert.org#
1635871Snate@binkert.org########################################################################
1646003Snate@binkert.orguse_vars = set([ 'AS', 'AR', 'CC', 'CXX', 'HOME', 'LD_LIBRARY_PATH', 'PATH',
1656003Snate@binkert.org                 'RANLIB' ])
166955SN/A
1675871Snate@binkert.orguse_env = {}
1685871Snate@binkert.orgfor key,val in os.environ.iteritems():
1695871Snate@binkert.org    if key in use_vars or key.startswith("M5"):
1705871Snate@binkert.org        use_env[key] = val
171955SN/A
1725871Snate@binkert.orgenv = Environment(ENV=use_env)
1735871Snate@binkert.orgenv.root = Dir(".")          # The current directory (where this file lives).
1745871Snate@binkert.orgenv.srcdir = Dir("src")      # The source directory
1751533SN/A
1765871Snate@binkert.org########################################################################
1775871Snate@binkert.org#
1785863Snate@binkert.org# Mercurial Stuff.
1795871Snate@binkert.org#
1805871Snate@binkert.org# If the M5 directory is a mercurial repository, we should do some
1815871Snate@binkert.org# extra things.
1825871Snate@binkert.org#
1835871Snate@binkert.org########################################################################
1845863Snate@binkert.org
1855871Snate@binkert.orghgdir = env.root.Dir(".hg")
1865863Snate@binkert.org
1875871Snate@binkert.orgmercurial_style_message = """
1884678Snate@binkert.orgYou're missing the M5 style hook.
1894678Snate@binkert.orgPlease install the hook so we can ensure that all code fits a common style.
1904678Snate@binkert.org
1914678Snate@binkert.orgAll you'd need to do is add the following lines to your repository .hg/hgrc
1924678Snate@binkert.orgor your personal .hgrc
1934678Snate@binkert.org----------------
1944678Snate@binkert.org
1954678Snate@binkert.org[extensions]
1964678Snate@binkert.orgstyle = %s/util/style.py
1974678Snate@binkert.org
1984678Snate@binkert.org[hooks]
1994678Snate@binkert.orgpretxncommit.style = python:style.check_whitespace
2005871Snate@binkert.org""" % (env.root)
2014678Snate@binkert.org
2025871Snate@binkert.orgmercurial_bin_not_found = """
2035871Snate@binkert.orgMercurial binary cannot be found, unfortunately this means that we
2045871Snate@binkert.orgcannot easily determine the version of M5 that you are running and
2055871Snate@binkert.orgthis makes error messages more difficult to collect.  Please consider
2065871Snate@binkert.orginstalling mercurial if you choose to post an error message
2075871Snate@binkert.org"""
2085871Snate@binkert.org
2095871Snate@binkert.orgmercurial_lib_not_found = """
2105871Snate@binkert.orgMercurial libraries cannot be found, ignoring style hook
2115871Snate@binkert.orgIf you are actually a M5 developer, please fix this and
2125871Snate@binkert.orgrun the style hook. It is important.
2135871Snate@binkert.org"""
2145871Snate@binkert.org
2155990Ssaidi@eecs.umich.eduhg_info = "Unknown"
2165871Snate@binkert.orgif hgdir.exists():
2175871Snate@binkert.org    # 1) Grab repository revision if we know it.
2185871Snate@binkert.org    cmd = "hg id -n -i -t -b"
2194678Snate@binkert.org    try:
2205871Snate@binkert.org        hg_info = read_command(cmd, cwd=env.root.abspath).strip()
2215871Snate@binkert.org    except OSError:
2225871Snate@binkert.org        print mercurial_bin_not_found
2235871Snate@binkert.org
2245871Snate@binkert.org    # 2) Ensure that the style hook is in place.
2255871Snate@binkert.org    try:
2265871Snate@binkert.org        ui = None
2275871Snate@binkert.org        if ARGUMENTS.get('IGNORE_STYLE') != 'True':
2285871Snate@binkert.org            from mercurial import ui
2295871Snate@binkert.org            ui = ui.ui()
2304678Snate@binkert.org    except ImportError:
2315871Snate@binkert.org        print mercurial_lib_not_found
2324678Snate@binkert.org
2335871Snate@binkert.org    if ui is not None:
2345871Snate@binkert.org        ui.readconfig(hgdir.File('hgrc').abspath)
2355871Snate@binkert.org        style_hook = ui.config('hooks', 'pretxncommit.style', None)
2365871Snate@binkert.org
2375871Snate@binkert.org        if not style_hook:
2385871Snate@binkert.org            print mercurial_style_message
2395871Snate@binkert.org            sys.exit(1)
2405871Snate@binkert.orgelse:
2415871Snate@binkert.org    print ".hg directory not found"
2425990Ssaidi@eecs.umich.eduenv['HG_INFO'] = hg_info
2435863Snate@binkert.org
244955SN/A###################################################
245955SN/A#
2462632Sstever@eecs.umich.edu# Figure out which configurations to set up based on the path(s) of
2472632Sstever@eecs.umich.edu# the target(s).
248955SN/A#
249955SN/A###################################################
250955SN/A
251955SN/A# Find default configuration & binary.
2525863Snate@binkert.orgDefault(environ.get('M5_DEFAULT_BINARY', 'build/ALPHA_SE/m5.debug'))
253955SN/A
2542632Sstever@eecs.umich.edu# helper function: find last occurrence of element in list
2552632Sstever@eecs.umich.edudef rfind(l, elt, offs = -1):
2562632Sstever@eecs.umich.edu    for i in range(len(l)+offs, 0, -1):
2572632Sstever@eecs.umich.edu        if l[i] == elt:
2582632Sstever@eecs.umich.edu            return i
2592632Sstever@eecs.umich.edu    raise ValueError, "element not found"
2602632Sstever@eecs.umich.edu
2612632Sstever@eecs.umich.edu# Each target must have 'build' in the interior of the path; the
2622632Sstever@eecs.umich.edu# directory below this will determine the build parameters.  For
2632632Sstever@eecs.umich.edu# example, for target 'foo/bar/build/ALPHA_SE/arch/alpha/blah.do' we
2642632Sstever@eecs.umich.edu# recognize that ALPHA_SE specifies the configuration because it
2652632Sstever@eecs.umich.edu# follow 'build' in the bulid path.
2662632Sstever@eecs.umich.edu
2673718Sstever@eecs.umich.edu# Generate absolute paths to targets so we can see where the build dir is
2683718Sstever@eecs.umich.eduif COMMAND_LINE_TARGETS:
2693718Sstever@eecs.umich.edu    # Ask SCons which directory it was invoked from
2703718Sstever@eecs.umich.edu    launch_dir = GetLaunchDir()
2713718Sstever@eecs.umich.edu    # Make targets relative to invocation directory
2725863Snate@binkert.org    abs_targets = [ normpath(joinpath(launch_dir, str(x))) for x in \
2735863Snate@binkert.org                    COMMAND_LINE_TARGETS]
2743718Sstever@eecs.umich.eduelse:
2753718Sstever@eecs.umich.edu    # Default targets are relative to root of tree
2765863Snate@binkert.org    abs_targets = [ normpath(joinpath(ROOT, str(x))) for x in \
2775863Snate@binkert.org                    DEFAULT_TARGETS]
2783718Sstever@eecs.umich.edu
2793718Sstever@eecs.umich.edu
2802634Sstever@eecs.umich.edu# Generate a list of the unique build roots and configs that the
2812634Sstever@eecs.umich.edu# collected targets reference.
2825863Snate@binkert.orgvariant_paths = []
2832638Sstever@eecs.umich.edubuild_root = None
2842632Sstever@eecs.umich.edufor t in abs_targets:
2852632Sstever@eecs.umich.edu    path_dirs = t.split('/')
2862632Sstever@eecs.umich.edu    try:
2872632Sstever@eecs.umich.edu        build_top = rfind(path_dirs, 'build', -2)
2882632Sstever@eecs.umich.edu    except:
2892632Sstever@eecs.umich.edu        print "Error: no non-leaf 'build' dir found on target path", t
2901858SN/A        Exit(1)
2913716Sstever@eecs.umich.edu    this_build_root = joinpath('/',*path_dirs[:build_top+1])
2922638Sstever@eecs.umich.edu    if not build_root:
2932638Sstever@eecs.umich.edu        build_root = this_build_root
2942638Sstever@eecs.umich.edu    else:
2952638Sstever@eecs.umich.edu        if this_build_root != build_root:
2962638Sstever@eecs.umich.edu            print "Error: build targets not under same build root\n"\
2972638Sstever@eecs.umich.edu                  "  %s\n  %s" % (build_root, this_build_root)
2982638Sstever@eecs.umich.edu            Exit(1)
2995863Snate@binkert.org    variant_path = joinpath('/',*path_dirs[:build_top+2])
3005863Snate@binkert.org    if variant_path not in variant_paths:
3015863Snate@binkert.org        variant_paths.append(variant_path)
302955SN/A
3035341Sstever@gmail.com# Make sure build_root exists (might not if this is the first build there)
3045341Sstever@gmail.comif not isdir(build_root):
3055863Snate@binkert.org    mkdir(build_root)
3065341Sstever@gmail.com
3074494Ssaidi@eecs.umich.eduExport('env')
3084494Ssaidi@eecs.umich.edu
3095863Snate@binkert.orgenv.SConsignFile(joinpath(build_root, "sconsign"))
3101105SN/A
3112667Sstever@eecs.umich.edu# Default duplicate option is to use hard links, but this messes up
3122667Sstever@eecs.umich.edu# when you use emacs to edit a file in the target dir, as emacs moves
3132667Sstever@eecs.umich.edu# file to file~ then copies to file, breaking the link.  Symbolic
3142667Sstever@eecs.umich.edu# (soft) links work better.
3152667Sstever@eecs.umich.eduenv.SetOption('duplicate', 'soft-copy')
3162667Sstever@eecs.umich.edu
3175341Sstever@gmail.com#
3185863Snate@binkert.org# Set up global sticky variables... these are common to an entire build
3195341Sstever@gmail.com# tree (not specific to a particular build like ALPHA_SE)
3205341Sstever@gmail.com#
3215341Sstever@gmail.com
3225863Snate@binkert.org# Variable validators & converters for global sticky variables
3235341Sstever@gmail.comdef PathListMakeAbsolute(val):
3245341Sstever@gmail.com    if not val:
3255341Sstever@gmail.com        return val
3265863Snate@binkert.org    f = lambda p: abspath(expanduser(p))
3275341Sstever@gmail.com    return ':'.join(map(f, val.split(':')))
3285341Sstever@gmail.com
3295341Sstever@gmail.comdef PathListAllExist(key, val, env):
3305341Sstever@gmail.com    if not val:
3315341Sstever@gmail.com        return
3325341Sstever@gmail.com    paths = val.split(':')
3335341Sstever@gmail.com    for path in paths:
3345341Sstever@gmail.com        if not isdir(path):
3355341Sstever@gmail.com            raise SCons.Errors.UserError("Path does not exist: '%s'" % path)
3365341Sstever@gmail.com
3375863Snate@binkert.orgglobal_sticky_vars_file = joinpath(build_root, 'variables.global')
3385341Sstever@gmail.com
3395863Snate@binkert.orgglobal_sticky_vars = Variables(global_sticky_vars_file, args=ARGUMENTS)
3405341Sstever@gmail.com
3415863Snate@binkert.orgglobal_sticky_vars.AddVariables(
3425863Snate@binkert.org    ('CC', 'C compiler', environ.get('CC', env['CC'])),
3435863Snate@binkert.org    ('CXX', 'C++ compiler', environ.get('CXX', env['CXX'])),
3445397Ssaidi@eecs.umich.edu    ('BATCH', 'Use batch pool for build and tests', False),
3455397Ssaidi@eecs.umich.edu    ('BATCH_CMD', 'Batch pool submission command name', 'qdo'),
3465341Sstever@gmail.com    ('EXTRAS', 'Add Extra directories to the compilation', '',
3475341Sstever@gmail.com     PathListAllExist, PathListMakeAbsolute)
3485341Sstever@gmail.com    )    
3495341Sstever@gmail.com
3505341Sstever@gmail.com# base help text
3515341Sstever@gmail.comhelp_text = '''
3525341Sstever@gmail.comUsage: scons [scons options] [build options] [target(s)]
3535341Sstever@gmail.com
3545863Snate@binkert.orgGlobal sticky options:
3555341Sstever@gmail.com'''
3565341Sstever@gmail.com
3575863Snate@binkert.orghelp_text += global_sticky_vars.GenerateHelpText(env)
3585341Sstever@gmail.com
3595863Snate@binkert.org# Update env with values from ARGUMENTS & file global_sticky_vars_file
3605863Snate@binkert.orgglobal_sticky_vars.Update(env)
3615341Sstever@gmail.com
3625863Snate@binkert.org# Save sticky variable settings back to current variables file
3635863Snate@binkert.orgglobal_sticky_vars.Save(global_sticky_vars_file, env)
3645341Sstever@gmail.com
3655863Snate@binkert.org# Parse EXTRAS variable to build list of all directories where we're
3665341Sstever@gmail.com# look for sources etc.  This list is exported as base_dir_list.
3675871Snate@binkert.orgbase_dir = env.srcdir.abspath
3685341Sstever@gmail.comif env['EXTRAS']:
3695742Snate@binkert.org    extras_dir_list = env['EXTRAS'].split(':')
3705742Snate@binkert.orgelse:
3715742Snate@binkert.org    extras_dir_list = []
3725341Sstever@gmail.com
3735742Snate@binkert.orgExport('base_dir')
3745742Snate@binkert.orgExport('extras_dir_list')
3755341Sstever@gmail.com
3766017Snate@binkert.org# the ext directory should be on the #includes path
3776017Snate@binkert.orgenv.Append(CPPPATH=[Dir('ext')])
3786017Snate@binkert.org
3792632Sstever@eecs.umich.edu# M5_PLY is used by isa_parser.py to find the PLY package.
3806016Snate@binkert.orgenv.Append(ENV = { 'M5_PLY' : Dir('ext/ply').abspath })
3815871Snate@binkert.org
3825871Snate@binkert.orgCXX_version = read_command([env['CXX'],'--version'], exception=False)
3835871Snate@binkert.orgCXX_V = read_command([env['CXX'],'-V'], exception=False)
3845871Snate@binkert.org
3855871Snate@binkert.orgenv['GCC'] = CXX_version and CXX_version.find('g++') >= 0
3865871Snate@binkert.orgenv['SUNCC'] = CXX_V and CXX_V.find('Sun C++') >= 0
3875871Snate@binkert.orgenv['ICC'] = CXX_V and CXX_V.find('Intel') >= 0
3883942Ssaidi@eecs.umich.eduif env['GCC'] + env['SUNCC'] + env['ICC'] > 1:
3893940Ssaidi@eecs.umich.edu    print 'Error: How can we have two at the same time?'
3903918Ssaidi@eecs.umich.edu    Exit(1)
3913918Ssaidi@eecs.umich.edu
3921858SN/A# Set up default C++ compiler flags
3933918Ssaidi@eecs.umich.eduif env['GCC']:
3943918Ssaidi@eecs.umich.edu    env.Append(CCFLAGS='-pipe')
3953918Ssaidi@eecs.umich.edu    env.Append(CCFLAGS='-fno-strict-aliasing')
3963918Ssaidi@eecs.umich.edu    env.Append(CCFLAGS=Split('-Wall -Wno-sign-compare -Werror -Wundef'))
3975571Snate@binkert.org    env.Append(CXXFLAGS='-Wno-deprecated')
3983940Ssaidi@eecs.umich.eduelif env['ICC']:
3993940Ssaidi@eecs.umich.edu    pass #Fix me... add warning flags once we clean up icc warnings
4003918Ssaidi@eecs.umich.eduelif env['SUNCC']:
4013918Ssaidi@eecs.umich.edu    env.Append(CCFLAGS='-Qoption ccfe')
4023918Ssaidi@eecs.umich.edu    env.Append(CCFLAGS='-features=gcc')
4033918Ssaidi@eecs.umich.edu    env.Append(CCFLAGS='-features=extensions')
4043918Ssaidi@eecs.umich.edu    env.Append(CCFLAGS='-library=stlport4')
4053918Ssaidi@eecs.umich.edu    env.Append(CCFLAGS='-xar')
4065871Snate@binkert.org    #env.Append(CCFLAGS='-instances=semiexplicit')
4073918Ssaidi@eecs.umich.eduelse:
4083918Ssaidi@eecs.umich.edu    print 'Error: Don\'t know what compiler options to use for your compiler.'
4093940Ssaidi@eecs.umich.edu    print '       Please fix SConstruct and src/SConscript and try again.'
4103918Ssaidi@eecs.umich.edu    Exit(1)
4113918Ssaidi@eecs.umich.edu
4125397Ssaidi@eecs.umich.edu# Do this after we save setting back, or else we'll tack on an
4135397Ssaidi@eecs.umich.edu# extra 'qdo' every time we run scons.
4145397Ssaidi@eecs.umich.eduif env['BATCH']:
4155708Ssaidi@eecs.umich.edu    env['CC']     = env['BATCH_CMD'] + ' ' + env['CC']
4165708Ssaidi@eecs.umich.edu    env['CXX']    = env['BATCH_CMD'] + ' ' + env['CXX']
4175708Ssaidi@eecs.umich.edu    env['AS']     = env['BATCH_CMD'] + ' ' + env['AS']
4185708Ssaidi@eecs.umich.edu    env['AR']     = env['BATCH_CMD'] + ' ' + env['AR']
4195708Ssaidi@eecs.umich.edu    env['RANLIB'] = env['BATCH_CMD'] + ' ' + env['RANLIB']
4205397Ssaidi@eecs.umich.edu
4211851SN/Aif sys.platform == 'cygwin':
4221851SN/A    # cygwin has some header file issues...
4231858SN/A    env.Append(CCFLAGS=Split("-Wno-uninitialized"))
424955SN/A
4253053Sstever@eecs.umich.edu# Check for SWIG
4263053Sstever@eecs.umich.eduif not env.has_key('SWIG'):
4273053Sstever@eecs.umich.edu    print 'Error: SWIG utility not found.'
4283053Sstever@eecs.umich.edu    print '       Please install (see http://www.swig.org) and retry.'
4293053Sstever@eecs.umich.edu    Exit(1)
4303053Sstever@eecs.umich.edu
4313053Sstever@eecs.umich.edu# Check for appropriate SWIG version
4325871Snate@binkert.orgswig_version = read_command(('swig', '-version'), exception='').split()
4333053Sstever@eecs.umich.edu# First 3 words should be "SWIG Version x.y.z"
4344742Sstever@eecs.umich.eduif len(swig_version) < 3 or \
4354742Sstever@eecs.umich.edu        swig_version[0] != 'SWIG' or swig_version[1] != 'Version':
4363053Sstever@eecs.umich.edu    print 'Error determining SWIG version.'
4373053Sstever@eecs.umich.edu    Exit(1)
4383053Sstever@eecs.umich.edu
4393053Sstever@eecs.umich.edumin_swig_version = '1.3.28'
4403053Sstever@eecs.umich.eduif compare_versions(swig_version[2], min_swig_version) < 0:
4413053Sstever@eecs.umich.edu    print 'Error: SWIG version', min_swig_version, 'or newer required.'
4423053Sstever@eecs.umich.edu    print '       Installed version:', swig_version[2]
4433053Sstever@eecs.umich.edu    Exit(1)
4443053Sstever@eecs.umich.edu
4452667Sstever@eecs.umich.edu# Set up SWIG flags & scanner
4464554Sbinkertn@umich.eduswig_flags=Split('-c++ -python -modern -templatereduce $_CPPINCFLAGS')
4474554Sbinkertn@umich.eduenv.Append(SWIGFLAGS=swig_flags)
4482667Sstever@eecs.umich.edu
4494554Sbinkertn@umich.edu# filter out all existing swig scanners, they mess up the dependency
4504554Sbinkertn@umich.edu# stuff for some reason
4514554Sbinkertn@umich.eduscanners = []
4524554Sbinkertn@umich.edufor scanner in env['SCANNERS']:
4534554Sbinkertn@umich.edu    skeys = scanner.skeys
4544554Sbinkertn@umich.edu    if skeys == '.i':
4554554Sbinkertn@umich.edu        continue
4564781Snate@binkert.org
4574554Sbinkertn@umich.edu    if isinstance(skeys, (list, tuple)) and '.i' in skeys:
4584554Sbinkertn@umich.edu        continue
4592667Sstever@eecs.umich.edu
4604554Sbinkertn@umich.edu    scanners.append(scanner)
4614554Sbinkertn@umich.edu
4624554Sbinkertn@umich.edu# add the new swig scanner that we like better
4634554Sbinkertn@umich.edufrom SCons.Scanner import ClassicCPP as CPPScanner
4642667Sstever@eecs.umich.eduswig_inc_re = '^[ \t]*[%,#][ \t]*(?:include|import)[ \t]*(<|")([^>"]+)(>|")'
4654554Sbinkertn@umich.eduscanners.append(CPPScanner("SwigScan", [ ".i" ], "CPPPATH", swig_inc_re))
4662667Sstever@eecs.umich.edu
4674554Sbinkertn@umich.edu# replace the scanners list that has what we want
4684554Sbinkertn@umich.eduenv['SCANNERS'] = scanners
4692667Sstever@eecs.umich.edu
4705522Snate@binkert.org# Add a custom Check function to the Configure context so that we can
4715522Snate@binkert.org# figure out if the compiler adds leading underscores to global
4725522Snate@binkert.org# variables.  This is needed for the autogenerated asm files that we
4735522Snate@binkert.org# use for embedding the python code.
4745522Snate@binkert.orgdef CheckLeading(context):
4755522Snate@binkert.org    context.Message("Checking for leading underscore in global variables...")
4765522Snate@binkert.org    # 1) Define a global variable called x from asm so the C compiler
4775522Snate@binkert.org    #    won't change the symbol at all.
4785522Snate@binkert.org    # 2) Declare that variable.
4795522Snate@binkert.org    # 3) Use the variable
4805522Snate@binkert.org    #
4815522Snate@binkert.org    # If the compiler prepends an underscore, this will successfully
4825522Snate@binkert.org    # link because the external symbol 'x' will be called '_x' which
4835522Snate@binkert.org    # was defined by the asm statement.  If the compiler does not
4845522Snate@binkert.org    # prepend an underscore, this will not successfully link because
4855522Snate@binkert.org    # '_x' will have been defined by assembly, while the C portion of
4865522Snate@binkert.org    # the code will be trying to use 'x'
4875522Snate@binkert.org    ret = context.TryLink('''
4885522Snate@binkert.org        asm(".globl _x; _x: .byte 0");
4895522Snate@binkert.org        extern int x;
4905522Snate@binkert.org        int main() { return x; }
4915522Snate@binkert.org        ''', extension=".c")
4925522Snate@binkert.org    context.env.Append(LEADING_UNDERSCORE=ret)
4935522Snate@binkert.org    context.Result(ret)
4945522Snate@binkert.org    return ret
4955522Snate@binkert.org
4962638Sstever@eecs.umich.edu# Platform-specific configuration.  Note again that we assume that all
4972638Sstever@eecs.umich.edu# builds under a given build root run on the same host platform.
4982638Sstever@eecs.umich.educonf = Configure(env,
4993716Sstever@eecs.umich.edu                 conf_dir = joinpath(build_root, '.scons_config'),
5005522Snate@binkert.org                 log_file = joinpath(build_root, 'scons_config.log'),
5015522Snate@binkert.org                 custom_tests = { 'CheckLeading' : CheckLeading })
5025522Snate@binkert.org
5035522Snate@binkert.org# Check for leading underscores.  Don't really need to worry either
5045522Snate@binkert.org# way so don't need to check the return code.
5055522Snate@binkert.orgconf.CheckLeading()
5061858SN/A
5075227Ssaidi@eecs.umich.edu# Check if we should compile a 64 bit binary on Mac OS X/Darwin
5085227Ssaidi@eecs.umich.edutry:
5095227Ssaidi@eecs.umich.edu    import platform
5105227Ssaidi@eecs.umich.edu    uname = platform.uname()
5115227Ssaidi@eecs.umich.edu    if uname[0] == 'Darwin' and compare_versions(uname[2], '9.0.0') >= 0:
5125863Snate@binkert.org        if int(read_command('sysctl -n hw.cpu64bit_capable')[0]):
5135227Ssaidi@eecs.umich.edu            env.Append(CCFLAGS='-arch x86_64')
5145227Ssaidi@eecs.umich.edu            env.Append(CFLAGS='-arch x86_64')
5155227Ssaidi@eecs.umich.edu            env.Append(LINKFLAGS='-arch x86_64')
5165227Ssaidi@eecs.umich.edu            env.Append(ASFLAGS='-arch x86_64')
5175227Ssaidi@eecs.umich.eduexcept:
5185227Ssaidi@eecs.umich.edu    pass
5195227Ssaidi@eecs.umich.edu
5205204Sstever@gmail.com# Recent versions of scons substitute a "Null" object for Configure()
5215204Sstever@gmail.com# when configuration isn't necessary, e.g., if the "--help" option is
5225204Sstever@gmail.com# present.  Unfortuantely this Null object always returns false,
5235204Sstever@gmail.com# breaking all our configuration checks.  We replace it with our own
5245204Sstever@gmail.com# more optimistic null object that returns True instead.
5255204Sstever@gmail.comif not conf:
5265204Sstever@gmail.com    def NullCheck(*args, **kwargs):
5275204Sstever@gmail.com        return True
5285204Sstever@gmail.com
5295204Sstever@gmail.com    class NullConf:
5305204Sstever@gmail.com        def __init__(self, env):
5315204Sstever@gmail.com            self.env = env
5325204Sstever@gmail.com        def Finish(self):
5335204Sstever@gmail.com            return self.env
5345204Sstever@gmail.com        def __getattr__(self, mname):
5355204Sstever@gmail.com            return NullCheck
5365204Sstever@gmail.com
5375204Sstever@gmail.com    conf = NullConf(env)
5385204Sstever@gmail.com
5393118Sstever@eecs.umich.edu# Find Python include and library directories for embedding the
5403118Sstever@eecs.umich.edu# interpreter.  For consistency, we will use the same Python
5413118Sstever@eecs.umich.edu# installation used to run scons (and thus this script).  If you want
5423118Sstever@eecs.umich.edu# to link in an alternate version, see above for instructions on how
5433118Sstever@eecs.umich.edu# to invoke scons with a different copy of the Python interpreter.
5445863Snate@binkert.orgfrom distutils import sysconfig
5453118Sstever@eecs.umich.edu
5465863Snate@binkert.orgpy_getvar = sysconfig.get_config_var
5473118Sstever@eecs.umich.edu
5485863Snate@binkert.orgpy_version = 'python' + py_getvar('VERSION')
5495863Snate@binkert.org
5505863Snate@binkert.orgpy_general_include = sysconfig.get_python_inc()
5515863Snate@binkert.orgpy_platform_include = sysconfig.get_python_inc(plat_specific=True)
5525863Snate@binkert.orgpy_includes = [ py_general_include ]
5535863Snate@binkert.orgif py_platform_include != py_general_include:
5545863Snate@binkert.org    py_includes.append(py_platform_include)
5555863Snate@binkert.org
5566003Snate@binkert.orgpy_lib_path = [ py_getvar('LIBDIR') ]
5575863Snate@binkert.org# add the prefix/lib/pythonX.Y/config dir, but only if there is no
5585863Snate@binkert.org# shared library in prefix/lib/.
5595863Snate@binkert.orgif not py_getvar('Py_ENABLE_SHARED'):
5605863Snate@binkert.org    py_lib_path.append('-L' + py_getvar('LIBPL'))
5615863Snate@binkert.org
5625863Snate@binkert.orgpy_libs = []
5635863Snate@binkert.orgfor lib in py_getvar('LIBS').split() + py_getvar('SYSLIBS').split():
5645863Snate@binkert.org    if lib not in py_libs:
5655863Snate@binkert.org        py_libs.append(lib)
5665863Snate@binkert.orgpy_libs.append('-l' + py_version)
5675863Snate@binkert.org
5685863Snate@binkert.orgenv.Append(CPPPATH=py_includes)
5695863Snate@binkert.orgenv.Append(LIBPATH=py_lib_path)
5705863Snate@binkert.org
5715863Snate@binkert.org# verify that this stuff works
5723118Sstever@eecs.umich.eduif not conf.CheckHeader('Python.h', '<>'):
5735863Snate@binkert.org    print "Error: can't find Python.h header in", py_includes
5743118Sstever@eecs.umich.edu    Exit(1)
5753118Sstever@eecs.umich.edu
5765863Snate@binkert.orgfor lib in py_libs:
5775863Snate@binkert.org    assert lib.startswith('-l')
5785863Snate@binkert.org    lib = lib[2:]
5795863Snate@binkert.org    if not conf.CheckLib(lib):
5805863Snate@binkert.org        print "Error: can't find library %s required by python" % lib
5815863Snate@binkert.org        Exit(1)
5823118Sstever@eecs.umich.edu
5833483Ssaidi@eecs.umich.edu# On Solaris you need to use libsocket for socket ops
5843494Ssaidi@eecs.umich.eduif not conf.CheckLibWithHeader(None, 'sys/socket.h', 'C++', 'accept(0,0,0);'):
5853494Ssaidi@eecs.umich.edu   if not conf.CheckLibWithHeader('socket', 'sys/socket.h', 'C++', 'accept(0,0,0);'):
5863483Ssaidi@eecs.umich.edu       print "Can't find library with socket calls (e.g. accept())"
5873483Ssaidi@eecs.umich.edu       Exit(1)
5883483Ssaidi@eecs.umich.edu
5893053Sstever@eecs.umich.edu# Check for zlib.  If the check passes, libz will be automatically
5903053Sstever@eecs.umich.edu# added to the LIBS environment variable.
5913918Ssaidi@eecs.umich.eduif not conf.CheckLibWithHeader('z', 'zlib.h', 'C++','zlibVersion();'):
5923053Sstever@eecs.umich.edu    print 'Error: did not find needed zlib compression library '\
5933053Sstever@eecs.umich.edu          'and/or zlib.h header file.'
5943053Sstever@eecs.umich.edu    print '       Please install zlib and try again.'
5953053Sstever@eecs.umich.edu    Exit(1)
5963053Sstever@eecs.umich.edu
5971858SN/A# Check for <fenv.h> (C99 FP environment control)
5981858SN/Ahave_fenv = conf.CheckHeader('fenv.h', '<>')
5991858SN/Aif not have_fenv:
6001858SN/A    print "Warning: Header file <fenv.h> not found."
6011858SN/A    print "         This host has no IEEE FP rounding mode control."
6021858SN/A
6035863Snate@binkert.org######################################################################
6045863Snate@binkert.org#
6051859SN/A# Check for mysql.
6065863Snate@binkert.org#
6071858SN/Amysql_config = WhereIs('mysql_config')
6085863Snate@binkert.orghave_mysql = bool(mysql_config)
6091858SN/A
6101859SN/A# Check MySQL version.
6111859SN/Aif have_mysql:
6125863Snate@binkert.org    mysql_version = read_command(mysql_config + ' --version')
6133053Sstever@eecs.umich.edu    min_mysql_version = '4.1'
6143053Sstever@eecs.umich.edu    if compare_versions(mysql_version, min_mysql_version) < 0:
6153053Sstever@eecs.umich.edu        print 'Warning: MySQL', min_mysql_version, 'or newer required.'
6163053Sstever@eecs.umich.edu        print '         Version', mysql_version, 'detected.'
6171859SN/A        have_mysql = False
6181859SN/A
6191859SN/A# Set up mysql_config commands.
6201859SN/Aif have_mysql:
6211859SN/A    mysql_config_include = mysql_config + ' --include'
6221859SN/A    if os.system(mysql_config_include + ' > /dev/null') != 0:
6231859SN/A        # older mysql_config versions don't support --include, use
6241859SN/A        # --cflags instead
6251862SN/A        mysql_config_include = mysql_config + ' --cflags | sed s/\\\'//g'
6261859SN/A    # This seems to work in all versions
6271859SN/A    mysql_config_libs = mysql_config + ' --libs'
6281859SN/A
6295863Snate@binkert.org######################################################################
6305863Snate@binkert.org#
6315863Snate@binkert.org# Finish the configuration
6325863Snate@binkert.org#
6331858SN/Aenv = conf.Finish()
6341858SN/A
6355863Snate@binkert.org######################################################################
6365863Snate@binkert.org#
6375863Snate@binkert.org# Collect all non-global variables
6385863Snate@binkert.org#
6395863Snate@binkert.org
6405871Snate@binkert.orgExport('env')
6415871Snate@binkert.org
6422139SN/A# Define the universe of supported ISAs
6434202Sbinkertn@umich.eduall_isa_list = [ ]
6444202Sbinkertn@umich.eduExport('all_isa_list')
6452139SN/A
6462155SN/A# Define the universe of supported CPU models
6474202Sbinkertn@umich.eduall_cpu_list = [ ]
6484202Sbinkertn@umich.edudefault_cpus = [ ]
6494202Sbinkertn@umich.eduExport('all_cpu_list', 'default_cpus')
6502155SN/A
6515863Snate@binkert.org# Sticky variables get saved in the variables file so they persist from
6521869SN/A# one invocation to the next (unless overridden, in which case the new
6531869SN/A# value becomes sticky).
6545863Snate@binkert.orgsticky_vars = Variables(args=ARGUMENTS)
6555863Snate@binkert.orgExport('sticky_vars')
6564202Sbinkertn@umich.edu
6576108Snate@binkert.org# Sticky variables that should be exported
6586108Snate@binkert.orgexport_vars = []
6596108Snate@binkert.orgExport('export_vars')
6606108Snate@binkert.org
6615863Snate@binkert.org# Non-sticky variables only apply to the current build.
6625863Snate@binkert.orgnonsticky_vars = Variables(args=ARGUMENTS)
6635863Snate@binkert.orgExport('nonsticky_vars')
6644202Sbinkertn@umich.edu
6654202Sbinkertn@umich.edu# Walk the tree and execute all SConsopts scripts that wil add to the
6665863Snate@binkert.org# above variables
6675742Snate@binkert.orgfor bdir in [ base_dir ] + extras_dir_list:
6685742Snate@binkert.org    for root, dirs, files in os.walk(bdir):
6695341Sstever@gmail.com        if 'SConsopts' in files:
6705342Sstever@gmail.com            print "Reading", joinpath(root, 'SConsopts')
6715342Sstever@gmail.com            SConscript(joinpath(root, 'SConsopts'))
6724202Sbinkertn@umich.edu
6734202Sbinkertn@umich.eduall_isa_list.sort()
6744202Sbinkertn@umich.eduall_cpu_list.sort()
6754202Sbinkertn@umich.edudefault_cpus.sort()
6764202Sbinkertn@umich.edu
6775863Snate@binkert.orgsticky_vars.AddVariables(
6785863Snate@binkert.org    EnumVariable('TARGET_ISA', 'Target ISA', 'alpha', all_isa_list),
6795863Snate@binkert.org    BoolVariable('FULL_SYSTEM', 'Full-system support', False),
6805863Snate@binkert.org    ListVariable('CPU_MODELS', 'CPU models', default_cpus, all_cpu_list),
6815863Snate@binkert.org    BoolVariable('NO_FAST_ALLOC', 'Disable fast object allocator', False),
6825863Snate@binkert.org    BoolVariable('FAST_ALLOC_DEBUG', 'Enable fast object allocator debugging',
6835863Snate@binkert.org                 False),
6845863Snate@binkert.org    BoolVariable('FAST_ALLOC_STATS', 'Enable fast object allocator statistics',
6855863Snate@binkert.org                 False),
6865863Snate@binkert.org    BoolVariable('EFENCE', 'Link with Electric Fence malloc debugger',
6875863Snate@binkert.org                 False),
6885863Snate@binkert.org    BoolVariable('SS_COMPATIBLE_FP',
6895863Snate@binkert.org                 'Make floating-point results compatible with SimpleScalar',
6905863Snate@binkert.org                 False),
6915863Snate@binkert.org    BoolVariable('USE_SSE2',
6925863Snate@binkert.org                 'Compile for SSE2 (-msse2) to get IEEE FP on x86 hosts',
6935863Snate@binkert.org                 False),
6945863Snate@binkert.org    BoolVariable('USE_MYSQL', 'Use MySQL for stats output', have_mysql),
6955863Snate@binkert.org    BoolVariable('USE_FENV', 'Use <fenv.h> IEEE mode control', have_fenv),
6965863Snate@binkert.org    BoolVariable('USE_CHECKER', 'Use checker for detailed CPU models', False),
6975952Ssaidi@eecs.umich.edu    BoolVariable('CP_ANNOTATE', 'Enable critical path annotation capability', False),
6981869SN/A    )
6991858SN/A
7005863Snate@binkert.orgnonsticky_vars.AddVariables(
7015863Snate@binkert.org    BoolVariable('update_ref', 'Update test reference outputs', False)
7021869SN/A    )
7031858SN/A
7045863Snate@binkert.org# These variables get exported to #defines in config/*.hh (see src/SConscript).
7056108Snate@binkert.orgexport_vars += ['FULL_SYSTEM', 'USE_FENV', 'USE_MYSQL',
7066108Snate@binkert.org                'NO_FAST_ALLOC', 'FAST_ALLOC_DEBUG', 'FAST_ALLOC_STATS',
7076108Snate@binkert.org                'SS_COMPATIBLE_FP', 'USE_CHECKER', 'TARGET_ISA', 'CP_ANNOTATE']
7081858SN/A
709955SN/A###################################################
710955SN/A#
7111869SN/A# Define a SCons builder for configuration flag headers.
7121869SN/A#
7131869SN/A###################################################
7141869SN/A
7151869SN/A# This function generates a config header file that #defines the
7165863Snate@binkert.org# variable symbol to the current variable setting (0 or 1).  The source
7175863Snate@binkert.org# operands are the name of the variable and a Value node containing the
7185863Snate@binkert.org# value of the variable.
7191869SN/Adef build_config_file(target, source, env):
7205863Snate@binkert.org    (variable, value) = [s.get_contents() for s in source]
7211869SN/A    f = file(str(target[0]), 'w')
7225863Snate@binkert.org    print >> f, '#define', variable, value
7231869SN/A    f.close()
7241869SN/A    return None
7251869SN/A
7261869SN/A# Generate the message to be printed when building the config file.
7271869SN/Adef build_config_file_string(target, source, env):
7285863Snate@binkert.org    (variable, value) = [s.get_contents() for s in source]
7295863Snate@binkert.org    return "Defining %s as %s in %s." % (variable, value, target[0])
7301869SN/A
7311869SN/A# Combine the two functions into a scons Action object.
7321869SN/Aconfig_action = Action(build_config_file, build_config_file_string)
7331869SN/A
7341869SN/A# The emitter munges the source & target node lists to reflect what
7351869SN/A# we're really doing.
7361869SN/Adef config_emitter(target, source, env):
7375863Snate@binkert.org    # extract variable name from Builder arg
7385863Snate@binkert.org    variable = str(target[0])
7391869SN/A    # True target is config header file
7405863Snate@binkert.org    target = joinpath('config', variable.lower() + '.hh')
7415863Snate@binkert.org    val = env[variable]
7423356Sbinkertn@umich.edu    if isinstance(val, bool):
7433356Sbinkertn@umich.edu        # Force value to 0/1
7443356Sbinkertn@umich.edu        val = int(val)
7453356Sbinkertn@umich.edu    elif isinstance(val, str):
7463356Sbinkertn@umich.edu        val = '"' + val + '"'
7474781Snate@binkert.org
7485863Snate@binkert.org    # Sources are variable name & value (packaged in SCons Value nodes)
7495863Snate@binkert.org    return ([target], [Value(variable), Value(val)])
7501869SN/A
7511869SN/Aconfig_builder = Builder(emitter = config_emitter, action = config_action)
7521869SN/A
7531869SN/Aenv.Append(BUILDERS = { 'ConfigFile' : config_builder })
7541869SN/A
7552638Sstever@eecs.umich.edu# libelf build is shared across all configs in the build root.
7562638Sstever@eecs.umich.eduenv.SConscript('ext/libelf/SConscript',
7575871Snate@binkert.org               variant_dir = joinpath(build_root, 'libelf'))
7582638Sstever@eecs.umich.edu
7595749Scws3k@cs.virginia.edu# gzstream build is shared across all configs in the build root.
7605749Scws3k@cs.virginia.eduenv.SConscript('ext/gzstream/SConscript',
7615871Snate@binkert.org               variant_dir = joinpath(build_root, 'gzstream'))
7625749Scws3k@cs.virginia.edu
7631869SN/A###################################################
7641869SN/A#
7653546Sgblack@eecs.umich.edu# This function is used to set up a directory with switching headers
7663546Sgblack@eecs.umich.edu#
7673546Sgblack@eecs.umich.edu###################################################
7683546Sgblack@eecs.umich.edu
7694202Sbinkertn@umich.eduenv['ALL_ISA_LIST'] = all_isa_list
7705863Snate@binkert.orgdef make_switching_dir(dname, switch_headers, env):
7713546Sgblack@eecs.umich.edu    # Generate the header.  target[0] is the full path of the output
7723546Sgblack@eecs.umich.edu    # header to generate.  'source' is a dummy variable, since we get the
7733546Sgblack@eecs.umich.edu    # list of ISAs from env['ALL_ISA_LIST'].
7743546Sgblack@eecs.umich.edu    def gen_switch_hdr(target, source, env):
7754781Snate@binkert.org        fname = str(target[0])
7765863Snate@binkert.org        bname = basename(fname)
7774781Snate@binkert.org        f = open(fname, 'w')
7784781Snate@binkert.org        f.write('#include "arch/isa_specific.hh"\n')
7794781Snate@binkert.org        cond = '#if'
7804781Snate@binkert.org        for isa in all_isa_list:
7814781Snate@binkert.org            f.write('%s THE_ISA == %s_ISA\n#include "%s/%s/%s"\n'
7825863Snate@binkert.org                    % (cond, isa.upper(), dname, isa, bname))
7834781Snate@binkert.org            cond = '#elif'
7844781Snate@binkert.org        f.write('#else\n#error "THE_ISA not set"\n#endif\n')
7854781Snate@binkert.org        f.close()
7864781Snate@binkert.org        return 0
7873546Sgblack@eecs.umich.edu
7883546Sgblack@eecs.umich.edu    # String to print when generating header
7893546Sgblack@eecs.umich.edu    def gen_switch_hdr_string(target, source, env):
7904781Snate@binkert.org        return "Generating switch header " + str(target[0])
7913546Sgblack@eecs.umich.edu
7923546Sgblack@eecs.umich.edu    # Build SCons Action object. 'varlist' specifies env vars that this
7933546Sgblack@eecs.umich.edu    # action depends on; when env['ALL_ISA_LIST'] changes these actions
7943546Sgblack@eecs.umich.edu    # should get re-executed.
7953546Sgblack@eecs.umich.edu    switch_hdr_action = Action(gen_switch_hdr, gen_switch_hdr_string,
7963546Sgblack@eecs.umich.edu                               varlist=['ALL_ISA_LIST'])
7973546Sgblack@eecs.umich.edu
7983546Sgblack@eecs.umich.edu    # Instantiate actions for each header
7993546Sgblack@eecs.umich.edu    for hdr in switch_headers:
8003546Sgblack@eecs.umich.edu        env.Command(hdr, [], switch_hdr_action)
8014202Sbinkertn@umich.eduExport('make_switching_dir')
8023546Sgblack@eecs.umich.edu
8033546Sgblack@eecs.umich.edu###################################################
8043546Sgblack@eecs.umich.edu#
805955SN/A# Define build environments for selected configurations.
806955SN/A#
807955SN/A###################################################
808955SN/A
8091858SN/A# rename base env
8101858SN/Abase_env = env
8111858SN/A
8125863Snate@binkert.orgfor variant_path in variant_paths:
8135863Snate@binkert.org    print "Building in", variant_path
8145343Sstever@gmail.com
8155343Sstever@gmail.com    # Make a copy of the build-root environment to use for this config.
8165863Snate@binkert.org    env = base_env.Clone()
8175863Snate@binkert.org    env['BUILDDIR'] = variant_path
8184773Snate@binkert.org
8195863Snate@binkert.org    # variant_dir is the tail component of build path, and is used to
8202632Sstever@eecs.umich.edu    # determine the build parameters (e.g., 'ALPHA_SE')
8215863Snate@binkert.org    (build_root, variant_dir) = splitpath(variant_path)
8222023SN/A
8235863Snate@binkert.org    # Set env variables according to the build directory config.
8245863Snate@binkert.org    sticky_vars.files = []
8255863Snate@binkert.org    # Variables for $BUILD_ROOT/$VARIANT_DIR are stored in
8265863Snate@binkert.org    # $BUILD_ROOT/variables/$VARIANT_DIR so you can nuke
8275863Snate@binkert.org    # $BUILD_ROOT/$VARIANT_DIR without losing your variables settings.
8285863Snate@binkert.org    current_vars_file = joinpath(build_root, 'variables', variant_dir)
8295863Snate@binkert.org    if isfile(current_vars_file):
8305863Snate@binkert.org        sticky_vars.files.append(current_vars_file)
8315863Snate@binkert.org        print "Using saved variables file %s" % current_vars_file
8322632Sstever@eecs.umich.edu    else:
8335863Snate@binkert.org        # Build dir-specific variables file doesn't exist.
8342023SN/A
8352632Sstever@eecs.umich.edu        # Make sure the directory is there so we can create it later
8365863Snate@binkert.org        opt_dir = dirname(current_vars_file)
8375342Sstever@gmail.com        if not isdir(opt_dir):
8385863Snate@binkert.org            mkdir(opt_dir)
8392632Sstever@eecs.umich.edu
8405863Snate@binkert.org        # Get default build variables from source tree.  Variables are
8415863Snate@binkert.org        # normally determined by name of $VARIANT_DIR, but can be
8422632Sstever@eecs.umich.edu        # overriden by 'default=' arg on command line.
8435863Snate@binkert.org        default_vars_file = joinpath('build_opts',
8445863Snate@binkert.org                                     ARGUMENTS.get('default', variant_dir))
8455863Snate@binkert.org        if isfile(default_vars_file):
8465863Snate@binkert.org            sticky_vars.files.append(default_vars_file)
8475863Snate@binkert.org            print "Variables file %s not found,\n  using defaults in %s" \
8485863Snate@binkert.org                  % (current_vars_file, default_vars_file)
8492632Sstever@eecs.umich.edu        else:
8505863Snate@binkert.org            print "Error: cannot find variables file %s or %s" \
8515863Snate@binkert.org                  % (current_vars_file, default_vars_file)
8522632Sstever@eecs.umich.edu            Exit(1)
8531888SN/A
8545863Snate@binkert.org    # Apply current variable settings to env
8555863Snate@binkert.org    sticky_vars.Update(env)
8565863Snate@binkert.org    nonsticky_vars.Update(env)
8571858SN/A
8585863Snate@binkert.org    help_text += "\nSticky variables for %s:\n" % variant_dir \
8595863Snate@binkert.org                 + sticky_vars.GenerateHelpText(env) \
8605863Snate@binkert.org                 + "\nNon-sticky variables for %s:\n" % variant_dir \
8615863Snate@binkert.org                 + nonsticky_vars.GenerateHelpText(env)
8622598SN/A
8635863Snate@binkert.org    # Process variable settings.
8641858SN/A
8651858SN/A    if not have_fenv and env['USE_FENV']:
8661858SN/A        print "Warning: <fenv.h> not available; " \
8675863Snate@binkert.org              "forcing USE_FENV to False in", variant_dir + "."
8681858SN/A        env['USE_FENV'] = False
8691858SN/A
8701858SN/A    if not env['USE_FENV']:
8715863Snate@binkert.org        print "Warning: No IEEE FP rounding mode control in", variant_dir + "."
8721871SN/A        print "         FP results may deviate slightly from other platforms."
8731858SN/A
8741858SN/A    if env['EFENCE']:
8751858SN/A        env.Append(LIBS=['efence'])
8761858SN/A
8771858SN/A    if env['USE_MYSQL']:
8781858SN/A        if not have_mysql:
8791858SN/A            print "Warning: MySQL not available; " \
8805863Snate@binkert.org                  "forcing USE_MYSQL to False in", variant_dir + "."
8811858SN/A            env['USE_MYSQL'] = False
8821858SN/A        else:
8835863Snate@binkert.org            print "Compiling in", variant_dir, "with MySQL support."
8841859SN/A            env.ParseConfig(mysql_config_libs)
8851859SN/A            env.ParseConfig(mysql_config_include)
8861869SN/A
8875863Snate@binkert.org    # Save sticky variable settings back to current variables file
8885863Snate@binkert.org    sticky_vars.Save(current_vars_file, env)
8891869SN/A
8901965SN/A    if env['USE_SSE2']:
8911965SN/A        env.Append(CCFLAGS='-msse2')
8921965SN/A
8932761Sstever@eecs.umich.edu    # The src/SConscript file sets up the build rules in 'env' according
8945863Snate@binkert.org    # to the configured variables.  It returns a list of environments,
8951869SN/A    # one for each variant build (debug, opt, etc.)
8965863Snate@binkert.org    envList = SConscript('src/SConscript', variant_dir = variant_path,
8972667Sstever@eecs.umich.edu                         exports = 'env')
8981869SN/A
8991869SN/A    # Set up the regression tests for each build.
9002929Sktlim@umich.edu    for e in envList:
9012929Sktlim@umich.edu        SConscript('tests/SConscript',
9025863Snate@binkert.org                   variant_dir = joinpath(variant_path, 'tests', e.Label),
9032929Sktlim@umich.edu                   exports = { 'env' : e }, duplicate = False)
904955SN/A
9052598SN/AHelp(help_text)
906