Skip to content

Teach GetXcodeSDK to look in the Xcode that contains LLDB #1316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lldb/include/lldb/Host/HostInfoBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ class HostInfoBase {
static bool ComputePathRelativeToLibrary(FileSpec &file_spec,
llvm::StringRef dir);

static FileSpec GetXcodeContentsDirectory() { return {}; }
static FileSpec GetXcodeDeveloperDirectory() { return {}; }

/// Return the directory containing a specific Xcode SDK.
static llvm::StringRef GetXcodeSDKPath(XcodeSDK sdk) { return {}; }

Expand Down
3 changes: 2 additions & 1 deletion lldb/include/lldb/Host/macosx/HostInfoMacOSX.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ class HostInfoMacOSX : public HostInfoPosix {
static bool GetOSBuildString(std::string &s);
static bool GetOSKernelDescription(std::string &s);
static FileSpec GetProgramFileSpec();
static std::string FindXcodeContentsDirectoryInPath(llvm::StringRef path);
static FileSpec GetXcodeContentsDirectory();
static FileSpec GetXcodeDeveloperDirectory();

/// Query xcrun to find an Xcode SDK directory.
static llvm::StringRef GetXcodeSDKPath(XcodeSDK sdk);
Expand Down
2 changes: 2 additions & 0 deletions lldb/include/lldb/Utility/XcodeSDK.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ class XcodeSDK {
static std::string GetCanonicalName(Info info);
/// Return the best-matching SDK type for a specific triple.
static XcodeSDK::Type GetSDKTypeForTriple(const llvm::Triple &triple);

static std::string FindXcodeContentsDirectoryInPath(llvm::StringRef path);
};

} // namespace lldb_private
Expand Down
73 changes: 72 additions & 1 deletion lldb/source/Host/macosx/objcxx/HostInfoMacOSX.mm
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,82 @@ static void ParseOSVersion(llvm::VersionTuple &version, NSString *Key) {
}
}

FileSpec HostInfoMacOSX::GetXcodeContentsDirectory() {
static FileSpec g_xcode_contents_path;
static std::once_flag g_once_flag;
std::call_once(g_once_flag, [&]() {
// Try the shlib dir first.
if (FileSpec fspec = HostInfo::GetShlibDir()) {
if (FileSystem::Instance().Exists(fspec)) {
std::string xcode_contents_dir =
XcodeSDK::FindXcodeContentsDirectoryInPath(fspec.GetPath());
if (!xcode_contents_dir.empty()) {
g_xcode_contents_path = FileSpec(xcode_contents_dir);
return;
}
}
}

if (const char *developer_dir_env_var = getenv("DEVELOPER_DIR")) {
FileSpec fspec(developer_dir_env_var);
if (FileSystem::Instance().Exists(fspec)) {
// FIXME: This looks like it couldn't possibly work!
std::string xcode_contents_dir =
XcodeSDK::FindXcodeContentsDirectoryInPath(fspec.GetPath());
if (!xcode_contents_dir.empty()) {
g_xcode_contents_path = FileSpec(xcode_contents_dir);
return;
}
}
}

FileSpec fspec(HostInfo::GetXcodeSDKPath(XcodeSDK::GetAnyMacOS()));
if (fspec) {
if (FileSystem::Instance().Exists(fspec)) {
std::string xcode_contents_dir =
XcodeSDK::FindXcodeContentsDirectoryInPath(fspec.GetPath());
if (!xcode_contents_dir.empty()) {
g_xcode_contents_path = FileSpec(xcode_contents_dir);
return;
}
}
}
});
return g_xcode_contents_path;
}

lldb_private::FileSpec HostInfoMacOSX::GetXcodeDeveloperDirectory() {
static lldb_private::FileSpec g_developer_directory;
static llvm::once_flag g_once_flag;
llvm::call_once(g_once_flag, []() {
if (FileSpec fspec = GetXcodeContentsDirectory()) {
fspec.AppendPathComponent("Developer");
if (FileSystem::Instance().Exists(fspec))
g_developer_directory = fspec;
}
});
return g_developer_directory;
}

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 = "xcrun --show-sdk-path --sdk " + sdk_name;
std::string xcrun_cmd;
Environment env = Host::GetEnvironment();
std::string developer_dir = env.lookup("DEVELOPER_DIR");
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();
}
if (!developer_dir.empty())
xcrun_cmd = "/usr/bin/env DEVELOPER_DIR=\"" + developer_dir + "\" ";
xcrun_cmd += "xcrun --show-sdk-path --sdk " + sdk_name;

