SConstruct revision 1852
112396SRiken.Gohil@arm.com# -*- mode:python -*-
212811Sandreas.sandberg@arm.com
312396SRiken.Gohil@arm.com# Copyright (c) 2004-2005 The Regents of The University of Michigan
412396SRiken.Gohil@arm.com# All rights reserved.
512396SRiken.Gohil@arm.com#
612396SRiken.Gohil@arm.com# Redistribution and use in source and binary forms, with or without
712396SRiken.Gohil@arm.com# modification, are permitted provided that the following conditions are
812396SRiken.Gohil@arm.com# met: redistributions of source code must retain the above copyright
912396SRiken.Gohil@arm.com# notice, this list of conditions and the following disclaimer;
1012396SRiken.Gohil@arm.com# redistributions in binary form must reproduce the above copyright
1112396SRiken.Gohil@arm.com# notice, this list of conditions and the following disclaimer in the
1212396SRiken.Gohil@arm.com# documentation and/or other materials provided with the distribution;
1312396SRiken.Gohil@arm.com# neither the name of the copyright holders nor the names of its
1412396SRiken.Gohil@arm.com# contributors may be used to endorse or promote products derived from
1512396SRiken.Gohil@arm.com# this software without specific prior written permission.
1612396SRiken.Gohil@arm.com#
1712396SRiken.Gohil@arm.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1812396SRiken.Gohil@arm.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1912396SRiken.Gohil@arm.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2012396SRiken.Gohil@arm.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2112396SRiken.Gohil@arm.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2212396SRiken.Gohil@arm.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2312396SRiken.Gohil@arm.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2412396SRiken.Gohil@arm.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2512396SRiken.Gohil@arm.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2612396SRiken.Gohil@arm.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2712396SRiken.Gohil@arm.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2812396SRiken.Gohil@arm.com
2912396SRiken.Gohil@arm.com###################################################
3012396SRiken.Gohil@arm.com#
3112396SRiken.Gohil@arm.com# SCons top-level build description (SConstruct) file.
3212396SRiken.Gohil@arm.com#
3312396SRiken.Gohil@arm.com# To build M5, you need a directory with three things:
3412396SRiken.Gohil@arm.com# 1. A copy of this file (named SConstruct).
3512396SRiken.Gohil@arm.com# 2. A link named 'm5' to the top of the M5 simulator source tree.
3612396SRiken.Gohil@arm.com# 3. A link named 'ext' to the top of the M5 external source tree.
3712396SRiken.Gohil@arm.com#
3812396SRiken.Gohil@arm.com# Then type 'scons' to build the default configuration (see below), or
3912396SRiken.Gohil@arm.com# 'scons <CONFIG>/<binary>' to build some other configuration (e.g.,
4012396SRiken.Gohil@arm.com# 'ALPHA_FS/m5.opt' for the optimized full-system version).
4112396SRiken.Gohil@arm.com#
4212396SRiken.Gohil@arm.com###################################################
4312396SRiken.Gohil@arm.com
4412396SRiken.Gohil@arm.com# Python library imports
4512396SRiken.Gohil@arm.comimport sys
4612396SRiken.Gohil@arm.comimport os
4712396SRiken.Gohil@arm.com
4812396SRiken.Gohil@arm.com# Check for recent-enough Python version
4912396SRiken.Gohil@arm.com(major, minor) = sys.version_info[:2]
5012396SRiken.Gohil@arm.comif major < 2 or (major == 2 and minor < 3):
5112396SRiken.Gohil@arm.com    print "Error: M5 requires Python 2.3 or later."
5212396SRiken.Gohil@arm.com    sys.exit(1)
5312396SRiken.Gohil@arm.com
5412396SRiken.Gohil@arm.com# The absolute path to the current directory (where this file lives).
5512811Sandreas.sandberg@arm.comROOT = Dir('.').abspath
5612811Sandreas.sandberg@arm.com
5712396SRiken.Gohil@arm.com# Paths to the M5 and external source trees (local symlinks).
5812396SRiken.Gohil@arm.comSRCDIR = os.path.join(ROOT, 'm5')
5912396SRiken.Gohil@arm.comEXT_SRCDIR = os.path.join(ROOT, 'ext')
6012396SRiken.Gohil@arm.com
6112396SRiken.Gohil@arm.com# Check for 'm5' and 'ext' links, die if they don't exist.
6212396SRiken.Gohil@arm.comif not os.path.isdir(SRCDIR):
6312396SRiken.Gohil@arm.com    print "Error: '%s' must be a link to the M5 source tree." % SRCDIR
6412396SRiken.Gohil@arm.com    sys.exit(1)
6512396SRiken.Gohil@arm.com
6612396SRiken.Gohil@arm.comif not os.path.isdir('ext'):
6712396SRiken.Gohil@arm.com    print "Error: '%s' must be a link to the M5 external source tree." \
6812396SRiken.Gohil@arm.com          % EXT_SRCDIR
6912396SRiken.Gohil@arm.com    sys.exit(1)
7012396SRiken.Gohil@arm.com
7112396SRiken.Gohil@arm.com# tell python where to find m5 python code
7212396SRiken.Gohil@arm.comsys.path.append(os.path.join(SRCDIR, 'python'))
7312396SRiken.Gohil@arm.com
7412396SRiken.Gohil@arm.com
7512396SRiken.Gohil@arm.com###################################################
7612396SRiken.Gohil@arm.com#
7712396SRiken.Gohil@arm.com# Define Configurations
7812396SRiken.Gohil@arm.com#
7912396SRiken.Gohil@arm.com# The build system infers the build options from the subdirectory name
8012396SRiken.Gohil@arm.com# that the simulator is built under.  The subdirectory name must be of
8112396SRiken.Gohil@arm.com# the form <CONFIG>[.<OPT>]*, where <CONFIG> is a base configuration
8212396SRiken.Gohil@arm.com# (e.g., ALPHA_SE or ALPHA_FS) and OPT is an option (e.g., MYSQL).  The
8312396SRiken.Gohil@arm.com# following code defines the standard configurations and options.
8412396SRiken.Gohil@arm.com# Additional local configurations and options are read from the file
8512396SRiken.Gohil@arm.com# 'local_configs' if it exists.
8612396SRiken.Gohil@arm.com#
8712396SRiken.Gohil@arm.com# Each base configuration or option is defined in two steps: a
8812396SRiken.Gohil@arm.com# function that takes an SCons build environment and modifies it
8912396SRiken.Gohil@arm.com# appropriately for that config or option, and an entry in the
9012396SRiken.Gohil@arm.com# 'configs_map' or 'options_map' dictionary that maps the directory
9112396SRiken.Gohil@arm.com# name string to the function.  (The directory names are all upper
9212844Sgiacomo.travaglini@arm.com# case, by convention.)
9312396SRiken.Gohil@arm.com#
9412396SRiken.Gohil@arm.com###################################################
9512396SRiken.Gohil@arm.com
9612844Sgiacomo.travaglini@arm.com# Base non-full-system Alpha ISA configuration.
9712396SRiken.Gohil@arm.comdef AlphaSyscallEmulConfig(env):
9812396SRiken.Gohil@arm.com    env.Replace(TARGET_ISA = 'alpha')
9912396SRiken.Gohil@arm.com    env.Append(CPPDEFINES = 'SS_COMPATIBLE_FP')
10012396SRiken.Gohil@arm.com
10112396SRiken.Gohil@arm.com# Base full-system configuration.
10212396SRiken.Gohil@arm.comdef AlphaFullSysConfig(env):
10312396SRiken.Gohil@arm.com    env.Replace(TARGET_ISA = 'alpha')
10412396SRiken.Gohil@arm.com    env.Replace(FULL_SYSTEM = True)
10512396SRiken.Gohil@arm.com    env.Append(CPPDEFINES = ['FULL_SYSTEM'])
10612396SRiken.Gohil@arm.com
10712396SRiken.Gohil@arm.com# Base configurations map.
10812396SRiken.Gohil@arm.comconfigs_map = {
10912396SRiken.Gohil@arm.com    'ALPHA_SE' : AlphaSyscallEmulConfig,
11012396SRiken.Gohil@arm.com    'ALPHA_FS' : AlphaFullSysConfig
11112396SRiken.Gohil@arm.com    }
11212396SRiken.Gohil@arm.com
11312396SRiken.Gohil@arm.com# Disable FastAlloc object allocation.
11412396SRiken.Gohil@arm.comdef NoFastAllocOpt(env):
11512396SRiken.Gohil@arm.com    env.Append(CPPDEFINES = 'NO_FAST_ALLOC')
11612396SRiken.Gohil@arm.com
11712396SRiken.Gohil@arm.com# Enable efence
11812396SRiken.Gohil@arm.comdef EfenceOpt(env):
11912396SRiken.Gohil@arm.com    env.Append(LIBS=['efence'])
12012396SRiken.Gohil@arm.com
12112396SRiken.Gohil@arm.com# Configuration options map.
12212396SRiken.Gohil@arm.comoptions_map = {
12312396SRiken.Gohil@arm.com    'NO_FAST_ALLOC' : NoFastAllocOpt,
12412396SRiken.Gohil@arm.com    'EFENCE' : EfenceOpt
12512396SRiken.Gohil@arm.com    }
12612396SRiken.Gohil@arm.com
12712396SRiken.Gohil@arm.com# The 'local_configs' file can be used to define additional base
12812396SRiken.Gohil@arm.com# configurations and/or options without changing this file.
12912396SRiken.Gohil@arm.comif os.path.isfile('local_configs'):
13012396SRiken.Gohil@arm.com    SConscript('local_configs', exports = ['configs_map', 'options_map'])
13112396SRiken.Gohil@arm.com
13212396SRiken.Gohil@arm.com# This function parses a directory name of the form <CONFIG>[.<OPT>]*
13312396SRiken.Gohil@arm.com# and sets up the build environment appropriately.  Returns True if
13412396SRiken.Gohil@arm.com# successful, False if the base config or any of the options were not
13512396SRiken.Gohil@arm.com# defined.
13612396SRiken.Gohil@arm.comdef set_dir_options(dir, env):
13712811Sandreas.sandberg@arm.com    parts = dir.split('.')
13812811Sandreas.sandberg@arm.com    config = parts[0]
13912811Sandreas.sandberg@arm.com    opts = parts[1:]
14012844Sgiacomo.travaglini@arm.com    try:
14112844Sgiacomo.travaglini@arm.com        configs_map[config](env)
14212844Sgiacomo.travaglini@arm.com        map(lambda opt: options_map[opt](env), opts)
14312844Sgiacomo.travaglini@arm.com        return True
14412811Sandreas.sandberg@arm.com    except KeyError, key:
14512811Sandreas.sandberg@arm.com        print "Config/option '%s' not found." % key
14612811Sandreas.sandberg@arm.com        return False
14712811Sandreas.sandberg@arm.com
14812811Sandreas.sandberg@arm.com# Set the default configuration and binary.  The default target (if
14912811Sandreas.sandberg@arm.com# scons is invoked at the top level with no command-line targets) is
15012811Sandreas.sandberg@arm.com# 'ALPHA_SE/m5.debug'.  If scons is invoked in a subdirectory with no
15112811Sandreas.sandberg@arm.com# command-line targets, the configuration
15212811Sandreas.sandberg@arm.com
15312811Sandreas.sandberg@arm.com###################################################
15412811Sandreas.sandberg@arm.com#
15512811Sandreas.sandberg@arm.com# Figure out which configurations to set up.
15612811Sandreas.sandberg@arm.com#
15712844Sgiacomo.travaglini@arm.com#
15812844Sgiacomo.travaglini@arm.com# It's prohibitive to do all the combinations of base configurations
15912844Sgiacomo.travaglini@arm.com# and options, so we have to infer which ones the user wants.
16012811Sandreas.sandberg@arm.com#
16112811Sandreas.sandberg@arm.com# 1. If there are command-line targets, the configuration(s) are inferred
16212811Sandreas.sandberg@arm.com#    from the directories of those targets.  If scons was invoked from a
16312811Sandreas.sandberg@arm.com#    subdirectory (using 'scons -u'), those targets have to be
16412811Sandreas.sandberg@arm.com#    interpreted relative to that subdirectory.
16512811Sandreas.sandberg@arm.com#
16612811Sandreas.sandberg@arm.com# 2. If there are no command-line targets, and scons was invoked from a
16712811Sandreas.sandberg@arm.com#    subdirectory (using 'scons -u'), the configuration is inferred from
16812811Sandreas.sandberg@arm.com#    the name of the subdirectory.
16912811Sandreas.sandberg@arm.com#
17012811Sandreas.sandberg@arm.com# 3. If there are no command-line targets and scons was invoked from
17112811Sandreas.sandberg@arm.com#    the root build directory, a default configuration is used.  The
17212811Sandreas.sandberg@arm.com#    built-in default is ALPHA_SE, but this can be overridden by setting the
17312396SRiken.Gohil@arm.com#    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_SE')
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_SE/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                          ALPHA_TLASER = False,
244                          USE_MYSQL = False)
245
246default_env.SConsignFile("sconsign")
247
248# For some reason, the CC and CXX variables don't get passed into the
249# environment correctly.  This is probably some sort of scons bug that
250# will eventually be fixed.
251if os.environ.has_key('CC'):
252    default_env.Replace(CC=os.environ['CC'])
253
254if os.environ.has_key('CXX'):
255    default_env.Replace(CXX=os.environ['CXX'])
256
257# M5_EXT is used by isa_parser.py to find the PLY package.
258default_env.Append(ENV = { 'M5_EXT' : EXT_SRCDIR })
259
260default_env.Append(CCFLAGS='-pipe')
261default_env.Append(CCFLAGS='-fno-strict-aliasing')
262default_env.Append(CCFLAGS=Split('-Wall -Wno-sign-compare -Werror -Wundef'))
263if sys.platform == 'cygwin':
264    # cygwin has some header file issues...
265    default_env.Append(CCFLAGS=Split("-Wno-uninitialized"))
266default_env.Append(CPPPATH=[os.path.join(EXT_SRCDIR + '/dnet')])
267
268# libelf build is described in its own SConscript file.  Using a
269# dictionary for exports lets us export "default_env" so the
270# SConscript will see it as "env".  SConscript-global is the build in
271# build/libelf shared among all configs.
272default_env.SConscript('m5/libelf/SConscript-global',
273                       exports={'env' : default_env})
274
275###################################################
276#
277# Define build environments for selected configurations.
278#
279###################################################
280
281for build_dir in build_dirs:
282    # Make a copy of the default environment to use for this config.
283    env = default_env.Copy()
284    # Modify 'env' according to the build directory config.
285    print "Configuring options for directory '%s'." % build_dir
286    if not set_dir_options(build_dir, env):
287        print "Skipping directory '%s'." % build_dir
288        continue
289
290    # The m5/SConscript file sets up the build rules in 'env' according
291    # to the configured options.
292    SConscript('m5/SConscript', build_dir = build_dir, exports = 'env',
293               duplicate=0)
294
295
296###################################################
297#
298# Let SCons do its thing.  At this point SCons will use the defined
299# build environments to build the requested targets.
300#
301###################################################
302
303