CMakeDetermineCCompiler.cmake 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # determine the compiler to use for C programs
  2. # NOTE, a generator may set CMAKE_C_COMPILER before
  3. # loading this file to force a compiler.
  4. # use environment variable CC first if defined by user, next use
  5. # the cmake variable CMAKE_GENERATOR_CC which can be defined by a generator
  6. # as a default compiler
  7. # If the internal cmake variable _CMAKE_TOOLCHAIN_PREFIX is set, this is used
  8. # as prefix for the tools (e.g. arm-elf-gcc, arm-elf-ar etc.). This works
  9. # currently with the GNU crosscompilers.
  10. #
  11. # Sets the following variables:
  12. # CMAKE_C_COMPILER
  13. # CMAKE_AR
  14. # CMAKE_RANLIB
  15. # CMAKE_COMPILER_IS_GNUCC
  16. #
  17. # If not already set before, it also sets
  18. # _CMAKE_TOOLCHAIN_PREFIX
  19. IF(NOT CMAKE_C_COMPILER)
  20. SET(CMAKE_CXX_COMPILER_INIT NOTFOUND)
  21. # prefer the environment variable CC
  22. IF($ENV{CC} MATCHES ".+")
  23. GET_FILENAME_COMPONENT(CMAKE_C_COMPILER_INIT $ENV{CC} PROGRAM PROGRAM_ARGS CMAKE_C_FLAGS_ENV_INIT)
  24. IF(CMAKE_C_FLAGS_ENV_INIT)
  25. SET(CMAKE_C_COMPILER_ARG1 "${CMAKE_C_FLAGS_ENV_INIT}" CACHE STRING "First argument to C compiler")
  26. ENDIF(CMAKE_C_FLAGS_ENV_INIT)
  27. IF(NOT EXISTS ${CMAKE_C_COMPILER_INIT})
  28. MESSAGE(FATAL_ERROR "Could not find compiler set in environment variable CC:\n$ENV{CC}.")
  29. ENDIF(NOT EXISTS ${CMAKE_C_COMPILER_INIT})
  30. ENDIF($ENV{CC} MATCHES ".+")
  31. # next try prefer the compiler specified by the generator
  32. IF(CMAKE_GENERATOR_CC)
  33. IF(NOT CMAKE_C_COMPILER_INIT)
  34. SET(CMAKE_C_COMPILER_INIT ${CMAKE_GENERATOR_CC})
  35. ENDIF(NOT CMAKE_C_COMPILER_INIT)
  36. ENDIF(CMAKE_GENERATOR_CC)
  37. # finally list compilers to try
  38. IF(CMAKE_C_COMPILER_INIT)
  39. SET(CMAKE_C_COMPILER_LIST ${CMAKE_C_COMPILER_INIT})
  40. ELSE(CMAKE_C_COMPILER_INIT)
  41. SET(CMAKE_C_COMPILER_LIST ${_CMAKE_TOOLCHAIN_PREFIX}gcc ${_CMAKE_TOOLCHAIN_PREFIX}cc cl bcc xlc)
  42. ENDIF(CMAKE_C_COMPILER_INIT)
  43. # Find the compiler.
  44. IF (_CMAKE_USER_CXX_COMPILER_PATH)
  45. FIND_PROGRAM(CMAKE_C_COMPILER NAMES ${CMAKE_C_COMPILER_LIST} PATHS ${_CMAKE_USER_CXX_COMPILER_PATH} DOC "C compiler" NO_DEFAULT_PATH)
  46. ENDIF (_CMAKE_USER_CXX_COMPILER_PATH)
  47. FIND_PROGRAM(CMAKE_C_COMPILER NAMES ${CMAKE_C_COMPILER_LIST} DOC "C compiler")
  48. IF(CMAKE_C_COMPILER_INIT AND NOT CMAKE_C_COMPILER)
  49. SET(CMAKE_C_COMPILER "${CMAKE_C_COMPILER_INIT}" CACHE FILEPATH "C compiler" FORCE)
  50. ENDIF(CMAKE_C_COMPILER_INIT AND NOT CMAKE_C_COMPILER)
  51. ELSE(NOT CMAKE_C_COMPILER)
  52. # we only get here if CMAKE_C_COMPILER was specified using -D or a pre-made CMakeCache.txt
  53. # (e.g. via ctest) or set in CMAKE_TOOLCHAIN_FILE
  54. # if CMAKE_C_COMPILER is a list of length 2, use the first item as
  55. # CMAKE_C_COMPILER and the 2nd one as CMAKE_C_COMPILER_ARG1
  56. LIST(LENGTH CMAKE_C_COMPILER _CMAKE_C_COMPILER_LIST_LENGTH)
  57. IF("${_CMAKE_C_COMPILER_LIST_LENGTH}" EQUAL 2)
  58. LIST(GET CMAKE_C_COMPILER 1 CMAKE_C_COMPILER_ARG1)
  59. LIST(GET CMAKE_C_COMPILER 0 CMAKE_C_COMPILER)
  60. ENDIF("${_CMAKE_C_COMPILER_LIST_LENGTH}" EQUAL 2)
  61. # if a compiler was specified by the user but without path,
  62. # now try to find it with the full path
  63. # if it is found, force it into the cache,
  64. # if not, don't overwrite the setting (which was given by the user) with "NOTFOUND"
  65. # if the C compiler already had a path, reuse it for searching the CXX compiler
  66. GET_FILENAME_COMPONENT(_CMAKE_USER_C_COMPILER_PATH "${CMAKE_C_COMPILER}" PATH)
  67. IF(NOT _CMAKE_USER_C_COMPILER_PATH)
  68. FIND_PROGRAM(CMAKE_C_COMPILER_WITH_PATH NAMES ${CMAKE_C_COMPILER})
  69. MARK_AS_ADVANCED(CMAKE_C_COMPILER_WITH_PATH)
  70. IF(CMAKE_C_COMPILER_WITH_PATH)
  71. SET(CMAKE_C_COMPILER ${CMAKE_C_COMPILER_WITH_PATH} CACHE STRING "C compiler" FORCE)
  72. ENDIF(CMAKE_C_COMPILER_WITH_PATH)
  73. ENDIF(NOT _CMAKE_USER_C_COMPILER_PATH)
  74. ENDIF(NOT CMAKE_C_COMPILER)
  75. MARK_AS_ADVANCED(CMAKE_C_COMPILER)
  76. IF (NOT _CMAKE_TOOLCHAIN_LOCATION)
  77. GET_FILENAME_COMPONENT(_CMAKE_TOOLCHAIN_LOCATION "${CMAKE_C_COMPILER}" PATH)
  78. ENDIF (NOT _CMAKE_TOOLCHAIN_LOCATION)
  79. # if we have a gcc cross compiler, they have usually some prefix, like
  80. # e.g. powerpc-linux-gcc, arm-elf-gcc or i586-mingw32msvc-gcc
  81. # the other tools of the toolchain usually have the same prefix
  82. IF (NOT _CMAKE_TOOLCHAIN_PREFIX)
  83. GET_FILENAME_COMPONENT(COMPILER_BASENAME "${CMAKE_C_COMPILER}" NAME_WE)
  84. IF (COMPILER_BASENAME MATCHES "^(.+-)g?cc")
  85. STRING(REGEX REPLACE "^(.+-)g?cc" "\\1" _CMAKE_TOOLCHAIN_PREFIX "${COMPILER_BASENAME}")
  86. ENDIF (COMPILER_BASENAME MATCHES "^(.+-)g?cc")
  87. ENDIF (NOT _CMAKE_TOOLCHAIN_PREFIX)
  88. # Build a small source file to identify the compiler.
  89. IF(${CMAKE_GENERATOR} MATCHES "Visual Studio")
  90. SET(CMAKE_C_COMPILER_ID_RUN 1)
  91. SET(CMAKE_C_PLATFORM_ID "Windows")
  92. # TODO: Set the compiler id. It is probably MSVC but
  93. # the user may be using an integrated Intel compiler.
  94. # SET(CMAKE_C_COMPILER_ID "MSVC")
  95. ENDIF(${CMAKE_GENERATOR} MATCHES "Visual Studio")
  96. IF(NOT CMAKE_C_COMPILER_ID_RUN)
  97. SET(CMAKE_C_COMPILER_ID_RUN 1)
  98. # Each entry in this list is a set of extra flags to try
  99. # adding to the compile line to see if it helps produce
  100. # a valid identification file.
  101. SET(CMAKE_C_COMPILER_ID_TEST_FLAGS
  102. # Try compiling to an object file only.
  103. "-c"
  104. )
  105. # Try to identify the compiler.
  106. SET(CMAKE_C_COMPILER_ID)
  107. FILE(READ ${CMAKE_ROOT}/Modules/CMakePlatformId.h.in
  108. CMAKE_C_COMPILER_ID_PLATFORM_CONTENT)
  109. INCLUDE(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerId.cmake)
  110. CMAKE_DETERMINE_COMPILER_ID(C CFLAGS CMakeCCompilerId.c)
  111. # Set old compiler and platform id variables.
  112. IF("${CMAKE_C_COMPILER_ID}" MATCHES "GNU")
  113. SET(CMAKE_COMPILER_IS_GNUCC 1)
  114. ENDIF("${CMAKE_C_COMPILER_ID}" MATCHES "GNU")
  115. IF("${CMAKE_C_PLATFORM_ID}" MATCHES "MinGW")
  116. SET(CMAKE_COMPILER_IS_MINGW 1)
  117. ELSEIF("${CMAKE_C_PLATFORM_ID}" MATCHES "Cygwin")
  118. SET(CMAKE_COMPILER_IS_CYGWIN 1)
  119. ENDIF("${CMAKE_C_PLATFORM_ID}" MATCHES "MinGW")
  120. ENDIF(NOT CMAKE_C_COMPILER_ID_RUN)
  121. INCLUDE(CMakeFindBinUtils)
  122. # configure variables set in this file for fast reload later on
  123. CONFIGURE_FILE(${CMAKE_ROOT}/Modules/CMakeCCompiler.cmake.in
  124. "${CMAKE_PLATFORM_ROOT_BIN}/CMakeCCompiler.cmake"
  125. @ONLY IMMEDIATE # IMMEDIATE must be here for compatibility mode <= 2.0
  126. )
  127. SET(CMAKE_C_COMPILER_ENV_VAR "CC")