RunCMake.cmake 5.7 KB

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