BuildBot fix, compiler complains about array decay to pointer
[llvm-core.git] / unittests / Support / Host.cpp
blob623591803e07abc8608ab80359c1181f5c1af146
1 //========- unittests/Support/Host.cpp - Host.cpp tests --------------========//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/Host.h"
11 #include "llvm/ADT/SmallVector.h"
12 #include "llvm/ADT/Triple.h"
13 #include "llvm/Support/FileSystem.h"
14 #include "llvm/Support/Path.h"
15 #include "llvm/Support/Program.h"
17 #include "gtest/gtest.h"
19 #define ASSERT_NO_ERROR(x) \
20 if (std::error_code ASSERT_NO_ERROR_ec = x) { \
21 SmallString<128> MessageStorage; \
22 raw_svector_ostream Message(MessageStorage); \
23 Message << #x ": did not return errc::success.\n" \
24 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
25 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
26 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
27 } else { \
30 using namespace llvm;
32 class HostTest : public testing::Test {
33 Triple Host;
35 protected:
36 bool isSupportedArchAndOS() {
37 // Initially this is only testing detection of the number of
38 // physical cores, which is currently only supported/tested for
39 // x86_64 Linux and Darwin.
40 return (Host.getArch() == Triple::x86_64 &&
41 (Host.isOSDarwin() || Host.getOS() == Triple::Linux));
44 HostTest() : Host(Triple::normalize(sys::getProcessTriple())) {}
47 TEST_F(HostTest, NumPhysicalCores) {
48 int Num = sys::getHostNumPhysicalCores();
50 if (isSupportedArchAndOS())
51 ASSERT_GT(Num, 0);
52 else
53 ASSERT_EQ(Num, -1);
56 TEST(getLinuxHostCPUName, ARM) {
57 StringRef CortexA9ProcCpuinfo = R"(
58 processor : 0
59 model name : ARMv7 Processor rev 10 (v7l)
60 BogoMIPS : 1393.66
61 Features : half thumb fastmult vfp edsp thumbee neon vfpv3 tls vfpd32
62 CPU implementer : 0x41
63 CPU architecture: 7
64 CPU variant : 0x2
65 CPU part : 0xc09
66 CPU revision : 10
68 processor : 1
69 model name : ARMv7 Processor rev 10 (v7l)
70 BogoMIPS : 1393.66
71 Features : half thumb fastmult vfp edsp thumbee neon vfpv3 tls vfpd32
72 CPU implementer : 0x41
73 CPU architecture: 7
74 CPU variant : 0x2
75 CPU part : 0xc09
76 CPU revision : 10
78 Hardware : Generic OMAP4 (Flattened Device Tree)
79 Revision : 0000
80 Serial : 0000000000000000
81 )";
83 EXPECT_EQ(sys::detail::getHostCPUNameForARM(CortexA9ProcCpuinfo),
84 "cortex-a9");
85 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x41\n"
86 "CPU part : 0xc0f"),
87 "cortex-a15");
88 // Verify that both CPU implementer and CPU part are checked:
89 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x40\n"
90 "CPU part : 0xc0f"),
91 "generic");
92 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x51\n"
93 "CPU part : 0x06f"),
94 "krait");
97 TEST(getLinuxHostCPUName, AArch64) {
98 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x41\n"
99 "CPU part : 0xd03"),
100 "cortex-a53");
101 // Verify that both CPU implementer and CPU part are checked:
102 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x40\n"
103 "CPU part : 0xd03"),
104 "generic");
105 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x51\n"
106 "CPU part : 0x201"),
107 "kryo");
108 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x51\n"
109 "CPU part : 0x800"),
110 "cortex-a73");
111 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x51\n"
112 "CPU part : 0x801"),
113 "cortex-a73");
114 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x51\n"
115 "CPU part : 0xc00"),
116 "falkor");
117 EXPECT_EQ(sys::detail::getHostCPUNameForARM("CPU implementer : 0x51\n"
118 "CPU part : 0xc01"),
119 "saphira");
121 // MSM8992/4 weirdness
122 StringRef MSM8992ProcCpuInfo = R"(
123 Processor : AArch64 Processor rev 3 (aarch64)
124 processor : 0
125 processor : 1
126 processor : 2
127 processor : 3
128 processor : 4
129 processor : 5
130 Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
131 CPU implementer : 0x41
132 CPU architecture: 8
133 CPU variant : 0x0
134 CPU part : 0xd03
135 CPU revision : 3
137 Hardware : Qualcomm Technologies, Inc MSM8992
140 EXPECT_EQ(sys::detail::getHostCPUNameForARM(MSM8992ProcCpuInfo),
141 "cortex-a53");
143 // Exynos big.LITTLE weirdness
144 const std::string ExynosProcCpuInfo = R"(
145 processor : 0
146 Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
147 CPU implementer : 0x41
148 CPU architecture: 8
149 CPU variant : 0x0
150 CPU part : 0xd03
152 processor : 1
153 Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
154 CPU implementer : 0x53
155 CPU architecture: 8
158 // Verify default for Exynos.
159 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ExynosProcCpuInfo +
160 "CPU variant : 0xc\n"
161 "CPU part : 0xafe"),
162 "exynos-m1");
163 // Verify Exynos M1.
164 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ExynosProcCpuInfo +
165 "CPU variant : 0x1\n"
166 "CPU part : 0x001"),
167 "exynos-m1");
168 // Verify Exynos M2.
169 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ExynosProcCpuInfo +
170 "CPU variant : 0x4\n"
171 "CPU part : 0x001"),
172 "exynos-m2");
174 const std::string ThunderX2T99ProcCpuInfo = R"(
175 processor : 0
176 BogoMIPS : 400.00
177 Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics
178 CPU implementer : 0x43
179 CPU architecture: 8
180 CPU variant : 0x1
181 CPU part : 0x0af
184 // Verify different versions of ThunderX2T99.
185 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo +
186 "CPU implementer : 0x42\n"
187 "CPU part : 0x516"),
188 "thunderx2t99");
190 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo +
191 "CPU implementer : 0x42\n"
192 "CPU part : 0x0516"),
193 "thunderx2t99");
195 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo +
196 "CPU implementer : 0x43\n"
197 "CPU part : 0x516"),
198 "thunderx2t99");
200 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo +
201 "CPU implementer : 0x43\n"
202 "CPU part : 0x0516"),
203 "thunderx2t99");
205 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo +
206 "CPU implementer : 0x42\n"
207 "CPU part : 0xaf"),
208 "thunderx2t99");
210 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo +
211 "CPU implementer : 0x42\n"
212 "CPU part : 0x0af"),
213 "thunderx2t99");
215 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo +
216 "CPU implementer : 0x43\n"
217 "CPU part : 0xaf"),
218 "thunderx2t99");
220 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderX2T99ProcCpuInfo +
221 "CPU implementer : 0x43\n"
222 "CPU part : 0x0af"),
223 "thunderx2t99");
225 // Verify ThunderXT88.
226 const std::string ThunderXT88ProcCpuInfo = R"(
227 processor : 0
228 BogoMIPS : 200.00
229 Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
230 CPU implementer : 0x43
231 CPU architecture: 8
232 CPU variant : 0x1
233 CPU part : 0x0a1
236 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderXT88ProcCpuInfo +
237 "CPU implementer : 0x43\n"
238 "CPU part : 0x0a1"),
239 "thunderxt88");
241 EXPECT_EQ(sys::detail::getHostCPUNameForARM(ThunderXT88ProcCpuInfo +
242 "CPU implementer : 0x43\n"
243 "CPU part : 0xa1"),
244 "thunderxt88");
247 #if defined(__APPLE__)
248 TEST_F(HostTest, getMacOSHostVersion) {
249 using namespace llvm::sys;
250 llvm::Triple HostTriple(getProcessTriple());
251 if (!HostTriple.isMacOSX())
252 return;
254 SmallString<128> TestDirectory;
255 ASSERT_NO_ERROR(fs::createUniqueDirectory("host_test", TestDirectory));
256 SmallString<128> OutputFile(TestDirectory);
257 path::append(OutputFile, "out");
259 const char *SwVersPath = "/usr/bin/sw_vers";
260 StringRef argv[] = {SwVersPath, "-productVersion"};
261 StringRef OutputPath = OutputFile.str();
262 const Optional<StringRef> Redirects[] = {/*STDIN=*/None,
263 /*STDOUT=*/OutputPath,
264 /*STDERR=*/None};
265 int RetCode = ExecuteAndWait(SwVersPath, argv, /*env=*/llvm::None, Redirects);
266 ASSERT_EQ(0, RetCode);
268 int FD = 0;
269 ASSERT_NO_ERROR(fs::openFileForRead(OutputPath, FD));
270 off_t Size = ::lseek(FD, 0, SEEK_END);
271 ASSERT_NE(-1, Size);
272 ::lseek(FD, 0, SEEK_SET);
273 std::unique_ptr<char[]> Buffer = llvm::make_unique<char[]>(Size);
274 ASSERT_EQ(::read(FD, Buffer.get(), Size), Size);
275 ::close(FD);
277 // Ensure that the two versions match.
278 StringRef SystemVersion(Buffer.get(), Size);
279 unsigned SystemMajor, SystemMinor, SystemMicro;
280 ASSERT_EQ(llvm::Triple((Twine("x86_64-apple-macos") + SystemVersion))
281 .getMacOSXVersion(SystemMajor, SystemMinor, SystemMicro),
282 true);
283 unsigned HostMajor, HostMinor, HostMicro;
284 ASSERT_EQ(HostTriple.getMacOSXVersion(HostMajor, HostMinor, HostMicro), true);
286 // Don't compare the 'Micro' version, as it's always '0' for the 'Darwin'
287 // triples.
288 ASSERT_EQ(std::tie(SystemMajor, SystemMinor), std::tie(HostMajor, HostMinor));
290 ASSERT_NO_ERROR(fs::remove(OutputPath));
291 ASSERT_NO_ERROR(fs::remove(TestDirectory.str()));
293 #endif