From 6e7e7f0c6e45c183bb1d6c28be213f327d11d6e0 Mon Sep 17 00:00:00 2001 From: Adrian Prantl Date: Thu, 11 Sep 2025 14:20:51 -0700 Subject: [PATCH] [LLDB] Update macOS Swift resource directory computation For a while now Xcode no longer ships a separate Swift resource directory inside of LLDB.framework, so this method should find the matching Swift compiler's resource dir next to its install location. rdar://160406978 (cherry picked from commit 5c9900cbaa7cb33b1abf1e28077d32309bf4309f) --- .../macosx/objcxx/HostInfoMacOSXSwift.cpp | 49 +++++++++++++++---- lldb/unittests/Host/HostInfoSwiftTest.cpp | 20 ++++++++ 2 files changed, 59 insertions(+), 10 deletions(-) diff --git a/lldb/source/Host/macosx/objcxx/HostInfoMacOSXSwift.cpp b/lldb/source/Host/macosx/objcxx/HostInfoMacOSXSwift.cpp index f16d615db7498..243ca53aa0807 100644 --- a/lldb/source/Host/macosx/objcxx/HostInfoMacOSXSwift.cpp +++ b/lldb/source/Host/macosx/objcxx/HostInfoMacOSXSwift.cpp @@ -29,17 +29,46 @@ bool HostInfoMacOSX::ComputeSwiftResourceDirectory(FileSpec &lldb_shlib_spec, if (!lldb_shlib_spec) return false; - std::string raw_path = lldb_shlib_spec.GetPath(); - size_t framework_pos = raw_path.find("LLDB.framework"); - if (framework_pos == std::string::npos) + std::string shlib_path = lldb_shlib_spec.GetPath(); + std::string resource_dir; + + llvm::StringRef path(shlib_path); + // "LLDB.framework/Versions/A/LLDB" -> "LLDB.framework" + while (!path.empty() && llvm::sys::path::filename(path) != "LLDB.framework") + path = llvm::sys::path::parent_path(path); + // POSIX-style install. + if (path.empty() || llvm::sys::path::filename(path) != "LLDB.framework") return HostInfoPosix::ComputeSwiftResourceDirectory(lldb_shlib_spec, - file_spec, verify); - - framework_pos += strlen("LLDB.framework"); - raw_path.resize(framework_pos); - raw_path.append("/Resources/Swift"); - if (!verify || VerifySwiftPath(raw_path)) { - file_spec.SetDirectory(raw_path); + file_spec, verify); + + assert(llvm::sys::path::filename(path) == "LLDB.framework"); + path = llvm::sys::path::parent_path(path); + if (llvm::sys::path::filename(path) == "PrivateFrameworks") { + // Toolchain: + // Xcode.app/Contents/Developer/Toolchains/OSX26.0.xctoolchain/System/Library/PrivateFrameworks/LLDB.framework + // Xcode.app/Contents/Developer/Toolchains/OSX26.0.xctoolchain/usr/lib/swift + path = llvm::sys::path::parent_path(path); + if (llvm::sys::path::filename(path) != "Library") + return {}; + path = llvm::sys::path::parent_path(path); + if (llvm::sys::path::filename(path) != "System") + return {}; + path = llvm::sys::path::parent_path(path); + resource_dir = std::string(path) + "/usr/lib/swift"; + } else if (llvm::sys::path::filename(path) == "SharedFrameworks") { + // Xcode: + // Xcode.app/Contents/SharedFrameworks/LLDB.framework + // Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift + path = llvm::sys::path::parent_path(path); + resource_dir = + std::string(path) + + "/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift"; + } else { + // Local build: LLDB.framework + resource_dir = std::string(path) + "/LLDB.framework/Resources/Swift"; + } + if (!verify || VerifySwiftPath(resource_dir)) { + file_spec.SetDirectory(resource_dir); FileSystem::Instance().Resolve(file_spec); return true; } diff --git a/lldb/unittests/Host/HostInfoSwiftTest.cpp b/lldb/unittests/Host/HostInfoSwiftTest.cpp index 4b2c5e1263de4..1d79a1c975676 100644 --- a/lldb/unittests/Host/HostInfoSwiftTest.cpp +++ b/lldb/unittests/Host/HostInfoSwiftTest.cpp @@ -73,6 +73,26 @@ TEST_F(SwiftHostTest, ComputeSwiftResourceDirectory) { } #if defined(__APPLE__) +TEST_F(SwiftHostTest, MacOSXToolchain) { + std::string path_to_liblldb = + "/Applications/Xcode.app/Contents/Developer/Toolchains/" + "OSX26.0.xctoolchain/System/Library/PrivateFrameworks/LLDB.framework"; + std::string path_to_swift_dir = + "/Applications/Xcode.app/Contents/Developer/Toolchains/" + "OSX26.0.xctoolchain/usr/lib/swift"; + EXPECT_EQ(ComputeSwiftResourceDirectoryHelper(path_to_liblldb), + path_to_swift_dir); +} +TEST_F(SwiftHostTest, MacOSXXcode) { + std::string path_to_liblldb = + "/Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework"; + std::string path_to_swift_dir = + "/Applications/Xcode.app/Contents/Developer/Toolchains/" + "XcodeDefault.xctoolchain/usr/lib/swift"; + EXPECT_EQ(ComputeSwiftResourceDirectoryHelper(path_to_liblldb), + path_to_swift_dir); +} + TEST_F(SwiftHostTest, MacOSX) { // test for LLDB.framework std::string path_to_liblldb = "/foo/bar/lib/LLDB.framework";