Skip to content

Commit e3b0414

Browse files
committed
[lldb] Change the xcrun (fallback) logic in GetXcodeSDK
This changes the logic in GetXcodeSDK to find an SDK with xcrun. The code now executes the following steps: 1. If DEVELOPER_DIR is set in the environment, it invokes xcrun with the given developer dir. If this fails we stop and don't fall back. 2. If the shlib dir is set and exists,it invokes xcrun with the developer dir corresponding to the shlib dir. If this fails we fall back to 3. 3. We run xcrun without a developer dir. The new behavior introduced in this patch is that we fall back to running xcrun without a developer dir if running it based on the shlib dir failed. A situation where this matters is when you're running lldb from an Xcode that has no SDKs and that is not xcode-selected. Based on lldb's shlib dir pointing into this Xcode installation, it will do an xcrun with the developer set to the Xcode without any SDKs which will fail. With this patch, when that happens, we'll fall back to trying the xcode-selected Xcode by running xcrun without a developer dir. Differential revision: https://reviews.llvm.org/D88866
1 parent dfa70a4 commit e3b0414

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

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

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -373,26 +373,19 @@ static void ParseOSVersion(llvm::VersionTuple &version, NSString *Key) {
373373
static std::string GetXcodeSDK(XcodeSDK sdk) {
374374
XcodeSDK::Info info = sdk.Parse();
375375
std::string sdk_name = XcodeSDK::GetCanonicalName(info);
376-
auto find_sdk = [](std::string sdk_name) -> std::string {
377-
std::string xcrun_cmd;
378-
std::string developer_dir = GetEnvDeveloperDir();
379-
if (developer_dir.empty())
380-
if (FileSpec fspec = HostInfo::GetShlibDir())
381-
if (FileSystem::Instance().Exists(fspec)) {
382-
FileSpec path(
383-
XcodeSDK::FindXcodeContentsDirectoryInPath(fspec.GetPath()));
384-
if (path.RemoveLastPathComponent())
385-
developer_dir = path.GetPath();
386-
}
376+
377+
auto xcrun = [](const std::string &sdk,
378+
llvm::StringRef developer_dir = "") -> std::string {
379+
std::string xcrun_cmd = "xcrun --show-sdk-path --sdk " + sdk;
387380
if (!developer_dir.empty())
388-
xcrun_cmd = "/usr/bin/env DEVELOPER_DIR=\"" + developer_dir + "\" ";
389-
xcrun_cmd += "xcrun --show-sdk-path --sdk " + sdk_name;
381+
xcrun_cmd = "/usr/bin/env DEVELOPER_DIR=\"" + developer_dir.str() +
382+
"\" " + xcrun_cmd;
390383

391384
int status = 0;
392385
int signo = 0;
393386
std::string output_str;
394387
lldb_private::Status error =
395-
Host::RunShellCommand(xcrun_cmd.c_str(), FileSpec(), &status, &signo,
388+
Host::RunShellCommand(xcrun_cmd, FileSpec(), &status, &signo,
396389
&output_str, std::chrono::seconds(15));
397390

398391
// Check that xcrun return something useful.
@@ -414,6 +407,33 @@ FileSpec path(
414407
return output.str();
415408
};
416409

410+
auto find_sdk = [&xcrun](const std::string &sdk_name) -> std::string {
411+
// Invoke xcrun with the developer dir specified in the environment.
412+
std::string developer_dir = GetEnvDeveloperDir();
413+
if (!developer_dir.empty()) {
414+
// Don't fallback if DEVELOPER_DIR was set.
415+
return xcrun(sdk_name, developer_dir);
416+
}
417+
418+
// Invoke xcrun with the shlib dir.
419+
if (FileSpec fspec = HostInfo::GetShlibDir()) {
420+
if (FileSystem::Instance().Exists(fspec)) {
421+
std::string contents_dir =
422+
XcodeSDK::FindXcodeContentsDirectoryInPath(fspec.GetPath());
423+
llvm::StringRef shlib_developer_dir =
424+
llvm::sys::path::parent_path(contents_dir);
425+
if (!shlib_developer_dir.empty()) {
426+
std::string sdk = xcrun(sdk_name, std::move(shlib_developer_dir));
427+
if (!sdk.empty())
428+
return sdk;
429+
}
430+
}
431+
}
432+
433+
// Invoke xcrun without a developer dir as a last resort.
434+
return xcrun(sdk_name);
435+
};
436+
417437
std::string path = find_sdk(sdk_name);
418438
while (path.empty()) {
419439
// Try an alternate spelling of the name ("macosx10.9internal").

0 commit comments

Comments
 (0)