Skip to content

Commit 3d7b926

Browse files
committed
Move GetXcode*Directory into HostInfo (NFC)
These functions really don't belong into PlatformDarwin, since they actualy query state of the Host and not of the remote platform.
1 parent 45e1a22 commit 3d7b926

16 files changed

+136
-135
lines changed

lldb/include/lldb/Host/HostInfoBase.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ class HostInfoBase {
9292
static bool ComputePathRelativeToLibrary(FileSpec &file_spec,
9393
llvm::StringRef dir);
9494

95+
static FileSpec GetXcodeContentsDirectory() { return {}; }
96+
static FileSpec GetXcodeDeveloperDirectory() { return {}; }
97+
9598
/// Return the directory containing a specific Xcode SDK.
9699
static llvm::StringRef GetXcodeSDKPath(XcodeSDK sdk) { return {}; }
97100

lldb/include/lldb/Host/macosx/HostInfoMacOSX.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ class HostInfoMacOSX : public HostInfoPosix {
3232
static bool GetOSBuildString(std::string &s);
3333
static bool GetOSKernelDescription(std::string &s);
3434
static FileSpec GetProgramFileSpec();
35-
static std::string FindXcodeContentsDirectoryInPath(llvm::StringRef path);
35+
static FileSpec GetXcodeContentsDirectory();
36+
static FileSpec GetXcodeDeveloperDirectory();
3637

3738
/// Query xcrun to find an Xcode SDK directory.
3839
static llvm::StringRef GetXcodeSDKPath(XcodeSDK sdk);

lldb/include/lldb/Utility/XcodeSDK.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ class XcodeSDK {
8787
static std::string GetCanonicalName(Info info);
8888
/// Return the best-matching SDK type for a specific triple.
8989
static XcodeSDK::Type GetSDKTypeForTriple(const llvm::Triple &triple);
90+
91+
static std::string FindXcodeContentsDirectoryInPath(llvm::StringRef path);
9092
};
9193

9294
} // namespace lldb_private

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,63 @@ static void ParseOSVersion(llvm::VersionTuple &version, NSString *Key) {
297297
}
298298
}
299299

