1#! /usr/bin/env python2.7
2# Copyright (c) 2005-2007 The Regents of The University of Michigan
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met: redistributions of source code must retain the above copyright
8# notice, this list of conditions and the following disclaimer;
9# redistributions in binary form must reproduce the above copyright
10# notice, this list of conditions and the following disclaimer in the
11# documentation and/or other materials provided with the distribution;
12# neither the name of the copyright holders nor the names of its
13# contributors may be used to endorse or promote products derived from
14# this software without specific prior written permission.
15#
16# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27#
28# Authors: Steve Reinhardt
29
30import sys
31import os
32import optparse
33import datetime
34from subprocess import call
35
36progname = os.path.basename(sys.argv[0])
37
38optparser = optparse.OptionParser()
39add_option = optparser.add_option
40add_option('-v', '--verbose', action='store_true', default=False,
41           help='echo commands before executing')
42add_option('--builds',
43           default='ALPHA,' \
44           'MIPS,' \
45           'NULL,' \
46           'NULL_MOESI_hammer,' \
47           'NULL_MESI_Two_Level,'  \
48           'NULL_MOESI_CMP_directory,' \
49           'NULL_MOESI_CMP_token,' \
50           'POWER,' \
51           'SPARC,' \
52           'X86,X86_MESI_Two_Level,' \
53           'ARM,' \
54           'RISCV,' \
55           'HSAIL_X86',
56           help="comma-separated build targets to test (default: '%default')")
57add_option('--modes',
58           default='se,fs',
59           help="comma-separated modes to test (default: '%default')")
60add_option('--test-variants', default='opt',
61           help="comma-separated build variants to test (default: '%default')"\
62           ", set to '' for none")
63add_option('--compile-variants', default='debug,fast',
64           help="comma-separated build variants to compile only (not test) " \
65           "(default: '%default'), set to '' for none", metavar='VARIANTS')
66add_option('--scons-opts', default='', metavar='OPTS',
67           help='scons options')
68add_option('-j', '--jobs', type='int', default=1, metavar='N',
69           help='number of parallel jobs to use (0 to use all cores)')
70add_option('-k', '--keep-going', action='store_true',
71           help='keep going after errors')
72add_option('--update-ref', action='store_true',
73           help='update reference outputs')
74add_option('-D', '--build-dir', default='', metavar='DIR',
75           help='build directory location')
76add_option('-n', "--no-exec", default=False, action='store_true',
77           help="don't actually invoke scons, just echo SCons command line")
78
79(options, tests) = optparser.parse_args()
80
81
82# split a comma-separated list, but return an empty list if given the
83# empty string
84def split_if_nonempty(s):
85    if not s:
86        return []
87    return s.split(',')
88
89# split list options on ',' to get Python lists
90builds = split_if_nonempty(options.builds)
91modes = split_if_nonempty(options.modes)
92test_variants = split_if_nonempty(options.test_variants)
93compile_variants = split_if_nonempty(options.compile_variants)
94
95options.build_dir = os.path.join(options.build_dir, 'build')
96
97# Call os.system() and raise exception if return status is non-zero
98def system(cmd):
99    try:
100        retcode = call(cmd, shell=True)
101        if retcode < 0:
102            print >>sys.stderr, "Child was terminated by signal", -retcode
103            print >>sys.stderr, "When attemping to execute: %s" % cmd
104            sys.exit(1)
105        elif retcode > 0:
106            print >>sys.stderr, "Child returned", retcode
107            print >>sys.stderr, "When attemping to execute: %s" % cmd
108            sys.exit(1)
109    except OSError, e:
110        print >>sys.stderr, "Execution failed:", e
111        print >>sys.stderr, "When attemping to execute: %s" % cmd
112        sys.exit(1)
113
114targets = []
115
116# start with compile-only targets, if any
117if compile_variants:
118    targets += ['%s/%s/gem5.%s' % (options.build_dir, build, variant)
119                for variant in compile_variants
120                for build in builds]
121
122# By default run the 'quick' tests, all expands to quick and long
123if not tests:
124    tests = ['quick']
125elif 'all' in tests:
126    tests = ['quick', 'long']
127
128# set up test targets for scons, since we don't have any quick SPARC
129# full-system tests exclude it
130targets += ['%s/%s/tests/%s/%s/%s' % (options.build_dir, build, variant, test,
131                                      mode)
132            for build in builds
133            for variant in test_variants
134            for test in tests
135            for mode in modes
136            if not (build == 'SPARC' and test == 'quick' and mode == 'fs')]
137
138def cpu_count():
139    if 'bsd' in sys.platform or sys.platform == 'darwin':
140        try:
141            return int(os.popen('sysctl -n hw.ncpu').read())
142        except ValueError:
143            pass
144    else:
145        try:
146            return os.sysconf('SC_NPROCESSORS_ONLN')
147        except (ValueError, OSError, AttributeError):
148            pass
149
150    raise NotImplementedError('cannot determine number of cpus')
151
152scons_opts = options.scons_opts
153if options.jobs != 1:
154    if options.jobs == 0:
155        options.jobs = cpu_count()
156    scons_opts += ' -j %d' % options.jobs
157if options.keep_going:
158    scons_opts += ' -k'
159if options.update_ref:
160    scons_opts += ' --update-ref'
161
162# We generally compile gem5.fast only to make sure it compiles OK;
163# it's not very useful to run as a regression test since assertions
164# are disabled.  Thus there's not much point spending time on
165# link-time optimization.
166scons_opts += ' --no-lto'
167
168cmd = 'scons --ignore-style %s %s' % (scons_opts, ' '.join(targets))
169if options.no_exec:
170    print cmd
171else:
172    system(cmd)
173    sys.exit(0)
174