CMakeDetermineCXXCompiler.cmake 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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(NOT CMAKE_CXX_COMPILER)
  37. SET(CMAKE_CXX_COMPILER_INIT NOTFOUND)
  38. # prefer the environment variable CXX
  39. IF($ENV{CXX} MATCHES ".+")
  40. GET_FILENAME_COMPONENT(CMAKE_CXX_COMPILER_INIT $ENV{CXX} PROGRAM PROGRAM_ARGS CMAKE_CXX_FLAGS_ENV_INIT)
  41. IF(CMAKE_CXX_FLAGS_ENV_INIT)
  42. SET(CMAKE_CXX_COMPILER_ARG1 "${CMAKE_CXX_FLAGS_ENV_INIT}" CACHE STRING "First argument to CXX compiler")
  43. ENDIF(CMAKE_CXX_FLAGS_ENV_INIT)
  44. IF(NOT EXISTS ${CMAKE_CXX_COMPILER_INIT})
  45. MESSAGE(FATAL_ERROR "Could not find compiler set in environment variable CXX:\n$ENV{CXX}.\n${CMAKE_CXX_COMPILER_INIT}")
  46. ENDIF(NOT EXISTS ${CMAKE_CXX_COMPILER_INIT})
  47. ENDIF($ENV{CXX} MATCHES ".+")
  48. # next prefer the generator specified compiler
  49. IF(CMAKE_GENERATOR_CXX)
  50. IF(NOT CMAKE_CXX_COMPILER_INIT)
  51. SET(CMAKE_CXX_COMPILER_INIT ${CMAKE_GENERATOR_CXX})
  52. ENDIF(NOT CMAKE_CXX_COMPILER_INIT)
  53. ENDIF(CMAKE_GENERATOR_CXX)
  54. # finally list compilers to try
  55. IF(NOT CMAKE_CXX_COMPILER_INIT)
  56. SET(CMAKE_CXX_COMPILER_LIST CC ${_CMAKE_TOOLCHAIN_PREFIX}c++ ${_CMAKE_TOOLCHAIN_PREFIX}g++ aCC cl bcc xlC clang++)
  57. ENDIF()
  58. _cmake_find_compiler(CXX)
  59. ELSE(NOT CMAKE_CXX_COMPILER)
  60. # we only get here if CMAKE_CXX_COMPILER was specified using -D or a pre-made CMakeCache.txt
  61. # (e.g. via ctest) or set in CMAKE_TOOLCHAIN_FILE
  62. #
  63. # if CMAKE_CXX_COMPILER is a list of length 2, use the first item as
  64. # CMAKE_CXX_COMPILER and the 2nd one as CMAKE_CXX_COMPILER_ARG1
  65. LIST(LENGTH CMAKE_CXX_COMPILER _CMAKE_CXX_COMPILER_LIST_LENGTH)
  66. IF("${_CMAKE_CXX_COMPILER_LIST_LENGTH}" EQUAL 2)
  67. LIST(GET CMAKE_CXX_COMPILER 1 CMAKE_CXX_COMPILER_ARG1)
  68. LIST(GET CMAKE_CXX_COMPILER 0 CMAKE_CXX_COMPILER)
  69. ENDIF("${_CMAKE_CXX_COMPILER_LIST_LENGTH}" EQUAL 2)
  70. # if a compiler was specified by the user but without path,
  71. # now try to find it with the full path
  72. # if it is found, force it into the cache,
  73. # if not, don't overwrite the setting (which was given by the user) with "NOTFOUND"
  74. # if the CXX compiler already had a path, reuse it for searching the C compiler
  75. GET_FILENAME_COMPONENT(_CMAKE_USER_CXX_COMPILER_PATH "${CMAKE_CXX_COMPILER}" PATH)
  76. IF(NOT _CMAKE_USER_CXX_COMPILER_PATH)
  77. FIND_PROGRAM(CMAKE_CXX_COMPILER_WITH_PATH NAMES ${CMAKE_CXX_COMPILER})
  78. MARK_AS_ADVANCED(CMAKE_CXX_COMPILER_WITH_PATH)
  79. IF(CMAKE_CXX_COMPILER_WITH_PATH)
  80. SET(CMAKE_CXX_COMPILER ${CMAKE_CXX_COMPILER_WITH_PATH} CACHE STRING "CXX compiler" FORCE)
  81. ENDIF(CMAKE_CXX_COMPILER_WITH_PATH)
  82. ENDIF(NOT _CMAKE_USER_CXX_COMPILER_PATH)
  83. ENDIF(NOT CMAKE_CXX_COMPILER)
  84. MARK_AS_ADVANCED(CMAKE_CXX_COMPILER)
  85. IF (NOT _CMAKE_TOOLCHAIN_LOCATION)
  86. GET_FILENAME_COMPONENT(_CMAKE_TOOLCHAIN_LOCATION "${CMAKE_CXX_COMPILER}" PATH)
  87. ENDIF (NOT _CMAKE_TOOLCHAIN_LOCATION)
  88. # This block was used before the compiler was identified by building a
  89. # source file. Unless g++ crashes when building a small C++
  90. # executable this should no longer be needed.
  91. #
  92. # The g++ that comes with BeOS 5 segfaults if you run "g++ -E"
  93. # ("gcc -E" is fine), which throws up a system dialog box that hangs cmake
  94. # until the user clicks "OK"...so for now, we just assume it's g++.
  95. # IF(BEOS)
  96. # SET(CMAKE_COMPILER_IS_GNUCXX 1)
  97. # SET(CMAKE_COMPILER_IS_GNUCXX_RUN 1)
  98. # ENDIF(BEOS)
  99. # Build a small source file to identify the compiler.
  100. IF(${CMAKE_GENERATOR} MATCHES "Visual Studio")
  101. SET(CMAKE_CXX_COMPILER_ID_RUN 1)
  102. SET(CMAKE_CXX_PLATFORM_ID "Windows")
  103. SET(CMAKE_CXX_COMPILER_ID "MSVC")
  104. ENDIF(${CMAKE_GENERATOR} MATCHES "Visual Studio")
  105. IF(NOT CMAKE_CXX_COMPILER_ID_RUN)
  106. SET(CMAKE_CXX_COMPILER_ID_RUN 1)
  107. # Each entry in this list is a set of extra flags to try
  108. # adding to the compile line to see if it helps produce
  109. # a valid identification file.
  110. SET(CMAKE_CXX_COMPILER_ID_TEST_FLAGS
  111. # Try compiling to an object file only.
  112. "-c"
  113. )
  114. # Try to identify the compiler.
  115. SET(CMAKE_CXX_COMPILER_ID)
  116. FILE(READ ${CMAKE_ROOT}/Modules/CMakePlatformId.h.in
  117. CMAKE_CXX_COMPILER_ID_PLATFORM_CONTENT)
  118. INCLUDE(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerId.cmake)
  119. CMAKE_DETERMINE_COMPILER_ID(CXX CXXFLAGS CMakeCXXCompilerId.cpp)
  120. # Set old compiler and platform id variables.
  121. IF("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
  122. SET(CMAKE_COMPILER_IS_GNUCXX 1)
  123. ENDIF("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
  124. IF("${CMAKE_CXX_PLATFORM_ID}" MATCHES "MinGW")
  125. SET(CMAKE_COMPILER_IS_MINGW 1)
  126. ELSEIF("${CMAKE_CXX_PLATFORM_ID}" MATCHES "Cygwin")
  127. SET(CMAKE_COMPILER_IS_CYGWIN 1)
  128. ENDIF("${CMAKE_CXX_PLATFORM_ID}" MATCHES "MinGW")
  129. ENDIF(NOT CMAKE_CXX_COMPILER_ID_RUN)
  130. # if we have a g++ cross compiler, they have usually some prefix, like
  131. # e.g. powerpc-linux-g++, arm-elf-g++ or i586-mingw32msvc-g++ , optionally
  132. # with a 3-component version number at the end (e.g. arm-eabi-gcc-4.5.2).
  133. # The other tools of the toolchain usually have the same prefix
  134. # NAME_WE cannot be used since then this test will fail for names lile
  135. # "arm-unknown-nto-qnx6.3.0-gcc.exe", where BASENAME would be
  136. # "arm-unknown-nto-qnx6" instead of the correct "arm-unknown-nto-qnx6.3.0-"
  137. IF (CMAKE_CROSSCOMPILING
  138. AND "${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU"
  139. AND NOT _CMAKE_TOOLCHAIN_PREFIX)
  140. GET_FILENAME_COMPONENT(COMPILER_BASENAME "${CMAKE_CXX_COMPILER}" NAME)
  141. IF (COMPILER_BASENAME MATCHES "^(.+-)[gc]\\+\\+(-[0-9]+\\.[0-9]+\\.[0-9]+)?(\\.exe)?$")
  142. SET(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
  143. ENDIF (COMPILER_BASENAME MATCHES "^(.+-)[gc]\\+\\+(-[0-9]+\\.[0-9]+\\.[0-9]+)?(\\.exe)?$")
  144. # if "llvm-" is part of the prefix, remove it, since llvm doesn't have its own binutils
  145. # but uses the regular ar, objcopy, etc. (instead of llvm-objcopy etc.)
  146. IF ("${_CMAKE_TOOLCHAIN_PREFIX}" MATCHES "(.+-)?llvm-$")
  147. SET(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
  148. ENDIF ("${_CMAKE_TOOLCHAIN_PREFIX}" MATCHES "(.+-)?llvm-$")
  149. ENDIF (CMAKE_CROSSCOMPILING
  150. AND "${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU"
  151. AND NOT _CMAKE_TOOLCHAIN_PREFIX)
  152. INCLUDE(${CMAKE_ROOT}/Modules/CMakeClDeps.cmake)
  153. INCLUDE(CMakeFindBinUtils)
  154. IF(MSVC_CXX_ARCHITECTURE_ID)
  155. SET(SET_MSVC_CXX_ARCHITECTURE_ID
  156. "SET(MSVC_CXX_ARCHITECTURE_ID ${MSVC_CXX_ARCHITECTURE_ID})")
  157. ENDIF(MSVC_CXX_ARCHITECTURE_ID)
  158. # configure all variables set in this file
  159. CONFIGURE_FILE(${CMAKE_ROOT}/Modules/CMakeCXXCompiler.cmake.in
  160. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeCXXCompiler.cmake
  161. @ONLY IMMEDIATE # IMMEDIATE must be here for compatibility mode <= 2.0
  162. )
  163. SET(CMAKE_CXX_COMPILER_ENV_VAR "CXX")