SConstruct revision 5588
1955SN/A# -*- mode:python -*-
2955SN/A
37816Ssteve.reinhardt@amd.com# Copyright (c) 2004-2005 The Regents of The University of Michigan
45871Snate@binkert.org# All rights reserved.
51762SN/A#
6955SN/A# Redistribution and use in source and binary forms, with or without
7955SN/A# modification, are permitted provided that the following conditions are
8955SN/A# met: redistributions of source code must retain the above copyright
9955SN/A# notice, this list of conditions and the following disclaimer;
10955SN/A# redistributions in binary form must reproduce the above copyright
11955SN/A# notice, this list of conditions and the following disclaimer in the
12955SN/A# documentation and/or other materials provided with the distribution;
13955SN/A# neither the name of the copyright holders nor the names of its
14955SN/A# contributors may be used to endorse or promote products derived from
15955SN/A# this software without specific prior written permission.
16955SN/A#
17955SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18955SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19955SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20955SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21955SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22955SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23955SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24955SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25955SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26955SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27955SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28955SN/A#
29955SN/A# Authors: Steve Reinhardt
302665Ssaidi@eecs.umich.edu
312665Ssaidi@eecs.umich.edu###################################################
325863Snate@binkert.org#
33955SN/A# SCons top-level build description (SConstruct) file.
34955SN/A#
35955SN/A# While in this directory ('m5'), just type 'scons' to build the default
36955SN/A# configuration (see below), or type 'scons build/<CONFIG>/<binary>'
37955SN/A# to build some other configuration (e.g., 'build/ALPHA_FS/m5.opt' for
388878Ssteve.reinhardt@amd.com# the optimized full-system version).
392632Sstever@eecs.umich.edu#
408878Ssteve.reinhardt@amd.com# You can build M5 in a different directory as long as there is a
412632Sstever@eecs.umich.edu# 'build/<CONFIG>' somewhere along the target path.  The build system
42955SN/A# expects that all configs under the same build directory are being
438878Ssteve.reinhardt@amd.com# built for the same host system.
442632Sstever@eecs.umich.edu#
452761Sstever@eecs.umich.edu# Examples:
462632Sstever@eecs.umich.edu#
472632Sstever@eecs.umich.edu#   The following two commands are equivalent.  The '-u' option tells
482632Sstever@eecs.umich.edu#   scons to search up the directory tree for this SConstruct file.
492761Sstever@eecs.umich.edu#   % cd <path-to-src>/m5 ; scons build/ALPHA_FS/m5.debug
502761Sstever@eecs.umich.edu#   % cd <path-to-src>/m5/build/ALPHA_FS; scons -u m5.debug
512761Sstever@eecs.umich.edu#
528878Ssteve.reinhardt@amd.com#   The following two commands are equivalent and demonstrate building
538878Ssteve.reinhardt@amd.com#   in a directory outside of the source tree.  The '-C' option tells
542761Sstever@eecs.umich.edu#   scons to chdir to the specified directory to find this SConstruct
552761Sstever@eecs.umich.edu#   file.
562761Sstever@eecs.umich.edu#   % cd <path-to-src>/m5 ; scons /local/foo/build/ALPHA_FS/m5.debug
572761Sstever@eecs.umich.edu#   % cd /local/foo/build/ALPHA_FS; scons -C <path-to-src>/m5 m5.debug
582761Sstever@eecs.umich.edu#
598878Ssteve.reinhardt@amd.com# You can use 'scons -H' to print scons options.  If you're in this
608878Ssteve.reinhardt@amd.com# 'm5' directory (or use -u or -C to tell scons where to find this
612632Sstever@eecs.umich.edu# file), you can use 'scons -h' to print all the M5-specific build
622632Sstever@eecs.umich.edu# options as well.
638878Ssteve.reinhardt@amd.com#
648878Ssteve.reinhardt@amd.com###################################################
652632Sstever@eecs.umich.edu
66955SN/Aimport sys
67955SN/Aimport os
68955SN/Aimport re
695863Snate@binkert.org
705863Snate@binkert.orgfrom os.path import isdir, isfile, join as joinpath
715863Snate@binkert.org
725863Snate@binkert.orgimport SCons
735863Snate@binkert.org
745863Snate@binkert.org# Check for recent-enough Python and SCons versions.  If your system's
755863Snate@binkert.org# default installation of Python is not recent enough, you can use a
765863Snate@binkert.org# non-default installation of the Python interpreter by either (1)
775863Snate@binkert.org# rearranging your PATH so that scons finds the non-default 'python'
785863Snate@binkert.org# first or (2) explicitly invoking an alternative interpreter on the
795863Snate@binkert.org# scons script, e.g., "/usr/local/bin/python2.4 `which scons` [args]".
808878Ssteve.reinhardt@amd.comEnsurePythonVersion(2,4)
815863Snate@binkert.org
825863Snate@binkert.org# Import subprocess after we check the version since it doesn't exist in
835863Snate@binkert.org# Python < 2.4.
845863Snate@binkert.orgimport subprocess
855863Snate@binkert.org
865863Snate@binkert.org# helper function: compare arrays or strings of version numbers.
875863Snate@binkert.org# E.g., compare_version((1,3,25), (1,4,1)')
885863Snate@binkert.org# returns -1, 0, 1 if v1 is <, ==, > v2
895863Snate@binkert.orgdef compare_versions(v1, v2):
905863Snate@binkert.org    def make_version_list(v):
915863Snate@binkert.org        if isinstance(v, (list,tuple)):
925863Snate@binkert.org            return v
935863Snate@binkert.org        elif isinstance(v, str):
945863Snate@binkert.org            return map(lambda x: int(re.match('\d+', x).group()), v.split('.'))
955863Snate@binkert.org        else:
968878Ssteve.reinhardt@amd.com            raise TypeError
975863Snate@binkert.org
985863Snate@binkert.org    v1 = make_version_list(v1)
995863Snate@binkert.org    v2 = make_version_list(v2)
1006654Snate@binkert.org    # Compare corresponding elements of lists
101955SN/A    for n1,n2 in zip(v1, v2):
1025396Ssaidi@eecs.umich.edu        if n1 < n2: return -1
1035863Snate@binkert.org        if n1 > n2: return  1
1045863Snate@binkert.org    # all corresponding values are equal... see if one has extra values
1054202Sbinkertn@umich.edu    if len(v1) < len(v2): return -1
1065863Snate@binkert.org    if len(v1) > len(v2): return  1
1075863Snate@binkert.org    return 0
1085863Snate@binkert.org
1095863Snate@binkert.org# SCons version numbers need special processing because they can have
110955SN/A# charecters and an release date embedded in them. This function does
1116654Snate@binkert.org# the magic to extract them in a similar way to the SCons internal function
1125273Sstever@gmail.com# function does and then checks that the current version is not contained in
1135871Snate@binkert.org# a list of version tuples (bad_ver_strs)
1145273Sstever@gmail.comdef CheckSCons(bad_ver_strs):
1156655Snate@binkert.org    def scons_ver(v):
1168878Ssteve.reinhardt@amd.com        num_parts = v.split(' ')[0].split('.')
1176655Snate@binkert.org        major = int(num_parts[0])
1186655Snate@binkert.org        minor = int(re.match('\d+', num_parts[1]).group())
1196655Snate@binkert.org        rev = 0
1206655Snate@binkert.org        rdate = 0
1215871Snate@binkert.org        if len(num_parts) > 2:
1226654Snate@binkert.org            try: rev = int(re.match('\d+', num_parts[2]).group())
1238947Sandreas.hansson@arm.com            except: pass
1245396Ssaidi@eecs.umich.edu            rev_parts = num_parts[2].split('d')
1258120Sgblack@eecs.umich.edu            if len(rev_parts) > 1:
1268120Sgblack@eecs.umich.edu                rdate = int(re.match('\d+', rev_parts[1]).group())
1278120Sgblack@eecs.umich.edu
1288120Sgblack@eecs.umich.edu        return (major, minor, rev, rdate)
1298120Sgblack@eecs.umich.edu
1308120Sgblack@eecs.umich.edu    sc_ver = scons_ver(SCons.__version__)
1318120Sgblack@eecs.umich.edu    for bad_ver in bad_ver_strs:
1328120Sgblack@eecs.umich.edu        bv = (scons_ver(bad_ver[0]), scons_ver(bad_ver[1]))
1338879Ssteve.reinhardt@amd.com        if  compare_versions(sc_ver, bv[0]) != -1 and\
1348879Ssteve.reinhardt@amd.com            compare_versions(sc_ver, bv[1]) != 1:
1358879Ssteve.reinhardt@amd.com            print "The version of SCons that you have installed: ", SCons.__version__
1368879Ssteve.reinhardt@amd.com            print "has a bug that prevents it from working correctly with M5."
1378879Ssteve.reinhardt@amd.com            print "Please install a version NOT contained within the following",
1388879Ssteve.reinhardt@amd.com            print "ranges (inclusive):"
1398879Ssteve.reinhardt@amd.com            for bad_ver in bad_ver_strs:
1408879Ssteve.reinhardt@amd.com                print "    %s - %s" % bad_ver
1418879Ssteve.reinhardt@amd.com            Exit(2)
1428879Ssteve.reinhardt@amd.com
1438879Ssteve.reinhardt@amd.comCheckSCons(( 
1448879Ssteve.reinhardt@amd.com    # We need a version that is 0.96.91 or newer
1458879Ssteve.reinhardt@amd.com    ('0.0.0', '0.96.90'), 
1468120Sgblack@eecs.umich.edu    ))
1478120Sgblack@eecs.umich.edu
1488120Sgblack@eecs.umich.edu
1498120Sgblack@eecs.umich.edu# The absolute path to the current directory (where this file lives).
1508120Sgblack@eecs.umich.eduROOT = Dir('.').abspath
1518120Sgblack@eecs.umich.edu
1528120Sgblack@eecs.umich.edu# Path to the M5 source tree.
1538120Sgblack@eecs.umich.eduSRCDIR = joinpath(ROOT, 'src')
1548120Sgblack@eecs.umich.edu
1558120Sgblack@eecs.umich.edu# tell python where to find m5 python code
1568120Sgblack@eecs.umich.edusys.path.append(joinpath(ROOT, 'src/python'))
1578120Sgblack@eecs.umich.edu
1588120Sgblack@eecs.umich.edudef check_style_hook(ui):
1598120Sgblack@eecs.umich.edu    ui.readconfig(joinpath(ROOT, '.hg', 'hgrc'))
1608879Ssteve.reinhardt@amd.com    style_hook = ui.config('hooks', 'pretxncommit.style', None)
1618879Ssteve.reinhardt@amd.com
1628879Ssteve.reinhardt@amd.com    if not style_hook:
1638879Ssteve.reinhardt@amd.com        print """\
1648879Ssteve.reinhardt@amd.comYou're missing the M5 style hook.
1658879Ssteve.reinhardt@amd.comPlease install the hook so we can ensure that all code fits a common style.
1668879Ssteve.reinhardt@amd.com
1678879Ssteve.reinhardt@amd.comAll you'd need to do is add the following lines to your repository .hg/hgrc
1688879Ssteve.reinhardt@amd.comor your personal .hgrc
1698879Ssteve.reinhardt@amd.com----------------
1708879Ssteve.reinhardt@amd.com
1718879Ssteve.reinhardt@amd.com[extensions]
1728120Sgblack@eecs.umich.edustyle = %s/util/style.py
1738947Sandreas.hansson@arm.com
1747816Ssteve.reinhardt@amd.com[hooks]
1755871Snate@binkert.orgpretxncommit.style = python:style.check_whitespace
1765871Snate@binkert.org""" % (ROOT)
1776121Snate@binkert.org        sys.exit(1)
1785871Snate@binkert.org
1795871Snate@binkert.orgif ARGUMENTS.get('IGNORE_STYLE') != 'True' and isdir(joinpath(ROOT, '.hg')):
1806003Snate@binkert.org    try:
1818980Ssteve.reinhardt@amd.com        from mercurial import ui
182955SN/A        check_style_hook(ui.ui())
1835871Snate@binkert.org    except ImportError:
1845871Snate@binkert.org        pass
1855871Snate@binkert.org
1865871Snate@binkert.org###################################################
187955SN/A#
1886121Snate@binkert.org# Figure out which configurations to set up based on the path(s) of
1898881Smarc.orr@gmail.com# the target(s).
1906121Snate@binkert.org#
1916121Snate@binkert.org###################################################
1921533SN/A
1936655Snate@binkert.org# Find default configuration & binary.
1946655Snate@binkert.orgDefault(os.environ.get('M5_DEFAULT_BINARY', 'build/ALPHA_SE/m5.debug'))
1956655Snate@binkert.org
1966655Snate@binkert.org# helper function: find last occurrence of element in list
1975871Snate@binkert.orgdef rfind(l, elt, offs = -1):
1985871Snate@binkert.org    for i in range(len(l)+offs, 0, -1):
1995863Snate@binkert.org        if l[i] == elt:
2005871Snate@binkert.org            return i
2018878Ssteve.reinhardt@amd.com    raise ValueError, "element not found"
2025871Snate@binkert.org
2035871Snate@binkert.org# Each target must have 'build' in the interior of the path; the
2045871Snate@binkert.org# directory below this will determine the build parameters.  For
2055863Snate@binkert.org# example, for target 'foo/bar/build/ALPHA_SE/arch/alpha/blah.do' we
2066121Snate@binkert.org# recognize that ALPHA_SE specifies the configuration because it
2075863Snate@binkert.org# follow 'build' in the bulid path.
2085871Snate@binkert.org
2098336Ssteve.reinhardt@amd.com# Generate absolute paths to targets so we can see where the build dir is
2108336Ssteve.reinhardt@amd.comif COMMAND_LINE_TARGETS:
2118336Ssteve.reinhardt@amd.com    # Ask SCons which directory it was invoked from
2128336Ssteve.reinhardt@amd.com    launch_dir = GetLaunchDir()
2134678Snate@binkert.org    # Make targets relative to invocation directory
2148336Ssteve.reinhardt@amd.com    abs_targets = map(lambda x: os.path.normpath(joinpath(launch_dir, str(x))),
2158336Ssteve.reinhardt@amd.com                      COMMAND_LINE_TARGETS)
2168336Ssteve.reinhardt@amd.comelse:
2174678Snate@binkert.org    # Default targets are relative to root of tree
2184678Snate@binkert.org    abs_targets = map(lambda x: os.path.normpath(joinpath(ROOT, str(x))),
2194678Snate@binkert.org                      DEFAULT_TARGETS)
2204678Snate@binkert.org
2217827Snate@binkert.org
2227827Snate@binkert.org# Generate a list of the unique build roots and configs that the
2238336Ssteve.reinhardt@amd.com# collected targets reference.
2244678Snate@binkert.orgbuild_paths = []
2258336Ssteve.reinhardt@amd.combuild_root = None
2268336Ssteve.reinhardt@amd.comfor t in abs_targets:
2278336Ssteve.reinhardt@amd.com    path_dirs = t.split('/')
2288336Ssteve.reinhardt@amd.com    try:
2298336Ssteve.reinhardt@amd.com        build_top = rfind(path_dirs, 'build', -2)
2308336Ssteve.reinhardt@amd.com    except:
2315871Snate@binkert.org        print "Error: no non-leaf 'build' dir found on target path", t
2325871Snate@binkert.org        Exit(1)
2338336Ssteve.reinhardt@amd.com    this_build_root = joinpath('/',*path_dirs[:build_top+1])
2348336Ssteve.reinhardt@amd.com    if not build_root:
2358336Ssteve.reinhardt@amd.com        build_root = this_build_root
2368336Ssteve.reinhardt@amd.com    else:
2378336Ssteve.reinhardt@amd.com        if this_build_root != build_root:
2385871Snate@binkert.org            print "Error: build targets not under same build root\n"\
2398336Ssteve.reinhardt@amd.com                  "  %s\n  %s" % (build_root, this_build_root)
2408336Ssteve.reinhardt@amd.com            Exit(1)
2418336Ssteve.reinhardt@amd.com    build_path = joinpath('/',*path_dirs[:build_top+2])
2428336Ssteve.reinhardt@amd.com    if build_path not in build_paths:
2438336Ssteve.reinhardt@amd.com        build_paths.append(build_path)
2444678Snate@binkert.org
2455871Snate@binkert.org# Make sure build_root exists (might not if this is the first build there)
2464678Snate@binkert.orgif not isdir(build_root):
2478336Ssteve.reinhardt@amd.com    os.mkdir(build_root)
2488336Ssteve.reinhardt@amd.com
2498336Ssteve.reinhardt@amd.com###################################################
2508336Ssteve.reinhardt@amd.com#
2518336Ssteve.reinhardt@amd.com# Set up the default build environment.  This environment is copied
2528336Ssteve.reinhardt@amd.com# and modified according to each selected configuration.
2538336Ssteve.reinhardt@amd.com#
2548336Ssteve.reinhardt@amd.com###################################################
2558336Ssteve.reinhardt@amd.com
2568336Ssteve.reinhardt@amd.comenv = Environment(ENV = os.environ,  # inherit user's environment vars
2578336Ssteve.reinhardt@amd.com                  ROOT = ROOT,
2588336Ssteve.reinhardt@amd.com                  SRCDIR = SRCDIR)
2598336Ssteve.reinhardt@amd.com
2608336Ssteve.reinhardt@amd.comExport('env')
2618336Ssteve.reinhardt@amd.com
2628336Ssteve.reinhardt@amd.comenv.SConsignFile(joinpath(build_root,"sconsign"))
2638336Ssteve.reinhardt@amd.com
2645871Snate@binkert.org# Default duplicate option is to use hard links, but this messes up
2656121Snate@binkert.org# when you use emacs to edit a file in the target dir, as emacs moves
266955SN/A# file to file~ then copies to file, breaking the link.  Symbolic
267955SN/A# (soft) links work better.
2682632Sstever@eecs.umich.eduenv.SetOption('duplicate', 'soft-copy')
2692632Sstever@eecs.umich.edu
270955SN/A# I waffle on this setting... it does avoid a few painful but
271955SN/A# unnecessary builds, but it also seems to make trivial builds take
272955SN/A# noticeably longer.
273955SN/Aif False:
2748878Ssteve.reinhardt@amd.com    env.TargetSignatures('content')
275955SN/A
2762632Sstever@eecs.umich.edu#
2772632Sstever@eecs.umich.edu# Set up global sticky options... these are common to an entire build
2782632Sstever@eecs.umich.edu# tree (not specific to a particular build like ALPHA_SE)
2792632Sstever@eecs.umich.edu#
2802632Sstever@eecs.umich.edu
2812632Sstever@eecs.umich.edu# Option validators & converters for global sticky options
2822632Sstever@eecs.umich.edudef PathListMakeAbsolute(val):
2838268Ssteve.reinhardt@amd.com    if not val:
2848268Ssteve.reinhardt@amd.com        return val
2858268Ssteve.reinhardt@amd.com    f = lambda p: os.path.abspath(os.path.expanduser(p))
2868268Ssteve.reinhardt@amd.com    return ':'.join(map(f, val.split(':')))
2878268Ssteve.reinhardt@amd.com
2888268Ssteve.reinhardt@amd.comdef PathListAllExist(key, val, env):
2898268Ssteve.reinhardt@amd.com    if not val:
2902632Sstever@eecs.umich.edu        return
2912632Sstever@eecs.umich.edu    paths = val.split(':')
2922632Sstever@eecs.umich.edu    for path in paths:
2932632Sstever@eecs.umich.edu        if not isdir(path):
2948268Ssteve.reinhardt@amd.com            raise SCons.Errors.UserError("Path does not exist: '%s'" % path)
2952632Sstever@eecs.umich.edu
2968268Ssteve.reinhardt@amd.comglobal_sticky_opts_file = joinpath(build_root, 'options.global')
2978268Ssteve.reinhardt@amd.com
2988268Ssteve.reinhardt@amd.comglobal_sticky_opts = Options(global_sticky_opts_file, args=ARGUMENTS)
2998268Ssteve.reinhardt@amd.com
3003718Sstever@eecs.umich.eduglobal_sticky_opts.AddOptions(
3012634Sstever@eecs.umich.edu    ('CC', 'C compiler', os.environ.get('CC', env['CC'])),
3022634Sstever@eecs.umich.edu    ('CXX', 'C++ compiler', os.environ.get('CXX', env['CXX'])),
3035863Snate@binkert.org    ('BATCH', 'Use batch pool for build and tests', False),
3042638Sstever@eecs.umich.edu    ('BATCH_CMD', 'Batch pool submission command name', 'qdo'),
3058268Ssteve.reinhardt@amd.com    ('EXTRAS', 'Add Extra directories to the compilation', '',
3062632Sstever@eecs.umich.edu     PathListAllExist, PathListMakeAbsolute)
3072632Sstever@eecs.umich.edu    )    
3082632Sstever@eecs.umich.edu
3092632Sstever@eecs.umich.edu
3102632Sstever@eecs.umich.edu# base help text
3111858SN/Ahelp_text = '''
3123716Sstever@eecs.umich.eduUsage: scons [scons options] [build options] [target(s)]
3132638Sstever@eecs.umich.edu
3142638Sstever@eecs.umich.edu'''
3152638Sstever@eecs.umich.edu
3162638Sstever@eecs.umich.eduhelp_text += "Global sticky options:\n" \
3172638Sstever@eecs.umich.edu             + global_sticky_opts.GenerateHelpText(env)
3182638Sstever@eecs.umich.edu
3192638Sstever@eecs.umich.edu# Update env with values from ARGUMENTS & file global_sticky_opts_file
3205863Snate@binkert.orgglobal_sticky_opts.Update(env)
3215863Snate@binkert.org
3225863Snate@binkert.org# Save sticky option settings back to current options file
323955SN/Aglobal_sticky_opts.Save(global_sticky_opts_file, env)
3245341Sstever@gmail.com
3255341Sstever@gmail.com# Parse EXTRAS option to build list of all directories where we're
3265863Snate@binkert.org# look for sources etc.  This list is exported as base_dir_list.
3277756SAli.Saidi@ARM.combase_dir_list = [joinpath(ROOT, 'src')]
3285341Sstever@gmail.comif env['EXTRAS']:
3296121Snate@binkert.org    base_dir_list += env['EXTRAS'].split(':')
3304494Ssaidi@eecs.umich.edu
3316121Snate@binkert.orgExport('base_dir_list')
3321105SN/A
3332667Sstever@eecs.umich.edu# M5_PLY is used by isa_parser.py to find the PLY package.
3342667Sstever@eecs.umich.eduenv.Append(ENV = { 'M5_PLY' : str(Dir('ext/ply')) })
3352667Sstever@eecs.umich.eduenv['GCC'] = subprocess.Popen(env['CXX'] + ' --version', shell=True,
3362667Sstever@eecs.umich.edu        stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
3376121Snate@binkert.org        close_fds=True).communicate()[0].find('g++') >= 0
3382667Sstever@eecs.umich.eduenv['SUNCC'] = subprocess.Popen(env['CXX'] + ' -V', shell=True,
3395341Sstever@gmail.com        stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
3405863Snate@binkert.org        close_fds=True).communicate()[0].find('Sun C++') >= 0
3415341Sstever@gmail.comenv['ICC'] = subprocess.Popen(env['CXX'] + ' -V', shell=True,
3425341Sstever@gmail.com        stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
3435341Sstever@gmail.com        close_fds=True).communicate()[0].find('Intel') >= 0
3448120Sgblack@eecs.umich.eduif env['GCC'] + env['SUNCC'] + env['ICC'] > 1:
3455341Sstever@gmail.com    print 'Error: How can we have two at the same time?'
3468120Sgblack@eecs.umich.edu    Exit(1)
3475341Sstever@gmail.com
3488120Sgblack@eecs.umich.edu
3496121Snate@binkert.org# Set up default C++ compiler flags
3506121Snate@binkert.orgif env['GCC']:
3518980Ssteve.reinhardt@amd.com    env.Append(CCFLAGS='-pipe')
3525397Ssaidi@eecs.umich.edu    env.Append(CCFLAGS='-fno-strict-aliasing')
3535397Ssaidi@eecs.umich.edu    env.Append(CCFLAGS=Split('-Wall -Wno-sign-compare -Werror -Wundef'))
3547727SAli.Saidi@ARM.com    env.Append(CXXFLAGS='-Wno-deprecated')
3558268Ssteve.reinhardt@amd.comelif env['ICC']:
3566168Snate@binkert.org    pass #Fix me... add warning flags once we clean up icc warnings
3575341Sstever@gmail.comelif env['SUNCC']:
3588120Sgblack@eecs.umich.edu    env.Append(CCFLAGS='-Qoption ccfe')
3598120Sgblack@eecs.umich.edu    env.Append(CCFLAGS='-features=gcc')
3608120Sgblack@eecs.umich.edu    env.Append(CCFLAGS='-features=extensions')
3616814Sgblack@eecs.umich.edu    env.Append(CCFLAGS='-library=stlport4')
3625863Snate@binkert.org    env.Append(CCFLAGS='-xar')
3638120Sgblack@eecs.umich.edu#    env.Append(CCFLAGS='-instances=semiexplicit')
3645341Sstever@gmail.comelse:
3655863Snate@binkert.org    print 'Error: Don\'t know what compiler options to use for your compiler.'
3668268Ssteve.reinhardt@amd.com    print '       Please fix SConstruct and src/SConscript and try again.'
3676121Snate@binkert.org    Exit(1)
3686121Snate@binkert.org
3698268Ssteve.reinhardt@amd.com# Do this after we save setting back, or else we'll tack on an
3705742Snate@binkert.org# extra 'qdo' every time we run scons.
3715742Snate@binkert.orgif env['BATCH']:
3725341Sstever@gmail.com    env['CC']  = env['BATCH_CMD'] + ' ' + env['CC']
3735742Snate@binkert.org    env['CXX'] = env['BATCH_CMD'] + ' ' + env['CXX']
3745742Snate@binkert.org
3755341Sstever@gmail.comif sys.platform == 'cygwin':
3766017Snate@binkert.org    # cygwin has some header file issues...
3776121Snate@binkert.org    env.Append(CCFLAGS=Split("-Wno-uninitialized"))
3786017Snate@binkert.orgenv.Append(CPPPATH=[Dir('ext/dnet')])
3797816Ssteve.reinhardt@amd.com
3807756SAli.Saidi@ARM.com# Check for SWIG
3817756SAli.Saidi@ARM.comif not env.has_key('SWIG'):
3827756SAli.Saidi@ARM.com    print 'Error: SWIG utility not found.'
3837756SAli.Saidi@ARM.com    print '       Please install (see http://www.swig.org) and retry.'
3847756SAli.Saidi@ARM.com    Exit(1)
3857756SAli.Saidi@ARM.com
3867756SAli.Saidi@ARM.com# Check for appropriate SWIG version
3877756SAli.Saidi@ARM.comswig_version = os.popen('swig -version').read().split()
3887816Ssteve.reinhardt@amd.com# First 3 words should be "SWIG Version x.y.z"
3897816Ssteve.reinhardt@amd.comif len(swig_version) < 3 or \
3907816Ssteve.reinhardt@amd.com        swig_version[0] != 'SWIG' or swig_version[1] != 'Version':
3917816Ssteve.reinhardt@amd.com    print 'Error determining SWIG version.'
3927816Ssteve.reinhardt@amd.com    Exit(1)
3937816Ssteve.reinhardt@amd.com
3947816Ssteve.reinhardt@amd.commin_swig_version = '1.3.28'
3957816Ssteve.reinhardt@amd.comif compare_versions(swig_version[2], min_swig_version) < 0:
3967816Ssteve.reinhardt@amd.com    print 'Error: SWIG version', min_swig_version, 'or newer required.'
3977816Ssteve.reinhardt@amd.com    print '       Installed version:', swig_version[2]
3987756SAli.Saidi@ARM.com    Exit(1)
3997816Ssteve.reinhardt@amd.com
4007816Ssteve.reinhardt@amd.com# Set up SWIG flags & scanner
4017816Ssteve.reinhardt@amd.comswig_flags=Split('-c++ -python -modern -templatereduce $_CPPINCFLAGS')
4027816Ssteve.reinhardt@amd.comenv.Append(SWIGFLAGS=swig_flags)
4037816Ssteve.reinhardt@amd.com
4047816Ssteve.reinhardt@amd.com# filter out all existing swig scanners, they mess up the dependency
4057816Ssteve.reinhardt@amd.com# stuff for some reason
4067816Ssteve.reinhardt@amd.comscanners = []
4077816Ssteve.reinhardt@amd.comfor scanner in env['SCANNERS']:
4087816Ssteve.reinhardt@amd.com    skeys = scanner.skeys
4097816Ssteve.reinhardt@amd.com    if skeys == '.i':
4107816Ssteve.reinhardt@amd.com        continue
4117816Ssteve.reinhardt@amd.com
4127816Ssteve.reinhardt@amd.com    if isinstance(skeys, (list, tuple)) and '.i' in skeys:
4137816Ssteve.reinhardt@amd.com        continue
4147816Ssteve.reinhardt@amd.com
4157816Ssteve.reinhardt@amd.com    scanners.append(scanner)
4167816Ssteve.reinhardt@amd.com
4177816Ssteve.reinhardt@amd.com# add the new swig scanner that we like better
4187816Ssteve.reinhardt@amd.comfrom SCons.Scanner import ClassicCPP as CPPScanner
4197816Ssteve.reinhardt@amd.comswig_inc_re = '^[ \t]*[%,#][ \t]*(?:include|import)[ \t]*(<|")([^>"]+)(>|")'
4207816Ssteve.reinhardt@amd.comscanners.append(CPPScanner("SwigScan", [ ".i" ], "CPPPATH", swig_inc_re))
4217816Ssteve.reinhardt@amd.com
4227816Ssteve.reinhardt@amd.com# replace the scanners list that has what we want
4237816Ssteve.reinhardt@amd.comenv['SCANNERS'] = scanners
4247816Ssteve.reinhardt@amd.com
4257816Ssteve.reinhardt@amd.com# Add a custom Check function to the Configure context so that we can
4267816Ssteve.reinhardt@amd.com# figure out if the compiler adds leading underscores to global
4277816Ssteve.reinhardt@amd.com# variables.  This is needed for the autogenerated asm files that we
4287816Ssteve.reinhardt@amd.com# use for embedding the python code.
4297816Ssteve.reinhardt@amd.comdef CheckLeading(context):
4307816Ssteve.reinhardt@amd.com    context.Message("Checking for leading underscore in global variables...")
4317816Ssteve.reinhardt@amd.com    # 1) Define a global variable called x from asm so the C compiler
4327816Ssteve.reinhardt@amd.com    #    won't change the symbol at all.
4337816Ssteve.reinhardt@amd.com    # 2) Declare that variable.
4347816Ssteve.reinhardt@amd.com    # 3) Use the variable
4357816Ssteve.reinhardt@amd.com    #
4367816Ssteve.reinhardt@amd.com    # If the compiler prepends an underscore, this will successfully
4377816Ssteve.reinhardt@amd.com    # link because the external symbol 'x' will be called '_x' which
4387816Ssteve.reinhardt@amd.com    # was defined by the asm statement.  If the compiler does not
4397816Ssteve.reinhardt@amd.com    # prepend an underscore, this will not successfully link because
4407816Ssteve.reinhardt@amd.com    # '_x' will have been defined by assembly, while the C portion of
4417816Ssteve.reinhardt@amd.com    # the code will be trying to use 'x'
4427816Ssteve.reinhardt@amd.com    ret = context.TryLink('''
4437816Ssteve.reinhardt@amd.com        asm(".globl _x; _x: .byte 0");
4447816Ssteve.reinhardt@amd.com        extern int x;
4457816Ssteve.reinhardt@amd.com        int main() { return x; }
4467816Ssteve.reinhardt@amd.com        ''', extension=".c")
4477816Ssteve.reinhardt@amd.com    context.env.Append(LEADING_UNDERSCORE=ret)
4487816Ssteve.reinhardt@amd.com    context.Result(ret)
4497816Ssteve.reinhardt@amd.com    return ret
4507816Ssteve.reinhardt@amd.com
4517816Ssteve.reinhardt@amd.com# Platform-specific configuration.  Note again that we assume that all
4527816Ssteve.reinhardt@amd.com# builds under a given build root run on the same host platform.
4537816Ssteve.reinhardt@amd.comconf = Configure(env,
4547816Ssteve.reinhardt@amd.com                 conf_dir = joinpath(build_root, '.scons_config'),
4557816Ssteve.reinhardt@amd.com                 log_file = joinpath(build_root, 'scons_config.log'),
4567816Ssteve.reinhardt@amd.com                 custom_tests = { 'CheckLeading' : CheckLeading })
4577816Ssteve.reinhardt@amd.com
4587816Ssteve.reinhardt@amd.com# Check for leading underscores.  Don't really need to worry either
4597816Ssteve.reinhardt@amd.com# way so don't need to check the return code.
4608947Sandreas.hansson@arm.comconf.CheckLeading()
4618947Sandreas.hansson@arm.com
4627756SAli.Saidi@ARM.com# Check if we should compile a 64 bit binary on Mac OS X/Darwin
4638120Sgblack@eecs.umich.edutry:
4647756SAli.Saidi@ARM.com    import platform
4657756SAli.Saidi@ARM.com    uname = platform.uname()
4667756SAli.Saidi@ARM.com    if uname[0] == 'Darwin' and compare_versions(uname[2], '9.0.0') >= 0:
4677756SAli.Saidi@ARM.com        if int(subprocess.Popen('sysctl -n hw.cpu64bit_capable', shell=True,
4687816Ssteve.reinhardt@amd.com               stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
4697816Ssteve.reinhardt@amd.com               close_fds=True).communicate()[0][0]):
4707816Ssteve.reinhardt@amd.com            env.Append(CCFLAGS='-arch x86_64')
4717816Ssteve.reinhardt@amd.com            env.Append(CFLAGS='-arch x86_64')
4727816Ssteve.reinhardt@amd.com            env.Append(LINKFLAGS='-arch x86_64')
4737816Ssteve.reinhardt@amd.com            env.Append(ASFLAGS='-arch x86_64')
4747816Ssteve.reinhardt@amd.comexcept:
4757816Ssteve.reinhardt@amd.com    pass
4767816Ssteve.reinhardt@amd.com
4777816Ssteve.reinhardt@amd.com# Recent versions of scons substitute a "Null" object for Configure()
4787756SAli.Saidi@ARM.com# when configuration isn't necessary, e.g., if the "--help" option is
4797756SAli.Saidi@ARM.com# present.  Unfortuantely this Null object always returns false,
4806654Snate@binkert.org# breaking all our configuration checks.  We replace it with our own
4816654Snate@binkert.org# more optimistic null object that returns True instead.
4825871Snate@binkert.orgif not conf:
4836121Snate@binkert.org    def NullCheck(*args, **kwargs):
4846121Snate@binkert.org        return True
4856121Snate@binkert.org
4868946Sandreas.hansson@arm.com    class NullConf:
4878737Skoansin.tan@gmail.com        def __init__(self, env):
4883940Ssaidi@eecs.umich.edu            self.env = env
4893918Ssaidi@eecs.umich.edu        def Finish(self):
4903918Ssaidi@eecs.umich.edu            return self.env
4911858SN/A        def __getattr__(self, mname):
4926121Snate@binkert.org            return NullCheck
4937739Sgblack@eecs.umich.edu
4947739Sgblack@eecs.umich.edu    conf = NullConf(env)
4956143Snate@binkert.org
4967618SAli.Saidi@arm.com# Find Python include and library directories for embedding the
4977618SAli.Saidi@arm.com# interpreter.  For consistency, we will use the same Python
4987618SAli.Saidi@arm.com# installation used to run scons (and thus this script).  If you want
4997618SAli.Saidi@arm.com# to link in an alternate version, see above for instructions on how
5008614Sgblack@eecs.umich.edu# to invoke scons with a different copy of the Python interpreter.
5017618SAli.Saidi@arm.com
5027618SAli.Saidi@arm.com# Get brief Python version name (e.g., "python2.4") for locating
5037618SAli.Saidi@arm.com# include & library files
5047739Sgblack@eecs.umich.edupy_version_name = 'python' + sys.version[:3]
5058946Sandreas.hansson@arm.com
5068946Sandreas.hansson@arm.com# include path, e.g. /usr/local/include/python2.4
5076121Snate@binkert.orgpy_header_path = joinpath(sys.exec_prefix, 'include', py_version_name)
5083940Ssaidi@eecs.umich.eduenv.Append(CPPPATH = py_header_path)
5096121Snate@binkert.org# verify that it works
5107739Sgblack@eecs.umich.eduif not conf.CheckHeader('Python.h', '<>'):
5117739Sgblack@eecs.umich.edu    print "Error: can't find Python.h header in", py_header_path
5127739Sgblack@eecs.umich.edu    Exit(1)
5137739Sgblack@eecs.umich.edu
5147739Sgblack@eecs.umich.edu# add library path too if it's not in the default place
5157739Sgblack@eecs.umich.edupy_lib_path = None
5168737Skoansin.tan@gmail.comif sys.exec_prefix != '/usr':
5178737Skoansin.tan@gmail.com    py_lib_path = joinpath(sys.exec_prefix, 'lib')
5188737Skoansin.tan@gmail.comelif sys.platform == 'cygwin':
5198737Skoansin.tan@gmail.com    # cygwin puts the .dll in /bin for some reason
5208737Skoansin.tan@gmail.com    py_lib_path = '/bin'
5218737Skoansin.tan@gmail.comif py_lib_path:
5228737Skoansin.tan@gmail.com    env.Append(LIBPATH = py_lib_path)
5238737Skoansin.tan@gmail.com    print 'Adding', py_lib_path, 'to LIBPATH for', py_version_name
5248737Skoansin.tan@gmail.comif not conf.CheckLib(py_version_name):
5258737Skoansin.tan@gmail.com    print "Error: can't find Python library", py_version_name
5268737Skoansin.tan@gmail.com    Exit(1)
5278737Skoansin.tan@gmail.com
5288737Skoansin.tan@gmail.com# On Solaris you need to use libsocket for socket ops
5298737Skoansin.tan@gmail.comif not conf.CheckLibWithHeader(None, 'sys/socket.h', 'C++', 'accept(0,0,0);'):
5308737Skoansin.tan@gmail.com   if not conf.CheckLibWithHeader('socket', 'sys/socket.h', 'C++', 'accept(0,0,0);'):
5318737Skoansin.tan@gmail.com       print "Can't find library with socket calls (e.g. accept())"
5328737Skoansin.tan@gmail.com       Exit(1)
5338737Skoansin.tan@gmail.com
5348946Sandreas.hansson@arm.com# Check for zlib.  If the check passes, libz will be automatically
5358946Sandreas.hansson@arm.com# added to the LIBS environment variable.
5368946Sandreas.hansson@arm.comif not conf.CheckLibWithHeader('z', 'zlib.h', 'C++','zlibVersion();'):
5378946Sandreas.hansson@arm.com    print 'Error: did not find needed zlib compression library '\
5388946Sandreas.hansson@arm.com          'and/or zlib.h header file.'
5398946Sandreas.hansson@arm.com    print '       Please install zlib and try again.'
5403918Ssaidi@eecs.umich.edu    Exit(1)
5413918Ssaidi@eecs.umich.edu
5423940Ssaidi@eecs.umich.edu# Check for <fenv.h> (C99 FP environment control)
5433918Ssaidi@eecs.umich.eduhave_fenv = conf.CheckHeader('fenv.h', '<>')
5443918Ssaidi@eecs.umich.eduif not have_fenv:
5456157Snate@binkert.org    print "Warning: Header file <fenv.h> not found."
5466157Snate@binkert.org    print "         This host has no IEEE FP rounding mode control."
5476157Snate@binkert.org
5486157Snate@binkert.org# Check for mysql.
5495397Ssaidi@eecs.umich.edumysql_config = WhereIs('mysql_config')
5505397Ssaidi@eecs.umich.eduhave_mysql = mysql_config != None
5516121Snate@binkert.org
5526121Snate@binkert.org# Check MySQL version.
5536121Snate@binkert.orgif have_mysql:
5546121Snate@binkert.org    mysql_version = os.popen(mysql_config + ' --version').read()
5556121Snate@binkert.org    min_mysql_version = '4.1'
5566121Snate@binkert.org    if compare_versions(mysql_version, min_mysql_version) < 0:
5575397Ssaidi@eecs.umich.edu        print 'Warning: MySQL', min_mysql_version, 'or newer required.'
5581851SN/A        print '         Version', mysql_version, 'detected.'
5591851SN/A        have_mysql = False
5607739Sgblack@eecs.umich.edu
561955SN/A# Set up mysql_config commands.
5623053Sstever@eecs.umich.eduif have_mysql:
5636121Snate@binkert.org    mysql_config_include = mysql_config + ' --include'
5643053Sstever@eecs.umich.edu    if os.system(mysql_config_include + ' > /dev/null') != 0:
5653053Sstever@eecs.umich.edu        # older mysql_config versions don't support --include, use
5663053Sstever@eecs.umich.edu        # --cflags instead
5673053Sstever@eecs.umich.edu        mysql_config_include = mysql_config + ' --cflags | sed s/\\\'//g'
5683053Sstever@eecs.umich.edu    # This seems to work in all versions
5696654Snate@binkert.org    mysql_config_libs = mysql_config + ' --libs'
5703053Sstever@eecs.umich.edu
5714742Sstever@eecs.umich.eduenv = conf.Finish()
5724742Sstever@eecs.umich.edu
5733053Sstever@eecs.umich.edu# Define the universe of supported ISAs
5743053Sstever@eecs.umich.eduall_isa_list = [ ]
5753053Sstever@eecs.umich.eduExport('all_isa_list')
5768960Ssteve.reinhardt@amd.com
5776654Snate@binkert.org# Define the universe of supported CPU models
5783053Sstever@eecs.umich.eduall_cpu_list = [ ]
5793053Sstever@eecs.umich.edudefault_cpus = [ ]
5803053Sstever@eecs.umich.eduExport('all_cpu_list', 'default_cpus')
5813053Sstever@eecs.umich.edu
5822667Sstever@eecs.umich.edu# Sticky options get saved in the options file so they persist from
5834554Sbinkertn@umich.edu# one invocation to the next (unless overridden, in which case the new
5846121Snate@binkert.org# value becomes sticky).
5852667Sstever@eecs.umich.edusticky_opts = Options(args=ARGUMENTS)
5864554Sbinkertn@umich.eduExport('sticky_opts')
5874554Sbinkertn@umich.edu
5884554Sbinkertn@umich.edu# Non-sticky options only apply to the current build.
5896121Snate@binkert.orgnonsticky_opts = Options(args=ARGUMENTS)
5904554Sbinkertn@umich.eduExport('nonsticky_opts')
5914554Sbinkertn@umich.edu
5924554Sbinkertn@umich.edu# Walk the tree and execute all SConsopts scripts that wil add to the
5934781Snate@binkert.org# above options
5944554Sbinkertn@umich.edufor base_dir in base_dir_list:
5954554Sbinkertn@umich.edu    for root, dirs, files in os.walk(base_dir):
5962667Sstever@eecs.umich.edu        if 'SConsopts' in files:
5974554Sbinkertn@umich.edu            print "Reading", joinpath(root, 'SConsopts')
5984554Sbinkertn@umich.edu            SConscript(joinpath(root, 'SConsopts'))
5994554Sbinkertn@umich.edu
6004554Sbinkertn@umich.eduall_isa_list.sort()
6012667Sstever@eecs.umich.eduall_cpu_list.sort()
6024554Sbinkertn@umich.edudefault_cpus.sort()
6032667Sstever@eecs.umich.edu
6044554Sbinkertn@umich.edusticky_opts.AddOptions(
6056121Snate@binkert.org    EnumOption('TARGET_ISA', 'Target ISA', 'alpha', all_isa_list),
6062667Sstever@eecs.umich.edu    BoolOption('FULL_SYSTEM', 'Full-system support', False),
6075522Snate@binkert.org    # There's a bug in scons 0.96.1 that causes ListOptions with list
6085522Snate@binkert.org    # values (more than one value) not to be able to be restored from
6095522Snate@binkert.org    # a saved option file.  If this causes trouble then upgrade to
6105522Snate@binkert.org    # scons 0.96.90 or later.
6115522Snate@binkert.org    ListOption('CPU_MODELS', 'CPU models', default_cpus, all_cpu_list),
6125522Snate@binkert.org    BoolOption('NO_FAST_ALLOC', 'Disable fast object allocator', False),
6135522Snate@binkert.org    BoolOption('FAST_ALLOC_DEBUG', 'Enable fast object allocator debugging',
6145522Snate@binkert.org               False),
6155522Snate@binkert.org    BoolOption('FAST_ALLOC_STATS', 'Enable fast object allocator statistics',
6165522Snate@binkert.org               False),
6175522Snate@binkert.org    BoolOption('EFENCE', 'Link with Electric Fence malloc debugger',
6185522Snate@binkert.org               False),
6195522Snate@binkert.org    BoolOption('SS_COMPATIBLE_FP',
6205522Snate@binkert.org               'Make floating-point results compatible with SimpleScalar',
6215522Snate@binkert.org               False),
6225522Snate@binkert.org    BoolOption('USE_SSE2',
6235522Snate@binkert.org               'Compile for SSE2 (-msse2) to get IEEE FP on x86 hosts',
6245522Snate@binkert.org               False),
6255522Snate@binkert.org    BoolOption('USE_MYSQL', 'Use MySQL for stats output', have_mysql),
6265522Snate@binkert.org    BoolOption('USE_FENV', 'Use <fenv.h> IEEE mode control', have_fenv),
6275522Snate@binkert.org    BoolOption('USE_CHECKER', 'Use checker for detailed CPU models', False),
6285522Snate@binkert.org    )
6295522Snate@binkert.org
6305522Snate@binkert.orgnonsticky_opts.AddOptions(
6315522Snate@binkert.org    BoolOption('update_ref', 'Update test reference outputs', False)
6325522Snate@binkert.org    )
6332638Sstever@eecs.umich.edu
6342638Sstever@eecs.umich.edu# These options get exported to #defines in config/*.hh (see src/SConscript).
6356121Snate@binkert.orgenv.ExportOptions = ['FULL_SYSTEM', 'ALPHA_TLASER', 'USE_FENV', \
6363716Sstever@eecs.umich.edu                     'USE_MYSQL', 'NO_FAST_ALLOC', 'FAST_ALLOC_DEBUG', \
6375522Snate@binkert.org                     'FAST_ALLOC_STATS', 'SS_COMPATIBLE_FP', \
6385522Snate@binkert.org                     'USE_CHECKER', 'TARGET_ISA']
6395522Snate@binkert.org
6405522Snate@binkert.org# Define a handy 'no-op' action
6415522Snate@binkert.orgdef no_action(target, source, env):
6425522Snate@binkert.org    return 0
6431858SN/A
6445227Ssaidi@eecs.umich.eduenv.NoAction = Action(no_action, None)
6455227Ssaidi@eecs.umich.edu
6465227Ssaidi@eecs.umich.edu###################################################
6475227Ssaidi@eecs.umich.edu#
6486654Snate@binkert.org# Define a SCons builder for configuration flag headers.
6496654Snate@binkert.org#
6507769SAli.Saidi@ARM.com###################################################
6517769SAli.Saidi@ARM.com
6527769SAli.Saidi@ARM.com# This function generates a config header file that #defines the
6537769SAli.Saidi@ARM.com# option symbol to the current option setting (0 or 1).  The source
6545227Ssaidi@eecs.umich.edu# operands are the name of the option and a Value node containing the
6555227Ssaidi@eecs.umich.edu# value of the option.
6565227Ssaidi@eecs.umich.edudef build_config_file(target, source, env):
6575204Sstever@gmail.com    (option, value) = [s.get_contents() for s in source]
6585204Sstever@gmail.com    f = file(str(target[0]), 'w')
6595204Sstever@gmail.com    print >> f, '#define', option, value
6605204Sstever@gmail.com    f.close()
6615204Sstever@gmail.com    return None
6625204Sstever@gmail.com
6635204Sstever@gmail.com# Generate the message to be printed when building the config file.
6645204Sstever@gmail.comdef build_config_file_string(target, source, env):
6655204Sstever@gmail.com    (option, value) = [s.get_contents() for s in source]
6665204Sstever@gmail.com    return "Defining %s as %s in %s." % (option, value, target[0])
6675204Sstever@gmail.com
6685204Sstever@gmail.com# Combine the two functions into a scons Action object.
6695204Sstever@gmail.comconfig_action = Action(build_config_file, build_config_file_string)
6705204Sstever@gmail.com
6715204Sstever@gmail.com# The emitter munges the source & target node lists to reflect what
6725204Sstever@gmail.com# we're really doing.
6735204Sstever@gmail.comdef config_emitter(target, source, env):
6746121Snate@binkert.org    # extract option name from Builder arg
6755204Sstever@gmail.com    option = str(target[0])
6763118Sstever@eecs.umich.edu    # True target is config header file
6773118Sstever@eecs.umich.edu    target = joinpath('config', option.lower() + '.hh')
6783118Sstever@eecs.umich.edu    val = env[option]
6793118Sstever@eecs.umich.edu    if isinstance(val, bool):
6803118Sstever@eecs.umich.edu        # Force value to 0/1
6815863Snate@binkert.org        val = int(val)
6823118Sstever@eecs.umich.edu    elif isinstance(val, str):
6835863Snate@binkert.org        val = '"' + val + '"'
6843118Sstever@eecs.umich.edu
6857457Snate@binkert.org    # Sources are option name & value (packaged in SCons Value nodes)
6867457Snate@binkert.org    return ([target], [Value(option), Value(val)])
6875863Snate@binkert.org
6885863Snate@binkert.orgconfig_builder = Builder(emitter = config_emitter, action = config_action)
6895863Snate@binkert.org
6905863Snate@binkert.orgenv.Append(BUILDERS = { 'ConfigFile' : config_builder })
6915863Snate@binkert.org
6925863Snate@binkert.org###################################################
6935863Snate@binkert.org#
6946003Snate@binkert.org# Define a SCons builder for copying files.  This is used by the
6955863Snate@binkert.org# Python zipfile code in src/python/SConscript, but is placed up here
6965863Snate@binkert.org# since it's potentially more generally applicable.
6975863Snate@binkert.org#
6986120Snate@binkert.org###################################################
6995863Snate@binkert.org
7005863Snate@binkert.orgcopy_builder = Builder(action = Copy("$TARGET", "$SOURCE"))
7015863Snate@binkert.org
7028655Sandreas.hansson@arm.comenv.Append(BUILDERS = { 'CopyFile' : copy_builder })
7038655Sandreas.hansson@arm.com
7048655Sandreas.hansson@arm.com###################################################
7058655Sandreas.hansson@arm.com#
7068655Sandreas.hansson@arm.com# Define a simple SCons builder to concatenate files.
7078655Sandreas.hansson@arm.com#
7088655Sandreas.hansson@arm.com# Used to append the Python zip archive to the executable.
7098655Sandreas.hansson@arm.com#
7106120Snate@binkert.org###################################################
7115863Snate@binkert.org
7126121Snate@binkert.orgconcat_builder = Builder(action = Action(['cat $SOURCES > $TARGET',
7136121Snate@binkert.org                                          'chmod +x $TARGET']))
7145863Snate@binkert.org
7157727SAli.Saidi@ARM.comenv.Append(BUILDERS = { 'Concat' : concat_builder })
7167727SAli.Saidi@ARM.com
7177727SAli.Saidi@ARM.com
7187727SAli.Saidi@ARM.com# libelf build is shared across all configs in the build root.
7197727SAli.Saidi@ARM.comenv.SConscript('ext/libelf/SConscript',
7207727SAli.Saidi@ARM.com               build_dir = joinpath(build_root, 'libelf'),
7215863Snate@binkert.org               exports = 'env')
7223118Sstever@eecs.umich.edu
7235863Snate@binkert.org###################################################
7243118Sstever@eecs.umich.edu#
7253118Sstever@eecs.umich.edu# This function is used to set up a directory with switching headers
7265863Snate@binkert.org#
7275863Snate@binkert.org###################################################
7285863Snate@binkert.org
7295863Snate@binkert.orgenv['ALL_ISA_LIST'] = all_isa_list
7303118Sstever@eecs.umich.edudef make_switching_dir(dirname, switch_headers, env):
7313483Ssaidi@eecs.umich.edu    # Generate the header.  target[0] is the full path of the output
7323494Ssaidi@eecs.umich.edu    # header to generate.  'source' is a dummy variable, since we get the
7333494Ssaidi@eecs.umich.edu    # list of ISAs from env['ALL_ISA_LIST'].
7343483Ssaidi@eecs.umich.edu    def gen_switch_hdr(target, source, env):
7353483Ssaidi@eecs.umich.edu        fname = str(target[0])
7363483Ssaidi@eecs.umich.edu        basename = os.path.basename(fname)
7373053Sstever@eecs.umich.edu        f = open(fname, 'w')
7383053Sstever@eecs.umich.edu        f.write('#include "arch/isa_specific.hh"\n')
7393918Ssaidi@eecs.umich.edu        cond = '#if'
7403053Sstever@eecs.umich.edu        for isa in all_isa_list:
7413053Sstever@eecs.umich.edu            f.write('%s THE_ISA == %s_ISA\n#include "%s/%s/%s"\n'
7423053Sstever@eecs.umich.edu                    % (cond, isa.upper(), dirname, isa, basename))
7433053Sstever@eecs.umich.edu            cond = '#elif'
7443053Sstever@eecs.umich.edu        f.write('#else\n#error "THE_ISA not set"\n#endif\n')
7457840Snate@binkert.org        f.close()
7467865Sgblack@eecs.umich.edu        return 0
7477865Sgblack@eecs.umich.edu
7487865Sgblack@eecs.umich.edu    # String to print when generating header
7497865Sgblack@eecs.umich.edu    def gen_switch_hdr_string(target, source, env):
7507865Sgblack@eecs.umich.edu        return "Generating switch header " + str(target[0])
7517840Snate@binkert.org
7527840Snate@binkert.org    # Build SCons Action object. 'varlist' specifies env vars that this
7537840Snate@binkert.org    # action depends on; when env['ALL_ISA_LIST'] changes these actions
7547840Snate@binkert.org    # should get re-executed.
7551858SN/A    switch_hdr_action = Action(gen_switch_hdr, gen_switch_hdr_string,
7561858SN/A                               varlist=['ALL_ISA_LIST'])
7571858SN/A
7581858SN/A    # Instantiate actions for each header
7591858SN/A    for hdr in switch_headers:
7601858SN/A        env.Command(hdr, [], switch_hdr_action)
7615863Snate@binkert.orgExport('make_switching_dir')
7625863Snate@binkert.org
7635863Snate@binkert.org###################################################
7645863Snate@binkert.org#
7656121Snate@binkert.org# Define build environments for selected configurations.
7661858SN/A#
7675863Snate@binkert.org###################################################
7685863Snate@binkert.org
7695863Snate@binkert.org# rename base env
7705863Snate@binkert.orgbase_env = env
7715863Snate@binkert.org
7722139SN/Afor build_path in build_paths:
7734202Sbinkertn@umich.edu    print "Building in", build_path
7744202Sbinkertn@umich.edu
7752139SN/A    # Make a copy of the build-root environment to use for this config.
7766994Snate@binkert.org    env = base_env.Copy()
7776994Snate@binkert.org    env['BUILDDIR'] = build_path
7786994Snate@binkert.org
7796994Snate@binkert.org    # build_dir is the tail component of build path, and is used to
7806994Snate@binkert.org    # determine the build parameters (e.g., 'ALPHA_SE')
7816994Snate@binkert.org    (build_root, build_dir) = os.path.split(build_path)
7826994Snate@binkert.org
7836994Snate@binkert.org    # Set env options according to the build directory config.
7846994Snate@binkert.org    sticky_opts.files = []
7856994Snate@binkert.org    # Options for $BUILD_ROOT/$BUILD_DIR are stored in
7866994Snate@binkert.org    # $BUILD_ROOT/options/$BUILD_DIR so you can nuke
7876994Snate@binkert.org    # $BUILD_ROOT/$BUILD_DIR without losing your options settings.
7886994Snate@binkert.org    current_opts_file = joinpath(build_root, 'options', build_dir)
7896994Snate@binkert.org    if isfile(current_opts_file):
7906994Snate@binkert.org        sticky_opts.files.append(current_opts_file)
7916994Snate@binkert.org        print "Using saved options file %s" % current_opts_file
7926994Snate@binkert.org    else:
7936994Snate@binkert.org        # Build dir-specific options file doesn't exist.
7946994Snate@binkert.org
7956994Snate@binkert.org        # Make sure the directory is there so we can create it later
7966994Snate@binkert.org        opt_dir = os.path.dirname(current_opts_file)
7976994Snate@binkert.org        if not isdir(opt_dir):
7986994Snate@binkert.org            os.mkdir(opt_dir)
7996994Snate@binkert.org
8006994Snate@binkert.org        # Get default build options from source tree.  Options are
8016994Snate@binkert.org        # normally determined by name of $BUILD_DIR, but can be
8026994Snate@binkert.org        # overriden by 'default=' arg on command line.
8036994Snate@binkert.org        default_opts_file = joinpath('build_opts',
8042155SN/A                                     ARGUMENTS.get('default', build_dir))
8055863Snate@binkert.org        if isfile(default_opts_file):
8061869SN/A            sticky_opts.files.append(default_opts_file)
8071869SN/A            print "Options file %s not found,\n  using defaults in %s" \
8085863Snate@binkert.org                  % (current_opts_file, default_opts_file)
8095863Snate@binkert.org        else:
8104202Sbinkertn@umich.edu            print "Error: cannot find options file %s or %s" \
8116108Snate@binkert.org                  % (current_opts_file, default_opts_file)
8126108Snate@binkert.org            Exit(1)
8136108Snate@binkert.org
8146108Snate@binkert.org    # Apply current option settings to env
8154202Sbinkertn@umich.edu    sticky_opts.Update(env)
8165863Snate@binkert.org    nonsticky_opts.Update(env)
8178474Sgblack@eecs.umich.edu
8188474Sgblack@eecs.umich.edu    help_text += "\nSticky options for %s:\n" % build_dir \
8195742Snate@binkert.org                 + sticky_opts.GenerateHelpText(env) \
8208268Ssteve.reinhardt@amd.com                 + "\nNon-sticky options for %s:\n" % build_dir \
8218268Ssteve.reinhardt@amd.com                 + nonsticky_opts.GenerateHelpText(env)
8228268Ssteve.reinhardt@amd.com
8235742Snate@binkert.org    # Process option settings.
8245341Sstever@gmail.com
8258474Sgblack@eecs.umich.edu    if not have_fenv and env['USE_FENV']:
8268474Sgblack@eecs.umich.edu        print "Warning: <fenv.h> not available; " \
8275342Sstever@gmail.com              "forcing USE_FENV to False in", build_dir + "."
8284202Sbinkertn@umich.edu        env['USE_FENV'] = False
8294202Sbinkertn@umich.edu
8304202Sbinkertn@umich.edu    if not env['USE_FENV']:
8315863Snate@binkert.org        print "Warning: No IEEE FP rounding mode control in", build_dir + "."
8325863Snate@binkert.org        print "         FP results may deviate slightly from other platforms."
8336994Snate@binkert.org
8346994Snate@binkert.org    if env['EFENCE']:
8356994Snate@binkert.org        env.Append(LIBS=['efence'])
8365863Snate@binkert.org
8375863Snate@binkert.org    if env['USE_MYSQL']:
8385863Snate@binkert.org        if not have_mysql:
8395863Snate@binkert.org            print "Warning: MySQL not available; " \
8405863Snate@binkert.org                  "forcing USE_MYSQL to False in", build_dir + "."
8415863Snate@binkert.org            env['USE_MYSQL'] = False
8425863Snate@binkert.org        else:
8435863Snate@binkert.org            print "Compiling in", build_dir, "with MySQL support."
8447840Snate@binkert.org            env.ParseConfig(mysql_config_libs)
8455863Snate@binkert.org            env.ParseConfig(mysql_config_include)
8465952Ssaidi@eecs.umich.edu
8471869SN/A    # Save sticky option settings back to current options file
8481858SN/A    sticky_opts.Save(current_opts_file, env)
8495863Snate@binkert.org
8509044SAli.Saidi@ARM.com    if env['USE_SSE2']:
8518805Sgblack@eecs.umich.edu        env.Append(CCFLAGS='-msse2')
8521858SN/A
853955SN/A    # The src/SConscript file sets up the build rules in 'env' according
854955SN/A    # to the configured options.  It returns a list of environments,
8551869SN/A    # one for each variant build (debug, opt, etc.)
8561869SN/A    envList = SConscript('src/SConscript', build_dir = build_path,
8571869SN/A                         exports = 'env')
8581869SN/A
8591869SN/A    # Set up the regression tests for each build.
8605863Snate@binkert.org    for e in envList:
8615863Snate@binkert.org        SConscript('tests/SConscript',
8625863Snate@binkert.org                   build_dir = joinpath(build_path, 'tests', e.Label),
8631869SN/A                   exports = { 'env' : e }, duplicate = False)
8645863Snate@binkert.org
8651869SN/AHelp(help_text)
8665863Snate@binkert.org
8671869SN/A
8681869SN/A###################################################
8691869SN/A#
8701869SN/A# Let SCons do its thing.  At this point SCons will use the defined
8718483Sgblack@eecs.umich.edu# build environments to build the requested targets.
8721869SN/A#
8731869SN/A###################################################
8741869SN/A
8751869SN/A