blob: f574978d7985b56584e5ad4026bce0361f6534d1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
#=============================================================================
# Copyright 2007-2009 Kitware, Inc.
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
# (To distribute this file outside of CMake, substitute the full
# License text for the above reference.)
# Function to compile a source file to identify the compiler. This is
# used internally by CMake and should not be included by user code.
# If successful, sets CMAKE_<lang>_COMPILER_ID and CMAKE_<lang>_PLATFORM_ID
function(CMAKE_DETERMINE_COMPILER_ID lang flagvar src)
# Make sure the compiler arguments are clean.
string(STRIP "${CMAKE_${lang}_COMPILER_ARG1}" CMAKE_${lang}_COMPILER_ID_ARG1)
string(REGEX REPLACE " +" ";" CMAKE_${lang}_COMPILER_ID_ARG1 "${CMAKE_${lang}_COMPILER_ID_ARG1}")
# Make sure user-specified compiler flags are used.
if(CMAKE_${lang}_FLAGS)
set(CMAKE_${lang}_COMPILER_ID_FLAGS ${CMAKE_${lang}_FLAGS})
else()
set(CMAKE_${lang}_COMPILER_ID_FLAGS $ENV{${flagvar}})
endif()
string(REGEX REPLACE " " ";" CMAKE_${lang}_COMPILER_ID_FLAGS_LIST "${CMAKE_${lang}_COMPILER_ID_FLAGS}")
# Compute the directory in which to run the test.
set(CMAKE_${lang}_COMPILER_ID_DIR ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CompilerId${lang})
# Try building with no extra flags and then try each set
# of helper flags. Stop when the compiler is identified.
foreach(flags "" ${CMAKE_${lang}_COMPILER_ID_TEST_FLAGS})
if(NOT CMAKE_${lang}_COMPILER_ID)
CMAKE_DETERMINE_COMPILER_ID_BUILD("${lang}" "${flags}" "${src}")
foreach(file ${COMPILER_${lang}_PRODUCED_FILES})
CMAKE_DETERMINE_COMPILER_ID_CHECK("${lang}" "${CMAKE_${lang}_COMPILER_ID_DIR}/${file}" "${src}")
endforeach()
endif()
endforeach()
# If the compiler is still unknown, try to query its vendor.
if(NOT CMAKE_${lang}_COMPILER_ID)
CMAKE_DETERMINE_COMPILER_ID_VENDOR(${lang})
endif()
# if the format is unknown after all files have been checked, put "Unknown" in the cache
if(NOT CMAKE_EXECUTABLE_FORMAT)
set(CMAKE_EXECUTABLE_FORMAT "Unknown" CACHE INTERNAL "Executable file format")
endif()
# Display the final identification result.
if(CMAKE_${lang}_COMPILER_ID)
if(CMAKE_${lang}_COMPILER_VERSION)
set(_version " ${CMAKE_${lang}_COMPILER_VERSION}")
else()
set(_version "")
endif()
message(STATUS "The ${lang} compiler identification is "
"${CMAKE_${lang}_COMPILER_ID}${_version}")
else()
message(STATUS "The ${lang} compiler identification is unknown")
endif()
set(CMAKE_${lang}_COMPILER_ID "${CMAKE_${lang}_COMPILER_ID}" PARENT_SCOPE)
set(CMAKE_${lang}_PLATFORM_ID "${CMAKE_${lang}_PLATFORM_ID}" PARENT_SCOPE)
set(MSVC_${lang}_ARCHITECTURE_ID "${MSVC_${lang}_ARCHITECTURE_ID}"
PARENT_SCOPE)
set(CMAKE_${lang}_COMPILER_VERSION "${CMAKE_${lang}_COMPILER_VERSION}" PARENT_SCOPE)
endfunction()
#-----------------------------------------------------------------------------
# Function to write the compiler id source file.
function(CMAKE_DETERMINE_COMPILER_ID_WRITE lang src)
file(READ ${CMAKE_ROOT}/Modules/${src}.in ID_CONTENT_IN)
string(CONFIGURE "${ID_CONTENT_IN}" ID_CONTENT_OUT @ONLY)
file(WRITE ${CMAKE_${lang}_COMPILER_ID_DIR}/${src} "${ID_CONTENT_OUT}")
endfunction()
#-----------------------------------------------------------------------------
# Function to build the compiler id source file and look for output
# files.
function(CMAKE_DETERMINE_COMPILER_ID_BUILD lang testflags src)
# Create a clean working directory.
file(REMOVE_RECURSE ${CMAKE_${lang}_COMPILER_ID_DIR})
file(MAKE_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR})
CMAKE_DETERMINE_COMPILER_ID_WRITE("${lang}" "${src}")
# Construct a description of this test case.
set(COMPILER_DESCRIPTION
"Compiler: ${CMAKE_${lang}_COMPILER} ${CMAKE_${lang}_COMPILER_ID_ARG1}
Build flags: ${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST}
Id flags: ${testflags}
")
# Compile the compiler identification source.
if(COMMAND EXECUTE_PROCESS)
execute_process(
COMMAND ${CMAKE_${lang}_COMPILER}
${CMAKE_${lang}_COMPILER_ID_ARG1}
${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST}
${testflags}
"${src}"
WORKING_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR}
OUTPUT_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
ERROR_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
RESULT_VARIABLE CMAKE_${lang}_COMPILER_ID_RESULT
)
else()
exec_program(
${CMAKE_${lang}_COMPILER} ${CMAKE_${lang}_COMPILER_ID_DIR}
ARGS ${CMAKE_${lang}_COMPILER_ID_ARG1}
${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST}
${testflags}
\"${src}\"
OUTPUT_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
RETURN_VALUE CMAKE_${lang}_COMPILER_ID_RESULT
)
endif()
# Check the result of compilation.
if(CMAKE_${lang}_COMPILER_ID_RESULT)
# Compilation failed.
set(MSG
"Compiling the ${lang} compiler identification source file \"${src}\" failed.
${COMPILER_DESCRIPTION}
The output was:
${CMAKE_${lang}_COMPILER_ID_RESULT}
${CMAKE_${lang}_COMPILER_ID_OUTPUT}
")
file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log "${MSG}")
#if(NOT CMAKE_${lang}_COMPILER_ID_ALLOW_FAIL)
# message(FATAL_ERROR "${MSG}")
#endif()
# No output files should be inspected.
set(COMPILER_${lang}_PRODUCED_FILES)
else()
# Compilation succeeded.
file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
"Compiling the ${lang} compiler identification source file \"${src}\" succeeded.
${COMPILER_DESCRIPTION}
The output was:
${CMAKE_${lang}_COMPILER_ID_RESULT}
${CMAKE_${lang}_COMPILER_ID_OUTPUT}
")
# Find the executable produced by the compiler, try all files in the
# binary dir.
file(GLOB COMPILER_${lang}_PRODUCED_FILES
RELATIVE ${CMAKE_${lang}_COMPILER_ID_DIR}
${CMAKE_${lang}_COMPILER_ID_DIR}/*)
list(REMOVE_ITEM COMPILER_${lang}_PRODUCED_FILES "${src}")
foreach(file ${COMPILER_${lang}_PRODUCED_FILES})
file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
"Compilation of the ${lang} compiler identification source \""
"${src}\" produced \"${file}\"\n\n")
endforeach()
if(NOT COMPILER_${lang}_PRODUCED_FILES)
# No executable was found.
file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
"Compilation of the ${lang} compiler identification source \""
"${src}\" did not produce an executable in \""
"${CMAKE_${lang}_COMPILER_ID_DIR}\".\n\n")
endif()
endif()
# Return the files produced by the compilation.
set(COMPILER_${lang}_PRODUCED_FILES "${COMPILER_${lang}_PRODUCED_FILES}" PARENT_SCOPE)
endfunction()
#-----------------------------------------------------------------------------
# Function to extract the compiler id from an executable.
function(CMAKE_DETERMINE_COMPILER_ID_CHECK lang file)
# Look for a compiler id if not yet known.
if(NOT CMAKE_${lang}_COMPILER_ID)
# Read the compiler identification string from the executable file.
set(COMPILER_ID)
set(COMPILER_VERSION)
set(PLATFORM_ID)
file(STRINGS ${file}
CMAKE_${lang}_COMPILER_ID_STRINGS LIMIT_COUNT 4 REGEX "INFO:")
set(HAVE_COMPILER_TWICE 0)
foreach(info ${CMAKE_${lang}_COMPILER_ID_STRINGS})
if("${info}" MATCHES ".*INFO:compiler\\[([^]\"]*)\\].*")
if(COMPILER_ID)
set(COMPILER_ID_TWICE 1)
endif()
string(REGEX REPLACE ".*INFO:compiler\\[([^]]*)\\].*" "\\1"
COMPILER_ID "${info}")
endif()
if("${info}" MATCHES ".*INFO:platform\\[([^]\"]*)\\].*")
string(REGEX REPLACE ".*INFO:platform\\[([^]]*)\\].*" "\\1"
PLATFORM_ID "${info}")
endif()
if("${info}" MATCHES ".*INFO:arch\\[([^]\"]*)\\].*")
string(REGEX REPLACE ".*INFO:arch\\[([^]]*)\\].*" "\\1"
ARCHITECTURE_ID "${info}")
endif()
if("${info}" MATCHES ".*INFO:compiler_version\\[([^]\"]*)\\].*")
string(REGEX REPLACE ".*INFO:compiler_version\\[([^]]*)\\].*" "\\1" COMPILER_VERSION "${info}")
string(REGEX REPLACE "^0+([0-9])" "\\1" COMPILER_VERSION "${COMPILER_VERSION}")
string(REGEX REPLACE "\\.0+([0-9])" ".\\1" COMPILER_VERSION "${COMPILER_VERSION}")
endif()
endforeach()
# Check if a valid compiler and platform were found.
if(COMPILER_ID AND NOT COMPILER_ID_TWICE)
set(CMAKE_${lang}_COMPILER_ID "${COMPILER_ID}")
set(CMAKE_${lang}_PLATFORM_ID "${PLATFORM_ID}")
set(MSVC_${lang}_ARCHITECTURE_ID "${ARCHITECTURE_ID}")
set(CMAKE_${lang}_COMPILER_VERSION "${COMPILER_VERSION}")
endif()
# Check the compiler identification string.
if(CMAKE_${lang}_COMPILER_ID)
# The compiler identification was found.
file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
"The ${lang} compiler identification is ${CMAKE_${lang}_COMPILER_ID}, found in \""
"${file}\"\n\n")
else()
# The compiler identification could not be found.
file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
"The ${lang} compiler identification could not be found in \""
"${file}\"\n\n")
endif()
endif()
# try to figure out the executable format: ELF, COFF, Mach-O
if(NOT CMAKE_EXECUTABLE_FORMAT)
file(READ ${file} CMAKE_EXECUTABLE_MAGIC LIMIT 4 HEX)
# ELF files start with 0x7f"ELF"
if("${CMAKE_EXECUTABLE_MAGIC}" STREQUAL "7f454c46")
set(CMAKE_EXECUTABLE_FORMAT "ELF" CACHE INTERNAL "Executable file format")
endif()
# # COFF (.exe) files start with "MZ"
# if("${CMAKE_EXECUTABLE_MAGIC}" MATCHES "4d5a....")
# set(CMAKE_EXECUTABLE_FORMAT "COFF" CACHE STRING "Executable file format")
# endif()
#
# # Mach-O files start with CAFEBABE or FEEDFACE, according to http://radio.weblogs.com/0100490/2003/01/28.html
# if("${CMAKE_EXECUTABLE_MAGIC}" MATCHES "cafebabe")
# set(CMAKE_EXECUTABLE_FORMAT "MACHO" CACHE STRING "Executable file format")
# endif()
# if("${CMAKE_EXECUTABLE_MAGIC}" MATCHES "feedface")
# set(CMAKE_EXECUTABLE_FORMAT "MACHO" CACHE STRING "Executable file format")
# endif()
endif()
if(NOT DEFINED CMAKE_EXECUTABLE_FORMAT)
set(CMAKE_EXECUTABLE_FORMAT)
endif()
# Return the information extracted.
set(CMAKE_${lang}_COMPILER_ID "${CMAKE_${lang}_COMPILER_ID}" PARENT_SCOPE)
set(CMAKE_${lang}_PLATFORM_ID "${CMAKE_${lang}_PLATFORM_ID}" PARENT_SCOPE)
set(MSVC_${lang}_ARCHITECTURE_ID "${MSVC_${lang}_ARCHITECTURE_ID}"
PARENT_SCOPE)
set(CMAKE_${lang}_COMPILER_VERSION "${CMAKE_${lang}_COMPILER_VERSION}" PARENT_SCOPE)
set(CMAKE_EXECUTABLE_FORMAT "${CMAKE_EXECUTABLE_FORMAT}" PARENT_SCOPE)
endfunction()
#-----------------------------------------------------------------------------
# Function to query the compiler vendor.
# This uses a table with entries of the form
# list(APPEND CMAKE_${lang}_COMPILER_ID_VENDORS ${vendor})
# set(CMAKE_${lang}_COMPILER_ID_VENDOR_FLAGS_${vendor} -some-vendor-flag)
# set(CMAKE_${lang}_COMPILER_ID_VENDOR_REGEX_${vendor} "Some Vendor Output")
# We try running the compiler with the flag for each vendor and
# matching its regular expression in the output.
function(CMAKE_DETERMINE_COMPILER_ID_VENDOR lang)
if(NOT CMAKE_${lang}_COMPILER_ID_DIR)
# We get here when this function is called not from within CMAKE_DETERMINE_COMPILER_ID()
# This is done e.g. for detecting the compiler ID for assemblers.
# Compute the directory in which to run the test and Create a clean working directory.
set(CMAKE_${lang}_COMPILER_ID_DIR ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CompilerId${lang})
file(REMOVE_RECURSE ${CMAKE_${lang}_COMPILER_ID_DIR})
file(MAKE_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR})
endif()
foreach(vendor ${CMAKE_${lang}_COMPILER_ID_VENDORS})
set(flags ${CMAKE_${lang}_COMPILER_ID_VENDOR_FLAGS_${vendor}})
set(regex ${CMAKE_${lang}_COMPILER_ID_VENDOR_REGEX_${vendor}})
execute_process(
COMMAND ${CMAKE_${lang}_COMPILER}
${CMAKE_${lang}_COMPILER_ID_ARG1}
${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST}
${flags}
WORKING_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR}
OUTPUT_VARIABLE output ERROR_VARIABLE output
RESULT_VARIABLE result
TIMEOUT 10
)
if("${output}" MATCHES "${regex}")
file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
"Checking whether the ${lang} compiler is ${vendor} using \"${flags}\" "
"matched \"${regex}\":\n${output}")
set(CMAKE_${lang}_COMPILER_ID "${vendor}" PARENT_SCOPE)
break()
else()
if("${result}" MATCHES "timeout")
file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
"Checking whether the ${lang} compiler is ${vendor} using \"${flags}\" "
"terminated after 10 s due to timeout.")
else()
file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
"Checking whether the ${lang} compiler is ${vendor} using \"${flags}\" "
"did not match \"${regex}\":\n${output}")
endif()
endif()
endforeach()
endfunction()
|