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 # if we have a g++ cross compiler, they have usually some prefix, like
92 # e.g. powerpc-linux-g++, arm-elf-g++ or i586-mingw32msvc-g++
93 # the other tools of the toolchain usually have the same prefix
94 IF (NOT _CMAKE_TOOLCHAIN_PREFIX)
95 GET_FILENAME_COMPONENT(COMPILER_BASENAME "${CMAKE_CXX_COMPILER}" NAME_WE)
96 IF (COMPILER_BASENAME MATCHES "^(.+-)[gc]\\+\\+")
97 STRING(REGEX REPLACE "^(.+-)[gc]\\+\\+" "\\1" _CMAKE_TOOLCHAIN_PREFIX "${COMPILER_BASENAME}")
98 ENDIF (COMPILER_BASENAME MATCHES "^(.+-)[gc]\\+\\+")
99 ENDIF (NOT _CMAKE_TOOLCHAIN_PREFIX)
101 # This block was used before the compiler was identified by building a
102 # source file. Unless g++ crashes when building a small C++
103 # executable this should no longer be needed.
105 # The g++ that comes with BeOS 5 segfaults if you run "g++ -E"
106 # ("gcc -E" is fine), which throws up a system dialog box that hangs cmake
107 # until the user clicks "OK"...so for now, we just assume it's g++.
109 # SET(CMAKE_COMPILER_IS_GNUCXX 1)
110 # SET(CMAKE_COMPILER_IS_GNUCXX_RUN 1)
113 # Build a small source file to identify the compiler.
114 IF(${CMAKE_GENERATOR} MATCHES "Visual Studio")
115 SET(CMAKE_CXX_COMPILER_ID_RUN 1)
116 SET(CMAKE_CXX_PLATFORM_ID "Windows")
118 # TODO: Set the compiler id. It is probably MSVC but
119 # the user may be using an integrated Intel compiler.
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 # Try to identify the compiler.
126 SET(CMAKE_CXX_COMPILER_ID)
127 INCLUDE(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerId.cmake)
128 CMAKE_DETERMINE_COMPILER_ID(CXX CXXFLAGS ${CMAKE_ROOT}/Modules/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 INCLUDE(CMakeFindBinUtils)
143 # configure all variables set in this file
144 CONFIGURE_FILE(${CMAKE_ROOT}/Modules/CMakeCXXCompiler.cmake.in
145 ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeCXXCompiler.cmake IMMEDIATE)
147 SET(CMAKE_CXX_COMPILER_ENV_VAR "CXX")