Skip to content

Commit

Permalink
[lldb/PlatformDarwin] Expose current toolchain and CL tools directory
Browse files Browse the repository at this point in the history
Expose two methods to find the current toolchain and the current command
line tools directory. These are used by Swift to find the resource
directory.
  • Loading branch information
JDevlieghere committed Mar 18, 2020
1 parent 1497066 commit 5ffb30f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
Expand Up @@ -1829,6 +1829,21 @@ lldb_private::Status PlatformDarwin::FindBundleBinaryInExecSearchPaths(
return Status();
}

std::string PlatformDarwin::FindComponentInPath(llvm::StringRef path,
llvm::StringRef component) {
auto begin = llvm::sys::path::begin(path);
auto end = llvm::sys::path::end(path);
for (auto it = begin; it != end; ++it) {
if (it->contains(component)) {
llvm::SmallString<128> buffer;
llvm::sys::path::append(buffer, begin, ++it,
llvm::sys::path::Style::posix);
return buffer.str().str();
}
}
return {};
}

std::string
PlatformDarwin::FindXcodeContentsDirectoryInPath(llvm::StringRef path) {
auto begin = llvm::sys::path::begin(path);
Expand Down Expand Up @@ -1959,3 +1974,15 @@ FileSpec PlatformDarwin::GetXcodeContentsDirectory() {
});
return g_xcode_contents_path;
}

FileSpec PlatformDarwin::GetCurrentToolchainDirectory() {
if (FileSpec fspec = HostInfo::GetShlibDir())
return FileSpec(FindComponentInPath(fspec.GetPath(), ".xctoolchain"));
return {};
}

FileSpec PlatformDarwin::GetCurrentCommandLineToolsDirectory() {
if (FileSpec fspec = HostInfo::GetShlibDir())
return FileSpec(FindComponentInPath(fspec.GetPath(), "CommandLineTools"));
return {};
}
9 changes: 9 additions & 0 deletions lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.h
Expand Up @@ -100,6 +100,13 @@ class PlatformDarwin : public PlatformPOSIX {
static lldb_private::FileSpec GetXcodeSDK(SDKType type);
static lldb_private::FileSpec GetXcodeContentsDirectory();

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

/// Return the command line tools directory the current LLDB instance is
/// located in.
static lldb_private::FileSpec GetCurrentCommandLineToolsDirectory();

protected:
struct CrashInfoAnnotations {
uint64_t version; // unsigned long
Expand Down Expand Up @@ -172,6 +179,8 @@ class PlatformDarwin : public PlatformPOSIX {
const lldb_private::FileSpecList *module_search_paths_ptr,
lldb::ModuleSP *old_module_sp_ptr, bool *did_create_ptr);

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

std::string m_developer_directory;
Expand Down
18 changes: 18 additions & 0 deletions lldb/unittests/Platform/PlatformDarwinTest.cpp
Expand Up @@ -19,6 +19,7 @@ using namespace lldb_private;

struct PlatformDarwinTester : public PlatformDarwin {
public:
using PlatformDarwin::FindComponentInPath;
using PlatformDarwin::FindXcodeContentsDirectoryInPath;
static bool SDKSupportsModules(SDKType desired_type,
const lldb_private::FileSpec &sdk_path) {
Expand Down Expand Up @@ -132,3 +133,20 @@ TEST(PlatformDarwinTest, GetSDKNameForType) {
EXPECT_EQ(
"", PlatformDarwin::GetSDKNameForType(PlatformDarwin::SDKType::unknown));
}

TEST(PlatformDarwinTest, FindComponentInPath) {
EXPECT_EQ("/path/to/foo",
PlatformDarwinTester::FindComponentInPath("/path/to/foo/", "foo"));

EXPECT_EQ("/path/to/foo",
PlatformDarwinTester::FindComponentInPath("/path/to/foo", "foo"));

EXPECT_EQ("/path/to/foobar", PlatformDarwinTester::FindComponentInPath(
"/path/to/foobar", "foo"));

EXPECT_EQ("/path/to/foobar", PlatformDarwinTester::FindComponentInPath(
"/path/to/foobar", "bar"));

EXPECT_EQ("",
PlatformDarwinTester::FindComponentInPath("/path/to/foo", "bar"));
}

0 comments on commit 5ffb30f

Please sign in to comment.