Refactoring: enhance include file filtering

In preparation of support of 'CMAKE_DEPENDS_IN_PROJECT_ONLY'
when dependencies are generated by the compiler.
This commit is contained in:
Marc Chevrier
2020-11-23 11:51:11 +01:00
parent 3401403f69
commit b6068ce407
+14 -24
View File
@@ -9,12 +9,14 @@
#include <utility> #include <utility>
#include <cm/memory> #include <cm/memory>
#include <cm/string_view>
#include <cm/vector> #include <cm/vector>
#include <cmext/algorithm> #include <cmext/algorithm>
#include "cmsys/FStream.hxx" #include "cmsys/FStream.hxx"
#include "cmsys/Terminal.h" #include "cmsys/Terminal.h"
#include "cmCMakePath.h"
#include "cmCustomCommand.h" // IWYU pragma: keep #include "cmCustomCommand.h" // IWYU pragma: keep
#include "cmCustomCommandGenerator.h" #include "cmCustomCommandGenerator.h"
#include "cmFileTimeCache.h" #include "cmFileTimeCache.h"
@@ -1742,44 +1744,32 @@ class NotInProjectDir
{ {
public: public:
// Constructor with the source and binary directory's path // Constructor with the source and binary directory's path
NotInProjectDir(std::string sourceDir, std::string binaryDir) NotInProjectDir(cm::string_view sourceDir, cm::string_view binaryDir)
: SourceDir(std::move(sourceDir)) : SourceDir(sourceDir)
, BinaryDir(std::move(binaryDir)) , BinaryDir(binaryDir)
{ {
} }
// Operator evaluating the predicate // Operator evaluating the predicate
bool operator()(const std::string& path) const bool operator()(const std::string& p) const
{ {
auto path = cmCMakePath(p).Normal();
// Keep all relative paths: // Keep all relative paths:
if (!cmSystemTools::FileIsFullPath(path)) { if (path.IsRelative()) {
return false; return false;
} }
// If it's an absolute path, check if it starts with the source // If it's an absolute path, check if it starts with the source
// directory: // directory:
return ( return !(cmCMakePath(SourceDir).IsPrefix(path) ||
!(IsInDirectory(SourceDir, path) || IsInDirectory(BinaryDir, path))); cmCMakePath(BinaryDir).IsPrefix(path));
} }
private: private:
// Helper function used by the predicate
static bool IsInDirectory(const std::string& baseDir,
const std::string& testDir)
{
// First check if the test directory "starts with" the base directory:
if (!cmHasPrefix(testDir, baseDir)) {
return false;
}
// If it does, then check that it's either the same string, or that the
// next character is a slash:
return ((testDir.size() == baseDir.size()) ||
(testDir[baseDir.size()] == '/'));
}
// The path to the source directory // The path to the source directory
std::string SourceDir; cm::string_view SourceDir;
// The path to the binary directory // The path to the binary directory
std::string BinaryDir; cm::string_view BinaryDir;
}; };
} }
@@ -1862,7 +1852,7 @@ void cmLocalUnixMakefileGenerator3::WriteDependLanguageInfo(
this->GetIncludeDirectories(includes, target, implicitLang.first, this->GetIncludeDirectories(includes, target, implicitLang.first,
this->GetConfigName()); this->GetConfigName());
std::string binaryDir = this->GetState()->GetBinaryDirectory(); std::string const& binaryDir = this->GetState()->GetBinaryDirectory();
if (this->Makefile->IsOn("CMAKE_DEPENDS_IN_PROJECT_ONLY")) { if (this->Makefile->IsOn("CMAKE_DEPENDS_IN_PROJECT_ONLY")) {
std::string const& sourceDir = this->GetState()->GetSourceDirectory(); std::string const& sourceDir = this->GetState()->GetSourceDirectory();
cm::erase_if(includes, ::NotInProjectDir(sourceDir, binaryDir)); cm::erase_if(includes, ::NotInProjectDir(sourceDir, binaryDir));