SConstruct (6157:eaf2fd8f54c0) SConstruct (6168:ba6fe02228db)
1# -*- mode:python -*-
2
3# Copyright (c) 2009 The Hewlett-Packard Development Company
4# Copyright (c) 2004-2005 The Regents of The University of Michigan
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions are
9# met: redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer;
11# redistributions in binary form must reproduce the above copyright
12# notice, this list of conditions and the following disclaimer in the
13# documentation and/or other materials provided with the distribution;
14# neither the name of the copyright holders nor the names of its
15# contributors may be used to endorse or promote products derived from
16# this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29#
30# Authors: Steve Reinhardt
31# Nathan Binkert
32
33###################################################
34#
35# SCons top-level build description (SConstruct) file.
36#
37# While in this directory ('m5'), just type 'scons' to build the default
38# configuration (see below), or type 'scons build/<CONFIG>/<binary>'
39# to build some other configuration (e.g., 'build/ALPHA_FS/m5.opt' for
40# the optimized full-system version).
41#
42# You can build M5 in a different directory as long as there is a
43# 'build/<CONFIG>' somewhere along the target path. The build system
44# expects that all configs under the same build directory are being
45# built for the same host system.
46#
47# Examples:
48#
49# The following two commands are equivalent. The '-u' option tells
50# scons to search up the directory tree for this SConstruct file.
51# % cd <path-to-src>/m5 ; scons build/ALPHA_FS/m5.debug
52# % cd <path-to-src>/m5/build/ALPHA_FS; scons -u m5.debug
53#
54# The following two commands are equivalent and demonstrate building
55# in a directory outside of the source tree. The '-C' option tells
56# scons to chdir to the specified directory to find this SConstruct
57# file.
58# % cd <path-to-src>/m5 ; scons /local/foo/build/ALPHA_FS/m5.debug
59# % cd /local/foo/build/ALPHA_FS; scons -C <path-to-src>/m5 m5.debug
60#
61# You can use 'scons -H' to print scons options. If you're in this
62# 'm5' directory (or use -u or -C to tell scons where to find this
63# file), you can use 'scons -h' to print all the M5-specific build
64# options as well.
65#
66###################################################
67
68# Check for recent-enough Python and SCons versions.
69try:
70 # Really old versions of scons only take two options for the
71 # function, so check once without the revision and once with the
72 # revision, the first instance will fail for stuff other than
73 # 0.98, and the second will fail for 0.98.0
74 EnsureSConsVersion(0, 98)
75 EnsureSConsVersion(0, 98, 1)
76except SystemExit, e:
77 print """
78For more details, see:
79 http://m5sim.org/wiki/index.php/Compiling_M5
80"""
81 raise
82
83# We ensure the python version early because we have stuff that
84# requires python 2.4
85try:
86 EnsurePythonVersion(2, 4)
87except SystemExit, e:
88 print """
89You can use a non-default installation of the Python interpreter by
90either (1) rearranging your PATH so that scons finds the non-default
91'python' first or (2) explicitly invoking an alternative interpreter
92on the scons script.
93
94For more details, see:
95 http://m5sim.org/wiki/index.php/Using_a_non-default_Python_installation
96"""
97 raise
98
99import os
100import re
101import subprocess
102import sys
103
104from os import mkdir, environ
105from os.path import abspath, basename, dirname, expanduser, normpath
106from os.path import exists, isdir, isfile
107from os.path import join as joinpath, split as splitpath
108
109import SCons
110import SCons.Node
111
112def read_command(cmd, **kwargs):
113 """run the command cmd, read the results and return them
114 this is sorta like `cmd` in shell"""
115 from subprocess import Popen, PIPE, STDOUT
116
117 if isinstance(cmd, str):
118 cmd = cmd.split()
119
120 no_exception = 'exception' in kwargs
121 exception = kwargs.pop('exception', None)
122
123 kwargs.setdefault('shell', False)
124 kwargs.setdefault('stdout', PIPE)
125 kwargs.setdefault('stderr', STDOUT)
126 kwargs.setdefault('close_fds', True)
127 try:
128 subp = Popen(cmd, **kwargs)
129 except Exception, e:
130 if no_exception:
131 return exception
132 raise
133
134 return subp.communicate()[0]
135
136# helper function: compare arrays or strings of version numbers.
137# E.g., compare_version((1,3,25), (1,4,1)')
138# returns -1, 0, 1 if v1 is <, ==, > v2
139def compare_versions(v1, v2):
140 def make_version_list(v):
141 if isinstance(v, (list,tuple)):
142 return v
143 elif isinstance(v, str):
144 return map(lambda x: int(re.match('\d+', x).group()), v.split('.'))
145 else:
146 raise TypeError
147
148 v1 = make_version_list(v1)
149 v2 = make_version_list(v2)
150 # Compare corresponding elements of lists
151 for n1,n2 in zip(v1, v2):
152 if n1 < n2: return -1
153 if n1 > n2: return 1
154 # all corresponding values are equal... see if one has extra values
155 if len(v1) < len(v2): return -1
156 if len(v1) > len(v2): return 1
157 return 0
158
159########################################################################
160#
161# Set up the main build environment.
162#
163########################################################################
164use_vars = set([ 'AS', 'AR', 'CC', 'CXX', 'HOME', 'LD_LIBRARY_PATH', 'PATH',
165 'RANLIB' ])
166
167use_env = {}
168for key,val in os.environ.iteritems():
169 if key in use_vars or key.startswith("M5"):
170 use_env[key] = val
171
172main = Environment(ENV=use_env)
173main.root = Dir(".") # The current directory (where this file lives).
174main.srcdir = Dir("src") # The source directory
175
176########################################################################
177#
178# Mercurial Stuff.
179#
180# If the M5 directory is a mercurial repository, we should do some
181# extra things.
182#
183########################################################################
184
185hgdir = main.root.Dir(".hg")
186
187mercurial_style_message = """
188You're missing the M5 style hook.
189Please install the hook so we can ensure that all code fits a common style.
190
191All you'd need to do is add the following lines to your repository .hg/hgrc
192or your personal .hgrc
193----------------
194
195[extensions]
196style = %s/util/style.py
197
198[hooks]
199pretxncommit.style = python:style.check_whitespace
200""" % (main.root)
201
202mercurial_bin_not_found = """
203Mercurial binary cannot be found, unfortunately this means that we
204cannot easily determine the version of M5 that you are running and
205this makes error messages more difficult to collect. Please consider
206installing mercurial if you choose to post an error message
207"""
208
209mercurial_lib_not_found = """
210Mercurial libraries cannot be found, ignoring style hook
211If you are actually a M5 developer, please fix this and
212run the style hook. It is important.
213"""
214
215hg_info = "Unknown"
216if hgdir.exists():
217 # 1) Grab repository revision if we know it.
218 cmd = "hg id -n -i -t -b"
219 try:
220 hg_info = read_command(cmd, cwd=main.root.abspath).strip()
221 except OSError:
222 print mercurial_bin_not_found
223
224 # 2) Ensure that the style hook is in place.
225 try:
226 ui = None
227 if ARGUMENTS.get('IGNORE_STYLE') != 'True':
228 from mercurial import ui
229 ui = ui.ui()
230 except ImportError:
231 print mercurial_lib_not_found
232
233 if ui is not None:
234 ui.readconfig(hgdir.File('hgrc').abspath)
235 style_hook = ui.config('hooks', 'pretxncommit.style', None)
236
237 if not style_hook:
238 print mercurial_style_message
239 sys.exit(1)
240else:
241 print ".hg directory not found"
242
243main['HG_INFO'] = hg_info
244
245###################################################
246#
247# Figure out which configurations to set up based on the path(s) of
248# the target(s).
249#
250###################################################
251
252# Find default configuration & binary.
253Default(environ.get('M5_DEFAULT_BINARY', 'build/ALPHA_SE/m5.debug'))
254
255# helper function: find last occurrence of element in list
256def rfind(l, elt, offs = -1):
257 for i in range(len(l)+offs, 0, -1):
258 if l[i] == elt:
259 return i
260 raise ValueError, "element not found"
261
262# Each target must have 'build' in the interior of the path; the
263# directory below this will determine the build parameters. For
264# example, for target 'foo/bar/build/ALPHA_SE/arch/alpha/blah.do' we
265# recognize that ALPHA_SE specifies the configuration because it
266# follow 'build' in the bulid path.
267
268# Generate absolute paths to targets so we can see where the build dir is
269if COMMAND_LINE_TARGETS:
270 # Ask SCons which directory it was invoked from
271 launch_dir = GetLaunchDir()
272 # Make targets relative to invocation directory
273 abs_targets = [ normpath(joinpath(launch_dir, str(x))) for x in \
274 COMMAND_LINE_TARGETS]
275else:
276 # Default targets are relative to root of tree
277 abs_targets = [ normpath(joinpath(main.root.abspath, str(x))) for x in \
278 DEFAULT_TARGETS]
279
280
281# Generate a list of the unique build roots and configs that the
282# collected targets reference.
283variant_paths = []
284build_root = None
285for t in abs_targets:
286 path_dirs = t.split('/')
287 try:
288 build_top = rfind(path_dirs, 'build', -2)
289 except:
290 print "Error: no non-leaf 'build' dir found on target path", t
291 Exit(1)
292 this_build_root = joinpath('/',*path_dirs[:build_top+1])
293 if not build_root:
294 build_root = this_build_root
295 else:
296 if this_build_root != build_root:
297 print "Error: build targets not under same build root\n"\
298 " %s\n %s" % (build_root, this_build_root)
299 Exit(1)
300 variant_path = joinpath('/',*path_dirs[:build_top+2])
301 if variant_path not in variant_paths:
302 variant_paths.append(variant_path)
303
304# Make sure build_root exists (might not if this is the first build there)
305if not isdir(build_root):
306 mkdir(build_root)
307
308Export('main')
309
310main.SConsignFile(joinpath(build_root, "sconsign"))
311
312# Default duplicate option is to use hard links, but this messes up
313# when you use emacs to edit a file in the target dir, as emacs moves
314# file to file~ then copies to file, breaking the link. Symbolic
315# (soft) links work better.
316main.SetOption('duplicate', 'soft-copy')
317
318#
319# Set up global sticky variables... these are common to an entire build
320# tree (not specific to a particular build like ALPHA_SE)
321#
322
323# Variable validators & converters for global sticky variables
324def PathListMakeAbsolute(val):
325 if not val:
326 return val
327 f = lambda p: abspath(expanduser(p))
328 return ':'.join(map(f, val.split(':')))
329
330def PathListAllExist(key, val, env):
331 if not val:
332 return
333 paths = val.split(':')
334 for path in paths:
335 if not isdir(path):
336 raise SCons.Errors.UserError("Path does not exist: '%s'" % path)
337
338global_sticky_vars_file = joinpath(build_root, 'variables.global')
339
340global_sticky_vars = Variables(global_sticky_vars_file, args=ARGUMENTS)
341
342global_sticky_vars.AddVariables(
343 ('CC', 'C compiler', environ.get('CC', main['CC'])),
344 ('CXX', 'C++ compiler', environ.get('CXX', main['CXX'])),
345 ('BATCH', 'Use batch pool for build and tests', False),
346 ('BATCH_CMD', 'Batch pool submission command name', 'qdo'),
347 ('EXTRAS', 'Add Extra directories to the compilation', '',
1# -*- mode:python -*-
2
3# Copyright (c) 2009 The Hewlett-Packard Development Company
4# Copyright (c) 2004-2005 The Regents of The University of Michigan
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions are
9# met: redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer;
11# redistributions in binary form must reproduce the above copyright
12# notice, this list of conditions and the following disclaimer in the
13# documentation and/or other materials provided with the distribution;
14# neither the name of the copyright holders nor the names of its
15# contributors may be used to endorse or promote products derived from
16# this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29#
30# Authors: Steve Reinhardt
31# Nathan Binkert
32
33###################################################
34#
35# SCons top-level build description (SConstruct) file.
36#
37# While in this directory ('m5'), just type 'scons' to build the default
38# configuration (see below), or type 'scons build/<CONFIG>/<binary>'
39# to build some other configuration (e.g., 'build/ALPHA_FS/m5.opt' for
40# the optimized full-system version).
41#
42# You can build M5 in a different directory as long as there is a
43# 'build/<CONFIG>' somewhere along the target path. The build system
44# expects that all configs under the same build directory are being
45# built for the same host system.
46#
47# Examples:
48#
49# The following two commands are equivalent. The '-u' option tells
50# scons to search up the directory tree for this SConstruct file.
51# % cd <path-to-src>/m5 ; scons build/ALPHA_FS/m5.debug
52# % cd <path-to-src>/m5/build/ALPHA_FS; scons -u m5.debug
53#
54# The following two commands are equivalent and demonstrate building
55# in a directory outside of the source tree. The '-C' option tells
56# scons to chdir to the specified directory to find this SConstruct
57# file.
58# % cd <path-to-src>/m5 ; scons /local/foo/build/ALPHA_FS/m5.debug
59# % cd /local/foo/build/ALPHA_FS; scons -C <path-to-src>/m5 m5.debug
60#
61# You can use 'scons -H' to print scons options. If you're in this
62# 'm5' directory (or use -u or -C to tell scons where to find this
63# file), you can use 'scons -h' to print all the M5-specific build
64# options as well.
65#
66###################################################
67
68# Check for recent-enough Python and SCons versions.
69try:
70 # Really old versions of scons only take two options for the
71 # function, so check once without the revision and once with the
72 # revision, the first instance will fail for stuff other than
73 # 0.98, and the second will fail for 0.98.0
74 EnsureSConsVersion(0, 98)
75 EnsureSConsVersion(0, 98, 1)
76except SystemExit, e:
77 print """
78For more details, see:
79 http://m5sim.org/wiki/index.php/Compiling_M5
80"""
81 raise
82
83# We ensure the python version early because we have stuff that
84# requires python 2.4
85try:
86 EnsurePythonVersion(2, 4)
87except SystemExit, e:
88 print """
89You can use a non-default installation of the Python interpreter by
90either (1) rearranging your PATH so that scons finds the non-default
91'python' first or (2) explicitly invoking an alternative interpreter
92on the scons script.
93
94For more details, see:
95 http://m5sim.org/wiki/index.php/Using_a_non-default_Python_installation
96"""
97 raise
98
99import os
100import re
101import subprocess
102import sys
103
104from os import mkdir, environ
105from os.path import abspath, basename, dirname, expanduser, normpath
106from os.path import exists, isdir, isfile
107from os.path import join as joinpath, split as splitpath
108
109import SCons
110import SCons.Node
111
112def read_command(cmd, **kwargs):
113 """run the command cmd, read the results and return them
114 this is sorta like `cmd` in shell"""
115 from subprocess import Popen, PIPE, STDOUT
116
117 if isinstance(cmd, str):
118 cmd = cmd.split()
119
120 no_exception = 'exception' in kwargs
121 exception = kwargs.pop('exception', None)
122
123 kwargs.setdefault('shell', False)
124 kwargs.setdefault('stdout', PIPE)
125 kwargs.setdefault('stderr', STDOUT)
126 kwargs.setdefault('close_fds', True)
127 try:
128 subp = Popen(cmd, **kwargs)
129 except Exception, e:
130 if no_exception:
131 return exception
132 raise
133
134 return subp.communicate()[0]
135
136# helper function: compare arrays or strings of version numbers.
137# E.g., compare_version((1,3,25), (1,4,1)')
138# returns -1, 0, 1 if v1 is <, ==, > v2
139def compare_versions(v1, v2):
140 def make_version_list(v):
141 if isinstance(v, (list,tuple)):
142 return v
143 elif isinstance(v, str):
144 return map(lambda x: int(re.match('\d+', x).group()), v.split('.'))
145 else:
146 raise TypeError
147
148 v1 = make_version_list(v1)
149 v2 = make_version_list(v2)
150 # Compare corresponding elements of lists
151 for n1,n2 in zip(v1, v2):
152 if n1 < n2: return -1
153 if n1 > n2: return 1
154 # all corresponding values are equal... see if one has extra values
155 if len(v1) < len(v2): return -1
156 if len(v1) > len(v2): return 1
157 return 0
158
159########################################################################
160#
161# Set up the main build environment.
162#
163########################################################################
164use_vars = set([ 'AS', 'AR', 'CC', 'CXX', 'HOME', 'LD_LIBRARY_PATH', 'PATH',
165 'RANLIB' ])
166
167use_env = {}
168for key,val in os.environ.iteritems():
169 if key in use_vars or key.startswith("M5"):
170 use_env[key] = val
171
172main = Environment(ENV=use_env)
173main.root = Dir(".") # The current directory (where this file lives).
174main.srcdir = Dir("src") # The source directory
175
176########################################################################
177#
178# Mercurial Stuff.
179#
180# If the M5 directory is a mercurial repository, we should do some
181# extra things.
182#
183########################################################################
184
185hgdir = main.root.Dir(".hg")
186
187mercurial_style_message = """
188You're missing the M5 style hook.
189Please install the hook so we can ensure that all code fits a common style.
190
191All you'd need to do is add the following lines to your repository .hg/hgrc
192or your personal .hgrc
193----------------
194
195[extensions]
196style = %s/util/style.py
197
198[hooks]
199pretxncommit.style = python:style.check_whitespace
200""" % (main.root)
201
202mercurial_bin_not_found = """
203Mercurial binary cannot be found, unfortunately this means that we
204cannot easily determine the version of M5 that you are running and
205this makes error messages more difficult to collect. Please consider
206installing mercurial if you choose to post an error message
207"""
208
209mercurial_lib_not_found = """
210Mercurial libraries cannot be found, ignoring style hook
211If you are actually a M5 developer, please fix this and
212run the style hook. It is important.
213"""
214
215hg_info = "Unknown"
216if hgdir.exists():
217 # 1) Grab repository revision if we know it.
218 cmd = "hg id -n -i -t -b"
219 try:
220 hg_info = read_command(cmd, cwd=main.root.abspath).strip()
221 except OSError:
222 print mercurial_bin_not_found
223
224 # 2) Ensure that the style hook is in place.
225 try:
226 ui = None
227 if ARGUMENTS.get('IGNORE_STYLE') != 'True':
228 from mercurial import ui
229 ui = ui.ui()
230 except ImportError:
231 print mercurial_lib_not_found
232
233 if ui is not None:
234 ui.readconfig(hgdir.File('hgrc').abspath)
235 style_hook = ui.config('hooks', 'pretxncommit.style', None)
236
237 if not style_hook:
238 print mercurial_style_message
239 sys.exit(1)
240else:
241 print ".hg directory not found"
242
243main['HG_INFO'] = hg_info
244
245###################################################
246#
247# Figure out which configurations to set up based on the path(s) of
248# the target(s).
249#
250###################################################
251
252# Find default configuration & binary.
253Default(environ.get('M5_DEFAULT_BINARY', 'build/ALPHA_SE/m5.debug'))
254
255# helper function: find last occurrence of element in list
256def rfind(l, elt, offs = -1):
257 for i in range(len(l)+offs, 0, -1):
258 if l[i] == elt:
259 return i
260 raise ValueError, "element not found"
261
262# Each target must have 'build' in the interior of the path; the
263# directory below this will determine the build parameters. For
264# example, for target 'foo/bar/build/ALPHA_SE/arch/alpha/blah.do' we
265# recognize that ALPHA_SE specifies the configuration because it
266# follow 'build' in the bulid path.
267
268# Generate absolute paths to targets so we can see where the build dir is
269if COMMAND_LINE_TARGETS:
270 # Ask SCons which directory it was invoked from
271 launch_dir = GetLaunchDir()
272 # Make targets relative to invocation directory
273 abs_targets = [ normpath(joinpath(launch_dir, str(x))) for x in \
274 COMMAND_LINE_TARGETS]
275else:
276 # Default targets are relative to root of tree
277 abs_targets = [ normpath(joinpath(main.root.abspath, str(x))) for x in \
278 DEFAULT_TARGETS]
279
280
281# Generate a list of the unique build roots and configs that the
282# collected targets reference.
283variant_paths = []
284build_root = None
285for t in abs_targets:
286 path_dirs = t.split('/')
287 try:
288 build_top = rfind(path_dirs, 'build', -2)
289 except:
290 print "Error: no non-leaf 'build' dir found on target path", t
291 Exit(1)
292 this_build_root = joinpath('/',*path_dirs[:build_top+1])
293 if not build_root:
294 build_root = this_build_root
295 else:
296 if this_build_root != build_root:
297 print "Error: build targets not under same build root\n"\
298 " %s\n %s" % (build_root, this_build_root)
299 Exit(1)
300 variant_path = joinpath('/',*path_dirs[:build_top+2])
301 if variant_path not in variant_paths:
302 variant_paths.append(variant_path)
303
304# Make sure build_root exists (might not if this is the first build there)
305if not isdir(build_root):
306 mkdir(build_root)
307
308Export('main')
309
310main.SConsignFile(joinpath(build_root, "sconsign"))
311
312# Default duplicate option is to use hard links, but this messes up
313# when you use emacs to edit a file in the target dir, as emacs moves
314# file to file~ then copies to file, breaking the link. Symbolic
315# (soft) links work better.
316main.SetOption('duplicate', 'soft-copy')
317
318#
319# Set up global sticky variables... these are common to an entire build
320# tree (not specific to a particular build like ALPHA_SE)
321#
322
323# Variable validators & converters for global sticky variables
324def PathListMakeAbsolute(val):
325 if not val:
326 return val
327 f = lambda p: abspath(expanduser(p))
328 return ':'.join(map(f, val.split(':')))
329
330def PathListAllExist(key, val, env):
331 if not val:
332 return
333 paths = val.split(':')
334 for path in paths:
335 if not isdir(path):
336 raise SCons.Errors.UserError("Path does not exist: '%s'" % path)
337
338global_sticky_vars_file = joinpath(build_root, 'variables.global')
339
340global_sticky_vars = Variables(global_sticky_vars_file, args=ARGUMENTS)
341
342global_sticky_vars.AddVariables(
343 ('CC', 'C compiler', environ.get('CC', main['CC'])),
344 ('CXX', 'C++ compiler', environ.get('CXX', main['CXX'])),
345 ('BATCH', 'Use batch pool for build and tests', False),
346 ('BATCH_CMD', 'Batch pool submission command name', 'qdo'),
347 ('EXTRAS', 'Add Extra directories to the compilation', '',
348 PathListAllExist, PathListMakeAbsolute)
349 )
348 PathListAllExist, PathListMakeAbsolute),
349 BoolVariable('RUBY', 'Build with Ruby', False),
350 )
350
351# base help text
352help_text = '''
353Usage: scons [scons options] [build options] [target(s)]
354
355Global sticky options:
356'''
357
358help_text += global_sticky_vars.GenerateHelpText(main)
359
360# Update main environment with values from ARGUMENTS & global_sticky_vars_file
361global_sticky_vars.Update(main)
362
363# Save sticky variable settings back to current variables file
364global_sticky_vars.Save(global_sticky_vars_file, main)
365
366# Parse EXTRAS variable to build list of all directories where we're
367# look for sources etc. This list is exported as base_dir_list.
368base_dir = main.srcdir.abspath
369if main['EXTRAS']:
370 extras_dir_list = main['EXTRAS'].split(':')
371else:
372 extras_dir_list = []
373
374Export('base_dir')
375Export('extras_dir_list')
376
377# the ext directory should be on the #includes path
378main.Append(CPPPATH=[Dir('ext')])
379
380# M5_PLY is used by isa_parser.py to find the PLY package.
381main.Append(ENV = { 'M5_PLY' : Dir('ext/ply').abspath })
382
383CXX_version = read_command([main['CXX'],'--version'], exception=False)
384CXX_V = read_command([main['CXX'],'-V'], exception=False)
385
386main['GCC'] = CXX_version and CXX_version.find('g++') >= 0
387main['SUNCC'] = CXX_V and CXX_V.find('Sun C++') >= 0
388main['ICC'] = CXX_V and CXX_V.find('Intel') >= 0
389if main['GCC'] + main['SUNCC'] + main['ICC'] > 1:
390 print 'Error: How can we have two at the same time?'
391 Exit(1)
392
393# Set up default C++ compiler flags
394if main['GCC']:
395 main.Append(CCFLAGS='-pipe')
396 main.Append(CCFLAGS='-fno-strict-aliasing')
397 main.Append(CCFLAGS=['-Wall', '-Wno-sign-compare', '-Wundef'])
398 main.Append(CXXFLAGS='-Wno-deprecated')
399elif main['ICC']:
400 pass #Fix me... add warning flags once we clean up icc warnings
401elif main['SUNCC']:
402 main.Append(CCFLAGS='-Qoption ccfe')
403 main.Append(CCFLAGS='-features=gcc')
404 main.Append(CCFLAGS='-features=extensions')
405 main.Append(CCFLAGS='-library=stlport4')
406 main.Append(CCFLAGS='-xar')
407 #main.Append(CCFLAGS='-instances=semiexplicit')
408else:
409 print 'Error: Don\'t know what compiler options to use for your compiler.'
410 print ' Please fix SConstruct and src/SConscript and try again.'
411 Exit(1)
412
413# Set up common yacc/bison flags (needed for Ruby)
414main['YACCFLAGS'] = '-d'
415main['YACCHXXFILESUFFIX'] = '.hh'
416
417# Do this after we save setting back, or else we'll tack on an
418# extra 'qdo' every time we run scons.
419if main['BATCH']:
420 main['CC'] = main['BATCH_CMD'] + ' ' + main['CC']
421 main['CXX'] = main['BATCH_CMD'] + ' ' + main['CXX']
422 main['AS'] = main['BATCH_CMD'] + ' ' + main['AS']
423 main['AR'] = main['BATCH_CMD'] + ' ' + main['AR']
424 main['RANLIB'] = main['BATCH_CMD'] + ' ' + main['RANLIB']
425
426if sys.platform == 'cygwin':
427 # cygwin has some header file issues...
428 main.Append(CCFLAGS=Split("-Wno-uninitialized"))
429
430# Check for SWIG
431if not main.has_key('SWIG'):
432 print 'Error: SWIG utility not found.'
433 print ' Please install (see http://www.swig.org) and retry.'
434 Exit(1)
435
436# Check for appropriate SWIG version
437swig_version = read_command(('swig', '-version'), exception='').split()
438# First 3 words should be "SWIG Version x.y.z"
439if len(swig_version) < 3 or \
440 swig_version[0] != 'SWIG' or swig_version[1] != 'Version':
441 print 'Error determining SWIG version.'
442 Exit(1)
443
444min_swig_version = '1.3.28'
445if compare_versions(swig_version[2], min_swig_version) < 0:
446 print 'Error: SWIG version', min_swig_version, 'or newer required.'
447 print ' Installed version:', swig_version[2]
448 Exit(1)
449
450# Set up SWIG flags & scanner
451swig_flags=Split('-c++ -python -modern -templatereduce $_CPPINCFLAGS')
452main.Append(SWIGFLAGS=swig_flags)
453
454# filter out all existing swig scanners, they mess up the dependency
455# stuff for some reason
456scanners = []
457for scanner in main['SCANNERS']:
458 skeys = scanner.skeys
459 if skeys == '.i':
460 continue
461
462 if isinstance(skeys, (list, tuple)) and '.i' in skeys:
463 continue
464
465 scanners.append(scanner)
466
467# add the new swig scanner that we like better
468from SCons.Scanner import ClassicCPP as CPPScanner
469swig_inc_re = '^[ \t]*[%,#][ \t]*(?:include|import)[ \t]*(<|")([^>"]+)(>|")'
470scanners.append(CPPScanner("SwigScan", [ ".i" ], "CPPPATH", swig_inc_re))
471
472# replace the scanners list that has what we want
473main['SCANNERS'] = scanners
474
475# Add a custom Check function to the Configure context so that we can
476# figure out if the compiler adds leading underscores to global
477# variables. This is needed for the autogenerated asm files that we
478# use for embedding the python code.
479def CheckLeading(context):
480 context.Message("Checking for leading underscore in global variables...")
481 # 1) Define a global variable called x from asm so the C compiler
482 # won't change the symbol at all.
483 # 2) Declare that variable.
484 # 3) Use the variable
485 #
486 # If the compiler prepends an underscore, this will successfully
487 # link because the external symbol 'x' will be called '_x' which
488 # was defined by the asm statement. If the compiler does not
489 # prepend an underscore, this will not successfully link because
490 # '_x' will have been defined by assembly, while the C portion of
491 # the code will be trying to use 'x'
492 ret = context.TryLink('''
493 asm(".globl _x; _x: .byte 0");
494 extern int x;
495 int main() { return x; }
496 ''', extension=".c")
497 context.env.Append(LEADING_UNDERSCORE=ret)
498 context.Result(ret)
499 return ret
500
501# Platform-specific configuration. Note again that we assume that all
502# builds under a given build root run on the same host platform.
503conf = Configure(main,
504 conf_dir = joinpath(build_root, '.scons_config'),
505 log_file = joinpath(build_root, 'scons_config.log'),
506 custom_tests = { 'CheckLeading' : CheckLeading })
507
508# Check for leading underscores. Don't really need to worry either
509# way so don't need to check the return code.
510conf.CheckLeading()
511
512# Check if we should compile a 64 bit binary on Mac OS X/Darwin
513try:
514 import platform
515 uname = platform.uname()
516 if uname[0] == 'Darwin' and compare_versions(uname[2], '9.0.0') >= 0:
517 if int(read_command('sysctl -n hw.cpu64bit_capable')[0]):
518 main.Append(CCFLAGS='-arch x86_64')
519 main.Append(CFLAGS='-arch x86_64')
520 main.Append(LINKFLAGS='-arch x86_64')
521 main.Append(ASFLAGS='-arch x86_64')
522except:
523 pass
524
525# Recent versions of scons substitute a "Null" object for Configure()
526# when configuration isn't necessary, e.g., if the "--help" option is
527# present. Unfortuantely this Null object always returns false,
528# breaking all our configuration checks. We replace it with our own
529# more optimistic null object that returns True instead.
530if not conf:
531 def NullCheck(*args, **kwargs):
532 return True
533
534 class NullConf:
535 def __init__(self, env):
536 self.env = env
537 def Finish(self):
538 return self.env
539 def __getattr__(self, mname):
540 return NullCheck
541
542 conf = NullConf(main)
543
544# Find Python include and library directories for embedding the
545# interpreter. For consistency, we will use the same Python
546# installation used to run scons (and thus this script). If you want
547# to link in an alternate version, see above for instructions on how
548# to invoke scons with a different copy of the Python interpreter.
549from distutils import sysconfig
550
551py_getvar = sysconfig.get_config_var
552
553py_version = 'python' + py_getvar('VERSION')
554
555py_general_include = sysconfig.get_python_inc()
556py_platform_include = sysconfig.get_python_inc(plat_specific=True)
557py_includes = [ py_general_include ]
558if py_platform_include != py_general_include:
559 py_includes.append(py_platform_include)
560
561py_lib_path = [ py_getvar('LIBDIR') ]
562# add the prefix/lib/pythonX.Y/config dir, but only if there is no
563# shared library in prefix/lib/.
564if not py_getvar('Py_ENABLE_SHARED'):
565 py_lib_path.append(py_getvar('LIBPL'))
566
567py_libs = []
568for lib in py_getvar('LIBS').split() + py_getvar('SYSLIBS').split():
569 assert lib.startswith('-l')
570 lib = lib[2:]
571 if lib not in py_libs:
572 py_libs.append(lib)
573py_libs.append(py_version)
574
575main.Append(CPPPATH=py_includes)
576main.Append(LIBPATH=py_lib_path)
577
578# verify that this stuff works
579if not conf.CheckHeader('Python.h', '<>'):
580 print "Error: can't find Python.h header in", py_includes
581 Exit(1)
582
583for lib in py_libs:
584 if not conf.CheckLib(lib):
585 print "Error: can't find library %s required by python" % lib
586 Exit(1)
587
588# On Solaris you need to use libsocket for socket ops
589if not conf.CheckLibWithHeader(None, 'sys/socket.h', 'C++', 'accept(0,0,0);'):
590 if not conf.CheckLibWithHeader('socket', 'sys/socket.h', 'C++', 'accept(0,0,0);'):
591 print "Can't find library with socket calls (e.g. accept())"
592 Exit(1)
593
594# Check for zlib. If the check passes, libz will be automatically
595# added to the LIBS environment variable.
596if not conf.CheckLibWithHeader('z', 'zlib.h', 'C++','zlibVersion();'):
597 print 'Error: did not find needed zlib compression library '\
598 'and/or zlib.h header file.'
599 print ' Please install zlib and try again.'
600 Exit(1)
601
602# Check for <fenv.h> (C99 FP environment control)
603have_fenv = conf.CheckHeader('fenv.h', '<>')
604if not have_fenv:
605 print "Warning: Header file <fenv.h> not found."
606 print " This host has no IEEE FP rounding mode control."
607
608######################################################################
609#
610# Check for mysql.
611#
612mysql_config = WhereIs('mysql_config')
613have_mysql = bool(mysql_config)
614
615# Check MySQL version.
616if have_mysql:
617 mysql_version = read_command(mysql_config + ' --version')
618 min_mysql_version = '4.1'
619 if compare_versions(mysql_version, min_mysql_version) < 0:
620 print 'Warning: MySQL', min_mysql_version, 'or newer required.'
621 print ' Version', mysql_version, 'detected.'
622 have_mysql = False
623
624# Set up mysql_config commands.
625if have_mysql:
626 mysql_config_include = mysql_config + ' --include'
627 if os.system(mysql_config_include + ' > /dev/null') != 0:
628 # older mysql_config versions don't support --include, use
629 # --cflags instead
630 mysql_config_include = mysql_config + ' --cflags | sed s/\\\'//g'
631 # This seems to work in all versions
632 mysql_config_libs = mysql_config + ' --libs'
633
634######################################################################
635#
636# Finish the configuration
637#
638main = conf.Finish()
639
640######################################################################
641#
642# Collect all non-global variables
643#
644
645# Define the universe of supported ISAs
646all_isa_list = [ ]
647Export('all_isa_list')
648
649# Define the universe of supported CPU models
650all_cpu_list = [ ]
651default_cpus = [ ]
652Export('all_cpu_list', 'default_cpus')
653
654# Sticky variables get saved in the variables file so they persist from
655# one invocation to the next (unless overridden, in which case the new
656# value becomes sticky).
657sticky_vars = Variables(args=ARGUMENTS)
658Export('sticky_vars')
659
660# Sticky variables that should be exported
661export_vars = []
662Export('export_vars')
663
664# Non-sticky variables only apply to the current build.
665nonsticky_vars = Variables(args=ARGUMENTS)
666Export('nonsticky_vars')
667
668# Walk the tree and execute all SConsopts scripts that wil add to the
669# above variables
670for bdir in [ base_dir ] + extras_dir_list:
671 for root, dirs, files in os.walk(bdir):
672 if 'SConsopts' in files:
673 print "Reading", joinpath(root, 'SConsopts')
674 SConscript(joinpath(root, 'SConsopts'))
675
676all_isa_list.sort()
677all_cpu_list.sort()
678default_cpus.sort()
679
680sticky_vars.AddVariables(
681 EnumVariable('TARGET_ISA', 'Target ISA', 'alpha', all_isa_list),
682 BoolVariable('FULL_SYSTEM', 'Full-system support', False),
683 ListVariable('CPU_MODELS', 'CPU models', default_cpus, all_cpu_list),
684 BoolVariable('NO_FAST_ALLOC', 'Disable fast object allocator', False),
685 BoolVariable('FAST_ALLOC_DEBUG', 'Enable fast object allocator debugging',
686 False),
687 BoolVariable('FAST_ALLOC_STATS', 'Enable fast object allocator statistics',
688 False),
689 BoolVariable('EFENCE', 'Link with Electric Fence malloc debugger',
690 False),
691 BoolVariable('SS_COMPATIBLE_FP',
692 'Make floating-point results compatible with SimpleScalar',
693 False),
694 BoolVariable('USE_SSE2',
695 'Compile for SSE2 (-msse2) to get IEEE FP on x86 hosts',
696 False),
697 BoolVariable('USE_MYSQL', 'Use MySQL for stats output', have_mysql),
698 BoolVariable('USE_FENV', 'Use <fenv.h> IEEE mode control', have_fenv),
699 BoolVariable('USE_CHECKER', 'Use checker for detailed CPU models', False),
700 BoolVariable('CP_ANNOTATE', 'Enable critical path annotation capability', False),
701 )
702
703nonsticky_vars.AddVariables(
704 BoolVariable('update_ref', 'Update test reference outputs', False)
705 )
706
707# These variables get exported to #defines in config/*.hh (see src/SConscript).
708export_vars += ['FULL_SYSTEM', 'USE_FENV', 'USE_MYSQL',
709 'NO_FAST_ALLOC', 'FAST_ALLOC_DEBUG', 'FAST_ALLOC_STATS',
710 'SS_COMPATIBLE_FP', 'USE_CHECKER', 'TARGET_ISA', 'CP_ANNOTATE']
711
712###################################################
713#
714# Define a SCons builder for configuration flag headers.
715#
716###################################################
717
718# This function generates a config header file that #defines the
719# variable symbol to the current variable setting (0 or 1). The source
720# operands are the name of the variable and a Value node containing the
721# value of the variable.
722def build_config_file(target, source, env):
723 (variable, value) = [s.get_contents() for s in source]
724 f = file(str(target[0]), 'w')
725 print >> f, '#define', variable, value
726 f.close()
727 return None
728
729# Generate the message to be printed when building the config file.
730def build_config_file_string(target, source, env):
731 (variable, value) = [s.get_contents() for s in source]
732 return "Defining %s as %s in %s." % (variable, value, target[0])
733
734# Combine the two functions into a scons Action object.
735config_action = Action(build_config_file, build_config_file_string)
736
737# The emitter munges the source & target node lists to reflect what
738# we're really doing.
739def config_emitter(target, source, env):
740 # extract variable name from Builder arg
741 variable = str(target[0])
742 # True target is config header file
743 target = joinpath('config', variable.lower() + '.hh')
744 val = env[variable]
745 if isinstance(val, bool):
746 # Force value to 0/1
747 val = int(val)
748 elif isinstance(val, str):
749 val = '"' + val + '"'
750
751 # Sources are variable name & value (packaged in SCons Value nodes)
752 return ([target], [Value(variable), Value(val)])
753
754config_builder = Builder(emitter = config_emitter, action = config_action)
755
756main.Append(BUILDERS = { 'ConfigFile' : config_builder })
757
758# libelf build is shared across all configs in the build root.
759main.SConscript('ext/libelf/SConscript',
760 variant_dir = joinpath(build_root, 'libelf'))
761
762# gzstream build is shared across all configs in the build root.
763main.SConscript('ext/gzstream/SConscript',
764 variant_dir = joinpath(build_root, 'gzstream'))
765
766###################################################
767#
768# This function is used to set up a directory with switching headers
769#
770###################################################
771
772main['ALL_ISA_LIST'] = all_isa_list
773def make_switching_dir(dname, switch_headers, env):
774 # Generate the header. target[0] is the full path of the output
775 # header to generate. 'source' is a dummy variable, since we get the
776 # list of ISAs from env['ALL_ISA_LIST'].
777 def gen_switch_hdr(target, source, env):
778 fname = str(target[0])
779 bname = basename(fname)
780 f = open(fname, 'w')
781 f.write('#include "arch/isa_specific.hh"\n')
782 cond = '#if'
783 for isa in all_isa_list:
784 f.write('%s THE_ISA == %s_ISA\n#include "%s/%s/%s"\n'
785 % (cond, isa.upper(), dname, isa, bname))
786 cond = '#elif'
787 f.write('#else\n#error "THE_ISA not set"\n#endif\n')
788 f.close()
789 return 0
790
791 # String to print when generating header
792 def gen_switch_hdr_string(target, source, env):
793 return "Generating switch header " + str(target[0])
794
795 # Build SCons Action object. 'varlist' specifies env vars that this
796 # action depends on; when env['ALL_ISA_LIST'] changes these actions
797 # should get re-executed.
798 switch_hdr_action = Action(gen_switch_hdr, gen_switch_hdr_string,
799 varlist=['ALL_ISA_LIST'])
800
801 # Instantiate actions for each header
802 for hdr in switch_headers:
803 env.Command(hdr, [], switch_hdr_action)
804Export('make_switching_dir')
805
806###################################################
807#
808# Define build environments for selected configurations.
809#
810###################################################
811
812for variant_path in variant_paths:
813 print "Building in", variant_path
814
815 # Make a copy of the build-root environment to use for this config.
816 env = main.Clone()
817 env['BUILDDIR'] = variant_path
818
819 # variant_dir is the tail component of build path, and is used to
820 # determine the build parameters (e.g., 'ALPHA_SE')
821 (build_root, variant_dir) = splitpath(variant_path)
822
823 # Set env variables according to the build directory config.
824 sticky_vars.files = []
825 # Variables for $BUILD_ROOT/$VARIANT_DIR are stored in
826 # $BUILD_ROOT/variables/$VARIANT_DIR so you can nuke
827 # $BUILD_ROOT/$VARIANT_DIR without losing your variables settings.
828 current_vars_file = joinpath(build_root, 'variables', variant_dir)
829 if isfile(current_vars_file):
830 sticky_vars.files.append(current_vars_file)
831 print "Using saved variables file %s" % current_vars_file
832 else:
833 # Build dir-specific variables file doesn't exist.
834
835 # Make sure the directory is there so we can create it later
836 opt_dir = dirname(current_vars_file)
837 if not isdir(opt_dir):
838 mkdir(opt_dir)
839
840 # Get default build variables from source tree. Variables are
841 # normally determined by name of $VARIANT_DIR, but can be
842 # overriden by 'default=' arg on command line.
843 default_vars_file = joinpath('build_opts',
844 ARGUMENTS.get('default', variant_dir))
845 if isfile(default_vars_file):
846 sticky_vars.files.append(default_vars_file)
847 print "Variables file %s not found,\n using defaults in %s" \
848 % (current_vars_file, default_vars_file)
849 else:
850 print "Error: cannot find variables file %s or %s" \
851 % (current_vars_file, default_vars_file)
852 Exit(1)
853
854 # Apply current variable settings to env
855 sticky_vars.Update(env)
856 nonsticky_vars.Update(env)
857
858 help_text += "\nSticky variables for %s:\n" % variant_dir \
859 + sticky_vars.GenerateHelpText(env) \
860 + "\nNon-sticky variables for %s:\n" % variant_dir \
861 + nonsticky_vars.GenerateHelpText(env)
862
863 # Process variable settings.
864
865 if not have_fenv and env['USE_FENV']:
866 print "Warning: <fenv.h> not available; " \
867 "forcing USE_FENV to False in", variant_dir + "."
868 env['USE_FENV'] = False
869
870 if not env['USE_FENV']:
871 print "Warning: No IEEE FP rounding mode control in", variant_dir + "."
872 print " FP results may deviate slightly from other platforms."
873
874 if env['EFENCE']:
875 env.Append(LIBS=['efence'])
876
877 if env['USE_MYSQL']:
878 if not have_mysql:
879 print "Warning: MySQL not available; " \
880 "forcing USE_MYSQL to False in", variant_dir + "."
881 env['USE_MYSQL'] = False
882 else:
883 print "Compiling in", variant_dir, "with MySQL support."
884 env.ParseConfig(mysql_config_libs)
885 env.ParseConfig(mysql_config_include)
886
887 # Save sticky variable settings back to current variables file
888 sticky_vars.Save(current_vars_file, env)
889
890 if env['USE_SSE2']:
891 env.Append(CCFLAGS='-msse2')
892
893 # The src/SConscript file sets up the build rules in 'env' according
894 # to the configured variables. It returns a list of environments,
895 # one for each variant build (debug, opt, etc.)
896 envList = SConscript('src/SConscript', variant_dir = variant_path,
897 exports = 'env')
898
899 # Set up the regression tests for each build.
900 for e in envList:
901 SConscript('tests/SConscript',
902 variant_dir = joinpath(variant_path, 'tests', e.Label),
903 exports = { 'env' : e }, duplicate = False)
904
905Help(help_text)
351
352# base help text
353help_text = '''
354Usage: scons [scons options] [build options] [target(s)]
355
356Global sticky options:
357'''
358
359help_text += global_sticky_vars.GenerateHelpText(main)
360
361# Update main environment with values from ARGUMENTS & global_sticky_vars_file
362global_sticky_vars.Update(main)
363
364# Save sticky variable settings back to current variables file
365global_sticky_vars.Save(global_sticky_vars_file, main)
366
367# Parse EXTRAS variable to build list of all directories where we're
368# look for sources etc. This list is exported as base_dir_list.
369base_dir = main.srcdir.abspath
370if main['EXTRAS']:
371 extras_dir_list = main['EXTRAS'].split(':')
372else:
373 extras_dir_list = []
374
375Export('base_dir')
376Export('extras_dir_list')
377
378# the ext directory should be on the #includes path
379main.Append(CPPPATH=[Dir('ext')])
380
381# M5_PLY is used by isa_parser.py to find the PLY package.
382main.Append(ENV = { 'M5_PLY' : Dir('ext/ply').abspath })
383
384CXX_version = read_command([main['CXX'],'--version'], exception=False)
385CXX_V = read_command([main['CXX'],'-V'], exception=False)
386
387main['GCC'] = CXX_version and CXX_version.find('g++') >= 0
388main['SUNCC'] = CXX_V and CXX_V.find('Sun C++') >= 0
389main['ICC'] = CXX_V and CXX_V.find('Intel') >= 0
390if main['GCC'] + main['SUNCC'] + main['ICC'] > 1:
391 print 'Error: How can we have two at the same time?'
392 Exit(1)
393
394# Set up default C++ compiler flags
395if main['GCC']:
396 main.Append(CCFLAGS='-pipe')
397 main.Append(CCFLAGS='-fno-strict-aliasing')
398 main.Append(CCFLAGS=['-Wall', '-Wno-sign-compare', '-Wundef'])
399 main.Append(CXXFLAGS='-Wno-deprecated')
400elif main['ICC']:
401 pass #Fix me... add warning flags once we clean up icc warnings
402elif main['SUNCC']:
403 main.Append(CCFLAGS='-Qoption ccfe')
404 main.Append(CCFLAGS='-features=gcc')
405 main.Append(CCFLAGS='-features=extensions')
406 main.Append(CCFLAGS='-library=stlport4')
407 main.Append(CCFLAGS='-xar')
408 #main.Append(CCFLAGS='-instances=semiexplicit')
409else:
410 print 'Error: Don\'t know what compiler options to use for your compiler.'
411 print ' Please fix SConstruct and src/SConscript and try again.'
412 Exit(1)
413
414# Set up common yacc/bison flags (needed for Ruby)
415main['YACCFLAGS'] = '-d'
416main['YACCHXXFILESUFFIX'] = '.hh'
417
418# Do this after we save setting back, or else we'll tack on an
419# extra 'qdo' every time we run scons.
420if main['BATCH']:
421 main['CC'] = main['BATCH_CMD'] + ' ' + main['CC']
422 main['CXX'] = main['BATCH_CMD'] + ' ' + main['CXX']
423 main['AS'] = main['BATCH_CMD'] + ' ' + main['AS']
424 main['AR'] = main['BATCH_CMD'] + ' ' + main['AR']
425 main['RANLIB'] = main['BATCH_CMD'] + ' ' + main['RANLIB']
426
427if sys.platform == 'cygwin':
428 # cygwin has some header file issues...
429 main.Append(CCFLAGS=Split("-Wno-uninitialized"))
430
431# Check for SWIG
432if not main.has_key('SWIG'):
433 print 'Error: SWIG utility not found.'
434 print ' Please install (see http://www.swig.org) and retry.'
435 Exit(1)
436
437# Check for appropriate SWIG version
438swig_version = read_command(('swig', '-version'), exception='').split()
439# First 3 words should be "SWIG Version x.y.z"
440if len(swig_version) < 3 or \
441 swig_version[0] != 'SWIG' or swig_version[1] != 'Version':
442 print 'Error determining SWIG version.'
443 Exit(1)
444
445min_swig_version = '1.3.28'
446if compare_versions(swig_version[2], min_swig_version) < 0:
447 print 'Error: SWIG version', min_swig_version, 'or newer required.'
448 print ' Installed version:', swig_version[2]
449 Exit(1)
450
451# Set up SWIG flags & scanner
452swig_flags=Split('-c++ -python -modern -templatereduce $_CPPINCFLAGS')
453main.Append(SWIGFLAGS=swig_flags)
454
455# filter out all existing swig scanners, they mess up the dependency
456# stuff for some reason
457scanners = []
458for scanner in main['SCANNERS']:
459 skeys = scanner.skeys
460 if skeys == '.i':
461 continue
462
463 if isinstance(skeys, (list, tuple)) and '.i' in skeys:
464 continue
465
466 scanners.append(scanner)
467
468# add the new swig scanner that we like better
469from SCons.Scanner import ClassicCPP as CPPScanner
470swig_inc_re = '^[ \t]*[%,#][ \t]*(?:include|import)[ \t]*(<|")([^>"]+)(>|")'
471scanners.append(CPPScanner("SwigScan", [ ".i" ], "CPPPATH", swig_inc_re))
472
473# replace the scanners list that has what we want
474main['SCANNERS'] = scanners
475
476# Add a custom Check function to the Configure context so that we can
477# figure out if the compiler adds leading underscores to global
478# variables. This is needed for the autogenerated asm files that we
479# use for embedding the python code.
480def CheckLeading(context):
481 context.Message("Checking for leading underscore in global variables...")
482 # 1) Define a global variable called x from asm so the C compiler
483 # won't change the symbol at all.
484 # 2) Declare that variable.
485 # 3) Use the variable
486 #
487 # If the compiler prepends an underscore, this will successfully
488 # link because the external symbol 'x' will be called '_x' which
489 # was defined by the asm statement. If the compiler does not
490 # prepend an underscore, this will not successfully link because
491 # '_x' will have been defined by assembly, while the C portion of
492 # the code will be trying to use 'x'
493 ret = context.TryLink('''
494 asm(".globl _x; _x: .byte 0");
495 extern int x;
496 int main() { return x; }
497 ''', extension=".c")
498 context.env.Append(LEADING_UNDERSCORE=ret)
499 context.Result(ret)
500 return ret
501
502# Platform-specific configuration. Note again that we assume that all
503# builds under a given build root run on the same host platform.
504conf = Configure(main,
505 conf_dir = joinpath(build_root, '.scons_config'),
506 log_file = joinpath(build_root, 'scons_config.log'),
507 custom_tests = { 'CheckLeading' : CheckLeading })
508
509# Check for leading underscores. Don't really need to worry either
510# way so don't need to check the return code.
511conf.CheckLeading()
512
513# Check if we should compile a 64 bit binary on Mac OS X/Darwin
514try:
515 import platform
516 uname = platform.uname()
517 if uname[0] == 'Darwin' and compare_versions(uname[2], '9.0.0') >= 0:
518 if int(read_command('sysctl -n hw.cpu64bit_capable')[0]):
519 main.Append(CCFLAGS='-arch x86_64')
520 main.Append(CFLAGS='-arch x86_64')
521 main.Append(LINKFLAGS='-arch x86_64')
522 main.Append(ASFLAGS='-arch x86_64')
523except:
524 pass
525
526# Recent versions of scons substitute a "Null" object for Configure()
527# when configuration isn't necessary, e.g., if the "--help" option is
528# present. Unfortuantely this Null object always returns false,
529# breaking all our configuration checks. We replace it with our own
530# more optimistic null object that returns True instead.
531if not conf:
532 def NullCheck(*args, **kwargs):
533 return True
534
535 class NullConf:
536 def __init__(self, env):
537 self.env = env
538 def Finish(self):
539 return self.env
540 def __getattr__(self, mname):
541 return NullCheck
542
543 conf = NullConf(main)
544
545# Find Python include and library directories for embedding the
546# interpreter. For consistency, we will use the same Python
547# installation used to run scons (and thus this script). If you want
548# to link in an alternate version, see above for instructions on how
549# to invoke scons with a different copy of the Python interpreter.
550from distutils import sysconfig
551
552py_getvar = sysconfig.get_config_var
553
554py_version = 'python' + py_getvar('VERSION')
555
556py_general_include = sysconfig.get_python_inc()
557py_platform_include = sysconfig.get_python_inc(plat_specific=True)
558py_includes = [ py_general_include ]
559if py_platform_include != py_general_include:
560 py_includes.append(py_platform_include)
561
562py_lib_path = [ py_getvar('LIBDIR') ]
563# add the prefix/lib/pythonX.Y/config dir, but only if there is no
564# shared library in prefix/lib/.
565if not py_getvar('Py_ENABLE_SHARED'):
566 py_lib_path.append(py_getvar('LIBPL'))
567
568py_libs = []
569for lib in py_getvar('LIBS').split() + py_getvar('SYSLIBS').split():
570 assert lib.startswith('-l')
571 lib = lib[2:]
572 if lib not in py_libs:
573 py_libs.append(lib)
574py_libs.append(py_version)
575
576main.Append(CPPPATH=py_includes)
577main.Append(LIBPATH=py_lib_path)
578
579# verify that this stuff works
580if not conf.CheckHeader('Python.h', '<>'):
581 print "Error: can't find Python.h header in", py_includes
582 Exit(1)
583
584for lib in py_libs:
585 if not conf.CheckLib(lib):
586 print "Error: can't find library %s required by python" % lib
587 Exit(1)
588
589# On Solaris you need to use libsocket for socket ops
590if not conf.CheckLibWithHeader(None, 'sys/socket.h', 'C++', 'accept(0,0,0);'):
591 if not conf.CheckLibWithHeader('socket', 'sys/socket.h', 'C++', 'accept(0,0,0);'):
592 print "Can't find library with socket calls (e.g. accept())"
593 Exit(1)
594
595# Check for zlib. If the check passes, libz will be automatically
596# added to the LIBS environment variable.
597if not conf.CheckLibWithHeader('z', 'zlib.h', 'C++','zlibVersion();'):
598 print 'Error: did not find needed zlib compression library '\
599 'and/or zlib.h header file.'
600 print ' Please install zlib and try again.'
601 Exit(1)
602
603# Check for <fenv.h> (C99 FP environment control)
604have_fenv = conf.CheckHeader('fenv.h', '<>')
605if not have_fenv:
606 print "Warning: Header file <fenv.h> not found."
607 print " This host has no IEEE FP rounding mode control."
608
609######################################################################
610#
611# Check for mysql.
612#
613mysql_config = WhereIs('mysql_config')
614have_mysql = bool(mysql_config)
615
616# Check MySQL version.
617if have_mysql:
618 mysql_version = read_command(mysql_config + ' --version')
619 min_mysql_version = '4.1'
620 if compare_versions(mysql_version, min_mysql_version) < 0:
621 print 'Warning: MySQL', min_mysql_version, 'or newer required.'
622 print ' Version', mysql_version, 'detected.'
623 have_mysql = False
624
625# Set up mysql_config commands.
626if have_mysql:
627 mysql_config_include = mysql_config + ' --include'
628 if os.system(mysql_config_include + ' > /dev/null') != 0:
629 # older mysql_config versions don't support --include, use
630 # --cflags instead
631 mysql_config_include = mysql_config + ' --cflags | sed s/\\\'//g'
632 # This seems to work in all versions
633 mysql_config_libs = mysql_config + ' --libs'
634
635######################################################################
636#
637# Finish the configuration
638#
639main = conf.Finish()
640
641######################################################################
642#
643# Collect all non-global variables
644#
645
646# Define the universe of supported ISAs
647all_isa_list = [ ]
648Export('all_isa_list')
649
650# Define the universe of supported CPU models
651all_cpu_list = [ ]
652default_cpus = [ ]
653Export('all_cpu_list', 'default_cpus')
654
655# Sticky variables get saved in the variables file so they persist from
656# one invocation to the next (unless overridden, in which case the new
657# value becomes sticky).
658sticky_vars = Variables(args=ARGUMENTS)
659Export('sticky_vars')
660
661# Sticky variables that should be exported
662export_vars = []
663Export('export_vars')
664
665# Non-sticky variables only apply to the current build.
666nonsticky_vars = Variables(args=ARGUMENTS)
667Export('nonsticky_vars')
668
669# Walk the tree and execute all SConsopts scripts that wil add to the
670# above variables
671for bdir in [ base_dir ] + extras_dir_list:
672 for root, dirs, files in os.walk(bdir):
673 if 'SConsopts' in files:
674 print "Reading", joinpath(root, 'SConsopts')
675 SConscript(joinpath(root, 'SConsopts'))
676
677all_isa_list.sort()
678all_cpu_list.sort()
679default_cpus.sort()
680
681sticky_vars.AddVariables(
682 EnumVariable('TARGET_ISA', 'Target ISA', 'alpha', all_isa_list),
683 BoolVariable('FULL_SYSTEM', 'Full-system support', False),
684 ListVariable('CPU_MODELS', 'CPU models', default_cpus, all_cpu_list),
685 BoolVariable('NO_FAST_ALLOC', 'Disable fast object allocator', False),
686 BoolVariable('FAST_ALLOC_DEBUG', 'Enable fast object allocator debugging',
687 False),
688 BoolVariable('FAST_ALLOC_STATS', 'Enable fast object allocator statistics',
689 False),
690 BoolVariable('EFENCE', 'Link with Electric Fence malloc debugger',
691 False),
692 BoolVariable('SS_COMPATIBLE_FP',
693 'Make floating-point results compatible with SimpleScalar',
694 False),
695 BoolVariable('USE_SSE2',
696 'Compile for SSE2 (-msse2) to get IEEE FP on x86 hosts',
697 False),
698 BoolVariable('USE_MYSQL', 'Use MySQL for stats output', have_mysql),
699 BoolVariable('USE_FENV', 'Use <fenv.h> IEEE mode control', have_fenv),
700 BoolVariable('USE_CHECKER', 'Use checker for detailed CPU models', False),
701 BoolVariable('CP_ANNOTATE', 'Enable critical path annotation capability', False),
702 )
703
704nonsticky_vars.AddVariables(
705 BoolVariable('update_ref', 'Update test reference outputs', False)
706 )
707
708# These variables get exported to #defines in config/*.hh (see src/SConscript).
709export_vars += ['FULL_SYSTEM', 'USE_FENV', 'USE_MYSQL',
710 'NO_FAST_ALLOC', 'FAST_ALLOC_DEBUG', 'FAST_ALLOC_STATS',
711 'SS_COMPATIBLE_FP', 'USE_CHECKER', 'TARGET_ISA', 'CP_ANNOTATE']
712
713###################################################
714#
715# Define a SCons builder for configuration flag headers.
716#
717###################################################
718
719# This function generates a config header file that #defines the
720# variable symbol to the current variable setting (0 or 1). The source
721# operands are the name of the variable and a Value node containing the
722# value of the variable.
723def build_config_file(target, source, env):
724 (variable, value) = [s.get_contents() for s in source]
725 f = file(str(target[0]), 'w')
726 print >> f, '#define', variable, value
727 f.close()
728 return None
729
730# Generate the message to be printed when building the config file.
731def build_config_file_string(target, source, env):
732 (variable, value) = [s.get_contents() for s in source]
733 return "Defining %s as %s in %s." % (variable, value, target[0])
734
735# Combine the two functions into a scons Action object.
736config_action = Action(build_config_file, build_config_file_string)
737
738# The emitter munges the source & target node lists to reflect what
739# we're really doing.
740def config_emitter(target, source, env):
741 # extract variable name from Builder arg
742 variable = str(target[0])
743 # True target is config header file
744 target = joinpath('config', variable.lower() + '.hh')
745 val = env[variable]
746 if isinstance(val, bool):
747 # Force value to 0/1
748 val = int(val)
749 elif isinstance(val, str):
750 val = '"' + val + '"'
751
752 # Sources are variable name & value (packaged in SCons Value nodes)
753 return ([target], [Value(variable), Value(val)])
754
755config_builder = Builder(emitter = config_emitter, action = config_action)
756
757main.Append(BUILDERS = { 'ConfigFile' : config_builder })
758
759# libelf build is shared across all configs in the build root.
760main.SConscript('ext/libelf/SConscript',
761 variant_dir = joinpath(build_root, 'libelf'))
762
763# gzstream build is shared across all configs in the build root.
764main.SConscript('ext/gzstream/SConscript',
765 variant_dir = joinpath(build_root, 'gzstream'))
766
767###################################################
768#
769# This function is used to set up a directory with switching headers
770#
771###################################################
772
773main['ALL_ISA_LIST'] = all_isa_list
774def make_switching_dir(dname, switch_headers, env):
775 # Generate the header. target[0] is the full path of the output
776 # header to generate. 'source' is a dummy variable, since we get the
777 # list of ISAs from env['ALL_ISA_LIST'].
778 def gen_switch_hdr(target, source, env):
779 fname = str(target[0])
780 bname = basename(fname)
781 f = open(fname, 'w')
782 f.write('#include "arch/isa_specific.hh"\n')
783 cond = '#if'
784 for isa in all_isa_list:
785 f.write('%s THE_ISA == %s_ISA\n#include "%s/%s/%s"\n'
786 % (cond, isa.upper(), dname, isa, bname))
787 cond = '#elif'
788 f.write('#else\n#error "THE_ISA not set"\n#endif\n')
789 f.close()
790 return 0
791
792 # String to print when generating header
793 def gen_switch_hdr_string(target, source, env):
794 return "Generating switch header " + str(target[0])
795
796 # Build SCons Action object. 'varlist' specifies env vars that this
797 # action depends on; when env['ALL_ISA_LIST'] changes these actions
798 # should get re-executed.
799 switch_hdr_action = Action(gen_switch_hdr, gen_switch_hdr_string,
800 varlist=['ALL_ISA_LIST'])
801
802 # Instantiate actions for each header
803 for hdr in switch_headers:
804 env.Command(hdr, [], switch_hdr_action)
805Export('make_switching_dir')
806
807###################################################
808#
809# Define build environments for selected configurations.
810#
811###################################################
812
813for variant_path in variant_paths:
814 print "Building in", variant_path
815
816 # Make a copy of the build-root environment to use for this config.
817 env = main.Clone()
818 env['BUILDDIR'] = variant_path
819
820 # variant_dir is the tail component of build path, and is used to
821 # determine the build parameters (e.g., 'ALPHA_SE')
822 (build_root, variant_dir) = splitpath(variant_path)
823
824 # Set env variables according to the build directory config.
825 sticky_vars.files = []
826 # Variables for $BUILD_ROOT/$VARIANT_DIR are stored in
827 # $BUILD_ROOT/variables/$VARIANT_DIR so you can nuke
828 # $BUILD_ROOT/$VARIANT_DIR without losing your variables settings.
829 current_vars_file = joinpath(build_root, 'variables', variant_dir)
830 if isfile(current_vars_file):
831 sticky_vars.files.append(current_vars_file)
832 print "Using saved variables file %s" % current_vars_file
833 else:
834 # Build dir-specific variables file doesn't exist.
835
836 # Make sure the directory is there so we can create it later
837 opt_dir = dirname(current_vars_file)
838 if not isdir(opt_dir):
839 mkdir(opt_dir)
840
841 # Get default build variables from source tree. Variables are
842 # normally determined by name of $VARIANT_DIR, but can be
843 # overriden by 'default=' arg on command line.
844 default_vars_file = joinpath('build_opts',
845 ARGUMENTS.get('default', variant_dir))
846 if isfile(default_vars_file):
847 sticky_vars.files.append(default_vars_file)
848 print "Variables file %s not found,\n using defaults in %s" \
849 % (current_vars_file, default_vars_file)
850 else:
851 print "Error: cannot find variables file %s or %s" \
852 % (current_vars_file, default_vars_file)
853 Exit(1)
854
855 # Apply current variable settings to env
856 sticky_vars.Update(env)
857 nonsticky_vars.Update(env)
858
859 help_text += "\nSticky variables for %s:\n" % variant_dir \
860 + sticky_vars.GenerateHelpText(env) \
861 + "\nNon-sticky variables for %s:\n" % variant_dir \
862 + nonsticky_vars.GenerateHelpText(env)
863
864 # Process variable settings.
865
866 if not have_fenv and env['USE_FENV']:
867 print "Warning: <fenv.h> not available; " \
868 "forcing USE_FENV to False in", variant_dir + "."
869 env['USE_FENV'] = False
870
871 if not env['USE_FENV']:
872 print "Warning: No IEEE FP rounding mode control in", variant_dir + "."
873 print " FP results may deviate slightly from other platforms."
874
875 if env['EFENCE']:
876 env.Append(LIBS=['efence'])
877
878 if env['USE_MYSQL']:
879 if not have_mysql:
880 print "Warning: MySQL not available; " \
881 "forcing USE_MYSQL to False in", variant_dir + "."
882 env['USE_MYSQL'] = False
883 else:
884 print "Compiling in", variant_dir, "with MySQL support."
885 env.ParseConfig(mysql_config_libs)
886 env.ParseConfig(mysql_config_include)
887
888 # Save sticky variable settings back to current variables file
889 sticky_vars.Save(current_vars_file, env)
890
891 if env['USE_SSE2']:
892 env.Append(CCFLAGS='-msse2')
893
894 # The src/SConscript file sets up the build rules in 'env' according
895 # to the configured variables. It returns a list of environments,
896 # one for each variant build (debug, opt, etc.)
897 envList = SConscript('src/SConscript', variant_dir = variant_path,
898 exports = 'env')
899
900 # Set up the regression tests for each build.
901 for e in envList:
902 SConscript('tests/SConscript',
903 variant_dir = joinpath(variant_path, 'tests', e.Label),
904 exports = { 'env' : e }, duplicate = False)
905
906Help(help_text)