Refactor file extension queries to be more consistent

It was very easy to forgot to check against all language file
extensions. This updates the internal API to have a unified API.
This commit is contained in:
Robert Maynard
2020-06-15 09:23:49 -04:00
parent 6f7853cb42
commit 7628153edb
10 changed files with 53 additions and 61 deletions

View File

@@ -55,7 +55,7 @@ bool cmAuxSourceDirectoryCommand(std::vector<std::string> const& args,
auto ext = cm::string_view(file).substr(dotpos + 1);
// Process only source files
auto cm = mf.GetCMakeInstance();
if (dotpos > 0 && cm->IsSourceExtension(ext)) {
if (dotpos > 0 && cm->IsACLikeSourceExtension(ext)) {
std::string fullname = cmStrCat(templateDirectory, '/', file);
// add the file as a class file so
// depends can be done

View File

@@ -370,7 +370,7 @@ void cmExtraCodeBlocksGenerator::CreateNewProjectFile(
std::string lang = s->GetOrDetermineLanguage();
if (lang == "C" || lang == "CXX" || lang == "CUDA") {
std::string const& srcext = s->GetExtension();
isCFile = cm->IsSourceExtension(srcext);
isCFile = cm->IsACLikeSourceExtension(srcext);
}
std::string const& fullPath = s->ResolveFullPath();

View File

@@ -227,8 +227,7 @@ std::string cmExtraCodeLiteGenerator::CollectSourceFiles(
cmSystemTools::LowerCase(s->GetExtension());
// check whether it is a source or a include file
// then put it accordingly into one of the two containers
if (cm->IsSourceExtension(extLower) || cm->IsCudaExtension(extLower) ||
cm->IsFortranExtension(extLower)) {
if (cm->IsAKnownSourceExtension(extLower)) {
cFiles[fullPath] = s;
} else {
otherFiles.insert(fullPath);

View File

@@ -789,9 +789,9 @@ bool cmQtAutoGenInitializer::InitScanFiles()
// Register files that will be scanned by moc or uic
if (this->MocOrUicEnabled()) {
if (cm->IsHeaderExtension(extLower)) {
if (cm->IsAHeaderExtension(extLower)) {
addMUHeader(makeMUFile(sf, fullPath, true), extLower);
} else if (cm->IsSourceExtension(extLower)) {
} else if (cm->IsACLikeSourceExtension(extLower)) {
addMUSource(makeMUFile(sf, fullPath, true));
}
}
@@ -895,14 +895,14 @@ bool cmQtAutoGenInitializer::InitScanFiles()
std::string const& extLower =
cmSystemTools::LowerCase(sf->GetExtension());
if (cm->IsHeaderExtension(extLower)) {
if (cm->IsAHeaderExtension(extLower)) {
if (!cm::contains(this->AutogenTarget.Headers, sf.get())) {
auto muf = makeMUFile(sf.get(), fullPath, false);
if (muf->SkipMoc || muf->SkipUic) {
addMUHeader(std::move(muf), extLower);
}
}
} else if (cm->IsSourceExtension(extLower)) {
} else if (cm->IsACLikeSourceExtension(extLower)) {
if (!cm::contains(this->AutogenTarget.Sources, sf.get())) {
auto muf = makeMUFile(sf.get(), fullPath, false);
if (muf->SkipMoc || muf->SkipUic) {

View File

@@ -2,7 +2,6 @@
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmSourceFile.h"
#include <array>
#include <utility>
#include "cmGlobalGenerator.h"
@@ -130,13 +129,11 @@ bool cmSourceFile::FindFullPath(std::string* error)
// Location path
std::string const& lPath = this->Location.GetFullPath();
// List of extension lists
std::array<std::vector<std::string> const*, 2> const extsLists = {
{ &makefile->GetCMakeInstance()->GetSourceExtensions(),
&makefile->GetCMakeInstance()->GetHeaderExtensions() }
};
std::vector<std::string> exts =
makefile->GetCMakeInstance()->GetAllExtensions();
// Tries to find the file in a given directory
auto findInDir = [this, &extsLists, &lPath](std::string const& dir) -> bool {
auto findInDir = [this, &exts, &lPath](std::string const& dir) -> bool {
// Compute full path
std::string const fullPath = cmSystemTools::CollapseFullPath(lPath, dir);
// Try full path
@@ -145,14 +142,12 @@ bool cmSourceFile::FindFullPath(std::string* error)
return true;
}
// Try full path with extension
for (auto& exts : extsLists) {
for (std::string const& ext : *exts) {
if (!ext.empty()) {
std::string extPath = cmStrCat(fullPath, '.', ext);
if (cmSystemTools::FileExists(extPath)) {
this->FullPath = extPath;
return true;
}
for (std::string const& ext : exts) {
if (!ext.empty()) {
std::string extPath = cmStrCat(fullPath, '.', ext);
if (cmSystemTools::FileExists(extPath)) {
this->FullPath = extPath;
return true;
}
}
}
@@ -175,11 +170,9 @@ bool cmSourceFile::FindFullPath(std::string* error)
// Compose error
std::string err =
cmStrCat("Cannot find source file:\n ", lPath, "\nTried extensions");
for (auto exts : extsLists) {
for (std::string const& ext : *exts) {
err += " .";
err += ext;
}
for (std::string const& ext : exts) {
err += " .";
err += ext;
}
if (error != nullptr) {
*error = std::move(err);

View File

@@ -101,7 +101,7 @@ void cmSourceFileLocation::UpdateExtension(const std::string& name)
cmMakefile const* mf = this->Makefile;
auto cm = mf->GetCMakeInstance();
if (!gg->GetLanguageFromExtension(ext.c_str()).empty() ||
cm->IsSourceExtension(ext) || cm->IsHeaderExtension(ext)) {
cm->IsAKnownExtension(ext)) {
// This is a known extension. Use the given filename with extension.
this->Name = cmSystemTools::GetFilenameName(name);
this->AmbiguousExtension = false;
@@ -157,7 +157,7 @@ bool cmSourceFileLocation::MatchesAmbiguousExtension(
auto ext = cm::string_view(this->Name).substr(loc.Name.size() + 1);
cmMakefile const* mf = this->Makefile;
auto cm = mf->GetCMakeInstance();
return cm->IsSourceExtension(ext) || cm->IsHeaderExtension(ext);
return cm->IsAKnownExtension(ext);
}
bool cmSourceFileLocation::Matches(cmSourceFileLocation const& loc)

View File

@@ -194,7 +194,7 @@ cmake::cmake(Role role, cmState::Mode mode)
};
// The "c" extension MUST precede the "C" extension.
setupExts(this->SourceFileExtensions,
setupExts(this->CLikeSourceFileExtensions,
{ "c", "C", "c++", "cc", "cpp", "cxx", "cu", "m", "M", "mm" });
setupExts(this->HeaderFileExtensions,
{ "h", "hh", "h++", "hm", "hpp", "hxx", "in", "txx" });
@@ -1959,6 +1959,17 @@ void cmake::AddGlobCacheEntry(bool recurse, bool listDirectories,
backtrace);
}
std::vector<std::string> cmake::GetAllExtensions() const
{
std::vector<std::string> allExt = this->CLikeSourceFileExtensions.ordered;
allExt.insert(allExt.end(), this->HeaderFileExtensions.ordered.begin(),
this->HeaderFileExtensions.ordered.end());
// cuda extensions are also in SourceFileExtensions so we ignore it here
allExt.insert(allExt.end(), this->FortranFileExtensions.ordered.begin(),
this->FortranFileExtensions.ordered.end());
return allExt;
}
std::string cmake::StripExtension(const std::string& file) const
{
auto dotpos = file.rfind('.');
@@ -1968,7 +1979,7 @@ std::string cmake::StripExtension(const std::string& file) const
#else
auto ext = cm::string_view(file).substr(dotpos + 1);
#endif
if (this->IsSourceExtension(ext) || this->IsHeaderExtension(ext)) {
if (this->IsAKnownExtension(ext)) {
return file.substr(0, dotpos);
}
}

View File

@@ -264,46 +264,35 @@ public:
this->GeneratorToolsetSet = true;
}
const std::vector<std::string>& GetSourceExtensions() const
bool IsAKnownSourceExtension(cm::string_view ext) const
{
return this->SourceFileExtensions.ordered;
return this->CLikeSourceFileExtensions.Test(ext) ||
this->CudaFileExtensions.Test(ext) ||
this->FortranFileExtensions.Test(ext);
}
bool IsSourceExtension(cm::string_view ext) const
bool IsACLikeSourceExtension(cm::string_view ext) const
{
return this->SourceFileExtensions.Test(ext);
return this->CLikeSourceFileExtensions.Test(ext);
}
bool IsAKnownExtension(cm::string_view ext) const
{
return this->IsAKnownSourceExtension(ext) || this->IsAHeaderExtension(ext);
}
std::vector<std::string> GetAllExtensions() const;
const std::vector<std::string>& GetHeaderExtensions() const
{
return this->HeaderFileExtensions.ordered;
}
bool IsHeaderExtension(cm::string_view ext) const
bool IsAHeaderExtension(cm::string_view ext) const
{
return this->HeaderFileExtensions.Test(ext);
}
const std::vector<std::string>& GetCudaExtensions() const
{
return this->CudaFileExtensions.ordered;
}
bool IsCudaExtension(cm::string_view ext) const
{
return this->CudaFileExtensions.Test(ext);
}
const std::vector<std::string>& GetFortranExtensions() const
{
return this->FortranFileExtensions.ordered;
}
bool IsFortranExtension(cm::string_view ext) const
{
return this->FortranFileExtensions.Test(ext);
}
// Strips the extension (if present and known) from a filename
std::string StripExtension(const std::string& file) const;
@@ -628,7 +617,7 @@ private:
std::string CheckStampList;
std::string VSSolutionFile;
std::string EnvironmentGenerator;
FileExtensions SourceFileExtensions;
FileExtensions CLikeSourceFileExtensions;
FileExtensions HeaderFileExtensions;
FileExtensions CudaFileExtensions;
FileExtensions FortranFileExtensions;

View File

@@ -3,7 +3,7 @@ CMake Error at MissingSource.cmake:1 \(add_library\):
missing.c
Tried extensions( \.[A-Za-z+]+|
)*
Tried extensions \.c \.C .*
.*
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)

View File

@@ -3,7 +3,7 @@ CMake Error at global-interface.cmake:2 \(add_library\):
GLOBAL
Tried extensions( \.[A-Za-z+]+|
)*
Tried extensions \.c \.C .*
.*
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)