SConstruct revision 5385:658926ff82ed
1955SN/A# -*- mode:python -*-
2955SN/A
39812Sandreas.hansson@arm.com# Copyright (c) 2004-2005 The Regents of The University of Michigan
49812Sandreas.hansson@arm.com# All rights reserved.
59812Sandreas.hansson@arm.com#
69812Sandreas.hansson@arm.com# Redistribution and use in source and binary forms, with or without
79812Sandreas.hansson@arm.com# modification, are permitted provided that the following conditions are
89812Sandreas.hansson@arm.com# met: redistributions of source code must retain the above copyright
99812Sandreas.hansson@arm.com# notice, this list of conditions and the following disclaimer;
109812Sandreas.hansson@arm.com# redistributions in binary form must reproduce the above copyright
119812Sandreas.hansson@arm.com# notice, this list of conditions and the following disclaimer in the
129812Sandreas.hansson@arm.com# documentation and/or other materials provided with the distribution;
139812Sandreas.hansson@arm.com# neither the name of the copyright holders nor the names of its
149812Sandreas.hansson@arm.com# contributors may be used to endorse or promote products derived from
157816Ssteve.reinhardt@amd.com# this software without specific prior written permission.
165871Snate@binkert.org#
171762SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18955SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19955SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20955SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21955SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22955SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23955SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24955SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25955SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26955SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27955SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28955SN/A#
29955SN/A# Authors: Steve Reinhardt
30955SN/A
31955SN/A###################################################
32955SN/A#
33955SN/A# SCons top-level build description (SConstruct) file.
34955SN/A#
35955SN/A# While in this directory ('m5'), just type 'scons' to build the default
36955SN/A# configuration (see below), or type 'scons build/<CONFIG>/<binary>'
37955SN/A# to build some other configuration (e.g., 'build/ALPHA_FS/m5.opt' for
38955SN/A# the optimized full-system version).
39955SN/A#
40955SN/A# You can build M5 in a different directory as long as there is a
41955SN/A# 'build/<CONFIG>' somewhere along the target path.  The build system
422665Ssaidi@eecs.umich.edu# expects that all configs under the same build directory are being
432665Ssaidi@eecs.umich.edu# built for the same host system.
445863Snate@binkert.org#
45955SN/A# Examples:
46955SN/A#
47955SN/A#   The following two commands are equivalent.  The '-u' option tells
48955SN/A#   scons to search up the directory tree for this SConstruct file.
49955SN/A#   % cd <path-to-src>/m5 ; scons build/ALPHA_FS/m5.debug
508878Ssteve.reinhardt@amd.com#   % cd <path-to-src>/m5/build/ALPHA_FS; scons -u m5.debug
512632Sstever@eecs.umich.edu#
528878Ssteve.reinhardt@amd.com#   The following two commands are equivalent and demonstrate building
532632Sstever@eecs.umich.edu#   in a directory outside of the source tree.  The '-C' option tells
54955SN/A#   scons to chdir to the specified directory to find this SConstruct
558878Ssteve.reinhardt@amd.com#   file.
562632Sstever@eecs.umich.edu#   % cd <path-to-src>/m5 ; scons /local/foo/build/ALPHA_FS/m5.debug
572761Sstever@eecs.umich.edu#   % cd /local/foo/build/ALPHA_FS; scons -C <path-to-src>/m5 m5.debug
582632Sstever@eecs.umich.edu#
592632Sstever@eecs.umich.edu# You can use 'scons -H' to print scons options.  If you're in this
602632Sstever@eecs.umich.edu# 'm5' directory (or use -u or -C to tell scons where to find this
612761Sstever@eecs.umich.edu# file), you can use 'scons -h' to print all the M5-specific build
622761Sstever@eecs.umich.edu# options as well.
632761Sstever@eecs.umich.edu#
648878Ssteve.reinhardt@amd.com###################################################
658878Ssteve.reinhardt@amd.com
662761Sstever@eecs.umich.eduimport sys
672761Sstever@eecs.umich.eduimport os
682761Sstever@eecs.umich.edu
692761Sstever@eecs.umich.edufrom os.path import isdir, isfile, join as joinpath
702761Sstever@eecs.umich.edu
718878Ssteve.reinhardt@amd.comimport SCons
728878Ssteve.reinhardt@amd.com
732632Sstever@eecs.umich.edu# Check for recent-enough Python and SCons versions.  If your system's
742632Sstever@eecs.umich.edu# default installation of Python is not recent enough, you can use a
758878Ssteve.reinhardt@amd.com# non-default installation of the Python interpreter by either (1)
768878Ssteve.reinhardt@amd.com# rearranging your PATH so that scons finds the non-default 'python'
772632Sstever@eecs.umich.edu# first or (2) explicitly invoking an alternative interpreter on the
78955SN/A# scons script, e.g., "/usr/local/bin/python2.4 `which scons` [args]".
79955SN/AEnsurePythonVersion(2,4)
80955SN/A
815863Snate@binkert.org# Import subprocess after we check the version since it doesn't exist in
825863Snate@binkert.org# Python < 2.4.
835863Snate@binkert.orgimport subprocess
845863Snate@binkert.org
855863Snate@binkert.org# Ironically, SCons 0.96 dies if you give EnsureSconsVersion a
865863Snate@binkert.org# 3-element version number.
875863Snate@binkert.orgmin_scons_version = (0,96,91)
885863Snate@binkert.orgtry:
895863Snate@binkert.org    EnsureSConsVersion(*min_scons_version)
905863Snate@binkert.orgexcept:
915863Snate@binkert.org    print "Error checking current SCons version."
928878Ssteve.reinhardt@amd.com    print "SCons", ".".join(map(str,min_scons_version)), "or greater required."
935863Snate@binkert.org    Exit(2)
945863Snate@binkert.org
955863Snate@binkert.org
969812Sandreas.hansson@arm.com# The absolute path to the current directory (where this file lives).
979812Sandreas.hansson@arm.comROOT = Dir('.').abspath
985863Snate@binkert.org
999812Sandreas.hansson@arm.com# Path to the M5 source tree.
1005863Snate@binkert.orgSRCDIR = joinpath(ROOT, 'src')
1015863Snate@binkert.org
1025863Snate@binkert.org# tell python where to find m5 python code
1039812Sandreas.hansson@arm.comsys.path.append(joinpath(ROOT, 'src/python'))
1049812Sandreas.hansson@arm.com
1055863Snate@binkert.orgdef check_style_hook(ui):
1065863Snate@binkert.org    ui.readconfig(joinpath(ROOT, '.hg', 'hgrc'))
1078878Ssteve.reinhardt@amd.com    style_hook = ui.config('hooks', 'pretxncommit.style', None)
1085863Snate@binkert.org
1095863Snate@binkert.org    if not style_hook:
1105863Snate@binkert.org        print """\
1116654Snate@binkert.orgYou're missing the M5 style hook.
11210196SCurtis.Dunham@arm.comPlease install the hook so we can ensure that all code fits a common style.
113955SN/A
1145396Ssaidi@eecs.umich.eduAll you'd need to do is add the following lines to your repository .hg/hgrc
1155863Snate@binkert.orgor your personal .hgrc
1165863Snate@binkert.org----------------
1174202Sbinkertn@umich.edu
1185863Snate@binkert.org[extensions]
1195863Snate@binkert.orgstyle = %s/util/style.py
1205863Snate@binkert.org
1215863Snate@binkert.org[hooks]
122955SN/Apretxncommit.style = python:style.check_whitespace
1236654Snate@binkert.org""" % (ROOT)
1245273Sstever@gmail.com        sys.exit(1)
1255871Snate@binkert.org
1265273Sstever@gmail.comif ARGUMENTS.get('IGNORE_STYLE') != 'True' and isdir(joinpath(ROOT, '.hg')):
1276655Snate@binkert.org    try:
1288878Ssteve.reinhardt@amd.com        from mercurial import ui
1296655Snate@binkert.org        check_style_hook(ui.ui())
1306655Snate@binkert.org    except ImportError:
1319219Spower.jg@gmail.com        pass
1326655Snate@binkert.org
1335871Snate@binkert.org###################################################
1346654Snate@binkert.org#
1358947Sandreas.hansson@arm.com# Figure out which configurations to set up based on the path(s) of
1365396Ssaidi@eecs.umich.edu# the target(s).
1378120Sgblack@eecs.umich.edu#
1388120Sgblack@eecs.umich.edu###################################################
1398120Sgblack@eecs.umich.edu
1408120Sgblack@eecs.umich.edu# Find default configuration & binary.
1418120Sgblack@eecs.umich.eduDefault(os.environ.get('M5_DEFAULT_BINARY', 'build/ALPHA_SE/m5.debug'))
1428120Sgblack@eecs.umich.edu
1438120Sgblack@eecs.umich.edu# helper function: find last occurrence of element in list
1448120Sgblack@eecs.umich.edudef rfind(l, elt, offs = -1):
1458879Ssteve.reinhardt@amd.com    for i in range(len(l)+offs, 0, -1):
1468879Ssteve.reinhardt@amd.com        if l[i] == elt:
1478879Ssteve.reinhardt@amd.com            return i
1488879Ssteve.reinhardt@amd.com    raise ValueError, "element not found"
1498879Ssteve.reinhardt@amd.com
1508879Ssteve.reinhardt@amd.com# helper function: compare dotted version numbers.
1518879Ssteve.reinhardt@amd.com# E.g., compare_version('1.3.25', '1.4.1')
1528879Ssteve.reinhardt@amd.com# returns -1, 0, 1 if v1 is <, ==, > v2
1538879Ssteve.reinhardt@amd.comdef compare_versions(v1, v2):
1548879Ssteve.reinhardt@amd.com    # Convert dotted strings to lists
1558879Ssteve.reinhardt@amd.com    v1 = map(int, v1.split('.'))
1568879Ssteve.reinhardt@amd.com    v2 = map(int, v2.split('.'))
1578879Ssteve.reinhardt@amd.com    # Compare corresponding elements of lists
1588120Sgblack@eecs.umich.edu    for n1,n2 in zip(v1, v2):
1598120Sgblack@eecs.umich.edu        if n1 < n2: return -1
1608120Sgblack@eecs.umich.edu        if n1 > n2: return  1
1618120Sgblack@eecs.umich.edu    # all corresponding values are equal... see if one has extra values
1628120Sgblack@eecs.umich.edu    if len(v1) < len(v2): return -1
1638120Sgblack@eecs.umich.edu    if len(v1) > len(v2): return  1
1648120Sgblack@eecs.umich.edu    return 0
1658120Sgblack@eecs.umich.edu
1668120Sgblack@eecs.umich.edu# Each target must have 'build' in the interior of the path; the
1678120Sgblack@eecs.umich.edu# directory below this will determine the build parameters.  For
1688120Sgblack@eecs.umich.edu# example, for target 'foo/bar/build/ALPHA_SE/arch/alpha/blah.do' we
1698120Sgblack@eecs.umich.edu# recognize that ALPHA_SE specifies the configuration because it
1708120Sgblack@eecs.umich.edu# follow 'build' in the bulid path.
1718120Sgblack@eecs.umich.edu
1728879Ssteve.reinhardt@amd.com# Generate absolute paths to targets so we can see where the build dir is
1738879Ssteve.reinhardt@amd.comif COMMAND_LINE_TARGETS:
1748879Ssteve.reinhardt@amd.com    # Ask SCons which directory it was invoked from
1758879Ssteve.reinhardt@amd.com    launch_dir = GetLaunchDir()
1768879Ssteve.reinhardt@amd.com    # Make targets relative to invocation directory
1778879Ssteve.reinhardt@amd.com    abs_targets = map(lambda x: os.path.normpath(joinpath(launch_dir, str(x))),
1788879Ssteve.reinhardt@amd.com                      COMMAND_LINE_TARGETS)
1798879Ssteve.reinhardt@amd.comelse:
1809227Sandreas.hansson@arm.com    # Default targets are relative to root of tree
1819227Sandreas.hansson@arm.com    abs_targets = map(lambda x: os.path.normpath(joinpath(ROOT, str(x))),
1828879Ssteve.reinhardt@amd.com                      DEFAULT_TARGETS)
1838879Ssteve.reinhardt@amd.com
1848879Ssteve.reinhardt@amd.com
1858879Ssteve.reinhardt@amd.com# Generate a list of the unique build roots and configs that the
1868120Sgblack@eecs.umich.edu# collected targets reference.
1878947Sandreas.hansson@arm.combuild_paths = []
1887816Ssteve.reinhardt@amd.combuild_root = None
1895871Snate@binkert.orgfor t in abs_targets:
1905871Snate@binkert.org    path_dirs = t.split('/')
1916121Snate@binkert.org    try:
1925871Snate@binkert.org        build_top = rfind(path_dirs, 'build', -2)
1935871Snate@binkert.org    except:
1949926Sstan.czerniawski@arm.com        print "Error: no non-leaf 'build' dir found on target path", t
1959926Sstan.czerniawski@arm.com        Exit(1)
1969119Sandreas.hansson@arm.com    this_build_root = joinpath('/',*path_dirs[:build_top+1])
19710068Sandreas.hansson@arm.com    if not build_root:
19810068Sandreas.hansson@arm.com        build_root = this_build_root
199955SN/A    else:
2009416SAndreas.Sandberg@ARM.com        if this_build_root != build_root:
2019416SAndreas.Sandberg@ARM.com            print "Error: build targets not under same build root\n"\
2029416SAndreas.Sandberg@ARM.com                  "  %s\n  %s" % (build_root, this_build_root)
2039416SAndreas.Sandberg@ARM.com            Exit(1)
2049416SAndreas.Sandberg@ARM.com    build_path = joinpath('/',*path_dirs[:build_top+2])
2059416SAndreas.Sandberg@ARM.com    if build_path not in build_paths:
2069416SAndreas.Sandberg@ARM.com        build_paths.append(build_path)
2075871Snate@binkert.org
2085871Snate@binkert.org# Make sure build_root exists (might not if this is the first build there)
2099416SAndreas.Sandberg@ARM.comif not isdir(build_root):
2109416SAndreas.Sandberg@ARM.com    os.mkdir(build_root)
2115871Snate@binkert.org
212955SN/A###################################################
2136121Snate@binkert.org#
2148881Smarc.orr@gmail.com# Set up the default build environment.  This environment is copied
2156121Snate@binkert.org# and modified according to each selected configuration.
2166121Snate@binkert.org#
2171533SN/A###################################################
2189239Sandreas.hansson@arm.com
2199239Sandreas.hansson@arm.comenv = Environment(ENV = os.environ,  # inherit user's environment vars
2209239Sandreas.hansson@arm.com                  ROOT = ROOT,
2219239Sandreas.hansson@arm.com                  SRCDIR = SRCDIR)
2229239Sandreas.hansson@arm.com
2239239Sandreas.hansson@arm.comExport('env')
2249239Sandreas.hansson@arm.com
2259239Sandreas.hansson@arm.comenv.SConsignFile(joinpath(build_root,"sconsign"))
2269239Sandreas.hansson@arm.com
2279239Sandreas.hansson@arm.com# Default duplicate option is to use hard links, but this messes up
2289239Sandreas.hansson@arm.com# when you use emacs to edit a file in the target dir, as emacs moves
2299239Sandreas.hansson@arm.com# file to file~ then copies to file, breaking the link.  Symbolic
2306655Snate@binkert.org# (soft) links work better.
2316655Snate@binkert.orgenv.SetOption('duplicate', 'soft-copy')
2326655Snate@binkert.org
2336655Snate@binkert.org# I waffle on this setting... it does avoid a few painful but
2345871Snate@binkert.org# unnecessary builds, but it also seems to make trivial builds take
2355871Snate@binkert.org# noticeably longer.
2365863Snate@binkert.orgif False:
2375871Snate@binkert.org    env.TargetSignatures('content')
2388878Ssteve.reinhardt@amd.com
2395871Snate@binkert.org#
2405871Snate@binkert.org# Set up global sticky options... these are common to an entire build
2415871Snate@binkert.org# tree (not specific to a particular build like ALPHA_SE)
2425863Snate@binkert.org#
2436121Snate@binkert.org
2445863Snate@binkert.org# Option validators & converters for global sticky options
2455871Snate@binkert.orgdef PathListMakeAbsolute(val):
2468336Ssteve.reinhardt@amd.com    if not val:
2478336Ssteve.reinhardt@amd.com        return val
2488336Ssteve.reinhardt@amd.com    f = lambda p: os.path.abspath(os.path.expanduser(p))
2498336Ssteve.reinhardt@amd.com    return ':'.join(map(f, val.split(':')))
2504678Snate@binkert.org
2518336Ssteve.reinhardt@amd.comdef PathListAllExist(key, val, env):
2528336Ssteve.reinhardt@amd.com    if not val:
2538336Ssteve.reinhardt@amd.com        return
2544678Snate@binkert.org    paths = val.split(':')
2554678Snate@binkert.org    for path in paths:
2564678Snate@binkert.org        if not isdir(path):
2574678Snate@binkert.org            raise SCons.Errors.UserError("Path does not exist: '%s'" % path)
2587827Snate@binkert.org
2597827Snate@binkert.orgglobal_sticky_opts_file = joinpath(build_root, 'options.global')
2608336Ssteve.reinhardt@amd.com
2614678Snate@binkert.orgglobal_sticky_opts = Options(global_sticky_opts_file, args=ARGUMENTS)
2628336Ssteve.reinhardt@amd.com
2638336Ssteve.reinhardt@amd.comglobal_sticky_opts.AddOptions(
2648336Ssteve.reinhardt@amd.com    ('CC', 'C compiler', os.environ.get('CC', env['CC'])),
2658336Ssteve.reinhardt@amd.com    ('CXX', 'C++ compiler', os.environ.get('CXX', env['CXX'])),
2668336Ssteve.reinhardt@amd.com    ('EXTRAS', 'Add Extra directories to the compilation', '',
2678336Ssteve.reinhardt@amd.com     PathListAllExist, PathListMakeAbsolute)
2685871Snate@binkert.org    )    
2695871Snate@binkert.org
2708336Ssteve.reinhardt@amd.com
2718336Ssteve.reinhardt@amd.com# base help text
2728336Ssteve.reinhardt@amd.comhelp_text = '''
2738336Ssteve.reinhardt@amd.comUsage: scons [scons options] [build options] [target(s)]
2748336Ssteve.reinhardt@amd.com
2755871Snate@binkert.org'''
2768336Ssteve.reinhardt@amd.com
2778336Ssteve.reinhardt@amd.comhelp_text += "Global sticky options:\n" \
2788336Ssteve.reinhardt@amd.com             + global_sticky_opts.GenerateHelpText(env)
2798336Ssteve.reinhardt@amd.com
2808336Ssteve.reinhardt@amd.com# Update env with values from ARGUMENTS & file global_sticky_opts_file
2814678Snate@binkert.orgglobal_sticky_opts.Update(env)
2825871Snate@binkert.org
2834678Snate@binkert.org# Save sticky option settings back to current options file
2848336Ssteve.reinhardt@amd.comglobal_sticky_opts.Save(global_sticky_opts_file, env)
2858336Ssteve.reinhardt@amd.com
2868336Ssteve.reinhardt@amd.com# Parse EXTRAS option to build list of all directories where we're
2878336Ssteve.reinhardt@amd.com# look for sources etc.  This list is exported as base_dir_list.
2888336Ssteve.reinhardt@amd.combase_dir_list = [joinpath(ROOT, 'src')]
2898336Ssteve.reinhardt@amd.comif env['EXTRAS']:
2908336Ssteve.reinhardt@amd.com    base_dir_list += env['EXTRAS'].split(':')
2918336Ssteve.reinhardt@amd.com
2928336Ssteve.reinhardt@amd.comExport('base_dir_list')
2938336Ssteve.reinhardt@amd.com
2948336Ssteve.reinhardt@amd.com# M5_PLY is used by isa_parser.py to find the PLY package.
2958336Ssteve.reinhardt@amd.comenv.Append(ENV = { 'M5_PLY' : str(Dir('ext/ply')) })
2968336Ssteve.reinhardt@amd.comenv['GCC'] = False
2978336Ssteve.reinhardt@amd.comenv['SUNCC'] = False
2988336Ssteve.reinhardt@amd.comenv['ICC'] = False
2998336Ssteve.reinhardt@amd.comenv['GCC'] = subprocess.Popen(env['CXX'] + ' --version', shell=True,
3008336Ssteve.reinhardt@amd.com        stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
3015871Snate@binkert.org        close_fds=True).communicate()[0].find('GCC') >= 0
3026121Snate@binkert.orgenv['SUNCC'] = subprocess.Popen(env['CXX'] + ' -V', shell=True,
303955SN/A        stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
304955SN/A        close_fds=True).communicate()[0].find('Sun C++') >= 0
3052632Sstever@eecs.umich.eduenv['ICC'] = subprocess.Popen(env['CXX'] + ' -V', shell=True,
3062632Sstever@eecs.umich.edu        stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
307955SN/A        close_fds=True).communicate()[0].find('Intel') >= 0
308955SN/Aif env['GCC'] + env['SUNCC'] + env['ICC'] > 1:
309955SN/A    print 'Error: How can we have two at the same time?'
310955SN/A    Exit(1)
3118878Ssteve.reinhardt@amd.com
312955SN/A
3132632Sstever@eecs.umich.edu# Set up default C++ compiler flags
3142632Sstever@eecs.umich.eduif env['GCC']:
3152632Sstever@eecs.umich.edu    env.Append(CCFLAGS='-pipe')
3162632Sstever@eecs.umich.edu    env.Append(CCFLAGS='-fno-strict-aliasing')
3172632Sstever@eecs.umich.edu    env.Append(CCFLAGS=Split('-Wall -Wno-sign-compare -Werror -Wundef'))
3182632Sstever@eecs.umich.eduelif env['ICC']:
3192632Sstever@eecs.umich.edu    pass #Fix me... add warning flags once we clean up icc warnings
3208268Ssteve.reinhardt@amd.comelif env['SUNCC']:
3218268Ssteve.reinhardt@amd.com    env.Append(CCFLAGS='-Qoption ccfe')
3228268Ssteve.reinhardt@amd.com    env.Append(CCFLAGS='-features=gcc')
3238268Ssteve.reinhardt@amd.com    env.Append(CCFLAGS='-features=extensions')
3248268Ssteve.reinhardt@amd.com    env.Append(CCFLAGS='-library=stlport4')
3258268Ssteve.reinhardt@amd.com    env.Append(CCFLAGS='-xar')
3268268Ssteve.reinhardt@amd.com#    env.Append(CCFLAGS='-instances=semiexplicit')
3272632Sstever@eecs.umich.eduelse:
3282632Sstever@eecs.umich.edu    print 'Error: Don\'t know what compiler options to use for your compiler.'
3292632Sstever@eecs.umich.edu    print '       Please fix SConstruct and src/SConscript and try again.'
3302632Sstever@eecs.umich.edu    Exit(1)
3318268Ssteve.reinhardt@amd.com
3322632Sstever@eecs.umich.eduif sys.platform == 'cygwin':
3338268Ssteve.reinhardt@amd.com    # cygwin has some header file issues...
3348268Ssteve.reinhardt@amd.com    env.Append(CCFLAGS=Split("-Wno-uninitialized"))
3358268Ssteve.reinhardt@amd.comenv.Append(CPPPATH=[Dir('ext/dnet')])
3368268Ssteve.reinhardt@amd.com
3373718Sstever@eecs.umich.edu# Check for SWIG
3382634Sstever@eecs.umich.eduif not env.has_key('SWIG'):
3392634Sstever@eecs.umich.edu    print 'Error: SWIG utility not found.'
3405863Snate@binkert.org    print '       Please install (see http://www.swig.org) and retry.'
3412638Sstever@eecs.umich.edu    Exit(1)
3428268Ssteve.reinhardt@amd.com
3432632Sstever@eecs.umich.edu# Check for appropriate SWIG version
3442632Sstever@eecs.umich.eduswig_version = os.popen('swig -version').read().split()
3452632Sstever@eecs.umich.edu# First 3 words should be "SWIG Version x.y.z"
3462632Sstever@eecs.umich.eduif len(swig_version) < 3 or \
3472632Sstever@eecs.umich.edu        swig_version[0] != 'SWIG' or swig_version[1] != 'Version':
3481858SN/A    print 'Error determining SWIG version.'
3493716Sstever@eecs.umich.edu    Exit(1)
3502638Sstever@eecs.umich.edu
3512638Sstever@eecs.umich.edumin_swig_version = '1.3.28'
3522638Sstever@eecs.umich.eduif compare_versions(swig_version[2], min_swig_version) < 0:
3532638Sstever@eecs.umich.edu    print 'Error: SWIG version', min_swig_version, 'or newer required.'
3542638Sstever@eecs.umich.edu    print '       Installed version:', swig_version[2]
3552638Sstever@eecs.umich.edu    Exit(1)
3562638Sstever@eecs.umich.edu
3575863Snate@binkert.org# Set up SWIG flags & scanner
3585863Snate@binkert.orgswig_flags=Split('-c++ -python -modern -templatereduce $_CPPINCFLAGS')
3595863Snate@binkert.orgenv.Append(SWIGFLAGS=swig_flags)
360955SN/A
3615341Sstever@gmail.com# filter out all existing swig scanners, they mess up the dependency
3625341Sstever@gmail.com# stuff for some reason
3635863Snate@binkert.orgscanners = []
3647756SAli.Saidi@ARM.comfor scanner in env['SCANNERS']:
3655341Sstever@gmail.com    skeys = scanner.skeys
3666121Snate@binkert.org    if skeys == '.i':
3674494Ssaidi@eecs.umich.edu        continue
3686121Snate@binkert.org
3691105SN/A    if isinstance(skeys, (list, tuple)) and '.i' in skeys:
3702667Sstever@eecs.umich.edu        continue
3712667Sstever@eecs.umich.edu
3722667Sstever@eecs.umich.edu    scanners.append(scanner)
3732667Sstever@eecs.umich.edu
3746121Snate@binkert.org# add the new swig scanner that we like better
3752667Sstever@eecs.umich.edufrom SCons.Scanner import ClassicCPP as CPPScanner
3765341Sstever@gmail.comswig_inc_re = '^[ \t]*[%,#][ \t]*(?:include|import)[ \t]*(<|")([^>"]+)(>|")'
3775863Snate@binkert.orgscanners.append(CPPScanner("SwigScan", [ ".i" ], "CPPPATH", swig_inc_re))
3785341Sstever@gmail.com
3795341Sstever@gmail.com# replace the scanners list that has what we want
3805341Sstever@gmail.comenv['SCANNERS'] = scanners
3818120Sgblack@eecs.umich.edu
3825341Sstever@gmail.com# Platform-specific configuration.  Note again that we assume that all
3838120Sgblack@eecs.umich.edu# builds under a given build root run on the same host platform.
3845341Sstever@gmail.comconf = Configure(env,
3858120Sgblack@eecs.umich.edu                 conf_dir = joinpath(build_root, '.scons_config'),
3866121Snate@binkert.org                 log_file = joinpath(build_root, 'scons_config.log'))
3876121Snate@binkert.org
3888980Ssteve.reinhardt@amd.com# Check if we should compile a 64 bit binary on Mac OS X/Darwin
3899396Sandreas.hansson@arm.comtry:
3905397Ssaidi@eecs.umich.edu    import platform
3915397Ssaidi@eecs.umich.edu    uname = platform.uname()
3927727SAli.Saidi@ARM.com    if uname[0] == 'Darwin' and compare_versions(uname[2], '9.0.0') >= 0:
3938268Ssteve.reinhardt@amd.com        if int(subprocess.Popen('sysctl -n hw.cpu64bit_capable', shell=True,
3946168Snate@binkert.org               stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
3955341Sstever@gmail.com               close_fds=True).communicate()[0][0]):
3968120Sgblack@eecs.umich.edu            env.Append(CCFLAGS='-arch x86_64')
3978120Sgblack@eecs.umich.edu            env.Append(CFLAGS='-arch x86_64')
3988120Sgblack@eecs.umich.edu            env.Append(LINKFLAGS='-arch x86_64')
3996814Sgblack@eecs.umich.edu            env.Append(ASFLAGS='-arch x86_64')
4005863Snate@binkert.org            env['OSX64bit'] = True
4018120Sgblack@eecs.umich.eduexcept:
4025341Sstever@gmail.com    pass
4035863Snate@binkert.org
4048268Ssteve.reinhardt@amd.com# Recent versions of scons substitute a "Null" object for Configure()
4056121Snate@binkert.org# when configuration isn't necessary, e.g., if the "--help" option is
4066121Snate@binkert.org# present.  Unfortuantely this Null object always returns false,
4078268Ssteve.reinhardt@amd.com# breaking all our configuration checks.  We replace it with our own
4085742Snate@binkert.org# more optimistic null object that returns True instead.
4095742Snate@binkert.orgif not conf:
4105341Sstever@gmail.com    def NullCheck(*args, **kwargs):
4115742Snate@binkert.org        return True
4125742Snate@binkert.org
4135341Sstever@gmail.com    class NullConf:
4146017Snate@binkert.org        def __init__(self, env):
4156121Snate@binkert.org            self.env = env
4166017Snate@binkert.org        def Finish(self):
4177816Ssteve.reinhardt@amd.com            return self.env
4187756SAli.Saidi@ARM.com        def __getattr__(self, mname):
4197756SAli.Saidi@ARM.com            return NullCheck
4207756SAli.Saidi@ARM.com
4217756SAli.Saidi@ARM.com    conf = NullConf(env)
4227756SAli.Saidi@ARM.com
4237756SAli.Saidi@ARM.com# Find Python include and library directories for embedding the
4247756SAli.Saidi@ARM.com# interpreter.  For consistency, we will use the same Python
4257756SAli.Saidi@ARM.com# installation used to run scons (and thus this script).  If you want
4267816Ssteve.reinhardt@amd.com# to link in an alternate version, see above for instructions on how
4277816Ssteve.reinhardt@amd.com# to invoke scons with a different copy of the Python interpreter.
4287816Ssteve.reinhardt@amd.com
4297816Ssteve.reinhardt@amd.com# Get brief Python version name (e.g., "python2.4") for locating
4307816Ssteve.reinhardt@amd.com# include & library files
4317816Ssteve.reinhardt@amd.compy_version_name = 'python' + sys.version[:3]
4327816Ssteve.reinhardt@amd.com
4337816Ssteve.reinhardt@amd.com# include path, e.g. /usr/local/include/python2.4
4347816Ssteve.reinhardt@amd.compy_header_path = joinpath(sys.exec_prefix, 'include', py_version_name)
4357816Ssteve.reinhardt@amd.comenv.Append(CPPPATH = py_header_path)
4367756SAli.Saidi@ARM.com# verify that it works
4377816Ssteve.reinhardt@amd.comif not conf.CheckHeader('Python.h', '<>'):
4387816Ssteve.reinhardt@amd.com    print "Error: can't find Python.h header in", py_header_path
4397816Ssteve.reinhardt@amd.com    Exit(1)
4407816Ssteve.reinhardt@amd.com
4417816Ssteve.reinhardt@amd.com# add library path too if it's not in the default place
4427816Ssteve.reinhardt@amd.compy_lib_path = None
4437816Ssteve.reinhardt@amd.comif sys.exec_prefix != '/usr':
4447816Ssteve.reinhardt@amd.com    py_lib_path = joinpath(sys.exec_prefix, 'lib')
4457816Ssteve.reinhardt@amd.comelif sys.platform == 'cygwin':
4467816Ssteve.reinhardt@amd.com    # cygwin puts the .dll in /bin for some reason
4477816Ssteve.reinhardt@amd.com    py_lib_path = '/bin'
4487816Ssteve.reinhardt@amd.comif py_lib_path:
4497816Ssteve.reinhardt@amd.com    env.Append(LIBPATH = py_lib_path)
4507816Ssteve.reinhardt@amd.com    print 'Adding', py_lib_path, 'to LIBPATH for', py_version_name
4517816Ssteve.reinhardt@amd.comif not conf.CheckLib(py_version_name):
4527816Ssteve.reinhardt@amd.com    print "Error: can't find Python library", py_version_name
4537816Ssteve.reinhardt@amd.com    Exit(1)
4547816Ssteve.reinhardt@amd.com
4557816Ssteve.reinhardt@amd.com# On Solaris you need to use libsocket for socket ops
4567816Ssteve.reinhardt@amd.comif not conf.CheckLibWithHeader(None, 'sys/socket.h', 'C++', 'accept(0,0,0);'):
4577816Ssteve.reinhardt@amd.com   if not conf.CheckLibWithHeader('socket', 'sys/socket.h', 'C++', 'accept(0,0,0);'):
4587816Ssteve.reinhardt@amd.com       print "Can't find library with socket calls (e.g. accept())"
4597816Ssteve.reinhardt@amd.com       Exit(1)
4607816Ssteve.reinhardt@amd.com
4617816Ssteve.reinhardt@amd.com# Check for zlib.  If the check passes, libz will be automatically
4627816Ssteve.reinhardt@amd.com# added to the LIBS environment variable.
4637816Ssteve.reinhardt@amd.comif not conf.CheckLibWithHeader('z', 'zlib.h', 'C++','zlibVersion();'):
4647816Ssteve.reinhardt@amd.com    print 'Error: did not find needed zlib compression library '\
4657816Ssteve.reinhardt@amd.com          'and/or zlib.h header file.'
4667816Ssteve.reinhardt@amd.com    print '       Please install zlib and try again.'
4677816Ssteve.reinhardt@amd.com    Exit(1)
4687816Ssteve.reinhardt@amd.com
4697816Ssteve.reinhardt@amd.com# Check for <fenv.h> (C99 FP environment control)
4707816Ssteve.reinhardt@amd.comhave_fenv = conf.CheckHeader('fenv.h', '<>')
4717816Ssteve.reinhardt@amd.comif not have_fenv:
4727816Ssteve.reinhardt@amd.com    print "Warning: Header file <fenv.h> not found."
4737816Ssteve.reinhardt@amd.com    print "         This host has no IEEE FP rounding mode control."
4747816Ssteve.reinhardt@amd.com
4757816Ssteve.reinhardt@amd.com# Check for mysql.
4767816Ssteve.reinhardt@amd.commysql_config = WhereIs('mysql_config')
4777816Ssteve.reinhardt@amd.comhave_mysql = mysql_config != None
4787816Ssteve.reinhardt@amd.com
4797816Ssteve.reinhardt@amd.com# Check MySQL version.
4807816Ssteve.reinhardt@amd.comif have_mysql:
4817816Ssteve.reinhardt@amd.com    mysql_version = os.popen(mysql_config + ' --version').read()
4827816Ssteve.reinhardt@amd.com    min_mysql_version = '4.1'
4837816Ssteve.reinhardt@amd.com    if compare_versions(mysql_version, min_mysql_version) < 0:
4847816Ssteve.reinhardt@amd.com        print 'Warning: MySQL', min_mysql_version, 'or newer required.'
4857816Ssteve.reinhardt@amd.com        print '         Version', mysql_version, 'detected.'
4867816Ssteve.reinhardt@amd.com        have_mysql = False
4877816Ssteve.reinhardt@amd.com
4887816Ssteve.reinhardt@amd.com# Set up mysql_config commands.
4897816Ssteve.reinhardt@amd.comif have_mysql:
4907816Ssteve.reinhardt@amd.com    mysql_config_include = mysql_config + ' --include'
4917816Ssteve.reinhardt@amd.com    if os.system(mysql_config_include + ' > /dev/null') != 0:
4927816Ssteve.reinhardt@amd.com        # older mysql_config versions don't support --include, use
4937816Ssteve.reinhardt@amd.com        # --cflags instead
4947816Ssteve.reinhardt@amd.com        mysql_config_include = mysql_config + ' --cflags | sed s/\\\'//g'
4957816Ssteve.reinhardt@amd.com    # This seems to work in all versions
4967816Ssteve.reinhardt@amd.com    mysql_config_libs = mysql_config + ' --libs'
4977816Ssteve.reinhardt@amd.com
4988947Sandreas.hansson@arm.comenv = conf.Finish()
4998947Sandreas.hansson@arm.com
5007756SAli.Saidi@ARM.com# Define the universe of supported ISAs
5018120Sgblack@eecs.umich.eduall_isa_list = [ ]
5027756SAli.Saidi@ARM.comExport('all_isa_list')
5037756SAli.Saidi@ARM.com
5047756SAli.Saidi@ARM.com# Define the universe of supported CPU models
5057756SAli.Saidi@ARM.comall_cpu_list = [ ]
5067816Ssteve.reinhardt@amd.comdefault_cpus = [ ]
5077816Ssteve.reinhardt@amd.comExport('all_cpu_list', 'default_cpus')
5087816Ssteve.reinhardt@amd.com
5097816Ssteve.reinhardt@amd.com# Sticky options get saved in the options file so they persist from
5107816Ssteve.reinhardt@amd.com# one invocation to the next (unless overridden, in which case the new
5117816Ssteve.reinhardt@amd.com# value becomes sticky).
5127816Ssteve.reinhardt@amd.comsticky_opts = Options(args=ARGUMENTS)
5137816Ssteve.reinhardt@amd.comExport('sticky_opts')
5147816Ssteve.reinhardt@amd.com
5157816Ssteve.reinhardt@amd.com# Non-sticky options only apply to the current build.
5167756SAli.Saidi@ARM.comnonsticky_opts = Options(args=ARGUMENTS)
5177756SAli.Saidi@ARM.comExport('nonsticky_opts')
5189227Sandreas.hansson@arm.com
5199227Sandreas.hansson@arm.com# Walk the tree and execute all SConsopts scripts that wil add to the
5209227Sandreas.hansson@arm.com# above options
5219227Sandreas.hansson@arm.comfor base_dir in base_dir_list:
5229590Sandreas@sandberg.pp.se    for root, dirs, files in os.walk(base_dir):
5239590Sandreas@sandberg.pp.se        if 'SConsopts' in files:
5249590Sandreas@sandberg.pp.se            print "Reading", joinpath(root, 'SConsopts')
5259590Sandreas@sandberg.pp.se            SConscript(joinpath(root, 'SConsopts'))
5269590Sandreas@sandberg.pp.se
5279590Sandreas@sandberg.pp.seall_isa_list.sort()
5286654Snate@binkert.orgall_cpu_list.sort()
5296654Snate@binkert.orgdefault_cpus.sort()
5305871Snate@binkert.org
5316121Snate@binkert.orgsticky_opts.AddOptions(
5328946Sandreas.hansson@arm.com    EnumOption('TARGET_ISA', 'Target ISA', 'alpha', all_isa_list),
5339419Sandreas.hansson@arm.com    BoolOption('FULL_SYSTEM', 'Full-system support', False),
5343940Ssaidi@eecs.umich.edu    # There's a bug in scons 0.96.1 that causes ListOptions with list
5353918Ssaidi@eecs.umich.edu    # values (more than one value) not to be able to be restored from
5363918Ssaidi@eecs.umich.edu    # a saved option file.  If this causes trouble then upgrade to
5371858SN/A    # scons 0.96.90 or later.
5389556Sandreas.hansson@arm.com    ListOption('CPU_MODELS', 'CPU models', default_cpus, all_cpu_list),
5399556Sandreas.hansson@arm.com    BoolOption('NO_FAST_ALLOC', 'Disable fast object allocator', False),
5409556Sandreas.hansson@arm.com    BoolOption('FAST_ALLOC_DEBUG', 'Enable fast object allocator debugging',
5419556Sandreas.hansson@arm.com               False),
5429556Sandreas.hansson@arm.com    BoolOption('FAST_ALLOC_STATS', 'Enable fast object allocator statistics',
5439556Sandreas.hansson@arm.com               False),
5449556Sandreas.hansson@arm.com    BoolOption('EFENCE', 'Link with Electric Fence malloc debugger',
5459556Sandreas.hansson@arm.com               False),
5469556Sandreas.hansson@arm.com    BoolOption('SS_COMPATIBLE_FP',
5479556Sandreas.hansson@arm.com               'Make floating-point results compatible with SimpleScalar',
5489556Sandreas.hansson@arm.com               False),
5499556Sandreas.hansson@arm.com    BoolOption('USE_SSE2',
5509556Sandreas.hansson@arm.com               'Compile for SSE2 (-msse2) to get IEEE FP on x86 hosts',
5519556Sandreas.hansson@arm.com               False),
5529556Sandreas.hansson@arm.com    BoolOption('USE_MYSQL', 'Use MySQL for stats output', have_mysql),
5539556Sandreas.hansson@arm.com    BoolOption('USE_FENV', 'Use <fenv.h> IEEE mode control', have_fenv),
5549556Sandreas.hansson@arm.com    BoolOption('USE_CHECKER', 'Use checker for detailed CPU models', False),
5559556Sandreas.hansson@arm.com    BoolOption('BATCH', 'Use batch pool for build and tests', False),
5569556Sandreas.hansson@arm.com    ('BATCH_CMD', 'Batch pool submission command name', 'qdo'),
5579556Sandreas.hansson@arm.com    ('PYTHONHOME',
5589556Sandreas.hansson@arm.com     'Override the default PYTHONHOME for this system (use with caution)',
5599556Sandreas.hansson@arm.com     '%s:%s' % (sys.prefix, sys.exec_prefix)),
5609556Sandreas.hansson@arm.com    )
5619556Sandreas.hansson@arm.com
5629556Sandreas.hansson@arm.comnonsticky_opts.AddOptions(
5639556Sandreas.hansson@arm.com    BoolOption('update_ref', 'Update test reference outputs', False)
5649556Sandreas.hansson@arm.com    )
5659556Sandreas.hansson@arm.com
5669556Sandreas.hansson@arm.com# These options get exported to #defines in config/*.hh (see src/SConscript).
5679556Sandreas.hansson@arm.comenv.ExportOptions = ['FULL_SYSTEM', 'ALPHA_TLASER', 'USE_FENV', \
5689556Sandreas.hansson@arm.com                     'USE_MYSQL', 'NO_FAST_ALLOC', 'FAST_ALLOC_DEBUG', \
5699556Sandreas.hansson@arm.com                     'FAST_ALLOC_STATS', 'SS_COMPATIBLE_FP', \
5706121Snate@binkert.org                     'USE_CHECKER', 'PYTHONHOME', 'TARGET_ISA']
57110238Sandreas.hansson@arm.com
57210238Sandreas.hansson@arm.com# Define a handy 'no-op' action
57310238Sandreas.hansson@arm.comdef no_action(target, source, env):
57410238Sandreas.hansson@arm.com    return 0
5759420Sandreas.hansson@arm.com
57610238Sandreas.hansson@arm.comenv.NoAction = Action(no_action, None)
57710238Sandreas.hansson@arm.com
5789420Sandreas.hansson@arm.com###################################################
5799420Sandreas.hansson@arm.com#
5809420Sandreas.hansson@arm.com# Define a SCons builder for configuration flag headers.
5819420Sandreas.hansson@arm.com#
5829420Sandreas.hansson@arm.com###################################################
58310264Sandreas.hansson@arm.com
58410264Sandreas.hansson@arm.com# This function generates a config header file that #defines the
58510264Sandreas.hansson@arm.com# option symbol to the current option setting (0 or 1).  The source
58610264Sandreas.hansson@arm.com# operands are the name of the option and a Value node containing the
58710264Sandreas.hansson@arm.com# value of the option.
58810264Sandreas.hansson@arm.comdef build_config_file(target, source, env):
58910264Sandreas.hansson@arm.com    (option, value) = [s.get_contents() for s in source]
59010264Sandreas.hansson@arm.com    f = file(str(target[0]), 'w')
59110264Sandreas.hansson@arm.com    print >> f, '#define', option, value
59210264Sandreas.hansson@arm.com    f.close()
59310264Sandreas.hansson@arm.com    return None
59410264Sandreas.hansson@arm.com
59510264Sandreas.hansson@arm.com# Generate the message to be printed when building the config file.
59610264Sandreas.hansson@arm.comdef build_config_file_string(target, source, env):
59710264Sandreas.hansson@arm.com    (option, value) = [s.get_contents() for s in source]
59810264Sandreas.hansson@arm.com    return "Defining %s as %s in %s." % (option, value, target[0])
59910238Sandreas.hansson@arm.com
60010238Sandreas.hansson@arm.com# Combine the two functions into a scons Action object.
60110238Sandreas.hansson@arm.comconfig_action = Action(build_config_file, build_config_file_string)
60210238Sandreas.hansson@arm.com
60310238Sandreas.hansson@arm.com# The emitter munges the source & target node lists to reflect what
60410238Sandreas.hansson@arm.com# we're really doing.
60510416Sandreas.hansson@arm.comdef config_emitter(target, source, env):
60610238Sandreas.hansson@arm.com    # extract option name from Builder arg
6079227Sandreas.hansson@arm.com    option = str(target[0])
60810238Sandreas.hansson@arm.com    # True target is config header file
60910416Sandreas.hansson@arm.com    target = joinpath('config', option.lower() + '.hh')
61010416Sandreas.hansson@arm.com    val = env[option]
6119227Sandreas.hansson@arm.com    if isinstance(val, bool):
6129590Sandreas@sandberg.pp.se        # Force value to 0/1
6139590Sandreas@sandberg.pp.se        val = int(val)
6149590Sandreas@sandberg.pp.se    elif isinstance(val, str):
6158737Skoansin.tan@gmail.com        val = '"' + val + '"'
61610238Sandreas.hansson@arm.com
61710238Sandreas.hansson@arm.com    # Sources are option name & value (packaged in SCons Value nodes)
6189420Sandreas.hansson@arm.com    return ([target], [Value(option), Value(val)])
6198737Skoansin.tan@gmail.com
62010106SMitch.Hayenga@arm.comconfig_builder = Builder(emitter = config_emitter, action = config_action)
6218737Skoansin.tan@gmail.com
6228737Skoansin.tan@gmail.comenv.Append(BUILDERS = { 'ConfigFile' : config_builder })
62310238Sandreas.hansson@arm.com
62410238Sandreas.hansson@arm.com###################################################
6258737Skoansin.tan@gmail.com#
6268737Skoansin.tan@gmail.com# Define a SCons builder for copying files.  This is used by the
6278737Skoansin.tan@gmail.com# Python zipfile code in src/python/SConscript, but is placed up here
6288737Skoansin.tan@gmail.com# since it's potentially more generally applicable.
6298737Skoansin.tan@gmail.com#
6308737Skoansin.tan@gmail.com###################################################
6319556Sandreas.hansson@arm.com
6329556Sandreas.hansson@arm.comcopy_builder = Builder(action = Copy("$TARGET", "$SOURCE"))
6339556Sandreas.hansson@arm.com
6349556Sandreas.hansson@arm.comenv.Append(BUILDERS = { 'CopyFile' : copy_builder })
6359556Sandreas.hansson@arm.com
6369556Sandreas.hansson@arm.com###################################################
6379556Sandreas.hansson@arm.com#
6389556Sandreas.hansson@arm.com# Define a simple SCons builder to concatenate files.
63910278SAndreas.Sandberg@ARM.com#
64010278SAndreas.Sandberg@ARM.com# Used to append the Python zip archive to the executable.
64110278SAndreas.Sandberg@ARM.com#
64210278SAndreas.Sandberg@ARM.com###################################################
64310278SAndreas.Sandberg@ARM.com
64410278SAndreas.Sandberg@ARM.comconcat_builder = Builder(action = Action(['cat $SOURCES > $TARGET',
6459556Sandreas.hansson@arm.com                                          'chmod +x $TARGET']))
6469590Sandreas@sandberg.pp.se
6479590Sandreas@sandberg.pp.seenv.Append(BUILDERS = { 'Concat' : concat_builder })
6489420Sandreas.hansson@arm.com
6499846Sandreas.hansson@arm.com
6509846Sandreas.hansson@arm.com# libelf build is shared across all configs in the build root.
6519846Sandreas.hansson@arm.comenv.SConscript('ext/libelf/SConscript',
6529846Sandreas.hansson@arm.com               build_dir = joinpath(build_root, 'libelf'),
6538946Sandreas.hansson@arm.com               exports = 'env')
6543918Ssaidi@eecs.umich.edu
6559068SAli.Saidi@ARM.com###################################################
6569068SAli.Saidi@ARM.com#
6579068SAli.Saidi@ARM.com# This function is used to set up a directory with switching headers
6589068SAli.Saidi@ARM.com#
6599068SAli.Saidi@ARM.com###################################################
6609068SAli.Saidi@ARM.com
6619068SAli.Saidi@ARM.comenv['ALL_ISA_LIST'] = all_isa_list
6629068SAli.Saidi@ARM.comdef make_switching_dir(dirname, switch_headers, env):
6639068SAli.Saidi@ARM.com    # Generate the header.  target[0] is the full path of the output
6649419Sandreas.hansson@arm.com    # header to generate.  'source' is a dummy variable, since we get the
6659068SAli.Saidi@ARM.com    # list of ISAs from env['ALL_ISA_LIST'].
6669068SAli.Saidi@ARM.com    def gen_switch_hdr(target, source, env):
6679068SAli.Saidi@ARM.com        fname = str(target[0])
6689068SAli.Saidi@ARM.com        basename = os.path.basename(fname)
6699068SAli.Saidi@ARM.com        f = open(fname, 'w')
6709068SAli.Saidi@ARM.com        f.write('#include "arch/isa_specific.hh"\n')
6713918Ssaidi@eecs.umich.edu        cond = '#if'
6723918Ssaidi@eecs.umich.edu        for isa in all_isa_list:
6736157Snate@binkert.org            f.write('%s THE_ISA == %s_ISA\n#include "%s/%s/%s"\n'
6746157Snate@binkert.org                    % (cond, isa.upper(), dirname, isa, basename))
6756157Snate@binkert.org            cond = '#elif'
6766157Snate@binkert.org        f.write('#else\n#error "THE_ISA not set"\n#endif\n')
6775397Ssaidi@eecs.umich.edu        f.close()
6785397Ssaidi@eecs.umich.edu        return 0
6796121Snate@binkert.org
6806121Snate@binkert.org    # String to print when generating header
6816121Snate@binkert.org    def gen_switch_hdr_string(target, source, env):
6826121Snate@binkert.org        return "Generating switch header " + str(target[0])
6836121Snate@binkert.org
6846121Snate@binkert.org    # Build SCons Action object. 'varlist' specifies env vars that this
6855397Ssaidi@eecs.umich.edu    # action depends on; when env['ALL_ISA_LIST'] changes these actions
6861851SN/A    # should get re-executed.
6871851SN/A    switch_hdr_action = Action(gen_switch_hdr, gen_switch_hdr_string,
6887739Sgblack@eecs.umich.edu                               varlist=['ALL_ISA_LIST'])
689955SN/A
6909396Sandreas.hansson@arm.com    # Instantiate actions for each header
6919396Sandreas.hansson@arm.com    for hdr in switch_headers:
6929396Sandreas.hansson@arm.com        env.Command(hdr, [], switch_hdr_action)
6939396Sandreas.hansson@arm.comExport('make_switching_dir')
6949396Sandreas.hansson@arm.com
6959396Sandreas.hansson@arm.com###################################################
6969396Sandreas.hansson@arm.com#
6979396Sandreas.hansson@arm.com# Define build environments for selected configurations.
6989396Sandreas.hansson@arm.com#
6999396Sandreas.hansson@arm.com###################################################
7009396Sandreas.hansson@arm.com
7019396Sandreas.hansson@arm.com# rename base env
7029396Sandreas.hansson@arm.combase_env = env
7039396Sandreas.hansson@arm.com
7049396Sandreas.hansson@arm.comfor build_path in build_paths:
7059396Sandreas.hansson@arm.com    print "Building in", build_path
7069477Sandreas.hansson@arm.com
7079477Sandreas.hansson@arm.com    # Make a copy of the build-root environment to use for this config.
7089477Sandreas.hansson@arm.com    env = base_env.Copy()
7099477Sandreas.hansson@arm.com    env['BUILDDIR'] = build_path
7109477Sandreas.hansson@arm.com
7119477Sandreas.hansson@arm.com    # build_dir is the tail component of build path, and is used to
7129477Sandreas.hansson@arm.com    # determine the build parameters (e.g., 'ALPHA_SE')
7139477Sandreas.hansson@arm.com    (build_root, build_dir) = os.path.split(build_path)
7149477Sandreas.hansson@arm.com
7159477Sandreas.hansson@arm.com    # Set env options according to the build directory config.
7169477Sandreas.hansson@arm.com    sticky_opts.files = []
7179477Sandreas.hansson@arm.com    # Options for $BUILD_ROOT/$BUILD_DIR are stored in
7189477Sandreas.hansson@arm.com    # $BUILD_ROOT/options/$BUILD_DIR so you can nuke
7199477Sandreas.hansson@arm.com    # $BUILD_ROOT/$BUILD_DIR without losing your options settings.
7209477Sandreas.hansson@arm.com    current_opts_file = joinpath(build_root, 'options', build_dir)
7219477Sandreas.hansson@arm.com    if isfile(current_opts_file):
7229477Sandreas.hansson@arm.com        sticky_opts.files.append(current_opts_file)
7239477Sandreas.hansson@arm.com        print "Using saved options file %s" % current_opts_file
7249477Sandreas.hansson@arm.com    else:
7259477Sandreas.hansson@arm.com        # Build dir-specific options file doesn't exist.
7269477Sandreas.hansson@arm.com
7279477Sandreas.hansson@arm.com        # Make sure the directory is there so we can create it later
7289396Sandreas.hansson@arm.com        opt_dir = os.path.dirname(current_opts_file)
7293053Sstever@eecs.umich.edu        if not isdir(opt_dir):
7306121Snate@binkert.org            os.mkdir(opt_dir)
7313053Sstever@eecs.umich.edu
7323053Sstever@eecs.umich.edu        # Get default build options from source tree.  Options are
7333053Sstever@eecs.umich.edu        # normally determined by name of $BUILD_DIR, but can be
7343053Sstever@eecs.umich.edu        # overriden by 'default=' arg on command line.
7353053Sstever@eecs.umich.edu        default_opts_file = joinpath('build_opts',
7369072Sandreas.hansson@arm.com                                     ARGUMENTS.get('default', build_dir))
7373053Sstever@eecs.umich.edu        if isfile(default_opts_file):
7384742Sstever@eecs.umich.edu            sticky_opts.files.append(default_opts_file)
7394742Sstever@eecs.umich.edu            print "Options file %s not found,\n  using defaults in %s" \
7403053Sstever@eecs.umich.edu                  % (current_opts_file, default_opts_file)
7413053Sstever@eecs.umich.edu        else:
7423053Sstever@eecs.umich.edu            print "Error: cannot find options file %s or %s" \
74310181SCurtis.Dunham@arm.com                  % (current_opts_file, default_opts_file)
7446654Snate@binkert.org            Exit(1)
7453053Sstever@eecs.umich.edu
7463053Sstever@eecs.umich.edu    # Apply current option settings to env
7473053Sstever@eecs.umich.edu    sticky_opts.Update(env)
7483053Sstever@eecs.umich.edu    nonsticky_opts.Update(env)
74910425Sandreas.hansson@arm.com
75010425Sandreas.hansson@arm.com    help_text += "\nSticky options for %s:\n" % build_dir \
75110425Sandreas.hansson@arm.com                 + sticky_opts.GenerateHelpText(env) \
75210425Sandreas.hansson@arm.com                 + "\nNon-sticky options for %s:\n" % build_dir \
75310425Sandreas.hansson@arm.com                 + nonsticky_opts.GenerateHelpText(env)
75410425Sandreas.hansson@arm.com
75510425Sandreas.hansson@arm.com    # Process option settings.
75610425Sandreas.hansson@arm.com
75710425Sandreas.hansson@arm.com    if not have_fenv and env['USE_FENV']:
75810425Sandreas.hansson@arm.com        print "Warning: <fenv.h> not available; " \
75910425Sandreas.hansson@arm.com              "forcing USE_FENV to False in", build_dir + "."
7602667Sstever@eecs.umich.edu        env['USE_FENV'] = False
7614554Sbinkertn@umich.edu
7626121Snate@binkert.org    if not env['USE_FENV']:
7632667Sstever@eecs.umich.edu        print "Warning: No IEEE FP rounding mode control in", build_dir + "."
76410384SCurtis.Dunham@arm.com        print "         FP results may deviate slightly from other platforms."
76510384SCurtis.Dunham@arm.com
76610384SCurtis.Dunham@arm.com    if env['EFENCE']:
76710384SCurtis.Dunham@arm.com        env.Append(LIBS=['efence'])
76810384SCurtis.Dunham@arm.com
7694554Sbinkertn@umich.edu    if env['USE_MYSQL']:
7704554Sbinkertn@umich.edu        if not have_mysql:
7714554Sbinkertn@umich.edu            print "Warning: MySQL not available; " \
7726121Snate@binkert.org                  "forcing USE_MYSQL to False in", build_dir + "."
7734554Sbinkertn@umich.edu            env['USE_MYSQL'] = False
7744554Sbinkertn@umich.edu        else:
7754554Sbinkertn@umich.edu            print "Compiling in", build_dir, "with MySQL support."
7764781Snate@binkert.org            env.ParseConfig(mysql_config_libs)
7774554Sbinkertn@umich.edu            env.ParseConfig(mysql_config_include)
7784554Sbinkertn@umich.edu
7792667Sstever@eecs.umich.edu    # Save sticky option settings back to current options file
7804554Sbinkertn@umich.edu    sticky_opts.Save(current_opts_file, env)
7814554Sbinkertn@umich.edu
7824554Sbinkertn@umich.edu    # Do this after we save setting back, or else we'll tack on an
7834554Sbinkertn@umich.edu    # extra 'qdo' every time we run scons.
7842667Sstever@eecs.umich.edu    if env['BATCH']:
7854554Sbinkertn@umich.edu        env['CC']  = env['BATCH_CMD'] + ' ' + env['CC']
7862667Sstever@eecs.umich.edu        env['CXX'] = env['BATCH_CMD'] + ' ' + env['CXX']
7874554Sbinkertn@umich.edu
7886121Snate@binkert.org    if env['USE_SSE2']:
7892667Sstever@eecs.umich.edu        env.Append(CCFLAGS='-msse2')
7905522Snate@binkert.org
7915522Snate@binkert.org    # The src/SConscript file sets up the build rules in 'env' according
7925522Snate@binkert.org    # to the configured options.  It returns a list of environments,
7935522Snate@binkert.org    # one for each variant build (debug, opt, etc.)
7945522Snate@binkert.org    envList = SConscript('src/SConscript', build_dir = build_path,
7955522Snate@binkert.org                         exports = 'env')
7965522Snate@binkert.org
7975522Snate@binkert.org    # Set up the regression tests for each build.
7985522Snate@binkert.org    for e in envList:
7995522Snate@binkert.org        SConscript('tests/SConscript',
8005522Snate@binkert.org                   build_dir = joinpath(build_path, 'tests', e.Label),
8015522Snate@binkert.org                   exports = { 'env' : e }, duplicate = False)
8025522Snate@binkert.org
8035522Snate@binkert.orgHelp(help_text)
8045522Snate@binkert.org
8055522Snate@binkert.org
8065522Snate@binkert.org###################################################
8075522Snate@binkert.org#
8085522Snate@binkert.org# Let SCons do its thing.  At this point SCons will use the defined
8095522Snate@binkert.org# build environments to build the requested targets.
8105522Snate@binkert.org#
8115522Snate@binkert.org###################################################
8125522Snate@binkert.org
8135522Snate@binkert.org