SConscript revision 13656:2727dfddacf3
1955SN/A# Copyright 2018 Google, Inc.
2955SN/A#
31762SN/A# Redistribution and use in source and binary forms, with or without
4955SN/A# modification, are permitted provided that the following conditions are
5955SN/A# met: redistributions of source code must retain the above copyright
6955SN/A# notice, this list of conditions and the following disclaimer;
7955SN/A# redistributions in binary form must reproduce the above copyright
8955SN/A# notice, this list of conditions and the following disclaimer in the
9955SN/A# documentation and/or other materials provided with the distribution;
10955SN/A# neither the name of the copyright holders nor the names of its
11955SN/A# contributors may be used to endorse or promote products derived from
12955SN/A# this software without specific prior written permission.
13955SN/A#
14955SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15955SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16955SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17955SN/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
282665Ssaidi@eecs.umich.edufrom __future__ import print_function
294762Snate@binkert.org
30955SN/AImport('*')
315522Snate@binkert.org
326143Snate@binkert.orgif env['USE_SYSTEMC']:
334762Snate@binkert.org
345522Snate@binkert.org    from gem5_scons import Transform
35955SN/A
365522Snate@binkert.org    import os.path
37955SN/A    import json
385522Snate@binkert.org
394202Sbinkertn@umich.edu    src = str(Dir('.').srcdir)
405742Snate@binkert.org
41955SN/A    class SystemCTest(object):
424381Sbinkertn@umich.edu        def __init__(self, dirname, name):
434381Sbinkertn@umich.edu            self.name = name
44955SN/A            self.reldir = os.path.relpath(dirname, src)
45955SN/A            self.target = os.path.join(self.reldir, name)
46955SN/A            self.sources = []
474202Sbinkertn@umich.edu            self.deps = []
48955SN/A
494382Sbinkertn@umich.edu            self.compile_only = False
504382Sbinkertn@umich.edu
514382Sbinkertn@umich.edu        def add_source(self, source):
526654Snate@binkert.org            self.sources.append(os.path.join(self.reldir, source))
535517Snate@binkert.org
547674Snate@binkert.org        def add_sources(self, sources):
557674Snate@binkert.org            for source in sources:
566143Snate@binkert.org                self.sources.append(os.path.join(self.reldir, '..', source))
576143Snate@binkert.org
586143Snate@binkert.org        def properties(self):
596143Snate@binkert.org            return {
606143Snate@binkert.org                'name' : self.name,
616143Snate@binkert.org                'path' : self.reldir,
626143Snate@binkert.org                'compile_only' : self.compile_only,
636143Snate@binkert.org                'deps' : self.deps
646143Snate@binkert.org            }
656143Snate@binkert.org
666143Snate@binkert.org    test_dir = Dir('.')
676143Snate@binkert.org    class SystemCTestBin(Executable):
686143Snate@binkert.org        def __init__(self, test):
696143Snate@binkert.org            super(SystemCTestBin, self).__init__(test.target, *test.sources)
706143Snate@binkert.org            self.reldir = test.reldir
714762Snate@binkert.org            self.test_deps = test.deps
726143Snate@binkert.org
736143Snate@binkert.org        @classmethod
746143Snate@binkert.org        def declare_all(cls, env):
756143Snate@binkert.org            env = env.Clone()
766143Snate@binkert.org
776143Snate@binkert.org            # Turn off extra warnings and Werror for the tests.
786143Snate@binkert.org            to_remove = ['-Wall', '-Wundef', '-Wextra', '-Werror']
796143Snate@binkert.org            env['CCFLAGS'] = \
806143Snate@binkert.org                filter(lambda f: f not in to_remove, env['CCFLAGS'])
816143Snate@binkert.org
826143Snate@binkert.org            env.Append(CPPPATH=test_dir.Dir('include'))
836143Snate@binkert.org
846143Snate@binkert.org            shared_lib_path = env['SHARED_LIB'][0].abspath
856143Snate@binkert.org            sl_dir, sl_base = os.path.split(shared_lib_path)
866143Snate@binkert.org            env.Append(LIBPATH=[sl_dir], LIBS=[sl_base])
876143Snate@binkert.org
886143Snate@binkert.org            super(SystemCTestBin, cls).declare_all(env)
896143Snate@binkert.org
906143Snate@binkert.org        def declare(self, env):
916143Snate@binkert.org            env = env.Clone()
926143Snate@binkert.org            sources = list(self.sources)
937065Snate@binkert.org            for f in self.filters:
946143Snate@binkert.org                sources += Source.all.apply_filter(f)
956143Snate@binkert.org            objs = self.srcs_to_objs(env, sources)
966143Snate@binkert.org            objs = objs + env['MAIN_OBJS']
976143Snate@binkert.org            relpath = os.path.relpath(
986143Snate@binkert.org                    env['SHARED_LIB'][0].dir.abspath,
996143Snate@binkert.org                    self.path(env).dir.abspath)
1006143Snate@binkert.org            env.Append(LINKFLAGS=Split('-z origin'))
1016143Snate@binkert.org            env.Append(RPATH=[
1026143Snate@binkert.org                    env.Literal(os.path.join('\\$$ORIGIN', relpath))])
1036143Snate@binkert.org            test_bin = super(SystemCTestBin, self).declare(env, objs)
1046143Snate@binkert.org            test_dir = self.dir.Dir(self.reldir)
1056143Snate@binkert.org            for dep in self.test_deps:
1066143Snate@binkert.org                env.Depends(test_bin, test_dir.File(dep))
1076143Snate@binkert.org            return test_bin
1086143Snate@binkert.org
1096143Snate@binkert.org    tests = []
1106143Snate@binkert.org    def new_test(dirname, name):
1116143Snate@binkert.org        test = SystemCTest(dirname, name)
1126143Snate@binkert.org        tests.append(test)
1136143Snate@binkert.org        return test
1146143Snate@binkert.org
1155522Snate@binkert.org
1166143Snate@binkert.org    def scan_dir_for_tests(subdir):
1176143Snate@binkert.org        def visitor(arg, dirname, names):
1186143Snate@binkert.org            # If there's a 'DONTRUN' file in this directory, skip it and any
1196143Snate@binkert.org            # child directories.
1206143Snate@binkert.org            if 'DONTRUN' in names:
1216143Snate@binkert.org                del names[:]
1226143Snate@binkert.org                return
1236143Snate@binkert.org
1246143Snate@binkert.org            endswith = lambda sfx: filter(lambda n: n.endswith(sfx), names)
1256143Snate@binkert.org
1265522Snate@binkert.org            cpps = endswith('.cpp')
1275522Snate@binkert.org            if not cpps:
1285522Snate@binkert.org                return
1295522Snate@binkert.org
1305604Snate@binkert.org            def get_entries(fname):
1315604Snate@binkert.org                with open(os.path.join(dirname, fname)) as content:
1326143Snate@binkert.org                    lines = content.readlines
1336143Snate@binkert.org                    # Get rid of leading and trailing whitespace.
1344762Snate@binkert.org                    lines = map(lambda x: x.strip(), content.readlines())
1354762Snate@binkert.org                    # Get rid of blank lines.
1366143Snate@binkert.org                    lines = filter(lambda x: x, lines)
1376727Ssteve.reinhardt@amd.com                    return lines
1386727Ssteve.reinhardt@amd.com
1396727Ssteve.reinhardt@amd.com            # If there's only one source file, then that files name is the test
1404762Snate@binkert.org            # name, and it's the source for that test.
1416143Snate@binkert.org            if len(cpps) == 1:
1426143Snate@binkert.org                cpp = cpps[0]
1436143Snate@binkert.org
1446143Snate@binkert.org                test = new_test(dirname, os.path.splitext(cpp)[0])
1456727Ssteve.reinhardt@amd.com                test.add_source(cpp)
1466143Snate@binkert.org
1477674Snate@binkert.org            # Otherwise, expect there to be a file that ends in .f. That files
1487674Snate@binkert.org            # name is the test name, and it will list the source files with
1495604Snate@binkert.org            # one preceeding path component.
1506143Snate@binkert.org            else:
1516143Snate@binkert.org                fs = endswith('.f')
1526143Snate@binkert.org                if len(fs) != 1:
1534762Snate@binkert.org                    print("In %s, expected 1 *.f file, but found %d.",
1546143Snate@binkert.org                          dirname, len(fs))
1554762Snate@binkert.org                    for f in fs:
1564762Snate@binkert.org                        print(os.path.join(dirname, f))
1574762Snate@binkert.org                    return
1586143Snate@binkert.org                f = fs[0]
1596143Snate@binkert.org
1604762Snate@binkert.org                test = new_test(dirname, os.path.splitext(f)[0])
1616143Snate@binkert.org                # Add all the sources to this test.
1626143Snate@binkert.org                test.add_sources(get_entries(f))
1636143Snate@binkert.org
1646143Snate@binkert.org            if 'COMPILE' in names:
1654762Snate@binkert.org                test.compile_only = True
1666143Snate@binkert.org
1674762Snate@binkert.org            if 'DEPS' in names:
1686143Snate@binkert.org                test.deps = get_entries('DEPS')
1694762Snate@binkert.org
1706143Snate@binkert.org        subdir_src = Dir('.').srcdir.Dir(subdir)
1716143Snate@binkert.org        os.path.walk(str(subdir_src), visitor, None)
1726143Snate@binkert.org
1736143Snate@binkert.org    scan_dir_for_tests('systemc')
1746143Snate@binkert.org    scan_dir_for_tests('tlm')
1756143Snate@binkert.org
1766143Snate@binkert.org
1776143Snate@binkert.org    def build_tests_json(target, source, env):
1786143Snate@binkert.org        data = { test.target : test.properties() for test in tests }
1796143Snate@binkert.org        with open(str(target[0]), "w") as tests_json:
1806143Snate@binkert.org            json.dump(data, tests_json)
1816143Snate@binkert.org
1826143Snate@binkert.org    AlwaysBuild(env.Command(File('tests.json'), None,
183955SN/A                MakeAction(build_tests_json, Transform("TESTJSON"))))
1845584Snate@binkert.org
1855584Snate@binkert.org
1865584Snate@binkert.org    for test in tests:
1875584Snate@binkert.org        SystemCTestBin(test)
1886143Snate@binkert.org