[clang] [clang][driver][darwin] Use DefaultDeploymentTarget when inferring deployment target from SDK (PR #217983)

Akira Hatanaka via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 21 10:36:10 PDT 2026


https://github.com/ahatanak updated https://github.com/llvm/llvm-project/pull/217983

>From 5945e820e671966cced41f17def609222da89bdd Mon Sep 17 00:00:00 2001
From: Akira Hatanaka <ahatanak at gmail.com>
Date: Fri, 21 Aug 2026 09:54:18 -0700
Subject: [PATCH] [clang][driver][darwin] Use DefaultDeploymentTarget when
 inferring deployment target from SDK

Use "DefaultDeploymentTarget" from SDKSettings.json for the inferred
deployment target version, falling back to "Version" if it isn't
present. This only affects the deployment target; other uses of
"Version" (e.g., -target-sdk-version=) are unchanged.

rdar://184842209
---
 clang/include/clang/Basic/DarwinSDKInfo.h     | 14 +++++++++++--
 clang/lib/Basic/DarwinSDKInfo.cpp             |  9 ++++++--
 clang/lib/Driver/ToolChains/Darwin.cpp        |  2 +-
 .../Driver/darwin-default-deployment-target.c | 21 +++++++++++++++++++
 4 files changed, 41 insertions(+), 5 deletions(-)
 create mode 100644 clang/test/Driver/darwin-default-deployment-target.c

diff --git a/clang/include/clang/Basic/DarwinSDKInfo.h b/clang/include/clang/Basic/DarwinSDKInfo.h
index 2d55f4f61cd90..e635e32391b09 100644
--- a/clang/include/clang/Basic/DarwinSDKInfo.h
+++ b/clang/include/clang/Basic/DarwinSDKInfo.h
@@ -170,7 +170,8 @@ class DarwinSDKInfo {
   DarwinSDKInfo(
       std::string FilePath, llvm::Triple::OSType OS,
       llvm::Triple::EnvironmentType Environment, VersionTuple Version,
-      StringRef DisplayName, VersionTuple MaximumDeploymentTarget,
+      VersionTuple DefaultDeploymentTarget, StringRef DisplayName,
+      VersionTuple MaximumDeploymentTarget,
       PlatformInfoStorageType PlatformInfos,
       llvm::DenseMap<OSEnvPair::StorageType,
                      std::optional<RelatedTargetVersionMapping>>
@@ -178,7 +179,8 @@ class DarwinSDKInfo {
               llvm::DenseMap<OSEnvPair::StorageType,
                              std::optional<RelatedTargetVersionMapping>>())
       : FilePath(std::move(FilePath)), OS(OS), Environment(Environment),
-        Version(Version), DisplayName(DisplayName),
+        Version(Version), DefaultDeploymentTarget(DefaultDeploymentTarget),
+        DisplayName(DisplayName),
         MaximumDeploymentTarget(MaximumDeploymentTarget),
         PlatformInfos(std::move(PlatformInfos)),
         VersionMappings(std::move(VersionMappings)) {
@@ -203,6 +205,13 @@ class DarwinSDKInfo {
 
   const llvm::VersionTuple &getVersion() const { return Version; }
 
+  /// Returns the value of the "DefaultDeploymentTarget" key from
+  /// SDKSettings.json, or "Version" when the SDK doesn't specify a
+  /// "DefaultDeploymentTarget".
+  const llvm::VersionTuple &getDefaultDeploymentTarget() const {
+    return DefaultDeploymentTarget;
+  }
+
   const StringRef getDisplayName() const { return DisplayName; }
 
   const llvm::Triple &getCanonicalPlatformTriple() const {
@@ -240,6 +249,7 @@ class DarwinSDKInfo {
   llvm::Triple::OSType OS;
   llvm::Triple::EnvironmentType Environment;
   VersionTuple Version;
+  VersionTuple DefaultDeploymentTarget;
   std::string DisplayName;
   VersionTuple MaximumDeploymentTarget;
   PlatformInfoStorageType PlatformInfos;
diff --git a/clang/lib/Basic/DarwinSDKInfo.cpp b/clang/lib/Basic/DarwinSDKInfo.cpp
index 4e44da8febc3e..4a1590dd001ff 100644
--- a/clang/lib/Basic/DarwinSDKInfo.cpp
+++ b/clang/lib/Basic/DarwinSDKInfo.cpp
@@ -259,6 +259,10 @@ DarwinSDKInfo::parseDarwinSDKSettingsJSON(std::string FilePath,
   auto Version = getVersionKey(*Obj, "Version");
   if (!Version)
     return std::nullopt;
+  // "DefaultDeploymentTarget" is usually the same as "Version", but the two
+  // can diverge, so prefer "DefaultDeploymentTarget" when it's present.
+  VersionTuple DefaultDeploymentTarget =
+      getVersionKey(*Obj, "DefaultDeploymentTarget").value_or(*Version);
   auto MaximumDeploymentVersion =
       getVersionKey(*Obj, "MaximumDeploymentTarget");
   if (!MaximumDeploymentVersion)
@@ -318,7 +322,8 @@ DarwinSDKInfo::parseDarwinSDKSettingsJSON(std::string FilePath,
 
   return DarwinSDKInfo(std::move(FilePath), OSAndEnvironment.first,
                        OSAndEnvironment.second, std::move(*Version),
-                       DisplayName, std::move(*MaximumDeploymentVersion),
+                       DefaultDeploymentTarget, DisplayName,
+                       std::move(*MaximumDeploymentVersion),
                        std::move(PlatformInfos), std::move(VersionMappings));
 }
 
@@ -350,7 +355,7 @@ DarwinSDKInfo::DarwinSDKInfo(llvm::Triple::OSType OS,
                              llvm::Triple::EnvironmentType Environment,
                              VersionTuple Version, StringRef DisplayName,
                              VersionTuple MaximumDeploymentTarget)
-    : DarwinSDKInfo("", OS, Environment, Version, DisplayName,
+    : DarwinSDKInfo("", OS, Environment, Version, Version, DisplayName,
                     MaximumDeploymentTarget,
                     legacyPlatformInfos(OS, Environment)) {}
 
diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp b/clang/lib/Driver/ToolChains/Darwin.cpp
index d3de04fc5155e..98ace0720343f 100644
--- a/clang/lib/Driver/ToolChains/Darwin.cpp
+++ b/clang/lib/Driver/ToolChains/Darwin.cpp
@@ -2057,7 +2057,7 @@ struct DarwinPlatform {
                                           const DarwinSDKInfo &SDKInfo) {
     const llvm::Triple &PlatformTriple = SDKInfo.getCanonicalPlatformTriple();
     const llvm::Triple::OSType OS = PlatformTriple.getOS();
-    VersionTuple Version = SDKInfo.getVersion();
+    VersionTuple Version = SDKInfo.getDefaultDeploymentTarget();
     if (OS == llvm::Triple::MacOSX)
       Version = getVersionFromString(
           getSystemOrSDKMacOSVersion(Version.getAsString()));
diff --git a/clang/test/Driver/darwin-default-deployment-target.c b/clang/test/Driver/darwin-default-deployment-target.c
new file mode 100644
index 0000000000000..0fc8f2be94dbe
--- /dev/null
+++ b/clang/test/Driver/darwin-default-deployment-target.c
@@ -0,0 +1,21 @@
+// Ensure that the deployment target inferred from the SDK when none is
+// specified on the command line uses "DefaultDeploymentTarget" rather than
+// "Version" when the SDK specifies both and they differ.
+
+// RUN: rm -rf %t/SDKs/iPhoneOS18.0.sdk
+// RUN: mkdir -p %t/SDKs/iPhoneOS18.0.sdk
+// RUN: echo '{"CanonicalName": "iphoneos18.0", "Version": "18.0", "DefaultDeploymentTarget": "17.0", "MaximumDeploymentTarget": "18.0.99"}' \
+// RUN:   > %t/SDKs/iPhoneOS18.0.sdk/SDKSettings.json
+// RUN: %clang -target arm64-apple-darwin -isysroot %t/SDKs/iPhoneOS18.0.sdk -c -### %s 2>&1 \
+// RUN:   | FileCheck %s
+
+// CHECK: "-triple" "arm64-apple-ios17.0.0"
+// CHECK-SAME: -target-sdk-version=18.0
+
+// An explicit deployment target on the command line overrides the SDK's
+// "DefaultDeploymentTarget".
+// RUN: %clang -target arm64-apple-darwin -isysroot %t/SDKs/iPhoneOS18.0.sdk -miphoneos-version-min=12.0 -c -### %s 2>&1 \
+// RUN:   | FileCheck --check-prefix=OVERRIDE %s
+
+// OVERRIDE: "-triple" "arm64-apple-ios12.0.0"
+// OVERRIDE-SAME: -target-sdk-version=18.0



More information about the cfe-commits mailing list