SConstruct revision 2139
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#
332178SN/A# To build M5, you need a directory with three things:
342178SN/A# 1. A copy of this file (named SConstruct).
352178SN/A# 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
452155SN/Aimport sys
462178SN/Aimport os
472155SN/A
482155SN/A# Check for recent-enough Python and SCons versions
492178SN/AEnsurePythonVersion(2,3)
502155SN/AEnsureSConsVersion(0,96)
512155SN/A
522623SN/A# The absolute path to the current directory (where this file lives).
533918Ssaidi@eecs.umich.eduROOT = Dir('.').abspath
542623SN/A
552623SN/A# Paths to the M5 and external source trees (local symlinks).
563918Ssaidi@eecs.umich.eduSRCDIR = os.path.join(ROOT, 'm5')
572155SN/AEXT_SRCDIR = os.path.join(ROOT, 'ext')
582155SN/A
592292SN/A# Check for 'm5' and 'ext' links, die if they don't exist.
603918Ssaidi@eecs.umich.eduif not os.path.isdir(SRCDIR):
612292SN/A    print "Error: '%s' must be a link to the M5 source tree." % SRCDIR
622292SN/A    Exit(1)
632292SN/A
643918Ssaidi@eecs.umich.eduif not os.path.isdir('ext'):
652292SN/A    print "Error: '%s' must be a link to the M5 external source tree." \
662292SN/A          % EXT_SRCDIR
672766Sktlim@umich.edu    Exit(1)
682766Sktlim@umich.edu
692766Sktlim@umich.edu# tell python where to find m5 python code
702921Sktlim@umich.edusys.path.append(os.path.join(SRCDIR, 'python'))
712921Sktlim@umich.edu
722766Sktlim@umich.edu###################################################
732766Sktlim@umich.edu#
745529Snate@binkert.org# Figure out which configurations to set up.
752766Sktlim@umich.edu#
764762Snate@binkert.org#
772155SN/A# 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
832766Sktlim@umich.edu#    interpreted relative to that subdirectory.
842155SN/A#
852623SN/A# 2. If there are no command-line targets, and scons was invoked from a
862155SN/A#    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
902178SN/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.
932766Sktlim@umich.edu#
942178SN/A# 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.
972178SN/A#
982766Sktlim@umich.edu###################################################
992766Sktlim@umich.edu
1002766Sktlim@umich.edu# Find default configuration & binary.
1012788Sktlim@umich.edudefault_config = os.environ.get('M5_DEFAULT_CONFIG', 'ALPHA_SE')
1022178SN/Adefault_binary = os.environ.get('M5_DEFAULT_BINARY', 'm5.debug')
1032733Sktlim@umich.edu
1042733Sktlim@umich.edu# Ask SCons which directory it was invoked from.  If you invoke SCons
1052817Sksewell@umich.edu# from a subdirectory you must use the '-u' flag.
1062733Sktlim@umich.edulaunch_dir = GetLaunchDir()
1074486Sbinkertn@umich.edu
1084486Sbinkertn@umich.edu# Build a list 'my_targets' of all the targets relative to ROOT.
1094776Sgblack@eecs.umich.eduif launch_dir == ROOT:
1104776Sgblack@eecs.umich.edu    # invoked from root build dir
1114486Sbinkertn@umich.edu    if len(COMMAND_LINE_TARGETS) != 0:
1124202Sbinkertn@umich.edu        # easy: use specified targets as is
1134202Sbinkertn@umich.edu        my_targets = COMMAND_LINE_TARGETS
1144202Sbinkertn@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)
1174776Sgblack@eecs.umich.edu        my_targets = [target]
1184202Sbinkertn@umich.edu        Default(target)
1194202Sbinkertn@umich.eduelse:
1204202Sbinkertn@umich.edu    # invoked from subdirectory
1214202Sbinkertn@umich.edu    if not launch_dir.startswith(ROOT):
1225217Ssaidi@eecs.umich.edu        print "Error: launch dir (%s) not a subdirectory of ROOT (%s)!" \
1234202Sbinkertn@umich.edu              (launch_dir, ROOT)
1242155SN/A        Exit(1)
1254202Sbinkertn@umich.edu    # make launch_dir relative to ROOT (strip ROOT plus slash off front)
1264486Sbinkertn@umich.edu    launch_dir = launch_dir[len(ROOT)+1:]
1274486Sbinkertn@umich.edu    if len(COMMAND_LINE_TARGETS) != 0:
1284202Sbinkertn@umich.edu        # make specified targets relative to ROOT
1294202Sbinkertn@umich.edu        my_targets = map(lambda x: os.path.join(launch_dir, x),
1302821Sktlim@umich.edu                         COMMAND_LINE_TARGETS)
1314776Sgblack@eecs.umich.edu    else:
1324776Sgblack@eecs.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]
1374776Sgblack@eecs.umich.edu        Default(target)
1384776Sgblack@eecs.umich.edu
1392766Sktlim@umich.edu# Normalize target paths (gets rid of '..' in the middle, etc.)
1404202Sbinkertn@umich.edumy_targets = map(os.path.normpath, my_targets)
1415192Ssaidi@eecs.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:
1452733Sktlim@umich.edu    dir = t.split('/')[0]
1462733Sktlim@umich.edu    if dir not in build_dirs:
1472874Sktlim@umich.edu        build_dirs.append(dir)
1482874Sktlim@umich.edu
1492874Sktlim@umich.edu###################################################
1504202Sbinkertn@umich.edu#
1512733Sktlim@umich.edu# Set up the default build environment.  This environment is copied
1525400Ssaidi@eecs.umich.edu# and modified according to each selected configuration.
1535400Ssaidi@eecs.umich.edu#
1545398Ssaidi@eecs.umich.edu###################################################
1555398Ssaidi@eecs.umich.edu
1565192Ssaidi@eecs.umich.eduenv = Environment(ENV = os.environ,  # inherit user's environment vars
1575192Ssaidi@eecs.umich.edu                  ROOT = ROOT,
1585192Ssaidi@eecs.umich.edu                  SRCDIR = SRCDIR,
1595217Ssaidi@eecs.umich.edu                  EXT_SRCDIR = EXT_SRCDIR)
1605192Ssaidi@eecs.umich.edu
1615192Ssaidi@eecs.umich.eduenv.SConsignFile("sconsign")
1625192Ssaidi@eecs.umich.edu
1635192Ssaidi@eecs.umich.edu# I waffle on this setting... it does avoid a few painful but
1645192Ssaidi@eecs.umich.edu# unnecessary builds, but it also seems to make trivial builds take
1655192Ssaidi@eecs.umich.edu# noticeably longer.
1665192Ssaidi@eecs.umich.eduif False:
1675192Ssaidi@eecs.umich.edu    env.TargetSignatures('content')
1685192Ssaidi@eecs.umich.edu
1695192Ssaidi@eecs.umich.edu# M5_EXT is used by isa_parser.py to find the PLY package.
1705192Ssaidi@eecs.umich.eduenv.Append(ENV = { 'M5_EXT' : EXT_SRCDIR })
1715192Ssaidi@eecs.umich.edu
1725192Ssaidi@eecs.umich.edu# Set up default C++ compiler flags
1735192Ssaidi@eecs.umich.eduenv.Append(CCFLAGS='-pipe')
1745192Ssaidi@eecs.umich.eduenv.Append(CCFLAGS='-fno-strict-aliasing')
1755192Ssaidi@eecs.umich.eduenv.Append(CCFLAGS=Split('-Wall -Wno-sign-compare -Werror -Wundef'))
1765192Ssaidi@eecs.umich.eduif sys.platform == 'cygwin':
1775192Ssaidi@eecs.umich.edu    # cygwin has some header file issues...
1785192Ssaidi@eecs.umich.edu    env.Append(CCFLAGS=Split("-Wno-uninitialized"))
1795192Ssaidi@eecs.umich.eduenv.Append(CPPPATH=[os.path.join(EXT_SRCDIR + '/dnet')])
180
181# Default libraries
182env.Append(LIBS=['z'])
183
184# Platform-specific configuration
185conf = Configure(env)
186
187# Check for <fenv.h> (C99 FP environment control)
188have_fenv = conf.CheckHeader('fenv.h', '<>')
189if 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# Sticky options get saved in the options file so they persist from
225# one invocation to the next (unless overridden, in which case the new
226# value becomes sticky).
227sticky_opts = Options(args=ARGUMENTS)
228sticky_opts.AddOptions(
229    EnumOption('TARGET_ISA', 'Target ISA', 'alpha', env['ALL_ISA_LIST']),
230    BoolOption('FULL_SYSTEM', 'Full-system support', False),
231    BoolOption('ALPHA_TLASER',
232               'Model Alpha TurboLaser platform (vs. Tsunami)', False),
233    BoolOption('NO_FAST_ALLOC', 'Disable fast object allocator', False),
234    BoolOption('EFENCE', 'Link with Electric Fence malloc debugger',
235               False),
236    BoolOption('SS_COMPATIBLE_FP',
237               'Make floating-point results compatible with SimpleScalar',
238               False),
239    BoolOption('USE_SSE2',
240               'Compile for SSE2 (-msse2) to get IEEE FP on x86 hosts',
241               False),
242    BoolOption('STATS_BINNING', 'Bin statistics by CPU mode', have_mysql),
243    BoolOption('USE_MYSQL', 'Use MySQL for stats output', have_mysql),
244    BoolOption('USE_FENV', 'Use <fenv.h> IEEE mode control', have_fenv),
245    ('CC', 'C compiler', os.environ.get('CC', env['CC'])),
246    ('CXX', 'C++ compiler', os.environ.get('CXX', env['CXX'])),
247    BoolOption('BATCH', 'Use batch pool for build and tests', False),
248    ('BATCH_CMD', 'Batch pool submission command name', 'qdo')
249    )
250
251# Non-sticky options only apply to the current build.
252nonsticky_opts = Options(args=ARGUMENTS)
253nonsticky_opts.AddOptions(
254    BoolOption('update_ref', 'Update test reference outputs', False)
255    )
256
257# These options get exported to #defines in config/*.hh (see m5/SConscript).
258env.ExportOptions = ['FULL_SYSTEM', 'ALPHA_TLASER', 'USE_FENV', \
259                     'USE_MYSQL', 'NO_FAST_ALLOC', 'SS_COMPATIBLE_FP', \
260                     'STATS_BINNING']
261
262# Define a handy 'no-op' action
263def no_action(target, source, env):
264    return 0
265
266env.NoAction = Action(no_action, None)
267
268# libelf build is described in its own SConscript file.
269# SConscript-global is the build in build/libelf shared among all
270# configs.
271env.SConscript('m5/libelf/SConscript-global', exports = 'env')
272
273###################################################
274#
275# Define a SCons builder for configuration flag headers.
276#
277###################################################
278
279# This function generates a config header file that #defines the
280# option symbol to the current option setting (0 or 1).  The source
281# operands are the name of the option and a Value node containing the
282# value of the option.
283def build_config_file(target, source, env):
284    (option, value) = [s.get_contents() for s in source]
285    f = file(str(target[0]), 'w')
286    print >> f, '#define', option, value
287    f.close()
288    return None
289
290# Generate the message to be printed when building the config file.
291def build_config_file_string(target, source, env):
292    (option, value) = [s.get_contents() for s in source]
293    return "Defining %s as %s in %s." % (option, value, target[0])
294
295# Combine the two functions into a scons Action object.
296config_action = Action(build_config_file, build_config_file_string)
297
298# The emitter munges the source & target node lists to reflect what
299# we're really doing.
300def config_emitter(target, source, env):
301    # extract option name from Builder arg
302    option = str(target[0])
303    # True target is config header file
304    target = os.path.join('config', option.lower() + '.hh')
305    # Force value to 0/1 even if it's a Python bool
306    val = int(eval(str(env[option])))
307    # Sources are option name & value (packaged in SCons Value nodes)
308    return ([target], [Value(option), Value(val)])
309
310config_builder = Builder(emitter = config_emitter, action = config_action)
311
312env.Append(BUILDERS = { 'ConfigFile' : config_builder })
313
314###################################################
315#
316# Define build environments for selected configurations.
317#
318###################################################
319
320# rename base env
321base_env = env
322
323for build_dir in build_dirs:
324    # Make a copy of the default environment to use for this config.
325    env = base_env.Copy()
326
327    # Record what build_dir was in the environment
328    env.Append(BUILD_DIR=build_dir);
329
330    # Set env according to the build directory config.
331
332    sticky_opts.files = []
333    # Name of default options file is taken from 'default=' on command
334    # line if set, otherwise name of build dir.
335    default_options_file = os.path.join('build_options', 'default',
336                                        ARGUMENTS.get('default', build_dir))
337    if os.path.isfile(default_options_file):
338        sticky_opts.files.append(default_options_file)
339    current_options_file = os.path.join('build_options', 'current', build_dir)
340    if os.path.isfile(current_options_file):
341        sticky_opts.files.append(current_options_file)
342    else:
343        # if file doesn't exist, make sure at least the directory is there
344        # so we can create it later
345        opt_dir = os.path.dirname(current_options_file)
346        if not os.path.isdir(opt_dir):
347            os.mkdir(opt_dir)
348    if not sticky_opts.files:
349        print "%s: No options file found in build_options, using defaults." \
350              % build_dir
351
352    # Apply current option settings to env
353    sticky_opts.Update(env)
354    nonsticky_opts.Update(env)
355
356    # Process option settings.
357
358    if not have_fenv and env['USE_FENV']:
359        print "Warning: <fenv.h> not available; " \
360              "forcing USE_FENV to False in", build_dir + "."
361        env['USE_FENV'] = False
362
363    if not env['USE_FENV']:
364        print "Warning: No IEEE FP rounding mode control in", build_dir + "."
365        print "         FP results may deviate slightly from other platforms."
366
367    if env['EFENCE']:
368        env.Append(LIBS=['efence'])
369
370    if env['USE_MYSQL']:
371        if not have_mysql:
372            print "Warning: MySQL not available; " \
373                  "forcing USE_MYSQL to False in", build_dir + "."
374            env['USE_MYSQL'] = False
375        else:
376            print "Compiling in", build_dir, "with MySQL support."
377            env.ParseConfig(mysql_config_libs)
378            env.ParseConfig(mysql_config_include)
379
380    # Save sticky option settings back to current options file
381    sticky_opts.Save(current_options_file, env)
382
383    # Do this after we save setting back, or else we'll tack on an
384    # extra 'qdo' every time we run scons.
385    if env['BATCH']:
386        env['CC']  = env['BATCH_CMD'] + ' ' + env['CC']
387        env['CXX'] = env['BATCH_CMD'] + ' ' + env['CXX']
388
389    if env['USE_SSE2']:
390        env.Append(CCFLAGS='-msse2')
391
392    # The m5/SConscript file sets up the build rules in 'env' according
393    # to the configured options.  It returns a list of environments,
394    # one for each variant build (debug, opt, etc.)
395    envList = SConscript('m5/SConscript', build_dir = build_dir,
396                         exports = 'env', duplicate = False)
397
398    # Set up the regression tests for each build.
399    for e in envList:
400        SConscript('m5-test/SConscript',
401                   build_dir = os.path.join(build_dir, 'test', e.Label),
402                   exports = { 'env' : e }, duplicate = False)
403
404###################################################
405#
406# Let SCons do its thing.  At this point SCons will use the defined
407# build environments to build the requested targets.
408#
409###################################################
410
411