CMakeTestCCompiler.cmake 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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_C_COMPILER_FORCED)
  4. # The compiler configuration was forced by the user.
  5. # Assume the user has configured all compiler information.
  6. set(CMAKE_C_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 CMakeCCompiler.cmake.
  17. unset(CMAKE_C_COMPILER_WORKS CACHE)
  18. # Try to identify the ABI and configure it into CMakeCCompiler.cmake
  19. include(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerABI.cmake)
  20. CMAKE_DETERMINE_COMPILER_ABI(C ${CMAKE_ROOT}/Modules/CMakeCCompilerABI.c)
  21. if(CMAKE_C_ABI_COMPILED)
  22. # The compiler worked so skip dedicated test below.
  23. set(CMAKE_C_COMPILER_WORKS TRUE)
  24. message(STATUS "Check for working C compiler: ${CMAKE_C_COMPILER} - skipped")
  25. endif()
  26. # This file is used by EnableLanguage in cmGlobalGenerator to
  27. # determine that that 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_C_COMPILER_WORKS)
  32. PrintTestCompilerStatus("C")
  33. __TestCompiler_setTryCompileTargetType()
  34. file(WRITE ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testCCompiler.c
  35. "#ifdef __cplusplus\n"
  36. "# error \"The CMAKE_C_COMPILER is set to a C++ compiler\"\n"
  37. "#endif\n"
  38. "#if defined(__CLASSIC_C__)\n"
  39. "int main(argc, argv)\n"
  40. " int argc;\n"
  41. " char* argv[];\n"
  42. "#else\n"
  43. "int main(int argc, char* argv[])\n"
  44. "#endif\n"
  45. "{ (void)argv; return argc-1;}\n")
  46. # Clear result from normal variable.
  47. unset(CMAKE_C_COMPILER_WORKS)
  48. # Puts test result in cache variable.
  49. try_compile(CMAKE_C_COMPILER_WORKS ${CMAKE_BINARY_DIR}
  50. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testCCompiler.c
  51. OUTPUT_VARIABLE __CMAKE_C_COMPILER_OUTPUT)
  52. # Move result from cache to normal variable.
  53. set(CMAKE_C_COMPILER_WORKS ${CMAKE_C_COMPILER_WORKS})
  54. unset(CMAKE_C_COMPILER_WORKS CACHE)
  55. __TestCompiler_restoreTryCompileTargetType()
  56. if(NOT CMAKE_C_COMPILER_WORKS)
  57. PrintTestCompilerResult(CHECK_FAIL "broken")
  58. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  59. "Determining if the C compiler works failed with "
  60. "the following output:\n${__CMAKE_C_COMPILER_OUTPUT}\n\n")
  61. string(REPLACE "\n" "\n " _output "${__CMAKE_C_COMPILER_OUTPUT}")
  62. message(FATAL_ERROR "The C compiler\n \"${CMAKE_C_COMPILER}\"\n"
  63. "is not able to compile a simple test program.\nIt fails "
  64. "with the following output:\n ${_output}\n\n"
  65. "CMake will not be able to correctly generate this project.")
  66. endif()
  67. PrintTestCompilerResult(CHECK_PASS "works")
  68. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  69. "Determining if the C compiler works passed with "
  70. "the following output:\n${__CMAKE_C_COMPILER_OUTPUT}\n\n")
  71. endif()
  72. # Try to identify the compiler features
  73. include(${CMAKE_ROOT}/Modules/CMakeDetermineCompileFeatures.cmake)
  74. CMAKE_DETERMINE_COMPILE_FEATURES(C)
  75. # Re-configure to save learned information.
  76. configure_file(
  77. ${CMAKE_ROOT}/Modules/CMakeCCompiler.cmake.in
  78. ${CMAKE_PLATFORM_INFO_DIR}/CMakeCCompiler.cmake
  79. @ONLY
  80. )
  81. include(${CMAKE_PLATFORM_INFO_DIR}/CMakeCCompiler.cmake)
  82. if(CMAKE_C_SIZEOF_DATA_PTR)
  83. foreach(f ${CMAKE_C_ABI_FILES})
  84. include(${f})
  85. endforeach()
  86. unset(CMAKE_C_ABI_FILES)
  87. endif()
  88. set(CMAKE_TRY_COMPILE_TARGET_TYPE ${__CMAKE_SAVED_TRY_COMPILE_TARGET_TYPE})
  89. unset(__CMAKE_SAVED_TRY_COMPILE_TARGET_TYPE)
  90. unset(__CMAKE_C_COMPILER_OUTPUT)