RunCMakeTest.cmake 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. include(RunCMake)
  2. # Detect the compiler in use in the current environment.
  3. run_cmake(FindCompiler)
  4. include(${RunCMake_BINARY_DIR}/FindCompiler-build/cc.cmake)
  5. if(NOT CMAKE_C_COMPILER)
  6. message(FATAL_ERROR "FindCompiler provided no compiler!")
  7. endif()
  8. if(NOT IS_ABSOLUTE "${CMAKE_C_COMPILER}")
  9. message(FATAL_ERROR "FindCompiler provided non-absolute path \"${CMAKE_C_COMPILER}\"!")
  10. endif()
  11. if(NOT EXISTS "${CMAKE_C_COMPILER}")
  12. message(FATAL_ERROR "FindCompiler provided non-existing path \"${CMAKE_C_COMPILER}\"!")
  13. endif()
  14. # Now that we have the full compiler path, hide CC.
  15. unset(ENV{CC})
  16. # Wrap around the real compiler so we can change the compiler
  17. # path without changing the underlying compiler.
  18. set(ccIn ${RunCMake_SOURCE_DIR}/cc.sh.in)
  19. set(cc1 ${RunCMake_BINARY_DIR}/cc1.sh)
  20. set(cc2 ${RunCMake_BINARY_DIR}/cc2.sh)
  21. set(cc3 CMAKE_C_COMPILER-NOTFOUND)
  22. configure_file(${ccIn} ${cc1} @ONLY)
  23. configure_file(${ccIn} ${cc2} @ONLY)
  24. # Use a single build tree for remaining tests without cleaning.
  25. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/ChangeCompiler-build)
  26. set(RunCMake_TEST_NO_CLEAN 1)
  27. file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
  28. # Check build with compiler wrapper 1.
  29. set(RunCMake_TEST_OPTIONS -DCMAKE_C_COMPILER=${cc1})
  30. set(ENV{RunCMake_TEST} "FirstCompiler")
  31. run_cmake(FirstCompiler)
  32. include(${RunCMake_TEST_BINARY_DIR}/cc.cmake)
  33. if(NOT "${CMAKE_C_COMPILER}" STREQUAL "${cc1}")
  34. message(FATAL_ERROR "FirstCompiler built with compiler:\n ${CMAKE_C_COMPILER}\nand not with:\n ${cc1}")
  35. endif()
  36. # Check rebuild with compiler wrapper 2.
  37. set(RunCMake_TEST_OPTIONS -DCMAKE_C_COMPILER=${cc2})
  38. set(ENV{RunCMake_TEST} "SecondCompiler")
  39. run_cmake(SecondCompiler)
  40. include(${RunCMake_TEST_BINARY_DIR}/cc.cmake)
  41. if(NOT "${CMAKE_C_COMPILER}" STREQUAL "${cc2}")
  42. message(FATAL_ERROR "SecondCompiler built with compiler:\n ${CMAKE_C_COMPILER}\nand not with:\n ${cc2}")
  43. endif()
  44. # Check failure with an empty compiler string.
  45. set(RunCMake_TEST_OPTIONS -DCMAKE_C_COMPILER=)
  46. set(ENV{RunCMake_TEST} "EmptyCompiler")
  47. run_cmake(EmptyCompiler)
  48. include(${RunCMake_TEST_BINARY_DIR}/cc.cmake)
  49. if(NOT "${CMAKE_C_COMPILER}" STREQUAL "${cc3}")
  50. message(FATAL_ERROR "Empty built with compiler:\n ${CMAKE_C_COMPILER}\nand not with:\n ${cc3}")
  51. endif()