300+
FileSpec HostInfoMacOSX::GetXcodeContentsDirectory() {
301+
static FileSpec g_xcode_contents_path;
302+
static std::once_flag g_once_flag;
303+
std::call_once(g_once_flag, [&]() {
304+
// Try the shlib dir first.
305+
if (FileSpec fspec = HostInfo::GetShlibDir()) {
306+
if (FileSystem::Instance().Exists(fspec)) {
307+
std::string xcode_contents_dir =
308+
XcodeSDK::FindXcodeContentsDirectoryInPath(fspec.GetPath());
309+
if (!xcode_contents_dir.empty()) {
310+
g_xcode_contents_path = FileSpec(xcode_contents_dir);
311+
return;
312+
}
313+
}
314+
}
315+
316+
if (const char *developer_dir_env_var = getenv("DEVELOPER_DIR")) {
317+
FileSpec fspec(developer_dir_env_var);
318+
if (FileSystem::Instance().Exists(fspec)) {
319+
// FIXME: This looks like it couldn't possibly work!
320+
std::string xcode_contents_dir =
321+
XcodeSDK::FindXcodeContentsDirectoryInPath(fspec.GetPath());
322+
if (!xcode_contents_dir.empty()) {
323+
g_xcode_contents_path = FileSpec(xcode_contents_dir);
324+
return;
325+
}
326+
}
327+
}
328+
329+
FileSpec fspec(HostInfo::GetXcodeSDKPath(XcodeSDK::GetAnyMacOS()));
330+
if (fspec) {
331+
if (FileSystem::Instance().Exists(fspec)) {
332+
std::string xcode_contents_dir =
333+
XcodeSDK::FindXcodeContentsDirectoryInPath(fspec.GetPath());
334+
if (!xcode_contents_dir.empty()) {
335+
g_xcode_contents_path = FileSpec(xcode_contents_dir);
336+
return;
337+
}
338+
}
339+
}
340+
});
341+
return g_xcode_contents_path;
342+
}
343+
344+
lldb_private::FileSpec HostInfoMacOSX::GetXcodeDeveloperDirectory() {
345+
static lldb_private::FileSpec g_developer_directory;
346+
static llvm::once_flag g_once_flag;
347+
llvm::call_once(g_once_flag, []() {
348+
if (FileSpec fspec = GetXcodeContentsDirectory()) {
349+
fspec.AppendPathComponent("Developer");
350+
if (FileSystem::Instance().Exists(fspec))
351+
g_developer_directory = fspec;
352+
}
353+
});
354+
return g_developer_directory;
355+
}
356+
300357
static std::string GetXcodeSDK(XcodeSDK sdk) {
301358
XcodeSDK::Info info = sdk.Parse();
302359
std::string sdk_name = XcodeSDK::GetCanonicalName(info);

lldb/source/Plugins/Platform/MacOSX/PlatformAppleSimulator.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <mutex>
1616
#include <thread>
1717
#include "lldb/Host/PseudoTerminal.h"
18+
#include "lldb/Host/HostInfo.h"
1819
#include "lldb/Target/Process.h"
1920
#include "lldb/Utility/LLDBAssert.h"
2021
#include "lldb/Utility/Status.h"
@@ -77,7 +78,7 @@ void PlatformAppleSimulator::GetStatus(Stream &strm) {
7778
// simulator
7879
PlatformAppleSimulator::LoadCoreSimulator();
7980

80-
std::string developer_dir = GetXcodeDeveloperDirectory().GetPath();
81+
std::string developer_dir = HostInfo::GetXcodeDeveloperDirectory().GetPath();
8182
CoreSimulatorSupport::DeviceSet devices =
8283
CoreSimulatorSupport::DeviceSet::GetAvailableDevices(
8384
developer_dir.c_str());
@@ -124,7 +125,7 @@ Status PlatformAppleSimulator::ConnectRemote(Args &args) {
124125
const char *arg_cstr = args.GetArgumentAtIndex(0);
125126
if (arg_cstr) {
126127
std::string arg_str(arg_cstr);
127-
std::string developer_dir = GetXcodeDeveloperDirectory().GetPath();
128+
std::string developer_dir = HostInfo::GetXcodeDeveloperDirectory().GetPath();
128129
CoreSimulatorSupport::DeviceSet devices =
129130
CoreSimulatorSupport::DeviceSet::GetAvailableDevices(
130131
developer_dir.c_str());
@@ -214,7 +215,7 @@ FileSpec PlatformAppleSimulator::GetCoreSimulatorPath() {
214215
#if defined(__APPLE__)
215216
std::lock_guard<std::mutex> guard(m_core_sim_path_mutex);
216217
if (!m_core_simulator_framework_path.hasValue()) {
217-
if (FileSpec fspec = GetXcodeDeveloperDirectory()) {
218+
if (FileSpec fspec = HostInfo::GetXcodeDeveloperDirectory()) {
218219
std::string developer_dir = fspec.GetPath();
219220
StreamString cs_path;
220221
cs_path.Printf(
@@ -247,7 +248,7 @@ CoreSimulatorSupport::Device PlatformAppleSimulator::GetSimulatorDevice() {
247248
if (!m_device.hasValue()) {
248249
const CoreSimulatorSupport::DeviceType::ProductFamilyID dev_id =
249250
CoreSimulatorSupport::DeviceType::ProductFamilyID::iPhone;
250-
std::string developer_dir = GetXcodeDeveloperDirectory().GetPath();
251+
std::string developer_dir = HostInfo::GetXcodeDeveloperDirectory().GetPath();
251252
m_device = CoreSimulatorSupport::DeviceSet::GetAvailableDevices(
252253
developer_dir.c_str())
253254
.GetFanciest(dev_id);

lldb/source/Plugins/Platform/MacOSX/PlatformAppleTVSimulator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ EnumerateDirectoryCallback(void *baton, llvm::sys::fs::file_type ft,
255255
const char *PlatformAppleTVSimulator::GetSDKDirectoryAsCString() {
256256
std::lock_guard<std::mutex> guard(m_sdk_dir_mutex);
257257
if (m_sdk_directory.empty()) {
258-
if (FileSpec fspec = GetXcodeDeveloperDirectory()) {
258+
if (FileSpec fspec = HostInfo::GetXcodeDeveloperDirectory()) {
259259
std::string developer_dir = fspec.GetPath();
260260
char sdks_directory[PATH_MAX];
261261
char sdk_dirname[PATH_MAX];

lldb/source/Plugins/Platform/MacOSX/PlatformAppleWatchSimulator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ EnumerateDirectoryCallback(void *baton, llvm::sys::fs::file_type ft,
255255
const char *PlatformAppleWatchSimulator::GetSDKDirectoryAsCString() {
256256
std::lock_guard<std::mutex> guard(m_sdk_dir_mutex);
257257
if (m_sdk_directory.empty()) {
258-
if (FileSpec fspec = GetXcodeDeveloperDirectory()) {
258+
if (FileSpec fspec = HostInfo::GetXcodeDeveloperDirectory()) {
259259
std::string developer_dir = fspec.GetPath();
260260
char sdks_directory[PATH_MAX];
261261
char sdk_dirname[PATH_MAX];

lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp

Lines changed: 2 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,19 +1133,6 @@ static FileSpec GetXcodeSelectPath() {
11331133
return g_xcode_select_filespec;
11341134
}
11351135

1136-
lldb_private::FileSpec PlatformDarwin::GetXcodeDeveloperDirectory() {
1137-
static lldb_private::FileSpec g_developer_directory;
1138-
static llvm::once_flag g_once_flag;
1139-
llvm::call_once(g_once_flag, []() {
1140-
if (FileSpec fspec = GetXcodeContentsDirectory()) {
1141-
fspec.AppendPathComponent("Developer");
1142-
if (FileSystem::Instance().Exists(fspec))
1143-
g_developer_directory = fspec;
1144-
}
1145-
});
1146-
return g_developer_directory;
1147-
}
1148-
11491136
BreakpointSP PlatformDarwin::SetThreadCreationBreakpoint(Target &target) {
11501137
BreakpointSP bp_sp;
11511138
static const char *g_bp_names[] = {
@@ -1260,7 +1247,7 @@ FileSpec PlatformDarwin::FindSDKInXcodeForModules(XcodeSDK::Type sdk_type,
12601247
}
12611248

12621249
FileSpec PlatformDarwin::GetSDKDirectoryForModules(XcodeSDK::Type sdk_type) {
1263-
FileSpec sdks_spec = GetXcodeContentsDirectory();
1250+
FileSpec sdks_spec = HostInfo::GetXcodeContentsDirectory();
12641251
sdks_spec.AppendPathComponent("Developer");
12651252
sdks_spec.AppendPathComponent("Platforms");
12661253

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

15881575
// When locating executables, trust the DEVELOPER_DIR first if it is set
1589-
FileSpec xcode_contents_dir = GetXcodeContentsDirectory();
1576+
FileSpec xcode_contents_dir = HostInfo::GetXcodeContentsDirectory();
15901577
if (xcode_contents_dir) {
15911578
FileSpec xcode_lldb_resources = xcode_contents_dir;
15921579
xcode_lldb_resources.AppendPathComponent("SharedFrameworks");
@@ -1738,72 +1725,6 @@ std::string PlatformDarwin::FindComponentInPath(llvm::StringRef path,
17381725
return {};
17391726
}
17401727

1741-
std::string
1742-
PlatformDarwin::FindXcodeContentsDirectoryInPath(llvm::StringRef path) {
1743-
auto begin = llvm::sys::path::begin(path);
1744-
auto end = llvm::sys::path::end(path);
1745-
1746-
// Iterate over the path components until we find something that ends with
1747-
// .app. If the next component is Contents then we've found the Contents
1748-
// directory.
1749-
for (auto it = begin; it != end; ++it) {
1750-
if (it->endswith(".app")) {
1751-
auto next = it;
1752-
if (++next != end && *next == "Contents") {
1753-
llvm::SmallString<128> buffer;
1754-
llvm::sys::path::append(buffer, begin, ++next,
1755-
llvm::sys::path::Style::posix);
1756-
return buffer.str().str();
1757-
}
1758-
}
1759-
}
1760-
1761-
return {};
1762-
}
1763-
1764-
FileSpec PlatformDarwin::GetXcodeContentsDirectory() {
1765-
static FileSpec g_xcode_contents_path;
1766-
static std::once_flag g_once_flag;
1767-
std::call_once(g_once_flag, [&]() {
1768-
// Try the shlib dir first.
1769-
if (FileSpec fspec = HostInfo::GetShlibDir()) {
1770-
if (FileSystem::Instance().Exists(fspec)) {
1771-
std::string xcode_contents_dir =
1772-
FindXcodeContentsDirectoryInPath(fspec.GetPath());
1773-
if (!xcode_contents_dir.empty()) {
1774-
g_xcode_contents_path = FileSpec(xcode_contents_dir);
1775-
return;
1776-
}
1777-
}
1778-
}
1779-
1780-
if (const char *developer_dir_env_var = getenv("DEVELOPER_DIR")) {
1781-
FileSpec fspec(developer_dir_env_var);
1782-
if (FileSystem::Instance().Exists(fspec)) {
1783-
std::string xcode_contents_dir =
1784-
FindXcodeContentsDirectoryInPath(fspec.GetPath());
1785-
if (!xcode_contents_dir.empty()) {
1786-
g_xcode_contents_path = FileSpec(xcode_contents_dir);
1787-
return;
1788-
}
1789-
}
1790-
}
1791-
1792-
FileSpec fspec(HostInfo::GetXcodeSDKPath(XcodeSDK::GetAnyMacOS()));
1793-
if (fspec) {
1794-
if (FileSystem::Instance().Exists(fspec)) {
1795-
std::string xcode_contents_dir =
1796-
FindXcodeContentsDirectoryInPath(fspec.GetPath());
1797-
if (!xcode_contents_dir.empty()) {
1798-
g_xcode_contents_path = FileSpec(xcode_contents_dir);
1799-
return;
1800-
}
1801-
}
1802-
}
1803-
});
1804-
return g_xcode_contents_path;
1805-
}
1806-
18071728
FileSpec PlatformDarwin::GetCurrentToolchainDirectory() {
18081729
if (FileSpec fspec = HostInfo::GetShlibDir())
18091730
return FileSpec(FindComponentInPath(fspec.GetPath(), ".xctoolchain"));

lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,6 @@ class PlatformDarwin : public PlatformPOSIX {
8989
llvm::Expected<lldb_private::StructuredData::DictionarySP>
9090
FetchExtendedCrashInformation(lldb_private::Process &process) override;
9191

92-
static lldb_private::FileSpec GetXcodeContentsDirectory();
93-
static lldb_private::FileSpec GetXcodeDeveloperDirectory();
94-
9592
/// Return the toolchain directory the current LLDB instance is located in.
9693
static lldb_private::FileSpec GetCurrentToolchainDirectory();
9794

@@ -165,7 +162,6 @@ class PlatformDarwin : public PlatformPOSIX {
165162

166163
static std::string FindComponentInPath(llvm::StringRef path,
167164
llvm::StringRef component);
168-
static std::string FindXcodeContentsDirectoryInPath(llvm::StringRef path);
169165

170166
std::string m_developer_directory;
171167
llvm::StringMap<std::string> m_sdk_path;

lldb/source/Plugins/Platform/MacOSX/PlatformDarwinKernel.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "lldb/Core/ModuleSpec.h"
1818
#include "lldb/Core/PluginManager.h"
1919
#include "lldb/Host/Host.h"
20+
#include "lldb/Host/HostInfo.h"
2021
#include "lldb/Interpreter/OptionValueFileSpecList.h"
2122
#include "lldb/Interpreter/OptionValueProperties.h"
2223
#include "lldb/Interpreter/Property.h"
@@ -327,7 +328,7 @@ void PlatformDarwinKernel::CollectKextAndKernelDirectories() {
327328

328329
// DeveloperDirectory is something like
329330
// "/Applications/Xcode.app/Contents/Developer"
330-
std::string developer_dir = GetXcodeDeveloperDirectory().GetPath();
331+
std::string developer_dir = HostInfo::GetXcodeDeveloperDirectory().GetPath();
331332
if (developer_dir.empty())
332333
developer_dir = "/Applications/Xcode.app/Contents/Developer";
333334

lldb/source/Plugins/Platform/MacOSX/PlatformMacOSX.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ ConstString PlatformMacOSX::GetSDKDirectory(lldb_private::Target &target) {
197197
return {};
198198

199199
// First try to find an SDK that matches the given SDK version.
200-
if (FileSpec fspec = GetXcodeContentsDirectory()) {
200+
if (FileSpec fspec = HostInfo::GetXcodeContentsDirectory()) {
201201
StreamString sdk_path;
202202
sdk_path.Printf("%s/Developer/Platforms/MacOSX.platform/Developer/"
203203
"SDKs/MacOSX%u.%u.sdk",

lldb/source/Plugins/Platform/MacOSX/PlatformRemoteDarwinDevice.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "lldb/Core/PluginManager.h"
1616
#include "lldb/Host/FileSystem.h"
1717
#include "lldb/Host/Host.h"
18+
#include "lldb/Host/HostInfo.h"
1819
#include "lldb/Target/Process.h"
1920
#include "lldb/Target/Target.h"
2021
#include "lldb/Utility/FileSpec.h"
@@ -342,7 +343,7 @@ PlatformRemoteDarwinDevice::GetSDKDirectoryForLatestOSVersion() {
342343
const char *PlatformRemoteDarwinDevice::GetDeviceSupportDirectory() {
343344
std::string platform_dir = "/Platforms/" + GetPlatformName() + "/DeviceSupport";
344345
if (m_device_support_directory.empty()) {
345-
if (FileSpec fspec = GetXcodeDeveloperDirectory()) {
346+
if (FileSpec fspec = HostInfo::GetXcodeDeveloperDirectory()) {
346347
m_device_support_directory = fspec.GetPath();
347348
m_device_support_directory.append(platform_dir.c_str());
348349
} else {

lldb/source/Plugins/Platform/MacOSX/PlatformiOSSimulator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ EnumerateDirectoryCallback(void *baton, llvm::sys::fs::file_type ft,
260260
const char *PlatformiOSSimulator::GetSDKDirectoryAsCString() {
261261
std::lock_guard<std::mutex> guard(m_sdk_dir_mutex);
262262
if (m_sdk_directory.empty()) {
263-
if (FileSpec fspec = GetXcodeDeveloperDirectory()) {
263+
if (FileSpec fspec = HostInfo::GetXcodeDeveloperDirectory()) {
264264
std::string developer_dir = fspec.GetPath();
265265
char sdks_directory[PATH_MAX];
266266
char sdk_dirname[PATH_MAX];

lldb/source/Utility/XcodeSDK.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,25 @@ XcodeSDK::Type XcodeSDK::GetSDKTypeForTriple(const llvm::Triple &triple) {
285285
return XcodeSDK::unknown;
286286
}
287287
}
288+
289+
std::string XcodeSDK::FindXcodeContentsDirectoryInPath(llvm::StringRef path) {
290+
auto begin = llvm::sys::path::begin(path);
291+
auto end = llvm::sys::path::end(path);
292+
293+
// Iterate over the path components until we find something that ends with
294+
// .app. If the next component is Contents then we've found the Contents
295+
// directory.
296+
for (auto it = begin; it != end; ++it) {
297+
if (it->endswith(".app")) {
298+
auto next = it;
299+
if (++next != end && *next == "Contents") {
300+
llvm::SmallString<128> buffer;
301+
llvm::sys::path::append(buffer, begin, ++next,
302+
llvm::sys::path::Style::posix);
303+
return buffer.str().str();
304+
}
305+
}
306+
}
307+
308+
return {};
309+
}

0 commit comments

Comments
 (0)