Răsfoiți Sursa

cmObjectLocation: add a class to track object locations

With the feature to use a shorter path for the build tree, the install
tree may still want to use a longer path.
Ben Boeckel 5 luni în urmă
părinte
comite
757c40c478
4 a modificat fișierele cu 100 adăugiri și 0 ștergeri
  1. 2 0
      Source/CMakeLists.txt
  2. 47 0
      Source/cmObjectLocation.cxx
  3. 50 0
      Source/cmObjectLocation.h
  4. 1 0
      bootstrap

+ 2 - 0
Source/CMakeLists.txt

@@ -401,6 +401,8 @@ add_library(
   cmMSVC60LinkLineComputer.h
   cmOSXBundleGenerator.cxx
   cmOSXBundleGenerator.h
+  cmObjectLocation.cxx
+  cmObjectLocation.h
   cmOutputConverter.cxx
   cmOutputConverter.h
   cmNewLineStyle.h

+ 47 - 0
Source/cmObjectLocation.cxx

@@ -0,0 +1,47 @@
+/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
+   file LICENSE.rst or https://cmake.org/licensing for details.  */
+
+#include "cmObjectLocation.h"
+
+void cmObjectLocation::Update(std::string path)
+{
+  this->Path = std::move(path);
+}
+
+std::string const& cmObjectLocation::GetPath() const
+{
+  return this->Path;
+}
+
+cm::string_view cmObjectLocation::GetDirectory() const
+{
+  auto const pos = this->Path.rfind('/');
+  if (pos == std::string::npos) {
+    return {};
+  }
+  return cm::string_view(this->Path.c_str(), pos);
+}
+
+cm::string_view cmObjectLocation::GetName() const
+{
+  auto const pos = this->Path.rfind('/');
+  if (pos == std::string::npos) {
+    return this->Path;
+  }
+  auto const nameStart = pos + 1;
+  return cm::string_view(this->Path.c_str() + nameStart,
+                         this->Path.size() - nameStart);
+}
+
+cmObjectLocation const& cmObjectLocations::GetLocation(UseShortPath use) const
+{
+  if (use == UseShortPath::Yes && this->ShortLoc) {
+    return *this->ShortLoc;
+  }
+  return this->LongLoc;
+}
+
+std::string const& cmObjectLocations::GetPath(UseShortPath use) const
+{
+  return this->GetLocation(use).GetPath();
+}

+ 50 - 0
Source/cmObjectLocation.h

@@ -0,0 +1,50 @@
+/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
+   file LICENSE.rst or https://cmake.org/licensing for details.  */
+#pragma once
+
+#include "cmConfigure.h" // IWYU pragma: keep
+
+#include <string>
+#include <utility>
+
+#include <cm/optional>
+#include <cm/string_view>
+
+struct cmObjectLocation
+{
+public:
+  cmObjectLocation() = default;
+  cmObjectLocation(std::string path)
+    : Path(std::move(path))
+  {
+  }
+
+  void Update(std::string path);
+
+  // Get the path to the object under the common directory for the target's
+  // objects.
+  std::string const& GetPath() const;
+  // Get the directory of the object file under the common directory.
+  cm::string_view GetDirectory() const;
+  // Get the name of the object file.
+  cm::string_view GetName() const;
+
+private:
+  std::string Path;
+};
+
+struct cmObjectLocations
+{
+  cmObjectLocations() = default;
+
+  cm::optional<cmObjectLocation> ShortLoc;
+  cmObjectLocation LongLoc;
+
+  enum class UseShortPath
+  {
+    Yes,
+    No,
+  };
+  cmObjectLocation const& GetLocation(UseShortPath use) const;
+  std::string const& GetPath(UseShortPath use) const;
+};

+ 1 - 0
bootstrap

@@ -454,6 +454,7 @@ CMAKE_CXX_SOURCES="\
   cmOSXBundleGenerator \
   cmOptionCommand \
   cmOrderDirectories \
+  cmObjectLocation \
   cmOutputConverter \
   cmParseArgumentsCommand \
   cmPathLabel \