CMakeDetermineCXXCompiler.cmake 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 "First argument 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 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. )
  73. endif()
  74. # Build a small source file to identify the compiler.
  75. if(NOT CMAKE_CXX_COMPILER_ID_RUN)
  76. set(CMAKE_CXX_COMPILER_ID_RUN 1)
  77. # Try to identify the compiler.
  78. set(CMAKE_CXX_COMPILER_ID)
  79. set(CMAKE_CXX_PLATFORM_ID)
  80. file(READ ${CMAKE_ROOT}/Modules/CMakePlatformId.h.in
  81. CMAKE_CXX_COMPILER_ID_PLATFORM_CONTENT)
  82. # The IAR compiler produces weird output.
  83. # See https://gitlab.kitware.com/cmake/cmake/issues/10176#note_153591
  84. list(APPEND CMAKE_CXX_COMPILER_ID_VENDORS IAR)
  85. set(CMAKE_CXX_COMPILER_ID_VENDOR_FLAGS_IAR )
  86. set(CMAKE_CXX_COMPILER_ID_VENDOR_REGEX_IAR "IAR .+ Compiler")
  87. # Match the link line from xcodebuild output of the form
  88. # Ld ...
  89. # ...
  90. # /path/to/cc ...CompilerIdCXX/...
  91. # to extract the compiler front-end for the language.
  92. 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)/)?CompilerIdCXX[ \t\n\\\"]")
  93. set(CMAKE_CXX_COMPILER_ID_TOOL_MATCH_INDEX 2)
  94. include(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerId.cmake)
  95. CMAKE_DETERMINE_COMPILER_ID(CXX CXXFLAGS CMakeCXXCompilerId.cpp)
  96. # Set old compiler and platform id variables.
  97. if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  98. set(CMAKE_COMPILER_IS_GNUCXX 1)
  99. endif()
  100. if(CMAKE_CXX_PLATFORM_ID MATCHES "MinGW")
  101. set(CMAKE_COMPILER_IS_MINGW 1)
  102. elseif(CMAKE_CXX_PLATFORM_ID MATCHES "Cygwin")
  103. set(CMAKE_COMPILER_IS_CYGWIN 1)
  104. endif()
  105. else()
  106. if(NOT DEFINED CMAKE_CXX_COMPILER_FRONTEND_VARIANT)
  107. # Some toolchain files set our internal CMAKE_CXX_COMPILER_ID_RUN
  108. # variable but are not aware of CMAKE_CXX_COMPILER_FRONTEND_VARIANT.
  109. # They pre-date our support for the GNU-like variant targeting the
  110. # MSVC ABI so we do not consider that here.
  111. if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
  112. if("x${CMAKE_CXX_SIMULATE_ID}" STREQUAL "xMSVC")
  113. set(CMAKE_CXX_COMPILER_FRONTEND_VARIANT "MSVC")
  114. else()
  115. set(CMAKE_CXX_COMPILER_FRONTEND_VARIANT "GNU")
  116. endif()
  117. else()
  118. set(CMAKE_CXX_COMPILER_FRONTEND_VARIANT "")
  119. endif()
  120. endif()
  121. endif()
  122. if (NOT _CMAKE_TOOLCHAIN_LOCATION)
  123. get_filename_component(_CMAKE_TOOLCHAIN_LOCATION "${CMAKE_CXX_COMPILER}" PATH)
  124. endif ()
  125. # if we have a g++ cross compiler, they have usually some prefix, like
  126. # e.g. powerpc-linux-g++, arm-elf-g++ or i586-mingw32msvc-g++ , optionally
  127. # with a 3-component version number at the end (e.g. arm-eabi-gcc-4.5.2).
  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 like
  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 AND NOT _CMAKE_TOOLCHAIN_PREFIX)
  133. if("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU|Clang|QCC")
  134. get_filename_component(COMPILER_BASENAME "${CMAKE_CXX_COMPILER}" NAME)
  135. if (COMPILER_BASENAME MATCHES "^(.+-)(clan)?[gc]\\+\\+(-[0-9]+(\\.[0-9]+)*)?(-[^.]+)?(\\.exe)?$")
  136. set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
  137. set(_CMAKE_COMPILER_SUFFIX ${CMAKE_MATCH_5})
  138. elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
  139. if(CMAKE_CXX_COMPILER_TARGET)
  140. set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_CXX_COMPILER_TARGET}-)
  141. endif()
  142. elseif(COMPILER_BASENAME MATCHES "QCC(\\.exe)?$")
  143. if(CMAKE_CXX_COMPILER_TARGET MATCHES "gcc_nto([a-z0-9]+_[0-9]+|[^_le]+)(le)")
  144. set(_CMAKE_TOOLCHAIN_PREFIX nto${CMAKE_MATCH_1}-)
  145. endif()
  146. endif ()
  147. # if "llvm-" is part of the prefix, remove it, since llvm doesn't have its own binutils
  148. # but uses the regular ar, objcopy, etc. (instead of llvm-objcopy etc.)
  149. if ("${_CMAKE_TOOLCHAIN_PREFIX}" MATCHES "(.+-)?llvm-$")
  150. set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
  151. endif ()
  152. elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "TI")
  153. # TI compilers are named e.g. cl6x, cl470 or armcl.exe
  154. get_filename_component(COMPILER_BASENAME "${CMAKE_CXX_COMPILER}" NAME)
  155. if (COMPILER_BASENAME MATCHES "^(.+)?cl([^.]+)?(\\.exe)?$")
  156. set(_CMAKE_TOOLCHAIN_PREFIX "${CMAKE_MATCH_1}")
  157. set(_CMAKE_TOOLCHAIN_SUFFIX "${CMAKE_MATCH_2}")
  158. endif ()
  159. endif()
  160. endif ()
  161. set(_CMAKE_PROCESSING_LANGUAGE "CXX")
  162. include(CMakeFindBinUtils)
  163. include(Compiler/${CMAKE_CXX_COMPILER_ID}-FindBinUtils OPTIONAL)
  164. unset(_CMAKE_PROCESSING_LANGUAGE)
  165. if(CMAKE_CXX_COMPILER_ARCHITECTURE_ID)
  166. set(_SET_CMAKE_CXX_COMPILER_ARCHITECTURE_ID
  167. "set(CMAKE_CXX_COMPILER_ARCHITECTURE_ID ${CMAKE_CXX_COMPILER_ARCHITECTURE_ID})")
  168. else()
  169. set(_SET_CMAKE_CXX_COMPILER_ARCHITECTURE_ID "")
  170. endif()
  171. if(MSVC_CXX_ARCHITECTURE_ID)
  172. set(SET_MSVC_CXX_ARCHITECTURE_ID
  173. "set(MSVC_CXX_ARCHITECTURE_ID ${MSVC_CXX_ARCHITECTURE_ID})")
  174. endif()
  175. if(CMAKE_CXX_XCODE_ARCHS)
  176. set(SET_CMAKE_XCODE_ARCHS
  177. "set(CMAKE_XCODE_ARCHS \"${CMAKE_CXX_XCODE_ARCHS}\")")
  178. endif()
  179. # configure all variables set in this file
  180. configure_file(${CMAKE_ROOT}/Modules/CMakeCXXCompiler.cmake.in
  181. ${CMAKE_PLATFORM_INFO_DIR}/CMakeCXXCompiler.cmake
  182. @ONLY
  183. )
  184. set(CMAKE_CXX_COMPILER_ENV_VAR "CXX")