Skip to content

Commit 97ca349

Browse files
committed
Factor out xcrun into a function (NFC)
1 parent 23da210 commit 97ca349

File tree

1 file changed

+67
-65
lines changed

1 file changed

+67
-65
lines changed

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

Lines changed: 67 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -373,84 +373,85 @@ static void ParseOSVersion(llvm::VersionTuple &version, NSString *Key) {
373373
return g_developer_directory;
374374
}
375375

376-
llvm::Expected<std::string> GetXcodeSDK(XcodeSDK sdk) {
377-
XcodeSDK::Info info = sdk.Parse();
378-
std::string sdk_name = XcodeSDK::GetCanonicalName(info);
379-
if (sdk_name.empty())
380-
return llvm::createStringError(llvm::inconvertibleErrorCode(),
381-
"Unrecognized SDK type: " + sdk.GetString());
376+
static llvm::Expected<std::string>
377+
xcrun(const std::string &sdk, llvm::ArrayRef<llvm : StringRef> arguments,
378+
llvm::StringRef developer_dir = "") {
379+
Args args;
380+
if (!developer_dir.empty()) {
381+
args.AppendArgument("/usr/bin/env");
382+
args.AppendArgument("DEVELOPER_DIR=" + developer_dir.str());
383+
}
384+
args.AppendArgument("/usr/bin/xcrun");
385+
args.AppendArgument("--sdk");
386+
args.AppendArgument(sdk);
387+
for (auto arg: arguments)
388+
args.AppendArguent(arg);
382389

383390
Log *log = GetLog(LLDBLog::Host);
391+
if (log) {
392+
std::string cmdstr;
393+
args.GetCommandString(cmdstr);
394+
log->Printf("GetXcodeSDK() running shell cmd '%s'", cmdstr.c_str());
395+
}
384396

385-
auto xcrun = [](const std::string &sdk,
386-
llvm::StringRef developer_dir =
387-
"") -> llvm::Expected<std::string> {
388-
Args args;
389-
if (!developer_dir.empty()) {
390-
args.AppendArgument("/usr/bin/env");
391-
args.AppendArgument("DEVELOPER_DIR=" + developer_dir.str());
392-
}
393-
args.AppendArgument("/usr/bin/xcrun");
394-
args.AppendArgument("--show-sdk-path");
395-
args.AppendArgument("--sdk");
396-
args.AppendArgument(sdk);
397-
398-
Log *log = GetLog(LLDBLog::Host);
399-
if (log) {
400-
std::string cmdstr;
401-
args.GetCommandString(cmdstr);
402-
log->Printf("GetXcodeSDK() running shell cmd '%s'", cmdstr.c_str());
403-
}
397+
int status = 0;
398+
int signo = 0;
399+
std::string output_str;
400+
// The first time after Xcode was updated or freshly installed,
401+
// xcrun can take surprisingly long to build up its database.
402+
auto timeout = std::chrono::seconds(60);
403+
bool run_in_shell = false;
404+
lldb_private::Status error = Host::RunShellCommand(
405+
args, FileSpec(), &status, &signo, &output_str, timeout, run_in_shell);
406+
407+
// Check that xcrun returned something useful.
408+
if (error.Fail()) {
409+
// Catastrophic error.
410+
LLDB_LOG(log, "xcrun failed to execute: %s", error.AsCString());
411+
return error.ToError();
412+
}
413+
if (status != 0) {
414+
// xcrun didn't find a matching SDK. Not an error, we'll try
415+
// different spellings.
416+
LLDB_LOG(log, "xcrun returned exit code %d", status);
417+
return "";
418+
}
419+
if (output_str.empty()) {
420+
LLDB_LOG(log, "xcrun returned no results");
421+
return "";
422+
}
404423

405-
int status = 0;
406-
int signo = 0;
407-
std::string output_str;
408-
// The first time after Xcode was updated or freshly installed,
409-
// xcrun can take surprisingly long to build up its database.
410-
auto timeout = std::chrono::seconds(60);
411-
bool run_in_shell = false;
412-
lldb_private::Status error = Host::RunShellCommand(
413-
args, FileSpec(), &status, &signo, &output_str, timeout, run_in_shell);
414-
415-
// Check that xcrun returned something useful.
416-
if (error.Fail()) {
417-
// Catastrophic error.
418-
LLDB_LOG(log, "xcrun failed to execute: %s", error.AsCString());
419-
return error.ToError();
420-
}
421-
if (status != 0) {
422-
// xcrun didn't find a matching SDK. Not an error, we'll try
423-
// different spellings.
424-
LLDB_LOG(log, "xcrun returned exit code %d", status);
425-
return "";
426-
}
427-
if (output_str.empty()) {
428-
LLDB_LOG(log, "xcrun returned no results");
429-
return "";
430-
}
424+
// Convert to a StringRef so we can manipulate the string without modifying
425+
// the underlying data.
426+
llvm::StringRef output(output_str);
431427

432-
// Convert to a StringRef so we can manipulate the string without modifying
433-
// the underlying data.
434-
llvm::StringRef output(output_str);
428+
// Remove any trailing newline characters.
429+
output = output.rtrim();
435430

436-
// Remove any trailing newline characters.
437-
output = output.rtrim();
431+
// Strip any leading newline characters and everything before them.
432+
const size_t last_newline = output.rfind('\n');
433+
if (last_newline != llvm::StringRef::npos)
434+
output = output.substr(last_newline + 1);
438435

439-
// Strip any leading newline characters and everything before them.
440-
const size_t last_newline = output.rfind('\n');
441-
if (last_newline != llvm::StringRef::npos)
442-
output = output.substr(last_newline + 1);
436+
return output.str();
437+
};
443438

444-
return output.str();
445-
};
439+
llvm::Expected<std::string> GetXcodeSDK(XcodeSDK sdk) {
440+
XcodeSDK::Info info = sdk.Parse();
441+
std::string sdk_name = XcodeSDK::GetCanonicalName(info);
442+
if (sdk_name.empty())
443+
return llvm::createStringError(llvm::inconvertibleErrorCode(),
444+
"Unrecognized SDK type: " + sdk.GetString());
445+
446+
Log *log = GetLog(LLDBLog::Host);
446447

447448
auto find_sdk =
448-
[&xcrun](const std::string &sdk_name) -> llvm::Expected<std::string> {
449+
[](const std::string &sdk_name) -> llvm::Expected<std::string> {
449450
// Invoke xcrun with the developer dir specified in the environment.
450451
std::string developer_dir = GetEnvDeveloperDir();
451452
if (!developer_dir.empty()) {
452453
// Don't fallback if DEVELOPER_DIR was set.
453-
return xcrun(sdk_name, developer_dir);
454+
return xcrun(sdk_name, {"--show-sdk-path"}, developer_dir);
454455
}
455456

456457
// Invoke xcrun with the shlib dir.
@@ -461,7 +462,8 @@ static void ParseOSVersion(llvm::VersionTuple &version, NSString *Key) {
461462
llvm::StringRef shlib_developer_dir =
462463
llvm::sys::path::parent_path(contents_dir);
463464
if (!shlib_developer_dir.empty()) {
464-
auto sdk = xcrun(sdk_name, std::move(shlib_developer_dir));
465+
auto sdk = xcrun(sdk_name, {"--show-sdk-path"},
466+
std::move(shlib_developer_dir));
465467
if (!sdk)
466468
return sdk.takeError();
467469
if (!sdk->empty())

0 commit comments

Comments
 (0)