CMakeDetermineCCompiler.cmake 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 "Xcode")
  30. set(CMAKE_C_COMPILER_XCODE_TYPE sourcecode.c.c)
  31. _cmake_find_compiler_path(C)
  32. else()
  33. if(NOT CMAKE_C_COMPILER)
  34. set(CMAKE_C_COMPILER_INIT NOTFOUND)
  35. # prefer the environment variable CC
  36. if(NOT $ENV{CC} STREQUAL "")
  37. get_filename_component(CMAKE_C_COMPILER_INIT $ENV{CC} PROGRAM PROGRAM_ARGS CMAKE_C_FLAGS_ENV_INIT)
  38. if(CMAKE_C_FLAGS_ENV_INIT)
  39. set(CMAKE_C_COMPILER_ARG1 "${CMAKE_C_FLAGS_ENV_INIT}" CACHE STRING "First argument to C compiler")
  40. endif()
  41. if(NOT EXISTS ${CMAKE_C_COMPILER_INIT})
  42. message(FATAL_ERROR "Could not find compiler set in environment variable CC:\n$ENV{CC}.")
  43. endif()
  44. endif()
  45. # next try prefer the compiler specified by the generator
  46. if(CMAKE_GENERATOR_CC)
  47. if(NOT CMAKE_C_COMPILER_INIT)
  48. set(CMAKE_C_COMPILER_INIT ${CMAKE_GENERATOR_CC})
  49. endif()
  50. endif()
  51. # finally list compilers to try
  52. if(NOT CMAKE_C_COMPILER_INIT)
  53. set(CMAKE_C_COMPILER_LIST ${_CMAKE_TOOLCHAIN_PREFIX}cc ${_CMAKE_TOOLCHAIN_PREFIX}gcc cl bcc xlc clang)
  54. endif()
  55. _cmake_find_compiler(C)
  56. else()
  57. _cmake_find_compiler_path(C)
  58. endif()
  59. mark_as_advanced(CMAKE_C_COMPILER)
  60. # Each entry in this list is a set of extra flags to try
  61. # adding to the compile line to see if it helps produce
  62. # a valid identification file.
  63. set(CMAKE_C_COMPILER_ID_TEST_FLAGS_FIRST)
  64. set(CMAKE_C_COMPILER_ID_TEST_FLAGS
  65. # Try compiling to an object file only.
  66. "-c"
  67. # Try enabling ANSI mode on HP.
  68. "-Aa"
  69. # Try compiling K&R-compatible code (needed by Bruce C Compiler).
  70. "-D__CLASSIC_C__"
  71. )
  72. endif()
  73. # Build a small source file to identify the compiler.
  74. if(NOT CMAKE_C_COMPILER_ID_RUN)
  75. set(CMAKE_C_COMPILER_ID_RUN 1)
  76. # Try to identify the compiler.
  77. set(CMAKE_C_COMPILER_ID)
  78. set(CMAKE_C_PLATFORM_ID)
  79. file(READ ${CMAKE_ROOT}/Modules/CMakePlatformId.h.in
  80. CMAKE_C_COMPILER_ID_PLATFORM_CONTENT)
  81. # The IAR compiler produces weird output.
  82. # See https://gitlab.kitware.com/cmake/cmake/issues/10176#note_153591
  83. list(APPEND CMAKE_C_COMPILER_ID_VENDORS IAR)
  84. set(CMAKE_C_COMPILER_ID_VENDOR_FLAGS_IAR )
  85. set(CMAKE_C_COMPILER_ID_VENDOR_REGEX_IAR "IAR .+ Compiler")
  86. # Match the link line from xcodebuild output of the form
  87. # Ld ...
  88. # ...
  89. # /path/to/cc ...CompilerIdC/...
  90. # to extract the compiler front-end for the language.
  91. 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.xctest/)?CompilerIdC[ \t\n\\\"]")
  92. set(CMAKE_C_COMPILER_ID_TOOL_MATCH_INDEX 2)
  93. include(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerId.cmake)
  94. CMAKE_DETERMINE_COMPILER_ID(C CFLAGS CMakeCCompilerId.c)
  95. # Set old compiler and platform id variables.
  96. if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
  97. set(CMAKE_COMPILER_IS_GNUCC 1)
  98. endif()
  99. if(CMAKE_C_PLATFORM_ID MATCHES "MinGW")
  100. set(CMAKE_COMPILER_IS_MINGW 1)
  101. elseif(CMAKE_C_PLATFORM_ID MATCHES "Cygwin")
  102. set(CMAKE_COMPILER_IS_CYGWIN 1)
  103. endif()
  104. endif()
  105. if (NOT _CMAKE_TOOLCHAIN_LOCATION)
  106. get_filename_component(_CMAKE_TOOLCHAIN_LOCATION "${CMAKE_C_COMPILER}" PATH)
  107. endif ()
  108. # If we have a gcc cross compiler, they have usually some prefix, like
  109. # e.g. powerpc-linux-gcc, arm-elf-gcc or i586-mingw32msvc-gcc, optionally
  110. # with a 3-component version number at the end (e.g. arm-eabi-gcc-4.5.2).
  111. # The other tools of the toolchain usually have the same prefix
  112. # NAME_WE cannot be used since then this test will fail for names like
  113. # "arm-unknown-nto-qnx6.3.0-gcc.exe", where BASENAME would be
  114. # "arm-unknown-nto-qnx6" instead of the correct "arm-unknown-nto-qnx6.3.0-"
  115. if (CMAKE_CROSSCOMPILING AND NOT _CMAKE_TOOLCHAIN_PREFIX)
  116. if(CMAKE_C_COMPILER_ID MATCHES "GNU" OR CMAKE_C_COMPILER_ID MATCHES "Clang")
  117. get_filename_component(COMPILER_BASENAME "${CMAKE_C_COMPILER}" NAME)
  118. if (COMPILER_BASENAME MATCHES "^(.+-)(clang|g?cc)(-[0-9]+\\.[0-9]+\\.[0-9]+)?(\\.exe)?$")
  119. set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
  120. elseif(CMAKE_C_COMPILER_ID MATCHES "Clang")
  121. if(CMAKE_C_COMPILER_TARGET)
  122. set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_C_COMPILER_TARGET}-)
  123. endif()
  124. elseif(COMPILER_BASENAME MATCHES "qcc(\\.exe)?$")
  125. if(CMAKE_C_COMPILER_TARGET MATCHES "gcc_nto([^_le]+)(le)?")
  126. set(_CMAKE_TOOLCHAIN_PREFIX nto${CMAKE_MATCH_1}-)
  127. endif()
  128. endif ()
  129. # if "llvm-" is part of the prefix, remove it, since llvm doesn't have its own binutils
  130. # but uses the regular ar, objcopy, etc. (instead of llvm-objcopy etc.)
  131. if ("${_CMAKE_TOOLCHAIN_PREFIX}" MATCHES "(.+-)?llvm-$")
  132. set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
  133. endif ()
  134. elseif(CMAKE_C_COMPILER_ID MATCHES "TI")
  135. # TI compilers are named e.g. cl6x, cl470 or armcl.exe
  136. get_filename_component(COMPILER_BASENAME "${CMAKE_C_COMPILER}" NAME)
  137. if (COMPILER_BASENAME MATCHES "^(.+)?cl([^.]+)?(\\.exe)?$")
  138. set(_CMAKE_TOOLCHAIN_PREFIX "${CMAKE_MATCH_1}")
  139. set(_CMAKE_TOOLCHAIN_SUFFIX "${CMAKE_MATCH_2}")
  140. endif ()
  141. endif()
  142. endif ()
  143. include(CMakeFindBinUtils)
  144. if(MSVC_C_ARCHITECTURE_ID)
  145. set(SET_MSVC_C_ARCHITECTURE_ID
  146. "set(MSVC_C_ARCHITECTURE_ID ${MSVC_C_ARCHITECTURE_ID})")
  147. endif()
  148. # configure variables set in this file for fast reload later on
  149. configure_file(${CMAKE_ROOT}/Modules/CMakeCCompiler.cmake.in
  150. ${CMAKE_PLATFORM_INFO_DIR}/CMakeCCompiler.cmake
  151. @ONLY
  152. )
  153. set(CMAKE_C_COMPILER_ENV_VAR "CC")