Imported Upstream version 2.8.9
[platform/upstream/cmake.git] / Modules / CMakeDetermineCCompiler.cmake
1
2 #=============================================================================
3 # Copyright 2002-2009 Kitware, Inc.
4 #
5 # Distributed under the OSI-approved BSD License (the "License");
6 # see accompanying file Copyright.txt for details.
7 #
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.)
14
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.
24 #
25 # Sets the following variables:
26 #   CMAKE_C_COMPILER
27 #   CMAKE_AR
28 #   CMAKE_RANLIB
29 #   CMAKE_COMPILER_IS_GNUCC
30 #
31 # If not already set before, it also sets
32 #   _CMAKE_TOOLCHAIN_PREFIX
33
34 IF(NOT CMAKE_C_COMPILER)
35   SET(CMAKE_C_COMPILER_INIT NOTFOUND)
36
37   # prefer the environment variable CC
38   IF($ENV{CC} MATCHES ".+")
39     GET_FILENAME_COMPONENT(CMAKE_C_COMPILER_INIT $ENV{CC} PROGRAM PROGRAM_ARGS CMAKE_C_FLAGS_ENV_INIT)
40     IF(CMAKE_C_FLAGS_ENV_INIT)
41       SET(CMAKE_C_COMPILER_ARG1 "${CMAKE_C_FLAGS_ENV_INIT}" CACHE STRING "First argument to C compiler")
42     ENDIF(CMAKE_C_FLAGS_ENV_INIT)
43     IF(NOT EXISTS ${CMAKE_C_COMPILER_INIT})
44       MESSAGE(FATAL_ERROR "Could not find compiler set in environment variable CC:\n$ENV{CC}.")
45     ENDIF(NOT EXISTS ${CMAKE_C_COMPILER_INIT})
46   ENDIF($ENV{CC} MATCHES ".+")
47
48   # next try prefer the compiler specified by the generator
49   IF(CMAKE_GENERATOR_CC)
50     IF(NOT CMAKE_C_COMPILER_INIT)
51       SET(CMAKE_C_COMPILER_INIT ${CMAKE_GENERATOR_CC})
52     ENDIF(NOT CMAKE_C_COMPILER_INIT)
53   ENDIF(CMAKE_GENERATOR_CC)
54
55   # finally list compilers to try
56   IF(CMAKE_C_COMPILER_INIT)
57     SET(CMAKE_C_COMPILER_LIST ${CMAKE_C_COMPILER_INIT})
58   ELSE(CMAKE_C_COMPILER_INIT)
59     SET(CMAKE_C_COMPILER_LIST ${_CMAKE_TOOLCHAIN_PREFIX}gcc ${_CMAKE_TOOLCHAIN_PREFIX}cc cl bcc xlc)
60   ENDIF(CMAKE_C_COMPILER_INIT)
61
62   # Find the compiler.
63   IF (_CMAKE_USER_CXX_COMPILER_PATH)
64     FIND_PROGRAM(CMAKE_C_COMPILER NAMES ${CMAKE_C_COMPILER_LIST} PATHS ${_CMAKE_USER_CXX_COMPILER_PATH} DOC "C compiler" NO_DEFAULT_PATH)
65   ENDIF (_CMAKE_USER_CXX_COMPILER_PATH)
66   FIND_PROGRAM(CMAKE_C_COMPILER NAMES ${CMAKE_C_COMPILER_LIST} DOC "C compiler")
67
68   IF(CMAKE_C_COMPILER_INIT AND NOT CMAKE_C_COMPILER)
69     SET(CMAKE_C_COMPILER "${CMAKE_C_COMPILER_INIT}" CACHE FILEPATH "C compiler" FORCE)
70   ENDIF(CMAKE_C_COMPILER_INIT AND NOT CMAKE_C_COMPILER)
71 ELSE(NOT CMAKE_C_COMPILER)
72
73   # we only get here if CMAKE_C_COMPILER was specified using -D or a pre-made CMakeCache.txt
74   # (e.g. via ctest) or set in CMAKE_TOOLCHAIN_FILE
75   # if CMAKE_C_COMPILER is a list of length 2, use the first item as
76   # CMAKE_C_COMPILER and the 2nd one as CMAKE_C_COMPILER_ARG1
77
78   LIST(LENGTH CMAKE_C_COMPILER _CMAKE_C_COMPILER_LIST_LENGTH)
79   IF("${_CMAKE_C_COMPILER_LIST_LENGTH}" EQUAL 2)
80     LIST(GET CMAKE_C_COMPILER 1 CMAKE_C_COMPILER_ARG1)
81     LIST(GET CMAKE_C_COMPILER 0 CMAKE_C_COMPILER)
82   ENDIF("${_CMAKE_C_COMPILER_LIST_LENGTH}" EQUAL 2)
83
84   # if a compiler was specified by the user but without path,
85   # now try to find it with the full path
86   # if it is found, force it into the cache,
87   # if not, don't overwrite the setting (which was given by the user) with "NOTFOUND"
88   # if the C compiler already had a path, reuse it for searching the CXX compiler
89   GET_FILENAME_COMPONENT(_CMAKE_USER_C_COMPILER_PATH "${CMAKE_C_COMPILER}" PATH)
90   IF(NOT _CMAKE_USER_C_COMPILER_PATH)
91     FIND_PROGRAM(CMAKE_C_COMPILER_WITH_PATH NAMES ${CMAKE_C_COMPILER})
92     MARK_AS_ADVANCED(CMAKE_C_COMPILER_WITH_PATH)
93     IF(CMAKE_C_COMPILER_WITH_PATH)
94       SET(CMAKE_C_COMPILER ${CMAKE_C_COMPILER_WITH_PATH} CACHE STRING "C compiler" FORCE)
95     ENDIF(CMAKE_C_COMPILER_WITH_PATH)
96   ENDIF(NOT _CMAKE_USER_C_COMPILER_PATH)
97 ENDIF(NOT CMAKE_C_COMPILER)
98 MARK_AS_ADVANCED(CMAKE_C_COMPILER)
99
100 IF (NOT _CMAKE_TOOLCHAIN_LOCATION)
101   GET_FILENAME_COMPONENT(_CMAKE_TOOLCHAIN_LOCATION "${CMAKE_C_COMPILER}" PATH)
102 ENDIF (NOT _CMAKE_TOOLCHAIN_LOCATION)
103
104 # Build a small source file to identify the compiler.
105 IF(${CMAKE_GENERATOR} MATCHES "Visual Studio")
106   SET(CMAKE_C_COMPILER_ID_RUN 1)
107   SET(CMAKE_C_PLATFORM_ID "Windows")
108   SET(CMAKE_C_COMPILER_ID "MSVC")
109 ENDIF(${CMAKE_GENERATOR} MATCHES "Visual Studio")
110
111 IF(NOT CMAKE_C_COMPILER_ID_RUN)
112   SET(CMAKE_C_COMPILER_ID_RUN 1)
113
114   # Each entry in this list is a set of extra flags to try
115   # adding to the compile line to see if it helps produce
116   # a valid identification file.
117   SET(CMAKE_C_COMPILER_ID_TEST_FLAGS
118     # Try compiling to an object file only.
119     "-c"
120
121     # Try enabling ANSI mode on HP.
122     "-Aa"
123     )
124
125   # Try to identify the compiler.
126   SET(CMAKE_C_COMPILER_ID)
127   FILE(READ ${CMAKE_ROOT}/Modules/CMakePlatformId.h.in
128     CMAKE_C_COMPILER_ID_PLATFORM_CONTENT)
129   INCLUDE(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerId.cmake)
130   CMAKE_DETERMINE_COMPILER_ID(C CFLAGS CMakeCCompilerId.c)
131
132   # Set old compiler and platform id variables.
133   IF("${CMAKE_C_COMPILER_ID}" MATCHES "GNU")
134     SET(CMAKE_COMPILER_IS_GNUCC 1)
135   ENDIF("${CMAKE_C_COMPILER_ID}" MATCHES "GNU")
136   IF("${CMAKE_C_PLATFORM_ID}" MATCHES "MinGW")
137     SET(CMAKE_COMPILER_IS_MINGW 1)
138   ELSEIF("${CMAKE_C_PLATFORM_ID}" MATCHES "Cygwin")
139     SET(CMAKE_COMPILER_IS_CYGWIN 1)
140   ENDIF("${CMAKE_C_PLATFORM_ID}" MATCHES "MinGW")
141 ENDIF(NOT CMAKE_C_COMPILER_ID_RUN)
142
143 # If we have a gcc cross compiler, they have usually some prefix, like
144 # e.g. powerpc-linux-gcc, arm-elf-gcc or i586-mingw32msvc-gcc, optionally
145 # with a 3-component version number at the end (e.g. arm-eabi-gcc-4.5.2).
146 # The other tools of the toolchain usually have the same prefix
147 # NAME_WE cannot be used since then this test will fail for names lile
148 # "arm-unknown-nto-qnx6.3.0-gcc.exe", where BASENAME would be
149 # "arm-unknown-nto-qnx6" instead of the correct "arm-unknown-nto-qnx6.3.0-"
150 IF (CMAKE_CROSSCOMPILING
151     AND "${CMAKE_C_COMPILER_ID}" MATCHES "GNU"
152     AND NOT _CMAKE_TOOLCHAIN_PREFIX)
153   GET_FILENAME_COMPONENT(COMPILER_BASENAME "${CMAKE_C_COMPILER}" NAME)
154   IF (COMPILER_BASENAME MATCHES "^(.+-)g?cc(-[0-9]+\\.[0-9]+\\.[0-9]+)?(\\.exe)?$")
155     SET(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
156   ENDIF (COMPILER_BASENAME MATCHES "^(.+-)g?cc(-[0-9]+\\.[0-9]+\\.[0-9]+)?(\\.exe)?$")
157
158   # if "llvm-" is part of the prefix, remove it, since llvm doesn't have its own binutils
159   # but uses the regular ar, objcopy, etc. (instead of llvm-objcopy etc.)
160   IF ("${_CMAKE_TOOLCHAIN_PREFIX}" MATCHES "(.+-)?llvm-$")
161     SET(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
162   ENDIF ("${_CMAKE_TOOLCHAIN_PREFIX}" MATCHES "(.+-)?llvm-$")
163
164 ENDIF (CMAKE_CROSSCOMPILING
165     AND "${CMAKE_C_COMPILER_ID}" MATCHES "GNU"
166     AND NOT _CMAKE_TOOLCHAIN_PREFIX)
167
168 INCLUDE(${CMAKE_ROOT}/Modules/CMakeClDeps.cmake)
169 INCLUDE(CMakeFindBinUtils)
170 IF(MSVC_C_ARCHITECTURE_ID)
171   SET(SET_MSVC_C_ARCHITECTURE_ID
172     "SET(MSVC_C_ARCHITECTURE_ID ${MSVC_C_ARCHITECTURE_ID})")
173 ENDIF(MSVC_C_ARCHITECTURE_ID)
174 # configure variables set in this file for fast reload later on
175 CONFIGURE_FILE(${CMAKE_ROOT}/Modules/CMakeCCompiler.cmake.in
176   "${CMAKE_PLATFORM_ROOT_BIN}/CMakeCCompiler.cmake"
177   @ONLY IMMEDIATE # IMMEDIATE must be here for compatibility mode <= 2.0
178   )
179 SET(CMAKE_C_COMPILER_ENV_VAR "CC")