CMakeDetermineCCompiler.cmake 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 distribute 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. include(${CMAKE_ROOT}/Modules/CMakeDetermineCompiler.cmake)
  32. # Load system-specific compiler preferences for this language.
  33. include(Platform/${CMAKE_SYSTEM_NAME}-C OPTIONAL)
  34. if(NOT CMAKE_C_COMPILER_NAMES)
  35. set(CMAKE_C_COMPILER_NAMES cc)
  36. endif()
  37. if(${CMAKE_GENERATOR} MATCHES "Visual Studio")
  38. elseif("${CMAKE_GENERATOR}" MATCHES "Xcode")
  39. set(CMAKE_C_COMPILER_XCODE_TYPE sourcecode.c.c)
  40. else()
  41. if(NOT CMAKE_C_COMPILER)
  42. set(CMAKE_C_COMPILER_INIT NOTFOUND)
  43. # prefer the environment variable CC
  44. if($ENV{CC} MATCHES ".+")
  45. get_filename_component(CMAKE_C_COMPILER_INIT $ENV{CC} PROGRAM PROGRAM_ARGS CMAKE_C_FLAGS_ENV_INIT)
  46. if(CMAKE_C_FLAGS_ENV_INIT)
  47. set(CMAKE_C_COMPILER_ARG1 "${CMAKE_C_FLAGS_ENV_INIT}" CACHE STRING "First argument to C compiler")
  48. endif()
  49. if(NOT EXISTS ${CMAKE_C_COMPILER_INIT})
  50. message(FATAL_ERROR "Could not find compiler set in environment variable CC:\n$ENV{CC}.")
  51. endif()
  52. endif()
  53. # next try prefer the compiler specified by the generator
  54. if(CMAKE_GENERATOR_CC)
  55. if(NOT CMAKE_C_COMPILER_INIT)
  56. set(CMAKE_C_COMPILER_INIT ${CMAKE_GENERATOR_CC})
  57. endif()
  58. endif()
  59. # finally list compilers to try
  60. if(NOT CMAKE_C_COMPILER_INIT)
  61. set(CMAKE_C_COMPILER_LIST ${_CMAKE_TOOLCHAIN_PREFIX}cc ${_CMAKE_TOOLCHAIN_PREFIX}gcc cl bcc xlc clang)
  62. endif()
  63. _cmake_find_compiler(C)
  64. else()
  65. # we only get here if CMAKE_C_COMPILER was specified using -D or a pre-made CMakeCache.txt
  66. # (e.g. via ctest) or set in CMAKE_TOOLCHAIN_FILE
  67. # if CMAKE_C_COMPILER is a list of length 2, use the first item as
  68. # CMAKE_C_COMPILER and the 2nd one as CMAKE_C_COMPILER_ARG1
  69. list(LENGTH CMAKE_C_COMPILER _CMAKE_C_COMPILER_LIST_LENGTH)
  70. if("${_CMAKE_C_COMPILER_LIST_LENGTH}" EQUAL 2)
  71. list(GET CMAKE_C_COMPILER 1 CMAKE_C_COMPILER_ARG1)
  72. list(GET CMAKE_C_COMPILER 0 CMAKE_C_COMPILER)
  73. endif()
  74. # if a compiler was specified by the user but without path,
  75. # now try to find it with the full path
  76. # if it is found, force it into the cache,
  77. # if not, don't overwrite the setting (which was given by the user) with "NOTFOUND"
  78. # if the C compiler already had a path, reuse it for searching the CXX compiler
  79. get_filename_component(_CMAKE_USER_C_COMPILER_PATH "${CMAKE_C_COMPILER}" PATH)
  80. if(NOT _CMAKE_USER_C_COMPILER_PATH)
  81. find_program(CMAKE_C_COMPILER_WITH_PATH NAMES ${CMAKE_C_COMPILER})
  82. if(CMAKE_C_COMPILER_WITH_PATH)
  83. set(CMAKE_C_COMPILER ${CMAKE_C_COMPILER_WITH_PATH} CACHE STRING "C compiler" FORCE)
  84. endif()
  85. unset(CMAKE_C_COMPILER_WITH_PATH CACHE)
  86. endif()
  87. endif()
  88. mark_as_advanced(CMAKE_C_COMPILER)
  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. endif()
  99. # Build a small source file to identify the compiler.
  100. if(NOT CMAKE_C_COMPILER_ID_RUN)
  101. set(CMAKE_C_COMPILER_ID_RUN 1)
  102. # Try to identify the compiler.
  103. set(CMAKE_C_COMPILER_ID)
  104. file(READ ${CMAKE_ROOT}/Modules/CMakePlatformId.h.in
  105. CMAKE_C_COMPILER_ID_PLATFORM_CONTENT)
  106. # The IAR compiler produces weird output.
  107. # See http://www.cmake.org/Bug/view.php?id=10176#c19598
  108. list(APPEND CMAKE_C_COMPILER_ID_VENDORS IAR)
  109. set(CMAKE_C_COMPILER_ID_VENDOR_FLAGS_IAR )
  110. set(CMAKE_C_COMPILER_ID_VENDOR_REGEX_IAR "IAR .+ Compiler")
  111. include(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerId.cmake)
  112. CMAKE_DETERMINE_COMPILER_ID(C CFLAGS CMakeCCompilerId.c)
  113. # Set old compiler and platform id variables.
  114. if("${CMAKE_C_COMPILER_ID}" MATCHES "GNU")
  115. set(CMAKE_COMPILER_IS_GNUCC 1)
  116. endif()
  117. if("${CMAKE_C_PLATFORM_ID}" MATCHES "MinGW")
  118. set(CMAKE_COMPILER_IS_MINGW 1)
  119. elseif("${CMAKE_C_PLATFORM_ID}" MATCHES "Cygwin")
  120. set(CMAKE_COMPILER_IS_CYGWIN 1)
  121. endif()
  122. endif()
  123. if (NOT _CMAKE_TOOLCHAIN_LOCATION)
  124. get_filename_component(_CMAKE_TOOLCHAIN_LOCATION "${CMAKE_C_COMPILER}" PATH)
  125. endif ()
  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, optionally
  128. # with a 3-component version number at the end (e.g. arm-eabi-gcc-4.5.2).
  129. # The other tools of the toolchain usually have the same prefix
  130. # NAME_WE cannot be used since then this test will fail for names lile
  131. # "arm-unknown-nto-qnx6.3.0-gcc.exe", where BASENAME would be
  132. # "arm-unknown-nto-qnx6" instead of the correct "arm-unknown-nto-qnx6.3.0-"
  133. if (CMAKE_CROSSCOMPILING AND NOT _CMAKE_TOOLCHAIN_PREFIX)
  134. if("${CMAKE_C_COMPILER_ID}" MATCHES "GNU" OR "${CMAKE_C_COMPILER_ID}" MATCHES "Clang")
  135. get_filename_component(COMPILER_BASENAME "${CMAKE_C_COMPILER}" NAME)
  136. if (COMPILER_BASENAME MATCHES "^(.+-)(clang|g?cc)(-[0-9]+\\.[0-9]+\\.[0-9]+)?(\\.exe)?$")
  137. set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
  138. elseif("${CMAKE_C_COMPILER_ID}" MATCHES "Clang")
  139. set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_C_COMPILER_TARGET}-)
  140. elseif(COMPILER_BASENAME MATCHES "qcc(\\.exe)?$")
  141. if(CMAKE_C_COMPILER_TARGET MATCHES "gcc_nto([^_le]+)(le)?.*$")
  142. set(_CMAKE_TOOLCHAIN_PREFIX nto${CMAKE_MATCH_1}-)
  143. endif()
  144. endif ()
  145. # if "llvm-" is part of the prefix, remove it, since llvm doesn't have its own binutils
  146. # but uses the regular ar, objcopy, etc. (instead of llvm-objcopy etc.)
  147. if ("${_CMAKE_TOOLCHAIN_PREFIX}" MATCHES "(.+-)?llvm-$")
  148. set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
  149. endif ()
  150. elseif("${CMAKE_C_COMPILER_ID}" MATCHES "TI")
  151. # TI compilers are named e.g. cl6x, cl470 or armcl.exe
  152. get_filename_component(COMPILER_BASENAME "${CMAKE_C_COMPILER}" NAME)
  153. if (COMPILER_BASENAME MATCHES "^(.+)?cl([^.]+)?(\\.exe)?$")
  154. set(_CMAKE_TOOLCHAIN_PREFIX "${CMAKE_MATCH_1}")
  155. set(_CMAKE_TOOLCHAIN_SUFFIX "${CMAKE_MATCH_2}")
  156. endif ()
  157. endif()
  158. endif ()
  159. include(CMakeFindBinUtils)
  160. if(MSVC_C_ARCHITECTURE_ID)
  161. include(${CMAKE_ROOT}/Modules/CMakeClDeps.cmake)
  162. set(SET_MSVC_C_ARCHITECTURE_ID
  163. "set(MSVC_C_ARCHITECTURE_ID ${MSVC_C_ARCHITECTURE_ID})")
  164. endif()
  165. # configure variables set in this file for fast reload later on
  166. configure_file(${CMAKE_ROOT}/Modules/CMakeCCompiler.cmake.in
  167. ${CMAKE_PLATFORM_INFO_DIR}/CMakeCCompiler.cmake
  168. @ONLY
  169. )
  170. set(CMAKE_C_COMPILER_ENV_VAR "CC")