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