[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
This commit is contained in:
Jonas Devlieghere 2020-10-06 15:12:23 -07:00
parent dfa70a483a
commit e3b0414b0e
1 changed files with 34 additions and 14 deletions

View File

@ -373,26 +373,19 @@ lldb_private::FileSpec HostInfoMacOSX::GetXcodeDeveloperDirectory() {
static std::string GetXcodeSDK(XcodeSDK sdk) {
XcodeSDK::Info info = sdk.Parse();
std::string sdk_name = XcodeSDK::GetCanonicalName(info);
auto find_sdk = [](std::string sdk_name) -> std::string {
std::string xcrun_cmd;
std::string developer_dir = GetEnvDeveloperDir();
if (developer_dir.empty())
if (FileSpec fspec = HostInfo::GetShlibDir())
if (FileSystem::Instance().Exists(fspec)) {
FileSpec path(
XcodeSDK::FindXcodeContentsDirectoryInPath(fspec.GetPath()));
if (path.RemoveLastPathComponent())
developer_dir = path.GetPath();
}
auto xcrun = [](const std::string &sdk,
llvm::StringRef developer_dir = "") -> std::string {
std::string xcrun_cmd = "xcrun --show-sdk-path --sdk " + sdk;
if (!developer_dir.empty())
xcrun_cmd = "/usr/bin/env DEVELOPER_DIR=\"" + developer_dir + "\" ";
xcrun_cmd += "xcrun --show-sdk-path --sdk " + sdk_name;
xcrun_cmd = "/usr/bin/env DEVELOPER_DIR=\"" + developer_dir.str() +
"\" " + xcrun_cmd;
int status = 0;
int signo = 0;
std::string output_str;
lldb_private::Status error =
Host::RunShellCommand(xcrun_cmd.c_str(), FileSpec(), &status, &signo,
Host::RunShellCommand(xcrun_cmd, FileSpec(), &status, &signo,
&output_str, std::chrono::seconds(15));
// Check that xcrun return something useful.
@ -414,6 +407,33 @@ static std::string GetXcodeSDK(XcodeSDK sdk) {
return output.str();
};
auto find_sdk = [&xcrun](const std::string &sdk_name) -> std::string {
// Invoke xcrun with the developer dir specified in the environment.
std::string developer_dir = GetEnvDeveloperDir();
if (!developer_dir.empty()) {
// Don't fallback if DEVELOPER_DIR was set.
return xcrun(sdk_name, developer_dir);
}
// Invoke xcrun with the shlib dir.
if (FileSpec fspec = HostInfo::GetShlibDir()) {
if (FileSystem::Instance().Exists(fspec)) {
std::string contents_dir =
XcodeSDK::FindXcodeContentsDirectoryInPath(fspec.GetPath());
llvm::StringRef shlib_developer_dir =
llvm::sys::path::parent_path(contents_dir);
if (!shlib_developer_dir.empty()) {
std::string sdk = xcrun(sdk_name, std::move(shlib_developer_dir));
if (!sdk.empty())
return sdk;
}
}
}
// Invoke xcrun without a developer dir as a last resort.
return xcrun(sdk_name);
};
std::string path = find_sdk(sdk_name);
while (path.empty()) {
// Try an alternate spelling of the name ("macosx10.9internal").