Sfoglia il codice sorgente

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.
Robert Maynard 5 anni fa
parent
commit
7628153edb

+ 1 - 1
Source/cmAuxSourceDirectoryCommand.cxx

@@ -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

+ 1 - 1
Source/cmExtraCodeBlocksGenerator.cxx

@@ -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();

+ 1 - 2
Source/cmExtraCodeLiteGenerator.cxx

@@ -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);

+ 4 - 4
Source/cmQtAutoGenInitializer.cxx

@@ -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) {

+ 12 - 19
Source/cmSourceFile.cxx

@@ -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);

+ 2 - 2
Source/cmSourceFileLocation.cxx

@@ -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)

+ 13 - 2
Source/cmake.cxx

@@ -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);
     }
   }

+ 14 - 25
Source/cmake.h

@@ -264,44 +264,33 @@ 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);
   }
 
-  const std::vector<std::string>& GetHeaderExtensions() const
-  {
-    return this->HeaderFileExtensions.ordered;
-  }
-
-  bool IsHeaderExtension(cm::string_view ext) const
-  {
-    return this->HeaderFileExtensions.Test(ext);
-  }
-
-  const std::vector<std::string>& GetCudaExtensions() const
+  bool IsAKnownExtension(cm::string_view ext) const
   {
-    return this->CudaFileExtensions.ordered;
+    return this->IsAKnownSourceExtension(ext) || this->IsAHeaderExtension(ext);
   }
 
-  bool IsCudaExtension(cm::string_view ext) const
-  {
-    return this->CudaFileExtensions.Test(ext);
-  }
+  std::vector<std::string> GetAllExtensions() const;
 
-  const std::vector<std::string>& GetFortranExtensions() const
+  const std::vector<std::string>& GetHeaderExtensions() const
   {
-    return this->FortranFileExtensions.ordered;
+    return this->HeaderFileExtensions.ordered;
   }
 
-  bool IsFortranExtension(cm::string_view ext) const
+  bool IsAHeaderExtension(cm::string_view ext) const
   {
-    return this->FortranFileExtensions.Test(ext);
+    return this->HeaderFileExtensions.Test(ext);
   }
 
   // Strips the extension (if present and known) from a filename
@@ -628,7 +617,7 @@ private:
   std::string CheckStampList;
   std::string VSSolutionFile;
   std::string EnvironmentGenerator;
-  FileExtensions SourceFileExtensions;
+  FileExtensions CLikeSourceFileExtensions;
   FileExtensions HeaderFileExtensions;
   FileExtensions CudaFileExtensions;
   FileExtensions FortranFileExtensions;

+ 2 - 2
Tests/RunCMake/ObjectLibrary/MissingSource-stderr.txt

@@ -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\)

+ 2 - 2
Tests/RunCMake/interface_library/global-interface-stderr.txt

@@ -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\)