SConscript revision 12869:1ad10753e8c3
1955SN/A# Copyright 2018 Google, Inc.
2955SN/A#
39812Sandreas.hansson@arm.com# Redistribution and use in source and binary forms, with or without
49812Sandreas.hansson@arm.com# modification, are permitted provided that the following conditions are
59812Sandreas.hansson@arm.com# met: redistributions of source code must retain the above copyright
69812Sandreas.hansson@arm.com# notice, this list of conditions and the following disclaimer;
79812Sandreas.hansson@arm.com# redistributions in binary form must reproduce the above copyright
89812Sandreas.hansson@arm.com# notice, this list of conditions and the following disclaimer in the
99812Sandreas.hansson@arm.com# documentation and/or other materials provided with the distribution;
109812Sandreas.hansson@arm.com# neither the name of the copyright holders nor the names of its
119812Sandreas.hansson@arm.com# contributors may be used to endorse or promote products derived from
129812Sandreas.hansson@arm.com# this software without specific prior written permission.
139812Sandreas.hansson@arm.com#
149812Sandreas.hansson@arm.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
157816Ssteve.reinhardt@amd.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
165871Snate@binkert.org# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
171762SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18955SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19955SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20955SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21955SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22955SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23955SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24955SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25955SN/A#
26955SN/A# Authors: Gabe Black
27955SN/A
28955SN/Afrom __future__ import print_function
29955SN/A
30955SN/AImport('*')
31955SN/A
32955SN/Aif env['USE_SYSTEMC']:
33955SN/A
34955SN/A    from gem5_scons import Transform
35955SN/A
36955SN/A    import os.path
37955SN/A    import json
38955SN/A
39955SN/A    src = str(Dir('.').srcdir)
40955SN/A
41955SN/A    class SystemCTest(object):
422665Ssaidi@eecs.umich.edu        def __init__(self, dirname, name):
432665Ssaidi@eecs.umich.edu            self.name = name
445863Snate@binkert.org            self.reldir = os.path.relpath(dirname, src)
45955SN/A            self.target = os.path.join(self.reldir, name)
46955SN/A            self.sources = []
47955SN/A
48955SN/A            self.compile_only = False
49955SN/A
508878Ssteve.reinhardt@amd.com        def add_source(self, source):
512632Sstever@eecs.umich.edu            self.sources.append(os.path.join(self.reldir, source))
528878Ssteve.reinhardt@amd.com
532632Sstever@eecs.umich.edu        def add_sources(self, sources):
54955SN/A            for source in sources:
558878Ssteve.reinhardt@amd.com                self.sources.append(os.path.join(self.reldir, '..', source))
562632Sstever@eecs.umich.edu
572761Sstever@eecs.umich.edu        def properties(self):
582632Sstever@eecs.umich.edu            return {
592632Sstever@eecs.umich.edu                'name' : self.name,
602632Sstever@eecs.umich.edu                'path' : self.reldir,
612761Sstever@eecs.umich.edu                'compile_only' : self.compile_only
622761Sstever@eecs.umich.edu            }
632761Sstever@eecs.umich.edu
648878Ssteve.reinhardt@amd.com    ext_dir = Dir('..').Dir('ext')
658878Ssteve.reinhardt@amd.com    class SystemCTestBin(Executable):
662761Sstever@eecs.umich.edu        def __init__(self, test):
672761Sstever@eecs.umich.edu            super(SystemCTestBin, self).__init__(test.target, *test.sources)
682761Sstever@eecs.umich.edu
692761Sstever@eecs.umich.edu        @classmethod
702761Sstever@eecs.umich.edu        def declare_all(cls, env):
718878Ssteve.reinhardt@amd.com            env = env.Clone()
728878Ssteve.reinhardt@amd.com
732632Sstever@eecs.umich.edu            # Turn off extra warnings and Werror for the tests.
742632Sstever@eecs.umich.edu            to_remove = ['-Wall', '-Wundef', '-Wextra', '-Werror']
758878Ssteve.reinhardt@amd.com            env['CCFLAGS'] = \
768878Ssteve.reinhardt@amd.com                filter(lambda f: f not in to_remove, env['CCFLAGS'])
772632Sstever@eecs.umich.edu
78955SN/A            env.Append(CPPPATH=ext_dir)
79955SN/A
80955SN/A            super(SystemCTestBin, cls).declare_all(env)
815863Snate@binkert.org
825863Snate@binkert.org        def declare(self, env):
835863Snate@binkert.org            sources = list(self.sources)
845863Snate@binkert.org            for f in self.filters:
855863Snate@binkert.org                sources = Source.all.apply_filter(f)
865863Snate@binkert.org            objs = self.srcs_to_objs(env, sources)
875863Snate@binkert.org            objs = objs + env['SHARED_LIB'] + env['MAIN_OBJS']
885863Snate@binkert.org            return super(SystemCTestBin, self).declare(env, objs)
895863Snate@binkert.org
905863Snate@binkert.org    tests = []
915863Snate@binkert.org    def new_test(dirname, name):
928878Ssteve.reinhardt@amd.com        test = SystemCTest(dirname, name)
935863Snate@binkert.org        tests.append(test)
945863Snate@binkert.org        return test
955863Snate@binkert.org
969812Sandreas.hansson@arm.com
979812Sandreas.hansson@arm.com    def scan_dir_for_tests(subdir):
985863Snate@binkert.org        def visitor(arg, dirname, names):
999812Sandreas.hansson@arm.com            # If there's a 'DONTRUN' file in this directory, skip it and any
1005863Snate@binkert.org            # child directories.
1015863Snate@binkert.org            if 'DONTRUN' in names:
1025863Snate@binkert.org                del names[:]
1039812Sandreas.hansson@arm.com                return
1049812Sandreas.hansson@arm.com
1055863Snate@binkert.org            endswith = lambda sfx: filter(lambda n: n.endswith(sfx), names)
1065863Snate@binkert.org
1078878Ssteve.reinhardt@amd.com            cpps = endswith('.cpp')
1085863Snate@binkert.org            if not cpps:
1095863Snate@binkert.org                return
1105863Snate@binkert.org
1116654Snate@binkert.org            # If there's only one source file, then that files name is the test
112955SN/A            # name, and it's the source for that test.
1135396Ssaidi@eecs.umich.edu            if len(cpps) == 1:
1145863Snate@binkert.org                cpp = cpps[0]
1155863Snate@binkert.org
1164202Sbinkertn@umich.edu                test = new_test(dirname, os.path.splitext(cpp)[0])
1175863Snate@binkert.org                test.add_source(cpp)
1185863Snate@binkert.org
1195863Snate@binkert.org            # Otherwise, expect there to be a file that ends in .f. That files
1205863Snate@binkert.org            # name is the test name, and it will list the source files with
121955SN/A            # one preceeding path component.
1226654Snate@binkert.org            else:
1235273Sstever@gmail.com                fs = endswith('.f')
1245871Snate@binkert.org                if len(fs) != 1:
1255273Sstever@gmail.com                    print("In %s, expected 1 *.f file, but found %d.",
1266655Snate@binkert.org                          dirname, len(fs))
1278878Ssteve.reinhardt@amd.com                    for f in fs:
1286655Snate@binkert.org                        print(os.path.join(dirname, f))
1296655Snate@binkert.org                    return
1309219Spower.jg@gmail.com                f = fs[0]
1316655Snate@binkert.org
1325871Snate@binkert.org                test = new_test(dirname, os.path.splitext(f)[0])
1336654Snate@binkert.org                with open(os.path.join(dirname, f)) as content:
1348947Sandreas.hansson@arm.com                    lines = content.readlines
1355396Ssaidi@eecs.umich.edu                    # Get rid of leading and trailing whitespace.
1368120Sgblack@eecs.umich.edu                    lines = map(lambda x: x.strip(), content.readlines())
1378120Sgblack@eecs.umich.edu                    # Get rid of blank lines.
1388120Sgblack@eecs.umich.edu                    lines = filter(lambda x: x, lines)
1398120Sgblack@eecs.umich.edu                    # Add all the sources to this test.
1408120Sgblack@eecs.umich.edu                    test.add_sources(lines)
1418120Sgblack@eecs.umich.edu
1428120Sgblack@eecs.umich.edu            if 'COMPILE' in names:
1438120Sgblack@eecs.umich.edu                test.compile_only = True
1448879Ssteve.reinhardt@amd.com
1458879Ssteve.reinhardt@amd.com        subdir_src = Dir('.').srcdir.Dir(subdir)
1468879Ssteve.reinhardt@amd.com        os.path.walk(str(subdir_src), visitor, None)
1478879Ssteve.reinhardt@amd.com
1488879Ssteve.reinhardt@amd.com    scan_dir_for_tests('systemc')
1498879Ssteve.reinhardt@amd.com
1508879Ssteve.reinhardt@amd.com
1518879Ssteve.reinhardt@amd.com    def build_tests_json(target, source, env):
1528879Ssteve.reinhardt@amd.com        data = { test.target : test.properties() for test in tests }
1538879Ssteve.reinhardt@amd.com        with open(str(target[0]), "w") as tests_json:
1548879Ssteve.reinhardt@amd.com            json.dump(data, tests_json)
1558879Ssteve.reinhardt@amd.com
1568879Ssteve.reinhardt@amd.com    AlwaysBuild(env.Command(File('tests.json'), None,
1578120Sgblack@eecs.umich.edu                MakeAction(build_tests_json, Transform("TESTJSON"))))
1588120Sgblack@eecs.umich.edu
1598120Sgblack@eecs.umich.edu
1608120Sgblack@eecs.umich.edu    for test in tests:
1618120Sgblack@eecs.umich.edu        SystemCTestBin(test)
1628120Sgblack@eecs.umich.edu