SConstruct revision 5522
1955SN/A# -*- mode:python -*-
2955SN/A
31762SN/A# Copyright (c) 2004-2005 The Regents of The University of Michigan
4955SN/A# All rights reserved.
5955SN/A#
6955SN/A# Redistribution and use in source and binary forms, with or without
7955SN/A# modification, are permitted provided that the following conditions are
8955SN/A# met: redistributions of source code must retain the above copyright
9955SN/A# notice, this list of conditions and the following disclaimer;
10955SN/A# redistributions in binary form must reproduce the above copyright
11955SN/A# notice, this list of conditions and the following disclaimer in the
12955SN/A# documentation and/or other materials provided with the distribution;
13955SN/A# neither the name of the copyright holders nor the names of its
14955SN/A# contributors may be used to endorse or promote products derived from
15955SN/A# this software without specific prior written permission.
16955SN/A#
17955SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18955SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19955SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20955SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21955SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22955SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23955SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24955SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25955SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26955SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27955SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
282665Ssaidi@eecs.umich.edu#
292665Ssaidi@eecs.umich.edu# Authors: Steve Reinhardt
30955SN/A
31955SN/A###################################################
32955SN/A#
33955SN/A# SCons top-level build description (SConstruct) file.
34955SN/A#
352632Sstever@eecs.umich.edu# While in this directory ('m5'), just type 'scons' to build the default
362632Sstever@eecs.umich.edu# configuration (see below), or type 'scons build/<CONFIG>/<binary>'
372632Sstever@eecs.umich.edu# to build some other configuration (e.g., 'build/ALPHA_FS/m5.opt' for
382632Sstever@eecs.umich.edu# the optimized full-system version).
39955SN/A#
402632Sstever@eecs.umich.edu# 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
422761Sstever@eecs.umich.edu# expects that all configs under the same build directory are being
432632Sstever@eecs.umich.edu# built for the same host system.
442632Sstever@eecs.umich.edu#
452632Sstever@eecs.umich.edu# Examples:
462761Sstever@eecs.umich.edu#
472761Sstever@eecs.umich.edu#   The following two commands are equivalent.  The '-u' option tells
482761Sstever@eecs.umich.edu#   scons to search up the directory tree for this SConstruct file.
492632Sstever@eecs.umich.edu#   % cd <path-to-src>/m5 ; scons build/ALPHA_FS/m5.debug
502632Sstever@eecs.umich.edu#   % cd <path-to-src>/m5/build/ALPHA_FS; scons -u m5.debug
512761Sstever@eecs.umich.edu#
522761Sstever@eecs.umich.edu#   The following two commands are equivalent and demonstrate building
532761Sstever@eecs.umich.edu#   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.
562632Sstever@eecs.umich.edu#   % cd <path-to-src>/m5 ; scons /local/foo/build/ALPHA_FS/m5.debug
572632Sstever@eecs.umich.edu#   % cd /local/foo/build/ALPHA_FS; scons -C <path-to-src>/m5 m5.debug
582632Sstever@eecs.umich.edu#
592632Sstever@eecs.umich.edu# You can use 'scons -H' to print scons options.  If you're in this
602632Sstever@eecs.umich.edu# '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.
63955SN/A#
64955SN/A###################################################
65955SN/A
66955SN/Aimport sys
67955SN/Aimport os
685396Ssaidi@eecs.umich.eduimport re
694202Sbinkertn@umich.edu
705342Sstever@gmail.comfrom os.path import isdir, isfile, join as joinpath
71955SN/A
725273Sstever@gmail.comimport SCons
735273Sstever@gmail.com
742656Sstever@eecs.umich.edu# Check for recent-enough Python and SCons versions.  If your system's
752656Sstever@eecs.umich.edu# default installation of Python is not recent enough, you can use a
762656Sstever@eecs.umich.edu# non-default installation of the Python interpreter by either (1)
772656Sstever@eecs.umich.edu# rearranging your PATH so that scons finds the non-default 'python'
782656Sstever@eecs.umich.edu# first or (2) explicitly invoking an alternative interpreter on the
792656Sstever@eecs.umich.edu# scons script, e.g., "/usr/local/bin/python2.4 `which scons` [args]".
802656Sstever@eecs.umich.eduEnsurePythonVersion(2,4)
812653Sstever@eecs.umich.edu
825227Ssaidi@eecs.umich.edu# Import subprocess after we check the version since it doesn't exist in
835227Ssaidi@eecs.umich.edu# Python < 2.4.
845227Ssaidi@eecs.umich.eduimport subprocess
855227Ssaidi@eecs.umich.edu
865396Ssaidi@eecs.umich.edu# helper function: compare arrays or strings of version numbers.
875396Ssaidi@eecs.umich.edu# E.g., compare_version((1,3,25), (1,4,1)')
885396Ssaidi@eecs.umich.edu# returns -1, 0, 1 if v1 is <, ==, > v2
895396Ssaidi@eecs.umich.edudef compare_versions(v1, v2):
905396Ssaidi@eecs.umich.edu    def make_version_list(v):
915396Ssaidi@eecs.umich.edu        if isinstance(v, (list,tuple)):
925396Ssaidi@eecs.umich.edu            return v
935396Ssaidi@eecs.umich.edu        elif isinstance(v, str):
945396Ssaidi@eecs.umich.edu            return map(int, v.split('.'))
955396Ssaidi@eecs.umich.edu        else:
965396Ssaidi@eecs.umich.edu            raise TypeError
975396Ssaidi@eecs.umich.edu
985396Ssaidi@eecs.umich.edu    v1 = make_version_list(v1)
995396Ssaidi@eecs.umich.edu    v2 = make_version_list(v2)
1005396Ssaidi@eecs.umich.edu    # Compare corresponding elements of lists
1015396Ssaidi@eecs.umich.edu    for n1,n2 in zip(v1, v2):
1025396Ssaidi@eecs.umich.edu        if n1 < n2: return -1
1035396Ssaidi@eecs.umich.edu        if n1 > n2: return  1
1045396Ssaidi@eecs.umich.edu    # all corresponding values are equal... see if one has extra values
1055396Ssaidi@eecs.umich.edu    if len(v1) < len(v2): return -1
1065396Ssaidi@eecs.umich.edu    if len(v1) > len(v2): return  1
1075396Ssaidi@eecs.umich.edu    return 0
1085396Ssaidi@eecs.umich.edu
1095396Ssaidi@eecs.umich.edu# SCons version numbers need special processing because they can have
1105396Ssaidi@eecs.umich.edu# charecters and an release date embedded in them. This function does
1115396Ssaidi@eecs.umich.edu# the magic to extract them in a similar way to the SCons internal function
1125396Ssaidi@eecs.umich.edu# function does and then checks that the current version is not contained in
1135396Ssaidi@eecs.umich.edu# a list of version tuples (bad_ver_strs)
1145396Ssaidi@eecs.umich.edudef CheckSCons(bad_ver_strs):
1155396Ssaidi@eecs.umich.edu    def scons_ver(v):
1165396Ssaidi@eecs.umich.edu        num_parts = v.split(' ')[0].split('.')
1175396Ssaidi@eecs.umich.edu        major = int(num_parts[0])
1185396Ssaidi@eecs.umich.edu        minor = int(re.match('\d+', num_parts[1]).group())
1195396Ssaidi@eecs.umich.edu        rev = 0
1205396Ssaidi@eecs.umich.edu        rdate = 0
1215396Ssaidi@eecs.umich.edu        if len(num_parts) > 2:
1225396Ssaidi@eecs.umich.edu            try: rev = int(re.match('\d+', num_parts[2]).group())
1235396Ssaidi@eecs.umich.edu            except: pass
1245396Ssaidi@eecs.umich.edu            rev_parts = num_parts[2].split('d')
1255396Ssaidi@eecs.umich.edu            if len(rev_parts) > 1:
1265396Ssaidi@eecs.umich.edu                rdate = int(re.match('\d+', rev_parts[1]).group())
1275396Ssaidi@eecs.umich.edu
1285396Ssaidi@eecs.umich.edu        return (major, minor, rev, rdate)
1295396Ssaidi@eecs.umich.edu
1305396Ssaidi@eecs.umich.edu    sc_ver = scons_ver(SCons.__version__)
1315396Ssaidi@eecs.umich.edu    for bad_ver in bad_ver_strs:
1325396Ssaidi@eecs.umich.edu        bv = (scons_ver(bad_ver[0]), scons_ver(bad_ver[1]))
1335396Ssaidi@eecs.umich.edu        if  compare_versions(sc_ver, bv[0]) != -1 and\
1345396Ssaidi@eecs.umich.edu            compare_versions(sc_ver, bv[1]) != 1:
1355396Ssaidi@eecs.umich.edu            print "The version of SCons that you have installed: ", SCons.__version__
1365396Ssaidi@eecs.umich.edu            print "has a bug that prevents it from working correctly with M5."
1375396Ssaidi@eecs.umich.edu            print "Please install a version NOT contained within the following",
1385396Ssaidi@eecs.umich.edu            print "ranges (inclusive):"
1395396Ssaidi@eecs.umich.edu            for bad_ver in bad_ver_strs:
1405396Ssaidi@eecs.umich.edu                print "    %s - %s" % bad_ver
1415396Ssaidi@eecs.umich.edu            Exit(2)
1425396Ssaidi@eecs.umich.edu
1435396Ssaidi@eecs.umich.eduCheckSCons(( 
1445396Ssaidi@eecs.umich.edu    # We need a version that is 0.96.91 or newer
1455396Ssaidi@eecs.umich.edu    ('0.0.0', '0.96.90'), 
1465396Ssaidi@eecs.umich.edu    ))
1474781Snate@binkert.org
1481852SN/A
149955SN/A# The absolute path to the current directory (where this file lives).
150955SN/AROOT = Dir('.').abspath
151955SN/A
1523717Sstever@eecs.umich.edu# Path to the M5 source tree.
1533716Sstever@eecs.umich.eduSRCDIR = joinpath(ROOT, 'src')
154955SN/A
1551533SN/A# tell python where to find m5 python code
1563716Sstever@eecs.umich.edusys.path.append(joinpath(ROOT, 'src/python'))
1571533SN/A
1584678Snate@binkert.orgdef check_style_hook(ui):
1594678Snate@binkert.org    ui.readconfig(joinpath(ROOT, '.hg', 'hgrc'))
1604678Snate@binkert.org    style_hook = ui.config('hooks', 'pretxncommit.style', None)
1614678Snate@binkert.org
1624678Snate@binkert.org    if not style_hook:
1634678Snate@binkert.org        print """\
1644678Snate@binkert.orgYou're missing the M5 style hook.
1654678Snate@binkert.orgPlease install the hook so we can ensure that all code fits a common style.
1664678Snate@binkert.org
1674678Snate@binkert.orgAll you'd need to do is add the following lines to your repository .hg/hgrc
1684678Snate@binkert.orgor your personal .hgrc
1694678Snate@binkert.org----------------
1704678Snate@binkert.org
1714678Snate@binkert.org[extensions]
1724678Snate@binkert.orgstyle = %s/util/style.py
1734678Snate@binkert.org
1744678Snate@binkert.org[hooks]
1754678Snate@binkert.orgpretxncommit.style = python:style.check_whitespace
1764678Snate@binkert.org""" % (ROOT)
1774678Snate@binkert.org        sys.exit(1)
1784678Snate@binkert.org
1794973Ssaidi@eecs.umich.eduif ARGUMENTS.get('IGNORE_STYLE') != 'True' and isdir(joinpath(ROOT, '.hg')):
1804678Snate@binkert.org    try:
1814678Snate@binkert.org        from mercurial import ui
1824678Snate@binkert.org        check_style_hook(ui.ui())
1834678Snate@binkert.org    except ImportError:
1844678Snate@binkert.org        pass
1854678Snate@binkert.org
186955SN/A###################################################
187955SN/A#
1882632Sstever@eecs.umich.edu# Figure out which configurations to set up based on the path(s) of
1892632Sstever@eecs.umich.edu# the target(s).
190955SN/A#
191955SN/A###################################################
192955SN/A
193955SN/A# Find default configuration & binary.
1942632Sstever@eecs.umich.eduDefault(os.environ.get('M5_DEFAULT_BINARY', 'build/ALPHA_SE/m5.debug'))
195955SN/A
1962632Sstever@eecs.umich.edu# helper function: find last occurrence of element in list
1972632Sstever@eecs.umich.edudef rfind(l, elt, offs = -1):
1982632Sstever@eecs.umich.edu    for i in range(len(l)+offs, 0, -1):
1992632Sstever@eecs.umich.edu        if l[i] == elt:
2002632Sstever@eecs.umich.edu            return i
2012632Sstever@eecs.umich.edu    raise ValueError, "element not found"
2022632Sstever@eecs.umich.edu
2032632Sstever@eecs.umich.edu# Each target must have 'build' in the interior of the path; the
2042632Sstever@eecs.umich.edu# directory below this will determine the build parameters.  For
2052632Sstever@eecs.umich.edu# example, for target 'foo/bar/build/ALPHA_SE/arch/alpha/blah.do' we
2062632Sstever@eecs.umich.edu# recognize that ALPHA_SE specifies the configuration because it
2072632Sstever@eecs.umich.edu# follow 'build' in the bulid path.
2082632Sstever@eecs.umich.edu
2093718Sstever@eecs.umich.edu# Generate absolute paths to targets so we can see where the build dir is
2103718Sstever@eecs.umich.eduif COMMAND_LINE_TARGETS:
2113718Sstever@eecs.umich.edu    # Ask SCons which directory it was invoked from
2123718Sstever@eecs.umich.edu    launch_dir = GetLaunchDir()
2133718Sstever@eecs.umich.edu    # Make targets relative to invocation directory
2143718Sstever@eecs.umich.edu    abs_targets = map(lambda x: os.path.normpath(joinpath(launch_dir, str(x))),
2153718Sstever@eecs.umich.edu                      COMMAND_LINE_TARGETS)
2163718Sstever@eecs.umich.eduelse:
2173718Sstever@eecs.umich.edu    # Default targets are relative to root of tree
2183718Sstever@eecs.umich.edu    abs_targets = map(lambda x: os.path.normpath(joinpath(ROOT, str(x))),
2193718Sstever@eecs.umich.edu                      DEFAULT_TARGETS)
2203718Sstever@eecs.umich.edu
2213718Sstever@eecs.umich.edu
2222634Sstever@eecs.umich.edu# Generate a list of the unique build roots and configs that the
2232634Sstever@eecs.umich.edu# collected targets reference.
2242632Sstever@eecs.umich.edubuild_paths = []
2252638Sstever@eecs.umich.edubuild_root = None
2262632Sstever@eecs.umich.edufor t in abs_targets:
2272632Sstever@eecs.umich.edu    path_dirs = t.split('/')
2282632Sstever@eecs.umich.edu    try:
2292632Sstever@eecs.umich.edu        build_top = rfind(path_dirs, 'build', -2)
2302632Sstever@eecs.umich.edu    except:
2312632Sstever@eecs.umich.edu        print "Error: no non-leaf 'build' dir found on target path", t
2321858SN/A        Exit(1)
2333716Sstever@eecs.umich.edu    this_build_root = joinpath('/',*path_dirs[:build_top+1])
2342638Sstever@eecs.umich.edu    if not build_root:
2352638Sstever@eecs.umich.edu        build_root = this_build_root
2362638Sstever@eecs.umich.edu    else:
2372638Sstever@eecs.umich.edu        if this_build_root != build_root:
2382638Sstever@eecs.umich.edu            print "Error: build targets not under same build root\n"\
2392638Sstever@eecs.umich.edu                  "  %s\n  %s" % (build_root, this_build_root)
2402638Sstever@eecs.umich.edu            Exit(1)
2413716Sstever@eecs.umich.edu    build_path = joinpath('/',*path_dirs[:build_top+2])
2422634Sstever@eecs.umich.edu    if build_path not in build_paths:
2432634Sstever@eecs.umich.edu        build_paths.append(build_path)
244955SN/A
2455341Sstever@gmail.com# Make sure build_root exists (might not if this is the first build there)
2465341Sstever@gmail.comif not isdir(build_root):
2475341Sstever@gmail.com    os.mkdir(build_root)
2485341Sstever@gmail.com
249955SN/A###################################################
250955SN/A#
251955SN/A# Set up the default build environment.  This environment is copied
252955SN/A# and modified according to each selected configuration.
253955SN/A#
254955SN/A###################################################
255955SN/A
2561858SN/Aenv = Environment(ENV = os.environ,  # inherit user's environment vars
2571858SN/A                  ROOT = ROOT,
2582632Sstever@eecs.umich.edu                  SRCDIR = SRCDIR)
259955SN/A
2604494Ssaidi@eecs.umich.eduExport('env')
2614494Ssaidi@eecs.umich.edu
2623716Sstever@eecs.umich.eduenv.SConsignFile(joinpath(build_root,"sconsign"))
2631105SN/A
2642667Sstever@eecs.umich.edu# Default duplicate option is to use hard links, but this messes up
2652667Sstever@eecs.umich.edu# when you use emacs to edit a file in the target dir, as emacs moves
2662667Sstever@eecs.umich.edu# file to file~ then copies to file, breaking the link.  Symbolic
2672667Sstever@eecs.umich.edu# (soft) links work better.
2682667Sstever@eecs.umich.eduenv.SetOption('duplicate', 'soft-copy')
2692667Sstever@eecs.umich.edu
2701869SN/A# I waffle on this setting... it does avoid a few painful but
2711869SN/A# unnecessary builds, but it also seems to make trivial builds take
2721869SN/A# noticeably longer.
2731869SN/Aif False:
2741869SN/A    env.TargetSignatures('content')
2751065SN/A
2765341Sstever@gmail.com#
2775341Sstever@gmail.com# Set up global sticky options... these are common to an entire build
2785341Sstever@gmail.com# tree (not specific to a particular build like ALPHA_SE)
2795341Sstever@gmail.com#
2805341Sstever@gmail.com
2815341Sstever@gmail.com# Option validators & converters for global sticky options
2825341Sstever@gmail.comdef PathListMakeAbsolute(val):
2835341Sstever@gmail.com    if not val:
2845341Sstever@gmail.com        return val
2855341Sstever@gmail.com    f = lambda p: os.path.abspath(os.path.expanduser(p))
2865341Sstever@gmail.com    return ':'.join(map(f, val.split(':')))
2875341Sstever@gmail.com
2885341Sstever@gmail.comdef PathListAllExist(key, val, env):
2895341Sstever@gmail.com    if not val:
2905341Sstever@gmail.com        return
2915341Sstever@gmail.com    paths = val.split(':')
2925341Sstever@gmail.com    for path in paths:
2935341Sstever@gmail.com        if not isdir(path):
2945341Sstever@gmail.com            raise SCons.Errors.UserError("Path does not exist: '%s'" % path)
2955341Sstever@gmail.com
2965341Sstever@gmail.comglobal_sticky_opts_file = joinpath(build_root, 'options.global')
2975341Sstever@gmail.com
2985341Sstever@gmail.comglobal_sticky_opts = Options(global_sticky_opts_file, args=ARGUMENTS)
2995341Sstever@gmail.com
3005341Sstever@gmail.comglobal_sticky_opts.AddOptions(
3015341Sstever@gmail.com    ('CC', 'C compiler', os.environ.get('CC', env['CC'])),
3025341Sstever@gmail.com    ('CXX', 'C++ compiler', os.environ.get('CXX', env['CXX'])),
3035397Ssaidi@eecs.umich.edu    ('BATCH', 'Use batch pool for build and tests', False),
3045397Ssaidi@eecs.umich.edu    ('BATCH_CMD', 'Batch pool submission command name', 'qdo'),
3055341Sstever@gmail.com    ('EXTRAS', 'Add Extra directories to the compilation', '',
3065341Sstever@gmail.com     PathListAllExist, PathListMakeAbsolute)
3075341Sstever@gmail.com    )    
3085341Sstever@gmail.com
3095341Sstever@gmail.com
3105341Sstever@gmail.com# base help text
3115341Sstever@gmail.comhelp_text = '''
3125341Sstever@gmail.comUsage: scons [scons options] [build options] [target(s)]
3135341Sstever@gmail.com
3145341Sstever@gmail.com'''
3155341Sstever@gmail.com
3165341Sstever@gmail.comhelp_text += "Global sticky options:\n" \
3175341Sstever@gmail.com             + global_sticky_opts.GenerateHelpText(env)
3185341Sstever@gmail.com
3195341Sstever@gmail.com# Update env with values from ARGUMENTS & file global_sticky_opts_file
3205341Sstever@gmail.comglobal_sticky_opts.Update(env)
3215341Sstever@gmail.com
3225341Sstever@gmail.com# Save sticky option settings back to current options file
3235341Sstever@gmail.comglobal_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
3265341Sstever@gmail.com# look for sources etc.  This list is exported as base_dir_list.
3275344Sstever@gmail.combase_dir_list = [joinpath(ROOT, 'src')]
3285341Sstever@gmail.comif env['EXTRAS']:
3295341Sstever@gmail.com    base_dir_list += env['EXTRAS'].split(':')
3305341Sstever@gmail.com
3315341Sstever@gmail.comExport('base_dir_list')
3325341Sstever@gmail.com
3332632Sstever@eecs.umich.edu# M5_PLY is used by isa_parser.py to find the PLY package.
3345199Sstever@gmail.comenv.Append(ENV = { 'M5_PLY' : str(Dir('ext/ply')) })
3353918Ssaidi@eecs.umich.eduenv['GCC'] = False
3363918Ssaidi@eecs.umich.eduenv['SUNCC'] = False
3373940Ssaidi@eecs.umich.eduenv['ICC'] = False
3384781Snate@binkert.orgenv['GCC'] = subprocess.Popen(env['CXX'] + ' --version', shell=True,
3394781Snate@binkert.org        stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
3403918Ssaidi@eecs.umich.edu        close_fds=True).communicate()[0].find('GCC') >= 0
3414781Snate@binkert.orgenv['SUNCC'] = subprocess.Popen(env['CXX'] + ' -V', shell=True,
3424781Snate@binkert.org        stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
3433918Ssaidi@eecs.umich.edu        close_fds=True).communicate()[0].find('Sun C++') >= 0
3444781Snate@binkert.orgenv['ICC'] = subprocess.Popen(env['CXX'] + ' -V', shell=True,
3454781Snate@binkert.org        stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
3463940Ssaidi@eecs.umich.edu        close_fds=True).communicate()[0].find('Intel') >= 0
3473942Ssaidi@eecs.umich.eduif env['GCC'] + env['SUNCC'] + env['ICC'] > 1:
3483940Ssaidi@eecs.umich.edu    print 'Error: How can we have two at the same time?'
3493918Ssaidi@eecs.umich.edu    Exit(1)
3503918Ssaidi@eecs.umich.edu
351955SN/A
3521858SN/A# Set up default C++ compiler flags
3533918Ssaidi@eecs.umich.eduif env['GCC']:
3543918Ssaidi@eecs.umich.edu    env.Append(CCFLAGS='-pipe')
3553918Ssaidi@eecs.umich.edu    env.Append(CCFLAGS='-fno-strict-aliasing')
3563918Ssaidi@eecs.umich.edu    env.Append(CCFLAGS=Split('-Wall -Wno-sign-compare -Werror -Wundef'))
3573940Ssaidi@eecs.umich.eduelif env['ICC']:
3583940Ssaidi@eecs.umich.edu    pass #Fix me... add warning flags once we clean up icc warnings
3593918Ssaidi@eecs.umich.eduelif env['SUNCC']:
3603918Ssaidi@eecs.umich.edu    env.Append(CCFLAGS='-Qoption ccfe')
3613918Ssaidi@eecs.umich.edu    env.Append(CCFLAGS='-features=gcc')
3623918Ssaidi@eecs.umich.edu    env.Append(CCFLAGS='-features=extensions')
3633918Ssaidi@eecs.umich.edu    env.Append(CCFLAGS='-library=stlport4')
3643918Ssaidi@eecs.umich.edu    env.Append(CCFLAGS='-xar')
3653918Ssaidi@eecs.umich.edu#    env.Append(CCFLAGS='-instances=semiexplicit')
3663918Ssaidi@eecs.umich.eduelse:
3673918Ssaidi@eecs.umich.edu    print 'Error: Don\'t know what compiler options to use for your compiler.'
3683940Ssaidi@eecs.umich.edu    print '       Please fix SConstruct and src/SConscript and try again.'
3693918Ssaidi@eecs.umich.edu    Exit(1)
3703918Ssaidi@eecs.umich.edu
3715397Ssaidi@eecs.umich.edu# Do this after we save setting back, or else we'll tack on an
3725397Ssaidi@eecs.umich.edu# extra 'qdo' every time we run scons.
3735397Ssaidi@eecs.umich.eduif env['BATCH']:
3745397Ssaidi@eecs.umich.edu    env['CC']  = env['BATCH_CMD'] + ' ' + env['CC']
3755397Ssaidi@eecs.umich.edu    env['CXX'] = env['BATCH_CMD'] + ' ' + env['CXX']
3765397Ssaidi@eecs.umich.edu
3771851SN/Aif sys.platform == 'cygwin':
3781851SN/A    # cygwin has some header file issues...
3791858SN/A    env.Append(CCFLAGS=Split("-Wno-uninitialized"))
3805200Sstever@gmail.comenv.Append(CPPPATH=[Dir('ext/dnet')])
381955SN/A
3823053Sstever@eecs.umich.edu# Check for SWIG
3833053Sstever@eecs.umich.eduif not env.has_key('SWIG'):
3843053Sstever@eecs.umich.edu    print 'Error: SWIG utility not found.'
3853053Sstever@eecs.umich.edu    print '       Please install (see http://www.swig.org) and retry.'
3863053Sstever@eecs.umich.edu    Exit(1)
3873053Sstever@eecs.umich.edu
3883053Sstever@eecs.umich.edu# Check for appropriate SWIG version
3893053Sstever@eecs.umich.eduswig_version = os.popen('swig -version').read().split()
3903053Sstever@eecs.umich.edu# First 3 words should be "SWIG Version x.y.z"
3914742Sstever@eecs.umich.eduif len(swig_version) < 3 or \
3924742Sstever@eecs.umich.edu        swig_version[0] != 'SWIG' or swig_version[1] != 'Version':
3933053Sstever@eecs.umich.edu    print 'Error determining SWIG version.'
3943053Sstever@eecs.umich.edu    Exit(1)
3953053Sstever@eecs.umich.edu
3963053Sstever@eecs.umich.edumin_swig_version = '1.3.28'
3973053Sstever@eecs.umich.eduif compare_versions(swig_version[2], min_swig_version) < 0:
3983053Sstever@eecs.umich.edu    print 'Error: SWIG version', min_swig_version, 'or newer required.'
3993053Sstever@eecs.umich.edu    print '       Installed version:', swig_version[2]
4003053Sstever@eecs.umich.edu    Exit(1)
4013053Sstever@eecs.umich.edu
4022667Sstever@eecs.umich.edu# Set up SWIG flags & scanner
4034554Sbinkertn@umich.eduswig_flags=Split('-c++ -python -modern -templatereduce $_CPPINCFLAGS')
4044554Sbinkertn@umich.eduenv.Append(SWIGFLAGS=swig_flags)
4052667Sstever@eecs.umich.edu
4064554Sbinkertn@umich.edu# filter out all existing swig scanners, they mess up the dependency
4074554Sbinkertn@umich.edu# stuff for some reason
4084554Sbinkertn@umich.eduscanners = []
4094554Sbinkertn@umich.edufor scanner in env['SCANNERS']:
4104554Sbinkertn@umich.edu    skeys = scanner.skeys
4114554Sbinkertn@umich.edu    if skeys == '.i':
4124554Sbinkertn@umich.edu        continue
4134781Snate@binkert.org
4144554Sbinkertn@umich.edu    if isinstance(skeys, (list, tuple)) and '.i' in skeys:
4154554Sbinkertn@umich.edu        continue
4162667Sstever@eecs.umich.edu
4174554Sbinkertn@umich.edu    scanners.append(scanner)
4184554Sbinkertn@umich.edu
4194554Sbinkertn@umich.edu# add the new swig scanner that we like better
4204554Sbinkertn@umich.edufrom SCons.Scanner import ClassicCPP as CPPScanner
4212667Sstever@eecs.umich.eduswig_inc_re = '^[ \t]*[%,#][ \t]*(?:include|import)[ \t]*(<|")([^>"]+)(>|")'
4224554Sbinkertn@umich.eduscanners.append(CPPScanner("SwigScan", [ ".i" ], "CPPPATH", swig_inc_re))
4232667Sstever@eecs.umich.edu
4244554Sbinkertn@umich.edu# replace the scanners list that has what we want
4254554Sbinkertn@umich.eduenv['SCANNERS'] = scanners
4262667Sstever@eecs.umich.edu
4275522Snate@binkert.org# Add a custom Check function to the Configure context so that we can
4285522Snate@binkert.org# figure out if the compiler adds leading underscores to global
4295522Snate@binkert.org# variables.  This is needed for the autogenerated asm files that we
4305522Snate@binkert.org# use for embedding the python code.
4315522Snate@binkert.orgdef CheckLeading(context):
4325522Snate@binkert.org    context.Message("Checking for leading underscore in global variables...")
4335522Snate@binkert.org    # 1) Define a global variable called x from asm so the C compiler
4345522Snate@binkert.org    #    won't change the symbol at all.
4355522Snate@binkert.org    # 2) Declare that variable.
4365522Snate@binkert.org    # 3) Use the variable
4375522Snate@binkert.org    #
4385522Snate@binkert.org    # If the compiler prepends an underscore, this will successfully
4395522Snate@binkert.org    # link because the external symbol 'x' will be called '_x' which
4405522Snate@binkert.org    # was defined by the asm statement.  If the compiler does not
4415522Snate@binkert.org    # prepend an underscore, this will not successfully link because
4425522Snate@binkert.org    # '_x' will have been defined by assembly, while the C portion of
4435522Snate@binkert.org    # the code will be trying to use 'x'
4445522Snate@binkert.org    ret = context.TryLink('''
4455522Snate@binkert.org        asm(".globl _x; _x: .byte 0");
4465522Snate@binkert.org        extern int x;
4475522Snate@binkert.org        int main() { return x; }
4485522Snate@binkert.org        ''', extension=".c")
4495522Snate@binkert.org    context.env.Append(LEADING_UNDERSCORE=ret)
4505522Snate@binkert.org    context.Result(ret)
4515522Snate@binkert.org    return ret
4525522Snate@binkert.org
4532638Sstever@eecs.umich.edu# Platform-specific configuration.  Note again that we assume that all
4542638Sstever@eecs.umich.edu# builds under a given build root run on the same host platform.
4552638Sstever@eecs.umich.educonf = Configure(env,
4563716Sstever@eecs.umich.edu                 conf_dir = joinpath(build_root, '.scons_config'),
4575522Snate@binkert.org                 log_file = joinpath(build_root, 'scons_config.log'),
4585522Snate@binkert.org                 custom_tests = { 'CheckLeading' : CheckLeading })
4595522Snate@binkert.org
4605522Snate@binkert.org# Check for leading underscores.  Don't really need to worry either
4615522Snate@binkert.org# way so don't need to check the return code.
4625522Snate@binkert.orgconf.CheckLeading()
4631858SN/A
4645227Ssaidi@eecs.umich.edu# Check if we should compile a 64 bit binary on Mac OS X/Darwin
4655227Ssaidi@eecs.umich.edutry:
4665227Ssaidi@eecs.umich.edu    import platform
4675227Ssaidi@eecs.umich.edu    uname = platform.uname()
4685227Ssaidi@eecs.umich.edu    if uname[0] == 'Darwin' and compare_versions(uname[2], '9.0.0') >= 0:
4695227Ssaidi@eecs.umich.edu        if int(subprocess.Popen('sysctl -n hw.cpu64bit_capable', shell=True,
4705227Ssaidi@eecs.umich.edu               stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
4715227Ssaidi@eecs.umich.edu               close_fds=True).communicate()[0][0]):
4725227Ssaidi@eecs.umich.edu            env.Append(CCFLAGS='-arch x86_64')
4735227Ssaidi@eecs.umich.edu            env.Append(CFLAGS='-arch x86_64')
4745227Ssaidi@eecs.umich.edu            env.Append(LINKFLAGS='-arch x86_64')
4755227Ssaidi@eecs.umich.edu            env.Append(ASFLAGS='-arch x86_64')
4765227Ssaidi@eecs.umich.eduexcept:
4775227Ssaidi@eecs.umich.edu    pass
4785227Ssaidi@eecs.umich.edu
4795204Sstever@gmail.com# Recent versions of scons substitute a "Null" object for Configure()
4805204Sstever@gmail.com# when configuration isn't necessary, e.g., if the "--help" option is
4815204Sstever@gmail.com# present.  Unfortuantely this Null object always returns false,
4825204Sstever@gmail.com# breaking all our configuration checks.  We replace it with our own
4835204Sstever@gmail.com# more optimistic null object that returns True instead.
4845204Sstever@gmail.comif not conf:
4855204Sstever@gmail.com    def NullCheck(*args, **kwargs):
4865204Sstever@gmail.com        return True
4875204Sstever@gmail.com
4885204Sstever@gmail.com    class NullConf:
4895204Sstever@gmail.com        def __init__(self, env):
4905204Sstever@gmail.com            self.env = env
4915204Sstever@gmail.com        def Finish(self):
4925204Sstever@gmail.com            return self.env
4935204Sstever@gmail.com        def __getattr__(self, mname):
4945204Sstever@gmail.com            return NullCheck
4955204Sstever@gmail.com
4965204Sstever@gmail.com    conf = NullConf(env)
4975204Sstever@gmail.com
4983118Sstever@eecs.umich.edu# Find Python include and library directories for embedding the
4993118Sstever@eecs.umich.edu# interpreter.  For consistency, we will use the same Python
5003118Sstever@eecs.umich.edu# installation used to run scons (and thus this script).  If you want
5013118Sstever@eecs.umich.edu# to link in an alternate version, see above for instructions on how
5023118Sstever@eecs.umich.edu# to invoke scons with a different copy of the Python interpreter.
5033118Sstever@eecs.umich.edu
5043118Sstever@eecs.umich.edu# Get brief Python version name (e.g., "python2.4") for locating
5053118Sstever@eecs.umich.edu# include & library files
5063118Sstever@eecs.umich.edupy_version_name = 'python' + sys.version[:3]
5073118Sstever@eecs.umich.edu
5083118Sstever@eecs.umich.edu# include path, e.g. /usr/local/include/python2.4
5093716Sstever@eecs.umich.edupy_header_path = joinpath(sys.exec_prefix, 'include', py_version_name)
5103118Sstever@eecs.umich.eduenv.Append(CPPPATH = py_header_path)
5113118Sstever@eecs.umich.edu# verify that it works
5123118Sstever@eecs.umich.eduif not conf.CheckHeader('Python.h', '<>'):
5133118Sstever@eecs.umich.edu    print "Error: can't find Python.h header in", py_header_path
5143118Sstever@eecs.umich.edu    Exit(1)
5153118Sstever@eecs.umich.edu
5163118Sstever@eecs.umich.edu# add library path too if it's not in the default place
5173118Sstever@eecs.umich.edupy_lib_path = None
5183118Sstever@eecs.umich.eduif sys.exec_prefix != '/usr':
5193716Sstever@eecs.umich.edu    py_lib_path = joinpath(sys.exec_prefix, 'lib')
5203118Sstever@eecs.umich.eduelif sys.platform == 'cygwin':
5213118Sstever@eecs.umich.edu    # cygwin puts the .dll in /bin for some reason
5223118Sstever@eecs.umich.edu    py_lib_path = '/bin'
5233118Sstever@eecs.umich.eduif py_lib_path:
5243118Sstever@eecs.umich.edu    env.Append(LIBPATH = py_lib_path)
5253118Sstever@eecs.umich.edu    print 'Adding', py_lib_path, 'to LIBPATH for', py_version_name
5263118Sstever@eecs.umich.eduif not conf.CheckLib(py_version_name):
5273118Sstever@eecs.umich.edu    print "Error: can't find Python library", py_version_name
5283118Sstever@eecs.umich.edu    Exit(1)
5293118Sstever@eecs.umich.edu
5303483Ssaidi@eecs.umich.edu# On Solaris you need to use libsocket for socket ops
5313494Ssaidi@eecs.umich.eduif not conf.CheckLibWithHeader(None, 'sys/socket.h', 'C++', 'accept(0,0,0);'):
5323494Ssaidi@eecs.umich.edu   if not conf.CheckLibWithHeader('socket', 'sys/socket.h', 'C++', 'accept(0,0,0);'):
5333483Ssaidi@eecs.umich.edu       print "Can't find library with socket calls (e.g. accept())"
5343483Ssaidi@eecs.umich.edu       Exit(1)
5353483Ssaidi@eecs.umich.edu
5363053Sstever@eecs.umich.edu# Check for zlib.  If the check passes, libz will be automatically
5373053Sstever@eecs.umich.edu# added to the LIBS environment variable.
5383918Ssaidi@eecs.umich.eduif not conf.CheckLibWithHeader('z', 'zlib.h', 'C++','zlibVersion();'):
5393053Sstever@eecs.umich.edu    print 'Error: did not find needed zlib compression library '\
5403053Sstever@eecs.umich.edu          'and/or zlib.h header file.'
5413053Sstever@eecs.umich.edu    print '       Please install zlib and try again.'
5423053Sstever@eecs.umich.edu    Exit(1)
5433053Sstever@eecs.umich.edu
5441858SN/A# Check for <fenv.h> (C99 FP environment control)
5451858SN/Ahave_fenv = conf.CheckHeader('fenv.h', '<>')
5461858SN/Aif not have_fenv:
5471858SN/A    print "Warning: Header file <fenv.h> not found."
5481858SN/A    print "         This host has no IEEE FP rounding mode control."
5491858SN/A
5501859SN/A# Check for mysql.
5511858SN/Amysql_config = WhereIs('mysql_config')
5521858SN/Ahave_mysql = mysql_config != None
5531858SN/A
5541859SN/A# Check MySQL version.
5551859SN/Aif have_mysql:
5561862SN/A    mysql_version = os.popen(mysql_config + ' --version').read()
5573053Sstever@eecs.umich.edu    min_mysql_version = '4.1'
5583053Sstever@eecs.umich.edu    if compare_versions(mysql_version, min_mysql_version) < 0:
5593053Sstever@eecs.umich.edu        print 'Warning: MySQL', min_mysql_version, 'or newer required.'
5603053Sstever@eecs.umich.edu        print '         Version', mysql_version, 'detected.'
5611859SN/A        have_mysql = False
5621859SN/A
5631859SN/A# Set up mysql_config commands.
5641859SN/Aif have_mysql:
5651859SN/A    mysql_config_include = mysql_config + ' --include'
5661859SN/A    if os.system(mysql_config_include + ' > /dev/null') != 0:
5671859SN/A        # older mysql_config versions don't support --include, use
5681859SN/A        # --cflags instead
5691862SN/A        mysql_config_include = mysql_config + ' --cflags | sed s/\\\'//g'
5701859SN/A    # This seems to work in all versions
5711859SN/A    mysql_config_libs = mysql_config + ' --libs'
5721859SN/A
5731858SN/Aenv = conf.Finish()
5741858SN/A
5752139SN/A# Define the universe of supported ISAs
5764202Sbinkertn@umich.eduall_isa_list = [ ]
5774202Sbinkertn@umich.eduExport('all_isa_list')
5782139SN/A
5792155SN/A# Define the universe of supported CPU models
5804202Sbinkertn@umich.eduall_cpu_list = [ ]
5814202Sbinkertn@umich.edudefault_cpus = [ ]
5824202Sbinkertn@umich.eduExport('all_cpu_list', 'default_cpus')
5832155SN/A
5841869SN/A# Sticky options get saved in the options file so they persist from
5851869SN/A# one invocation to the next (unless overridden, in which case the new
5861869SN/A# value becomes sticky).
5871869SN/Asticky_opts = Options(args=ARGUMENTS)
5884202Sbinkertn@umich.eduExport('sticky_opts')
5894202Sbinkertn@umich.edu
5904202Sbinkertn@umich.edu# Non-sticky options only apply to the current build.
5914202Sbinkertn@umich.edunonsticky_opts = Options(args=ARGUMENTS)
5924202Sbinkertn@umich.eduExport('nonsticky_opts')
5934202Sbinkertn@umich.edu
5944202Sbinkertn@umich.edu# Walk the tree and execute all SConsopts scripts that wil add to the
5954202Sbinkertn@umich.edu# above options
5965341Sstever@gmail.comfor base_dir in base_dir_list:
5975341Sstever@gmail.com    for root, dirs, files in os.walk(base_dir):
5985341Sstever@gmail.com        if 'SConsopts' in files:
5995342Sstever@gmail.com            print "Reading", joinpath(root, 'SConsopts')
6005342Sstever@gmail.com            SConscript(joinpath(root, 'SConsopts'))
6014202Sbinkertn@umich.edu
6024202Sbinkertn@umich.eduall_isa_list.sort()
6034202Sbinkertn@umich.eduall_cpu_list.sort()
6044202Sbinkertn@umich.edudefault_cpus.sort()
6054202Sbinkertn@umich.edu
6061869SN/Asticky_opts.AddOptions(
6074202Sbinkertn@umich.edu    EnumOption('TARGET_ISA', 'Target ISA', 'alpha', all_isa_list),
6081869SN/A    BoolOption('FULL_SYSTEM', 'Full-system support', False),
6092508SN/A    # There's a bug in scons 0.96.1 that causes ListOptions with list
6102508SN/A    # values (more than one value) not to be able to be restored from
6112508SN/A    # a saved option file.  If this causes trouble then upgrade to
6122508SN/A    # scons 0.96.90 or later.
6134202Sbinkertn@umich.edu    ListOption('CPU_MODELS', 'CPU models', default_cpus, all_cpu_list),
6141869SN/A    BoolOption('NO_FAST_ALLOC', 'Disable fast object allocator', False),
6155385Sstever@gmail.com    BoolOption('FAST_ALLOC_DEBUG', 'Enable fast object allocator debugging',
6165385Sstever@gmail.com               False),
6175385Sstever@gmail.com    BoolOption('FAST_ALLOC_STATS', 'Enable fast object allocator statistics',
6185385Sstever@gmail.com               False),
6191869SN/A    BoolOption('EFENCE', 'Link with Electric Fence malloc debugger',
6201869SN/A               False),
6211869SN/A    BoolOption('SS_COMPATIBLE_FP',
6221869SN/A               'Make floating-point results compatible with SimpleScalar',
6231869SN/A               False),
6241965SN/A    BoolOption('USE_SSE2',
6251965SN/A               'Compile for SSE2 (-msse2) to get IEEE FP on x86 hosts',
6261965SN/A               False),
6271869SN/A    BoolOption('USE_MYSQL', 'Use MySQL for stats output', have_mysql),
6281869SN/A    BoolOption('USE_FENV', 'Use <fenv.h> IEEE mode control', have_fenv),
6292733Sktlim@umich.edu    BoolOption('USE_CHECKER', 'Use checker for detailed CPU models', False),
6301869SN/A    )
6311858SN/A
6321869SN/Anonsticky_opts.AddOptions(
6331869SN/A    BoolOption('update_ref', 'Update test reference outputs', False)
6341869SN/A    )
6351858SN/A
6362761Sstever@eecs.umich.edu# These options get exported to #defines in config/*.hh (see src/SConscript).
6371869SN/Aenv.ExportOptions = ['FULL_SYSTEM', 'ALPHA_TLASER', 'USE_FENV', \
6385385Sstever@gmail.com                     'USE_MYSQL', 'NO_FAST_ALLOC', 'FAST_ALLOC_DEBUG', \
6395385Sstever@gmail.com                     'FAST_ALLOC_STATS', 'SS_COMPATIBLE_FP', \
6405522Snate@binkert.org                     'USE_CHECKER', 'TARGET_ISA']
6411869SN/A
6421869SN/A# Define a handy 'no-op' action
6431869SN/Adef no_action(target, source, env):
6441869SN/A    return 0
6451869SN/A
6461869SN/Aenv.NoAction = Action(no_action, None)
6471858SN/A
648955SN/A###################################################
649955SN/A#
6501869SN/A# Define a SCons builder for configuration flag headers.
6511869SN/A#
6521869SN/A###################################################
6531869SN/A
6541869SN/A# This function generates a config header file that #defines the
6551869SN/A# option symbol to the current option setting (0 or 1).  The source
6561869SN/A# operands are the name of the option and a Value node containing the
6571869SN/A# value of the option.
6581869SN/Adef build_config_file(target, source, env):
6591869SN/A    (option, value) = [s.get_contents() for s in source]
6601869SN/A    f = file(str(target[0]), 'w')
6611869SN/A    print >> f, '#define', option, value
6621869SN/A    f.close()
6631869SN/A    return None
6641869SN/A
6651869SN/A# Generate the message to be printed when building the config file.
6661869SN/Adef build_config_file_string(target, source, env):
6671869SN/A    (option, value) = [s.get_contents() for s in source]
6681869SN/A    return "Defining %s as %s in %s." % (option, value, target[0])
6691869SN/A
6701869SN/A# Combine the two functions into a scons Action object.
6711869SN/Aconfig_action = Action(build_config_file, build_config_file_string)
6721869SN/A
6731869SN/A# The emitter munges the source & target node lists to reflect what
6741869SN/A# we're really doing.
6751869SN/Adef config_emitter(target, source, env):
6761869SN/A    # extract option name from Builder arg
6771869SN/A    option = str(target[0])
6781869SN/A    # True target is config header file
6793716Sstever@eecs.umich.edu    target = joinpath('config', option.lower() + '.hh')
6803356Sbinkertn@umich.edu    val = env[option]
6813356Sbinkertn@umich.edu    if isinstance(val, bool):
6823356Sbinkertn@umich.edu        # Force value to 0/1
6833356Sbinkertn@umich.edu        val = int(val)
6843356Sbinkertn@umich.edu    elif isinstance(val, str):
6853356Sbinkertn@umich.edu        val = '"' + val + '"'
6864781Snate@binkert.org
6871869SN/A    # Sources are option name & value (packaged in SCons Value nodes)
6881869SN/A    return ([target], [Value(option), Value(val)])
6891869SN/A
6901869SN/Aconfig_builder = Builder(emitter = config_emitter, action = config_action)
6911869SN/A
6921869SN/Aenv.Append(BUILDERS = { 'ConfigFile' : config_builder })
6931869SN/A
6942655Sstever@eecs.umich.edu###################################################
6952655Sstever@eecs.umich.edu#
6962655Sstever@eecs.umich.edu# Define a SCons builder for copying files.  This is used by the
6972655Sstever@eecs.umich.edu# Python zipfile code in src/python/SConscript, but is placed up here
6982655Sstever@eecs.umich.edu# since it's potentially more generally applicable.
6992655Sstever@eecs.umich.edu#
7002655Sstever@eecs.umich.edu###################################################
7012655Sstever@eecs.umich.edu
7022655Sstever@eecs.umich.educopy_builder = Builder(action = Copy("$TARGET", "$SOURCE"))
7032655Sstever@eecs.umich.edu
7042655Sstever@eecs.umich.eduenv.Append(BUILDERS = { 'CopyFile' : copy_builder })
7052655Sstever@eecs.umich.edu
7062655Sstever@eecs.umich.edu###################################################
7072655Sstever@eecs.umich.edu#
7082655Sstever@eecs.umich.edu# Define a simple SCons builder to concatenate files.
7092655Sstever@eecs.umich.edu#
7102655Sstever@eecs.umich.edu# Used to append the Python zip archive to the executable.
7112655Sstever@eecs.umich.edu#
7122655Sstever@eecs.umich.edu###################################################
7132655Sstever@eecs.umich.edu
7142655Sstever@eecs.umich.educoncat_builder = Builder(action = Action(['cat $SOURCES > $TARGET',
7152655Sstever@eecs.umich.edu                                          'chmod +x $TARGET']))
7162655Sstever@eecs.umich.edu
7172655Sstever@eecs.umich.eduenv.Append(BUILDERS = { 'Concat' : concat_builder })
7182655Sstever@eecs.umich.edu
7192655Sstever@eecs.umich.edu
7202638Sstever@eecs.umich.edu# libelf build is shared across all configs in the build root.
7212638Sstever@eecs.umich.eduenv.SConscript('ext/libelf/SConscript',
7223716Sstever@eecs.umich.edu               build_dir = joinpath(build_root, 'libelf'),
7232638Sstever@eecs.umich.edu               exports = 'env')
7242638Sstever@eecs.umich.edu
7251869SN/A###################################################
7261869SN/A#
7273546Sgblack@eecs.umich.edu# This function is used to set up a directory with switching headers
7283546Sgblack@eecs.umich.edu#
7293546Sgblack@eecs.umich.edu###################################################
7303546Sgblack@eecs.umich.edu
7314202Sbinkertn@umich.eduenv['ALL_ISA_LIST'] = all_isa_list
7323546Sgblack@eecs.umich.edudef make_switching_dir(dirname, switch_headers, env):
7333546Sgblack@eecs.umich.edu    # Generate the header.  target[0] is the full path of the output
7343546Sgblack@eecs.umich.edu    # header to generate.  'source' is a dummy variable, since we get the
7353546Sgblack@eecs.umich.edu    # list of ISAs from env['ALL_ISA_LIST'].
7363546Sgblack@eecs.umich.edu    def gen_switch_hdr(target, source, env):
7374781Snate@binkert.org        fname = str(target[0])
7384781Snate@binkert.org        basename = os.path.basename(fname)
7394781Snate@binkert.org        f = open(fname, 'w')
7404781Snate@binkert.org        f.write('#include "arch/isa_specific.hh"\n')
7414781Snate@binkert.org        cond = '#if'
7424781Snate@binkert.org        for isa in all_isa_list:
7434781Snate@binkert.org            f.write('%s THE_ISA == %s_ISA\n#include "%s/%s/%s"\n'
7444781Snate@binkert.org                    % (cond, isa.upper(), dirname, isa, basename))
7454781Snate@binkert.org            cond = '#elif'
7464781Snate@binkert.org        f.write('#else\n#error "THE_ISA not set"\n#endif\n')
7474781Snate@binkert.org        f.close()
7484781Snate@binkert.org        return 0
7493546Sgblack@eecs.umich.edu
7503546Sgblack@eecs.umich.edu    # String to print when generating header
7513546Sgblack@eecs.umich.edu    def gen_switch_hdr_string(target, source, env):
7524781Snate@binkert.org        return "Generating switch header " + str(target[0])
7533546Sgblack@eecs.umich.edu
7543546Sgblack@eecs.umich.edu    # Build SCons Action object. 'varlist' specifies env vars that this
7553546Sgblack@eecs.umich.edu    # action depends on; when env['ALL_ISA_LIST'] changes these actions
7563546Sgblack@eecs.umich.edu    # should get re-executed.
7573546Sgblack@eecs.umich.edu    switch_hdr_action = Action(gen_switch_hdr, gen_switch_hdr_string,
7583546Sgblack@eecs.umich.edu                               varlist=['ALL_ISA_LIST'])
7593546Sgblack@eecs.umich.edu
7603546Sgblack@eecs.umich.edu    # Instantiate actions for each header
7613546Sgblack@eecs.umich.edu    for hdr in switch_headers:
7623546Sgblack@eecs.umich.edu        env.Command(hdr, [], switch_hdr_action)
7634202Sbinkertn@umich.eduExport('make_switching_dir')
7643546Sgblack@eecs.umich.edu
7653546Sgblack@eecs.umich.edu###################################################
7663546Sgblack@eecs.umich.edu#
767955SN/A# Define build environments for selected configurations.
768955SN/A#
769955SN/A###################################################
770955SN/A
7711858SN/A# rename base env
7721858SN/Abase_env = env
7731858SN/A
7742632Sstever@eecs.umich.edufor build_path in build_paths:
7752632Sstever@eecs.umich.edu    print "Building in", build_path
7765343Sstever@gmail.com
7775343Sstever@gmail.com    # Make a copy of the build-root environment to use for this config.
7785343Sstever@gmail.com    env = base_env.Copy()
7794773Snate@binkert.org    env['BUILDDIR'] = build_path
7804773Snate@binkert.org
7812632Sstever@eecs.umich.edu    # build_dir is the tail component of build path, and is used to
7822632Sstever@eecs.umich.edu    # determine the build parameters (e.g., 'ALPHA_SE')
7832632Sstever@eecs.umich.edu    (build_root, build_dir) = os.path.split(build_path)
7842023SN/A
7852632Sstever@eecs.umich.edu    # Set env options according to the build directory config.
7862632Sstever@eecs.umich.edu    sticky_opts.files = []
7872632Sstever@eecs.umich.edu    # Options for $BUILD_ROOT/$BUILD_DIR are stored in
7882632Sstever@eecs.umich.edu    # $BUILD_ROOT/options/$BUILD_DIR so you can nuke
7892632Sstever@eecs.umich.edu    # $BUILD_ROOT/$BUILD_DIR without losing your options settings.
7903716Sstever@eecs.umich.edu    current_opts_file = joinpath(build_root, 'options', build_dir)
7915342Sstever@gmail.com    if isfile(current_opts_file):
7922632Sstever@eecs.umich.edu        sticky_opts.files.append(current_opts_file)
7932632Sstever@eecs.umich.edu        print "Using saved options file %s" % current_opts_file
7942632Sstever@eecs.umich.edu    else:
7952632Sstever@eecs.umich.edu        # Build dir-specific options file doesn't exist.
7962023SN/A
7972632Sstever@eecs.umich.edu        # Make sure the directory is there so we can create it later
7982632Sstever@eecs.umich.edu        opt_dir = os.path.dirname(current_opts_file)
7995342Sstever@gmail.com        if not isdir(opt_dir):
8001889SN/A            os.mkdir(opt_dir)
8012632Sstever@eecs.umich.edu
8022632Sstever@eecs.umich.edu        # Get default build options from source tree.  Options are
8032632Sstever@eecs.umich.edu        # normally determined by name of $BUILD_DIR, but can be
8042632Sstever@eecs.umich.edu        # overriden by 'default=' arg on command line.
8053716Sstever@eecs.umich.edu        default_opts_file = joinpath('build_opts',
8063716Sstever@eecs.umich.edu                                     ARGUMENTS.get('default', build_dir))
8075342Sstever@gmail.com        if isfile(default_opts_file):
8082632Sstever@eecs.umich.edu            sticky_opts.files.append(default_opts_file)
8092632Sstever@eecs.umich.edu            print "Options file %s not found,\n  using defaults in %s" \
8102632Sstever@eecs.umich.edu                  % (current_opts_file, default_opts_file)
8112632Sstever@eecs.umich.edu        else:
8122632Sstever@eecs.umich.edu            print "Error: cannot find options file %s or %s" \
8132632Sstever@eecs.umich.edu                  % (current_opts_file, default_opts_file)
8142632Sstever@eecs.umich.edu            Exit(1)
8151888SN/A
8161888SN/A    # Apply current option settings to env
8171869SN/A    sticky_opts.Update(env)
8181869SN/A    nonsticky_opts.Update(env)
8191858SN/A
8205341Sstever@gmail.com    help_text += "\nSticky options for %s:\n" % build_dir \
8212598SN/A                 + sticky_opts.GenerateHelpText(env) \
8222598SN/A                 + "\nNon-sticky options for %s:\n" % build_dir \
8232598SN/A                 + nonsticky_opts.GenerateHelpText(env)
8242598SN/A
8251858SN/A    # Process option settings.
8261858SN/A
8271858SN/A    if not have_fenv and env['USE_FENV']:
8281858SN/A        print "Warning: <fenv.h> not available; " \
8291858SN/A              "forcing USE_FENV to False in", build_dir + "."
8301858SN/A        env['USE_FENV'] = False
8311858SN/A
8321858SN/A    if not env['USE_FENV']:
8331858SN/A        print "Warning: No IEEE FP rounding mode control in", build_dir + "."
8341871SN/A        print "         FP results may deviate slightly from other platforms."
8351858SN/A
8361858SN/A    if env['EFENCE']:
8371858SN/A        env.Append(LIBS=['efence'])
8381858SN/A
8391858SN/A    if env['USE_MYSQL']:
8401858SN/A        if not have_mysql:
8411858SN/A            print "Warning: MySQL not available; " \
8421858SN/A                  "forcing USE_MYSQL to False in", build_dir + "."
8431858SN/A            env['USE_MYSQL'] = False
8441858SN/A        else:
8451858SN/A            print "Compiling in", build_dir, "with MySQL support."
8461859SN/A            env.ParseConfig(mysql_config_libs)
8471859SN/A            env.ParseConfig(mysql_config_include)
8481869SN/A
8491888SN/A    # Save sticky option settings back to current options file
8502632Sstever@eecs.umich.edu    sticky_opts.Save(current_opts_file, env)
8511869SN/A
8521965SN/A    if env['USE_SSE2']:
8531965SN/A        env.Append(CCFLAGS='-msse2')
8541965SN/A
8552761Sstever@eecs.umich.edu    # The src/SConscript file sets up the build rules in 'env' according
8561869SN/A    # to the configured options.  It returns a list of environments,
8571869SN/A    # one for each variant build (debug, opt, etc.)
8582632Sstever@eecs.umich.edu    envList = SConscript('src/SConscript', build_dir = build_path,
8592667Sstever@eecs.umich.edu                         exports = 'env')
8601869SN/A
8611869SN/A    # Set up the regression tests for each build.
8622929Sktlim@umich.edu    for e in envList:
8632929Sktlim@umich.edu        SConscript('tests/SConscript',
8643716Sstever@eecs.umich.edu                   build_dir = joinpath(build_path, 'tests', e.Label),
8652929Sktlim@umich.edu                   exports = { 'env' : e }, duplicate = False)
866955SN/A
8672598SN/AHelp(help_text)
8682598SN/A
8693546Sgblack@eecs.umich.edu
870955SN/A###################################################
871955SN/A#
872955SN/A# Let SCons do its thing.  At this point SCons will use the defined
8731530SN/A# build environments to build the requested targets.
874955SN/A#
875955SN/A###################################################
876955SN/A
877