SConstruct revision 6016
1955SN/A# -*- mode:python -*-
2955SN/A
39812Sandreas.hansson@arm.com# Copyright (c) 2009 The Hewlett-Packard Development Company
49812Sandreas.hansson@arm.com# Copyright (c) 2004-2005 The Regents of The University of Michigan
59812Sandreas.hansson@arm.com# All rights reserved.
69812Sandreas.hansson@arm.com#
79812Sandreas.hansson@arm.com# Redistribution and use in source and binary forms, with or without
89812Sandreas.hansson@arm.com# modification, are permitted provided that the following conditions are
99812Sandreas.hansson@arm.com# met: redistributions of source code must retain the above copyright
109812Sandreas.hansson@arm.com# notice, this list of conditions and the following disclaimer;
119812Sandreas.hansson@arm.com# redistributions in binary form must reproduce the above copyright
129812Sandreas.hansson@arm.com# notice, this list of conditions and the following disclaimer in the
139812Sandreas.hansson@arm.com# documentation and/or other materials provided with the distribution;
149812Sandreas.hansson@arm.com# neither the name of the copyright holders nor the names of its
157816Ssteve.reinhardt@amd.com# contributors may be used to endorse or promote products derived from
165871Snate@binkert.org# this software without specific prior written permission.
171762SN/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.
29955SN/A#
30955SN/A# Authors: Steve Reinhardt
31955SN/A#          Nathan Binkert
32955SN/A
33955SN/A###################################################
34955SN/A#
35955SN/A# SCons top-level build description (SConstruct) file.
36955SN/A#
37955SN/A# While in this directory ('m5'), just type 'scons' to build the default
38955SN/A# configuration (see below), or type 'scons build/<CONFIG>/<binary>'
39955SN/A# to build some other configuration (e.g., 'build/ALPHA_FS/m5.opt' for
40955SN/A# the optimized full-system version).
41955SN/A#
422665Ssaidi@eecs.umich.edu# You can build M5 in a different directory as long as there is a
432665Ssaidi@eecs.umich.edu# 'build/<CONFIG>' somewhere along the target path.  The build system
445863Snate@binkert.org# expects that all configs under the same build directory are being
45955SN/A# built for the same host system.
46955SN/A#
47955SN/A# Examples:
48955SN/A#
49955SN/A#   The following two commands are equivalent.  The '-u' option tells
508878Ssteve.reinhardt@amd.com#   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
528878Ssteve.reinhardt@amd.com#   % cd <path-to-src>/m5/build/ALPHA_FS; scons -u m5.debug
532632Sstever@eecs.umich.edu#
54955SN/A#   The following two commands are equivalent and demonstrate building
558878Ssteve.reinhardt@amd.com#   in a directory outside of the source tree.  The '-C' option tells
562632Sstever@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#
612761Sstever@eecs.umich.edu# You can use 'scons -H' to print scons options.  If you're in this
622761Sstever@eecs.umich.edu# 'm5' directory (or use -u or -C to tell scons where to find this
632761Sstever@eecs.umich.edu# file), you can use 'scons -h' to print all the M5-specific build
648878Ssteve.reinhardt@amd.com# options as well.
658878Ssteve.reinhardt@amd.com#
662761Sstever@eecs.umich.edu###################################################
672761Sstever@eecs.umich.edu
682761Sstever@eecs.umich.edu# Check for recent-enough Python and SCons versions.
692761Sstever@eecs.umich.edutry:
702761Sstever@eecs.umich.edu    # Really old versions of scons only take two options for the
718878Ssteve.reinhardt@amd.com    # function, so check once without the revision and once with the
728878Ssteve.reinhardt@amd.com    # revision, the first instance will fail for stuff other than
732632Sstever@eecs.umich.edu    # 0.98, and the second will fail for 0.98.0
742632Sstever@eecs.umich.edu    EnsureSConsVersion(0, 98)
758878Ssteve.reinhardt@amd.com    EnsureSConsVersion(0, 98, 1)
768878Ssteve.reinhardt@amd.comexcept SystemExit, e:
772632Sstever@eecs.umich.edu    print """
78955SN/AFor more details, see:
79955SN/A    http://m5sim.org/wiki/index.php/Compiling_M5
80955SN/A"""
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
928878Ssteve.reinhardt@amd.comon 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
969812Sandreas.hansson@arm.com"""
979812Sandreas.hansson@arm.com    raise
985863Snate@binkert.org
999812Sandreas.hansson@arm.comimport os
1005863Snate@binkert.orgimport re
1015863Snate@binkert.orgimport subprocess
1025863Snate@binkert.orgimport sys
1039812Sandreas.hansson@arm.com
1049812Sandreas.hansson@arm.comfrom os import mkdir, environ
1055863Snate@binkert.orgfrom os.path import abspath, basename, dirname, expanduser, normpath
1065863Snate@binkert.orgfrom os.path import exists,  isdir, isfile
1078878Ssteve.reinhardt@amd.comfrom os.path import join as joinpath, split as splitpath
1085863Snate@binkert.org
1095863Snate@binkert.orgimport SCons
1105863Snate@binkert.orgimport SCons.Node
1116654Snate@binkert.org
11210196SCurtis.Dunham@arm.comdef read_command(cmd, **kwargs):
113955SN/A    """run the command cmd, read the results and return them
1145396Ssaidi@eecs.umich.edu    this is sorta like `cmd` in shell"""
1155863Snate@binkert.org    from subprocess import Popen, PIPE, STDOUT
1165863Snate@binkert.org
1174202Sbinkertn@umich.edu    if isinstance(cmd, str):
1185863Snate@binkert.org        cmd = cmd.split()
1195863Snate@binkert.org
1205863Snate@binkert.org    no_exception = 'exception' in kwargs
1215863Snate@binkert.org    exception = kwargs.pop('exception', None)
122955SN/A    
1236654Snate@binkert.org    kwargs.setdefault('shell', False)
1245273Sstever@gmail.com    kwargs.setdefault('stdout', PIPE)
1255871Snate@binkert.org    kwargs.setdefault('stderr', STDOUT)
1265273Sstever@gmail.com    kwargs.setdefault('close_fds', True)
1276655Snate@binkert.org    try:
1288878Ssteve.reinhardt@amd.com        subp = Popen(cmd, **kwargs)
1296655Snate@binkert.org    except Exception, e:
1306655Snate@binkert.org        if no_exception:
1319219Spower.jg@gmail.com            return exception
1326655Snate@binkert.org        raise
1335871Snate@binkert.org
1346654Snate@binkert.org    return subp.communicate()[0]
1358947Sandreas.hansson@arm.com
1365396Ssaidi@eecs.umich.edu# helper function: compare arrays or strings of version numbers.
1378120Sgblack@eecs.umich.edu# E.g., compare_version((1,3,25), (1,4,1)')
1388120Sgblack@eecs.umich.edu# returns -1, 0, 1 if v1 is <, ==, > v2
1398120Sgblack@eecs.umich.edudef compare_versions(v1, v2):
1408120Sgblack@eecs.umich.edu    def make_version_list(v):
1418120Sgblack@eecs.umich.edu        if isinstance(v, (list,tuple)):
1428120Sgblack@eecs.umich.edu            return v
1438120Sgblack@eecs.umich.edu        elif isinstance(v, str):
1448120Sgblack@eecs.umich.edu            return map(lambda x: int(re.match('\d+', x).group()), v.split('.'))
1458879Ssteve.reinhardt@amd.com        else:
1468879Ssteve.reinhardt@amd.com            raise TypeError
1478879Ssteve.reinhardt@amd.com
1488879Ssteve.reinhardt@amd.com    v1 = make_version_list(v1)
1498879Ssteve.reinhardt@amd.com    v2 = make_version_list(v2)
1508879Ssteve.reinhardt@amd.com    # Compare corresponding elements of lists
1518879Ssteve.reinhardt@amd.com    for n1,n2 in zip(v1, v2):
1528879Ssteve.reinhardt@amd.com        if n1 < n2: return -1
1538879Ssteve.reinhardt@amd.com        if n1 > n2: return  1
1548879Ssteve.reinhardt@amd.com    # all corresponding values are equal... see if one has extra values
1558879Ssteve.reinhardt@amd.com    if len(v1) < len(v2): return -1
1568879Ssteve.reinhardt@amd.com    if len(v1) > len(v2): return  1
1578879Ssteve.reinhardt@amd.com    return 0
1588120Sgblack@eecs.umich.edu
1598120Sgblack@eecs.umich.edu########################################################################
1608120Sgblack@eecs.umich.edu#
1618120Sgblack@eecs.umich.edu# Set up the base build environment.
1628120Sgblack@eecs.umich.edu#
1638120Sgblack@eecs.umich.edu########################################################################
1648120Sgblack@eecs.umich.eduuse_vars = set([ 'AS', 'AR', 'CC', 'CXX', 'HOME', 'LD_LIBRARY_PATH', 'PATH',
1658120Sgblack@eecs.umich.edu                 'RANLIB' ])
1668120Sgblack@eecs.umich.edu
1678120Sgblack@eecs.umich.eduuse_env = {}
1688120Sgblack@eecs.umich.edufor key,val in os.environ.iteritems():
1698120Sgblack@eecs.umich.edu    if key in use_vars or key.startswith("M5"):
1708120Sgblack@eecs.umich.edu        use_env[key] = val
1718120Sgblack@eecs.umich.edu
1728879Ssteve.reinhardt@amd.comenv = Environment(ENV=use_env)
1738879Ssteve.reinhardt@amd.comenv.root = Dir(".")          # The current directory (where this file lives).
1748879Ssteve.reinhardt@amd.comenv.srcdir = Dir("src")      # The source directory
1758879Ssteve.reinhardt@amd.com
17610458Sandreas.hansson@arm.com########################################################################
17710458Sandreas.hansson@arm.com#
17810458Sandreas.hansson@arm.com# Mercurial Stuff.
1798879Ssteve.reinhardt@amd.com#
1808879Ssteve.reinhardt@amd.com# If the M5 directory is a mercurial repository, we should do some
1818879Ssteve.reinhardt@amd.com# extra things.
1828879Ssteve.reinhardt@amd.com#
1839227Sandreas.hansson@arm.com########################################################################
1849227Sandreas.hansson@arm.com
1858879Ssteve.reinhardt@amd.comhgdir = env.root.Dir(".hg")
1868879Ssteve.reinhardt@amd.com
1878879Ssteve.reinhardt@amd.commercurial_style_message = """
1888879Ssteve.reinhardt@amd.comYou're missing the M5 style hook.
18910453SAndrew.Bardsley@arm.comPlease install the hook so we can ensure that all code fits a common style.
19010453SAndrew.Bardsley@arm.com
19110453SAndrew.Bardsley@arm.comAll you'd need to do is add the following lines to your repository .hg/hgrc
19210456SCurtis.Dunham@arm.comor your personal .hgrc
19310456SCurtis.Dunham@arm.com----------------
19410456SCurtis.Dunham@arm.com
19510457Sandreas.hansson@arm.com[extensions]
19610457Sandreas.hansson@arm.comstyle = %s/util/style.py
1978120Sgblack@eecs.umich.edu
1988947Sandreas.hansson@arm.com[hooks]
1997816Ssteve.reinhardt@amd.compretxncommit.style = python:style.check_whitespace
2005871Snate@binkert.org""" % (env.root)
2015871Snate@binkert.org
2026121Snate@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
2059926Sstan.czerniawski@arm.comthis makes error messages more difficult to collect.  Please consider
2069926Sstan.czerniawski@arm.cominstalling mercurial if you choose to post an error message
2079119Sandreas.hansson@arm.com"""
20810068Sandreas.hansson@arm.com
20910068Sandreas.hansson@arm.commercurial_lib_not_found = """
210955SN/AMercurial libraries cannot be found, ignoring style hook
2119416SAndreas.Sandberg@ARM.comIf you are actually a M5 developer, please fix this and
2129416SAndreas.Sandberg@ARM.comrun the style hook. It is important.
2139416SAndreas.Sandberg@ARM.com"""
2149416SAndreas.Sandberg@ARM.com
2159416SAndreas.Sandberg@ARM.comhg_info = "Unknown"
2169416SAndreas.Sandberg@ARM.comif hgdir.exists():
2179416SAndreas.Sandberg@ARM.com    # 1) Grab repository revision if we know it.
2185871Snate@binkert.org    cmd = "hg id -n -i -t -b"
21910584Sandreas.hansson@arm.com    try:
2209416SAndreas.Sandberg@ARM.com        hg_info = read_command(cmd, cwd=env.root.abspath).strip()
2219416SAndreas.Sandberg@ARM.com    except OSError:
2225871Snate@binkert.org        print mercurial_bin_not_found
223955SN/A
2246121Snate@binkert.org    # 2) Ensure that the style hook is in place.
2258881Smarc.orr@gmail.com    try:
2266121Snate@binkert.org        ui = None
2276121Snate@binkert.org        if ARGUMENTS.get('IGNORE_STYLE') != 'True':
2281533SN/A            from mercurial import ui
2299239Sandreas.hansson@arm.com            ui = ui.ui()
2309239Sandreas.hansson@arm.com    except ImportError:
2319239Sandreas.hansson@arm.com        print mercurial_lib_not_found
2329239Sandreas.hansson@arm.com
2339239Sandreas.hansson@arm.com    if ui is not None:
2349239Sandreas.hansson@arm.com        ui.readconfig(hgdir.File('hgrc').abspath)
2359239Sandreas.hansson@arm.com        style_hook = ui.config('hooks', 'pretxncommit.style', None)
2369239Sandreas.hansson@arm.com
2379239Sandreas.hansson@arm.com        if not style_hook:
2389239Sandreas.hansson@arm.com            print mercurial_style_message
2399239Sandreas.hansson@arm.com            sys.exit(1)
2409239Sandreas.hansson@arm.comelse:
2416655Snate@binkert.org    print ".hg directory not found"
2426655Snate@binkert.orgenv['HG_INFO'] = hg_info
2436655Snate@binkert.org
2446655Snate@binkert.org###################################################
2455871Snate@binkert.org#
2465871Snate@binkert.org# Figure out which configurations to set up based on the path(s) of
2475863Snate@binkert.org# the target(s).
2485871Snate@binkert.org#
2498878Ssteve.reinhardt@amd.com###################################################
2505871Snate@binkert.org
2515871Snate@binkert.org# Find default configuration & binary.
2525871Snate@binkert.orgDefault(environ.get('M5_DEFAULT_BINARY', 'build/ALPHA_SE/m5.debug'))
2535863Snate@binkert.org
2546121Snate@binkert.org# helper function: find last occurrence of element in list
2555863Snate@binkert.orgdef rfind(l, elt, offs = -1):
2565871Snate@binkert.org    for i in range(len(l)+offs, 0, -1):
2578336Ssteve.reinhardt@amd.com        if l[i] == elt:
2588336Ssteve.reinhardt@amd.com            return i
2598336Ssteve.reinhardt@amd.com    raise ValueError, "element not found"
2608336Ssteve.reinhardt@amd.com
2614678Snate@binkert.org# Each target must have 'build' in the interior of the path; the
2628336Ssteve.reinhardt@amd.com# directory below this will determine the build parameters.  For
2638336Ssteve.reinhardt@amd.com# example, for target 'foo/bar/build/ALPHA_SE/arch/alpha/blah.do' we
2648336Ssteve.reinhardt@amd.com# recognize that ALPHA_SE specifies the configuration because it
2654678Snate@binkert.org# follow 'build' in the bulid path.
2664678Snate@binkert.org
2674678Snate@binkert.org# Generate absolute paths to targets so we can see where the build dir is
2684678Snate@binkert.orgif COMMAND_LINE_TARGETS:
2697827Snate@binkert.org    # Ask SCons which directory it was invoked from
2707827Snate@binkert.org    launch_dir = GetLaunchDir()
2718336Ssteve.reinhardt@amd.com    # Make targets relative to invocation directory
2724678Snate@binkert.org    abs_targets = [ normpath(joinpath(launch_dir, str(x))) for x in \
2738336Ssteve.reinhardt@amd.com                    COMMAND_LINE_TARGETS]
2748336Ssteve.reinhardt@amd.comelse:
2758336Ssteve.reinhardt@amd.com    # Default targets are relative to root of tree
2768336Ssteve.reinhardt@amd.com    abs_targets = [ normpath(joinpath(ROOT, str(x))) for x in \
2778336Ssteve.reinhardt@amd.com                    DEFAULT_TARGETS]
2788336Ssteve.reinhardt@amd.com
2795871Snate@binkert.org
2805871Snate@binkert.org# Generate a list of the unique build roots and configs that the
2818336Ssteve.reinhardt@amd.com# collected targets reference.
2828336Ssteve.reinhardt@amd.comvariant_paths = []
2838336Ssteve.reinhardt@amd.combuild_root = None
2848336Ssteve.reinhardt@amd.comfor t in abs_targets:
2858336Ssteve.reinhardt@amd.com    path_dirs = t.split('/')
2865871Snate@binkert.org    try:
2878336Ssteve.reinhardt@amd.com        build_top = rfind(path_dirs, 'build', -2)
2888336Ssteve.reinhardt@amd.com    except:
2898336Ssteve.reinhardt@amd.com        print "Error: no non-leaf 'build' dir found on target path", t
2908336Ssteve.reinhardt@amd.com        Exit(1)
2918336Ssteve.reinhardt@amd.com    this_build_root = joinpath('/',*path_dirs[:build_top+1])
2924678Snate@binkert.org    if not build_root:
2935871Snate@binkert.org        build_root = this_build_root
2944678Snate@binkert.org    else:
2958336Ssteve.reinhardt@amd.com        if this_build_root != build_root:
2968336Ssteve.reinhardt@amd.com            print "Error: build targets not under same build root\n"\
2978336Ssteve.reinhardt@amd.com                  "  %s\n  %s" % (build_root, this_build_root)
2988336Ssteve.reinhardt@amd.com            Exit(1)
2998336Ssteve.reinhardt@amd.com    variant_path = joinpath('/',*path_dirs[:build_top+2])
3008336Ssteve.reinhardt@amd.com    if variant_path not in variant_paths:
3018336Ssteve.reinhardt@amd.com        variant_paths.append(variant_path)
3028336Ssteve.reinhardt@amd.com
3038336Ssteve.reinhardt@amd.com# Make sure build_root exists (might not if this is the first build there)
3048336Ssteve.reinhardt@amd.comif not isdir(build_root):
3058336Ssteve.reinhardt@amd.com    mkdir(build_root)
3068336Ssteve.reinhardt@amd.com
3078336Ssteve.reinhardt@amd.comExport('env')
3088336Ssteve.reinhardt@amd.com
3098336Ssteve.reinhardt@amd.comenv.SConsignFile(joinpath(build_root, "sconsign"))
3108336Ssteve.reinhardt@amd.com
3118336Ssteve.reinhardt@amd.com# Default duplicate option is to use hard links, but this messes up
3125871Snate@binkert.org# when you use emacs to edit a file in the target dir, as emacs moves
3136121Snate@binkert.org# file to file~ then copies to file, breaking the link.  Symbolic
314955SN/A# (soft) links work better.
315955SN/Aenv.SetOption('duplicate', 'soft-copy')
3162632Sstever@eecs.umich.edu
3172632Sstever@eecs.umich.edu#
318955SN/A# Set up global sticky variables... these are common to an entire build
319955SN/A# tree (not specific to a particular build like ALPHA_SE)
320955SN/A#
321955SN/A
3228878Ssteve.reinhardt@amd.com# Variable validators & converters for global sticky variables
323955SN/Adef PathListMakeAbsolute(val):
3242632Sstever@eecs.umich.edu    if not val:
3252632Sstever@eecs.umich.edu        return val
3262632Sstever@eecs.umich.edu    f = lambda p: abspath(expanduser(p))
3272632Sstever@eecs.umich.edu    return ':'.join(map(f, val.split(':')))
3282632Sstever@eecs.umich.edu
3292632Sstever@eecs.umich.edudef PathListAllExist(key, val, env):
3302632Sstever@eecs.umich.edu    if not val:
3318268Ssteve.reinhardt@amd.com        return
3328268Ssteve.reinhardt@amd.com    paths = val.split(':')
3338268Ssteve.reinhardt@amd.com    for path in paths:
3348268Ssteve.reinhardt@amd.com        if not isdir(path):
3358268Ssteve.reinhardt@amd.com            raise SCons.Errors.UserError("Path does not exist: '%s'" % path)
3368268Ssteve.reinhardt@amd.com
3378268Ssteve.reinhardt@amd.comglobal_sticky_vars_file = joinpath(build_root, 'variables.global')
3382632Sstever@eecs.umich.edu
3392632Sstever@eecs.umich.eduglobal_sticky_vars = Variables(global_sticky_vars_file, args=ARGUMENTS)
3402632Sstever@eecs.umich.edu
3412632Sstever@eecs.umich.eduglobal_sticky_vars.AddVariables(
3428268Ssteve.reinhardt@amd.com    ('CC', 'C compiler', environ.get('CC', env['CC'])),
3432632Sstever@eecs.umich.edu    ('CXX', 'C++ compiler', environ.get('CXX', env['CXX'])),
3448268Ssteve.reinhardt@amd.com    ('BATCH', 'Use batch pool for build and tests', False),
3458268Ssteve.reinhardt@amd.com    ('BATCH_CMD', 'Batch pool submission command name', 'qdo'),
3468268Ssteve.reinhardt@amd.com    ('EXTRAS', 'Add Extra directories to the compilation', '',
3478268Ssteve.reinhardt@amd.com     PathListAllExist, PathListMakeAbsolute)
3483718Sstever@eecs.umich.edu    )    
3492634Sstever@eecs.umich.edu
3502634Sstever@eecs.umich.edu# base help text
3515863Snate@binkert.orghelp_text = '''
3522638Sstever@eecs.umich.eduUsage: scons [scons options] [build options] [target(s)]
3538268Ssteve.reinhardt@amd.com
3542632Sstever@eecs.umich.eduGlobal sticky options:
3552632Sstever@eecs.umich.edu'''
3562632Sstever@eecs.umich.edu
3572632Sstever@eecs.umich.eduhelp_text += global_sticky_vars.GenerateHelpText(env)
3582632Sstever@eecs.umich.edu
3591858SN/A# Update env with values from ARGUMENTS & file global_sticky_vars_file
3603716Sstever@eecs.umich.eduglobal_sticky_vars.Update(env)
3612638Sstever@eecs.umich.edu
3622638Sstever@eecs.umich.edu# Save sticky variable settings back to current variables file
3632638Sstever@eecs.umich.eduglobal_sticky_vars.Save(global_sticky_vars_file, env)
3642638Sstever@eecs.umich.edu
3652638Sstever@eecs.umich.edu# Parse EXTRAS variable to build list of all directories where we're
3662638Sstever@eecs.umich.edu# look for sources etc.  This list is exported as base_dir_list.
3672638Sstever@eecs.umich.edubase_dir = env.srcdir.abspath
3685863Snate@binkert.orgif env['EXTRAS']:
3695863Snate@binkert.org    extras_dir_list = env['EXTRAS'].split(':')
3705863Snate@binkert.orgelse:
371955SN/A    extras_dir_list = []
3725341Sstever@gmail.com
3735341Sstever@gmail.comExport('base_dir')
3745863Snate@binkert.orgExport('extras_dir_list')
3757756SAli.Saidi@ARM.com
3765341Sstever@gmail.com# M5_PLY is used by isa_parser.py to find the PLY package.
3776121Snate@binkert.orgenv.Append(ENV = { 'M5_PLY' : Dir('ext/ply').abspath })
3784494Ssaidi@eecs.umich.edu
3796121Snate@binkert.orgCXX_version = read_command([env['CXX'],'--version'], exception=False)
3801105SN/ACXX_V = read_command([env['CXX'],'-V'], exception=False)
3812667Sstever@eecs.umich.edu
3822667Sstever@eecs.umich.eduenv['GCC'] = CXX_version and CXX_version.find('g++') >= 0
3832667Sstever@eecs.umich.eduenv['SUNCC'] = CXX_V and CXX_V.find('Sun C++') >= 0
3842667Sstever@eecs.umich.eduenv['ICC'] = CXX_V and CXX_V.find('Intel') >= 0
3856121Snate@binkert.orgif env['GCC'] + env['SUNCC'] + env['ICC'] > 1:
3862667Sstever@eecs.umich.edu    print 'Error: How can we have two at the same time?'
3875341Sstever@gmail.com    Exit(1)
3885863Snate@binkert.org
3895341Sstever@gmail.com# Set up default C++ compiler flags
3905341Sstever@gmail.comif env['GCC']:
3915341Sstever@gmail.com    env.Append(CCFLAGS='-pipe')
3928120Sgblack@eecs.umich.edu    env.Append(CCFLAGS='-fno-strict-aliasing')
3935341Sstever@gmail.com    env.Append(CCFLAGS=Split('-Wall -Wno-sign-compare -Werror -Wundef'))
3948120Sgblack@eecs.umich.edu    env.Append(CXXFLAGS='-Wno-deprecated')
3955341Sstever@gmail.comelif env['ICC']:
3968120Sgblack@eecs.umich.edu    pass #Fix me... add warning flags once we clean up icc warnings
3976121Snate@binkert.orgelif env['SUNCC']:
3986121Snate@binkert.org    env.Append(CCFLAGS='-Qoption ccfe')
3998980Ssteve.reinhardt@amd.com    env.Append(CCFLAGS='-features=gcc')
4009396Sandreas.hansson@arm.com    env.Append(CCFLAGS='-features=extensions')
4015397Ssaidi@eecs.umich.edu    env.Append(CCFLAGS='-library=stlport4')
4025397Ssaidi@eecs.umich.edu    env.Append(CCFLAGS='-xar')
4037727SAli.Saidi@ARM.com    #env.Append(CCFLAGS='-instances=semiexplicit')
4048268Ssteve.reinhardt@amd.comelse:
4056168Snate@binkert.org    print 'Error: Don\'t know what compiler options to use for your compiler.'
4065341Sstever@gmail.com    print '       Please fix SConstruct and src/SConscript and try again.'
4078120Sgblack@eecs.umich.edu    Exit(1)
4088120Sgblack@eecs.umich.edu
4098120Sgblack@eecs.umich.edu# Do this after we save setting back, or else we'll tack on an
4106814Sgblack@eecs.umich.edu# extra 'qdo' every time we run scons.
4115863Snate@binkert.orgif env['BATCH']:
4128120Sgblack@eecs.umich.edu    env['CC']     = env['BATCH_CMD'] + ' ' + env['CC']
4135341Sstever@gmail.com    env['CXX']    = env['BATCH_CMD'] + ' ' + env['CXX']
4145863Snate@binkert.org    env['AS']     = env['BATCH_CMD'] + ' ' + env['AS']
4158268Ssteve.reinhardt@amd.com    env['AR']     = env['BATCH_CMD'] + ' ' + env['AR']
4166121Snate@binkert.org    env['RANLIB'] = env['BATCH_CMD'] + ' ' + env['RANLIB']
4176121Snate@binkert.org
4188268Ssteve.reinhardt@amd.comif sys.platform == 'cygwin':
4195742Snate@binkert.org    # cygwin has some header file issues...
4205742Snate@binkert.org    env.Append(CCFLAGS=Split("-Wno-uninitialized"))
4215341Sstever@gmail.comenv.Append(CPPPATH=[Dir('ext/dnet')])
4225742Snate@binkert.org
4235742Snate@binkert.org# Check for SWIG
4245341Sstever@gmail.comif not env.has_key('SWIG'):
4256017Snate@binkert.org    print 'Error: SWIG utility not found.'
4266121Snate@binkert.org    print '       Please install (see http://www.swig.org) and retry.'
4276017Snate@binkert.org    Exit(1)
4287816Ssteve.reinhardt@amd.com
4297756SAli.Saidi@ARM.com# Check for appropriate SWIG version
4307756SAli.Saidi@ARM.comswig_version = read_command(('swig', '-version'), exception='').split()
4317756SAli.Saidi@ARM.com# First 3 words should be "SWIG Version x.y.z"
4327756SAli.Saidi@ARM.comif len(swig_version) < 3 or \
4337756SAli.Saidi@ARM.com        swig_version[0] != 'SWIG' or swig_version[1] != 'Version':
4347756SAli.Saidi@ARM.com    print 'Error determining SWIG version.'
4357756SAli.Saidi@ARM.com    Exit(1)
4367756SAli.Saidi@ARM.com
4377816Ssteve.reinhardt@amd.commin_swig_version = '1.3.28'
4387816Ssteve.reinhardt@amd.comif compare_versions(swig_version[2], min_swig_version) < 0:
4397816Ssteve.reinhardt@amd.com    print 'Error: SWIG version', min_swig_version, 'or newer required.'
4407816Ssteve.reinhardt@amd.com    print '       Installed version:', swig_version[2]
4417816Ssteve.reinhardt@amd.com    Exit(1)
4427816Ssteve.reinhardt@amd.com
4437816Ssteve.reinhardt@amd.com# Set up SWIG flags & scanner
4447816Ssteve.reinhardt@amd.comswig_flags=Split('-c++ -python -modern -templatereduce $_CPPINCFLAGS')
4457816Ssteve.reinhardt@amd.comenv.Append(SWIGFLAGS=swig_flags)
4467816Ssteve.reinhardt@amd.com
4477756SAli.Saidi@ARM.com# filter out all existing swig scanners, they mess up the dependency
4487816Ssteve.reinhardt@amd.com# stuff for some reason
4497816Ssteve.reinhardt@amd.comscanners = []
4507816Ssteve.reinhardt@amd.comfor scanner in env['SCANNERS']:
4517816Ssteve.reinhardt@amd.com    skeys = scanner.skeys
4527816Ssteve.reinhardt@amd.com    if skeys == '.i':
4537816Ssteve.reinhardt@amd.com        continue
4547816Ssteve.reinhardt@amd.com
4557816Ssteve.reinhardt@amd.com    if isinstance(skeys, (list, tuple)) and '.i' in skeys:
4567816Ssteve.reinhardt@amd.com        continue
4577816Ssteve.reinhardt@amd.com
4587816Ssteve.reinhardt@amd.com    scanners.append(scanner)
4597816Ssteve.reinhardt@amd.com
4607816Ssteve.reinhardt@amd.com# add the new swig scanner that we like better
4617816Ssteve.reinhardt@amd.comfrom SCons.Scanner import ClassicCPP as CPPScanner
4627816Ssteve.reinhardt@amd.comswig_inc_re = '^[ \t]*[%,#][ \t]*(?:include|import)[ \t]*(<|")([^>"]+)(>|")'
4637816Ssteve.reinhardt@amd.comscanners.append(CPPScanner("SwigScan", [ ".i" ], "CPPPATH", swig_inc_re))
4647816Ssteve.reinhardt@amd.com
4657816Ssteve.reinhardt@amd.com# replace the scanners list that has what we want
4667816Ssteve.reinhardt@amd.comenv['SCANNERS'] = scanners
4677816Ssteve.reinhardt@amd.com
4687816Ssteve.reinhardt@amd.com# Add a custom Check function to the Configure context so that we can
4697816Ssteve.reinhardt@amd.com# figure out if the compiler adds leading underscores to global
4707816Ssteve.reinhardt@amd.com# variables.  This is needed for the autogenerated asm files that we
4717816Ssteve.reinhardt@amd.com# use for embedding the python code.
4727816Ssteve.reinhardt@amd.comdef CheckLeading(context):
4737816Ssteve.reinhardt@amd.com    context.Message("Checking for leading underscore in global variables...")
4747816Ssteve.reinhardt@amd.com    # 1) Define a global variable called x from asm so the C compiler
4757816Ssteve.reinhardt@amd.com    #    won't change the symbol at all.
4767816Ssteve.reinhardt@amd.com    # 2) Declare that variable.
4777816Ssteve.reinhardt@amd.com    # 3) Use the variable
4787816Ssteve.reinhardt@amd.com    #
4797816Ssteve.reinhardt@amd.com    # If the compiler prepends an underscore, this will successfully
4807816Ssteve.reinhardt@amd.com    # link because the external symbol 'x' will be called '_x' which
4817816Ssteve.reinhardt@amd.com    # was defined by the asm statement.  If the compiler does not
4827816Ssteve.reinhardt@amd.com    # prepend an underscore, this will not successfully link because
4837816Ssteve.reinhardt@amd.com    # '_x' will have been defined by assembly, while the C portion of
4847816Ssteve.reinhardt@amd.com    # the code will be trying to use 'x'
4857816Ssteve.reinhardt@amd.com    ret = context.TryLink('''
4867816Ssteve.reinhardt@amd.com        asm(".globl _x; _x: .byte 0");
4877816Ssteve.reinhardt@amd.com        extern int x;
4887816Ssteve.reinhardt@amd.com        int main() { return x; }
4897816Ssteve.reinhardt@amd.com        ''', extension=".c")
4907816Ssteve.reinhardt@amd.com    context.env.Append(LEADING_UNDERSCORE=ret)
4917816Ssteve.reinhardt@amd.com    context.Result(ret)
4927816Ssteve.reinhardt@amd.com    return ret
4937816Ssteve.reinhardt@amd.com
4947816Ssteve.reinhardt@amd.com# Platform-specific configuration.  Note again that we assume that all
4957816Ssteve.reinhardt@amd.com# builds under a given build root run on the same host platform.
4967816Ssteve.reinhardt@amd.comconf = Configure(env,
4977816Ssteve.reinhardt@amd.com                 conf_dir = joinpath(build_root, '.scons_config'),
4987816Ssteve.reinhardt@amd.com                 log_file = joinpath(build_root, 'scons_config.log'),
4997816Ssteve.reinhardt@amd.com                 custom_tests = { 'CheckLeading' : CheckLeading })
5007816Ssteve.reinhardt@amd.com
5017816Ssteve.reinhardt@amd.com# Check for leading underscores.  Don't really need to worry either
5027816Ssteve.reinhardt@amd.com# way so don't need to check the return code.
5037816Ssteve.reinhardt@amd.comconf.CheckLeading()
5047816Ssteve.reinhardt@amd.com
5057816Ssteve.reinhardt@amd.com# Check if we should compile a 64 bit binary on Mac OS X/Darwin
5067816Ssteve.reinhardt@amd.comtry:
5077816Ssteve.reinhardt@amd.com    import platform
5087816Ssteve.reinhardt@amd.com    uname = platform.uname()
5098947Sandreas.hansson@arm.com    if uname[0] == 'Darwin' and compare_versions(uname[2], '9.0.0') >= 0:
5108947Sandreas.hansson@arm.com        if int(read_command('sysctl -n hw.cpu64bit_capable')[0]):
5117756SAli.Saidi@ARM.com            env.Append(CCFLAGS='-arch x86_64')
5128120Sgblack@eecs.umich.edu            env.Append(CFLAGS='-arch x86_64')
5137756SAli.Saidi@ARM.com            env.Append(LINKFLAGS='-arch x86_64')
5147756SAli.Saidi@ARM.com            env.Append(ASFLAGS='-arch x86_64')
5157756SAli.Saidi@ARM.comexcept:
5167756SAli.Saidi@ARM.com    pass
5177816Ssteve.reinhardt@amd.com
5187816Ssteve.reinhardt@amd.com# Recent versions of scons substitute a "Null" object for Configure()
5197816Ssteve.reinhardt@amd.com# when configuration isn't necessary, e.g., if the "--help" option is
5207816Ssteve.reinhardt@amd.com# present.  Unfortuantely this Null object always returns false,
5217816Ssteve.reinhardt@amd.com# breaking all our configuration checks.  We replace it with our own
5227816Ssteve.reinhardt@amd.com# more optimistic null object that returns True instead.
5237816Ssteve.reinhardt@amd.comif not conf:
5247816Ssteve.reinhardt@amd.com    def NullCheck(*args, **kwargs):
5257816Ssteve.reinhardt@amd.com        return True
5267816Ssteve.reinhardt@amd.com
5277756SAli.Saidi@ARM.com    class NullConf:
5287756SAli.Saidi@ARM.com        def __init__(self, env):
5299227Sandreas.hansson@arm.com            self.env = env
5309227Sandreas.hansson@arm.com        def Finish(self):
5319227Sandreas.hansson@arm.com            return self.env
5329227Sandreas.hansson@arm.com        def __getattr__(self, mname):
5339590Sandreas@sandberg.pp.se            return NullCheck
5349590Sandreas@sandberg.pp.se
5359590Sandreas@sandberg.pp.se    conf = NullConf(env)
5369590Sandreas@sandberg.pp.se
5379590Sandreas@sandberg.pp.se# Find Python include and library directories for embedding the
5389590Sandreas@sandberg.pp.se# interpreter.  For consistency, we will use the same Python
5396654Snate@binkert.org# installation used to run scons (and thus this script).  If you want
5406654Snate@binkert.org# to link in an alternate version, see above for instructions on how
5415871Snate@binkert.org# to invoke scons with a different copy of the Python interpreter.
5426121Snate@binkert.orgfrom distutils import sysconfig
5438946Sandreas.hansson@arm.com
5449419Sandreas.hansson@arm.compy_getvar = sysconfig.get_config_var
5453940Ssaidi@eecs.umich.edu
5463918Ssaidi@eecs.umich.edupy_version = 'python' + py_getvar('VERSION')
5473918Ssaidi@eecs.umich.edu
5481858SN/Apy_general_include = sysconfig.get_python_inc()
5499556Sandreas.hansson@arm.compy_platform_include = sysconfig.get_python_inc(plat_specific=True)
5509556Sandreas.hansson@arm.compy_includes = [ py_general_include ]
5519556Sandreas.hansson@arm.comif py_platform_include != py_general_include:
5529556Sandreas.hansson@arm.com    py_includes.append(py_platform_include)
5539556Sandreas.hansson@arm.com
5549556Sandreas.hansson@arm.compy_lib_path = [ py_getvar('LIBDIR') ]
5559556Sandreas.hansson@arm.com# add the prefix/lib/pythonX.Y/config dir, but only if there is no
5569556Sandreas.hansson@arm.com# shared library in prefix/lib/.
5579556Sandreas.hansson@arm.comif not py_getvar('Py_ENABLE_SHARED'):
5589556Sandreas.hansson@arm.com    py_lib_path.append('-L' + py_getvar('LIBPL'))
5599556Sandreas.hansson@arm.com
5609556Sandreas.hansson@arm.compy_libs = []
5619556Sandreas.hansson@arm.comfor lib in py_getvar('LIBS').split() + py_getvar('SYSLIBS').split():
5629556Sandreas.hansson@arm.com    if lib not in py_libs:
5639556Sandreas.hansson@arm.com        py_libs.append(lib)
5649556Sandreas.hansson@arm.compy_libs.append('-l' + py_version)
5659556Sandreas.hansson@arm.com
5669556Sandreas.hansson@arm.comenv.Append(CPPPATH=py_includes)
5679556Sandreas.hansson@arm.comenv.Append(LIBPATH=py_lib_path)
5689556Sandreas.hansson@arm.com
5699556Sandreas.hansson@arm.com# verify that this stuff works
5709556Sandreas.hansson@arm.comif not conf.CheckHeader('Python.h', '<>'):
5719556Sandreas.hansson@arm.com    print "Error: can't find Python.h header in", py_includes
5729556Sandreas.hansson@arm.com    Exit(1)
5739556Sandreas.hansson@arm.com
5749556Sandreas.hansson@arm.comfor lib in py_libs:
5759556Sandreas.hansson@arm.com    assert lib.startswith('-l')
5769556Sandreas.hansson@arm.com    lib = lib[2:]
5779556Sandreas.hansson@arm.com    if not conf.CheckLib(lib):
5789556Sandreas.hansson@arm.com        print "Error: can't find library %s required by python" % lib
5799556Sandreas.hansson@arm.com        Exit(1)
5809556Sandreas.hansson@arm.com
5816121Snate@binkert.org# On Solaris you need to use libsocket for socket ops
58210238Sandreas.hansson@arm.comif not conf.CheckLibWithHeader(None, 'sys/socket.h', 'C++', 'accept(0,0,0);'):
58310238Sandreas.hansson@arm.com   if not conf.CheckLibWithHeader('socket', 'sys/socket.h', 'C++', 'accept(0,0,0);'):
58410238Sandreas.hansson@arm.com       print "Can't find library with socket calls (e.g. accept())"
58510238Sandreas.hansson@arm.com       Exit(1)
5869420Sandreas.hansson@arm.com
58710238Sandreas.hansson@arm.com# Check for zlib.  If the check passes, libz will be automatically
58810238Sandreas.hansson@arm.com# added to the LIBS environment variable.
5899420Sandreas.hansson@arm.comif not conf.CheckLibWithHeader('z', 'zlib.h', 'C++','zlibVersion();'):
5909420Sandreas.hansson@arm.com    print 'Error: did not find needed zlib compression library '\
5919420Sandreas.hansson@arm.com          'and/or zlib.h header file.'
5929420Sandreas.hansson@arm.com    print '       Please install zlib and try again.'
5939420Sandreas.hansson@arm.com    Exit(1)
59410264Sandreas.hansson@arm.com
59510264Sandreas.hansson@arm.com# Check for <fenv.h> (C99 FP environment control)
59610264Sandreas.hansson@arm.comhave_fenv = conf.CheckHeader('fenv.h', '<>')
59710264Sandreas.hansson@arm.comif not have_fenv:
59810264Sandreas.hansson@arm.com    print "Warning: Header file <fenv.h> not found."
59910264Sandreas.hansson@arm.com    print "         This host has no IEEE FP rounding mode control."
60010264Sandreas.hansson@arm.com
60110264Sandreas.hansson@arm.com######################################################################
60210264Sandreas.hansson@arm.com#
60310264Sandreas.hansson@arm.com# Check for mysql.
60410264Sandreas.hansson@arm.com#
60510264Sandreas.hansson@arm.commysql_config = WhereIs('mysql_config')
60610264Sandreas.hansson@arm.comhave_mysql = bool(mysql_config)
60710264Sandreas.hansson@arm.com
60810264Sandreas.hansson@arm.com# Check MySQL version.
60910264Sandreas.hansson@arm.comif have_mysql:
61010457Sandreas.hansson@arm.com    mysql_version = read_command(mysql_config + ' --version')
61110457Sandreas.hansson@arm.com    min_mysql_version = '4.1'
61210457Sandreas.hansson@arm.com    if compare_versions(mysql_version, min_mysql_version) < 0:
61310457Sandreas.hansson@arm.com        print 'Warning: MySQL', min_mysql_version, 'or newer required.'
61410457Sandreas.hansson@arm.com        print '         Version', mysql_version, 'detected.'
61510457Sandreas.hansson@arm.com        have_mysql = False
61610457Sandreas.hansson@arm.com
61710457Sandreas.hansson@arm.com# Set up mysql_config commands.
61810457Sandreas.hansson@arm.comif have_mysql:
61910238Sandreas.hansson@arm.com    mysql_config_include = mysql_config + ' --include'
62010238Sandreas.hansson@arm.com    if os.system(mysql_config_include + ' > /dev/null') != 0:
62110238Sandreas.hansson@arm.com        # older mysql_config versions don't support --include, use
62210238Sandreas.hansson@arm.com        # --cflags instead
62310238Sandreas.hansson@arm.com        mysql_config_include = mysql_config + ' --cflags | sed s/\\\'//g'
62410238Sandreas.hansson@arm.com    # This seems to work in all versions
62510416Sandreas.hansson@arm.com    mysql_config_libs = mysql_config + ' --libs'
62610238Sandreas.hansson@arm.com
6279227Sandreas.hansson@arm.com######################################################################
62810238Sandreas.hansson@arm.com#
62910416Sandreas.hansson@arm.com# Finish the configuration
63010416Sandreas.hansson@arm.com#
6319227Sandreas.hansson@arm.comenv = conf.Finish()
6329590Sandreas@sandberg.pp.se
6339590Sandreas@sandberg.pp.se######################################################################
6349590Sandreas@sandberg.pp.se#
6358737Skoansin.tan@gmail.com# Collect all non-global variables
63610238Sandreas.hansson@arm.com#
63710238Sandreas.hansson@arm.com
6389420Sandreas.hansson@arm.comExport('env')
6398737Skoansin.tan@gmail.com
64010106SMitch.Hayenga@arm.com# Define the universe of supported ISAs
6418737Skoansin.tan@gmail.comall_isa_list = [ ]
6428737Skoansin.tan@gmail.comExport('all_isa_list')
64310238Sandreas.hansson@arm.com
64410238Sandreas.hansson@arm.com# Define the universe of supported CPU models
6458737Skoansin.tan@gmail.comall_cpu_list = [ ]
6468737Skoansin.tan@gmail.comdefault_cpus = [ ]
6478737Skoansin.tan@gmail.comExport('all_cpu_list', 'default_cpus')
6488737Skoansin.tan@gmail.com
6498737Skoansin.tan@gmail.com# Sticky variables get saved in the variables file so they persist from
6508737Skoansin.tan@gmail.com# one invocation to the next (unless overridden, in which case the new
6519556Sandreas.hansson@arm.com# value becomes sticky).
6529556Sandreas.hansson@arm.comsticky_vars = Variables(args=ARGUMENTS)
6539556Sandreas.hansson@arm.comExport('sticky_vars')
6549556Sandreas.hansson@arm.com
6559556Sandreas.hansson@arm.com# Non-sticky variables only apply to the current build.
6569556Sandreas.hansson@arm.comnonsticky_vars = Variables(args=ARGUMENTS)
6579556Sandreas.hansson@arm.comExport('nonsticky_vars')
6589556Sandreas.hansson@arm.com
65910278SAndreas.Sandberg@ARM.com# Walk the tree and execute all SConsopts scripts that wil add to the
66010278SAndreas.Sandberg@ARM.com# above variables
66110278SAndreas.Sandberg@ARM.comfor bdir in [ base_dir ] + extras_dir_list:
66210278SAndreas.Sandberg@ARM.com    for root, dirs, files in os.walk(bdir):
66310278SAndreas.Sandberg@ARM.com        if 'SConsopts' in files:
66410278SAndreas.Sandberg@ARM.com            print "Reading", joinpath(root, 'SConsopts')
6659556Sandreas.hansson@arm.com            SConscript(joinpath(root, 'SConsopts'))
6669590Sandreas@sandberg.pp.se
6679590Sandreas@sandberg.pp.seall_isa_list.sort()
6689420Sandreas.hansson@arm.comall_cpu_list.sort()
6699846Sandreas.hansson@arm.comdefault_cpus.sort()
6709846Sandreas.hansson@arm.com
6719846Sandreas.hansson@arm.comsticky_vars.AddVariables(
6729846Sandreas.hansson@arm.com    EnumVariable('TARGET_ISA', 'Target ISA', 'alpha', all_isa_list),
6738946Sandreas.hansson@arm.com    BoolVariable('FULL_SYSTEM', 'Full-system support', False),
6743918Ssaidi@eecs.umich.edu    ListVariable('CPU_MODELS', 'CPU models', default_cpus, all_cpu_list),
6759068SAli.Saidi@ARM.com    BoolVariable('NO_FAST_ALLOC', 'Disable fast object allocator', False),
6769068SAli.Saidi@ARM.com    BoolVariable('FAST_ALLOC_DEBUG', 'Enable fast object allocator debugging',
6779068SAli.Saidi@ARM.com                 False),
6789068SAli.Saidi@ARM.com    BoolVariable('FAST_ALLOC_STATS', 'Enable fast object allocator statistics',
6799068SAli.Saidi@ARM.com                 False),
6809068SAli.Saidi@ARM.com    BoolVariable('EFENCE', 'Link with Electric Fence malloc debugger',
6819068SAli.Saidi@ARM.com                 False),
6829068SAli.Saidi@ARM.com    BoolVariable('SS_COMPATIBLE_FP',
6839068SAli.Saidi@ARM.com                 'Make floating-point results compatible with SimpleScalar',
6849419Sandreas.hansson@arm.com                 False),
6859068SAli.Saidi@ARM.com    BoolVariable('USE_SSE2',
6869068SAli.Saidi@ARM.com                 'Compile for SSE2 (-msse2) to get IEEE FP on x86 hosts',
6879068SAli.Saidi@ARM.com                 False),
6889068SAli.Saidi@ARM.com    BoolVariable('USE_MYSQL', 'Use MySQL for stats output', have_mysql),
6899068SAli.Saidi@ARM.com    BoolVariable('USE_FENV', 'Use <fenv.h> IEEE mode control', have_fenv),
6909068SAli.Saidi@ARM.com    BoolVariable('USE_CHECKER', 'Use checker for detailed CPU models', False),
6913918Ssaidi@eecs.umich.edu    BoolVariable('CP_ANNOTATE', 'Enable critical path annotation capability', False),
6923918Ssaidi@eecs.umich.edu    )
6936157Snate@binkert.org
6946157Snate@binkert.orgnonsticky_vars.AddVariables(
6956157Snate@binkert.org    BoolVariable('update_ref', 'Update test reference outputs', False)
6966157Snate@binkert.org    )
6975397Ssaidi@eecs.umich.edu
6985397Ssaidi@eecs.umich.edu# These variables get exported to #defines in config/*.hh (see src/SConscript).
6996121Snate@binkert.orgenv.ExportVariables = ['FULL_SYSTEM', 'ALPHA_TLASER', 'USE_FENV', \
7006121Snate@binkert.org                       'USE_MYSQL', 'NO_FAST_ALLOC', 'FAST_ALLOC_DEBUG', \
7016121Snate@binkert.org                       'FAST_ALLOC_STATS', 'SS_COMPATIBLE_FP', \
7026121Snate@binkert.org                       'USE_CHECKER', 'TARGET_ISA', 'CP_ANNOTATE']
7036121Snate@binkert.org
7046121Snate@binkert.org###################################################
7055397Ssaidi@eecs.umich.edu#
7061851SN/A# Define a SCons builder for configuration flag headers.
7071851SN/A#
7087739Sgblack@eecs.umich.edu###################################################
709955SN/A
7109396Sandreas.hansson@arm.com# This function generates a config header file that #defines the
7119396Sandreas.hansson@arm.com# variable symbol to the current variable setting (0 or 1).  The source
7129396Sandreas.hansson@arm.com# operands are the name of the variable and a Value node containing the
7139396Sandreas.hansson@arm.com# value of the variable.
7149396Sandreas.hansson@arm.comdef build_config_file(target, source, env):
7159396Sandreas.hansson@arm.com    (variable, value) = [s.get_contents() for s in source]
7169396Sandreas.hansson@arm.com    f = file(str(target[0]), 'w')
7179396Sandreas.hansson@arm.com    print >> f, '#define', variable, value
7189396Sandreas.hansson@arm.com    f.close()
7199396Sandreas.hansson@arm.com    return None
7209396Sandreas.hansson@arm.com
7219396Sandreas.hansson@arm.com# Generate the message to be printed when building the config file.
7229396Sandreas.hansson@arm.comdef build_config_file_string(target, source, env):
7239396Sandreas.hansson@arm.com    (variable, value) = [s.get_contents() for s in source]
7249396Sandreas.hansson@arm.com    return "Defining %s as %s in %s." % (variable, value, target[0])
7259396Sandreas.hansson@arm.com
7269477Sandreas.hansson@arm.com# Combine the two functions into a scons Action object.
7279477Sandreas.hansson@arm.comconfig_action = Action(build_config_file, build_config_file_string)
7289477Sandreas.hansson@arm.com
7299477Sandreas.hansson@arm.com# The emitter munges the source & target node lists to reflect what
7309477Sandreas.hansson@arm.com# we're really doing.
7319477Sandreas.hansson@arm.comdef config_emitter(target, source, env):
7329477Sandreas.hansson@arm.com    # extract variable name from Builder arg
7339477Sandreas.hansson@arm.com    variable = str(target[0])
7349477Sandreas.hansson@arm.com    # True target is config header file
7359477Sandreas.hansson@arm.com    target = joinpath('config', variable.lower() + '.hh')
7369477Sandreas.hansson@arm.com    val = env[variable]
7379477Sandreas.hansson@arm.com    if isinstance(val, bool):
7389477Sandreas.hansson@arm.com        # Force value to 0/1
7399477Sandreas.hansson@arm.com        val = int(val)
7409477Sandreas.hansson@arm.com    elif isinstance(val, str):
7419477Sandreas.hansson@arm.com        val = '"' + val + '"'
7429477Sandreas.hansson@arm.com
7439477Sandreas.hansson@arm.com    # Sources are variable name & value (packaged in SCons Value nodes)
7449477Sandreas.hansson@arm.com    return ([target], [Value(variable), Value(val)])
7459477Sandreas.hansson@arm.com
7469477Sandreas.hansson@arm.comconfig_builder = Builder(emitter = config_emitter, action = config_action)
7479477Sandreas.hansson@arm.com
7489396Sandreas.hansson@arm.comenv.Append(BUILDERS = { 'ConfigFile' : config_builder })
7493053Sstever@eecs.umich.edu
7506121Snate@binkert.org# libelf build is shared across all configs in the build root.
7513053Sstever@eecs.umich.eduenv.SConscript('ext/libelf/SConscript',
7523053Sstever@eecs.umich.edu               variant_dir = joinpath(build_root, 'libelf'))
7533053Sstever@eecs.umich.edu
7543053Sstever@eecs.umich.edu# gzstream build is shared across all configs in the build root.
7553053Sstever@eecs.umich.eduenv.SConscript('ext/gzstream/SConscript',
7569072Sandreas.hansson@arm.com               variant_dir = joinpath(build_root, 'gzstream'))
7573053Sstever@eecs.umich.edu
7584742Sstever@eecs.umich.edu###################################################
7594742Sstever@eecs.umich.edu#
7603053Sstever@eecs.umich.edu# This function is used to set up a directory with switching headers
7613053Sstever@eecs.umich.edu#
7623053Sstever@eecs.umich.edu###################################################
76310181SCurtis.Dunham@arm.com
7646654Snate@binkert.orgenv['ALL_ISA_LIST'] = all_isa_list
7653053Sstever@eecs.umich.edudef make_switching_dir(dname, switch_headers, env):
7663053Sstever@eecs.umich.edu    # Generate the header.  target[0] is the full path of the output
7673053Sstever@eecs.umich.edu    # header to generate.  'source' is a dummy variable, since we get the
7683053Sstever@eecs.umich.edu    # list of ISAs from env['ALL_ISA_LIST'].
76910425Sandreas.hansson@arm.com    def gen_switch_hdr(target, source, env):
77010425Sandreas.hansson@arm.com        fname = str(target[0])
77110425Sandreas.hansson@arm.com        bname = basename(fname)
77210425Sandreas.hansson@arm.com        f = open(fname, 'w')
77310425Sandreas.hansson@arm.com        f.write('#include "arch/isa_specific.hh"\n')
77410425Sandreas.hansson@arm.com        cond = '#if'
77510425Sandreas.hansson@arm.com        for isa in all_isa_list:
77610425Sandreas.hansson@arm.com            f.write('%s THE_ISA == %s_ISA\n#include "%s/%s/%s"\n'
77710425Sandreas.hansson@arm.com                    % (cond, isa.upper(), dname, isa, bname))
77810425Sandreas.hansson@arm.com            cond = '#elif'
77910425Sandreas.hansson@arm.com        f.write('#else\n#error "THE_ISA not set"\n#endif\n')
7802667Sstever@eecs.umich.edu        f.close()
7814554Sbinkertn@umich.edu        return 0
7826121Snate@binkert.org
7832667Sstever@eecs.umich.edu    # String to print when generating header
78410384SCurtis.Dunham@arm.com    def gen_switch_hdr_string(target, source, env):
78510384SCurtis.Dunham@arm.com        return "Generating switch header " + str(target[0])
78610384SCurtis.Dunham@arm.com
78710384SCurtis.Dunham@arm.com    # Build SCons Action object. 'varlist' specifies env vars that this
78810384SCurtis.Dunham@arm.com    # action depends on; when env['ALL_ISA_LIST'] changes these actions
7894554Sbinkertn@umich.edu    # should get re-executed.
7904554Sbinkertn@umich.edu    switch_hdr_action = Action(gen_switch_hdr, gen_switch_hdr_string,
7914554Sbinkertn@umich.edu                               varlist=['ALL_ISA_LIST'])
7926121Snate@binkert.org
7934554Sbinkertn@umich.edu    # Instantiate actions for each header
7944554Sbinkertn@umich.edu    for hdr in switch_headers:
7954554Sbinkertn@umich.edu        env.Command(hdr, [], switch_hdr_action)
7964781Snate@binkert.orgExport('make_switching_dir')
7974554Sbinkertn@umich.edu
7984554Sbinkertn@umich.edu###################################################
7992667Sstever@eecs.umich.edu#
8004554Sbinkertn@umich.edu# Define build environments for selected configurations.
8014554Sbinkertn@umich.edu#
8024554Sbinkertn@umich.edu###################################################
8034554Sbinkertn@umich.edu
8042667Sstever@eecs.umich.edu# rename base env
8054554Sbinkertn@umich.edubase_env = env
8062667Sstever@eecs.umich.edu
8074554Sbinkertn@umich.edufor variant_path in variant_paths:
8086121Snate@binkert.org    print "Building in", variant_path
8092667Sstever@eecs.umich.edu
8105522Snate@binkert.org    # Make a copy of the build-root environment to use for this config.
8115522Snate@binkert.org    env = base_env.Clone()
8125522Snate@binkert.org    env['BUILDDIR'] = variant_path
8135522Snate@binkert.org
8145522Snate@binkert.org    # variant_dir is the tail component of build path, and is used to
8155522Snate@binkert.org    # determine the build parameters (e.g., 'ALPHA_SE')
8165522Snate@binkert.org    (build_root, variant_dir) = splitpath(variant_path)
8175522Snate@binkert.org
8185522Snate@binkert.org    # Set env variables according to the build directory config.
8195522Snate@binkert.org    sticky_vars.files = []
8205522Snate@binkert.org    # Variables for $BUILD_ROOT/$VARIANT_DIR are stored in
8215522Snate@binkert.org    # $BUILD_ROOT/variables/$VARIANT_DIR so you can nuke
8225522Snate@binkert.org    # $BUILD_ROOT/$VARIANT_DIR without losing your variables settings.
8235522Snate@binkert.org    current_vars_file = joinpath(build_root, 'variables', variant_dir)
8245522Snate@binkert.org    if isfile(current_vars_file):
8255522Snate@binkert.org        sticky_vars.files.append(current_vars_file)
8265522Snate@binkert.org        print "Using saved variables file %s" % current_vars_file
8275522Snate@binkert.org    else:
8285522Snate@binkert.org        # Build dir-specific variables file doesn't exist.
8295522Snate@binkert.org
8305522Snate@binkert.org        # Make sure the directory is there so we can create it later
8315522Snate@binkert.org        opt_dir = dirname(current_vars_file)
8325522Snate@binkert.org        if not isdir(opt_dir):
8335522Snate@binkert.org            mkdir(opt_dir)
8345522Snate@binkert.org
8355522Snate@binkert.org        # Get default build variables from source tree.  Variables are
8369986Sandreas@sandberg.pp.se        # normally determined by name of $VARIANT_DIR, but can be
8379986Sandreas@sandberg.pp.se        # overriden by 'default=' arg on command line.
8389986Sandreas@sandberg.pp.se        default_vars_file = joinpath('build_opts',
8399986Sandreas@sandberg.pp.se                                     ARGUMENTS.get('default', variant_dir))
8409986Sandreas@sandberg.pp.se        if isfile(default_vars_file):
8419986Sandreas@sandberg.pp.se            sticky_vars.files.append(default_vars_file)
8429986Sandreas@sandberg.pp.se            print "Variables file %s not found,\n  using defaults in %s" \
8439986Sandreas@sandberg.pp.se                  % (current_vars_file, default_vars_file)
8449986Sandreas@sandberg.pp.se        else:
8459986Sandreas@sandberg.pp.se            print "Error: cannot find variables file %s or %s" \
8469986Sandreas@sandberg.pp.se                  % (current_vars_file, default_vars_file)
8479986Sandreas@sandberg.pp.se            Exit(1)
8489986Sandreas@sandberg.pp.se
8499986Sandreas@sandberg.pp.se    # Apply current variable settings to env
8509986Sandreas@sandberg.pp.se    sticky_vars.Update(env)
8519986Sandreas@sandberg.pp.se    nonsticky_vars.Update(env)
8529986Sandreas@sandberg.pp.se
8539986Sandreas@sandberg.pp.se    help_text += "\nSticky variables for %s:\n" % variant_dir \
8549986Sandreas@sandberg.pp.se                 + sticky_vars.GenerateHelpText(env) \
8559986Sandreas@sandberg.pp.se                 + "\nNon-sticky variables for %s:\n" % variant_dir \
8562638Sstever@eecs.umich.edu                 + nonsticky_vars.GenerateHelpText(env)
8572638Sstever@eecs.umich.edu
8586121Snate@binkert.org    # Process variable settings.
8593716Sstever@eecs.umich.edu
8605522Snate@binkert.org    if not have_fenv and env['USE_FENV']:
8619986Sandreas@sandberg.pp.se        print "Warning: <fenv.h> not available; " \
8629986Sandreas@sandberg.pp.se              "forcing USE_FENV to False in", variant_dir + "."
8639986Sandreas@sandberg.pp.se        env['USE_FENV'] = False
8649986Sandreas@sandberg.pp.se
8655522Snate@binkert.org    if not env['USE_FENV']:
8665522Snate@binkert.org        print "Warning: No IEEE FP rounding mode control in", variant_dir + "."
8675522Snate@binkert.org        print "         FP results may deviate slightly from other platforms."
8685522Snate@binkert.org
8691858SN/A    if env['EFENCE']:
8705227Ssaidi@eecs.umich.edu        env.Append(LIBS=['efence'])
8715227Ssaidi@eecs.umich.edu
8725227Ssaidi@eecs.umich.edu    if env['USE_MYSQL']:
8735227Ssaidi@eecs.umich.edu        if not have_mysql:
8746654Snate@binkert.org            print "Warning: MySQL not available; " \
8756654Snate@binkert.org                  "forcing USE_MYSQL to False in", variant_dir + "."
8767769SAli.Saidi@ARM.com            env['USE_MYSQL'] = False
8777769SAli.Saidi@ARM.com        else:
8787769SAli.Saidi@ARM.com            print "Compiling in", variant_dir, "with MySQL support."
8797769SAli.Saidi@ARM.com            env.ParseConfig(mysql_config_libs)
8805227Ssaidi@eecs.umich.edu            env.ParseConfig(mysql_config_include)
8815227Ssaidi@eecs.umich.edu
8825227Ssaidi@eecs.umich.edu    # Save sticky variable settings back to current variables file
8835204Sstever@gmail.com    sticky_vars.Save(current_vars_file, env)
8845204Sstever@gmail.com
8855204Sstever@gmail.com    if env['USE_SSE2']:
8865204Sstever@gmail.com        env.Append(CCFLAGS='-msse2')
8875204Sstever@gmail.com
8885204Sstever@gmail.com    # The src/SConscript file sets up the build rules in 'env' according
8895204Sstever@gmail.com    # to the configured variables.  It returns a list of environments,
8905204Sstever@gmail.com    # one for each variant build (debug, opt, etc.)
8915204Sstever@gmail.com    envList = SConscript('src/SConscript', variant_dir = variant_path,
8925204Sstever@gmail.com                         exports = 'env')
8935204Sstever@gmail.com
8945204Sstever@gmail.com    # Set up the regression tests for each build.
8955204Sstever@gmail.com    for e in envList:
8965204Sstever@gmail.com        SConscript('tests/SConscript',
8975204Sstever@gmail.com                   variant_dir = joinpath(variant_path, 'tests', e.Label),
8985204Sstever@gmail.com                   exports = { 'env' : e }, duplicate = False)
8995204Sstever@gmail.com
9006121Snate@binkert.orgHelp(help_text)
9015204Sstever@gmail.com