SConscript revision 11504
112855Sgabeblack@google.com# -*- mode:python -*-
212855Sgabeblack@google.com#
312855Sgabeblack@google.com# Copyright (c) 2016 ARM Limited
412855Sgabeblack@google.com# All rights reserved
512855Sgabeblack@google.com#
612855Sgabeblack@google.com# The license below extends only to copyright in the software and shall
712855Sgabeblack@google.com# not be construed as granting a license to any other intellectual
812855Sgabeblack@google.com# property including but not limited to intellectual property relating
912855Sgabeblack@google.com# to a hardware implementation of the functionality of the software
1012855Sgabeblack@google.com# licensed hereunder.  You may use the software subject to the license
1112855Sgabeblack@google.com# terms below provided that you ensure that this notice is replicated
1212855Sgabeblack@google.com# unmodified and in its entirety in all distributions of the software,
1312855Sgabeblack@google.com# modified or unmodified, in source code or in binary form.
1412855Sgabeblack@google.com#
1512855Sgabeblack@google.com# Copyright (c) 2004-2006 The Regents of The University of Michigan
1612855Sgabeblack@google.com# All rights reserved.
1712855Sgabeblack@google.com#
1812855Sgabeblack@google.com# Redistribution and use in source and binary forms, with or without
1912855Sgabeblack@google.com# modification, are permitted provided that the following conditions are
2012855Sgabeblack@google.com# met: redistributions of source code must retain the above copyright
2112855Sgabeblack@google.com# notice, this list of conditions and the following disclaimer;
2212855Sgabeblack@google.com# redistributions in binary form must reproduce the above copyright
2312855Sgabeblack@google.com# notice, this list of conditions and the following disclaimer in the
2412855Sgabeblack@google.com# documentation and/or other materials provided with the distribution;
2512855Sgabeblack@google.com# neither the name of the copyright holders nor the names of its
2612855Sgabeblack@google.com# contributors may be used to endorse or promote products derived from
2712855Sgabeblack@google.com# this software without specific prior written permission.
2812855Sgabeblack@google.com#
2912855Sgabeblack@google.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3012855Sgabeblack@google.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3112855Sgabeblack@google.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3212855Sgabeblack@google.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3312855Sgabeblack@google.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3412855Sgabeblack@google.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3512855Sgabeblack@google.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3612855Sgabeblack@google.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3712855Sgabeblack@google.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3812855Sgabeblack@google.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3912855Sgabeblack@google.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4012855Sgabeblack@google.com#
4112855Sgabeblack@google.com# Authors: Steve Reinhardt
4212855Sgabeblack@google.com#          Kevin Lim
4312855Sgabeblack@google.com#          Andreas Sandberg
4412855Sgabeblack@google.com
4512855Sgabeblack@google.comfrom SCons.Script.SConscript import SConsEnvironment
4612855Sgabeblack@google.comimport os
4712855Sgabeblack@google.comimport pickle
4812855Sgabeblack@google.comimport sys
4912855Sgabeblack@google.com
5012855Sgabeblack@google.comsys.path.insert(0, Dir(".").srcnode().abspath)
5112855Sgabeblack@google.comimport testing.tests as tests
5212855Sgabeblack@google.comimport testing.results as results
5312855Sgabeblack@google.com
5412855Sgabeblack@google.comImport('env')
5512855Sgabeblack@google.com
5612855Sgabeblack@google.com# get the termcap from the environment
5712855Sgabeblack@google.comtermcap = env['TERMCAP']
5812855Sgabeblack@google.com
5912855Sgabeblack@google.com# Dict that accumulates lists of tests by category (quick, medium, long)
6012855Sgabeblack@google.comenv.Tests = {}
6112855Sgabeblack@google.comgpu_isa = env['TARGET_GPU_ISA'] if env['BUILD_GPU'] else None
6212855Sgabeblack@google.comfor cat in tests.all_categories:
6312855Sgabeblack@google.com    env.Tests[cat] = tuple(
6412855Sgabeblack@google.com        tests.get_tests(env["TARGET_ISA"],
6512855Sgabeblack@google.com                        categories=(cat, ),
6612855Sgabeblack@google.com                        ruby_protocol=env["PROTOCOL"],
6712855Sgabeblack@google.com                        gpu_isa=gpu_isa))
6812855Sgabeblack@google.com
6912855Sgabeblack@google.comdef color_message(color, msg):
7012855Sgabeblack@google.com    return color + msg + termcap.Normal
7112855Sgabeblack@google.com
7212855Sgabeblack@google.comdef run_test(target, source, env):
7312855Sgabeblack@google.com    """Run a test and produce results as a pickle file.
7412855Sgabeblack@google.com
7512855Sgabeblack@google.com    Targets are as follows:
7612855Sgabeblack@google.com    target[0] : Pickle file
7712855Sgabeblack@google.com
7812855Sgabeblack@google.com    Sources are:
7912855Sgabeblack@google.com    source[0] : gem5 binary
8012855Sgabeblack@google.com    source[1] : tests/run.py script
8112855Sgabeblack@google.com    source[2:] : reference files
8212855Sgabeblack@google.com
8312855Sgabeblack@google.com    """
8412855Sgabeblack@google.com    tgt_dir = os.path.dirname(str(target[0]))
8512855Sgabeblack@google.com    config = tests.ClassicConfig(*tgt_dir.split('/')[-6:])
8612855Sgabeblack@google.com    test = tests.ClassicTest(source[0].abspath, tgt_dir, config,
8712855Sgabeblack@google.com                             timeout=5*60*60,
8812855Sgabeblack@google.com                             skip_diff_out=True)
8912855Sgabeblack@google.com
9012855Sgabeblack@google.com    for ref in test.ref_files():
9112855Sgabeblack@google.com        out_file = os.path.join(tgt_dir, ref)
9212855Sgabeblack@google.com        if os.path.exists(out_file):
9312855Sgabeblack@google.com            env.Execute(Delete(out_file))
9412855Sgabeblack@google.com
9512855Sgabeblack@google.com    with open(target[0].abspath, "wb") as fout:
9612855Sgabeblack@google.com        formatter = results.Pickle(fout=fout)
9712855Sgabeblack@google.com        formatter.dump_suites([ test.run() ])
9812855Sgabeblack@google.com
9912855Sgabeblack@google.com    return 0
10012855Sgabeblack@google.com
10112855Sgabeblack@google.comdef run_test_string(target, source, env):
10212855Sgabeblack@google.com    return env.subst("Running test in ${TARGETS[0].dir}.",
10312855Sgabeblack@google.com                     target=target, source=source)
10412855Sgabeblack@google.com
10512855Sgabeblack@google.comtestAction = env.Action(run_test, run_test_string)
10612855Sgabeblack@google.com
10712855Sgabeblack@google.comdef print_test(target, source, env):
10812855Sgabeblack@google.com    """Run a test and produce results as a pickle file.
10912855Sgabeblack@google.com
11012855Sgabeblack@google.com    Targets are as follows:
11112855Sgabeblack@google.com    target[*] : Dummy targets
11212855Sgabeblack@google.com
11312855Sgabeblack@google.com    Sources are:
11412855Sgabeblack@google.com    source[0] : Pickle file
11512855Sgabeblack@google.com
11612855Sgabeblack@google.com    """
11712855Sgabeblack@google.com    with open(source[0].abspath, "rb") as fin:
11812855Sgabeblack@google.com        result = pickle.load(fin)
11912855Sgabeblack@google.com
12012855Sgabeblack@google.com    assert len(result) == 1
12112855Sgabeblack@google.com    result = result[0]
12212855Sgabeblack@google.com
12312855Sgabeblack@google.com    run = result.results[0]
12412855Sgabeblack@google.com    assert run.name == "gem5"
12512855Sgabeblack@google.com
12612855Sgabeblack@google.com    formatter = None
12712855Sgabeblack@google.com    if not run:
12812855Sgabeblack@google.com        status = color_message(termcap.Red, "FAILED!")
12912855Sgabeblack@google.com        formatter = results.Text()
13012855Sgabeblack@google.com    elif run.skipped():
131        status = color_message(termcap.Cyan, "skipped.")
132    elif result:
133        status = color_message(termcap.Green, "passed.")
134    else:
135        status = color_message(termcap.Yellow, "CHANGED!")
136        formatter = results.Text()
137
138    if formatter:
139        formatter.dump_suites([result])
140
141    print "***** %s: %s" % (source[0].dir, status)
142    return 0
143
144printAction = env.Action(print_test, strfunction=None)
145
146def update_test(target, source, env):
147    """Update test reference data
148
149    Targets are as follows:
150    target[0] : Dummy file
151
152    Sources are:
153    source[0] : Pickle file
154    """
155
156    src_dir = os.path.dirname(str(source[0]))
157    config = tests.ClassicConfig(*src_dir.split('/')[-6:])
158    test = tests.ClassicTest(source[0].abspath, src_dir, config)
159    ref_dir = test.ref_dir
160
161    with open(source[0].abspath, "rb") as fin:
162        result = pickle.load(fin)
163
164    assert len(result) == 1
165    result = result[0]
166
167    run = result.results[0]
168    assert run.name == "gem5"
169
170    if run.skipped():
171        print "*** %s: %s: Test skipped, not updating." % (
172            source[0].dir, color_message(termcap.Yellow, "WARNING"), )
173        return 0
174    elif result:
175        print "*** %s: %s: Test successful, not updating." % (
176            source[0].dir, color_message(termcap.Green, "skipped"), )
177        return 0
178    elif not run.success():
179        print "*** %s: %s: Test failed, not updating." % (
180            source[0].dir, color_message(termcap.Red, "ERROR"), )
181        return 1
182
183    print "** Updating %s" % (test, )
184    test.update_ref()
185
186    return 0
187
188def update_test_string(target, source, env):
189    return env.subst("Updating ${SOURCES[0].dir}",
190                     target=target, source=source)
191
192updateAction = env.Action(update_test, update_test_string)
193
194def test_builder(test_tuple):
195    """Define a test."""
196
197    out_dir = "/".join(test_tuple)
198    binary = env.M5Binary.abspath
199    test = tests.ClassicTest(binary, out_dir, test_tuple)
200
201    def tgt(name):
202        return os.path.join(out_dir, name)
203
204    def ref(name):
205        return os.path.join(test.ref_dir, name)
206
207    pickle_file = tgt("status.pickle")
208    targets = [
209        pickle_file,
210    ]
211
212    sources = [
213        env.M5Binary,
214        "run.py",
215    ] + [ ref(f) for f in test.ref_files() ]
216
217    env.Command(targets, sources, testAction)
218
219    # phony target to echo status
220    if GetOption('update_ref'):
221        p = env.Command(tgt("_update"), [pickle_file], updateAction)
222    else:
223        p = env.Command(tgt("_print"), [pickle_file], printAction)
224
225    env.AlwaysBuild(p)
226
227def list_tests(target, source, env):
228    """Create a list of tests
229
230    Targets are as follows:
231    target[0] : List file (e.g., tests/opt/all.list,  tests/opt/quick.list)
232
233    Sources are: -
234
235    """
236
237    tgt_name = os.path.basename(str(target[0]))
238    base, ext = os.path.splitext(tgt_name)
239    categories = tests.all_categories if base == "all" else (base, )
240
241    with open(target[0].abspath, "w") as fout:
242        for cat in categories:
243            for test in env.Tests[cat]:
244                print >> fout,"/".join(test)
245
246    return 0
247
248testListAction = env.Action(list_tests, strfunction=None)
249
250env.Command("all.list", tuple(), testListAction)
251for cat, test_list in env.Tests.items():
252    env.Command("%s.list" % cat, tuple(), testListAction)
253    for test in test_list:
254        test_builder(test)
255