CMakeTestCCompiler.cmake 3.3 KB

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