SConscript revision 12866
12929Sktlim@umich.edu# -*- mode:python -*-
211504Sandreas.sandberg@arm.com#
311504Sandreas.sandberg@arm.com# Copyright (c) 2016 ARM Limited
411504Sandreas.sandberg@arm.com# All rights reserved
511504Sandreas.sandberg@arm.com#
611504Sandreas.sandberg@arm.com# The license below extends only to copyright in the software and shall
711504Sandreas.sandberg@arm.com# not be construed as granting a license to any other intellectual
811504Sandreas.sandberg@arm.com# property including but not limited to intellectual property relating
911504Sandreas.sandberg@arm.com# to a hardware implementation of the functionality of the software
1011504Sandreas.sandberg@arm.com# licensed hereunder.  You may use the software subject to the license
1111504Sandreas.sandberg@arm.com# terms below provided that you ensure that this notice is replicated
1211504Sandreas.sandberg@arm.com# unmodified and in its entirety in all distributions of the software,
1311504Sandreas.sandberg@arm.com# modified or unmodified, in source code or in binary form.
1411504Sandreas.sandberg@arm.com#
152932Sktlim@umich.edu# Copyright (c) 2004-2006 The Regents of The University of Michigan
162929Sktlim@umich.edu# All rights reserved.
172929Sktlim@umich.edu#
182929Sktlim@umich.edu# Redistribution and use in source and binary forms, with or without
192929Sktlim@umich.edu# modification, are permitted provided that the following conditions are
202929Sktlim@umich.edu# met: redistributions of source code must retain the above copyright
212929Sktlim@umich.edu# notice, this list of conditions and the following disclaimer;
222929Sktlim@umich.edu# redistributions in binary form must reproduce the above copyright
232929Sktlim@umich.edu# notice, this list of conditions and the following disclaimer in the
242929Sktlim@umich.edu# documentation and/or other materials provided with the distribution;
252929Sktlim@umich.edu# neither the name of the copyright holders nor the names of its
262929Sktlim@umich.edu# contributors may be used to endorse or promote products derived from
272929Sktlim@umich.edu# this software without specific prior written permission.
282929Sktlim@umich.edu#
292929Sktlim@umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
302929Sktlim@umich.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
312929Sktlim@umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
322929Sktlim@umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
332929Sktlim@umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
342929Sktlim@umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
352929Sktlim@umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
362929Sktlim@umich.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
372929Sktlim@umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
382929Sktlim@umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
392929Sktlim@umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
402932Sktlim@umich.edu#
412932Sktlim@umich.edu# Authors: Steve Reinhardt
422932Sktlim@umich.edu#          Kevin Lim
4311504Sandreas.sandberg@arm.com#          Andreas Sandberg
442929Sktlim@umich.edu
4512563Sgabeblack@google.comfrom __future__ import print_function
4612563Sgabeblack@google.com
472929Sktlim@umich.edufrom SCons.Script.SConscript import SConsEnvironment
4811504Sandreas.sandberg@arm.comimport os
4911504Sandreas.sandberg@arm.comimport pickle
5011504Sandreas.sandberg@arm.comimport sys
5111504Sandreas.sandberg@arm.com
5211504Sandreas.sandberg@arm.comsys.path.insert(0, Dir(".").srcnode().abspath)
5311504Sandreas.sandberg@arm.comimport testing.tests as tests
5411504Sandreas.sandberg@arm.comimport testing.results as results
5512246Sgabeblack@google.comfrom gem5_scons.util import get_termcap
562929Sktlim@umich.edu
572929Sktlim@umich.eduImport('env')
582929Sktlim@umich.edu
598947Sandreas.hansson@arm.com# get the termcap from the environment
6012246Sgabeblack@google.comtermcap = get_termcap()
618947Sandreas.hansson@arm.com
622929Sktlim@umich.edu# Dict that accumulates lists of tests by category (quick, medium, long)
632929Sktlim@umich.eduenv.Tests = {}
6411504Sandreas.sandberg@arm.comgpu_isa = env['TARGET_GPU_ISA'] if env['BUILD_GPU'] else None
6511504Sandreas.sandberg@arm.comfor cat in tests.all_categories:
6611504Sandreas.sandberg@arm.com    env.Tests[cat] = tuple(
6711504Sandreas.sandberg@arm.com        tests.get_tests(env["TARGET_ISA"],
6811504Sandreas.sandberg@arm.com                        categories=(cat, ),
6911504Sandreas.sandberg@arm.com                        ruby_protocol=env["PROTOCOL"],
7011504Sandreas.sandberg@arm.com                        gpu_isa=gpu_isa))
712929Sktlim@umich.edu
7211504Sandreas.sandberg@arm.comdef color_message(color, msg):
7311504Sandreas.sandberg@arm.com    return color + msg + termcap.Normal
746007Ssteve.reinhardt@amd.com
756007Ssteve.reinhardt@amd.comdef run_test(target, source, env):
7611504Sandreas.sandberg@arm.com    """Run a test and produce results as a pickle file.
772929Sktlim@umich.edu
782929Sktlim@umich.edu    Targets are as follows:
7911504Sandreas.sandberg@arm.com    target[0] : Pickle file
806007Ssteve.reinhardt@amd.com
816007Ssteve.reinhardt@amd.com    Sources are:
829781Sandreas.hansson@arm.com    source[0] : gem5 binary
836007Ssteve.reinhardt@amd.com    source[1] : tests/run.py script
8411504Sandreas.sandberg@arm.com    source[2:] : reference files
852929Sktlim@umich.edu
862929Sktlim@umich.edu    """
8711504Sandreas.sandberg@arm.com    tgt_dir = os.path.dirname(str(target[0]))
8811504Sandreas.sandberg@arm.com    config = tests.ClassicConfig(*tgt_dir.split('/')[-6:])
8911504Sandreas.sandberg@arm.com    test = tests.ClassicTest(source[0].abspath, tgt_dir, config,
9011504Sandreas.sandberg@arm.com                             timeout=5*60*60,
9111504Sandreas.sandberg@arm.com                             skip_diff_out=True)
926007Ssteve.reinhardt@amd.com
9311504Sandreas.sandberg@arm.com    for ref in test.ref_files():
9411504Sandreas.sandberg@arm.com        out_file = os.path.join(tgt_dir, ref)
9511504Sandreas.sandberg@arm.com        if os.path.exists(out_file):
9611504Sandreas.sandberg@arm.com            env.Execute(Delete(out_file))
976007Ssteve.reinhardt@amd.com
9811504Sandreas.sandberg@arm.com    with open(target[0].abspath, "wb") as fout:
9911504Sandreas.sandberg@arm.com        formatter = results.Pickle(fout=fout)
10011504Sandreas.sandberg@arm.com        formatter.dump_suites([ test.run() ])
1016007Ssteve.reinhardt@amd.com
1022929Sktlim@umich.edu    return 0
1032929Sktlim@umich.edu
1046007Ssteve.reinhardt@amd.comdef run_test_string(target, source, env):
1056007Ssteve.reinhardt@amd.com    return env.subst("Running test in ${TARGETS[0].dir}.",
1062929Sktlim@umich.edu                     target=target, source=source)
1072929Sktlim@umich.edu
1086007Ssteve.reinhardt@amd.comtestAction = env.Action(run_test, run_test_string)
1092929Sktlim@umich.edu
1102929Sktlim@umich.edudef print_test(target, source, env):
11111504Sandreas.sandberg@arm.com    """Run a test and produce results as a pickle file.
1128947Sandreas.hansson@arm.com
11311504Sandreas.sandberg@arm.com    Targets are as follows:
11411504Sandreas.sandberg@arm.com    target[*] : Dummy targets
1158947Sandreas.hansson@arm.com
11611504Sandreas.sandberg@arm.com    Sources are:
11711504Sandreas.sandberg@arm.com    source[0] : Pickle file
1188947Sandreas.hansson@arm.com
11911504Sandreas.sandberg@arm.com    """
12011504Sandreas.sandberg@arm.com    with open(source[0].abspath, "rb") as fin:
12111504Sandreas.sandberg@arm.com        result = pickle.load(fin)
1228947Sandreas.hansson@arm.com
12311504Sandreas.sandberg@arm.com    assert len(result) == 1
12411504Sandreas.sandberg@arm.com    result = result[0]
12511504Sandreas.sandberg@arm.com
12611504Sandreas.sandberg@arm.com    formatter = None
12711542Sandreas.sandberg@arm.com    if result.skipped():
12811542Sandreas.sandberg@arm.com        status = color_message(termcap.Cyan, "skipped.")
12911542Sandreas.sandberg@arm.com    elif result.changed():
13011542Sandreas.sandberg@arm.com        status = color_message(termcap.Yellow, "CHANGED!")
13111504Sandreas.sandberg@arm.com        formatter = results.Text()
13211504Sandreas.sandberg@arm.com    elif result:
13311504Sandreas.sandberg@arm.com        status = color_message(termcap.Green, "passed.")
13411504Sandreas.sandberg@arm.com    else:
13511542Sandreas.sandberg@arm.com        status = color_message(termcap.Red, "FAILED!")
13611504Sandreas.sandberg@arm.com        formatter = results.Text()
13711504Sandreas.sandberg@arm.com
13811504Sandreas.sandberg@arm.com    if formatter:
13911504Sandreas.sandberg@arm.com        formatter.dump_suites([result])
14011504Sandreas.sandberg@arm.com
14112563Sgabeblack@google.com    print("***** %s: %s" % (source[0].dir, status))
1422929Sktlim@umich.edu    return 0
1432929Sktlim@umich.edu
14411504Sandreas.sandberg@arm.comprintAction = env.Action(print_test, strfunction=None)
1454937Sstever@gmail.com
1462929Sktlim@umich.edudef update_test(target, source, env):
14711504Sandreas.sandberg@arm.com    """Update test reference data
1482929Sktlim@umich.edu
14911504Sandreas.sandberg@arm.com    Targets are as follows:
15011504Sandreas.sandberg@arm.com    target[0] : Dummy file
1512929Sktlim@umich.edu
15211504Sandreas.sandberg@arm.com    Sources are:
15311504Sandreas.sandberg@arm.com    source[0] : Pickle file
1542929Sktlim@umich.edu    """
15511504Sandreas.sandberg@arm.com
15611504Sandreas.sandberg@arm.com    src_dir = os.path.dirname(str(source[0]))
15711504Sandreas.sandberg@arm.com    config = tests.ClassicConfig(*src_dir.split('/')[-6:])
15811504Sandreas.sandberg@arm.com    test = tests.ClassicTest(source[0].abspath, src_dir, config)
15911504Sandreas.sandberg@arm.com    ref_dir = test.ref_dir
16011504Sandreas.sandberg@arm.com
16111504Sandreas.sandberg@arm.com    with open(source[0].abspath, "rb") as fin:
16211504Sandreas.sandberg@arm.com        result = pickle.load(fin)
16311504Sandreas.sandberg@arm.com
16411504Sandreas.sandberg@arm.com    assert len(result) == 1
16511504Sandreas.sandberg@arm.com    result = result[0]
16611504Sandreas.sandberg@arm.com
16711542Sandreas.sandberg@arm.com    if result.skipped():
16812563Sgabeblack@google.com        print("*** %s: %s: Test skipped, not updating." %
16912563Sgabeblack@google.com              (source[0].dir, color_message(termcap.Yellow, "WARNING")))
17011504Sandreas.sandberg@arm.com        return 0
17111504Sandreas.sandberg@arm.com    elif result:
17212563Sgabeblack@google.com        print("*** %s: %s: Test successful, not updating." %
17312563Sgabeblack@google.com              (source[0].dir, color_message(termcap.Green, "skipped")))
17411504Sandreas.sandberg@arm.com        return 0
17511542Sandreas.sandberg@arm.com    elif result.failed_run():
17612563Sgabeblack@google.com        print("*** %s: %s: Test failed, not updating." %
17712563Sgabeblack@google.com              (source[0].dir, color_message(termcap.Red, "ERROR")))
17811504Sandreas.sandberg@arm.com        return 1
17911504Sandreas.sandberg@arm.com
18012563Sgabeblack@google.com    print("** Updating %s" % test)
18111504Sandreas.sandberg@arm.com    test.update_ref()
18211504Sandreas.sandberg@arm.com
1832929Sktlim@umich.edu    return 0
1842929Sktlim@umich.edu
1852929Sktlim@umich.edudef update_test_string(target, source, env):
18611504Sandreas.sandberg@arm.com    return env.subst("Updating ${SOURCES[0].dir}",
1872929Sktlim@umich.edu                     target=target, source=source)
1882929Sktlim@umich.edu
1892929Sktlim@umich.eduupdateAction = env.Action(update_test, update_test_string)
1902929Sktlim@umich.edu
19111504Sandreas.sandberg@arm.comdef test_builder(test_tuple):
1922997Sstever@eecs.umich.edu    """Define a test."""
1932929Sktlim@umich.edu
19411504Sandreas.sandberg@arm.com    out_dir = "/".join(test_tuple)
19511504Sandreas.sandberg@arm.com    binary = env.M5Binary.abspath
19611504Sandreas.sandberg@arm.com    test = tests.ClassicTest(binary, out_dir, test_tuple)
1972929Sktlim@umich.edu
19811504Sandreas.sandberg@arm.com    def tgt(name):
19911504Sandreas.sandberg@arm.com        return os.path.join(out_dir, name)
2002929Sktlim@umich.edu
20111504Sandreas.sandberg@arm.com    def ref(name):
20211504Sandreas.sandberg@arm.com        return os.path.join(test.ref_dir, name)
2032997Sstever@eecs.umich.edu
20411504Sandreas.sandberg@arm.com    pickle_file = tgt("status.pickle")
20511504Sandreas.sandberg@arm.com    targets = [
20611504Sandreas.sandberg@arm.com        pickle_file,
20711504Sandreas.sandberg@arm.com    ]
2082997Sstever@eecs.umich.edu
20911504Sandreas.sandberg@arm.com    sources = [
21011504Sandreas.sandberg@arm.com        env.M5Binary,
21111504Sandreas.sandberg@arm.com        "run.py",
21211504Sandreas.sandberg@arm.com    ] + [ ref(f) for f in test.ref_files() ]
21311504Sandreas.sandberg@arm.com
21411504Sandreas.sandberg@arm.com    env.Command(targets, sources, testAction)
2152929Sktlim@umich.edu
2162997Sstever@eecs.umich.edu    # phony target to echo status
2178120Sgblack@eecs.umich.edu    if GetOption('update_ref'):
21811504Sandreas.sandberg@arm.com        p = env.Command(tgt("_update"), [pickle_file], updateAction)
2192997Sstever@eecs.umich.edu    else:
22011504Sandreas.sandberg@arm.com        p = env.Command(tgt("_print"), [pickle_file], printAction)
2212929Sktlim@umich.edu
2222997Sstever@eecs.umich.edu    env.AlwaysBuild(p)
2232929Sktlim@umich.edu
22411504Sandreas.sandberg@arm.comdef list_tests(target, source, env):
22511504Sandreas.sandberg@arm.com    """Create a list of tests
2262929Sktlim@umich.edu
22711504Sandreas.sandberg@arm.com    Targets are as follows:
22811504Sandreas.sandberg@arm.com    target[0] : List file (e.g., tests/opt/all.list,  tests/opt/quick.list)
2293691Shsul@eecs.umich.edu
23011504Sandreas.sandberg@arm.com    Sources are: -
2313005Sstever@eecs.umich.edu
23211504Sandreas.sandberg@arm.com    """
23311105Spower.jg@gmail.com
23411504Sandreas.sandberg@arm.com    tgt_name = os.path.basename(str(target[0]))
23511504Sandreas.sandberg@arm.com    base, ext = os.path.splitext(tgt_name)
23611504Sandreas.sandberg@arm.com    categories = tests.all_categories if base == "all" else (base, )
2376166Ssteve.reinhardt@amd.com
23811504Sandreas.sandberg@arm.com    with open(target[0].abspath, "w") as fout:
23911504Sandreas.sandberg@arm.com        for cat in categories:
24011504Sandreas.sandberg@arm.com            for test in env.Tests[cat]:
24112563Sgabeblack@google.com                print("/".join(test), file=fout)
24211504Sandreas.sandberg@arm.com
24311504Sandreas.sandberg@arm.com    return 0
24411504Sandreas.sandberg@arm.com
24511504Sandreas.sandberg@arm.comtestListAction = env.Action(list_tests, strfunction=None)
24611504Sandreas.sandberg@arm.com
24711504Sandreas.sandberg@arm.comenv.Command("all.list", tuple(), testListAction)
24811504Sandreas.sandberg@arm.comfor cat, test_list in env.Tests.items():
24911504Sandreas.sandberg@arm.com    env.Command("%s.list" % cat, tuple(), testListAction)
25011504Sandreas.sandberg@arm.com    for test in test_list:
25111504Sandreas.sandberg@arm.com        test_builder(test)
252