SConstruct revision 1533
1# -*- mode:python -*-
2
3# Copyright (c) 2004 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# 'KERNEL/m5.opt' for the optimized full-system version).
41#
42###################################################
43
44# Python library imports
45import sys
46import os
47
48# The absolute path to the current directory (where this file lives).
49ROOT = Dir('.').abspath
50
51# Paths to the M5 and external source trees (local symlinks).
52SRCDIR = os.path.join(ROOT, 'm5')
53EXT_SRCDIR = os.path.join(ROOT, 'ext')
54
55# Check for 'm5' and 'ext' links, die if they don't exist.
56if not os.path.isdir(SRCDIR):
57    print "Error: '%s' must be a link to the M5 source tree." % SRCDIR
58    sys.exit(1)
59
60if not os.path.isdir('ext'):
61    print "Error: '%s' must be a link to the M5 external source tree." \
62          % EXT_SRCDIR
63    sys.exit(1)
64
65# tell python where to find m5 python code
66sys.path.append(os.path.join(SRCDIR, 'python'))
67
68
69###################################################
70#
71# Define Configurations
72#
73# The build system infers the build options from the subdirectory name
74# that the simulator is built under.  The subdirectory name must be of
75# the form <CONFIG>[.<OPT>]*, where <CONFIG> is a base configuration
76# (e.g., ALPHA or KERNEL) and OPT is an option (e.g., MYSQL).  The
77# following code defines the standard configurations and options.
78# Additional local configurations and options are read from the file
79# 'local_configs' if it exists.
80#
81# Each base configuration or option is defined in two steps: a
82# function that takes an SCons build environment and modifies it
83# appropriately for that config or option, and an entry in the
84# 'configs_map' or 'options_map' dictionary that maps the directory
85# name string to the function.  (The directory names are all upper
86# case, by convention.)
87#
88###################################################
89
90# Base non-full-system Alpha ISA configuration.
91def AlphaConfig(env):
92    env.Replace(TARGET_ISA = 'alpha')
93    env.Append(CPPDEFINES = 'SS_COMPATIBLE_FP')
94
95# Base full-system configuration.
96def KernelConfig(env):
97    env.Replace(TARGET_ISA = 'alpha')
98    env.Replace(FULL_SYSTEM = True)
99    env.Append(CPPDEFINES = ['FULL_SYSTEM'])
100
101# Base configurations map.
102configs_map = {
103    'ALPHA' : AlphaConfig,
104    'KERNEL' : KernelConfig
105    }
106
107# Enable detailed full-system binning.
108def MeasureOpt(env):
109    env.Replace(USE_MYSQL = True)
110    env.Append(CPPDEFINES = 'FS_MEASURE')
111
112# Enable MySql database output for stats.
113def MySqlOpt(env):
114    env.Replace(USE_MYSQL = True)
115
116# Disable FastAlloc object allocation.
117def NoFastAllocOpt(env):
118    env.Append(CPPDEFINES = 'NO_FAST_ALLOC')
119
120# Configuration options map.
121options_map = {
122    'MEASURE' : MeasureOpt,
123    'MYSQL' : MySqlOpt,
124    'NO_FAST_ALLOC' : NoFastAllocOpt
125    }
126
127# The 'local_configs' file can be used to define additional base
128# configurations and/or options without changing this file.
129if os.path.isfile('local_configs'):
130    SConscript('local_configs', exports = ['configs_map', 'options_map'])
131
132# This function parses a directory name of the form <CONFIG>[.<OPT>]*
133# and sets up the build environment appropriately.  Returns True if
134# successful, False if the base config or any of the options were not
135# defined.
136def set_dir_options(dir, env):
137    parts = dir.split('.')
138    config = parts[0]
139    opts = parts[1:]
140    try:
141        configs_map[config](env)
142        map(lambda opt: options_map[opt](env), opts)
143        return True
144    except KeyError, key:
145        print "Config/option '%s' not found." % key
146        return False
147
148# Set the default configuration and binary.  The default target (if
149# scons is invoked at the top level with no command-line targets) is
150# 'ALPHA/m5.debug'.  If scons is invoked in a subdirectory with no
151# command-line targets, the configuration
152
153###################################################
154#
155# Figure out which configurations to set up.
156#
157#
158# It's prohibitive to do all the combinations of base configurations
159# and options, so we have to infer which ones the user wants.
160#
161# 1. If there are command-line targets, the configuration(s) are inferred
162#    from the directories of those targets.  If scons was invoked from a
163#    subdirectory (using 'scons -u'), those targets have to be
164#    interpreted relative to that subdirectory.
165#
166# 2. If there are no command-line targets, and scons was invoked from a
167#    subdirectory (using 'scons -u'), the configuration is inferred from
168#    the name of the subdirectory.
169#
170# 3. If there are no command-line targets and scons was invoked from
171#    the root build directory, a default configuration is used.  The
172#    built-in default is ALPHA, but this can be overridden by setting the
173#    M5_DEFAULT_CONFIG shell environment veriable.
174#
175# In cases 2 & 3, the specific file target defaults to 'm5.debug', but
176# this can be overridden by setting the M5_DEFAULT_BINARY shell
177# environment veriable.
178#
179###################################################
180
181# Find default configuration & binary.
182default_config = os.environ.get('M5_DEFAULT_CONFIG', 'ALPHA')
183default_binary = os.environ.get('M5_DEFAULT_BINARY', 'm5.debug')
184
185# Ask SCons which directory it was invoked from.  If you invoke SCons
186# from a subdirectory you must use the '-u' flag.
187launch_dir = GetLaunchDir()
188
189# Build a list 'my_targets' of all the targets relative to ROOT.
190if launch_dir == ROOT:
191    # invoked from root build dir
192    if len(COMMAND_LINE_TARGETS) != 0:
193        # easy: use specified targets as is
194        my_targets = COMMAND_LINE_TARGETS
195    else:
196        # default target (ALPHA/m5.debug, unless overridden)
197        target = os.path.join(default_config, default_binary)
198        my_targets = [target]
199        Default(target)
200else:
201    # invoked from subdirectory
202    if not launch_dir.startswith(ROOT):
203        print "Error: launch dir (%s) not a subdirectory of ROOT (%s)!" \
204              (launch_dir, ROOT)
205        sys.exit(1)
206    # make launch_dir relative to ROOT (strip ROOT plus slash off front)
207    launch_dir = launch_dir[len(ROOT)+1:]
208    if len(COMMAND_LINE_TARGETS) != 0:
209        # make specified targets relative to ROOT
210        my_targets = map(lambda x: os.path.join(launch_dir, x),
211                         COMMAND_LINE_TARGETS)
212    else:
213        # build default binary (m5.debug, unless overridden) using the
214        # config inferred by the invocation directory (the first
215        # subdirectory after ROOT)
216        target = os.path.join(launch_dir.split('/')[0], default_binary)
217        my_targets = [target]
218        Default(target)
219
220# Normalize target paths (gets rid of '..' in the middle, etc.)
221my_targets = map(os.path.normpath, my_targets)
222
223# Generate a list of the unique configs that the collected targets reference.
224build_dirs = []
225for t in my_targets:
226    dir = t.split('/')[0]
227    if dir not in build_dirs:
228        build_dirs.append(dir)
229
230###################################################
231#
232# Set up the default build environment.  This environment is copied
233# and modified according to each selected configuration.
234#
235###################################################
236
237default_env = Environment(ENV = os.environ,  # inherit user's enviroment vars
238                          ROOT = ROOT,
239                          SRCDIR = SRCDIR,
240                          EXT_SRCDIR = EXT_SRCDIR,
241                          CPPDEFINES = [],
242                          FULL_SYSTEM = False,
243                          USE_MYSQL = False)
244
245default_env.SConsignFile("sconsign")
246
247# For some reason, the CC and CXX variables don't get passed into the
248# environment correctly.  This is probably some sort of scons bug that
249# will eventually be fixed.
250if os.environ.has_key('CC'):
251    default_env.Replace(CC=os.environ['CC'])
252
253if os.environ.has_key('CXX'):
254    default_env.Replace(CXX=os.environ['CXX'])
255
256# M5_EXT is used by isa_parser.py to find the PLY package.
257default_env.Append(ENV = { 'M5_EXT' : EXT_SRCDIR })
258
259default_env.Append(CCFLAGS='-pipe')
260default_env.Append(CCFLAGS='-fno-strict-aliasing')
261default_env.Append(CCFLAGS=Split('-Wall -Wno-sign-compare -Werror -Wundef'))
262default_env.Append(CPPPATH=[os.path.join(EXT_SRCDIR + '/dnet')])
263
264# libelf build is described in its own SConscript file.  Using a
265# dictionary for exports lets us export "default_env" so the
266# SConscript will see it as "env".  SConscript-global is the build in
267# build/libelf shared among all configs.
268default_env.SConscript('m5/libelf/SConscript-global',
269                       exports={'env' : default_env})
270
271###################################################
272#
273# Define build environments for selected configurations.
274#
275###################################################
276
277for build_dir in build_dirs:
278    # Make a copy of the default environment to use for this config.
279    env = default_env.Copy()
280    # Modify 'env' according to the build directory config.
281    print "Configuring options for directory '%s'." % build_dir
282    if not set_dir_options(build_dir, env):
283        print "Skipping directory '%s'." % build_dir
284        continue
285
286    # The m5/SConscript file sets up the build rules in 'env' according
287    # to the configured options.
288    SConscript('m5/SConscript', build_dir = build_dir, exports = 'env',
289               duplicate=0)
290
291
292###################################################
293#
294# Let SCons do its thing.  At this point SCons will use the defined
295# build environments to build the requested targets.
296#
297###################################################
298
299