scons: Make src/systemc/tests/SConscript python 3 compatible.
[gem5.git] / src / systemc / tests / SConscript
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 from __future__ import print_function
27
28 Import('*')
29
30 if env['USE_SYSTEMC'] and GetOption('with_systemc_tests'):
31
32 from gem5_scons import Transform
33
34 import os
35 import os.path
36 import json
37
38 src = str(Dir('.').srcdir)
39
40 class SystemCTest(object):
41 def __init__(self, dirname, name):
42 self.name = name
43 self.reldir = os.path.relpath(dirname, src)
44 self.target = os.path.join(self.reldir, name)
45 self.sources = []
46 self.deps = []
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 'deps' : self.deps
63 }
64
65 test_dir = Dir('.')
66 class SystemCTestBin(Executable):
67 def __init__(self, test):
68 super(SystemCTestBin, self).__init__(test.target, *test.sources)
69 self.reldir = test.reldir
70 self.test_deps = test.deps
71
72 @classmethod
73 def declare_all(cls, env):
74 env = env.Clone()
75
76 # Turn off extra warnings and Werror for the tests.
77 to_remove = ['-Wall', '-Wundef', '-Wextra', '-Werror']
78 env['CCFLAGS'] = \
79 list(filter(lambda f: f not in to_remove, env['CCFLAGS']))
80
81 env.Append(CPPPATH=test_dir.Dir('include'))
82
83 shared_lib_path = env['SHARED_LIB'][0].abspath
84 sl_dir, sl_base = os.path.split(shared_lib_path)
85 env.Append(LIBPATH=[sl_dir], LIBS=[sl_base])
86
87 super(SystemCTestBin, cls).declare_all(env)
88
89 def declare(self, env):
90 env = env.Clone()
91 sources = list(self.sources)
92 for f in self.filters:
93 sources += Source.all.apply_filter(f)
94 objs = self.srcs_to_objs(env, sources)
95 objs = objs + env['MAIN_OBJS']
96 relpath = os.path.relpath(
97 env['SHARED_LIB'][0].dir.abspath,
98 self.path(env).dir.abspath)
99 env.Append(LINKFLAGS=Split('-z origin'))
100 env.Append(RPATH=[
101 env.Literal(os.path.join('\\$$ORIGIN', relpath))])
102 test_bin = super(SystemCTestBin, self).declare(env, objs)
103 test_dir = self.dir.Dir(self.reldir)
104 for dep in self.test_deps:
105 env.Depends(test_bin, test_dir.File(dep))
106 return test_bin
107
108 tests = []
109 def new_test(dirname, name):
110 test = SystemCTest(dirname, name)
111 tests.append(test)
112 return test
113
114
115 def scan_dir_for_tests(subdir):
116 subdir_src = Dir('.').srcdir.Dir(subdir)
117 for root, dirs, files in os.walk(str(subdir_src)):
118 # If there's a 'DONTRUN' file in this directory, skip it and any
119 # child directories.
120 if 'DONTRUN' in files:
121 del dirs[:]
122 return
123
124 endswith = lambda sfx: list(filter(
125 lambda n: n.endswith(sfx), files))
126
127 cpps = endswith('.cpp')
128 if not cpps:
129 continue
130
131 def get_entries(fname):
132 with open(os.path.join(root, fname)) as content:
133 lines = content.readlines
134 # Get rid of leading and trailing whitespace.
135 lines = map(lambda x: x.strip(), content.readlines())
136 # Get rid of blank lines.
137 lines = list(filter(lambda x: x, lines))
138 return lines
139
140 # If there's only one source file, then that files name is the test
141 # name, and it's the source for that test.
142 if len(cpps) == 1:
143 cpp = cpps[0]
144
145 test = new_test(root, os.path.splitext(cpp)[0])
146 test.add_source(cpp)
147
148 # Otherwise, expect there to be a file that ends in .f. That files
149 # name is the test name, and it will list the source files with
150 # one preceeding path component.
151 else:
152 fs = endswith('.f')
153 if len(fs) != 1:
154 print("In %s, expected 1 *.f file, but found %d.",
155 root, len(fs))
156 for f in fs:
157 print(os.path.join(root, f))
158 return
159 f = fs[0]
160
161 test = new_test(root, os.path.splitext(f)[0])
162 # Add all the sources to this test.
163 test.add_sources(get_entries(f))
164
165 if 'COMPILE' in files:
166 test.compile_only = True
167
168 if 'DEPS' in files:
169 test.deps = get_entries('DEPS')
170
171 scan_dir_for_tests('systemc')
172 scan_dir_for_tests('tlm')
173
174
175 def build_tests_json(target, source, env):
176 data = { test.target : test.properties() for test in tests }
177 with open(str(target[0]), "w") as tests_json:
178 json.dump(data, tests_json)
179
180 AlwaysBuild(env.Command(File('tests.json'), None,
181 MakeAction(build_tests_json, Transform("TESTJSON"))))
182
183
184 for test in tests:
185 SystemCTestBin(test)