1
0

RunCMake.cmake 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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(EXISTS ${top_src}/${test}-std${o}.txt)
  21. file(READ ${top_src}/${test}-std${o}.txt expect_std${o})
  22. string(REGEX REPLACE "\n+$" "" expect_std${o} "${expect_std${o}}")
  23. else()
  24. unset(expect_std${o})
  25. endif()
  26. endforeach()
  27. set(RunCMake_TEST_SOURCE_DIR "${top_src}")
  28. if(NOT RunCMake_TEST_BINARY_DIR)
  29. set(RunCMake_TEST_BINARY_DIR "${top_bin}/${test}-build")
  30. endif()
  31. if(NOT RunCMake_TEST_NO_CLEAN)
  32. file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
  33. endif()
  34. file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
  35. if(NOT DEFINED RunCMake_TEST_OPTIONS)
  36. set(RunCMake_TEST_OPTIONS "")
  37. endif()
  38. if(APPLE)
  39. list(APPEND RunCMake_TEST_OPTIONS -DCMAKE_POLICY_DEFAULT_CMP0025=NEW)
  40. endif()
  41. execute_process(
  42. COMMAND ${CMAKE_COMMAND} "${RunCMake_TEST_SOURCE_DIR}"
  43. -G "${RunCMake_GENERATOR}"
  44. -T "${RunCMake_GENERATOR_TOOLSET}"
  45. -DRunCMake_TEST=${test}
  46. ${RunCMake_TEST_OPTIONS}
  47. WORKING_DIRECTORY "${RunCMake_TEST_BINARY_DIR}"
  48. OUTPUT_VARIABLE actual_stdout
  49. ERROR_VARIABLE actual_stderr
  50. RESULT_VARIABLE actual_result
  51. )
  52. set(msg "")
  53. if(NOT "${actual_result}" STREQUAL "${expect_result}")
  54. set(msg "${msg}Result is [${actual_result}], not [${expect_result}].\n")
  55. endif()
  56. foreach(o out err)
  57. string(REGEX REPLACE "(^|\n)(==[0-9]+==[^\n]*\n)+" "\\1" actual_std${o} "${actual_std${o}}")
  58. string(REGEX REPLACE "\n+$" "" actual_std${o} "${actual_std${o}}")
  59. set(expect_${o} "")
  60. if(DEFINED expect_std${o})
  61. if(NOT "${actual_std${o}}" MATCHES "${expect_std${o}}")
  62. string(REGEX REPLACE "\n" "\n expect-${o}> " expect_${o}
  63. " expect-${o}> ${expect_std${o}}")
  64. set(expect_${o} "Expected std${o} to match:\n${expect_${o}}\n")
  65. set(msg "${msg}std${o} does not match that expected.\n")
  66. endif()
  67. endif()
  68. endforeach()
  69. unset(RunCMake_TEST_FAILED)
  70. include(${top_src}/${test}-check.cmake OPTIONAL)
  71. if(RunCMake_TEST_FAILED)
  72. set(msg "${RunCMake_TEST_FAILED}\n${msg}")
  73. endif()
  74. if(msg)
  75. string(REGEX REPLACE "\n" "\n actual-out> " actual_out " actual-out> ${actual_stdout}")
  76. string(REGEX REPLACE "\n" "\n actual-err> " actual_err " actual-err> ${actual_stderr}")
  77. message(SEND_ERROR "${test} - FAILED:\n"
  78. "${msg}"
  79. "${expect_out}"
  80. "Actual stdout:\n${actual_out}\n"
  81. "${expect_err}"
  82. "Actual stderr:\n${actual_err}\n"
  83. )
  84. else()
  85. message(STATUS "${test} - PASSED")
  86. endif()
  87. endfunction()