SConstruct revision 1858
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# Make a first pass to verify that build dirs are valid
150for build_dir in build_dirs:
151    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
203env = conf.Finish()
204
205# The source operand is a Value node containing the value of the option.
206def build_config_file(target, source, env, option):
207    f = file(str(target[0]), 'w')
208    print >> f, '#define', option, source[0]
209    f.close()
210    return None
211
212def config_builder(env, option):
213    target = os.path.join('config', option.lower() + '.hh')
214    source = Value(env[option])
215    def my_build_config_file(target, source, env):
216        build_config_file(target, source, env, option)
217    env.Command(target, source, my_build_config_file)
218
219env.Append(BUILDERS = { 'ConfigFile' : config_builder })
220
221# libelf build is described in its own SConscript file.
222# SConscript-global is the build in build/libelf shared among all
223# configs.
224env.SConscript('m5/libelf/SConscript-global', exports = 'env')
225
226###################################################
227#
228# Define build environments for selected configurations.
229#
230###################################################
231
232# rename base env
233base_env = env
234
235for build_dir in build_dirs:
236    # Make a copy of the default environment to use for this config.
237    env = base_env.Copy()
238    # Set env according to the build directory config.
239    options_file = os.path.join(build_dir, 'build_options')
240    opts = Options(options_file, ARGUMENTS)
241    opts.AddOptions(
242        EnumOption('TARGET_ISA', 'Target ISA', 'alpha', ('alpha')),
243        BoolOption('FULL_SYSTEM', 'Full-system support', False),
244        BoolOption('ALPHA_TLASER',
245                   'Model Alpha TurboLaser platform (vs. Tsunami)', False),
246        BoolOption('NO_FAST_ALLOC', 'Disable fast object allocator', False),
247        BoolOption('EFENCE', 'Link with Electric Fence malloc debugger',
248                   False),
249        BoolOption('SS_COMPATIBLE_FP',
250                   'Make floating-point results compatible with SimpleScalar',
251                   False),
252        BoolOption('STATS_BINNING', 'Bin statistics by CPU mode', have_mysql),
253        BoolOption('USE_MYSQL', 'Use MySQL for stats output', have_mysql),
254        BoolOption('USE_FENV', 'Use <fenv.h> IEEE mode control', have_fenv)
255        )
256
257    opts.Update(env)
258    opts.Save(options_file, env)
259
260    env.ExportOptions = ['FULL_SYSTEM', 'ALPHA_TLASER', 'USE_FENV', \
261                         'USE_MYSQL', 'NO_FAST_ALLOC', 'SS_COMPATIBLE_FP', \
262                         'STATS_BINNING']
263
264    # Process option settings.
265
266    if not have_fenv and env['USE_FENV']:
267        print "Warning: <fenv.h> not available; " \
268              "forcing USE_FENV to False in", build_dir + "."
269        env['USE_FENV'] = False
270
271    if not env['USE_FENV']:
272        print "Warning: No IEEE FP rounding mode control in", build_dir + "."
273        print "         FP results may deviate slightly", \
274              "and some regression tests may fail."
275
276    if env['EFENCE']:
277        env.Append(LIBS=['efence'])
278
279    if env['USE_MYSQL']:
280        if not have_mysql:
281            print "Warning: MySQL not available; " \
282                  "forcing USE_MYSQL to False in", build_dir + "."
283            env['USE_MYSQL'] = False
284        else:
285            print "Compiling in", build_dir, "with MySQL support."
286            env.ParseConfig(mysql_config + ' --libs')
287            env.ParseConfig(mysql_config + ' --include')
288    
289    # The m5/SConscript file sets up the build rules in 'env' according
290    # to the configured options.
291    SConscript('m5/SConscript', build_dir = build_dir, exports = 'env',
292               duplicate=0)
293
294###################################################
295#
296# Let SCons do its thing.  At this point SCons will use the defined
297# build environments to build the requested targets.
298#
299###################################################
300
301