Skip to content

Commit ade3c6a

Browse files
committed
Host: generalise GetXcodeSDKPath
This generalises the GetXcodeSDKPath hook to a GetSDKRoot path which will be re-used for the Windows support to compute a language specific SDK path on the platform. Because there may be other options that we wish to use to compute the SDK path, sink the XcodeSDK parameter into a structure which can pass a disaggregated set of options. Furthermore, optionalise the parameter as Xcode is not available for all platforms. Differential Revision: https://reviews.llvm.org/D149397 Reviewed By: JDevlieghere
1 parent 8c8fe11 commit ade3c6a

File tree

7 files changed

+42
-12
lines changed

7 files changed

+42
-12
lines changed

lldb/include/lldb/Host/HostInfoBase.h

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@ struct SharedCacheImageInfo {
3131
lldb::DataBufferSP data_sp;
3232
};
3333

34+
namespace {
35+
struct HostInfoError : public llvm::ErrorInfo<HostInfoError> {
36+
static char ID;
37+
const std::string message_;
38+
39+
HostInfoError(const std::string message) : message_(std::move(message)) {}
40+
41+
void log(llvm::raw_ostream &OS) const override { OS << "HostInfoError"; }
42+
43+
std::error_code convertToErrorCode() const override {
44+
return llvm::inconvertibleErrorCode();
45+
}
46+
};
47+
48+
char HostInfoError::ID = 0;
49+
} // namespace
50+
3451
class HostInfoBase {
3552
private:
3653
// Static class, unconstructable.
@@ -108,10 +125,14 @@ class HostInfoBase {
108125

109126
static FileSpec GetXcodeContentsDirectory() { return {}; }
110127
static FileSpec GetXcodeDeveloperDirectory() { return {}; }
111-
112-
/// Return the directory containing a specific Xcode SDK.
113-
static llvm::Expected<llvm::StringRef> GetXcodeSDKPath(XcodeSDK sdk) {
114-
return "";
128+
129+
struct SDKOptions {
130+
std::optional<XcodeSDK> XcodeSDK;
131+
};
132+
133+
/// Return the directory containing something like a SDK (reused for Swift).
134+
static llvm::Expected<llvm::StringRef> GetSDKRoot(SDKOptions options) {
135+
return llvm::make_error<HostInfoError>("cannot determine SDK root");
115136
}
116137

117138
/// Return information about module \p image_name if it is loaded in

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class HostInfoMacOSX : public HostInfoPosix {
3131
static FileSpec GetXcodeDeveloperDirectory();
3232

3333
/// Query xcrun to find an Xcode SDK directory.
34-
static llvm::Expected<llvm::StringRef> GetXcodeSDKPath(XcodeSDK sdk);
34+
static llvm::Expected<llvm::StringRef> GetSDKRoot(SDKOptions options);
3535

3636
/// Shared cache utilities
3737
static SharedCacheImageInfo

lldb/source/Core/Module.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,8 +1607,8 @@ std::optional<std::string> Module::RemapSourceFile(llvm::StringRef path) const {
16071607

16081608
void Module::RegisterXcodeSDK(llvm::StringRef sdk_name,
16091609
llvm::StringRef sysroot) {
1610-
XcodeSDK sdk(sdk_name.str());
1611-
auto sdk_path_or_err = HostInfo::GetXcodeSDKPath(sdk);
1610+
auto sdk_path_or_err =
1611+
HostInfo::GetSDKRoot(HostInfo::SDKOptions{sdk_name.str()});
16121612

16131613
if (!sdk_path_or_err) {
16141614
Debugger::ReportError("Error while searching for Xcode SDK: " +

lldb/source/Host/macosx/objcxx/HostInfoMacOSX.mm

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,8 @@ static void ParseOSVersion(llvm::VersionTuple &version, NSString *Key) {
338338
}
339339
}
340340

341-
auto sdk_path_or_err = HostInfo::GetXcodeSDKPath(XcodeSDK::GetAnyMacOS());
341+
auto sdk_path_or_err =
342+
HostInfo::GetSDKRoot(SDKOptions{XcodeSDK::GetAnyMacOS()});
342343
if (!sdk_path_or_err) {
343344
Log *log = GetLog(LLDBLog::Host);
344345
LLDB_LOGF(log, "Error while searching for Xcode SDK: %s",
@@ -519,7 +520,7 @@ static void ParseOSVersion(llvm::VersionTuple &version, NSString *Key) {
519520
return path;
520521
}
521522

522-
llvm::Expected<llvm::StringRef> HostInfoMacOSX::GetXcodeSDKPath(XcodeSDK sdk) {
523+
llvm::Expected<llvm::StringRef> HostInfoMacOSX::GetSDKRoot(SDKOptions options) {
523524
struct ErrorOrPath {
524525
std::string str;
525526
bool is_error;
@@ -530,6 +531,11 @@ static void ParseOSVersion(llvm::VersionTuple &version, NSString *Key) {
530531
std::lock_guard<std::mutex> guard(g_sdk_path_mutex);
531532
LLDB_SCOPED_TIMER();
532533

534+
if (!options.XcodeSDK)
535+
return llvm::createStringError(llvm::inconvertibleErrorCode(),
536+
"XCodeSDK not specified");
537+
XcodeSDK sdk = *options.XcodeSDK;
538+
533539
auto key = sdk.GetString();
534540
auto it = g_sdk_path.find(key);
535541
if (it != g_sdk_path.end()) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,8 @@ static llvm::StringRef GetXcodeSDKDir(std::string preferred,
284284
std::string secondary) {
285285
llvm::StringRef sdk;
286286
auto get_sdk = [&](std::string sdk) -> llvm::StringRef {
287-
auto sdk_path_or_err = HostInfo::GetXcodeSDKPath(XcodeSDK(std::move(sdk)));
287+
auto sdk_path_or_err =
288+
HostInfo::GetSDKRoot(HostInfo::SDKOptions{XcodeSDK(std::move(sdk))});
288289
if (!sdk_path_or_err) {
289290
Debugger::ReportError("Error while searching for Xcode SDK: " +
290291
toString(sdk_path_or_err.takeError()));

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ ConstString PlatformMacOSX::GetSDKDirectory(lldb_private::Target &target) {
124124
}
125125

126126
// Use the default SDK as a fallback.
127-
auto sdk_path_or_err = HostInfo::GetXcodeSDKPath(XcodeSDK::GetAnyMacOS());
127+
auto sdk_path_or_err =
128+
HostInfo::GetSDKRoot(HostInfo::SDKOptions{XcodeSDK::GetAnyMacOS()});
128129
if (!sdk_path_or_err) {
129130
Debugger::ReportError("Error while searching for Xcode SDK: " +
130131
toString(sdk_path_or_err.takeError()));

lldb/unittests/Host/HostInfoTest.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ TEST_F(HostInfoTest, GetHostname) {
5757
#if defined(__APPLE__)
5858
TEST_F(HostInfoTest, GetXcodeSDK) {
5959
auto get_sdk = [](std::string sdk, bool error = false) -> llvm::StringRef {
60-
auto sdk_path_or_err = HostInfo::GetXcodeSDKPath(XcodeSDK(std::move(sdk)));
60+
auto sdk_path_or_err =
61+
HostInfo::GetSDKRoot(HostInfo::SDKOptions{XcodeSDK(std::move(sdk))});
6162
if (!error) {
6263
EXPECT_TRUE((bool)sdk_path_or_err);
6364
return *sdk_path_or_err;

0 commit comments

Comments
 (0)