cmake/Source/cmFilePathChecksum.cxx

87 lines
3.2 KiB
C++
Raw Normal View History

2017-04-14 19:02:05 +02:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmFilePathChecksum.h"
2020-02-01 23:06:01 +01:00
#include <vector>
2017-04-14 19:02:05 +02:00
#include "cmBase32.h"
#include "cmCryptoHash.h"
#include "cmMakefile.h"
#include "cmSystemTools.h"
2019-11-11 23:01:05 +01:00
cmFilePathChecksum::cmFilePathChecksum() = default;
2017-04-14 19:02:05 +02:00
2018-01-26 17:06:56 +01:00
cmFilePathChecksum::cmFilePathChecksum(std::string const& currentSrcDir,
std::string const& currentBinDir,
std::string const& projectSrcDir,
std::string const& projectBinDir)
2017-04-14 19:02:05 +02:00
{
2021-09-14 00:13:48 +02:00
this->setupParentDirs(currentSrcDir, currentBinDir, projectSrcDir,
projectBinDir);
2017-04-14 19:02:05 +02:00
}
cmFilePathChecksum::cmFilePathChecksum(cmMakefile* makefile)
{
2021-09-14 00:13:48 +02:00
this->setupParentDirs(makefile->GetCurrentSourceDirectory(),
makefile->GetCurrentBinaryDirectory(),
makefile->GetHomeDirectory(),
makefile->GetHomeOutputDirectory());
2017-04-14 19:02:05 +02:00
}
2018-01-26 17:06:56 +01:00
void cmFilePathChecksum::setupParentDirs(std::string const& currentSrcDir,
std::string const& currentBinDir,
std::string const& projectSrcDir,
std::string const& projectBinDir)
2017-04-14 19:02:05 +02:00
{
2018-04-23 21:13:27 +02:00
this->parentDirs[0].first = cmSystemTools::GetRealPath(currentSrcDir);
this->parentDirs[1].first = cmSystemTools::GetRealPath(currentBinDir);
this->parentDirs[2].first = cmSystemTools::GetRealPath(projectSrcDir);
this->parentDirs[3].first = cmSystemTools::GetRealPath(projectBinDir);
2017-04-14 19:02:05 +02:00
2018-01-26 17:06:56 +01:00
this->parentDirs[0].second = "CurrentSource";
this->parentDirs[1].second = "CurrentBinary";
this->parentDirs[2].second = "ProjectSource";
this->parentDirs[3].second = "ProjectBinary";
2017-04-14 19:02:05 +02:00
}
2018-01-26 17:06:56 +01:00
std::string cmFilePathChecksum::get(std::string const& filePath) const
2017-04-14 19:02:05 +02:00
{
std::string relPath;
std::string relSeed;
{
2018-04-23 21:13:27 +02:00
std::string const fileReal = cmSystemTools::GetRealPath(filePath);
2017-04-14 19:02:05 +02:00
std::string parentDir;
// Find closest project parent directory
2018-01-26 17:06:56 +01:00
for (auto const& pDir : this->parentDirs) {
if (!pDir.first.empty() &&
cmsys::SystemTools::IsSubDirectory(fileReal, pDir.first)) {
parentDir = pDir.first;
relSeed = pDir.second;
2017-04-14 19:02:05 +02:00
break;
}
}
// Use file system root as fallback parent directory
if (parentDir.empty()) {
relSeed = "FileSystemRoot";
cmsys::SystemTools::SplitPathRootComponent(fileReal, &parentDir);
}
// Calculate relative path from project parent directory
relPath = cmsys::SystemTools::RelativePath(
parentDir, cmsys::SystemTools::GetParentDirectory(fileReal));
}
// Calculate the file ( seed + relative path ) binary checksum
std::vector<unsigned char> hashBytes =
cmCryptoHash(cmCryptoHash::AlgoSHA256).ByteHashString(relSeed + relPath);
// Convert binary checksum to string
2019-11-11 23:01:05 +01:00
return cmBase32Encoder().encodeString(hashBytes.data(), hashBytes.size(),
2017-04-14 19:02:05 +02:00
false);
}
2018-01-26 17:06:56 +01:00
std::string cmFilePathChecksum::getPart(std::string const& filePath,
2017-07-20 19:35:53 +02:00
size_t length) const
2017-04-14 19:02:05 +02:00
{
2021-09-14 00:13:48 +02:00
return this->get(filePath).substr(0, length);
2017-04-14 19:02:05 +02:00
}