SConscript revision 12866
16157Snate@binkert.org# Copyright 2018 Google, Inc.
26157Snate@binkert.org#
36157Snate@binkert.org# Redistribution and use in source and binary forms, with or without
46157Snate@binkert.org# modification, are permitted provided that the following conditions are
56157Snate@binkert.org# met: redistributions of source code must retain the above copyright
66157Snate@binkert.org# notice, this list of conditions and the following disclaimer;
76157Snate@binkert.org# redistributions in binary form must reproduce the above copyright
86157Snate@binkert.org# notice, this list of conditions and the following disclaimer in the
96157Snate@binkert.org# documentation and/or other materials provided with the distribution;
106157Snate@binkert.org# neither the name of the copyright holders nor the names of its
116157Snate@binkert.org# contributors may be used to endorse or promote products derived from
126157Snate@binkert.org# this software without specific prior written permission.
136157Snate@binkert.org#
146157Snate@binkert.org# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
156157Snate@binkert.org# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
166157Snate@binkert.org# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
176157Snate@binkert.org# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
186157Snate@binkert.org# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
196157Snate@binkert.org# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
206157Snate@binkert.org# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
216157Snate@binkert.org# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
226157Snate@binkert.org# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
236157Snate@binkert.org# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
246157Snate@binkert.org# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
256157Snate@binkert.org#
266157Snate@binkert.org# Authors: Gabe Black
276157Snate@binkert.org
286157Snate@binkert.orgfrom __future__ import print_function
296157Snate@binkert.org
306157Snate@binkert.orgImport('*')
316157Snate@binkert.org
326157Snate@binkert.orgif env['USE_SYSTEMC']:
336157Snate@binkert.org
346157Snate@binkert.org    from gem5_scons import Transform
356157Snate@binkert.org
366157Snate@binkert.org    import os.path
376157Snate@binkert.org    import json
386157Snate@binkert.org
396157Snate@binkert.org    src = str(Dir('.').srcdir)
4010133Sandreas.hansson@arm.com
4110133Sandreas.hansson@arm.com    class SystemCTest(object):
4210133Sandreas.hansson@arm.com        def __init__(self, dirname, name):
4310133Sandreas.hansson@arm.com            self.name = name
4410133Sandreas.hansson@arm.com            self.reldir = os.path.relpath(dirname, src)
4510133Sandreas.hansson@arm.com            self.target = os.path.join(self.reldir, name)
4610133Sandreas.hansson@arm.com            self.sources = []
4710133Sandreas.hansson@arm.com
4810133Sandreas.hansson@arm.com            self.compile_only = False
4910133Sandreas.hansson@arm.com
5010133Sandreas.hansson@arm.com        def add_source(self, source):
5110133Sandreas.hansson@arm.com            self.sources.append(os.path.join(self.reldir, source))
5210133Sandreas.hansson@arm.com
5310133Sandreas.hansson@arm.com        def add_sources(self, sources):
5410133Sandreas.hansson@arm.com            for source in sources:
5510133Sandreas.hansson@arm.com                self.sources.append(os.path.join(self.reldir, '..', source))
5610133Sandreas.hansson@arm.com
5710133Sandreas.hansson@arm.com        def properties(self):
5810133Sandreas.hansson@arm.com            return {
5910133Sandreas.hansson@arm.com                'name' : self.name,
6010133Sandreas.hansson@arm.com                'path' : self.reldir,
6110133Sandreas.hansson@arm.com                'compile_only' : self.compile_only
628492Snilay@cs.wisc.edu            }
636168Snate@binkert.org
646168Snate@binkert.org    ext_dir = Dir('..').Dir('ext')
656157Snate@binkert.org    class SystemCTestBin(UnitTest):
666157Snate@binkert.org        def __init__(self, test):
676157Snate@binkert.org            super(SystemCTestBin, self).__init__(
686157Snate@binkert.org                    test.target, *test.sources, main=True)
696157Snate@binkert.org
706157Snate@binkert.org        @classmethod
716157Snate@binkert.org        def declare_all(cls, env):
726157Snate@binkert.org            env = env.Clone()
736157Snate@binkert.org
746157Snate@binkert.org            # Turn off extra warnings and Werror for the tests.
756157Snate@binkert.org            to_remove = ['-Wall', '-Wundef', '-Wextra', '-Werror']
766157Snate@binkert.org            env['CCFLAGS'] = \
776157Snate@binkert.org                filter(lambda f: f not in to_remove, env['CCFLAGS'])
786157Snate@binkert.org
796157Snate@binkert.org            env.Append(CPPPATH=ext_dir)
806157Snate@binkert.org
816157Snate@binkert.org            super(SystemCTestBin, cls).declare_all(env)
826157Snate@binkert.org
836157Snate@binkert.org    tests = []
846157Snate@binkert.org    def new_test(dirname, name):
856157Snate@binkert.org        test = SystemCTest(dirname, name)
866157Snate@binkert.org        tests.append(test)
876157Snate@binkert.org        return test
886157Snate@binkert.org
896157Snate@binkert.org
906157Snate@binkert.org    def scan_dir_for_tests(subdir):
916157Snate@binkert.org        def visitor(arg, dirname, names):
926157Snate@binkert.org            # If there's a 'DONTRUN' file in this directory, skip it and any
936157Snate@binkert.org            # child directories.
946157Snate@binkert.org            if 'DONTRUN' in names:
956157Snate@binkert.org                del names[:]
966157Snate@binkert.org                return
976157Snate@binkert.org
986157Snate@binkert.org            endswith = lambda sfx: filter(lambda n: n.endswith(sfx), names)
996157Snate@binkert.org
1006157Snate@binkert.org            cpps = endswith('.cpp')
1016157Snate@binkert.org            if not cpps:
1026157Snate@binkert.org                return
1036157Snate@binkert.org
1046157Snate@binkert.org            # If there's only one source file, then that files name is the test
1056157Snate@binkert.org            # name, and it's the source for that test.
1066157Snate@binkert.org            if len(cpps) == 1:
1076157Snate@binkert.org                cpp = cpps[0]
1088483Sgblack@eecs.umich.edu
1098483Sgblack@eecs.umich.edu                test = new_test(dirname, os.path.splitext(cpp)[0])
1106157Snate@binkert.org                test.add_source(cpp)
1116882SBrad.Beckmann@amd.com
1126286Snate@binkert.org            # Otherwise, expect there to be a file that ends in .f. That files
1136286Snate@binkert.org            # name is the test name, and it will list the source files with
1148092Snilay@cs.wisc.edu            # one preceeding path component.
1156286Snate@binkert.org            else:
1166286Snate@binkert.org                fs = endswith('.f')
1176157Snate@binkert.org                if len(fs) != 1:
11811208Sjoseph.gross@amd.com                    print("In %s, expected 1 *.f file, but found %d.",
1196157Snate@binkert.org                          dirname, len(fs))
12010301Snilay@cs.wisc.edu                    for f in fs:
1216157Snate@binkert.org                        print(os.path.join(dirname, f))
1226157Snate@binkert.org                    return
12311122Snilay@cs.wisc.edu                f = fs[0]
12410301Snilay@cs.wisc.edu
1259363Snilay@cs.wisc.edu                test = new_test(dirname, os.path.splitext(f)[0])
12610301Snilay@cs.wisc.edu                with open(os.path.join(dirname, f)) as content:
1276286Snate@binkert.org                    lines = content.readlines
12810301Snilay@cs.wisc.edu                    # Get rid of leading and trailing whitespace.
12910301Snilay@cs.wisc.edu                    lines = map(lambda x: x.strip(), content.readlines())
13010301Snilay@cs.wisc.edu                    # Get rid of blank lines.
13110301Snilay@cs.wisc.edu                    lines = filter(lambda x: x, lines)
1326157Snate@binkert.org                    # Add all the sources to this test.
13310301Snilay@cs.wisc.edu                    test.add_sources(lines)
13410301Snilay@cs.wisc.edu
135            if 'COMPILE' in names:
136                test.compile_only = True
137
138        subdir_src = Dir('.').srcdir.Dir(subdir)
139        os.path.walk(str(subdir_src), visitor, None)
140
141    scan_dir_for_tests('systemc')
142
143
144    def build_tests_json(target, source, env):
145        data = { test.target : test.properties() for test in tests }
146        with open(str(target[0]), "w") as tests_json:
147            json.dump(data, tests_json)
148
149    AlwaysBuild(env.Command(File('tests.json'), None,
150                MakeAction(build_tests_json, Transform("TESTJSON"))))
151
152
153    for test in tests:
154        SystemCTestBin(test)
155