CMakeDetermineCXXCompiler.cmake 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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_CXX_COMPILER before
  5. # loading this file to force a compiler.
  6. # use environment variable CXX first if defined by user, next use
  7. # the cmake variable CMAKE_GENERATOR_CXX 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-g++, arm-elf-ar etc.)
  11. #
  12. # Sets the following variables:
  13. # CMAKE_CXX_COMPILER
  14. # CMAKE_COMPILER_IS_GNUCXX
  15. # CMAKE_AR
  16. # CMAKE_RANLIB
  17. #
  18. # If not already set before, it also sets
  19. # _CMAKE_TOOLCHAIN_PREFIX
  20. include(${CMAKE_ROOT}/Modules/CMakeDetermineCompiler.cmake)
  21. # Load system-specific compiler preferences for this language.
  22. include(Platform/${CMAKE_SYSTEM_NAME}-Determine-CXX OPTIONAL)
  23. include(Platform/${CMAKE_SYSTEM_NAME}-CXX OPTIONAL)
  24. if(NOT CMAKE_CXX_COMPILER_NAMES)
  25. set(CMAKE_CXX_COMPILER_NAMES CC)
  26. endif()
  27. if(${CMAKE_GENERATOR} MATCHES "Visual Studio")
  28. elseif("${CMAKE_GENERATOR}" MATCHES "Green Hills MULTI")
  29. elseif("${CMAKE_GENERATOR}" MATCHES "Xcode")
  30. set(CMAKE_CXX_COMPILER_XCODE_TYPE sourcecode.cpp.cpp)
  31. _cmake_find_compiler_path(CXX)
  32. else()
  33. if(NOT CMAKE_CXX_COMPILER)
  34. set(CMAKE_CXX_COMPILER_INIT NOTFOUND)
  35. # prefer the environment variable CXX
  36. if(NOT $ENV{CXX} STREQUAL "")
  37. get_filename_component(CMAKE_CXX_COMPILER_INIT $ENV{CXX} PROGRAM PROGRAM_ARGS CMAKE_CXX_FLAGS_ENV_INIT)
  38. if(CMAKE_CXX_FLAGS_ENV_INIT)
  39. set(CMAKE_CXX_COMPILER_ARG1 "${CMAKE_CXX_FLAGS_ENV_INIT}" CACHE STRING "Arguments to CXX compiler")
  40. endif()
  41. if(NOT EXISTS ${CMAKE_CXX_COMPILER_INIT})
  42. message(FATAL_ERROR "Could not find compiler set in environment variable CXX:\n$ENV{CXX}.\n${CMAKE_CXX_COMPILER_INIT}")
  43. endif()
  44. endif()
  45. # next prefer the generator specified compiler
  46. if(CMAKE_GENERATOR_CXX)
  47. if(NOT CMAKE_CXX_COMPILER_INIT)
  48. set(CMAKE_CXX_COMPILER_INIT ${CMAKE_GENERATOR_CXX})
  49. endif()
  50. endif()
  51. # finally list compilers to try
  52. if(NOT CMAKE_CXX_COMPILER_INIT)
  53. set(CMAKE_CXX_COMPILER_LIST CC ${_CMAKE_TOOLCHAIN_PREFIX}c++ ${_CMAKE_TOOLCHAIN_PREFIX}g++ aCC cl bcc xlC icpx icx clang++)
  54. endif()
  55. _cmake_find_compiler(CXX)
  56. else()
  57. _cmake_find_compiler_path(CXX)
  58. endif()
  59. mark_as_advanced(CMAKE_CXX_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_CXX_COMPILER_ID_TEST_FLAGS_FIRST)
  64. set(CMAKE_CXX_COMPILER_ID_TEST_FLAGS
  65. # Try compiling to an object file only.
  66. "-c"
  67. # IAR does not detect language automatically
  68. "--c++"
  69. "--ec++"
  70. # ARMClang need target options
  71. "--target=arm-arm-none-eabi -mcpu=cortex-m3"
  72. # MSVC needs at least one include directory for __has_include to function,
  73. # but custom toolchains may run MSVC with no INCLUDE env var and no -I flags.
  74. # Also avoid linking so this works with no LIB env var.
  75. "-c -I__does_not_exist__"
  76. )
  77. endif()
  78. if(CMAKE_CXX_COMPILER_TARGET)
  79. set(CMAKE_CXX_COMPILER_ID_TEST_FLAGS_FIRST "-c --target=${CMAKE_CXX_COMPILER_TARGET}")
  80. endif()
  81. # Build a small source file to identify the compiler.
  82. if(NOT CMAKE_CXX_COMPILER_ID_RUN)
  83. set(CMAKE_CXX_COMPILER_ID_RUN 1)
  84. # Try to identify the compiler.
  85. set(CMAKE_CXX_COMPILER_ID)
  86. set(CMAKE_CXX_PLATFORM_ID)
  87. file(READ ${CMAKE_ROOT}/Modules/CMakePlatformId.h.in
  88. CMAKE_CXX_COMPILER_ID_PLATFORM_CONTENT)
  89. # The IAR compiler produces weird output.
  90. # See https://gitlab.kitware.com/cmake/cmake/-/issues/10176#note_153591
  91. list(APPEND CMAKE_CXX_COMPILER_ID_VENDORS IAR)
  92. set(CMAKE_CXX_COMPILER_ID_VENDOR_FLAGS_IAR )
  93. set(CMAKE_CXX_COMPILER_ID_VENDOR_REGEX_IAR "IAR .+ Compiler")
  94. # Match the link line from xcodebuild output of the form
  95. # Ld ...
  96. # ...
  97. # /path/to/cc ...CompilerIdCXX/...
  98. # to extract the compiler front-end for the language.
  99. set(CMAKE_CXX_COMPILER_ID_TOOL_MATCH_REGEX "\nLd[^\n]*(\n[ \t]+[^\n]*)*\n[ \t]+([^ \t\r\n]+)[^\r\n]*-o[^\r\n]*CompilerIdCXX/(\\./)?(CompilerIdCXX.(framework|xctest|build/[^ \t\r\n]+)/)?CompilerIdCXX[ \t\n\\\"]")
  100. set(CMAKE_CXX_COMPILER_ID_TOOL_MATCH_INDEX 2)
  101. include(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerId.cmake)
  102. CMAKE_DETERMINE_COMPILER_ID(CXX CXXFLAGS CMakeCXXCompilerId.cpp)
  103. _cmake_find_compiler_sysroot(CXX)
  104. # Set old compiler and platform id variables.
  105. if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  106. set(CMAKE_COMPILER_IS_GNUCXX 1)
  107. endif()
  108. else()
  109. if(NOT DEFINED CMAKE_CXX_COMPILER_FRONTEND_VARIANT)
  110. # Some toolchain files set our internal CMAKE_CXX_COMPILER_ID_RUN
  111. # variable but are not aware of CMAKE_CXX_COMPILER_FRONTEND_VARIANT.
  112. # They pre-date our support for the GNU-like variant targeting the
  113. # MSVC ABI so we do not consider that here.
  114. if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang"
  115. OR "x${CMAKE_CXX_COMPILER_ID}" STREQUAL "xIntelLLVM")
  116. if("x${CMAKE_CXX_SIMULATE_ID}" STREQUAL "xMSVC")
  117. set(CMAKE_CXX_COMPILER_FRONTEND_VARIANT "MSVC")
  118. else()
  119. set(CMAKE_CXX_COMPILER_FRONTEND_VARIANT "GNU")
  120. endif()
  121. else()
  122. set(CMAKE_CXX_COMPILER_FRONTEND_VARIANT "")
  123. endif()
  124. endif()
  125. endif()
  126. if (NOT _CMAKE_TOOLCHAIN_LOCATION)
  127. get_filename_component(_CMAKE_TOOLCHAIN_LOCATION "${CMAKE_CXX_COMPILER}" PATH)
  128. endif ()
  129. # if we have a g++ cross compiler, they have usually some prefix, like
  130. # e.g. powerpc-linux-g++, arm-elf-g++ or i586-mingw32msvc-g++ , optionally
  131. # with a 3-component version number at the end (e.g. arm-eabi-gcc-4.5.2).
  132. # The other tools of the toolchain usually have the same prefix
  133. # NAME_WE cannot be used since then this test will fail for names like
  134. # "arm-unknown-nto-qnx6.3.0-gcc.exe", where BASENAME would be
  135. # "arm-unknown-nto-qnx6" instead of the correct "arm-unknown-nto-qnx6.3.0-"
  136. if (NOT _CMAKE_TOOLCHAIN_PREFIX)
  137. if("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU|Clang|QCC")
  138. get_filename_component(COMPILER_BASENAME "${CMAKE_CXX_COMPILER}" NAME)
  139. if (COMPILER_BASENAME MATCHES "^(.+-)?(clang\\+\\+|[gc]\\+\\+|clang-cl)(-[0-9]+(\\.[0-9]+)*)?(-[^.]+)?(\\.exe)?$")
  140. set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
  141. set(_CMAKE_TOOLCHAIN_SUFFIX ${CMAKE_MATCH_3})
  142. set(_CMAKE_COMPILER_SUFFIX ${CMAKE_MATCH_5})
  143. elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
  144. if(CMAKE_CXX_COMPILER_TARGET)
  145. set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_CXX_COMPILER_TARGET}-)
  146. endif()
  147. elseif(COMPILER_BASENAME MATCHES "QCC(\\.exe)?$")
  148. if(CMAKE_CXX_COMPILER_TARGET MATCHES "gcc_nto([a-z0-9]+_[0-9]+|[^_le]+)(le)")
  149. set(_CMAKE_TOOLCHAIN_PREFIX nto${CMAKE_MATCH_1}-)
  150. endif()
  151. endif ()
  152. # if "llvm-" is part of the prefix, remove it, since llvm doesn't have its own binutils
  153. # but uses the regular ar, objcopy, etc. (instead of llvm-objcopy etc.)
  154. if ("${_CMAKE_TOOLCHAIN_PREFIX}" MATCHES "(.+-)?llvm-$")
  155. set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
  156. endif ()
  157. elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "TI")
  158. # TI compilers are named e.g. cl6x, cl470 or armcl.exe
  159. get_filename_component(COMPILER_BASENAME "${CMAKE_CXX_COMPILER}" NAME)
  160. if (COMPILER_BASENAME MATCHES "^(.+)?cl([^.]+)?(\\.exe)?$")
  161. set(_CMAKE_TOOLCHAIN_PREFIX "${CMAKE_MATCH_1}")
  162. set(_CMAKE_TOOLCHAIN_SUFFIX "${CMAKE_MATCH_2}")
  163. endif ()
  164. endif()
  165. endif ()
  166. set(_CMAKE_PROCESSING_LANGUAGE "CXX")
  167. include(CMakeFindBinUtils)
  168. include(Compiler/${CMAKE_CXX_COMPILER_ID}-FindBinUtils OPTIONAL)
  169. unset(_CMAKE_PROCESSING_LANGUAGE)
  170. if(CMAKE_CXX_COMPILER_SYSROOT)
  171. string(CONCAT _SET_CMAKE_CXX_COMPILER_SYSROOT
  172. "set(CMAKE_CXX_COMPILER_SYSROOT \"${CMAKE_CXX_COMPILER_SYSROOT}\")\n"
  173. "set(CMAKE_COMPILER_SYSROOT \"${CMAKE_CXX_COMPILER_SYSROOT}\")")
  174. else()
  175. set(_SET_CMAKE_CXX_COMPILER_SYSROOT "")
  176. endif()
  177. if(CMAKE_CXX_COMPILER_ARCHITECTURE_ID)
  178. set(_SET_CMAKE_CXX_COMPILER_ARCHITECTURE_ID
  179. "set(CMAKE_CXX_COMPILER_ARCHITECTURE_ID ${CMAKE_CXX_COMPILER_ARCHITECTURE_ID})")
  180. else()
  181. set(_SET_CMAKE_CXX_COMPILER_ARCHITECTURE_ID "")
  182. endif()
  183. if(MSVC_CXX_ARCHITECTURE_ID)
  184. set(SET_MSVC_CXX_ARCHITECTURE_ID
  185. "set(MSVC_CXX_ARCHITECTURE_ID ${MSVC_CXX_ARCHITECTURE_ID})")
  186. endif()
  187. if(CMAKE_CXX_XCODE_ARCHS)
  188. set(SET_CMAKE_XCODE_ARCHS
  189. "set(CMAKE_XCODE_ARCHS \"${CMAKE_CXX_XCODE_ARCHS}\")")
  190. endif()
  191. # configure all variables set in this file
  192. configure_file(${CMAKE_ROOT}/Modules/CMakeCXXCompiler.cmake.in
  193. ${CMAKE_PLATFORM_INFO_DIR}/CMakeCXXCompiler.cmake
  194. @ONLY
  195. )
  196. set(CMAKE_CXX_COMPILER_ENV_VAR "CXX")