From cf092e8c65bbd3abcf4f254e2ad54db83d9eaeb7 Mon Sep 17 00:00:00 2001 From: Alex Lorenz Date: Mon, 19 Jun 2017 10:57:27 +0000 Subject: [PATCH] [driver][macOS] Pick the system version for the deployment target if the SDK is newer than the system This commit improves the driver by making sure that it picks the system version for the deployment target when the version of the macOS SDK is newer than the system version. rdar://29449467 Differential Revision: https://reviews.llvm.org/D34175 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@305678 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Driver/ToolChains/Darwin.cpp | 21 ++++++++++++++++++++- test/Driver/darwin-sdkroot.c | 9 +++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/Driver/ToolChains/Darwin.cpp b/lib/Driver/ToolChains/Darwin.cpp index e41b50c40b..32925fde5e 100644 --- a/lib/Driver/ToolChains/Darwin.cpp +++ b/lib/Driver/ToolChains/Darwin.cpp @@ -1118,6 +1118,25 @@ void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args, } } +/// Returns the most appropriate macOS target version for the current process. +/// +/// If the macOS SDK version is the same or earlier than the system version, +/// then the SDK version is returned. Otherwise the system version is returned. +static std::string getSystemOrSDKMacOSVersion(StringRef MacOSSDKVersion) { + unsigned Major, Minor, Micro; + llvm::Triple(llvm::sys::getProcessTriple()) + .getMacOSXVersion(Major, Minor, Micro); + VersionTuple SystemVersion(Major, Minor, Micro); + bool HadExtra; + if (!Driver::GetReleaseVersion(MacOSSDKVersion, Major, Minor, Micro, + HadExtra)) + return MacOSSDKVersion; + VersionTuple SDKVersion(Major, Minor, Micro); + if (SDKVersion > SystemVersion) + return SystemVersion.getAsString(); + return MacOSSDKVersion; +} + void Darwin::AddDeploymentTarget(DerivedArgList &Args) const { const OptTable &Opts = getDriver().getOpts(); @@ -1210,7 +1229,7 @@ void Darwin::AddDeploymentTarget(DerivedArgList &Args) const { SDK.startswith("iPhoneSimulator")) iOSTarget = Version; else if (SDK.startswith("MacOSX")) - OSXTarget = Version; + OSXTarget = getSystemOrSDKMacOSVersion(Version); else if (SDK.startswith("WatchOS") || SDK.startswith("WatchSimulator")) WatchOSTarget = Version; diff --git a/test/Driver/darwin-sdkroot.c b/test/Driver/darwin-sdkroot.c index 4f1fca2785..52479a1418 100644 --- a/test/Driver/darwin-sdkroot.c +++ b/test/Driver/darwin-sdkroot.c @@ -74,3 +74,12 @@ // CHECK-MACOSX: "-triple" "x86_64-apple-macosx10.10.0" // CHECK-MACOSX: ld // CHECK-MACOSX: "-macosx_version_min" "10.10.0" + +// Ensure that we never pick a version that's based on the SDK that's newer than +// the system version: +// RUN: rm -rf %t/SDKs/MacOSX10.99.99.sdk +// RUN: mkdir -p %t/SDKs/MacOSX10.99.99.sdk +// RUN: %clang -target x86_64-apple-darwin -isysroot %t/SDKs/MacOSX10.99.99.sdk %s -### 2>&1 \ +// RUN: | FileCheck --check-prefix=CHECK-MACOSX-SYSTEM-VERSION %s + +// CHECK-MACOSX-SYSTEM-VERSION-NOT: 10.99.99" -- 2.40.0