CMakeTestCXXCompiler.cmake 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. if(CMAKE_CXX_COMPILER_FORCED)
  4. # The compiler configuration was forced by the user.
  5. # Assume the user has configured all compiler information.
  6. set(CMAKE_CXX_COMPILER_WORKS TRUE)
  7. return()
  8. endif()
  9. include(CMakeTestCompilerCommon)
  10. # work around enforced code signing and / or missing executable target type
  11. set(__CMAKE_SAVED_TRY_COMPILE_TARGET_TYPE ${CMAKE_TRY_COMPILE_TARGET_TYPE})
  12. if(_CMAKE_FEATURE_DETECTION_TARGET_TYPE)
  13. set(CMAKE_TRY_COMPILE_TARGET_TYPE ${_CMAKE_FEATURE_DETECTION_TARGET_TYPE})
  14. endif()
  15. # Remove any cached result from an older CMake version.
  16. # We now store this in CMakeCXXCompiler.cmake.
  17. unset(CMAKE_CXX_COMPILER_WORKS CACHE)
  18. # Try to identify the ABI and configure it into CMakeCXXCompiler.cmake
  19. include(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerABI.cmake)
  20. CMAKE_DETERMINE_COMPILER_ABI(CXX ${CMAKE_ROOT}/Modules/CMakeCXXCompilerABI.cpp)
  21. if(CMAKE_CXX_ABI_COMPILED)
  22. # The compiler worked so skip dedicated test below.
  23. set(CMAKE_CXX_COMPILER_WORKS TRUE)
  24. message(STATUS "Check for working CXX compiler: ${CMAKE_CXX_COMPILER} - skipped")
  25. endif()
  26. # This file is used by EnableLanguage in cmGlobalGenerator to
  27. # determine that the selected C++ compiler can actually compile
  28. # and link the most basic of programs. If not, a fatal error
  29. # is set and cmake stops processing commands and will not generate
  30. # any makefiles or projects.
  31. if(NOT CMAKE_CXX_COMPILER_WORKS)
  32. PrintTestCompilerStatus("CXX")
  33. __TestCompiler_setTryCompileTargetType()
  34. file(WRITE ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testCXXCompiler.cxx
  35. "#ifndef __cplusplus\n"
  36. "# error \"The CMAKE_CXX_COMPILER is set to a C compiler\"\n"
  37. "#endif\n"
  38. "int main(){return 0;}\n")
  39. # Clear result from normal variable.
  40. unset(CMAKE_CXX_COMPILER_WORKS)
  41. # Puts test result in cache variable.
  42. try_compile(CMAKE_CXX_COMPILER_WORKS ${CMAKE_BINARY_DIR}
  43. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testCXXCompiler.cxx
  44. OUTPUT_VARIABLE __CMAKE_CXX_COMPILER_OUTPUT)
  45. # Move result from cache to normal variable.
  46. set(CMAKE_CXX_COMPILER_WORKS ${CMAKE_CXX_COMPILER_WORKS})
  47. unset(CMAKE_CXX_COMPILER_WORKS CACHE)
  48. __TestCompiler_restoreTryCompileTargetType()
  49. if(NOT CMAKE_CXX_COMPILER_WORKS)
  50. PrintTestCompilerResult(CHECK_FAIL "broken")
  51. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  52. "Determining if the CXX compiler works failed with "
  53. "the following output:\n${__CMAKE_CXX_COMPILER_OUTPUT}\n\n")
  54. string(REPLACE "\n" "\n " _output "${__CMAKE_CXX_COMPILER_OUTPUT}")
  55. message(FATAL_ERROR "The C++ compiler\n \"${CMAKE_CXX_COMPILER}\"\n"
  56. "is not able to compile a simple test program.\nIt fails "
  57. "with the following output:\n ${_output}\n\n"
  58. "CMake will not be able to correctly generate this project.")
  59. endif()
  60. PrintTestCompilerResult(CHECK_PASS "works")
  61. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  62. "Determining if the CXX compiler works passed with "
  63. "the following output:\n${__CMAKE_CXX_COMPILER_OUTPUT}\n\n")
  64. endif()
  65. # Try to identify the compiler features
  66. include(${CMAKE_ROOT}/Modules/CMakeDetermineCompileFeatures.cmake)
  67. CMAKE_DETERMINE_COMPILE_FEATURES(CXX)
  68. # Re-configure to save learned information.
  69. configure_file(
  70. ${CMAKE_ROOT}/Modules/CMakeCXXCompiler.cmake.in
  71. ${CMAKE_PLATFORM_INFO_DIR}/CMakeCXXCompiler.cmake
  72. @ONLY
  73. )
  74. include(${CMAKE_PLATFORM_INFO_DIR}/CMakeCXXCompiler.cmake)
  75. if(CMAKE_CXX_SIZEOF_DATA_PTR)
  76. foreach(f ${CMAKE_CXX_ABI_FILES})
  77. include(${f})
  78. endforeach()
  79. unset(CMAKE_CXX_ABI_FILES)
  80. endif()
  81. set(CMAKE_TRY_COMPILE_TARGET_TYPE ${__CMAKE_SAVED_TRY_COMPILE_TARGET_TYPE})
  82. unset(__CMAKE_SAVED_TRY_COMPILE_TARGET_TYPE)
  83. unset(__CMAKE_CXX_COMPILER_OUTPUT)