浏览代码

ExternalData: Warn on missing file instead of failing

When the primary source tree path named by a DATA{} reference does not
exist, produce an AUTHOR_WARNING instead of a FATAL_ERROR.  This is
useful when writing a new DATA{} reference to a test reference output
that has not been created yet.  This way the developer can run the test,
manually verify the output, and then copy it into place to provide the
reference and eliminate the warning.

If the named source tree path is expected to be a file but exists as a
directory, we still need to produce a FATAL_ERROR.
Brad King 11 年之前
父节点
当前提交
ccd29b9af8

+ 29 - 15
Modules/ExternalData.cmake

@@ -421,6 +421,7 @@ function(_ExternalData_arg target arg options var_file)
   set(external "") # Entries external to the source tree.
   set(internal "") # Entries internal to the source tree.
   set(have_original ${data_is_directory})
+  set(have_original_as_dir 0)
 
   # Process options.
   set(series_option "")
@@ -470,11 +471,18 @@ function(_ExternalData_arg target arg options var_file)
   endif()
 
   if(NOT have_original)
-    message(FATAL_ERROR "Data file referenced by argument\n"
+    if(have_original_as_dir)
+      set(msg_kind FATAL_ERROR)
+      set(msg "that is directory instead of a file!")
+    else()
+      set(msg_kind AUTHOR_WARNING)
+      set(msg "that does not exist as a file (with or without an extension)!")
+    endif()
+    message(${msg_kind} "Data file referenced by argument\n"
       "  ${arg}\n"
       "corresponds to source tree path\n"
       "  ${reldata}\n"
-      "that does not exist as a file (with or without an extension)!")
+      "${msg}")
   endif()
 
   if(external)
@@ -591,27 +599,33 @@ function(_ExternalData_arg_find_files pattern regex)
       set(alg "")
     endif()
     if("x${relname}" MATCHES "^x${regex}$" # matches
-        AND NOT IS_DIRECTORY "${top_src}/${entry}" # not a directory
         AND NOT "x${relname}" MATCHES "(^x|/)\\.ExternalData_" # not staged obj
         )
-      set(name "${top_src}/${relname}")
-      set(file "${top_bin}/${relname}")
-      if(alg)
-        list(APPEND external "${file}|${name}|${alg}")
-      elseif(ExternalData_LINK_CONTENT)
-        _ExternalData_link_content("${name}" alg)
-        list(APPEND external "${file}|${name}|${alg}")
-      elseif(NOT top_same)
-        list(APPEND internal "${file}|${name}")
-      endif()
-      if("${relname}" STREQUAL "${reldata}")
-        set(have_original 1)
+      if(IS_DIRECTORY "${top_src}/${entry}")
+        if("${relname}" STREQUAL "${reldata}")
+          set(have_original_as_dir 1)
+        endif()
+      else()
+        set(name "${top_src}/${relname}")
+        set(file "${top_bin}/${relname}")
+        if(alg)
+          list(APPEND external "${file}|${name}|${alg}")
+        elseif(ExternalData_LINK_CONTENT)
+          _ExternalData_link_content("${name}" alg)
+          list(APPEND external "${file}|${name}|${alg}")
+        elseif(NOT top_same)
+          list(APPEND internal "${file}|${name}")
+        endif()
+        if("${relname}" STREQUAL "${reldata}")
+          set(have_original 1)
+        endif()
       endif()
     endif()
   endforeach()
   set(external "${external}" PARENT_SCOPE)
   set(internal "${internal}" PARENT_SCOPE)
   set(have_original "${have_original}" PARENT_SCOPE)
+  set(have_original_as_dir "${have_original_as_dir}" PARENT_SCOPE)
 endfunction()
 
 #-----------------------------------------------------------------------------

+ 2 - 0
Tests/Module/ExternalData/CMakeLists.txt

