SConstruct revision 6654
1955SN/A# -*- mode:python -*-
2955SN/A
37816Ssteve.reinhardt@amd.com# Copyright (c) 2009 The Hewlett-Packard Development Company
45871Snate@binkert.org# Copyright (c) 2004-2005 The Regents of The University of Michigan
51762SN/A# All rights reserved.
6955SN/A#
7955SN/A# Redistribution and use in source and binary forms, with or without
8955SN/A# modification, are permitted provided that the following conditions are
9955SN/A# met: redistributions of source code must retain the above copyright
10955SN/A# notice, this list of conditions and the following disclaimer;
11955SN/A# redistributions in binary form must reproduce the above copyright
12955SN/A# notice, this list of conditions and the following disclaimer in the
13955SN/A# documentation and/or other materials provided with the distribution;
14955SN/A# neither the name of the copyright holders nor the names of its
15955SN/A# contributors may be used to endorse or promote products derived from
16955SN/A# this software without specific prior written permission.
17955SN/A#
18955SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19955SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20955SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21955SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22955SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23955SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24955SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25955SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26955SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27955SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28955SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29955SN/A#
302665Ssaidi@eecs.umich.edu# Authors: Steve Reinhardt
312665Ssaidi@eecs.umich.edu#          Nathan Binkert
325863Snate@binkert.org
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
382632Sstever@eecs.umich.edu# configuration (see below), or type 'scons build/<CONFIG>/<binary>'
392632Sstever@eecs.umich.edu# to build some other configuration (e.g., 'build/ALPHA_FS/m5.opt' for
402632Sstever@eecs.umich.edu# the optimized full-system version).
412632Sstever@eecs.umich.edu#
42955SN/A# You can build M5 in a different directory as long as there is a
432632Sstever@eecs.umich.edu# 'build/<CONFIG>' somewhere along the target path.  The build system
442632Sstever@eecs.umich.edu# expects that all configs under the same build directory are being
452761Sstever@eecs.umich.edu# built for the same host system.
462632Sstever@eecs.umich.edu#
472632Sstever@eecs.umich.edu# Examples:
482632Sstever@eecs.umich.edu#
492761Sstever@eecs.umich.edu#   The following two commands are equivalent.  The '-u' option tells
502761Sstever@eecs.umich.edu#   scons to search up the directory tree for this SConstruct file.
512761Sstever@eecs.umich.edu#   % cd <path-to-src>/m5 ; scons build/ALPHA_FS/m5.debug
522632Sstever@eecs.umich.edu#   % cd <path-to-src>/m5/build/ALPHA_FS; scons -u m5.debug
532632Sstever@eecs.umich.edu#
542761Sstever@eecs.umich.edu#   The following two commands are equivalent and demonstrate building
552761Sstever@eecs.umich.edu#   in a directory outside of the source tree.  The '-C' option tells
562761Sstever@eecs.umich.edu#   scons to chdir to the specified directory to find this SConstruct
572761Sstever@eecs.umich.edu#   file.
582761Sstever@eecs.umich.edu#   % cd <path-to-src>/m5 ; scons /local/foo/build/ALPHA_FS/m5.debug
592632Sstever@eecs.umich.edu#   % cd /local/foo/build/ALPHA_FS; scons -C <path-to-src>/m5 m5.debug
602632Sstever@eecs.umich.edu#
612632Sstever@eecs.umich.edu# You can use 'scons -H' to print scons options.  If you're in this
622632Sstever@eecs.umich.edu# 'm5' directory (or use -u or -C to tell scons where to find this
632632Sstever@eecs.umich.edu# file), you can use 'scons -h' to print all the M5-specific build
642632Sstever@eecs.umich.edu# options as well.
652632Sstever@eecs.umich.edu#
66955SN/A###################################################
67955SN/A
68955SN/A# Check for recent-enough Python and SCons versions.
695863Snate@binkert.orgtry:
705863Snate@binkert.org    # Really old versions of scons only take two options for the
715863Snate@binkert.org    # function, so check once without the revision and once with the
725863Snate@binkert.org    # revision, the first instance will fail for stuff other than
735863Snate@binkert.org    # 0.98, and the second will fail for 0.98.0
745863Snate@binkert.org    EnsureSConsVersion(0, 98)
755863Snate@binkert.org    EnsureSConsVersion(0, 98, 1)
765863Snate@binkert.orgexcept SystemExit, e:
775863Snate@binkert.org    print """
785863Snate@binkert.orgFor more details, see:
795863Snate@binkert.org    http://m5sim.org/wiki/index.php/Compiling_M5
805863Snate@binkert.org"""
815863Snate@binkert.org    raise
825863Snate@binkert.org
835863Snate@binkert.org# We ensure the python version early because we have stuff that
845863Snate@binkert.org# requires python 2.4
855863Snate@binkert.orgtry:
865863Snate@binkert.org    EnsurePythonVersion(2, 4)
875863Snate@binkert.orgexcept SystemExit, e:
885863Snate@binkert.org    print """
895863Snate@binkert.orgYou can use a non-default installation of the Python interpreter by
905863Snate@binkert.orgeither (1) rearranging your PATH so that scons finds the non-default
915863Snate@binkert.org'python' first or (2) explicitly invoking an alternative interpreter
925863Snate@binkert.orgon the scons script.
935863Snate@binkert.org
945863Snate@binkert.orgFor more details, see:
955863Snate@binkert.org    http://m5sim.org/wiki/index.php/Using_a_non-default_Python_installation
965863Snate@binkert.org"""
975863Snate@binkert.org    raise
985863Snate@binkert.org
995863Snate@binkert.org# Global Python includes
1006654Snate@binkert.orgimport os
101955SN/Aimport re
1025396Ssaidi@eecs.umich.eduimport subprocess
1035863Snate@binkert.orgimport sys
1045863Snate@binkert.org
1054202Sbinkertn@umich.edufrom os import mkdir, environ
1065863Snate@binkert.orgfrom os.path import abspath, basename, dirname, expanduser, normpath
1075863Snate@binkert.orgfrom os.path import exists,  isdir, isfile
1085863Snate@binkert.orgfrom os.path import join as joinpath, split as splitpath
1095863Snate@binkert.org
110955SN/A# SCons includes
1116654Snate@binkert.orgimport SCons
1125273Sstever@gmail.comimport SCons.Node
1135871Snate@binkert.org
1145273Sstever@gmail.com# M5 includes
1156655Snate@binkert.orgsys.path[1:1] = [ Dir('src/python').srcnode().abspath ]
1166655Snate@binkert.org
1176655Snate@binkert.orgfrom m5.util import compareVersions, readCommand
1186655Snate@binkert.org
1196655Snate@binkert.org########################################################################
1206655Snate@binkert.org#
1215871Snate@binkert.org# Set up the main build environment.
1226654Snate@binkert.org#
1235396Ssaidi@eecs.umich.edu########################################################################
1248120Sgblack@eecs.umich.eduuse_vars = set([ 'AS', 'AR', 'CC', 'CXX', 'HOME', 'LD_LIBRARY_PATH', 'PATH',
1258120Sgblack@eecs.umich.edu                 'RANLIB' ])
1268120Sgblack@eecs.umich.edu
1278120Sgblack@eecs.umich.eduuse_env = {}
1288120Sgblack@eecs.umich.edufor key,val in os.environ.iteritems():
1298120Sgblack@eecs.umich.edu    if key in use_vars or key.startswith("M5"):
1308120Sgblack@eecs.umich.edu        use_env[key] = val
1318120Sgblack@eecs.umich.edu
1328120Sgblack@eecs.umich.edumain = Environment(ENV=use_env)
1338120Sgblack@eecs.umich.edumain.root = Dir(".")         # The current directory (where this file lives).
1348120Sgblack@eecs.umich.edumain.srcdir = Dir("src")     # The source directory
1358120Sgblack@eecs.umich.edu
1368120Sgblack@eecs.umich.edu########################################################################
1378120Sgblack@eecs.umich.edu#
1388120Sgblack@eecs.umich.edu# Mercurial Stuff.
1398120Sgblack@eecs.umich.edu#
1408120Sgblack@eecs.umich.edu# If the M5 directory is a mercurial repository, we should do some
1418120Sgblack@eecs.umich.edu# extra things.
1428120Sgblack@eecs.umich.edu#
1438120Sgblack@eecs.umich.edu########################################################################
1448120Sgblack@eecs.umich.edu
1458120Sgblack@eecs.umich.eduhgdir = main.root.Dir(".hg")
1468120Sgblack@eecs.umich.edu
1478120Sgblack@eecs.umich.edumercurial_style_message = """
1488120Sgblack@eecs.umich.eduYou're missing the M5 style hook.
1498120Sgblack@eecs.umich.eduPlease install the hook so we can ensure that all code fits a common style.
1508120Sgblack@eecs.umich.edu
1518120Sgblack@eecs.umich.eduAll you'd need to do is add the following lines to your repository .hg/hgrc
1528120Sgblack@eecs.umich.eduor your personal .hgrc
1538120Sgblack@eecs.umich.edu----------------
1548120Sgblack@eecs.umich.edu
1558120Sgblack@eecs.umich.edu[extensions]
1568120Sgblack@eecs.umich.edustyle = %s/util/style.py
1578120Sgblack@eecs.umich.edu
1588120Sgblack@eecs.umich.edu[hooks]
1598120Sgblack@eecs.umich.edupretxncommit.style = python:style.check_whitespace
1607816Ssteve.reinhardt@amd.com""" % (main.root)
1617816Ssteve.reinhardt@amd.com
1627816Ssteve.reinhardt@amd.commercurial_bin_not_found = """
1637816Ssteve.reinhardt@amd.comMercurial binary cannot be found, unfortunately this means that we
1647816Ssteve.reinhardt@amd.comcannot easily determine the version of M5 that you are running and
1657816Ssteve.reinhardt@amd.comthis makes error messages more difficult to collect.  Please consider
1667816Ssteve.reinhardt@amd.cominstalling mercurial if you choose to post an error message
1677816Ssteve.reinhardt@amd.com"""
1687816Ssteve.reinhardt@amd.com
1695871Snate@binkert.orgmercurial_lib_not_found = """
1705871Snate@binkert.orgMercurial libraries cannot be found, ignoring style hook
1716121Snate@binkert.orgIf you are actually a M5 developer, please fix this and
1725871Snate@binkert.orgrun the style hook. It is important.
1735871Snate@binkert.org"""
1746003Snate@binkert.org
1756655Snate@binkert.orghg_info = "Unknown"
176955SN/Aif hgdir.exists():
1775871Snate@binkert.org    # 1) Grab repository revision if we know it.
1785871Snate@binkert.org    cmd = "hg id -n -i -t -b"
1795871Snate@binkert.org    try:
1805871Snate@binkert.org        hg_info = readCommand(cmd, cwd=main.root.abspath).strip()
181955SN/A    except OSError:
1826121Snate@binkert.org        print mercurial_bin_not_found
1836121Snate@binkert.org
1846121Snate@binkert.org    # 2) Ensure that the style hook is in place.
1851533SN/A    try:
1866655Snate@binkert.org        ui = None
1876655Snate@binkert.org        if ARGUMENTS.get('IGNORE_STYLE') != 'True':
1886655Snate@binkert.org            from mercurial import ui
1896655Snate@binkert.org            ui = ui.ui()
1905871Snate@binkert.org    except ImportError:
1915871Snate@binkert.org        print mercurial_lib_not_found
1925863Snate@binkert.org
1935871Snate@binkert.org    if ui is not None:
1945871Snate@binkert.org        ui.readconfig(hgdir.File('hgrc').abspath)
1955871Snate@binkert.org        style_hook = ui.config('hooks', 'pretxncommit.style', None)
1965871Snate@binkert.org
1975871Snate@binkert.org        if not style_hook:
1985863Snate@binkert.org            print mercurial_style_message
1996121Snate@binkert.org            sys.exit(1)
2005863Snate@binkert.orgelse:
2015871Snate@binkert.org    print ".hg directory not found"
2024678Snate@binkert.org
2034678Snate@binkert.orgmain['HG_INFO'] = hg_info
2044678Snate@binkert.org
2054678Snate@binkert.org###################################################
2064678Snate@binkert.org#
2074678Snate@binkert.org# Figure out which configurations to set up based on the path(s) of
2084678Snate@binkert.org# the target(s).
2094678Snate@binkert.org#
2104678Snate@binkert.org###################################################
2114678Snate@binkert.org
2124678Snate@binkert.org# Find default configuration & binary.
2137827Snate@binkert.orgDefault(environ.get('M5_DEFAULT_BINARY', 'build/ALPHA_SE/m5.debug'))
2147827Snate@binkert.org
2156121Snate@binkert.org# helper function: find last occurrence of element in list
2164678Snate@binkert.orgdef rfind(l, elt, offs = -1):
2175871Snate@binkert.org    for i in range(len(l)+offs, 0, -1):
2185871Snate@binkert.org        if l[i] == elt:
2195871Snate@binkert.org            return i
2205871Snate@binkert.org    raise ValueError, "element not found"
2215871Snate@binkert.org
2225871Snate@binkert.org# Each target must have 'build' in the interior of the path; the
2235871Snate@binkert.org# directory below this will determine the build parameters.  For
2245871Snate@binkert.org# example, for target 'foo/bar/build/ALPHA_SE/arch/alpha/blah.do' we
2255871Snate@binkert.org# recognize that ALPHA_SE specifies the configuration because it
2265871Snate@binkert.org# follow 'build' in the bulid path.
2275871Snate@binkert.org
2285871Snate@binkert.org# Generate absolute paths to targets so we can see where the build dir is
2295871Snate@binkert.orgif COMMAND_LINE_TARGETS:
2305990Ssaidi@eecs.umich.edu    # Ask SCons which directory it was invoked from
2315871Snate@binkert.org    launch_dir = GetLaunchDir()
2325871Snate@binkert.org    # Make targets relative to invocation directory
2335871Snate@binkert.org    abs_targets = [ normpath(joinpath(launch_dir, str(x))) for x in \
2344678Snate@binkert.org                    COMMAND_LINE_TARGETS]
2356654Snate@binkert.orgelse:
2365871Snate@binkert.org    # Default targets are relative to root of tree
2375871Snate@binkert.org    abs_targets = [ normpath(joinpath(main.root.abspath, str(x))) for x in \
2385871Snate@binkert.org                    DEFAULT_TARGETS]
2395871Snate@binkert.org
2405871Snate@binkert.org
2415871Snate@binkert.org# Generate a list of the unique build roots and configs that the
2428120Sgblack@eecs.umich.edu# collected targets reference.
2435871Snate@binkert.orgvariant_paths = []
2445871Snate@binkert.orgbuild_root = None
2454678Snate@binkert.orgfor t in abs_targets:
2465871Snate@binkert.org    path_dirs = t.split('/')
2474678Snate@binkert.org    try:
2485871Snate@binkert.org        build_top = rfind(path_dirs, 'build', -2)
2495871Snate@binkert.org    except:
2505871Snate@binkert.org        print "Error: no non-leaf 'build' dir found on target path", t
2515871Snate@binkert.org        Exit(1)
2525871Snate@binkert.org    this_build_root = joinpath('/',*path_dirs[:build_top+1])
2535871Snate@binkert.org    if not build_root:
2545871Snate@binkert.org        build_root = this_build_root
2555871Snate@binkert.org    else:
2565871Snate@binkert.org        if this_build_root != build_root:
2576121Snate@binkert.org            print "Error: build targets not under same build root\n"\
2586121Snate@binkert.org                  "  %s\n  %s" % (build_root, this_build_root)
2595863Snate@binkert.org            Exit(1)
260955SN/A    variant_path = joinpath('/',*path_dirs[:build_top+2])
261955SN/A    if variant_path not in variant_paths:
2622632Sstever@eecs.umich.edu        variant_paths.append(variant_path)
2632632Sstever@eecs.umich.edu
264955SN/A# Make sure build_root exists (might not if this is the first build there)
265955SN/Aif not isdir(build_root):
266955SN/A    mkdir(build_root)
267955SN/A
2685863Snate@binkert.orgExport('main')
269955SN/A
2702632Sstever@eecs.umich.edumain.SConsignFile(joinpath(build_root, "sconsign"))
2712632Sstever@eecs.umich.edu
2722632Sstever@eecs.umich.edu# Default duplicate option is to use hard links, but this messes up
2732632Sstever@eecs.umich.edu# when you use emacs to edit a file in the target dir, as emacs moves
2742632Sstever@eecs.umich.edu# file to file~ then copies to file, breaking the link.  Symbolic
2752632Sstever@eecs.umich.edu# (soft) links work better.
2762632Sstever@eecs.umich.edumain.SetOption('duplicate', 'soft-copy')
2772632Sstever@eecs.umich.edu
2782632Sstever@eecs.umich.edu#
2792632Sstever@eecs.umich.edu# Set up global sticky variables... these are common to an entire build
2802632Sstever@eecs.umich.edu# tree (not specific to a particular build like ALPHA_SE)
2812632Sstever@eecs.umich.edu#
2822632Sstever@eecs.umich.edu
2833718Sstever@eecs.umich.edu# Variable validators & converters for global sticky variables
2843718Sstever@eecs.umich.edudef PathListMakeAbsolute(val):
2853718Sstever@eecs.umich.edu    if not val:
2863718Sstever@eecs.umich.edu        return val
2873718Sstever@eecs.umich.edu    f = lambda p: abspath(expanduser(p))
2885863Snate@binkert.org    return ':'.join(map(f, val.split(':')))
2895863Snate@binkert.org
2903718Sstever@eecs.umich.edudef PathListAllExist(key, val, env):
2913718Sstever@eecs.umich.edu    if not val:
2926121Snate@binkert.org        return
2935863Snate@binkert.org    paths = val.split(':')
2943718Sstever@eecs.umich.edu    for path in paths:
2953718Sstever@eecs.umich.edu        if not isdir(path):
2962634Sstever@eecs.umich.edu            raise SCons.Errors.UserError("Path does not exist: '%s'" % path)
2972634Sstever@eecs.umich.edu
2985863Snate@binkert.orgglobal_sticky_vars_file = joinpath(build_root, 'variables.global')
2992638Sstever@eecs.umich.edu
3002632Sstever@eecs.umich.eduglobal_sticky_vars = Variables(global_sticky_vars_file, args=ARGUMENTS)
3012632Sstever@eecs.umich.edu
3022632Sstever@eecs.umich.eduglobal_sticky_vars.AddVariables(
3032632Sstever@eecs.umich.edu    ('CC', 'C compiler', environ.get('CC', main['CC'])),
3042632Sstever@eecs.umich.edu    ('CXX', 'C++ compiler', environ.get('CXX', main['CXX'])),
3052632Sstever@eecs.umich.edu    ('BATCH', 'Use batch pool for build and tests', False),
3061858SN/A    ('BATCH_CMD', 'Batch pool submission command name', 'qdo'),
3073716Sstever@eecs.umich.edu    ('EXTRAS', 'Add Extra directories to the compilation', '',
3082638Sstever@eecs.umich.edu     PathListAllExist, PathListMakeAbsolute),
3092638Sstever@eecs.umich.edu    BoolVariable('RUBY', 'Build with Ruby', False),
3102638Sstever@eecs.umich.edu    )
3112638Sstever@eecs.umich.edu
3122638Sstever@eecs.umich.edu# base help text
3132638Sstever@eecs.umich.eduhelp_text = '''
3142638Sstever@eecs.umich.eduUsage: scons [scons options] [build options] [target(s)]
3155863Snate@binkert.org
3165863Snate@binkert.orgGlobal sticky options:
3175863Snate@binkert.org'''
318955SN/A
3195341Sstever@gmail.comhelp_text += global_sticky_vars.GenerateHelpText(main)
3205341Sstever@gmail.com
3215863Snate@binkert.org# Update main environment with values from ARGUMENTS & global_sticky_vars_file
3227756SAli.Saidi@ARM.comglobal_sticky_vars.Update(main)
3235341Sstever@gmail.com
3246121Snate@binkert.org# Save sticky variable settings back to current variables file
3254494Ssaidi@eecs.umich.eduglobal_sticky_vars.Save(global_sticky_vars_file, main)
3266121Snate@binkert.org
3271105SN/A# Parse EXTRAS variable to build list of all directories where we're
3282667Sstever@eecs.umich.edu# look for sources etc.  This list is exported as base_dir_list.
3292667Sstever@eecs.umich.edubase_dir = main.srcdir.abspath
3302667Sstever@eecs.umich.eduif main['EXTRAS']:
3312667Sstever@eecs.umich.edu    extras_dir_list = main['EXTRAS'].split(':')
3326121Snate@binkert.orgelse:
3332667Sstever@eecs.umich.edu    extras_dir_list = []
3345341Sstever@gmail.com
3355863Snate@binkert.orgExport('base_dir')
3365341Sstever@gmail.comExport('extras_dir_list')
3375341Sstever@gmail.com
3385341Sstever@gmail.com# the ext directory should be on the #includes path
3395863Snate@binkert.orgmain.Append(CPPPATH=[Dir('ext')])
3405341Sstever@gmail.com
3415341Sstever@gmail.com# M5_PLY is used by isa_parser.py to find the PLY package.
3425341Sstever@gmail.commain.Append(ENV = { 'M5_PLY' : Dir('ext/ply').abspath })
3435863Snate@binkert.org
3445341Sstever@gmail.comCXX_version = readCommand([main['CXX'],'--version'], exception=False)
3455341Sstever@gmail.comCXX_V = readCommand([main['CXX'],'-V'], exception=False)
3465341Sstever@gmail.com
3475341Sstever@gmail.commain['GCC'] = CXX_version and CXX_version.find('g++') >= 0
3485341Sstever@gmail.commain['SUNCC'] = CXX_V and CXX_V.find('Sun C++') >= 0
3495341Sstever@gmail.commain['ICC'] = CXX_V and CXX_V.find('Intel') >= 0
3505341Sstever@gmail.comif main['GCC'] + main['SUNCC'] + main['ICC'] > 1:
3515341Sstever@gmail.com    print 'Error: How can we have two at the same time?'
3525341Sstever@gmail.com    Exit(1)
3535341Sstever@gmail.com
3548120Sgblack@eecs.umich.edu# Set up default C++ compiler flags
3555341Sstever@gmail.comif main['GCC']:
3568120Sgblack@eecs.umich.edu    main.Append(CCFLAGS='-pipe')
3575341Sstever@gmail.com    main.Append(CCFLAGS='-fno-strict-aliasing')
3588120Sgblack@eecs.umich.edu    main.Append(CCFLAGS=['-Wall', '-Wno-sign-compare', '-Wundef'])
3596121Snate@binkert.org    main.Append(CXXFLAGS='-Wno-deprecated')
3606121Snate@binkert.orgelif main['ICC']:
3615397Ssaidi@eecs.umich.edu    pass #Fix me... add warning flags once we clean up icc warnings
3625397Ssaidi@eecs.umich.eduelif main['SUNCC']:
3637727SAli.Saidi@ARM.com    main.Append(CCFLAGS='-Qoption ccfe')
3645341Sstever@gmail.com    main.Append(CCFLAGS='-features=gcc')
3656168Snate@binkert.org    main.Append(CCFLAGS='-features=extensions')
3666168Snate@binkert.org    main.Append(CCFLAGS='-library=stlport4')
3675341Sstever@gmail.com    main.Append(CCFLAGS='-xar')
3688120Sgblack@eecs.umich.edu    #main.Append(CCFLAGS='-instances=semiexplicit')
3698120Sgblack@eecs.umich.eduelse:
3708120Sgblack@eecs.umich.edu    print 'Error: Don\'t know what compiler options to use for your compiler.'
3716814Sgblack@eecs.umich.edu    print '       Please fix SConstruct and src/SConscript and try again.'
3725863Snate@binkert.org    Exit(1)
3738120Sgblack@eecs.umich.edu
3745341Sstever@gmail.com# Set up common yacc/bison flags (needed for Ruby)
3755863Snate@binkert.orgmain['YACCFLAGS'] = '-d'
3765341Sstever@gmail.commain['YACCHXXFILESUFFIX'] = '.hh'
3776121Snate@binkert.org
3786121Snate@binkert.org# Do this after we save setting back, or else we'll tack on an
3796121Snate@binkert.org# extra 'qdo' every time we run scons.
3805742Snate@binkert.orgif main['BATCH']:
3815742Snate@binkert.org    main['CC']     = main['BATCH_CMD'] + ' ' + main['CC']
3825341Sstever@gmail.com    main['CXX']    = main['BATCH_CMD'] + ' ' + main['CXX']
3835742Snate@binkert.org    main['AS']     = main['BATCH_CMD'] + ' ' + main['AS']
3845742Snate@binkert.org    main['AR']     = main['BATCH_CMD'] + ' ' + main['AR']
3855341Sstever@gmail.com    main['RANLIB'] = main['BATCH_CMD'] + ' ' + main['RANLIB']
3866017Snate@binkert.org
3876121Snate@binkert.orgif sys.platform == 'cygwin':
3886017Snate@binkert.org    # cygwin has some header file issues...
3897816Ssteve.reinhardt@amd.com    main.Append(CCFLAGS=Split("-Wno-uninitialized"))
3907756SAli.Saidi@ARM.com
3917756SAli.Saidi@ARM.com# Check for SWIG
3927756SAli.Saidi@ARM.comif not main.has_key('SWIG'):
3937756SAli.Saidi@ARM.com    print 'Error: SWIG utility not found.'
3947756SAli.Saidi@ARM.com    print '       Please install (see http://www.swig.org) and retry.'
3957756SAli.Saidi@ARM.com    Exit(1)
3967756SAli.Saidi@ARM.com
3977756SAli.Saidi@ARM.com# Check for appropriate SWIG version
3987816Ssteve.reinhardt@amd.comswig_version = readCommand(('swig', '-version'), exception='').split()
3997816Ssteve.reinhardt@amd.com# First 3 words should be "SWIG Version x.y.z"
4007816Ssteve.reinhardt@amd.comif len(swig_version) < 3 or \
4017816Ssteve.reinhardt@amd.com        swig_version[0] != 'SWIG' or swig_version[1] != 'Version':
4027816Ssteve.reinhardt@amd.com    print 'Error determining SWIG version.'
4037816Ssteve.reinhardt@amd.com    Exit(1)
4047816Ssteve.reinhardt@amd.com
4057816Ssteve.reinhardt@amd.commin_swig_version = '1.3.28'
4067816Ssteve.reinhardt@amd.comif compareVersions(swig_version[2], min_swig_version) < 0:
4077816Ssteve.reinhardt@amd.com    print 'Error: SWIG version', min_swig_version, 'or newer required.'
4087756SAli.Saidi@ARM.com    print '       Installed version:', swig_version[2]
4097816Ssteve.reinhardt@amd.com    Exit(1)
4107816Ssteve.reinhardt@amd.com
4117816Ssteve.reinhardt@amd.com# Set up SWIG flags & scanner
4127816Ssteve.reinhardt@amd.comswig_flags=Split('-c++ -python -modern -templatereduce $_CPPINCFLAGS')
4137816Ssteve.reinhardt@amd.commain.Append(SWIGFLAGS=swig_flags)
4147816Ssteve.reinhardt@amd.com
4157816Ssteve.reinhardt@amd.com# filter out all existing swig scanners, they mess up the dependency
4167816Ssteve.reinhardt@amd.com# stuff for some reason
4177816Ssteve.reinhardt@amd.comscanners = []
4187816Ssteve.reinhardt@amd.comfor scanner in main['SCANNERS']:
4197816Ssteve.reinhardt@amd.com    skeys = scanner.skeys
4207816Ssteve.reinhardt@amd.com    if skeys == '.i':
4217816Ssteve.reinhardt@amd.com        continue
4227816Ssteve.reinhardt@amd.com
4237816Ssteve.reinhardt@amd.com    if isinstance(skeys, (list, tuple)) and '.i' in skeys:
4247816Ssteve.reinhardt@amd.com        continue
4257816Ssteve.reinhardt@amd.com
4267816Ssteve.reinhardt@amd.com    scanners.append(scanner)
4277816Ssteve.reinhardt@amd.com
4287816Ssteve.reinhardt@amd.com# add the new swig scanner that we like better
4297816Ssteve.reinhardt@amd.comfrom SCons.Scanner import ClassicCPP as CPPScanner
4307816Ssteve.reinhardt@amd.comswig_inc_re = '^[ \t]*[%,#][ \t]*(?:include|import)[ \t]*(<|")([^>"]+)(>|")'
4317816Ssteve.reinhardt@amd.comscanners.append(CPPScanner("SwigScan", [ ".i" ], "CPPPATH", swig_inc_re))
4327816Ssteve.reinhardt@amd.com
4337816Ssteve.reinhardt@amd.com# replace the scanners list that has what we want
4347816Ssteve.reinhardt@amd.commain['SCANNERS'] = scanners
4357816Ssteve.reinhardt@amd.com
4367816Ssteve.reinhardt@amd.com# Add a custom Check function to the Configure context so that we can
4377816Ssteve.reinhardt@amd.com# figure out if the compiler adds leading underscores to global
4387816Ssteve.reinhardt@amd.com# variables.  This is needed for the autogenerated asm files that we
4397816Ssteve.reinhardt@amd.com# use for embedding the python code.
4407816Ssteve.reinhardt@amd.comdef CheckLeading(context):
4417816Ssteve.reinhardt@amd.com    context.Message("Checking for leading underscore in global variables...")
4427816Ssteve.reinhardt@amd.com    # 1) Define a global variable called x from asm so the C compiler
4437816Ssteve.reinhardt@amd.com    #    won't change the symbol at all.
4447816Ssteve.reinhardt@amd.com    # 2) Declare that variable.
4457816Ssteve.reinhardt@amd.com    # 3) Use the variable
4467816Ssteve.reinhardt@amd.com    #
4477816Ssteve.reinhardt@amd.com    # If the compiler prepends an underscore, this will successfully
4487816Ssteve.reinhardt@amd.com    # link because the external symbol 'x' will be called '_x' which
4497816Ssteve.reinhardt@amd.com    # was defined by the asm statement.  If the compiler does not
4507816Ssteve.reinhardt@amd.com    # prepend an underscore, this will not successfully link because
4517816Ssteve.reinhardt@amd.com    # '_x' will have been defined by assembly, while the C portion of
4527816Ssteve.reinhardt@amd.com    # the code will be trying to use 'x'
4537816Ssteve.reinhardt@amd.com    ret = context.TryLink('''
4547816Ssteve.reinhardt@amd.com        asm(".globl _x; _x: .byte 0");
4557816Ssteve.reinhardt@amd.com        extern int x;
4567816Ssteve.reinhardt@amd.com        int main() { return x; }
4577816Ssteve.reinhardt@amd.com        ''', extension=".c")
4587816Ssteve.reinhardt@amd.com    context.env.Append(LEADING_UNDERSCORE=ret)
4597816Ssteve.reinhardt@amd.com    context.Result(ret)
4607816Ssteve.reinhardt@amd.com    return ret
4617816Ssteve.reinhardt@amd.com
4627816Ssteve.reinhardt@amd.com# Platform-specific configuration.  Note again that we assume that all
4637816Ssteve.reinhardt@amd.com# builds under a given build root run on the same host platform.
4647816Ssteve.reinhardt@amd.comconf = Configure(main,
4657816Ssteve.reinhardt@amd.com                 conf_dir = joinpath(build_root, '.scons_config'),
4667816Ssteve.reinhardt@amd.com                 log_file = joinpath(build_root, 'scons_config.log'),
4677816Ssteve.reinhardt@amd.com                 custom_tests = { 'CheckLeading' : CheckLeading })
4687816Ssteve.reinhardt@amd.com
4697816Ssteve.reinhardt@amd.com# Check for leading underscores.  Don't really need to worry either
4707756SAli.Saidi@ARM.com# way so don't need to check the return code.
4718120Sgblack@eecs.umich.educonf.CheckLeading()
4727756SAli.Saidi@ARM.com
4737756SAli.Saidi@ARM.com# Check if we should compile a 64 bit binary on Mac OS X/Darwin
4747756SAli.Saidi@ARM.comtry:
4757756SAli.Saidi@ARM.com    import platform
4767816Ssteve.reinhardt@amd.com    uname = platform.uname()
4777816Ssteve.reinhardt@amd.com    if uname[0] == 'Darwin' and compareVersions(uname[2], '9.0.0') >= 0:
4787816Ssteve.reinhardt@amd.com        if int(readCommand('sysctl -n hw.cpu64bit_capable')[0]):
4797816Ssteve.reinhardt@amd.com            main.Append(CCFLAGS='-arch x86_64')
4807816Ssteve.reinhardt@amd.com            main.Append(CFLAGS='-arch x86_64')
4817816Ssteve.reinhardt@amd.com            main.Append(LINKFLAGS='-arch x86_64')
4827816Ssteve.reinhardt@amd.com            main.Append(ASFLAGS='-arch x86_64')
4837816Ssteve.reinhardt@amd.comexcept:
4847816Ssteve.reinhardt@amd.com    pass
4857816Ssteve.reinhardt@amd.com
4867756SAli.Saidi@ARM.com# Recent versions of scons substitute a "Null" object for Configure()
4877756SAli.Saidi@ARM.com# when configuration isn't necessary, e.g., if the "--help" option is
4886654Snate@binkert.org# present.  Unfortuantely this Null object always returns false,
4896654Snate@binkert.org# breaking all our configuration checks.  We replace it with our own
4905871Snate@binkert.org# more optimistic null object that returns True instead.
4916121Snate@binkert.orgif not conf:
4926121Snate@binkert.org    def NullCheck(*args, **kwargs):
4936121Snate@binkert.org        return True
4946121Snate@binkert.org
4953940Ssaidi@eecs.umich.edu    class NullConf:
4963918Ssaidi@eecs.umich.edu        def __init__(self, env):
4973918Ssaidi@eecs.umich.edu            self.env = env
4981858SN/A        def Finish(self):
4996121Snate@binkert.org            return self.env
5007739Sgblack@eecs.umich.edu        def __getattr__(self, mname):
5017739Sgblack@eecs.umich.edu            return NullCheck
5026143Snate@binkert.org
5037739Sgblack@eecs.umich.edu    conf = NullConf(main)
5047618SAli.Saidi@arm.com
5057618SAli.Saidi@arm.com# Find Python include and library directories for embedding the
5067618SAli.Saidi@arm.com# interpreter.  For consistency, we will use the same Python
5077618SAli.Saidi@arm.com# installation used to run scons (and thus this script).  If you want
5087618SAli.Saidi@arm.com# to link in an alternate version, see above for instructions on how
5097618SAli.Saidi@arm.com# to invoke scons with a different copy of the Python interpreter.
5107618SAli.Saidi@arm.comfrom distutils import sysconfig
5117739Sgblack@eecs.umich.edu
5126121Snate@binkert.orgpy_getvar = sysconfig.get_config_var
5133940Ssaidi@eecs.umich.edu
5146121Snate@binkert.orgpy_version = 'python' + py_getvar('VERSION')
5157739Sgblack@eecs.umich.edu
5167739Sgblack@eecs.umich.edupy_general_include = sysconfig.get_python_inc()
5177739Sgblack@eecs.umich.edupy_platform_include = sysconfig.get_python_inc(plat_specific=True)
5187739Sgblack@eecs.umich.edupy_includes = [ py_general_include ]
5197739Sgblack@eecs.umich.eduif py_platform_include != py_general_include:
5207739Sgblack@eecs.umich.edu    py_includes.append(py_platform_include)
5213918Ssaidi@eecs.umich.edu
5223918Ssaidi@eecs.umich.edupy_lib_path = [ py_getvar('LIBDIR') ]
5233940Ssaidi@eecs.umich.edu# add the prefix/lib/pythonX.Y/config dir, but only if there is no
5243918Ssaidi@eecs.umich.edu# shared library in prefix/lib/.
5253918Ssaidi@eecs.umich.eduif not py_getvar('Py_ENABLE_SHARED'):
5266157Snate@binkert.org    py_lib_path.append(py_getvar('LIBPL'))
5276157Snate@binkert.org
5286157Snate@binkert.orgpy_libs = []
5296157Snate@binkert.orgfor lib in py_getvar('LIBS').split() + py_getvar('SYSLIBS').split():
5305397Ssaidi@eecs.umich.edu    assert lib.startswith('-l')
5315397Ssaidi@eecs.umich.edu    lib = lib[2:]   
5326121Snate@binkert.org    if lib not in py_libs:
5336121Snate@binkert.org        py_libs.append(lib)
5346121Snate@binkert.orgpy_libs.append(py_version)
5356121Snate@binkert.org
5366121Snate@binkert.orgmain.Append(CPPPATH=py_includes)
5376121Snate@binkert.orgmain.Append(LIBPATH=py_lib_path)
5385397Ssaidi@eecs.umich.edu
5391851SN/A# verify that this stuff works
5401851SN/Aif not conf.CheckHeader('Python.h', '<>'):
5417739Sgblack@eecs.umich.edu    print "Error: can't find Python.h header in", py_includes
542955SN/A    Exit(1)
5433053Sstever@eecs.umich.edu
5446121Snate@binkert.orgfor lib in py_libs:
5453053Sstever@eecs.umich.edu    if not conf.CheckLib(lib):
5463053Sstever@eecs.umich.edu        print "Error: can't find library %s required by python" % lib
5473053Sstever@eecs.umich.edu        Exit(1)
5483053Sstever@eecs.umich.edu
5493053Sstever@eecs.umich.edu# On Solaris you need to use libsocket for socket ops
5506654Snate@binkert.orgif not conf.CheckLibWithHeader(None, 'sys/socket.h', 'C++', 'accept(0,0,0);'):
5513053Sstever@eecs.umich.edu   if not conf.CheckLibWithHeader('socket', 'sys/socket.h', 'C++', 'accept(0,0,0);'):
5524742Sstever@eecs.umich.edu       print "Can't find library with socket calls (e.g. accept())"
5534742Sstever@eecs.umich.edu       Exit(1)
5543053Sstever@eecs.umich.edu
5553053Sstever@eecs.umich.edu# Check for zlib.  If the check passes, libz will be automatically
5563053Sstever@eecs.umich.edu# added to the LIBS environment variable.
5573053Sstever@eecs.umich.eduif not conf.CheckLibWithHeader('z', 'zlib.h', 'C++','zlibVersion();'):
5586654Snate@binkert.org    print 'Error: did not find needed zlib compression library '\
5593053Sstever@eecs.umich.edu          'and/or zlib.h header file.'
5603053Sstever@eecs.umich.edu    print '       Please install zlib and try again.'
5613053Sstever@eecs.umich.edu    Exit(1)
5623053Sstever@eecs.umich.edu
5632667Sstever@eecs.umich.edu# Check for <fenv.h> (C99 FP environment control)
5644554Sbinkertn@umich.eduhave_fenv = conf.CheckHeader('fenv.h', '<>')
5656121Snate@binkert.orgif not have_fenv:
5662667Sstever@eecs.umich.edu    print "Warning: Header file <fenv.h> not found."
5674554Sbinkertn@umich.edu    print "         This host has no IEEE FP rounding mode control."
5684554Sbinkertn@umich.edu
5694554Sbinkertn@umich.edu######################################################################
5706121Snate@binkert.org#
5714554Sbinkertn@umich.edu# Check for mysql.
5724554Sbinkertn@umich.edu#
5734554Sbinkertn@umich.edumysql_config = WhereIs('mysql_config')
5744781Snate@binkert.orghave_mysql = bool(mysql_config)
5754554Sbinkertn@umich.edu
5764554Sbinkertn@umich.edu# Check MySQL version.
5772667Sstever@eecs.umich.eduif have_mysql:
5784554Sbinkertn@umich.edu    mysql_version = readCommand(mysql_config + ' --version')
5794554Sbinkertn@umich.edu    min_mysql_version = '4.1'
5804554Sbinkertn@umich.edu    if compareVersions(mysql_version, min_mysql_version) < 0:
5814554Sbinkertn@umich.edu        print 'Warning: MySQL', min_mysql_version, 'or newer required.'
5822667Sstever@eecs.umich.edu        print '         Version', mysql_version, 'detected.'
5834554Sbinkertn@umich.edu        have_mysql = False
5842667Sstever@eecs.umich.edu
5854554Sbinkertn@umich.edu# Set up mysql_config commands.
5866121Snate@binkert.orgif have_mysql:
5872667Sstever@eecs.umich.edu    mysql_config_include = mysql_config + ' --include'
5885522Snate@binkert.org    if os.system(mysql_config_include + ' > /dev/null') != 0:
5895522Snate@binkert.org        # older mysql_config versions don't support --include, use
5905522Snate@binkert.org        # --cflags instead
5915522Snate@binkert.org        mysql_config_include = mysql_config + ' --cflags | sed s/\\\'//g'
5925522Snate@binkert.org    # This seems to work in all versions
5935522Snate@binkert.org    mysql_config_libs = mysql_config + ' --libs'
5945522Snate@binkert.org
5955522Snate@binkert.org######################################################################
5965522Snate@binkert.org#
5975522Snate@binkert.org# Finish the configuration
5985522Snate@binkert.org#
5995522Snate@binkert.orgmain = conf.Finish()
6005522Snate@binkert.org
6015522Snate@binkert.org######################################################################
6025522Snate@binkert.org#
6035522Snate@binkert.org# Collect all non-global variables
6045522Snate@binkert.org#
6055522Snate@binkert.org
6065522Snate@binkert.org# Define the universe of supported ISAs
6075522Snate@binkert.orgall_isa_list = [ ]
6085522Snate@binkert.orgExport('all_isa_list')
6095522Snate@binkert.org
6105522Snate@binkert.org# Define the universe of supported CPU models
6115522Snate@binkert.orgall_cpu_list = [ ]
6125522Snate@binkert.orgdefault_cpus = [ ]
6135522Snate@binkert.orgExport('all_cpu_list', 'default_cpus')
6142638Sstever@eecs.umich.edu
6152638Sstever@eecs.umich.edu# Sticky variables get saved in the variables file so they persist from
6166121Snate@binkert.org# one invocation to the next (unless overridden, in which case the new
6173716Sstever@eecs.umich.edu# value becomes sticky).
6185522Snate@binkert.orgsticky_vars = Variables(args=ARGUMENTS)
6195522Snate@binkert.orgExport('sticky_vars')
6205522Snate@binkert.org
6215522Snate@binkert.org# Sticky variables that should be exported
6225522Snate@binkert.orgexport_vars = []
6235522Snate@binkert.orgExport('export_vars')
6241858SN/A
6255227Ssaidi@eecs.umich.edu# Non-sticky variables only apply to the current build.
6265227Ssaidi@eecs.umich.edunonsticky_vars = Variables(args=ARGUMENTS)
6275227Ssaidi@eecs.umich.eduExport('nonsticky_vars')
6285227Ssaidi@eecs.umich.edu
6296654Snate@binkert.org# Walk the tree and execute all SConsopts scripts that wil add to the
6306654Snate@binkert.org# above variables
6317769SAli.Saidi@ARM.comfor bdir in [ base_dir ] + extras_dir_list:
6327769SAli.Saidi@ARM.com    for root, dirs, files in os.walk(bdir):
6337769SAli.Saidi@ARM.com        if 'SConsopts' in files:
6347769SAli.Saidi@ARM.com            print "Reading", joinpath(root, 'SConsopts')
6355227Ssaidi@eecs.umich.edu            SConscript(joinpath(root, 'SConsopts'))
6365227Ssaidi@eecs.umich.edu
6375227Ssaidi@eecs.umich.eduall_isa_list.sort()
6385204Sstever@gmail.comall_cpu_list.sort()
6395204Sstever@gmail.comdefault_cpus.sort()
6405204Sstever@gmail.com
6415204Sstever@gmail.comsticky_vars.AddVariables(
6425204Sstever@gmail.com    EnumVariable('TARGET_ISA', 'Target ISA', 'alpha', all_isa_list),
6435204Sstever@gmail.com    BoolVariable('FULL_SYSTEM', 'Full-system support', False),
6445204Sstever@gmail.com    ListVariable('CPU_MODELS', 'CPU models', default_cpus, all_cpu_list),
6455204Sstever@gmail.com    BoolVariable('NO_FAST_ALLOC', 'Disable fast object allocator', False),
6465204Sstever@gmail.com    BoolVariable('FAST_ALLOC_DEBUG', 'Enable fast object allocator debugging',
6475204Sstever@gmail.com                 False),
6485204Sstever@gmail.com    BoolVariable('FAST_ALLOC_STATS', 'Enable fast object allocator statistics',
6495204Sstever@gmail.com                 False),
6505204Sstever@gmail.com    BoolVariable('EFENCE', 'Link with Electric Fence malloc debugger',
6515204Sstever@gmail.com                 False),
6525204Sstever@gmail.com    BoolVariable('SS_COMPATIBLE_FP',
6535204Sstever@gmail.com                 'Make floating-point results compatible with SimpleScalar',
6545204Sstever@gmail.com                 False),
6556121Snate@binkert.org    BoolVariable('USE_SSE2',
6565204Sstever@gmail.com                 'Compile for SSE2 (-msse2) to get IEEE FP on x86 hosts',
6573118Sstever@eecs.umich.edu                 False),
6583118Sstever@eecs.umich.edu    BoolVariable('USE_MYSQL', 'Use MySQL for stats output', have_mysql),
6593118Sstever@eecs.umich.edu    BoolVariable('USE_FENV', 'Use <fenv.h> IEEE mode control', have_fenv),
6603118Sstever@eecs.umich.edu    BoolVariable('USE_CHECKER', 'Use checker for detailed CPU models', False),
6613118Sstever@eecs.umich.edu    BoolVariable('CP_ANNOTATE', 'Enable critical path annotation capability', False),
6625863Snate@binkert.org    )
6633118Sstever@eecs.umich.edu
6645863Snate@binkert.orgnonsticky_vars.AddVariables(
6653118Sstever@eecs.umich.edu    BoolVariable('update_ref', 'Update test reference outputs', False)
6667457Snate@binkert.org    )
6677457Snate@binkert.org
6685863Snate@binkert.org# These variables get exported to #defines in config/*.hh (see src/SConscript).
6695863Snate@binkert.orgexport_vars += ['FULL_SYSTEM', 'USE_FENV', 'USE_MYSQL',
6705863Snate@binkert.org                'NO_FAST_ALLOC', 'FAST_ALLOC_DEBUG', 'FAST_ALLOC_STATS',
6715863Snate@binkert.org                'SS_COMPATIBLE_FP', 'USE_CHECKER', 'TARGET_ISA', 'CP_ANNOTATE']
6725863Snate@binkert.org
6735863Snate@binkert.org###################################################
6745863Snate@binkert.org#
6756003Snate@binkert.org# Define a SCons builder for configuration flag headers.
6765863Snate@binkert.org#
6775863Snate@binkert.org###################################################
6785863Snate@binkert.org
6796120Snate@binkert.org# This function generates a config header file that #defines the
6805863Snate@binkert.org# variable symbol to the current variable setting (0 or 1).  The source
6815863Snate@binkert.org# operands are the name of the variable and a Value node containing the
6825863Snate@binkert.org# value of the variable.
6836120Snate@binkert.orgdef build_config_file(target, source, env):
6846120Snate@binkert.org    (variable, value) = [s.get_contents() for s in source]
6855863Snate@binkert.org    f = file(str(target[0]), 'w')
6865863Snate@binkert.org    print >> f, '#define', variable, value
6876120Snate@binkert.org    f.close()
6885863Snate@binkert.org    return None
6896121Snate@binkert.org
6906121Snate@binkert.org# Generate the message to be printed when building the config file.
6915863Snate@binkert.orgdef build_config_file_string(target, source, env):
6927727SAli.Saidi@ARM.com    (variable, value) = [s.get_contents() for s in source]
6937727SAli.Saidi@ARM.com    return "Defining %s as %s in %s." % (variable, value, target[0])
6947727SAli.Saidi@ARM.com
6957727SAli.Saidi@ARM.com# Combine the two functions into a scons Action object.
6967727SAli.Saidi@ARM.comconfig_action = Action(build_config_file, build_config_file_string)
6977727SAli.Saidi@ARM.com
6985863Snate@binkert.org# The emitter munges the source & target node lists to reflect what
6993118Sstever@eecs.umich.edu# we're really doing.
7005863Snate@binkert.orgdef config_emitter(target, source, env):
7013118Sstever@eecs.umich.edu    # extract variable name from Builder arg
7023118Sstever@eecs.umich.edu    variable = str(target[0])
7035863Snate@binkert.org    # True target is config header file
7045863Snate@binkert.org    target = joinpath('config', variable.lower() + '.hh')
7055863Snate@binkert.org    val = env[variable]
7065863Snate@binkert.org    if isinstance(val, bool):
7073118Sstever@eecs.umich.edu        # Force value to 0/1
7083483Ssaidi@eecs.umich.edu        val = int(val)
7093494Ssaidi@eecs.umich.edu    elif isinstance(val, str):
7103494Ssaidi@eecs.umich.edu        val = '"' + val + '"'
7113483Ssaidi@eecs.umich.edu
7123483Ssaidi@eecs.umich.edu    # Sources are variable name & value (packaged in SCons Value nodes)
7133483Ssaidi@eecs.umich.edu    return ([target], [Value(variable), Value(val)])
7143053Sstever@eecs.umich.edu
7153053Sstever@eecs.umich.educonfig_builder = Builder(emitter = config_emitter, action = config_action)
7163918Ssaidi@eecs.umich.edu
7173053Sstever@eecs.umich.edumain.Append(BUILDERS = { 'ConfigFile' : config_builder })
7183053Sstever@eecs.umich.edu
7193053Sstever@eecs.umich.edu# libelf build is shared across all configs in the build root.
7203053Sstever@eecs.umich.edumain.SConscript('ext/libelf/SConscript',
7213053Sstever@eecs.umich.edu                variant_dir = joinpath(build_root, 'libelf'))
7227840Snate@binkert.org
7237865Sgblack@eecs.umich.edu# gzstream build is shared across all configs in the build root.
7247865Sgblack@eecs.umich.edumain.SConscript('ext/gzstream/SConscript',
7257865Sgblack@eecs.umich.edu                variant_dir = joinpath(build_root, 'gzstream'))
7267865Sgblack@eecs.umich.edu
7277865Sgblack@eecs.umich.edu###################################################
7287840Snate@binkert.org#
7297840Snate@binkert.org# This function is used to set up a directory with switching headers
7307840Snate@binkert.org#
7317840Snate@binkert.org###################################################
7321858SN/A
7331858SN/Amain['ALL_ISA_LIST'] = all_isa_list
7341858SN/Adef make_switching_dir(dname, switch_headers, env):
7351858SN/A    # Generate the header.  target[0] is the full path of the output
7361858SN/A    # header to generate.  'source' is a dummy variable, since we get the
7371858SN/A    # list of ISAs from env['ALL_ISA_LIST'].
7385863Snate@binkert.org    def gen_switch_hdr(target, source, env):
7395863Snate@binkert.org        fname = str(target[0])
7401859SN/A        bname = basename(fname)
7415863Snate@binkert.org        f = open(fname, 'w')
7421858SN/A        f.write('#include "arch/isa_specific.hh"\n')
7435863Snate@binkert.org        cond = '#if'
7441858SN/A        for isa in all_isa_list:
7451859SN/A            f.write('%s THE_ISA == %s_ISA\n#include "%s/%s/%s"\n'
7461859SN/A                    % (cond, isa.upper(), dname, isa, bname))
7476654Snate@binkert.org            cond = '#elif'
7483053Sstever@eecs.umich.edu        f.write('#else\n#error "THE_ISA not set"\n#endif\n')
7496654Snate@binkert.org        f.close()
7503053Sstever@eecs.umich.edu        return 0
7513053Sstever@eecs.umich.edu
7521859SN/A    # String to print when generating header
7531859SN/A    def gen_switch_hdr_string(target, source, env):
7541859SN/A        return "Generating switch header " + str(target[0])
7551859SN/A
7561859SN/A    # Build SCons Action object. 'varlist' specifies env vars that this
7571859SN/A    # action depends on; when env['ALL_ISA_LIST'] changes these actions
7581859SN/A    # should get re-executed.
7591859SN/A    switch_hdr_action = Action(gen_switch_hdr, gen_switch_hdr_string,
7601862SN/A                               varlist=['ALL_ISA_LIST'])
7611859SN/A
7621859SN/A    # Instantiate actions for each header
7631859SN/A    for hdr in switch_headers:
7645863Snate@binkert.org        env.Command(hdr, [], switch_hdr_action)
7655863Snate@binkert.orgExport('make_switching_dir')
7665863Snate@binkert.org
7675863Snate@binkert.org###################################################
7686121Snate@binkert.org#
7691858SN/A# Define build environments for selected configurations.
7705863Snate@binkert.org#
7715863Snate@binkert.org###################################################
7725863Snate@binkert.org
7735863Snate@binkert.orgfor variant_path in variant_paths:
7745863Snate@binkert.org    print "Building in", variant_path
7752139SN/A
7764202Sbinkertn@umich.edu    # Make a copy of the build-root environment to use for this config.
7774202Sbinkertn@umich.edu    env = main.Clone()
7782139SN/A    env['BUILDDIR'] = variant_path
7796994Snate@binkert.org
7806994Snate@binkert.org    # variant_dir is the tail component of build path, and is used to
7816994Snate@binkert.org    # determine the build parameters (e.g., 'ALPHA_SE')
7826994Snate@binkert.org    (build_root, variant_dir) = splitpath(variant_path)
7836994Snate@binkert.org
7846994Snate@binkert.org    # Set env variables according to the build directory config.
7856994Snate@binkert.org    sticky_vars.files = []
7866994Snate@binkert.org    # Variables for $BUILD_ROOT/$VARIANT_DIR are stored in
7876994Snate@binkert.org    # $BUILD_ROOT/variables/$VARIANT_DIR so you can nuke
7886994Snate@binkert.org    # $BUILD_ROOT/$VARIANT_DIR without losing your variables settings.
7896994Snate@binkert.org    current_vars_file = joinpath(build_root, 'variables', variant_dir)
7906994Snate@binkert.org    if isfile(current_vars_file):
7916994Snate@binkert.org        sticky_vars.files.append(current_vars_file)
7926994Snate@binkert.org        print "Using saved variables file %s" % current_vars_file
7936994Snate@binkert.org    else:
7946994Snate@binkert.org        # Build dir-specific variables file doesn't exist.
7956994Snate@binkert.org
7966994Snate@binkert.org        # Make sure the directory is there so we can create it later
7976994Snate@binkert.org        opt_dir = dirname(current_vars_file)
7986994Snate@binkert.org        if not isdir(opt_dir):
7996994Snate@binkert.org            mkdir(opt_dir)
8006994Snate@binkert.org
8016994Snate@binkert.org        # Get default build variables from source tree.  Variables are
8026994Snate@binkert.org        # normally determined by name of $VARIANT_DIR, but can be
8036994Snate@binkert.org        # overriden by 'default=' arg on command line.
8046994Snate@binkert.org        default_vars_file = joinpath('build_opts',
8056994Snate@binkert.org                                     ARGUMENTS.get('default', variant_dir))
8066994Snate@binkert.org        if isfile(default_vars_file):
8072155SN/A            sticky_vars.files.append(default_vars_file)
8085863Snate@binkert.org            print "Variables file %s not found,\n  using defaults in %s" \
8091869SN/A                  % (current_vars_file, default_vars_file)
8101869SN/A        else:
8115863Snate@binkert.org            print "Error: cannot find variables file %s or %s" \
8125863Snate@binkert.org                  % (current_vars_file, default_vars_file)
8134202Sbinkertn@umich.edu            Exit(1)
8146108Snate@binkert.org
8156108Snate@binkert.org    # Apply current variable settings to env
8166108Snate@binkert.org    sticky_vars.Update(env)
8176108Snate@binkert.org    nonsticky_vars.Update(env)
8184202Sbinkertn@umich.edu
8195863Snate@binkert.org    help_text += "\nSticky variables for %s:\n" % variant_dir \
8205742Snate@binkert.org                 + sticky_vars.GenerateHelpText(env) \
8215742Snate@binkert.org                 + "\nNon-sticky variables for %s:\n" % variant_dir \
8225341Sstever@gmail.com                 + nonsticky_vars.GenerateHelpText(env)
8235342Sstever@gmail.com
8245342Sstever@gmail.com    # Process variable settings.
8254202Sbinkertn@umich.edu
8264202Sbinkertn@umich.edu    if not have_fenv and env['USE_FENV']:
8274202Sbinkertn@umich.edu        print "Warning: <fenv.h> not available; " \
8285863Snate@binkert.org              "forcing USE_FENV to False in", variant_dir + "."
8295863Snate@binkert.org        env['USE_FENV'] = False
8305863Snate@binkert.org
8316994Snate@binkert.org    if not env['USE_FENV']:
8326994Snate@binkert.org        print "Warning: No IEEE FP rounding mode control in", variant_dir + "."
8336994Snate@binkert.org        print "         FP results may deviate slightly from other platforms."
8345863Snate@binkert.org
8355863Snate@binkert.org    if env['EFENCE']:
8365863Snate@binkert.org        env.Append(LIBS=['efence'])
8375863Snate@binkert.org
8385863Snate@binkert.org    if env['USE_MYSQL']:
8395863Snate@binkert.org        if not have_mysql:
8405863Snate@binkert.org            print "Warning: MySQL not available; " \
8415863Snate@binkert.org                  "forcing USE_MYSQL to False in", variant_dir + "."
8425863Snate@binkert.org            env['USE_MYSQL'] = False
8435863Snate@binkert.org        else:
8445863Snate@binkert.org            print "Compiling in", variant_dir, "with MySQL support."
8455863Snate@binkert.org            env.ParseConfig(mysql_config_libs)
8465863Snate@binkert.org            env.ParseConfig(mysql_config_include)
8475863Snate@binkert.org
8487840Snate@binkert.org    # Save sticky variable settings back to current variables file
8495863Snate@binkert.org    sticky_vars.Save(current_vars_file, env)
8505863Snate@binkert.org
8515952Ssaidi@eecs.umich.edu    if env['USE_SSE2']:
8527450Sstever@gmail.com        env.Append(CCFLAGS='-msse2')
8531869SN/A
8541858SN/A    # The src/SConscript file sets up the build rules in 'env' according
8555863Snate@binkert.org    # to the configured variables.  It returns a list of environments,
8566108Snate@binkert.org    # one for each variant build (debug, opt, etc.)
8576108Snate@binkert.org    envList = SConscript('src/SConscript', variant_dir = variant_path,
8587840Snate@binkert.org                         exports = 'env')
8597840Snate@binkert.org
8601858SN/A    # Set up the regression tests for each build.
861955SN/A    for e in envList:
862955SN/A        SConscript('tests/SConscript',
8631869SN/A                   variant_dir = joinpath(variant_path, 'tests', e.Label),
8641869SN/A                   exports = { 'env' : e }, duplicate = False)
8651869SN/A
8661869SN/AHelp(help_text)
8671869SN/A