SConscript revision 3020:a33d8709d348
110260SAndrew.Bardsley@arm.com# -*- mode:python -*-
210260SAndrew.Bardsley@arm.com
310260SAndrew.Bardsley@arm.com# Copyright (c) 2004-2006 The Regents of The University of Michigan
410260SAndrew.Bardsley@arm.com# All rights reserved.
510260SAndrew.Bardsley@arm.com#
610260SAndrew.Bardsley@arm.com# Redistribution and use in source and binary forms, with or without
710260SAndrew.Bardsley@arm.com# modification, are permitted provided that the following conditions are
810260SAndrew.Bardsley@arm.com# met: redistributions of source code must retain the above copyright
910260SAndrew.Bardsley@arm.com# notice, this list of conditions and the following disclaimer;
1010260SAndrew.Bardsley@arm.com# redistributions in binary form must reproduce the above copyright
1110260SAndrew.Bardsley@arm.com# notice, this list of conditions and the following disclaimer in the
1210260SAndrew.Bardsley@arm.com# documentation and/or other materials provided with the distribution;
1310315Snilay@cs.wisc.edu# neither the name of the copyright holders nor the names of its
1410260SAndrew.Bardsley@arm.com# contributors may be used to endorse or promote products derived from
1510260SAndrew.Bardsley@arm.com# this software without specific prior written permission.
1610260SAndrew.Bardsley@arm.com#
1710260SAndrew.Bardsley@arm.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1810315Snilay@cs.wisc.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1910260SAndrew.Bardsley@arm.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2010260SAndrew.Bardsley@arm.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2110315Snilay@cs.wisc.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2210315Snilay@cs.wisc.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2310260SAndrew.Bardsley@arm.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2410260SAndrew.Bardsley@arm.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2510260SAndrew.Bardsley@arm.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2610260SAndrew.Bardsley@arm.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2710260SAndrew.Bardsley@arm.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2810260SAndrew.Bardsley@arm.com#
2910315Snilay@cs.wisc.edu# Authors: Steve Reinhardt
3010315Snilay@cs.wisc.edu#          Kevin Lim
3110260SAndrew.Bardsley@arm.com
3210260SAndrew.Bardsley@arm.comimport os
3310260SAndrew.Bardsley@arm.comimport sys
3410260SAndrew.Bardsley@arm.comimport glob
3510260SAndrew.Bardsley@arm.comfrom SCons.Script.SConscript import SConsEnvironment
3610260SAndrew.Bardsley@arm.com
3710260SAndrew.Bardsley@arm.comImport('env')
3810260SAndrew.Bardsley@arm.com
3910260SAndrew.Bardsley@arm.comenv['DIFFOUT'] = File('diff-out')
4010260SAndrew.Bardsley@arm.com
4110260SAndrew.Bardsley@arm.com# Dict that accumulates lists of tests by category (quick, medium, long)
4210260SAndrew.Bardsley@arm.comenv.Tests = {}
4310260SAndrew.Bardsley@arm.com
4410260SAndrew.Bardsley@arm.comdef contents(node):
4510260SAndrew.Bardsley@arm.com    return file(str(node)).read()
4610260SAndrew.Bardsley@arm.com
4710260SAndrew.Bardsley@arm.comdef check_test(target, source, env):
4810260SAndrew.Bardsley@arm.com    """Check output from running test.
4910260SAndrew.Bardsley@arm.com
5010260SAndrew.Bardsley@arm.com    Targets are as follows:
5110260SAndrew.Bardsley@arm.com    target[0] : outdiff
5210260SAndrew.Bardsley@arm.com    target[1] : statsdiff
5310260SAndrew.Bardsley@arm.com    target[2] : status
5410260SAndrew.Bardsley@arm.com
5510260SAndrew.Bardsley@arm.com    """
5610260SAndrew.Bardsley@arm.com    # make sure target files are all gone
5710315Snilay@cs.wisc.edu    for t in target:
5810260SAndrew.Bardsley@arm.com        if os.path.exists(t.abspath):
5910315Snilay@cs.wisc.edu            Execute(Delete(t.abspath))
6010260SAndrew.Bardsley@arm.com    # Run diff on output & ref directories to find differences.
6110260SAndrew.Bardsley@arm.com    # Exclude m5stats.txt since we will use diff-out on that.
6210260SAndrew.Bardsley@arm.com    Execute(env.subst('diff -ubr ${SOURCES[0].dir} ${SOURCES[1].dir} ' +
6310260SAndrew.Bardsley@arm.com                      '-I "^command line:" ' +		# for stdout file
6410260SAndrew.Bardsley@arm.com                      '-I "^M5 compiled " ' +		# for stderr file
6510260SAndrew.Bardsley@arm.com                      '-I "^M5 started " ' +		# for stderr file
6610260SAndrew.Bardsley@arm.com                      '-I "^M5 executing on " ' +	# for stderr file
6710260SAndrew.Bardsley@arm.com                      '-I "^Simulation complete at" ' +	# for stderr file
6810260SAndrew.Bardsley@arm.com                      '-I "^Listening for" ' +		# for stderr file
6910260SAndrew.Bardsley@arm.com                      '--exclude=m5stats.txt --exclude=SCCS ' +
7010260SAndrew.Bardsley@arm.com                      '--exclude=${TARGETS[0].file} ' +
7110260SAndrew.Bardsley@arm.com                      '> ${TARGETS[0]}', target=target, source=source), None)
7210260SAndrew.Bardsley@arm.com    print "===== Output differences ====="
7310260SAndrew.Bardsley@arm.com    print contents(target[0])
7410260SAndrew.Bardsley@arm.com    # Run diff-out on m5stats.txt file
7510260SAndrew.Bardsley@arm.com    status = Execute(env.subst('$DIFFOUT $SOURCES > ${TARGETS[1]}',
7610260SAndrew.Bardsley@arm.com                               target=target, source=source),
7710260SAndrew.Bardsley@arm.com                     strfunction=None)
7810260SAndrew.Bardsley@arm.com    print "===== Statistics differences ====="
7910260SAndrew.Bardsley@arm.com    print contents(target[1])
8010260SAndrew.Bardsley@arm.com    # Generate status file contents based on exit status of diff-out
8110260SAndrew.Bardsley@arm.com    if status == 0:
8210260SAndrew.Bardsley@arm.com        status_str = "passed."
8310260SAndrew.Bardsley@arm.com    else:
8410260SAndrew.Bardsley@arm.com        status_str = "FAILED!"
8510260SAndrew.Bardsley@arm.com    f = file(str(target[2]), 'w')
8610260SAndrew.Bardsley@arm.com    print >>f, env.subst('${TARGETS[2].dir}', target=target, source=source), \
8710260SAndrew.Bardsley@arm.com          status_str
8810260SAndrew.Bardsley@arm.com    f.close()
8910260SAndrew.Bardsley@arm.com    # done
9010260SAndrew.Bardsley@arm.com    return 0
9110260SAndrew.Bardsley@arm.com
9210260SAndrew.Bardsley@arm.comdef check_test_string(target, source, env):
9310260SAndrew.Bardsley@arm.com    return env.subst("Comparing outputs in ${TARGETS[0].dir}.",
9410260SAndrew.Bardsley@arm.com                     target=target, source=source)
9510260SAndrew.Bardsley@arm.com
9610260SAndrew.Bardsley@arm.comtestAction = env.Action(check_test, check_test_string)
9710260SAndrew.Bardsley@arm.com
9810260SAndrew.Bardsley@arm.comdef print_test(target, source, env):
9910260SAndrew.Bardsley@arm.com    print '***** ' + contents(source[0])
10010260SAndrew.Bardsley@arm.com    return 0
10110260SAndrew.Bardsley@arm.com
10210260SAndrew.Bardsley@arm.comprintAction = env.Action(print_test, strfunction = None)
10310260SAndrew.Bardsley@arm.com
10410260SAndrew.Bardsley@arm.comdef update_test(target, source, env):
10510260SAndrew.Bardsley@arm.com    """Update reference test outputs.
10610260SAndrew.Bardsley@arm.com
10710260SAndrew.Bardsley@arm.com    Target is phony.  First two sources are the ref & new m5stats.txt
10810260SAndrew.Bardsley@arm.com    files, respectively.  We actually copy everything in the
10910260SAndrew.Bardsley@arm.com    respective directories except the status & diff output files.
11010260SAndrew.Bardsley@arm.com
11110260SAndrew.Bardsley@arm.com    """
11210260SAndrew.Bardsley@arm.com    dest_dir = str(source[0].get_dir())
11310260SAndrew.Bardsley@arm.com    src_dir = str(source[1].get_dir())
11410260SAndrew.Bardsley@arm.com    dest_files = os.listdir(dest_dir)
11510260SAndrew.Bardsley@arm.com    src_files = os.listdir(src_dir)
11610260SAndrew.Bardsley@arm.com    # Exclude status & diff outputs
11710260SAndrew.Bardsley@arm.com    for f in ('outdiff', 'statsdiff', 'status'):
11810315Snilay@cs.wisc.edu        if f in src_files:
11910260SAndrew.Bardsley@arm.com            src_files.remove(f)
12010260SAndrew.Bardsley@arm.com    for f in src_files:
12110260SAndrew.Bardsley@arm.com        if f in dest_files:
12210260SAndrew.Bardsley@arm.com            print "  Replacing file", f
12310260SAndrew.Bardsley@arm.com            dest_files.remove(f)
12410260SAndrew.Bardsley@arm.com        else:
12510260SAndrew.Bardsley@arm.com            print "  Creating new file", f
12610260SAndrew.Bardsley@arm.com        copyAction = Copy(os.path.join(dest_dir, f), os.path.join(src_dir, f))
12710260SAndrew.Bardsley@arm.com        copyAction.strfunction = None
12810260SAndrew.Bardsley@arm.com        Execute(copyAction)
12910260SAndrew.Bardsley@arm.com    # warn about any files in dest not overwritten (other than SCCS dir)
13010260SAndrew.Bardsley@arm.com    if 'SCCS' in dest_files:
13110260SAndrew.Bardsley@arm.com        dest_files.remove('SCCS')
13210260SAndrew.Bardsley@arm.com    if dest_files:
13310260SAndrew.Bardsley@arm.com        print "Warning: file(s) in", dest_dir, "not updated:",
13410260SAndrew.Bardsley@arm.com        print ', '.join(dest_files)
13510260SAndrew.Bardsley@arm.com    return 0
13610260SAndrew.Bardsley@arm.com
13710260SAndrew.Bardsley@arm.comdef update_test_string(target, source, env):
13810260SAndrew.Bardsley@arm.com    return env.subst("Updating ${SOURCES[0].dir} from ${SOURCES[1].dir}",
13910260SAndrew.Bardsley@arm.com                     target=target, source=source)
14010260SAndrew.Bardsley@arm.com
14110260SAndrew.Bardsley@arm.comupdateAction = env.Action(update_test, update_test_string)
14210260SAndrew.Bardsley@arm.com
14310260SAndrew.Bardsley@arm.comdef test_builder(env, ref_dir):
14410260SAndrew.Bardsley@arm.com    """Define a test."""
14510260SAndrew.Bardsley@arm.com
14610260SAndrew.Bardsley@arm.com    (category, name, _ref, isa, opsys, config) = ref_dir.split('/')
14710260SAndrew.Bardsley@arm.com    assert(_ref == 'ref')
14810260SAndrew.Bardsley@arm.com
14910260SAndrew.Bardsley@arm.com    # target path (where test output goes) is the same except without
15010260SAndrew.Bardsley@arm.com    # the 'ref' component
15110260SAndrew.Bardsley@arm.com    tgt_dir = os.path.join(category, name, isa, opsys, config)
15210260SAndrew.Bardsley@arm.com
15310260SAndrew.Bardsley@arm.com    # prepend file name with tgt_dir
15410260SAndrew.Bardsley@arm.com    def tgt(f):
15510260SAndrew.Bardsley@arm.com        return os.path.join(tgt_dir, f)
15610260SAndrew.Bardsley@arm.com
15710260SAndrew.Bardsley@arm.com    ref_stats = os.path.join(ref_dir, 'm5stats.txt')
15810260SAndrew.Bardsley@arm.com    new_stats = tgt('m5stats.txt')
15910260SAndrew.Bardsley@arm.com    status_file = tgt('status')
16010260SAndrew.Bardsley@arm.com
16110260SAndrew.Bardsley@arm.com    # Base command for running test.  We mess around with indirectly
16210260SAndrew.Bardsley@arm.com    # referring to files via SOURCES and TARGETS so that scons can
16310260SAndrew.Bardsley@arm.com    # mess with paths all it wants to and we still get the right
16410260SAndrew.Bardsley@arm.com    # files.
16510260SAndrew.Bardsley@arm.com    base_cmd = '${SOURCES[0]} -d $TARGET.dir ${SOURCES[1]} %s' % tgt_dir
16610260SAndrew.Bardsley@arm.com    # stdout and stderr files
16710260SAndrew.Bardsley@arm.com    cmd_stdout = '${TARGETS[0]}'
16810260SAndrew.Bardsley@arm.com    cmd_stderr = '${TARGETS[1]}'
16910260SAndrew.Bardsley@arm.com
17010260SAndrew.Bardsley@arm.com    # Prefix test run with batch job submission command if appropriate.
17110260SAndrew.Bardsley@arm.com    # Output redirection is also different for batch runs.
17210260SAndrew.Bardsley@arm.com    # Batch command also supports timeout arg (in seconds, not minutes).
17310260SAndrew.Bardsley@arm.com    if env['BATCH']:
17410260SAndrew.Bardsley@arm.com        cmd = [env['BATCH_CMD'], '-t', str(timeout * 60),
17510260SAndrew.Bardsley@arm.com               '-o', cmd_stdout, '-e', cmd_stderr, base_cmd]
17610260SAndrew.Bardsley@arm.com    else:
17710260SAndrew.Bardsley@arm.com        cmd = [base_cmd, '>', cmd_stdout, '2>', cmd_stderr]
17810260SAndrew.Bardsley@arm.com            
17910260SAndrew.Bardsley@arm.com    env.Command([tgt('stdout'), tgt('stderr'), new_stats],
18010260SAndrew.Bardsley@arm.com                [env.M5Binary, 'run.py'], ' '.join(cmd))
18110260SAndrew.Bardsley@arm.com
18210260SAndrew.Bardsley@arm.com    # order of targets is important... see check_test
18310260SAndrew.Bardsley@arm.com    env.Command([tgt('outdiff'), tgt('statsdiff'), status_file],
18410260SAndrew.Bardsley@arm.com                [ref_stats, new_stats],
18510260SAndrew.Bardsley@arm.com                testAction)
18610260SAndrew.Bardsley@arm.com
18710260SAndrew.Bardsley@arm.com    # phony target to echo status
18810260SAndrew.Bardsley@arm.com    if env['update_ref']:
18910260SAndrew.Bardsley@arm.com        p = env.Command(tgt('_update'),
19010260SAndrew.Bardsley@arm.com                        [ref_stats, new_stats, status_file],
19110260SAndrew.Bardsley@arm.com                        updateAction)
19210315Snilay@cs.wisc.edu    else:
19310260SAndrew.Bardsley@arm.com        p = env.Command(tgt('_print'), [status_file], printAction)
19410260SAndrew.Bardsley@arm.com
19510260SAndrew.Bardsley@arm.com    env.AlwaysBuild(p)
19610260SAndrew.Bardsley@arm.com
19710260SAndrew.Bardsley@arm.com
19810260SAndrew.Bardsley@arm.com# Figure out applicable configs based on build type
19910260SAndrew.Bardsley@arm.comconfigs = []
20010260SAndrew.Bardsley@arm.comif env['FULL_SYSTEM']:
20110260SAndrew.Bardsley@arm.com    if env['TARGET_ISA'] == 'alpha':
20210260SAndrew.Bardsley@arm.com        if not env['ALPHA_TLASER']:
20310260SAndrew.Bardsley@arm.com            configs += ['tsunami-simple-atomic',
20410260SAndrew.Bardsley@arm.com                        'tsunami-simple-timing',
20510260SAndrew.Bardsley@arm.com                        'tsunami-simple-atomic-dual',
20610260SAndrew.Bardsley@arm.com                        'tsunami-simple-timing-dual']
20710260SAndrew.Bardsley@arm.comelse:
20810260SAndrew.Bardsley@arm.com    configs += ['simple-atomic', 'simple-timing']
20910260SAndrew.Bardsley@arm.com
21010260SAndrew.Bardsley@arm.comcwd = os.getcwd()
21110260SAndrew.Bardsley@arm.comos.chdir(str(Dir('.').srcdir))
21210260SAndrew.Bardsley@arm.comfor config in configs:
21310260SAndrew.Bardsley@arm.com    dirs = glob.glob('*/*/ref/%s/*/%s' % (env['TARGET_ISA'], config))
21410260SAndrew.Bardsley@arm.com    for d in dirs:
21510260SAndrew.Bardsley@arm.com        test_builder(env, d)
21610260SAndrew.Bardsley@arm.comos.chdir(cwd)
21710260SAndrew.Bardsley@arm.com