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.
This commit is contained in:
Ben Boeckel
2025-05-19 12:13:44 +02:00
parent 42c5200612
commit 757c40c478
4 changed files with 100 additions and 0 deletions

View File

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

View File

@@ -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
Source/cmObjectLocation.h Normal file
View File

@@ -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;
};

View File

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