SConstruct revision 1859
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
272665SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
282665SN/A
292SN/A###################################################
302SN/A#
314826Ssaidi@eecs.umich.edu# SCons top-level build description (SConstruct) file.
324826Ssaidi@eecs.umich.edu#
332SN/A# To build M5, you need a directory with three things:
346216Snate@binkert.org# 1. A copy of this file (named SConstruct).
352SN/A# 2. A link named 'm5' to the top of the M5 simulator source tree.
364826Ssaidi@eecs.umich.edu# 3. A link named 'ext' to the top of the M5 external source tree.
3774SN/A#
386216Snate@binkert.org# Then type 'scons' to build the default configuration (see below), or
394084SN/A# 'scons <CONFIG>/<binary>' to build some other configuration (e.g.,
402SN/A# 'ALPHA_FS/m5.opt' for the optimized full-system version).
412680SN/A#
422SN/A###################################################
433535SN/A
442SN/A# Python library imports
452SN/Aimport sys
462680SN/Aimport os
472SN/A
482SN/A# Check for recent-enough Python and SCons versions
492SN/AEnsurePythonVersion(2,3)
502SN/AEnsureSConsVersion(0,96)
512SN/A
522SN/A# The absolute path to the current directory (where this file lives).
532SN/AROOT = Dir('.').abspath
542SN/A
552SN/A# Paths to the M5 and external source trees (local symlinks).
562SN/ASRCDIR = os.path.join(ROOT, 'm5')
572SN/AEXT_SRCDIR = os.path.join(ROOT, 'ext')
582SN/A
592SN/A# Check for 'm5' and 'ext' links, die if they don't exist.
602SN/Aif not os.path.isdir(SRCDIR):
612SN/A    print "Error: '%s' must be a link to the M5 source tree." % SRCDIR
622SN/A    Exit(1)
632SN/A
642SN/Aif not os.path.isdir('ext'):
652SN/A    print "Error: '%s' must be a link to the M5 external source tree." \
662SN/A          % EXT_SRCDIR
673535SN/A    Exit(1)
682680SN/A
692SN/A# tell python where to find m5 python code
703535SN/Asys.path.append(os.path.join(SRCDIR, 'python'))
712680SN/A
723535SN/A###################################################
732SN/A#
742680SN/A# Figure out which configurations to set up.
752SN/A#
763535SN/A#
772680SN/A# It's prohibitive to do all the combinations of base configurations
782SN/A# and options, so we have to infer which ones the user wants.
792SN/A#
802SN/A# 1. If there are command-line targets, the configuration(s) are inferred
812SN/A#    from the directories of those targets.  If scons was invoked from a
822SN/A#    subdirectory (using 'scons -u'), those targets have to be
834826Ssaidi@eecs.umich.edu#    interpreted relative to that subdirectory.
844826Ssaidi@eecs.umich.edu#
854826Ssaidi@eecs.umich.edu# 2. If there are no command-line targets, and scons was invoked from a
864826Ssaidi@eecs.umich.edu#    subdirectory (using 'scons -u'), the configuration is inferred from
874826Ssaidi@eecs.umich.edu#    the name of the subdirectory.
883535SN/A#
892SN/A# 3. If there are no command-line targets and scons was invoked from
902SN/A#    the root build directory, a default configuration is used.  The
912SN/A#    built-in default is ALPHA_SE, but this can be overridden by setting the
922SN/A#    M5_DEFAULT_CONFIG shell environment veriable.
932SN/A#
943535SN/A# In cases 2 & 3, the specific file target defaults to 'm5.debug', but
953535SN/A# this can be overridden by setting the M5_DEFAULT_BINARY shell
962SN/A# environment veriable.
972SN/A#
982SN/A###################################################
992SN/A
1002SN/A# Find default configuration & binary.
1013535SN/Adefault_config = os.environ.get('M5_DEFAULT_CONFIG', 'ALPHA_SE')
1022SN/Adefault_binary = os.environ.get('M5_DEFAULT_BINARY', 'm5.debug')
1032SN/A
1042SN/A# Ask SCons which directory it was invoked from.  If you invoke SCons
1052SN/A# from a subdirectory you must use the '-u' flag.
1062SN/Alaunch_dir = GetLaunchDir()
1073535SN/A
1083535SN/A# Build a list 'my_targets' of all the targets relative to ROOT.
1092SN/Aif launch_dir == ROOT:
1102SN/A    # invoked from root build dir
1112SN/A    if len(COMMAND_LINE_TARGETS) != 0:
1122SN/A        # easy: use specified targets as is
1132SN/A        my_targets = COMMAND_LINE_TARGETS
1143535SN/A    else:
1152SN/A        # default target (ALPHA_SE/m5.debug, unless overridden)
1162SN/A        target = os.path.join(default_config, default_binary)
1172SN/A        my_targets = [target]
1182SN/A        Default(target)
1192SN/Aelse:
1203535SN/A    # invoked from subdirectory
1212SN/A    if not launch_dir.startswith(ROOT):
1222SN/A        print "Error: launch dir (%s) not a subdirectory of ROOT (%s)!" \
1232SN/A              (launch_dir, ROOT)
1242SN/A        Exit(1)
1252SN/A    # make launch_dir relative to ROOT (strip ROOT plus slash off front)
1263535SN/A    launch_dir = launch_dir[len(ROOT)+1:]
1273535SN/A    if len(COMMAND_LINE_TARGETS) != 0:
1282SN/A        # make specified targets relative to ROOT
1292SN/A        my_targets = map(lambda x: os.path.join(launch_dir, x),
1302SN/A                         COMMAND_LINE_TARGETS)
1312SN/A    else:
1322SN/A        # build default binary (m5.debug, unless overridden) using the
1332SN/A        # config inferred by the invocation directory (the first
1342SN/A        # subdirectory after ROOT)
1352SN/A        target = os.path.join(launch_dir.split('/')[0], default_binary)
1362SN/A        my_targets = [target]
1372SN/A        Default(target)
1382SN/A
1392SN/A# Normalize target paths (gets rid of '..' in the middle, etc.)
1402680SN/Amy_targets = map(os.path.normpath, my_targets)
1412SN/A
1422SN/A# Generate a list of the unique configs that the collected targets reference.
1432SN/Abuild_dirs = []
1442SN/Afor t in my_targets:
1452SN/A    dir = t.split('/')[0]
1462680SN/A    if dir not in build_dirs:
1472SN/A        build_dirs.append(dir)
1482SN/A
1492SN/A# Make a first pass to verify that build dirs are valid
1502SN/Afor build_dir in build_dirs:
1514826Ssaidi@eecs.umich.edu    if not os.path.isdir(build_dir):
152        print "Error: build directory", build_dir, "does not exist."
153        Exit(1)
154
155###################################################
156#
157# Set up the default build environment.  This environment is copied
158# and modified according to each selected configuration.
159#
160###################################################
161
162env = Environment(ENV = os.environ,  # inherit user's environment vars
163                  ROOT = ROOT,
164                  SRCDIR = SRCDIR,
165                  EXT_SRCDIR = EXT_SRCDIR)
166
167env.SConsignFile("sconsign")
168
169if os.environ.has_key('CC'):
170    env.Replace(CC=os.environ['CC'])
171
172if os.environ.has_key('CXX'):
173    env.Replace(CXX=os.environ['CXX'])
174
175# M5_EXT is used by isa_parser.py to find the PLY package.
176env.Append(ENV = { 'M5_EXT' : EXT_SRCDIR })
177
178# Set up default C++ compiler flags
179env.Append(CCFLAGS='-pipe')
180env.Append(CCFLAGS='-fno-strict-aliasing')
181env.Append(CCFLAGS=Split('-Wall -Wno-sign-compare -Werror -Wundef'))
182if sys.platform == 'cygwin':
183    # cygwin has some header file issues...
184    env.Append(CCFLAGS=Split("-Wno-uninitialized"))
185env.Append(CPPPATH=[os.path.join(EXT_SRCDIR + '/dnet')])
186
187# Default libraries
188env.Append(LIBS=['z'])
189
190# Platform-specific configuration
191conf = Configure(env)
192
193# Check for <fenv.h> (C99 FP environment control)
194have_fenv = conf.CheckHeader('fenv.h', '<>')
195if not have_fenv:
196    print "Warning: Header file <fenv.h> not found."
197    print "         This host has no IEEE FP rounding mode control."
198
199# Check for mysql.
200mysql_config = WhereIs('mysql_config')
201have_mysql = mysql_config != None
202
203# Check MySQL version.
204if have_mysql:
205    mysql_vers = os.popen(mysql_config + ' --version').read()
206    mv = [int(v) for v in mysql_vers.split('.')]
207    # This version check is probably overly conservative, but it deals
208    # with the versions we have installed.
209    if mv[0] < 3 or (mv[0] == 3 and mv[1] < 23) or (mv[0] == 4 and mv[1] < 1):
210        print "Warning: MySQL v3.23 or v4.1 or newer required."
211        have_mysql = False
212
213# Set up mysql_config commands.
214if have_mysql:
215    mysql_config_include = mysql_config + ' --include'
216    if os.system(mysql_config_include + ' > /dev/null') != 0:
217        # older mysql_config versions don't support --include, use
218        # --cflags instead
219        mysql_config_include = mysql_config + ' --cflags'
220    # This seems to work in all versions
221    mysql_config_libs = mysql_config + ' --libs'
222
223env = conf.Finish()
224
225# The source operand is a Value node containing the value of the option.
226def build_config_file(target, source, env, option):
227    f = file(str(target[0]), 'w')
228    print >> f, '#define', option, source[0]
229    f.close()
230    return None
231
232def config_builder(env, option):
233    target = os.path.join('config', option.lower() + '.hh')
234    source = Value(env[option])
235    def my_build_config_file(target, source, env):
236        build_config_file(target, source, env, option)
237    env.Command(target, source, my_build_config_file)
238
239env.Append(BUILDERS = { 'ConfigFile' : config_builder })
240
241# libelf build is described in its own SConscript file.
242# SConscript-global is the build in build/libelf shared among all
243# configs.
244env.SConscript('m5/libelf/SConscript-global', exports = 'env')
245
246###################################################
247#
248# Define build environments for selected configurations.
249#
250###################################################
251
252# rename base env
253base_env = env
254
255for build_dir in build_dirs:
256    # Make a copy of the default environment to use for this config.
257    env = base_env.Copy()
258    # Set env according to the build directory config.
259    options_file = os.path.join(build_dir, 'build_options')
260    opts = Options(options_file, ARGUMENTS)
261    opts.AddOptions(
262        EnumOption('TARGET_ISA', 'Target ISA', 'alpha', ('alpha')),
263        BoolOption('FULL_SYSTEM', 'Full-system support', False),
264        BoolOption('ALPHA_TLASER',
265                   'Model Alpha TurboLaser platform (vs. Tsunami)', False),
266        BoolOption('NO_FAST_ALLOC', 'Disable fast object allocator', False),
267        BoolOption('EFENCE', 'Link with Electric Fence malloc debugger',
268                   False),
269        BoolOption('SS_COMPATIBLE_FP',
270                   'Make floating-point results compatible with SimpleScalar',
271                   False),
272        BoolOption('STATS_BINNING', 'Bin statistics by CPU mode', have_mysql),
273        BoolOption('USE_MYSQL', 'Use MySQL for stats output', have_mysql),
274        BoolOption('USE_FENV', 'Use <fenv.h> IEEE mode control', have_fenv)
275        )
276
277    opts.Update(env)
278    opts.Save(options_file, env)
279
280    env.ExportOptions = ['FULL_SYSTEM', 'ALPHA_TLASER', 'USE_FENV', \
281                         'USE_MYSQL', 'NO_FAST_ALLOC', 'SS_COMPATIBLE_FP', \
282                         'STATS_BINNING']
283
284    # Process option settings.
285
286    if not have_fenv and env['USE_FENV']:
287        print "Warning: <fenv.h> not available; " \
288              "forcing USE_FENV to False in", build_dir + "."
289        env['USE_FENV'] = False
290
291    if not env['USE_FENV']:
292        print "Warning: No IEEE FP rounding mode control in", build_dir + "."
293        print "         FP results may deviate slightly", \
294              "and some regression tests may fail."
295
296    if env['EFENCE']:
297        env.Append(LIBS=['efence'])
298
299    if env['USE_MYSQL']:
300        if not have_mysql:
301            print "Warning: MySQL not available; " \
302                  "forcing USE_MYSQL to False in", build_dir + "."
303            env['USE_MYSQL'] = False
304        else:
305            print "Compiling in", build_dir, "with MySQL support."
306            env.ParseConfig(mysql_config_libs)
307            env.ParseConfig(mysql_config_include)
308    
309    # The m5/SConscript file sets up the build rules in 'env' according
310    # to the configured options.
311    SConscript('m5/SConscript', build_dir = build_dir, exports = 'env',
312               duplicate=0)
313
314###################################################
315#
316# Let SCons do its thing.  At this point SCons will use the defined
317# build environments to build the requested targets.
318#
319###################################################
320
321