SConstruct revision 2508
13005Sstever@eecs.umich.edu# -*- mode:python -*-
23005Sstever@eecs.umich.edu
33005Sstever@eecs.umich.edu# Copyright (c) 2004-2005 The Regents of The University of Michigan
43005Sstever@eecs.umich.edu# All rights reserved.
53005Sstever@eecs.umich.edu#
63005Sstever@eecs.umich.edu# Redistribution and use in source and binary forms, with or without
73005Sstever@eecs.umich.edu# modification, are permitted provided that the following conditions are
83005Sstever@eecs.umich.edu# met: redistributions of source code must retain the above copyright
93005Sstever@eecs.umich.edu# notice, this list of conditions and the following disclaimer;
103005Sstever@eecs.umich.edu# redistributions in binary form must reproduce the above copyright
113005Sstever@eecs.umich.edu# notice, this list of conditions and the following disclaimer in the
123005Sstever@eecs.umich.edu# documentation and/or other materials provided with the distribution;
133005Sstever@eecs.umich.edu# neither the name of the copyright holders nor the names of its
143005Sstever@eecs.umich.edu# contributors may be used to endorse or promote products derived from
153005Sstever@eecs.umich.edu# this software without specific prior written permission.
163005Sstever@eecs.umich.edu#
173005Sstever@eecs.umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
183005Sstever@eecs.umich.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
193005Sstever@eecs.umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
203005Sstever@eecs.umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
213005Sstever@eecs.umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
223005Sstever@eecs.umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
233005Sstever@eecs.umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
243005Sstever@eecs.umich.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
253005Sstever@eecs.umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
263005Sstever@eecs.umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
273005Sstever@eecs.umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
283005Sstever@eecs.umich.edu
292889SN/A###################################################
302889SN/A#
312710SN/A# SCons top-level build description (SConstruct) file.
322710SN/A#
332934SN/A# To build M5, you need a directory with three things:
342934SN/A# 1. A copy of this file (named SConstruct).
352549SN/A# 2. A link named 'm5' to the top of the M5 simulator source tree.
362995SN/A# 3. A link named 'ext' to the top of the M5 external source tree.
372549SN/A#
383088Sstever@eecs.umich.edu# Then type 'scons' to build the default configuration (see below), or
393088Sstever@eecs.umich.edu# 'scons <CONFIG>/<binary>' to build some other configuration (e.g.,
403088Sstever@eecs.umich.edu# 'ALPHA_FS/m5.opt' for the optimized full-system version).
412889SN/A#
422710SN/A###################################################
432917SN/A
442710SN/A# Python library imports
453312Sstever@eecs.umich.eduimport sys
462917SN/Aimport os
472948SN/A
482995SN/A# Check for recent-enough Python and SCons versions
492995SN/AEnsurePythonVersion(2,3)
502995SN/AEnsureSConsVersion(0,96)
512995SN/A
522995SN/A# The absolute path to the current directory (where this file lives).
533143Shsul@eecs.umich.eduROOT = Dir('.').abspath
543025Ssaidi@eecs.umich.edu
553143Shsul@eecs.umich.edu# Paths to the M5 and external source trees (local symlinks).
563143Shsul@eecs.umich.eduSRCDIR = os.path.join(ROOT, 'm5')
573143Shsul@eecs.umich.eduEXT_SRCDIR = os.path.join(ROOT, 'ext')
583143Shsul@eecs.umich.edu
593183Shsul@eecs.umich.edu# Check for 'm5' and 'ext' links, die if they don't exist.
603183Shsul@eecs.umich.eduif not os.path.isdir(SRCDIR):
612710SN/A    print "Error: '%s' must be a link to the M5 source tree." % SRCDIR
622710SN/A    Exit(1)
632710SN/A
642710SN/Aif not os.path.isdir('ext'):
652710SN/A    print "Error: '%s' must be a link to the M5 external source tree." \
662710SN/A          % EXT_SRCDIR
672710SN/A    Exit(1)
683304Sstever@eecs.umich.edu
693304Sstever@eecs.umich.edu# tell python where to find m5 python code
703304Sstever@eecs.umich.edusys.path.append(os.path.join(SRCDIR, 'python'))
713304Sstever@eecs.umich.edu
723304Sstever@eecs.umich.edu###################################################
732934SN/A#
743304Sstever@eecs.umich.edu# Figure out which configurations to set up.
753304Sstever@eecs.umich.edu#
762934SN/A#
773304Sstever@eecs.umich.edu# It's prohibitive to do all the combinations of base configurations
783304Sstever@eecs.umich.edu# and options, so we have to infer which ones the user wants.
792934SN/A#
803304Sstever@eecs.umich.edu# 1. If there are command-line targets, the configuration(s) are inferred
813304Sstever@eecs.umich.edu#    from the directories of those targets.  If scons was invoked from a
822566SN/A#    subdirectory (using 'scons -u'), those targets have to be
833304Sstever@eecs.umich.edu#    interpreted relative to that subdirectory.
843304Sstever@eecs.umich.edu#
852995SN/A# 2. If there are no command-line targets, and scons was invoked from a
862995SN/A#    subdirectory (using 'scons -u'), the configuration is inferred from
873304Sstever@eecs.umich.edu#    the name of the subdirectory.
883304Sstever@eecs.umich.edu#
893304Sstever@eecs.umich.edu# 3. If there are no command-line targets and scons was invoked from
902995SN/A#    the root build directory, a default configuration is used.  The
912995SN/A#    built-in default is ALPHA_SE, but this can be overridden by setting the
922995SN/A#    M5_DEFAULT_CONFIG shell environment veriable.
932917SN/A#
942995SN/A# In cases 2 & 3, the specific file target defaults to 'm5.debug', but
953304Sstever@eecs.umich.edu# this can be overridden by setting the M5_DEFAULT_BINARY shell
962995SN/A# environment veriable.
973304Sstever@eecs.umich.edu#
983304Sstever@eecs.umich.edu###################################################
993304Sstever@eecs.umich.edu
1003312Sstever@eecs.umich.edu# Find default configuration & binary.
1013312Sstever@eecs.umich.edudefault_config = os.environ.get('M5_DEFAULT_CONFIG', 'ALPHA_SE')
1023312Sstever@eecs.umich.edudefault_binary = os.environ.get('M5_DEFAULT_BINARY', 'm5.debug')
1033312Sstever@eecs.umich.edu
1043312Sstever@eecs.umich.edu# Ask SCons which directory it was invoked from.  If you invoke SCons
1053005Sstever@eecs.umich.edu# from a subdirectory you must use the '-u' flag.
1063005Sstever@eecs.umich.edulaunch_dir = GetLaunchDir()
1073304Sstever@eecs.umich.edu
1083304Sstever@eecs.umich.edu# Build a list 'my_targets' of all the targets relative to ROOT.
1093304Sstever@eecs.umich.eduif launch_dir == ROOT:
1103304Sstever@eecs.umich.edu    # invoked from root build dir
1113304Sstever@eecs.umich.edu    if len(COMMAND_LINE_TARGETS) != 0:
1123005Sstever@eecs.umich.edu        # easy: use specified targets as is
1133304Sstever@eecs.umich.edu        my_targets = COMMAND_LINE_TARGETS
1143005Sstever@eecs.umich.edu    else:
1153005Sstever@eecs.umich.edu        # default target (ALPHA_SE/m5.debug, unless overridden)
1163005Sstever@eecs.umich.edu        target = os.path.join(default_config, default_binary)
1172566SN/A        my_targets = [target]
1182710SN/A        Default(target)
1192710SN/Aelse:
1203183Shsul@eecs.umich.edu    # invoked from subdirectory
1213183Shsul@eecs.umich.edu    if not launch_dir.startswith(ROOT):
1223183Shsul@eecs.umich.edu        print "Error: launch dir (%s) not a subdirectory of ROOT (%s)!" \
1233183Shsul@eecs.umich.edu              (launch_dir, ROOT)
1243183Shsul@eecs.umich.edu        Exit(1)
1253183Shsul@eecs.umich.edu    # make launch_dir relative to ROOT (strip ROOT plus slash off front)
1263183Shsul@eecs.umich.edu    launch_dir = launch_dir[len(ROOT)+1:]
1273183Shsul@eecs.umich.edu    if len(COMMAND_LINE_TARGETS) != 0:
1283183Shsul@eecs.umich.edu        # make specified targets relative to ROOT
1293183Shsul@eecs.umich.edu        my_targets = map(lambda x: os.path.join(launch_dir, x),
1303183Shsul@eecs.umich.edu                         COMMAND_LINE_TARGETS)
1313183Shsul@eecs.umich.edu    else:
1323183Shsul@eecs.umich.edu        # build default binary (m5.debug, unless overridden) using the
1333183Shsul@eecs.umich.edu        # config inferred by the invocation directory (the first
1343183Shsul@eecs.umich.edu        # subdirectory after ROOT)
1353183Shsul@eecs.umich.edu        target = os.path.join(launch_dir.split('/')[0], default_binary)
1363183Shsul@eecs.umich.edu        my_targets = [target]
1373183Shsul@eecs.umich.edu        Default(target)
1383183Shsul@eecs.umich.edu
1393183Shsul@eecs.umich.edu# Normalize target paths (gets rid of '..' in the middle, etc.)
1403183Shsul@eecs.umich.edumy_targets = map(os.path.normpath, my_targets)
1413183Shsul@eecs.umich.edu
1423183Shsul@eecs.umich.edu# Generate a list of the unique configs that the collected targets reference.
1433183Shsul@eecs.umich.edubuild_dirs = []
1443183Shsul@eecs.umich.edufor t in my_targets:
1452917SN/A    dir = t.split('/')[0]
1463046Sstever@eecs.umich.edu    if dir not in build_dirs:
1472948SN/A        build_dirs.append(dir)
1482948SN/A
1492948SN/A###################################################
1503046Sstever@eecs.umich.edu#
1512917SN/A# Set up the default build environment.  This environment is copied
1523046Sstever@eecs.umich.edu# and modified according to each selected configuration.
1533022Shsul@eecs.umich.edu#
1543046Sstever@eecs.umich.edu###################################################
1553022Shsul@eecs.umich.edu
1563022Shsul@eecs.umich.eduenv = Environment(ENV = os.environ,  # inherit user's environment vars
1573143Shsul@eecs.umich.edu                  ROOT = ROOT,
1583143Shsul@eecs.umich.edu                  SRCDIR = SRCDIR,
1593143Shsul@eecs.umich.edu                  EXT_SRCDIR = EXT_SRCDIR)
1603143Shsul@eecs.umich.edu
1613143Shsul@eecs.umich.eduenv.SConsignFile("sconsign")
1623133Shsul@eecs.umich.edu
1633133Shsul@eecs.umich.edu# I waffle on this setting... it does avoid a few painful but
1643133Shsul@eecs.umich.edu# unnecessary builds, but it also seems to make trivial builds take
1653133Shsul@eecs.umich.edu# noticeably longer.
1662710SN/Aif False:
1672740SN/A    env.TargetSignatures('content')
168
169# M5_EXT is used by isa_parser.py to find the PLY package.
170env.Append(ENV = { 'M5_EXT' : EXT_SRCDIR })
171
172# Set up default C++ compiler flags
173env.Append(CCFLAGS='-pipe')
174env.Append(CCFLAGS='-fno-strict-aliasing')
175env.Append(CCFLAGS=Split('-Wall -Wno-sign-compare -Werror -Wundef'))
176if sys.platform == 'cygwin':
177    # cygwin has some header file issues...
178    env.Append(CCFLAGS=Split("-Wno-uninitialized"))
179env.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# 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    # There's a bug in scons 0.96.1 that causes ListOptions with list
235    # values (more than one value) not to be able to be restored from
236    # a saved option file.  If this causes trouble then upgrade to
237    # scons 0.96.90 or later.
238    ListOption('CPU_MODELS', 'CPU models', 'all', env['ALL_CPU_LIST']),
239    BoolOption('ALPHA_TLASER',
240               'Model Alpha TurboLaser platform (vs. Tsunami)', False),
241    BoolOption('NO_FAST_ALLOC', 'Disable fast object allocator', False),
242    BoolOption('EFENCE', 'Link with Electric Fence malloc debugger',
243               False),
244    BoolOption('SS_COMPATIBLE_FP',
245               'Make floating-point results compatible with SimpleScalar',
246               False),
247    BoolOption('USE_SSE2',
248               'Compile for SSE2 (-msse2) to get IEEE FP on x86 hosts',
249               False),
250    BoolOption('STATS_BINNING', 'Bin statistics by CPU mode', have_mysql),
251    BoolOption('USE_MYSQL', 'Use MySQL for stats output', have_mysql),
252    BoolOption('USE_FENV', 'Use <fenv.h> IEEE mode control', have_fenv),
253    ('CC', 'C compiler', os.environ.get('CC', env['CC'])),
254    ('CXX', 'C++ compiler', os.environ.get('CXX', env['CXX'])),
255    BoolOption('BATCH', 'Use batch pool for build and tests', False),
256    ('BATCH_CMD', 'Batch pool submission command name', 'qdo')
257    )
258
259# Non-sticky options only apply to the current build.
260nonsticky_opts = Options(args=ARGUMENTS)
261nonsticky_opts.AddOptions(
262    BoolOption('update_ref', 'Update test reference outputs', False)
263    )
264
265# These options get exported to #defines in config/*.hh (see m5/SConscript).
266env.ExportOptions = ['FULL_SYSTEM', 'ALPHA_TLASER', 'USE_FENV', \
267                     'USE_MYSQL', 'NO_FAST_ALLOC', 'SS_COMPATIBLE_FP', \
268                     'STATS_BINNING']
269
270# Define a handy 'no-op' action
271def no_action(target, source, env):
272    return 0
273
274env.NoAction = Action(no_action, None)
275
276# libelf build is described in its own SConscript file.
277# SConscript-global is the build in build/libelf shared among all
278# configs.
279env.SConscript('m5/libelf/SConscript-global', exports = 'env')
280
281###################################################
282#
283# Define a SCons builder for configuration flag headers.
284#
285###################################################
286
287# This function generates a config header file that #defines the
288# option symbol to the current option setting (0 or 1).  The source
289# operands are the name of the option and a Value node containing the
290# value of the option.
291def build_config_file(target, source, env):
292    (option, value) = [s.get_contents() for s in source]
293    f = file(str(target[0]), 'w')
294    print >> f, '#define', option, value
295    f.close()
296    return None
297
298# Generate the message to be printed when building the config file.
299def build_config_file_string(target, source, env):
300    (option, value) = [s.get_contents() for s in source]
301    return "Defining %s as %s in %s." % (option, value, target[0])
302
303# Combine the two functions into a scons Action object.
304config_action = Action(build_config_file, build_config_file_string)
305
306# The emitter munges the source & target node lists to reflect what
307# we're really doing.
308def config_emitter(target, source, env):
309    # extract option name from Builder arg
310    option = str(target[0])
311    # True target is config header file
312    target = os.path.join('config', option.lower() + '.hh')
313    # Force value to 0/1 even if it's a Python bool
314    val = int(eval(str(env[option])))
315    # Sources are option name & value (packaged in SCons Value nodes)
316    return ([target], [Value(option), Value(val)])
317
318config_builder = Builder(emitter = config_emitter, action = config_action)
319
320env.Append(BUILDERS = { 'ConfigFile' : config_builder })
321
322###################################################
323#
324# Define build environments for selected configurations.
325#
326###################################################
327
328# rename base env
329base_env = env
330
331for build_dir in build_dirs:
332    # Make a copy of the default environment to use for this config.
333    env = base_env.Copy()
334
335    # Record what build_dir was in the environment
336    env.Append(BUILD_DIR=build_dir);
337
338    # Set env according to the build directory config.
339
340    sticky_opts.files = []
341    # Name of default options file is taken from 'default=' on command
342    # line if set, otherwise name of build dir.
343    default_options_file = os.path.join('default_options',
344                                        ARGUMENTS.get('default', build_dir))
345    if os.path.isfile(default_options_file):
346        sticky_opts.files.append(default_options_file)
347    current_options_file = os.path.join('options', build_dir)
348    if os.path.isfile(current_options_file):
349        sticky_opts.files.append(current_options_file)
350    else:
351        # if file doesn't exist, make sure at least the directory is there
352        # so we can create it later
353        opt_dir = os.path.dirname(current_options_file)
354        if not os.path.isdir(opt_dir):
355            os.mkdir(opt_dir)
356    if not sticky_opts.files:
357        print "%s: No options file found in options, using defaults." \
358              % build_dir
359
360    # Apply current option settings to env
361    sticky_opts.Update(env)
362    nonsticky_opts.Update(env)
363
364    # Process option settings.
365
366    if not have_fenv and env['USE_FENV']:
367        print "Warning: <fenv.h> not available; " \
368              "forcing USE_FENV to False in", build_dir + "."
369        env['USE_FENV'] = False
370
371    if not env['USE_FENV']:
372        print "Warning: No IEEE FP rounding mode control in", build_dir + "."
373        print "         FP results may deviate slightly from other platforms."
374
375    if env['EFENCE']:
376        env.Append(LIBS=['efence'])
377
378    if env['USE_MYSQL']:
379        if not have_mysql:
380            print "Warning: MySQL not available; " \
381                  "forcing USE_MYSQL to False in", build_dir + "."
382            env['USE_MYSQL'] = False
383        else:
384            print "Compiling in", build_dir, "with MySQL support."
385            env.ParseConfig(mysql_config_libs)
386            env.ParseConfig(mysql_config_include)
387
388    # Save sticky option settings back to current options file
389    sticky_opts.Save(current_options_file, env)
390
391    # Do this after we save setting back, or else we'll tack on an
392    # extra 'qdo' every time we run scons.
393    if env['BATCH']:
394        env['CC']  = env['BATCH_CMD'] + ' ' + env['CC']
395        env['CXX'] = env['BATCH_CMD'] + ' ' + env['CXX']
396
397    if env['USE_SSE2']:
398        env.Append(CCFLAGS='-msse2')
399
400    # The m5/SConscript file sets up the build rules in 'env' according
401    # to the configured options.  It returns a list of environments,
402    # one for each variant build (debug, opt, etc.)
403    envList = SConscript('m5/SConscript', build_dir = build_dir,
404                         exports = 'env', duplicate = False)
405
406    # Set up the regression tests for each build.
407    for e in envList:
408        SConscript('m5-test/SConscript',
409                   build_dir = os.path.join(build_dir, 'test', e.Label),
410                   exports = { 'env' : e }, duplicate = False)
411
412###################################################
413#
414# Let SCons do its thing.  At this point SCons will use the defined
415# build environments to build the requested targets.
416#
417###################################################
418
419