[Lldb-commits] [lldb] [lldb] Prefer selected Darwin platform SDK root for modules (PR #195028)

Li Teng via lldb-commits lldb-commits at lists.llvm.org
Thu Apr 30 00:45:55 PDT 2026


https://github.com/lnteng updated https://github.com/llvm/llvm-project/pull/195028

>From 8987be49b878b093df1a71f16f7e342a2f68b3ff Mon Sep 17 00:00:00 2001
From: "liteng.5382" <liteng.5382 at bytedance.com>
Date: Thu, 30 Apr 2026 15:24:47 +0800
Subject: [PATCH] [lldb] Prefer selected Darwin platform SDK root for modules

---
 .../Platform/MacOSX/PlatformDarwin.cpp        | 25 ++++++++---
 .../unittests/Platform/PlatformDarwinTest.cpp | 44 +++++++++++++++++++
 2 files changed, 62 insertions(+), 7 deletions(-)

diff --git a/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp b/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
index 8db4824be7da5..f07ced4363372 100644
--- a/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
+++ b/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
@@ -1242,13 +1242,24 @@ void PlatformDarwin::AddClangModuleCompilationOptionsForSDKType(
   FileSpec sysroot_spec;
 
   if (target) {
-    auto sysroot_spec_or_err = ::ResolveSDKPathFromDebugInfo(target);
-    if (!sysroot_spec_or_err) {
-      LLDB_LOG_ERROR(GetLog(LLDBLog::Types | LLDBLog::Host),
-                     sysroot_spec_or_err.takeError(),
-                     "Failed to resolve sysroot: {0}");
-    } else {
-      sysroot_spec = *sysroot_spec_or_err;
+    if (PlatformSP platform_sp = target->GetPlatform()) {
+      FileSpec platform_sdk_spec(platform_sp->GetSDKRootDirectory());
+      if (platform_sdk_spec) {
+        FileSystem::Instance().Resolve(platform_sdk_spec);
+        if (FileSystem::Instance().IsDirectory(platform_sdk_spec.GetPath()))
+          sysroot_spec = platform_sdk_spec;
+      }
+    }
+
+    if (!FileSystem::Instance().IsDirectory(sysroot_spec.GetPath())) {
+      auto sysroot_spec_or_err = ::ResolveSDKPathFromDebugInfo(target);
+      if (!sysroot_spec_or_err) {
+        LLDB_LOG_ERROR(GetLog(LLDBLog::Types | LLDBLog::Host),
+                       sysroot_spec_or_err.takeError(),
+                       "Failed to resolve sysroot: {0}");
+      } else {
+        sysroot_spec = *sysroot_spec_or_err;
+      }
     }
   }
 
diff --git a/lldb/unittests/Platform/PlatformDarwinTest.cpp b/lldb/unittests/Platform/PlatformDarwinTest.cpp
index 04af8081a274e..34c67d1ad5687 100644
--- a/lldb/unittests/Platform/PlatformDarwinTest.cpp
+++ b/lldb/unittests/Platform/PlatformDarwinTest.cpp
@@ -18,6 +18,8 @@
 #include "lldb/Core/Debugger.h"
 #include "lldb/Core/PluginManager.h"
 #include "lldb/Host/HostInfo.h"
+#include "lldb/Interpreter/CommandInterpreter.h"
+#include "lldb/Interpreter/CommandReturnObject.h"
 
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/FileSystem.h"
@@ -126,6 +128,48 @@ TEST(PlatformDarwinTest, TestParseVersionBuildDir) {
   EXPECT_EQ(llvm::VersionTuple(3, 4, 5), V);
 }
 
+TEST_F(PlatformDarwinLocateTest,
+       AddClangModuleCompilationOptionsUsesSelectedPlatformSDKRoot) {
+  llvm::SmallString<128> sdk_root(m_tmp_root_dir);
+  llvm::sys::path::append(sdk_root, "Target.sdk");
+  ASSERT_FALSE(llvm::sys::fs::create_directory(sdk_root))
+      << "Failed to create test SDK root directory.";
+  std::string sdk_root_path = sdk_root.str().str();
+
+  CommandReturnObject result(/*colors*/ false);
+  std::string command =
+      llvm::formatv("platform select ios-simulator -S \"{0}\"", sdk_root_path)
+          .str();
+  m_debugger_sp->GetCommandInterpreter().HandleCommand(command.c_str(),
+                                                       eLazyBoolNo, result);
+  ASSERT_TRUE(result.Succeeded()) << result.GetErrorString();
+
+  PlatformSP platform_sp =
+      m_debugger_sp->GetPlatformList().GetSelectedPlatform();
+  ASSERT_TRUE(platform_sp);
+  ASSERT_EQ(platform_sp->GetPluginName(), llvm::StringRef("ios-simulator"));
+  ASSERT_EQ(platform_sp->GetSDKRootDirectory(), sdk_root_path);
+
+  TargetSP target_sp;
+  ArchSpec arch("x86_64-apple-ios-simulator");
+  m_debugger_sp->GetTargetList().CreateTarget(*m_debugger_sp, "", arch,
+                                              lldb_private::eLoadDependentsNo,
+                                              platform_sp, target_sp);
+  ASSERT_TRUE(target_sp);
+
+  std::vector<std::string> options;
+  platform_sp->AddClangModuleCompilationOptions(target_sp.get(), options);
+
+  bool found_sdk_root = false;
+  for (size_t i = 0; i + 1 < options.size(); ++i) {
+    if (options[i] == "-isysroot" && options[i + 1] == sdk_root_path) {
+      found_sdk_root = true;
+      break;
+    }
+  }
+  EXPECT_TRUE(found_sdk_root);
+}
+
 TEST_F(PlatformDarwinLocateTest,
        LocateExecutableScriptingResourcesFromDSYM_StripExtensions) {
   // Tests that LocateExecutableScriptingResourcesFromDSYM will strip module



More information about the lldb-commits mailing list