SConstruct revision 2155
12155SN/A# -*- mode:python -*-
22155SN/A
32155SN/A# Copyright (c) 2004-2005 The Regents of The University of Michigan
42155SN/A# All rights reserved.
52155SN/A#
62155SN/A# Redistribution and use in source and binary forms, with or without
72155SN/A# modification, are permitted provided that the following conditions are
82155SN/A# met: redistributions of source code must retain the above copyright
92155SN/A# notice, this list of conditions and the following disclaimer;
102155SN/A# redistributions in binary form must reproduce the above copyright
112155SN/A# notice, this list of conditions and the following disclaimer in the
122155SN/A# documentation and/or other materials provided with the distribution;
132155SN/A# neither the name of the copyright holders nor the names of its
142155SN/A# contributors may be used to endorse or promote products derived from
152155SN/A# this software without specific prior written permission.
162155SN/A#
172155SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
182155SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
192155SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
202155SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
212155SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
222155SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
232155SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
242155SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
252155SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
262155SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
272155SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
282665Ssaidi@eecs.umich.edu
292665Ssaidi@eecs.umich.edu###################################################
302155SN/A#
314202Sbinkertn@umich.edu# SCons top-level build description (SConstruct) file.
322155SN/A#
337768SAli.Saidi@ARM.com# To build M5, you need a directory with three things:
347768SAli.Saidi@ARM.com# 1. A copy of this file (named SConstruct).
357768SAli.Saidi@ARM.com# 2. A link named 'm5' to the top of the M5 simulator source tree.
362178SN/A# 3. A link named 'ext' to the top of the M5 external source tree.
372178SN/A#
382178SN/A# Then type 'scons' to build the default configuration (see below), or
392178SN/A# 'scons <CONFIG>/<binary>' to build some other configuration (e.g.,
402178SN/A# 'ALPHA_FS/m5.opt' for the optimized full-system version).
412178SN/A#
422178SN/A###################################################
432178SN/A
442178SN/A# Python library imports
452178SN/Aimport sys
462178SN/Aimport os
472155SN/A
485865Sksewell@umich.edu# Check for recent-enough Python and SCons versions
496181Sksewell@umich.eduEnsurePythonVersion(2,3)
506181Sksewell@umich.eduEnsureSConsVersion(0,96)
515865Sksewell@umich.edu
523918Ssaidi@eecs.umich.edu# The absolute path to the current directory (where this file lives).
535865Sksewell@umich.eduROOT = Dir('.').abspath
542623SN/A
553918Ssaidi@eecs.umich.edu# Paths to the M5 and external source trees (local symlinks).
562155SN/ASRCDIR = os.path.join(ROOT, 'm5')
572155SN/AEXT_SRCDIR = os.path.join(ROOT, 'ext')
582292SN/A
596181Sksewell@umich.edu# Check for 'm5' and 'ext' links, die if they don't exist.
606181Sksewell@umich.eduif not os.path.isdir(SRCDIR):
613918Ssaidi@eecs.umich.edu    print "Error: '%s' must be a link to the M5 source tree." % SRCDIR
622292SN/A    Exit(1)
632292SN/A
642292SN/Aif not os.path.isdir('ext'):
653918Ssaidi@eecs.umich.edu    print "Error: '%s' must be a link to the M5 external source tree." \
662292SN/A          % EXT_SRCDIR
672292SN/A    Exit(1)
682766Sktlim@umich.edu
692766Sktlim@umich.edu# tell python where to find m5 python code
702766Sktlim@umich.edusys.path.append(os.path.join(SRCDIR, 'python'))
712921Sktlim@umich.edu
722921Sktlim@umich.edu###################################################
732766Sktlim@umich.edu#
742766Sktlim@umich.edu# Figure out which configurations to set up.
755529Snate@binkert.org#
762766Sktlim@umich.edu#
774762Snate@binkert.org# It's prohibitive to do all the combinations of base configurations
782155SN/A# and options, so we have to infer which ones the user wants.
792155SN/A#
802155SN/A# 1. If there are command-line targets, the configuration(s) are inferred
812155SN/A#    from the directories of those targets.  If scons was invoked from a
822155SN/A#    subdirectory (using 'scons -u'), those targets have to be
832155SN/A#    interpreted relative to that subdirectory.
842766Sktlim@umich.edu#
852155SN/A# 2. If there are no command-line targets, and scons was invoked from a
865865Sksewell@umich.edu#    subdirectory (using 'scons -u'), the configuration is inferred from
872155SN/A#    the name of the subdirectory.
882155SN/A#
892155SN/A# 3. If there are no command-line targets and scons was invoked from
902155SN/A#    the root build directory, a default configuration is used.  The
912178SN/A#    built-in default is ALPHA_SE, but this can be overridden by setting the
922178SN/A#    M5_DEFAULT_CONFIG shell environment veriable.
937756SAli.Saidi@ARM.com#
942766Sktlim@umich.edu# In cases 2 & 3, the specific file target defaults to 'm5.debug', but
952178SN/A# this can be overridden by setting the M5_DEFAULT_BINARY shell
962178SN/A# environment veriable.
976994Snate@binkert.org#
982178SN/A###################################################
992766Sktlim@umich.edu
1002766Sktlim@umich.edu# Find default configuration & binary.
1012766Sktlim@umich.edudefault_config = os.environ.get('M5_DEFAULT_CONFIG', 'ALPHA_SE')
1022788Sktlim@umich.edudefault_binary = os.environ.get('M5_DEFAULT_BINARY', 'm5.debug')
1032178SN/A
1042733Sktlim@umich.edu# Ask SCons which directory it was invoked from.  If you invoke SCons
1052733Sktlim@umich.edu# from a subdirectory you must use the '-u' flag.
1062817Sksewell@umich.edulaunch_dir = GetLaunchDir()
1072733Sktlim@umich.edu
1084486Sbinkertn@umich.edu# Build a list 'my_targets' of all the targets relative to ROOT.
1094486Sbinkertn@umich.eduif launch_dir == ROOT:
1104776Sgblack@eecs.umich.edu    # invoked from root build dir
1114776Sgblack@eecs.umich.edu    if len(COMMAND_LINE_TARGETS) != 0:
1128739Sgblack@eecs.umich.edu        # easy: use specified targets as is
1136365Sgblack@eecs.umich.edu        my_targets = COMMAND_LINE_TARGETS
1144486Sbinkertn@umich.edu    else:
1154202Sbinkertn@umich.edu        # default target (ALPHA_SE/m5.debug, unless overridden)
1164202Sbinkertn@umich.edu        target = os.path.join(default_config, default_binary)
1174202Sbinkertn@umich.edu        my_targets = [target]
1188541Sgblack@eecs.umich.edu        Default(target)
1194202Sbinkertn@umich.eduelse:
1204202Sbinkertn@umich.edu    # invoked from subdirectory
1214776Sgblack@eecs.umich.edu    if not launch_dir.startswith(ROOT):
1228739Sgblack@eecs.umich.edu        print "Error: launch dir (%s) not a subdirectory of ROOT (%s)!" \
1236365Sgblack@eecs.umich.edu              (launch_dir, ROOT)
1244202Sbinkertn@umich.edu        Exit(1)
1258777Sgblack@eecs.umich.edu    # make launch_dir relative to ROOT (strip ROOT plus slash off front)
1264202Sbinkertn@umich.edu    launch_dir = launch_dir[len(ROOT)+1:]
1274202Sbinkertn@umich.edu    if len(COMMAND_LINE_TARGETS) != 0:
1284202Sbinkertn@umich.edu        # make specified targets relative to ROOT
1295217Ssaidi@eecs.umich.edu        my_targets = map(lambda x: os.path.join(launch_dir, x),
1304202Sbinkertn@umich.edu                         COMMAND_LINE_TARGETS)
1312155SN/A    else:
1324202Sbinkertn@umich.edu        # build default binary (m5.debug, unless overridden) using the
1334776Sgblack@eecs.umich.edu        # config inferred by the invocation directory (the first
1344776Sgblack@eecs.umich.edu        # subdirectory after ROOT)
1354776Sgblack@eecs.umich.edu        target = os.path.join(launch_dir.split('/')[0], default_binary)
1364776Sgblack@eecs.umich.edu        my_targets = [target]
1372766Sktlim@umich.edu        Default(target)
1384202Sbinkertn@umich.edu
1398335Snate@binkert.org# Normalize target paths (gets rid of '..' in the middle, etc.)
1402733Sktlim@umich.edumy_targets = map(os.path.normpath, my_targets)
1412733Sktlim@umich.edu
1422733Sktlim@umich.edu# Generate a list of the unique configs that the collected targets reference.
1432733Sktlim@umich.edubuild_dirs = []
1442733Sktlim@umich.edufor t in my_targets:
1452874Sktlim@umich.edu    dir = t.split('/')[0]
1462874Sktlim@umich.edu    if dir not in build_dirs:
1472874Sktlim@umich.edu        build_dirs.append(dir)
1484202Sbinkertn@umich.edu
1492733Sktlim@umich.edu###################################################
1505192Ssaidi@eecs.umich.edu#
1518335Snate@binkert.org# Set up the default build environment.  This environment is copied
1528335Snate@binkert.org# and modified according to each selected configuration.
1538335Snate@binkert.org#
1548335Snate@binkert.org###################################################
1558335Snate@binkert.org
1568335Snate@binkert.orgenv = Environment(ENV = os.environ,  # inherit user's environment vars
1578335Snate@binkert.org                  ROOT = ROOT,
1588335Snate@binkert.org                  SRCDIR = SRCDIR,
1598335Snate@binkert.org                  EXT_SRCDIR = EXT_SRCDIR)
1608335Snate@binkert.org
1618335Snate@binkert.orgenv.SConsignFile("sconsign")
1628335Snate@binkert.org
1638335Snate@binkert.org# I waffle on this setting... it does avoid a few painful but
1648335Snate@binkert.org# unnecessary builds, but it also seems to make trivial builds take
1658335Snate@binkert.org# noticeably longer.
1668335Snate@binkert.orgif False:
1678335Snate@binkert.org    env.TargetSignatures('content')
1688335Snate@binkert.org
1698335Snate@binkert.org# M5_EXT is used by isa_parser.py to find the PLY package.
1708335Snate@binkert.orgenv.Append(ENV = { 'M5_EXT' : EXT_SRCDIR })
1718335Snate@binkert.org
1728335Snate@binkert.org# Set up default C++ compiler flags
1738335Snate@binkert.orgenv.Append(CCFLAGS='-pipe')
1748335Snate@binkert.orgenv.Append(CCFLAGS='-fno-strict-aliasing')
1758471SGiacomo.Gabrielli@arm.comenv.Append(CCFLAGS=Split('-Wall -Wno-sign-compare -Werror -Wundef'))
1768335Snate@binkert.orgif sys.platform == 'cygwin':
1778335Snate@binkert.org    # cygwin has some header file issues...
1785192Ssaidi@eecs.umich.edu    env.Append(CCFLAGS=Split("-Wno-uninitialized"))
1798232Snate@binkert.orgenv.Append(CPPPATH=[os.path.join(EXT_SRCDIR + '/dnet')])
1808232Snate@binkert.org
1818232Snate@binkert.org# Default libraries
1828300Schander.sudanthi@arm.comenv.Append(LIBS=['z'])
1838300Schander.sudanthi@arm.com
1845192Ssaidi@eecs.umich.edu# Platform-specific configuration
1858300Schander.sudanthi@arm.comconf = Configure(env)
1868300Schander.sudanthi@arm.com
1876036Sksewell@umich.edu# Check for <fenv.h> (C99 FP environment control)
1888300Schander.sudanthi@arm.comhave_fenv = conf.CheckHeader('fenv.h', '<>')
1898300Schander.sudanthi@arm.comif not have_fenv:
190    print "Warning: Header file <fenv.h> not found."
191    print "         This host has no IEEE FP rounding mode control."
192
193# Check for mysql.
194mysql_config = WhereIs('mysql_config')
195have_mysql = mysql_config != None
196
197# Check MySQL version.
198if have_mysql:
199    mysql_version = os.popen(mysql_config + ' --version').read()
200    mysql_version = mysql_version.split('.')
201    mysql_major = int(mysql_version[0])
202    mysql_minor = int(mysql_version[1])
203    # This version check is probably overly conservative, but it deals
204    # with the versions we have installed.
205    if mysql_major < 4 or (mysql_major == 4 and mysql_minor < 1):
206        print "Warning: MySQL v4.1 or newer required."
207        have_mysql = False
208
209# Set up mysql_config commands.
210if have_mysql:
211    mysql_config_include = mysql_config + ' --include'
212    if os.system(mysql_config_include + ' > /dev/null') != 0:
213        # older mysql_config versions don't support --include, use
214        # --cflags instead
215        mysql_config_include = mysql_config + ' --cflags | sed s/\\\'//g'
216    # This seems to work in all versions
217    mysql_config_libs = mysql_config + ' --libs'
218
219env = conf.Finish()
220
221# Define the universe of supported ISAs
222env['ALL_ISA_LIST'] = ['alpha', 'sparc', 'mips']
223
224# Define the universe of supported CPU models
225env['ALL_CPU_LIST'] = ['SimpleCPU', 'FastCPU', 'FullCPU', 'AlphaFullCPU']
226
227# Sticky options get saved in the options file so they persist from
228# one invocation to the next (unless overridden, in which case the new
229# value becomes sticky).
230sticky_opts = Options(args=ARGUMENTS)
231sticky_opts.AddOptions(
232    EnumOption('TARGET_ISA', 'Target ISA', 'alpha', env['ALL_ISA_LIST']),
233    BoolOption('FULL_SYSTEM', 'Full-system support', False),
234    BoolOption('ALPHA_TLASER',
235               'Model Alpha TurboLaser platform (vs. Tsunami)', False),
236    BoolOption('NO_FAST_ALLOC', 'Disable fast object allocator', False),
237    BoolOption('EFENCE', 'Link with Electric Fence malloc debugger',
238               False),
239    BoolOption('SS_COMPATIBLE_FP',
240               'Make floating-point results compatible with SimpleScalar',
241               False),
242    BoolOption('USE_SSE2',
243               'Compile for SSE2 (-msse2) to get IEEE FP on x86 hosts',
244               False),
245    BoolOption('STATS_BINNING', 'Bin statistics by CPU mode', have_mysql),
246    BoolOption('USE_MYSQL', 'Use MySQL for stats output', have_mysql),
247    BoolOption('USE_FENV', 'Use <fenv.h> IEEE mode control', have_fenv),
248    ('CC', 'C compiler', os.environ.get('CC', env['CC'])),
249    ('CXX', 'C++ compiler', os.environ.get('CXX', env['CXX'])),
250    BoolOption('BATCH', 'Use batch pool for build and tests', False),
251    ('BATCH_CMD', 'Batch pool submission command name', 'qdo')
252    )
253
254# Non-sticky options only apply to the current build.
255nonsticky_opts = Options(args=ARGUMENTS)
256nonsticky_opts.AddOptions(
257    # This really should be a sticky option, but there's a bug in
258    # scons 0.96.1 that causes ListOptions not to be able to be
259    # restored from a saved option file.  It looks like this is fixed
260    # in 0.96.9, but there's a different bug in that version that means we
261    # can't just upgrade.
262    ListOption('CPU_MODELS', 'CPU models', 'all', env['ALL_CPU_LIST']),
263    BoolOption('update_ref', 'Update test reference outputs', False)
264    )
265
266# These options get exported to #defines in config/*.hh (see m5/SConscript).
267env.ExportOptions = ['FULL_SYSTEM', 'ALPHA_TLASER', 'USE_FENV', \
268                     'USE_MYSQL', 'NO_FAST_ALLOC', 'SS_COMPATIBLE_FP', \
269                     'STATS_BINNING']
270
271# Define a handy 'no-op' action
272def no_action(target, source, env):
273    return 0
274
275env.NoAction = Action(no_action, None)
276
277# libelf build is described in its own SConscript file.
278# SConscript-global is the build in build/libelf shared among all
279# configs.
280env.SConscript('m5/libelf/SConscript-global', exports = 'env')
281
282###################################################
283#
284# Define a SCons builder for configuration flag headers.
285#
286###################################################
287
288# This function generates a config header file that #defines the
289# option symbol to the current option setting (0 or 1).  The source
290# operands are the name of the option and a Value node containing the
291# value of the option.
292def build_config_file(target, source, env):
293    (option, value) = [s.get_contents() for s in source]
294    f = file(str(target[0]), 'w')
295    print >> f, '#define', option, value
296    f.close()
297    return None
298
299# Generate the message to be printed when building the config file.
300def build_config_file_string(target, source, env):
301    (option, value) = [s.get_contents() for s in source]
302    return "Defining %s as %s in %s." % (option, value, target[0])
303
304# Combine the two functions into a scons Action object.
305config_action = Action(build_config_file, build_config_file_string)
306
307# The emitter munges the source & target node lists to reflect what
308# we're really doing.
309def config_emitter(target, source, env):
310    # extract option name from Builder arg
311    option = str(target[0])
312    # True target is config header file
313    target = os.path.join('config', option.lower() + '.hh')
314    # Force value to 0/1 even if it's a Python bool
315    val = int(eval(str(env[option])))
316    # Sources are option name & value (packaged in SCons Value nodes)
317    return ([target], [Value(option), Value(val)])
318
319config_builder = Builder(emitter = config_emitter, action = config_action)
320
321env.Append(BUILDERS = { 'ConfigFile' : config_builder })
322
323###################################################
324#
325# Define build environments for selected configurations.
326#
327###################################################
328
329# rename base env
330base_env = env
331
332for build_dir in build_dirs:
333    # Make a copy of the default environment to use for this config.
334    env = base_env.Copy()
335
336    # Record what build_dir was in the environment
337    env.Append(BUILD_DIR=build_dir);
338
339    # Set env according to the build directory config.
340
341    sticky_opts.files = []
342    # Name of default options file is taken from 'default=' on command
343    # line if set, otherwise name of build dir.
344    default_options_file = os.path.join('build_options', 'default',
345                                        ARGUMENTS.get('default', build_dir))
346    if os.path.isfile(default_options_file):
347        sticky_opts.files.append(default_options_file)
348    current_options_file = os.path.join('build_options', 'current', build_dir)
349    if os.path.isfile(current_options_file):
350        sticky_opts.files.append(current_options_file)
351    else:
352        # if file doesn't exist, make sure at least the directory is there
353        # so we can create it later
354        opt_dir = os.path.dirname(current_options_file)
355        if not os.path.isdir(opt_dir):
356            os.mkdir(opt_dir)
357    if not sticky_opts.files:
358        print "%s: No options file found in build_options, using defaults." \
359              % build_dir
360
361    # Apply current option settings to env
362    sticky_opts.Update(env)
363    nonsticky_opts.Update(env)
364
365    # Process option settings.
366
367    if not have_fenv and env['USE_FENV']:
368        print "Warning: <fenv.h> not available; " \
369              "forcing USE_FENV to False in", build_dir + "."
370        env['USE_FENV'] = False
371
372    if not env['USE_FENV']:
373        print "Warning: No IEEE FP rounding mode control in", build_dir + "."
374        print "         FP results may deviate slightly from other platforms."
375
376    if env['EFENCE']:
377        env.Append(LIBS=['efence'])
378
379    if env['USE_MYSQL']:
380        if not have_mysql:
381            print "Warning: MySQL not available; " \
382                  "forcing USE_MYSQL to False in", build_dir + "."
383            env['USE_MYSQL'] = False
384        else:
385            print "Compiling in", build_dir, "with MySQL support."
386            env.ParseConfig(mysql_config_libs)
387            env.ParseConfig(mysql_config_include)
388
389    # Save sticky option settings back to current options file
390    sticky_opts.Save(current_options_file, env)
391
392    # Do this after we save setting back, or else we'll tack on an
393    # extra 'qdo' every time we run scons.
394    if env['BATCH']:
395        env['CC']  = env['BATCH_CMD'] + ' ' + env['CC']
396        env['CXX'] = env['BATCH_CMD'] + ' ' + env['CXX']
397
398    if env['USE_SSE2']:
399        env.Append(CCFLAGS='-msse2')
400
401    # The m5/SConscript file sets up the build rules in 'env' according
402    # to the configured options.  It returns a list of environments,
403    # one for each variant build (debug, opt, etc.)
404    envList = SConscript('m5/SConscript', build_dir = build_dir,
405                         exports = 'env', duplicate = False)
406
407    # Set up the regression tests for each build.
408    for e in envList:
409        SConscript('m5-test/SConscript',
410                   build_dir = os.path.join(build_dir, 'test', e.Label),
411                   exports = { 'env' : e }, duplicate = False)
412
413###################################################
414#
415# Let SCons do its thing.  At this point SCons will use the defined
416# build environments to build the requested targets.
417#
418###################################################
419
420