diff --git a/llvm/lib/Support/Host.cpp b/llvm/lib/Support/Host.cpp --- a/llvm/lib/Support/Host.cpp +++ b/llvm/lib/Support/Host.cpp @@ -1927,5 +1927,16 @@ if (sizeof(void *) == 4 && PT.isArch64Bit()) PT = PT.get32BitArchVariant(); +#if defined(__APPLE__) + // Handle current CMake universal binary compilation. +#if defined(__aarch64__) + if (PT.getArch() != llvm::Triple::aarch64) + PT.setArch(llvm::Triple::aarch64); +#elif defined(__x86_64__) || defined(_M_X64) + if (PT.getArch() != llvm::Triple::x86_64) + PT.setArch(llvm::Triple::x86_64); +#endif +#endif + return PT.str(); } diff --git a/llvm/unittests/Support/Host.cpp b/llvm/unittests/Support/Host.cpp --- a/llvm/unittests/Support/Host.cpp +++ b/llvm/unittests/Support/Host.cpp @@ -465,6 +465,25 @@ } } +TEST_F(HostTest, getMacOSHostArch) { + llvm::Triple HostTriple(llvm::sys::getProcessTriple()); + if (!HostTriple.isMacOSX()) + GTEST_SKIP(); + std::string HostTripleStr = HostTriple.normalize(); + + const char *SwVersPath = "/usr/bin/arch"; + StringRef argv[] = {SwVersPath}; + std::unique_ptr Buffer; + off_t Size; + ASSERT_EQ(runAndGetCommandOutput(SwVersPath, argv, Buffer, Size), true); + StringRef ArchStr = StringRef(Buffer.get(), Size).rtrim(); + + if (ArchStr == "i386") + ASSERT_EQ(StringRef(HostTripleStr).starts_with("x86"), true); + if (ArchStr == "arm64") + ASSERT_EQ(StringRef(HostTripleStr).starts_with("arm"), true); +} + // Helper to return AIX system version. Must return void to use ASSERT_*. static void getAIXSystemVersion(VersionTuple &SystemVersion) { const char *ExePath = "/usr/bin/oslevel";