SConstruct revision 3356
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/A# Python library imports
67955SN/Aimport sys
68955SN/Aimport os
69955SN/A
702656Sstever@eecs.umich.edu# Check for recent-enough Python and SCons versions.  If your system's
712656Sstever@eecs.umich.edu# default installation of Python is not recent enough, you can use a
722656Sstever@eecs.umich.edu# non-default installation of the Python interpreter by either (1)
732656Sstever@eecs.umich.edu# rearranging your PATH so that scons finds the non-default 'python'
742656Sstever@eecs.umich.edu# first or (2) explicitly invoking an alternative interpreter on the
752656Sstever@eecs.umich.edu# scons script, e.g., "/usr/local/bin/python2.4 `which scons` [args]".
762656Sstever@eecs.umich.eduEnsurePythonVersion(2,4)
772653Sstever@eecs.umich.edu
782653Sstever@eecs.umich.edu# Ironically, SCons 0.96 dies if you give EnsureSconsVersion a
792653Sstever@eecs.umich.edu# 3-element version number.
802653Sstever@eecs.umich.edumin_scons_version = (0,96,91)
812653Sstever@eecs.umich.edutry:
822653Sstever@eecs.umich.edu    EnsureSConsVersion(*min_scons_version)
832653Sstever@eecs.umich.eduexcept:
842653Sstever@eecs.umich.edu    print "Error checking current SCons version."
852653Sstever@eecs.umich.edu    print "SCons", ".".join(map(str,min_scons_version)), "or greater required."
862653Sstever@eecs.umich.edu    Exit(2)
872653Sstever@eecs.umich.edu    
881852SN/A
89955SN/A# The absolute path to the current directory (where this file lives).
90955SN/AROOT = Dir('.').abspath
91955SN/A
922632Sstever@eecs.umich.edu# Paths to the M5 and external source trees.
932632Sstever@eecs.umich.eduSRCDIR = os.path.join(ROOT, 'src')
94955SN/A
951533SN/A# tell python where to find m5 python code
962632Sstever@eecs.umich.edusys.path.append(os.path.join(ROOT, 'src/python'))
971533SN/A
98955SN/A###################################################
99955SN/A#
1002632Sstever@eecs.umich.edu# Figure out which configurations to set up based on the path(s) of
1012632Sstever@eecs.umich.edu# the target(s).
102955SN/A#
103955SN/A###################################################
104955SN/A
105955SN/A# Find default configuration & binary.
1062632Sstever@eecs.umich.eduDefault(os.environ.get('M5_DEFAULT_BINARY', 'build/ALPHA_SE/m5.debug'))
107955SN/A
1082632Sstever@eecs.umich.edu# Ask SCons which directory it was invoked from.
109955SN/Alaunch_dir = GetLaunchDir()
110955SN/A
1112632Sstever@eecs.umich.edu# Make targets relative to invocation directory
1122632Sstever@eecs.umich.eduabs_targets = map(lambda x: os.path.normpath(os.path.join(launch_dir, str(x))),
1132632Sstever@eecs.umich.edu                  BUILD_TARGETS)
1142632Sstever@eecs.umich.edu
1152632Sstever@eecs.umich.edu# helper function: find last occurrence of element in list
1162632Sstever@eecs.umich.edudef rfind(l, elt, offs = -1):
1172632Sstever@eecs.umich.edu    for i in range(len(l)+offs, 0, -1):
1182632Sstever@eecs.umich.edu        if l[i] == elt:
1192632Sstever@eecs.umich.edu            return i
1202632Sstever@eecs.umich.edu    raise ValueError, "element not found"
1212632Sstever@eecs.umich.edu
1223053Sstever@eecs.umich.edu# helper function: compare dotted version numbers.
1233053Sstever@eecs.umich.edu# E.g., compare_version('1.3.25', '1.4.1')
1243053Sstever@eecs.umich.edu# returns -1, 0, 1 if v1 is <, ==, > v2
1253053Sstever@eecs.umich.edudef compare_versions(v1, v2):
1263053Sstever@eecs.umich.edu    # Convert dotted strings to lists
1273053Sstever@eecs.umich.edu    v1 = map(int, v1.split('.'))
1283053Sstever@eecs.umich.edu    v2 = map(int, v2.split('.'))
1293053Sstever@eecs.umich.edu    # Compare corresponding elements of lists
1303053Sstever@eecs.umich.edu    for n1,n2 in zip(v1, v2):
1313053Sstever@eecs.umich.edu        if n1 < n2: return -1
1323053Sstever@eecs.umich.edu        if n1 > n2: return  1
1333053Sstever@eecs.umich.edu    # all corresponding values are equal... see if one has extra values
1343053Sstever@eecs.umich.edu    if len(v1) < len(v2): return -1
1353053Sstever@eecs.umich.edu    if len(v1) > len(v2): return  1
1363053Sstever@eecs.umich.edu    return 0
1373053Sstever@eecs.umich.edu
1382632Sstever@eecs.umich.edu# Each target must have 'build' in the interior of the path; the
1392632Sstever@eecs.umich.edu# directory below this will determine the build parameters.  For
1402632Sstever@eecs.umich.edu# example, for target 'foo/bar/build/ALPHA_SE/arch/alpha/blah.do' we
1412632Sstever@eecs.umich.edu# recognize that ALPHA_SE specifies the configuration because it
1422632Sstever@eecs.umich.edu# follow 'build' in the bulid path.
1432632Sstever@eecs.umich.edu
1442634Sstever@eecs.umich.edu# Generate a list of the unique build roots and configs that the
1452634Sstever@eecs.umich.edu# collected targets reference.
1462632Sstever@eecs.umich.edubuild_paths = []
1472638Sstever@eecs.umich.edubuild_root = None
1482632Sstever@eecs.umich.edufor t in abs_targets:
1492632Sstever@eecs.umich.edu    path_dirs = t.split('/')
1502632Sstever@eecs.umich.edu    try:
1512632Sstever@eecs.umich.edu        build_top = rfind(path_dirs, 'build', -2)
1522632Sstever@eecs.umich.edu    except:
1532632Sstever@eecs.umich.edu        print "Error: no non-leaf 'build' dir found on target path", t
1541858SN/A        Exit(1)
1552638Sstever@eecs.umich.edu    this_build_root = os.path.join('/',*path_dirs[:build_top+1])
1562638Sstever@eecs.umich.edu    if not build_root:
1572638Sstever@eecs.umich.edu        build_root = this_build_root
1582638Sstever@eecs.umich.edu    else:
1592638Sstever@eecs.umich.edu        if this_build_root != build_root:
1602638Sstever@eecs.umich.edu            print "Error: build targets not under same build root\n"\
1612638Sstever@eecs.umich.edu                  "  %s\n  %s" % (build_root, this_build_root)
1622638Sstever@eecs.umich.edu            Exit(1)
1632634Sstever@eecs.umich.edu    build_path = os.path.join('/',*path_dirs[:build_top+2])
1642634Sstever@eecs.umich.edu    if build_path not in build_paths:
1652634Sstever@eecs.umich.edu        build_paths.append(build_path)
166955SN/A
167955SN/A###################################################
168955SN/A#
169955SN/A# Set up the default build environment.  This environment is copied
170955SN/A# and modified according to each selected configuration.
171955SN/A#
172955SN/A###################################################
173955SN/A
1741858SN/Aenv = Environment(ENV = os.environ,  # inherit user's environment vars
1751858SN/A                  ROOT = ROOT,
1762632Sstever@eecs.umich.edu                  SRCDIR = SRCDIR)
177955SN/A
1782776Sstever@eecs.umich.eduenv.SConsignFile(os.path.join(build_root,"sconsign"))
1791105SN/A
1802667Sstever@eecs.umich.edu# Default duplicate option is to use hard links, but this messes up
1812667Sstever@eecs.umich.edu# when you use emacs to edit a file in the target dir, as emacs moves
1822667Sstever@eecs.umich.edu# file to file~ then copies to file, breaking the link.  Symbolic
1832667Sstever@eecs.umich.edu# (soft) links work better.
1842667Sstever@eecs.umich.eduenv.SetOption('duplicate', 'soft-copy')
1852667Sstever@eecs.umich.edu
1861869SN/A# I waffle on this setting... it does avoid a few painful but
1871869SN/A# unnecessary builds, but it also seems to make trivial builds take
1881869SN/A# noticeably longer.
1891869SN/Aif False:
1901869SN/A    env.TargetSignatures('content')
1911065SN/A
1922632Sstever@eecs.umich.edu# M5_PLY is used by isa_parser.py to find the PLY package.
1932632Sstever@eecs.umich.eduenv.Append(ENV = { 'M5_PLY' : Dir('ext/ply') })
194955SN/A
1951858SN/A# Set up default C++ compiler flags
1961858SN/Aenv.Append(CCFLAGS='-pipe')
1971858SN/Aenv.Append(CCFLAGS='-fno-strict-aliasing')
1981858SN/Aenv.Append(CCFLAGS=Split('-Wall -Wno-sign-compare -Werror -Wundef'))
1991851SN/Aif sys.platform == 'cygwin':
2001851SN/A    # cygwin has some header file issues...
2011858SN/A    env.Append(CCFLAGS=Split("-Wno-uninitialized"))
2022632Sstever@eecs.umich.eduenv.Append(CPPPATH=[Dir('ext/dnet')])
203955SN/A
2043053Sstever@eecs.umich.edu# Check for SWIG
2053053Sstever@eecs.umich.eduif not env.has_key('SWIG'):
2063053Sstever@eecs.umich.edu    print 'Error: SWIG utility not found.'
2073053Sstever@eecs.umich.edu    print '       Please install (see http://www.swig.org) and retry.'
2083053Sstever@eecs.umich.edu    Exit(1)
2093053Sstever@eecs.umich.edu
2103053Sstever@eecs.umich.edu# Check for appropriate SWIG version
2113053Sstever@eecs.umich.eduswig_version = os.popen('swig -version').read().split()
2123053Sstever@eecs.umich.edu# First 3 words should be "SWIG Version x.y.z"
2133053Sstever@eecs.umich.eduif swig_version[0] != 'SWIG' or swig_version[1] != 'Version':
2143053Sstever@eecs.umich.edu    print 'Error determining SWIG version.'
2153053Sstever@eecs.umich.edu    Exit(1)
2163053Sstever@eecs.umich.edu
2173053Sstever@eecs.umich.edumin_swig_version = '1.3.28'
2183053Sstever@eecs.umich.eduif compare_versions(swig_version[2], min_swig_version) < 0:
2193053Sstever@eecs.umich.edu    print 'Error: SWIG version', min_swig_version, 'or newer required.'
2203053Sstever@eecs.umich.edu    print '       Installed version:', swig_version[2]
2213053Sstever@eecs.umich.edu    Exit(1)
2223053Sstever@eecs.umich.edu
2232667Sstever@eecs.umich.edu# Set up SWIG flags & scanner
2242667Sstever@eecs.umich.eduenv.Append(SWIGFLAGS=Split('-c++ -python -modern $_CPPINCFLAGS'))
2252667Sstever@eecs.umich.edu
2262667Sstever@eecs.umich.eduimport SCons.Scanner
2272667Sstever@eecs.umich.edu
2282667Sstever@eecs.umich.eduswig_inc_re = '^[ \t]*[%,#][ \t]*(?:include|import)[ \t]*(<|")([^>"]+)(>|")'
2292667Sstever@eecs.umich.edu
2302667Sstever@eecs.umich.eduswig_scanner = SCons.Scanner.ClassicCPP("SwigScan", ".i", "CPPPATH",
2312667Sstever@eecs.umich.edu                                        swig_inc_re)
2322667Sstever@eecs.umich.edu
2332667Sstever@eecs.umich.eduenv.Append(SCANNERS = swig_scanner)
2342667Sstever@eecs.umich.edu
2352638Sstever@eecs.umich.edu# Platform-specific configuration.  Note again that we assume that all
2362638Sstever@eecs.umich.edu# builds under a given build root run on the same host platform.
2372638Sstever@eecs.umich.educonf = Configure(env,
2382638Sstever@eecs.umich.edu                 conf_dir = os.path.join(build_root, '.scons_config'),
2392638Sstever@eecs.umich.edu                 log_file = os.path.join(build_root, 'scons_config.log'))
2401858SN/A
2413118Sstever@eecs.umich.edu# Find Python include and library directories for embedding the
2423118Sstever@eecs.umich.edu# interpreter.  For consistency, we will use the same Python
2433118Sstever@eecs.umich.edu# installation used to run scons (and thus this script).  If you want
2443118Sstever@eecs.umich.edu# to link in an alternate version, see above for instructions on how
2453118Sstever@eecs.umich.edu# to invoke scons with a different copy of the Python interpreter.
2463118Sstever@eecs.umich.edu
2473118Sstever@eecs.umich.edu# Get brief Python version name (e.g., "python2.4") for locating
2483118Sstever@eecs.umich.edu# include & library files
2493118Sstever@eecs.umich.edupy_version_name = 'python' + sys.version[:3]
2503118Sstever@eecs.umich.edu
2513118Sstever@eecs.umich.edu# include path, e.g. /usr/local/include/python2.4
2523118Sstever@eecs.umich.edupy_header_path = os.path.join(sys.exec_prefix, 'include', py_version_name)
2533118Sstever@eecs.umich.eduenv.Append(CPPPATH = py_header_path)
2543118Sstever@eecs.umich.edu# verify that it works
2553118Sstever@eecs.umich.eduif not conf.CheckHeader('Python.h', '<>'):
2563118Sstever@eecs.umich.edu    print "Error: can't find Python.h header in", py_header_path
2573118Sstever@eecs.umich.edu    Exit(1)
2583118Sstever@eecs.umich.edu
2593118Sstever@eecs.umich.edu# add library path too if it's not in the default place
2603118Sstever@eecs.umich.edupy_lib_path = None
2613118Sstever@eecs.umich.eduif sys.exec_prefix != '/usr':
2623118Sstever@eecs.umich.edu    py_lib_path = os.path.join(sys.exec_prefix, 'lib')
2633118Sstever@eecs.umich.eduelif sys.platform == 'cygwin':
2643118Sstever@eecs.umich.edu    # cygwin puts the .dll in /bin for some reason
2653118Sstever@eecs.umich.edu    py_lib_path = '/bin'
2663118Sstever@eecs.umich.eduif py_lib_path:
2673118Sstever@eecs.umich.edu    env.Append(LIBPATH = py_lib_path)
2683118Sstever@eecs.umich.edu    print 'Adding', py_lib_path, 'to LIBPATH for', py_version_name
2693118Sstever@eecs.umich.eduif not conf.CheckLib(py_version_name):
2703118Sstever@eecs.umich.edu    print "Error: can't find Python library", py_version_name
2713118Sstever@eecs.umich.edu    Exit(1)
2723118Sstever@eecs.umich.edu
2733053Sstever@eecs.umich.edu# Check for zlib.  If the check passes, libz will be automatically
2743053Sstever@eecs.umich.edu# added to the LIBS environment variable.
2753053Sstever@eecs.umich.eduif not conf.CheckLibWithHeader('z', 'zlib.h', 'C++'):
2763053Sstever@eecs.umich.edu    print 'Error: did not find needed zlib compression library '\
2773053Sstever@eecs.umich.edu          'and/or zlib.h header file.'
2783053Sstever@eecs.umich.edu    print '       Please install zlib and try again.'
2793053Sstever@eecs.umich.edu    Exit(1)
2803053Sstever@eecs.umich.edu
2811858SN/A# Check for <fenv.h> (C99 FP environment control)
2821858SN/Ahave_fenv = conf.CheckHeader('fenv.h', '<>')
2831858SN/Aif not have_fenv:
2841858SN/A    print "Warning: Header file <fenv.h> not found."
2851858SN/A    print "         This host has no IEEE FP rounding mode control."
2861858SN/A
2871859SN/A# Check for mysql.
2881858SN/Amysql_config = WhereIs('mysql_config')
2891858SN/Ahave_mysql = mysql_config != None
2901858SN/A
2911859SN/A# Check MySQL version.
2921859SN/Aif have_mysql:
2931862SN/A    mysql_version = os.popen(mysql_config + ' --version').read()
2943053Sstever@eecs.umich.edu    min_mysql_version = '4.1'
2953053Sstever@eecs.umich.edu    if compare_versions(mysql_version, min_mysql_version) < 0:
2963053Sstever@eecs.umich.edu        print 'Warning: MySQL', min_mysql_version, 'or newer required.'
2973053Sstever@eecs.umich.edu        print '         Version', mysql_version, 'detected.'
2981859SN/A        have_mysql = False
2991859SN/A
3001859SN/A# Set up mysql_config commands.
3011859SN/Aif have_mysql:
3021859SN/A    mysql_config_include = mysql_config + ' --include'
3031859SN/A    if os.system(mysql_config_include + ' > /dev/null') != 0:
3041859SN/A        # older mysql_config versions don't support --include, use
3051859SN/A        # --cflags instead
3061862SN/A        mysql_config_include = mysql_config + ' --cflags | sed s/\\\'//g'
3071859SN/A    # This seems to work in all versions
3081859SN/A    mysql_config_libs = mysql_config + ' --libs'
3091859SN/A
3101858SN/Aenv = conf.Finish()
3111858SN/A
3122139SN/A# Define the universe of supported ISAs
3132139SN/Aenv['ALL_ISA_LIST'] = ['alpha', 'sparc', 'mips']
3142139SN/A
3152155SN/A# Define the universe of supported CPU models
3162623SN/Aenv['ALL_CPU_LIST'] = ['AtomicSimpleCPU', 'TimingSimpleCPU',
3172817Sksewell@umich.edu                       'FullCPU', 'O3CPU',
3182792Sktlim@umich.edu                       'OzoneCPU']
3192155SN/A
3201869SN/A# Sticky options get saved in the options file so they persist from
3211869SN/A# one invocation to the next (unless overridden, in which case the new
3221869SN/A# value becomes sticky).
3231869SN/Asticky_opts = Options(args=ARGUMENTS)
3241869SN/Asticky_opts.AddOptions(
3252139SN/A    EnumOption('TARGET_ISA', 'Target ISA', 'alpha', env['ALL_ISA_LIST']),
3261869SN/A    BoolOption('FULL_SYSTEM', 'Full-system support', False),
3272508SN/A    # There's a bug in scons 0.96.1 that causes ListOptions with list
3282508SN/A    # values (more than one value) not to be able to be restored from
3292508SN/A    # a saved option file.  If this causes trouble then upgrade to
3302508SN/A    # scons 0.96.90 or later.
3312635Sstever@eecs.umich.edu    ListOption('CPU_MODELS', 'CPU models', 'AtomicSimpleCPU,TimingSimpleCPU',
3322635Sstever@eecs.umich.edu               env['ALL_CPU_LIST']),
3331869SN/A    BoolOption('ALPHA_TLASER',
3341869SN/A               'Model Alpha TurboLaser platform (vs. Tsunami)', False),
3351869SN/A    BoolOption('NO_FAST_ALLOC', 'Disable fast object allocator', False),
3361869SN/A    BoolOption('EFENCE', 'Link with Electric Fence malloc debugger',
3371869SN/A               False),
3381869SN/A    BoolOption('SS_COMPATIBLE_FP',
3391869SN/A               'Make floating-point results compatible with SimpleScalar',
3401869SN/A               False),
3411965SN/A    BoolOption('USE_SSE2',
3421965SN/A               'Compile for SSE2 (-msse2) to get IEEE FP on x86 hosts',
3431965SN/A               False),
3441869SN/A    BoolOption('USE_MYSQL', 'Use MySQL for stats output', have_mysql),
3451869SN/A    BoolOption('USE_FENV', 'Use <fenv.h> IEEE mode control', have_fenv),
3462733Sktlim@umich.edu    BoolOption('USE_CHECKER', 'Use checker for detailed CPU models', False),
3471869SN/A    ('CC', 'C compiler', os.environ.get('CC', env['CC'])),
3481884SN/A    ('CXX', 'C++ compiler', os.environ.get('CXX', env['CXX'])),
3491884SN/A    BoolOption('BATCH', 'Use batch pool for build and tests', False),
3503356Sbinkertn@umich.edu    ('BATCH_CMD', 'Batch pool submission command name', 'qdo'),
3513356Sbinkertn@umich.edu    ('PYTHONHOME',
3523356Sbinkertn@umich.edu     'Override the default PYTHONHOME for this system (use with caution)',
3533356Sbinkertn@umich.edu     '%s:%s' % (sys.prefix, sys.exec_prefix))
3541869SN/A    )
3551858SN/A
3561869SN/A# Non-sticky options only apply to the current build.
3571869SN/Anonsticky_opts = Options(args=ARGUMENTS)
3581869SN/Anonsticky_opts.AddOptions(
3591869SN/A    BoolOption('update_ref', 'Update test reference outputs', False)
3601869SN/A    )
3611858SN/A
3622761Sstever@eecs.umich.edu# These options get exported to #defines in config/*.hh (see src/SConscript).
3631869SN/Aenv.ExportOptions = ['FULL_SYSTEM', 'ALPHA_TLASER', 'USE_FENV', \
3642733Sktlim@umich.edu                     'USE_MYSQL', 'NO_FAST_ALLOC', 'SS_COMPATIBLE_FP', \
3653356Sbinkertn@umich.edu                     'USE_CHECKER', 'PYTHONHOME']
3661869SN/A
3671869SN/A# Define a handy 'no-op' action
3681869SN/Adef no_action(target, source, env):
3691869SN/A    return 0
3701869SN/A
3711869SN/Aenv.NoAction = Action(no_action, None)
3721858SN/A
373955SN/A###################################################
374955SN/A#
3751869SN/A# Define a SCons builder for configuration flag headers.
3761869SN/A#
3771869SN/A###################################################
3781869SN/A
3791869SN/A# This function generates a config header file that #defines the
3801869SN/A# option symbol to the current option setting (0 or 1).  The source
3811869SN/A# operands are the name of the option and a Value node containing the
3821869SN/A# value of the option.
3831869SN/Adef build_config_file(target, source, env):
3841869SN/A    (option, value) = [s.get_contents() for s in source]
3851869SN/A    f = file(str(target[0]), 'w')
3861869SN/A    print >> f, '#define', option, value
3871869SN/A    f.close()
3881869SN/A    return None
3891869SN/A
3901869SN/A# Generate the message to be printed when building the config file.
3911869SN/Adef build_config_file_string(target, source, env):
3921869SN/A    (option, value) = [s.get_contents() for s in source]
3931869SN/A    return "Defining %s as %s in %s." % (option, value, target[0])
3941869SN/A
3951869SN/A# Combine the two functions into a scons Action object.
3961869SN/Aconfig_action = Action(build_config_file, build_config_file_string)
3971869SN/A
3981869SN/A# The emitter munges the source & target node lists to reflect what
3991869SN/A# we're really doing.
4001869SN/Adef config_emitter(target, source, env):
4011869SN/A    # extract option name from Builder arg
4021869SN/A    option = str(target[0])
4031869SN/A    # True target is config header file
4041869SN/A    target = os.path.join('config', option.lower() + '.hh')
4053356Sbinkertn@umich.edu    val = env[option]
4063356Sbinkertn@umich.edu    if isinstance(val, bool):
4073356Sbinkertn@umich.edu        # Force value to 0/1
4083356Sbinkertn@umich.edu        val = int(val)
4093356Sbinkertn@umich.edu    elif isinstance(val, str):
4103356Sbinkertn@umich.edu        val = '"' + val + '"'
4113356Sbinkertn@umich.edu        
4121869SN/A    # Sources are option name & value (packaged in SCons Value nodes)
4131869SN/A    return ([target], [Value(option), Value(val)])
4141869SN/A
4151869SN/Aconfig_builder = Builder(emitter = config_emitter, action = config_action)
4161869SN/A
4171869SN/Aenv.Append(BUILDERS = { 'ConfigFile' : config_builder })
4181869SN/A
4192655Sstever@eecs.umich.edu###################################################
4202655Sstever@eecs.umich.edu#
4212655Sstever@eecs.umich.edu# Define a SCons builder for copying files.  This is used by the
4222655Sstever@eecs.umich.edu# Python zipfile code in src/python/SConscript, but is placed up here
4232655Sstever@eecs.umich.edu# since it's potentially more generally applicable.
4242655Sstever@eecs.umich.edu#
4252655Sstever@eecs.umich.edu###################################################
4262655Sstever@eecs.umich.edu
4272655Sstever@eecs.umich.educopy_builder = Builder(action = Copy("$TARGET", "$SOURCE"))
4282655Sstever@eecs.umich.edu
4292655Sstever@eecs.umich.eduenv.Append(BUILDERS = { 'CopyFile' : copy_builder })
4302655Sstever@eecs.umich.edu
4312655Sstever@eecs.umich.edu###################################################
4322655Sstever@eecs.umich.edu#
4332655Sstever@eecs.umich.edu# Define a simple SCons builder to concatenate files.
4342655Sstever@eecs.umich.edu#
4352655Sstever@eecs.umich.edu# Used to append the Python zip archive to the executable.
4362655Sstever@eecs.umich.edu#
4372655Sstever@eecs.umich.edu###################################################
4382655Sstever@eecs.umich.edu
4392655Sstever@eecs.umich.educoncat_builder = Builder(action = Action(['cat $SOURCES > $TARGET',
4402655Sstever@eecs.umich.edu                                          'chmod +x $TARGET']))
4412655Sstever@eecs.umich.edu
4422655Sstever@eecs.umich.eduenv.Append(BUILDERS = { 'Concat' : concat_builder })
4432655Sstever@eecs.umich.edu
4442655Sstever@eecs.umich.edu
4452634Sstever@eecs.umich.edu# base help text
4462634Sstever@eecs.umich.eduhelp_text = '''
4472634Sstever@eecs.umich.eduUsage: scons [scons options] [build options] [target(s)]
4482634Sstever@eecs.umich.edu
4492634Sstever@eecs.umich.edu'''
4502634Sstever@eecs.umich.edu
4512638Sstever@eecs.umich.edu# libelf build is shared across all configs in the build root.
4522638Sstever@eecs.umich.eduenv.SConscript('ext/libelf/SConscript',
4532638Sstever@eecs.umich.edu               build_dir = os.path.join(build_root, 'libelf'),
4542638Sstever@eecs.umich.edu               exports = 'env')
4552638Sstever@eecs.umich.edu
4561869SN/A###################################################
4571869SN/A#
458955SN/A# Define build environments for selected configurations.
459955SN/A#
460955SN/A###################################################
461955SN/A
4621858SN/A# rename base env
4631858SN/Abase_env = env
4641858SN/A
4652632Sstever@eecs.umich.edufor build_path in build_paths:
4662632Sstever@eecs.umich.edu    print "Building in", build_path
4672632Sstever@eecs.umich.edu    # build_dir is the tail component of build path, and is used to
4682632Sstever@eecs.umich.edu    # determine the build parameters (e.g., 'ALPHA_SE')
4692632Sstever@eecs.umich.edu    (build_root, build_dir) = os.path.split(build_path)
4702634Sstever@eecs.umich.edu    # Make a copy of the build-root environment to use for this config.
4712638Sstever@eecs.umich.edu    env = base_env.Copy()
4722023SN/A
4732632Sstever@eecs.umich.edu    # Set env options according to the build directory config.
4742632Sstever@eecs.umich.edu    sticky_opts.files = []
4752632Sstever@eecs.umich.edu    # Options for $BUILD_ROOT/$BUILD_DIR are stored in
4762632Sstever@eecs.umich.edu    # $BUILD_ROOT/options/$BUILD_DIR so you can nuke
4772632Sstever@eecs.umich.edu    # $BUILD_ROOT/$BUILD_DIR without losing your options settings.
4782632Sstever@eecs.umich.edu    current_opts_file = os.path.join(build_root, 'options', build_dir)
4792632Sstever@eecs.umich.edu    if os.path.isfile(current_opts_file):
4802632Sstever@eecs.umich.edu        sticky_opts.files.append(current_opts_file)
4812632Sstever@eecs.umich.edu        print "Using saved options file %s" % current_opts_file
4822632Sstever@eecs.umich.edu    else:
4832632Sstever@eecs.umich.edu        # Build dir-specific options file doesn't exist.
4842023SN/A
4852632Sstever@eecs.umich.edu        # Make sure the directory is there so we can create it later
4862632Sstever@eecs.umich.edu        opt_dir = os.path.dirname(current_opts_file)
4871889SN/A        if not os.path.isdir(opt_dir):
4881889SN/A            os.mkdir(opt_dir)
4892632Sstever@eecs.umich.edu
4902632Sstever@eecs.umich.edu        # Get default build options from source tree.  Options are
4912632Sstever@eecs.umich.edu        # normally determined by name of $BUILD_DIR, but can be
4922632Sstever@eecs.umich.edu        # overriden by 'default=' arg on command line.
4932632Sstever@eecs.umich.edu        default_opts_file = os.path.join('build_opts',
4942632Sstever@eecs.umich.edu                                         ARGUMENTS.get('default', build_dir))
4952632Sstever@eecs.umich.edu        if os.path.isfile(default_opts_file):
4962632Sstever@eecs.umich.edu            sticky_opts.files.append(default_opts_file)
4972632Sstever@eecs.umich.edu            print "Options file %s not found,\n  using defaults in %s" \
4982632Sstever@eecs.umich.edu                  % (current_opts_file, default_opts_file)
4992632Sstever@eecs.umich.edu        else:
5002632Sstever@eecs.umich.edu            print "Error: cannot find options file %s or %s" \
5012632Sstever@eecs.umich.edu                  % (current_opts_file, default_opts_file)
5022632Sstever@eecs.umich.edu            Exit(1)
5031888SN/A
5041888SN/A    # Apply current option settings to env
5051869SN/A    sticky_opts.Update(env)
5061869SN/A    nonsticky_opts.Update(env)
5071858SN/A
5082598SN/A    help_text += "Sticky options for %s:\n" % build_dir \
5092598SN/A                 + sticky_opts.GenerateHelpText(env) \
5102598SN/A                 + "\nNon-sticky options for %s:\n" % build_dir \
5112598SN/A                 + nonsticky_opts.GenerateHelpText(env)
5122598SN/A
5131858SN/A    # Process option settings.
5141858SN/A
5151858SN/A    if not have_fenv and env['USE_FENV']:
5161858SN/A        print "Warning: <fenv.h> not available; " \
5171858SN/A              "forcing USE_FENV to False in", build_dir + "."
5181858SN/A        env['USE_FENV'] = False
5191858SN/A
5201858SN/A    if not env['USE_FENV']:
5211858SN/A        print "Warning: No IEEE FP rounding mode control in", build_dir + "."
5221871SN/A        print "         FP results may deviate slightly from other platforms."
5231858SN/A
5241858SN/A    if env['EFENCE']:
5251858SN/A        env.Append(LIBS=['efence'])
5261858SN/A
5271858SN/A    if env['USE_MYSQL']:
5281858SN/A        if not have_mysql:
5291858SN/A            print "Warning: MySQL not available; " \
5301858SN/A                  "forcing USE_MYSQL to False in", build_dir + "."
5311858SN/A            env['USE_MYSQL'] = False
5321858SN/A        else:
5331858SN/A            print "Compiling in", build_dir, "with MySQL support."
5341859SN/A            env.ParseConfig(mysql_config_libs)
5351859SN/A            env.ParseConfig(mysql_config_include)
5361869SN/A
5371888SN/A    # Save sticky option settings back to current options file
5382632Sstever@eecs.umich.edu    sticky_opts.Save(current_opts_file, env)
5391869SN/A
5401884SN/A    # Do this after we save setting back, or else we'll tack on an
5411884SN/A    # extra 'qdo' every time we run scons.
5421884SN/A    if env['BATCH']:
5431884SN/A        env['CC']  = env['BATCH_CMD'] + ' ' + env['CC']
5441884SN/A        env['CXX'] = env['BATCH_CMD'] + ' ' + env['CXX']
5451884SN/A
5461965SN/A    if env['USE_SSE2']:
5471965SN/A        env.Append(CCFLAGS='-msse2')
5481965SN/A
5492761Sstever@eecs.umich.edu    # The src/SConscript file sets up the build rules in 'env' according
5501869SN/A    # to the configured options.  It returns a list of environments,
5511869SN/A    # one for each variant build (debug, opt, etc.)
5522632Sstever@eecs.umich.edu    envList = SConscript('src/SConscript', build_dir = build_path,
5532667Sstever@eecs.umich.edu                         exports = 'env')
5541869SN/A
5551869SN/A    # Set up the regression tests for each build.
5562929Sktlim@umich.edu    for e in envList:
5572929Sktlim@umich.edu        SConscript('tests/SConscript',
5583036Sstever@eecs.umich.edu                   build_dir = os.path.join(build_path, 'tests', e.Label),
5592929Sktlim@umich.edu                   exports = { 'env' : e }, duplicate = False)
560955SN/A
5612598SN/AHelp(help_text)
5622598SN/A
563955SN/A###################################################
564955SN/A#
565955SN/A# Let SCons do its thing.  At this point SCons will use the defined
5661530SN/A# build environments to build the requested targets.
567955SN/A#
568955SN/A###################################################
569955SN/A
570