RunCMake.cmake 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. foreach(arg
  2. RunCMake_GENERATOR
  3. RunCMake_SOURCE_DIR
  4. RunCMake_BINARY_DIR
  5. )
  6. if(NOT DEFINED ${arg})
  7. message(FATAL_ERROR "${arg} not given!")
  8. endif()
  9. endforeach()
  10. function(run_cmake test)
  11. if(DEFINED ENV{RunCMake_TEST_FILTER} AND NOT test MATCHES "$ENV{RunCMake_TEST_FILTER}")
  12. return()
  13. endif()
  14. set(top_src "${RunCMake_SOURCE_DIR}")
  15. set(top_bin "${RunCMake_BINARY_DIR}")
  16. if(EXISTS ${top_src}/${test}-result.txt)
  17. file(READ ${top_src}/${test}-result.txt expect_result)
  18. string(REGEX REPLACE "\n+$" "" expect_result "${expect_result}")
  19. else()
  20. set(expect_result 0)
  21. endif()
  22. string(TOLOWER ${CMAKE_HOST_SYSTEM_NAME} platform_name)
  23. if(platform_name MATCHES cygwin)
  24. #remove all additional bits from cygwin name
  25. set(platform_name cygwin)
  26. endif()
  27. foreach(o out err)
  28. if(RunCMake-std${o}-file AND EXISTS ${top_src}/${RunCMake-std${o}-file})
  29. file(READ ${top_src}/${RunCMake-std${o}-file} expect_std${o})
  30. string(REGEX REPLACE "\n+$" "" expect_std${o} "${expect_std${o}}")
  31. elseif(EXISTS ${top_src}/${test}-std${o}-${platform_name}.txt)
  32. file(READ ${top_src}/${test}-std${o}-${platform_name}.txt expect_std${o})
  33. string(REGEX REPLACE "\n+$" "" expect_std${o} "${expect_std${o}}")
  34. elseif(EXISTS ${top_src}/${test}-std${o}.txt)
  35. file(READ ${top_src}/${test}-std${o}.txt expect_std${o})
  36. string(REGEX REPLACE "\n+$" "" expect_std${o} "${expect_std${o}}")
  37. else()
  38. unset(expect_std${o})
  39. endif()
  40. endforeach()
  41. if (NOT expect_stderr)
  42. if (NOT RunCMake_DEFAULT_stderr)
  43. set(RunCMake_DEFAULT_stderr "^$")
  44. endif()
  45. set(expect_stderr ${RunCMake_DEFAULT_stderr})
  46. endif()
  47. if (NOT RunCMake_TEST_SOURCE_DIR)
  48. set(RunCMake_TEST_SOURCE_DIR "${top_src}")
  49. endif()
  50. if(NOT RunCMake_TEST_BINARY_DIR)
  51. set(RunCMake_TEST_BINARY_DIR "${top_bin}/${test}-build")
  52. endif()
  53. if(NOT RunCMake_TEST_NO_CLEAN)
  54. file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
  55. endif()
  56. file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
  57. if(RunCMake-prep-file AND EXISTS ${top_src}/${RunCMake-prep-file})
  58. include(${top_src}/${RunCMake-prep-file})
  59. else()
  60. include(${top_src}/${test}-prep.cmake OPTIONAL)
  61. endif()
  62. if(NOT DEFINED RunCMake_TEST_OPTIONS)
  63. set(RunCMake_TEST_OPTIONS "")
  64. endif()
  65. if(APPLE)
  66. list(APPEND RunCMake_TEST_OPTIONS -DCMAKE_POLICY_DEFAULT_CMP0025=NEW)
  67. endif()
  68. if(RunCMake_MAKE_PROGRAM)
  69. list(APPEND RunCMake_TEST_OPTIONS "-DCMAKE_MAKE_PROGRAM=${RunCMake_MAKE_PROGRAM}")
  70. endif()
  71. if(RunCMake_TEST_OUTPUT_MERGE)
  72. set(actual_stderr_var actual_stdout)
  73. set(actual_stderr "")
  74. else()
  75. set(actual_stderr_var actual_stderr)
  76. endif()
  77. if(DEFINED RunCMake_TEST_TIMEOUT)
  78. set(maybe_timeout TIMEOUT ${RunCMake_TEST_TIMEOUT})
  79. else()
  80. set(maybe_timeout "")
  81. endif()
  82. if(RunCMake-stdin-file AND EXISTS ${top_src}/${RunCMake-stdin-file})
  83. set(maybe_input_file INPUT_FILE ${top_src}/${RunCMake-stdin-file})
  84. elseif(EXISTS ${top_src}/${test}-stdin.txt)
  85. set(maybe_input_file INPUT_FILE ${top_src}/${test}-stdin.txt)
  86. else()
  87. set(maybe_input_file "")
  88. endif()
  89. if(RunCMake_TEST_COMMAND)
  90. if(NOT RunCMake_TEST_COMMAND_WORKING_DIRECTORY)
  91. set(RunCMake_TEST_COMMAND_WORKING_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
  92. endif()
  93. execute_process(
  94. COMMAND ${RunCMake_TEST_COMMAND}
  95. WORKING_DIRECTORY "${RunCMake_TEST_COMMAND_WORKING_DIRECTORY}"
  96. OUTPUT_VARIABLE actual_stdout
  97. ERROR_VARIABLE ${actual_stderr_var}
  98. RESULT_VARIABLE actual_result
  99. ENCODING UTF8
  100. ${maybe_timeout}
  101. ${maybe_input_file}
  102. )
  103. else()
  104. if(RunCMake_GENERATOR_INSTANCE)
  105. set(_D_CMAKE_GENERATOR_INSTANCE "-DCMAKE_GENERATOR_INSTANCE=${RunCMake_GENERATOR_INSTANCE}")
  106. else()
  107. set(_D_CMAKE_GENERATOR_INSTANCE "")
  108. endif()
  109. if(NOT RunCMake_TEST_NO_SOURCE_DIR)
  110. set(maybe_source_dir "${RunCMake_TEST_SOURCE_DIR}")
  111. else()
  112. set(maybe_source_dir "")
  113. endif()
  114. execute_process(
  115. COMMAND ${CMAKE_COMMAND}
  116. ${maybe_source_dir}
  117. -G "${RunCMake_GENERATOR}"
  118. -A "${RunCMake_GENERATOR_PLATFORM}"
  119. -T "${RunCMake_GENERATOR_TOOLSET}"
  120. ${_D_CMAKE_GENERATOR_INSTANCE}
  121. -DRunCMake_TEST=${test}
  122. --no-warn-unused-cli
  123. ${RunCMake_TEST_OPTIONS}
  124. WORKING_DIRECTORY "${RunCMake_TEST_BINARY_DIR}"
  125. OUTPUT_VARIABLE actual_stdout
  126. ERROR_VARIABLE ${actual_stderr_var}
  127. RESULT_VARIABLE actual_result
  128. ENCODING UTF8
  129. ${maybe_timeout}
  130. ${maybe_input_file}
  131. )
  132. endif()
  133. set(msg "")
  134. if(NOT "${actual_result}" MATCHES "${expect_result}")
  135. string(APPEND msg "Result is [${actual_result}], not [${expect_result}].\n")
  136. endif()
  137. # Special case: remove ninja no-op line from stderr, but not stdout.
  138. # Test cases that look for it should use RunCMake_TEST_OUTPUT_MERGE.
  139. string(REGEX REPLACE "(^|\r?\n)ninja: no work to do\\.\r?\n" "\\1" actual_stderr "${actual_stderr}")
  140. # Remove incidental content from both stdout and stderr.
  141. string(CONCAT ignore_line_regex
  142. "(^|\n)((==[0-9]+=="
  143. "|BullseyeCoverage"
  144. "|[a-z]+\\([0-9]+\\) malloc:"
  145. "|clang[^:]*: warning: the object size sanitizer has no effect at -O0, but is explicitly enabled:"
  146. "|Error kstat returned"
  147. "|Hit xcodebuild bug"
  148. "|Recompacting log\\.\\.\\."
  149. "|LICENSE WARNING:"
  150. "|Your license to use PGI[^\n]*expired"
  151. "|Please obtain a new version at"
  152. "|contact PGI Sales at"
  153. "|[^\n]*install_name_tool: warning: changes being made to the file will invalidate the code signature in:"
  154. "|[^\n]*xcodebuild[^\n]*warning: file type[^\n]*is based on missing file type"
  155. "|[^\n]*objc[^\n]*: Class AMSupportURL[^\n]* One of the two will be used. Which one is undefined."
  156. "|[^\n]*is a member of multiple groups"
  157. "|[^\n]*offset in archive not a multiple of 8"
  158. "|[^\n]*from Time Machine by path"
  159. "|[^\n]*Bullseye Testing Technology"
  160. ")[^\n]*\n)+"
  161. )
  162. foreach(o out err)
  163. string(REGEX REPLACE "\r\n" "\n" actual_std${o} "${actual_std${o}}")
  164. string(REGEX REPLACE "${ignore_line_regex}" "\\1" actual_std${o} "${actual_std${o}}")
  165. string(REGEX REPLACE "\n+$" "" actual_std${o} "${actual_std${o}}")
  166. set(expect_${o} "")
  167. if(DEFINED expect_std${o})
  168. if(NOT "${actual_std${o}}" MATCHES "${expect_std${o}}")
  169. string(REGEX REPLACE "\n" "\n expect-${o}> " expect_${o}
  170. " expect-${o}> ${expect_std${o}}")
  171. set(expect_${o} "Expected std${o} to match:\n${expect_${o}}\n")
  172. string(APPEND msg "std${o} does not match that expected.\n")
  173. endif()
  174. endif()
  175. endforeach()
  176. unset(RunCMake_TEST_FAILED)
  177. if(RunCMake-check-file AND EXISTS ${top_src}/${RunCMake-check-file})
  178. include(${top_src}/${RunCMake-check-file})
  179. else()
  180. include(${top_src}/${test}-check.cmake OPTIONAL)
  181. endif()
  182. if(RunCMake_TEST_FAILED)
  183. set(msg "${RunCMake_TEST_FAILED}\n${msg}")
  184. endif()
  185. if(msg AND RunCMake_TEST_COMMAND)
  186. string(REPLACE ";" "\" \"" command "\"${RunCMake_TEST_COMMAND}\"")
  187. string(APPEND msg "Command was:\n command> ${command}\n")
  188. endif()
  189. if(msg)
  190. string(REGEX REPLACE "\n" "\n actual-out> " actual_out " actual-out> ${actual_stdout}")
  191. string(REGEX REPLACE "\n" "\n actual-err> " actual_err " actual-err> ${actual_stderr}")
  192. message(SEND_ERROR "${test} - FAILED:\n"
  193. "${msg}"
  194. "${expect_out}"
  195. "Actual stdout:\n${actual_out}\n"
  196. "${expect_err}"
  197. "Actual stderr:\n${actual_err}\n"
  198. )
  199. else()
  200. message(STATUS "${test} - PASSED")
  201. endif()
  202. endfunction()
  203. function(run_cmake_command test)
  204. set(RunCMake_TEST_COMMAND "${ARGN}")
  205. run_cmake(${test})
  206. endfunction()
  207. function(run_cmake_with_options test)
  208. set(RunCMake_TEST_OPTIONS "${ARGN}")
  209. run_cmake(${test})
  210. endfunction()
  211. function(ensure_files_match expected_file actual_file)
  212. if(NOT EXISTS "${expected_file}")
  213. message(FATAL_ERROR "Expected file does not exist:\n ${expected_file}")
  214. endif()
  215. if(NOT EXISTS "${actual_file}")
  216. message(FATAL_ERROR "Actual file does not exist:\n ${actual_file}")
  217. endif()
  218. file(READ "${expected_file}" expected_file_content)
  219. file(READ "${actual_file}" actual_file_content)
  220. if(NOT "${expected_file_content}" STREQUAL "${actual_file_content}")
  221. message(FATAL_ERROR "Actual file content does not match expected:\n
  222. \n
  223. expected file: ${expected_file}\n
  224. expected content:\n
  225. ${expected_file_content}\n
  226. \n
  227. actual file: ${actual_file}\n
  228. actual content:\n
  229. ${actual_file_content}\n
  230. ")
  231. endif()
  232. endfunction()
  233. # Protect RunCMake tests from calling environment.
  234. unset(ENV{MAKEFLAGS})