SConscript revision 12866
14479Sbinkertn@umich.edu# Copyright 2018 Google, Inc.
24479Sbinkertn@umich.edu#
34479Sbinkertn@umich.edu# Redistribution and use in source and binary forms, with or without
44479Sbinkertn@umich.edu# modification, are permitted provided that the following conditions are
54479Sbinkertn@umich.edu# met: redistributions of source code must retain the above copyright
64479Sbinkertn@umich.edu# notice, this list of conditions and the following disclaimer;
74479Sbinkertn@umich.edu# redistributions in binary form must reproduce the above copyright
84479Sbinkertn@umich.edu# notice, this list of conditions and the following disclaimer in the
94479Sbinkertn@umich.edu# documentation and/or other materials provided with the distribution;
104479Sbinkertn@umich.edu# neither the name of the copyright holders nor the names of its
11# contributors may be used to endorse or promote products derived from
12# this software without specific prior written permission.
13#
14# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25#
26# Authors: Gabe Black
27
28from __future__ import print_function
29
30Import('*')
31
32if env['USE_SYSTEMC']:
33
34    from gem5_scons import Transform
35
36    import os.path
37    import json
38
39    src = str(Dir('.').srcdir)
40
41    class SystemCTest(object):
42        def __init__(self, dirname, name):
43            self.name = name
44            self.reldir = os.path.relpath(dirname, src)
45            self.target = os.path.join(self.reldir, name)
46            self.sources = []
47
48            self.compile_only = False
49
50        def add_source(self, source):
51            self.sources.append(os.path.join(self.reldir, source))
52
53        def add_sources(self, sources):
54            for source in sources:
55                self.sources.append(os.path.join(self.reldir, '..', source))
56
57        def properties(self):
58            return {
59                'name' : self.name,
60                'path' : self.reldir,
61                'compile_only' : self.compile_only
62            }
63
64    ext_dir = Dir('..').Dir('ext')
65    class SystemCTestBin(UnitTest):
66        def __init__(self, test):
67            super(SystemCTestBin, self).__init__(
68                    test.target, *test.sources, main=True)
69
70        @classmethod
71        def declare_all(cls, env):
72            env = env.Clone()
73
74            # Turn off extra warnings and Werror for the tests.
75            to_remove = ['-Wall', '-Wundef', '-Wextra', '-Werror']
76            env['CCFLAGS'] = \
77                filter(lambda f: f not in to_remove, env['CCFLAGS'])
78
79            env.Append(CPPPATH=ext_dir)
80
81            super(SystemCTestBin, cls).declare_all(env)
82
83    tests = []
84    def new_test(dirname, name):
85        test = SystemCTest(dirname, name)
86        tests.append(test)
87        return test
88
89
90    def scan_dir_for_tests(subdir):
91        def visitor(arg, dirname, names):
92            # If there's a 'DONTRUN' file in this directory, skip it and any
93            # child directories.
94            if 'DONTRUN' in names:
95                del names[:]
96                return
97
98            endswith = lambda sfx: filter(lambda n: n.endswith(sfx), names)
99
100            cpps = endswith('.cpp')
101            if not cpps:
102                return
103
104            # If there's only one source file, then that files name is the test
105            # name, and it's the source for that test.
106            if len(cpps) == 1:
107                cpp = cpps[0]
108
109                test = new_test(dirname, os.path.splitext(cpp)[0])
110                test.add_source(cpp)
111
112            # Otherwise, expect there to be a file that ends in .f. That files
113            # name is the test name, and it will list the source files with
114            # one preceeding path component.
115            else:
116                fs = endswith('.f')
117                if len(fs) != 1:
118                    print("In %s, expected 1 *.f file, but found %d.",
119                          dirname, len(fs))
120                    for f in fs:
121                        print(os.path.join(dirname, f))
122                    return
123                f = fs[0]
124
125                test = new_test(dirname, os.path.splitext(f)[0])
126                with open(os.path.join(dirname, f)) as content:
127                    lines = content.readlines
128                    # Get rid of leading and trailing whitespace.
129                    lines = map(lambda x: x.strip(), content.readlines())
130                    # Get rid of blank lines.
131                    lines = filter(lambda x: x, lines)
132                    # Add all the sources to this test.
133                    test.add_sources(lines)
134
135            if 'COMPILE' in names:
136                test.compile_only = True
137
138        subdir_src = Dir('.').srcdir.Dir(subdir)
139        os.path.walk(str(subdir_src), visitor, None)
140
141    scan_dir_for_tests('systemc')
142
143
144    def build_tests_json(target, source, env):
145        data = { test.target : test.properties() for test in tests }
146        with open(str(target[0]), "w") as tests_json:
147            json.dump(data, tests_json)
148
149    AlwaysBuild(env.Command(File('tests.json'), None,
150                MakeAction(build_tests_json, Transform("TESTJSON"))))
151
152
153    for test in tests:
154        SystemCTestBin(test)
155