RunCMake.cmake 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. execute_process(
  81. COMMAND ${CMAKE_COMMAND} "${RunCMake_TEST_SOURCE_DIR}"
  82. -G "${RunCMake_GENERATOR}"
  83. -A "${RunCMake_GENERATOR_PLATFORM}"
  84. -T "${RunCMake_GENERATOR_TOOLSET}"
  85. -DRunCMake_TEST=${test}
  86. --no-warn-unused-cli
  87. ${RunCMake_TEST_OPTIONS}
  88. WORKING_DIRECTORY "${RunCMake_TEST_BINARY_DIR}"
  89. OUTPUT_VARIABLE actual_stdout
  90. ERROR_VARIABLE ${actual_stderr_var}
  91. RESULT_VARIABLE actual_result
  92. ENCODING UTF8
  93. ${maybe_timeout}
  94. )
  95. endif()
  96. set(msg "")
  97. if(NOT "${actual_result}" MATCHES "${expect_result}")
  98. string(APPEND msg "Result is [${actual_result}], not [${expect_result}].\n")
  99. endif()
  100. foreach(o out err)
  101. string(REGEX REPLACE "\r\n" "\n" actual_std${o} "${actual_std${o}}")
  102. string(REGEX REPLACE "(^|\n)((==[0-9]+==|BullseyeCoverage|[a-z]+\\([0-9]+\\) malloc:|Error kstat returned|Hit xcodebuild bug|[^\n]*is a member of multiple groups|[^\n]*from Time Machine by path|[^\n]*Bullseye Testing Technology)[^\n]*\n)+" "\\1" actual_std${o} "${actual_std${o}}")
  103. string(REGEX REPLACE "\n+$" "" actual_std${o} "${actual_std${o}}")
  104. set(expect_${o} "")
  105. if(DEFINED expect_std${o})
  106. if(NOT "${actual_std${o}}" MATCHES "${expect_std${o}}")
  107. string(REGEX REPLACE "\n" "\n expect-${o}> " expect_${o}
  108. " expect-${o}> ${expect_std${o}}")
  109. set(expect_${o} "Expected std${o} to match:\n${expect_${o}}\n")
  110. string(APPEND msg "std${o} does not match that expected.\n")
  111. endif()
  112. endif()
  113. endforeach()
  114. unset(RunCMake_TEST_FAILED)
  115. if(RunCMake-check-file AND EXISTS ${top_src}/${RunCMake-check-file})
  116. include(${top_src}/${RunCMake-check-file})
  117. else()
  118. include(${top_src}/${test}-check.cmake OPTIONAL)
  119. endif()
  120. if(RunCMake_TEST_FAILED)
  121. set(msg "${RunCMake_TEST_FAILED}\n${msg}")
  122. endif()
  123. if(msg AND RunCMake_TEST_COMMAND)
  124. string(REPLACE ";" "\" \"" command "\"${RunCMake_TEST_COMMAND}\"")
  125. string(APPEND msg "Command was:\n command> ${command}\n")
  126. endif()
  127. if(msg)
  128. string(REGEX REPLACE "\n" "\n actual-out> " actual_out " actual-out> ${actual_stdout}")
  129. string(REGEX REPLACE "\n" "\n actual-err> " actual_err " actual-err> ${actual_stderr}")
  130. message(SEND_ERROR "${test} - FAILED:\n"
  131. "${msg}"
  132. "${expect_out}"
  133. "Actual stdout:\n${actual_out}\n"
  134. "${expect_err}"
  135. "Actual stderr:\n${actual_err}\n"
  136. )
  137. else()
  138. message(STATUS "${test} - PASSED")
  139. endif()
  140. endfunction()
  141. function(run_cmake_command test)
  142. set(RunCMake_TEST_COMMAND "${ARGN}")
  143. run_cmake(${test})
  144. endfunction()
  145. # Protect RunCMake tests from calling environment.
  146. unset(ENV{MAKEFLAGS})