SConscript revision 13656
14826Ssaidi@eecs.umich.edu# Copyright 2018 Google, Inc.
24826Ssaidi@eecs.umich.edu#
34826Ssaidi@eecs.umich.edu# Redistribution and use in source and binary forms, with or without
44826Ssaidi@eecs.umich.edu# modification, are permitted provided that the following conditions are
54826Ssaidi@eecs.umich.edu# met: redistributions of source code must retain the above copyright
64826Ssaidi@eecs.umich.edu# notice, this list of conditions and the following disclaimer;
74826Ssaidi@eecs.umich.edu# redistributions in binary form must reproduce the above copyright
84826Ssaidi@eecs.umich.edu# notice, this list of conditions and the following disclaimer in the
94826Ssaidi@eecs.umich.edu# documentation and/or other materials provided with the distribution;
104826Ssaidi@eecs.umich.edu# neither the name of the copyright holders nor the names of its
114826Ssaidi@eecs.umich.edu# contributors may be used to endorse or promote products derived from
124826Ssaidi@eecs.umich.edu# this software without specific prior written permission.
134826Ssaidi@eecs.umich.edu#
144826Ssaidi@eecs.umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
154826Ssaidi@eecs.umich.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
164826Ssaidi@eecs.umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
174826Ssaidi@eecs.umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
184826Ssaidi@eecs.umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
194826Ssaidi@eecs.umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
204826Ssaidi@eecs.umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
214826Ssaidi@eecs.umich.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
224826Ssaidi@eecs.umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
234826Ssaidi@eecs.umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
244826Ssaidi@eecs.umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
254826Ssaidi@eecs.umich.edu#
264826Ssaidi@eecs.umich.edu# Authors: Gabe Black
274826Ssaidi@eecs.umich.edu
284826Ssaidi@eecs.umich.edufrom __future__ import print_function
294826Ssaidi@eecs.umich.edu
304826Ssaidi@eecs.umich.eduImport('*')
314826Ssaidi@eecs.umich.edu
324826Ssaidi@eecs.umich.eduif env['USE_SYSTEMC']:
334826Ssaidi@eecs.umich.edu
348706Sandreas.hansson@arm.com    from gem5_scons import Transform
358780Sgblack@eecs.umich.edu
364826Ssaidi@eecs.umich.edu    import os.path
375569Snate@binkert.org    import json
384826Ssaidi@eecs.umich.edu
395569Snate@binkert.org    src = str(Dir('.').srcdir)
407707Sgblack@eecs.umich.edu
414826Ssaidi@eecs.umich.edu    class SystemCTest(object):
428806Sgblack@eecs.umich.edu        def __init__(self, dirname, name):
438780Sgblack@eecs.umich.edu            self.name = name
448780Sgblack@eecs.umich.edu            self.reldir = os.path.relpath(dirname, src)
454826Ssaidi@eecs.umich.edu            self.target = os.path.join(self.reldir, name)
468806Sgblack@eecs.umich.edu            self.sources = []
478806Sgblack@eecs.umich.edu            self.deps = []
488806Sgblack@eecs.umich.edu
498806Sgblack@eecs.umich.edu            self.compile_only = False
508806Sgblack@eecs.umich.edu
518806Sgblack@eecs.umich.edu        def add_source(self, source):
528806Sgblack@eecs.umich.edu            self.sources.append(os.path.join(self.reldir, source))
538806Sgblack@eecs.umich.edu
548806Sgblack@eecs.umich.edu        def add_sources(self, sources):
558806Sgblack@eecs.umich.edu            for source in sources:
568806Sgblack@eecs.umich.edu                self.sources.append(os.path.join(self.reldir, '..', source))
578806Sgblack@eecs.umich.edu
588806Sgblack@eecs.umich.edu        def properties(self):
598806Sgblack@eecs.umich.edu            return {
604826Ssaidi@eecs.umich.edu                'name' : self.name,
614826Ssaidi@eecs.umich.edu                'path' : self.reldir,
626329Sgblack@eecs.umich.edu                'compile_only' : self.compile_only,
636329Sgblack@eecs.umich.edu                'deps' : self.deps
646329Sgblack@eecs.umich.edu            }
656329Sgblack@eecs.umich.edu
666329Sgblack@eecs.umich.edu    test_dir = Dir('.')
676329Sgblack@eecs.umich.edu    class SystemCTestBin(Executable):
686329Sgblack@eecs.umich.edu        def __init__(self, test):
696329Sgblack@eecs.umich.edu            super(SystemCTestBin, self).__init__(test.target, *test.sources)
706329Sgblack@eecs.umich.edu            self.reldir = test.reldir
716329Sgblack@eecs.umich.edu            self.test_deps = test.deps
726329Sgblack@eecs.umich.edu
736329Sgblack@eecs.umich.edu        @classmethod
746329Sgblack@eecs.umich.edu        def declare_all(cls, env):
756329Sgblack@eecs.umich.edu            env = env.Clone()
766329Sgblack@eecs.umich.edu
777720Sgblack@eecs.umich.edu            # Turn off extra warnings and Werror for the tests.
786329Sgblack@eecs.umich.edu            to_remove = ['-Wall', '-Wundef', '-Wextra', '-Werror']
796329Sgblack@eecs.umich.edu            env['CCFLAGS'] = \
806329Sgblack@eecs.umich.edu                filter(lambda f: f not in to_remove, env['CCFLAGS'])
816329Sgblack@eecs.umich.edu
826329Sgblack@eecs.umich.edu            env.Append(CPPPATH=test_dir.Dir('include'))
836329Sgblack@eecs.umich.edu
846329Sgblack@eecs.umich.edu            shared_lib_path = env['SHARED_LIB'][0].abspath
856329Sgblack@eecs.umich.edu            sl_dir, sl_base = os.path.split(shared_lib_path)
866329Sgblack@eecs.umich.edu            env.Append(LIBPATH=[sl_dir], LIBS=[sl_base])
876329Sgblack@eecs.umich.edu
886329Sgblack@eecs.umich.edu            super(SystemCTestBin, cls).declare_all(env)
896329Sgblack@eecs.umich.edu
906329Sgblack@eecs.umich.edu        def declare(self, env):
916329Sgblack@eecs.umich.edu            env = env.Clone()
926329Sgblack@eecs.umich.edu            sources = list(self.sources)
936329Sgblack@eecs.umich.edu            for f in self.filters:
946329Sgblack@eecs.umich.edu                sources += Source.all.apply_filter(f)
957693SAli.Saidi@ARM.com            objs = self.srcs_to_objs(env, sources)
967693SAli.Saidi@ARM.com            objs = objs + env['MAIN_OBJS']
977693SAli.Saidi@ARM.com            relpath = os.path.relpath(
987720Sgblack@eecs.umich.edu                    env['SHARED_LIB'][0].dir.abspath,
997720Sgblack@eecs.umich.edu                    self.path(env).dir.abspath)
1007720Sgblack@eecs.umich.edu            env.Append(LINKFLAGS=Split('-z origin'))
1017693SAli.Saidi@ARM.com            env.Append(RPATH=[
1027693SAli.Saidi@ARM.com                    env.Literal(os.path.join('\\$$ORIGIN', relpath))])
1037693SAli.Saidi@ARM.com            test_bin = super(SystemCTestBin, self).declare(env, objs)
1044826Ssaidi@eecs.umich.edu            test_dir = self.dir.Dir(self.reldir)
1054826Ssaidi@eecs.umich.edu            for dep in self.test_deps:
106                env.Depends(test_bin, test_dir.File(dep))
107            return test_bin
108
109    tests = []
110    def new_test(dirname, name):
111        test = SystemCTest(dirname, name)
112        tests.append(test)
113        return test
114
115
116    def scan_dir_for_tests(subdir):
117        def visitor(arg, dirname, names):
118            # If there's a 'DONTRUN' file in this directory, skip it and any
119            # child directories.
120            if 'DONTRUN' in names:
121                del names[:]
122                return
123
124            endswith = lambda sfx: filter(lambda n: n.endswith(sfx), names)
125
126            cpps = endswith('.cpp')
127            if not cpps:
128                return
129
130            def get_entries(fname):
131                with open(os.path.join(dirname, fname)) as content:
132                    lines = content.readlines
133                    # Get rid of leading and trailing whitespace.
134                    lines = map(lambda x: x.strip(), content.readlines())
135                    # Get rid of blank lines.
136                    lines = filter(lambda x: x, lines)
137                    return lines
138
139            # If there's only one source file, then that files name is the test
140            # name, and it's the source for that test.
141            if len(cpps) == 1:
142                cpp = cpps[0]
143
144                test = new_test(dirname, os.path.splitext(cpp)[0])
145                test.add_source(cpp)
146
147            # Otherwise, expect there to be a file that ends in .f. That files
148            # name is the test name, and it will list the source files with
149            # one preceeding path component.
150            else:
151                fs = endswith('.f')
152                if len(fs) != 1:
153                    print("In %s, expected 1 *.f file, but found %d.",
154                          dirname, len(fs))
155                    for f in fs:
156                        print(os.path.join(dirname, f))
157                    return
158                f = fs[0]
159
160                test = new_test(dirname, os.path.splitext(f)[0])
161                # Add all the sources to this test.
162                test.add_sources(get_entries(f))
163
164            if 'COMPILE' in names:
165                test.compile_only = True
166
167            if 'DEPS' in names:
168                test.deps = get_entries('DEPS')
169
170        subdir_src = Dir('.').srcdir.Dir(subdir)
171        os.path.walk(str(subdir_src), visitor, None)
172
173    scan_dir_for_tests('systemc')
174    scan_dir_for_tests('tlm')
175
176
177    def build_tests_json(target, source, env):
178        data = { test.target : test.properties() for test in tests }
179        with open(str(target[0]), "w") as tests_json:
180            json.dump(data, tests_json)
181
182    AlwaysBuild(env.Command(File('tests.json'), None,
183                MakeAction(build_tests_json, Transform("TESTJSON"))))
184
185
186    for test in tests:
187        SystemCTestBin(test)
188