2 # determine the compiler to use for C++ programs
3 # NOTE, a generator may set CMAKE_CXX_COMPILER before
4 # loading this file to force a compiler.
5 # use environment variable CXX first if defined by user, next use
6 # the cmake variable CMAKE_GENERATOR_CXX which can be defined by a generator
7 # as a default compiler
8 # If the internal cmake variable _CMAKE_TOOLCHAIN_PREFIX is set, this is used
9 # as prefix for the tools (e.g. arm-elf-g++, arm-elf-ar etc.)
11 # Sets the following variables:
13 # CMAKE_COMPILER_IS_GNUCXX
17 # If not already set before, it also sets
18 # _CMAKE_TOOLCHAIN_PREFIX
20 IF(NOT CMAKE_CXX_COMPILER)
21 SET(CMAKE_CXX_COMPILER_INIT NOTFOUND)
23 # prefer the environment variable CXX
24 IF($ENV{CXX} MATCHES ".+")
25 GET_FILENAME_COMPONENT(CMAKE_CXX_COMPILER_INIT $ENV{CXX} PROGRAM PROGRAM_ARGS CMAKE_CXX_FLAGS_ENV_INIT)
26 IF(CMAKE_CXX_FLAGS_ENV_INIT)
27 SET(CMAKE_CXX_COMPILER_ARG1 "${CMAKE_CXX_FLAGS_ENV_INIT}" CACHE STRING "First argument to CXX compiler")
28 ENDIF(CMAKE_CXX_FLAGS_ENV_INIT)
29 IF(NOT EXISTS ${CMAKE_CXX_COMPILER_INIT})
30 MESSAGE(FATAL_ERROR "Could not find compiler set in environment variable CXX:\n$ENV{CXX}.\n${CMAKE_CXX_COMPILER_INIT}")
31 ENDIF(NOT EXISTS ${CMAKE_CXX_COMPILER_INIT})
32 ENDIF($ENV{CXX} MATCHES ".+")
34 # next prefer the generator specified compiler
35 IF(CMAKE_GENERATOR_CXX)
36 IF(NOT CMAKE_CXX_COMPILER_INIT)
37 SET(CMAKE_CXX_COMPILER_INIT ${CMAKE_GENERATOR_CXX})
38 ENDIF(NOT CMAKE_CXX_COMPILER_INIT)
39 ENDIF(CMAKE_GENERATOR_CXX)
41 # finally list compilers to try
42 IF(CMAKE_CXX_COMPILER_INIT)
43 SET(CMAKE_CXX_COMPILER_LIST ${CMAKE_CXX_COMPILER_INIT})
44 ELSE(CMAKE_CXX_COMPILER_INIT)
45 SET(CMAKE_CXX_COMPILER_LIST ${_CMAKE_TOOLCHAIN_PREFIX}c++ ${_CMAKE_TOOLCHAIN_PREFIX}g++ CC aCC cl bcc xlC)
46 ENDIF(CMAKE_CXX_COMPILER_INIT)
49 IF (_CMAKE_USER_C_COMPILER_PATH)
50 FIND_PROGRAM(CMAKE_CXX_COMPILER NAMES ${CMAKE_CXX_COMPILER_LIST} PATHS ${_CMAKE_USER_C_COMPILER_PATH} DOC "C++ compiler" NO_DEFAULT_PATH)
51 ENDIF (_CMAKE_USER_C_COMPILER_PATH)
52 FIND_PROGRAM(CMAKE_CXX_COMPILER NAMES ${CMAKE_CXX_COMPILER_LIST} DOC "C++ compiler")
54 IF(CMAKE_CXX_COMPILER_INIT AND NOT CMAKE_CXX_COMPILER)
55 SET(CMAKE_CXX_COMPILER "${CMAKE_CXX_COMPILER_INIT}" CACHE FILEPATH "C++ compiler" FORCE)
56 ENDIF(CMAKE_CXX_COMPILER_INIT AND NOT CMAKE_CXX_COMPILER)
57 ELSE(NOT CMAKE_CXX_COMPILER)
59 # we only get here if CMAKE_CXX_COMPILER was specified using -D or a pre-made CMakeCache.txt
60 # (e.g. via ctest) or set in CMAKE_TOOLCHAIN_FILE
62 # if CMAKE_CXX_COMPILER is a list of length 2, use the first item as
63 # CMAKE_CXX_COMPILER and the 2nd one as CMAKE_CXX_COMPILER_ARG1
65 LIST(LENGTH CMAKE_CXX_COMPILER _CMAKE_CXX_COMPILER_LIST_LENGTH)
66 IF("${_CMAKE_CXX_COMPILER_LIST_LENGTH}" EQUAL 2)
67 LIST(GET CMAKE_CXX_COMPILER 1 CMAKE_CXX_COMPILER_ARG1)
68 LIST(GET CMAKE_CXX_COMPILER 0 CMAKE_CXX_COMPILER)
69 ENDIF("${_CMAKE_CXX_COMPILER_LIST_LENGTH}" EQUAL 2)
71 # if a compiler was specified by the user but without path,
72 # now try to find it with the full path
73 # if it is found, force it into the cache,
74 # if not, don't overwrite the setting (which was given by the user) with "NOTFOUND"
75 # if the CXX compiler already had a path, reuse it for searching the C compiler
76 GET_FILENAME_COMPONENT(_CMAKE_USER_CXX_COMPILER_PATH "${CMAKE_CXX_COMPILER}" PATH)
77 IF(NOT _CMAKE_USER_CXX_COMPILER_PATH)
78 FIND_PROGRAM(CMAKE_CXX_COMPILER_WITH_PATH NAMES ${CMAKE_CXX_COMPILER})
79 MARK_AS_ADVANCED(CMAKE_CXX_COMPILER_WITH_PATH)
80 IF(CMAKE_CXX_COMPILER_WITH_PATH)
81 SET(CMAKE_CXX_COMPILER ${CMAKE_CXX_COMPILER_WITH_PATH} CACHE STRING "CXX compiler" FORCE)
82 ENDIF(CMAKE_CXX_COMPILER_WITH_PATH)
83 ENDIF(NOT _CMAKE_USER_CXX_COMPILER_PATH)
84 ENDIF(NOT CMAKE_CXX_COMPILER)
85 MARK_AS_ADVANCED(CMAKE_CXX_COMPILER)
87 IF (NOT _CMAKE_TOOLCHAIN_LOCATION)
88 GET_FILENAME_COMPONENT(_CMAKE_TOOLCHAIN_LOCATION "${CMAKE_CXX_COMPILER}" PATH)
89 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.
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++.
99 # SET(CMAKE_COMPILER_IS_GNUCXX 1)
100 # SET(CMAKE_COMPILER_IS_GNUCXX_RUN 1)
103 # Build a small source file to identify the compiler.
104 IF(${CMAKE_GENERATOR} MATCHES "Visual Studio")
105 SET(CMAKE_CXX_COMPILER_ID_RUN 1)
106 SET(CMAKE_CXX_PLATFORM_ID "Windows")
108 # TODO: Set the compiler id. It is probably MSVC but
109 # the user may be using an integrated Intel compiler.
110 # SET(CMAKE_CXX_COMPILER_ID "MSVC")
111 ENDIF(${CMAKE_GENERATOR} MATCHES "Visual Studio")
112 IF(NOT CMAKE_CXX_COMPILER_ID_RUN)
113 SET(CMAKE_CXX_COMPILER_ID_RUN 1)
115 # Each entry in this list is a set of extra flags to try
116 # adding to the compile line to see if it helps produce
117 # a valid identification file.
118 SET(CMAKE_CXX_COMPILER_ID_TEST_FLAGS
119 # Try compiling to an object file only.
123 # Try to identify the compiler.
124 SET(CMAKE_CXX_COMPILER_ID)
125 FILE(READ ${CMAKE_ROOT}/Modules/CMakePlatformId.h.in
126 CMAKE_CXX_COMPILER_ID_PLATFORM_CONTENT)
127 INCLUDE(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerId.cmake)
128 CMAKE_DETERMINE_COMPILER_ID(CXX CXXFLAGS CMakeCXXCompilerId.cpp)
130 # Set old compiler and platform id variables.
131 IF("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
132 SET(CMAKE_COMPILER_IS_GNUCXX 1)
133 ENDIF("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
134 IF("${CMAKE_CXX_PLATFORM_ID}" MATCHES "MinGW")
135 SET(CMAKE_COMPILER_IS_MINGW 1)
136 ELSEIF("${CMAKE_CXX_PLATFORM_ID}" MATCHES "Cygwin")
137 SET(CMAKE_COMPILER_IS_CYGWIN 1)
138 ENDIF("${CMAKE_CXX_PLATFORM_ID}" MATCHES "MinGW")
139 ENDIF(NOT CMAKE_CXX_COMPILER_ID_RUN)
141 # if we have a g++ cross compiler, they have usually some prefix, like
142 # e.g. powerpc-linux-g++, arm-elf-g++ or i586-mingw32msvc-g++
143 # the other tools of the toolchain usually have the same prefix
144 # NAME_WE cannot be used since then this test will fail for names lile
145 # "arm-unknown-nto-qnx6.3.0-gcc.exe", where BASENAME would be
146 # "arm-unknown-nto-qnx6" instead of the correct "arm-unknown-nto-qnx6.3.0-"
147 IF (CMAKE_CROSSCOMPILING
148 AND "${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU"
149 AND NOT _CMAKE_TOOLCHAIN_PREFIX)
150 GET_FILENAME_COMPONENT(COMPILER_BASENAME "${CMAKE_CXX_COMPILER}" NAME)
151 IF (COMPILER_BASENAME MATCHES "^(.+-)[gc]\\+\\+(\\.exe)?$")
152 SET(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
153 ENDIF (COMPILER_BASENAME MATCHES "^(.+-)[gc]\\+\\+(\\.exe)?$")
155 # if "llvm-" is part of the prefix, remove it, since llvm doesn't have its own binutils
156 # but uses the regular ar, objcopy, etc. (instead of llvm-objcopy etc.)
157 IF ("${_CMAKE_TOOLCHAIN_PREFIX}" MATCHES "(.+-)?llvm-$")
158 SET(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
159 ENDIF ("${_CMAKE_TOOLCHAIN_PREFIX}" MATCHES "(.+-)?llvm-$")
161 ENDIF (CMAKE_CROSSCOMPILING
162 AND "${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU"
163 AND NOT _CMAKE_TOOLCHAIN_PREFIX)
165 INCLUDE(CMakeFindBinUtils)
167 # configure all variables set in this file
168 CONFIGURE_FILE(${CMAKE_ROOT}/Modules/CMakeCXXCompiler.cmake.in
169 ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeCXXCompiler.cmake
170 @ONLY IMMEDIATE # IMMEDIATE must be here for compatibility mode <= 2.0
173 SET(CMAKE_CXX_COMPILER_ENV_VAR "CXX")