SConstruct revision 6655
12292SN/A# -*- mode:python -*- 22727Sktlim@umich.edu 32292SN/A# Copyright (c) 2009 The Hewlett-Packard Development Company 42292SN/A# Copyright (c) 2004-2005 The Regents of The University of Michigan 52292SN/A# All rights reserved. 62292SN/A# 72292SN/A# Redistribution and use in source and binary forms, with or without 82292SN/A# modification, are permitted provided that the following conditions are 92292SN/A# met: redistributions of source code must retain the above copyright 102292SN/A# notice, this list of conditions and the following disclaimer; 112292SN/A# redistributions in binary form must reproduce the above copyright 122292SN/A# notice, this list of conditions and the following disclaimer in the 132292SN/A# documentation and/or other materials provided with the distribution; 142292SN/A# neither the name of the copyright holders nor the names of its 152292SN/A# contributors may be used to endorse or promote products derived from 162292SN/A# this software without specific prior written permission. 172292SN/A# 182292SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 192292SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 202292SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 212292SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 222292SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 232292SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 242292SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 252292SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 262292SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 272689Sktlim@umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 282689Sktlim@umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 292292SN/A# 302292SN/A# Authors: Steve Reinhardt 312329SN/A# Nathan Binkert 322980Sgblack@eecs.umich.edu 332329SN/A################################################### 342329SN/A# 352292SN/A# SCons top-level build description (SConstruct) file. 362292SN/A# 372292SN/A# While in this directory ('m5'), just type 'scons' to build the default 382907Sktlim@umich.edu# configuration (see below), or type 'scons build/<CONFIG>/<binary>' 392907Sktlim@umich.edu# to build some other configuration (e.g., 'build/ALPHA_FS/m5.opt' for 402907Sktlim@umich.edu# the optimized full-system version). 412907Sktlim@umich.edu# 422907Sktlim@umich.edu# You can build M5 in a different directory as long as there is a 432907Sktlim@umich.edu# 'build/<CONFIG>' somewhere along the target path. The build system 442907Sktlim@umich.edu# expects that all configs under the same build directory are being 452907Sktlim@umich.edu# built for the same host system. 462907Sktlim@umich.edu# 472907Sktlim@umich.edu# Examples: 482907Sktlim@umich.edu# 493639Sktlim@umich.edu# The following two commands are equivalent. The '-u' option tells 502907Sktlim@umich.edu# scons to search up the directory tree for this SConstruct file. 512907Sktlim@umich.edu# % cd <path-to-src>/m5 ; scons build/ALPHA_FS/m5.debug 522907Sktlim@umich.edu# % cd <path-to-src>/m5/build/ALPHA_FS; scons -u m5.debug 532907Sktlim@umich.edu# 542907Sktlim@umich.edu# The following two commands are equivalent and demonstrate building 552907Sktlim@umich.edu# in a directory outside of the source tree. The '-C' option tells 563647Srdreslin@umich.edu# scons to chdir to the specified directory to find this SConstruct 573647Srdreslin@umich.edu# file. 583647Srdreslin@umich.edu# % cd <path-to-src>/m5 ; scons /local/foo/build/ALPHA_FS/m5.debug 593647Srdreslin@umich.edu# % cd /local/foo/build/ALPHA_FS; scons -C <path-to-src>/m5 m5.debug 603647Srdreslin@umich.edu# 612907Sktlim@umich.edu# You can use 'scons -H' to print scons options. If you're in this 623647Srdreslin@umich.edu# 'm5' directory (or use -u or -C to tell scons where to find this 632907Sktlim@umich.edu# file), you can use 'scons -h' to print all the M5-specific build 642907Sktlim@umich.edu# options as well. 652907Sktlim@umich.edu# 662907Sktlim@umich.edu################################################### 672907Sktlim@umich.edu 682907Sktlim@umich.edu# Check for recent-enough Python and SCons versions. 692907Sktlim@umich.edutry: 703310Srdreslin@umich.edu # Really old versions of scons only take two options for the 713310Srdreslin@umich.edu # function, so check once without the revision and once with the 723310Srdreslin@umich.edu # revision, the first instance will fail for stuff other than 733310Srdreslin@umich.edu # 0.98, and the second will fail for 0.98.0 743310Srdreslin@umich.edu EnsureSConsVersion(0, 98) 753339Srdreslin@umich.edu EnsureSConsVersion(0, 98, 1) 763310Srdreslin@umich.eduexcept SystemExit, e: 773310Srdreslin@umich.edu print """ 782907Sktlim@umich.eduFor more details, see: 792907Sktlim@umich.edu http://m5sim.org/wiki/index.php/Compiling_M5 802907Sktlim@umich.edu""" 812907Sktlim@umich.edu raise 822907Sktlim@umich.edu 832907Sktlim@umich.edu# We ensure the python version early because we have stuff that 842907Sktlim@umich.edu# requires python 2.4 853014Srdreslin@umich.edutry: 863014Srdreslin@umich.edu EnsurePythonVersion(2, 4) 873014Srdreslin@umich.eduexcept SystemExit, e: 883014Srdreslin@umich.edu print """ 893014Srdreslin@umich.eduYou can use a non-default installation of the Python interpreter by 902907Sktlim@umich.edueither (1) rearranging your PATH so that scons finds the non-default 912907Sktlim@umich.edu'python' first or (2) explicitly invoking an alternative interpreter 922907Sktlim@umich.eduon the scons script. 932907Sktlim@umich.edu 942907Sktlim@umich.eduFor more details, see: 952907Sktlim@umich.edu http://m5sim.org/wiki/index.php/Using_a_non-default_Python_installation 962907Sktlim@umich.edu""" 972292SN/A raise 982907Sktlim@umich.edu 992907Sktlim@umich.edu# Global Python includes 1002907Sktlim@umich.eduimport os 1012292SN/Aimport re 1022292SN/Aimport subprocess 1032292SN/Aimport sys 1043647Srdreslin@umich.edu 1053647Srdreslin@umich.edufrom os import mkdir, environ 1062292SN/Afrom os.path import abspath, basename, dirname, expanduser, normpath 1072292SN/Afrom os.path import exists, isdir, isfile 1082292SN/Afrom os.path import join as joinpath, split as splitpath 1092980Sgblack@eecs.umich.edu 1102292SN/A# SCons includes 1112292SN/Aimport SCons 1122292SN/Aimport SCons.Node 1132292SN/A 1142292SN/Aextra_python_paths = [ 1152292SN/A Dir('src/python').srcnode().abspath, # M5 includes 1162292SN/A Dir('ext/ply').srcnode().abspath, # ply is used by several files 1172292SN/A ] 1182292SN/A 1192292SN/Asys.path[1:1] = extra_python_paths 1202292SN/A 1212292SN/Afrom m5.util import compareVersions, readCommand 1222292SN/A 1232292SN/A######################################################################## 1242292SN/A# 1252292SN/A# Set up the main build environment. 1262292SN/A# 1272292SN/A######################################################################## 1282292SN/Ause_vars = set([ 'AS', 'AR', 'CC', 'CXX', 'HOME', 'LD_LIBRARY_PATH', 'PATH', 1292292SN/A 'PYTHONPATH', 'RANLIB' ]) 1302292SN/A 1312292SN/Ause_env = {} 1322292SN/Afor key,val in os.environ.iteritems(): 1332292SN/A if key in use_vars or key.startswith("M5"): 1342292SN/A use_env[key] = val 1352292SN/A 1362292SN/Amain = Environment(ENV=use_env) 1372292SN/Amain.root = Dir(".") # The current directory (where this file lives). 1382292SN/Amain.srcdir = Dir("src") # The source directory 1392292SN/A 1402292SN/A# add useful python code PYTHONPATH so it can be used by subprocesses 1412292SN/A# as well 1422292SN/Amain.AppendENVPath('PYTHONPATH', extra_python_paths) 1432292SN/A 1442292SN/A######################################################################## 1452292SN/A# 1462292SN/A# Mercurial Stuff. 1472292SN/A# 1482292SN/A# If the M5 directory is a mercurial repository, we should do some 1492292SN/A# extra things. 1502292SN/A# 1512292SN/A######################################################################## 1522292SN/A 1532292SN/Ahgdir = main.root.Dir(".hg") 1542292SN/A 1552292SN/Amercurial_style_message = """ 1562292SN/AYou're missing the M5 style hook. 1572292SN/APlease install the hook so we can ensure that all code fits a common style. 1582907Sktlim@umich.edu 1592907Sktlim@umich.eduAll you'd need to do is add the following lines to your repository .hg/hgrc 1602292SN/Aor your personal .hgrc 1612292SN/A---------------- 1622292SN/A 1632292SN/A[extensions] 1642292SN/Astyle = %s/util/style.py 1652292SN/A 1662292SN/A[hooks] 1672292SN/Apretxncommit.style = python:style.check_whitespace 1682292SN/A""" % (main.root) 1692292SN/A 1702292SN/Amercurial_bin_not_found = """ 1712292SN/AMercurial binary cannot be found, unfortunately this means that we 1722292SN/Acannot easily determine the version of M5 that you are running and 1732727Sktlim@umich.eduthis makes error messages more difficult to collect. Please consider 1742727Sktlim@umich.eduinstalling mercurial if you choose to post an error message 1752727Sktlim@umich.edu""" 1762727Sktlim@umich.edu 1772727Sktlim@umich.edumercurial_lib_not_found = """ 1782727Sktlim@umich.eduMercurial libraries cannot be found, ignoring style hook 1792727Sktlim@umich.eduIf you are actually a M5 developer, please fix this and 1802727Sktlim@umich.edurun the style hook. It is important. 1812727Sktlim@umich.edu""" 1822727Sktlim@umich.edu 1832980Sgblack@eecs.umich.eduhg_info = "Unknown" 1842292SN/Aif hgdir.exists(): 1852292SN/A # 1) Grab repository revision if we know it. 1862292SN/A cmd = "hg id -n -i -t -b" 1872292SN/A try: 1882292SN/A hg_info = readCommand(cmd, cwd=main.root.abspath).strip() 1892292SN/A except OSError: 1902292SN/A print mercurial_bin_not_found 1912733Sktlim@umich.edu 1922292SN/A # 2) Ensure that the style hook is in place. 1932292SN/A try: 1942292SN/A ui = None 1952907Sktlim@umich.edu if ARGUMENTS.get('IGNORE_STYLE') != 'True': 1962907Sktlim@umich.edu from mercurial import ui 1972292SN/A ui = ui.ui() 1982292SN/A except ImportError: 1992292SN/A print mercurial_lib_not_found 2002292SN/A 2012292SN/A if ui is not None: 2022292SN/A ui.readconfig(hgdir.File('hgrc').abspath) 2032292SN/A style_hook = ui.config('hooks', 'pretxncommit.style', None) 2042292SN/A 2052292SN/A if not style_hook: 2062292SN/A print mercurial_style_message 2072292SN/A sys.exit(1) 2082292SN/Aelse: 2092292SN/A print ".hg directory not found" 2102292SN/A 2112292SN/Amain['HG_INFO'] = hg_info 2122292SN/A 2132292SN/A################################################### 2142307SN/A# 2152307SN/A# Figure out which configurations to set up based on the path(s) of 2162307SN/A# the target(s). 2172307SN/A# 2182307SN/A################################################### 2192307SN/A 2202307SN/A# Find default configuration & binary. 2212307SN/ADefault(environ.get('M5_DEFAULT_BINARY', 'build/ALPHA_SE/m5.debug')) 2222307SN/A 2232307SN/A# helper function: find last occurrence of element in list 2242307SN/Adef rfind(l, elt, offs = -1): 2252307SN/A for i in range(len(l)+offs, 0, -1): 2262307SN/A if l[i] == elt: 2272307SN/A return i 2282307SN/A raise ValueError, "element not found" 2292307SN/A 2302307SN/A# Each target must have 'build' in the interior of the path; the 2312307SN/A# directory below this will determine the build parameters. For 2322292SN/A# example, for target 'foo/bar/build/ALPHA_SE/arch/alpha/blah.do' we 2332292SN/A# recognize that ALPHA_SE specifies the configuration because it 2342292SN/A# follow 'build' in the bulid path. 2352292SN/A 2362292SN/A# Generate absolute paths to targets so we can see where the build dir is 2372292SN/Aif COMMAND_LINE_TARGETS: 2382292SN/A # Ask SCons which directory it was invoked from 2392292SN/A launch_dir = GetLaunchDir() 2402292SN/A # Make targets relative to invocation directory 2412292SN/A abs_targets = [ normpath(joinpath(launch_dir, str(x))) for x in \ 2422292SN/A COMMAND_LINE_TARGETS] 2432292SN/Aelse: 2442292SN/A # Default targets are relative to root of tree 2452292SN/A abs_targets = [ normpath(joinpath(main.root.abspath, str(x))) for x in \ 2462292SN/A DEFAULT_TARGETS] 2473867Sbinkertn@umich.edu 2482292SN/A 2492292SN/A# Generate a list of the unique build roots and configs that the 2502292SN/A# collected targets reference. 2512292SN/Avariant_paths = [] 2522292SN/Abuild_root = None 2532292SN/Afor t in abs_targets: 2542292SN/A path_dirs = t.split('/') 2552292SN/A try: 2562292SN/A build_top = rfind(path_dirs, 'build', -2) 2572292SN/A except: 2582292SN/A print "Error: no non-leaf 'build' dir found on target path", t 2593867Sbinkertn@umich.edu Exit(1) 2603867Sbinkertn@umich.edu this_build_root = joinpath('/',*path_dirs[:build_top+1]) 2613867Sbinkertn@umich.edu if not build_root: 2623867Sbinkertn@umich.edu build_root = this_build_root 2633867Sbinkertn@umich.edu else: 2643867Sbinkertn@umich.edu if this_build_root != build_root: 2653867Sbinkertn@umich.edu print "Error: build targets not under same build root\n"\ 2662292SN/A " %s\n %s" % (build_root, this_build_root) 2672292SN/A Exit(1) 2682292SN/A variant_path = joinpath('/',*path_dirs[:build_top+2]) 2692292SN/A if variant_path not in variant_paths: 2702292SN/A variant_paths.append(variant_path) 2712292SN/A 2722292SN/A# Make sure build_root exists (might not if this is the first build there) 2732292SN/Aif not isdir(build_root): 2742292SN/A mkdir(build_root) 2752292SN/A 2762292SN/AExport('main') 2772292SN/A 2782292SN/Amain.SConsignFile(joinpath(build_root, "sconsign")) 2792292SN/A 2802292SN/A# Default duplicate option is to use hard links, but this messes up 2812292SN/A# when you use emacs to edit a file in the target dir, as emacs moves 2822292SN/A# file to file~ then copies to file, breaking the link. Symbolic 2832292SN/A# (soft) links work better. 2842292SN/Amain.SetOption('duplicate', 'soft-copy') 2852292SN/A 2862292SN/A# 2872292SN/A# Set up global sticky variables... these are common to an entire build 2882292SN/A# tree (not specific to a particular build like ALPHA_SE) 2892292SN/A# 2903867Sbinkertn@umich.edu 2913867Sbinkertn@umich.edu# Variable validators & converters for global sticky variables 2922292SN/Adef PathListMakeAbsolute(val): 2933867Sbinkertn@umich.edu if not val: 2943867Sbinkertn@umich.edu return val 2952292SN/A f = lambda p: abspath(expanduser(p)) 2962292SN/A return ':'.join(map(f, val.split(':'))) 2972292SN/A 2982292SN/Adef PathListAllExist(key, val, env): 2992292SN/A if not val: 3002292SN/A return 3012292SN/A paths = val.split(':') 3022292SN/A for path in paths: 3032292SN/A if not isdir(path): 3042292SN/A raise SCons.Errors.UserError("Path does not exist: '%s'" % path) 3052292SN/A 3062292SN/Aglobal_sticky_vars_file = joinpath(build_root, 'variables.global') 3072292SN/A 3082292SN/Aglobal_sticky_vars = Variables(global_sticky_vars_file, args=ARGUMENTS) 3092292SN/A 3102292SN/Aglobal_sticky_vars.AddVariables( 3112292SN/A ('CC', 'C compiler', environ.get('CC', main['CC'])), 3122292SN/A ('CXX', 'C++ compiler', environ.get('CXX', main['CXX'])), 3132292SN/A ('BATCH', 'Use batch pool for build and tests', False), 3142292SN/A ('BATCH_CMD', 'Batch pool submission command name', 'qdo'), 3152292SN/A ('EXTRAS', 'Add Extra directories to the compilation', '', 3162292SN/A PathListAllExist, PathListMakeAbsolute), 3172292SN/A BoolVariable('RUBY', 'Build with Ruby', False), 3182292SN/A ) 3192292SN/A 3202292SN/A# base help text 3212292SN/Ahelp_text = ''' 3222292SN/AUsage: scons [scons options] [build options] [target(s)] 3232292SN/A 3242292SN/AGlobal sticky options: 3252292SN/A''' 3262292SN/A 3272292SN/Ahelp_text += global_sticky_vars.GenerateHelpText(main) 3282292SN/A 3292292SN/A# Update main environment with values from ARGUMENTS & global_sticky_vars_file 3302292SN/Aglobal_sticky_vars.Update(main) 3312292SN/A 3322292SN/A# Save sticky variable settings back to current variables file 3332292SN/Aglobal_sticky_vars.Save(global_sticky_vars_file, main) 3342292SN/A 3352292SN/A# Parse EXTRAS variable to build list of all directories where we're 3362292SN/A# look for sources etc. This list is exported as base_dir_list. 3372292SN/Abase_dir = main.srcdir.abspath 3382292SN/Aif main['EXTRAS']: 3392292SN/A extras_dir_list = main['EXTRAS'].split(':') 3403867Sbinkertn@umich.eduelse: 3413867Sbinkertn@umich.edu extras_dir_list = [] 3422292SN/A 3433867Sbinkertn@umich.eduExport('base_dir') 3443867Sbinkertn@umich.eduExport('extras_dir_list') 3452292SN/A 3462292SN/A# the ext directory should be on the #includes path 3472329SN/Amain.Append(CPPPATH=[Dir('ext')]) 3482329SN/A 3492292SN/ACXX_version = readCommand([main['CXX'],'--version'], exception=False) 3502292SN/ACXX_V = readCommand([main['CXX'],'-V'], exception=False) 3512292SN/A 3522292SN/Amain['GCC'] = CXX_version and CXX_version.find('g++') >= 0 3532292SN/Amain['SUNCC'] = CXX_V and CXX_V.find('Sun C++') >= 0 3542292SN/Amain['ICC'] = CXX_V and CXX_V.find('Intel') >= 0 3552292SN/Aif main['GCC'] + main['SUNCC'] + main['ICC'] > 1: 3562292SN/A print 'Error: How can we have two at the same time?' 3572292SN/A Exit(1) 3582292SN/A 3592292SN/A# Set up default C++ compiler flags 3603867Sbinkertn@umich.eduif main['GCC']: 3613867Sbinkertn@umich.edu main.Append(CCFLAGS='-pipe') 3622292SN/A main.Append(CCFLAGS='-fno-strict-aliasing') 3633867Sbinkertn@umich.edu main.Append(CCFLAGS=['-Wall', '-Wno-sign-compare', '-Wundef']) 3643867Sbinkertn@umich.edu main.Append(CXXFLAGS='-Wno-deprecated') 3653867Sbinkertn@umich.eduelif main['ICC']: 3662292SN/A pass #Fix me... add warning flags once we clean up icc warnings 3672292SN/Aelif main['SUNCC']: 3682292SN/A main.Append(CCFLAGS='-Qoption ccfe') 3692292SN/A main.Append(CCFLAGS='-features=gcc') 3702292SN/A main.Append(CCFLAGS='-features=extensions') 3712292SN/A main.Append(CCFLAGS='-library=stlport4') 3722292SN/A main.Append(CCFLAGS='-xar') 3732292SN/A #main.Append(CCFLAGS='-instances=semiexplicit') 3742292SN/Aelse: 3752292SN/A print 'Error: Don\'t know what compiler options to use for your compiler.' 3762292SN/A print ' Please fix SConstruct and src/SConscript and try again.' 3772292SN/A Exit(1) 3782292SN/A 3793867Sbinkertn@umich.edu# Set up common yacc/bison flags (needed for Ruby) 3803867Sbinkertn@umich.edumain['YACCFLAGS'] = '-d' 3812292SN/Amain['YACCHXXFILESUFFIX'] = '.hh' 3823867Sbinkertn@umich.edu 3833867Sbinkertn@umich.edu# Do this after we save setting back, or else we'll tack on an 3843867Sbinkertn@umich.edu# extra 'qdo' every time we run scons. 3852292SN/Aif main['BATCH']: 3862292SN/A main['CC'] = main['BATCH_CMD'] + ' ' + main['CC'] 3872292SN/A main['CXX'] = main['BATCH_CMD'] + ' ' + main['CXX'] 3882292SN/A main['AS'] = main['BATCH_CMD'] + ' ' + main['AS'] 3892292SN/A main['AR'] = main['BATCH_CMD'] + ' ' + main['AR'] 3902292SN/A main['RANLIB'] = main['BATCH_CMD'] + ' ' + main['RANLIB'] 3912292SN/A 3922292SN/Aif sys.platform == 'cygwin': 3932292SN/A # cygwin has some header file issues... 3942292SN/A main.Append(CCFLAGS="-Wno-uninitialized") 3952292SN/A 3962292SN/A# Check for SWIG 3973867Sbinkertn@umich.eduif not main.has_key('SWIG'): 3983867Sbinkertn@umich.edu print 'Error: SWIG utility not found.' 3992292SN/A print ' Please install (see http://www.swig.org) and retry.' 4003867Sbinkertn@umich.edu Exit(1) 4013867Sbinkertn@umich.edu 4023867Sbinkertn@umich.edu# Check for appropriate SWIG version 4032292SN/Aswig_version = readCommand(('swig', '-version'), exception='').split() 4042292SN/A# First 3 words should be "SWIG Version x.y.z" 4052292SN/Aif len(swig_version) < 3 or \ 4062292SN/A swig_version[0] != 'SWIG' or swig_version[1] != 'Version': 4072292SN/A print 'Error determining SWIG version.' 4082292SN/A Exit(1) 4092292SN/A 4102292SN/Amin_swig_version = '1.3.28' 4112292SN/Aif compareVersions(swig_version[2], min_swig_version) < 0: 4122292SN/A print 'Error: SWIG version', min_swig_version, 'or newer required.' 4132292SN/A print ' Installed version:', swig_version[2] 4142292SN/A Exit(1) 4153867Sbinkertn@umich.edu 4163867Sbinkertn@umich.edu# Set up SWIG flags & scanner 4172292SN/Aswig_flags=Split('-c++ -python -modern -templatereduce $_CPPINCFLAGS') 4183867Sbinkertn@umich.edumain.Append(SWIGFLAGS=swig_flags) 4193867Sbinkertn@umich.edu 4203867Sbinkertn@umich.edu# filter out all existing swig scanners, they mess up the dependency 4212292SN/A# stuff for some reason 4222292SN/Ascanners = [] 4232292SN/Afor scanner in main['SCANNERS']: 4242292SN/A skeys = scanner.skeys 4252292SN/A if skeys == '.i': 4262292SN/A continue 4272292SN/A 4282292SN/A if isinstance(skeys, (list, tuple)) and '.i' in skeys: 4292292SN/A continue 4302292SN/A 4312292SN/A scanners.append(scanner) 4322292SN/A 4333867Sbinkertn@umich.edu# add the new swig scanner that we like better 4343867Sbinkertn@umich.edufrom SCons.Scanner import ClassicCPP as CPPScanner 4352292SN/Aswig_inc_re = '^[ \t]*[%,#][ \t]*(?:include|import)[ \t]*(<|")([^>"]+)(>|")' 4363867Sbinkertn@umich.eduscanners.append(CPPScanner("SwigScan", [ ".i" ], "CPPPATH", swig_inc_re)) 4373867Sbinkertn@umich.edu 4383867Sbinkertn@umich.edu# replace the scanners list that has what we want 4392292SN/Amain['SCANNERS'] = scanners 4402292SN/A 4412292SN/A# Add a custom Check function to the Configure context so that we can 4422292SN/A# figure out if the compiler adds leading underscores to global 4432292SN/A# variables. This is needed for the autogenerated asm files that we 4442292SN/A# use for embedding the python code. 4452292SN/Adef CheckLeading(context): 4462292SN/A context.Message("Checking for leading underscore in global variables...") 4472292SN/A # 1) Define a global variable called x from asm so the C compiler 4482292SN/A # won't change the symbol at all. 4492292SN/A # 2) Declare that variable. 4502292SN/A # 3) Use the variable 4513867Sbinkertn@umich.edu # 4523867Sbinkertn@umich.edu # If the compiler prepends an underscore, this will successfully 4532292SN/A # link because the external symbol 'x' will be called '_x' which 4543867Sbinkertn@umich.edu # was defined by the asm statement. If the compiler does not 4553867Sbinkertn@umich.edu # prepend an underscore, this will not successfully link because 4563867Sbinkertn@umich.edu # '_x' will have been defined by assembly, while the C portion of 4572292SN/A # the code will be trying to use 'x' 4582292SN/A ret = context.TryLink(''' 4592292SN/A asm(".globl _x; _x: .byte 0"); 4602292SN/A extern int x; 4612292SN/A int main() { return x; } 4622292SN/A ''', extension=".c") 4632292SN/A context.env.Append(LEADING_UNDERSCORE=ret) 4642292SN/A context.Result(ret) 4652292SN/A return ret 4662292SN/A 4672292SN/A# Platform-specific configuration. Note again that we assume that all 4682292SN/A# builds under a given build root run on the same host platform. 4692292SN/Aconf = Configure(main, 4702292SN/A conf_dir = joinpath(build_root, '.scons_config'), 4712292SN/A log_file = joinpath(build_root, 'scons_config.log'), 4722292SN/A custom_tests = { 'CheckLeading' : CheckLeading }) 4732292SN/A 4742292SN/A# Check for leading underscores. Don't really need to worry either 4752292SN/A# way so don't need to check the return code. 4762292SN/Aconf.CheckLeading() 4773867Sbinkertn@umich.edu 4783867Sbinkertn@umich.edu# Check if we should compile a 64 bit binary on Mac OS X/Darwin 4792292SN/Atry: 4803867Sbinkertn@umich.edu import platform 4813867Sbinkertn@umich.edu uname = platform.uname() 4823867Sbinkertn@umich.edu if uname[0] == 'Darwin' and compareVersions(uname[2], '9.0.0') >= 0: 4833867Sbinkertn@umich.edu if int(readCommand('sysctl -n hw.cpu64bit_capable')[0]): 4842292SN/A main.Append(CCFLAGS='-arch x86_64') 4852292SN/A main.Append(CFLAGS='-arch x86_64') 4862292SN/A main.Append(LINKFLAGS='-arch x86_64') 4872292SN/A main.Append(ASFLAGS='-arch x86_64') 4882292SN/Aexcept: 4892292SN/A pass 4902292SN/A 4912292SN/A# Recent versions of scons substitute a "Null" object for Configure() 4922292SN/A# when configuration isn't necessary, e.g., if the "--help" option is 4932292SN/A# present. Unfortuantely this Null object always returns false, 4942292SN/A# breaking all our configuration checks. We replace it with our own 4952292SN/A# more optimistic null object that returns True instead. 4963867Sbinkertn@umich.eduif not conf: 4972292SN/A def NullCheck(*args, **kwargs): 4982292SN/A return True 4992292SN/A 5002292SN/A class NullConf: 5012292SN/A def __init__(self, env): 5022292SN/A self.env = env 5032292SN/A def Finish(self): 5042292SN/A return self.env 5052292SN/A def __getattr__(self, mname): 5063867Sbinkertn@umich.edu return NullCheck 5073867Sbinkertn@umich.edu 5082292SN/A conf = NullConf(main) 5093867Sbinkertn@umich.edu 5103867Sbinkertn@umich.edu# Find Python include and library directories for embedding the 5113867Sbinkertn@umich.edu# interpreter. For consistency, we will use the same Python 5122292SN/A# installation used to run scons (and thus this script). If you want 5132292SN/A# to link in an alternate version, see above for instructions on how 5142292SN/A# to invoke scons with a different copy of the Python interpreter. 5152292SN/Afrom distutils import sysconfig 5162292SN/A 5172292SN/Apy_getvar = sysconfig.get_config_var 5182292SN/A 5192292SN/Apy_version = 'python' + py_getvar('VERSION') 5202292SN/A 5212292SN/Apy_general_include = sysconfig.get_python_inc() 5222292SN/Apy_platform_include = sysconfig.get_python_inc(plat_specific=True) 5232292SN/Apy_includes = [ py_general_include ] 5242292SN/Aif py_platform_include != py_general_include: 5252292SN/A py_includes.append(py_platform_include) 5262292SN/A 5272292SN/Apy_lib_path = [ py_getvar('LIBDIR') ] 5282292SN/A# add the prefix/lib/pythonX.Y/config dir, but only if there is no 5292292SN/A# shared library in prefix/lib/. 5302292SN/Aif not py_getvar('Py_ENABLE_SHARED'): 5312292SN/A py_lib_path.append(py_getvar('LIBPL')) 5322292SN/A 5332292SN/Apy_libs = [] 5342292SN/Afor lib in py_getvar('LIBS').split() + py_getvar('SYSLIBS').split(): 5353867Sbinkertn@umich.edu assert lib.startswith('-l') 5363867Sbinkertn@umich.edu lib = lib[2:] 5372292SN/A if lib not in py_libs: 5383867Sbinkertn@umich.edu py_libs.append(lib) 5393867Sbinkertn@umich.edupy_libs.append(py_version) 5403867Sbinkertn@umich.edu 5412292SN/Amain.Append(CPPPATH=py_includes) 5422292SN/Amain.Append(LIBPATH=py_lib_path) 5432292SN/A 5442292SN/A# verify that this stuff works 5452292SN/Aif not conf.CheckHeader('Python.h', '<>'): 5462292SN/A print "Error: can't find Python.h header in", py_includes 5472292SN/A Exit(1) 5482292SN/A 5492292SN/Afor lib in py_libs: 5502292SN/A if not conf.CheckLib(lib): 5512292SN/A print "Error: can't find library %s required by python" % lib 5522292SN/A Exit(1) 5532292SN/A 5542292SN/A# On Solaris you need to use libsocket for socket ops 5552292SN/Aif not conf.CheckLibWithHeader(None, 'sys/socket.h', 'C++', 'accept(0,0,0);'): 5562292SN/A if not conf.CheckLibWithHeader('socket', 'sys/socket.h', 'C++', 'accept(0,0,0);'): 5572292SN/A print "Can't find library with socket calls (e.g. accept())" 5582292SN/A Exit(1) 5592292SN/A 5602292SN/A# Check for zlib. If the check passes, libz will be automatically 5612292SN/A# added to the LIBS environment variable. 5622292SN/Aif not conf.CheckLibWithHeader('z', 'zlib.h', 'C++','zlibVersion();'): 5632292SN/A print 'Error: did not find needed zlib compression library '\ 5643867Sbinkertn@umich.edu 'and/or zlib.h header file.' 5653867Sbinkertn@umich.edu print ' Please install zlib and try again.' 5662292SN/A Exit(1) 5673867Sbinkertn@umich.edu 5683867Sbinkertn@umich.edu# Check for <fenv.h> (C99 FP environment control) 5693867Sbinkertn@umich.eduhave_fenv = conf.CheckHeader('fenv.h', '<>') 5702292SN/Aif not have_fenv: 5712292SN/A print "Warning: Header file <fenv.h> not found." 5722292SN/A print " This host has no IEEE FP rounding mode control." 5732292SN/A 5742292SN/A###################################################################### 5752292SN/A# 5762292SN/A# Check for mysql. 5772292SN/A# 5782292SN/Amysql_config = WhereIs('mysql_config') 5792292SN/Ahave_mysql = bool(mysql_config) 5802292SN/A 5812292SN/A# Check MySQL version. 5822292SN/Aif have_mysql: 5832292SN/A mysql_version = readCommand(mysql_config + ' --version') 5842292SN/A min_mysql_version = '4.1' 5852292SN/A if compareVersions(mysql_version, min_mysql_version) < 0: 5862292SN/A print 'Warning: MySQL', min_mysql_version, 'or newer required.' 5872292SN/A print ' Version', mysql_version, 'detected.' 5882292SN/A have_mysql = False 5892292SN/A 5902292SN/A# Set up mysql_config commands. 5913867Sbinkertn@umich.eduif have_mysql: 5923867Sbinkertn@umich.edu mysql_config_include = mysql_config + ' --include' 5932292SN/A if os.system(mysql_config_include + ' > /dev/null') != 0: 5943867Sbinkertn@umich.edu # older mysql_config versions don't support --include, use 5952864Sktlim@umich.edu # --cflags instead 5962864Sktlim@umich.edu mysql_config_include = mysql_config + ' --cflags | sed s/\\\'//g' 5973867Sbinkertn@umich.edu # This seems to work in all versions 5983867Sbinkertn@umich.edu mysql_config_libs = mysql_config + ' --libs' 5993867Sbinkertn@umich.edu 6002292SN/A###################################################################### 6012292SN/A# 6022292SN/A# Finish the configuration 6032292SN/A# 6042292SN/Amain = conf.Finish() 6052292SN/A 6062292SN/A###################################################################### 6072292SN/A# 6082292SN/A# Collect all non-global variables 6092292SN/A# 6102292SN/A 6113867Sbinkertn@umich.edu# Define the universe of supported ISAs 6123867Sbinkertn@umich.eduall_isa_list = [ ] 6132292SN/AExport('all_isa_list') 6143867Sbinkertn@umich.edu 6153867Sbinkertn@umich.edu# Define the universe of supported CPU models 6163867Sbinkertn@umich.eduall_cpu_list = [ ] 6172292SN/Adefault_cpus = [ ] 6182292SN/AExport('all_cpu_list', 'default_cpus') 6192292SN/A 6202292SN/A# Sticky variables get saved in the variables file so they persist from 6212292SN/A# one invocation to the next (unless overridden, in which case the new 6222292SN/A# value becomes sticky). 6232292SN/Asticky_vars = Variables(args=ARGUMENTS) 6242292SN/AExport('sticky_vars') 6252292SN/A 6262292SN/A# Sticky variables that should be exported 6272292SN/Aexport_vars = [] 6283867Sbinkertn@umich.eduExport('export_vars') 6293867Sbinkertn@umich.edu 6302292SN/A# Non-sticky variables only apply to the current build. 6313867Sbinkertn@umich.edunonsticky_vars = Variables(args=ARGUMENTS) 6323867Sbinkertn@umich.eduExport('nonsticky_vars') 6333867Sbinkertn@umich.edu 6342292SN/A# Walk the tree and execute all SConsopts scripts that wil add to the 6352292SN/A# above variables 6362292SN/Afor bdir in [ base_dir ] + extras_dir_list: 637 for root, dirs, files in os.walk(bdir): 638 if 'SConsopts' in files: 639 print "Reading", joinpath(root, 'SConsopts') 640 SConscript(joinpath(root, 'SConsopts')) 641 642all_isa_list.sort() 643all_cpu_list.sort() 644default_cpus.sort() 645 646sticky_vars.AddVariables( 647 EnumVariable('TARGET_ISA', 'Target ISA', 'alpha', all_isa_list), 648 BoolVariable('FULL_SYSTEM', 'Full-system support', False), 649 ListVariable('CPU_MODELS', 'CPU models', default_cpus, all_cpu_list), 650 BoolVariable('NO_FAST_ALLOC', 'Disable fast object allocator', False), 651 BoolVariable('FAST_ALLOC_DEBUG', 'Enable fast object allocator debugging', 652 False), 653 BoolVariable('FAST_ALLOC_STATS', 'Enable fast object allocator statistics', 654 False), 655 BoolVariable('EFENCE', 'Link with Electric Fence malloc debugger', 656 False), 657 BoolVariable('SS_COMPATIBLE_FP', 658 'Make floating-point results compatible with SimpleScalar', 659 False), 660 BoolVariable('USE_SSE2', 661 'Compile for SSE2 (-msse2) to get IEEE FP on x86 hosts', 662 False), 663 BoolVariable('USE_MYSQL', 'Use MySQL for stats output', have_mysql), 664 BoolVariable('USE_FENV', 'Use <fenv.h> IEEE mode control', have_fenv), 665 BoolVariable('USE_CHECKER', 'Use checker for detailed CPU models', False), 666 BoolVariable('CP_ANNOTATE', 'Enable critical path annotation capability', False), 667 ) 668 669nonsticky_vars.AddVariables( 670 BoolVariable('update_ref', 'Update test reference outputs', False) 671 ) 672 673# These variables get exported to #defines in config/*.hh (see src/SConscript). 674export_vars += ['FULL_SYSTEM', 'USE_FENV', 'USE_MYSQL', 675 'NO_FAST_ALLOC', 'FAST_ALLOC_DEBUG', 'FAST_ALLOC_STATS', 676 'SS_COMPATIBLE_FP', 'USE_CHECKER', 'TARGET_ISA', 'CP_ANNOTATE'] 677 678################################################### 679# 680# Define a SCons builder for configuration flag headers. 681# 682################################################### 683 684# This function generates a config header file that #defines the 685# variable symbol to the current variable setting (0 or 1). The source 686# operands are the name of the variable and a Value node containing the 687# value of the variable. 688def build_config_file(target, source, env): 689 (variable, value) = [s.get_contents() for s in source] 690 f = file(str(target[0]), 'w') 691 print >> f, '#define', variable, value 692 f.close() 693 return None 694 695# Generate the message to be printed when building the config file. 696def build_config_file_string(target, source, env): 697 (variable, value) = [s.get_contents() for s in source] 698 return "Defining %s as %s in %s." % (variable, value, target[0]) 699 700# Combine the two functions into a scons Action object. 701config_action = Action(build_config_file, build_config_file_string) 702 703# The emitter munges the source & target node lists to reflect what 704# we're really doing. 705def config_emitter(target, source, env): 706 # extract variable name from Builder arg 707 variable = str(target[0]) 708 # True target is config header file 709 target = joinpath('config', variable.lower() + '.hh') 710 val = env[variable] 711 if isinstance(val, bool): 712 # Force value to 0/1 713 val = int(val) 714 elif isinstance(val, str): 715 val = '"' + val + '"' 716 717 # Sources are variable name & value (packaged in SCons Value nodes) 718 return ([target], [Value(variable), Value(val)]) 719 720config_builder = Builder(emitter = config_emitter, action = config_action) 721 722main.Append(BUILDERS = { 'ConfigFile' : config_builder }) 723 724# libelf build is shared across all configs in the build root. 725main.SConscript('ext/libelf/SConscript', 726 variant_dir = joinpath(build_root, 'libelf')) 727 728# gzstream build is shared across all configs in the build root. 729main.SConscript('ext/gzstream/SConscript', 730 variant_dir = joinpath(build_root, 'gzstream')) 731 732################################################### 733# 734# This function is used to set up a directory with switching headers 735# 736################################################### 737 738main['ALL_ISA_LIST'] = all_isa_list 739def make_switching_dir(dname, switch_headers, env): 740 # Generate the header. target[0] is the full path of the output 741 # header to generate. 'source' is a dummy variable, since we get the 742 # list of ISAs from env['ALL_ISA_LIST']. 743 def gen_switch_hdr(target, source, env): 744 fname = str(target[0]) 745 bname = basename(fname) 746 f = open(fname, 'w') 747 f.write('#include "arch/isa_specific.hh"\n') 748 cond = '#if' 749 for isa in all_isa_list: 750 f.write('%s THE_ISA == %s_ISA\n#include "%s/%s/%s"\n' 751 % (cond, isa.upper(), dname, isa, bname)) 752 cond = '#elif' 753 f.write('#else\n#error "THE_ISA not set"\n#endif\n') 754 f.close() 755 return 0 756 757 # String to print when generating header 758 def gen_switch_hdr_string(target, source, env): 759 return "Generating switch header " + str(target[0]) 760 761 # Build SCons Action object. 'varlist' specifies env vars that this 762 # action depends on; when env['ALL_ISA_LIST'] changes these actions 763 # should get re-executed. 764 switch_hdr_action = Action(gen_switch_hdr, gen_switch_hdr_string, 765 varlist=['ALL_ISA_LIST']) 766 767 # Instantiate actions for each header 768 for hdr in switch_headers: 769 env.Command(hdr, [], switch_hdr_action) 770Export('make_switching_dir') 771 772################################################### 773# 774# Define build environments for selected configurations. 775# 776################################################### 777 778for variant_path in variant_paths: 779 print "Building in", variant_path 780 781 # Make a copy of the build-root environment to use for this config. 782 env = main.Clone() 783 env['BUILDDIR'] = variant_path 784 785 # variant_dir is the tail component of build path, and is used to 786 # determine the build parameters (e.g., 'ALPHA_SE') 787 (build_root, variant_dir) = splitpath(variant_path) 788 789 # Set env variables according to the build directory config. 790 sticky_vars.files = [] 791 # Variables for $BUILD_ROOT/$VARIANT_DIR are stored in 792 # $BUILD_ROOT/variables/$VARIANT_DIR so you can nuke 793 # $BUILD_ROOT/$VARIANT_DIR without losing your variables settings. 794 current_vars_file = joinpath(build_root, 'variables', variant_dir) 795 if isfile(current_vars_file): 796 sticky_vars.files.append(current_vars_file) 797 print "Using saved variables file %s" % current_vars_file 798 else: 799 # Build dir-specific variables file doesn't exist. 800 801 # Make sure the directory is there so we can create it later 802 opt_dir = dirname(current_vars_file) 803 if not isdir(opt_dir): 804 mkdir(opt_dir) 805 806 # Get default build variables from source tree. Variables are 807 # normally determined by name of $VARIANT_DIR, but can be 808 # overriden by 'default=' arg on command line. 809 default_vars_file = joinpath('build_opts', 810 ARGUMENTS.get('default', variant_dir)) 811 if isfile(default_vars_file): 812 sticky_vars.files.append(default_vars_file) 813 print "Variables file %s not found,\n using defaults in %s" \ 814 % (current_vars_file, default_vars_file) 815 else: 816 print "Error: cannot find variables file %s or %s" \ 817 % (current_vars_file, default_vars_file) 818 Exit(1) 819 820 # Apply current variable settings to env 821 sticky_vars.Update(env) 822 nonsticky_vars.Update(env) 823 824 help_text += "\nSticky variables for %s:\n" % variant_dir \ 825 + sticky_vars.GenerateHelpText(env) \ 826 + "\nNon-sticky variables for %s:\n" % variant_dir \ 827 + nonsticky_vars.GenerateHelpText(env) 828 829 # Process variable settings. 830 831 if not have_fenv and env['USE_FENV']: 832 print "Warning: <fenv.h> not available; " \ 833 "forcing USE_FENV to False in", variant_dir + "." 834 env['USE_FENV'] = False 835 836 if not env['USE_FENV']: 837 print "Warning: No IEEE FP rounding mode control in", variant_dir + "." 838 print " FP results may deviate slightly from other platforms." 839 840 if env['EFENCE']: 841 env.Append(LIBS=['efence']) 842 843 if env['USE_MYSQL']: 844 if not have_mysql: 845 print "Warning: MySQL not available; " \ 846 "forcing USE_MYSQL to False in", variant_dir + "." 847 env['USE_MYSQL'] = False 848 else: 849 print "Compiling in", variant_dir, "with MySQL support." 850 env.ParseConfig(mysql_config_libs) 851 env.ParseConfig(mysql_config_include) 852 853 # Save sticky variable settings back to current variables file 854 sticky_vars.Save(current_vars_file, env) 855 856 if env['USE_SSE2']: 857 env.Append(CCFLAGS='-msse2') 858 859 # The src/SConscript file sets up the build rules in 'env' according 860 # to the configured variables. It returns a list of environments, 861 # one for each variant build (debug, opt, etc.) 862 envList = SConscript('src/SConscript', variant_dir = variant_path, 863 exports = 'env') 864 865 # Set up the regression tests for each build. 866 for e in envList: 867 SConscript('tests/SConscript', 868 variant_dir = joinpath(variant_path, 'tests', e.Label), 869 exports = { 'env' : e }, duplicate = False) 870 871Help(help_text) 872