1 # determine the compiler to use for C++ programs
2 # NOTE, a generator may set CMAKE_CXX_COMPILER before
3 # loading this file to force a compiler.
4 # use environment variable CXX first if defined by user, next use
5 # the cmake variable CMAKE_GENERATOR_CXX which can be defined by a generator
6 # as a default compiler
8 IF(NOT CMAKE_CXX_COMPILER)
9 SET(CMAKE_CXX_COMPILER_INIT NOTFOUND)
11 # prefer the environment variable CXX
12 IF($ENV{CXX} MATCHES ".+")
13 GET_FILENAME_COMPONENT(CMAKE_CXX_COMPILER_INIT $ENV{CXX} PROGRAM PROGRAM_ARGS CMAKE_CXX_FLAGS_ENV_INIT)
14 IF(EXISTS ${CMAKE_CXX_COMPILER_INIT})
15 ELSE(EXISTS ${CMAKE_CXX_COMPILER_INIT})
16 MESSAGE(FATAL_ERROR "Could not find compiler set in environment variable CXX:\n$ENV{CXX}.\n${CMAKE_CXX_COMPILER_INIT}")
17 ENDIF(EXISTS ${CMAKE_CXX_COMPILER_INIT})
18 ENDIF($ENV{CXX} MATCHES ".+")
20 # next prefer the generator specified compiler
21 IF(CMAKE_GENERATOR_CXX)
22 IF(NOT CMAKE_CXX_COMPILER_INIT)
23 SET(CMAKE_CXX_COMPILER_INIT ${CMAKE_GENERATOR_CXX})
24 ENDIF(NOT CMAKE_CXX_COMPILER_INIT)
25 ENDIF(CMAKE_GENERATOR_CXX)
27 # if no compiler has been found yet, then try to find one
28 IF(NOT CMAKE_CXX_COMPILER_INIT)
29 # if not in the envionment then search for the compiler in the path
30 SET(CMAKE_CXX_COMPILER_LIST c++ g++ CC aCC cl bcc xlC)
31 FIND_PROGRAM(CMAKE_CXX_COMPILER_FULLPATH NAMES ${CMAKE_CXX_COMPILER_LIST})
32 GET_FILENAME_COMPONENT(CMAKE_CXX_COMPILER_INIT
33 ${CMAKE_CXX_COMPILER_FULLPATH} NAME)
34 SET(CMAKE_CXX_COMPILER_FULLPATH "${CMAKE_CXX_COMPILER_FULLPATH}" CACHE INTERNAL "full path to the compiler cmake found")
35 ENDIF(NOT CMAKE_CXX_COMPILER_INIT)
36 SET(CMAKE_CXX_COMPILER ${CMAKE_CXX_COMPILER_INIT}
37 CACHE STRING "C++ compiler")
38 ENDIF(NOT CMAKE_CXX_COMPILER)
39 MARK_AS_ADVANCED(CMAKE_CXX_COMPILER)
41 # do not test for GNU if the generator is visual studio
42 IF(${CMAKE_GENERATOR} MATCHES "Visual Studio")
43 SET(CMAKE_COMPILER_IS_GNUCXX_RUN 1)
44 ENDIF(${CMAKE_GENERATOR} MATCHES "Visual Studio")
46 IF(NOT CMAKE_COMPILER_IS_GNUCXX_RUN)
47 # test to see if the cxx compiler is gnu
48 SET(CMAKE_COMPILER_IS_GNUCXX_RUN 1)
49 EXEC_PROGRAM(${CMAKE_CXX_COMPILER} ARGS -E "\"${CMAKE_ROOT}/Modules/CMakeTestGNU.c\"" OUTPUT_VARIABLE CMAKE_COMPILER_OUTPUT RETURN_VALUE CMAKE_COMPILER_RETURN)
50 IF(NOT CMAKE_COMPILER_RETURN)
51 IF("${CMAKE_COMPILER_OUTPUT}" MATCHES ".*THIS_IS_GNU.*" )
52 SET(CMAKE_COMPILER_IS_GNUCXX 1)
53 FILE(APPEND ${CMAKE_BINARY_DIR}/CMakeOutput.log
54 "Determining if the C++ compiler is GNU succeeded with "
55 "the following output:\n${CMAKE_COMPILER_OUTPUT}\n\n")
56 ELSE("${CMAKE_COMPILER_OUTPUT}" MATCHES ".*THIS_IS_GNU.*" )
57 FILE(APPEND ${CMAKE_BINARY_DIR}/CMakeOutput.log
58 "Determining if the C++ compiler is GNU failed with "
59 "the following output:\n${CMAKE_COMPILER_OUTPUT}\n\n")
60 ENDIF("${CMAKE_COMPILER_OUTPUT}" MATCHES ".*THIS_IS_GNU.*" )
61 ENDIF(NOT CMAKE_COMPILER_RETURN)
62 ENDIF(NOT CMAKE_COMPILER_IS_GNUCXX_RUN)
64 # configure all variables set in this file
65 CONFIGURE_FILE(${CMAKE_ROOT}/Modules/CMakeCXXCompiler.cmake.in
66 ${CMAKE_BINARY_DIR}/CMakeCXXCompiler.cmake IMMEDIATE)
68 SET(CMAKE_CXX_COMPILER_ENV_VAR "CXX")