SConscript revision 12866
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 = []
4712866Sgabeblack@google.com
4812866Sgabeblack@google.com            self.compile_only = False
4912866Sgabeblack@google.com
5012866Sgabeblack@google.com        def add_source(self, source):
5112866Sgabeblack@google.com            self.sources.append(os.path.join(self.reldir, source))
5212866Sgabeblack@google.com
5312866Sgabeblack@google.com        def add_sources(self, sources):
5412866Sgabeblack@google.com            for source in sources:
5512866Sgabeblack@google.com                self.sources.append(os.path.join(self.reldir, '..', source))
5612866Sgabeblack@google.com
5712866Sgabeblack@google.com        def properties(self):
5812866Sgabeblack@google.com            return {
5912866Sgabeblack@google.com                'name' : self.name,
6012866Sgabeblack@google.com                'path' : self.reldir,
6112866Sgabeblack@google.com                'compile_only' : self.compile_only
6212866Sgabeblack@google.com            }
6312866Sgabeblack@google.com
6412866Sgabeblack@google.com    ext_dir = Dir('..').Dir('ext')
6512866Sgabeblack@google.com    class SystemCTestBin(UnitTest):
6612866Sgabeblack@google.com        def __init__(self, test):
6712866Sgabeblack@google.com            super(SystemCTestBin, self).__init__(
6812866Sgabeblack@google.com                    test.target, *test.sources, main=True)
6912866Sgabeblack@google.com
7012866Sgabeblack@google.com        @classmethod
7112866Sgabeblack@google.com        def declare_all(cls, env):
7212866Sgabeblack@google.com            env = env.Clone()
7312866Sgabeblack@google.com
7412866Sgabeblack@google.com            # Turn off extra warnings and Werror for the tests.
7512866Sgabeblack@google.com            to_remove = ['-Wall', '-Wundef', '-Wextra', '-Werror']
7612866Sgabeblack@google.com            env['CCFLAGS'] = \
7712866Sgabeblack@google.com                filter(lambda f: f not in to_remove, env['CCFLAGS'])
7812866Sgabeblack@google.com
7912866Sgabeblack@google.com            env.Append(CPPPATH=ext_dir)
8012866Sgabeblack@google.com
8112866Sgabeblack@google.com            super(SystemCTestBin, cls).declare_all(env)
8212866Sgabeblack@google.com
8312866Sgabeblack@google.com    tests = []
8412866Sgabeblack@google.com    def new_test(dirname, name):
8512866Sgabeblack@google.com        test = SystemCTest(dirname, name)
8612866Sgabeblack@google.com        tests.append(test)
8712866Sgabeblack@google.com        return test
8812866Sgabeblack@google.com
8912866Sgabeblack@google.com
9012866Sgabeblack@google.com    def scan_dir_for_tests(subdir):
9112866Sgabeblack@google.com        def visitor(arg, dirname, names):
9212866Sgabeblack@google.com            # If there's a 'DONTRUN' file in this directory, skip it and any
9312866Sgabeblack@google.com            # child directories.
9412866Sgabeblack@google.com            if 'DONTRUN' in names:
9512866Sgabeblack@google.com                del names[:]
9612866Sgabeblack@google.com                return
9712866Sgabeblack@google.com
9812866Sgabeblack@google.com            endswith = lambda sfx: filter(lambda n: n.endswith(sfx), names)
9912866Sgabeblack@google.com
10012866Sgabeblack@google.com            cpps = endswith('.cpp')
10112866Sgabeblack@google.com            if not cpps:
10212866Sgabeblack@google.com                return
10312866Sgabeblack@google.com
10412866Sgabeblack@google.com            # If there's only one source file, then that files name is the test
10512866Sgabeblack@google.com            # name, and it's the source for that test.
10612866Sgabeblack@google.com            if len(cpps) == 1:
10712866Sgabeblack@google.com                cpp = cpps[0]
10812866Sgabeblack@google.com
10912866Sgabeblack@google.com                test = new_test(dirname, os.path.splitext(cpp)[0])
11012866Sgabeblack@google.com                test.add_source(cpp)
11112866Sgabeblack@google.com
11212866Sgabeblack@google.com            # Otherwise, expect there to be a file that ends in .f. That files
11312866Sgabeblack@google.com            # name is the test name, and it will list the source files with
11412866Sgabeblack@google.com            # one preceeding path component.
11512866Sgabeblack@google.com            else:
11612866Sgabeblack@google.com                fs = endswith('.f')
11712866Sgabeblack@google.com                if len(fs) != 1:
11812866Sgabeblack@google.com                    print("In %s, expected 1 *.f file, but found %d.",
11912866Sgabeblack@google.com                          dirname, len(fs))
12012866Sgabeblack@google.com                    for f in fs:
12112866Sgabeblack@google.com                        print(os.path.join(dirname, f))
12212866Sgabeblack@google.com                    return
12312866Sgabeblack@google.com                f = fs[0]
12412866Sgabeblack@google.com
12512866Sgabeblack@google.com                test = new_test(dirname, os.path.splitext(f)[0])
12612866Sgabeblack@google.com                with open(os.path.join(dirname, f)) as content:
12712866Sgabeblack@google.com                    lines = content.readlines
12812866Sgabeblack@google.com                    # Get rid of leading and trailing whitespace.
12912866Sgabeblack@google.com                    lines = map(lambda x: x.strip(), content.readlines())
13012866Sgabeblack@google.com                    # Get rid of blank lines.
13112866Sgabeblack@google.com                    lines = filter(lambda x: x, lines)
13212866Sgabeblack@google.com                    # Add all the sources to this test.
13312866Sgabeblack@google.com                    test.add_sources(lines)
13412866Sgabeblack@google.com
13512866Sgabeblack@google.com            if 'COMPILE' in names:
13612866Sgabeblack@google.com                test.compile_only = True
13712866Sgabeblack@google.com
13812866Sgabeblack@google.com        subdir_src = Dir('.').srcdir.Dir(subdir)
13912866Sgabeblack@google.com        os.path.walk(str(subdir_src), visitor, None)
14012866Sgabeblack@google.com
14112866Sgabeblack@google.com    scan_dir_for_tests('systemc')
14212866Sgabeblack@google.com
14312866Sgabeblack@google.com
14412866Sgabeblack@google.com    def build_tests_json(target, source, env):
14512866Sgabeblack@google.com        data = { test.target : test.properties() for test in tests }
14612866Sgabeblack@google.com        with open(str(target[0]), "w") as tests_json:
14712866Sgabeblack@google.com            json.dump(data, tests_json)
14812866Sgabeblack@google.com
14912866Sgabeblack@google.com    AlwaysBuild(env.Command(File('tests.json'), None,
15012866Sgabeblack@google.com                MakeAction(build_tests_json, Transform("TESTJSON"))))
15112866Sgabeblack@google.com
15212866Sgabeblack@google.com
15312866Sgabeblack@google.com    for test in tests:
15412866Sgabeblack@google.com        SystemCTestBin(test)
155