@@ -23,6 +23,8 @@ ExternalData_Add_Test(Data1
   COMMAND ${CMAKE_COMMAND}
     -D Data=DATA{Data.dat}
     ${Data1CheckSpaces}
+    -D DataMissing=DATA{DataMissing.dat}
+    -D DataMissingWithAssociated=DATA{DataMissing.dat,Data.dat}
     -D SeriesA=DATA{SeriesA.dat,:}
     -D SeriesB=DATA{SeriesB.dat,:}
     -D SeriesC=DATA{SeriesC.dat,:}

+ 22 - 0
Tests/Module/ExternalData/Data1Check.cmake

@@ -8,6 +8,28 @@ if(DEFINED DataSpace)
     message(SEND_ERROR "Input file:\n  ${DataSpace}\ndoes not have expected content, but [[${lines}]]")
   endif()
 endif()
+if(DataMissing)
+  if(EXISTS "${DataMissing}")
+    message(SEND_ERROR
+      "Input file:\n"
+      "  ${DataMissing}\n"
+      "exists but should not."
+      )
+  endif()
+else()
+  message(SEND_ERROR "DataMissing is not set!")
+endif()
+if(DataMissingWithAssociated)
+  if(EXISTS "${DataMissingWithAssociated}")
+    message(SEND_ERROR
+      "Input file:\n"
+      "  ${DataMissingWithAssociated}\n"
+      "exists but should not."
+      )
+  endif()
+else()
+  message(SEND_ERROR "DataMissingWithAssociated is not set!")
+endif()
 set(SeriesAn1 "1\\.dat")
 set(SeriesBn1 "_1\\.dat")
 set(SeriesCn1 "\\.1\\.dat")

+ 1 - 1
Tests/RunCMake/ExternalData/Directory1-stderr.txt

@@ -7,7 +7,7 @@ CMake Error at .*/Modules/ExternalData.cmake:[0-9]+ \(message\):
 
     Directory1
 
-  that does not exist as a file \(with or without an extension\)!
+  that is directory instead of a file!
 Call Stack \(most recent call first\):
   .*
   Directory1.cmake:3 \(ExternalData_Add_Test\)

+ 0 - 1
Tests/RunCMake/ExternalData/Directory3-result.txt

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

+ 2 - 1
Tests/RunCMake/ExternalData/Directory3-stderr.txt

@@ -1,4 +1,4 @@
-CMake Error at .*/Modules/ExternalData.cmake:[0-9]+ \(message\):
+CMake Warning \(dev\) at .*/Modules/ExternalData.cmake:[0-9]+ \(message\):
   Data file referenced by argument
 
     DATA{Directory3/\*}
@@ -12,3 +12,4 @@ Call Stack \(most recent call first\):
   .*
   Directory3.cmake:3 \(ExternalData_Add_Test\)
   CMakeLists.txt:3 \(include\)
+This warning is for project developers.  Use -Wno-dev to suppress it.

+ 0 - 1
Tests/RunCMake/ExternalData/MissingData-result.txt

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

+ 3 - 2
Tests/RunCMake/ExternalData/MissingData-stderr.txt

@@ -1,4 +1,4 @@
-CMake Error at .*/Modules/ExternalData.cmake:[0-9]+ \(message\):
+CMake Warning \(dev\) at .*/Modules/ExternalData.cmake:[0-9]+ \(message\):
   Data file referenced by argument
 
     DATA{MissingData.txt}
@@ -10,5 +10,6 @@ CMake Error at .*/Modules/ExternalData.cmake:[0-9]+ \(message\):
   that does not exist as a file \(with or without an extension\)!
 Call Stack \(most recent call first\):
   .*
-  MissingData.cmake:2 \(ExternalData_Add_Test\)
+  MissingData.cmake:4 \(ExternalData_Expand_Arguments\)
   CMakeLists.txt:3 \(include\)
+This warning is for project developers.  Use -Wno-dev to suppress it.

+ 1 - 0
Tests/RunCMake/ExternalData/MissingData-stdout.txt

@@ -0,0 +1 @@
+-- Missing data reference correctly transformed!

+ 9 - 4
Tests/RunCMake/ExternalData/MissingData.cmake

@@ -1,5 +1,10 @@
 include(ExternalData)
-ExternalData_Add_Test(Data
-  NAME Test
-  COMMAND ${CMAKE_COMMAND} -E echo DATA{MissingData.txt}
-  )
+
+set(output "${CMAKE_SOURCE_DIR}/MissingData.txt")
+ExternalData_Expand_Arguments(Data args DATA{MissingData.txt})
+if("x${args}" STREQUAL "x${output}")
+  message(STATUS "Missing data reference correctly transformed!")
+else()
+  message(FATAL_ERROR "Missing data reference transformed to:\n  ${args}\n"
+    "but we expected:\n  ${output}")
+endif()

+ 15 - 0
Tests/RunCMake/ExternalData/MissingDataWithAssociated-stderr.txt

@@ -0,0 +1,15 @@
+CMake Warning \(dev\) at .*/Modules/ExternalData.cmake:[0-9]+ \(message\):
+  Data file referenced by argument
+
+    DATA{MissingData.txt,Data.txt}
+
+  corresponds to source tree path
+
+    MissingData.txt
+
+  that does not exist as a file \(with or without an extension\)!
+Call Stack \(most recent call first\):
+  .*
+  MissingDataWithAssociated.cmake:4 \(ExternalData_Expand_Arguments\)
+  CMakeLists.txt:3 \(include\)
+This warning is for project developers.  Use -Wno-dev to suppress it.

+ 1 - 0
Tests/RunCMake/ExternalData/MissingDataWithAssociated-stdout.txt

@@ -0,0 +1 @@
+-- Missing data reference correctly transformed!

+ 10 - 0
Tests/RunCMake/ExternalData/MissingDataWithAssociated.cmake

@@ -0,0 +1,10 @@
+include(ExternalData)
+
+set(output "${CMAKE_BINARY_DIR}/MissingData.txt")
+ExternalData_Expand_Arguments(Data args DATA{MissingData.txt,Data.txt})
+if("x${args}" STREQUAL "x${output}")
+  message(STATUS "Missing data reference correctly transformed!")
+else()
+  message(FATAL_ERROR "Missing data reference transformed to:\n  ${args}\n"
+    "but we expected:\n  ${output}")
+endif()

+ 1 - 0
Tests/RunCMake/ExternalData/RunCMakeTest.cmake

@@ -15,6 +15,7 @@ run_cmake(LinkContentMD5)
 run_cmake(LinkContentSHA1)
 run_cmake(LinkDirectory1)
 run_cmake(MissingData)
+run_cmake(MissingDataWithAssociated)
 run_cmake(NoLinkInSource)
 run_cmake(NoURLTemplates)
 run_cmake(NormalData1)