From: Alex Lorenz Date: Fri, 7 Jul 2017 09:53:47 +0000 (+0000) Subject: [Support] sys::getProcessTriple should return a macOS triple using X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=25b2f9273d88ee530ec4156966fec654e58b8d0e;p=llvm [Support] sys::getProcessTriple should return a macOS triple using the system's version of macOS sys::getProcessTriple returns LLVM_HOST_TRIPLE, whose system version might not be the actual version of the system on which the compiler running. This commit ensures that, for macOS, sys::getProcessTriple returns a triple with the system's macOS version. rdar://33177551 Differential Revision: https://reviews.llvm.org/D34446 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@307372 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Support/Host.cpp b/lib/Support/Host.cpp index 232efe648b0..31d43fed193 100644 --- a/lib/Support/Host.cpp +++ b/lib/Support/Host.cpp @@ -1494,7 +1494,8 @@ bool sys::getHostCPUFeatures(StringMap &Features) { return false; } #endif std::string sys::getProcessTriple() { - Triple PT(Triple::normalize(LLVM_HOST_TRIPLE)); + std::string TargetTripleString = updateTripleOSVersion(LLVM_HOST_TRIPLE); + Triple PT(Triple::normalize(TargetTripleString)); if (sizeof(void *) == 8 && PT.isArch32Bit()) PT = PT.get64BitArchVariant(); diff --git a/lib/Support/Unix/Host.inc b/lib/Support/Unix/Host.inc index 0ba6a25aa19..5580e63893c 100644 --- a/lib/Support/Unix/Host.inc +++ b/lib/Support/Unix/Host.inc @@ -34,18 +34,31 @@ static std::string getOSVersion() { return info.release; } -std::string sys::getDefaultTargetTriple() { - std::string TargetTripleString(LLVM_DEFAULT_TARGET_TRIPLE); - - // On darwin, we want to update the version to match that of the - // target. +static std::string updateTripleOSVersion(std::string TargetTripleString) { + // On darwin, we want to update the version to match that of the target. std::string::size_type DarwinDashIdx = TargetTripleString.find("-darwin"); if (DarwinDashIdx != std::string::npos) { TargetTripleString.resize(DarwinDashIdx + strlen("-darwin")); TargetTripleString += getOSVersion(); + return TargetTripleString; + } + std::string::size_type MacOSDashIdx = TargetTripleString.find("-macos"); + if (MacOSDashIdx != std::string::npos) { + TargetTripleString.resize(MacOSDashIdx); + // Reset the OS to darwin as the OS version from `uname` doesn't use the + // macOS version scheme. + TargetTripleString += "-darwin"; + TargetTripleString += getOSVersion(); } + return TargetTripleString; +} + +std::string sys::getDefaultTargetTriple() { + std::string TargetTripleString = + updateTripleOSVersion(LLVM_DEFAULT_TARGET_TRIPLE); - // Override the default target with an environment variable named by LLVM_TARGET_TRIPLE_ENV. + // Override the default target with an environment variable named by + // LLVM_TARGET_TRIPLE_ENV. #if defined(LLVM_TARGET_TRIPLE_ENV) if (const char *EnvTriple = std::getenv(LLVM_TARGET_TRIPLE_ENV)) TargetTripleString = EnvTriple; diff --git a/lib/Support/Windows/Host.inc b/lib/Support/Windows/Host.inc index 7e196cf0ce1..461fb7833b8 100644 --- a/lib/Support/Windows/Host.inc +++ b/lib/Support/Windows/Host.inc @@ -17,6 +17,10 @@ using namespace llvm; +static void updateTripleOSVersion(std::string &) { + // Do nothing. +} + std::string sys::getDefaultTargetTriple() { const char *Triple = LLVM_DEFAULT_TARGET_TRIPLE; diff --git a/unittests/Support/Host.cpp b/unittests/Support/Host.cpp index fd53697793c..4f895e7163c 100644 --- a/unittests/Support/Host.cpp +++ b/unittests/Support/Host.cpp @@ -10,9 +10,23 @@ #include "llvm/Support/Host.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/Triple.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/Path.h" +#include "llvm/Support/Program.h" #include "gtest/gtest.h" +#define ASSERT_NO_ERROR(x) \ + if (std::error_code ASSERT_NO_ERROR_ec = x) { \ + SmallString<128> MessageStorage; \ + raw_svector_ostream Message(MessageStorage); \ + Message << #x ": did not return errc::success.\n" \ + << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \ + << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \ + GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \ + } else { \ + } + using namespace llvm; class HostTest : public testing::Test { @@ -114,3 +128,50 @@ Hardware : Qualcomm Technologies, Inc MSM8992 EXPECT_EQ(sys::detail::getHostCPUNameForARM(MSM8992ProcCpuInfo), "cortex-a53"); } + +#if defined(__APPLE__) +TEST_F(HostTest, getMacOSHostVersion) { + using namespace llvm::sys; + llvm::Triple HostTriple(getProcessTriple()); + if (!HostTriple.isMacOSX()) + return; + + SmallString<128> TestDirectory; + ASSERT_NO_ERROR(fs::createUniqueDirectory("host_test", TestDirectory)); + SmallString<128> OutputFile(TestDirectory); + path::append(OutputFile, "out"); + + const char *SwVersPath = "/usr/bin/sw_vers"; + const char *argv[] = {SwVersPath, "-productVersion", nullptr}; + StringRef OutputPath = OutputFile.str(); + const StringRef *Redirects[] = {/*STDIN=*/nullptr, /*STDOUT=*/&OutputPath, + /*STDERR=*/nullptr}; + int RetCode = ExecuteAndWait(SwVersPath, argv, /*env=*/nullptr, Redirects); + ASSERT_EQ(0, RetCode); + + int FD = 0; + ASSERT_NO_ERROR(fs::openFileForRead(OutputPath, FD)); + off_t Size = ::lseek(FD, 0, SEEK_END); + ASSERT_NE(-1, Size); + ::lseek(FD, 0, SEEK_SET); + std::unique_ptr Buffer = llvm::make_unique(Size); + ASSERT_EQ(::read(FD, Buffer.get(), Size), Size); + ::close(FD); + + // Ensure that the two versions match. + StringRef SystemVersion(Buffer.get(), Size); + unsigned SystemMajor, SystemMinor, SystemMicro; + ASSERT_EQ(llvm::Triple((Twine("x86_64-apple-macos") + SystemVersion)) + .getMacOSXVersion(SystemMajor, SystemMinor, SystemMicro), + true); + unsigned HostMajor, HostMinor, HostMicro; + ASSERT_EQ(HostTriple.getMacOSXVersion(HostMajor, HostMinor, HostMicro), true); + + // Don't compare the 'Micro' version, as it's always '0' for the 'Darwin' + // triples. + ASSERT_EQ(std::tie(SystemMajor, SystemMinor), std::tie(HostMajor, HostMinor)); + + ASSERT_NO_ERROR(fs::remove(OutputPath)); + ASSERT_NO_ERROR(fs::remove(TestDirectory.str())); +} +#endif