2 #=============================================================================
3 # Copyright 2002-2009 Kitware, Inc.
5 # Distributed under the OSI-approved BSD License (the "License");
6 # see accompanying file Copyright.txt for details.
8 # This software is distributed WITHOUT ANY WARRANTY; without even the
9 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 # See the License for more information.
11 #=============================================================================
12 # (To distribute this file outside of CMake, substitute the full
13 # License text for the above reference.)
15 # determine the compiler to use for C++ programs
16 # NOTE, a generator may set CMAKE_CXX_COMPILER before
17 # loading this file to force a compiler.
18 # use environment variable CXX first if defined by user, next use
19 # the cmake variable CMAKE_GENERATOR_CXX which can be defined by a generator
20 # as a default compiler
21 # If the internal cmake variable _CMAKE_TOOLCHAIN_PREFIX is set, this is used
22 # as prefix for the tools (e.g. arm-elf-g++, arm-elf-ar etc.)
24 # Sets the following variables:
26 # CMAKE_COMPILER_IS_GNUCXX
30 # If not already set before, it also sets
31 # _CMAKE_TOOLCHAIN_PREFIX
33 include(${CMAKE_ROOT}/Modules/CMakeDetermineCompiler.cmake)
35 # Load system-specific compiler preferences for this language.
36 include(Platform/${CMAKE_SYSTEM_NAME}-CXX OPTIONAL)
37 if(NOT CMAKE_CXX_COMPILER_NAMES)
38 set(CMAKE_CXX_COMPILER_NAMES CC)
41 if(${CMAKE_GENERATOR} MATCHES "Visual Studio")
42 elseif("${CMAKE_GENERATOR}" MATCHES "Xcode")
43 set(CMAKE_CXX_COMPILER_XCODE_TYPE sourcecode.cpp.cpp)
45 if(NOT CMAKE_CXX_COMPILER)
46 set(CMAKE_CXX_COMPILER_INIT NOTFOUND)
48 # prefer the environment variable CXX
49 if($ENV{CXX} MATCHES ".+")
50 get_filename_component(CMAKE_CXX_COMPILER_INIT $ENV{CXX} PROGRAM PROGRAM_ARGS CMAKE_CXX_FLAGS_ENV_INIT)
51 if(CMAKE_CXX_FLAGS_ENV_INIT)
52 set(CMAKE_CXX_COMPILER_ARG1 "${CMAKE_CXX_FLAGS_ENV_INIT}" CACHE STRING "First argument to CXX compiler")
54 if(NOT EXISTS ${CMAKE_CXX_COMPILER_INIT})
55 message(FATAL_ERROR "Could not find compiler set in environment variable CXX:\n$ENV{CXX}.\n${CMAKE_CXX_COMPILER_INIT}")
59 # next prefer the generator specified compiler
60 if(CMAKE_GENERATOR_CXX)
61 if(NOT CMAKE_CXX_COMPILER_INIT)
62 set(CMAKE_CXX_COMPILER_INIT ${CMAKE_GENERATOR_CXX})
66 # finally list compilers to try
67 if(NOT CMAKE_CXX_COMPILER_INIT)
68 set(CMAKE_CXX_COMPILER_LIST CC ${_CMAKE_TOOLCHAIN_PREFIX}c++ ${_CMAKE_TOOLCHAIN_PREFIX}g++ aCC cl bcc xlC clang++)
71 _cmake_find_compiler(CXX)
74 # we only get here if CMAKE_CXX_COMPILER was specified using -D or a pre-made CMakeCache.txt
75 # (e.g. via ctest) or set in CMAKE_TOOLCHAIN_FILE
77 # if CMAKE_CXX_COMPILER is a list of length 2, use the first item as
78 # CMAKE_CXX_COMPILER and the 2nd one as CMAKE_CXX_COMPILER_ARG1
80 list(LENGTH CMAKE_CXX_COMPILER _CMAKE_CXX_COMPILER_LIST_LENGTH)
81 if("${_CMAKE_CXX_COMPILER_LIST_LENGTH}" EQUAL 2)
82 list(GET CMAKE_CXX_COMPILER 1 CMAKE_CXX_COMPILER_ARG1)
83 list(GET CMAKE_CXX_COMPILER 0 CMAKE_CXX_COMPILER)
86 # if a compiler was specified by the user but without path,
87 # now try to find it with the full path
88 # if it is found, force it into the cache,
89 # if not, don't overwrite the setting (which was given by the user) with "NOTFOUND"
90 # if the CXX compiler already had a path, reuse it for searching the C compiler
91 get_filename_component(_CMAKE_USER_CXX_COMPILER_PATH "${CMAKE_CXX_COMPILER}" PATH)
92 if(NOT _CMAKE_USER_CXX_COMPILER_PATH)
93 find_program(CMAKE_CXX_COMPILER_WITH_PATH NAMES ${CMAKE_CXX_COMPILER})
94 mark_as_advanced(CMAKE_CXX_COMPILER_WITH_PATH)
95 if(CMAKE_CXX_COMPILER_WITH_PATH)
96 set(CMAKE_CXX_COMPILER ${CMAKE_CXX_COMPILER_WITH_PATH} CACHE STRING "CXX compiler" FORCE)
100 mark_as_advanced(CMAKE_CXX_COMPILER)
102 # Each entry in this list is a set of extra flags to try
103 # adding to the compile line to see if it helps produce
104 # a valid identification file.
105 set(CMAKE_CXX_COMPILER_ID_TEST_FLAGS
106 # Try compiling to an object file only.
111 # Build a small source file to identify the compiler.
112 if(NOT CMAKE_CXX_COMPILER_ID_RUN)
113 set(CMAKE_CXX_COMPILER_ID_RUN 1)
115 # Try to identify the compiler.
116 set(CMAKE_CXX_COMPILER_ID)
117 file(READ ${CMAKE_ROOT}/Modules/CMakePlatformId.h.in
118 CMAKE_CXX_COMPILER_ID_PLATFORM_CONTENT)
119 include(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerId.cmake)
120 CMAKE_DETERMINE_COMPILER_ID(CXX CXXFLAGS CMakeCXXCompilerId.cpp)
122 # Set old compiler and platform id variables.
123 if("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
124 set(CMAKE_COMPILER_IS_GNUCXX 1)
126 if("${CMAKE_CXX_PLATFORM_ID}" MATCHES "MinGW")
127 set(CMAKE_COMPILER_IS_MINGW 1)
128 elseif("${CMAKE_CXX_PLATFORM_ID}" MATCHES "Cygwin")
129 set(CMAKE_COMPILER_IS_CYGWIN 1)
133 if (NOT _CMAKE_TOOLCHAIN_LOCATION)
134 get_filename_component(_CMAKE_TOOLCHAIN_LOCATION "${CMAKE_CXX_COMPILER}" PATH)
137 # if we have a g++ cross compiler, they have usually some prefix, like
138 # e.g. powerpc-linux-g++, arm-elf-g++ or i586-mingw32msvc-g++ , optionally
139 # with a 3-component version number at the end (e.g. arm-eabi-gcc-4.5.2).
140 # The other tools of the toolchain usually have the same prefix
141 # NAME_WE cannot be used since then this test will fail for names lile
142 # "arm-unknown-nto-qnx6.3.0-gcc.exe", where BASENAME would be
143 # "arm-unknown-nto-qnx6" instead of the correct "arm-unknown-nto-qnx6.3.0-"
144 if (CMAKE_CROSSCOMPILING
145 AND "${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU"
146 AND NOT _CMAKE_TOOLCHAIN_PREFIX)
147 get_filename_component(COMPILER_BASENAME "${CMAKE_CXX_COMPILER}" NAME)
148 if (COMPILER_BASENAME MATCHES "^(.+-)[gc]\\+\\+(-[0-9]+\\.[0-9]+\\.[0-9]+)?(\\.exe)?$")
149 set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
152 # if "llvm-" is part of the prefix, remove it, since llvm doesn't have its own binutils
153 # but uses the regular ar, objcopy, etc. (instead of llvm-objcopy etc.)
154 if ("${_CMAKE_TOOLCHAIN_PREFIX}" MATCHES "(.+-)?llvm-$")
155 set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
160 include(${CMAKE_ROOT}/Modules/CMakeClDeps.cmake)
161 include(CMakeFindBinUtils)
162 if(MSVC_CXX_ARCHITECTURE_ID)
163 set(SET_MSVC_CXX_ARCHITECTURE_ID
164 "set(MSVC_CXX_ARCHITECTURE_ID ${MSVC_CXX_ARCHITECTURE_ID})")
166 # configure all variables set in this file
167 configure_file(${CMAKE_ROOT}/Modules/CMakeCXXCompiler.cmake.in
168 ${CMAKE_PLATFORM_INFO_DIR}/CMakeCXXCompiler.cmake
169 @ONLY IMMEDIATE # IMMEDIATE must be here for compatibility mode <= 2.0
172 set(CMAKE_CXX_COMPILER_ENV_VAR "CXX")