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_C_COMPILER before
17 # loading this file to force a compiler.
18 # use environment variable CC first if defined by user, next use
19 # the cmake variable CMAKE_GENERATOR_CC 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-gcc, arm-elf-ar etc.). This works
23 # currently with the GNU crosscompilers.
25 # Sets the following variables:
29 # CMAKE_COMPILER_IS_GNUCC
31 # If not already set before, it also sets
32 # _CMAKE_TOOLCHAIN_PREFIX
34 include(${CMAKE_ROOT}/Modules/CMakeDetermineCompiler.cmake)
36 # Load system-specific compiler preferences for this language.
37 include(Platform/${CMAKE_SYSTEM_NAME}-C OPTIONAL)
38 if(NOT CMAKE_C_COMPILER_NAMES)
39 set(CMAKE_C_COMPILER_NAMES cc)
42 if(${CMAKE_GENERATOR} MATCHES "Visual Studio")
43 elseif("${CMAKE_GENERATOR}" MATCHES "Xcode")
44 set(CMAKE_C_COMPILER_XCODE_TYPE sourcecode.c.c)
46 if(NOT CMAKE_C_COMPILER)
47 set(CMAKE_C_COMPILER_INIT NOTFOUND)
49 # prefer the environment variable CC
50 if($ENV{CC} MATCHES ".+")
51 get_filename_component(CMAKE_C_COMPILER_INIT $ENV{CC} PROGRAM PROGRAM_ARGS CMAKE_C_FLAGS_ENV_INIT)
52 if(CMAKE_C_FLAGS_ENV_INIT)
53 set(CMAKE_C_COMPILER_ARG1 "${CMAKE_C_FLAGS_ENV_INIT}" CACHE STRING "First argument to C compiler")
55 if(NOT EXISTS ${CMAKE_C_COMPILER_INIT})
56 message(FATAL_ERROR "Could not find compiler set in environment variable CC:\n$ENV{CC}.")
60 # next try prefer the compiler specified by the generator
61 if(CMAKE_GENERATOR_CC)
62 if(NOT CMAKE_C_COMPILER_INIT)
63 set(CMAKE_C_COMPILER_INIT ${CMAKE_GENERATOR_CC})
67 # finally list compilers to try
68 if(NOT CMAKE_C_COMPILER_INIT)
69 set(CMAKE_C_COMPILER_LIST ${_CMAKE_TOOLCHAIN_PREFIX}cc ${_CMAKE_TOOLCHAIN_PREFIX}gcc cl bcc xlc clang)
72 _cmake_find_compiler(C)
76 # we only get here if CMAKE_C_COMPILER was specified using -D or a pre-made CMakeCache.txt
77 # (e.g. via ctest) or set in CMAKE_TOOLCHAIN_FILE
78 # if CMAKE_C_COMPILER is a list of length 2, use the first item as
79 # CMAKE_C_COMPILER and the 2nd one as CMAKE_C_COMPILER_ARG1
81 list(LENGTH CMAKE_C_COMPILER _CMAKE_C_COMPILER_LIST_LENGTH)
82 if("${_CMAKE_C_COMPILER_LIST_LENGTH}" EQUAL 2)
83 list(GET CMAKE_C_COMPILER 1 CMAKE_C_COMPILER_ARG1)
84 list(GET CMAKE_C_COMPILER 0 CMAKE_C_COMPILER)
87 # if a compiler was specified by the user but without path,
88 # now try to find it with the full path
89 # if it is found, force it into the cache,
90 # if not, don't overwrite the setting (which was given by the user) with "NOTFOUND"
91 # if the C compiler already had a path, reuse it for searching the CXX compiler
92 get_filename_component(_CMAKE_USER_C_COMPILER_PATH "${CMAKE_C_COMPILER}" PATH)
93 if(NOT _CMAKE_USER_C_COMPILER_PATH)
94 find_program(CMAKE_C_COMPILER_WITH_PATH NAMES ${CMAKE_C_COMPILER})
95 mark_as_advanced(CMAKE_C_COMPILER_WITH_PATH)
96 if(CMAKE_C_COMPILER_WITH_PATH)
97 set(CMAKE_C_COMPILER ${CMAKE_C_COMPILER_WITH_PATH} CACHE STRING "C compiler" FORCE)
101 mark_as_advanced(CMAKE_C_COMPILER)
103 # Each entry in this list is a set of extra flags to try
104 # adding to the compile line to see if it helps produce
105 # a valid identification file.
106 set(CMAKE_C_COMPILER_ID_TEST_FLAGS
107 # Try compiling to an object file only.
110 # Try enabling ANSI mode on HP.
115 # Build a small source file to identify the compiler.
116 if(NOT CMAKE_C_COMPILER_ID_RUN)
117 set(CMAKE_C_COMPILER_ID_RUN 1)
119 # Try to identify the compiler.
120 set(CMAKE_C_COMPILER_ID)
121 file(READ ${CMAKE_ROOT}/Modules/CMakePlatformId.h.in
122 CMAKE_C_COMPILER_ID_PLATFORM_CONTENT)
123 include(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerId.cmake)
124 CMAKE_DETERMINE_COMPILER_ID(C CFLAGS CMakeCCompilerId.c)
126 # Set old compiler and platform id variables.
127 if("${CMAKE_C_COMPILER_ID}" MATCHES "GNU")
128 set(CMAKE_COMPILER_IS_GNUCC 1)
130 if("${CMAKE_C_PLATFORM_ID}" MATCHES "MinGW")
131 set(CMAKE_COMPILER_IS_MINGW 1)
132 elseif("${CMAKE_C_PLATFORM_ID}" MATCHES "Cygwin")
133 set(CMAKE_COMPILER_IS_CYGWIN 1)
137 if (NOT _CMAKE_TOOLCHAIN_LOCATION)
138 get_filename_component(_CMAKE_TOOLCHAIN_LOCATION "${CMAKE_C_COMPILER}" PATH)
141 # If we have a gcc cross compiler, they have usually some prefix, like
142 # e.g. powerpc-linux-gcc, arm-elf-gcc or i586-mingw32msvc-gcc, optionally
143 # with a 3-component version number at the end (e.g. arm-eabi-gcc-4.5.2).
144 # The other tools of the toolchain usually have the same prefix
145 # NAME_WE cannot be used since then this test will fail for names lile
146 # "arm-unknown-nto-qnx6.3.0-gcc.exe", where BASENAME would be
147 # "arm-unknown-nto-qnx6" instead of the correct "arm-unknown-nto-qnx6.3.0-"
148 if (CMAKE_CROSSCOMPILING
149 AND "${CMAKE_C_COMPILER_ID}" MATCHES "GNU"
150 AND NOT _CMAKE_TOOLCHAIN_PREFIX)
151 get_filename_component(COMPILER_BASENAME "${CMAKE_C_COMPILER}" NAME)
152 if (COMPILER_BASENAME MATCHES "^(.+-)g?cc(-[0-9]+\\.[0-9]+\\.[0-9]+)?(\\.exe)?$")
153 set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
156 # if "llvm-" is part of the prefix, remove it, since llvm doesn't have its own binutils
157 # but uses the regular ar, objcopy, etc. (instead of llvm-objcopy etc.)
158 if ("${_CMAKE_TOOLCHAIN_PREFIX}" MATCHES "(.+-)?llvm-$")
159 set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
164 include(${CMAKE_ROOT}/Modules/CMakeClDeps.cmake)
165 include(CMakeFindBinUtils)
166 if(MSVC_C_ARCHITECTURE_ID)
167 set(SET_MSVC_C_ARCHITECTURE_ID
168 "set(MSVC_C_ARCHITECTURE_ID ${MSVC_C_ARCHITECTURE_ID})")
170 # configure variables set in this file for fast reload later on
171 configure_file(${CMAKE_ROOT}/Modules/CMakeCCompiler.cmake.in
172 ${CMAKE_PLATFORM_INFO_DIR}/CMakeCCompiler.cmake
173 @ONLY IMMEDIATE # IMMEDIATE must be here for compatibility mode <= 2.0
175 set(CMAKE_C_COMPILER_ENV_VAR "CC")