SConstruct revision 3718
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
693716Sstever@eecs.umich.edufrom os.path import join as joinpath
70955SN/A
712656Sstever@eecs.umich.edu# Check for recent-enough Python and SCons versions.  If your system's
722656Sstever@eecs.umich.edu# default installation of Python is not recent enough, you can use a
732656Sstever@eecs.umich.edu# non-default installation of the Python interpreter by either (1)
742656Sstever@eecs.umich.edu# rearranging your PATH so that scons finds the non-default 'python'
752656Sstever@eecs.umich.edu# first or (2) explicitly invoking an alternative interpreter on the
762656Sstever@eecs.umich.edu# scons script, e.g., "/usr/local/bin/python2.4 `which scons` [args]".
772656Sstever@eecs.umich.eduEnsurePythonVersion(2,4)
782653Sstever@eecs.umich.edu
792653Sstever@eecs.umich.edu# Ironically, SCons 0.96 dies if you give EnsureSconsVersion a
802653Sstever@eecs.umich.edu# 3-element version number.
812653Sstever@eecs.umich.edumin_scons_version = (0,96,91)
822653Sstever@eecs.umich.edutry:
832653Sstever@eecs.umich.edu    EnsureSConsVersion(*min_scons_version)
842653Sstever@eecs.umich.eduexcept:
852653Sstever@eecs.umich.edu    print "Error checking current SCons version."
862653Sstever@eecs.umich.edu    print "SCons", ".".join(map(str,min_scons_version)), "or greater required."
872653Sstever@eecs.umich.edu    Exit(2)
882653Sstever@eecs.umich.edu    
891852SN/A
90955SN/A# The absolute path to the current directory (where this file lives).
91955SN/AROOT = Dir('.').abspath
92955SN/A
933717Sstever@eecs.umich.edu# Path to the M5 source tree.
943716Sstever@eecs.umich.eduSRCDIR = joinpath(ROOT, 'src')
95955SN/A
961533SN/A# tell python where to find m5 python code
973716Sstever@eecs.umich.edusys.path.append(joinpath(ROOT, 'src/python'))
981533SN/A
99955SN/A###################################################
100955SN/A#
1012632Sstever@eecs.umich.edu# Figure out which configurations to set up based on the path(s) of
1022632Sstever@eecs.umich.edu# the target(s).
103955SN/A#
104955SN/A###################################################
105955SN/A
106955SN/A# Find default configuration & binary.
1072632Sstever@eecs.umich.eduDefault(os.environ.get('M5_DEFAULT_BINARY', 'build/ALPHA_SE/m5.debug'))
108955SN/A
1092632Sstever@eecs.umich.edu# helper function: find last occurrence of element in list
1102632Sstever@eecs.umich.edudef rfind(l, elt, offs = -1):
1112632Sstever@eecs.umich.edu    for i in range(len(l)+offs, 0, -1):
1122632Sstever@eecs.umich.edu        if l[i] == elt:
1132632Sstever@eecs.umich.edu            return i
1142632Sstever@eecs.umich.edu    raise ValueError, "element not found"
1152632Sstever@eecs.umich.edu
1163053Sstever@eecs.umich.edu# helper function: compare dotted version numbers.
1173053Sstever@eecs.umich.edu# E.g., compare_version('1.3.25', '1.4.1')
1183053Sstever@eecs.umich.edu# returns -1, 0, 1 if v1 is <, ==, > v2
1193053Sstever@eecs.umich.edudef compare_versions(v1, v2):
1203053Sstever@eecs.umich.edu    # Convert dotted strings to lists
1213053Sstever@eecs.umich.edu    v1 = map(int, v1.split('.'))
1223053Sstever@eecs.umich.edu    v2 = map(int, v2.split('.'))
1233053Sstever@eecs.umich.edu    # Compare corresponding elements of lists
1243053Sstever@eecs.umich.edu    for n1,n2 in zip(v1, v2):
1253053Sstever@eecs.umich.edu        if n1 < n2: return -1
1263053Sstever@eecs.umich.edu        if n1 > n2: return  1
1273053Sstever@eecs.umich.edu    # all corresponding values are equal... see if one has extra values
1283053Sstever@eecs.umich.edu    if len(v1) < len(v2): return -1
1293053Sstever@eecs.umich.edu    if len(v1) > len(v2): return  1
1303053Sstever@eecs.umich.edu    return 0
1313053Sstever@eecs.umich.edu
1322632Sstever@eecs.umich.edu# Each target must have 'build' in the interior of the path; the
1332632Sstever@eecs.umich.edu# directory below this will determine the build parameters.  For
1342632Sstever@eecs.umich.edu# example, for target 'foo/bar/build/ALPHA_SE/arch/alpha/blah.do' we
1352632Sstever@eecs.umich.edu# recognize that ALPHA_SE specifies the configuration because it
1362632Sstever@eecs.umich.edu# follow 'build' in the bulid path.
1372632Sstever@eecs.umich.edu
1383718Sstever@eecs.umich.edu# Generate absolute paths to targets so we can see where the build dir is
1393718Sstever@eecs.umich.eduif COMMAND_LINE_TARGETS:
1403718Sstever@eecs.umich.edu    # Ask SCons which directory it was invoked from
1413718Sstever@eecs.umich.edu    launch_dir = GetLaunchDir()
1423718Sstever@eecs.umich.edu    # Make targets relative to invocation directory
1433718Sstever@eecs.umich.edu    abs_targets = map(lambda x: os.path.normpath(joinpath(launch_dir, str(x))),
1443718Sstever@eecs.umich.edu                      COMMAND_LINE_TARGETS)
1453718Sstever@eecs.umich.eduelse:
1463718Sstever@eecs.umich.edu    # Default targets are relative to root of tree
1473718Sstever@eecs.umich.edu    abs_targets = map(lambda x: os.path.normpath(joinpath(ROOT, str(x))),
1483718Sstever@eecs.umich.edu                      DEFAULT_TARGETS)
1493718Sstever@eecs.umich.edu
1503718Sstever@eecs.umich.edu
1512634Sstever@eecs.umich.edu# Generate a list of the unique build roots and configs that the
1522634Sstever@eecs.umich.edu# collected targets reference.
1532632Sstever@eecs.umich.edubuild_paths = []
1542638Sstever@eecs.umich.edubuild_root = None
1552632Sstever@eecs.umich.edufor t in abs_targets:
1562632Sstever@eecs.umich.edu    path_dirs = t.split('/')
1572632Sstever@eecs.umich.edu    try:
1582632Sstever@eecs.umich.edu        build_top = rfind(path_dirs, 'build', -2)
1592632Sstever@eecs.umich.edu    except:
1602632Sstever@eecs.umich.edu        print "Error: no non-leaf 'build' dir found on target path", t
1611858SN/A        Exit(1)
1623716Sstever@eecs.umich.edu    this_build_root = joinpath('/',*path_dirs[:build_top+1])
1632638Sstever@eecs.umich.edu    if not build_root:
1642638Sstever@eecs.umich.edu        build_root = this_build_root
1652638Sstever@eecs.umich.edu    else:
1662638Sstever@eecs.umich.edu        if this_build_root != build_root:
1672638Sstever@eecs.umich.edu            print "Error: build targets not under same build root\n"\
1682638Sstever@eecs.umich.edu                  "  %s\n  %s" % (build_root, this_build_root)
1692638Sstever@eecs.umich.edu            Exit(1)
1703716Sstever@eecs.umich.edu    build_path = joinpath('/',*path_dirs[:build_top+2])
1712634Sstever@eecs.umich.edu    if build_path not in build_paths:
1722634Sstever@eecs.umich.edu        build_paths.append(build_path)
173955SN/A
174955SN/A###################################################
175955SN/A#
176955SN/A# Set up the default build environment.  This environment is copied
177955SN/A# and modified according to each selected configuration.
178955SN/A#
179955SN/A###################################################
180955SN/A
1811858SN/Aenv = Environment(ENV = os.environ,  # inherit user's environment vars
1821858SN/A                  ROOT = ROOT,
1832632Sstever@eecs.umich.edu                  SRCDIR = SRCDIR)
184955SN/A
1853643Ssaidi@eecs.umich.edu#Parse CC/CXX early so that we use the correct compiler for 
1863643Ssaidi@eecs.umich.edu# to test for dependencies/versions/libraries/includes
1873643Ssaidi@eecs.umich.eduif ARGUMENTS.get('CC', None):
1883643Ssaidi@eecs.umich.edu    env['CC'] = ARGUMENTS.get('CC')
1893643Ssaidi@eecs.umich.edu
1903643Ssaidi@eecs.umich.eduif ARGUMENTS.get('CXX', None):
1913643Ssaidi@eecs.umich.edu    env['CXX'] = ARGUMENTS.get('CXX')
1923643Ssaidi@eecs.umich.edu
1933716Sstever@eecs.umich.eduenv.SConsignFile(joinpath(build_root,"sconsign"))
1941105SN/A
1952667Sstever@eecs.umich.edu# Default duplicate option is to use hard links, but this messes up
1962667Sstever@eecs.umich.edu# when you use emacs to edit a file in the target dir, as emacs moves
1972667Sstever@eecs.umich.edu# file to file~ then copies to file, breaking the link.  Symbolic
1982667Sstever@eecs.umich.edu# (soft) links work better.
1992667Sstever@eecs.umich.eduenv.SetOption('duplicate', 'soft-copy')
2002667Sstever@eecs.umich.edu
2011869SN/A# I waffle on this setting... it does avoid a few painful but
2021869SN/A# unnecessary builds, but it also seems to make trivial builds take
2031869SN/A# noticeably longer.
2041869SN/Aif False:
2051869SN/A    env.TargetSignatures('content')
2061065SN/A
2072632Sstever@eecs.umich.edu# M5_PLY is used by isa_parser.py to find the PLY package.
2082632Sstever@eecs.umich.eduenv.Append(ENV = { 'M5_PLY' : Dir('ext/ply') })
209955SN/A
2101858SN/A# Set up default C++ compiler flags
2111858SN/Aenv.Append(CCFLAGS='-pipe')
2121858SN/Aenv.Append(CCFLAGS='-fno-strict-aliasing')
2131858SN/Aenv.Append(CCFLAGS=Split('-Wall -Wno-sign-compare -Werror -Wundef'))
2141851SN/Aif sys.platform == 'cygwin':
2151851SN/A    # cygwin has some header file issues...
2161858SN/A    env.Append(CCFLAGS=Split("-Wno-uninitialized"))
2172632Sstever@eecs.umich.eduenv.Append(CPPPATH=[Dir('ext/dnet')])
218955SN/A
2193053Sstever@eecs.umich.edu# Check for SWIG
2203053Sstever@eecs.umich.eduif not env.has_key('SWIG'):
2213053Sstever@eecs.umich.edu    print 'Error: SWIG utility not found.'
2223053Sstever@eecs.umich.edu    print '       Please install (see http://www.swig.org) and retry.'
2233053Sstever@eecs.umich.edu    Exit(1)
2243053Sstever@eecs.umich.edu
2253053Sstever@eecs.umich.edu# Check for appropriate SWIG version
2263053Sstever@eecs.umich.eduswig_version = os.popen('swig -version').read().split()
2273053Sstever@eecs.umich.edu# First 3 words should be "SWIG Version x.y.z"
2283053Sstever@eecs.umich.eduif swig_version[0] != 'SWIG' or swig_version[1] != 'Version':
2293053Sstever@eecs.umich.edu    print 'Error determining SWIG version.'
2303053Sstever@eecs.umich.edu    Exit(1)
2313053Sstever@eecs.umich.edu
2323053Sstever@eecs.umich.edumin_swig_version = '1.3.28'
2333053Sstever@eecs.umich.eduif compare_versions(swig_version[2], min_swig_version) < 0:
2343053Sstever@eecs.umich.edu    print 'Error: SWIG version', min_swig_version, 'or newer required.'
2353053Sstever@eecs.umich.edu    print '       Installed version:', swig_version[2]
2363053Sstever@eecs.umich.edu    Exit(1)
2373053Sstever@eecs.umich.edu
2382667Sstever@eecs.umich.edu# Set up SWIG flags & scanner
2392667Sstever@eecs.umich.eduenv.Append(SWIGFLAGS=Split('-c++ -python -modern $_CPPINCFLAGS'))
2402667Sstever@eecs.umich.edu
2412667Sstever@eecs.umich.eduimport SCons.Scanner
2422667Sstever@eecs.umich.edu
2432667Sstever@eecs.umich.eduswig_inc_re = '^[ \t]*[%,#][ \t]*(?:include|import)[ \t]*(<|")([^>"]+)(>|")'
2442667Sstever@eecs.umich.edu
2452667Sstever@eecs.umich.eduswig_scanner = SCons.Scanner.ClassicCPP("SwigScan", ".i", "CPPPATH",
2462667Sstever@eecs.umich.edu                                        swig_inc_re)
2472667Sstever@eecs.umich.edu
2482667Sstever@eecs.umich.eduenv.Append(SCANNERS = swig_scanner)
2492667Sstever@eecs.umich.edu
2502638Sstever@eecs.umich.edu# Platform-specific configuration.  Note again that we assume that all
2512638Sstever@eecs.umich.edu# builds under a given build root run on the same host platform.
2522638Sstever@eecs.umich.educonf = Configure(env,
2533716Sstever@eecs.umich.edu                 conf_dir = joinpath(build_root, '.scons_config'),
2543716Sstever@eecs.umich.edu                 log_file = joinpath(build_root, 'scons_config.log'))
2551858SN/A
2563118Sstever@eecs.umich.edu# Find Python include and library directories for embedding the
2573118Sstever@eecs.umich.edu# interpreter.  For consistency, we will use the same Python
2583118Sstever@eecs.umich.edu# installation used to run scons (and thus this script).  If you want
2593118Sstever@eecs.umich.edu# to link in an alternate version, see above for instructions on how
2603118Sstever@eecs.umich.edu# to invoke scons with a different copy of the Python interpreter.
2613118Sstever@eecs.umich.edu
2623118Sstever@eecs.umich.edu# Get brief Python version name (e.g., "python2.4") for locating
2633118Sstever@eecs.umich.edu# include & library files
2643118Sstever@eecs.umich.edupy_version_name = 'python' + sys.version[:3]
2653118Sstever@eecs.umich.edu
2663118Sstever@eecs.umich.edu# include path, e.g. /usr/local/include/python2.4
2673716Sstever@eecs.umich.edupy_header_path = joinpath(sys.exec_prefix, 'include', py_version_name)
2683118Sstever@eecs.umich.eduenv.Append(CPPPATH = py_header_path)
2693118Sstever@eecs.umich.edu# verify that it works
2703118Sstever@eecs.umich.eduif not conf.CheckHeader('Python.h', '<>'):
2713118Sstever@eecs.umich.edu    print "Error: can't find Python.h header in", py_header_path
2723118Sstever@eecs.umich.edu    Exit(1)
2733118Sstever@eecs.umich.edu
2743118Sstever@eecs.umich.edu# add library path too if it's not in the default place
2753118Sstever@eecs.umich.edupy_lib_path = None
2763118Sstever@eecs.umich.eduif sys.exec_prefix != '/usr':
2773716Sstever@eecs.umich.edu    py_lib_path = joinpath(sys.exec_prefix, 'lib')
2783118Sstever@eecs.umich.eduelif sys.platform == 'cygwin':
2793118Sstever@eecs.umich.edu    # cygwin puts the .dll in /bin for some reason
2803118Sstever@eecs.umich.edu    py_lib_path = '/bin'
2813118Sstever@eecs.umich.eduif py_lib_path:
2823118Sstever@eecs.umich.edu    env.Append(LIBPATH = py_lib_path)
2833118Sstever@eecs.umich.edu    print 'Adding', py_lib_path, 'to LIBPATH for', py_version_name
2843118Sstever@eecs.umich.eduif not conf.CheckLib(py_version_name):
2853118Sstever@eecs.umich.edu    print "Error: can't find Python library", py_version_name
2863118Sstever@eecs.umich.edu    Exit(1)
2873118Sstever@eecs.umich.edu
2883483Ssaidi@eecs.umich.edu# On Solaris you need to use libsocket for socket ops
2893494Ssaidi@eecs.umich.eduif not conf.CheckLibWithHeader(None, 'sys/socket.h', 'C++', 'accept(0,0,0);'):
2903494Ssaidi@eecs.umich.edu   if not conf.CheckLibWithHeader('socket', 'sys/socket.h', 'C++', 'accept(0,0,0);'):
2913483Ssaidi@eecs.umich.edu       print "Can't find library with socket calls (e.g. accept())"
2923483Ssaidi@eecs.umich.edu       Exit(1)
2933483Ssaidi@eecs.umich.edu
2943053Sstever@eecs.umich.edu# Check for zlib.  If the check passes, libz will be automatically
2953053Sstever@eecs.umich.edu# added to the LIBS environment variable.
2963053Sstever@eecs.umich.eduif not conf.CheckLibWithHeader('z', 'zlib.h', 'C++'):
2973053Sstever@eecs.umich.edu    print 'Error: did not find needed zlib compression library '\
2983053Sstever@eecs.umich.edu          'and/or zlib.h header file.'
2993053Sstever@eecs.umich.edu    print '       Please install zlib and try again.'
3003053Sstever@eecs.umich.edu    Exit(1)
3013053Sstever@eecs.umich.edu
3021858SN/A# Check for <fenv.h> (C99 FP environment control)
3031858SN/Ahave_fenv = conf.CheckHeader('fenv.h', '<>')
3041858SN/Aif not have_fenv:
3051858SN/A    print "Warning: Header file <fenv.h> not found."
3061858SN/A    print "         This host has no IEEE FP rounding mode control."
3071858SN/A
3081859SN/A# Check for mysql.
3091858SN/Amysql_config = WhereIs('mysql_config')
3101858SN/Ahave_mysql = mysql_config != None
3111858SN/A
3121859SN/A# Check MySQL version.
3131859SN/Aif have_mysql:
3141862SN/A    mysql_version = os.popen(mysql_config + ' --version').read()
3153053Sstever@eecs.umich.edu    min_mysql_version = '4.1'
3163053Sstever@eecs.umich.edu    if compare_versions(mysql_version, min_mysql_version) < 0:
3173053Sstever@eecs.umich.edu        print 'Warning: MySQL', min_mysql_version, 'or newer required.'
3183053Sstever@eecs.umich.edu        print '         Version', mysql_version, 'detected.'
3191859SN/A        have_mysql = False
3201859SN/A
3211859SN/A# Set up mysql_config commands.
3221859SN/Aif have_mysql:
3231859SN/A    mysql_config_include = mysql_config + ' --include'
3241859SN/A    if os.system(mysql_config_include + ' > /dev/null') != 0:
3251859SN/A        # older mysql_config versions don't support --include, use
3261859SN/A        # --cflags instead
3271862SN/A        mysql_config_include = mysql_config + ' --cflags | sed s/\\\'//g'
3281859SN/A    # This seems to work in all versions
3291859SN/A    mysql_config_libs = mysql_config + ' --libs'
3301859SN/A
3311858SN/Aenv = conf.Finish()
3321858SN/A
3332139SN/A# Define the universe of supported ISAs
3342139SN/Aenv['ALL_ISA_LIST'] = ['alpha', 'sparc', 'mips']
3352139SN/A
3362155SN/A# Define the universe of supported CPU models
3372623SN/Aenv['ALL_CPU_LIST'] = ['AtomicSimpleCPU', 'TimingSimpleCPU',
3383583Sbinkertn@umich.edu                       'O3CPU', 'OzoneCPU']
3393583Sbinkertn@umich.edu
3403717Sstever@eecs.umich.eduif os.path.isdir(joinpath(SRCDIR, 'encumbered/cpu/full')):
3413583Sbinkertn@umich.edu    env['ALL_CPU_LIST'] += ['FullCPU']
3422155SN/A
3431869SN/A# Sticky options get saved in the options file so they persist from
3441869SN/A# one invocation to the next (unless overridden, in which case the new
3451869SN/A# value becomes sticky).
3461869SN/Asticky_opts = Options(args=ARGUMENTS)
3471869SN/Asticky_opts.AddOptions(
3482139SN/A    EnumOption('TARGET_ISA', 'Target ISA', 'alpha', env['ALL_ISA_LIST']),
3491869SN/A    BoolOption('FULL_SYSTEM', 'Full-system support', False),
3502508SN/A    # There's a bug in scons 0.96.1 that causes ListOptions with list
3512508SN/A    # values (more than one value) not to be able to be restored from
3522508SN/A    # a saved option file.  If this causes trouble then upgrade to
3532508SN/A    # scons 0.96.90 or later.
3543685Sktlim@umich.edu    ListOption('CPU_MODELS', 'CPU models', 'AtomicSimpleCPU,TimingSimpleCPU,O3CPU',
3552635Sstever@eecs.umich.edu               env['ALL_CPU_LIST']),
3561869SN/A    BoolOption('ALPHA_TLASER',
3571869SN/A               'Model Alpha TurboLaser platform (vs. Tsunami)', False),
3581869SN/A    BoolOption('NO_FAST_ALLOC', 'Disable fast object allocator', False),
3591869SN/A    BoolOption('EFENCE', 'Link with Electric Fence malloc debugger',
3601869SN/A               False),
3611869SN/A    BoolOption('SS_COMPATIBLE_FP',
3621869SN/A               'Make floating-point results compatible with SimpleScalar',
3631869SN/A               False),
3641965SN/A    BoolOption('USE_SSE2',
3651965SN/A               'Compile for SSE2 (-msse2) to get IEEE FP on x86 hosts',
3661965SN/A               False),
3671869SN/A    BoolOption('USE_MYSQL', 'Use MySQL for stats output', have_mysql),
3681869SN/A    BoolOption('USE_FENV', 'Use <fenv.h> IEEE mode control', have_fenv),
3692733Sktlim@umich.edu    BoolOption('USE_CHECKER', 'Use checker for detailed CPU models', False),
3701869SN/A    ('CC', 'C compiler', os.environ.get('CC', env['CC'])),
3711884SN/A    ('CXX', 'C++ compiler', os.environ.get('CXX', env['CXX'])),
3721884SN/A    BoolOption('BATCH', 'Use batch pool for build and tests', False),
3733356Sbinkertn@umich.edu    ('BATCH_CMD', 'Batch pool submission command name', 'qdo'),
3743356Sbinkertn@umich.edu    ('PYTHONHOME',
3753356Sbinkertn@umich.edu     'Override the default PYTHONHOME for this system (use with caution)',
3763356Sbinkertn@umich.edu     '%s:%s' % (sys.prefix, sys.exec_prefix))
3771869SN/A    )
3781858SN/A
3791869SN/A# Non-sticky options only apply to the current build.
3801869SN/Anonsticky_opts = Options(args=ARGUMENTS)
3811869SN/Anonsticky_opts.AddOptions(
3821869SN/A    BoolOption('update_ref', 'Update test reference outputs', False)
3831869SN/A    )
3841858SN/A
3852761Sstever@eecs.umich.edu# These options get exported to #defines in config/*.hh (see src/SConscript).
3861869SN/Aenv.ExportOptions = ['FULL_SYSTEM', 'ALPHA_TLASER', 'USE_FENV', \
3872733Sktlim@umich.edu                     'USE_MYSQL', 'NO_FAST_ALLOC', 'SS_COMPATIBLE_FP', \
3883584Ssaidi@eecs.umich.edu                     'USE_CHECKER', 'PYTHONHOME', 'TARGET_ISA']
3891869SN/A
3901869SN/A# Define a handy 'no-op' action
3911869SN/Adef no_action(target, source, env):
3921869SN/A    return 0
3931869SN/A
3941869SN/Aenv.NoAction = Action(no_action, None)
3951858SN/A
396955SN/A###################################################
397955SN/A#
3981869SN/A# Define a SCons builder for configuration flag headers.
3991869SN/A#
4001869SN/A###################################################
4011869SN/A
4021869SN/A# This function generates a config header file that #defines the
4031869SN/A# option symbol to the current option setting (0 or 1).  The source
4041869SN/A# operands are the name of the option and a Value node containing the
4051869SN/A# value of the option.
4061869SN/Adef build_config_file(target, source, env):
4071869SN/A    (option, value) = [s.get_contents() for s in source]
4081869SN/A    f = file(str(target[0]), 'w')
4091869SN/A    print >> f, '#define', option, value
4101869SN/A    f.close()
4111869SN/A    return None
4121869SN/A
4131869SN/A# Generate the message to be printed when building the config file.
4141869SN/Adef build_config_file_string(target, source, env):
4151869SN/A    (option, value) = [s.get_contents() for s in source]
4161869SN/A    return "Defining %s as %s in %s." % (option, value, target[0])
4171869SN/A
4181869SN/A# Combine the two functions into a scons Action object.
4191869SN/Aconfig_action = Action(build_config_file, build_config_file_string)
4201869SN/A
4211869SN/A# The emitter munges the source & target node lists to reflect what
4221869SN/A# we're really doing.
4231869SN/Adef config_emitter(target, source, env):
4241869SN/A    # extract option name from Builder arg
4251869SN/A    option = str(target[0])
4261869SN/A    # True target is config header file
4273716Sstever@eecs.umich.edu    target = joinpath('config', option.lower() + '.hh')
4283356Sbinkertn@umich.edu    val = env[option]
4293356Sbinkertn@umich.edu    if isinstance(val, bool):
4303356Sbinkertn@umich.edu        # Force value to 0/1
4313356Sbinkertn@umich.edu        val = int(val)
4323356Sbinkertn@umich.edu    elif isinstance(val, str):
4333356Sbinkertn@umich.edu        val = '"' + val + '"'
4343356Sbinkertn@umich.edu        
4351869SN/A    # Sources are option name & value (packaged in SCons Value nodes)
4361869SN/A    return ([target], [Value(option), Value(val)])
4371869SN/A
4381869SN/Aconfig_builder = Builder(emitter = config_emitter, action = config_action)
4391869SN/A
4401869SN/Aenv.Append(BUILDERS = { 'ConfigFile' : config_builder })
4411869SN/A
4422655Sstever@eecs.umich.edu###################################################
4432655Sstever@eecs.umich.edu#
4442655Sstever@eecs.umich.edu# Define a SCons builder for copying files.  This is used by the
4452655Sstever@eecs.umich.edu# Python zipfile code in src/python/SConscript, but is placed up here
4462655Sstever@eecs.umich.edu# since it's potentially more generally applicable.
4472655Sstever@eecs.umich.edu#
4482655Sstever@eecs.umich.edu###################################################
4492655Sstever@eecs.umich.edu
4502655Sstever@eecs.umich.educopy_builder = Builder(action = Copy("$TARGET", "$SOURCE"))
4512655Sstever@eecs.umich.edu
4522655Sstever@eecs.umich.eduenv.Append(BUILDERS = { 'CopyFile' : copy_builder })
4532655Sstever@eecs.umich.edu
4542655Sstever@eecs.umich.edu###################################################
4552655Sstever@eecs.umich.edu#
4562655Sstever@eecs.umich.edu# Define a simple SCons builder to concatenate files.
4572655Sstever@eecs.umich.edu#
4582655Sstever@eecs.umich.edu# Used to append the Python zip archive to the executable.
4592655Sstever@eecs.umich.edu#
4602655Sstever@eecs.umich.edu###################################################
4612655Sstever@eecs.umich.edu
4622655Sstever@eecs.umich.educoncat_builder = Builder(action = Action(['cat $SOURCES > $TARGET',
4632655Sstever@eecs.umich.edu                                          'chmod +x $TARGET']))
4642655Sstever@eecs.umich.edu
4652655Sstever@eecs.umich.eduenv.Append(BUILDERS = { 'Concat' : concat_builder })
4662655Sstever@eecs.umich.edu
4672655Sstever@eecs.umich.edu
4682634Sstever@eecs.umich.edu# base help text
4692634Sstever@eecs.umich.eduhelp_text = '''
4702634Sstever@eecs.umich.eduUsage: scons [scons options] [build options] [target(s)]
4712634Sstever@eecs.umich.edu
4722634Sstever@eecs.umich.edu'''
4732634Sstever@eecs.umich.edu
4742638Sstever@eecs.umich.edu# libelf build is shared across all configs in the build root.
4752638Sstever@eecs.umich.eduenv.SConscript('ext/libelf/SConscript',
4763716Sstever@eecs.umich.edu               build_dir = joinpath(build_root, 'libelf'),
4772638Sstever@eecs.umich.edu               exports = 'env')
4782638Sstever@eecs.umich.edu
4791869SN/A###################################################
4801869SN/A#
4813546Sgblack@eecs.umich.edu# This function is used to set up a directory with switching headers
4823546Sgblack@eecs.umich.edu#
4833546Sgblack@eecs.umich.edu###################################################
4843546Sgblack@eecs.umich.edu
4853546Sgblack@eecs.umich.edudef make_switching_dir(dirname, switch_headers, env):
4863546Sgblack@eecs.umich.edu    # Generate the header.  target[0] is the full path of the output
4873546Sgblack@eecs.umich.edu    # header to generate.  'source' is a dummy variable, since we get the
4883546Sgblack@eecs.umich.edu    # list of ISAs from env['ALL_ISA_LIST'].
4893546Sgblack@eecs.umich.edu    def gen_switch_hdr(target, source, env):
4903546Sgblack@eecs.umich.edu	fname = str(target[0])
4913546Sgblack@eecs.umich.edu	basename = os.path.basename(fname)
4923546Sgblack@eecs.umich.edu	f = open(fname, 'w')
4933546Sgblack@eecs.umich.edu	f.write('#include "arch/isa_specific.hh"\n')
4943546Sgblack@eecs.umich.edu	cond = '#if'
4953546Sgblack@eecs.umich.edu	for isa in env['ALL_ISA_LIST']:
4963546Sgblack@eecs.umich.edu	    f.write('%s THE_ISA == %s_ISA\n#include "%s/%s/%s"\n'
4973546Sgblack@eecs.umich.edu		    % (cond, isa.upper(), dirname, isa, basename))
4983546Sgblack@eecs.umich.edu	    cond = '#elif'
4993546Sgblack@eecs.umich.edu	f.write('#else\n#error "THE_ISA not set"\n#endif\n')
5003546Sgblack@eecs.umich.edu	f.close()
5013546Sgblack@eecs.umich.edu	return 0
5023546Sgblack@eecs.umich.edu
5033546Sgblack@eecs.umich.edu    # String to print when generating header
5043546Sgblack@eecs.umich.edu    def gen_switch_hdr_string(target, source, env):
5053546Sgblack@eecs.umich.edu	return "Generating switch header " + str(target[0])
5063546Sgblack@eecs.umich.edu
5073546Sgblack@eecs.umich.edu    # Build SCons Action object. 'varlist' specifies env vars that this
5083546Sgblack@eecs.umich.edu    # action depends on; when env['ALL_ISA_LIST'] changes these actions
5093546Sgblack@eecs.umich.edu    # should get re-executed.
5103546Sgblack@eecs.umich.edu    switch_hdr_action = Action(gen_switch_hdr, gen_switch_hdr_string,
5113546Sgblack@eecs.umich.edu                               varlist=['ALL_ISA_LIST'])
5123546Sgblack@eecs.umich.edu
5133546Sgblack@eecs.umich.edu    # Instantiate actions for each header
5143546Sgblack@eecs.umich.edu    for hdr in switch_headers:
5153546Sgblack@eecs.umich.edu        env.Command(hdr, [], switch_hdr_action)
5163546Sgblack@eecs.umich.edu
5173546Sgblack@eecs.umich.eduenv.make_switching_dir = make_switching_dir
5183546Sgblack@eecs.umich.edu
5193546Sgblack@eecs.umich.edu###################################################
5203546Sgblack@eecs.umich.edu#
521955SN/A# Define build environments for selected configurations.
522955SN/A#
523955SN/A###################################################
524955SN/A
5251858SN/A# rename base env
5261858SN/Abase_env = env
5271858SN/A
5282632Sstever@eecs.umich.edufor build_path in build_paths:
5292632Sstever@eecs.umich.edu    print "Building in", build_path
5302632Sstever@eecs.umich.edu    # build_dir is the tail component of build path, and is used to
5312632Sstever@eecs.umich.edu    # determine the build parameters (e.g., 'ALPHA_SE')
5322632Sstever@eecs.umich.edu    (build_root, build_dir) = os.path.split(build_path)
5332634Sstever@eecs.umich.edu    # Make a copy of the build-root environment to use for this config.
5342638Sstever@eecs.umich.edu    env = base_env.Copy()
5352023SN/A
5362632Sstever@eecs.umich.edu    # Set env options according to the build directory config.
5372632Sstever@eecs.umich.edu    sticky_opts.files = []
5382632Sstever@eecs.umich.edu    # Options for $BUILD_ROOT/$BUILD_DIR are stored in
5392632Sstever@eecs.umich.edu    # $BUILD_ROOT/options/$BUILD_DIR so you can nuke
5402632Sstever@eecs.umich.edu    # $BUILD_ROOT/$BUILD_DIR without losing your options settings.
5413716Sstever@eecs.umich.edu    current_opts_file = joinpath(build_root, 'options', build_dir)
5422632Sstever@eecs.umich.edu    if os.path.isfile(current_opts_file):
5432632Sstever@eecs.umich.edu        sticky_opts.files.append(current_opts_file)
5442632Sstever@eecs.umich.edu        print "Using saved options file %s" % current_opts_file
5452632Sstever@eecs.umich.edu    else:
5462632Sstever@eecs.umich.edu        # Build dir-specific options file doesn't exist.
5472023SN/A
5482632Sstever@eecs.umich.edu        # Make sure the directory is there so we can create it later
5492632Sstever@eecs.umich.edu        opt_dir = os.path.dirname(current_opts_file)
5501889SN/A        if not os.path.isdir(opt_dir):
5511889SN/A            os.mkdir(opt_dir)
5522632Sstever@eecs.umich.edu
5532632Sstever@eecs.umich.edu        # Get default build options from source tree.  Options are
5542632Sstever@eecs.umich.edu        # normally determined by name of $BUILD_DIR, but can be
5552632Sstever@eecs.umich.edu        # overriden by 'default=' arg on command line.
5563716Sstever@eecs.umich.edu        default_opts_file = joinpath('build_opts',
5573716Sstever@eecs.umich.edu                                     ARGUMENTS.get('default', build_dir))
5582632Sstever@eecs.umich.edu        if os.path.isfile(default_opts_file):
5592632Sstever@eecs.umich.edu            sticky_opts.files.append(default_opts_file)
5602632Sstever@eecs.umich.edu            print "Options file %s not found,\n  using defaults in %s" \
5612632Sstever@eecs.umich.edu                  % (current_opts_file, default_opts_file)
5622632Sstever@eecs.umich.edu        else:
5632632Sstever@eecs.umich.edu            print "Error: cannot find options file %s or %s" \
5642632Sstever@eecs.umich.edu                  % (current_opts_file, default_opts_file)
5652632Sstever@eecs.umich.edu            Exit(1)
5661888SN/A
5671888SN/A    # Apply current option settings to env
5681869SN/A    sticky_opts.Update(env)
5691869SN/A    nonsticky_opts.Update(env)
5701858SN/A
5712598SN/A    help_text += "Sticky options for %s:\n" % build_dir \
5722598SN/A                 + sticky_opts.GenerateHelpText(env) \
5732598SN/A                 + "\nNon-sticky options for %s:\n" % build_dir \
5742598SN/A                 + nonsticky_opts.GenerateHelpText(env)
5752598SN/A
5761858SN/A    # Process option settings.
5771858SN/A
5781858SN/A    if not have_fenv and env['USE_FENV']:
5791858SN/A        print "Warning: <fenv.h> not available; " \
5801858SN/A              "forcing USE_FENV to False in", build_dir + "."
5811858SN/A        env['USE_FENV'] = False
5821858SN/A
5831858SN/A    if not env['USE_FENV']:
5841858SN/A        print "Warning: No IEEE FP rounding mode control in", build_dir + "."
5851871SN/A        print "         FP results may deviate slightly from other platforms."
5861858SN/A
5871858SN/A    if env['EFENCE']:
5881858SN/A        env.Append(LIBS=['efence'])
5891858SN/A
5901858SN/A    if env['USE_MYSQL']:
5911858SN/A        if not have_mysql:
5921858SN/A            print "Warning: MySQL not available; " \
5931858SN/A                  "forcing USE_MYSQL to False in", build_dir + "."
5941858SN/A            env['USE_MYSQL'] = False
5951858SN/A        else:
5961858SN/A            print "Compiling in", build_dir, "with MySQL support."
5971859SN/A            env.ParseConfig(mysql_config_libs)
5981859SN/A            env.ParseConfig(mysql_config_include)
5991869SN/A
6001888SN/A    # Save sticky option settings back to current options file
6012632Sstever@eecs.umich.edu    sticky_opts.Save(current_opts_file, env)
6021869SN/A
6031884SN/A    # Do this after we save setting back, or else we'll tack on an
6041884SN/A    # extra 'qdo' every time we run scons.
6051884SN/A    if env['BATCH']:
6061884SN/A        env['CC']  = env['BATCH_CMD'] + ' ' + env['CC']
6071884SN/A        env['CXX'] = env['BATCH_CMD'] + ' ' + env['CXX']
6081884SN/A
6091965SN/A    if env['USE_SSE2']:
6101965SN/A        env.Append(CCFLAGS='-msse2')
6111965SN/A
6122761Sstever@eecs.umich.edu    # The src/SConscript file sets up the build rules in 'env' according
6131869SN/A    # to the configured options.  It returns a list of environments,
6141869SN/A    # one for each variant build (debug, opt, etc.)
6152632Sstever@eecs.umich.edu    envList = SConscript('src/SConscript', build_dir = build_path,
6162667Sstever@eecs.umich.edu                         exports = 'env')
6171869SN/A
6181869SN/A    # Set up the regression tests for each build.
6192929Sktlim@umich.edu    for e in envList:
6202929Sktlim@umich.edu        SConscript('tests/SConscript',
6213716Sstever@eecs.umich.edu                   build_dir = joinpath(build_path, 'tests', e.Label),
6222929Sktlim@umich.edu                   exports = { 'env' : e }, duplicate = False)
623955SN/A
6242598SN/AHelp(help_text)
6252598SN/A
6263546Sgblack@eecs.umich.edu
627955SN/A###################################################
628955SN/A#
629955SN/A# Let SCons do its thing.  At this point SCons will use the defined
6301530SN/A# build environments to build the requested targets.
631955SN/A#
632955SN/A###################################################
633955SN/A
634