SConstruct revision 5863
1955SN/A# -*- mode:python -*-
2955SN/A
31762SN/A# Copyright (c) 2004-2005 The Regents of The University of Michigan
4955SN/A# All rights reserved.
5955SN/A#
6955SN/A# Redistribution and use in source and binary forms, with or without
7955SN/A# modification, are permitted provided that the following conditions are
8955SN/A# met: redistributions of source code must retain the above copyright
9955SN/A# notice, this list of conditions and the following disclaimer;
10955SN/A# redistributions in binary form must reproduce the above copyright
11955SN/A# notice, this list of conditions and the following disclaimer in the
12955SN/A# documentation and/or other materials provided with the distribution;
13955SN/A# neither the name of the copyright holders nor the names of its
14955SN/A# contributors may be used to endorse or promote products derived from
15955SN/A# this software without specific prior written permission.
16955SN/A#
17955SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18955SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19955SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20955SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21955SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22955SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23955SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24955SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25955SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26955SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27955SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
282665Ssaidi@eecs.umich.edu#
292665Ssaidi@eecs.umich.edu# Authors: Steve Reinhardt
305863Snate@binkert.org#          Nathan Binkert
31955SN/A
32955SN/A###################################################
33955SN/A#
34955SN/A# SCons top-level build description (SConstruct) file.
35955SN/A#
362632Sstever@eecs.umich.edu# While in this directory ('m5'), just type 'scons' to build the default
372632Sstever@eecs.umich.edu# configuration (see below), or type 'scons build/<CONFIG>/<binary>'
382632Sstever@eecs.umich.edu# to build some other configuration (e.g., 'build/ALPHA_FS/m5.opt' for
392632Sstever@eecs.umich.edu# the optimized full-system version).
40955SN/A#
412632Sstever@eecs.umich.edu# You can build M5 in a different directory as long as there is a
422632Sstever@eecs.umich.edu# 'build/<CONFIG>' somewhere along the target path.  The build system
432761Sstever@eecs.umich.edu# expects that all configs under the same build directory are being
442632Sstever@eecs.umich.edu# built for the same host system.
452632Sstever@eecs.umich.edu#
462632Sstever@eecs.umich.edu# Examples:
472761Sstever@eecs.umich.edu#
482761Sstever@eecs.umich.edu#   The following two commands are equivalent.  The '-u' option tells
492761Sstever@eecs.umich.edu#   scons to search up the directory tree for this SConstruct file.
502632Sstever@eecs.umich.edu#   % cd <path-to-src>/m5 ; scons build/ALPHA_FS/m5.debug
512632Sstever@eecs.umich.edu#   % cd <path-to-src>/m5/build/ALPHA_FS; scons -u m5.debug
522761Sstever@eecs.umich.edu#
532761Sstever@eecs.umich.edu#   The following two commands are equivalent and demonstrate building
542761Sstever@eecs.umich.edu#   in a directory outside of the source tree.  The '-C' option tells
552761Sstever@eecs.umich.edu#   scons to chdir to the specified directory to find this SConstruct
562761Sstever@eecs.umich.edu#   file.
572632Sstever@eecs.umich.edu#   % cd <path-to-src>/m5 ; scons /local/foo/build/ALPHA_FS/m5.debug
582632Sstever@eecs.umich.edu#   % cd /local/foo/build/ALPHA_FS; scons -C <path-to-src>/m5 m5.debug
592632Sstever@eecs.umich.edu#
602632Sstever@eecs.umich.edu# You can use 'scons -H' to print scons options.  If you're in this
612632Sstever@eecs.umich.edu# 'm5' directory (or use -u or -C to tell scons where to find this
622632Sstever@eecs.umich.edu# file), you can use 'scons -h' to print all the M5-specific build
632632Sstever@eecs.umich.edu# options as well.
64955SN/A#
65955SN/A###################################################
66955SN/A
675863Snate@binkert.org# Check for recent-enough Python and SCons versions.
685863Snate@binkert.orgtry:
695863Snate@binkert.org    # Really old versions of scons only take two options for the
705863Snate@binkert.org    # function, so check once without the revision and once with the
715863Snate@binkert.org    # revision, the first instance will fail for stuff other than
725863Snate@binkert.org    # 0.98, and the second will fail for 0.98.0
735863Snate@binkert.org    EnsureSConsVersion(0, 98)
745863Snate@binkert.org    EnsureSConsVersion(0, 98, 1)
755863Snate@binkert.orgexcept SystemExit, e:
765863Snate@binkert.org    print """
775863Snate@binkert.orgFor more details, see:
785863Snate@binkert.org    http://m5sim.org/wiki/index.php/Compiling_M5
795863Snate@binkert.org"""
805863Snate@binkert.org    raise
815863Snate@binkert.org
825863Snate@binkert.org# We ensure the python version early because we have stuff that
835863Snate@binkert.org# requires python 2.4
845863Snate@binkert.orgtry:
855863Snate@binkert.org    EnsurePythonVersion(2, 4)
865863Snate@binkert.orgexcept SystemExit, e:
875863Snate@binkert.org    print """
885863Snate@binkert.orgYou can use a non-default installation of the Python interpreter by
895863Snate@binkert.orgeither (1) rearranging your PATH so that scons finds the non-default
905863Snate@binkert.org'python' first or (2) explicitly invoking an alternative interpreter
915863Snate@binkert.orgon the scons script.
925863Snate@binkert.org
935863Snate@binkert.orgFor more details, see:
945863Snate@binkert.org    http://m5sim.org/wiki/index.php/Using_a_non-default_Python_installation
955863Snate@binkert.org"""
965863Snate@binkert.org    raise
975863Snate@binkert.org
98955SN/Aimport os
995396Ssaidi@eecs.umich.eduimport re
1005863Snate@binkert.orgimport subprocess
1015863Snate@binkert.orgimport sys
1024202Sbinkertn@umich.edu
1035863Snate@binkert.orgfrom os import mkdir, environ
1045863Snate@binkert.orgfrom os.path import abspath, basename, dirname, expanduser, normpath
1055863Snate@binkert.orgfrom os.path import exists,  isdir, isfile
1065863Snate@binkert.orgfrom os.path import join as joinpath, split as splitpath
107955SN/A
1085273Sstever@gmail.comimport SCons
1095273Sstever@gmail.com
1105863Snate@binkert.orgdef read_command(cmd):
1115863Snate@binkert.org    """run the command cmd, read the results and return them
1125863Snate@binkert.org    this is sorta like `cmd` in shell"""
1135863Snate@binkert.org    from subprocess import Popen, PIPE, STDOUT
1145863Snate@binkert.org    subp = Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT, close_fds=True)
1155863Snate@binkert.org    return subp.communicate()[0]
1165227Ssaidi@eecs.umich.edu
1175396Ssaidi@eecs.umich.edu# helper function: compare arrays or strings of version numbers.
1185396Ssaidi@eecs.umich.edu# E.g., compare_version((1,3,25), (1,4,1)')
1195396Ssaidi@eecs.umich.edu# returns -1, 0, 1 if v1 is <, ==, > v2
1205396Ssaidi@eecs.umich.edudef compare_versions(v1, v2):
1215396Ssaidi@eecs.umich.edu    def make_version_list(v):
1225396Ssaidi@eecs.umich.edu        if isinstance(v, (list,tuple)):
1235396Ssaidi@eecs.umich.edu            return v
1245396Ssaidi@eecs.umich.edu        elif isinstance(v, str):
1255588Ssaidi@eecs.umich.edu            return map(lambda x: int(re.match('\d+', x).group()), v.split('.'))
1265396Ssaidi@eecs.umich.edu        else:
1275396Ssaidi@eecs.umich.edu            raise TypeError
1285396Ssaidi@eecs.umich.edu
1295396Ssaidi@eecs.umich.edu    v1 = make_version_list(v1)
1305396Ssaidi@eecs.umich.edu    v2 = make_version_list(v2)
1315396Ssaidi@eecs.umich.edu    # Compare corresponding elements of lists
1325396Ssaidi@eecs.umich.edu    for n1,n2 in zip(v1, v2):
1335396Ssaidi@eecs.umich.edu        if n1 < n2: return -1
1345396Ssaidi@eecs.umich.edu        if n1 > n2: return  1
1355396Ssaidi@eecs.umich.edu    # all corresponding values are equal... see if one has extra values
1365396Ssaidi@eecs.umich.edu    if len(v1) < len(v2): return -1
1375396Ssaidi@eecs.umich.edu    if len(v1) > len(v2): return  1
1385396Ssaidi@eecs.umich.edu    return 0
1395396Ssaidi@eecs.umich.edu
140955SN/A# The absolute path to the current directory (where this file lives).
141955SN/AROOT = Dir('.').abspath
142955SN/A
1433717Sstever@eecs.umich.edu# Path to the M5 source tree.
1443716Sstever@eecs.umich.eduSRCDIR = joinpath(ROOT, 'src')
145955SN/A
1461533SN/A# tell python where to find m5 python code
1473716Sstever@eecs.umich.edusys.path.append(joinpath(ROOT, 'src/python'))
1481533SN/A
1495863Snate@binkert.org###################################################
1505863Snate@binkert.org# Mercurial Stuff.
1515863Snate@binkert.org# 1) Grab repository revision if we know it.
1525863Snate@binkert.org# 2) Ensure that the style hook is in place.
1535863Snate@binkert.org###################################################
1545863Snate@binkert.org
1555863Snate@binkert.orghg_info = "Unknown"
1565863Snate@binkert.orgtry:
1575863Snate@binkert.org    if not exists(ROOT) or not isdir(ROOT) or \
1585863Snate@binkert.org           not exists(joinpath(ROOT, ".hg")):
1595863Snate@binkert.org        raise ValueError(".hg directory not found")
1605863Snate@binkert.org    hg_info = read_command("cd %s; hg id -n -i -t -b" % ROOT).strip()
1615863Snate@binkert.orgexcept ImportError, e:
1625863Snate@binkert.org    print "Mercurial not found"
1635863Snate@binkert.orgexcept ValueError, e:
1645863Snate@binkert.org    print e
1655863Snate@binkert.orgexcept Exception, e:
1665863Snate@binkert.org    print "Other mercurial exception: %s" % e
1675863Snate@binkert.org
1684678Snate@binkert.orgdef check_style_hook(ui):
1694678Snate@binkert.org    ui.readconfig(joinpath(ROOT, '.hg', 'hgrc'))
1704678Snate@binkert.org    style_hook = ui.config('hooks', 'pretxncommit.style', None)
1714678Snate@binkert.org
1724678Snate@binkert.org    if not style_hook:
1734678Snate@binkert.org        print """\
1744678Snate@binkert.orgYou're missing the M5 style hook.
1754678Snate@binkert.orgPlease install the hook so we can ensure that all code fits a common style.
1764678Snate@binkert.org
1774678Snate@binkert.orgAll you'd need to do is add the following lines to your repository .hg/hgrc
1784678Snate@binkert.orgor your personal .hgrc
1794678Snate@binkert.org----------------
1804678Snate@binkert.org
1814678Snate@binkert.org[extensions]
1824678Snate@binkert.orgstyle = %s/util/style.py
1834678Snate@binkert.org
1844678Snate@binkert.org[hooks]
1854678Snate@binkert.orgpretxncommit.style = python:style.check_whitespace
1864678Snate@binkert.org""" % (ROOT)
1874678Snate@binkert.org        sys.exit(1)
1884678Snate@binkert.org
1894973Ssaidi@eecs.umich.eduif ARGUMENTS.get('IGNORE_STYLE') != 'True' and isdir(joinpath(ROOT, '.hg')):
1904678Snate@binkert.org    try:
1914678Snate@binkert.org        from mercurial import ui
1924678Snate@binkert.org        check_style_hook(ui.ui())
1934678Snate@binkert.org    except ImportError:
1944678Snate@binkert.org        pass
1954678Snate@binkert.org
1965863Snate@binkert.org
197955SN/A###################################################
198955SN/A#
1992632Sstever@eecs.umich.edu# Figure out which configurations to set up based on the path(s) of
2002632Sstever@eecs.umich.edu# the target(s).
201955SN/A#
202955SN/A###################################################
203955SN/A
204955SN/A# Find default configuration & binary.
2055863Snate@binkert.orgDefault(environ.get('M5_DEFAULT_BINARY', 'build/ALPHA_SE/m5.debug'))
206955SN/A
2072632Sstever@eecs.umich.edu# helper function: find last occurrence of element in list
2082632Sstever@eecs.umich.edudef rfind(l, elt, offs = -1):
2092632Sstever@eecs.umich.edu    for i in range(len(l)+offs, 0, -1):
2102632Sstever@eecs.umich.edu        if l[i] == elt:
2112632Sstever@eecs.umich.edu            return i
2122632Sstever@eecs.umich.edu    raise ValueError, "element not found"
2132632Sstever@eecs.umich.edu
2142632Sstever@eecs.umich.edu# Each target must have 'build' in the interior of the path; the
2152632Sstever@eecs.umich.edu# directory below this will determine the build parameters.  For
2162632Sstever@eecs.umich.edu# example, for target 'foo/bar/build/ALPHA_SE/arch/alpha/blah.do' we
2172632Sstever@eecs.umich.edu# recognize that ALPHA_SE specifies the configuration because it
2182632Sstever@eecs.umich.edu# follow 'build' in the bulid path.
2192632Sstever@eecs.umich.edu
2203718Sstever@eecs.umich.edu# Generate absolute paths to targets so we can see where the build dir is
2213718Sstever@eecs.umich.eduif COMMAND_LINE_TARGETS:
2223718Sstever@eecs.umich.edu    # Ask SCons which directory it was invoked from
2233718Sstever@eecs.umich.edu    launch_dir = GetLaunchDir()
2243718Sstever@eecs.umich.edu    # Make targets relative to invocation directory
2255863Snate@binkert.org    abs_targets = [ normpath(joinpath(launch_dir, str(x))) for x in \
2265863Snate@binkert.org                    COMMAND_LINE_TARGETS]
2273718Sstever@eecs.umich.eduelse:
2283718Sstever@eecs.umich.edu    # Default targets are relative to root of tree
2295863Snate@binkert.org    abs_targets = [ normpath(joinpath(ROOT, str(x))) for x in \
2305863Snate@binkert.org                    DEFAULT_TARGETS]
2313718Sstever@eecs.umich.edu
2323718Sstever@eecs.umich.edu
2332634Sstever@eecs.umich.edu# Generate a list of the unique build roots and configs that the
2342634Sstever@eecs.umich.edu# collected targets reference.
2355863Snate@binkert.orgvariant_paths = []
2362638Sstever@eecs.umich.edubuild_root = None
2372632Sstever@eecs.umich.edufor t in abs_targets:
2382632Sstever@eecs.umich.edu    path_dirs = t.split('/')
2392632Sstever@eecs.umich.edu    try:
2402632Sstever@eecs.umich.edu        build_top = rfind(path_dirs, 'build', -2)
2412632Sstever@eecs.umich.edu    except:
2422632Sstever@eecs.umich.edu        print "Error: no non-leaf 'build' dir found on target path", t
2431858SN/A        Exit(1)
2443716Sstever@eecs.umich.edu    this_build_root = joinpath('/',*path_dirs[:build_top+1])
2452638Sstever@eecs.umich.edu    if not build_root:
2462638Sstever@eecs.umich.edu        build_root = this_build_root
2472638Sstever@eecs.umich.edu    else:
2482638Sstever@eecs.umich.edu        if this_build_root != build_root:
2492638Sstever@eecs.umich.edu            print "Error: build targets not under same build root\n"\
2502638Sstever@eecs.umich.edu                  "  %s\n  %s" % (build_root, this_build_root)
2512638Sstever@eecs.umich.edu            Exit(1)
2525863Snate@binkert.org    variant_path = joinpath('/',*path_dirs[:build_top+2])
2535863Snate@binkert.org    if variant_path not in variant_paths:
2545863Snate@binkert.org        variant_paths.append(variant_path)
255955SN/A
2565341Sstever@gmail.com# Make sure build_root exists (might not if this is the first build there)
2575341Sstever@gmail.comif not isdir(build_root):
2585863Snate@binkert.org    mkdir(build_root)
2595341Sstever@gmail.com
260955SN/A###################################################
261955SN/A#
262955SN/A# Set up the default build environment.  This environment is copied
263955SN/A# and modified according to each selected configuration.
264955SN/A#
265955SN/A###################################################
266955SN/A
2675863Snate@binkert.orgenv = Environment(ENV = environ,  # inherit user's environment vars
2681858SN/A                  ROOT = ROOT,
2695863Snate@binkert.org                  SRCDIR = SRCDIR,
2705863Snate@binkert.org                  HG_INFO = hg_info)
271955SN/A
2724494Ssaidi@eecs.umich.eduExport('env')
2734494Ssaidi@eecs.umich.edu
2745863Snate@binkert.orgenv.SConsignFile(joinpath(build_root, "sconsign"))
2751105SN/A
2762667Sstever@eecs.umich.edu# Default duplicate option is to use hard links, but this messes up
2772667Sstever@eecs.umich.edu# when you use emacs to edit a file in the target dir, as emacs moves
2782667Sstever@eecs.umich.edu# file to file~ then copies to file, breaking the link.  Symbolic
2792667Sstever@eecs.umich.edu# (soft) links work better.
2802667Sstever@eecs.umich.eduenv.SetOption('duplicate', 'soft-copy')
2812667Sstever@eecs.umich.edu
2825341Sstever@gmail.com#
2835863Snate@binkert.org# Set up global sticky variables... these are common to an entire build
2845341Sstever@gmail.com# tree (not specific to a particular build like ALPHA_SE)
2855341Sstever@gmail.com#
2865341Sstever@gmail.com
2875863Snate@binkert.org# Variable validators & converters for global sticky variables
2885341Sstever@gmail.comdef PathListMakeAbsolute(val):
2895341Sstever@gmail.com    if not val:
2905341Sstever@gmail.com        return val
2915863Snate@binkert.org    f = lambda p: abspath(expanduser(p))
2925341Sstever@gmail.com    return ':'.join(map(f, val.split(':')))
2935341Sstever@gmail.com
2945341Sstever@gmail.comdef PathListAllExist(key, val, env):
2955341Sstever@gmail.com    if not val:
2965341Sstever@gmail.com        return
2975341Sstever@gmail.com    paths = val.split(':')
2985341Sstever@gmail.com    for path in paths:
2995341Sstever@gmail.com        if not isdir(path):
3005341Sstever@gmail.com            raise SCons.Errors.UserError("Path does not exist: '%s'" % path)
3015341Sstever@gmail.com
3025863Snate@binkert.orgglobal_sticky_vars_file = joinpath(build_root, 'variables.global')
3035341Sstever@gmail.com
3045863Snate@binkert.orgglobal_sticky_vars = Variables(global_sticky_vars_file, args=ARGUMENTS)
3055341Sstever@gmail.com
3065863Snate@binkert.orgglobal_sticky_vars.AddVariables(
3075863Snate@binkert.org    ('CC', 'C compiler', environ.get('CC', env['CC'])),
3085863Snate@binkert.org    ('CXX', 'C++ compiler', environ.get('CXX', env['CXX'])),
3095397Ssaidi@eecs.umich.edu    ('BATCH', 'Use batch pool for build and tests', False),
3105397Ssaidi@eecs.umich.edu    ('BATCH_CMD', 'Batch pool submission command name', 'qdo'),
3115341Sstever@gmail.com    ('EXTRAS', 'Add Extra directories to the compilation', '',
3125341Sstever@gmail.com     PathListAllExist, PathListMakeAbsolute)
3135341Sstever@gmail.com    )    
3145341Sstever@gmail.com
3155341Sstever@gmail.com# base help text
3165341Sstever@gmail.comhelp_text = '''
3175341Sstever@gmail.comUsage: scons [scons options] [build options] [target(s)]
3185341Sstever@gmail.com
3195863Snate@binkert.orgGlobal sticky options:
3205341Sstever@gmail.com'''
3215341Sstever@gmail.com
3225863Snate@binkert.orghelp_text += global_sticky_vars.GenerateHelpText(env)
3235341Sstever@gmail.com
3245863Snate@binkert.org# Update env with values from ARGUMENTS & file global_sticky_vars_file
3255863Snate@binkert.orgglobal_sticky_vars.Update(env)
3265341Sstever@gmail.com
3275863Snate@binkert.org# Save sticky variable settings back to current variables file
3285863Snate@binkert.orgglobal_sticky_vars.Save(global_sticky_vars_file, env)
3295341Sstever@gmail.com
3305863Snate@binkert.org# Parse EXTRAS variable to build list of all directories where we're
3315341Sstever@gmail.com# look for sources etc.  This list is exported as base_dir_list.
3325742Snate@binkert.orgbase_dir = joinpath(ROOT, 'src')
3335341Sstever@gmail.comif env['EXTRAS']:
3345742Snate@binkert.org    extras_dir_list = env['EXTRAS'].split(':')
3355742Snate@binkert.orgelse:
3365742Snate@binkert.org    extras_dir_list = []
3375341Sstever@gmail.com
3385742Snate@binkert.orgExport('base_dir')
3395742Snate@binkert.orgExport('extras_dir_list')
3405341Sstever@gmail.com
3412632Sstever@eecs.umich.edu# M5_PLY is used by isa_parser.py to find the PLY package.
3425199Sstever@gmail.comenv.Append(ENV = { 'M5_PLY' : str(Dir('ext/ply')) })
3435863Snate@binkert.orgenv['GCC'] = read_command(env['CXX'] + ' --version').find('g++') >= 0
3445863Snate@binkert.orgenv['SUNCC'] = read_command(env['CXX'] + ' -V').find('Sun C++') >= 0
3455863Snate@binkert.orgenv['ICC'] = read_command(env['CXX'] + ' -V').find('Intel') >= 0
3463942Ssaidi@eecs.umich.eduif env['GCC'] + env['SUNCC'] + env['ICC'] > 1:
3473940Ssaidi@eecs.umich.edu    print 'Error: How can we have two at the same time?'
3483918Ssaidi@eecs.umich.edu    Exit(1)
3493918Ssaidi@eecs.umich.edu
3501858SN/A# Set up default C++ compiler flags
3513918Ssaidi@eecs.umich.eduif env['GCC']:
3523918Ssaidi@eecs.umich.edu    env.Append(CCFLAGS='-pipe')
3533918Ssaidi@eecs.umich.edu    env.Append(CCFLAGS='-fno-strict-aliasing')
3543918Ssaidi@eecs.umich.edu    env.Append(CCFLAGS=Split('-Wall -Wno-sign-compare -Werror -Wundef'))
3555571Snate@binkert.org    env.Append(CXXFLAGS='-Wno-deprecated')
3563940Ssaidi@eecs.umich.eduelif env['ICC']:
3573940Ssaidi@eecs.umich.edu    pass #Fix me... add warning flags once we clean up icc warnings
3583918Ssaidi@eecs.umich.eduelif env['SUNCC']:
3593918Ssaidi@eecs.umich.edu    env.Append(CCFLAGS='-Qoption ccfe')
3603918Ssaidi@eecs.umich.edu    env.Append(CCFLAGS='-features=gcc')
3613918Ssaidi@eecs.umich.edu    env.Append(CCFLAGS='-features=extensions')
3623918Ssaidi@eecs.umich.edu    env.Append(CCFLAGS='-library=stlport4')
3633918Ssaidi@eecs.umich.edu    env.Append(CCFLAGS='-xar')
3643918Ssaidi@eecs.umich.edu#    env.Append(CCFLAGS='-instances=semiexplicit')
3653918Ssaidi@eecs.umich.eduelse:
3663918Ssaidi@eecs.umich.edu    print 'Error: Don\'t know what compiler options to use for your compiler.'
3673940Ssaidi@eecs.umich.edu    print '       Please fix SConstruct and src/SConscript and try again.'
3683918Ssaidi@eecs.umich.edu    Exit(1)
3693918Ssaidi@eecs.umich.edu
3705397Ssaidi@eecs.umich.edu# Do this after we save setting back, or else we'll tack on an
3715397Ssaidi@eecs.umich.edu# extra 'qdo' every time we run scons.
3725397Ssaidi@eecs.umich.eduif env['BATCH']:
3735708Ssaidi@eecs.umich.edu    env['CC']     = env['BATCH_CMD'] + ' ' + env['CC']
3745708Ssaidi@eecs.umich.edu    env['CXX']    = env['BATCH_CMD'] + ' ' + env['CXX']
3755708Ssaidi@eecs.umich.edu    env['AS']     = env['BATCH_CMD'] + ' ' + env['AS']
3765708Ssaidi@eecs.umich.edu    env['AR']     = env['BATCH_CMD'] + ' ' + env['AR']
3775708Ssaidi@eecs.umich.edu    env['RANLIB'] = env['BATCH_CMD'] + ' ' + env['RANLIB']
3785397Ssaidi@eecs.umich.edu
3791851SN/Aif sys.platform == 'cygwin':
3801851SN/A    # cygwin has some header file issues...
3811858SN/A    env.Append(CCFLAGS=Split("-Wno-uninitialized"))
3825200Sstever@gmail.comenv.Append(CPPPATH=[Dir('ext/dnet')])
383955SN/A
3843053Sstever@eecs.umich.edu# Check for SWIG
3853053Sstever@eecs.umich.eduif not env.has_key('SWIG'):
3863053Sstever@eecs.umich.edu    print 'Error: SWIG utility not found.'
3873053Sstever@eecs.umich.edu    print '       Please install (see http://www.swig.org) and retry.'
3883053Sstever@eecs.umich.edu    Exit(1)
3893053Sstever@eecs.umich.edu
3903053Sstever@eecs.umich.edu# Check for appropriate SWIG version
3915863Snate@binkert.orgswig_version = read_command('swig -version').split()
3923053Sstever@eecs.umich.edu# First 3 words should be "SWIG Version x.y.z"
3934742Sstever@eecs.umich.eduif len(swig_version) < 3 or \
3944742Sstever@eecs.umich.edu        swig_version[0] != 'SWIG' or swig_version[1] != 'Version':
3953053Sstever@eecs.umich.edu    print 'Error determining SWIG version.'
3963053Sstever@eecs.umich.edu    Exit(1)
3973053Sstever@eecs.umich.edu
3983053Sstever@eecs.umich.edumin_swig_version = '1.3.28'
3993053Sstever@eecs.umich.eduif compare_versions(swig_version[2], min_swig_version) < 0:
4003053Sstever@eecs.umich.edu    print 'Error: SWIG version', min_swig_version, 'or newer required.'
4013053Sstever@eecs.umich.edu    print '       Installed version:', swig_version[2]
4023053Sstever@eecs.umich.edu    Exit(1)
4033053Sstever@eecs.umich.edu
4042667Sstever@eecs.umich.edu# Set up SWIG flags & scanner
4054554Sbinkertn@umich.eduswig_flags=Split('-c++ -python -modern -templatereduce $_CPPINCFLAGS')
4064554Sbinkertn@umich.eduenv.Append(SWIGFLAGS=swig_flags)
4072667Sstever@eecs.umich.edu
4084554Sbinkertn@umich.edu# filter out all existing swig scanners, they mess up the dependency
4094554Sbinkertn@umich.edu# stuff for some reason
4104554Sbinkertn@umich.eduscanners = []
4114554Sbinkertn@umich.edufor scanner in env['SCANNERS']:
4124554Sbinkertn@umich.edu    skeys = scanner.skeys
4134554Sbinkertn@umich.edu    if skeys == '.i':
4144554Sbinkertn@umich.edu        continue
4154781Snate@binkert.org
4164554Sbinkertn@umich.edu    if isinstance(skeys, (list, tuple)) and '.i' in skeys:
4174554Sbinkertn@umich.edu        continue
4182667Sstever@eecs.umich.edu
4194554Sbinkertn@umich.edu    scanners.append(scanner)
4204554Sbinkertn@umich.edu
4214554Sbinkertn@umich.edu# add the new swig scanner that we like better
4224554Sbinkertn@umich.edufrom SCons.Scanner import ClassicCPP as CPPScanner
4232667Sstever@eecs.umich.eduswig_inc_re = '^[ \t]*[%,#][ \t]*(?:include|import)[ \t]*(<|")([^>"]+)(>|")'
4244554Sbinkertn@umich.eduscanners.append(CPPScanner("SwigScan", [ ".i" ], "CPPPATH", swig_inc_re))
4252667Sstever@eecs.umich.edu
4264554Sbinkertn@umich.edu# replace the scanners list that has what we want
4274554Sbinkertn@umich.eduenv['SCANNERS'] = scanners
4282667Sstever@eecs.umich.edu
4295522Snate@binkert.org# Add a custom Check function to the Configure context so that we can
4305522Snate@binkert.org# figure out if the compiler adds leading underscores to global
4315522Snate@binkert.org# variables.  This is needed for the autogenerated asm files that we
4325522Snate@binkert.org# use for embedding the python code.
4335522Snate@binkert.orgdef CheckLeading(context):
4345522Snate@binkert.org    context.Message("Checking for leading underscore in global variables...")
4355522Snate@binkert.org    # 1) Define a global variable called x from asm so the C compiler
4365522Snate@binkert.org    #    won't change the symbol at all.
4375522Snate@binkert.org    # 2) Declare that variable.
4385522Snate@binkert.org    # 3) Use the variable
4395522Snate@binkert.org    #
4405522Snate@binkert.org    # If the compiler prepends an underscore, this will successfully
4415522Snate@binkert.org    # link because the external symbol 'x' will be called '_x' which
4425522Snate@binkert.org    # was defined by the asm statement.  If the compiler does not
4435522Snate@binkert.org    # prepend an underscore, this will not successfully link because
4445522Snate@binkert.org    # '_x' will have been defined by assembly, while the C portion of
4455522Snate@binkert.org    # the code will be trying to use 'x'
4465522Snate@binkert.org    ret = context.TryLink('''
4475522Snate@binkert.org        asm(".globl _x; _x: .byte 0");
4485522Snate@binkert.org        extern int x;
4495522Snate@binkert.org        int main() { return x; }
4505522Snate@binkert.org        ''', extension=".c")
4515522Snate@binkert.org    context.env.Append(LEADING_UNDERSCORE=ret)
4525522Snate@binkert.org    context.Result(ret)
4535522Snate@binkert.org    return ret
4545522Snate@binkert.org
4552638Sstever@eecs.umich.edu# Platform-specific configuration.  Note again that we assume that all
4562638Sstever@eecs.umich.edu# builds under a given build root run on the same host platform.
4572638Sstever@eecs.umich.educonf = Configure(env,
4583716Sstever@eecs.umich.edu                 conf_dir = joinpath(build_root, '.scons_config'),
4595522Snate@binkert.org                 log_file = joinpath(build_root, 'scons_config.log'),
4605522Snate@binkert.org                 custom_tests = { 'CheckLeading' : CheckLeading })
4615522Snate@binkert.org
4625522Snate@binkert.org# Check for leading underscores.  Don't really need to worry either
4635522Snate@binkert.org# way so don't need to check the return code.
4645522Snate@binkert.orgconf.CheckLeading()
4651858SN/A
4665227Ssaidi@eecs.umich.edu# Check if we should compile a 64 bit binary on Mac OS X/Darwin
4675227Ssaidi@eecs.umich.edutry:
4685227Ssaidi@eecs.umich.edu    import platform
4695227Ssaidi@eecs.umich.edu    uname = platform.uname()
4705227Ssaidi@eecs.umich.edu    if uname[0] == 'Darwin' and compare_versions(uname[2], '9.0.0') >= 0:
4715863Snate@binkert.org        if int(read_command('sysctl -n hw.cpu64bit_capable')[0]):
4725227Ssaidi@eecs.umich.edu            env.Append(CCFLAGS='-arch x86_64')
4735227Ssaidi@eecs.umich.edu            env.Append(CFLAGS='-arch x86_64')
4745227Ssaidi@eecs.umich.edu            env.Append(LINKFLAGS='-arch x86_64')
4755227Ssaidi@eecs.umich.edu            env.Append(ASFLAGS='-arch x86_64')
4765227Ssaidi@eecs.umich.eduexcept:
4775227Ssaidi@eecs.umich.edu    pass
4785227Ssaidi@eecs.umich.edu
4795204Sstever@gmail.com# Recent versions of scons substitute a "Null" object for Configure()
4805204Sstever@gmail.com# when configuration isn't necessary, e.g., if the "--help" option is
4815204Sstever@gmail.com# present.  Unfortuantely this Null object always returns false,
4825204Sstever@gmail.com# breaking all our configuration checks.  We replace it with our own
4835204Sstever@gmail.com# more optimistic null object that returns True instead.
4845204Sstever@gmail.comif not conf:
4855204Sstever@gmail.com    def NullCheck(*args, **kwargs):
4865204Sstever@gmail.com        return True
4875204Sstever@gmail.com
4885204Sstever@gmail.com    class NullConf:
4895204Sstever@gmail.com        def __init__(self, env):
4905204Sstever@gmail.com            self.env = env
4915204Sstever@gmail.com        def Finish(self):
4925204Sstever@gmail.com            return self.env
4935204Sstever@gmail.com        def __getattr__(self, mname):
4945204Sstever@gmail.com            return NullCheck
4955204Sstever@gmail.com
4965204Sstever@gmail.com    conf = NullConf(env)
4975204Sstever@gmail.com
4983118Sstever@eecs.umich.edu# Find Python include and library directories for embedding the
4993118Sstever@eecs.umich.edu# interpreter.  For consistency, we will use the same Python
5003118Sstever@eecs.umich.edu# installation used to run scons (and thus this script).  If you want
5013118Sstever@eecs.umich.edu# to link in an alternate version, see above for instructions on how
5023118Sstever@eecs.umich.edu# to invoke scons with a different copy of the Python interpreter.
5035863Snate@binkert.orgfrom distutils import sysconfig
5043118Sstever@eecs.umich.edu
5055863Snate@binkert.orgpy_getvar = sysconfig.get_config_var
5063118Sstever@eecs.umich.edu
5075863Snate@binkert.orgpy_version = 'python' + py_getvar('VERSION')
5085863Snate@binkert.org
5095863Snate@binkert.orgpy_general_include = sysconfig.get_python_inc()
5105863Snate@binkert.orgpy_platform_include = sysconfig.get_python_inc(plat_specific=True)
5115863Snate@binkert.orgpy_includes = [ py_general_include ]
5125863Snate@binkert.orgif py_platform_include != py_general_include:
5135863Snate@binkert.org    py_includes.append(py_platform_include)
5145863Snate@binkert.org
5155863Snate@binkert.orgpy_lib_path = []
5165863Snate@binkert.org# add the prefix/lib/pythonX.Y/config dir, but only if there is no
5175863Snate@binkert.org# shared library in prefix/lib/.
5185863Snate@binkert.orgif not py_getvar('Py_ENABLE_SHARED'):
5195863Snate@binkert.org    py_lib_path.append('-L' + py_getvar('LIBPL'))
5205863Snate@binkert.org
5215863Snate@binkert.orgpy_libs = []
5225863Snate@binkert.orgfor lib in py_getvar('LIBS').split() + py_getvar('SYSLIBS').split():
5235863Snate@binkert.org    if lib not in py_libs:
5245863Snate@binkert.org        py_libs.append(lib)
5255863Snate@binkert.orgpy_libs.append('-l' + py_version)
5265863Snate@binkert.org
5275863Snate@binkert.orgenv.Append(CPPPATH=py_includes)
5285863Snate@binkert.orgenv.Append(LIBPATH=py_lib_path)
5295863Snate@binkert.org#env.Append(LIBS=py_libs)
5305863Snate@binkert.org
5315863Snate@binkert.org# verify that this stuff works
5323118Sstever@eecs.umich.eduif not conf.CheckHeader('Python.h', '<>'):
5335863Snate@binkert.org    print "Error: can't find Python.h header in", py_includes
5343118Sstever@eecs.umich.edu    Exit(1)
5353118Sstever@eecs.umich.edu
5365863Snate@binkert.orgfor lib in py_libs:
5375863Snate@binkert.org    assert lib.startswith('-l')
5385863Snate@binkert.org    lib = lib[2:]
5395863Snate@binkert.org    if not conf.CheckLib(lib):
5405863Snate@binkert.org        print "Error: can't find library %s required by python" % lib
5415863Snate@binkert.org        Exit(1)
5423118Sstever@eecs.umich.edu
5433483Ssaidi@eecs.umich.edu# On Solaris you need to use libsocket for socket ops
5443494Ssaidi@eecs.umich.eduif not conf.CheckLibWithHeader(None, 'sys/socket.h', 'C++', 'accept(0,0,0);'):
5453494Ssaidi@eecs.umich.edu   if not conf.CheckLibWithHeader('socket', 'sys/socket.h', 'C++', 'accept(0,0,0);'):
5463483Ssaidi@eecs.umich.edu       print "Can't find library with socket calls (e.g. accept())"
5473483Ssaidi@eecs.umich.edu       Exit(1)
5483483Ssaidi@eecs.umich.edu
5493053Sstever@eecs.umich.edu# Check for zlib.  If the check passes, libz will be automatically
5503053Sstever@eecs.umich.edu# added to the LIBS environment variable.
5513918Ssaidi@eecs.umich.eduif not conf.CheckLibWithHeader('z', 'zlib.h', 'C++','zlibVersion();'):
5523053Sstever@eecs.umich.edu    print 'Error: did not find needed zlib compression library '\
5533053Sstever@eecs.umich.edu          'and/or zlib.h header file.'
5543053Sstever@eecs.umich.edu    print '       Please install zlib and try again.'
5553053Sstever@eecs.umich.edu    Exit(1)
5563053Sstever@eecs.umich.edu
5571858SN/A# Check for <fenv.h> (C99 FP environment control)
5581858SN/Ahave_fenv = conf.CheckHeader('fenv.h', '<>')
5591858SN/Aif not have_fenv:
5601858SN/A    print "Warning: Header file <fenv.h> not found."
5611858SN/A    print "         This host has no IEEE FP rounding mode control."
5621858SN/A
5635863Snate@binkert.org######################################################################
5645863Snate@binkert.org#
5651859SN/A# Check for mysql.
5665863Snate@binkert.org#
5671858SN/Amysql_config = WhereIs('mysql_config')
5685863Snate@binkert.orghave_mysql = bool(mysql_config)
5691858SN/A
5701859SN/A# Check MySQL version.
5711859SN/Aif have_mysql:
5725863Snate@binkert.org    mysql_version = read_command(mysql_config + ' --version')
5733053Sstever@eecs.umich.edu    min_mysql_version = '4.1'
5743053Sstever@eecs.umich.edu    if compare_versions(mysql_version, min_mysql_version) < 0:
5753053Sstever@eecs.umich.edu        print 'Warning: MySQL', min_mysql_version, 'or newer required.'
5763053Sstever@eecs.umich.edu        print '         Version', mysql_version, 'detected.'
5771859SN/A        have_mysql = False
5781859SN/A
5791859SN/A# Set up mysql_config commands.
5801859SN/Aif have_mysql:
5811859SN/A    mysql_config_include = mysql_config + ' --include'
5821859SN/A    if os.system(mysql_config_include + ' > /dev/null') != 0:
5831859SN/A        # older mysql_config versions don't support --include, use
5841859SN/A        # --cflags instead
5851862SN/A        mysql_config_include = mysql_config + ' --cflags | sed s/\\\'//g'
5861859SN/A    # This seems to work in all versions
5871859SN/A    mysql_config_libs = mysql_config + ' --libs'
5881859SN/A
5895863Snate@binkert.org######################################################################
5905863Snate@binkert.org#
5915863Snate@binkert.org# Finish the configuration
5925863Snate@binkert.org#
5931858SN/Aenv = conf.Finish()
5941858SN/A
5955863Snate@binkert.org######################################################################
5965863Snate@binkert.org#
5975863Snate@binkert.org# Collect all non-global variables
5985863Snate@binkert.org#
5995863Snate@binkert.org
6002139SN/A# Define the universe of supported ISAs
6014202Sbinkertn@umich.eduall_isa_list = [ ]
6024202Sbinkertn@umich.eduExport('all_isa_list')
6032139SN/A
6042155SN/A# Define the universe of supported CPU models
6054202Sbinkertn@umich.eduall_cpu_list = [ ]
6064202Sbinkertn@umich.edudefault_cpus = [ ]
6074202Sbinkertn@umich.eduExport('all_cpu_list', 'default_cpus')
6082155SN/A
6095863Snate@binkert.org# Sticky variables get saved in the variables file so they persist from
6101869SN/A# one invocation to the next (unless overridden, in which case the new
6111869SN/A# value becomes sticky).
6125863Snate@binkert.orgsticky_vars = Variables(args=ARGUMENTS)
6135863Snate@binkert.orgExport('sticky_vars')
6144202Sbinkertn@umich.edu
6155863Snate@binkert.org# Non-sticky variables only apply to the current build.
6165863Snate@binkert.orgnonsticky_vars = Variables(args=ARGUMENTS)
6175863Snate@binkert.orgExport('nonsticky_vars')
6184202Sbinkertn@umich.edu
6194202Sbinkertn@umich.edu# Walk the tree and execute all SConsopts scripts that wil add to the
6205863Snate@binkert.org# above variables
6215742Snate@binkert.orgfor bdir in [ base_dir ] + extras_dir_list:
6225742Snate@binkert.org    for root, dirs, files in os.walk(bdir):
6235341Sstever@gmail.com        if 'SConsopts' in files:
6245342Sstever@gmail.com            print "Reading", joinpath(root, 'SConsopts')
6255342Sstever@gmail.com            SConscript(joinpath(root, 'SConsopts'))
6264202Sbinkertn@umich.edu
6274202Sbinkertn@umich.eduall_isa_list.sort()
6284202Sbinkertn@umich.eduall_cpu_list.sort()
6294202Sbinkertn@umich.edudefault_cpus.sort()
6304202Sbinkertn@umich.edu
6315863Snate@binkert.orgsticky_vars.AddVariables(
6325863Snate@binkert.org    EnumVariable('TARGET_ISA', 'Target ISA', 'alpha', all_isa_list),
6335863Snate@binkert.org    BoolVariable('FULL_SYSTEM', 'Full-system support', False),
6345863Snate@binkert.org    ListVariable('CPU_MODELS', 'CPU models', default_cpus, all_cpu_list),
6355863Snate@binkert.org    BoolVariable('NO_FAST_ALLOC', 'Disable fast object allocator', False),
6365863Snate@binkert.org    BoolVariable('FAST_ALLOC_DEBUG', 'Enable fast object allocator debugging',
6375863Snate@binkert.org                 False),
6385863Snate@binkert.org    BoolVariable('FAST_ALLOC_STATS', 'Enable fast object allocator statistics',
6395863Snate@binkert.org                 False),
6405863Snate@binkert.org    BoolVariable('EFENCE', 'Link with Electric Fence malloc debugger',
6415863Snate@binkert.org                 False),
6425863Snate@binkert.org    BoolVariable('SS_COMPATIBLE_FP',
6435863Snate@binkert.org                 'Make floating-point results compatible with SimpleScalar',
6445863Snate@binkert.org                 False),
6455863Snate@binkert.org    BoolVariable('USE_SSE2',
6465863Snate@binkert.org                 'Compile for SSE2 (-msse2) to get IEEE FP on x86 hosts',
6475863Snate@binkert.org                 False),
6485863Snate@binkert.org    BoolVariable('USE_MYSQL', 'Use MySQL for stats output', have_mysql),
6495863Snate@binkert.org    BoolVariable('USE_FENV', 'Use <fenv.h> IEEE mode control', have_fenv),
6505863Snate@binkert.org    BoolVariable('USE_CHECKER', 'Use checker for detailed CPU models', False),
6511869SN/A    )
6521858SN/A
6535863Snate@binkert.orgnonsticky_vars.AddVariables(
6545863Snate@binkert.org    BoolVariable('update_ref', 'Update test reference outputs', False)
6551869SN/A    )
6561858SN/A
6575863Snate@binkert.org# These variables get exported to #defines in config/*.hh (see src/SConscript).
6585863Snate@binkert.orgenv.ExportVariables = ['FULL_SYSTEM', 'ALPHA_TLASER', 'USE_FENV', \
6595863Snate@binkert.org                       'USE_MYSQL', 'NO_FAST_ALLOC', 'FAST_ALLOC_DEBUG', \
6605863Snate@binkert.org                       'FAST_ALLOC_STATS', 'SS_COMPATIBLE_FP', \
6615863Snate@binkert.org                       'USE_CHECKER', 'TARGET_ISA']
6621858SN/A
663955SN/A###################################################
664955SN/A#
6651869SN/A# Define a SCons builder for configuration flag headers.
6661869SN/A#
6671869SN/A###################################################
6681869SN/A
6691869SN/A# This function generates a config header file that #defines the
6705863Snate@binkert.org# variable symbol to the current variable setting (0 or 1).  The source
6715863Snate@binkert.org# operands are the name of the variable and a Value node containing the
6725863Snate@binkert.org# value of the variable.
6731869SN/Adef build_config_file(target, source, env):
6745863Snate@binkert.org    (variable, value) = [s.get_contents() for s in source]
6751869SN/A    f = file(str(target[0]), 'w')
6765863Snate@binkert.org    print >> f, '#define', variable, value
6771869SN/A    f.close()
6781869SN/A    return None
6791869SN/A
6801869SN/A# Generate the message to be printed when building the config file.
6811869SN/Adef build_config_file_string(target, source, env):
6825863Snate@binkert.org    (variable, value) = [s.get_contents() for s in source]
6835863Snate@binkert.org    return "Defining %s as %s in %s." % (variable, value, target[0])
6841869SN/A
6851869SN/A# Combine the two functions into a scons Action object.
6861869SN/Aconfig_action = Action(build_config_file, build_config_file_string)
6871869SN/A
6881869SN/A# The emitter munges the source & target node lists to reflect what
6891869SN/A# we're really doing.
6901869SN/Adef config_emitter(target, source, env):
6915863Snate@binkert.org    # extract variable name from Builder arg
6925863Snate@binkert.org    variable = str(target[0])
6931869SN/A    # True target is config header file
6945863Snate@binkert.org    target = joinpath('config', variable.lower() + '.hh')
6955863Snate@binkert.org    val = env[variable]
6963356Sbinkertn@umich.edu    if isinstance(val, bool):
6973356Sbinkertn@umich.edu        # Force value to 0/1
6983356Sbinkertn@umich.edu        val = int(val)
6993356Sbinkertn@umich.edu    elif isinstance(val, str):
7003356Sbinkertn@umich.edu        val = '"' + val + '"'
7014781Snate@binkert.org
7025863Snate@binkert.org    # Sources are variable name & value (packaged in SCons Value nodes)
7035863Snate@binkert.org    return ([target], [Value(variable), Value(val)])
7041869SN/A
7051869SN/Aconfig_builder = Builder(emitter = config_emitter, action = config_action)
7061869SN/A
7071869SN/Aenv.Append(BUILDERS = { 'ConfigFile' : config_builder })
7081869SN/A
7092638Sstever@eecs.umich.edu# libelf build is shared across all configs in the build root.
7102638Sstever@eecs.umich.eduenv.SConscript('ext/libelf/SConscript',
7115863Snate@binkert.org               variant_dir = joinpath(build_root, 'libelf'),
7122638Sstever@eecs.umich.edu               exports = 'env')
7132638Sstever@eecs.umich.edu
7145749Scws3k@cs.virginia.edu# gzstream build is shared across all configs in the build root.
7155749Scws3k@cs.virginia.eduenv.SConscript('ext/gzstream/SConscript',
7165863Snate@binkert.org               variant_dir = joinpath(build_root, 'gzstream'),
7175749Scws3k@cs.virginia.edu               exports = 'env')
7185749Scws3k@cs.virginia.edu
7191869SN/A###################################################
7201869SN/A#
7213546Sgblack@eecs.umich.edu# This function is used to set up a directory with switching headers
7223546Sgblack@eecs.umich.edu#
7233546Sgblack@eecs.umich.edu###################################################
7243546Sgblack@eecs.umich.edu
7254202Sbinkertn@umich.eduenv['ALL_ISA_LIST'] = all_isa_list
7265863Snate@binkert.orgdef make_switching_dir(dname, switch_headers, env):
7273546Sgblack@eecs.umich.edu    # Generate the header.  target[0] is the full path of the output
7283546Sgblack@eecs.umich.edu    # header to generate.  'source' is a dummy variable, since we get the
7293546Sgblack@eecs.umich.edu    # list of ISAs from env['ALL_ISA_LIST'].
7303546Sgblack@eecs.umich.edu    def gen_switch_hdr(target, source, env):
7314781Snate@binkert.org        fname = str(target[0])
7325863Snate@binkert.org        bname = basename(fname)
7334781Snate@binkert.org        f = open(fname, 'w')
7344781Snate@binkert.org        f.write('#include "arch/isa_specific.hh"\n')
7354781Snate@binkert.org        cond = '#if'
7364781Snate@binkert.org        for isa in all_isa_list:
7374781Snate@binkert.org            f.write('%s THE_ISA == %s_ISA\n#include "%s/%s/%s"\n'
7385863Snate@binkert.org                    % (cond, isa.upper(), dname, isa, bname))
7394781Snate@binkert.org            cond = '#elif'
7404781Snate@binkert.org        f.write('#else\n#error "THE_ISA not set"\n#endif\n')
7414781Snate@binkert.org        f.close()
7424781Snate@binkert.org        return 0
7433546Sgblack@eecs.umich.edu
7443546Sgblack@eecs.umich.edu    # String to print when generating header
7453546Sgblack@eecs.umich.edu    def gen_switch_hdr_string(target, source, env):
7464781Snate@binkert.org        return "Generating switch header " + str(target[0])
7473546Sgblack@eecs.umich.edu
7483546Sgblack@eecs.umich.edu    # Build SCons Action object. 'varlist' specifies env vars that this
7493546Sgblack@eecs.umich.edu    # action depends on; when env['ALL_ISA_LIST'] changes these actions
7503546Sgblack@eecs.umich.edu    # should get re-executed.
7513546Sgblack@eecs.umich.edu    switch_hdr_action = Action(gen_switch_hdr, gen_switch_hdr_string,
7523546Sgblack@eecs.umich.edu                               varlist=['ALL_ISA_LIST'])
7533546Sgblack@eecs.umich.edu
7543546Sgblack@eecs.umich.edu    # Instantiate actions for each header
7553546Sgblack@eecs.umich.edu    for hdr in switch_headers:
7563546Sgblack@eecs.umich.edu        env.Command(hdr, [], switch_hdr_action)
7574202Sbinkertn@umich.eduExport('make_switching_dir')
7583546Sgblack@eecs.umich.edu
7593546Sgblack@eecs.umich.edu###################################################
7603546Sgblack@eecs.umich.edu#
761955SN/A# Define build environments for selected configurations.
762955SN/A#
763955SN/A###################################################
764955SN/A
7651858SN/A# rename base env
7661858SN/Abase_env = env
7671858SN/A
7685863Snate@binkert.orgfor variant_path in variant_paths:
7695863Snate@binkert.org    print "Building in", variant_path
7705343Sstever@gmail.com
7715343Sstever@gmail.com    # Make a copy of the build-root environment to use for this config.
7725863Snate@binkert.org    env = base_env.Clone()
7735863Snate@binkert.org    env['BUILDDIR'] = variant_path
7744773Snate@binkert.org
7755863Snate@binkert.org    # variant_dir is the tail component of build path, and is used to
7762632Sstever@eecs.umich.edu    # determine the build parameters (e.g., 'ALPHA_SE')
7775863Snate@binkert.org    (build_root, variant_dir) = splitpath(variant_path)
7782023SN/A
7795863Snate@binkert.org    # Set env variables according to the build directory config.
7805863Snate@binkert.org    sticky_vars.files = []
7815863Snate@binkert.org    # Variables for $BUILD_ROOT/$VARIANT_DIR are stored in
7825863Snate@binkert.org    # $BUILD_ROOT/variables/$VARIANT_DIR so you can nuke
7835863Snate@binkert.org    # $BUILD_ROOT/$VARIANT_DIR without losing your variables settings.
7845863Snate@binkert.org    current_vars_file = joinpath(build_root, 'variables', variant_dir)
7855863Snate@binkert.org    if isfile(current_vars_file):
7865863Snate@binkert.org        sticky_vars.files.append(current_vars_file)
7875863Snate@binkert.org        print "Using saved variables file %s" % current_vars_file
7882632Sstever@eecs.umich.edu    else:
7895863Snate@binkert.org        # Build dir-specific variables file doesn't exist.
7902023SN/A
7912632Sstever@eecs.umich.edu        # Make sure the directory is there so we can create it later
7925863Snate@binkert.org        opt_dir = dirname(current_vars_file)
7935342Sstever@gmail.com        if not isdir(opt_dir):
7945863Snate@binkert.org            mkdir(opt_dir)
7952632Sstever@eecs.umich.edu
7965863Snate@binkert.org        # Get default build variables from source tree.  Variables are
7975863Snate@binkert.org        # normally determined by name of $VARIANT_DIR, but can be
7982632Sstever@eecs.umich.edu        # overriden by 'default=' arg on command line.
7995863Snate@binkert.org        default_vars_file = joinpath('build_opts',
8005863Snate@binkert.org                                     ARGUMENTS.get('default', variant_dir))
8015863Snate@binkert.org        if isfile(default_vars_file):
8025863Snate@binkert.org            sticky_vars.files.append(default_vars_file)
8035863Snate@binkert.org            print "Variables file %s not found,\n  using defaults in %s" \
8045863Snate@binkert.org                  % (current_vars_file, default_vars_file)
8052632Sstever@eecs.umich.edu        else:
8065863Snate@binkert.org            print "Error: cannot find variables file %s or %s" \
8075863Snate@binkert.org                  % (current_vars_file, default_vars_file)
8082632Sstever@eecs.umich.edu            Exit(1)
8091888SN/A
8105863Snate@binkert.org    # Apply current variable settings to env
8115863Snate@binkert.org    sticky_vars.Update(env)
8125863Snate@binkert.org    nonsticky_vars.Update(env)
8131858SN/A
8145863Snate@binkert.org    help_text += "\nSticky variables for %s:\n" % variant_dir \
8155863Snate@binkert.org                 + sticky_vars.GenerateHelpText(env) \
8165863Snate@binkert.org                 + "\nNon-sticky variables for %s:\n" % variant_dir \
8175863Snate@binkert.org                 + nonsticky_vars.GenerateHelpText(env)
8182598SN/A
8195863Snate@binkert.org    # Process variable settings.
8201858SN/A
8211858SN/A    if not have_fenv and env['USE_FENV']:
8221858SN/A        print "Warning: <fenv.h> not available; " \
8235863Snate@binkert.org              "forcing USE_FENV to False in", variant_dir + "."
8241858SN/A        env['USE_FENV'] = False
8251858SN/A
8261858SN/A    if not env['USE_FENV']:
8275863Snate@binkert.org        print "Warning: No IEEE FP rounding mode control in", variant_dir + "."
8281871SN/A        print "         FP results may deviate slightly from other platforms."
8291858SN/A
8301858SN/A    if env['EFENCE']:
8311858SN/A        env.Append(LIBS=['efence'])
8321858SN/A
8331858SN/A    if env['USE_MYSQL']:
8341858SN/A        if not have_mysql:
8351858SN/A            print "Warning: MySQL not available; " \
8365863Snate@binkert.org                  "forcing USE_MYSQL to False in", variant_dir + "."
8371858SN/A            env['USE_MYSQL'] = False
8381858SN/A        else:
8395863Snate@binkert.org            print "Compiling in", variant_dir, "with MySQL support."
8401859SN/A            env.ParseConfig(mysql_config_libs)
8411859SN/A            env.ParseConfig(mysql_config_include)
8421869SN/A
8435863Snate@binkert.org    # Save sticky variable settings back to current variables file
8445863Snate@binkert.org    sticky_vars.Save(current_vars_file, env)
8451869SN/A
8461965SN/A    if env['USE_SSE2']:
8471965SN/A        env.Append(CCFLAGS='-msse2')
8481965SN/A
8492761Sstever@eecs.umich.edu    # The src/SConscript file sets up the build rules in 'env' according
8505863Snate@binkert.org    # to the configured variables.  It returns a list of environments,
8511869SN/A    # one for each variant build (debug, opt, etc.)
8525863Snate@binkert.org    envList = SConscript('src/SConscript', variant_dir = variant_path,
8532667Sstever@eecs.umich.edu                         exports = 'env')
8541869SN/A
8551869SN/A    # Set up the regression tests for each build.
8562929Sktlim@umich.edu    for e in envList:
8572929Sktlim@umich.edu        SConscript('tests/SConscript',
8585863Snate@binkert.org                   variant_dir = joinpath(variant_path, 'tests', e.Label),
8592929Sktlim@umich.edu                   exports = { 'env' : e }, duplicate = False)
860955SN/A
8612598SN/AHelp(help_text)
8622598SN/A
8633546Sgblack@eecs.umich.edu
864955SN/A###################################################
865955SN/A#
866955SN/A# Let SCons do its thing.  At this point SCons will use the defined
8671530SN/A# build environments to build the requested targets.
868955SN/A#
869955SN/A###################################################
870955SN/A
871