CMakeBuildTest.cmake.in 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. # run the executable out of the Debug directory if using a
  29. # multi-config generator
  30. set(_isMultiConfig @_isMultiConfig@)
  31. if(_isMultiConfig)
  32. set(RUN_TEST "@CMAKE_BUILD_TEST_BINARY_DIR@/Debug/@CMAKE_BUILD_TEST_EXE@")
  33. else()
  34. set(RUN_TEST "@CMAKE_BUILD_TEST_BINARY_DIR@/@CMAKE_BUILD_TEST_EXE@")
  35. endif()
  36. # run the test results
  37. message("running [${RUN_TEST}]")
  38. execute_process(COMMAND "${RUN_TEST}" RESULT_VARIABLE RESULT)
  39. if(RESULT)
  40. message(FATAL_ERROR "Error running test @CMAKE_BUILD_TEST_EXE@")
  41. endif()
  42. # build it again with clean and only @CMAKE_BUILD_TEST_EXE@ target
  43. execute_process(COMMAND "${CMAKE_COMMAND}"
  44. --build "@CMAKE_BUILD_TEST_BINARY_DIR@" --config Debug
  45. --clean-first --target @CMAKE_BUILD_TEST_EXE@
  46. RESULT_VARIABLE RESULT)
  47. if(RESULT)
  48. message(FATAL_ERROR "Error running cmake --build")
  49. endif()
  50. # run it again after clean
  51. execute_process(COMMAND "${RUN_TEST}" RESULT_VARIABLE RESULT)
  52. if(RESULT)
  53. message(FATAL_ERROR "Error running test @CMAKE_BUILD_TEST_EXE@ after clean ")
  54. endif()