CMakeDetermineCCompiler.cmake 7.3 KB

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