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