CMakeTestCCompiler.cmake 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 exectuable 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. # This file is used by EnableLanguage in cmGlobalGenerator to
  19. # determine that that selected C compiler can actually compile
  20. # and link the most basic of programs. If not, a fatal error
  21. # is set and cmake stops processing commands and will not generate
  22. # any makefiles or projects.
  23. if(NOT CMAKE_C_COMPILER_WORKS)
  24. PrintTestCompilerStatus("C" "")
  25. __TestCompiler_setTryCompileTargetType()
  26. file(WRITE ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testCCompiler.c
  27. "#ifdef __cplusplus\n"
  28. "# error \"The CMAKE_C_COMPILER is set to a C++ compiler\"\n"
  29. "#endif\n"
  30. "#if defined(__CLASSIC_C__)\n"
  31. "int main(argc, argv)\n"
  32. " int argc;\n"
  33. " char* argv[];\n"
  34. "#else\n"
  35. "int main(int argc, char* argv[])\n"
  36. "#endif\n"
  37. "{ (void)argv; return argc-1;}\n")
  38. try_compile(CMAKE_C_COMPILER_WORKS ${CMAKE_BINARY_DIR}
  39. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testCCompiler.c
  40. OUTPUT_VARIABLE __CMAKE_C_COMPILER_OUTPUT)
  41. # Move result from cache to normal variable.
  42. set(CMAKE_C_COMPILER_WORKS ${CMAKE_C_COMPILER_WORKS})
  43. unset(CMAKE_C_COMPILER_WORKS CACHE)
  44. set(C_TEST_WAS_RUN 1)
  45. __TestCompiler_restoreTryCompileTargetType()
  46. endif()
  47. if(NOT CMAKE_C_COMPILER_WORKS)
  48. PrintTestCompilerStatus("C" " -- broken")
  49. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  50. "Determining if the C compiler works failed with "
  51. "the following output:\n${__CMAKE_C_COMPILER_OUTPUT}\n\n")
  52. string(REPLACE "\n" "\n " _output "${__CMAKE_C_COMPILER_OUTPUT}")
  53. message(FATAL_ERROR "The C compiler\n \"${CMAKE_C_COMPILER}\"\n"
  54. "is not able to compile a simple test program.\nIt fails "
  55. "with the following output:\n ${_output}\n\n"
  56. "CMake will not be able to correctly generate this project.")
  57. else()
  58. if(C_TEST_WAS_RUN)
  59. PrintTestCompilerStatus("C" " -- works")
  60. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  61. "Determining if the C compiler works passed with "
  62. "the following output:\n${__CMAKE_C_COMPILER_OUTPUT}\n\n")
  63. endif()
  64. # Try to identify the ABI and configure it into CMakeCCompiler.cmake
  65. include(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerABI.cmake)
  66. CMAKE_DETERMINE_COMPILER_ABI(C ${CMAKE_ROOT}/Modules/CMakeCCompilerABI.c)
  67. # Try to identify the compiler features
  68. include(${CMAKE_ROOT}/Modules/CMakeDetermineCompileFeatures.cmake)
  69. CMAKE_DETERMINE_COMPILE_FEATURES(C)
  70. # Re-configure to save learned information.
  71. configure_file(
  72. ${CMAKE_ROOT}/Modules/CMakeCCompiler.cmake.in
  73. ${CMAKE_PLATFORM_INFO_DIR}/CMakeCCompiler.cmake
  74. @ONLY
  75. )
  76. include(${CMAKE_PLATFORM_INFO_DIR}/CMakeCCompiler.cmake)
  77. if(CMAKE_C_SIZEOF_DATA_PTR)
  78. foreach(f ${CMAKE_C_ABI_FILES})
  79. include(${f})
  80. endforeach()
  81. unset(CMAKE_C_ABI_FILES)
  82. endif()
  83. endif()
  84. set(CMAKE_TRY_COMPILE_TARGET_TYPE ${__CMAKE_SAVED_TRY_COMPILE_TARGET_TYPE})
  85. unset(__CMAKE_SAVED_TRY_COMPILE_TARGET_TYPE)
  86. unset(__CMAKE_C_COMPILER_OUTPUT)