[Lldb-commits] [lldb] 5ffb30f - [lldb/PlatformDarwin] Expose current toolchain and CL tools directory

Jonas Devlieghere via lldb-commits lldb-commits at lists.llvm.org
Wed Mar 18 15:08:55 PDT 2020


Author: Jonas Devlieghere
Date: 2020-03-18T15:08:24-07:00
New Revision: 5ffb30fd6c7faff314b01ef0dc75f2683ca85cdf

URL: https://github.com/llvm/llvm-project/commit/5ffb30fd6c7faff314b01ef0dc75f2683ca85cdf
DIFF: https://github.com/llvm/llvm-project/commit/5ffb30fd6c7faff314b01ef0dc75f2683ca85cdf.diff

LOG: [lldb/PlatformDarwin] Expose current toolchain and CL tools directory

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.

Added: 
    

Modified: 
    lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
    lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.h
    lldb/unittests/Platform/PlatformDarwinTest.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp b/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
index aa1f8994ecb6..46dd3774e5a9 100644
--- a/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
+++ b/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
@@ -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);
@@ -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 {};
+}

diff  --git a/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.h b/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.h
index d385712db8e6..6d51edbc9294 100644
--- a/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.h
+++ b/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.h
@@ -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
@@ -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;

diff  --git a/lldb/unittests/Platform/PlatformDarwinTest.cpp b/lldb/unittests/Platform/PlatformDarwinTest.cpp
index 06287c63227b..20916f3cd125 100644
--- a/lldb/unittests/Platform/PlatformDarwinTest.cpp
+++ b/lldb/unittests/Platform/PlatformDarwinTest.cpp
@@ -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) {
@@ -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"));
+}


        


More information about the lldb-commits mailing list