CMakeDetermineCCompiler.cmake 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. # determine the compiler to use for C programs
  4. # NOTE, a generator may set CMAKE_C_COMPILER before
  5. # loading this file to force a compiler.
  6. # use environment variable CC first if defined by user, next use
  7. # the cmake variable CMAKE_GENERATOR_CC which can be defined by a generator
  8. # as a default compiler
  9. # If the internal cmake variable _CMAKE_TOOLCHAIN_PREFIX is set, this is used
  10. # as prefix for the tools (e.g. arm-elf-gcc, arm-elf-ar etc.). This works
  11. # currently with the GNU crosscompilers.
  12. #
  13. # Sets the following variables:
  14. # CMAKE_C_COMPILER
  15. # CMAKE_AR
  16. # CMAKE_RANLIB
  17. # CMAKE_COMPILER_IS_GNUCC
  18. #
  19. # If not already set before, it also sets
  20. # _CMAKE_TOOLCHAIN_PREFIX
  21. include(${CMAKE_ROOT}/Modules/CMakeDetermineCompiler.cmake)
  22. # Load system-specific compiler preferences for this language.
  23. include(Platform/${CMAKE_SYSTEM_NAME}-Determine-C OPTIONAL)
  24. include(Platform/${CMAKE_SYSTEM_NAME}-C OPTIONAL)
  25. if(NOT CMAKE_C_COMPILER_NAMES)
  26. set(CMAKE_C_COMPILER_NAMES cc)
  27. endif()
  28. if(${CMAKE_GENERATOR} MATCHES "Visual Studio")
  29. elseif("${CMAKE_GENERATOR}" MATCHES "Green Hills MULTI")
  30. elseif("${CMAKE_GENERATOR}" MATCHES "Xcode")
  31. set(CMAKE_C_COMPILER_XCODE_TYPE sourcecode.c.c)
  32. _cmake_find_compiler_path(C)
  33. else()
  34. if(NOT CMAKE_C_COMPILER)
  35. set(CMAKE_C_COMPILER_INIT NOTFOUND)
  36. # prefer the environment variable CC
  37. if(NOT $ENV{CC} STREQUAL "")
  38. get_filename_component(CMAKE_C_COMPILER_INIT $ENV{CC} PROGRAM PROGRAM_ARGS CMAKE_C_FLAGS_ENV_INIT)
  39. if(CMAKE_C_FLAGS_ENV_INIT)
  40. set(CMAKE_C_COMPILER_ARG1 "${CMAKE_C_FLAGS_ENV_INIT}" CACHE STRING "Arguments to C compiler")
  41. endif()
  42. if(NOT EXISTS ${CMAKE_C_COMPILER_INIT})
  43. message(FATAL_ERROR "Could not find compiler set in environment variable CC:\n$ENV{CC}.")
  44. endif()
  45. endif()
  46. # next try prefer the compiler specified by the generator
  47. if(CMAKE_GENERATOR_CC)
  48. if(NOT CMAKE_C_COMPILER_INIT)
  49. set(CMAKE_C_COMPILER_INIT ${CMAKE_GENERATOR_CC})
  50. endif()
  51. endif()
  52. # finally list compilers to try
  53. if(NOT CMAKE_C_COMPILER_INIT)
  54. set(CMAKE_C_COMPILER_LIST ${_CMAKE_TOOLCHAIN_PREFIX}cc ${_CMAKE_TOOLCHAIN_PREFIX}gcc cl bcc xlc icx clang)
  55. endif()
  56. _cmake_find_compiler(C)
  57. else()
  58. _cmake_find_compiler_path(C)
  59. endif()
  60. mark_as_advanced(CMAKE_C_COMPILER)
  61. # Each entry in this list is a set of extra flags to try
  62. # adding to the compile line to see if it helps produce
  63. # a valid identification file.
  64. set(CMAKE_C_COMPILER_ID_TEST_FLAGS_FIRST)
  65. set(CMAKE_C_COMPILER_ID_TEST_FLAGS
  66. # Try compiling to an object file only.
  67. "-c"
  68. # Try enabling ANSI mode on HP.
  69. "-Aa"
  70. # Try compiling K&R-compatible code (needed by Bruce C Compiler).
  71. "-D__CLASSIC_C__"
  72. # ARMClang need target options
  73. "--target=arm-arm-none-eabi -mcpu=cortex-m3"
  74. # MSVC needs at least one include directory for __has_include to function,
  75. # but custom toolchains may run MSVC with no INCLUDE env var and no -I flags.
  76. # Also avoid linking so this works with no LIB env var.
  77. "-c -I__does_not_exist__"
  78. )
  79. endif()
  80. if(CMAKE_C_COMPILER_TARGET)
  81. set(CMAKE_C_COMPILER_ID_TEST_FLAGS_FIRST "-c --target=${CMAKE_C_COMPILER_TARGET}")
  82. endif()
  83. # Build a small source file to identify the compiler.
  84. if(NOT CMAKE_C_COMPILER_ID_RUN)
  85. set(CMAKE_C_COMPILER_ID_RUN 1)
  86. # Try to identify the compiler.
  87. set(CMAKE_C_COMPILER_ID)
  88. set(CMAKE_C_PLATFORM_ID)
  89. file(READ ${CMAKE_ROOT}/Modules/CMakePlatformId.h.in
  90. CMAKE_C_COMPILER_ID_PLATFORM_CONTENT)
  91. # The IAR compiler produces weird output.
  92. # See https://gitlab.kitware.com/cmake/cmake/-/issues/10176#note_153591
  93. list(APPEND CMAKE_C_COMPILER_ID_VENDORS IAR)
  94. set(CMAKE_C_COMPILER_ID_VENDOR_FLAGS_IAR )
  95. set(CMAKE_C_COMPILER_ID_VENDOR_REGEX_IAR "IAR .+ Compiler")
  96. # Match the link line from xcodebuild output of the form
  97. # Ld ...
  98. # ...
  99. # /path/to/cc ...CompilerIdC/...
  100. # to extract the compiler front-end for the language.
  101. set(CMAKE_C_COMPILER_ID_TOOL_MATCH_REGEX "\nLd[^\n]*(\n[ \t]+[^\n]*)*\n[ \t]+([^ \t\r\n]+)[^\r\n]*-o[^\r\n]*CompilerIdC/(\\./)?(CompilerIdC.(framework|xctest|build/[^ \t\r\n]+)/)?CompilerIdC[ \t\n\\\"]")
  102. set(CMAKE_C_COMPILER_ID_TOOL_MATCH_INDEX 2)
  103. include(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerId.cmake)
  104. CMAKE_DETERMINE_COMPILER_ID(C CFLAGS CMakeCCompilerId.c)
  105. _cmake_find_compiler_sysroot(C)
  106. # Set old compiler and platform id variables.
  107. if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
  108. set(CMAKE_COMPILER_IS_GNUCC 1)
  109. endif()
  110. else()
  111. if(NOT DEFINED CMAKE_C_COMPILER_FRONTEND_VARIANT)
  112. # Some toolchain files set our internal CMAKE_C_COMPILER_ID_RUN
  113. # variable but are not aware of CMAKE_C_COMPILER_FRONTEND_VARIANT.
  114. # They pre-date our support for the GNU-like variant targeting the
  115. # MSVC ABI so we do not consider that here.
  116. if(CMAKE_C_COMPILER_ID STREQUAL "Clang"
  117. OR "x${CMAKE_C_COMPILER_ID}" STREQUAL "xIntelLLVM")
  118. if("x${CMAKE_C_SIMULATE_ID}" STREQUAL "xMSVC")
  119. set(CMAKE_C_COMPILER_FRONTEND_VARIANT "MSVC")
  120. else()
  121. set(CMAKE_C_COMPILER_FRONTEND_VARIANT "GNU")
  122. endif()
  123. else()
  124. set(CMAKE_C_COMPILER_FRONTEND_VARIANT "")
  125. endif()
  126. endif()
  127. endif()
  128. if (NOT _CMAKE_TOOLCHAIN_LOCATION)
  129. get_filename_component(_CMAKE_TOOLCHAIN_LOCATION "${CMAKE_C_COMPILER}" PATH)
  130. endif ()
  131. # If we have a gcc cross compiler, they have usually some prefix, like
  132. # e.g. powerpc-linux-gcc, arm-elf-gcc or i586-mingw32msvc-gcc, optionally
  133. # with a 3-component version number at the end (e.g. arm-eabi-gcc-4.5.2).
  134. # The other tools of the toolchain usually have the same prefix
  135. # NAME_WE cannot be used since then this test will fail for names like
  136. # "arm-unknown-nto-qnx6.3.0-gcc.exe", where BASENAME would be
  137. # "arm-unknown-nto-qnx6" instead of the correct "arm-unknown-nto-qnx6.3.0-"
  138. if (NOT _CMAKE_TOOLCHAIN_PREFIX)
  139. if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang|QCC|LCC")
  140. get_filename_component(COMPILER_BASENAME "${CMAKE_C_COMPILER}" NAME)
  141. if (COMPILER_BASENAME MATCHES "^(.+-)?(clang|g?cc)(-cl)?(-[0-9]+(\\.[0-9]+)*)?(-[^.]+)?(\\.exe)?$")
  142. set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
  143. set(_CMAKE_TOOLCHAIN_SUFFIX ${CMAKE_MATCH_4})
  144. set(_CMAKE_COMPILER_SUFFIX ${CMAKE_MATCH_6})
  145. elseif(CMAKE_C_COMPILER_ID MATCHES "Clang")
  146. if(CMAKE_C_COMPILER_TARGET)
  147. set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_C_COMPILER_TARGET}-)
  148. endif()
  149. elseif(COMPILER_BASENAME MATCHES "qcc(\\.exe)?$")
  150. if(CMAKE_C_COMPILER_TARGET MATCHES "gcc_nto([a-z0-9]+_[0-9]+|[^_le]+)(le)?")
  151. set(_CMAKE_TOOLCHAIN_PREFIX nto${CMAKE_MATCH_1}-)
  152. endif()
  153. endif ()
  154. # if "llvm-" is part of the prefix, remove it, since llvm doesn't have its own binutils
  155. # but uses the regular ar, objcopy, etc. (instead of llvm-objcopy etc.)
  156. if ("${_CMAKE_TOOLCHAIN_PREFIX}" MATCHES "(.+-)?llvm-$")
  157. set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
  158. endif ()
  159. elseif(CMAKE_C_COMPILER_ID MATCHES "TI")
  160. # TI compilers are named e.g. cl6x, cl470 or armcl.exe
  161. get_filename_component(COMPILER_BASENAME "${CMAKE_C_COMPILER}" NAME)
  162. if (COMPILER_BASENAME MATCHES "^(.+)?cl([^.]+)?(\\.exe)?$")
  163. set(_CMAKE_TOOLCHAIN_PREFIX "${CMAKE_MATCH_1}")
  164. set(_CMAKE_TOOLCHAIN_SUFFIX "${CMAKE_MATCH_2}")
  165. endif ()
  166. endif()
  167. endif ()
  168. set(_CMAKE_PROCESSING_LANGUAGE "C")
  169. include(CMakeFindBinUtils)
  170. include(Compiler/${CMAKE_C_COMPILER_ID}-FindBinUtils OPTIONAL)
  171. unset(_CMAKE_PROCESSING_LANGUAGE)
  172. if(CMAKE_C_COMPILER_SYSROOT)
  173. string(CONCAT _SET_CMAKE_C_COMPILER_SYSROOT
  174. "set(CMAKE_C_COMPILER_SYSROOT \"${CMAKE_C_COMPILER_SYSROOT}\")\n"
  175. "set(CMAKE_COMPILER_SYSROOT \"${CMAKE_C_COMPILER_SYSROOT}\")")
  176. else()
  177. set(_SET_CMAKE_C_COMPILER_SYSROOT "")
  178. endif()
  179. if(CMAKE_C_COMPILER_ARCHITECTURE_ID)
  180. set(_SET_CMAKE_C_COMPILER_ARCHITECTURE_ID
  181. "set(CMAKE_C_COMPILER_ARCHITECTURE_ID ${CMAKE_C_COMPILER_ARCHITECTURE_ID})")
  182. else()
  183. set(_SET_CMAKE_C_COMPILER_ARCHITECTURE_ID "")
  184. endif()
  185. if(MSVC_C_ARCHITECTURE_ID)
  186. set(SET_MSVC_C_ARCHITECTURE_ID
  187. "set(MSVC_C_ARCHITECTURE_ID ${MSVC_C_ARCHITECTURE_ID})")
  188. endif()
  189. if(CMAKE_C_XCODE_ARCHS)
  190. set(SET_CMAKE_XCODE_ARCHS
  191. "set(CMAKE_XCODE_ARCHS \"${CMAKE_C_XCODE_ARCHS}\")")
  192. endif()
  193. # configure variables set in this file for fast reload later on
  194. configure_file(${CMAKE_ROOT}/Modules/CMakeCCompiler.cmake.in
  195. ${CMAKE_PLATFORM_INFO_DIR}/CMakeCCompiler.cmake
  196. @ONLY
  197. )
  198. set(CMAKE_C_COMPILER_ENV_VAR "CC")