cmLocalGenerator: factor out creating a "safe" file path

This commit is contained in:
Ben Boeckel
2025-05-22 16:59:11 +02:00
parent 5374edc8bf
commit 03ed0ff57c
2 changed files with 24 additions and 14 deletions

View File

@@ -4056,6 +4056,27 @@ bool cmLocalGeneratorCheckObjectName(std::string& objName,
}
}
std::string cmLocalGenerator::CreateSafeObjectFileName(
std::string const& sin) const
{
// Start with the original name.
std::string ssin = sin;
// Avoid full paths by removing leading slashes.
ssin.erase(0, ssin.find_first_not_of('/'));
// Avoid full paths by removing colons.
std::replace(ssin.begin(), ssin.end(), ':', '_');
// Avoid relative paths that go up the tree.
cmSystemTools::ReplaceString(ssin, "../", "__/");
// Avoid spaces.
std::replace(ssin.begin(), ssin.end(), ' ', '_');
return ssin;
}
std::string& cmLocalGenerator::CreateSafeUniqueObjectFileName(
std::string const& sin, std::string const& dir_max)
{
@@ -4064,20 +4085,7 @@ std::string& cmLocalGenerator::CreateSafeUniqueObjectFileName(
// If no entry exists create one.
if (it == this->UniqueObjectNamesMap.end()) {
// Start with the original name.
std::string ssin = sin;
// Avoid full paths by removing leading slashes.
ssin.erase(0, ssin.find_first_not_of('/'));
// Avoid full paths by removing colons.
std::replace(ssin.begin(), ssin.end(), ':', '_');
// Avoid relative paths that go up the tree.
cmSystemTools::ReplaceString(ssin, "../", "__/");
// Avoid spaces.
std::replace(ssin.begin(), ssin.end(), ' ', '_');
auto ssin = this->CreateSafeObjectFileName(sin);
// Mangle the name if necessary.
if (this->Makefile->IsOn("CMAKE_MANGLE_OBJECT_FILE_NAMES")) {

View File

@@ -536,6 +536,8 @@ public:
// (If CMP0157 is NEW, we can do a split build)
bool IsSplitSwiftBuild() const;
std::string CreateSafeObjectFileName(std::string const& sin) const;
protected:
// The default implementation converts to a Windows shortpath to
// help older toolchains handle spaces and such. A generator may