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