SConstruct revision 2623
1# -*- mode:python -*-
2
3# Copyright (c) 2004-2005 The Regents of The University of Michigan
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions are
8# met: redistributions of source code must retain the above copyright
9# notice, this list of conditions and the following disclaimer;
10# redistributions in binary form must reproduce the above copyright
11# notice, this list of conditions and the following disclaimer in the
12# documentation and/or other materials provided with the distribution;
13# neither the name of the copyright holders nor the names of its
14# contributors may be used to endorse or promote products derived from
15# this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29###################################################
30#
31# SCons top-level build description (SConstruct) file.
32#
33# To build M5, you need a directory with three things:
34# 1. A copy of this file (named SConstruct).
35# 2. A link named 'm5' to the top of the M5 simulator source tree.
36# 3. A link named 'ext' to the top of the M5 external source tree.
37#
38# Then type 'scons' to build the default configuration (see below), or
39# 'scons <CONFIG>/<binary>' to build some other configuration (e.g.,
40# 'ALPHA_FS/m5.opt' for the optimized full-system version).
41#
42###################################################
43
44# Python library imports
45import sys
46import os
47
48# Check for recent-enough Python and SCons versions
49EnsurePythonVersion(2,3)
50EnsureSConsVersion(0,96)
51
52# The absolute path to the current directory (where this file lives).
53ROOT = Dir('.').abspath
54
55# Paths to the M5 and external source trees (local symlinks).
56SRCDIR = os.path.join(ROOT, 'm5')
57EXT_SRCDIR = os.path.join(ROOT, 'ext')
58
59# Check for 'm5' and 'ext' links, die if they don't exist.
60if not os.path.isdir(SRCDIR):
61    print "Error: '%s' must be a link to the M5 source tree." % SRCDIR
62    Exit(1)
63
64if not os.path.isdir('ext'):
65    print "Error: '%s' must be a link to the M5 external source tree." \
66          % EXT_SRCDIR
67    Exit(1)
68
69# tell python where to find m5 python code
70sys.path.append(os.path.join(SRCDIR, 'python'))
71
72###################################################
73#
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 < 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'] = ['AtomicSimpleCPU', 'TimingSimpleCPU',
226                       'FastCPU', 'FullCPU', 'AlphaFullCPU']
227
228# Sticky options get saved in the options file so they persist from
229# one invocation to the next (unless overridden, in which case the new
230# value becomes sticky).
231sticky_opts = Options(args=ARGUMENTS)
232sticky_opts.AddOptions(
233    EnumOption('TARGET_ISA', 'Target ISA', 'alpha', env['ALL_ISA_LIST']),
234    BoolOption('FULL_SYSTEM', 'Full-system support', False),
235    # There's a bug in scons 0.96.1 that causes ListOptions with list
236    # values (more than one value) not to be able to be restored from
237    # a saved option file.  If this causes trouble then upgrade to
238    # scons 0.96.90 or later.
239    ListOption('CPU_MODELS', 'CPU models', 'all', env['ALL_CPU_LIST']),
240    BoolOption('ALPHA_TLASER',
241               'Model Alpha TurboLaser platform (vs. Tsunami)', False),
242    BoolOption('NO_FAST_ALLOC', 'Disable fast object allocator', False),
243    BoolOption('EFENCE', 'Link with Electric Fence malloc debugger',
244               False),
245    BoolOption('SS_COMPATIBLE_FP',
246               'Make floating-point results compatible with SimpleScalar',
247               False),
248    BoolOption('USE_SSE2',
249               'Compile for SSE2 (-msse2) to get IEEE FP on x86 hosts',
250               False),
251    BoolOption('STATS_BINNING', 'Bin statistics by CPU mode', have_mysql),
252    BoolOption('USE_MYSQL', 'Use MySQL for stats output', have_mysql),
253    BoolOption('USE_FENV', 'Use <fenv.h> IEEE mode control', have_fenv),
254    ('CC', 'C compiler', os.environ.get('CC', env['CC'])),
255    ('CXX', 'C++ compiler', os.environ.get('CXX', env['CXX'])),
256    BoolOption('BATCH', 'Use batch pool for build and tests', False),
257    ('BATCH_CMD', 'Batch pool submission command name', 'qdo')
258    )
259
260# Non-sticky options only apply to the current build.
261nonsticky_opts = Options(args=ARGUMENTS)
262nonsticky_opts.AddOptions(
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
332help_text = '''
333Usage: scons [scons options] [build options] [target(s)]
334
335'''
336
337for build_dir in build_dirs:
338    # Make a copy of the default environment to use for this config.
339    env = base_env.Copy()
340
341    # Record what build_dir was in the environment
342    env.Append(BUILD_DIR=build_dir);
343
344    # Set env according to the build directory config.
345
346    sticky_opts.files = []
347    # Name of default options file is taken from 'default=' on command
348    # line if set, otherwise name of build dir.
349    default_options_file = os.path.join('default_options',
350                                        ARGUMENTS.get('default', build_dir))
351    if os.path.isfile(default_options_file):
352        sticky_opts.files.append(default_options_file)
353    current_options_file = os.path.join('options', build_dir)
354    if os.path.isfile(current_options_file):
355        sticky_opts.files.append(current_options_file)
356    else:
357        # if file doesn't exist, make sure at least the directory is there
358        # so we can create it later
359        opt_dir = os.path.dirname(current_options_file)
360        if not os.path.isdir(opt_dir):
361            os.mkdir(opt_dir)
362    if not sticky_opts.files:
363        print "%s: No options file found in options, using defaults." \
364              % build_dir
365
366    # Apply current option settings to env
367    sticky_opts.Update(env)
368    nonsticky_opts.Update(env)
369
370    help_text += "Sticky options for %s:\n" % build_dir \
371                 + sticky_opts.GenerateHelpText(env) \
372                 + "\nNon-sticky options for %s:\n" % build_dir \
373                 + nonsticky_opts.GenerateHelpText(env)
374
375    # Process option settings.
376
377    if not have_fenv and env['USE_FENV']:
378        print "Warning: <fenv.h> not available; " \
379              "forcing USE_FENV to False in", build_dir + "."
380        env['USE_FENV'] = False
381
382    if not env['USE_FENV']:
383        print "Warning: No IEEE FP rounding mode control in", build_dir + "."
384        print "         FP results may deviate slightly from other platforms."
385
386    if env['EFENCE']:
387        env.Append(LIBS=['efence'])
388
389    if env['USE_MYSQL']:
390        if not have_mysql:
391            print "Warning: MySQL not available; " \
392                  "forcing USE_MYSQL to False in", build_dir + "."
393            env['USE_MYSQL'] = False
394        else:
395            print "Compiling in", build_dir, "with MySQL support."
396            env.ParseConfig(mysql_config_libs)
397            env.ParseConfig(mysql_config_include)
398
399    # Save sticky option settings back to current options file
400    sticky_opts.Save(current_options_file, env)
401
402    # Do this after we save setting back, or else we'll tack on an
403    # extra 'qdo' every time we run scons.
404    if env['BATCH']:
405        env['CC']  = env['BATCH_CMD'] + ' ' + env['CC']
406        env['CXX'] = env['BATCH_CMD'] + ' ' + env['CXX']
407
408    if env['USE_SSE2']:
409        env.Append(CCFLAGS='-msse2')
410
411    # The m5/SConscript file sets up the build rules in 'env' according
412    # to the configured options.  It returns a list of environments,
413    # one for each variant build (debug, opt, etc.)
414    envList = SConscript('m5/SConscript', build_dir = build_dir,
415                         exports = 'env', duplicate = False)
416
417    # Set up the regression tests for each build.
418    for e in envList:
419        SConscript('m5-test/SConscript',
420                   build_dir = os.path.join(build_dir, 'test', e.Label),
421                   exports = { 'env' : e }, duplicate = False)
422
423Help(help_text)
424
425###################################################
426#
427# Let SCons do its thing.  At this point SCons will use the defined
428# build environments to build the requested targets.
429#
430###################################################
431
432