llvm.org GIT mirror llvm / d929ec6
Use AIX version detection at LLVM run-time Summary: AIX compilers define macros based on the version of the operating system. This patch implements updating of versionless AIX triples to include the host AIX version. Also, the host triple detection in the build system is adjusted to strip the AIX version information so that the run-time detection is preferred. Reviewers: xingxue, stefanp, nemanjai, jasonliu Reviewed By: xingxue Subscribers: mgorny, kristina, jdoerfert, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D58798 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@355995 91177308-0d34-0410-b5e6-96231b3b80d8 Hubert Tong 7 months ago
3 changed file(s) with 111 addition(s) and 25 deletion(s). Raw diff Collapse all Expand all
2222 if( NOT TT_RV EQUAL 0 )
2323 message(FATAL_ERROR "Failed to execute ${config_guess}")
2424 endif( NOT TT_RV EQUAL 0 )
25 set( value ${TT_OUT} )
25 # Defer to dynamic detection of the host AIX version.
26 string(REGEX REPLACE "-aix[0-9][^-]*" "-aix" value ${TT_OUT})
2627 endif( MSVC )
2728 set( ${var} ${value} PARENT_SCOPE )
2829 endfunction( get_host_triple var )
4848 TargetTripleString += "-darwin";
4949 TargetTripleString += getOSVersion();
5050 }
51 // On AIX, the AIX version and release should be that of the current host
52 // unless if the version has already been specified.
53 if (Triple(LLVM_HOST_TRIPLE).getOS() == Triple::AIX) {
54 Triple TT(TargetTripleString);
55 if (TT.getOS() == Triple::AIX && !TT.getOSMajorVersion()) {
56 struct utsname name;
57 if (uname(&name) != -1) {
58 std::string NewOSName = Triple::getOSTypeName(Triple::AIX);
59 NewOSName += name.version;
60 NewOSName += '.';
61 NewOSName += name.release;
62 NewOSName += ".0.0";
63 TT.setOSName(NewOSName);
64 return TT.str();
65 }
66 }
67 }
5168 return TargetTripleString;
5269 }
5370
66 //===----------------------------------------------------------------------===//
77
88 #include "llvm/Support/Host.h"
9 #include "llvm/Config/llvm-config.h"
910 #include "llvm/ADT/SmallVector.h"
1011 #include "llvm/ADT/Triple.h"
1112 #include "llvm/Support/FileSystem.h"
247248 "tsv110");
248249 }
249250
251 static bool runAndGetCommandOutput(
252 const char *ExePath, ArrayRef argv,
253 std::unique_ptr &Buffer, off_t &Size) {
254 bool Success = false;
255 [ExePath, argv, &Buffer, &Size, &Success] {
256 using namespace llvm::sys;
257 SmallString<128> TestDirectory;
258 ASSERT_NO_ERROR(fs::createUniqueDirectory("host_test", TestDirectory));
259
260 SmallString<128> OutputFile(TestDirectory);
261 path::append(OutputFile, "out");
262 StringRef OutputPath = OutputFile.str();
263
264 const Optional Redirects[] = {
265 /*STDIN=*/None, /*STDOUT=*/OutputPath, /*STDERR=*/None};
266 int RetCode = ExecuteAndWait(ExePath, argv, /*env=*/llvm::None, Redirects);
267 ASSERT_EQ(0, RetCode);
268
269 int FD = 0;
270 ASSERT_NO_ERROR(fs::openFileForRead(OutputPath, FD));
271 Size = ::lseek(FD, 0, SEEK_END);
272 ASSERT_NE(-1, Size);
273 ::lseek(FD, 0, SEEK_SET);
274 Buffer = llvm::make_unique(Size);
275 ASSERT_EQ(::read(FD, Buffer.get(), Size), Size);
276 ::close(FD);
277
278 ASSERT_NO_ERROR(fs::remove(OutputPath));
279 ASSERT_NO_ERROR(fs::remove(TestDirectory.str()));
280 Success = true;
281 }();
282 return Success;
283 }
284
285 TEST_F(HostTest, DummyRunAndGetCommandOutputUse) {
286 // Suppress defined-but-not-used warnings when the tests using the helper are
287 // disabled.
288 (void) runAndGetCommandOutput;
289 }
290
250291 #if defined(__APPLE__)
251292 TEST_F(HostTest, getMacOSHostVersion) {
252293 using namespace llvm::sys;
254295 if (!HostTriple.isMacOSX())
255296 return;
256297
257 SmallString<128> TestDirectory;
258 ASSERT_NO_ERROR(fs::createUniqueDirectory("host_test", TestDirectory));
259 SmallString<128> OutputFile(TestDirectory);
260 path::append(OutputFile, "out");
261
262298 const char *SwVersPath = "/usr/bin/sw_vers";
263299 StringRef argv[] = {SwVersPath, "-productVersion"};
264 StringRef OutputPath = OutputFile.str();
265 const Optional Redirects[] = {/*STDIN=*/None,
266 /*STDOUT=*/OutputPath,
267 /*STDERR=*/None};
268 int RetCode = ExecuteAndWait(SwVersPath, argv, /*env=*/llvm::None, Redirects);
269 ASSERT_EQ(0, RetCode);
270
271 int FD = 0;
272 ASSERT_NO_ERROR(fs::openFileForRead(OutputPath, FD));
273 off_t Size = ::lseek(FD, 0, SEEK_END);
274 ASSERT_NE(-1, Size);
275 ::lseek(FD, 0, SEEK_SET);
276 std::unique_ptr Buffer = llvm::make_unique(Size);
277 ASSERT_EQ(::read(FD, Buffer.get(), Size), Size);
278 ::close(FD);
300 std::unique_ptr Buffer;
301 off_t Size;
302 ASSERT_EQ(runAndGetCommandOutput(SwVersPath, argv, Buffer, Size), true);
303 StringRef SystemVersion(Buffer.get(), Size);
279304
280305 // Ensure that the two versions match.
281 StringRef SystemVersion(Buffer.get(), Size);
282306 unsigned SystemMajor, SystemMinor, SystemMicro;
283307 ASSERT_EQ(llvm::Triple((Twine("x86_64-apple-macos") + SystemVersion))
284308 .getMacOSXVersion(SystemMajor, SystemMinor, SystemMicro),
289313 // Don't compare the 'Micro' version, as it's always '0' for the 'Darwin'
290314 // triples.
291315 ASSERT_EQ(std::tie(SystemMajor, SystemMinor), std::tie(HostMajor, HostMinor));
292
293 ASSERT_NO_ERROR(fs::remove(OutputPath));
294 ASSERT_NO_ERROR(fs::remove(TestDirectory.str()));
295316 }
296317 #endif
318
319 #if defined(_AIX)
320 TEST_F(HostTest, AIXVersionDetect) {
321 using namespace llvm::sys;
322
323 llvm::Triple HostTriple(getProcessTriple());
324 ASSERT_EQ(HostTriple.getOS(), Triple::AIX);
325
326 llvm::Triple ConfiguredHostTriple(LLVM_HOST_TRIPLE);
327 ASSERT_EQ(ConfiguredHostTriple.getOS(), Triple::AIX);
328
329 const char *ExePath = "/usr/bin/oslevel";
330 StringRef argv[] = {ExePath};
331 std::unique_ptr Buffer;
332 off_t Size;
333 ASSERT_EQ(runAndGetCommandOutput(ExePath, argv, Buffer, Size), true);
334 StringRef SystemVersion(Buffer.get(), Size);
335
336 unsigned SystemMajor, SystemMinor, SystemMicro;
337 llvm::Triple((Twine("powerpc-ibm-aix") + SystemVersion))
338 .getOSVersion(SystemMajor, SystemMinor, SystemMicro);
339
340 // Ensure that the host triple version (major) and release (minor) numbers,
341 // unless explicitly configured, match with those of the current system.
342 if (!ConfiguredHostTriple.getOSMajorVersion()) {
343 unsigned HostMajor, HostMinor, HostMicro;
344 HostTriple.getOSVersion(HostMajor, HostMinor, HostMicro);
345 ASSERT_EQ(std::tie(SystemMajor, SystemMinor),
346 std::tie(HostMajor, HostMinor));
347 }
348
349 llvm::Triple TargetTriple(getDefaultTargetTriple());
350 if (TargetTriple.getOS() != Triple::AIX)
351 return;
352
353 // Ensure that the target triple version (major) and release (minor) numbers
354 // match with those of the current system.
355 llvm::Triple ConfiguredTargetTriple(LLVM_DEFAULT_TARGET_TRIPLE);
356 if (ConfiguredTargetTriple.getOSMajorVersion())
357 return; // The version was configured explicitly; skip.
358
359 unsigned TargetMajor, TargetMinor, TargetMicro;
360 TargetTriple.getOSVersion(TargetMajor, TargetMinor, TargetMicro);
361 ASSERT_EQ(std::tie(SystemMajor, SystemMinor),
362 std::tie(TargetMajor, TargetMinor));
363 }
364 #endif