SConscript revision 2997
1955SN/A# -*- mode:python -*-
2955SN/A
31762SN/A# Copyright (c) 2004-2006 The Regents of The University of Michigan
4955SN/A# All rights reserved.
5955SN/A#
6955SN/A# Redistribution and use in source and binary forms, with or without
7955SN/A# modification, are permitted provided that the following conditions are
8955SN/A# met: redistributions of source code must retain the above copyright
9955SN/A# notice, this list of conditions and the following disclaimer;
10955SN/A# redistributions in binary form must reproduce the above copyright
11955SN/A# notice, this list of conditions and the following disclaimer in the
12955SN/A# documentation and/or other materials provided with the distribution;
13955SN/A# neither the name of the copyright holders nor the names of its
14955SN/A# contributors may be used to endorse or promote products derived from
15955SN/A# this software without specific prior written permission.
16955SN/A#
17955SN/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.
282665Ssaidi@eecs.umich.edu#
292665Ssaidi@eecs.umich.edu# Authors: Steve Reinhardt
30955SN/A#          Kevin Lim
31955SN/A
32955SN/Aimport os
331608SN/Aimport sys
34955SN/Aimport glob
35955SN/Afrom SCons.Script.SConscript import SConsEnvironment
36955SN/A
37955SN/AImport('env')
38955SN/A
39955SN/Aenv['DIFFOUT'] = File('diff-out')
40955SN/A
41955SN/A# Dict that accumulates lists of tests by category (quick, medium, long)
42955SN/Aenv.Tests = {}
43955SN/A
44955SN/Adef contents(node):
45955SN/A    return file(str(node)).read()
46955SN/A
47955SN/Adef check_test(target, source, env):
482023SN/A    """Check output from running test.
49955SN/A
50955SN/A    Targets are as follows:
51955SN/A    target[0] : outdiff
52955SN/A    target[1] : statsdiff
53955SN/A    target[2] : status
54955SN/A
55955SN/A    """
56955SN/A    # make sure target files are all gone
57955SN/A    for t in target:
581031SN/A        if os.path.exists(t.abspath):
59955SN/A            Execute(Delete(t.abspath))
601388SN/A    # Run diff on output & ref directories to find differences.
61955SN/A    # Exclude m5stats.txt since we will use diff-out on that.
62955SN/A    Execute(env.subst('diff -ubr ${SOURCES[0].dir} ${SOURCES[1].dir} ' +
631296SN/A                      '-I "^command line:" ' +		# for stdout file
64955SN/A                      '-I "^M5 compiled on" ' +		# for stderr file
652609SN/A                      '-I "^M5 simulation started" ' +	# for stderr file
66955SN/A                      '-I "^Simulation complete at" ' +	# for stderr file
67955SN/A                      '-I "^Listening for" ' +		# for stderr file
68955SN/A                      '--exclude=m5stats.txt --exclude=SCCS ' +
69955SN/A                      '--exclude=${TARGETS[0].file} ' +
70955SN/A                      '> ${TARGETS[0]}', target=target, source=source), None)
71955SN/A    print "===== Output differences ====="
72955SN/A    print contents(target[0])
73955SN/A    # Run diff-out on m5stats.txt file
74955SN/A    status = Execute(env.subst('$DIFFOUT $SOURCES > ${TARGETS[1]}',
75955SN/A                               target=target, source=source),
76955SN/A                     strfunction=None)
77955SN/A    print "===== Statistics differences ====="
78955SN/A    print contents(target[1])
79955SN/A    # Generate status file contents based on exit status of diff-out
80955SN/A    if status == 0:
81955SN/A        status_str = "passed."
82955SN/A    else:
83955SN/A        status_str = "FAILED!"
841717SN/A    f = file(str(target[2]), 'w')
852190SN/A    print >>f, env.subst('${TARGETS[2].dir}', target=target, source=source), \
862652Ssaidi@eecs.umich.edu          status_str
87955SN/A    f.close()
882410SN/A    # done
89955SN/A    return 0
90955SN/A
911717SN/Adef check_test_string(target, source, env):
922568SN/A    return env.subst("Comparing outputs in ${TARGETS[0].dir}.",
932568SN/A                     target=target, source=source)
942568SN/A
952499SN/AtestAction = env.Action(check_test, check_test_string)
962462SN/A
972568SN/Adef print_test(target, source, env):
982395SN/A    print '***** ' + contents(source[0])
992405SN/A    return 0
100955SN/A
101955SN/AprintAction = env.Action(print_test, strfunction = None)
102955SN/A
103955SN/Adef update_test(target, source, env):
104955SN/A    """Update reference test outputs.
1052090SN/A
106955SN/A    Target is phony.  First two sources are the ref & new m5stats.txt
1072667Sstever@eecs.umich.edu    files, respectively.  We actually copy everything in the
108955SN/A    respective directories except the status & diff output files.
109955SN/A
1101696SN/A    """
111955SN/A    dest_dir = str(source[0].get_dir())
112955SN/A    src_dir = str(source[1].get_dir())
113955SN/A    dest_files = os.listdir(dest_dir)
1141127SN/A    src_files = os.listdir(src_dir)
115955SN/A    # Exclude status & diff outputs
116955SN/A    for f in ('outdiff', 'statsdiff', 'status'):
1172379SN/A        if f in src_files:
118955SN/A            src_files.remove(f)
119955SN/A    for f in src_files:
120955SN/A        if f in dest_files:
1212155SN/A            print "  Replacing file", f
1222155SN/A            dest_files.remove(f)
1232155SN/A        else:
1242155SN/A            print "  Creating new file", f
1252155SN/A        copyAction = Copy(os.path.join(dest_dir, f), os.path.join(src_dir, f))
1262155SN/A        copyAction.strfunction = None
1272155SN/A        Execute(copyAction)
1282155SN/A    # warn about any files in dest not overwritten (other than SCCS dir)
1292155SN/A    if 'SCCS' in dest_files:
1302155SN/A        dest_files.remove('SCCS')
1312155SN/A    if dest_files:
1322155SN/A        print "Warning: file(s) in", dest_dir, "not updated:",
1332155SN/A        print ', '.join(dest_files)
1342155SN/A    return 0
1352155SN/A
1362155SN/Adef update_test_string(target, source, env):
1372155SN/A    return env.subst("Updating ${SOURCES[0].dir} from ${SOURCES[1].dir}",
1382155SN/A                     target=target, source=source)
1392155SN/A
1402155SN/AupdateAction = env.Action(update_test, update_test_string)
1412155SN/A
1422155SN/Adef test_builder(env, ref_dir):
1432155SN/A    """Define a test."""
1442155SN/A
1452155SN/A    (category, name, _ref, isa, opsys, config) = ref_dir.split('/')
1462155SN/A    assert(_ref == 'ref')
1472155SN/A
1482155SN/A    # target path (where test output goes) is the same except without
1492155SN/A    # the 'ref' component
1502155SN/A    tgt_dir = os.path.join(category, name, isa, opsys, config)
1512155SN/A
1522155SN/A    # prepend file name with tgt_dir
1532155SN/A    def tgt(f):
1542155SN/A        return os.path.join(tgt_dir, f)
1552155SN/A
1562155SN/A    ref_stats = os.path.join(ref_dir, 'm5stats.txt')
1572155SN/A    new_stats = tgt('m5stats.txt')
1582155SN/A    status_file = tgt('status')
1592155SN/A
1602422SN/A    # Base command for running test.  We mess around with indirectly
1612422SN/A    # referring to files via SOURCES and TARGETS so that scons can
1622422SN/A    # mess with paths all it wants to and we still get the right
1632422SN/A    # files.
1642422SN/A    base_cmd = '${SOURCES[0]} -d $TARGET.dir ${SOURCES[1]} %s' % tgt_dir
1652422SN/A    # stdout and stderr files
1662422SN/A    cmd_stdout = '${TARGETS[0]}'
1672397SN/A    cmd_stderr = '${TARGETS[1]}'
1682397SN/A
1692422SN/A    # Prefix test run with batch job submission command if appropriate.
1702422SN/A    # Output redirection is also different for batch runs.
171955SN/A    # Batch command also supports timeout arg (in seconds, not minutes).
172955SN/A    if env['BATCH']:
173955SN/A        cmd = [env['BATCH_CMD'], '-t', str(timeout * 60),
174955SN/A               '-o', cmd_stdout, '-e', cmd_stderr, base_cmd]
175955SN/A    else:
176955SN/A        cmd = [base_cmd, '>', cmd_stdout, '2>', cmd_stderr]
177955SN/A            
178955SN/A    env.Command([tgt('stdout'), tgt('stderr'), new_stats],
1791078SN/A                [env.M5Binary, 'run.py'], ' '.join(cmd))
180955SN/A
181955SN/A    # order of targets is important... see check_test
182955SN/A    env.Command([tgt('outdiff'), tgt('statsdiff'), status_file],
183955SN/A                [ref_stats, new_stats],
1841917SN/A                testAction)
185955SN/A
186955SN/A    # phony target to echo status
187955SN/A    if env['update_ref']:
188955SN/A        p = env.Command(tgt('_update'),
189974SN/A                        [ref_stats, new_stats, status_file],
190955SN/A                        updateAction)
191955SN/A    else:
192955SN/A        p = env.Command(tgt('_print'), [status_file], printAction)
193955SN/A
1942566SN/A    env.AlwaysBuild(p)
1952566SN/A
196955SN/A
197955SN/Acwd = os.getcwd()
1982539SN/Aos.chdir(str(Dir('.').srcdir))
199955SN/Afor config in ['simple-atomic']:
200955SN/A    dirs = glob.glob('*/*/ref/%s/*/%s' % (env['TARGET_ISA'], config))
201955SN/A    for d in dirs:
2021817SN/A        test_builder(env, d)
2031154SN/Aos.chdir(cwd)
2041840SN/A