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