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