int status = 0;
int signo = 0;
std::string output_str;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <mutex>
#include <thread>
#include "lldb/Host/PseudoTerminal.h"
#include "lldb/Host/HostInfo.h"
#include "lldb/Target/Process.h"
#include "lldb/Utility/LLDBAssert.h"
#include "lldb/Utility/Status.h"
Expand Down Expand Up @@ -77,7 +78,7 @@ void PlatformAppleSimulator::GetStatus(Stream &strm) {
// simulator
PlatformAppleSimulator::LoadCoreSimulator();

std::string developer_dir = GetXcodeDeveloperDirectory().GetPath();
std::string developer_dir = HostInfo::GetXcodeDeveloperDirectory().GetPath();
CoreSimulatorSupport::DeviceSet devices =
CoreSimulatorSupport::DeviceSet::GetAvailableDevices(
developer_dir.c_str());
Expand Down Expand Up @@ -124,7 +125,7 @@ Status PlatformAppleSimulator::ConnectRemote(Args &args) {
const char *arg_cstr = args.GetArgumentAtIndex(0);
if (arg_cstr) {
std::string arg_str(arg_cstr);
std::string developer_dir = GetXcodeDeveloperDirectory().GetPath();
std::string developer_dir = HostInfo::GetXcodeDeveloperDirectory().GetPath();
CoreSimulatorSupport::DeviceSet devices =
CoreSimulatorSupport::DeviceSet::GetAvailableDevices(
developer_dir.c_str());
Expand Down Expand Up @@ -214,7 +215,7 @@ FileSpec PlatformAppleSimulator::GetCoreSimulatorPath() {
#if defined(__APPLE__)
std::lock_guard<std::mutex> guard(m_core_sim_path_mutex);
if (!m_core_simulator_framework_path.hasValue()) {
if (FileSpec fspec = GetXcodeDeveloperDirectory()) {
if (FileSpec fspec = HostInfo::GetXcodeDeveloperDirectory()) {
std::string developer_dir = fspec.GetPath();
StreamString cs_path;
cs_path.Printf(
Expand Down Expand Up @@ -247,7 +248,7 @@ CoreSimulatorSupport::Device PlatformAppleSimulator::GetSimulatorDevice() {
if (!m_device.hasValue()) {
const CoreSimulatorSupport::DeviceType::ProductFamilyID dev_id =
CoreSimulatorSupport::DeviceType::ProductFamilyID::iPhone;
std::string developer_dir = GetXcodeDeveloperDirectory().GetPath();
std::string developer_dir = HostInfo::GetXcodeDeveloperDirectory().GetPath();
m_device = CoreSimulatorSupport::DeviceSet::GetAvailableDevices(
developer_dir.c_str())
.GetFanciest(dev_id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ EnumerateDirectoryCallback(void *baton, llvm::sys::fs::file_type ft,
const char *PlatformAppleTVSimulator::GetSDKDirectoryAsCString() {
std::lock_guard<std::mutex> guard(m_sdk_dir_mutex);
if (m_sdk_directory.empty()) {
if (FileSpec fspec = GetXcodeDeveloperDirectory()) {
if (FileSpec fspec = HostInfo::GetXcodeDeveloperDirectory()) {
std::string developer_dir = fspec.GetPath();
char sdks_directory[PATH_MAX];
char sdk_dirname[PATH_MAX];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ EnumerateDirectoryCallback(void *baton, llvm::sys::fs::file_type ft,
const char *PlatformAppleWatchSimulator::GetSDKDirectoryAsCString() {
std::lock_guard<std::mutex> guard(m_sdk_dir_mutex);
if (m_sdk_directory.empty()) {
if (FileSpec fspec = GetXcodeDeveloperDirectory()) {
if (FileSpec fspec = HostInfo::GetXcodeDeveloperDirectory()) {
std::string developer_dir = fspec.GetPath();
char sdks_directory[PATH_MAX];
char sdk_dirname[PATH_MAX];
Expand Down
83 changes: 2 additions & 81 deletions lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1133,19 +1133,6 @@ static FileSpec GetXcodeSelectPath() {
return g_xcode_select_filespec;
}

lldb_private::FileSpec PlatformDarwin::GetXcodeDeveloperDirectory() {
static lldb_private::FileSpec g_developer_directory;
static llvm::once_flag g_once_flag;
llvm::call_once(g_once_flag, []() {
if (FileSpec fspec = GetXcodeContentsDirectory()) {
fspec.AppendPathComponent("Developer");
if (FileSystem::Instance().Exists(fspec))
g_developer_directory = fspec;
}
});
return g_developer_directory;
}

BreakpointSP PlatformDarwin::SetThreadCreationBreakpoint(Target &target) {
BreakpointSP bp_sp;
static const char *g_bp_names[] = {
Expand Down Expand Up @@ -1260,7 +1247,7 @@ FileSpec PlatformDarwin::FindSDKInXcodeForModules(XcodeSDK::Type sdk_type,
}

FileSpec PlatformDarwin::GetSDKDirectoryForModules(XcodeSDK::Type sdk_type) {
FileSpec sdks_spec = GetXcodeContentsDirectory();
FileSpec sdks_spec = HostInfo::GetXcodeContentsDirectory();
sdks_spec.AppendPathComponent("Developer");
sdks_spec.AppendPathComponent("Platforms");

Expand Down Expand Up @@ -1586,7 +1573,7 @@ lldb_private::FileSpec PlatformDarwin::LocateExecutable(const char *basename) {
llvm::call_once(g_once_flag, []() {

// When locating executables, trust the DEVELOPER_DIR first if it is set
FileSpec xcode_contents_dir = GetXcodeContentsDirectory();
FileSpec xcode_contents_dir = HostInfo::GetXcodeContentsDirectory();
if (xcode_contents_dir) {
FileSpec xcode_lldb_resources = xcode_contents_dir;
xcode_lldb_resources.AppendPathComponent("SharedFrameworks");
Expand Down Expand Up @@ -1738,72 +1725,6 @@ std::string PlatformDarwin::FindComponentInPath(llvm::StringRef path,
return {};
}

std::string
PlatformDarwin::FindXcodeContentsDirectoryInPath(llvm::StringRef path) {
auto begin = llvm::sys::path::begin(path);
auto end = llvm::sys::path::end(path);

// Iterate over the path components until we find something that ends with
// .app. If the next component is Contents then we've found the Contents
// directory.
for (auto it = begin; it != end; ++it) {
if (it->endswith(".app")) {
auto next = it;
if (++next != end && *next == "Contents") {
llvm::SmallString<128> buffer;
llvm::sys::path::append(buffer, begin, ++next,
llvm::sys::path::Style::posix);
return buffer.str().str();
}
}
}

return {};
}

FileSpec PlatformDarwin::GetXcodeContentsDirectory() {
static FileSpec g_xcode_contents_path;
static std::once_flag g_once_flag;
std::call_once(g_once_flag, [&]() {
// Try the shlib dir first.
if (FileSpec fspec = HostInfo::GetShlibDir()) {
if (FileSystem::Instance().Exists(fspec)) {
std::string xcode_contents_dir =
FindXcodeContentsDirectoryInPath(fspec.GetPath());
if (!xcode_contents_dir.empty()) {
g_xcode_contents_path = FileSpec(xcode_contents_dir);
return;
}
}
}

if (const char *developer_dir_env_var = getenv("DEVELOPER_DIR")) {
FileSpec fspec(developer_dir_env_var);
if (FileSystem::Instance().Exists(fspec)) {
std::string xcode_contents_dir =
FindXcodeContentsDirectoryInPath(fspec.GetPath());
if (!xcode_contents_dir.empty()) {
g_xcode_contents_path = FileSpec(xcode_contents_dir);
return;
}
}
}

FileSpec fspec(HostInfo::GetXcodeSDKPath(XcodeSDK::GetAnyMacOS()));
if (fspec) {
if (FileSystem::Instance().Exists(fspec)) {
std::string xcode_contents_dir =
FindXcodeContentsDirectoryInPath(fspec.GetPath());
if (!xcode_contents_dir.empty()) {
g_xcode_contents_path = FileSpec(xcode_contents_dir);
return;
}
}
}
});
return g_xcode_contents_path;
}

FileSpec PlatformDarwin::GetCurrentToolchainDirectory() {
if (FileSpec fspec = HostInfo::GetShlibDir())
return FileSpec(FindComponentInPath(fspec.GetPath(), ".xctoolchain"));
Expand Down
6 changes: 1 addition & 5 deletions lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,7 @@ class PlatformDarwin : public PlatformPOSIX {
llvm::Expected<lldb_private::StructuredData::DictionarySP>
FetchExtendedCrashInformation(lldb_private::Process &process) override;

static lldb_private::FileSpec GetXcodeContentsDirectory();
static lldb_private::FileSpec GetXcodeDeveloperDirectory();

/// Return the toolchain directroy the current LLDB instance is located in.
/// Return the toolchain directory the current LLDB instance is located in.
static lldb_private::FileSpec GetCurrentToolchainDirectory();

/// Return the command line tools directory the current LLDB instance is
Expand Down Expand Up @@ -166,7 +163,6 @@ class PlatformDarwin : public PlatformPOSIX {

static std::string FindComponentInPath(llvm::StringRef path,
llvm::StringRef component);
static std::string FindXcodeContentsDirectoryInPath(llvm::StringRef path);

std::string m_developer_directory;
llvm::StringMap<std::string> m_sdk_path;
Expand Down
3 changes: 2 additions & 1 deletion lldb/source/Plugins/Platform/MacOSX/PlatformDarwinKernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "lldb/Core/ModuleSpec.h"
#include "lldb/Core/PluginManager.h"
#include "lldb/Host/Host.h"
#include "lldb/Host/HostInfo.h"
#include "lldb/Interpreter/OptionValueFileSpecList.h"
#include "lldb/Interpreter/OptionValueProperties.h"
#include "lldb/Interpreter/Property.h"
Expand Down Expand Up @@ -328,7 +329,7 @@ void PlatformDarwinKernel::CollectKextAndKernelDirectories() {

// DeveloperDirectory is something like
// "/Applications/Xcode.app/Contents/Developer"
std::string developer_dir = GetXcodeDeveloperDirectory().GetPath();
std::string developer_dir = HostInfo::GetXcodeDeveloperDirectory().GetPath();
if (developer_dir.empty())
developer_dir = "/Applications/Xcode.app/Contents/Developer";

Expand Down
2 changes: 1 addition & 1 deletion lldb/source/Plugins/Platform/MacOSX/PlatformMacOSX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ ConstString PlatformMacOSX::GetSDKDirectory(lldb_private::Target &target) {
return {};

// First try to find an SDK that matches the given SDK version.
if (FileSpec fspec = GetXcodeContentsDirectory()) {
if (FileSpec fspec = HostInfo::GetXcodeContentsDirectory()) {
StreamString sdk_path;
sdk_path.Printf("%s/Developer/Platforms/MacOSX.platform/Developer/"
"SDKs/MacOSX%u.%u.sdk",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "lldb/Core/PluginManager.h"
#include "lldb/Host/FileSystem.h"
#include "lldb/Host/Host.h"
#include "lldb/Host/HostInfo.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/Target.h"
#include "lldb/Utility/FileSpec.h"
Expand Down Expand Up @@ -342,7 +343,7 @@ PlatformRemoteDarwinDevice::GetSDKDirectoryForLatestOSVersion() {
const char *PlatformRemoteDarwinDevice::GetDeviceSupportDirectory() {
std::string platform_dir = "/Platforms/" + GetPlatformName() + "/DeviceSupport";
if (m_device_support_directory.empty()) {
if (FileSpec fspec = GetXcodeDeveloperDirectory()) {
if (FileSpec fspec = HostInfo::GetXcodeDeveloperDirectory()) {
m_device_support_directory = fspec.GetPath();
m_device_support_directory.append(platform_dir.c_str());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ EnumerateDirectoryCallback(void *baton, llvm::sys::fs::file_type ft,
const char *PlatformiOSSimulator::GetSDKDirectoryAsCString() {
std::lock_guard<std::mutex> guard(m_sdk_dir_mutex);
if (m_sdk_directory.empty()) {
if (FileSpec fspec = GetXcodeDeveloperDirectory()) {
if (FileSpec fspec = HostInfo::GetXcodeDeveloperDirectory()) {
std::string developer_dir = fspec.GetPath();
char sdks_directory[PATH_MAX];
char sdk_dirname[PATH_MAX];
Expand Down
22 changes: 22 additions & 0 deletions lldb/source/Utility/XcodeSDK.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,25 @@ XcodeSDK::Type XcodeSDK::GetSDKTypeForTriple(const llvm::Triple &triple) {
return XcodeSDK::unknown;
}
}

std::string XcodeSDK::FindXcodeContentsDirectoryInPath(llvm::StringRef path) {
auto begin = llvm::sys::path::begin(path);
auto end = llvm::sys::path::end(path);

// Iterate over the path components until we find something that ends with
// .app. If the next component is Contents then we've found the Contents
// directory.
for (auto it = begin; it != end; ++it) {
if (it->endswith(".app")) {
auto next = it;
if (++next != end && *next == "Contents") {
llvm::SmallString<128> buffer;
llvm::sys::path::append(buffer, begin, ++next,
llvm::sys::path::Style::posix);
return buffer.str().str();
}
}
}

return {};
}
Loading