From 757c40c47849cb20b6ee88bd166b3990fffc8dab Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Mon, 19 May 2025 12:13:44 +0200 Subject: [PATCH] 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. --- Source/CMakeLists.txt | 2 ++ Source/cmObjectLocation.cxx | 47 ++++++++++++++++++++++++++++++++++ Source/cmObjectLocation.h | 50 +++++++++++++++++++++++++++++++++++++ bootstrap | 1 + 4 files changed, 100 insertions(+) create mode 100644 Source/cmObjectLocation.cxx create mode 100644 Source/cmObjectLocation.h diff --git a/Source/CMakeLists.txt b/Source/CMakeLists.txt index a8f5fd1c7e..600c95ec32 100644 --- a/Source/CMakeLists.txt +++ b/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 diff --git a/Source/cmObjectLocation.cxx b/Source/cmObjectLocation.cxx new file mode 100644 index 0000000000..bb0c2ee9dd --- /dev/null +++ b/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(); +} diff --git a/Source/cmObjectLocation.h b/Source/cmObjectLocation.h new file mode 100644 index 0000000000..2cf2564635 --- /dev/null +++ b/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 +#include + +#include +#include + +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 ShortLoc; + cmObjectLocation LongLoc; + + enum class UseShortPath + { + Yes, + No, + }; + cmObjectLocation const& GetLocation(UseShortPath use) const; + std::string const& GetPath(UseShortPath use) const; +}; diff --git a/bootstrap b/bootstrap index d88a88db96..e354aa9bdd 100755 --- a/bootstrap +++ b/bootstrap @@ -454,6 +454,7 @@ CMAKE_CXX_SOURCES="\ cmOSXBundleGenerator \ cmOptionCommand \ cmOrderDirectories \ + cmObjectLocation \ cmOutputConverter \ cmParseArgumentsCommand \ cmPathLabel \