Skip to content

Commit 77a8770

Browse files
committed
Reland "[lldb][HostInfoMacOSX] Try to use DW_AT_LLVM_sysroot instead of xcrun when looking up SDK" (#129621)"
This reverts commit 6041c74. Relands the original patch with the test-case data fixed. Weirldy the PR CI didn't seem to run the unit-tests? In any case, the problem was an incorrect expectation in the test-case data. Since we have both public and internal SDK in that test-case, we should `expect_mismatch` to be `true`.
1 parent e0eb4ed commit 77a8770

File tree

7 files changed

+71
-18
lines changed

7 files changed

+71
-18
lines changed

lldb/include/lldb/Host/macosx/HostInfoMacOSX.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class HostInfoMacOSX : public HostInfoPosix {
3232
static FileSpec GetXcodeDeveloperDirectory();
3333

3434
/// Query xcrun to find an Xcode SDK directory.
35+
///
36+
/// Note, this is an expensive operation if the SDK we're querying
37+
/// does not exist in an Xcode installation path on the host.
3538
static llvm::Expected<llvm::StringRef> GetSDKRoot(SDKOptions options);
3639
static llvm::Expected<llvm::StringRef> FindSDKTool(XcodeSDK sdk,
3740
llvm::StringRef tool);

lldb/include/lldb/Utility/XcodeSDK.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLDB_UTILITY_SDK_H
1010
#define LLDB_UTILITY_SDK_H
1111

12+
#include "lldb/Utility/FileSpec.h"
1213
#include "lldb/lldb-forward.h"
1314
#include "llvm/ADT/StringRef.h"
1415
#include "llvm/Support/VersionTuple.h"
@@ -23,6 +24,7 @@ namespace lldb_private {
2324
/// An abstraction for Xcode-style SDKs that works like \ref ArchSpec.
2425
class XcodeSDK {
2526
std::string m_name;
27+
FileSpec m_sysroot;
2628

2729
public:
2830
/// Different types of Xcode SDKs.
@@ -62,6 +64,8 @@ class XcodeSDK {
6264
/// directory component of a path one would pass to clang's -isysroot
6365
/// parameter. For example, "MacOSX.10.14.sdk".
6466
XcodeSDK(std::string &&name) : m_name(std::move(name)) {}
67+
XcodeSDK(std::string name, FileSpec sysroot)
68+
: m_name(std::move(name)), m_sysroot(std::move(sysroot)) {}
6569
static XcodeSDK GetAnyMacOS() { return XcodeSDK("MacOSX.sdk"); }
6670

6771
/// The merge function follows a strict order to maintain monotonicity:
@@ -79,6 +83,7 @@ class XcodeSDK {
7983
llvm::VersionTuple GetVersion() const;
8084
Type GetType() const;
8185
llvm::StringRef GetString() const;
86+
const FileSpec &GetSysroot() const;
8287
/// Whether this Xcode SDK supports Swift.
8388
bool SupportsSwift() const;
8489

lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,6 +1425,9 @@ PlatformDarwin::ResolveSDKPathFromDebugInfo(Module &module) {
14251425

14261426
auto [sdk, _] = std::move(*sdk_or_err);
14271427

1428+
if (FileSystem::Instance().Exists(sdk.GetSysroot()))
1429+
return sdk.GetSysroot().GetPath();
1430+
14281431
auto path_or_err = HostInfo::GetSDKRoot(HostInfo::SDKOptions{sdk});
14291432
if (!path_or_err)
14301433
return llvm::createStringError(

lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -993,21 +993,26 @@ XcodeSDK SymbolFileDWARF::ParseXcodeSDK(CompileUnit &comp_unit) {
993993
const char *sdk = cu_die.GetAttributeValueAsString(DW_AT_APPLE_sdk, nullptr);
994994
if (!sdk)
995995
return {};
996-
const char *sysroot =
996+
std::string sysroot =
997997
cu_die.GetAttributeValueAsString(DW_AT_LLVM_sysroot, "");
998-
// Register the sysroot path remapping with the module belonging to
999-
// the CU as well as the one belonging to the symbol file. The two
1000-
// would be different if this is an OSO object and module is the
1001-
// corresponding debug map, in which case both should be updated.
1002-
ModuleSP module_sp = comp_unit.GetModule();
1003-
if (module_sp)
1004-
module_sp->RegisterXcodeSDK(sdk, sysroot);
1005998

1006-
ModuleSP local_module_sp = m_objfile_sp->GetModule();
1007-
if (local_module_sp && local_module_sp != module_sp)
1008-
local_module_sp->RegisterXcodeSDK(sdk, sysroot);
999+
// RegisterXcodeSDK calls into xcrun which is not aware of CLT, which is
1000+
// expensive.
1001+
if (sysroot.find("/Library/Developer/CommandLineTools/SDKs") != 0) {
1002+
// Register the sysroot path remapping with the module belonging to
1003+
// the CU as well as the one belonging to the symbol file. The two
1004+
// would be different if this is an OSO object and module is the
1005+
// corresponding debug map, in which case both should be updated.
1006+
ModuleSP module_sp = comp_unit.GetModule();
1007+
if (module_sp)
1008+
module_sp->RegisterXcodeSDK(sdk, sysroot);
1009+
1010+
ModuleSP local_module_sp = m_objfile_sp->GetModule();
1011+
if (local_module_sp && local_module_sp != module_sp)
1012+
local_module_sp->RegisterXcodeSDK(sdk, sysroot);
1013+
}
10091014

1010-
return {sdk};
1015+
return {sdk, FileSpec{std::move(sysroot)}};
10111016
}
10121017

10131018
size_t SymbolFileDWARF::ParseFunctions(CompileUnit &comp_unit) {

lldb/source/Utility/XcodeSDK.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ XcodeSDK::Type XcodeSDK::GetType() const {
142142

143143
llvm::StringRef XcodeSDK::GetString() const { return m_name; }
144144

145+
const FileSpec &XcodeSDK::GetSysroot() const { return m_sysroot; }
146+
145147
bool XcodeSDK::Info::operator<(const Info &other) const {
146148
return std::tie(type, version, internal) <
147149
std::tie(other.type, other.version, other.internal);
@@ -153,17 +155,24 @@ bool XcodeSDK::Info::operator==(const Info &other) const {
153155
}
154156

155157
void XcodeSDK::Merge(const XcodeSDK &other) {
158+
auto add_internal_sdk_suffix = [](llvm::StringRef sdk) {
159+
return (sdk.substr(0, sdk.size() - 3) + "Internal.sdk").str();
160+
};
161+
156162
// The "bigger" SDK always wins.
157163
auto l = Parse();
158164
auto r = other.Parse();
159165
if (l < r)
160166
*this = other;
161167
else {
162168
// The Internal flag always wins.
163-
if (llvm::StringRef(m_name).ends_with(".sdk"))
164-
if (!l.internal && r.internal)
165-
m_name =
166-
m_name.substr(0, m_name.size() - 3) + std::string("Internal.sdk");
169+
if (!l.internal && r.internal) {
170+
if (llvm::StringRef(m_name).ends_with(".sdk"))
171+
m_name = add_internal_sdk_suffix(m_name);
172+
173+
if (m_sysroot.GetFileNameExtension() == ".sdk")
174+
m_sysroot.SetFilename(add_internal_sdk_suffix(m_sysroot.GetFilename()));
175+
}
167176
}
168177
}
169178

lldb/unittests/SymbolFile/DWARF/XcodeSDKModuleTests.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,13 +307,28 @@ SDKPathParsingTestData sdkPathParsingTestCases[] = {
307307
.expect_internal_sdk = true,
308308
.expect_sdk_path_pattern = "Internal.sdk"},
309309

310-
/// Two CUs with an internal SDK each
310+
/// Two CUs with a public (non-CommandLineTools) SDK each
311+
{.input_sdk_paths = {"/Path/To/SDKs/iPhoneOS14.1.sdk",
312+
"/Path/To/SDKs/MacOSX11.3.sdk"},
313+
.expect_mismatch = false,
314+
.expect_internal_sdk = false,
315+
.expect_sdk_path_pattern = "iPhoneOS14.1.sdk"},
316+
317+
/// One CU with CommandLineTools and the other a public SDK
311318
{.input_sdk_paths =
312319
{"/Library/Developer/CommandLineTools/SDKs/iPhoneOS14.1.sdk",
313-
"/Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk"},
320+
"/Path/To/SDKs/MacOSX11.3.sdk"},
314321
.expect_mismatch = false,
315322
.expect_internal_sdk = false,
316323
.expect_sdk_path_pattern = "iPhoneOS14.1.sdk"},
324+
325+
/// One CU with CommandLineTools and the other an internal SDK
326+
{.input_sdk_paths =
327+
{"/Library/Developer/CommandLineTools/SDKs/iPhoneOS14.1.sdk",
328+
"/Path/To/SDKs/MacOSX11.3.Internal.sdk"},
329+
.expect_mismatch = true,
330+
.expect_internal_sdk = true,
331+
.expect_sdk_path_pattern = "iPhoneOS14.1.Internal.sdk"},
317332
};
318333

319334
INSTANTIATE_TEST_SUITE_P(SDKPathParsingTests, SDKPathParsingMultiparamTests,

lldb/unittests/Utility/XcodeSDKTest.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,17 @@ TEST(XcodeSDKTest, ParseTest) {
3434
EXPECT_EQ(XcodeSDK("MacOSX10.9.sdk").GetVersion(), llvm::VersionTuple(10, 9));
3535
EXPECT_EQ(XcodeSDK("MacOSX10.15.4.sdk").GetVersion(), llvm::VersionTuple(10, 15));
3636
EXPECT_EQ(XcodeSDK("MacOSX.sdk").IsAppleInternalSDK(), false);
37+
EXPECT_EQ(
38+
XcodeSDK("MacOSX.sdk", FileSpec{"/Path/To/MacOSX.sdk"}).GetSysroot(),
39+
FileSpec("/Path/To/MacOSX.sdk"));
3740
EXPECT_EQ(XcodeSDK("MacOSX10.15.Internal.sdk").GetType(), XcodeSDK::MacOSX);
3841
EXPECT_EQ(XcodeSDK("MacOSX10.15.Internal.sdk").GetVersion(),
3942
llvm::VersionTuple(10, 15));
4043
EXPECT_EQ(XcodeSDK("MacOSX10.15.Internal.sdk").IsAppleInternalSDK(), true);
44+
EXPECT_FALSE(XcodeSDK("MacOSX10.15.Internal.sdk").GetSysroot());
4145
EXPECT_EQ(XcodeSDK().GetType(), XcodeSDK::unknown);
4246
EXPECT_EQ(XcodeSDK().GetVersion(), llvm::VersionTuple());
47+
EXPECT_FALSE(XcodeSDK().GetSysroot());
4348
}
4449

4550
TEST(XcodeSDKTest, MergeTest) {
@@ -60,6 +65,14 @@ TEST(XcodeSDKTest, MergeTest) {
6065
XcodeSDK empty;
6166
empty.Merge(XcodeSDK("MacOSX10.14.Internal.sdk"));
6267
EXPECT_EQ(empty.GetString(), llvm::StringRef("MacOSX10.14.Internal.sdk"));
68+
EXPECT_FALSE(empty.GetSysroot());
69+
empty.Merge(XcodeSDK("MacOSX9.5.Internal.sdk", FileSpec{"/Path/To/9.5.sdk"}));
70+
EXPECT_FALSE(empty.GetSysroot());
71+
empty.Merge(XcodeSDK("MacOSX12.5.sdk", FileSpec{"/Path/To/12.5.sdk"}));
72+
EXPECT_EQ(empty.GetSysroot(), FileSpec{"/Path/To/12.5.sdk"});
73+
empty.Merge(XcodeSDK("MacOSX11.5.Internal.sdk",
74+
FileSpec{"/Path/To/12.5.Internal.sdk"}));
75+
EXPECT_EQ(empty.GetSysroot(), FileSpec{"/Path/To/12.5.Internal.sdk"});
6376
}
6477

6578
#ifndef _WIN32

0 commit comments

Comments
 (0)