2 # determine the compiler to use for C programs
3 # NOTE, a generator may set CMAKE_C_COMPILER before
4 # loading this file to force a compiler.
5 # use environment variable CC first if defined by user, next use
6 # the cmake variable CMAKE_GENERATOR_CC 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-gcc, arm-elf-ar etc.). This works
10 # currently with the GNU crosscompilers.
12 # Sets the following variables:
16 # CMAKE_COMPILER_IS_GNUCC
18 # If not already set before, it also sets
19 # _CMAKE_TOOLCHAIN_PREFIX
21 IF(NOT CMAKE_C_COMPILER)
22 SET(CMAKE_CXX_COMPILER_INIT NOTFOUND)
24 # prefer the environment variable CC
25 IF($ENV{CC} MATCHES ".+")
26 GET_FILENAME_COMPONENT(CMAKE_C_COMPILER_INIT $ENV{CC} PROGRAM PROGRAM_ARGS CMAKE_C_FLAGS_ENV_INIT)
27 IF(CMAKE_C_FLAGS_ENV_INIT)
28 SET(CMAKE_C_COMPILER_ARG1 "${CMAKE_C_FLAGS_ENV_INIT}" CACHE STRING "First argument to C compiler")
29 ENDIF(CMAKE_C_FLAGS_ENV_INIT)
30 IF(NOT EXISTS ${CMAKE_C_COMPILER_INIT})
31 MESSAGE(FATAL_ERROR "Could not find compiler set in environment variable CC:\n$ENV{CC}.")
32 ENDIF(NOT EXISTS ${CMAKE_C_COMPILER_INIT})
33 ENDIF($ENV{CC} MATCHES ".+")
35 # next try prefer the compiler specified by the generator
36 IF(CMAKE_GENERATOR_CC)
37 IF(NOT CMAKE_C_COMPILER_INIT)
38 SET(CMAKE_C_COMPILER_INIT ${CMAKE_GENERATOR_CC})
39 ENDIF(NOT CMAKE_C_COMPILER_INIT)
40 ENDIF(CMAKE_GENERATOR_CC)
42 # finally list compilers to try
43 IF(CMAKE_C_COMPILER_INIT)
44 SET(CMAKE_C_COMPILER_LIST ${CMAKE_C_COMPILER_INIT})
45 ELSE(CMAKE_C_COMPILER_INIT)
46 SET(CMAKE_C_COMPILER_LIST ${_CMAKE_TOOLCHAIN_PREFIX}gcc ${_CMAKE_TOOLCHAIN_PREFIX}cc cl bcc xlc)
47 ENDIF(CMAKE_C_COMPILER_INIT)
50 IF (_CMAKE_USER_CXX_COMPILER_PATH)
51 FIND_PROGRAM(CMAKE_C_COMPILER NAMES ${CMAKE_C_COMPILER_LIST} PATHS ${_CMAKE_USER_CXX_COMPILER_PATH} DOC "C compiler" NO_DEFAULT_PATH)
52 ENDIF (_CMAKE_USER_CXX_COMPILER_PATH)
53 FIND_PROGRAM(CMAKE_C_COMPILER NAMES ${CMAKE_C_COMPILER_LIST} DOC "C compiler")
55 IF(CMAKE_C_COMPILER_INIT AND NOT CMAKE_C_COMPILER)
56 SET(CMAKE_C_COMPILER "${CMAKE_C_COMPILER_INIT}" CACHE FILEPATH "C compiler" FORCE)
57 ENDIF(CMAKE_C_COMPILER_INIT AND NOT CMAKE_C_COMPILER)
58 ELSE(NOT CMAKE_C_COMPILER)
60 # we only get here if CMAKE_C_COMPILER was specified using -D or a pre-made CMakeCache.txt
61 # (e.g. via ctest) or set in CMAKE_TOOLCHAIN_FILE
62 # if CMAKE_C_COMPILER is a list of length 2, use the first item as
63 # CMAKE_C_COMPILER and the 2nd one as CMAKE_C_COMPILER_ARG1
65 LIST(LENGTH CMAKE_C_COMPILER _CMAKE_C_COMPILER_LIST_LENGTH)
66 IF("${_CMAKE_C_COMPILER_LIST_LENGTH}" EQUAL 2)
67 LIST(GET CMAKE_C_COMPILER 1 CMAKE_C_COMPILER_ARG1)
68 LIST(GET CMAKE_C_COMPILER 0 CMAKE_C_COMPILER)
69 ENDIF("${_CMAKE_C_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 C compiler already had a path, reuse it for searching the CXX compiler
76 GET_FILENAME_COMPONENT(_CMAKE_USER_C_COMPILER_PATH "${CMAKE_C_COMPILER}" PATH)
77 IF(NOT _CMAKE_USER_C_COMPILER_PATH)
78 FIND_PROGRAM(CMAKE_C_COMPILER_WITH_PATH NAMES ${CMAKE_C_COMPILER})
79 MARK_AS_ADVANCED(CMAKE_C_COMPILER_WITH_PATH)
80 IF(CMAKE_C_COMPILER_WITH_PATH)
81 SET(CMAKE_C_COMPILER ${CMAKE_C_COMPILER_WITH_PATH} CACHE STRING "C compiler" FORCE)
82 ENDIF(CMAKE_C_COMPILER_WITH_PATH)
83 ENDIF(NOT _CMAKE_USER_C_COMPILER_PATH)
84 ENDIF(NOT CMAKE_C_COMPILER)
85 MARK_AS_ADVANCED(CMAKE_C_COMPILER)
87 IF (NOT _CMAKE_TOOLCHAIN_LOCATION)
88 GET_FILENAME_COMPONENT(_CMAKE_TOOLCHAIN_LOCATION "${CMAKE_C_COMPILER}" PATH)
89 ENDIF (NOT _CMAKE_TOOLCHAIN_LOCATION)
91 # Build a small source file to identify the compiler.
92 IF(${CMAKE_GENERATOR} MATCHES "Visual Studio")
93 SET(CMAKE_C_COMPILER_ID_RUN 1)
94 SET(CMAKE_C_PLATFORM_ID "Windows")
96 # TODO: Set the compiler id. It is probably MSVC but
97 # the user may be using an integrated Intel compiler.
98 # SET(CMAKE_C_COMPILER_ID "MSVC")
99 ENDIF(${CMAKE_GENERATOR} MATCHES "Visual Studio")
101 IF(NOT CMAKE_C_COMPILER_ID_RUN)
102 SET(CMAKE_C_COMPILER_ID_RUN 1)
104 # Each entry in this list is a set of extra flags to try
105 # adding to the compile line to see if it helps produce
106 # a valid identification file.
107 SET(CMAKE_C_COMPILER_ID_TEST_FLAGS
108 # Try compiling to an object file only.
112 # Try to identify the compiler.
113 SET(CMAKE_C_COMPILER_ID)
114 FILE(READ ${CMAKE_ROOT}/Modules/CMakePlatformId.h.in
115 CMAKE_C_COMPILER_ID_PLATFORM_CONTENT)
116 INCLUDE(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerId.cmake)
117 CMAKE_DETERMINE_COMPILER_ID(C CFLAGS CMakeCCompilerId.c)
119 # Set old compiler and platform id variables.
120 IF("${CMAKE_C_COMPILER_ID}" MATCHES "GNU")
121 SET(CMAKE_COMPILER_IS_GNUCC 1)
122 ENDIF("${CMAKE_C_COMPILER_ID}" MATCHES "GNU")
123 IF("${CMAKE_C_PLATFORM_ID}" MATCHES "MinGW")
124 SET(CMAKE_COMPILER_IS_MINGW 1)
125 ELSEIF("${CMAKE_C_PLATFORM_ID}" MATCHES "Cygwin")
126 SET(CMAKE_COMPILER_IS_CYGWIN 1)
127 ENDIF("${CMAKE_C_PLATFORM_ID}" MATCHES "MinGW")
128 ENDIF(NOT CMAKE_C_COMPILER_ID_RUN)
130 # If we have a gcc cross compiler, they have usually some prefix, like
131 # e.g. powerpc-linux-gcc, arm-elf-gcc or i586-mingw32msvc-gcc .
132 # The other tools of the toolchain usually have the same prefix
133 # NAME_WE cannot be used since then this test will fail for names lile
134 # "arm-unknown-nto-qnx6.3.0-gcc.exe", where BASENAME would be
135 # "arm-unknown-nto-qnx6" instead of the correct "arm-unknown-nto-qnx6.3.0-"
136 IF (CMAKE_CROSSCOMPILING
137 AND "${CMAKE_C_COMPILER_ID}" MATCHES "GNU"
138 AND NOT _CMAKE_TOOLCHAIN_PREFIX)
139 GET_FILENAME_COMPONENT(COMPILER_BASENAME "${CMAKE_C_COMPILER}" NAME)
140 IF (COMPILER_BASENAME MATCHES "^(.+-)g?cc(\\.exe)?$")
141 SET(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
142 ENDIF (COMPILER_BASENAME MATCHES "^(.+-)g?cc(\\.exe)?$")
144 # if "llvm-" is part of the prefix, remove it, since llvm doesn't have its own binutils
145 # but uses the regular ar, objcopy, etc. (instead of llvm-objcopy etc.)
146 IF ("${_CMAKE_TOOLCHAIN_PREFIX}" MATCHES "(.+-)?llvm-$")
147 SET(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
148 ENDIF ("${_CMAKE_TOOLCHAIN_PREFIX}" MATCHES "(.+-)?llvm-$")
150 ENDIF (CMAKE_CROSSCOMPILING
151 AND "${CMAKE_C_COMPILER_ID}" MATCHES "GNU"
152 AND NOT _CMAKE_TOOLCHAIN_PREFIX)
157 INCLUDE(CMakeFindBinUtils)
159 # configure variables set in this file for fast reload later on
160 CONFIGURE_FILE(${CMAKE_ROOT}/Modules/CMakeCCompiler.cmake.in
161 "${CMAKE_PLATFORM_ROOT_BIN}/CMakeCCompiler.cmake"
162 @ONLY IMMEDIATE # IMMEDIATE must be here for compatibility mode <= 2.0
165 SET(CMAKE_C_COMPILER_ENV_VAR "CC")