RunCMake.cmake 9.8 KB

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