SConscript revision 12869:1ad10753e8c3
112855Sgabeblack@google.com# Copyright 2018 Google, Inc.
212855Sgabeblack@google.com#
312855Sgabeblack@google.com# Redistribution and use in source and binary forms, with or without
412855Sgabeblack@google.com# modification, are permitted provided that the following conditions are
512855Sgabeblack@google.com# met: redistributions of source code must retain the above copyright
612855Sgabeblack@google.com# notice, this list of conditions and the following disclaimer;
712855Sgabeblack@google.com# redistributions in binary form must reproduce the above copyright
812855Sgabeblack@google.com# notice, this list of conditions and the following disclaimer in the
912855Sgabeblack@google.com# documentation and/or other materials provided with the distribution;
1012855Sgabeblack@google.com# neither the name of the copyright holders nor the names of its
1112855Sgabeblack@google.com# contributors may be used to endorse or promote products derived from
1212855Sgabeblack@google.com# this software without specific prior written permission.
1312855Sgabeblack@google.com#
1412855Sgabeblack@google.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1512855Sgabeblack@google.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1612855Sgabeblack@google.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1712855Sgabeblack@google.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1812855Sgabeblack@google.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1912855Sgabeblack@google.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2012855Sgabeblack@google.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2112855Sgabeblack@google.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2212855Sgabeblack@google.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2312855Sgabeblack@google.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2412855Sgabeblack@google.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2512855Sgabeblack@google.com#
2612855Sgabeblack@google.com# Authors: Gabe Black
2712855Sgabeblack@google.com
2812855Sgabeblack@google.comfrom __future__ import print_function
2912855Sgabeblack@google.com
3012855Sgabeblack@google.comImport('*')
3112855Sgabeblack@google.com
3212855Sgabeblack@google.comif env['USE_SYSTEMC']:
3312855Sgabeblack@google.com
3412855Sgabeblack@google.com    from gem5_scons import Transform
3512855Sgabeblack@google.com
3612855Sgabeblack@google.com    import os.path
3712855Sgabeblack@google.com    import json
3812855Sgabeblack@google.com
3912855Sgabeblack@google.com    src = str(Dir('.').srcdir)
4012855Sgabeblack@google.com
4112855Sgabeblack@google.com    class SystemCTest(object):
4212855Sgabeblack@google.com        def __init__(self, dirname, name):
4312855Sgabeblack@google.com            self.name = name
4412855Sgabeblack@google.com            self.reldir = os.path.relpath(dirname, src)
4512855Sgabeblack@google.com            self.target = os.path.join(self.reldir, name)
4612855Sgabeblack@google.com            self.sources = []
4712855Sgabeblack@google.com
4812855Sgabeblack@google.com            self.compile_only = False
4912855Sgabeblack@google.com
5012855Sgabeblack@google.com        def add_source(self, source):
5112855Sgabeblack@google.com            self.sources.append(os.path.join(self.reldir, source))
5212855Sgabeblack@google.com
5312855Sgabeblack@google.com        def add_sources(self, sources):
5412855Sgabeblack@google.com            for source in sources:
5512855Sgabeblack@google.com                self.sources.append(os.path.join(self.reldir, '..', source))
5612855Sgabeblack@google.com
5712855Sgabeblack@google.com        def properties(self):
5812855Sgabeblack@google.com            return {
5912855Sgabeblack@google.com                'name' : self.name,
6012855Sgabeblack@google.com                'path' : self.reldir,
6112855Sgabeblack@google.com                'compile_only' : self.compile_only
6212855Sgabeblack@google.com            }
6312855Sgabeblack@google.com
6412855Sgabeblack@google.com    ext_dir = Dir('..').Dir('ext')
6512855Sgabeblack@google.com    class SystemCTestBin(Executable):
6612855Sgabeblack@google.com        def __init__(self, test):
6712855Sgabeblack@google.com            super(SystemCTestBin, self).__init__(test.target, *test.sources)
6812855Sgabeblack@google.com
6912855Sgabeblack@google.com        @classmethod
70        def declare_all(cls, env):
71            env = env.Clone()
72
73            # Turn off extra warnings and Werror for the tests.
74            to_remove = ['-Wall', '-Wundef', '-Wextra', '-Werror']
75            env['CCFLAGS'] = \
76                filter(lambda f: f not in to_remove, env['CCFLAGS'])
77
78            env.Append(CPPPATH=ext_dir)
79
80            super(SystemCTestBin, cls).declare_all(env)
81
82        def declare(self, env):
83            sources = list(self.sources)
84            for f in self.filters:
85                sources = Source.all.apply_filter(f)
86            objs = self.srcs_to_objs(env, sources)
87            objs = objs + env['SHARED_LIB'] + env['MAIN_OBJS']
88            return super(SystemCTestBin, self).declare(env, objs)
89
90    tests = []
91    def new_test(dirname, name):
92        test = SystemCTest(dirname, name)
93        tests.append(test)
94        return test
95
96
97    def scan_dir_for_tests(subdir):
98        def visitor(arg, dirname, names):
99            # If there's a 'DONTRUN' file in this directory, skip it and any
100            # child directories.
101            if 'DONTRUN' in names:
102                del names[:]
103                return
104
105            endswith = lambda sfx: filter(lambda n: n.endswith(sfx), names)
106
107            cpps = endswith('.cpp')
108            if not cpps:
109                return
110
111            # If there's only one source file, then that files name is the test
112            # name, and it's the source for that test.
113            if len(cpps) == 1:
114                cpp = cpps[0]
115
116                test = new_test(dirname, os.path.splitext(cpp)[0])
117                test.add_source(cpp)
118
119            # Otherwise, expect there to be a file that ends in .f. That files
120            # name is the test name, and it will list the source files with
121            # one preceeding path component.
122            else:
123                fs = endswith('.f')
124                if len(fs) != 1:
125                    print("In %s, expected 1 *.f file, but found %d.",
126                          dirname, len(fs))
127                    for f in fs:
128                        print(os.path.join(dirname, f))
129                    return
130                f = fs[0]
131
132                test = new_test(dirname, os.path.splitext(f)[0])
133                with open(os.path.join(dirname, f)) as content:
134                    lines = content.readlines
135                    # Get rid of leading and trailing whitespace.
136                    lines = map(lambda x: x.strip(), content.readlines())
137                    # Get rid of blank lines.
138                    lines = filter(lambda x: x, lines)
139                    # Add all the sources to this test.
140                    test.add_sources(lines)
141
142            if 'COMPILE' in names:
143                test.compile_only = True
144
145        subdir_src = Dir('.').srcdir.Dir(subdir)
146        os.path.walk(str(subdir_src), visitor, None)
147
148    scan_dir_for_tests('systemc')
149
150
151    def build_tests_json(target, source, env):
152        data = { test.target : test.properties() for test in tests }
153        with open(str(target[0]), "w") as tests_json:
154            json.dump(data, tests_json)
155
156    AlwaysBuild(env.Command(File('tests.json'), None,
157                MakeAction(build_tests_json, Transform("TESTJSON"))))
158
159
160    for test in tests:
161        SystemCTestBin(test)
162