CMakeDetermineCXXCompiler.cmake 7.3 KB

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