CMakeBuildTest.cmake.in 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # create the binary directory
  2. make_directory("@CMAKE_BUILD_TEST_BINARY_DIR@")
  3. # remove the CMakeCache.txt file from the source dir
  4. # if there is one, so that in-source cmake tests
  5. # still pass
  6. message("Remove: @CMAKE_BUILD_TEST_SOURCE_DIR@/CMakeCache.txt")
  7. file(REMOVE "@CMAKE_BUILD_TEST_SOURCE_DIR@/CMakeCache.txt")
  8. # run cmake in the binary directory
  9. message("running: ${CMAKE_COMMAND}")
  10. execute_process(COMMAND "${CMAKE_COMMAND}"
  11. "@CMAKE_BUILD_TEST_SOURCE_DIR@"
  12. "-G@CMAKE_GENERATOR@"
  13. -A "@CMAKE_GENERATOR_PLATFORM@"
  14. -T "@CMAKE_GENERATOR_TOOLSET@"
  15. WORKING_DIRECTORY "@CMAKE_BUILD_TEST_BINARY_DIR@"
  16. RESULT_VARIABLE RESULT)
  17. if(RESULT)
  18. message(FATAL_ERROR "Error running cmake command")
  19. endif()
  20. # Now use the --build option to build the project
  21. message("running: ${CMAKE_COMMAND} --build")
  22. execute_process(COMMAND "${CMAKE_COMMAND}"
  23. --build "@CMAKE_BUILD_TEST_BINARY_DIR@" --config Debug
  24. RESULT_VARIABLE RESULT)
  25. if(RESULT)
  26. message(FATAL_ERROR "Error running cmake --build")
  27. endif()
  28. # check for configuration types
  29. set(CMAKE_CONFIGURATION_TYPES @CMAKE_CONFIGURATION_TYPES@)
  30. # run the executable out of the Debug directory if there
  31. # are configuration types
  32. if(CMAKE_CONFIGURATION_TYPES)
  33. set(RUN_TEST "@CMAKE_BUILD_TEST_BINARY_DIR@/Debug/@CMAKE_BUILD_TEST_EXE@")
  34. else()
  35. set(RUN_TEST "@CMAKE_BUILD_TEST_BINARY_DIR@/@CMAKE_BUILD_TEST_EXE@")
  36. endif()
  37. # run the test results
  38. message("running [${RUN_TEST}]")
  39. execute_process(COMMAND "${RUN_TEST}" RESULT_VARIABLE RESULT)
  40. if(RESULT)
  41. message(FATAL_ERROR "Error running test @CMAKE_BUILD_TEST_EXE@")
  42. endif()
  43. # build it again with clean and only @CMAKE_BUILD_TEST_EXE@ target
  44. execute_process(COMMAND "${CMAKE_COMMAND}"
  45. --build "@CMAKE_BUILD_TEST_BINARY_DIR@" --config Debug
  46. --clean-first --target @CMAKE_BUILD_TEST_EXE@
  47. RESULT_VARIABLE RESULT)
  48. if(RESULT)
  49. message(FATAL_ERROR "Error running cmake --build")
  50. endif()
  51. # run it again after clean
  52. execute_process(COMMAND "${RUN_TEST}" RESULT_VARIABLE RESULT)
  53. if(RESULT)
  54. message(FATAL_ERROR "Error running test @CMAKE_BUILD_TEST_EXE@ after clean ")
  55. endif()