SConscript revision 13458
16757SAli.Saidi@ARM.com# Copyright 2018 Google, Inc.
211574SCurtis.Dunham@arm.com#
36757SAli.Saidi@ARM.com# Redistribution and use in source and binary forms, with or without
46757SAli.Saidi@ARM.com# modification, are permitted provided that the following conditions are
57111Sgblack@eecs.umich.edu# met: redistributions of source code must retain the above copyright
67111Sgblack@eecs.umich.edu# notice, this list of conditions and the following disclaimer;
77111Sgblack@eecs.umich.edu# redistributions in binary form must reproduce the above copyright
87111Sgblack@eecs.umich.edu# notice, this list of conditions and the following disclaimer in the
97111Sgblack@eecs.umich.edu# documentation and/or other materials provided with the distribution;
107111Sgblack@eecs.umich.edu# neither the name of the copyright holders nor the names of its
117111Sgblack@eecs.umich.edu# contributors may be used to endorse or promote products derived from
127111Sgblack@eecs.umich.edu# this software without specific prior written permission.
137111Sgblack@eecs.umich.edu#
146757SAli.Saidi@ARM.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
156757SAli.Saidi@ARM.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
166757SAli.Saidi@ARM.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
176757SAli.Saidi@ARM.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
186757SAli.Saidi@ARM.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
196757SAli.Saidi@ARM.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
206757SAli.Saidi@ARM.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
216757SAli.Saidi@ARM.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
226757SAli.Saidi@ARM.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
236757SAli.Saidi@ARM.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
246757SAli.Saidi@ARM.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
256757SAli.Saidi@ARM.com#
266757SAli.Saidi@ARM.com# Authors: Gabe Black
276757SAli.Saidi@ARM.com
286757SAli.Saidi@ARM.comfrom __future__ import print_function
296757SAli.Saidi@ARM.com
306757SAli.Saidi@ARM.comImport('*')
316757SAli.Saidi@ARM.com
326757SAli.Saidi@ARM.comif env['USE_SYSTEMC']:
336757SAli.Saidi@ARM.com
346757SAli.Saidi@ARM.com    from gem5_scons import Transform
356757SAli.Saidi@ARM.com
366757SAli.Saidi@ARM.com    import os.path
376757SAli.Saidi@ARM.com    import json
386757SAli.Saidi@ARM.com
396735Sgblack@eecs.umich.edu    src = str(Dir('.').srcdir)
4010474Sandreas.hansson@arm.com
416757SAli.Saidi@ARM.com    class SystemCTest(object):
426757SAli.Saidi@ARM.com        def __init__(self, dirname, name):
437707Sgblack@eecs.umich.edu            self.name = name
4410037SARM gem5 Developers            self.reldir = os.path.relpath(dirname, src)
458782Sgblack@eecs.umich.edu            self.target = os.path.join(self.reldir, name)
466757SAli.Saidi@ARM.com            self.sources = []
478782Sgblack@eecs.umich.edu            self.deps = []
488887Sgeoffrey.blake@arm.com
498886SAli.Saidi@ARM.com            self.compile_only = False
506757SAli.Saidi@ARM.com
518706Sandreas.hansson@arm.com        def add_source(self, source):
528782Sgblack@eecs.umich.edu            self.sources.append(os.path.join(self.reldir, source))
537749SAli.Saidi@ARM.com
546735Sgblack@eecs.umich.edu        def add_sources(self, sources):
556735Sgblack@eecs.umich.edu            for source in sources:
566735Sgblack@eecs.umich.edu                self.sources.append(os.path.join(self.reldir, '..', source))
576735Sgblack@eecs.umich.edu
586735Sgblack@eecs.umich.edu        def properties(self):
596735Sgblack@eecs.umich.edu            return {
609058Satgutier@umich.edu                'name' : self.name,
616735Sgblack@eecs.umich.edu                'path' : self.reldir,
628886SAli.Saidi@ARM.com                'compile_only' : self.compile_only,
6310474Sandreas.hansson@arm.com                'deps' : self.deps
648286SAli.Saidi@ARM.com            }
656735Sgblack@eecs.umich.edu
666735Sgblack@eecs.umich.edu    test_dir = Dir('.')
677707Sgblack@eecs.umich.edu    class SystemCTestBin(Executable):
687707Sgblack@eecs.umich.edu        def __init__(self, test):
697707Sgblack@eecs.umich.edu            super(SystemCTestBin, self).__init__(test.target, *test.sources)
708806Sgblack@eecs.umich.edu            self.reldir = test.reldir
718806Sgblack@eecs.umich.edu            self.test_deps = test.deps
728806Sgblack@eecs.umich.edu
738806Sgblack@eecs.umich.edu        @classmethod
748706Sandreas.hansson@arm.com        def declare_all(cls, env):
757693SAli.Saidi@ARM.com            env = env.Clone()
767693SAli.Saidi@ARM.com
777693SAli.Saidi@ARM.com            # Turn off extra warnings and Werror for the tests.
7810037SARM gem5 Developers            to_remove = ['-Wall', '-Wundef', '-Wextra', '-Werror']
7910037SARM gem5 Developers            env['CCFLAGS'] = \
8010037SARM gem5 Developers                filter(lambda f: f not in to_remove, env['CCFLAGS'])
8110037SARM gem5 Developers
8210037SARM gem5 Developers            env.Append(CPPPATH=test_dir.Dir('include'))
8310037SARM gem5 Developers
847693SAli.Saidi@ARM.com            shared_lib_path = env['SHARED_LIB'][0].abspath
8510037SARM gem5 Developers            sl_dir, sl_base = os.path.split(shared_lib_path)
867693SAli.Saidi@ARM.com            env.Append(LIBPATH=[sl_dir], LIBS=[sl_base])
877693SAli.Saidi@ARM.com
8810037SARM gem5 Developers            super(SystemCTestBin, cls).declare_all(env)
8910318Sandreas.hansson@arm.com
9010037SARM gem5 Developers        def declare(self, env):
9110037SARM gem5 Developers            env = env.Clone()
9210037SARM gem5 Developers            sources = list(self.sources)
9310037SARM gem5 Developers            for f in self.filters:
9410037SARM gem5 Developers                sources = Source.all.apply_filter(f)
9510037SARM gem5 Developers            objs = self.srcs_to_objs(env, sources)
9610037SARM gem5 Developers            objs = objs + env['MAIN_OBJS']
9710037SARM gem5 Developers            relpath = os.path.relpath(
9810037SARM gem5 Developers                    env['SHARED_LIB'][0].dir.abspath,
9910037SARM gem5 Developers                    self.path(env).dir.abspath)
10010037SARM gem5 Developers            env.Append(LINKFLAGS=Split('-z origin'))
10110037SARM gem5 Developers            env.Append(RPATH=env.Literal(os.path.join('\\$$ORIGIN', relpath)))
10210037SARM gem5 Developers            test_bin = super(SystemCTestBin, self).declare(env, objs)
10310037SARM gem5 Developers            test_dir = self.dir.Dir(self.reldir)
10410037SARM gem5 Developers            for dep in self.test_deps:
10510037SARM gem5 Developers                env.Depends(test_bin, test_dir.File(dep))
10610037SARM gem5 Developers            return test_bin
10710037SARM gem5 Developers
10810037SARM gem5 Developers    tests = []
10910037SARM gem5 Developers    def new_test(dirname, name):
11010037SARM gem5 Developers        test = SystemCTest(dirname, name)
11110037SARM gem5 Developers        tests.append(test)
11210037SARM gem5 Developers        return test
11310037SARM gem5 Developers
11410037SARM gem5 Developers
11510037SARM gem5 Developers    def scan_dir_for_tests(subdir):
11610037SARM gem5 Developers        def visitor(arg, dirname, names):
11710037SARM gem5 Developers            # If there's a 'DONTRUN' file in this directory, skip it and any
1187693SAli.Saidi@ARM.com            # child directories.
11910037SARM gem5 Developers            if 'DONTRUN' in names:
12010037SARM gem5 Developers                del names[:]
12110037SARM gem5 Developers                return
12210037SARM gem5 Developers
12310037SARM gem5 Developers            endswith = lambda sfx: filter(lambda n: n.endswith(sfx), names)
1247693SAli.Saidi@ARM.com
1257650SAli.Saidi@ARM.com            cpps = endswith('.cpp')
12610037SARM gem5 Developers            if not cpps:
1276757SAli.Saidi@ARM.com                return
1286757SAli.Saidi@ARM.com
1297693SAli.Saidi@ARM.com            def get_entries(fname):
1307693SAli.Saidi@ARM.com                with open(os.path.join(dirname, fname)) as content:
1317693SAli.Saidi@ARM.com                    lines = content.readlines
1329920Syasuko.eckert@amd.com                    # Get rid of leading and trailing whitespace.
13310037SARM gem5 Developers                    lines = map(lambda x: x.strip(), content.readlines())
13410037SARM gem5 Developers                    # Get rid of blank lines.
13510037SARM gem5 Developers                    lines = filter(lambda x: x, lines)
13610037SARM gem5 Developers                    return lines
13710037SARM gem5 Developers
1388887Sgeoffrey.blake@arm.com            # If there's only one source file, then that files name is the test
1398887Sgeoffrey.blake@arm.com            # name, and it's the source for that test.
1408887Sgeoffrey.blake@arm.com            if len(cpps) == 1:
1418887Sgeoffrey.blake@arm.com                cpp = cpps[0]
1428887Sgeoffrey.blake@arm.com
1438887Sgeoffrey.blake@arm.com                test = new_test(dirname, os.path.splitext(cpp)[0])
1448887Sgeoffrey.blake@arm.com                test.add_source(cpp)
1457693SAli.Saidi@ARM.com
1467693SAli.Saidi@ARM.com            # Otherwise, expect there to be a file that ends in .f. That files
1477748SAli.Saidi@ARM.com            # name is the test name, and it will list the source files with
1487748SAli.Saidi@ARM.com            # one preceeding path component.
1497748SAli.Saidi@ARM.com            else:
1509920Syasuko.eckert@amd.com                fs = endswith('.f')
1519431SAndreas.Sandberg@ARM.com                if len(fs) != 1:
1528208SAli.Saidi@ARM.com                    print("In %s, expected 1 *.f file, but found %d.",
1539920Syasuko.eckert@amd.com                          dirname, len(fs))
1549431SAndreas.Sandberg@ARM.com                    for f in fs:
1558208SAli.Saidi@ARM.com                        print(os.path.join(dirname, f))
15610338SCurtis.Dunham@arm.com                    return
15710338SCurtis.Dunham@arm.com                f = fs[0]
1589920Syasuko.eckert@amd.com
1599920Syasuko.eckert@amd.com                test = new_test(dirname, os.path.splitext(f)[0])
1607748SAli.Saidi@ARM.com                # Add all the sources to this test.
1616759SAli.Saidi@ARM.com                test.add_sources(get_entries(f))
1627748SAli.Saidi@ARM.com
1637748SAli.Saidi@ARM.com            if 'COMPILE' in names:
1647748SAli.Saidi@ARM.com                test.compile_only = True
1657748SAli.Saidi@ARM.com
1667749SAli.Saidi@ARM.com            if 'DEPS' in names:
1677748SAli.Saidi@ARM.com                test.deps = get_entries('DEPS')
1687749SAli.Saidi@ARM.com
1697749SAli.Saidi@ARM.com        subdir_src = Dir('.').srcdir.Dir(subdir)
1707749SAli.Saidi@ARM.com        os.path.walk(str(subdir_src), visitor, None)
1717749SAli.Saidi@ARM.com
1726759SAli.Saidi@ARM.com    scan_dir_for_tests('systemc')
1737752SWilliam.Wang@arm.com
17410037SARM gem5 Developers
17510037SARM gem5 Developers    def build_tests_json(target, source, env):
17610037SARM gem5 Developers        data = { test.target : test.properties() for test in tests }
17710037SARM gem5 Developers        with open(str(target[0]), "w") as tests_json:
17810037SARM gem5 Developers            json.dump(data, tests_json)
17910037SARM gem5 Developers
18010037SARM gem5 Developers    AlwaysBuild(env.Command(File('tests.json'), None,
18110037SARM gem5 Developers                MakeAction(build_tests_json, Transform("TESTJSON"))))
18210037SARM gem5 Developers
18310037SARM gem5 Developers
18410037SARM gem5 Developers    for test in tests:
18510037SARM gem5 Developers        SystemCTestBin(test)
18610037SARM gem5 Developers