112866Sgabeblack@google.com# Copyright 2018 Google, Inc.
212866Sgabeblack@google.com#
312866Sgabeblack@google.com# Redistribution and use in source and binary forms, with or without
412866Sgabeblack@google.com# modification, are permitted provided that the following conditions are
512866Sgabeblack@google.com# met: redistributions of source code must retain the above copyright
612866Sgabeblack@google.com# notice, this list of conditions and the following disclaimer;
712866Sgabeblack@google.com# redistributions in binary form must reproduce the above copyright
812866Sgabeblack@google.com# notice, this list of conditions and the following disclaimer in the
912866Sgabeblack@google.com# documentation and/or other materials provided with the distribution;
1012866Sgabeblack@google.com# neither the name of the copyright holders nor the names of its
1112866Sgabeblack@google.com# contributors may be used to endorse or promote products derived from
1212866Sgabeblack@google.com# this software without specific prior written permission.
1312866Sgabeblack@google.com#
1412866Sgabeblack@google.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1512866Sgabeblack@google.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1612866Sgabeblack@google.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1712866Sgabeblack@google.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1812866Sgabeblack@google.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1912866Sgabeblack@google.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2012866Sgabeblack@google.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2112866Sgabeblack@google.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2212866Sgabeblack@google.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2312866Sgabeblack@google.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2412866Sgabeblack@google.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2512866Sgabeblack@google.com#
2612866Sgabeblack@google.com# Authors: Gabe Black
2712866Sgabeblack@google.com
2812866Sgabeblack@google.comfrom __future__ import print_function
2912866Sgabeblack@google.com
3012866Sgabeblack@google.comImport('*')
3112866Sgabeblack@google.com
3212866Sgabeblack@google.comif env['USE_SYSTEMC']:
3312866Sgabeblack@google.com
3412866Sgabeblack@google.com    from gem5_scons import Transform
3512866Sgabeblack@google.com
3612866Sgabeblack@google.com    import os.path
3712866Sgabeblack@google.com    import json
3812866Sgabeblack@google.com
3912866Sgabeblack@google.com    src = str(Dir('.').srcdir)
4012866Sgabeblack@google.com
4112866Sgabeblack@google.com    class SystemCTest(object):
4212866Sgabeblack@google.com        def __init__(self, dirname, name):
4312866Sgabeblack@google.com            self.name = name
4412866Sgabeblack@google.com            self.reldir = os.path.relpath(dirname, src)
4512866Sgabeblack@google.com            self.target = os.path.join(self.reldir, name)
4612866Sgabeblack@google.com            self.sources = []
4713458Sgabeblack@google.com            self.deps = []
4812866Sgabeblack@google.com
4912866Sgabeblack@google.com            self.compile_only = False
5012866Sgabeblack@google.com
5112866Sgabeblack@google.com        def add_source(self, source):
5212866Sgabeblack@google.com            self.sources.append(os.path.join(self.reldir, source))
5312866Sgabeblack@google.com
5412866Sgabeblack@google.com        def add_sources(self, sources):
5512866Sgabeblack@google.com            for source in sources:
5612866Sgabeblack@google.com                self.sources.append(os.path.join(self.reldir, '..', source))
5712866Sgabeblack@google.com
5812866Sgabeblack@google.com        def properties(self):
5912866Sgabeblack@google.com            return {
6012866Sgabeblack@google.com                'name' : self.name,
6112866Sgabeblack@google.com                'path' : self.reldir,
6213458Sgabeblack@google.com                'compile_only' : self.compile_only,
6313458Sgabeblack@google.com                'deps' : self.deps
6412866Sgabeblack@google.com            }
6512866Sgabeblack@google.com
6612922Sgabeblack@google.com    test_dir = Dir('.')
6712869Sgabeblack@google.com    class SystemCTestBin(Executable):
6812866Sgabeblack@google.com        def __init__(self, test):
6912869Sgabeblack@google.com            super(SystemCTestBin, self).__init__(test.target, *test.sources)
7013458Sgabeblack@google.com            self.reldir = test.reldir
7113458Sgabeblack@google.com            self.test_deps = test.deps
7212866Sgabeblack@google.com
7312866Sgabeblack@google.com        @classmethod
7412866Sgabeblack@google.com        def declare_all(cls, env):
7512866Sgabeblack@google.com            env = env.Clone()
7612866Sgabeblack@google.com
7712866Sgabeblack@google.com            # Turn off extra warnings and Werror for the tests.
7812866Sgabeblack@google.com            to_remove = ['-Wall', '-Wundef', '-Wextra', '-Werror']
7912866Sgabeblack@google.com            env['CCFLAGS'] = \
8012866Sgabeblack@google.com                filter(lambda f: f not in to_remove, env['CCFLAGS'])
8112866Sgabeblack@google.com
8212922Sgabeblack@google.com            env.Append(CPPPATH=test_dir.Dir('include'))
8312866Sgabeblack@google.com
8413098Sgabeblack@google.com            shared_lib_path = env['SHARED_LIB'][0].abspath
8513098Sgabeblack@google.com            sl_dir, sl_base = os.path.split(shared_lib_path)
8613098Sgabeblack@google.com            env.Append(LIBPATH=[sl_dir], LIBS=[sl_base])
8713098Sgabeblack@google.com
8812866Sgabeblack@google.com            super(SystemCTestBin, cls).declare_all(env)
8912866Sgabeblack@google.com
9012869Sgabeblack@google.com        def declare(self, env):
9113098Sgabeblack@google.com            env = env.Clone()
9212869Sgabeblack@google.com            sources = list(self.sources)
9312869Sgabeblack@google.com            for f in self.filters:
9413656Sgabeblack@google.com                sources += Source.all.apply_filter(f)
9512869Sgabeblack@google.com            objs = self.srcs_to_objs(env, sources)
9613098Sgabeblack@google.com            objs = objs + env['MAIN_OBJS']
9713098Sgabeblack@google.com            relpath = os.path.relpath(
9813098Sgabeblack@google.com                    env['SHARED_LIB'][0].dir.abspath,
9913098Sgabeblack@google.com                    self.path(env).dir.abspath)
10013098Sgabeblack@google.com            env.Append(LINKFLAGS=Split('-z origin'))
10113655Sgabeblack@google.com            env.Append(RPATH=[
10213655Sgabeblack@google.com                    env.Literal(os.path.join('\\$$ORIGIN', relpath))])
10313458Sgabeblack@google.com            test_bin = super(SystemCTestBin, self).declare(env, objs)
10413458Sgabeblack@google.com            test_dir = self.dir.Dir(self.reldir)
10513458Sgabeblack@google.com            for dep in self.test_deps:
10613458Sgabeblack@google.com                env.Depends(test_bin, test_dir.File(dep))
10713458Sgabeblack@google.com            return test_bin
10812869Sgabeblack@google.com
10912866Sgabeblack@google.com    tests = []
11012866Sgabeblack@google.com    def new_test(dirname, name):
11112866Sgabeblack@google.com        test = SystemCTest(dirname, name)
11212866Sgabeblack@google.com        tests.append(test)
11312866Sgabeblack@google.com        return test
11412866Sgabeblack@google.com
11512866Sgabeblack@google.com
11612866Sgabeblack@google.com    def scan_dir_for_tests(subdir):
11712866Sgabeblack@google.com        def visitor(arg, dirname, names):
11812866Sgabeblack@google.com            # If there's a 'DONTRUN' file in this directory, skip it and any
11912866Sgabeblack@google.com            # child directories.
12012866Sgabeblack@google.com            if 'DONTRUN' in names:
12112866Sgabeblack@google.com                del names[:]
12212866Sgabeblack@google.com                return
12312866Sgabeblack@google.com
12412866Sgabeblack@google.com            endswith = lambda sfx: filter(lambda n: n.endswith(sfx), names)
12512866Sgabeblack@google.com
12612866Sgabeblack@google.com            cpps = endswith('.cpp')
12712866Sgabeblack@google.com            if not cpps:
12812866Sgabeblack@google.com                return
12912866Sgabeblack@google.com
13013458Sgabeblack@google.com            def get_entries(fname):
13113458Sgabeblack@google.com                with open(os.path.join(dirname, fname)) as content:
13213458Sgabeblack@google.com                    lines = content.readlines
13313458Sgabeblack@google.com                    # Get rid of leading and trailing whitespace.
13413458Sgabeblack@google.com                    lines = map(lambda x: x.strip(), content.readlines())
13513458Sgabeblack@google.com                    # Get rid of blank lines.
13613458Sgabeblack@google.com                    lines = filter(lambda x: x, lines)
13713458Sgabeblack@google.com                    return lines
13813458Sgabeblack@google.com
13912866Sgabeblack@google.com            # If there's only one source file, then that files name is the test
14012866Sgabeblack@google.com            # name, and it's the source for that test.
14112866Sgabeblack@google.com            if len(cpps) == 1:
14212866Sgabeblack@google.com                cpp = cpps[0]
14312866Sgabeblack@google.com
14412866Sgabeblack@google.com                test = new_test(dirname, os.path.splitext(cpp)[0])
14512866Sgabeblack@google.com                test.add_source(cpp)
14612866Sgabeblack@google.com
14712866Sgabeblack@google.com            # Otherwise, expect there to be a file that ends in .f. That files
14812866Sgabeblack@google.com            # name is the test name, and it will list the source files with
14912866Sgabeblack@google.com            # one preceeding path component.
15012866Sgabeblack@google.com            else:
15112866Sgabeblack@google.com                fs = endswith('.f')
15212866Sgabeblack@google.com                if len(fs) != 1:
15312866Sgabeblack@google.com                    print("In %s, expected 1 *.f file, but found %d.",
15412866Sgabeblack@google.com                          dirname, len(fs))
15512866Sgabeblack@google.com                    for f in fs:
15612866Sgabeblack@google.com                        print(os.path.join(dirname, f))
15712866Sgabeblack@google.com                    return
15812866Sgabeblack@google.com                f = fs[0]
15912866Sgabeblack@google.com
16012866Sgabeblack@google.com                test = new_test(dirname, os.path.splitext(f)[0])
16113458Sgabeblack@google.com                # Add all the sources to this test.
16213458Sgabeblack@google.com                test.add_sources(get_entries(f))
16312866Sgabeblack@google.com
16412866Sgabeblack@google.com            if 'COMPILE' in names:
16512866Sgabeblack@google.com                test.compile_only = True
16612866Sgabeblack@google.com
16713458Sgabeblack@google.com            if 'DEPS' in names:
16813458Sgabeblack@google.com                test.deps = get_entries('DEPS')
16913458Sgabeblack@google.com
17012866Sgabeblack@google.com        subdir_src = Dir('.').srcdir.Dir(subdir)
17112866Sgabeblack@google.com        os.path.walk(str(subdir_src), visitor, None)
17212866Sgabeblack@google.com
17312866Sgabeblack@google.com    scan_dir_for_tests('systemc')
17413512Sgabeblack@google.com    scan_dir_for_tests('tlm')
17512866Sgabeblack@google.com
17612866Sgabeblack@google.com
17712866Sgabeblack@google.com    def build_tests_json(target, source, env):
17812866Sgabeblack@google.com        data = { test.target : test.properties() for test in tests }
17912866Sgabeblack@google.com        with open(str(target[0]), "w") as tests_json:
18012866Sgabeblack@google.com            json.dump(data, tests_json)
18112866Sgabeblack@google.com
18212866Sgabeblack@google.com    AlwaysBuild(env.Command(File('tests.json'), None,
18312866Sgabeblack@google.com                MakeAction(build_tests_json, Transform("TESTJSON"))))
18412866Sgabeblack@google.com
18512866Sgabeblack@google.com
18612866Sgabeblack@google.com    for test in tests:
18712866Sgabeblack@google.com        SystemCTestBin(test)
188