Skip to content

Commit d7a227c

Browse files
Merge pull request #11393 from adrian-prantl/cherry-pick-stable-21.x-LLDB-Update-macOS-Swift-resource-directory-computation
[Cherry-pick into stable/21.x] [LLDB] Update macOS Swift resource directory computation
2 parents 101fa45 + 6e7e7f0 commit d7a227c

File tree

2 files changed

+59
-10
lines changed

2 files changed

+59
-10
lines changed

lldb/source/Host/macosx/objcxx/HostInfoMacOSXSwift.cpp

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,46 @@ bool HostInfoMacOSX::ComputeSwiftResourceDirectory(FileSpec &lldb_shlib_spec,
2929
if (!lldb_shlib_spec)
3030
return false;
3131

32-
std::string raw_path = lldb_shlib_spec.GetPath();
33-
size_t framework_pos = raw_path.find("LLDB.framework");
34-
if (framework_pos == std::string::npos)
32+
std::string shlib_path = lldb_shlib_spec.GetPath();
33+
std::string resource_dir;
34+
35+
llvm::StringRef path(shlib_path);
36+
// "LLDB.framework/Versions/A/LLDB" -> "LLDB.framework"
37+
while (!path.empty() && llvm::sys::path::filename(path) != "LLDB.framework")
38+
path = llvm::sys::path::parent_path(path);
39+
// POSIX-style install.
40+
if (path.empty() || llvm::sys::path::filename(path) != "LLDB.framework")
3541
return HostInfoPosix::ComputeSwiftResourceDirectory(lldb_shlib_spec,
36-
file_spec, verify);
37-
38-
framework_pos += strlen("LLDB.framework");
39-
raw_path.resize(framework_pos);
40-
raw_path.append("/Resources/Swift");
41-
if (!verify || VerifySwiftPath(raw_path)) {
42-
file_spec.SetDirectory(raw_path);
42+
file_spec, verify);
43+
44+
assert(llvm::sys::path::filename(path) == "LLDB.framework");
45+
path = llvm::sys::path::parent_path(path);
46+
if (llvm::sys::path::filename(path) == "PrivateFrameworks") {
47+
// Toolchain:
48+
// Xcode.app/Contents/Developer/Toolchains/OSX26.0.xctoolchain/System/Library/PrivateFrameworks/LLDB.framework
49+
// Xcode.app/Contents/Developer/Toolchains/OSX26.0.xctoolchain/usr/lib/swift
50+
path = llvm::sys::path::parent_path(path);
51+
if (llvm::sys::path::filename(path) != "Library")
52+
return {};
53+
path = llvm::sys::path::parent_path(path);
54+
if (llvm::sys::path::filename(path) != "System")
55+
return {};
56+
path = llvm::sys::path::parent_path(path);
57+
resource_dir = std::string(path) + "/usr/lib/swift";
58+
} else if (llvm::sys::path::filename(path) == "SharedFrameworks") {
59+
// Xcode:
60+
// Xcode.app/Contents/SharedFrameworks/LLDB.framework
61+
// Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift
62+
path = llvm::sys::path::parent_path(path);
63+
resource_dir =
64+
std::string(path) +
65+
"/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift";
66+
} else {
67+
// Local build: LLDB.framework
68+
resource_dir = std::string(path) + "/LLDB.framework/Resources/Swift";
69+
}
70+
if (!verify || VerifySwiftPath(resource_dir)) {
71+
file_spec.SetDirectory(resource_dir);
4372
FileSystem::Instance().Resolve(file_spec);
4473
return true;
4574
}

lldb/unittests/Host/HostInfoSwiftTest.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,26 @@ TEST_F(SwiftHostTest, ComputeSwiftResourceDirectory) {
7373
}
7474

7575
#if defined(__APPLE__)
76+
TEST_F(SwiftHostTest, MacOSXToolchain) {
77+
std::string path_to_liblldb =
78+
"/Applications/Xcode.app/Contents/Developer/Toolchains/"
79+
"OSX26.0.xctoolchain/System/Library/PrivateFrameworks/LLDB.framework";
80+
std::string path_to_swift_dir =
81+
"/Applications/Xcode.app/Contents/Developer/Toolchains/"
82+
"OSX26.0.xctoolchain/usr/lib/swift";
83+
EXPECT_EQ(ComputeSwiftResourceDirectoryHelper(path_to_liblldb),
84+
path_to_swift_dir);
85+
}
86+
TEST_F(SwiftHostTest, MacOSXXcode) {
87+
std::string path_to_liblldb =
88+
"/Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework";
89+
std::string path_to_swift_dir =
90+
"/Applications/Xcode.app/Contents/Developer/Toolchains/"
91+
"XcodeDefault.xctoolchain/usr/lib/swift";
92+
EXPECT_EQ(ComputeSwiftResourceDirectoryHelper(path_to_liblldb),
93+
path_to_swift_dir);
94+
}
95+
7696
TEST_F(SwiftHostTest, MacOSX) {
7797
// test for LLDB.framework
7898
std::string path_to_liblldb = "/foo/bar/lib/LLDB.framework";

0 commit comments

Comments
 (0)