From 97ca34996dbe5a61e79d7c559af7b15dc39c08a5 Mon Sep 17 00:00:00 2001 From: Adrian Prantl Date: Fri, 26 May 2023 13:01:34 -0700 Subject: [PATCH] Factor out xcrun into a function (NFC) --- lldb/source/Host/macosx/objcxx/HostInfoMacOSX.mm | 132 ++++++++++++----------- 1 file changed, 67 insertions(+), 65 deletions(-) diff --git a/lldb/source/Host/macosx/objcxx/HostInfoMacOSX.mm b/lldb/source/Host/macosx/objcxx/HostInfoMacOSX.mm index 5a39ed3..22298da 100644 --- a/lldb/source/Host/macosx/objcxx/HostInfoMacOSX.mm +++ b/lldb/source/Host/macosx/objcxx/HostInfoMacOSX.mm @@ -373,84 +373,85 @@ lldb_private::FileSpec HostInfoMacOSX::GetXcodeDeveloperDirectory() { return g_developer_directory; } -llvm::Expected GetXcodeSDK(XcodeSDK sdk) { - XcodeSDK::Info info = sdk.Parse(); - std::string sdk_name = XcodeSDK::GetCanonicalName(info); - if (sdk_name.empty()) - return llvm::createStringError(llvm::inconvertibleErrorCode(), - "Unrecognized SDK type: " + sdk.GetString()); +static llvm::Expected +xcrun(const std::string &sdk, llvm::ArrayRef arguments, + llvm::StringRef developer_dir = "") { + Args args; + if (!developer_dir.empty()) { + args.AppendArgument("/usr/bin/env"); + args.AppendArgument("DEVELOPER_DIR=" + developer_dir.str()); + } + args.AppendArgument("/usr/bin/xcrun"); + args.AppendArgument("--sdk"); + args.AppendArgument(sdk); + for (auto arg: arguments) + args.AppendArguent(arg); Log *log = GetLog(LLDBLog::Host); + if (log) { + std::string cmdstr; + args.GetCommandString(cmdstr); + log->Printf("GetXcodeSDK() running shell cmd '%s'", cmdstr.c_str()); + } - auto xcrun = [](const std::string &sdk, - llvm::StringRef developer_dir = - "") -> llvm::Expected { - Args args; - if (!developer_dir.empty()) { - args.AppendArgument("/usr/bin/env"); - args.AppendArgument("DEVELOPER_DIR=" + developer_dir.str()); - } - args.AppendArgument("/usr/bin/xcrun"); - args.AppendArgument("--show-sdk-path"); - args.AppendArgument("--sdk"); - args.AppendArgument(sdk); - - Log *log = GetLog(LLDBLog::Host); - if (log) { - std::string cmdstr; - args.GetCommandString(cmdstr); - log->Printf("GetXcodeSDK() running shell cmd '%s'", cmdstr.c_str()); - } + int status = 0; + int signo = 0; + std::string output_str; + // The first time after Xcode was updated or freshly installed, + // xcrun can take surprisingly long to build up its database. + auto timeout = std::chrono::seconds(60); + bool run_in_shell = false; + lldb_private::Status error = Host::RunShellCommand( + args, FileSpec(), &status, &signo, &output_str, timeout, run_in_shell); + + // Check that xcrun returned something useful. + if (error.Fail()) { + // Catastrophic error. + LLDB_LOG(log, "xcrun failed to execute: %s", error.AsCString()); + return error.ToError(); + } + if (status != 0) { + // xcrun didn't find a matching SDK. Not an error, we'll try + // different spellings. + LLDB_LOG(log, "xcrun returned exit code %d", status); + return ""; + } + if (output_str.empty()) { + LLDB_LOG(log, "xcrun returned no results"); + return ""; + } - int status = 0; - int signo = 0; - std::string output_str; - // The first time after Xcode was updated or freshly installed, - // xcrun can take surprisingly long to build up its database. - auto timeout = std::chrono::seconds(60); - bool run_in_shell = false; - lldb_private::Status error = Host::RunShellCommand( - args, FileSpec(), &status, &signo, &output_str, timeout, run_in_shell); - - // Check that xcrun returned something useful. - if (error.Fail()) { - // Catastrophic error. - LLDB_LOG(log, "xcrun failed to execute: %s", error.AsCString()); - return error.ToError(); - } - if (status != 0) { - // xcrun didn't find a matching SDK. Not an error, we'll try - // different spellings. - LLDB_LOG(log, "xcrun returned exit code %d", status); - return ""; - } - if (output_str.empty()) { - LLDB_LOG(log, "xcrun returned no results"); - return ""; - } + // Convert to a StringRef so we can manipulate the string without modifying + // the underlying data. + llvm::StringRef output(output_str); - // Convert to a StringRef so we can manipulate the string without modifying - // the underlying data. - llvm::StringRef output(output_str); + // Remove any trailing newline characters. + output = output.rtrim(); - // Remove any trailing newline characters. - output = output.rtrim(); + // Strip any leading newline characters and everything before them. + const size_t last_newline = output.rfind('\n'); + if (last_newline != llvm::StringRef::npos) + output = output.substr(last_newline + 1); - // Strip any leading newline characters and everything before them. - const size_t last_newline = output.rfind('\n'); - if (last_newline != llvm::StringRef::npos) - output = output.substr(last_newline + 1); + return output.str(); +}; - return output.str(); - }; +llvm::Expected GetXcodeSDK(XcodeSDK sdk) { + XcodeSDK::Info info = sdk.Parse(); + std::string sdk_name = XcodeSDK::GetCanonicalName(info); + if (sdk_name.empty()) + return llvm::createStringError(llvm::inconvertibleErrorCode(), + "Unrecognized SDK type: " + sdk.GetString()); + + Log *log = GetLog(LLDBLog::Host); auto find_sdk = - [&xcrun](const std::string &sdk_name) -> llvm::Expected { + [](const std::string &sdk_name) -> llvm::Expected { // 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); + return xcrun(sdk_name, {"--show-sdk-path"}, developer_dir); } // Invoke xcrun with the shlib dir. @@ -461,7 +462,8 @@ llvm::Expected GetXcodeSDK(XcodeSDK sdk) { llvm::StringRef shlib_developer_dir = llvm::sys::path::parent_path(contents_dir); if (!shlib_developer_dir.empty()) { - auto sdk = xcrun(sdk_name, std::move(shlib_developer_dir)); + auto sdk = xcrun(sdk_name, {"--show-sdk-path"}, + std::move(shlib_developer_dir)); if (!sdk) return sdk.takeError(); if (!sdk->empty()) -- 2.7.4