SConscript revision 12922:a4f51f3405ac
1# Copyright 2018 Google, Inc.
2#
3# Redistribution and use in source and binary forms, with or without
4# modification, are permitted provided that the following conditions are
5# met: redistributions of source code must retain the above copyright
6# notice, this list of conditions and the following disclaimer;
7# redistributions in binary form must reproduce the above copyright
8# notice, this list of conditions and the following disclaimer in the
9# documentation and/or other materials provided with the distribution;
10# 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    test_dir = Dir('.')
66    class SystemCTestBin(Executable):
67        def __init__(self, test):
68            super(SystemCTestBin, self).__init__(test.target, *test.sources)
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=test_dir.Dir('include'))
80            env.Append(CPPPATH=ext_dir)
81
82            super(SystemCTestBin, cls).declare_all(env)
83
84        def declare(self, env):
85            sources = list(self.sources)
86            for f in self.filters:
87                sources = Source.all.apply_filter(f)
88            objs = self.srcs_to_objs(env, sources)
89            objs = objs + env['SHARED_LIB'] + env['MAIN_OBJS']
90            return super(SystemCTestBin, self).declare(env, objs)
91
92    tests = []
93    def new_test(dirname, name):
94        test = SystemCTest(dirname, name)
95        tests.append(test)
96        return test
97
98
99    def scan_dir_for_tests(subdir):
100        def visitor(arg, dirname, names):
101            # If there's a 'DONTRUN' file in this directory, skip it and any
102            # child directories.
103            if 'DONTRUN' in names:
104                del names[:]
105                return
106
107            endswith = lambda sfx: filter(lambda n: n.endswith(sfx), names)
108
109            cpps = endswith('.cpp')
110            if not cpps:
111                return
112
113            # If there's only one source file, then that files name is the test
114            # name, and it's the source for that test.
115            if len(cpps) == 1:
116                cpp = cpps[0]
117
118                test = new_test(dirname, os.path.splitext(cpp)[0])
119                test.add_source(cpp)
120
121            # Otherwise, expect there to be a file that ends in .f. That files
122            # name is the test name, and it will list the source files with
123            # one preceeding path component.
124            else:
125                fs = endswith('.f')
126                if len(fs) != 1:
127                    print("In %s, expected 1 *.f file, but found %d.",
128                          dirname, len(fs))
129                    for f in fs:
130                        print(os.path.join(dirname, f))
131                    return
132                f = fs[0]
133
134                test = new_test(dirname, os.path.splitext(f)[0])
135                with open(os.path.join(dirname, f)) as content:
136                    lines = content.readlines
137                    # Get rid of leading and trailing whitespace.
138                    lines = map(lambda x: x.strip(), content.readlines())
139                    # Get rid of blank lines.
140                    lines = filter(lambda x: x, lines)
141                    # Add all the sources to this test.
142                    test.add_sources(lines)
143
144            if 'COMPILE' in names:
145                test.compile_only = True
146
147        subdir_src = Dir('.').srcdir.Dir(subdir)
148        os.path.walk(str(subdir_src), visitor, None)
149
150    scan_dir_for_tests('systemc')
151
152
153    def build_tests_json(target, source, env):
154        data = { test.target : test.properties() for test in tests }
155        with open(str(target[0]), "w") as tests_json:
156            json.dump(data, tests_json)
157
158    AlwaysBuild(env.Command(File('tests.json'), None,
159                MakeAction(build_tests_json, Transform("TESTJSON"))))
160
161
162    for test in tests:
163        SystemCTestBin(test)
164