[PATCH] D34446: [Support] sys::getProcessTriple should return a macOS triple using the system's version of macOS

Alex Lorenz via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 21 06:23:44 PDT 2017


arphaman created this revision.

Right now, `sys::getProcessTriple` returns the `LLVM_HOST_TRIPLE`, whose system version might not be the actual version of the system on which the compiler running.  This patch ensures that for macOS, `sys::getProcessTriple` returns a triple with the system's macOS version.

The function `sys::getDefaultTargetTriple` does this version adjustment as well. However, it seems wrong to me (the default target OS might not be the host OS). I left it alone in this patch to avoid breaking things, but I think that the version adjustment in `sys::getDefaultTargetTriple` should check that the host OS is the same as the target OS before making the adjustment.

I'm not sure if it's possible to test this change (Please let me know if I'm wrong though). I do have a commit in Clang r305678 (that I reverted for now) that depends on this change.


Repository:
  rL LLVM

https://reviews.llvm.org/D34446

Files:
  lib/Support/Host.cpp
  lib/Support/Unix/Host.inc
  lib/Support/Windows/Host.inc


Index: lib/Support/Windows/Host.inc
===================================================================
--- lib/Support/Windows/Host.inc
+++ 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;
 
Index: lib/Support/Unix/Host.inc
===================================================================
--- lib/Support/Unix/Host.inc
+++ lib/Support/Unix/Host.inc
@@ -34,18 +34,30 @@
   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 void 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;
+  }
+  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();
   }
+}
+
+std::string sys::getDefaultTargetTriple() {
+  std::string TargetTripleString(LLVM_DEFAULT_TARGET_TRIPLE);
+  updateTripleOSVersion(TargetTripleString);
 
-  // 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;
Index: lib/Support/Host.cpp
===================================================================
--- lib/Support/Host.cpp
+++ lib/Support/Host.cpp
@@ -1486,7 +1486,9 @@
 #endif
 
 std::string sys::getProcessTriple() {
-  Triple PT(Triple::normalize(LLVM_HOST_TRIPLE));
+  std::string TargetTripleString(LLVM_HOST_TRIPLE);
+  updateTripleOSVersion(TargetTripleString);
+  Triple PT(Triple::normalize(TargetTripleString));
 
   if (sizeof(void *) == 8 && PT.isArch32Bit())
     PT = PT.get64BitArchVariant();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D34446.103364.patch
Type: text/x-patch
Size: 2556 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170621/7af6f5e9/attachment.bin>


More information about the llvm-commits mailing list