SConstruct revision 7739
1955SN/A# -*- mode:python -*-
2955SN/A
310841Sandreas.sandberg@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.com# Global Python includes
1005863Snate@binkert.orgimport os
1015863Snate@binkert.orgimport re
1025863Snate@binkert.orgimport subprocess
1039812Sandreas.hansson@arm.comimport sys
1049812Sandreas.hansson@arm.com
1055863Snate@binkert.orgfrom os import mkdir, environ
1065863Snate@binkert.orgfrom os.path import abspath, basename, dirname, expanduser, normpath
1078878Ssteve.reinhardt@amd.comfrom os.path import exists,  isdir, isfile
1085863Snate@binkert.orgfrom os.path import join as joinpath, split as splitpath
1095863Snate@binkert.org
1105863Snate@binkert.org# SCons includes
1116654Snate@binkert.orgimport SCons
11210196SCurtis.Dunham@arm.comimport SCons.Node
113955SN/A
1145396Ssaidi@eecs.umich.eduextra_python_paths = [
1155863Snate@binkert.org    Dir('src/python').srcnode().abspath, # M5 includes
1165863Snate@binkert.org    Dir('ext/ply').srcnode().abspath, # ply is used by several files
1174202Sbinkertn@umich.edu    ]
1185863Snate@binkert.org    
1195863Snate@binkert.orgsys.path[1:1] = extra_python_paths
1205863Snate@binkert.org
1215863Snate@binkert.orgfrom m5.util import compareVersions, readCommand
122955SN/A
1236654Snate@binkert.org########################################################################
1245273Sstever@gmail.com#
1255871Snate@binkert.org# Set up the main build environment.
1265273Sstever@gmail.com#
1276655Snate@binkert.org########################################################################
1288878Ssteve.reinhardt@amd.comuse_vars = set([ 'AS', 'AR', 'CC', 'CXX', 'HOME', 'LD_LIBRARY_PATH', 'PATH',
1296655Snate@binkert.org                 'PYTHONPATH', 'RANLIB' ])
1306655Snate@binkert.org
1319219Spower.jg@gmail.comuse_env = {}
1326655Snate@binkert.orgfor key,val in os.environ.iteritems():
1335871Snate@binkert.org    if key in use_vars or key.startswith("M5"):
1346654Snate@binkert.org        use_env[key] = val
1358947Sandreas.hansson@arm.com
1365396Ssaidi@eecs.umich.edumain = Environment(ENV=use_env)
1378120Sgblack@eecs.umich.edumain.root = Dir(".")         # The current directory (where this file lives).
1388120Sgblack@eecs.umich.edumain.srcdir = Dir("src")     # The source directory
1398120Sgblack@eecs.umich.edu
1408120Sgblack@eecs.umich.edu# add useful python code PYTHONPATH so it can be used by subprocesses
1418120Sgblack@eecs.umich.edu# as well
1428120Sgblack@eecs.umich.edumain.AppendENVPath('PYTHONPATH', extra_python_paths)
1438120Sgblack@eecs.umich.edu
1448120Sgblack@eecs.umich.edu########################################################################
1458879Ssteve.reinhardt@amd.com#
1468879Ssteve.reinhardt@amd.com# Mercurial Stuff.
1478879Ssteve.reinhardt@amd.com#
1488879Ssteve.reinhardt@amd.com# If the M5 directory is a mercurial repository, we should do some
1498879Ssteve.reinhardt@amd.com# extra things.
1508879Ssteve.reinhardt@amd.com#
1518879Ssteve.reinhardt@amd.com########################################################################
1528879Ssteve.reinhardt@amd.com
1538879Ssteve.reinhardt@amd.comhgdir = main.root.Dir(".hg")
1548879Ssteve.reinhardt@amd.com
1558879Ssteve.reinhardt@amd.commercurial_style_message = """
1568879Ssteve.reinhardt@amd.comYou're missing the M5 style hook.
1578879Ssteve.reinhardt@amd.comPlease install the hook so we can ensure that all code fits a common style.
1588120Sgblack@eecs.umich.edu
1598120Sgblack@eecs.umich.eduAll you'd need to do is add the following lines to your repository .hg/hgrc
1608120Sgblack@eecs.umich.eduor your personal .hgrc
1618120Sgblack@eecs.umich.edu----------------
1628120Sgblack@eecs.umich.edu
1638120Sgblack@eecs.umich.edu[extensions]
1648120Sgblack@eecs.umich.edustyle = %s/util/style.py
1658120Sgblack@eecs.umich.edu
1668120Sgblack@eecs.umich.edu[hooks]
1678120Sgblack@eecs.umich.edupretxncommit.style = python:style.check_whitespace
1688120Sgblack@eecs.umich.edu""" % (main.root)
1698120Sgblack@eecs.umich.edu
1708120Sgblack@eecs.umich.edumercurial_bin_not_found = """
1718120Sgblack@eecs.umich.eduMercurial binary cannot be found, unfortunately this means that we
1728879Ssteve.reinhardt@amd.comcannot easily determine the version of M5 that you are running and
1738879Ssteve.reinhardt@amd.comthis makes error messages more difficult to collect.  Please consider
1748879Ssteve.reinhardt@amd.cominstalling mercurial if you choose to post an error message
1758879Ssteve.reinhardt@amd.com"""
17610458Sandreas.hansson@arm.com
17710458Sandreas.hansson@arm.commercurial_lib_not_found = """
17810458Sandreas.hansson@arm.comMercurial libraries cannot be found, ignoring style hook
1798879Ssteve.reinhardt@amd.comIf you are actually a M5 developer, please fix this and
1808879Ssteve.reinhardt@amd.comrun the style hook. It is important.
1818879Ssteve.reinhardt@amd.com"""
1828879Ssteve.reinhardt@amd.com
1839227Sandreas.hansson@arm.comhg_info = "Unknown"
1849227Sandreas.hansson@arm.comif hgdir.exists():
1858879Ssteve.reinhardt@amd.com    # 1) Grab repository revision if we know it.
1868879Ssteve.reinhardt@amd.com    cmd = "hg id -n -i -t -b"
1878879Ssteve.reinhardt@amd.com    try:
1888879Ssteve.reinhardt@amd.com        hg_info = readCommand(cmd, cwd=main.root.abspath).strip()
18910453SAndrew.Bardsley@arm.com    except OSError:
19010453SAndrew.Bardsley@arm.com        print mercurial_bin_not_found
19110453SAndrew.Bardsley@arm.com
19210456SCurtis.Dunham@arm.com    # 2) Ensure that the style hook is in place.
19310456SCurtis.Dunham@arm.com    try:
19410456SCurtis.Dunham@arm.com        ui = None
19510457Sandreas.hansson@arm.com        if ARGUMENTS.get('IGNORE_STYLE') != 'True':
19610457Sandreas.hansson@arm.com            from mercurial import ui
1978120Sgblack@eecs.umich.edu            ui = ui.ui()
1988947Sandreas.hansson@arm.com    except ImportError:
1997816Ssteve.reinhardt@amd.com        print mercurial_lib_not_found
2005871Snate@binkert.org
2015871Snate@binkert.org    if ui is not None:
2026121Snate@binkert.org        ui.readconfig(hgdir.File('hgrc').abspath)
2035871Snate@binkert.org        style_hook = ui.config('hooks', 'pretxncommit.style', None)
2045871Snate@binkert.org
2059926Sstan.czerniawski@arm.com        if not style_hook:
2069926Sstan.czerniawski@arm.com            print mercurial_style_message
2079119Sandreas.hansson@arm.com            sys.exit(1)
20810068Sandreas.hansson@arm.comelse:
20910068Sandreas.hansson@arm.com    print ".hg directory not found"
210955SN/A
2119416SAndreas.Sandberg@ARM.commain['HG_INFO'] = hg_info
21211212Sjoseph.gross@amd.com
21311212Sjoseph.gross@amd.com###################################################
21411212Sjoseph.gross@amd.com#
21511212Sjoseph.gross@amd.com# Figure out which configurations to set up based on the path(s) of
21611212Sjoseph.gross@amd.com# the target(s).
2179416SAndreas.Sandberg@ARM.com#
2189416SAndreas.Sandberg@ARM.com###################################################
2195871Snate@binkert.org
22010584Sandreas.hansson@arm.com# Find default configuration & binary.
2219416SAndreas.Sandberg@ARM.comDefault(environ.get('M5_DEFAULT_BINARY', 'build/ALPHA_SE/m5.debug'))
2229416SAndreas.Sandberg@ARM.com
2235871Snate@binkert.org# helper function: find last occurrence of element in list
224955SN/Adef rfind(l, elt, offs = -1):
22510671Sandreas.hansson@arm.com    for i in range(len(l)+offs, 0, -1):
22610671Sandreas.hansson@arm.com        if l[i] == elt:
22710671Sandreas.hansson@arm.com            return i
22810671Sandreas.hansson@arm.com    raise ValueError, "element not found"
2298881Smarc.orr@gmail.com
2306121Snate@binkert.org# Each target must have 'build' in the interior of the path; the
2316121Snate@binkert.org# directory below this will determine the build parameters.  For
2321533SN/A# example, for target 'foo/bar/build/ALPHA_SE/arch/alpha/blah.do' we
2339239Sandreas.hansson@arm.com# recognize that ALPHA_SE specifies the configuration because it
2349239Sandreas.hansson@arm.com# follow 'build' in the bulid path.
2359239Sandreas.hansson@arm.com
2369239Sandreas.hansson@arm.com# Generate absolute paths to targets so we can see where the build dir is
2379239Sandreas.hansson@arm.comif COMMAND_LINE_TARGETS:
2389239Sandreas.hansson@arm.com    # Ask SCons which directory it was invoked from
2399239Sandreas.hansson@arm.com    launch_dir = GetLaunchDir()
2409239Sandreas.hansson@arm.com    # Make targets relative to invocation directory
2419239Sandreas.hansson@arm.com    abs_targets = [ normpath(joinpath(launch_dir, str(x))) for x in \
2429239Sandreas.hansson@arm.com                    COMMAND_LINE_TARGETS]
2439239Sandreas.hansson@arm.comelse:
2449239Sandreas.hansson@arm.com    # Default targets are relative to root of tree
2456655Snate@binkert.org    abs_targets = [ normpath(joinpath(main.root.abspath, str(x))) for x in \
2466655Snate@binkert.org                    DEFAULT_TARGETS]
2476655Snate@binkert.org
2486655Snate@binkert.org
2495871Snate@binkert.org# Generate a list of the unique build roots and configs that the
2505871Snate@binkert.org# collected targets reference.
2515863Snate@binkert.orgvariant_paths = []
2525871Snate@binkert.orgbuild_root = None
2538878Ssteve.reinhardt@amd.comfor t in abs_targets:
2545871Snate@binkert.org    path_dirs = t.split('/')
2555871Snate@binkert.org    try:
2565871Snate@binkert.org        build_top = rfind(path_dirs, 'build', -2)
2575863Snate@binkert.org    except:
2586121Snate@binkert.org        print "Error: no non-leaf 'build' dir found on target path", t
2595863Snate@binkert.org        Exit(1)
2605871Snate@binkert.org    this_build_root = joinpath('/',*path_dirs[:build_top+1])
2618336Ssteve.reinhardt@amd.com    if not build_root:
2628336Ssteve.reinhardt@amd.com        build_root = this_build_root
2638336Ssteve.reinhardt@amd.com    else:
2648336Ssteve.reinhardt@amd.com        if this_build_root != build_root:
2654678Snate@binkert.org            print "Error: build targets not under same build root\n"\
2668336Ssteve.reinhardt@amd.com                  "  %s\n  %s" % (build_root, this_build_root)
2678336Ssteve.reinhardt@amd.com            Exit(1)
2688336Ssteve.reinhardt@amd.com    variant_path = joinpath('/',*path_dirs[:build_top+2])
2694678Snate@binkert.org    if variant_path not in variant_paths:
2704678Snate@binkert.org        variant_paths.append(variant_path)
2714678Snate@binkert.org
2724678Snate@binkert.org# Make sure build_root exists (might not if this is the first build there)
2737827Snate@binkert.orgif not isdir(build_root):
2747827Snate@binkert.org    mkdir(build_root)
2758336Ssteve.reinhardt@amd.com
2764678Snate@binkert.orgExport('main')
2778336Ssteve.reinhardt@amd.com
2788336Ssteve.reinhardt@amd.commain.SConsignFile(joinpath(build_root, "sconsign"))
2798336Ssteve.reinhardt@amd.com
2808336Ssteve.reinhardt@amd.com# Default duplicate option is to use hard links, but this messes up
2818336Ssteve.reinhardt@amd.com# when you use emacs to edit a file in the target dir, as emacs moves
2828336Ssteve.reinhardt@amd.com# file to file~ then copies to file, breaking the link.  Symbolic
2835871Snate@binkert.org# (soft) links work better.
2845871Snate@binkert.orgmain.SetOption('duplicate', 'soft-copy')
2858336Ssteve.reinhardt@amd.com
2868336Ssteve.reinhardt@amd.com#
2878336Ssteve.reinhardt@amd.com# Set up global sticky variables... these are common to an entire build
2888336Ssteve.reinhardt@amd.com# tree (not specific to a particular build like ALPHA_SE)
2898336Ssteve.reinhardt@amd.com#
2905871Snate@binkert.org
2918336Ssteve.reinhardt@amd.com# Variable validators & converters for global sticky variables
2928336Ssteve.reinhardt@amd.comdef PathListMakeAbsolute(val):
2938336Ssteve.reinhardt@amd.com    if not val:
2948336Ssteve.reinhardt@amd.com        return val
2958336Ssteve.reinhardt@amd.com    f = lambda p: abspath(expanduser(p))
2964678Snate@binkert.org    return ':'.join(map(f, val.split(':')))
2975871Snate@binkert.org
2984678Snate@binkert.orgdef PathListAllExist(key, val, env):
2998336Ssteve.reinhardt@amd.com    if not val:
3008336Ssteve.reinhardt@amd.com        return
3018336Ssteve.reinhardt@amd.com    paths = val.split(':')
3028336Ssteve.reinhardt@amd.com    for path in paths:
3038336Ssteve.reinhardt@amd.com        if not isdir(path):
3048336Ssteve.reinhardt@amd.com            raise SCons.Errors.UserError("Path does not exist: '%s'" % path)
3058336Ssteve.reinhardt@amd.com
3068336Ssteve.reinhardt@amd.comglobal_sticky_vars_file = joinpath(build_root, 'variables.global')
3078336Ssteve.reinhardt@amd.com
3088336Ssteve.reinhardt@amd.comglobal_sticky_vars = Variables(global_sticky_vars_file, args=ARGUMENTS)
3098336Ssteve.reinhardt@amd.com
3108336Ssteve.reinhardt@amd.comglobal_sticky_vars.AddVariables(
3118336Ssteve.reinhardt@amd.com    ('CC', 'C compiler', environ.get('CC', main['CC'])),
3128336Ssteve.reinhardt@amd.com    ('CXX', 'C++ compiler', environ.get('CXX', main['CXX'])),
3138336Ssteve.reinhardt@amd.com    ('BATCH', 'Use batch pool for build and tests', False),
3148336Ssteve.reinhardt@amd.com    ('BATCH_CMD', 'Batch pool submission command name', 'qdo'),
3158336Ssteve.reinhardt@amd.com    ('M5_BUILD_CACHE', 'Cache built objects in this directory', False),
3165871Snate@binkert.org    ('EXTRAS', 'Add Extra directories to the compilation', '',
3176121Snate@binkert.org     PathListAllExist, PathListMakeAbsolute),
318955SN/A    )
319955SN/A
3202632Sstever@eecs.umich.edu# base help text
3212632Sstever@eecs.umich.eduhelp_text = '''
322955SN/AUsage: scons [scons options] [build options] [target(s)]
323955SN/A
324955SN/AGlobal sticky options:
325955SN/A'''
3268878Ssteve.reinhardt@amd.com
327955SN/A# Update main environment with values from ARGUMENTS & global_sticky_vars_file
3282632Sstever@eecs.umich.eduglobal_sticky_vars.Update(main)
3292632Sstever@eecs.umich.edu
3302632Sstever@eecs.umich.eduhelp_text += global_sticky_vars.GenerateHelpText(main)
3312632Sstever@eecs.umich.edu
3322632Sstever@eecs.umich.edu# Save sticky variable settings back to current variables file
3332632Sstever@eecs.umich.eduglobal_sticky_vars.Save(global_sticky_vars_file, main)
3342632Sstever@eecs.umich.edu
3358268Ssteve.reinhardt@amd.com# Parse EXTRAS variable to build list of all directories where we're
3368268Ssteve.reinhardt@amd.com# look for sources etc.  This list is exported as base_dir_list.
3378268Ssteve.reinhardt@amd.combase_dir = main.srcdir.abspath
3388268Ssteve.reinhardt@amd.comif main['EXTRAS']:
3398268Ssteve.reinhardt@amd.com    extras_dir_list = main['EXTRAS'].split(':')
3408268Ssteve.reinhardt@amd.comelse:
3418268Ssteve.reinhardt@amd.com    extras_dir_list = []
3422632Sstever@eecs.umich.edu
3432632Sstever@eecs.umich.eduExport('base_dir')
3442632Sstever@eecs.umich.eduExport('extras_dir_list')
3452632Sstever@eecs.umich.edu
3468268Ssteve.reinhardt@amd.com# the ext directory should be on the #includes path
3472632Sstever@eecs.umich.edumain.Append(CPPPATH=[Dir('ext')])
3488268Ssteve.reinhardt@amd.com
3498268Ssteve.reinhardt@amd.comCXX_version = readCommand([main['CXX'],'--version'], exception=False)
3508268Ssteve.reinhardt@amd.comCXX_V = readCommand([main['CXX'],'-V'], exception=False)
3518268Ssteve.reinhardt@amd.com
3523718Sstever@eecs.umich.edumain['GCC'] = CXX_version and CXX_version.find('g++') >= 0
3532634Sstever@eecs.umich.edumain['SUNCC'] = CXX_V and CXX_V.find('Sun C++') >= 0
3542634Sstever@eecs.umich.edumain['ICC'] = CXX_V and CXX_V.find('Intel') >= 0
3555863Snate@binkert.orgif main['GCC'] + main['SUNCC'] + main['ICC'] > 1:
3562638Sstever@eecs.umich.edu    print 'Error: How can we have two at the same time?'
3578268Ssteve.reinhardt@amd.com    Exit(1)
3582632Sstever@eecs.umich.edu
3592632Sstever@eecs.umich.edu# Set up default C++ compiler flags
3602632Sstever@eecs.umich.eduif main['GCC']:
3612632Sstever@eecs.umich.edu    main.Append(CCFLAGS=['-pipe'])
3622632Sstever@eecs.umich.edu    main.Append(CCFLAGS=['-fno-strict-aliasing'])
3631858SN/A    main.Append(CCFLAGS=['-Wall', '-Wno-sign-compare', '-Wundef'])
3643716Sstever@eecs.umich.edu    main.Append(CXXFLAGS=['-Wno-deprecated'])
3652638Sstever@eecs.umich.edu    # Read the GCC version to check for versions with bugs
3662638Sstever@eecs.umich.edu    # Note CCVERSION doesn't work here because it is run with the CC
3672638Sstever@eecs.umich.edu    # before we override it from the command line
3682638Sstever@eecs.umich.edu    gcc_version = readCommand([main['CXX'], '-dumpversion'], exception=False)
3692638Sstever@eecs.umich.edu    if not compareVersions(gcc_version, '4.4.1') or \
3702638Sstever@eecs.umich.edu       not compareVersions(gcc_version, '4.4.2'):
3712638Sstever@eecs.umich.edu        print 'Info: Tree vectorizer in GCC 4.4.1 & 4.4.2 is buggy, disabling.'
3725863Snate@binkert.org        main.Append(CCFLAGS=['-fno-tree-vectorize'])
3735863Snate@binkert.orgelif main['ICC']:
3745863Snate@binkert.org    pass #Fix me... add warning flags once we clean up icc warnings
375955SN/Aelif main['SUNCC']:
3765341Sstever@gmail.com    main.Append(CCFLAGS=['-Qoption ccfe'])
3775341Sstever@gmail.com    main.Append(CCFLAGS=['-features=gcc'])
3785863Snate@binkert.org    main.Append(CCFLAGS=['-features=extensions'])
3797756SAli.Saidi@ARM.com    main.Append(CCFLAGS=['-library=stlport4'])
3805341Sstever@gmail.com    main.Append(CCFLAGS=['-xar'])
3816121Snate@binkert.org    #main.Append(CCFLAGS=['-instances=semiexplicit'])
3824494Ssaidi@eecs.umich.eduelse:
3836121Snate@binkert.org    print 'Error: Don\'t know what compiler options to use for your compiler.'
3841105SN/A    print '       Please fix SConstruct and src/SConscript and try again.'
3852667Sstever@eecs.umich.edu    Exit(1)
3862667Sstever@eecs.umich.edu
3872667Sstever@eecs.umich.edu# Set up common yacc/bison flags (needed for Ruby)
3882667Sstever@eecs.umich.edumain['YACCFLAGS'] = '-d'
3896121Snate@binkert.orgmain['YACCHXXFILESUFFIX'] = '.hh'
3902667Sstever@eecs.umich.edu
3915341Sstever@gmail.com# Do this after we save setting back, or else we'll tack on an
3925863Snate@binkert.org# extra 'qdo' every time we run scons.
3935341Sstever@gmail.comif main['BATCH']:
3945341Sstever@gmail.com    main['CC']     = main['BATCH_CMD'] + ' ' + main['CC']
3955341Sstever@gmail.com    main['CXX']    = main['BATCH_CMD'] + ' ' + main['CXX']
3968120Sgblack@eecs.umich.edu    main['AS']     = main['BATCH_CMD'] + ' ' + main['AS']
3975341Sstever@gmail.com    main['AR']     = main['BATCH_CMD'] + ' ' + main['AR']
3988120Sgblack@eecs.umich.edu    main['RANLIB'] = main['BATCH_CMD'] + ' ' + main['RANLIB']
3995341Sstever@gmail.com
4008120Sgblack@eecs.umich.eduif sys.platform == 'cygwin':
4016121Snate@binkert.org    # cygwin has some header file issues...
4026121Snate@binkert.org    main.Append(CCFLAGS=["-Wno-uninitialized"])
4038980Ssteve.reinhardt@amd.com
4049396Sandreas.hansson@arm.com# Check for SWIG
4055397Ssaidi@eecs.umich.eduif not main.has_key('SWIG'):
4065397Ssaidi@eecs.umich.edu    print 'Error: SWIG utility not found.'
4077727SAli.Saidi@ARM.com    print '       Please install (see http://www.swig.org) and retry.'
4088268Ssteve.reinhardt@amd.com    Exit(1)
4096168Snate@binkert.org
4105341Sstever@gmail.com# Check for appropriate SWIG version
4118120Sgblack@eecs.umich.eduswig_version = readCommand(('swig', '-version'), exception='').split()
4128120Sgblack@eecs.umich.edu# First 3 words should be "SWIG Version x.y.z"
4138120Sgblack@eecs.umich.eduif len(swig_version) < 3 or \
4146814Sgblack@eecs.umich.edu        swig_version[0] != 'SWIG' or swig_version[1] != 'Version':
4155863Snate@binkert.org    print 'Error determining SWIG version.'
4168120Sgblack@eecs.umich.edu    Exit(1)
4175341Sstever@gmail.com
4185863Snate@binkert.orgmin_swig_version = '1.3.28'
4198268Ssteve.reinhardt@amd.comif compareVersions(swig_version[2], min_swig_version) < 0:
4206121Snate@binkert.org    print 'Error: SWIG version', min_swig_version, 'or newer required.'
4216121Snate@binkert.org    print '       Installed version:', swig_version[2]
4228268Ssteve.reinhardt@amd.com    Exit(1)
4235742Snate@binkert.org
4245742Snate@binkert.org# Set up SWIG flags & scanner
4255341Sstever@gmail.comswig_flags=Split('-c++ -python -modern -templatereduce $_CPPINCFLAGS')
4265742Snate@binkert.orgmain.Append(SWIGFLAGS=swig_flags)
4275742Snate@binkert.org
4285341Sstever@gmail.com# filter out all existing swig scanners, they mess up the dependency
4296017Snate@binkert.org# stuff for some reason
4306121Snate@binkert.orgscanners = []
4316017Snate@binkert.orgfor scanner in main['SCANNERS']:
4327816Ssteve.reinhardt@amd.com    skeys = scanner.skeys
4337756SAli.Saidi@ARM.com    if skeys == '.i':
4347756SAli.Saidi@ARM.com        continue
4357756SAli.Saidi@ARM.com
4367756SAli.Saidi@ARM.com    if isinstance(skeys, (list, tuple)) and '.i' in skeys:
4377756SAli.Saidi@ARM.com        continue
4387756SAli.Saidi@ARM.com
4397756SAli.Saidi@ARM.com    scanners.append(scanner)
4407756SAli.Saidi@ARM.com
4417816Ssteve.reinhardt@amd.com# add the new swig scanner that we like better
4427816Ssteve.reinhardt@amd.comfrom SCons.Scanner import ClassicCPP as CPPScanner
4437816Ssteve.reinhardt@amd.comswig_inc_re = '^[ \t]*[%,#][ \t]*(?:include|import)[ \t]*(<|")([^>"]+)(>|")'
4447816Ssteve.reinhardt@amd.comscanners.append(CPPScanner("SwigScan", [ ".i" ], "CPPPATH", swig_inc_re))
4457816Ssteve.reinhardt@amd.com
4467816Ssteve.reinhardt@amd.com# replace the scanners list that has what we want
4477816Ssteve.reinhardt@amd.commain['SCANNERS'] = scanners
4487816Ssteve.reinhardt@amd.com
4497816Ssteve.reinhardt@amd.com# Add a custom Check function to the Configure context so that we can
4507816Ssteve.reinhardt@amd.com# figure out if the compiler adds leading underscores to global
4517756SAli.Saidi@ARM.com# variables.  This is needed for the autogenerated asm files that we
4527816Ssteve.reinhardt@amd.com# use for embedding the python code.
4537816Ssteve.reinhardt@amd.comdef CheckLeading(context):
4547816Ssteve.reinhardt@amd.com    context.Message("Checking for leading underscore in global variables...")
4557816Ssteve.reinhardt@amd.com    # 1) Define a global variable called x from asm so the C compiler
4567816Ssteve.reinhardt@amd.com    #    won't change the symbol at all.
4577816Ssteve.reinhardt@amd.com    # 2) Declare that variable.
4587816Ssteve.reinhardt@amd.com    # 3) Use the variable
4597816Ssteve.reinhardt@amd.com    #
4607816Ssteve.reinhardt@amd.com    # If the compiler prepends an underscore, this will successfully
4617816Ssteve.reinhardt@amd.com    # link because the external symbol 'x' will be called '_x' which
4627816Ssteve.reinhardt@amd.com    # was defined by the asm statement.  If the compiler does not
4637816Ssteve.reinhardt@amd.com    # prepend an underscore, this will not successfully link because
4647816Ssteve.reinhardt@amd.com    # '_x' will have been defined by assembly, while the C portion of
4657816Ssteve.reinhardt@amd.com    # the code will be trying to use 'x'
4667816Ssteve.reinhardt@amd.com    ret = context.TryLink('''
4677816Ssteve.reinhardt@amd.com        asm(".globl _x; _x: .byte 0");
4687816Ssteve.reinhardt@amd.com        extern int x;
4697816Ssteve.reinhardt@amd.com        int main() { return x; }
4707816Ssteve.reinhardt@amd.com        ''', extension=".c")
4717816Ssteve.reinhardt@amd.com    context.env.Append(LEADING_UNDERSCORE=ret)
4727816Ssteve.reinhardt@amd.com    context.Result(ret)
4737816Ssteve.reinhardt@amd.com    return ret
4747816Ssteve.reinhardt@amd.com
4757816Ssteve.reinhardt@amd.com# Platform-specific configuration.  Note again that we assume that all
4767816Ssteve.reinhardt@amd.com# builds under a given build root run on the same host platform.
4777816Ssteve.reinhardt@amd.comconf = Configure(main,
4787816Ssteve.reinhardt@amd.com                 conf_dir = joinpath(build_root, '.scons_config'),
4797816Ssteve.reinhardt@amd.com                 log_file = joinpath(build_root, 'scons_config.log'),
4807816Ssteve.reinhardt@amd.com                 custom_tests = { 'CheckLeading' : CheckLeading })
4817816Ssteve.reinhardt@amd.com
4827816Ssteve.reinhardt@amd.com# Check for leading underscores.  Don't really need to worry either
4837816Ssteve.reinhardt@amd.com# way so don't need to check the return code.
4847816Ssteve.reinhardt@amd.comconf.CheckLeading()
4857816Ssteve.reinhardt@amd.com
4867816Ssteve.reinhardt@amd.com# Check if we should compile a 64 bit binary on Mac OS X/Darwin
4877816Ssteve.reinhardt@amd.comtry:
4887816Ssteve.reinhardt@amd.com    import platform
4897816Ssteve.reinhardt@amd.com    uname = platform.uname()
4907816Ssteve.reinhardt@amd.com    if uname[0] == 'Darwin' and compareVersions(uname[2], '9.0.0') >= 0:
4917816Ssteve.reinhardt@amd.com        if int(readCommand('sysctl -n hw.cpu64bit_capable')[0]):
4927816Ssteve.reinhardt@amd.com            main.Append(CCFLAGS=['-arch x86_64'])
4937816Ssteve.reinhardt@amd.com            main.Append(CFLAGS=['-arch x86_64'])
4947816Ssteve.reinhardt@amd.com            main.Append(LINKFLAGS=['-arch x86_64'])
4957816Ssteve.reinhardt@amd.com            main.Append(ASFLAGS=['-arch x86_64'])
4967816Ssteve.reinhardt@amd.comexcept:
4977816Ssteve.reinhardt@amd.com    pass
4987816Ssteve.reinhardt@amd.com
4997816Ssteve.reinhardt@amd.com# Recent versions of scons substitute a "Null" object for Configure()
5007816Ssteve.reinhardt@amd.com# when configuration isn't necessary, e.g., if the "--help" option is
5017816Ssteve.reinhardt@amd.com# present.  Unfortuantely this Null object always returns false,
5027816Ssteve.reinhardt@amd.com# breaking all our configuration checks.  We replace it with our own
5037816Ssteve.reinhardt@amd.com# more optimistic null object that returns True instead.
5047816Ssteve.reinhardt@amd.comif not conf:
5057816Ssteve.reinhardt@amd.com    def NullCheck(*args, **kwargs):
5067816Ssteve.reinhardt@amd.com        return True
5077816Ssteve.reinhardt@amd.com
5087816Ssteve.reinhardt@amd.com    class NullConf:
5097816Ssteve.reinhardt@amd.com        def __init__(self, env):
5107816Ssteve.reinhardt@amd.com            self.env = env
5117816Ssteve.reinhardt@amd.com        def Finish(self):
5127816Ssteve.reinhardt@amd.com            return self.env
5138947Sandreas.hansson@arm.com        def __getattr__(self, mname):
5148947Sandreas.hansson@arm.com            return NullCheck
5157756SAli.Saidi@ARM.com
5168120Sgblack@eecs.umich.edu    conf = NullConf(main)
5177756SAli.Saidi@ARM.com
5187756SAli.Saidi@ARM.com# Find Python include and library directories for embedding the
5197756SAli.Saidi@ARM.com# interpreter.  For consistency, we will use the same Python
5207756SAli.Saidi@ARM.com# installation used to run scons (and thus this script).  If you want
5217816Ssteve.reinhardt@amd.com# to link in an alternate version, see above for instructions on how
5227816Ssteve.reinhardt@amd.com# to invoke scons with a different copy of the Python interpreter.
5237816Ssteve.reinhardt@amd.comfrom distutils import sysconfig
5247816Ssteve.reinhardt@amd.com
5257816Ssteve.reinhardt@amd.compy_getvar = sysconfig.get_config_var
5267816Ssteve.reinhardt@amd.com
5277816Ssteve.reinhardt@amd.compy_debug = getattr(sys, 'pydebug', False)
5287816Ssteve.reinhardt@amd.compy_version = 'python' + py_getvar('VERSION') + (py_debug and "_d" or "")
5297816Ssteve.reinhardt@amd.com
5307816Ssteve.reinhardt@amd.compy_general_include = sysconfig.get_python_inc()
5317756SAli.Saidi@ARM.compy_platform_include = sysconfig.get_python_inc(plat_specific=True)
5327756SAli.Saidi@ARM.compy_includes = [ py_general_include ]
5339227Sandreas.hansson@arm.comif py_platform_include != py_general_include:
5349227Sandreas.hansson@arm.com    py_includes.append(py_platform_include)
5359227Sandreas.hansson@arm.com
5369227Sandreas.hansson@arm.compy_lib_path = [ py_getvar('LIBDIR') ]
5379590Sandreas@sandberg.pp.se# add the prefix/lib/pythonX.Y/config dir, but only if there is no
5389590Sandreas@sandberg.pp.se# shared library in prefix/lib/.
5399590Sandreas@sandberg.pp.seif not py_getvar('Py_ENABLE_SHARED'):
5409590Sandreas@sandberg.pp.se    py_lib_path.append(py_getvar('LIBPL'))
5419590Sandreas@sandberg.pp.se
5429590Sandreas@sandberg.pp.sepy_libs = []
5436654Snate@binkert.orgfor lib in py_getvar('LIBS').split() + py_getvar('SYSLIBS').split():
5446654Snate@binkert.org    assert lib.startswith('-l')
5455871Snate@binkert.org    lib = lib[2:]   
5466121Snate@binkert.org    if lib not in py_libs:
5478946Sandreas.hansson@arm.com        py_libs.append(lib)
5489419Sandreas.hansson@arm.compy_libs.append(py_version)
5493940Ssaidi@eecs.umich.edu
5503918Ssaidi@eecs.umich.edumain.Append(CPPPATH=py_includes)
5513918Ssaidi@eecs.umich.edumain.Append(LIBPATH=py_lib_path)
5521858SN/A
5539556Sandreas.hansson@arm.com# Cache build files in the supplied directory.
5549556Sandreas.hansson@arm.comif main['M5_BUILD_CACHE']:
5559556Sandreas.hansson@arm.com    print 'Using build cache located at', main['M5_BUILD_CACHE']
5569556Sandreas.hansson@arm.com    CacheDir(main['M5_BUILD_CACHE'])
5579556Sandreas.hansson@arm.com
5589556Sandreas.hansson@arm.com
5599556Sandreas.hansson@arm.com# verify that this stuff works
56010878Sandreas.hansson@arm.comif not conf.CheckHeader('Python.h', '<>'):
56110878Sandreas.hansson@arm.com    print "Error: can't find Python.h header in", py_includes
5629556Sandreas.hansson@arm.com    Exit(1)
5639556Sandreas.hansson@arm.com
5649556Sandreas.hansson@arm.comfor lib in py_libs:
5659556Sandreas.hansson@arm.com    if not conf.CheckLib(lib):
5669556Sandreas.hansson@arm.com        print "Error: can't find library %s required by python" % lib
5679556Sandreas.hansson@arm.com        Exit(1)
5689556Sandreas.hansson@arm.com
5699556Sandreas.hansson@arm.com# On Solaris you need to use libsocket for socket ops
5709556Sandreas.hansson@arm.comif not conf.CheckLibWithHeader(None, 'sys/socket.h', 'C++', 'accept(0,0,0);'):
5719556Sandreas.hansson@arm.com   if not conf.CheckLibWithHeader('socket', 'sys/socket.h', 'C++', 'accept(0,0,0);'):
5729556Sandreas.hansson@arm.com       print "Can't find library with socket calls (e.g. accept())"
5739556Sandreas.hansson@arm.com       Exit(1)
5749556Sandreas.hansson@arm.com
5759556Sandreas.hansson@arm.com# Check for zlib.  If the check passes, libz will be automatically
5769556Sandreas.hansson@arm.com# added to the LIBS environment variable.
5779556Sandreas.hansson@arm.comif not conf.CheckLibWithHeader('z', 'zlib.h', 'C++','zlibVersion();'):
5789556Sandreas.hansson@arm.com    print 'Error: did not find needed zlib compression library '\
5799556Sandreas.hansson@arm.com          'and/or zlib.h header file.'
5809556Sandreas.hansson@arm.com    print '       Please install zlib and try again.'
5819556Sandreas.hansson@arm.com    Exit(1)
5829556Sandreas.hansson@arm.com
5839556Sandreas.hansson@arm.com# Check for <fenv.h> (C99 FP environment control)
5846121Snate@binkert.orghave_fenv = conf.CheckHeader('fenv.h', '<>')
58510878Sandreas.hansson@arm.comif not have_fenv:
58610238Sandreas.hansson@arm.com    print "Warning: Header file <fenv.h> not found."
58710878Sandreas.hansson@arm.com    print "         This host has no IEEE FP rounding mode control."
5889420Sandreas.hansson@arm.com
58910878Sandreas.hansson@arm.com######################################################################
59010878Sandreas.hansson@arm.com#
5919420Sandreas.hansson@arm.com# Check for mysql.
5929420Sandreas.hansson@arm.com#
5939420Sandreas.hansson@arm.commysql_config = WhereIs('mysql_config')
5949420Sandreas.hansson@arm.comhave_mysql = bool(mysql_config)
5959420Sandreas.hansson@arm.com
59610264Sandreas.hansson@arm.com# Check MySQL version.
59710264Sandreas.hansson@arm.comif have_mysql:
59810264Sandreas.hansson@arm.com    mysql_version = readCommand(mysql_config + ' --version')
59910264Sandreas.hansson@arm.com    min_mysql_version = '4.1'
60010264Sandreas.hansson@arm.com    if compareVersions(mysql_version, min_mysql_version) < 0:
60110866Sandreas.hansson@arm.com        print 'Warning: MySQL', min_mysql_version, 'or newer required.'
60210866Sandreas.hansson@arm.com        print '         Version', mysql_version, 'detected.'
60310264Sandreas.hansson@arm.com        have_mysql = False
60410866Sandreas.hansson@arm.com
60510866Sandreas.hansson@arm.com# Set up mysql_config commands.
60610866Sandreas.hansson@arm.comif have_mysql:
60710866Sandreas.hansson@arm.com    mysql_config_include = mysql_config + ' --include'
60810866Sandreas.hansson@arm.com    if os.system(mysql_config_include + ' > /dev/null') != 0:
60910866Sandreas.hansson@arm.com        # older mysql_config versions don't support --include, use
61010866Sandreas.hansson@arm.com        # --cflags instead
61110264Sandreas.hansson@arm.com        mysql_config_include = mysql_config + ' --cflags | sed s/\\\'//g'
61210264Sandreas.hansson@arm.com    # This seems to work in all versions
61310264Sandreas.hansson@arm.com    mysql_config_libs = mysql_config + ' --libs'
61410264Sandreas.hansson@arm.com
61510264Sandreas.hansson@arm.com######################################################################
61610264Sandreas.hansson@arm.com#
61710264Sandreas.hansson@arm.com# Finish the configuration
61810457Sandreas.hansson@arm.com#
61910457Sandreas.hansson@arm.commain = conf.Finish()
62010457Sandreas.hansson@arm.com
62110457Sandreas.hansson@arm.com######################################################################
62210457Sandreas.hansson@arm.com#
62310457Sandreas.hansson@arm.com# Collect all non-global variables
62410457Sandreas.hansson@arm.com#
62510457Sandreas.hansson@arm.com
62610457Sandreas.hansson@arm.com# Define the universe of supported ISAs
62710238Sandreas.hansson@arm.comall_isa_list = [ ]
62810238Sandreas.hansson@arm.comExport('all_isa_list')
62910238Sandreas.hansson@arm.com
63010238Sandreas.hansson@arm.comclass CpuModel(object):
63110238Sandreas.hansson@arm.com    '''The CpuModel class encapsulates everything the ISA parser needs to
63210238Sandreas.hansson@arm.com    know about a particular CPU model.'''
63310416Sandreas.hansson@arm.com
63410238Sandreas.hansson@arm.com    # Dict of available CPU model objects.  Accessible as CpuModel.dict.
6359227Sandreas.hansson@arm.com    dict = {}
63610238Sandreas.hansson@arm.com    list = []
63710416Sandreas.hansson@arm.com    defaults = []
63810416Sandreas.hansson@arm.com
6399227Sandreas.hansson@arm.com    # Constructor.  Automatically adds models to CpuModel.dict.
6409590Sandreas@sandberg.pp.se    def __init__(self, name, filename, includes, strings, default=False):
6419590Sandreas@sandberg.pp.se        self.name = name           # name of model
6429590Sandreas@sandberg.pp.se        self.filename = filename   # filename for output exec code
6438737Skoansin.tan@gmail.com        self.includes = includes   # include files needed in exec file
64410878Sandreas.hansson@arm.com        # The 'strings' dict holds all the per-CPU symbols we can
64510878Sandreas.hansson@arm.com        # substitute into templates etc.
6469420Sandreas.hansson@arm.com        self.strings = strings
6478737Skoansin.tan@gmail.com
64810106SMitch.Hayenga@arm.com        # This cpu is enabled by default
6498737Skoansin.tan@gmail.com        self.default = default
6508737Skoansin.tan@gmail.com
65110878Sandreas.hansson@arm.com        # Add self to dict
65210878Sandreas.hansson@arm.com        if name in CpuModel.dict:
6538737Skoansin.tan@gmail.com            raise AttributeError, "CpuModel '%s' already registered" % name
6548737Skoansin.tan@gmail.com        CpuModel.dict[name] = self
6558737Skoansin.tan@gmail.com        CpuModel.list.append(name)
6568737Skoansin.tan@gmail.com
6578737Skoansin.tan@gmail.comExport('CpuModel')
6588737Skoansin.tan@gmail.com
6599556Sandreas.hansson@arm.com# Sticky variables get saved in the variables file so they persist from
6609556Sandreas.hansson@arm.com# one invocation to the next (unless overridden, in which case the new
6619556Sandreas.hansson@arm.com# value becomes sticky).
6629556Sandreas.hansson@arm.comsticky_vars = Variables(args=ARGUMENTS)
6639556Sandreas.hansson@arm.comExport('sticky_vars')
6649556Sandreas.hansson@arm.com
6659556Sandreas.hansson@arm.com# Sticky variables that should be exported
6669556Sandreas.hansson@arm.comexport_vars = []
66710278SAndreas.Sandberg@ARM.comExport('export_vars')
66810278SAndreas.Sandberg@ARM.com
66910278SAndreas.Sandberg@ARM.com# Non-sticky variables only apply to the current build.
67010278SAndreas.Sandberg@ARM.comnonsticky_vars = Variables(args=ARGUMENTS)
67110278SAndreas.Sandberg@ARM.comExport('nonsticky_vars')
67210278SAndreas.Sandberg@ARM.com
6739556Sandreas.hansson@arm.com# Walk the tree and execute all SConsopts scripts that wil add to the
6749590Sandreas@sandberg.pp.se# above variables
6759590Sandreas@sandberg.pp.sefor bdir in [ base_dir ] + extras_dir_list:
6769420Sandreas.hansson@arm.com    for root, dirs, files in os.walk(bdir):
6779846Sandreas.hansson@arm.com        if 'SConsopts' in files:
6789846Sandreas.hansson@arm.com            print "Reading", joinpath(root, 'SConsopts')
6799846Sandreas.hansson@arm.com            SConscript(joinpath(root, 'SConsopts'))
6809846Sandreas.hansson@arm.com
6818946Sandreas.hansson@arm.comall_isa_list.sort()
6823918Ssaidi@eecs.umich.edu
6839068SAli.Saidi@ARM.comsticky_vars.AddVariables(
6849068SAli.Saidi@ARM.com    EnumVariable('TARGET_ISA', 'Target ISA', 'alpha', all_isa_list),
6859068SAli.Saidi@ARM.com    BoolVariable('FULL_SYSTEM', 'Full-system support', False),
6869068SAli.Saidi@ARM.com    ListVariable('CPU_MODELS', 'CPU models',
6879068SAli.Saidi@ARM.com                 sorted(n for n,m in CpuModel.dict.iteritems() if m.default),
6889068SAli.Saidi@ARM.com                 sorted(CpuModel.list)),
6899068SAli.Saidi@ARM.com    BoolVariable('NO_FAST_ALLOC', 'Disable fast object allocator', False),
6909068SAli.Saidi@ARM.com    BoolVariable('FAST_ALLOC_DEBUG', 'Enable fast object allocator debugging',
6919068SAli.Saidi@ARM.com                 False),
6929419Sandreas.hansson@arm.com    BoolVariable('FAST_ALLOC_STATS', 'Enable fast object allocator statistics',
6939068SAli.Saidi@ARM.com                 False),
6949068SAli.Saidi@ARM.com    BoolVariable('EFENCE', 'Link with Electric Fence malloc debugger',
6959068SAli.Saidi@ARM.com                 False),
6969068SAli.Saidi@ARM.com    BoolVariable('SS_COMPATIBLE_FP',
6979068SAli.Saidi@ARM.com                 'Make floating-point results compatible with SimpleScalar',
6989068SAli.Saidi@ARM.com                 False),
6993918Ssaidi@eecs.umich.edu    BoolVariable('USE_SSE2',
7003918Ssaidi@eecs.umich.edu                 'Compile for SSE2 (-msse2) to get IEEE FP on x86 hosts',
7016157Snate@binkert.org                 False),
7026157Snate@binkert.org    BoolVariable('USE_MYSQL', 'Use MySQL for stats output', have_mysql),
7036157Snate@binkert.org    BoolVariable('USE_FENV', 'Use <fenv.h> IEEE mode control', have_fenv),
7046157Snate@binkert.org    BoolVariable('USE_CHECKER', 'Use checker for detailed CPU models', False),
7055397Ssaidi@eecs.umich.edu    BoolVariable('CP_ANNOTATE', 'Enable critical path annotation capability', False),
7065397Ssaidi@eecs.umich.edu    BoolVariable('RUBY', 'Build with Ruby', False),
7076121Snate@binkert.org    )
7086121Snate@binkert.org
7096121Snate@binkert.orgnonsticky_vars.AddVariables(
7106121Snate@binkert.org    BoolVariable('update_ref', 'Update test reference outputs', False)
7116121Snate@binkert.org    )
7126121Snate@binkert.org
7135397Ssaidi@eecs.umich.edu# These variables get exported to #defines in config/*.hh (see src/SConscript).
7141851SN/Aexport_vars += ['FULL_SYSTEM', 'USE_FENV', 'USE_MYSQL',
7151851SN/A                'NO_FAST_ALLOC', 'FAST_ALLOC_DEBUG', 'FAST_ALLOC_STATS',
7167739Sgblack@eecs.umich.edu                'SS_COMPATIBLE_FP', 'USE_CHECKER', 'TARGET_ISA', 'CP_ANNOTATE']
717955SN/A
7189396Sandreas.hansson@arm.com###################################################
7199396Sandreas.hansson@arm.com#
7209396Sandreas.hansson@arm.com# Define a SCons builder for configuration flag headers.
7219396Sandreas.hansson@arm.com#
7229396Sandreas.hansson@arm.com###################################################
7239396Sandreas.hansson@arm.com
7249396Sandreas.hansson@arm.com# This function generates a config header file that #defines the
7259396Sandreas.hansson@arm.com# variable symbol to the current variable setting (0 or 1).  The source
7269396Sandreas.hansson@arm.com# operands are the name of the variable and a Value node containing the
7279396Sandreas.hansson@arm.com# value of the variable.
7289396Sandreas.hansson@arm.comdef build_config_file(target, source, env):
7299396Sandreas.hansson@arm.com    (variable, value) = [s.get_contents() for s in source]
7309396Sandreas.hansson@arm.com    f = file(str(target[0]), 'w')
7319396Sandreas.hansson@arm.com    print >> f, '#define', variable, value
7329396Sandreas.hansson@arm.com    f.close()
7339396Sandreas.hansson@arm.com    return None
7349477Sandreas.hansson@arm.com
7359477Sandreas.hansson@arm.com# Generate the message to be printed when building the config file.
7369477Sandreas.hansson@arm.comdef build_config_file_string(target, source, env):
7379477Sandreas.hansson@arm.com    (variable, value) = [s.get_contents() for s in source]
7389477Sandreas.hansson@arm.com    return "Defining %s as %s in %s." % (variable, value, target[0])
7399477Sandreas.hansson@arm.com
7409477Sandreas.hansson@arm.com# Combine the two functions into a scons Action object.
7419477Sandreas.hansson@arm.comconfig_action = Action(build_config_file, build_config_file_string)
7429477Sandreas.hansson@arm.com
7439477Sandreas.hansson@arm.com# The emitter munges the source & target node lists to reflect what
7449477Sandreas.hansson@arm.com# we're really doing.
7459477Sandreas.hansson@arm.comdef config_emitter(target, source, env):
7469477Sandreas.hansson@arm.com    # extract variable name from Builder arg
7479477Sandreas.hansson@arm.com    variable = str(target[0])
7489477Sandreas.hansson@arm.com    # True target is config header file
7499477Sandreas.hansson@arm.com    target = joinpath('config', variable.lower() + '.hh')
7509477Sandreas.hansson@arm.com    val = env[variable]
7519477Sandreas.hansson@arm.com    if isinstance(val, bool):
7529477Sandreas.hansson@arm.com        # Force value to 0/1
7539477Sandreas.hansson@arm.com        val = int(val)
7549477Sandreas.hansson@arm.com    elif isinstance(val, str):
7559477Sandreas.hansson@arm.com        val = '"' + val + '"'
7569396Sandreas.hansson@arm.com
7573053Sstever@eecs.umich.edu    # Sources are variable name & value (packaged in SCons Value nodes)
7586121Snate@binkert.org    return ([target], [Value(variable), Value(val)])
7593053Sstever@eecs.umich.edu
7603053Sstever@eecs.umich.educonfig_builder = Builder(emitter = config_emitter, action = config_action)
7613053Sstever@eecs.umich.edu
7623053Sstever@eecs.umich.edumain.Append(BUILDERS = { 'ConfigFile' : config_builder })
7633053Sstever@eecs.umich.edu
7649072Sandreas.hansson@arm.com# libelf build is shared across all configs in the build root.
7653053Sstever@eecs.umich.edumain.SConscript('ext/libelf/SConscript',
7664742Sstever@eecs.umich.edu                variant_dir = joinpath(build_root, 'libelf'))
7674742Sstever@eecs.umich.edu
7683053Sstever@eecs.umich.edu# gzstream build is shared across all configs in the build root.
7693053Sstever@eecs.umich.edumain.SConscript('ext/gzstream/SConscript',
7703053Sstever@eecs.umich.edu                variant_dir = joinpath(build_root, 'gzstream'))
77110181SCurtis.Dunham@arm.com
7726654Snate@binkert.org###################################################
7733053Sstever@eecs.umich.edu#
7743053Sstever@eecs.umich.edu# This function is used to set up a directory with switching headers
7753053Sstever@eecs.umich.edu#
7763053Sstever@eecs.umich.edu###################################################
77710425Sandreas.hansson@arm.com
77810425Sandreas.hansson@arm.commain['ALL_ISA_LIST'] = all_isa_list
77910425Sandreas.hansson@arm.comdef make_switching_dir(dname, switch_headers, env):
78010425Sandreas.hansson@arm.com    # Generate the header.  target[0] is the full path of the output
78110425Sandreas.hansson@arm.com    # header to generate.  'source' is a dummy variable, since we get the
78210425Sandreas.hansson@arm.com    # list of ISAs from env['ALL_ISA_LIST'].
78310425Sandreas.hansson@arm.com    def gen_switch_hdr(target, source, env):
78410425Sandreas.hansson@arm.com        fname = str(target[0])
78510425Sandreas.hansson@arm.com        f = open(fname, 'w')
78610425Sandreas.hansson@arm.com        isa = env['TARGET_ISA'].lower()
78710425Sandreas.hansson@arm.com        print >>f, '#include "%s/%s/%s"' % (dname, isa, basename(fname))
7882667Sstever@eecs.umich.edu        f.close()
7894554Sbinkertn@umich.edu
7906121Snate@binkert.org    # String to print when generating header
7912667Sstever@eecs.umich.edu    def gen_switch_hdr_string(target, source, env):
79210710Sandreas.hansson@arm.com        return "Generating switch header " + str(target[0])
79310710Sandreas.hansson@arm.com
79410710Sandreas.hansson@arm.com    # Build SCons Action object. 'varlist' specifies env vars that this
79510710Sandreas.hansson@arm.com    # action depends on; when env['ALL_ISA_LIST'] changes these actions
79610710Sandreas.hansson@arm.com    # should get re-executed.
79710710Sandreas.hansson@arm.com    switch_hdr_action = Action(gen_switch_hdr, gen_switch_hdr_string,
79810710Sandreas.hansson@arm.com                               varlist=['ALL_ISA_LIST'])
79910710Sandreas.hansson@arm.com
80010710Sandreas.hansson@arm.com    # Instantiate actions for each header
80110384SCurtis.Dunham@arm.com    for hdr in switch_headers:
8024554Sbinkertn@umich.edu        env.Command(hdr, [], switch_hdr_action)
8034554Sbinkertn@umich.eduExport('make_switching_dir')
8044554Sbinkertn@umich.edu
8056121Snate@binkert.org###################################################
8064554Sbinkertn@umich.edu#
8074554Sbinkertn@umich.edu# Define build environments for selected configurations.
8084554Sbinkertn@umich.edu#
8094781Snate@binkert.org###################################################
8104554Sbinkertn@umich.edu
8114554Sbinkertn@umich.edufor variant_path in variant_paths:
8122667Sstever@eecs.umich.edu    print "Building in", variant_path
8134554Sbinkertn@umich.edu
8144554Sbinkertn@umich.edu    # Make a copy of the build-root environment to use for this config.
8154554Sbinkertn@umich.edu    env = main.Clone()
8164554Sbinkertn@umich.edu    env['BUILDDIR'] = variant_path
8172667Sstever@eecs.umich.edu
8184554Sbinkertn@umich.edu    # variant_dir is the tail component of build path, and is used to
8192667Sstever@eecs.umich.edu    # determine the build parameters (e.g., 'ALPHA_SE')
8204554Sbinkertn@umich.edu    (build_root, variant_dir) = splitpath(variant_path)
8216121Snate@binkert.org
8222667Sstever@eecs.umich.edu    # Set env variables according to the build directory config.
8239986Sandreas@sandberg.pp.se    sticky_vars.files = []
8249986Sandreas@sandberg.pp.se    # Variables for $BUILD_ROOT/$VARIANT_DIR are stored in
8259986Sandreas@sandberg.pp.se    # $BUILD_ROOT/variables/$VARIANT_DIR so you can nuke
8269986Sandreas@sandberg.pp.se    # $BUILD_ROOT/$VARIANT_DIR without losing your variables settings.
8279986Sandreas@sandberg.pp.se    current_vars_file = joinpath(build_root, 'variables', variant_dir)
8289986Sandreas@sandberg.pp.se    if isfile(current_vars_file):
8299986Sandreas@sandberg.pp.se        sticky_vars.files.append(current_vars_file)
8309986Sandreas@sandberg.pp.se        print "Using saved variables file %s" % current_vars_file
8319986Sandreas@sandberg.pp.se    else:
8329986Sandreas@sandberg.pp.se        # Build dir-specific variables file doesn't exist.
8339986Sandreas@sandberg.pp.se
8349986Sandreas@sandberg.pp.se        # Make sure the directory is there so we can create it later
8359986Sandreas@sandberg.pp.se        opt_dir = dirname(current_vars_file)
8369986Sandreas@sandberg.pp.se        if not isdir(opt_dir):
8379986Sandreas@sandberg.pp.se            mkdir(opt_dir)
8389986Sandreas@sandberg.pp.se
8399986Sandreas@sandberg.pp.se        # Get default build variables from source tree.  Variables are
8409986Sandreas@sandberg.pp.se        # normally determined by name of $VARIANT_DIR, but can be
8419986Sandreas@sandberg.pp.se        # overriden by 'default=' arg on command line.
8429986Sandreas@sandberg.pp.se        default_vars_file = joinpath('build_opts',
8432638Sstever@eecs.umich.edu                                     ARGUMENTS.get('default', variant_dir))
8442638Sstever@eecs.umich.edu        if isfile(default_vars_file):
8456121Snate@binkert.org            sticky_vars.files.append(default_vars_file)
8463716Sstever@eecs.umich.edu            print "Variables file %s not found,\n  using defaults in %s" \
8475522Snate@binkert.org                  % (current_vars_file, default_vars_file)
8489986Sandreas@sandberg.pp.se        else:
8499986Sandreas@sandberg.pp.se            print "Error: cannot find variables file %s or %s" \
8509986Sandreas@sandberg.pp.se                  % (current_vars_file, default_vars_file)
8515522Snate@binkert.org            Exit(1)
8525227Ssaidi@eecs.umich.edu
8535227Ssaidi@eecs.umich.edu    # Apply current variable settings to env
8545227Ssaidi@eecs.umich.edu    sticky_vars.Update(env)
8555227Ssaidi@eecs.umich.edu    nonsticky_vars.Update(env)
8566654Snate@binkert.org
8576654Snate@binkert.org    help_text += "\nSticky variables for %s:\n" % variant_dir \
8587769SAli.Saidi@ARM.com                 + sticky_vars.GenerateHelpText(env) \
8597769SAli.Saidi@ARM.com                 + "\nNon-sticky variables for %s:\n" % variant_dir \
8607769SAli.Saidi@ARM.com                 + nonsticky_vars.GenerateHelpText(env)
8617769SAli.Saidi@ARM.com
8625227Ssaidi@eecs.umich.edu    # Process variable settings.
8635227Ssaidi@eecs.umich.edu
8645227Ssaidi@eecs.umich.edu    if not have_fenv and env['USE_FENV']:
8655204Sstever@gmail.com        print "Warning: <fenv.h> not available; " \
8665204Sstever@gmail.com              "forcing USE_FENV to False in", variant_dir + "."
8675204Sstever@gmail.com        env['USE_FENV'] = False
8685204Sstever@gmail.com
8695204Sstever@gmail.com    if not env['USE_FENV']:
8705204Sstever@gmail.com        print "Warning: No IEEE FP rounding mode control in", variant_dir + "."
8715204Sstever@gmail.com        print "         FP results may deviate slightly from other platforms."
8725204Sstever@gmail.com
8735204Sstever@gmail.com    if env['EFENCE']:
8745204Sstever@gmail.com        env.Append(LIBS=['efence'])
8755204Sstever@gmail.com
8765204Sstever@gmail.com    if env['USE_MYSQL']:
8775204Sstever@gmail.com        if not have_mysql:
8785204Sstever@gmail.com            print "Warning: MySQL not available; " \
8795204Sstever@gmail.com                  "forcing USE_MYSQL to False in", variant_dir + "."
8805204Sstever@gmail.com            env['USE_MYSQL'] = False
8815204Sstever@gmail.com        else:
8826121Snate@binkert.org            print "Compiling in", variant_dir, "with MySQL support."
8835204Sstever@gmail.com            env.ParseConfig(mysql_config_libs)
8847727SAli.Saidi@ARM.com            env.ParseConfig(mysql_config_include)
8857727SAli.Saidi@ARM.com
8867727SAli.Saidi@ARM.com    # Save sticky variable settings back to current variables file
8877727SAli.Saidi@ARM.com    sticky_vars.Save(current_vars_file, env)
8887727SAli.Saidi@ARM.com
88910453SAndrew.Bardsley@arm.com    if env['USE_SSE2']:
89010453SAndrew.Bardsley@arm.com        env.Append(CCFLAGS=['-msse2'])
89110453SAndrew.Bardsley@arm.com
89210453SAndrew.Bardsley@arm.com    # The src/SConscript file sets up the build rules in 'env' according
89310453SAndrew.Bardsley@arm.com    # to the configured variables.  It returns a list of environments,
89410453SAndrew.Bardsley@arm.com    # one for each variant build (debug, opt, etc.)
89510453SAndrew.Bardsley@arm.com    envList = SConscript('src/SConscript', variant_dir = variant_path,
89610453SAndrew.Bardsley@arm.com                         exports = 'env')
89710453SAndrew.Bardsley@arm.com
89810453SAndrew.Bardsley@arm.com    # Set up the regression tests for each build.
89910453SAndrew.Bardsley@arm.com    for e in envList:
90010160Sandreas.hansson@arm.com        SConscript('tests/SConscript',
90110453SAndrew.Bardsley@arm.com                   variant_dir = joinpath(variant_path, 'tests', e.Label),
90210453SAndrew.Bardsley@arm.com                   exports = { 'env' : e }, duplicate = False)
90310453SAndrew.Bardsley@arm.com
90410453SAndrew.Bardsley@arm.comHelp(help_text)
90510453SAndrew.Bardsley@arm.com