SConscript revision 11974
12139SN/A# -*- mode:python -*-
22139SN/A#
32139SN/A# Copyright (c) 2016 ARM Limited
42139SN/A# All rights reserved
52139SN/A#
62139SN/A# The license below extends only to copyright in the software and shall
72139SN/A# not be construed as granting a license to any other intellectual
82139SN/A# property including but not limited to intellectual property relating
92139SN/A# to a hardware implementation of the functionality of the software
102139SN/A# licensed hereunder.  You may use the software subject to the license
112139SN/A# terms below provided that you ensure that this notice is replicated
122139SN/A# unmodified and in its entirety in all distributions of the software,
132139SN/A# modified or unmodified, in source code or in binary form.
142139SN/A#
152139SN/A# Copyright (c) 2004-2006 The Regents of The University of Michigan
162139SN/A# All rights reserved.
172139SN/A#
182139SN/A# Redistribution and use in source and binary forms, with or without
192139SN/A# modification, are permitted provided that the following conditions are
202139SN/A# met: redistributions of source code must retain the above copyright
212139SN/A# notice, this list of conditions and the following disclaimer;
222139SN/A# redistributions in binary form must reproduce the above copyright
232139SN/A# notice, this list of conditions and the following disclaimer in the
242139SN/A# documentation and/or other materials provided with the distribution;
252139SN/A# neither the name of the copyright holders nor the names of its
262139SN/A# contributors may be used to endorse or promote products derived from
272139SN/A# this software without specific prior written permission.
282665Ssaidi@eecs.umich.edu#
292665Ssaidi@eecs.umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
302139SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
314202Sbinkertn@umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
322139SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
334202Sbinkertn@umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
342152SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
352152SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
362139SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
372139SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
382139SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
392139SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
402139SN/A#
412152SN/A# Authors: Steve Reinhardt
422152SN/A#          Kevin Lim
432139SN/A#          Andreas Sandberg
442139SN/A
452139SN/Afrom SCons.Script.SConscript import SConsEnvironment
464781Snate@binkert.orgimport os
474781Snate@binkert.orgimport pickle
484781Snate@binkert.orgimport sys
494781Snate@binkert.org
504781Snate@binkert.orgsys.path.insert(0, Dir(".").srcnode().abspath)
513170Sstever@eecs.umich.eduimport testing.tests as tests
525664Sgblack@eecs.umich.eduimport testing.results as results
533806Ssaidi@eecs.umich.edu
546179Sksewell@umich.eduImport('env')
554781Snate@binkert.org
564781Snate@binkert.org# get the termcap from the environment
574781Snate@binkert.orgtermcap = env['TERMCAP']
584781Snate@binkert.org
594781Snate@binkert.org# Dict that accumulates lists of tests by category (quick, medium, long)
604781Snate@binkert.orgenv.Tests = {}
614781Snate@binkert.orggpu_isa = env['TARGET_GPU_ISA'] if env['BUILD_GPU'] else None
624781Snate@binkert.orgfor cat in tests.all_categories:
634781Snate@binkert.org    env.Tests[cat] = tuple(
642139SN/A        tests.get_tests(env["TARGET_ISA"],
652139SN/A                        categories=(cat, ),
663546Sgblack@eecs.umich.edu                        ruby_protocol=env["PROTOCOL"],
674202Sbinkertn@umich.edu                        gpu_isa=gpu_isa))
682152SN/A
692152SN/Adef color_message(color, msg):
702152SN/A    return color + msg + termcap.Normal
712152SN/A
722152SN/Adef run_test(target, source, env):
732152SN/A    """Run a test and produce results as a pickle file.
742152SN/A
752152SN/A    Targets are as follows:
762152SN/A    target[0] : Pickle file
772152SN/A
782152SN/A    Sources are:
792152SN/A    source[0] : gem5 binary
802504SN/A    source[1] : tests/run.py script
812504SN/A    source[2:] : reference files
822504SN/A
832504SN/A    """
842152SN/A    tgt_dir = os.path.dirname(str(target[0]))
852504SN/A    config = tests.ClassicConfig(*tgt_dir.split('/')[-6:])
862152SN/A    test = tests.ClassicTest(source[0].abspath, tgt_dir, config,
872152SN/A                             timeout=5*60*60,
882152SN/A                             skip_diff_out=True)
892152SN/A
902152SN/A    for ref in test.ref_files():
912152SN/A        out_file = os.path.join(tgt_dir, ref)
922152SN/A        if os.path.exists(out_file):
932152SN/A            env.Execute(Delete(out_file))
942632Sstever@eecs.umich.edu
952155SN/A    with open(target[0].abspath, "wb") as fout:
962155SN/A        formatter = results.Pickle(fout=fout)
972155SN/A        formatter.dump_suites([ test.run() ])
982155SN/A
992155SN/A    return 0
1002155SN/A
1015228Sgblack@eecs.umich.edudef run_test_string(target, source, env):
1022155SN/A    return env.subst("Running test in ${TARGETS[0].dir}.",
1032155SN/A                     target=target, source=source)
1042155SN/A
1052152SN/AtestAction = env.Action(run_test, run_test_string)
1062766Sktlim@umich.edu
1072766Sktlim@umich.edudef print_test(target, source, env):
1082766Sktlim@umich.edu    """Run a test and produce results as a pickle file.
1092766Sktlim@umich.edu
1102766Sktlim@umich.edu    Targets are as follows:
1112152SN/A    target[*] : Dummy targets
1122152SN/A
1132152SN/A    Sources are:
1142155SN/A    source[0] : Pickle file
1152152SN/A
1162152SN/A    """
1172718Sstever@eecs.umich.edu    with open(source[0].abspath, "rb") as fin:
1182921Sktlim@umich.edu        result = pickle.load(fin)
1192921Sktlim@umich.edu
1202921Sktlim@umich.edu    assert len(result) == 1
1212921Sktlim@umich.edu    result = result[0]
1222921Sktlim@umich.edu
1232921Sktlim@umich.edu    formatter = None
1242921Sktlim@umich.edu    if result.skipped():
1252921Sktlim@umich.edu        status = color_message(termcap.Cyan, "skipped.")
1262921Sktlim@umich.edu    elif result.changed():
1272152SN/A        status = color_message(termcap.Yellow, "CHANGED!")
1282152SN/A        formatter = results.Text()
1295944Sgblack@eecs.umich.edu    elif result:
1305944Sgblack@eecs.umich.edu        status = color_message(termcap.Green, "passed.")
1315944Sgblack@eecs.umich.edu    else:
1325944Sgblack@eecs.umich.edu        status = color_message(termcap.Red, "FAILED!")
1335944Sgblack@eecs.umich.edu        formatter = results.Text()
134
135    if formatter:
136        formatter.dump_suites([result])
137
138    print "***** %s: %s" % (source[0].dir, status)
139    return 0
140
141printAction = env.Action(print_test, strfunction=None)
142
143def update_test(target, source, env):
144    """Update test reference data
145
146    Targets are as follows:
147    target[0] : Dummy file
148
149    Sources are:
150    source[0] : Pickle file
151    """
152
153    src_dir = os.path.dirname(str(source[0]))
154    config = tests.ClassicConfig(*src_dir.split('/')[-6:])
155    test = tests.ClassicTest(source[0].abspath, src_dir, config)
156    ref_dir = test.ref_dir
157
158    with open(source[0].abspath, "rb") as fin:
159        result = pickle.load(fin)
160
161    assert len(result) == 1
162    result = result[0]
163
164    if result.skipped():
165        print "*** %s: %s: Test skipped, not updating." % (
166            source[0].dir, color_message(termcap.Yellow, "WARNING"), )
167        return 0
168    elif result:
169        print "*** %s: %s: Test successful, not updating." % (
170            source[0].dir, color_message(termcap.Green, "skipped"), )
171        return 0
172    elif result.failed_run():
173        print "*** %s: %s: Test failed, not updating." % (
174            source[0].dir, color_message(termcap.Red, "ERROR"), )
175        return 1
176
177    print "** Updating %s" % (test, )
178    test.update_ref()
179
180    return 0
181
182def update_test_string(target, source, env):
183    return env.subst("Updating ${SOURCES[0].dir}",
184                     target=target, source=source)
185
186updateAction = env.Action(update_test, update_test_string)
187
188def test_builder(test_tuple):
189    """Define a test."""
190
191    out_dir = "/".join(test_tuple)
192    binary = env.M5Binary.abspath
193    test = tests.ClassicTest(binary, out_dir, test_tuple)
194
195    def tgt(name):
196        return os.path.join(out_dir, name)
197
198    def ref(name):
199        return os.path.join(test.ref_dir, name)
200
201    pickle_file = tgt("status.pickle")
202    targets = [
203        pickle_file,
204    ]
205
206    sources = [
207        env.M5Binary,
208        "run.py",
209    ] + [ ref(f) for f in test.ref_files() ]
210
211    env.Command(targets, sources, testAction)
212
213    # phony target to echo status
214    if GetOption('update_ref'):
215        p = env.Command(tgt("_update"), [pickle_file], updateAction)
216    else:
217        p = env.Command(tgt("_print"), [pickle_file], printAction)
218
219    env.AlwaysBuild(p)
220
221def list_tests(target, source, env):
222    """Create a list of tests
223
224    Targets are as follows:
225    target[0] : List file (e.g., tests/opt/all.list,  tests/opt/quick.list)
226
227    Sources are: -
228
229    """
230
231    tgt_name = os.path.basename(str(target[0]))
232    base, ext = os.path.splitext(tgt_name)
233    categories = tests.all_categories if base == "all" else (base, )
234
235    with open(target[0].abspath, "w") as fout:
236        for cat in categories:
237            for test in env.Tests[cat]:
238                print >> fout,"/".join(test)
239
240    return 0
241
242testListAction = env.Action(list_tests, strfunction=None)
243
244env.Command("all.list", tuple(), testListAction)
245for cat, test_list in env.Tests.items():
246    env.Command("%s.list" % cat, tuple(), testListAction)
247    for test in test_list:
248        test_builder(test)
249