CMakeDetermineCCompiler.cmake 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. # Build a small source file to identify the compiler.
  80. IF(${CMAKE_GENERATOR} MATCHES "Visual Studio")
  81. SET(CMAKE_C_COMPILER_ID_RUN 1)
  82. SET(CMAKE_C_PLATFORM_ID "Windows")
  83. # TODO: Set the compiler id. It is probably MSVC but
  84. # the user may be using an integrated Intel compiler.
  85. # SET(CMAKE_C_COMPILER_ID "MSVC")
  86. ENDIF(${CMAKE_GENERATOR} MATCHES "Visual Studio")
  87. IF(NOT CMAKE_C_COMPILER_ID_RUN)
  88. SET(CMAKE_C_COMPILER_ID_RUN 1)
  89. # Each entry in this list is a set of extra flags to try
  90. # adding to the compile line to see if it helps produce
  91. # a valid identification file.
  92. SET(CMAKE_C_COMPILER_ID_TEST_FLAGS
  93. # Try compiling to an object file only.
  94. "-c"
  95. # Try enabling ANSI mode on HP.
  96. "-Aa"
  97. )
  98. # Try to identify the compiler.
  99. SET(CMAKE_C_COMPILER_ID)
  100. FILE(READ ${CMAKE_ROOT}/Modules/CMakePlatformId.h.in
  101. CMAKE_C_COMPILER_ID_PLATFORM_CONTENT)
  102. INCLUDE(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerId.cmake)
  103. CMAKE_DETERMINE_COMPILER_ID(C CFLAGS CMakeCCompilerId.c)
  104. # Set old compiler and platform id variables.
  105. IF("${CMAKE_C_COMPILER_ID}" MATCHES "GNU")
  106. SET(CMAKE_COMPILER_IS_GNUCC 1)
  107. ENDIF("${CMAKE_C_COMPILER_ID}" MATCHES "GNU")
  108. IF("${CMAKE_C_PLATFORM_ID}" MATCHES "MinGW")
  109. SET(CMAKE_COMPILER_IS_MINGW 1)
  110. ELSEIF("${CMAKE_C_PLATFORM_ID}" MATCHES "Cygwin")
  111. SET(CMAKE_COMPILER_IS_CYGWIN 1)
  112. ENDIF("${CMAKE_C_PLATFORM_ID}" MATCHES "MinGW")
  113. ENDIF(NOT CMAKE_C_COMPILER_ID_RUN)
  114. # If we have a gcc cross compiler, they have usually some prefix, like
  115. # e.g. powerpc-linux-gcc, arm-elf-gcc or i586-mingw32msvc-gcc .
  116. # The other tools of the toolchain usually have the same prefix
  117. # NAME_WE cannot be used since then this test will fail for names lile
  118. # "arm-unknown-nto-qnx6.3.0-gcc.exe", where BASENAME would be
  119. # "arm-unknown-nto-qnx6" instead of the correct "arm-unknown-nto-qnx6.3.0-"
  120. IF (CMAKE_CROSSCOMPILING
  121. AND "${CMAKE_C_COMPILER_ID}" MATCHES "GNU"
  122. AND NOT _CMAKE_TOOLCHAIN_PREFIX)
  123. GET_FILENAME_COMPONENT(COMPILER_BASENAME "${CMAKE_C_COMPILER}" NAME)
  124. IF (COMPILER_BASENAME MATCHES "^(.+-)g?cc(\\.exe)?$")
  125. SET(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
  126. ENDIF (COMPILER_BASENAME MATCHES "^(.+-)g?cc(\\.exe)?$")
  127. # if "llvm-" is part of the prefix, remove it, since llvm doesn't have its own binutils
  128. # but uses the regular ar, objcopy, etc. (instead of llvm-objcopy etc.)
  129. IF ("${_CMAKE_TOOLCHAIN_PREFIX}" MATCHES "(.+-)?llvm-$")
  130. SET(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
  131. ENDIF ("${_CMAKE_TOOLCHAIN_PREFIX}" MATCHES "(.+-)?llvm-$")
  132. ENDIF (CMAKE_CROSSCOMPILING
  133. AND "${CMAKE_C_COMPILER_ID}" MATCHES "GNU"
  134. AND NOT _CMAKE_TOOLCHAIN_PREFIX)
  135. INCLUDE(CMakeFindBinUtils)
  136. # configure variables set in this file for fast reload later on
  137. CONFIGURE_FILE(${CMAKE_ROOT}/Modules/CMakeCCompiler.cmake.in
  138. "${CMAKE_PLATFORM_ROOT_BIN}/CMakeCCompiler.cmake"
  139. @ONLY IMMEDIATE # IMMEDIATE must be here for compatibility mode <= 2.0
  140. )
  141. SET(CMAKE_C_COMPILER_ENV_VAR "CC")