CMakeDetermineCXXCompiler.cmake 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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_CXX_COMPILER before
  15. # loading this file to force a compiler.
  16. # use environment variable CXX first if defined by user, next use
  17. # the cmake variable CMAKE_GENERATOR_CXX 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-g++, arm-elf-ar etc.)
  21. #
  22. # Sets the following variables:
  23. # CMAKE_CXX_COMPILER
  24. # CMAKE_COMPILER_IS_GNUCXX
  25. # CMAKE_AR
  26. # CMAKE_RANLIB
  27. #
  28. # If not already set before, it also sets
  29. # _CMAKE_TOOLCHAIN_PREFIX
  30. include(${CMAKE_ROOT}/Modules/CMakeDetermineCompiler.cmake)
  31. # Load system-specific compiler preferences for this language.
  32. include(Platform/${CMAKE_SYSTEM_NAME}-CXX OPTIONAL)
  33. if(NOT CMAKE_CXX_COMPILER_NAMES)
  34. set(CMAKE_CXX_COMPILER_NAMES CC)
  35. endif()
  36. if(${CMAKE_GENERATOR} MATCHES "Visual Studio")
  37. elseif("${CMAKE_GENERATOR}" MATCHES "Xcode")
  38. set(CMAKE_CXX_COMPILER_XCODE_TYPE sourcecode.cpp.cpp)
  39. _cmake_find_compiler_path(CXX)
  40. else()
  41. if(NOT CMAKE_CXX_COMPILER)
  42. set(CMAKE_CXX_COMPILER_INIT NOTFOUND)
  43. # prefer the environment variable CXX
  44. if(NOT $ENV{CXX} STREQUAL "")
  45. get_filename_component(CMAKE_CXX_COMPILER_INIT $ENV{CXX} PROGRAM PROGRAM_ARGS CMAKE_CXX_FLAGS_ENV_INIT)
  46. if(CMAKE_CXX_FLAGS_ENV_INIT)
  47. set(CMAKE_CXX_COMPILER_ARG1 "${CMAKE_CXX_FLAGS_ENV_INIT}" CACHE STRING "First argument to CXX compiler")
  48. endif()
  49. if(NOT EXISTS ${CMAKE_CXX_COMPILER_INIT})
  50. message(FATAL_ERROR "Could not find compiler set in environment variable CXX:\n$ENV{CXX}.\n${CMAKE_CXX_COMPILER_INIT}")
  51. endif()
  52. endif()
  53. # next prefer the generator specified compiler
  54. if(CMAKE_GENERATOR_CXX)
  55. if(NOT CMAKE_CXX_COMPILER_INIT)
  56. set(CMAKE_CXX_COMPILER_INIT ${CMAKE_GENERATOR_CXX})
  57. endif()
  58. endif()
  59. # finally list compilers to try
  60. if(NOT CMAKE_CXX_COMPILER_INIT)
  61. set(CMAKE_CXX_COMPILER_LIST CC ${_CMAKE_TOOLCHAIN_PREFIX}c++ ${_CMAKE_TOOLCHAIN_PREFIX}g++ aCC cl bcc xlC clang++)
  62. endif()
  63. _cmake_find_compiler(CXX)
  64. else()
  65. _cmake_find_compiler_path(CXX)
  66. endif()
  67. mark_as_advanced(CMAKE_CXX_COMPILER)
  68. # Each entry in this list is a set of extra flags to try
  69. # adding to the compile line to see if it helps produce
  70. # a valid identification file.
  71. set(CMAKE_CXX_COMPILER_ID_TEST_FLAGS_FIRST)
  72. set(CMAKE_CXX_COMPILER_ID_TEST_FLAGS
  73. # Try compiling to an object file only.
  74. "-c"
  75. )
  76. endif()
  77. # Build a small source file to identify the compiler.
  78. if(NOT CMAKE_CXX_COMPILER_ID_RUN)
  79. set(CMAKE_CXX_COMPILER_ID_RUN 1)
  80. # Try to identify the compiler.
  81. set(CMAKE_CXX_COMPILER_ID)
  82. set(CMAKE_CXX_PLATFORM_ID)
  83. file(READ ${CMAKE_ROOT}/Modules/CMakePlatformId.h.in
  84. CMAKE_CXX_COMPILER_ID_PLATFORM_CONTENT)
  85. # The IAR compiler produces weird output.
  86. # See https://cmake.org/Bug/view.php?id=10176#c19598
  87. list(APPEND CMAKE_CXX_COMPILER_ID_VENDORS IAR)
  88. set(CMAKE_CXX_COMPILER_ID_VENDOR_FLAGS_IAR )
  89. set(CMAKE_CXX_COMPILER_ID_VENDOR_REGEX_IAR "IAR .+ Compiler")
  90. # Match the link line from xcodebuild output of the form
  91. # Ld ...
  92. # ...
  93. # /path/to/cc ...CompilerIdCXX/...
  94. # to extract the compiler front-end for the language.
  95. 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.xctest/)?CompilerIdCXX[ \t\n\\\"]")
  96. set(CMAKE_CXX_COMPILER_ID_TOOL_MATCH_INDEX 2)
  97. include(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerId.cmake)
  98. CMAKE_DETERMINE_COMPILER_ID(CXX CXXFLAGS CMakeCXXCompilerId.cpp)
  99. # Set old compiler and platform id variables.
  100. if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  101. set(CMAKE_COMPILER_IS_GNUCXX 1)
  102. endif()
  103. if(CMAKE_CXX_PLATFORM_ID MATCHES "MinGW")
  104. set(CMAKE_COMPILER_IS_MINGW 1)
  105. elseif(CMAKE_CXX_PLATFORM_ID MATCHES "Cygwin")
  106. set(CMAKE_COMPILER_IS_CYGWIN 1)
  107. endif()
  108. endif()
  109. if (NOT _CMAKE_TOOLCHAIN_LOCATION)
  110. get_filename_component(_CMAKE_TOOLCHAIN_LOCATION "${CMAKE_CXX_COMPILER}" PATH)
  111. endif ()
  112. # if we have a g++ cross compiler, they have usually some prefix, like
  113. # e.g. powerpc-linux-g++, arm-elf-g++ or i586-mingw32msvc-g++ , optionally
  114. # with a 3-component version number at the end (e.g. arm-eabi-gcc-4.5.2).
  115. # The other tools of the toolchain usually have the same prefix
  116. # NAME_WE cannot be used since then this test will fail for names like
  117. # "arm-unknown-nto-qnx6.3.0-gcc.exe", where BASENAME would be
  118. # "arm-unknown-nto-qnx6" instead of the correct "arm-unknown-nto-qnx6.3.0-"
  119. if (CMAKE_CROSSCOMPILING AND NOT _CMAKE_TOOLCHAIN_PREFIX)
  120. if("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU" OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
  121. get_filename_component(COMPILER_BASENAME "${CMAKE_CXX_COMPILER}" NAME)
  122. if (COMPILER_BASENAME MATCHES "^(.+-)(clan)?[gc]\\+\\+(-[0-9]+\\.[0-9]+\\.[0-9]+)?(\\.exe)?$")
  123. set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
  124. elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
  125. if(CMAKE_CXX_COMPILER_TARGET)
  126. set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_CXX_COMPILER_TARGET}-)
  127. endif()
  128. elseif(COMPILER_BASENAME MATCHES "QCC(\\.exe)?$")
  129. if(CMAKE_CXX_COMPILER_TARGET MATCHES "gcc_nto([^_le]+)(le)?")
  130. set(_CMAKE_TOOLCHAIN_PREFIX nto${CMAKE_MATCH_1}-)
  131. endif()
  132. endif ()
  133. # if "llvm-" is part of the prefix, remove it, since llvm doesn't have its own binutils
  134. # but uses the regular ar, objcopy, etc. (instead of llvm-objcopy etc.)
  135. if ("${_CMAKE_TOOLCHAIN_PREFIX}" MATCHES "(.+-)?llvm-$")
  136. set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
  137. endif ()
  138. elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "TI")
  139. # TI compilers are named e.g. cl6x, cl470 or armcl.exe
  140. get_filename_component(COMPILER_BASENAME "${CMAKE_CXX_COMPILER}" NAME)
  141. if (COMPILER_BASENAME MATCHES "^(.+)?cl([^.]+)?(\\.exe)?$")
  142. set(_CMAKE_TOOLCHAIN_PREFIX "${CMAKE_MATCH_1}")
  143. set(_CMAKE_TOOLCHAIN_SUFFIX "${CMAKE_MATCH_2}")
  144. endif ()
  145. endif()
  146. endif ()
  147. include(CMakeFindBinUtils)
  148. if(MSVC_CXX_ARCHITECTURE_ID)
  149. set(SET_MSVC_CXX_ARCHITECTURE_ID
  150. "set(MSVC_CXX_ARCHITECTURE_ID ${MSVC_CXX_ARCHITECTURE_ID})")
  151. endif()
  152. # configure all variables set in this file
  153. configure_file(${CMAKE_ROOT}/Modules/CMakeCXXCompiler.cmake.in
  154. ${CMAKE_PLATFORM_INFO_DIR}/CMakeCXXCompiler.cmake
  155. @ONLY
  156. )
  157. set(CMAKE_CXX_COMPILER_ENV_VAR "CXX")