RunCMake.cmake 9.9 KB

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