RunCMake.cmake 6.3 KB

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