RunCMake.cmake 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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(NOT DEFINED RunCMake_TEST_OPTIONS)
  47. set(RunCMake_TEST_OPTIONS "")
  48. endif()
  49. if(APPLE)
  50. list(APPEND RunCMake_TEST_OPTIONS -DCMAKE_POLICY_DEFAULT_CMP0025=NEW)
  51. endif()
  52. if(RunCMake_GENERATOR MATCHES "^Visual Studio 8 2005" AND NOT RunCMake_WARN_VS8)
  53. list(APPEND RunCMake_TEST_OPTIONS -DCMAKE_WARN_VS8=OFF)
  54. endif()
  55. if(RunCMake_MAKE_PROGRAM)
  56. list(APPEND RunCMake_TEST_OPTIONS "-DCMAKE_MAKE_PROGRAM=${RunCMake_MAKE_PROGRAM}")
  57. endif()
  58. if(RunCMake_TEST_OUTPUT_MERGE)
  59. set(actual_stderr_var actual_stdout)
  60. set(actual_stderr "")
  61. else()
  62. set(actual_stderr_var actual_stderr)
  63. endif()
  64. if(DEFINED RunCMake_TEST_TIMEOUT)
  65. set(maybe_timeout TIMEOUT ${RunCMake_TEST_TIMEOUT})
  66. else()
  67. set(maybe_timeout "")
  68. endif()
  69. if(RunCMake_TEST_COMMAND)
  70. execute_process(
  71. COMMAND ${RunCMake_TEST_COMMAND}
  72. WORKING_DIRECTORY "${RunCMake_TEST_BINARY_DIR}"
  73. OUTPUT_VARIABLE actual_stdout
  74. ERROR_VARIABLE ${actual_stderr_var}
  75. RESULT_VARIABLE actual_result
  76. ENCODING UTF8
  77. ${maybe_timeout}
  78. )
  79. else()
  80. if(RunCMake_GENERATOR_INSTANCE)
  81. set(_D_CMAKE_GENERATOR_INSTANCE "-DCMAKE_GENERATOR_INSTANCE=${RunCMake_GENERATOR_INSTANCE}")
  82. else()
  83. set(_D_CMAKE_GENERATOR_INSTANCE "")
  84. endif()
  85. execute_process(
  86. COMMAND ${CMAKE_COMMAND} "${RunCMake_TEST_SOURCE_DIR}"
  87. -G "${RunCMake_GENERATOR}"
  88. -A "${RunCMake_GENERATOR_PLATFORM}"
  89. -T "${RunCMake_GENERATOR_TOOLSET}"
  90. ${_D_CMAKE_GENERATOR_INSTANCE}
  91. -DRunCMake_TEST=${test}
  92. --no-warn-unused-cli
  93. ${RunCMake_TEST_OPTIONS}
  94. WORKING_DIRECTORY "${RunCMake_TEST_BINARY_DIR}"
  95. OUTPUT_VARIABLE actual_stdout
  96. ERROR_VARIABLE ${actual_stderr_var}
  97. RESULT_VARIABLE actual_result
  98. ENCODING UTF8
  99. ${maybe_timeout}
  100. )
  101. endif()
  102. set(msg "")
  103. if(NOT "${actual_result}" MATCHES "${expect_result}")
  104. string(APPEND msg "Result is [${actual_result}], not [${expect_result}].\n")
  105. endif()
  106. string(CONCAT ignore_line_regex
  107. "(^|\n)((==[0-9]+=="
  108. "|BullseyeCoverage"
  109. "|[a-z]+\\([0-9]+\\) malloc:"
  110. "|clang[^:]*: warning: the object size sanitizer has no effect at -O0, but is explicitly enabled:"
  111. "|Error kstat returned"
  112. "|Hit xcodebuild bug"
  113. "|[^\n]*is a member of multiple groups"
  114. "|[^\n]*from Time Machine by path"
  115. "|[^\n]*Bullseye Testing Technology"
  116. ")[^\n]*\n)+"
  117. )
  118. foreach(o out err)
  119. string(REGEX REPLACE "\r\n" "\n" actual_std${o} "${actual_std${o}}")
  120. string(REGEX REPLACE "${ignore_line_regex}" "\\1" actual_std${o} "${actual_std${o}}")
  121. string(REGEX REPLACE "\n+$" "" actual_std${o} "${actual_std${o}}")
  122. set(expect_${o} "")
  123. if(DEFINED expect_std${o})
  124. if(NOT "${actual_std${o}}" MATCHES "${expect_std${o}}")
  125. string(REGEX REPLACE "\n" "\n expect-${o}> " expect_${o}
  126. " expect-${o}> ${expect_std${o}}")
  127. set(expect_${o} "Expected std${o} to match:\n${expect_${o}}\n")
  128. string(APPEND msg "std${o} does not match that expected.\n")
  129. endif()
  130. endif()
  131. endforeach()
  132. unset(RunCMake_TEST_FAILED)
  133. if(RunCMake-check-file AND EXISTS ${top_src}/${RunCMake-check-file})
  134. include(${top_src}/${RunCMake-check-file})
  135. else()
  136. include(${top_src}/${test}-check.cmake OPTIONAL)
  137. endif()
  138. if(RunCMake_TEST_FAILED)
  139. set(msg "${RunCMake_TEST_FAILED}\n${msg}")
  140. endif()
  141. if(msg AND RunCMake_TEST_COMMAND)
  142. string(REPLACE ";" "\" \"" command "\"${RunCMake_TEST_COMMAND}\"")
  143. string(APPEND msg "Command was:\n command> ${command}\n")
  144. endif()
  145. if(msg)
  146. string(REGEX REPLACE "\n" "\n actual-out> " actual_out " actual-out> ${actual_stdout}")
  147. string(REGEX REPLACE "\n" "\n actual-err> " actual_err " actual-err> ${actual_stderr}")
  148. message(SEND_ERROR "${test} - FAILED:\n"
  149. "${msg}"
  150. "${expect_out}"
  151. "Actual stdout:\n${actual_out}\n"
  152. "${expect_err}"
  153. "Actual stderr:\n${actual_err}\n"
  154. )
  155. else()
  156. message(STATUS "${test} - PASSED")
  157. endif()
  158. endfunction()
  159. function(run_cmake_command test)
  160. set(RunCMake_TEST_COMMAND "${ARGN}")
  161. run_cmake(${test})
  162. endfunction()
  163. # Protect RunCMake tests from calling environment.
  164. unset(ENV{MAKEFLAGS})