1# SPDX-License-Identifier: MIT
2import os
3from oeqa.core.decorator import OETestTag
4from oeqa.core.case import OEPTestResultTestCase
5from oeqa.selftest.case import OESelftestTestCase
6from oeqa.utils.commands import bitbake, get_bb_var, get_bb_vars, runqemu
7
8def parse_values(content):
9    for i in content:
10        for v in ["PASS", "FAIL", "XPASS", "XFAIL", "UNRESOLVED", "UNSUPPORTED", "UNTESTED", "ERROR", "WARNING"]:
11            if i.startswith(v + ": "):
12                yield i[len(v) + 2:].strip(), v
13                break
14
15class GccSelfTestBase(OESelftestTestCase, OEPTestResultTestCase):
16    def check_skip(self, suite):
17        targets = get_bb_var("RUNTIMETARGET", "gcc-runtime").split()
18        if suite not in targets:
19            self.skipTest("Target does not use {0}".format(suite))
20
21    def run_check(self, *suites, ssh = None):
22        targets = set()
23        for s in suites:
24            if s == "gcc":
25                targets.add("check-gcc-c")
26            elif s == "g++":
27                targets.add("check-gcc-c++")
28            else:
29                targets.add("check-target-{}".format(s))
30
31        # configure ssh target
32        features = []
33        features.append('MAKE_CHECK_TARGETS = "{0}"'.format(" ".join(targets)))
34        if ssh is not None:
35            features.append('TOOLCHAIN_TEST_TARGET = "ssh"')
36            features.append('TOOLCHAIN_TEST_HOST = "{0}"'.format(ssh))
37            features.append('TOOLCHAIN_TEST_HOST_USER = "root"')
38            features.append('TOOLCHAIN_TEST_HOST_PORT = "22"')
39        self.write_config("\n".join(features))
40
41        recipe = "gcc-runtime"
42        bitbake("{} -c check".format(recipe))
43
44        bb_vars = get_bb_vars(["B", "TARGET_SYS"], recipe)
45        builddir, target_sys = bb_vars["B"], bb_vars["TARGET_SYS"]
46
47        for suite in suites:
48            sumspath = os.path.join(builddir, "gcc", "testsuite", suite, "{0}.sum".format(suite))
49            if not os.path.exists(sumspath): # check in target dirs
50                sumspath = os.path.join(builddir, target_sys, suite, "testsuite", "{0}.sum".format(suite))
51            if not os.path.exists(sumspath): # handle libstdc++-v3 -> libstdc++
52                sumspath = os.path.join(builddir, target_sys, suite, "testsuite", "{0}.sum".format(suite.split("-")[0]))
53            logpath = os.path.splitext(sumspath)[0] + ".log"
54
55            ptestsuite = "gcc-{}".format(suite) if suite != "gcc" else suite
56            ptestsuite = ptestsuite + "-user" if ssh is None else ptestsuite
57            self.ptest_section(ptestsuite, logfile = logpath)
58            with open(sumspath, "r") as f:
59                for test, result in parse_values(f):
60                    self.ptest_result(ptestsuite, test, result)
61
62    def run_check_emulated(self, *args, **kwargs):
63        # build core-image-minimal with required packages
64        default_installed_packages = ["libgcc", "libstdc++", "libatomic", "libgomp"]
65        features = []
66        features.append('IMAGE_FEATURES += "ssh-server-openssh"')
67        features.append('CORE_IMAGE_EXTRA_INSTALL += "{0}"'.format(" ".join(default_installed_packages)))
68        self.write_config("\n".join(features))
69        bitbake("core-image-minimal")
70
71        # wrap the execution with a qemu instance
72        with runqemu("core-image-minimal", runqemuparams = "nographic") as qemu:
73            # validate that SSH is working
74            status, _ = qemu.run("uname")
75            self.assertEqual(status, 0)
76
77            return self.run_check(*args, ssh=qemu.ip, **kwargs)
78
79@OETestTag("toolchain-user")
80class GccCrossSelfTest(GccSelfTestBase):
81    def test_cross_gcc(self):
82        self.run_check("gcc")
83
84@OETestTag("toolchain-user")
85class GxxCrossSelfTest(GccSelfTestBase):
86    def test_cross_gxx(self):
87        self.run_check("g++")
88
89@OETestTag("toolchain-user")
90class GccLibAtomicSelfTest(GccSelfTestBase):
91    def test_libatomic(self):
92        self.run_check("libatomic")
93
94@OETestTag("toolchain-user")
95class GccLibGompSelfTest(GccSelfTestBase):
96    def test_libgomp(self):
97        self.run_check("libgomp")
98
99@OETestTag("toolchain-user")
100class GccLibStdCxxSelfTest(GccSelfTestBase):
101    def test_libstdcxx(self):
102        self.run_check("libstdc++-v3")
103
104@OETestTag("toolchain-user")
105class GccLibSspSelfTest(GccSelfTestBase):
106    def test_libssp(self):
107        self.check_skip("libssp")
108        self.run_check("libssp")
109
110@OETestTag("toolchain-user")
111class GccLibItmSelfTest(GccSelfTestBase):
112    def test_libitm(self):
113        self.check_skip("libitm")
114        self.run_check("libitm")
115
116@OETestTag("toolchain-system")
117@OETestTag("runqemu")
118class GccCrossSelfTestSystemEmulated(GccSelfTestBase):
119    def test_cross_gcc(self):
120        self.run_check_emulated("gcc")
121
122@OETestTag("toolchain-system")
123@OETestTag("runqemu")
124class GxxCrossSelfTestSystemEmulated(GccSelfTestBase):
125    def test_cross_gxx(self):
126        self.run_check_emulated("g++")
127
128@OETestTag("toolchain-system")
129@OETestTag("runqemu")
130class GccLibAtomicSelfTestSystemEmulated(GccSelfTestBase):
131    def test_libatomic(self):
132        self.run_check_emulated("libatomic")
133
134@OETestTag("toolchain-system")
135@OETestTag("runqemu")
136class GccLibGompSelfTestSystemEmulated(GccSelfTestBase):
137    def test_libgomp(self):
138        self.run_check_emulated("libgomp")
139
140@OETestTag("toolchain-system")
141@OETestTag("runqemu")
142class GccLibStdCxxSelfTestSystemEmulated(GccSelfTestBase):
143    def test_libstdcxx(self):
144        self.run_check_emulated("libstdc++-v3")
145
146@OETestTag("toolchain-system")
147@OETestTag("runqemu")
148class GccLibSspSelfTestSystemEmulated(GccSelfTestBase):
149    def test_libssp(self):
150        self.check_skip("libssp")
151        self.run_check_emulated("libssp")
152
153@OETestTag("toolchain-system")
154@OETestTag("runqemu")
155class GccLibItmSelfTestSystemEmulated(GccSelfTestBase):
156    def test_libitm(self):
157        self.check_skip("libitm")
158        self.run_check_emulated("libitm")
159
160