SConstruct revision 2638:b419cc6a4419
12SN/A# -*- mode:python -*-
21762SN/A
32SN/A# Copyright (c) 2004-2005 The Regents of The University of Michigan
42SN/A# All rights reserved.
52SN/A#
62SN/A# Redistribution and use in source and binary forms, with or without
72SN/A# modification, are permitted provided that the following conditions are
82SN/A# met: redistributions of source code must retain the above copyright
92SN/A# notice, this list of conditions and the following disclaimer;
102SN/A# redistributions in binary form must reproduce the above copyright
112SN/A# notice, this list of conditions and the following disclaimer in the
122SN/A# documentation and/or other materials provided with the distribution;
132SN/A# neither the name of the copyright holders nor the names of its
142SN/A# contributors may be used to endorse or promote products derived from
152SN/A# this software without specific prior written permission.
162SN/A#
172SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
182SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
192SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
202SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
212SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
222SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
232SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
242SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
252SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
262SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
272665Ssaidi@eecs.umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
282665Ssaidi@eecs.umich.edu
292665Ssaidi@eecs.umich.edu###################################################
302SN/A#
312SN/A# SCons top-level build description (SConstruct) file.
325569Snate@binkert.org#
335569Snate@binkert.org# While in this directory ('m5'), just type 'scons' to build the default
342SN/A# configuration (see below), or type 'scons build/<CONFIG>/<binary>'
355569Snate@binkert.org# to build some other configuration (e.g., 'build/ALPHA_FS/m5.opt' for
363614Sgblack@eecs.umich.edu# the optimized full-system version).
377678Sgblack@eecs.umich.edu#
383614Sgblack@eecs.umich.edu# You can build M5 in a different directory as long as there is a
393614Sgblack@eecs.umich.edu# 'build/<CONFIG>' somewhere along the target path.  The build system
402166SN/A# expdects that all configs under the same build directory are being
412147SN/A# built for the same host system.
425569Snate@binkert.org#
432167SN/A# Examples:
442147SN/A#   These two commands are equivalent.  The '-u' option tells scons to
452090SN/A#   search up the directory tree for this SConstruct file.
462222SN/A#   % cd <path-to-src>/m5 ; scons build/ALPHA_FS/m5.debug
472090SN/A#   % cd <path-to-src>/m5/build/ALPHA_FS; scons -u m5.debug
482201SN/A#   These two commands are equivalent and demonstrate building in a
492201SN/A#   directory outside of the source tree.  The '-C' option tells scons
502201SN/A#   to chdir to the specified directory to find this SConstruct file.
512112SN/A#   % cd <path-to-src>/m5 ; scons /local/foo/build/ALPHA_FS/m5.debug
522174SN/A#   % cd /local/foo/build/ALPHA_FS; scons -C <path-to-src>/m5 m5.debug
537678Sgblack@eecs.umich.edu#
547678Sgblack@eecs.umich.edu# You can use 'scons -H' to print scons options.  If you're in this
552174SN/A# 'm5' directory (or use -u or -C to tell scons where to find this
562175SN/A# file), you can use 'scons -h' to print all the M5-specific build
572222SN/A# options as well.
582SN/A#
592SN/A###################################################
602203SN/A
612166SN/A# Python library imports
622166SN/Aimport sys
632203SN/Aimport os
642166SN/A
652222SN/A# Check for recent-enough Python and SCons versions
665569Snate@binkert.orgEnsurePythonVersion(2,3)
672166SN/AEnsureSConsVersion(0,96,91)
684695Sgblack@eecs.umich.edu
692166SN/A# The absolute path to the current directory (where this file lives).
702222SN/AROOT = Dir('.').abspath
714695Sgblack@eecs.umich.edu
722166SN/A# Paths to the M5 and external source trees.
732166SN/ASRCDIR = os.path.join(ROOT, 'src')
742203SN/A
752166SN/A# tell python where to find m5 python code
762166SN/Asys.path.append(os.path.join(ROOT, 'src/python'))
772203SN/A
782166SN/A###################################################
792222SN/A#
805569Snate@binkert.org# Figure out which configurations to set up based on the path(s) of
812166SN/A# the target(s).
824695Sgblack@eecs.umich.edu#
832166SN/A###################################################
842222SN/A
854695Sgblack@eecs.umich.edu# Find default configuration & binary.
862166SN/ADefault(os.environ.get('M5_DEFAULT_BINARY', 'build/ALPHA_SE/m5.debug'))
872166SN/A
882166SN/A# Ask SCons which directory it was invoked from.
892166SN/Alaunch_dir = GetLaunchDir()
902203SN/A
912166SN/A# Make targets relative to invocation directory
922166SN/Aabs_targets = map(lambda x: os.path.normpath(os.path.join(launch_dir, str(x))),
932147SN/A                  BUILD_TARGETS)
942090SN/A
952147SN/A# helper function: find last occurrence of element in list
962147SN/Adef rfind(l, elt, offs = -1):
972147SN/A    for i in range(len(l)+offs, 0, -1):
982222SN/A        if l[i] == elt:
995569Snate@binkert.org            return i
1002112SN/A    raise ValueError, "element not found"
1014695Sgblack@eecs.umich.edu
1022147SN/A# Each target must have 'build' in the interior of the path; the
1032222SN/A# directory below this will determine the build parameters.  For
1042147SN/A# example, for target 'foo/bar/build/ALPHA_SE/arch/alpha/blah.do' we
1052090SN/A# recognize that ALPHA_SE specifies the configuration because it
1062147SN/A# follow 'build' in the bulid path.
1072090SN/A
1082147SN/A# Generate a list of the unique build roots and configs that the
1092147SN/A# collected targets reference.
1102147SN/Abuild_paths = []
1112222SN/Abuild_root = None
1125569Snate@binkert.orgfor t in abs_targets:
1135569Snate@binkert.org    path_dirs = t.split('/')
1145569Snate@binkert.org    try:
1155569Snate@binkert.org        build_top = rfind(path_dirs, 'build', -2)
1162112SN/A    except:
1174695Sgblack@eecs.umich.edu        print "Error: no non-leaf 'build' dir found on target path", t
1182147SN/A        Exit(1)
1192222SN/A    this_build_root = os.path.join('/',*path_dirs[:build_top+1])
1202203SN/A    if not build_root:
1217678Sgblack@eecs.umich.edu        build_root = this_build_root
1227678Sgblack@eecs.umich.edu    else:
1232203SN/A        if this_build_root != build_root:
1242147SN/A            print "Error: build targets not under same build root\n"\
1252090SN/A                  "  %s\n  %s" % (build_root, this_build_root)
1262147SN/A            Exit(1)
1272090SN/A    build_path = os.path.join('/',*path_dirs[:build_top+2])
1282147SN/A    if build_path not in build_paths:
1292147SN/A        build_paths.append(build_path)
1302147SN/A
1312222SN/A###################################################
1325569Snate@binkert.org#
1335569Snate@binkert.org# Set up the default build environment.  This environment is copied
1345569Snate@binkert.org# and modified according to each selected configuration.
1355569Snate@binkert.org#
1362112SN/A###################################################
1374695Sgblack@eecs.umich.edu
1382147SN/Aenv = Environment(ENV = os.environ,  # inherit user's environment vars
1392222SN/A                  ROOT = ROOT,
1402147SN/A                  SRCDIR = SRCDIR)
1412090SN/A
1422502SN/Aenv.SConsignFile("sconsign")
1432502SN/A
1444997Sgblack@eecs.umich.edu# I waffle on this setting... it does avoid a few painful but
1455568Snate@binkert.org# unnecessary builds, but it also seems to make trivial builds take
1465736Snate@binkert.org# noticeably longer.
1472502SN/Aif False:
1485569Snate@binkert.org    env.TargetSignatures('content')
1492502SN/A
1505736Snate@binkert.org# M5_PLY is used by isa_parser.py to find the PLY package.
1512502SN/Aenv.Append(ENV = { 'M5_PLY' : Dir('ext/ply') })
1522502SN/A
1534695Sgblack@eecs.umich.edu# Set up default C++ compiler flags
1542502SN/Aenv.Append(CCFLAGS='-pipe')
1552502SN/Aenv.Append(CCFLAGS='-fno-strict-aliasing')
1562502SN/Aenv.Append(CCFLAGS=Split('-Wall -Wno-sign-compare -Werror -Wundef'))
1577678Sgblack@eecs.umich.eduif sys.platform == 'cygwin':
1587678Sgblack@eecs.umich.edu    # cygwin has some header file issues...
1592502SN/A    env.Append(CCFLAGS=Split("-Wno-uninitialized"))
1602502SN/Aenv.Append(CPPPATH=[Dir('ext/dnet')])
1612502SN/A
1622502SN/A# Default libraries
1632090SN/Aenv.Append(LIBS=['z'])
1642147SN/A
1652147SN/A# Platform-specific configuration.  Note again that we assume that all
1662147SN/A# builds under a given build root run on the same host platform.
1672222SN/Aconf = Configure(env,
1685569Snate@binkert.org                 conf_dir = os.path.join(build_root, '.scons_config'),
1692112SN/A                 log_file = os.path.join(build_root, 'scons_config.log'))
1705736Snate@binkert.org
1712502SN/A# Check for <fenv.h> (C99 FP environment control)
1722502SN/Ahave_fenv = conf.CheckHeader('fenv.h', '<>')
1734695Sgblack@eecs.umich.eduif not have_fenv:
1742147SN/A    print "Warning: Header file <fenv.h> not found."
1752222SN/A    print "         This host has no IEEE FP rounding mode control."
1764997Sgblack@eecs.umich.edu
1777678Sgblack@eecs.umich.edu# Check for mysql.
1787678Sgblack@eecs.umich.edumysql_config = WhereIs('mysql_config')
1794997Sgblack@eecs.umich.eduhave_mysql = mysql_config != None
1802147SN/A
1812090SN/A# Check MySQL version.
1822502SN/Aif have_mysql:
1832090SN/A    mysql_version = os.popen(mysql_config + ' --version').read()
1842147SN/A    mysql_version = mysql_version.split('.')
1852147SN/A    mysql_major = int(mysql_version[0])
1862147SN/A    mysql_minor = int(mysql_version[1])
1872222SN/A    # This version check is probably overly conservative, but it deals
1885569Snate@binkert.org    # with the versions we have installed.
1892112SN/A    if mysql_major < 4 or (mysql_major == 4 and mysql_minor < 1):
1905736Snate@binkert.org        print "Warning: MySQL v4.1 or newer required."
1912502SN/A        have_mysql = False
1922502SN/A
1934695Sgblack@eecs.umich.edu# Set up mysql_config commands.
1942147SN/Aif have_mysql:
1952222SN/A    mysql_config_include = mysql_config + ' --include'
1962147SN/A    if os.system(mysql_config_include + ' > /dev/null') != 0:
1972090SN/A        # older mysql_config versions don't support --include, use
1982502SN/A        # --cflags instead
1992090SN/A        mysql_config_include = mysql_config + ' --cflags | sed s/\\\'//g'
2002147SN/A    # This seems to work in all versions
2012147SN/A    mysql_config_libs = mysql_config + ' --libs'
2022147SN/A
2032222SN/Aenv = conf.Finish()
2045569Snate@binkert.org
2052112SN/A# Define the universe of supported ISAs
2065736Snate@binkert.orgenv['ALL_ISA_LIST'] = ['alpha', 'sparc', 'mips']
2072502SN/A
2082502SN/A# Define the universe of supported CPU models
2094695Sgblack@eecs.umich.eduenv['ALL_CPU_LIST'] = ['AtomicSimpleCPU', 'TimingSimpleCPU',
2102147SN/A                       'FullCPU', 'AlphaFullCPU']
2112222SN/A
2122147SN/A# Sticky options get saved in the options file so they persist from
2132090SN/A# one invocation to the next (unless overridden, in which case the new
2142502SN/A# value becomes sticky).
2152090SN/Asticky_opts = Options(args=ARGUMENTS)
2162147SN/Asticky_opts.AddOptions(
2172147SN/A    EnumOption('TARGET_ISA', 'Target ISA', 'alpha', env['ALL_ISA_LIST']),
2182147SN/A    BoolOption('FULL_SYSTEM', 'Full-system support', False),
2192222SN/A    # There's a bug in scons 0.96.1 that causes ListOptions with list
2205569Snate@binkert.org    # values (more than one value) not to be able to be restored from
2212112SN/A    # a saved option file.  If this causes trouble then upgrade to
2225736Snate@binkert.org    # scons 0.96.90 or later.
2232502SN/A    ListOption('CPU_MODELS', 'CPU models', 'AtomicSimpleCPU,TimingSimpleCPU',
2242502SN/A               env['ALL_CPU_LIST']),
2254695Sgblack@eecs.umich.edu    BoolOption('ALPHA_TLASER',
2262147SN/A               'Model Alpha TurboLaser platform (vs. Tsunami)', False),
2272222SN/A    BoolOption('NO_FAST_ALLOC', 'Disable fast object allocator', False),
2282147SN/A    BoolOption('EFENCE', 'Link with Electric Fence malloc debugger',
2292090SN/A               False),
2302502SN/A    BoolOption('SS_COMPATIBLE_FP',
2312090SN/A               'Make floating-point results compatible with SimpleScalar',
2322147SN/A               False),
2332147SN/A    BoolOption('USE_SSE2',
2342147SN/A               'Compile for SSE2 (-msse2) to get IEEE FP on x86 hosts',
2352222SN/A               False),
2365569Snate@binkert.org    BoolOption('STATS_BINNING', 'Bin statistics by CPU mode', have_mysql),
2372112SN/A    BoolOption('USE_MYSQL', 'Use MySQL for stats output', have_mysql),
2385736Snate@binkert.org    BoolOption('USE_FENV', 'Use <fenv.h> IEEE mode control', have_fenv),
2392502SN/A    ('CC', 'C compiler', os.environ.get('CC', env['CC'])),
2402502SN/A    ('CXX', 'C++ compiler', os.environ.get('CXX', env['CXX'])),
2414695Sgblack@eecs.umich.edu    BoolOption('BATCH', 'Use batch pool for build and tests', False),
2422147SN/A    ('BATCH_CMD', 'Batch pool submission command name', 'qdo')
2432222SN/A    )
2442147SN/A
2452090SN/A# Non-sticky options only apply to the current build.
2462502SN/Anonsticky_opts = Options(args=ARGUMENTS)
2472502SN/Anonsticky_opts.AddOptions(
2484997Sgblack@eecs.umich.edu    BoolOption('update_ref', 'Update test reference outputs', False)
2492502SN/A    )
2505569Snate@binkert.org
2512502SN/A# These options get exported to #defines in config/*.hh (see m5/SConscript).
2525569Snate@binkert.orgenv.ExportOptions = ['FULL_SYSTEM', 'ALPHA_TLASER', 'USE_FENV', \
2534695Sgblack@eecs.umich.edu                     'USE_MYSQL', 'NO_FAST_ALLOC', 'SS_COMPATIBLE_FP', \
2542505SN/A                     'STATS_BINNING']
2552505SN/A
2562502SN/A# Define a handy 'no-op' action
2577678Sgblack@eecs.umich.edudef no_action(target, source, env):
2587678Sgblack@eecs.umich.edu    return 0
2592502SN/A
2602502SN/Aenv.NoAction = Action(no_action, None)
2612502SN/A
2622502SN/A###################################################
2632090SN/A#
2642147SN/A# Define a SCons builder for configuration flag headers.
2652147SN/A#
2662147SN/A###################################################
2672222SN/A
2685569Snate@binkert.org# This function generates a config header file that #defines the
2692112SN/A# option symbol to the current option setting (0 or 1).  The source
2705569Snate@binkert.org# operands are the name of the option and a Value node containing the
2714695Sgblack@eecs.umich.edu# value of the option.
2722502SN/Adef build_config_file(target, source, env):
2732502SN/A    (option, value) = [s.get_contents() for s in source]
2744997Sgblack@eecs.umich.edu    f = file(str(target[0]), 'w')
2757678Sgblack@eecs.umich.edu    print >> f, '#define', option, value
2767678Sgblack@eecs.umich.edu    f.close()
2774997Sgblack@eecs.umich.edu    return None
2782502SN/A
2792502SN/A# Generate the message to be printed when building the config file.
2802502SN/Adef build_config_file_string(target, source, env):
2812502SN/A    (option, value) = [s.get_contents() for s in source]
2822502SN/A    return "Defining %s as %s in %s." % (option, value, target[0])
2832502SN/A
2842502SN/A# Combine the two functions into a scons Action object.
2852502SN/Aconfig_action = Action(build_config_file, build_config_file_string)
2865569Snate@binkert.org
2872502SN/A# The emitter munges the source & target node lists to reflect what
2885569Snate@binkert.org# we're really doing.
2894695Sgblack@eecs.umich.edudef config_emitter(target, source, env):
2902147SN/A    # extract option name from Builder arg
2912222SN/A    option = str(target[0])
2922147SN/A    # True target is config header file
2932090SN/A    target = os.path.join('config', option.lower() + '.hh')
2942147SN/A    # Force value to 0/1 even if it's a Python bool
2952090SN/A    val = int(eval(str(env[option])))
2962147SN/A    # Sources are option name & value (packaged in SCons Value nodes)
2972147SN/A    return ([target], [Value(option), Value(val)])
2982147SN/A
2992222SN/Aconfig_builder = Builder(emitter = config_emitter, action = config_action)
3005569Snate@binkert.org
3012112SN/Aenv.Append(BUILDERS = { 'ConfigFile' : config_builder })
3024695Sgblack@eecs.umich.edu
3032147SN/A# base help text
3042222SN/Ahelp_text = '''
3052147SN/AUsage: scons [scons options] [build options] [target(s)]
3062090SN/A
3072147SN/A'''
3082090SN/A
3092147SN/A# libelf build is shared across all configs in the build root.
3102147SN/Aenv.SConscript('ext/libelf/SConscript',
3112147SN/A               build_dir = os.path.join(build_root, 'libelf'),
3122222SN/A               exports = 'env')
3135569Snate@binkert.org
3142112SN/A###################################################
3154695Sgblack@eecs.umich.edu#
3162147SN/A# Define build environments for selected configurations.
3172222SN/A#
3182147SN/A###################################################
3192090SN/A
3202147SN/A# rename base env
3212090SN/Abase_env = env
3222147SN/A
3232147SN/Afor build_path in build_paths:
3242147SN/A    print "Building in", build_path
3252222SN/A    # build_dir is the tail component of build path, and is used to
3265569Snate@binkert.org    # determine the build parameters (e.g., 'ALPHA_SE')
3275569Snate@binkert.org    (build_root, build_dir) = os.path.split(build_path)
3285569Snate@binkert.org    # Make a copy of the build-root environment to use for this config.
3295569Snate@binkert.org    env = base_env.Copy()
3302112SN/A
3314695Sgblack@eecs.umich.edu    # Set env options according to the build directory config.
3322147SN/A    sticky_opts.files = []
3332222SN/A    # Options for $BUILD_ROOT/$BUILD_DIR are stored in
3342147SN/A    # $BUILD_ROOT/options/$BUILD_DIR so you can nuke
3352090SN/A    # $BUILD_ROOT/$BUILD_DIR without losing your options settings.
3362147SN/A    current_opts_file = os.path.join(build_root, 'options', build_dir)
3372090SN/A    if os.path.isfile(current_opts_file):
3382147SN/A        sticky_opts.files.append(current_opts_file)
3392147SN/A        print "Using saved options file %s" % current_opts_file
3402147SN/A    else:
3412222SN/A        # Build dir-specific options file doesn't exist.
3425569Snate@binkert.org
3432112SN/A        # Make sure the directory is there so we can create it later
3444695Sgblack@eecs.umich.edu        opt_dir = os.path.dirname(current_opts_file)
3452147SN/A        if not os.path.isdir(opt_dir):
3462222SN/A            os.mkdir(opt_dir)
3472147SN/A
3482090SN/A        # Get default build options from source tree.  Options are
3495569Snate@binkert.org        # normally determined by name of $BUILD_DIR, but can be
3502167SN/A        # overriden by 'default=' arg on command line.
3515569Snate@binkert.org        default_opts_file = os.path.join('build_opts',
352                                         ARGUMENTS.get('default', build_dir))
353        if os.path.isfile(default_opts_file):
354            sticky_opts.files.append(default_opts_file)
355            print "Options file %s not found,\n  using defaults in %s" \
356                  % (current_opts_file, default_opts_file)
357        else:
358            print "Error: cannot find options file %s or %s" \
359                  % (current_opts_file, default_opts_file)
360            Exit(1)
361
362    # Apply current option settings to env
363    sticky_opts.Update(env)
364    nonsticky_opts.Update(env)
365
366    help_text += "Sticky options for %s:\n" % build_dir \
367                 + sticky_opts.GenerateHelpText(env) \
368                 + "\nNon-sticky options for %s:\n" % build_dir \
369                 + nonsticky_opts.GenerateHelpText(env)
370
371    # Process option settings.
372
373    if not have_fenv and env['USE_FENV']:
374        print "Warning: <fenv.h> not available; " \
375              "forcing USE_FENV to False in", build_dir + "."
376        env['USE_FENV'] = False
377
378    if not env['USE_FENV']:
379        print "Warning: No IEEE FP rounding mode control in", build_dir + "."
380        print "         FP results may deviate slightly from other platforms."
381
382    if env['EFENCE']:
383        env.Append(LIBS=['efence'])
384
385    if env['USE_MYSQL']:
386        if not have_mysql:
387            print "Warning: MySQL not available; " \
388                  "forcing USE_MYSQL to False in", build_dir + "."
389            env['USE_MYSQL'] = False
390        else:
391            print "Compiling in", build_dir, "with MySQL support."
392            env.ParseConfig(mysql_config_libs)
393            env.ParseConfig(mysql_config_include)
394
395    # Save sticky option settings back to current options file
396    sticky_opts.Save(current_opts_file, env)
397
398    # Do this after we save setting back, or else we'll tack on an
399    # extra 'qdo' every time we run scons.
400    if env['BATCH']:
401        env['CC']  = env['BATCH_CMD'] + ' ' + env['CC']
402        env['CXX'] = env['BATCH_CMD'] + ' ' + env['CXX']
403
404    if env['USE_SSE2']:
405        env.Append(CCFLAGS='-msse2')
406
407    # The m5/SConscript file sets up the build rules in 'env' according
408    # to the configured options.  It returns a list of environments,
409    # one for each variant build (debug, opt, etc.)
410    envList = SConscript('src/SConscript', build_dir = build_path,
411                         exports = 'env', duplicate = False)
412
413    # Set up the regression tests for each build.
414#    for e in envList:
415#        SConscript('m5-test/SConscript',
416#                   build_dir = os.path.join(build_dir, 'test', e.Label),
417#                   exports = { 'env' : e }, duplicate = False)
418
419Help(help_text)
420
421###################################################
422#
423# Let SCons do its thing.  At this point SCons will use the defined
424# build environments to build the requested targets.
425#
426###################################################
427
428