SConstruct revision 3685
1955SN/A# -*- mode:python -*-
2955SN/A
35871Snate@binkert.org# Copyright (c) 2004-2005 The Regents of The University of Michigan
41762SN/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.
28955SN/A#
292665Ssaidi@eecs.umich.edu# Authors: Steve Reinhardt
302665Ssaidi@eecs.umich.edu
315863Snate@binkert.org###################################################
32955SN/A#
33955SN/A# SCons top-level build description (SConstruct) file.
34955SN/A#
35955SN/A# While in this directory ('m5'), just type 'scons' to build the default
36955SN/A# configuration (see below), or type 'scons build/<CONFIG>/<binary>'
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).
392632Sstever@eecs.umich.edu#
402632Sstever@eecs.umich.edu# You can build M5 in a different directory as long as there is a
41955SN/A# 'build/<CONFIG>' somewhere along the target path.  The build system
422632Sstever@eecs.umich.edu# expects that all configs under the same build directory are being
432632Sstever@eecs.umich.edu# built for the same host system.
442761Sstever@eecs.umich.edu#
452632Sstever@eecs.umich.edu# Examples:
462632Sstever@eecs.umich.edu#
472632Sstever@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.
492761Sstever@eecs.umich.edu#   % cd <path-to-src>/m5 ; scons build/ALPHA_FS/m5.debug
502761Sstever@eecs.umich.edu#   % cd <path-to-src>/m5/build/ALPHA_FS; scons -u m5.debug
512632Sstever@eecs.umich.edu#
522632Sstever@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.
562761Sstever@eecs.umich.edu#   % cd <path-to-src>/m5 ; scons /local/foo/build/ALPHA_FS/m5.debug
572761Sstever@eecs.umich.edu#   % cd /local/foo/build/ALPHA_FS; scons -C <path-to-src>/m5 m5.debug
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.
632632Sstever@eecs.umich.edu#
642632Sstever@eecs.umich.edu###################################################
65955SN/A
66955SN/A# Python library imports
67955SN/Aimport sys
685863Snate@binkert.orgimport os
695863Snate@binkert.org
705863Snate@binkert.org# Check for recent-enough Python and SCons versions.  If your system's
715863Snate@binkert.org# default installation of Python is not recent enough, you can use a
725863Snate@binkert.org# non-default installation of the Python interpreter by either (1)
735863Snate@binkert.org# rearranging your PATH so that scons finds the non-default 'python'
745863Snate@binkert.org# first or (2) explicitly invoking an alternative interpreter on the
755863Snate@binkert.org# scons script, e.g., "/usr/local/bin/python2.4 `which scons` [args]".
765863Snate@binkert.orgEnsurePythonVersion(2,4)
775863Snate@binkert.org
785863Snate@binkert.org# Ironically, SCons 0.96 dies if you give EnsureSconsVersion a
795863Snate@binkert.org# 3-element version number.
805863Snate@binkert.orgmin_scons_version = (0,96,91)
815863Snate@binkert.orgtry:
825863Snate@binkert.org    EnsureSConsVersion(*min_scons_version)
835863Snate@binkert.orgexcept:
845863Snate@binkert.org    print "Error checking current SCons version."
855863Snate@binkert.org    print "SCons", ".".join(map(str,min_scons_version)), "or greater required."
865863Snate@binkert.org    Exit(2)
875863Snate@binkert.org    
885863Snate@binkert.org
895863Snate@binkert.org# The absolute path to the current directory (where this file lives).
905863Snate@binkert.orgROOT = Dir('.').abspath
915863Snate@binkert.org
925863Snate@binkert.org# Paths to the M5 and external source trees.
935863Snate@binkert.orgSRCDIR = os.path.join(ROOT, 'src')
945863Snate@binkert.org
955863Snate@binkert.org# tell python where to find m5 python code
965863Snate@binkert.orgsys.path.append(os.path.join(ROOT, 'src/python'))
975863Snate@binkert.org
985863Snate@binkert.org###################################################
99955SN/A#
1005396Ssaidi@eecs.umich.edu# Figure out which configurations to set up based on the path(s) of
1015863Snate@binkert.org# the target(s).
1025863Snate@binkert.org#
1034202Sbinkertn@umich.edu###################################################
1045863Snate@binkert.org
1055863Snate@binkert.org# Find default configuration & binary.
1065863Snate@binkert.orgDefault(os.environ.get('M5_DEFAULT_BINARY', 'build/ALPHA_SE/m5.debug'))
1075863Snate@binkert.org
108955SN/A# Ask SCons which directory it was invoked from.
1095273Sstever@gmail.comlaunch_dir = GetLaunchDir()
1105871Snate@binkert.org
1115273Sstever@gmail.com# Make targets relative to invocation directory
1125871Snate@binkert.orgabs_targets = map(lambda x: os.path.normpath(os.path.join(launch_dir, str(x))),
1135863Snate@binkert.org                  BUILD_TARGETS)
1145863Snate@binkert.org
1155863Snate@binkert.org# helper function: find last occurrence of element in list
1165871Snate@binkert.orgdef rfind(l, elt, offs = -1):
1175872Snate@binkert.org    for i in range(len(l)+offs, 0, -1):
1185872Snate@binkert.org        if l[i] == elt:
1195872Snate@binkert.org            return i
1205871Snate@binkert.org    raise ValueError, "element not found"
1215871Snate@binkert.org
1225871Snate@binkert.org# helper function: compare dotted version numbers.
1235871Snate@binkert.org# E.g., compare_version('1.3.25', '1.4.1')
1245871Snate@binkert.org# returns -1, 0, 1 if v1 is <, ==, > v2
1255871Snate@binkert.orgdef compare_versions(v1, v2):
1265871Snate@binkert.org    # Convert dotted strings to lists
1275871Snate@binkert.org    v1 = map(int, v1.split('.'))
1285871Snate@binkert.org    v2 = map(int, v2.split('.'))
1295871Snate@binkert.org    # Compare corresponding elements of lists
1305871Snate@binkert.org    for n1,n2 in zip(v1, v2):
1315871Snate@binkert.org        if n1 < n2: return -1
1325871Snate@binkert.org        if n1 > n2: return  1
1335871Snate@binkert.org    # all corresponding values are equal... see if one has extra values
1345863Snate@binkert.org    if len(v1) < len(v2): return -1
1355227Ssaidi@eecs.umich.edu    if len(v1) > len(v2): return  1
1365396Ssaidi@eecs.umich.edu    return 0
1375396Ssaidi@eecs.umich.edu
1385396Ssaidi@eecs.umich.edu# Each target must have 'build' in the interior of the path; the
1395396Ssaidi@eecs.umich.edu# directory below this will determine the build parameters.  For
1405396Ssaidi@eecs.umich.edu# example, for target 'foo/bar/build/ALPHA_SE/arch/alpha/blah.do' we
1415396Ssaidi@eecs.umich.edu# recognize that ALPHA_SE specifies the configuration because it
1425396Ssaidi@eecs.umich.edu# follow 'build' in the bulid path.
1435396Ssaidi@eecs.umich.edu
1445588Ssaidi@eecs.umich.edu# Generate a list of the unique build roots and configs that the
1455396Ssaidi@eecs.umich.edu# collected targets reference.
1465396Ssaidi@eecs.umich.edubuild_paths = []
1475396Ssaidi@eecs.umich.edubuild_root = None
1485396Ssaidi@eecs.umich.edufor t in abs_targets:
1495396Ssaidi@eecs.umich.edu    path_dirs = t.split('/')
1505396Ssaidi@eecs.umich.edu    try:
1515396Ssaidi@eecs.umich.edu        build_top = rfind(path_dirs, 'build', -2)
1525396Ssaidi@eecs.umich.edu    except:
1535396Ssaidi@eecs.umich.edu        print "Error: no non-leaf 'build' dir found on target path", t
1545396Ssaidi@eecs.umich.edu        Exit(1)
1555396Ssaidi@eecs.umich.edu    this_build_root = os.path.join('/',*path_dirs[:build_top+1])
1565396Ssaidi@eecs.umich.edu    if not build_root:
1575396Ssaidi@eecs.umich.edu        build_root = this_build_root
1585396Ssaidi@eecs.umich.edu    else:
1595871Snate@binkert.org        if this_build_root != build_root:
1605871Snate@binkert.org            print "Error: build targets not under same build root\n"\
1615871Snate@binkert.org                  "  %s\n  %s" % (build_root, this_build_root)
1625871Snate@binkert.org            Exit(1)
1635871Snate@binkert.org    build_path = os.path.join('/',*path_dirs[:build_top+2])
1646003Snate@binkert.org    if build_path not in build_paths:
1656003Snate@binkert.org        build_paths.append(build_path)
166955SN/A
1675871Snate@binkert.org###################################################
1685871Snate@binkert.org#
1695871Snate@binkert.org# Set up the default build environment.  This environment is copied
1705871Snate@binkert.org# and modified according to each selected configuration.
171955SN/A#
1725871Snate@binkert.org###################################################
1735871Snate@binkert.org
1745871Snate@binkert.orgenv = Environment(ENV = os.environ,  # inherit user's environment vars
1751533SN/A                  ROOT = ROOT,
1765871Snate@binkert.org                  SRCDIR = SRCDIR)
1775871Snate@binkert.org
1785863Snate@binkert.org#Parse CC/CXX early so that we use the correct compiler for 
1795871Snate@binkert.org# to test for dependencies/versions/libraries/includes
1805871Snate@binkert.orgif ARGUMENTS.get('CC', None):
1815871Snate@binkert.org    env['CC'] = ARGUMENTS.get('CC')
1825871Snate@binkert.org
1835871Snate@binkert.orgif ARGUMENTS.get('CXX', None):
1845863Snate@binkert.org    env['CXX'] = ARGUMENTS.get('CXX')
1855871Snate@binkert.org
1865863Snate@binkert.orgenv.SConsignFile(os.path.join(build_root,"sconsign"))
1875871Snate@binkert.org
1884678Snate@binkert.org# Default duplicate option is to use hard links, but this messes up
1894678Snate@binkert.org# when you use emacs to edit a file in the target dir, as emacs moves
1904678Snate@binkert.org# file to file~ then copies to file, breaking the link.  Symbolic
1914678Snate@binkert.org# (soft) links work better.
1924678Snate@binkert.orgenv.SetOption('duplicate', 'soft-copy')
1934678Snate@binkert.org
1944678Snate@binkert.org# I waffle on this setting... it does avoid a few painful but
1954678Snate@binkert.org# unnecessary builds, but it also seems to make trivial builds take
1964678Snate@binkert.org# noticeably longer.
1974678Snate@binkert.orgif False:
1984678Snate@binkert.org    env.TargetSignatures('content')
1994678Snate@binkert.org
2005871Snate@binkert.org# M5_PLY is used by isa_parser.py to find the PLY package.
2014678Snate@binkert.orgenv.Append(ENV = { 'M5_PLY' : Dir('ext/ply') })
2025871Snate@binkert.org
2035871Snate@binkert.org# Set up default C++ compiler flags
2045871Snate@binkert.orgenv.Append(CCFLAGS='-pipe')
2055871Snate@binkert.orgenv.Append(CCFLAGS='-fno-strict-aliasing')
2065871Snate@binkert.orgenv.Append(CCFLAGS=Split('-Wall -Wno-sign-compare -Werror -Wundef'))
2075871Snate@binkert.orgif sys.platform == 'cygwin':
2085871Snate@binkert.org    # cygwin has some header file issues...
2095871Snate@binkert.org    env.Append(CCFLAGS=Split("-Wno-uninitialized"))
2105871Snate@binkert.orgenv.Append(CPPPATH=[Dir('ext/dnet')])
2115871Snate@binkert.org
2125871Snate@binkert.org# Check for SWIG
2135871Snate@binkert.orgif not env.has_key('SWIG'):
2145871Snate@binkert.org    print 'Error: SWIG utility not found.'
2155990Ssaidi@eecs.umich.edu    print '       Please install (see http://www.swig.org) and retry.'
2165871Snate@binkert.org    Exit(1)
2175871Snate@binkert.org
2185871Snate@binkert.org# Check for appropriate SWIG version
2194678Snate@binkert.orgswig_version = os.popen('swig -version').read().split()
2205871Snate@binkert.org# First 3 words should be "SWIG Version x.y.z"
2215871Snate@binkert.orgif swig_version[0] != 'SWIG' or swig_version[1] != 'Version':
2225871Snate@binkert.org    print 'Error determining SWIG version.'
2235871Snate@binkert.org    Exit(1)
2245871Snate@binkert.org
2255871Snate@binkert.orgmin_swig_version = '1.3.28'
2265871Snate@binkert.orgif compare_versions(swig_version[2], min_swig_version) < 0:
2275871Snate@binkert.org    print 'Error: SWIG version', min_swig_version, 'or newer required.'
2285871Snate@binkert.org    print '       Installed version:', swig_version[2]
2295871Snate@binkert.org    Exit(1)
2304678Snate@binkert.org
2315871Snate@binkert.org# Set up SWIG flags & scanner
2324678Snate@binkert.orgenv.Append(SWIGFLAGS=Split('-c++ -python -modern $_CPPINCFLAGS'))
2335871Snate@binkert.org
2345871Snate@binkert.orgimport SCons.Scanner
2355871Snate@binkert.org
2365871Snate@binkert.orgswig_inc_re = '^[ \t]*[%,#][ \t]*(?:include|import)[ \t]*(<|")([^>"]+)(>|")'
2375871Snate@binkert.org
2385871Snate@binkert.orgswig_scanner = SCons.Scanner.ClassicCPP("SwigScan", ".i", "CPPPATH",
2395871Snate@binkert.org                                        swig_inc_re)
2405871Snate@binkert.org
2415871Snate@binkert.orgenv.Append(SCANNERS = swig_scanner)
2425990Ssaidi@eecs.umich.edu
2435863Snate@binkert.org# Platform-specific configuration.  Note again that we assume that all
244955SN/A# builds under a given build root run on the same host platform.
245955SN/Aconf = Configure(env,
2462632Sstever@eecs.umich.edu                 conf_dir = os.path.join(build_root, '.scons_config'),
2472632Sstever@eecs.umich.edu                 log_file = os.path.join(build_root, 'scons_config.log'))
248955SN/A
249955SN/A# Find Python include and library directories for embedding the
250955SN/A# interpreter.  For consistency, we will use the same Python
251955SN/A# installation used to run scons (and thus this script).  If you want
2525863Snate@binkert.org# to link in an alternate version, see above for instructions on how
253955SN/A# to invoke scons with a different copy of the Python interpreter.
2542632Sstever@eecs.umich.edu
2552632Sstever@eecs.umich.edu# Get brief Python version name (e.g., "python2.4") for locating
2562632Sstever@eecs.umich.edu# include & library files
2572632Sstever@eecs.umich.edupy_version_name = 'python' + sys.version[:3]
2582632Sstever@eecs.umich.edu
2592632Sstever@eecs.umich.edu# include path, e.g. /usr/local/include/python2.4
2602632Sstever@eecs.umich.edupy_header_path = os.path.join(sys.exec_prefix, 'include', py_version_name)
2612632Sstever@eecs.umich.eduenv.Append(CPPPATH = py_header_path)
2622632Sstever@eecs.umich.edu# verify that it works
2632632Sstever@eecs.umich.eduif not conf.CheckHeader('Python.h', '<>'):
2642632Sstever@eecs.umich.edu    print "Error: can't find Python.h header in", py_header_path
2652632Sstever@eecs.umich.edu    Exit(1)
2662632Sstever@eecs.umich.edu
2673718Sstever@eecs.umich.edu# add library path too if it's not in the default place
2683718Sstever@eecs.umich.edupy_lib_path = None
2693718Sstever@eecs.umich.eduif sys.exec_prefix != '/usr':
2703718Sstever@eecs.umich.edu    py_lib_path = os.path.join(sys.exec_prefix, 'lib')
2713718Sstever@eecs.umich.eduelif sys.platform == 'cygwin':
2725863Snate@binkert.org    # cygwin puts the .dll in /bin for some reason
2735863Snate@binkert.org    py_lib_path = '/bin'
2743718Sstever@eecs.umich.eduif py_lib_path:
2753718Sstever@eecs.umich.edu    env.Append(LIBPATH = py_lib_path)
2765863Snate@binkert.org    print 'Adding', py_lib_path, 'to LIBPATH for', py_version_name
2775863Snate@binkert.orgif not conf.CheckLib(py_version_name):
2783718Sstever@eecs.umich.edu    print "Error: can't find Python library", py_version_name
2793718Sstever@eecs.umich.edu    Exit(1)
2802634Sstever@eecs.umich.edu
2812634Sstever@eecs.umich.edu# On Solaris you need to use libsocket for socket ops
2825863Snate@binkert.orgif not conf.CheckLibWithHeader(None, 'sys/socket.h', 'C++', 'accept(0,0,0);'):
2832638Sstever@eecs.umich.edu   if not conf.CheckLibWithHeader('socket', 'sys/socket.h', 'C++', 'accept(0,0,0);'):
2842632Sstever@eecs.umich.edu       print "Can't find library with socket calls (e.g. accept())"
2852632Sstever@eecs.umich.edu       Exit(1)
2862632Sstever@eecs.umich.edu
2872632Sstever@eecs.umich.edu# Check for zlib.  If the check passes, libz will be automatically
2882632Sstever@eecs.umich.edu# added to the LIBS environment variable.
2892632Sstever@eecs.umich.eduif not conf.CheckLibWithHeader('z', 'zlib.h', 'C++'):
2901858SN/A    print 'Error: did not find needed zlib compression library '\
2913716Sstever@eecs.umich.edu          'and/or zlib.h header file.'
2922638Sstever@eecs.umich.edu    print '       Please install zlib and try again.'
2932638Sstever@eecs.umich.edu    Exit(1)
2942638Sstever@eecs.umich.edu
2952638Sstever@eecs.umich.edu# Check for <fenv.h> (C99 FP environment control)
2962638Sstever@eecs.umich.eduhave_fenv = conf.CheckHeader('fenv.h', '<>')
2972638Sstever@eecs.umich.eduif not have_fenv:
2982638Sstever@eecs.umich.edu    print "Warning: Header file <fenv.h> not found."
2995863Snate@binkert.org    print "         This host has no IEEE FP rounding mode control."
3005863Snate@binkert.org
3015863Snate@binkert.org# Check for mysql.
302955SN/Amysql_config = WhereIs('mysql_config')
3035341Sstever@gmail.comhave_mysql = mysql_config != None
3045341Sstever@gmail.com
3055863Snate@binkert.org# Check MySQL version.
3065341Sstever@gmail.comif have_mysql:
3074494Ssaidi@eecs.umich.edu    mysql_version = os.popen(mysql_config + ' --version').read()
3084494Ssaidi@eecs.umich.edu    min_mysql_version = '4.1'
3095863Snate@binkert.org    if compare_versions(mysql_version, min_mysql_version) < 0:
3101105SN/A        print 'Warning: MySQL', min_mysql_version, 'or newer required.'
3112667Sstever@eecs.umich.edu        print '         Version', mysql_version, 'detected.'
3122667Sstever@eecs.umich.edu        have_mysql = False
3132667Sstever@eecs.umich.edu
3142667Sstever@eecs.umich.edu# Set up mysql_config commands.
3152667Sstever@eecs.umich.eduif have_mysql:
3162667Sstever@eecs.umich.edu    mysql_config_include = mysql_config + ' --include'
3175341Sstever@gmail.com    if os.system(mysql_config_include + ' > /dev/null') != 0:
3185863Snate@binkert.org        # older mysql_config versions don't support --include, use
3195341Sstever@gmail.com        # --cflags instead
3205341Sstever@gmail.com        mysql_config_include = mysql_config + ' --cflags | sed s/\\\'//g'
3215341Sstever@gmail.com    # This seems to work in all versions
3225863Snate@binkert.org    mysql_config_libs = mysql_config + ' --libs'
3235341Sstever@gmail.com
3245341Sstever@gmail.comenv = conf.Finish()
3255341Sstever@gmail.com
3265863Snate@binkert.org# Define the universe of supported ISAs
3275341Sstever@gmail.comenv['ALL_ISA_LIST'] = ['alpha', 'sparc', 'mips']
3285341Sstever@gmail.com
3295341Sstever@gmail.com# Define the universe of supported CPU models
3305341Sstever@gmail.comenv['ALL_CPU_LIST'] = ['AtomicSimpleCPU', 'TimingSimpleCPU',
3315341Sstever@gmail.com                       'O3CPU', 'OzoneCPU']
3325341Sstever@gmail.com
3335341Sstever@gmail.comif os.path.isdir(os.path.join(SRCDIR, 'src/encumbered/cpu/full')):
3345341Sstever@gmail.com    env['ALL_CPU_LIST'] += ['FullCPU']
3355341Sstever@gmail.com
3365341Sstever@gmail.com# Sticky options get saved in the options file so they persist from
3375863Snate@binkert.org# one invocation to the next (unless overridden, in which case the new
3385341Sstever@gmail.com# value becomes sticky).
3395863Snate@binkert.orgsticky_opts = Options(args=ARGUMENTS)
3405341Sstever@gmail.comsticky_opts.AddOptions(
3415863Snate@binkert.org    EnumOption('TARGET_ISA', 'Target ISA', 'alpha', env['ALL_ISA_LIST']),
3425863Snate@binkert.org    BoolOption('FULL_SYSTEM', 'Full-system support', False),
3435863Snate@binkert.org    # There's a bug in scons 0.96.1 that causes ListOptions with list
3445397Ssaidi@eecs.umich.edu    # values (more than one value) not to be able to be restored from
3455397Ssaidi@eecs.umich.edu    # a saved option file.  If this causes trouble then upgrade to
3465341Sstever@gmail.com    # scons 0.96.90 or later.
3475341Sstever@gmail.com    ListOption('CPU_MODELS', 'CPU models', 'AtomicSimpleCPU,TimingSimpleCPU,O3CPU',
3485341Sstever@gmail.com               env['ALL_CPU_LIST']),
3495341Sstever@gmail.com    BoolOption('ALPHA_TLASER',
3505341Sstever@gmail.com               'Model Alpha TurboLaser platform (vs. Tsunami)', False),
3515341Sstever@gmail.com    BoolOption('NO_FAST_ALLOC', 'Disable fast object allocator', False),
3525341Sstever@gmail.com    BoolOption('EFENCE', 'Link with Electric Fence malloc debugger',
3535341Sstever@gmail.com               False),
3545863Snate@binkert.org    BoolOption('SS_COMPATIBLE_FP',
3555341Sstever@gmail.com               'Make floating-point results compatible with SimpleScalar',
3565341Sstever@gmail.com               False),
3575863Snate@binkert.org    BoolOption('USE_SSE2',
3585341Sstever@gmail.com               'Compile for SSE2 (-msse2) to get IEEE FP on x86 hosts',
3595863Snate@binkert.org               False),
3605863Snate@binkert.org    BoolOption('USE_MYSQL', 'Use MySQL for stats output', have_mysql),
3615341Sstever@gmail.com    BoolOption('USE_FENV', 'Use <fenv.h> IEEE mode control', have_fenv),
3625863Snate@binkert.org    BoolOption('USE_CHECKER', 'Use checker for detailed CPU models', False),
3635863Snate@binkert.org    ('CC', 'C compiler', os.environ.get('CC', env['CC'])),
3645341Sstever@gmail.com    ('CXX', 'C++ compiler', os.environ.get('CXX', env['CXX'])),
3655863Snate@binkert.org    BoolOption('BATCH', 'Use batch pool for build and tests', False),
3665341Sstever@gmail.com    ('BATCH_CMD', 'Batch pool submission command name', 'qdo'),
3675871Snate@binkert.org    ('PYTHONHOME',
3685341Sstever@gmail.com     'Override the default PYTHONHOME for this system (use with caution)',
3695742Snate@binkert.org     '%s:%s' % (sys.prefix, sys.exec_prefix))
3705742Snate@binkert.org    )
3715742Snate@binkert.org
3725341Sstever@gmail.com# Non-sticky options only apply to the current build.
3735742Snate@binkert.orgnonsticky_opts = Options(args=ARGUMENTS)
3745742Snate@binkert.orgnonsticky_opts.AddOptions(
3755341Sstever@gmail.com    BoolOption('update_ref', 'Update test reference outputs', False)
3762632Sstever@eecs.umich.edu    )
3776016Snate@binkert.org
3785871Snate@binkert.org# These options get exported to #defines in config/*.hh (see src/SConscript).
3795871Snate@binkert.orgenv.ExportOptions = ['FULL_SYSTEM', 'ALPHA_TLASER', 'USE_FENV', \
3805871Snate@binkert.org                     'USE_MYSQL', 'NO_FAST_ALLOC', 'SS_COMPATIBLE_FP', \
3815871Snate@binkert.org                     'USE_CHECKER', 'PYTHONHOME', 'TARGET_ISA']
3825871Snate@binkert.org
3835871Snate@binkert.org# Define a handy 'no-op' action
3845871Snate@binkert.orgdef no_action(target, source, env):
3853942Ssaidi@eecs.umich.edu    return 0
3863940Ssaidi@eecs.umich.edu
3873918Ssaidi@eecs.umich.eduenv.NoAction = Action(no_action, None)
3883918Ssaidi@eecs.umich.edu
3891858SN/A###################################################
3903918Ssaidi@eecs.umich.edu#
3913918Ssaidi@eecs.umich.edu# Define a SCons builder for configuration flag headers.
3923918Ssaidi@eecs.umich.edu#
3933918Ssaidi@eecs.umich.edu###################################################
3945571Snate@binkert.org
3953940Ssaidi@eecs.umich.edu# This function generates a config header file that #defines the
3963940Ssaidi@eecs.umich.edu# option symbol to the current option setting (0 or 1).  The source
3973918Ssaidi@eecs.umich.edu# operands are the name of the option and a Value node containing the
3983918Ssaidi@eecs.umich.edu# value of the option.
3993918Ssaidi@eecs.umich.edudef build_config_file(target, source, env):
4003918Ssaidi@eecs.umich.edu    (option, value) = [s.get_contents() for s in source]
4013918Ssaidi@eecs.umich.edu    f = file(str(target[0]), 'w')
4023918Ssaidi@eecs.umich.edu    print >> f, '#define', option, value
4035871Snate@binkert.org    f.close()
4043918Ssaidi@eecs.umich.edu    return None
4053918Ssaidi@eecs.umich.edu
4063940Ssaidi@eecs.umich.edu# Generate the message to be printed when building the config file.
4073918Ssaidi@eecs.umich.edudef build_config_file_string(target, source, env):
4083918Ssaidi@eecs.umich.edu    (option, value) = [s.get_contents() for s in source]
4095397Ssaidi@eecs.umich.edu    return "Defining %s as %s in %s." % (option, value, target[0])
4105397Ssaidi@eecs.umich.edu
4115397Ssaidi@eecs.umich.edu# Combine the two functions into a scons Action object.
4125708Ssaidi@eecs.umich.educonfig_action = Action(build_config_file, build_config_file_string)
4135708Ssaidi@eecs.umich.edu
4145708Ssaidi@eecs.umich.edu# The emitter munges the source & target node lists to reflect what
4155708Ssaidi@eecs.umich.edu# we're really doing.
4165708Ssaidi@eecs.umich.edudef config_emitter(target, source, env):
4175397Ssaidi@eecs.umich.edu    # extract option name from Builder arg
4181851SN/A    option = str(target[0])
4191851SN/A    # True target is config header file
4201858SN/A    target = os.path.join('config', option.lower() + '.hh')
4215200Sstever@gmail.com    val = env[option]
422955SN/A    if isinstance(val, bool):
4233053Sstever@eecs.umich.edu        # Force value to 0/1
4243053Sstever@eecs.umich.edu        val = int(val)
4253053Sstever@eecs.umich.edu    elif isinstance(val, str):
4263053Sstever@eecs.umich.edu        val = '"' + val + '"'
4273053Sstever@eecs.umich.edu        
4283053Sstever@eecs.umich.edu    # Sources are option name & value (packaged in SCons Value nodes)
4293053Sstever@eecs.umich.edu    return ([target], [Value(option), Value(val)])
4305871Snate@binkert.org
4313053Sstever@eecs.umich.educonfig_builder = Builder(emitter = config_emitter, action = config_action)
4324742Sstever@eecs.umich.edu
4334742Sstever@eecs.umich.eduenv.Append(BUILDERS = { 'ConfigFile' : config_builder })
4343053Sstever@eecs.umich.edu
4353053Sstever@eecs.umich.edu###################################################
4363053Sstever@eecs.umich.edu#
4373053Sstever@eecs.umich.edu# Define a SCons builder for copying files.  This is used by the
4383053Sstever@eecs.umich.edu# Python zipfile code in src/python/SConscript, but is placed up here
4393053Sstever@eecs.umich.edu# since it's potentially more generally applicable.
4403053Sstever@eecs.umich.edu#
4413053Sstever@eecs.umich.edu###################################################
4423053Sstever@eecs.umich.edu
4432667Sstever@eecs.umich.educopy_builder = Builder(action = Copy("$TARGET", "$SOURCE"))
4444554Sbinkertn@umich.edu
4454554Sbinkertn@umich.eduenv.Append(BUILDERS = { 'CopyFile' : copy_builder })
4462667Sstever@eecs.umich.edu
4474554Sbinkertn@umich.edu###################################################
4484554Sbinkertn@umich.edu#
4494554Sbinkertn@umich.edu# Define a simple SCons builder to concatenate files.
4504554Sbinkertn@umich.edu#
4514554Sbinkertn@umich.edu# Used to append the Python zip archive to the executable.
4524554Sbinkertn@umich.edu#
4534554Sbinkertn@umich.edu###################################################
4544781Snate@binkert.org
4554554Sbinkertn@umich.educoncat_builder = Builder(action = Action(['cat $SOURCES > $TARGET',
4564554Sbinkertn@umich.edu                                          'chmod +x $TARGET']))
4572667Sstever@eecs.umich.edu
4584554Sbinkertn@umich.eduenv.Append(BUILDERS = { 'Concat' : concat_builder })
4594554Sbinkertn@umich.edu
4604554Sbinkertn@umich.edu
4614554Sbinkertn@umich.edu# base help text
4622667Sstever@eecs.umich.eduhelp_text = '''
4634554Sbinkertn@umich.eduUsage: scons [scons options] [build options] [target(s)]
4642667Sstever@eecs.umich.edu
4654554Sbinkertn@umich.edu'''
4664554Sbinkertn@umich.edu
4672667Sstever@eecs.umich.edu# libelf build is shared across all configs in the build root.
4685522Snate@binkert.orgenv.SConscript('ext/libelf/SConscript',
4695522Snate@binkert.org               build_dir = os.path.join(build_root, 'libelf'),
4705522Snate@binkert.org               exports = 'env')
4715522Snate@binkert.org
4725522Snate@binkert.org###################################################
4735522Snate@binkert.org#
4745522Snate@binkert.org# This function is used to set up a directory with switching headers
4755522Snate@binkert.org#
4765522Snate@binkert.org###################################################
4775522Snate@binkert.org
4785522Snate@binkert.orgdef make_switching_dir(dirname, switch_headers, env):
4795522Snate@binkert.org    # Generate the header.  target[0] is the full path of the output
4805522Snate@binkert.org    # header to generate.  'source' is a dummy variable, since we get the
4815522Snate@binkert.org    # list of ISAs from env['ALL_ISA_LIST'].
4825522Snate@binkert.org    def gen_switch_hdr(target, source, env):
4835522Snate@binkert.org	fname = str(target[0])
4845522Snate@binkert.org	basename = os.path.basename(fname)
4855522Snate@binkert.org	f = open(fname, 'w')
4865522Snate@binkert.org	f.write('#include "arch/isa_specific.hh"\n')
4875522Snate@binkert.org	cond = '#if'
4885522Snate@binkert.org	for isa in env['ALL_ISA_LIST']:
4895522Snate@binkert.org	    f.write('%s THE_ISA == %s_ISA\n#include "%s/%s/%s"\n'
4905522Snate@binkert.org		    % (cond, isa.upper(), dirname, isa, basename))
4915522Snate@binkert.org	    cond = '#elif'
4925522Snate@binkert.org	f.write('#else\n#error "THE_ISA not set"\n#endif\n')
4935522Snate@binkert.org	f.close()
4942638Sstever@eecs.umich.edu	return 0
4952638Sstever@eecs.umich.edu
4962638Sstever@eecs.umich.edu    # String to print when generating header
4973716Sstever@eecs.umich.edu    def gen_switch_hdr_string(target, source, env):
4985522Snate@binkert.org	return "Generating switch header " + str(target[0])
4995522Snate@binkert.org
5005522Snate@binkert.org    # Build SCons Action object. 'varlist' specifies env vars that this
5015522Snate@binkert.org    # action depends on; when env['ALL_ISA_LIST'] changes these actions
5025522Snate@binkert.org    # should get re-executed.
5035522Snate@binkert.org    switch_hdr_action = Action(gen_switch_hdr, gen_switch_hdr_string,
5041858SN/A                               varlist=['ALL_ISA_LIST'])
5055227Ssaidi@eecs.umich.edu
5065227Ssaidi@eecs.umich.edu    # Instantiate actions for each header
5075227Ssaidi@eecs.umich.edu    for hdr in switch_headers:
5085227Ssaidi@eecs.umich.edu        env.Command(hdr, [], switch_hdr_action)
5095227Ssaidi@eecs.umich.edu
5105863Snate@binkert.orgenv.make_switching_dir = make_switching_dir
5115227Ssaidi@eecs.umich.edu
5125227Ssaidi@eecs.umich.edu###################################################
5135227Ssaidi@eecs.umich.edu#
5145227Ssaidi@eecs.umich.edu# Define build environments for selected configurations.
5155227Ssaidi@eecs.umich.edu#
5165227Ssaidi@eecs.umich.edu###################################################
5175227Ssaidi@eecs.umich.edu
5185204Sstever@gmail.com# rename base env
5195204Sstever@gmail.combase_env = env
5205204Sstever@gmail.com
5215204Sstever@gmail.comfor build_path in build_paths:
5225204Sstever@gmail.com    print "Building in", build_path
5235204Sstever@gmail.com    # build_dir is the tail component of build path, and is used to
5245204Sstever@gmail.com    # determine the build parameters (e.g., 'ALPHA_SE')
5255204Sstever@gmail.com    (build_root, build_dir) = os.path.split(build_path)
5265204Sstever@gmail.com    # Make a copy of the build-root environment to use for this config.
5275204Sstever@gmail.com    env = base_env.Copy()
5285204Sstever@gmail.com
5295204Sstever@gmail.com    # Set env options according to the build directory config.
5305204Sstever@gmail.com    sticky_opts.files = []
5315204Sstever@gmail.com    # Options for $BUILD_ROOT/$BUILD_DIR are stored in
5325204Sstever@gmail.com    # $BUILD_ROOT/options/$BUILD_DIR so you can nuke
5335204Sstever@gmail.com    # $BUILD_ROOT/$BUILD_DIR without losing your options settings.
5345204Sstever@gmail.com    current_opts_file = os.path.join(build_root, 'options', build_dir)
5355204Sstever@gmail.com    if os.path.isfile(current_opts_file):
5365204Sstever@gmail.com        sticky_opts.files.append(current_opts_file)
5373118Sstever@eecs.umich.edu        print "Using saved options file %s" % current_opts_file
5383118Sstever@eecs.umich.edu    else:
5393118Sstever@eecs.umich.edu        # Build dir-specific options file doesn't exist.
5403118Sstever@eecs.umich.edu
5413118Sstever@eecs.umich.edu        # Make sure the directory is there so we can create it later
5425863Snate@binkert.org        opt_dir = os.path.dirname(current_opts_file)
5433118Sstever@eecs.umich.edu        if not os.path.isdir(opt_dir):
5445863Snate@binkert.org            os.mkdir(opt_dir)
5453118Sstever@eecs.umich.edu
5465863Snate@binkert.org        # Get default build options from source tree.  Options are
5475863Snate@binkert.org        # normally determined by name of $BUILD_DIR, but can be
5485863Snate@binkert.org        # overriden by 'default=' arg on command line.
5495863Snate@binkert.org        default_opts_file = os.path.join('build_opts',
5505863Snate@binkert.org                                         ARGUMENTS.get('default', build_dir))
5515863Snate@binkert.org        if os.path.isfile(default_opts_file):
5525863Snate@binkert.org            sticky_opts.files.append(default_opts_file)
5535863Snate@binkert.org            print "Options file %s not found,\n  using defaults in %s" \
5546003Snate@binkert.org                  % (current_opts_file, default_opts_file)
5555863Snate@binkert.org        else:
5565863Snate@binkert.org            print "Error: cannot find options file %s or %s" \
5575863Snate@binkert.org                  % (current_opts_file, default_opts_file)
5585863Snate@binkert.org            Exit(1)
5595863Snate@binkert.org
5605863Snate@binkert.org    # Apply current option settings to env
5615863Snate@binkert.org    sticky_opts.Update(env)
5625863Snate@binkert.org    nonsticky_opts.Update(env)
5635863Snate@binkert.org
5645863Snate@binkert.org    help_text += "Sticky options for %s:\n" % build_dir \
5655863Snate@binkert.org                 + sticky_opts.GenerateHelpText(env) \
5665863Snate@binkert.org                 + "\nNon-sticky options for %s:\n" % build_dir \
5675863Snate@binkert.org                 + nonsticky_opts.GenerateHelpText(env)
5685863Snate@binkert.org
5695863Snate@binkert.org    # Process option settings.
5703118Sstever@eecs.umich.edu
5715863Snate@binkert.org    if not have_fenv and env['USE_FENV']:
5723118Sstever@eecs.umich.edu        print "Warning: <fenv.h> not available; " \
5733118Sstever@eecs.umich.edu              "forcing USE_FENV to False in", build_dir + "."
5745863Snate@binkert.org        env['USE_FENV'] = False
5755863Snate@binkert.org
5765863Snate@binkert.org    if not env['USE_FENV']:
5775863Snate@binkert.org        print "Warning: No IEEE FP rounding mode control in", build_dir + "."
5785863Snate@binkert.org        print "         FP results may deviate slightly from other platforms."
5795863Snate@binkert.org
5803118Sstever@eecs.umich.edu    if env['EFENCE']:
5813483Ssaidi@eecs.umich.edu        env.Append(LIBS=['efence'])
5823494Ssaidi@eecs.umich.edu
5833494Ssaidi@eecs.umich.edu    if env['USE_MYSQL']:
5843483Ssaidi@eecs.umich.edu        if not have_mysql:
5853483Ssaidi@eecs.umich.edu            print "Warning: MySQL not available; " \
5863483Ssaidi@eecs.umich.edu                  "forcing USE_MYSQL to False in", build_dir + "."
5873053Sstever@eecs.umich.edu            env['USE_MYSQL'] = False
5883053Sstever@eecs.umich.edu        else:
5893918Ssaidi@eecs.umich.edu            print "Compiling in", build_dir, "with MySQL support."
5903053Sstever@eecs.umich.edu            env.ParseConfig(mysql_config_libs)
5913053Sstever@eecs.umich.edu            env.ParseConfig(mysql_config_include)
5923053Sstever@eecs.umich.edu
5933053Sstever@eecs.umich.edu    # Save sticky option settings back to current options file
5943053Sstever@eecs.umich.edu    sticky_opts.Save(current_opts_file, env)
5951858SN/A
5961858SN/A    # Do this after we save setting back, or else we'll tack on an
5971858SN/A    # extra 'qdo' every time we run scons.
5981858SN/A    if env['BATCH']:
5991858SN/A        env['CC']  = env['BATCH_CMD'] + ' ' + env['CC']
6001858SN/A        env['CXX'] = env['BATCH_CMD'] + ' ' + env['CXX']
6015863Snate@binkert.org
6025863Snate@binkert.org    if env['USE_SSE2']:
6031859SN/A        env.Append(CCFLAGS='-msse2')
6045863Snate@binkert.org
6051858SN/A    # The src/SConscript file sets up the build rules in 'env' according
6065863Snate@binkert.org    # to the configured options.  It returns a list of environments,
6071858SN/A    # one for each variant build (debug, opt, etc.)
6081859SN/A    envList = SConscript('src/SConscript', build_dir = build_path,
6091859SN/A                         exports = 'env')
6105863Snate@binkert.org
6113053Sstever@eecs.umich.edu    # Set up the regression tests for each build.
6123053Sstever@eecs.umich.edu    for e in envList:
6133053Sstever@eecs.umich.edu        SConscript('tests/SConscript',
6143053Sstever@eecs.umich.edu                   build_dir = os.path.join(build_path, 'tests', e.Label),
6151859SN/A                   exports = { 'env' : e }, duplicate = False)
6161859SN/A
6171859SN/AHelp(help_text)
6181859SN/A
6191859SN/A
6201859SN/A###################################################
6211859SN/A#
6221859SN/A# Let SCons do its thing.  At this point SCons will use the defined
6231862SN/A# build environments to build the requested targets.
6241859SN/A#
6251859SN/A###################################################
6261859SN/A
6275863Snate@binkert.org