Skip to content

Commit 907b2f4

Browse files
committed
fixup! store sysroot inside XcodeSDK
1 parent 2b5628e commit 907b2f4

File tree

7 files changed

+81
-18
lines changed

7 files changed

+81
-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
@@ -23,6 +23,8 @@ namespace lldb_private {
2323
/// An abstraction for Xcode-style SDKs that works like \ref ArchSpec.
2424
class XcodeSDK {
2525
std::string m_name;
26+
std::optional<std::string>
27+
m_sysroot_path; // TODO: should this be std::optional<FileSpec>?
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, std::string sysroot)
68+
: m_name(std::move(name)), m_sysroot_path(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+
std::optional<llvm::StringRef> GetSysroot() const;
8287
/// Whether this Xcode SDK supports Swift.
8388
bool SupportsSwift() const;
8489

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ static Status ExceptionMaskValidator(const char *string, void *unused) {
7979
return {};
8080
}
8181

82+
static bool XcodeSysrootExists(XcodeSDK const &sdk) {
83+
auto maybe_sysroot = sdk.GetSysroot();
84+
if (!maybe_sysroot)
85+
return false;
86+
87+
return FileSystem::Instance().Exists(*maybe_sysroot);
88+
}
89+
8290
/// Destructor.
8391
///
8492
/// The destructor is virtual since this class is designed to be
@@ -1395,6 +1403,7 @@ PlatformDarwin::GetSDKPathFromDebugInfo(Module &module) {
13951403
llvm::formatv("No symbol file available for module '{0}'",
13961404
module.GetFileSpec().GetFilename().AsCString("")));
13971405

1406+
std::string sysroot;
13981407
bool found_public_sdk = false;
13991408
bool found_internal_sdk = false;
14001409
XcodeSDK merged_sdk;
@@ -1425,6 +1434,9 @@ PlatformDarwin::ResolveSDKPathFromDebugInfo(Module &module) {
14251434

14261435
auto [sdk, _] = std::move(*sdk_or_err);
14271436

1437+
if (XcodeSysrootExists(sdk))
1438+
return sdk.GetSysroot()->str();
1439+
14281440
auto path_or_err = HostInfo::GetSDKRoot(HostInfo::SDKOptions{sdk});
14291441
if (!path_or_err)
14301442
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, std::move(sysroot)};
10111016
}
10121017

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

lldb/source/Utility/XcodeSDK.cpp

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

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

145+
std::optional<llvm::StringRef> XcodeSDK::GetSysroot() const {
146+
return m_sysroot_path;
147+
}
148+
145149
bool XcodeSDK::Info::operator<(const Info &other) const {
146150
return std::tie(type, version, internal) <
147151
std::tie(other.type, other.version, other.internal);
@@ -153,17 +157,24 @@ bool XcodeSDK::Info::operator==(const Info &other) const {
153157
}
154158

155159
void XcodeSDK::Merge(const XcodeSDK &other) {
160+
auto add_internal_sdk_suffix = [](std::string const &sdk) {
161+
return sdk.substr(0, sdk.size() - 3) + "Internal.sdk";
162+
};
163+
156164
// The "bigger" SDK always wins.
157165
auto l = Parse();
158166
auto r = other.Parse();
159167
if (l < r)
160168
*this = other;
161169
else {
162170
// 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");
171+
if (!l.internal && r.internal) {
172+
if (llvm::StringRef(m_name).ends_with(".sdk"))
173+
m_name = add_internal_sdk_suffix(m_name);
174+
175+
if (m_sysroot_path && llvm::StringRef(*m_sysroot_path).ends_with(".sdk"))
176+
m_sysroot_path.emplace(add_internal_sdk_suffix(*m_sysroot_path));
177+
}
167178
}
168179
}
169180

lldb/unittests/SymbolFile/DWARF/XcodeSDKModuleTests.cpp

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

313-
/// Two CUs with an internal SDK each
313+
/// Two CUs with a public (non-CommandLineTools) SDK each
314+
{.input_sdk_paths = {"/Path/To/SDKs/iPhoneOS14.1.sdk",
315+
"/Path/To/SDKs/MacOSX11.3.sdk"},
316+
.expect_mismatch = false,
317+
.expect_internal_sdk = false,
318+
.expect_sdk_path_pattern = "iPhoneOS14.1.sdk"},
319+
320+
/// One CU with CommandLineTools and the other a public SDK
314321
{.input_sdk_paths =
315322
{"/Library/Developer/CommandLineTools/SDKs/iPhoneOS14.1.sdk",
316-
"/Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk"},
323+
"/Path/To/SDKs/MacOSX11.3.sdk"},
317324
.expect_mismatch = false,
318325
.expect_internal_sdk = false,
319326
.expect_sdk_path_pattern = "iPhoneOS14.1.sdk"},
327+
328+
/// One CU with CommandLineTools and the other an internal SDK
329+
{.input_sdk_paths =
330+
{"/Library/Developer/CommandLineTools/SDKs/iPhoneOS14.1.sdk",
331+
"/Path/To/SDKs/MacOSX11.3.Internal.sdk"},
332+
.expect_mismatch = false,
333+
.expect_internal_sdk = false,
334+
.expect_sdk_path_pattern = "iPhoneOS14.1.Internal.sdk"},
320335
};
321336

