diff options
author | Alex Lorenz <arphaman@gmail.com> | 2017-07-07 09:53:47 +0000 |
---|---|---|
committer | Alex Lorenz <arphaman@gmail.com> | 2017-07-07 09:53:47 +0000 |
commit | 25b2f9273d88ee530ec4156966fec654e58b8d0e (patch) | |
tree | 8507ab80c6b9691204e2b8478b59f6b1883918b9 /unittests | |
parent | 20cee5d628070c24bfa54d8700afbfaf29f31f88 (diff) |
[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
Diffstat (limited to 'unittests')
-rw-r--r-- | unittests/Support/Host.cpp | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/unittests/Support/Host.cpp b/unittests/Support/Host.cpp index fd53697793c7..4f895e7163c5 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<char[]> Buffer = llvm::make_unique<char[]>(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 |