steven_wu created this revision.
steven_wu added reviewers: arphaman, dexonsmith.

When clang required to infer target os version from --target option and
the os version is not specified in targets, check the host triple. If the
host and target are both macOS, use host triple to infer target os
version.

rdar://problem/41651999


Repository:
  rC Clang

https://reviews.llvm.org/D48849

Files:
  lib/Driver/ToolChains/Darwin.cpp
  test/Driver/clang-g-opts.c


Index: test/Driver/clang-g-opts.c
===================================================================
--- test/Driver/clang-g-opts.c
+++ test/Driver/clang-g-opts.c
@@ -3,7 +3,7 @@
 // RUN:             | FileCheck --check-prefix=CHECK-WITH-G %s
 
 // Assert that the toolchains which should default to a lower Dwarf version do 
so.
-// RUN: %clang -### -S %s -g -target x86_64-apple-darwin 2>&1 \
+// RUN: %clang -### -S %s -g -target x86_64-apple-darwin8 2>&1 \
 // RUN:             | FileCheck --check-prefix=CHECK-WITH-G-DWARF2 %s
 // RUN: %clang -### -S %s -g -target i686-pc-openbsd 2>&1 \
 // RUN:             | FileCheck --check-prefix=CHECK-WITH-G-DWARF2 %s
@@ -21,7 +21,7 @@
 //
 // RUN: %clang -### -S %s -g0 -g -target x86_64-linux-gnu 2>&1 \
 // RUN:             | FileCheck --check-prefix=CHECK-WITH-G %s
-// RUN: %clang -### -S %s -g0 -g -target x86_64-apple-darwin 2>&1 \
+// RUN: %clang -### -S %s -g0 -g -target x86_64-apple-darwin8 2>&1 \
 // RUN:             | FileCheck --check-prefix=CHECK-WITH-G-STANDALONE %s
 // RUN: %clang -### -S %s -g0 -g -target i686-pc-openbsd 2>&1 \
 // RUN:             | FileCheck --check-prefix=CHECK-WITH-G-DWARF2 %s
Index: lib/Driver/ToolChains/Darwin.cpp
===================================================================
--- lib/Driver/ToolChains/Darwin.cpp
+++ lib/Driver/ToolChains/Darwin.cpp
@@ -1512,9 +1512,22 @@
   else if (MachOArchName == "armv7k")
     OSTy = llvm::Triple::WatchOS;
   else if (MachOArchName != "armv6m" && MachOArchName != "armv7m" &&
-           MachOArchName != "armv7em")
+           MachOArchName != "armv7em") {
+    // Default to macOS for other architectures.
     OSTy = llvm::Triple::MacOSX;
 
+    // If there is no version specified on triple, and both host and target are
+    // macos, use the host triple to infer OS version.
+    llvm::Triple SystemTriple(llvm::sys::getProcessTriple());
+    if (SystemTriple.isMacOSX()) {
+      unsigned Major, Minor, Micro;
+      Triple.getOSVersion(Major, Minor, Micro);
+      if (Major == 0)
+        return DarwinPlatform::createFromArch(
+            OSTy, getOSVersion(OSTy, SystemTriple, TheDriver));
+    }
+  }
+
   if (OSTy == llvm::Triple::UnknownOS)
     return None;
   return DarwinPlatform::createFromArch(OSTy,


Index: test/Driver/clang-g-opts.c
===================================================================
--- test/Driver/clang-g-opts.c
+++ test/Driver/clang-g-opts.c
@@ -3,7 +3,7 @@
 // RUN:             | FileCheck --check-prefix=CHECK-WITH-G %s
 
 // Assert that the toolchains which should default to a lower Dwarf version do so.
-// RUN: %clang -### -S %s -g -target x86_64-apple-darwin 2>&1 \
+// RUN: %clang -### -S %s -g -target x86_64-apple-darwin8 2>&1 \
 // RUN:             | FileCheck --check-prefix=CHECK-WITH-G-DWARF2 %s
 // RUN: %clang -### -S %s -g -target i686-pc-openbsd 2>&1 \
 // RUN:             | FileCheck --check-prefix=CHECK-WITH-G-DWARF2 %s
@@ -21,7 +21,7 @@
 //
 // RUN: %clang -### -S %s -g0 -g -target x86_64-linux-gnu 2>&1 \
 // RUN:             | FileCheck --check-prefix=CHECK-WITH-G %s
-// RUN: %clang -### -S %s -g0 -g -target x86_64-apple-darwin 2>&1 \
+// RUN: %clang -### -S %s -g0 -g -target x86_64-apple-darwin8 2>&1 \
 // RUN:             | FileCheck --check-prefix=CHECK-WITH-G-STANDALONE %s
 // RUN: %clang -### -S %s -g0 -g -target i686-pc-openbsd 2>&1 \
 // RUN:             | FileCheck --check-prefix=CHECK-WITH-G-DWARF2 %s
Index: lib/Driver/ToolChains/Darwin.cpp
===================================================================
--- lib/Driver/ToolChains/Darwin.cpp
+++ lib/Driver/ToolChains/Darwin.cpp
@@ -1512,9 +1512,22 @@
   else if (MachOArchName == "armv7k")
     OSTy = llvm::Triple::WatchOS;
   else if (MachOArchName != "armv6m" && MachOArchName != "armv7m" &&
-           MachOArchName != "armv7em")
+           MachOArchName != "armv7em") {
+    // Default to macOS for other architectures.
     OSTy = llvm::Triple::MacOSX;
 
+    // If there is no version specified on triple, and both host and target are
+    // macos, use the host triple to infer OS version.
+    llvm::Triple SystemTriple(llvm::sys::getProcessTriple());
+    if (SystemTriple.isMacOSX()) {
+      unsigned Major, Minor, Micro;
+      Triple.getOSVersion(Major, Minor, Micro);
+      if (Major == 0)
+        return DarwinPlatform::createFromArch(
+            OSTy, getOSVersion(OSTy, SystemTriple, TheDriver));
+    }
+  }
+
   if (OSTy == llvm::Triple::UnknownOS)
     return None;
   return DarwinPlatform::createFromArch(OSTy,
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to