322337
INSTANTIATE_TEST_SUITE_P(SDKPathParsingTests, SDKPathParsingMultiparamTests,

lldb/unittests/Utility/XcodeSDKTest.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,16 @@ 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(XcodeSDK("MacOSX.sdk", "/Path/To/MacOSX.sdk").GetSysroot(),
38+
"/Path/To/MacOSX.sdk");
3739
EXPECT_EQ(XcodeSDK("MacOSX10.15.Internal.sdk").GetType(), XcodeSDK::MacOSX);
3840
EXPECT_EQ(XcodeSDK("MacOSX10.15.Internal.sdk").GetVersion(),
3941
llvm::VersionTuple(10, 15));
4042
EXPECT_EQ(XcodeSDK("MacOSX10.15.Internal.sdk").IsAppleInternalSDK(), true);
43+
EXPECT_EQ(XcodeSDK("MacOSX10.15.Internal.sdk").GetSysroot(), std::nullopt);
4144
EXPECT_EQ(XcodeSDK().GetType(), XcodeSDK::unknown);
4245
EXPECT_EQ(XcodeSDK().GetVersion(), llvm::VersionTuple());
46+
EXPECT_EQ(XcodeSDK().GetSysroot(), std::nullopt);
4347
}
4448

4549
TEST(XcodeSDKTest, MergeTest) {
@@ -60,6 +64,14 @@ TEST(XcodeSDKTest, MergeTest) {
6064
XcodeSDK empty;
6165
empty.Merge(XcodeSDK("MacOSX10.14.Internal.sdk"));
6266
EXPECT_EQ(empty.GetString(), llvm::StringRef("MacOSX10.14.Internal.sdk"));
67+
EXPECT_EQ(empty.GetSysroot(), std::nullopt);
68+
empty.Merge(XcodeSDK("MacOSX9.5.Internal.sdk", "/Path/To/9.5.sdk"));
69+
EXPECT_EQ(empty.GetSysroot(), std::nullopt);
70+
empty.Merge(XcodeSDK("MacOSX12.5.sdk", "/Path/To/12.5.sdk"));
71+
EXPECT_EQ(empty.GetSysroot(), "/Path/To/12.5.sdk");
72+
empty.Merge(
73+
XcodeSDK("MacOSX11.5.Internal.sdk", "/Path/To/12.5.Internal.sdk"));
74+
EXPECT_EQ(empty.GetSysroot(), "/Path/To/12.5.Internal.sdk");
6375
}
6476

6577
#ifndef _WIN32

0 commit comments

Comments
 (0)