cmake: Refactor file extension list setup

Refactor the file extention list setup in cmake.h/cxx and add file extensions
lists for Cuda and Fortran.
This commit is contained in:
Sebastian Holtermann
2019-07-04 11:22:20 +02:00
parent 8214ad442f
commit e50fa44a35
2 changed files with 68 additions and 44 deletions
+29 -34
View File
@@ -104,7 +104,6 @@
#include <cstring> #include <cstring>
#include <initializer_list> #include <initializer_list>
#include <iostream> #include <iostream>
#include <iterator>
#include <memory> // IWYU pragma: keep #include <memory> // IWYU pragma: keep
#include <sstream> #include <sstream>
#include <stdio.h> #include <stdio.h>
@@ -122,9 +121,9 @@ typedef std::unordered_map<std::string, Json::Value> JsonValueMapType;
static bool cmakeCheckStampFile(const std::string& stampName); static bool cmakeCheckStampFile(const std::string& stampName);
static bool cmakeCheckStampList(const std::string& stampList); static bool cmakeCheckStampList(const std::string& stampList);
void cmWarnUnusedCliWarning(const std::string& variable, int /*unused*/, static void cmWarnUnusedCliWarning(const std::string& variable, int /*unused*/,
void* ctx, const char* /*unused*/, void* ctx, const char* /*unused*/,
const cmMakefile* /*unused*/) const cmMakefile* /*unused*/)
{ {
cmake* cm = reinterpret_cast<cmake*>(ctx); cmake* cm = reinterpret_cast<cmake*>(ctx);
cm->MarkCliAsUsed(variable); cm->MarkCliAsUsed(variable);
@@ -184,40 +183,36 @@ cmake::cmake(Role role, cmState::Mode mode)
// Make sure we can capture the build tool output. // Make sure we can capture the build tool output.
cmSystemTools::EnableVSConsoleOutput(); cmSystemTools::EnableVSConsoleOutput();
// Set up a list of source and header extensions // Set up a list of source and header extensions.
// these are used to find files when the extension // These are used to find files when the extension is not given.
// is not given {
// The "c" extension MUST precede the "C" extension. auto fillExts = [](FileExtensions& exts,
this->SourceFileExtensions.emplace_back("c"); std::initializer_list<const char*> extList) {
this->SourceFileExtensions.emplace_back("C"); // Fill ordered vector
exts.ordered.reserve(extList.size());
for (const char* ext : extList) {
exts.ordered.emplace_back(ext);
};
// Fill unordered set
exts.unordered.insert(exts.ordered.begin(), exts.ordered.end());
};
this->SourceFileExtensions.emplace_back("c++"); // Source extensions
this->SourceFileExtensions.emplace_back("cc"); // The "c" extension MUST precede the "C" extension.
this->SourceFileExtensions.emplace_back("cpp"); fillExts(this->SourceFileExtensions,
this->SourceFileExtensions.emplace_back("cxx"); { "c", "C", "c++", "cc", "cpp", "cxx", "cu", "m", "M", "mm" });
this->SourceFileExtensions.emplace_back("cu");
this->SourceFileExtensions.emplace_back("m");
this->SourceFileExtensions.emplace_back("M");
this->SourceFileExtensions.emplace_back("mm");
std::copy(this->SourceFileExtensions.begin(), // Header extensions
this->SourceFileExtensions.end(), fillExts(this->HeaderFileExtensions,
std::inserter(this->SourceFileExtensionsSet, { "h", "hh", "h++", "hm", "hpp", "hxx", "in", "txx" });
this->SourceFileExtensionsSet.end()));
this->HeaderFileExtensions.emplace_back("h"); // Cuda extensions
this->HeaderFileExtensions.emplace_back("hh"); fillExts(this->CudaFileExtensions, { "cu" });
this->HeaderFileExtensions.emplace_back("h++");
this->HeaderFileExtensions.emplace_back("hm");
this->HeaderFileExtensions.emplace_back("hpp");
this->HeaderFileExtensions.emplace_back("hxx");
this->HeaderFileExtensions.emplace_back("in");
this->HeaderFileExtensions.emplace_back("txx");
std::copy(this->HeaderFileExtensions.begin(), // Fortran extensions
this->HeaderFileExtensions.end(), fillExts(this->FortranFileExtensions,
std::inserter(this->HeaderFileExtensionsSet, { "f", "F", "for", "f77", "f90", "f95", "f03" });
this->HeaderFileExtensionsSet.end())); }
} }
cmake::~cmake() cmake::~cmake()
+39 -10
View File
@@ -121,6 +121,17 @@ public:
bool isAlias; bool isAlias;
}; };
struct FileExtensions
{
bool Test(std::string const& ext) const
{
return (this->unordered.find(ext) != this->unordered.end());
}
std::vector<std::string> ordered;
std::unordered_set<std::string> unordered;
};
typedef std::map<std::string, cmInstalledFile> InstalledFilesMap; typedef std::map<std::string, cmInstalledFile> InstalledFilesMap;
static const int NO_BUILD_PARALLEL_LEVEL = -1; static const int NO_BUILD_PARALLEL_LEVEL = -1;
@@ -233,24 +244,42 @@ public:
const std::vector<std::string>& GetSourceExtensions() const const std::vector<std::string>& GetSourceExtensions() const
{ {
return this->SourceFileExtensions; return this->SourceFileExtensions.ordered;
} }
bool IsSourceExtension(const std::string& ext) const bool IsSourceExtension(const std::string& ext) const
{ {
return this->SourceFileExtensionsSet.find(ext) != return this->SourceFileExtensions.Test(ext);
this->SourceFileExtensionsSet.end();
} }
const std::vector<std::string>& GetHeaderExtensions() const const std::vector<std::string>& GetHeaderExtensions() const
{ {
return this->HeaderFileExtensions; return this->HeaderFileExtensions.ordered;
} }
bool IsHeaderExtension(const std::string& ext) const bool IsHeaderExtension(const std::string& ext) const
{ {
return this->HeaderFileExtensionsSet.find(ext) != return this->HeaderFileExtensions.Test(ext);
this->HeaderFileExtensionsSet.end(); }
const std::vector<std::string>& GetCudaExtensions() const
{
return this->CudaFileExtensions.ordered;
}
bool IsCudaExtension(const std::string& ext) const
{
return this->CudaFileExtensions.Test(ext);
}
const std::vector<std::string>& GetFortranExtensions() const
{
return this->FortranFileExtensions.ordered;
}
bool IsFortranExtension(const std::string& ext) const
{
return this->FortranFileExtensions.Test(ext);
} }
// Strips the extension (if present and known) from a filename // Strips the extension (if present and known) from a filename
@@ -531,10 +560,10 @@ private:
std::string CheckStampList; std::string CheckStampList;
std::string VSSolutionFile; std::string VSSolutionFile;
std::string EnvironmentGenerator; std::string EnvironmentGenerator;
std::vector<std::string> SourceFileExtensions; FileExtensions SourceFileExtensions;
std::unordered_set<std::string> SourceFileExtensionsSet; FileExtensions HeaderFileExtensions;
std::vector<std::string> HeaderFileExtensions; FileExtensions CudaFileExtensions;
std::unordered_set<std::string> HeaderFileExtensionsSet; FileExtensions FortranFileExtensions;
bool ClearBuildSystem; bool ClearBuildSystem;
bool DebugTryCompile; bool DebugTryCompile;
std::unique_ptr<cmFileTimeCache> FileTimeCache; std::unique_ptr<cmFileTimeCache> FileTimeCache;