CMakeDetermineCXXCompiler.cmake 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. # determine the compiler to use for C++ programs
  2. # NOTE, a generator may set CMAKE_CXX_COMPILER before
  3. # loading this file to force a compiler.
  4. # use environment variable CXX first if defined by user, next use
  5. # the cmake variable CMAKE_GENERATOR_CXX which can be defined by a generator
  6. # as a default compiler
  7. # If the internal cmake variable _CMAKE_TOOLCHAIN_PREFIX is set, this is used
  8. # as prefix for the tools (e.g. arm-elf-g++, arm-elf-ar etc.)
  9. #
  10. # Sets the following variables:
  11. # CMAKE_CXX_COMPILER
  12. # CMAKE_COMPILER_IS_GNUCXX
  13. # CMAKE_AR
  14. # CMAKE_RANLIB
  15. #
  16. # If not already set before, it also sets
  17. # _CMAKE_TOOLCHAIN_PREFIX
  18. IF(NOT CMAKE_CXX_COMPILER)
  19. SET(CMAKE_CXX_COMPILER_INIT NOTFOUND)
  20. # prefer the environment variable CXX
  21. IF($ENV{CXX} MATCHES ".+")
  22. GET_FILENAME_COMPONENT(CMAKE_CXX_COMPILER_INIT $ENV{CXX} PROGRAM PROGRAM_ARGS CMAKE_CXX_FLAGS_ENV_INIT)
  23. IF(CMAKE_CXX_FLAGS_ENV_INIT)
  24. SET(CMAKE_CXX_COMPILER_ARG1 "${CMAKE_CXX_FLAGS_ENV_INIT}" CACHE STRING "First argument to CXX compiler")
  25. ENDIF(CMAKE_CXX_FLAGS_ENV_INIT)
  26. IF(NOT EXISTS ${CMAKE_CXX_COMPILER_INIT})
  27. MESSAGE(FATAL_ERROR "Could not find compiler set in environment variable CXX:\n$ENV{CXX}.\n${CMAKE_CXX_COMPILER_INIT}")
  28. ENDIF(NOT EXISTS ${CMAKE_CXX_COMPILER_INIT})
  29. ENDIF($ENV{CXX} MATCHES ".+")
  30. # next prefer the generator specified compiler
  31. IF(CMAKE_GENERATOR_CXX)
  32. IF(NOT CMAKE_CXX_COMPILER_INIT)
  33. SET(CMAKE_CXX_COMPILER_INIT ${CMAKE_GENERATOR_CXX})
  34. ENDIF(NOT CMAKE_CXX_COMPILER_INIT)
  35. ENDIF(CMAKE_GENERATOR_CXX)
  36. # finally list compilers to try
  37. IF(CMAKE_CXX_COMPILER_INIT)
  38. SET(CMAKE_CXX_COMPILER_LIST ${CMAKE_CXX_COMPILER_INIT})
  39. ELSE(CMAKE_CXX_COMPILER_INIT)
  40. SET(CMAKE_CXX_COMPILER_LIST ${_CMAKE_TOOLCHAIN_PREFIX}c++ ${_CMAKE_TOOLCHAIN_PREFIX}g++ CC aCC cl bcc xlC)
  41. ENDIF(CMAKE_CXX_COMPILER_INIT)
  42. # Find the compiler.
  43. IF (_CMAKE_USER_C_COMPILER_PATH)
  44. FIND_PROGRAM(CMAKE_CXX_COMPILER NAMES ${CMAKE_CXX_COMPILER_LIST} PATHS ${_CMAKE_USER_C_COMPILER_PATH} DOC "C++ compiler" NO_DEFAULT_PATH)
  45. ENDIF (_CMAKE_USER_C_COMPILER_PATH)
  46. FIND_PROGRAM(CMAKE_CXX_COMPILER NAMES ${CMAKE_CXX_COMPILER_LIST} DOC "C++ compiler")
  47. IF(CMAKE_CXX_COMPILER_INIT AND NOT CMAKE_CXX_COMPILER)
  48. SET(CMAKE_CXX_COMPILER "${CMAKE_CXX_COMPILER_INIT}" CACHE FILEPATH "C++ compiler" FORCE)
  49. ENDIF(CMAKE_CXX_COMPILER_INIT AND NOT CMAKE_CXX_COMPILER)
  50. ELSE(NOT CMAKE_CXX_COMPILER)
  51. # we only get here if CMAKE_CXX_COMPILER was specified using -D or a pre-made CMakeCache.txt
  52. # (e.g. via ctest) or set in CMAKE_TOOLCHAIN_FILE
  53. #
  54. # if CMAKE_CXX_COMPILER is a list of length 2, use the first item as
  55. # CMAKE_CXX_COMPILER and the 2nd one as CMAKE_CXX_COMPILER_ARG1
  56. LIST(LENGTH CMAKE_CXX_COMPILER _CMAKE_CXX_COMPILER_LIST_LENGTH)
  57. IF("${_CMAKE_CXX_COMPILER_LIST_LENGTH}" EQUAL 2)
  58. LIST(GET CMAKE_CXX_COMPILER 1 CMAKE_CXX_COMPILER_ARG1)
  59. LIST(GET CMAKE_CXX_COMPILER 0 CMAKE_CXX_COMPILER)
  60. ENDIF("${_CMAKE_CXX_COMPILER_LIST_LENGTH}" EQUAL 2)
  61. # if a compiler was specified by the user but without path,
  62. # now try to find it with the full path
  63. # if it is found, force it into the cache,
  64. # if not, don't overwrite the setting (which was given by the user) with "NOTFOUND"
  65. # if the CXX compiler already had a path, reuse it for searching the C compiler
  66. GET_FILENAME_COMPONENT(_CMAKE_USER_CXX_COMPILER_PATH "${CMAKE_CXX_COMPILER}" PATH)
  67. IF(NOT _CMAKE_USER_CXX_COMPILER_PATH)
  68. FIND_PROGRAM(CMAKE_CXX_COMPILER_WITH_PATH NAMES ${CMAKE_CXX_COMPILER})
  69. MARK_AS_ADVANCED(CMAKE_CXX_COMPILER_WITH_PATH)
  70. IF(CMAKE_CXX_COMPILER_WITH_PATH)
  71. SET(CMAKE_CXX_COMPILER ${CMAKE_CXX_COMPILER_WITH_PATH} CACHE STRING "CXX compiler" FORCE)
  72. ENDIF(CMAKE_CXX_COMPILER_WITH_PATH)
  73. ENDIF(NOT _CMAKE_USER_CXX_COMPILER_PATH)
  74. ENDIF(NOT CMAKE_CXX_COMPILER)
  75. MARK_AS_ADVANCED(CMAKE_CXX_COMPILER)
  76. IF (NOT _CMAKE_TOOLCHAIN_LOCATION)
  77. GET_FILENAME_COMPONENT(_CMAKE_TOOLCHAIN_LOCATION "${CMAKE_CXX_COMPILER}" PATH)
  78. ENDIF (NOT _CMAKE_TOOLCHAIN_LOCATION)
  79. # if we have a g++ cross compiler, they have usually some prefix, like
  80. # e.g. powerpc-linux-g++, arm-elf-g++ or i586-mingw32msvc-g++
  81. # the other tools of the toolchain usually have the same prefix
  82. # NAME_WE cannot be used since then this test will fail for names lile
  83. # "arm-unknown-nto-qnx6.3.0-gcc.exe", where BASENAME would be
  84. # "arm-unknown-nto-qnx6" instead of the correct "arm-unknown-nto-qnx6.3.0-"
  85. IF (NOT _CMAKE_TOOLCHAIN_PREFIX)
  86. GET_FILENAME_COMPONENT(COMPILER_BASENAME "${CMAKE_CXX_COMPILER}" NAME)
  87. IF (COMPILER_BASENAME MATCHES "^(.+-)[gc]\\+\\+(\\.exe)?$")
  88. STRING(REGEX REPLACE "^(.+-)[gc]\\+\\+(\\.exe)?$" "\\1" _CMAKE_TOOLCHAIN_PREFIX "${COMPILER_BASENAME}")
  89. ENDIF (COMPILER_BASENAME MATCHES "^(.+-)[gc]\\+\\+(\\.exe)?$")
  90. ENDIF (NOT _CMAKE_TOOLCHAIN_PREFIX)
  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. INCLUDE(CMakeFindBinUtils)
  136. # configure all variables set in this file
  137. CONFIGURE_FILE(${CMAKE_ROOT}/Modules/CMakeCXXCompiler.cmake.in
  138. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeCXXCompiler.cmake
  139. @ONLY IMMEDIATE # IMMEDIATE must be here for compatibility mode <= 2.0
  140. )
  141. SET(CMAKE_CXX_COMPILER_ENV_VAR "CXX")