SConscript revision 12869
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')
6512869Sgabeblack@google.com    class SystemCTestBin(Executable):
6612866Sgabeblack@google.com        def __init__(self, test):
6712869Sgabeblack@google.com            super(SystemCTestBin, self).__init__(test.target, *test.sources)
6812866Sgabeblack@google.com
6912866Sgabeblack@google.com        @classmethod
7012866Sgabeblack@google.com        def declare_all(cls, env):
7112866Sgabeblack@google.com            env = env.Clone()
7212866Sgabeblack@google.com
7312866Sgabeblack@google.com            # Turn off extra warnings and Werror for the tests.
7412866Sgabeblack@google.com            to_remove = ['-Wall', '-Wundef', '-Wextra', '-Werror']
7512866Sgabeblack@google.com            env['CCFLAGS'] = \
7612866Sgabeblack@google.com                filter(lambda f: f not in to_remove, env['CCFLAGS'])
7712866Sgabeblack@google.com
7812866Sgabeblack@google.com            env.Append(CPPPATH=ext_dir)
7912866Sgabeblack@google.com
8012866Sgabeblack@google.com            super(SystemCTestBin, cls).declare_all(env)
8112866Sgabeblack@google.com
8212869Sgabeblack@google.com        def declare(self, env):
8312869Sgabeblack@google.com            sources = list(self.sources)
8412869Sgabeblack@google.com            for f in self.filters:
8512869Sgabeblack@google.com                sources = Source.all.apply_filter(f)
8612869Sgabeblack@google.com            objs = self.srcs_to_objs(env, sources)
8712869Sgabeblack@google.com            objs = objs + env['SHARED_LIB'] + env['MAIN_OBJS']
8812869Sgabeblack@google.com            return super(SystemCTestBin, self).declare(env, objs)
8912869Sgabeblack@google.com
9012866Sgabeblack@google.com    tests = []
9112866Sgabeblack@google.com    def new_test(dirname, name):
9212866Sgabeblack@google.com        test = SystemCTest(dirname, name)
9312866Sgabeblack@google.com        tests.append(test)
9412866Sgabeblack@google.com        return test
9512866Sgabeblack@google.com
9612866Sgabeblack@google.com
9712866Sgabeblack@google.com    def scan_dir_for_tests(subdir):
9812866Sgabeblack@google.com        def visitor(arg, dirname, names):
9912866Sgabeblack@google.com            # If there's a 'DONTRUN' file in this directory, skip it and any
10012866Sgabeblack@google.com            # child directories.
10112866Sgabeblack@google.com            if 'DONTRUN' in names:
10212866Sgabeblack@google.com                del names[:]
10312866Sgabeblack@google.com                return
10412866Sgabeblack@google.com
10512866Sgabeblack@google.com            endswith = lambda sfx: filter(lambda n: n.endswith(sfx), names)
10612866Sgabeblack@google.com
10712866Sgabeblack@google.com            cpps = endswith('.cpp')
10812866Sgabeblack@google.com            if not cpps:
10912866Sgabeblack@google.com                return
11012866Sgabeblack@google.com
11112866Sgabeblack@google.com            # If there's only one source file, then that files name is the test
11212866Sgabeblack@google.com            # name, and it's the source for that test.
11312866Sgabeblack@google.com            if len(cpps) == 1:
11412866Sgabeblack@google.com                cpp = cpps[0]
11512866Sgabeblack@google.com
11612866Sgabeblack@google.com                test = new_test(dirname, os.path.splitext(cpp)[0])
11712866Sgabeblack@google.com                test.add_source(cpp)
11812866Sgabeblack@google.com
11912866Sgabeblack@google.com            # Otherwise, expect there to be a file that ends in .f. That files
12012866Sgabeblack@google.com            # name is the test name, and it will list the source files with
12112866Sgabeblack@google.com            # one preceeding path component.
12212866Sgabeblack@google.com            else:
12312866Sgabeblack@google.com                fs = endswith('.f')
12412866Sgabeblack@google.com                if len(fs) != 1:
12512866Sgabeblack@google.com                    print("In %s, expected 1 *.f file, but found %d.",
12612866Sgabeblack@google.com                          dirname, len(fs))
12712866Sgabeblack@google.com                    for f in fs:
12812866Sgabeblack@google.com                        print(os.path.join(dirname, f))
12912866Sgabeblack@google.com                    return
13012866Sgabeblack@google.com                f = fs[0]
13112866Sgabeblack@google.com
13212866Sgabeblack@google.com                test = new_test(dirname, os.path.splitext(f)[0])
13312866Sgabeblack@google.com                with open(os.path.join(dirname, f)) as content:
13412866Sgabeblack@google.com                    lines = content.readlines
13512866Sgabeblack@google.com                    # Get rid of leading and trailing whitespace.
13612866Sgabeblack@google.com                    lines = map(lambda x: x.strip(), content.readlines())
13712866Sgabeblack@google.com                    # Get rid of blank lines.
13812866Sgabeblack@google.com                    lines = filter(lambda x: x, lines)
13912866Sgabeblack@google.com                    # Add all the sources to this test.
14012866Sgabeblack@google.com                    test.add_sources(lines)
14112866Sgabeblack@google.com
14212866Sgabeblack@google.com            if 'COMPILE' in names:
14312866Sgabeblack@google.com                test.compile_only = True
14412866Sgabeblack@google.com
14512866Sgabeblack@google.com        subdir_src = Dir('.').srcdir.Dir(subdir)
14612866Sgabeblack@google.com        os.path.walk(str(subdir_src), visitor, None)
14712866Sgabeblack@google.com
14812866Sgabeblack@google.com    scan_dir_for_tests('systemc')
14912866Sgabeblack@google.com
15012866Sgabeblack@google.com
15112866Sgabeblack@google.com    def build_tests_json(target, source, env):
15212866Sgabeblack@google.com        data = { test.target : test.properties() for test in tests }
15312866Sgabeblack@google.com        with open(str(target[0]), "w") as tests_json:
15412866Sgabeblack@google.com            json.dump(data, tests_json)
15512866Sgabeblack@google.com
15612866Sgabeblack@google.com    AlwaysBuild(env.Command(File('tests.json'), None,
15712866Sgabeblack@google.com                MakeAction(build_tests_json, Transform("TESTJSON"))))
15812866Sgabeblack@google.com
15912866Sgabeblack@google.com
16012866Sgabeblack@google.com    for test in tests:
16112866Sgabeblack@google.com        SystemCTestBin(test)
162