Browse Source

include: refactor call sites of cmMakefile::ReadDependentFile

Fixes: #16773
Asit Dhal 5 years ago
parent
commit
3c324689a7

+ 1 - 1
Source/CTest/cmCTestTestHandler.cxx

@@ -122,7 +122,7 @@ bool cmCTestSubdirCommand(std::vector<std::string> const& args,
       readit = status.GetMakefile().ReadDependentFile(fname);
     }
     if (!readit) {
-      status.SetError(cmStrCat("Could not find include file: ", fname));
+      status.SetError(cmStrCat("Could not load include file: ", fname));
       return false;
     }
   }

+ 16 - 5
Source/cmIncludeCommand.cxx

@@ -146,11 +146,24 @@ bool cmIncludeCommand(std::vector<std::string> const& args,
 
   std::string listFile = cmSystemTools::CollapseFullPath(
     fname, status.GetMakefile().GetCurrentSourceDirectory());
-  if (optional && !cmSystemTools::FileExists(listFile)) {
+
+  const bool fileDoesnotExist = !cmSystemTools::FileExists(listFile);
+  const bool fileIsDirectory = cmSystemTools::FileIsDirectory(listFile);
+  if (fileDoesnotExist || fileIsDirectory) {
     if (!resultVarName.empty()) {
       status.GetMakefile().AddDefinition(resultVarName, "NOTFOUND");
     }
-    return true;
+    if (optional) {
+      return true;
+    }
+    if (fileDoesnotExist) {
+      status.SetError(cmStrCat("could not find requested file:\n  ", fname));
+      return false;
+    }
+    if (fileIsDirectory) {
+      status.SetError(cmStrCat("requested file is a directory:\n  ", fname));
+      return false;
+    }
   }
 
   bool readit =
@@ -163,9 +176,7 @@ bool cmIncludeCommand(std::vector<std::string> const& args,
   }
 
   if (!optional && !readit && !cmSystemTools::GetFatalErrorOccured()) {
-    std::string m = cmStrCat("could not find load file:\n"
-                             "  ",
-                             fname);
+    std::string m = cmStrCat("could not load requested file:\n  ", fname);
     status.SetError(m);
     return false;
   }

+ 12 - 1
Source/cmProjectCommand.cxx

@@ -358,6 +358,17 @@ static bool IncludeByVariable(cmExecutionStatus& status,
     return true;
   }
 
+  std::string includeFile =
+    cmSystemTools::CollapseFullPath(*include, mf.GetCurrentSourceDirectory());
+  if (!cmSystemTools::FileExists(includeFile)) {
+    status.SetError(cmStrCat("could not find requested file:\n  ", *include));
+    return false;
+  }
+  if (cmSystemTools::FileIsDirectory(includeFile)) {
+    status.SetError(cmStrCat("requested file is a directory:\n  ", *include));
+    return false;
+  }
+
   const bool readit = mf.ReadDependentFile(*include);
   if (readit) {
     return true;
@@ -367,7 +378,7 @@ static bool IncludeByVariable(cmExecutionStatus& status,
     return true;
   }
 
-  status.SetError(cmStrCat("could not find file:\n  ", *include));
+  status.SetError(cmStrCat("could not load requested file:\n  ", *include));
   return false;
 }
 

+ 1 - 1
Tests/RunCMake/include/ExportExportInclude-stderr.txt

@@ -1,5 +1,5 @@
 CMake Error at ExportExportInclude.cmake:6 \(include\):
-  include could not find load file:
+  include could not find requested file:
 
     .*/Tests/RunCMake/include/ExportExportInclude-build/theTargets.cmake
 Call Stack \(most recent call first\):

+ 1 - 0
Tests/RunCMake/include/IncludeIsDirectory-result.txt

@@ -0,0 +1 @@
+1

+ 6 - 0
Tests/RunCMake/include/IncludeIsDirectory-stderr.txt

@@ -0,0 +1,6 @@
+CMake Error at IncludeIsDirectory.cmake:1 \(include\):
+  include requested file is a directory:
+
+    .*/Tests/RunCMake/include/IncludeIsDirectory-build
+Call Stack \(most recent call first\):
+  CMakeLists.txt:3 \(include\)

+ 1 - 0
Tests/RunCMake/include/IncludeIsDirectory.cmake

@@ -0,0 +1 @@
+include("${CMAKE_CURRENT_BINARY_DIR}")

+ 1 - 0
Tests/RunCMake/include/IncludeMalformed-result.txt

@@ -0,0 +1 @@
+1

+ 13 - 0
Tests/RunCMake/include/IncludeMalformed-stderr.txt

@@ -0,0 +1,13 @@
+CMake Error at malformedInclude.cmake:1:
+  Parse error.  Function missing ending "\)".  End of file reached.
+Call Stack \(most recent call first\):
+  IncludeMalformed.cmake:1 \(include\)
+  CMakeLists.txt:3 \(include\)
+
+
+CMake Error at IncludeMalformed.cmake:1 \(include\):
+  include could not load requested file:
+
+    malformedInclude.cmake
+Call Stack \(most recent call first\):
+  CMakeLists.txt:3 \(include\)

+ 1 - 0
Tests/RunCMake/include/IncludeMalformed.cmake

@@ -0,0 +1 @@
+include("malformedInclude.cmake")

+ 2 - 0
Tests/RunCMake/include/RunCMakeTest.cmake

@@ -5,3 +5,5 @@ run_cmake(EmptyStringOptional)
 run_cmake(CMP0024-WARN)
 run_cmake(CMP0024-NEW)
 run_cmake(ExportExportInclude)
+run_cmake(IncludeIsDirectory)
+run_cmake(IncludeMalformed)

+ 1 - 0
Tests/RunCMake/include/malformedInclude.cmake

@@ -0,0 +1 @@
+if(