CMakeTestCCompiler.cmake 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #=============================================================================
  2. # Copyright 2003-2012 Kitware, Inc.
  3. #
  4. # Distributed under the OSI-approved BSD License (the "License");
  5. # see accompanying file Copyright.txt for details.
  6. #
  7. # This software is distributed WITHOUT ANY WARRANTY; without even the
  8. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. # See the License for more information.
  10. #=============================================================================
  11. # (To distribute this file outside of CMake, substitute the full
  12. # License text for the above reference.)
  13. if(CMAKE_C_COMPILER_FORCED)
  14. # The compiler configuration was forced by the user.
  15. # Assume the user has configured all compiler information.
  16. set(CMAKE_C_COMPILER_WORKS TRUE)
  17. return()
  18. endif()
  19. include(CMakeTestCompilerCommon)
  20. # Remove any cached result from an older CMake version.
  21. # We now store this in CMakeCCompiler.cmake.
  22. unset(CMAKE_C_COMPILER_WORKS CACHE)
  23. # This file is used by EnableLanguage in cmGlobalGenerator to
  24. # determine that that selected C compiler can actually compile
  25. # and link the most basic of programs. If not, a fatal error
  26. # is set and cmake stops processing commands and will not generate
  27. # any makefiles or projects.
  28. if(NOT CMAKE_C_COMPILER_WORKS)
  29. PrintTestCompilerStatus("C" "")
  30. file(WRITE ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testCCompiler.c
  31. "#ifdef __cplusplus\n"
  32. "# error \"The CMAKE_C_COMPILER is set to a C++ compiler\"\n"
  33. "#endif\n"
  34. "#if defined(__CLASSIC_C__)\n"
  35. "int main(argc, argv)\n"
  36. " int argc;\n"
  37. " char* argv[];\n"
  38. "#else\n"
  39. "int main(int argc, char* argv[])\n"
  40. "#endif\n"
  41. "{ (void)argv; return argc-1;}\n")
  42. try_compile(CMAKE_C_COMPILER_WORKS ${CMAKE_BINARY_DIR}
  43. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testCCompiler.c
  44. OUTPUT_VARIABLE __CMAKE_C_COMPILER_OUTPUT)
  45. # Move result from cache to normal variable.
  46. set(CMAKE_C_COMPILER_WORKS ${CMAKE_C_COMPILER_WORKS})
  47. unset(CMAKE_C_COMPILER_WORKS CACHE)
  48. set(C_TEST_WAS_RUN 1)
  49. endif()
  50. if(NOT CMAKE_C_COMPILER_WORKS)
  51. PrintTestCompilerStatus("C" " -- broken")
  52. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  53. "Determining if the C compiler works failed with "
  54. "the following output:\n${__CMAKE_C_COMPILER_OUTPUT}\n\n")
  55. message(FATAL_ERROR "The C compiler \"${CMAKE_C_COMPILER}\" "
  56. "is not able to compile a simple test program.\nIt fails "
  57. "with the following output:\n ${__CMAKE_C_COMPILER_OUTPUT}\n\n"
  58. "CMake will not be able to correctly generate this project.")
  59. else()
  60. if(C_TEST_WAS_RUN)
  61. PrintTestCompilerStatus("C" " -- works")
  62. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  63. "Determining if the C compiler works passed with "
  64. "the following output:\n${__CMAKE_C_COMPILER_OUTPUT}\n\n")
  65. endif()
  66. # Try to identify the ABI and configure it into CMakeCCompiler.cmake
  67. include(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerABI.cmake)
  68. CMAKE_DETERMINE_COMPILER_ABI(C ${CMAKE_ROOT}/Modules/CMakeCCompilerABI.c)
  69. # Try to identify the compiler features
  70. include(${CMAKE_ROOT}/Modules/CMakeDetermineCompileFeatures.cmake)
  71. CMAKE_DETERMINE_COMPILE_FEATURES(C)
  72. # Re-configure to save learned information.
  73. configure_file(
  74. ${CMAKE_ROOT}/Modules/CMakeCCompiler.cmake.in
  75. ${CMAKE_PLATFORM_INFO_DIR}/CMakeCCompiler.cmake
  76. @ONLY
  77. )
  78. include(${CMAKE_PLATFORM_INFO_DIR}/CMakeCCompiler.cmake)
  79. if(CMAKE_C_SIZEOF_DATA_PTR)
  80. foreach(f ${CMAKE_C_ABI_FILES})
  81. include(${f})
  82. endforeach()
  83. unset(CMAKE_C_ABI_FILES)
  84. endif()
  85. endif()
  86. unset(__CMAKE_C_COMPILER_OUTPUT)