SConstruct revision 5550:26231e06f86d
14519Sgblack@eecs.umich.edu# -*- mode:python -*-
24519Sgblack@eecs.umich.edu
34519Sgblack@eecs.umich.edu# Copyright (c) 2004-2005 The Regents of The University of Michigan
44519Sgblack@eecs.umich.edu# All rights reserved.
54519Sgblack@eecs.umich.edu#
64519Sgblack@eecs.umich.edu# Redistribution and use in source and binary forms, with or without
74519Sgblack@eecs.umich.edu# modification, are permitted provided that the following conditions are
84519Sgblack@eecs.umich.edu# met: redistributions of source code must retain the above copyright
94519Sgblack@eecs.umich.edu# notice, this list of conditions and the following disclaimer;
104519Sgblack@eecs.umich.edu# redistributions in binary form must reproduce the above copyright
114519Sgblack@eecs.umich.edu# notice, this list of conditions and the following disclaimer in the
124519Sgblack@eecs.umich.edu# documentation and/or other materials provided with the distribution;
134519Sgblack@eecs.umich.edu# neither the name of the copyright holders nor the names of its
144519Sgblack@eecs.umich.edu# contributors may be used to endorse or promote products derived from
154519Sgblack@eecs.umich.edu# this software without specific prior written permission.
164519Sgblack@eecs.umich.edu#
174519Sgblack@eecs.umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
184519Sgblack@eecs.umich.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
194519Sgblack@eecs.umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
204519Sgblack@eecs.umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
214519Sgblack@eecs.umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
224519Sgblack@eecs.umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
234519Sgblack@eecs.umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
244519Sgblack@eecs.umich.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
254519Sgblack@eecs.umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
264519Sgblack@eecs.umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
274519Sgblack@eecs.umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
284519Sgblack@eecs.umich.edu#
294519Sgblack@eecs.umich.edu# Authors: Steve Reinhardt
304519Sgblack@eecs.umich.edu
314519Sgblack@eecs.umich.edu###################################################
324519Sgblack@eecs.umich.edu#
334519Sgblack@eecs.umich.edu# SCons top-level build description (SConstruct) file.
344519Sgblack@eecs.umich.edu#
354519Sgblack@eecs.umich.edu# While in this directory ('m5'), just type 'scons' to build the default
364519Sgblack@eecs.umich.edu# configuration (see below), or type 'scons build/<CONFIG>/<binary>'
374519Sgblack@eecs.umich.edu# to build some other configuration (e.g., 'build/ALPHA_FS/m5.opt' for
384519Sgblack@eecs.umich.edu# the optimized full-system version).
394519Sgblack@eecs.umich.edu#
404519Sgblack@eecs.umich.edu# You can build M5 in a different directory as long as there is a
414519Sgblack@eecs.umich.edu# 'build/<CONFIG>' somewhere along the target path.  The build system
424519Sgblack@eecs.umich.edu# expects that all configs under the same build directory are being
434519Sgblack@eecs.umich.edu# built for the same host system.
444519Sgblack@eecs.umich.edu#
454519Sgblack@eecs.umich.edu# Examples:
464519Sgblack@eecs.umich.edu#
474519Sgblack@eecs.umich.edu#   The following two commands are equivalent.  The '-u' option tells
484519Sgblack@eecs.umich.edu#   scons to search up the directory tree for this SConstruct file.
494519Sgblack@eecs.umich.edu#   % cd <path-to-src>/m5 ; scons build/ALPHA_FS/m5.debug
504519Sgblack@eecs.umich.edu#   % cd <path-to-src>/m5/build/ALPHA_FS; scons -u m5.debug
514519Sgblack@eecs.umich.edu#
524519Sgblack@eecs.umich.edu#   The following two commands are equivalent and demonstrate building
534519Sgblack@eecs.umich.edu#   in a directory outside of the source tree.  The '-C' option tells
544519Sgblack@eecs.umich.edu#   scons to chdir to the specified directory to find this SConstruct
554519Sgblack@eecs.umich.edu#   file.
564519Sgblack@eecs.umich.edu#   % cd <path-to-src>/m5 ; scons /local/foo/build/ALPHA_FS/m5.debug
574519Sgblack@eecs.umich.edu#   % cd /local/foo/build/ALPHA_FS; scons -C <path-to-src>/m5 m5.debug
584519Sgblack@eecs.umich.edu#
594519Sgblack@eecs.umich.edu# You can use 'scons -H' to print scons options.  If you're in this
604519Sgblack@eecs.umich.edu# 'm5' directory (or use -u or -C to tell scons where to find this
614519Sgblack@eecs.umich.edu# file), you can use 'scons -h' to print all the M5-specific build
624519Sgblack@eecs.umich.edu# options as well.
634519Sgblack@eecs.umich.edu#
644519Sgblack@eecs.umich.edu###################################################
654519Sgblack@eecs.umich.edu
664519Sgblack@eecs.umich.eduimport sys
674519Sgblack@eecs.umich.eduimport os
684809Sgblack@eecs.umich.eduimport re
694519Sgblack@eecs.umich.edu
704519Sgblack@eecs.umich.edufrom os.path import isdir, isfile, join as joinpath
714688Sgblack@eecs.umich.edu
724688Sgblack@eecs.umich.eduimport SCons
734688Sgblack@eecs.umich.edu
744688Sgblack@eecs.umich.edu# Check for recent-enough Python and SCons versions.  If your system's
754688Sgblack@eecs.umich.edu# default installation of Python is not recent enough, you can use a
764688Sgblack@eecs.umich.edu# non-default installation of the Python interpreter by either (1)
774708Sgblack@eecs.umich.edu# rearranging your PATH so that scons finds the non-default 'python'
784708Sgblack@eecs.umich.edu# first or (2) explicitly invoking an alternative interpreter on the
794708Sgblack@eecs.umich.edu# scons script, e.g., "/usr/local/bin/python2.4 `which scons` [args]".
804708Sgblack@eecs.umich.eduEnsurePythonVersion(2,4)
814519Sgblack@eecs.umich.edu
824519Sgblack@eecs.umich.edu# Import subprocess after we check the version since it doesn't exist in
834519Sgblack@eecs.umich.edu# Python < 2.4.
844519Sgblack@eecs.umich.eduimport subprocess
854519Sgblack@eecs.umich.edu
864519Sgblack@eecs.umich.edu# helper function: compare arrays or strings of version numbers.
874519Sgblack@eecs.umich.edu# E.g., compare_version((1,3,25), (1,4,1)')
884519Sgblack@eecs.umich.edu# returns -1, 0, 1 if v1 is <, ==, > v2
894519Sgblack@eecs.umich.edudef compare_versions(v1, v2):
904519Sgblack@eecs.umich.edu    def make_version_list(v):
914519Sgblack@eecs.umich.edu        if isinstance(v, (list,tuple)):
924951Sgblack@eecs.umich.edu            return v
934519Sgblack@eecs.umich.edu        elif isinstance(v, str):
944519Sgblack@eecs.umich.edu            return map(int, v.split('.'))
954519Sgblack@eecs.umich.edu        else:
964519Sgblack@eecs.umich.edu            raise TypeError
974519Sgblack@eecs.umich.edu
984519Sgblack@eecs.umich.edu    v1 = make_version_list(v1)
994688Sgblack@eecs.umich.edu    v2 = make_version_list(v2)
1004688Sgblack@eecs.umich.edu    # Compare corresponding elements of lists
1014688Sgblack@eecs.umich.edu    for n1,n2 in zip(v1, v2):
1024688Sgblack@eecs.umich.edu        if n1 < n2: return -1
1034688Sgblack@eecs.umich.edu        if n1 > n2: return  1
1044688Sgblack@eecs.umich.edu    # all corresponding values are equal... see if one has extra values
1054708Sgblack@eecs.umich.edu    if len(v1) < len(v2): return -1
1064708Sgblack@eecs.umich.edu    if len(v1) > len(v2): return  1
1074708Sgblack@eecs.umich.edu    return 0
1084708Sgblack@eecs.umich.edu
1094519Sgblack@eecs.umich.edu# SCons version numbers need special processing because they can have
1104519Sgblack@eecs.umich.edu# charecters and an release date embedded in them. This function does
1114519Sgblack@eecs.umich.edu# the magic to extract them in a similar way to the SCons internal function
1124519Sgblack@eecs.umich.edu# function does and then checks that the current version is not contained in
1134519Sgblack@eecs.umich.edu# a list of version tuples (bad_ver_strs)
1144519Sgblack@eecs.umich.edudef CheckSCons(bad_ver_strs):
1154519Sgblack@eecs.umich.edu    def scons_ver(v):
1164519Sgblack@eecs.umich.edu        num_parts = v.split(' ')[0].split('.')
1174519Sgblack@eecs.umich.edu        major = int(num_parts[0])
1184519Sgblack@eecs.umich.edu        minor = int(re.match('\d+', num_parts[1]).group())
1194519Sgblack@eecs.umich.edu        rev = 0
1204519Sgblack@eecs.umich.edu        rdate = 0
1214519Sgblack@eecs.umich.edu        if len(num_parts) > 2:
1224519Sgblack@eecs.umich.edu            try: rev = int(re.match('\d+', num_parts[2]).group())
1234519Sgblack@eecs.umich.edu            except: pass
1244519Sgblack@eecs.umich.edu            rev_parts = num_parts[2].split('d')
1254519Sgblack@eecs.umich.edu            if len(rev_parts) > 1:
1264519Sgblack@eecs.umich.edu                rdate = int(re.match('\d+', rev_parts[1]).group())
1274519Sgblack@eecs.umich.edu
1284519Sgblack@eecs.umich.edu        return (major, minor, rev, rdate)
1294519Sgblack@eecs.umich.edu
1304712Sgblack@eecs.umich.edu    sc_ver = scons_ver(SCons.__version__)
1314519Sgblack@eecs.umich.edu    for bad_ver in bad_ver_strs:
1324519Sgblack@eecs.umich.edu        bv = (scons_ver(bad_ver[0]), scons_ver(bad_ver[1]))
1334519Sgblack@eecs.umich.edu        if  compare_versions(sc_ver, bv[0]) != -1 and\
1344519Sgblack@eecs.umich.edu            compare_versions(sc_ver, bv[1]) != 1:
1354712Sgblack@eecs.umich.edu            print "The version of SCons that you have installed: ", SCons.__version__
1364519Sgblack@eecs.umich.edu            print "has a bug that prevents it from working correctly with M5."
1374519Sgblack@eecs.umich.edu            print "Please install a version NOT contained within the following",
1384519Sgblack@eecs.umich.edu            print "ranges (inclusive):"
1394519Sgblack@eecs.umich.edu            for bad_ver in bad_ver_strs:
1404519Sgblack@eecs.umich.edu                print "    %s - %s" % bad_ver
1414519Sgblack@eecs.umich.edu            Exit(2)
1424519Sgblack@eecs.umich.edu
1434951Sgblack@eecs.umich.eduCheckSCons(( 
1444519Sgblack@eecs.umich.edu    # We need a version that is 0.96.91 or newer
1454519Sgblack@eecs.umich.edu    ('0.0.0', '0.96.90'), 
1464519Sgblack@eecs.umich.edu    ))
1474519Sgblack@eecs.umich.edu
1484519Sgblack@eecs.umich.edu
1494951Sgblack@eecs.umich.edu# The absolute path to the current directory (where this file lives).
1504519Sgblack@eecs.umich.eduROOT = Dir('.').abspath
1514519Sgblack@eecs.umich.edu
1524951Sgblack@eecs.umich.edu# Path to the M5 source tree.
1534712Sgblack@eecs.umich.eduSRCDIR = joinpath(ROOT, 'src')
1544519Sgblack@eecs.umich.edu
1554951Sgblack@eecs.umich.edu# tell python where to find m5 python code
1564519Sgblack@eecs.umich.edusys.path.append(joinpath(ROOT, 'src/python'))
1574951Sgblack@eecs.umich.edu
1584712Sgblack@eecs.umich.edudef check_style_hook(ui):
1594519Sgblack@eecs.umich.edu    ui.readconfig(joinpath(ROOT, '.hg', 'hgrc'))
1604519Sgblack@eecs.umich.edu    style_hook = ui.config('hooks', 'pretxncommit.style', None)
1614519Sgblack@eecs.umich.edu
1624519Sgblack@eecs.umich.edu    if not style_hook:
1634519Sgblack@eecs.umich.edu        print """\
1644519Sgblack@eecs.umich.eduYou're missing the M5 style hook.
1654519Sgblack@eecs.umich.eduPlease install the hook so we can ensure that all code fits a common style.
1664519Sgblack@eecs.umich.edu
1674519Sgblack@eecs.umich.eduAll you'd need to do is add the following lines to your repository .hg/hgrc
1684519Sgblack@eecs.umich.eduor your personal .hgrc
1694519Sgblack@eecs.umich.edu----------------
1704519Sgblack@eecs.umich.edu
1714519Sgblack@eecs.umich.edu[extensions]
1724519Sgblack@eecs.umich.edustyle = %s/util/style.py
1734519Sgblack@eecs.umich.edu
1744712Sgblack@eecs.umich.edu[hooks]
1754519Sgblack@eecs.umich.edupretxncommit.style = python:style.check_whitespace
1764581Sgblack@eecs.umich.edu""" % (ROOT)
1774688Sgblack@eecs.umich.edu        sys.exit(1)
1784581Sgblack@eecs.umich.edu
1794519Sgblack@eecs.umich.eduif ARGUMENTS.get('IGNORE_STYLE') != 'True' and isdir(joinpath(ROOT, '.hg')):
1804519Sgblack@eecs.umich.edu    try:
1814519Sgblack@eecs.umich.edu        from mercurial import ui
1824519Sgblack@eecs.umich.edu        check_style_hook(ui.ui())
1834519Sgblack@eecs.umich.edu    except ImportError:
1844519Sgblack@eecs.umich.edu        pass
1854519Sgblack@eecs.umich.edu
1864519Sgblack@eecs.umich.edu###################################################
1874712Sgblack@eecs.umich.edu#
1884519Sgblack@eecs.umich.edu# Figure out which configurations to set up based on the path(s) of
1894581Sgblack@eecs.umich.edu# the target(s).
1904688Sgblack@eecs.umich.edu#
1914581Sgblack@eecs.umich.edu###################################################
1924519Sgblack@eecs.umich.edu
1934519Sgblack@eecs.umich.edu# Find default configuration & binary.
1944519Sgblack@eecs.umich.eduDefault(os.environ.get('M5_DEFAULT_BINARY', 'build/ALPHA_SE/m5.debug'))
1954519Sgblack@eecs.umich.edu
1964519Sgblack@eecs.umich.edu# helper function: find last occurrence of element in list
1974519Sgblack@eecs.umich.edudef rfind(l, elt, offs = -1):
1984519Sgblack@eecs.umich.edu    for i in range(len(l)+offs, 0, -1):
1994951Sgblack@eecs.umich.edu        if l[i] == elt:
2004519Sgblack@eecs.umich.edu            return i
2014519Sgblack@eecs.umich.edu    raise ValueError, "element not found"
2024519Sgblack@eecs.umich.edu
2034519Sgblack@eecs.umich.edu# Each target must have 'build' in the interior of the path; the
2044951Sgblack@eecs.umich.edu# directory below this will determine the build parameters.  For
2054519Sgblack@eecs.umich.edu# example, for target 'foo/bar/build/ALPHA_SE/arch/alpha/blah.do' we
2064951Sgblack@eecs.umich.edu# recognize that ALPHA_SE specifies the configuration because it
2074712Sgblack@eecs.umich.edu# follow 'build' in the bulid path.
2084519Sgblack@eecs.umich.edu
2094581Sgblack@eecs.umich.edu# Generate absolute paths to targets so we can see where the build dir is
2104688Sgblack@eecs.umich.eduif COMMAND_LINE_TARGETS:
2114581Sgblack@eecs.umich.edu    # Ask SCons which directory it was invoked from
2124519Sgblack@eecs.umich.edu    launch_dir = GetLaunchDir()
2134519Sgblack@eecs.umich.edu    # Make targets relative to invocation directory
2144519Sgblack@eecs.umich.edu    abs_targets = map(lambda x: os.path.normpath(joinpath(launch_dir, str(x))),
2154519Sgblack@eecs.umich.edu                      COMMAND_LINE_TARGETS)
2164951Sgblack@eecs.umich.eduelse:
2174519Sgblack@eecs.umich.edu    # Default targets are relative to root of tree
2184519Sgblack@eecs.umich.edu    abs_targets = map(lambda x: os.path.normpath(joinpath(ROOT, str(x))),
2194951Sgblack@eecs.umich.edu                      DEFAULT_TARGETS)
2204712Sgblack@eecs.umich.edu
2214519Sgblack@eecs.umich.edu
2224581Sgblack@eecs.umich.edu# Generate a list of the unique build roots and configs that the
2234688Sgblack@eecs.umich.edu# collected targets reference.
2244581Sgblack@eecs.umich.edubuild_paths = []
2254519Sgblack@eecs.umich.edubuild_root = None
2264519Sgblack@eecs.umich.edufor t in abs_targets:
2274519Sgblack@eecs.umich.edu    path_dirs = t.split('/')
2284519Sgblack@eecs.umich.edu    try:
2294519Sgblack@eecs.umich.edu        build_top = rfind(path_dirs, 'build', -2)
2304519Sgblack@eecs.umich.edu    except:
2315040Sgblack@eecs.umich.edu        print "Error: no non-leaf 'build' dir found on target path", t
2325040Sgblack@eecs.umich.edu        Exit(1)
2335040Sgblack@eecs.umich.edu    this_build_root = joinpath('/',*path_dirs[:build_top+1])
2345040Sgblack@eecs.umich.edu    if not build_root:
2355040Sgblack@eecs.umich.edu        build_root = this_build_root
2365040Sgblack@eecs.umich.edu    else:
2375040Sgblack@eecs.umich.edu        if this_build_root != build_root:
2385040Sgblack@eecs.umich.edu            print "Error: build targets not under same build root\n"\
2395040Sgblack@eecs.umich.edu                  "  %s\n  %s" % (build_root, this_build_root)
2405040Sgblack@eecs.umich.edu            Exit(1)
2415040Sgblack@eecs.umich.edu    build_path = joinpath('/',*path_dirs[:build_top+2])
2425040Sgblack@eecs.umich.edu    if build_path not in build_paths:
2435040Sgblack@eecs.umich.edu        build_paths.append(build_path)
2445040Sgblack@eecs.umich.edu
2455040Sgblack@eecs.umich.edu# Make sure build_root exists (might not if this is the first build there)
2465040Sgblack@eecs.umich.eduif not isdir(build_root):
2475040Sgblack@eecs.umich.edu    os.mkdir(build_root)
2485040Sgblack@eecs.umich.edu
2495040Sgblack@eecs.umich.edu###################################################
2505040Sgblack@eecs.umich.edu#
2515040Sgblack@eecs.umich.edu# Set up the default build environment.  This environment is copied
2525040Sgblack@eecs.umich.edu# and modified according to each selected configuration.
2535040Sgblack@eecs.umich.edu#
2545040Sgblack@eecs.umich.edu###################################################
2555040Sgblack@eecs.umich.edu
2565040Sgblack@eecs.umich.eduenv = Environment(ENV = os.environ,  # inherit user's environment vars
2575040Sgblack@eecs.umich.edu                  ROOT = ROOT,
2585040Sgblack@eecs.umich.edu                  SRCDIR = SRCDIR)
2595040Sgblack@eecs.umich.edu
2605040Sgblack@eecs.umich.eduExport('env')
2615040Sgblack@eecs.umich.edu
2625040Sgblack@eecs.umich.eduenv.SConsignFile(joinpath(build_root,"sconsign"))
2635040Sgblack@eecs.umich.edu
2645040Sgblack@eecs.umich.edu# Default duplicate option is to use hard links, but this messes up
2655040Sgblack@eecs.umich.edu# when you use emacs to edit a file in the target dir, as emacs moves
2665040Sgblack@eecs.umich.edu# file to file~ then copies to file, breaking the link.  Symbolic
2675040Sgblack@eecs.umich.edu# (soft) links work better.
2685040Sgblack@eecs.umich.eduenv.SetOption('duplicate', 'soft-copy')
2695040Sgblack@eecs.umich.edu
2705040Sgblack@eecs.umich.edu# I waffle on this setting... it does avoid a few painful but
2715040Sgblack@eecs.umich.edu# unnecessary builds, but it also seems to make trivial builds take
2725040Sgblack@eecs.umich.edu# noticeably longer.
2735040Sgblack@eecs.umich.eduif False:
2745040Sgblack@eecs.umich.edu    env.TargetSignatures('content')
2755040Sgblack@eecs.umich.edu
2765040Sgblack@eecs.umich.edu#
2775040Sgblack@eecs.umich.edu# Set up global sticky options... these are common to an entire build
2785040Sgblack@eecs.umich.edu# tree (not specific to a particular build like ALPHA_SE)
2795040Sgblack@eecs.umich.edu#
2805040Sgblack@eecs.umich.edu
2815040Sgblack@eecs.umich.edu# Option validators & converters for global sticky options
2825040Sgblack@eecs.umich.edudef PathListMakeAbsolute(val):
2835040Sgblack@eecs.umich.edu    if not val:
2845040Sgblack@eecs.umich.edu        return val
2855040Sgblack@eecs.umich.edu    f = lambda p: os.path.abspath(os.path.expanduser(p))
2865061Sgblack@eecs.umich.edu    return ':'.join(map(f, val.split(':')))
2875040Sgblack@eecs.umich.edu
2885040Sgblack@eecs.umich.edudef PathListAllExist(key, val, env):
2895061Sgblack@eecs.umich.edu    if not val:
2905061Sgblack@eecs.umich.edu        return
2915061Sgblack@eecs.umich.edu    paths = val.split(':')
2925061Sgblack@eecs.umich.edu    for path in paths:
2935061Sgblack@eecs.umich.edu        if not isdir(path):
2945061Sgblack@eecs.umich.edu            raise SCons.Errors.UserError("Path does not exist: '%s'" % path)
2955061Sgblack@eecs.umich.edu
2965061Sgblack@eecs.umich.eduglobal_sticky_opts_file = joinpath(build_root, 'options.global')
2975040Sgblack@eecs.umich.edu
2985040Sgblack@eecs.umich.eduglobal_sticky_opts = Options(global_sticky_opts_file, args=ARGUMENTS)
2995040Sgblack@eecs.umich.edu
3005040Sgblack@eecs.umich.eduglobal_sticky_opts.AddOptions(
3015040Sgblack@eecs.umich.edu    ('CC', 'C compiler', os.environ.get('CC', env['CC'])),
3025040Sgblack@eecs.umich.edu    ('CXX', 'C++ compiler', os.environ.get('CXX', env['CXX'])),
3035040Sgblack@eecs.umich.edu    ('BATCH', 'Use batch pool for build and tests', False),
3045040Sgblack@eecs.umich.edu    ('BATCH_CMD', 'Batch pool submission command name', 'qdo'),
3055040Sgblack@eecs.umich.edu    ('EXTRAS', 'Add Extra directories to the compilation', '',
3065040Sgblack@eecs.umich.edu     PathListAllExist, PathListMakeAbsolute)
3075040Sgblack@eecs.umich.edu    )    
3085040Sgblack@eecs.umich.edu
3095040Sgblack@eecs.umich.edu
3105040Sgblack@eecs.umich.edu# base help text
3115040Sgblack@eecs.umich.eduhelp_text = '''
3125040Sgblack@eecs.umich.eduUsage: scons [scons options] [build options] [target(s)]
3135040Sgblack@eecs.umich.edu
3145040Sgblack@eecs.umich.edu'''
3155040Sgblack@eecs.umich.edu
3165040Sgblack@eecs.umich.eduhelp_text += "Global sticky options:\n" \
3175040Sgblack@eecs.umich.edu             + global_sticky_opts.GenerateHelpText(env)
3185040Sgblack@eecs.umich.edu
3195040Sgblack@eecs.umich.edu# Update env with values from ARGUMENTS & file global_sticky_opts_file
3205040Sgblack@eecs.umich.eduglobal_sticky_opts.Update(env)
3215040Sgblack@eecs.umich.edu
3224688Sgblack@eecs.umich.edu# Save sticky option settings back to current options file
3235040Sgblack@eecs.umich.eduglobal_sticky_opts.Save(global_sticky_opts_file, env)
3244688Sgblack@eecs.umich.edu
3254688Sgblack@eecs.umich.edu# Parse EXTRAS option to build list of all directories where we're
3264688Sgblack@eecs.umich.edu# look for sources etc.  This list is exported as base_dir_list.
3274688Sgblack@eecs.umich.edubase_dir_list = [joinpath(ROOT, 'src')]
3285040Sgblack@eecs.umich.eduif env['EXTRAS']:
3294688Sgblack@eecs.umich.edu    base_dir_list += env['EXTRAS'].split(':')
3305040Sgblack@eecs.umich.edu
3315040Sgblack@eecs.umich.eduExport('base_dir_list')
3325040Sgblack@eecs.umich.edu
3335040Sgblack@eecs.umich.edu# M5_PLY is used by isa_parser.py to find the PLY package.
3345040Sgblack@eecs.umich.eduenv.Append(ENV = { 'M5_PLY' : str(Dir('ext/ply')) })
3355040Sgblack@eecs.umich.eduenv['GCC'] = subprocess.Popen(env['CXX'] + ' --version', shell=True,
3365040Sgblack@eecs.umich.edu        stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
3375040Sgblack@eecs.umich.edu        close_fds=True).communicate()[0].find('g++') >= 0
3385040Sgblack@eecs.umich.eduenv['SUNCC'] = subprocess.Popen(env['CXX'] + ' -V', shell=True,
3395040Sgblack@eecs.umich.edu        stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
3405040Sgblack@eecs.umich.edu        close_fds=True).communicate()[0].find('Sun C++') >= 0
3415040Sgblack@eecs.umich.eduenv['ICC'] = subprocess.Popen(env['CXX'] + ' -V', shell=True,
3425040Sgblack@eecs.umich.edu        stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
3435040Sgblack@eecs.umich.edu        close_fds=True).communicate()[0].find('Intel') >= 0
3445040Sgblack@eecs.umich.eduif env['GCC'] + env['SUNCC'] + env['ICC'] > 1:
3455040Sgblack@eecs.umich.edu    print 'Error: How can we have two at the same time?'
3465040Sgblack@eecs.umich.edu    Exit(1)
3475040Sgblack@eecs.umich.edu
3485040Sgblack@eecs.umich.edu
3495040Sgblack@eecs.umich.edu# Set up default C++ compiler flags
3505040Sgblack@eecs.umich.eduif env['GCC']:
3515040Sgblack@eecs.umich.edu    env.Append(CCFLAGS='-pipe')
3524688Sgblack@eecs.umich.edu    env.Append(CCFLAGS='-fno-strict-aliasing')
3534688Sgblack@eecs.umich.edu    env.Append(CCFLAGS=Split('-Wall -Wno-sign-compare -Werror -Wundef'))
3545040Sgblack@eecs.umich.eduelif env['ICC']:
3555040Sgblack@eecs.umich.edu    pass #Fix me... add warning flags once we clean up icc warnings
3565040Sgblack@eecs.umich.eduelif env['SUNCC']:
3575040Sgblack@eecs.umich.edu    env.Append(CCFLAGS='-Qoption ccfe')
3584688Sgblack@eecs.umich.edu    env.Append(CCFLAGS='-features=gcc')
3594688Sgblack@eecs.umich.edu    env.Append(CCFLAGS='-features=extensions')
3605040Sgblack@eecs.umich.edu    env.Append(CCFLAGS='-library=stlport4')
3615040Sgblack@eecs.umich.edu    env.Append(CCFLAGS='-xar')
3625040Sgblack@eecs.umich.edu#    env.Append(CCFLAGS='-instances=semiexplicit')
3635040Sgblack@eecs.umich.eduelse:
3645040Sgblack@eecs.umich.edu    print 'Error: Don\'t know what compiler options to use for your compiler.'
3655040Sgblack@eecs.umich.edu    print '       Please fix SConstruct and src/SConscript and try again.'
3664519Sgblack@eecs.umich.edu    Exit(1)
3674519Sgblack@eecs.umich.edu
3685040Sgblack@eecs.umich.edu# Do this after we save setting back, or else we'll tack on an
3694688Sgblack@eecs.umich.edu# extra 'qdo' every time we run scons.
3704701Sgblack@eecs.umich.eduif env['BATCH']:
3714688Sgblack@eecs.umich.edu    env['CC']  = env['BATCH_CMD'] + ' ' + env['CC']
3724688Sgblack@eecs.umich.edu    env['CXX'] = env['BATCH_CMD'] + ' ' + env['CXX']
3734688Sgblack@eecs.umich.edu
3744688Sgblack@eecs.umich.eduif sys.platform == 'cygwin':
3754688Sgblack@eecs.umich.edu    # cygwin has some header file issues...
3764688Sgblack@eecs.umich.edu    env.Append(CCFLAGS=Split("-Wno-uninitialized"))
3774688Sgblack@eecs.umich.eduenv.Append(CPPPATH=[Dir('ext/dnet')])
3784519Sgblack@eecs.umich.edu
3794519Sgblack@eecs.umich.edu# Check for SWIG
3805040Sgblack@eecs.umich.eduif not env.has_key('SWIG'):
3815040Sgblack@eecs.umich.edu    print 'Error: SWIG utility not found.'
3825040Sgblack@eecs.umich.edu    print '       Please install (see http://www.swig.org) and retry.'
3834560Sgblack@eecs.umich.edu    Exit(1)
3845040Sgblack@eecs.umich.edu
3854688Sgblack@eecs.umich.edu# Check for appropriate SWIG version
3865040Sgblack@eecs.umich.eduswig_version = os.popen('swig -version').read().split()
3874519Sgblack@eecs.umich.edu# First 3 words should be "SWIG Version x.y.z"
3885040Sgblack@eecs.umich.eduif len(swig_version) < 3 or \
3894519Sgblack@eecs.umich.edu        swig_version[0] != 'SWIG' or swig_version[1] != 'Version':
3904519Sgblack@eecs.umich.edu    print 'Error determining SWIG version.'
3914519Sgblack@eecs.umich.edu    Exit(1)
3924539Sgblack@eecs.umich.edu
3934519Sgblack@eecs.umich.edumin_swig_version = '1.3.28'
3945040Sgblack@eecs.umich.eduif compare_versions(swig_version[2], min_swig_version) < 0:
3954688Sgblack@eecs.umich.edu    print 'Error: SWIG version', min_swig_version, 'or newer required.'
3965040Sgblack@eecs.umich.edu    print '       Installed version:', swig_version[2]
3975040Sgblack@eecs.umich.edu    Exit(1)
3985040Sgblack@eecs.umich.edu
3995040Sgblack@eecs.umich.edu# Set up SWIG flags & scanner
4005040Sgblack@eecs.umich.eduswig_flags=Split('-c++ -python -modern -templatereduce $_CPPINCFLAGS')
4015040Sgblack@eecs.umich.eduenv.Append(SWIGFLAGS=swig_flags)
4025040Sgblack@eecs.umich.edu
4035040Sgblack@eecs.umich.edu# filter out all existing swig scanners, they mess up the dependency
4044519Sgblack@eecs.umich.edu# stuff for some reason
4055040Sgblack@eecs.umich.eduscanners = []
4065040Sgblack@eecs.umich.edufor scanner in env['SCANNERS']:
4075040Sgblack@eecs.umich.edu    skeys = scanner.skeys
4085040Sgblack@eecs.umich.edu    if skeys == '.i':
4094519Sgblack@eecs.umich.edu        continue
4105040Sgblack@eecs.umich.edu
4115040Sgblack@eecs.umich.edu    if isinstance(skeys, (list, tuple)) and '.i' in skeys:
4125040Sgblack@eecs.umich.edu        continue
4135040Sgblack@eecs.umich.edu
4144519Sgblack@eecs.umich.edu    scanners.append(scanner)
4155040Sgblack@eecs.umich.edu
4165040Sgblack@eecs.umich.edu# add the new swig scanner that we like better
4175040Sgblack@eecs.umich.edufrom SCons.Scanner import ClassicCPP as CPPScanner
4184519Sgblack@eecs.umich.eduswig_inc_re = '^[ \t]*[%,#][ \t]*(?:include|import)[ \t]*(<|")([^>"]+)(>|")'
4195040Sgblack@eecs.umich.eduscanners.append(CPPScanner("SwigScan", [ ".i" ], "CPPPATH", swig_inc_re))
4205040Sgblack@eecs.umich.edu
4214595Sgblack@eecs.umich.edu# replace the scanners list that has what we want
4225040Sgblack@eecs.umich.eduenv['SCANNERS'] = scanners
4235040Sgblack@eecs.umich.edu
4244595Sgblack@eecs.umich.edu# Add a custom Check function to the Configure context so that we can
4255040Sgblack@eecs.umich.edu# figure out if the compiler adds leading underscores to global
4265040Sgblack@eecs.umich.edu# variables.  This is needed for the autogenerated asm files that we
4274732Sgblack@eecs.umich.edu# use for embedding the python code.
4284823Sgblack@eecs.umich.edudef CheckLeading(context):
4295040Sgblack@eecs.umich.edu    context.Message("Checking for leading underscore in global variables...")
4305040Sgblack@eecs.umich.edu    # 1) Define a global variable called x from asm so the C compiler
4315040Sgblack@eecs.umich.edu    #    won't change the symbol at all.
4325040Sgblack@eecs.umich.edu    # 2) Declare that variable.
4334732Sgblack@eecs.umich.edu    # 3) Use the variable
4344823Sgblack@eecs.umich.edu    #
4355040Sgblack@eecs.umich.edu    # If the compiler prepends an underscore, this will successfully
4365040Sgblack@eecs.umich.edu    # link because the external symbol 'x' will be called '_x' which
4375040Sgblack@eecs.umich.edu    # was defined by the asm statement.  If the compiler does not
4385040Sgblack@eecs.umich.edu    # prepend an underscore, this will not successfully link because
4395040Sgblack@eecs.umich.edu    # '_x' will have been defined by assembly, while the C portion of
4405040Sgblack@eecs.umich.edu    # the code will be trying to use 'x'
4415040Sgblack@eecs.umich.edu    ret = context.TryLink('''
4425040Sgblack@eecs.umich.edu        asm(".globl _x; _x: .byte 0");
4435040Sgblack@eecs.umich.edu        extern int x;
4445040Sgblack@eecs.umich.edu        int main() { return x; }
4455040Sgblack@eecs.umich.edu        ''', extension=".c")
4465040Sgblack@eecs.umich.edu    context.env.Append(LEADING_UNDERSCORE=ret)
4475040Sgblack@eecs.umich.edu    context.Result(ret)
4484809Sgblack@eecs.umich.edu    return ret
4494823Sgblack@eecs.umich.edu
4504823Sgblack@eecs.umich.edu# Platform-specific configuration.  Note again that we assume that all
4514809Sgblack@eecs.umich.edu# builds under a given build root run on the same host platform.
4525040Sgblack@eecs.umich.educonf = Configure(env,
4535040Sgblack@eecs.umich.edu                 conf_dir = joinpath(build_root, '.scons_config'),
4545040Sgblack@eecs.umich.edu                 log_file = joinpath(build_root, 'scons_config.log'),
4555040Sgblack@eecs.umich.edu                 custom_tests = { 'CheckLeading' : CheckLeading })
4564809Sgblack@eecs.umich.edu
4574823Sgblack@eecs.umich.edu# Check for leading underscores.  Don't really need to worry either
4584809Sgblack@eecs.umich.edu# way so don't need to check the return code.
4594809Sgblack@eecs.umich.educonf.CheckLeading()
4605040Sgblack@eecs.umich.edu
4615040Sgblack@eecs.umich.edu# Check if we should compile a 64 bit binary on Mac OS X/Darwin
4625040Sgblack@eecs.umich.edutry:
4635042Sgblack@eecs.umich.edu    import platform
4645040Sgblack@eecs.umich.edu    uname = platform.uname()
4655040Sgblack@eecs.umich.edu    if uname[0] == 'Darwin' and compare_versions(uname[2], '9.0.0') >= 0:
4665040Sgblack@eecs.umich.edu        if int(subprocess.Popen('sysctl -n hw.cpu64bit_capable', shell=True,
4674809Sgblack@eecs.umich.edu               stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
4684823Sgblack@eecs.umich.edu               close_fds=True).communicate()[0][0]):
4694823Sgblack@eecs.umich.edu            env.Append(CCFLAGS='-arch x86_64')
4704823Sgblack@eecs.umich.edu            env.Append(CFLAGS='-arch x86_64')
4714823Sgblack@eecs.umich.edu            env.Append(LINKFLAGS='-arch x86_64')
4724809Sgblack@eecs.umich.edu            env.Append(ASFLAGS='-arch x86_64')
4735060Sgblack@eecs.umich.eduexcept:
4745060Sgblack@eecs.umich.edu    pass
4754823Sgblack@eecs.umich.edu
4764809Sgblack@eecs.umich.edu# Recent versions of scons substitute a "Null" object for Configure()
4775040Sgblack@eecs.umich.edu# when configuration isn't necessary, e.g., if the "--help" option is
4785040Sgblack@eecs.umich.edu# present.  Unfortuantely this Null object always returns false,
4795040Sgblack@eecs.umich.edu# breaking all our configuration checks.  We replace it with our own
4805040Sgblack@eecs.umich.edu# more optimistic null object that returns True instead.
4814823Sgblack@eecs.umich.eduif not conf:
4824823Sgblack@eecs.umich.edu    def NullCheck(*args, **kwargs):
4834823Sgblack@eecs.umich.edu        return True
4844823Sgblack@eecs.umich.edu
4854823Sgblack@eecs.umich.edu    class NullConf:
4865040Sgblack@eecs.umich.edu        def __init__(self, env):
4874823Sgblack@eecs.umich.edu            self.env = env
4885040Sgblack@eecs.umich.edu        def Finish(self):
4895040Sgblack@eecs.umich.edu            return self.env
4904732Sgblack@eecs.umich.edu        def __getattr__(self, mname):
4915040Sgblack@eecs.umich.edu            return NullCheck
4925040Sgblack@eecs.umich.edu
4935040Sgblack@eecs.umich.edu    conf = NullConf(env)
4945040Sgblack@eecs.umich.edu
4955040Sgblack@eecs.umich.edu# Find Python include and library directories for embedding the
4965040Sgblack@eecs.umich.edu# interpreter.  For consistency, we will use the same Python
4975040Sgblack@eecs.umich.edu# installation used to run scons (and thus this script).  If you want
4984732Sgblack@eecs.umich.edu# to link in an alternate version, see above for instructions on how
4995040Sgblack@eecs.umich.edu# to invoke scons with a different copy of the Python interpreter.
5005040Sgblack@eecs.umich.edu
5015040Sgblack@eecs.umich.edu# Get brief Python version name (e.g., "python2.4") for locating
5024756Sgblack@eecs.umich.edu# include & library files
5034823Sgblack@eecs.umich.edupy_version_name = 'python' + sys.version[:3]
5045040Sgblack@eecs.umich.edu
5055040Sgblack@eecs.umich.edu# include path, e.g. /usr/local/include/python2.4
5065040Sgblack@eecs.umich.edupy_header_path = joinpath(sys.exec_prefix, 'include', py_version_name)
5075040Sgblack@eecs.umich.eduenv.Append(CPPPATH = py_header_path)
5084756Sgblack@eecs.umich.edu# verify that it works
5094732Sgblack@eecs.umich.eduif not conf.CheckHeader('Python.h', '<>'):
5104732Sgblack@eecs.umich.edu    print "Error: can't find Python.h header in", py_header_path
5114732Sgblack@eecs.umich.edu    Exit(1)
5124732Sgblack@eecs.umich.edu
5134823Sgblack@eecs.umich.edu# add library path too if it's not in the default place
5145040Sgblack@eecs.umich.edupy_lib_path = None
5155040Sgblack@eecs.umich.eduif sys.exec_prefix != '/usr':
5165040Sgblack@eecs.umich.edu    py_lib_path = joinpath(sys.exec_prefix, 'lib')
5175040Sgblack@eecs.umich.eduelif sys.platform == 'cygwin':
5184756Sgblack@eecs.umich.edu    # cygwin puts the .dll in /bin for some reason
5194732Sgblack@eecs.umich.edu    py_lib_path = '/bin'
5204732Sgblack@eecs.umich.eduif py_lib_path:
5214732Sgblack@eecs.umich.edu    env.Append(LIBPATH = py_lib_path)
5224732Sgblack@eecs.umich.edu    print 'Adding', py_lib_path, 'to LIBPATH for', py_version_name
5235032Sgblack@eecs.umich.eduif not conf.CheckLib(py_version_name):
5244823Sgblack@eecs.umich.edu    print "Error: can't find Python library", py_version_name
5255040Sgblack@eecs.umich.edu    Exit(1)
5265040Sgblack@eecs.umich.edu
5275040Sgblack@eecs.umich.edu# On Solaris you need to use libsocket for socket ops
5285040Sgblack@eecs.umich.eduif not conf.CheckLibWithHeader(None, 'sys/socket.h', 'C++', 'accept(0,0,0);'):
5294732Sgblack@eecs.umich.edu   if not conf.CheckLibWithHeader('socket', 'sys/socket.h', 'C++', 'accept(0,0,0);'):
5304756Sgblack@eecs.umich.edu       print "Can't find library with socket calls (e.g. accept())"
5314732Sgblack@eecs.umich.edu       Exit(1)
5324732Sgblack@eecs.umich.edu
5334823Sgblack@eecs.umich.edu# Check for zlib.  If the check passes, libz will be automatically
5344823Sgblack@eecs.umich.edu# added to the LIBS environment variable.
5354732Sgblack@eecs.umich.eduif not conf.CheckLibWithHeader('z', 'zlib.h', 'C++','zlibVersion();'):
5364732Sgblack@eecs.umich.edu    print 'Error: did not find needed zlib compression library '\
5374732Sgblack@eecs.umich.edu          'and/or zlib.h header file.'
5384732Sgblack@eecs.umich.edu    print '       Please install zlib and try again.'
5395040Sgblack@eecs.umich.edu    Exit(1)
5405040Sgblack@eecs.umich.edu
5415040Sgblack@eecs.umich.edu# Check for <fenv.h> (C99 FP environment control)
5425040Sgblack@eecs.umich.eduhave_fenv = conf.CheckHeader('fenv.h', '<>')
5434733Sgblack@eecs.umich.eduif not have_fenv:
5444756Sgblack@eecs.umich.edu    print "Warning: Header file <fenv.h> not found."
5454733Sgblack@eecs.umich.edu    print "         This host has no IEEE FP rounding mode control."
5464733Sgblack@eecs.umich.edu
5474733Sgblack@eecs.umich.edu# Check for mysql.
5484733Sgblack@eecs.umich.edumysql_config = WhereIs('mysql_config')
5494733Sgblack@eecs.umich.eduhave_mysql = mysql_config != None
5504823Sgblack@eecs.umich.edu
5514823Sgblack@eecs.umich.edu# Check MySQL version.
5524733Sgblack@eecs.umich.eduif have_mysql:
5534733Sgblack@eecs.umich.edu    mysql_version = os.popen(mysql_config + ' --version').read()
5544733Sgblack@eecs.umich.edu    min_mysql_version = '4.1'
5554733Sgblack@eecs.umich.edu    if compare_versions(mysql_version, min_mysql_version) < 0:
5565040Sgblack@eecs.umich.edu        print 'Warning: MySQL', min_mysql_version, 'or newer required.'
5575040Sgblack@eecs.umich.edu        print '         Version', mysql_version, 'detected.'
5585040Sgblack@eecs.umich.edu        have_mysql = False
5595040Sgblack@eecs.umich.edu
5604732Sgblack@eecs.umich.edu# Set up mysql_config commands.
5614756Sgblack@eecs.umich.eduif have_mysql:
5624732Sgblack@eecs.umich.edu    mysql_config_include = mysql_config + ' --include'
5634732Sgblack@eecs.umich.edu    if os.system(mysql_config_include + ' > /dev/null') != 0:
5644823Sgblack@eecs.umich.edu        # older mysql_config versions don't support --include, use
5654732Sgblack@eecs.umich.edu        # --cflags instead
5664823Sgblack@eecs.umich.edu        mysql_config_include = mysql_config + ' --cflags | sed s/\\\'//g'
5674732Sgblack@eecs.umich.edu    # This seems to work in all versions
5684732Sgblack@eecs.umich.edu    mysql_config_libs = mysql_config + ' --libs'
5694732Sgblack@eecs.umich.edu
5704732Sgblack@eecs.umich.eduenv = conf.Finish()
5715040Sgblack@eecs.umich.edu
5725040Sgblack@eecs.umich.edu# Define the universe of supported ISAs
5735040Sgblack@eecs.umich.eduall_isa_list = [ ]
5745040Sgblack@eecs.umich.eduExport('all_isa_list')
5754733Sgblack@eecs.umich.edu
5764756Sgblack@eecs.umich.edu# Define the universe of supported CPU models
5774733Sgblack@eecs.umich.eduall_cpu_list = [ ]
5784733Sgblack@eecs.umich.edudefault_cpus = [ ]
5794733Sgblack@eecs.umich.eduExport('all_cpu_list', 'default_cpus')
5804823Sgblack@eecs.umich.edu
5814733Sgblack@eecs.umich.edu# Sticky options get saved in the options file so they persist from
5824733Sgblack@eecs.umich.edu# one invocation to the next (unless overridden, in which case the new
5834733Sgblack@eecs.umich.edu# value becomes sticky).
5844823Sgblack@eecs.umich.edusticky_opts = Options(args=ARGUMENTS)
5854809Sgblack@eecs.umich.eduExport('sticky_opts')
5864733Sgblack@eecs.umich.edu
5874733Sgblack@eecs.umich.edu# Non-sticky options only apply to the current build.
5884733Sgblack@eecs.umich.edunonsticky_opts = Options(args=ARGUMENTS)
5894733Sgblack@eecs.umich.eduExport('nonsticky_opts')
5905040Sgblack@eecs.umich.edu
5914732Sgblack@eecs.umich.edu# Walk the tree and execute all SConsopts scripts that wil add to the
5925040Sgblack@eecs.umich.edu# above options
5935040Sgblack@eecs.umich.edufor base_dir in base_dir_list:
5945040Sgblack@eecs.umich.edu    for root, dirs, files in os.walk(base_dir):
5955040Sgblack@eecs.umich.edu        if 'SConsopts' in files:
5964732Sgblack@eecs.umich.edu            print "Reading", joinpath(root, 'SConsopts')
5975040Sgblack@eecs.umich.edu            SConscript(joinpath(root, 'SConsopts'))
5985040Sgblack@eecs.umich.edu
5995040Sgblack@eecs.umich.eduall_isa_list.sort()
6005040Sgblack@eecs.umich.eduall_cpu_list.sort()
6015040Sgblack@eecs.umich.edudefault_cpus.sort()
6025040Sgblack@eecs.umich.edu
6035040Sgblack@eecs.umich.edusticky_opts.AddOptions(
6045040Sgblack@eecs.umich.edu    EnumOption('TARGET_ISA', 'Target ISA', 'alpha', all_isa_list),
6055040Sgblack@eecs.umich.edu    BoolOption('FULL_SYSTEM', 'Full-system support', False),
6065040Sgblack@eecs.umich.edu    # There's a bug in scons 0.96.1 that causes ListOptions with list
6075040Sgblack@eecs.umich.edu    # values (more than one value) not to be able to be restored from
6085040Sgblack@eecs.umich.edu    # a saved option file.  If this causes trouble then upgrade to
6095040Sgblack@eecs.umich.edu    # scons 0.96.90 or later.
6105040Sgblack@eecs.umich.edu    ListOption('CPU_MODELS', 'CPU models', default_cpus, all_cpu_list),
6115040Sgblack@eecs.umich.edu    BoolOption('NO_FAST_ALLOC', 'Disable fast object allocator', False),
6125040Sgblack@eecs.umich.edu    BoolOption('FAST_ALLOC_DEBUG', 'Enable fast object allocator debugging',
6135040Sgblack@eecs.umich.edu               False),
6145040Sgblack@eecs.umich.edu    BoolOption('FAST_ALLOC_STATS', 'Enable fast object allocator statistics',
6155040Sgblack@eecs.umich.edu               False),
6165040Sgblack@eecs.umich.edu    BoolOption('EFENCE', 'Link with Electric Fence malloc debugger',
6175040Sgblack@eecs.umich.edu               False),
6185040Sgblack@eecs.umich.edu    BoolOption('SS_COMPATIBLE_FP',
6195040Sgblack@eecs.umich.edu               'Make floating-point results compatible with SimpleScalar',
6205040Sgblack@eecs.umich.edu               False),
6215011Sgblack@eecs.umich.edu    BoolOption('USE_SSE2',
6224951Sgblack@eecs.umich.edu               'Compile for SSE2 (-msse2) to get IEEE FP on x86 hosts',
6235011Sgblack@eecs.umich.edu               False),
6245011Sgblack@eecs.umich.edu    BoolOption('USE_MYSQL', 'Use MySQL for stats output', have_mysql),
6255040Sgblack@eecs.umich.edu    BoolOption('USE_FENV', 'Use <fenv.h> IEEE mode control', have_fenv),
6265040Sgblack@eecs.umich.edu    BoolOption('USE_CHECKER', 'Use checker for detailed CPU models', False),
6275040Sgblack@eecs.umich.edu    )
6285040Sgblack@eecs.umich.edu
6295040Sgblack@eecs.umich.edunonsticky_opts.AddOptions(
6304732Sgblack@eecs.umich.edu    BoolOption('update_ref', 'Update test reference outputs', False)
6315040Sgblack@eecs.umich.edu    )
6325040Sgblack@eecs.umich.edu
6334823Sgblack@eecs.umich.edu# These options get exported to #defines in config/*.hh (see src/SConscript).
6344595Sgblack@eecs.umich.eduenv.ExportOptions = ['FULL_SYSTEM', 'ALPHA_TLASER', 'USE_FENV', \
6355007Sgblack@eecs.umich.edu                     'USE_MYSQL', 'NO_FAST_ALLOC', 'FAST_ALLOC_DEBUG', \
6365007Sgblack@eecs.umich.edu                     'FAST_ALLOC_STATS', 'SS_COMPATIBLE_FP', \
6375007Sgblack@eecs.umich.edu                     'USE_CHECKER', 'TARGET_ISA']
6385040Sgblack@eecs.umich.edu
6394714Sgblack@eecs.umich.edu# Define a handy 'no-op' action
6405040Sgblack@eecs.umich.edudef no_action(target, source, env):
6415040Sgblack@eecs.umich.edu    return 0
6425046Sgblack@eecs.umich.edu
6435058Sgblack@eecs.umich.eduenv.NoAction = Action(no_action, None)
6445058Sgblack@eecs.umich.edu
6455058Sgblack@eecs.umich.edu###################################################
6465058Sgblack@eecs.umich.edu#
6475058Sgblack@eecs.umich.edu# Define a SCons builder for configuration flag headers.
6485058Sgblack@eecs.umich.edu#
6495058Sgblack@eecs.umich.edu###################################################
6505058Sgblack@eecs.umich.edu
6515058Sgblack@eecs.umich.edu# This function generates a config header file that #defines the
6525058Sgblack@eecs.umich.edu# option symbol to the current option setting (0 or 1).  The source
6535058Sgblack@eecs.umich.edu# operands are the name of the option and a Value node containing the
6545058Sgblack@eecs.umich.edu# value of the option.
6555058Sgblack@eecs.umich.edudef build_config_file(target, source, env):
6565058Sgblack@eecs.umich.edu    (option, value) = [s.get_contents() for s in source]
6575058Sgblack@eecs.umich.edu    f = file(str(target[0]), 'w')
6585058Sgblack@eecs.umich.edu    print >> f, '#define', option, value
6595058Sgblack@eecs.umich.edu    f.close()
6605058Sgblack@eecs.umich.edu    return None
6615058Sgblack@eecs.umich.edu
6625058Sgblack@eecs.umich.edu# Generate the message to be printed when building the config file.
6635058Sgblack@eecs.umich.edudef build_config_file_string(target, source, env):
6645058Sgblack@eecs.umich.edu    (option, value) = [s.get_contents() for s in source]
6655058Sgblack@eecs.umich.edu    return "Defining %s as %s in %s." % (option, value, target[0])
6665059Sgblack@eecs.umich.edu
6675059Sgblack@eecs.umich.edu# Combine the two functions into a scons Action object.
6685059Sgblack@eecs.umich.educonfig_action = Action(build_config_file, build_config_file_string)
6695058Sgblack@eecs.umich.edu
6705058Sgblack@eecs.umich.edu# The emitter munges the source & target node lists to reflect what
6715058Sgblack@eecs.umich.edu# we're really doing.
6725058Sgblack@eecs.umich.edudef config_emitter(target, source, env):
6735046Sgblack@eecs.umich.edu    # extract option name from Builder arg
6745046Sgblack@eecs.umich.edu    option = str(target[0])
6755046Sgblack@eecs.umich.edu    # True target is config header file
6765046Sgblack@eecs.umich.edu    target = joinpath('config', option.lower() + '.hh')
6775046Sgblack@eecs.umich.edu    val = env[option]
6785046Sgblack@eecs.umich.edu    if isinstance(val, bool):
6795046Sgblack@eecs.umich.edu        # Force value to 0/1
6805046Sgblack@eecs.umich.edu        val = int(val)
6815061Sgblack@eecs.umich.edu    elif isinstance(val, str):
6825046Sgblack@eecs.umich.edu        val = '"' + val + '"'
6835046Sgblack@eecs.umich.edu
6845046Sgblack@eecs.umich.edu    # Sources are option name & value (packaged in SCons Value nodes)
6855046Sgblack@eecs.umich.edu    return ([target], [Value(option), Value(val)])
6865046Sgblack@eecs.umich.edu
6875046Sgblack@eecs.umich.educonfig_builder = Builder(emitter = config_emitter, action = config_action)
6885046Sgblack@eecs.umich.edu
6895046Sgblack@eecs.umich.eduenv.Append(BUILDERS = { 'ConfigFile' : config_builder })
6905046Sgblack@eecs.umich.edu
6915047Sgblack@eecs.umich.edu###################################################
6925047Sgblack@eecs.umich.edu#
6935047Sgblack@eecs.umich.edu# Define a SCons builder for copying files.  This is used by the
6945047Sgblack@eecs.umich.edu# Python zipfile code in src/python/SConscript, but is placed up here
6955047Sgblack@eecs.umich.edu# since it's potentially more generally applicable.
6965047Sgblack@eecs.umich.edu#
6975047Sgblack@eecs.umich.edu###################################################
6985047Sgblack@eecs.umich.edu
6995047Sgblack@eecs.umich.educopy_builder = Builder(action = Copy("$TARGET", "$SOURCE"))
7005047Sgblack@eecs.umich.edu
7015047Sgblack@eecs.umich.eduenv.Append(BUILDERS = { 'CopyFile' : copy_builder })
7025047Sgblack@eecs.umich.edu
7035047Sgblack@eecs.umich.edu###################################################
7045047Sgblack@eecs.umich.edu#
7054519Sgblack@eecs.umich.edu# Define a simple SCons builder to concatenate files.
706#
707# Used to append the Python zip archive to the executable.
708#
709###################################################
710
711concat_builder = Builder(action = Action(['cat $SOURCES > $TARGET',
712                                          'chmod +x $TARGET']))
713
714env.Append(BUILDERS = { 'Concat' : concat_builder })
715
716
717# libelf build is shared across all configs in the build root.
718env.SConscript('ext/libelf/SConscript',
719               build_dir = joinpath(build_root, 'libelf'),
720               exports = 'env')
721
722###################################################
723#
724# This function is used to set up a directory with switching headers
725#
726###################################################
727
728env['ALL_ISA_LIST'] = all_isa_list
729def make_switching_dir(dirname, switch_headers, env):
730    # Generate the header.  target[0] is the full path of the output
731    # header to generate.  'source' is a dummy variable, since we get the
732    # list of ISAs from env['ALL_ISA_LIST'].
733    def gen_switch_hdr(target, source, env):
734        fname = str(target[0])
735        basename = os.path.basename(fname)
736        f = open(fname, 'w')
737        f.write('#include "arch/isa_specific.hh"\n')
738        cond = '#if'
739        for isa in all_isa_list:
740            f.write('%s THE_ISA == %s_ISA\n#include "%s/%s/%s"\n'
741                    % (cond, isa.upper(), dirname, isa, basename))
742            cond = '#elif'
743        f.write('#else\n#error "THE_ISA not set"\n#endif\n')
744        f.close()
745        return 0
746
747    # String to print when generating header
748    def gen_switch_hdr_string(target, source, env):
749        return "Generating switch header " + str(target[0])
750
751    # Build SCons Action object. 'varlist' specifies env vars that this
752    # action depends on; when env['ALL_ISA_LIST'] changes these actions
753    # should get re-executed.
754    switch_hdr_action = Action(gen_switch_hdr, gen_switch_hdr_string,
755                               varlist=['ALL_ISA_LIST'])
756
757    # Instantiate actions for each header
758    for hdr in switch_headers:
759        env.Command(hdr, [], switch_hdr_action)
760Export('make_switching_dir')
761
762###################################################
763#
764# Define build environments for selected configurations.
765#
766###################################################
767
768# rename base env
769base_env = env
770
771for build_path in build_paths:
772    print "Building in", build_path
773
774    # Make a copy of the build-root environment to use for this config.
775    env = base_env.Copy()
776    env['BUILDDIR'] = build_path
777
778    # build_dir is the tail component of build path, and is used to
779    # determine the build parameters (e.g., 'ALPHA_SE')
780    (build_root, build_dir) = os.path.split(build_path)
781
782    # Set env options according to the build directory config.
783    sticky_opts.files = []
784    # Options for $BUILD_ROOT/$BUILD_DIR are stored in
785    # $BUILD_ROOT/options/$BUILD_DIR so you can nuke
786    # $BUILD_ROOT/$BUILD_DIR without losing your options settings.
787    current_opts_file = joinpath(build_root, 'options', build_dir)
788    if isfile(current_opts_file):
789        sticky_opts.files.append(current_opts_file)
790        print "Using saved options file %s" % current_opts_file
791    else:
792        # Build dir-specific options file doesn't exist.
793
794        # Make sure the directory is there so we can create it later
795        opt_dir = os.path.dirname(current_opts_file)
796        if not isdir(opt_dir):
797            os.mkdir(opt_dir)
798
799        # Get default build options from source tree.  Options are
800        # normally determined by name of $BUILD_DIR, but can be
801        # overriden by 'default=' arg on command line.
802        default_opts_file = joinpath('build_opts',
803                                     ARGUMENTS.get('default', build_dir))
804        if isfile(default_opts_file):
805            sticky_opts.files.append(default_opts_file)
806            print "Options file %s not found,\n  using defaults in %s" \
807                  % (current_opts_file, default_opts_file)
808        else:
809            print "Error: cannot find options file %s or %s" \
810                  % (current_opts_file, default_opts_file)
811            Exit(1)
812
813    # Apply current option settings to env
814    sticky_opts.Update(env)
815    nonsticky_opts.Update(env)
816
817    help_text += "\nSticky options for %s:\n" % build_dir \
818                 + sticky_opts.GenerateHelpText(env) \
819                 + "\nNon-sticky options for %s:\n" % build_dir \
820                 + nonsticky_opts.GenerateHelpText(env)
821
822    # Process option settings.
823
824    if not have_fenv and env['USE_FENV']:
825        print "Warning: <fenv.h> not available; " \
826              "forcing USE_FENV to False in", build_dir + "."
827        env['USE_FENV'] = False
828
829    if not env['USE_FENV']:
830        print "Warning: No IEEE FP rounding mode control in", build_dir + "."
831        print "         FP results may deviate slightly from other platforms."
832
833    if env['EFENCE']:
834        env.Append(LIBS=['efence'])
835
836    if env['USE_MYSQL']:
837        if not have_mysql:
838            print "Warning: MySQL not available; " \
839                  "forcing USE_MYSQL to False in", build_dir + "."
840            env['USE_MYSQL'] = False
841        else:
842            print "Compiling in", build_dir, "with MySQL support."
843            env.ParseConfig(mysql_config_libs)
844            env.ParseConfig(mysql_config_include)
845
846    # Save sticky option settings back to current options file
847    sticky_opts.Save(current_opts_file, env)
848
849    if env['USE_SSE2']:
850        env.Append(CCFLAGS='-msse2')
851
852    # The src/SConscript file sets up the build rules in 'env' according
853    # to the configured options.  It returns a list of environments,
854    # one for each variant build (debug, opt, etc.)
855    envList = SConscript('src/SConscript', build_dir = build_path,
856                         exports = 'env')
857
858    # Set up the regression tests for each build.
859    for e in envList:
860        SConscript('tests/SConscript',
861                   build_dir = joinpath(build_path, 'tests', e.Label),
862                   exports = { 'env' : e }, duplicate = False)
863
864Help(help_text)
865
866
867###################################################
868#
869# Let SCons do its thing.  At this point SCons will use the defined
870# build environments to build the requested targets.
871#
872###################################################
873
874