RunCMake.cmake 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. foreach(o out err)
  23. if(RunCMake-std${o}-file AND EXISTS ${top_src}/${RunCMake-std${o}-file})
  24. file(READ ${top_src}/${RunCMake-std${o}-file} expect_std${o})
  25. string(REGEX REPLACE "\n+$" "" expect_std${o} "${expect_std${o}}")
  26. elseif(EXISTS ${top_src}/${test}-std${o}.txt)
  27. file(READ ${top_src}/${test}-std${o}.txt expect_std${o})
  28. string(REGEX REPLACE "\n+$" "" expect_std${o} "${expect_std${o}}")
  29. else()
  30. unset(expect_std${o})
  31. endif()
  32. endforeach()
  33. if (NOT expect_stderr)
  34. if (NOT RunCMake_DEFAULT_stderr)
  35. set(RunCMake_DEFAULT_stderr "^$")
  36. endif()
  37. set(expect_stderr ${RunCMake_DEFAULT_stderr})
  38. endif()
  39. if (NOT RunCMake_TEST_SOURCE_DIR)
  40. set(RunCMake_TEST_SOURCE_DIR "${top_src}")
  41. endif()
  42. if(NOT RunCMake_TEST_BINARY_DIR)
  43. set(RunCMake_TEST_BINARY_DIR "${top_bin}/${test}-build")
  44. endif()
  45. if(NOT RunCMake_TEST_NO_CLEAN)
  46. file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
  47. endif()
  48. file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
  49. if(RunCMake-prep-file AND EXISTS ${top_src}/${RunCMake-prep-file})
  50. include(${top_src}/${RunCMake-prep-file})
  51. else()
  52. include(${top_src}/${test}-prep.cmake OPTIONAL)
  53. endif()
  54. if(NOT DEFINED RunCMake_TEST_OPTIONS)
  55. set(RunCMake_TEST_OPTIONS "")
  56. endif()
  57. if(APPLE)
  58. list(APPEND RunCMake_TEST_OPTIONS -DCMAKE_POLICY_DEFAULT_CMP0025=NEW)
  59. endif()
  60. if(RunCMake_MAKE_PROGRAM)
  61. list(APPEND RunCMake_TEST_OPTIONS "-DCMAKE_MAKE_PROGRAM=${RunCMake_MAKE_PROGRAM}")
  62. endif()
  63. if(RunCMake_TEST_OUTPUT_MERGE)
  64. set(actual_stderr_var actual_stdout)
  65. set(actual_stderr "")
  66. else()
  67. set(actual_stderr_var actual_stderr)
  68. endif()
  69. if(DEFINED RunCMake_TEST_TIMEOUT)
  70. set(maybe_timeout TIMEOUT ${RunCMake_TEST_TIMEOUT})
  71. else()
  72. set(maybe_timeout "")
  73. endif()
  74. if(RunCMake-stdin-file AND EXISTS ${top_src}/${RunCMake-stdin-file})
  75. set(maybe_input_file INPUT_FILE ${top_src}/${RunCMake-stdin-file})
  76. elseif(EXISTS ${top_src}/${test}-stdin.txt)
  77. set(maybe_input_file INPUT_FILE ${top_src}/${test}-stdin.txt)
  78. else()
  79. set(maybe_input_file "")
  80. endif()
  81. if(RunCMake_TEST_COMMAND)
  82. execute_process(
  83. COMMAND ${RunCMake_TEST_COMMAND}
  84. WORKING_DIRECTORY "${RunCMake_TEST_BINARY_DIR}"
  85. OUTPUT_VARIABLE actual_stdout
  86. ERROR_VARIABLE ${actual_stderr_var}
  87. RESULT_VARIABLE actual_result
  88. ENCODING UTF8
  89. ${maybe_timeout}
  90. ${maybe_input_file}
  91. )
  92. else()
  93. if(RunCMake_GENERATOR_INSTANCE)
  94. set(_D_CMAKE_GENERATOR_INSTANCE "-DCMAKE_GENERATOR_INSTANCE=${RunCMake_GENERATOR_INSTANCE}")
  95. else()
  96. set(_D_CMAKE_GENERATOR_INSTANCE "")
  97. endif()
  98. execute_process(
  99. COMMAND ${CMAKE_COMMAND} "${RunCMake_TEST_SOURCE_DIR}"
  100. -G "${RunCMake_GENERATOR}"
  101. -A "${RunCMake_GENERATOR_PLATFORM}"
  102. -T "${RunCMake_GENERATOR_TOOLSET}"
  103. ${_D_CMAKE_GENERATOR_INSTANCE}
  104. -DRunCMake_TEST=${test}
  105. --no-warn-unused-cli
  106. ${RunCMake_TEST_OPTIONS}
  107. WORKING_DIRECTORY "${RunCMake_TEST_BINARY_DIR}"
  108. OUTPUT_VARIABLE actual_stdout
  109. ERROR_VARIABLE ${actual_stderr_var}
  110. RESULT_VARIABLE actual_result
  111. ENCODING UTF8
  112. ${maybe_timeout}
  113. ${maybe_input_file}
  114. )
  115. endif()
  116. set(msg "")
  117. if(NOT "${actual_result}" MATCHES "${expect_result}")
  118. string(APPEND msg "Result is [${actual_result}], not [${expect_result}].\n")
  119. endif()
  120. string(CONCAT ignore_line_regex
  121. "(^|\n)((==[0-9]+=="
  122. "|BullseyeCoverage"
  123. "|[a-z]+\\([0-9]+\\) malloc:"
  124. "|clang[^:]*: warning: the object size sanitizer has no effect at -O0, but is explicitly enabled:"
  125. "|Error kstat returned"
  126. "|Hit xcodebuild bug"
  127. "|[^\n]*xcodebuild[^\n]*warning: file type[^\n]*is based on missing file type"
  128. "|ld: 0711-224 WARNING: Duplicate symbol: .__init_aix_libgcc_cxa_atexit"
  129. "|ld: 0711-345 Use the -bloadmap or -bnoquiet option to obtain more information"
  130. "|[^\n]*is a member of multiple groups"
  131. "|[^\n]*from Time Machine by path"
  132. "|[^\n]*Bullseye Testing Technology"
  133. ")[^\n]*\n)+"
  134. )
  135. foreach(o out err)
  136. string(REGEX REPLACE "\r\n" "\n" actual_std${o} "${actual_std${o}}")
  137. string(REGEX REPLACE "${ignore_line_regex}" "\\1" actual_std${o} "${actual_std${o}}")
  138. string(REGEX REPLACE "\n+$" "" actual_std${o} "${actual_std${o}}")
  139. set(expect_${o} "")
  140. if(DEFINED expect_std${o})
  141. if(NOT "${actual_std${o}}" MATCHES "${expect_std${o}}")
  142. string(REGEX REPLACE "\n" "\n expect-${o}> " expect_${o}
  143. " expect-${o}> ${expect_std${o}}")
  144. set(expect_${o} "Expected std${o} to match:\n${expect_${o}}\n")
  145. string(APPEND msg "std${o} does not match that expected.\n")
  146. endif()
  147. endif()
  148. endforeach()
  149. unset(RunCMake_TEST_FAILED)
  150. if(RunCMake-check-file AND EXISTS ${top_src}/${RunCMake-check-file})
  151. include(${top_src}/${RunCMake-check-file})
  152. else()
  153. include(${top_src}/${test}-check.cmake OPTIONAL)
  154. endif()
  155. if(RunCMake_TEST_FAILED)
  156. set(msg "${RunCMake_TEST_FAILED}\n${msg}")
  157. endif()
  158. if(msg AND RunCMake_TEST_COMMAND)
  159. string(REPLACE ";" "\" \"" command "\"${RunCMake_TEST_COMMAND}\"")
  160. string(APPEND msg "Command was:\n command> ${command}\n")
  161. endif()
  162. if(msg)
  163. string(REGEX REPLACE "\n" "\n actual-out> " actual_out " actual-out> ${actual_stdout}")
  164. string(REGEX REPLACE "\n" "\n actual-err> " actual_err " actual-err> ${actual_stderr}")
  165. message(SEND_ERROR "${test} - FAILED:\n"
  166. "${msg}"
  167. "${expect_out}"
  168. "Actual stdout:\n${actual_out}\n"
  169. "${expect_err}"
  170. "Actual stderr:\n${actual_err}\n"
  171. )
  172. else()
  173. message(STATUS "${test} - PASSED")
  174. endif()
  175. endfunction()
  176. function(run_cmake_command test)
  177. set(RunCMake_TEST_COMMAND "${ARGN}")
  178. run_cmake(${test})
  179. endfunction()
  180. # Protect RunCMake tests from calling environment.
  181. unset(ENV{MAKEFLAGS})