SConscript revision 13098
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')
6512922Sgabeblack@google.com    test_dir = Dir('.')
6612869Sgabeblack@google.com    class SystemCTestBin(Executable):
6712866Sgabeblack@google.com        def __init__(self, test):
6812869Sgabeblack@google.com            super(SystemCTestBin, self).__init__(test.target, *test.sources)
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
7912922Sgabeblack@google.com            env.Append(CPPPATH=test_dir.Dir('include'))
8012866Sgabeblack@google.com            env.Append(CPPPATH=ext_dir)
8112866Sgabeblack@google.com
8213098Sgabeblack@google.com            shared_lib_path = env['SHARED_LIB'][0].abspath
8313098Sgabeblack@google.com            sl_dir, sl_base = os.path.split(shared_lib_path)
8413098Sgabeblack@google.com            env.Append(LIBPATH=[sl_dir], LIBS=[sl_base])
8513098Sgabeblack@google.com
8612866Sgabeblack@google.com            super(SystemCTestBin, cls).declare_all(env)
8712866Sgabeblack@google.com
8812869Sgabeblack@google.com        def declare(self, env):
8913098Sgabeblack@google.com            env = env.Clone()
9012869Sgabeblack@google.com            sources = list(self.sources)
9112869Sgabeblack@google.com            for f in self.filters:
9212869Sgabeblack@google.com                sources = Source.all.apply_filter(f)
9312869Sgabeblack@google.com            objs = self.srcs_to_objs(env, sources)
9413098Sgabeblack@google.com            objs = objs + env['MAIN_OBJS']
9513098Sgabeblack@google.com            relpath = os.path.relpath(
9613098Sgabeblack@google.com                    env['SHARED_LIB'][0].dir.abspath,
9713098Sgabeblack@google.com                    self.path(env).dir.abspath)
9813098Sgabeblack@google.com            env.Append(LINKFLAGS=Split('-z origin'))
9913098Sgabeblack@google.com            env.Append(RPATH=env.Literal(os.path.join('\\$$ORIGIN', relpath)))
10012869Sgabeblack@google.com            return super(SystemCTestBin, self).declare(env, objs)
10112869Sgabeblack@google.com
10212866Sgabeblack@google.com    tests = []
10312866Sgabeblack@google.com    def new_test(dirname, name):
10412866Sgabeblack@google.com        test = SystemCTest(dirname, name)
10512866Sgabeblack@google.com        tests.append(test)
10612866Sgabeblack@google.com        return test
10712866Sgabeblack@google.com
10812866Sgabeblack@google.com
10912866Sgabeblack@google.com    def scan_dir_for_tests(subdir):
11012866Sgabeblack@google.com        def visitor(arg, dirname, names):
11112866Sgabeblack@google.com            # If there's a 'DONTRUN' file in this directory, skip it and any
11212866Sgabeblack@google.com            # child directories.
11312866Sgabeblack@google.com            if 'DONTRUN' in names:
11412866Sgabeblack@google.com                del names[:]
11512866Sgabeblack@google.com                return
11612866Sgabeblack@google.com
11712866Sgabeblack@google.com            endswith = lambda sfx: filter(lambda n: n.endswith(sfx), names)
11812866Sgabeblack@google.com
11912866Sgabeblack@google.com            cpps = endswith('.cpp')
12012866Sgabeblack@google.com            if not cpps:
12112866Sgabeblack@google.com                return
12212866Sgabeblack@google.com
12312866Sgabeblack@google.com            # If there's only one source file, then that files name is the test
12412866Sgabeblack@google.com            # name, and it's the source for that test.
12512866Sgabeblack@google.com            if len(cpps) == 1:
12612866Sgabeblack@google.com                cpp = cpps[0]
12712866Sgabeblack@google.com
12812866Sgabeblack@google.com                test = new_test(dirname, os.path.splitext(cpp)[0])
12912866Sgabeblack@google.com                test.add_source(cpp)
13012866Sgabeblack@google.com
13112866Sgabeblack@google.com            # Otherwise, expect there to be a file that ends in .f. That files
13212866Sgabeblack@google.com            # name is the test name, and it will list the source files with
13312866Sgabeblack@google.com            # one preceeding path component.
13412866Sgabeblack@google.com            else:
13512866Sgabeblack@google.com                fs = endswith('.f')
13612866Sgabeblack@google.com                if len(fs) != 1:
13712866Sgabeblack@google.com                    print("In %s, expected 1 *.f file, but found %d.",
13812866Sgabeblack@google.com                          dirname, len(fs))
13912866Sgabeblack@google.com                    for f in fs:
14012866Sgabeblack@google.com                        print(os.path.join(dirname, f))
14112866Sgabeblack@google.com                    return
14212866Sgabeblack@google.com                f = fs[0]
14312866Sgabeblack@google.com
14412866Sgabeblack@google.com                test = new_test(dirname, os.path.splitext(f)[0])
14512866Sgabeblack@google.com                with open(os.path.join(dirname, f)) as content:
14612866Sgabeblack@google.com                    lines = content.readlines
14712866Sgabeblack@google.com                    # Get rid of leading and trailing whitespace.
14812866Sgabeblack@google.com                    lines = map(lambda x: x.strip(), content.readlines())
14912866Sgabeblack@google.com                    # Get rid of blank lines.
15012866Sgabeblack@google.com                    lines = filter(lambda x: x, lines)
15112866Sgabeblack@google.com                    # Add all the sources to this test.
15212866Sgabeblack@google.com                    test.add_sources(lines)
15312866Sgabeblack@google.com
15412866Sgabeblack@google.com            if 'COMPILE' in names:
15512866Sgabeblack@google.com                test.compile_only = True
15612866Sgabeblack@google.com
15712866Sgabeblack@google.com        subdir_src = Dir('.').srcdir.Dir(subdir)
15812866Sgabeblack@google.com        os.path.walk(str(subdir_src), visitor, None)
15912866Sgabeblack@google.com
16012866Sgabeblack@google.com    scan_dir_for_tests('systemc')
16112866Sgabeblack@google.com
16212866Sgabeblack@google.com
16312866Sgabeblack@google.com    def build_tests_json(target, source, env):
16412866Sgabeblack@google.com        data = { test.target : test.properties() for test in tests }
16512866Sgabeblack@google.com        with open(str(target[0]), "w") as tests_json:
16612866Sgabeblack@google.com            json.dump(data, tests_json)
16712866Sgabeblack@google.com
16812866Sgabeblack@google.com    AlwaysBuild(env.Command(File('tests.json'), None,
16912866Sgabeblack@google.com                MakeAction(build_tests_json, Transform("TESTJSON"))))
17012866Sgabeblack@google.com
17112866Sgabeblack@google.com
17212866Sgabeblack@google.com    for test in tests:
17312866Sgabeblack@google.com        SystemCTestBin(test)
174