SConstruct revision 1888
12086SN/A# -*- mode:python -*-
22086SN/A
32086SN/A# Copyright (c) 2004-2005 The Regents of The University of Michigan
42086SN/A# All rights reserved.
52086SN/A#
62086SN/A# Redistribution and use in source and binary forms, with or without
72086SN/A# modification, are permitted provided that the following conditions are
82086SN/A# met: redistributions of source code must retain the above copyright
92086SN/A# notice, this list of conditions and the following disclaimer;
102086SN/A# redistributions in binary form must reproduce the above copyright
112086SN/A# notice, this list of conditions and the following disclaimer in the
122086SN/A# documentation and/or other materials provided with the distribution;
132086SN/A# neither the name of the copyright holders nor the names of its
142086SN/A# contributors may be used to endorse or promote products derived from
152086SN/A# this software without specific prior written permission.
162086SN/A#
172086SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
182086SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
192086SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
202086SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
212086SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
222086SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
232086SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
242086SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
252086SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
262086SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
272086SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
282665Ssaidi@eecs.umich.edu
292665Ssaidi@eecs.umich.edu###################################################
302665Ssaidi@eecs.umich.edu#
312086SN/A# SCons top-level build description (SConstruct) file.
324202Sbinkertn@umich.edu#
332086SN/A# To build M5, you need a directory with three things:
344202Sbinkertn@umich.edu# 1. A copy of this file (named SConstruct).
354202Sbinkertn@umich.edu# 2. A link named 'm5' to the top of the M5 simulator source tree.
364202Sbinkertn@umich.edu# 3. A link named 'ext' to the top of the M5 external source tree.
378745Sgblack@eecs.umich.edu#
386313Sgblack@eecs.umich.edu# Then type 'scons' to build the default configuration (see below), or
396365Sgblack@eecs.umich.edu# 'scons <CONFIG>/<binary>' to build some other configuration (e.g.,
404997Sgblack@eecs.umich.edu# 'ALPHA_FS/m5.opt' for the optimized full-system version).
414202Sbinkertn@umich.edu#
424997Sgblack@eecs.umich.edu###################################################
438747Sgblack@eecs.umich.edu
444826Ssaidi@eecs.umich.edu# Python library imports
458760Sgblack@eecs.umich.eduimport sys
462086SN/Aimport os
478745Sgblack@eecs.umich.edu
486365Sgblack@eecs.umich.edu# Check for recent-enough Python and SCons versions
498745Sgblack@eecs.umich.eduEnsurePythonVersion(2,3)
506365Sgblack@eecs.umich.eduEnsureSConsVersion(0,96)
518335Snate@binkert.org
528335Snate@binkert.org# The absolute path to the current directory (where this file lives).
534997Sgblack@eecs.umich.eduROOT = Dir('.').abspath
544202Sbinkertn@umich.edu
554486Sbinkertn@umich.edu# Paths to the M5 and external source trees (local symlinks).
564486Sbinkertn@umich.eduSRCDIR = os.path.join(ROOT, 'm5')
574202Sbinkertn@umich.eduEXT_SRCDIR = os.path.join(ROOT, 'ext')
584202Sbinkertn@umich.edu
594202Sbinkertn@umich.edu# Check for 'm5' and 'ext' links, die if they don't exist.
602086SN/Aif not os.path.isdir(SRCDIR):
614202Sbinkertn@umich.edu    print "Error: '%s' must be a link to the M5 source tree." % SRCDIR
624202Sbinkertn@umich.edu    Exit(1)
634202Sbinkertn@umich.edu
642086SN/Aif not os.path.isdir('ext'):
654202Sbinkertn@umich.edu    print "Error: '%s' must be a link to the M5 external source tree." \
664202Sbinkertn@umich.edu          % EXT_SRCDIR
672086SN/A    Exit(1)
684202Sbinkertn@umich.edu
694202Sbinkertn@umich.edu# tell python where to find m5 python code
704202Sbinkertn@umich.edusys.path.append(os.path.join(SRCDIR, 'python'))
714202Sbinkertn@umich.edu
724202Sbinkertn@umich.edu###################################################
734202Sbinkertn@umich.edu#
74# Figure out which configurations to set up.
75#
76#
77# It's prohibitive to do all the combinations of base configurations
78# and options, so we have to infer which ones the user wants.
79#
80# 1. If there are command-line targets, the configuration(s) are inferred
81#    from the directories of those targets.  If scons was invoked from a
82#    subdirectory (using 'scons -u'), those targets have to be
83#    interpreted relative to that subdirectory.
84#
85# 2. If there are no command-line targets, and scons was invoked from a
86#    subdirectory (using 'scons -u'), the configuration is inferred from
87#    the name of the subdirectory.
88#
89# 3. If there are no command-line targets and scons was invoked from
90#    the root build directory, a default configuration is used.  The
91#    built-in default is ALPHA_SE, but this can be overridden by setting the
92#    M5_DEFAULT_CONFIG shell environment veriable.
93#
94# In cases 2 & 3, the specific file target defaults to 'm5.debug', but
95# this can be overridden by setting the M5_DEFAULT_BINARY shell
96# environment veriable.
97#
98###################################################
99
100# Find default configuration & binary.
101default_config = os.environ.get('M5_DEFAULT_CONFIG', 'ALPHA_SE')
102default_binary = os.environ.get('M5_DEFAULT_BINARY', 'm5.debug')
103
104# Ask SCons which directory it was invoked from.  If you invoke SCons
105# from a subdirectory you must use the '-u' flag.
106launch_dir = GetLaunchDir()
107
108# Build a list 'my_targets' of all the targets relative to ROOT.
109if launch_dir == ROOT:
110    # invoked from root build dir
111    if len(COMMAND_LINE_TARGETS) != 0:
112        # easy: use specified targets as is
113        my_targets = COMMAND_LINE_TARGETS
114    else:
115        # default target (ALPHA_SE/m5.debug, unless overridden)
116        target = os.path.join(default_config, default_binary)
117        my_targets = [target]
118        Default(target)
119else:
120    # invoked from subdirectory
121    if not launch_dir.startswith(ROOT):
122        print "Error: launch dir (%s) not a subdirectory of ROOT (%s)!" \
123              (launch_dir, ROOT)
124        Exit(1)
125    # make launch_dir relative to ROOT (strip ROOT plus slash off front)
126    launch_dir = launch_dir[len(ROOT)+1:]
127    if len(COMMAND_LINE_TARGETS) != 0:
128        # make specified targets relative to ROOT
129        my_targets = map(lambda x: os.path.join(launch_dir, x),
130                         COMMAND_LINE_TARGETS)
131    else:
132        # build default binary (m5.debug, unless overridden) using the
133        # config inferred by the invocation directory (the first
134        # subdirectory after ROOT)
135        target = os.path.join(launch_dir.split('/')[0], default_binary)
136        my_targets = [target]
137        Default(target)
138
139# Normalize target paths (gets rid of '..' in the middle, etc.)
140my_targets = map(os.path.normpath, my_targets)
141
142# Generate a list of the unique configs that the collected targets reference.
143build_dirs = []
144for t in my_targets:
145    dir = t.split('/')[0]
146    if dir not in build_dirs:
147        build_dirs.append(dir)
148
149###################################################
150#
151# Set up the default build environment.  This environment is copied
152# and modified according to each selected configuration.
153#
154###################################################
155
156env = Environment(ENV = os.environ,  # inherit user's environment vars
157                  ROOT = ROOT,
158                  SRCDIR = SRCDIR,
159                  EXT_SRCDIR = EXT_SRCDIR)
160
161env.SConsignFile("sconsign")
162
163# I waffle on this setting... it does avoid a few painful but
164# unnecessary builds, but it also seems to make trivial builds take
165# noticeably longer.
166if False:
167    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 < 3 or \
206           mysql_major == 3 and mysql_minor < 23 or \
207           mysql_major == 4 and mysql_minor < 1:
208        print "Warning: MySQL v3.23 or v4.1 or newer required."
209        have_mysql = False
210
211# Set up mysql_config commands.
212if have_mysql:
213    mysql_config_include = mysql_config + ' --include'
214    if os.system(mysql_config_include + ' > /dev/null') != 0:
215        # older mysql_config versions don't support --include, use
216        # --cflags instead
217        mysql_config_include = mysql_config + ' --cflags | sed s/\\\'//g'
218    # This seems to work in all versions
219    mysql_config_libs = mysql_config + ' --libs'
220
221env = conf.Finish()
222
223# Sticky options get saved in the options file so they persist from
224# one invocation to the next (unless overridden, in which case the new
225# value becomes sticky).
226sticky_opts = Options(args=ARGUMENTS)
227sticky_opts.AddOptions(
228    EnumOption('TARGET_ISA', 'Target ISA', 'alpha', ('alpha')),
229    BoolOption('FULL_SYSTEM', 'Full-system support', False),
230    BoolOption('ALPHA_TLASER',
231               'Model Alpha TurboLaser platform (vs. Tsunami)', False),
232    BoolOption('NO_FAST_ALLOC', 'Disable fast object allocator', False),
233    BoolOption('EFENCE', 'Link with Electric Fence malloc debugger',
234               False),
235    BoolOption('SS_COMPATIBLE_FP',
236               'Make floating-point results compatible with SimpleScalar',
237               False),
238    BoolOption('STATS_BINNING', 'Bin statistics by CPU mode', have_mysql),
239    BoolOption('USE_MYSQL', 'Use MySQL for stats output', have_mysql),
240    BoolOption('USE_FENV', 'Use <fenv.h> IEEE mode control', have_fenv),
241    ('CC', 'C compiler', os.environ.get('CC', env['CC'])),
242    ('CXX', 'C++ compiler', os.environ.get('CXX', env['CXX'])),
243    BoolOption('BATCH', 'Use batch pool for build and tests', False),
244    ('BATCH_CMD', 'Batch pool submission command name', 'qdo')
245    )
246
247# Non-sticky options only apply to the current build.
248nonsticky_opts = Options(args=ARGUMENTS)
249nonsticky_opts.AddOptions(
250    BoolOption('update_ref', 'Update test reference outputs', False)
251    )
252
253# These options get exported to #defines in config/*.hh (see m5/SConscript).
254env.ExportOptions = ['FULL_SYSTEM', 'ALPHA_TLASER', 'USE_FENV', \
255                     'USE_MYSQL', 'NO_FAST_ALLOC', 'SS_COMPATIBLE_FP', \
256                     'STATS_BINNING']
257
258# Define a handy 'no-op' action
259def no_action(target, source, env):
260    return 0
261
262env.NoAction = Action(no_action, None)
263
264# libelf build is described in its own SConscript file.
265# SConscript-global is the build in build/libelf shared among all
266# configs.
267env.SConscript('m5/libelf/SConscript-global', exports = 'env')
268
269###################################################
270#
271# Define a SCons builder for configuration flag headers.
272#
273###################################################
274
275# This function generates a config header file that #defines the
276# option symbol to the current option setting (0 or 1).  The source
277# operands are the name of the option and a Value node containing the
278# value of the option.
279def build_config_file(target, source, env):
280    (option, value) = [s.get_contents() for s in source]
281    f = file(str(target[0]), 'w')
282    print >> f, '#define', option, value
283    f.close()
284    return None
285
286# Generate the message to be printed when building the config file.
287def build_config_file_string(target, source, env):
288    (option, value) = [s.get_contents() for s in source]
289    return "Defining %s as %s in %s." % (option, value, target[0])
290
291# Combine the two functions into a scons Action object.
292config_action = Action(build_config_file, build_config_file_string)
293
294# The emitter munges the source & target node lists to reflect what
295# we're really doing.
296def config_emitter(target, source, env):
297    # extract option name from Builder arg
298    option = str(target[0])
299    # True target is config header file
300    target = os.path.join('config', option.lower() + '.hh')
301    # Force value to 0/1 even if it's a Python bool
302    val = int(eval(str(env[option])))
303    # Sources are option name & value (packaged in SCons Value nodes)
304    return ([target], [Value(option), Value(val)])
305
306config_builder = Builder(emitter = config_emitter, action = config_action)
307
308env.Append(BUILDERS = { 'ConfigFile' : config_builder })
309
310###################################################
311#
312# Define build environments for selected configurations.
313#
314###################################################
315
316# rename base env
317base_env = env
318
319for build_dir in build_dirs:
320    # Make a copy of the default environment to use for this config.
321    env = base_env.Copy()
322    # Set env according to the build directory config.
323
324    sticky_opts.files = []
325    default_options_file = os.path.join('build_options', 'default', build_dir)
326    if os.path.isfile(default_options_file):
327        sticky_opts.files.append(default_options_file)
328    current_options_file = os.path.join('build_options', 'current', build_dir)
329    if os.path.isfile(current_options_file):
330        sticky_opts.files.append(current_options_file)
331    if not sticky_opts.files:
332        print "%s: No options file found in build_options, using defaults." \
333              % build_dir
334
335    # Apply current option settings to env
336    sticky_opts.Update(env)
337    nonsticky_opts.Update(env)
338
339    # Process option settings.
340
341    if not have_fenv and env['USE_FENV']:
342        print "Warning: <fenv.h> not available; " \
343              "forcing USE_FENV to False in", build_dir + "."
344        env['USE_FENV'] = False
345
346    if not env['USE_FENV']:
347        print "Warning: No IEEE FP rounding mode control in", build_dir + "."
348        print "         FP results may deviate slightly from other platforms."
349
350    if env['EFENCE']:
351        env.Append(LIBS=['efence'])
352
353    if env['USE_MYSQL']:
354        if not have_mysql:
355            print "Warning: MySQL not available; " \
356                  "forcing USE_MYSQL to False in", build_dir + "."
357            env['USE_MYSQL'] = False
358        else:
359            print "Compiling in", build_dir, "with MySQL support."
360            env.ParseConfig(mysql_config_libs)
361            env.ParseConfig(mysql_config_include)
362
363    # Save sticky option settings back to current options file
364    sticky_opts.Save(current_options_file, env)
365
366    # Do this after we save setting back, or else we'll tack on an
367    # extra 'qdo' every time we run scons.
368    if env['BATCH']:
369        env['CC']  = env['BATCH_CMD'] + ' ' + env['CC']
370        env['CXX'] = env['BATCH_CMD'] + ' ' + env['CXX']
371
372    # The m5/SConscript file sets up the build rules in 'env' according
373    # to the configured options.  It returns a list of environments,
374    # one for each variant build (debug, opt, etc.)
375    envList = SConscript('m5/SConscript', build_dir = build_dir,
376                         exports = 'env', duplicate = False)
377
378    # Set up the regression tests for each build.
379    for e in envList:
380        SConscript('m5-test/SConscript',
381                   build_dir = os.path.join(build_dir, 'test', e.Label),
382                   exports = { 'env' : e }, duplicate = False)
383
384###################################################
385#
386# Let SCons do its thing.  At this point SCons will use the defined
387# build environments to build the requested targets.
388#
389###################################################
390
391