verify.py revision 12897:9f8c25581024
1#!/usr/bin/env python2
2#
3# Copyright 2018 Google, Inc.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met: redistributions of source code must retain the above copyright
8# notice, this list of conditions and the following disclaimer;
9# redistributions in binary form must reproduce the above copyright
10# notice, this list of conditions and the following disclaimer in the
11# documentation and/or other materials provided with the distribution;
12# neither the name of the copyright holders nor the names of its
13# contributors may be used to endorse or promote products derived from
14# this software without specific prior written permission.
15#
16# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27#
28# Authors: Gabe Black
29
30from __future__ import print_function
31
32import argparse
33import functools
34import inspect
35import itertools
36import json
37import logging
38import os
39import subprocess
40import sys
41
42script_path = os.path.abspath(inspect.getfile(inspect.currentframe()))
43script_dir = os.path.dirname(script_path)
44config_path = os.path.join(script_dir, 'config.py')
45
46systemc_rel_path = 'systemc'
47tests_rel_path = os.path.join(systemc_rel_path, 'tests')
48json_rel_path = os.path.join(tests_rel_path, 'tests.json')
49
50
51
52logging.basicConfig(level=logging.INFO)
53
54def scons(*args):
55    args = ['scons'] + list(args)
56    subprocess.check_call(args)
57
58
59
60class Test(object):
61    def __init__(self, target, suffix, build_dir, props):
62        self.target = target
63        self.suffix = suffix
64        self.build_dir = build_dir
65
66        for key, val in props.iteritems():
67            setattr(self, key, val)
68
69    def dir(self):
70        return os.path.join(self.build_dir, tests_rel_path, self.path)
71
72    def src_dir(self):
73        return os.path.join(script_dir, self.path)
74
75    def golden_dir(self):
76        return os.path.join(self.src_dir(), 'golden')
77
78    def bin(self):
79        return '.'.join([self.name, self.suffix])
80
81    def full_path(self):
82        return os.path.join(self.dir(), self.bin())
83
84    def m5out_dir(self):
85        return os.path.join(self.dir(), 'm5out.' + self.suffix)
86
87
88
89test_phase_classes = {}
90
91class TestPhaseMeta(type):
92    def __init__(cls, name, bases, d):
93        if not d.pop('abstract', False):
94            test_phase_classes[d['name']] = cls
95
96        super(TestPhaseMeta, cls).__init__(name, bases, d)
97
98class TestPhaseBase(object):
99    __metaclass__ = TestPhaseMeta
100    abstract = True
101
102    def __init__(self, main_args, *args):
103        self.main_args = main_args
104        self.args = args
105
106    def __lt__(self, other):
107        return self.number < other.number
108
109class CompilePhase(TestPhaseBase):
110    name = 'compile'
111    number = 1
112
113    def run(self, tests):
114        targets = list([test.full_path() for test in tests])
115        scons_args = list(self.args) + targets
116        scons(*scons_args)
117
118class RunPhase(TestPhaseBase):
119    name = 'execute'
120    number = 2
121
122    def run(self, tests):
123        for test in tests:
124            if test.compile_only:
125                continue
126            args = [
127                test.full_path(),
128                '-red', test.m5out_dir(),
129                '--listener-mode=off',
130                config_path
131            ]
132            subprocess.check_call(args)
133
134class VerifyPhase(TestPhaseBase):
135    name = 'verify'
136    number = 3
137
138    def run(self, tests):
139        for test in tests:
140            if test.compile_only:
141                continue
142            logging.info("Would verify %s", test.m5out_dir())
143
144
145
146parser = argparse.ArgumentParser(description='SystemC test utility')
147
148parser.add_argument('build_dir', metavar='BUILD_DIR',
149                    help='The build directory (ie. build/ARM).')
150
151parser.add_argument('--update-json', action='store_true',
152                    help='Update the json manifest of tests.')
153
154parser.add_argument('--flavor', choices=['debug', 'opt', 'fast'],
155                    default='opt',
156                    help='Flavor of binary to test.')
157
158parser.add_argument('--list', action='store_true',
159                    help='List the available tests')
160
161parser.add_argument('--filter', default='True',
162                    help='Python expression which filters tests based on '
163                    'their properties')
164
165def collect_phases(args):
166    phase_groups = [list(g) for k, g in
167                    itertools.groupby(args, lambda x: x != '--phase') if k]
168    main_args = parser.parse_args(phase_groups[0][1:])
169    phases = []
170    names = []
171    for group in phase_groups[1:]:
172        name = group[0]
173        if name in names:
174            raise RuntimeException('Phase %s specified more than once' % name)
175        phase = test_phase_classes[name]
176        phases.append(phase(main_args, *group[1:]))
177    phases.sort()
178    return main_args, phases
179
180main_args, phases = collect_phases(sys.argv)
181
182if len(phases) == 0:
183    phases = [
184        CompilePhase(main_args),
185        RunPhase(main_args),
186        VerifyPhase(main_args)
187    ]
188
189
190
191json_path = os.path.join(main_args.build_dir, json_rel_path)
192
193if main_args.update_json:
194    scons(os.path.join(json_path))
195
196with open(json_path) as f:
197    test_data = json.load(f)
198
199    if main_args.list:
200        for target, props in test_data.iteritems():
201            if not eval(main_args.filter, dict(props)):
202                continue
203            print('%s.%s' % (target, main_args.flavor))
204            for key, val in props.iteritems():
205                print('    %s: %s' % (key, val))
206    else:
207        tests_to_run = []
208        for target, props in test_data.iteritems():
209            if not eval(main_args.filter, props):
210                continue
211            tests_to_run.append(Test(target, main_args.flavor,
212                                     main_args.build_dir, props))
213
214        for phase in phases:
215            phase.run(tests_to_run)
216