ENH: Merging CompilerId updates from branch CMake-Modules-CompilerId to the main...
[cmake.git] / Modules / CMakeDetermineCompilerId.cmake
blob9a06acf70d79b9509c55fc3e5a08fe636c9fe52b
2 # Macro to compile a source file to identify the compiler.  This is
3 # used internally by CMake and should not be included by user code.
4 # If successful, sets CMAKE_<lang>_COMPILER_ID and CMAKE_<lang>_PLATFORM_ID
6 MACRO(CMAKE_DETERMINE_COMPILER_ID lang flagvar src)
7   # Store the compiler identification source file.
8   SET(CMAKE_${lang}_COMPILER_ID_SRC "${src}")
9   IF(WIN32 AND NOT CYGWIN)
10     # This seems to escape spaces:
11     #FILE(TO_NATIVE_PATH "${CMAKE_${lang}_COMPILER_ID_SRC}"
12     #  CMAKE_${lang}_COMPILER_ID_SRC)
13     STRING(REGEX REPLACE "/" "\\\\" CMAKE_${lang}_COMPILER_ID_SRC
14       "${CMAKE_${lang}_COMPILER_ID_SRC}")
15   ENDIF(WIN32 AND NOT CYGWIN)
17   # Make sure user-specified compiler flags are used.
18   IF(CMAKE_${lang}_FLAGS)
19     SET(CMAKE_${lang}_COMPILER_ID_FLAGS ${CMAKE_${lang}_FLAGS})
20   ELSE(CMAKE_${lang}_FLAGS)
21     SET(CMAKE_${lang}_COMPILER_ID_FLAGS $ENV{${flagvar}})
22   ENDIF(CMAKE_${lang}_FLAGS)
24   # Create an empty directory in which to run the test.
25   SET(CMAKE_${lang}_COMPILER_ID_DIR ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CompilerId${lang})
26   FILE(REMOVE_RECURSE ${CMAKE_${lang}_COMPILER_ID_DIR})
27   FILE(MAKE_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR})
29   # Compile the compiler identification source.
30   STRING(REGEX REPLACE " " ";" CMAKE_${lang}_COMPILER_ID_FLAGS_LIST "${CMAKE_${lang}_COMPILER_ID_FLAGS}")
31   IF(COMMAND EXECUTE_PROCESS)
32     EXECUTE_PROCESS(
33       COMMAND ${CMAKE_${lang}_COMPILER} ${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST} ${CMAKE_${lang}_COMPILER_ID_SRC}
34       WORKING_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR}
35       OUTPUT_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
36       ERROR_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
37       RESULT_VARIABLE CMAKE_${lang}_COMPILER_ID_RESULT
38       )
39   ELSE(COMMAND EXECUTE_PROCESS)
40     EXEC_PROGRAM(
41       ${CMAKE_${lang}_COMPILER} ${CMAKE_${lang}_COMPILER_ID_DIR}
42       ARGS ${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST} \"${CMAKE_${lang}_COMPILER_ID_SRC}\"
43       OUTPUT_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
44       RETURN_VALUE CMAKE_${lang}_COMPILER_ID_RESULT
45       )
46   ENDIF(COMMAND EXECUTE_PROCESS)
48   # Check the result of compilation.
49   IF(CMAKE_${lang}_COMPILER_ID_RESULT)
50     # Compilation failed.
51     FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
52       "Compiling the ${lang} compiler identification source file \""
53       "${CMAKE_${lang}_COMPILER_ID_SRC}\" failed with the following output:\n"
54       "${CMAKE_${lang}_COMPILER_ID_RESULT}\n"
55       "${CMAKE_${lang}_COMPILER_ID_OUTPUT}\n\n")
56   ELSE(CMAKE_${lang}_COMPILER_ID_RESULT)
57     # Compilation succeeded.
58     FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
59       "Compiling the ${lang} compiler identification source file \""
60       "${CMAKE_${lang}_COMPILER_ID_SRC}\" succeeded with the following output:\n"
61       "${CMAKE_${lang}_COMPILER_ID_OUTPUT}\n\n")
63     # Find the executable produced by the compiler.
64     SET(CMAKE_${lang}_COMPILER_ID_EXE)
65     GET_FILENAME_COMPONENT(CMAKE_${lang}_COMPILER_ID_SRC_BASE ${CMAKE_${lang}_COMPILER_ID_SRC} NAME_WE)
66     FOREACH(name a.out a.exe ${CMAKE_${lang}_COMPILER_ID_SRC_BASE}.exe)
67       IF(EXISTS ${CMAKE_${lang}_COMPILER_ID_DIR}/${name})
68         SET(CMAKE_${lang}_COMPILER_ID_EXE ${CMAKE_${lang}_COMPILER_ID_DIR}/${name})
69       ENDIF(EXISTS ${CMAKE_${lang}_COMPILER_ID_DIR}/${name})
70     ENDFOREACH(name)
72     # Check if the executable was found.
73     IF(CMAKE_${lang}_COMPILER_ID_EXE)
74       # The executable was found.
75       FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
76         "Compilation of the ${lang} compiler identification source \""
77         "${CMAKE_${lang}_COMPILER_ID_SRC}\" produced \""
78         "${CMAKE_${lang}_COMPILER_ID_EXE}\"\n\n")
80       # Read the compiler identification string from the executable file.
81       FILE(STRINGS ${CMAKE_${lang}_COMPILER_ID_EXE}
82         CMAKE_${lang}_COMPILER_ID_STRINGS LIMIT_COUNT 2 REGEX "INFO:")
83       FOREACH(info ${CMAKE_${lang}_COMPILER_ID_STRINGS})
84         IF("${info}" MATCHES ".*INFO:compiler\\[([^]]*)\\].*")
85           STRING(REGEX REPLACE ".*INFO:compiler\\[([^]]*)\\].*" "\\1"
86             CMAKE_${lang}_COMPILER_ID "${info}")
87         ENDIF("${info}" MATCHES ".*INFO:compiler\\[([^]]*)\\].*")
88         IF("${info}" MATCHES ".*INFO:platform\\[([^]]*)\\].*")
89           STRING(REGEX REPLACE ".*INFO:platform\\[([^]]*)\\].*" "\\1"
90             CMAKE_${lang}_PLATFORM_ID "${info}")
91         ENDIF("${info}" MATCHES ".*INFO:platform\\[([^]]*)\\].*")
92       ENDFOREACH(info)
94       # Check the compiler identification string.
95       IF(CMAKE_${lang}_COMPILER_ID)
96         # The compiler identification was found.
97         FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
98           "The ${lang} compiler identification is ${CMAKE_${lang}_COMPILER_ID}\n\n")
99       ELSE(CMAKE_${lang}_COMPILER_ID)
100         # The compiler identification could not be found.
101         FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
102           "The ${lang} compiler identification could not be found in \""
103           "${CMAKE_${lang}_COMPILER_ID_EXE}\"\n\n")
104       ENDIF(CMAKE_${lang}_COMPILER_ID)
105     ELSE(CMAKE_${lang}_COMPILER_ID_EXE)
106       # The executable was not found.
107       FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
108         "Compilation of the ${lang} compiler identification source \""
109         "${CMAKE_${lang}_COMPILER_ID_SRC}\" did not produce an executable in "
110         "${CMAKE_${lang}_COMPILER_ID_DIR} "
111         "with a name known to CMake.\n\n")
112     ENDIF(CMAKE_${lang}_COMPILER_ID_EXE)
113   ENDIF(CMAKE_${lang}_COMPILER_ID_RESULT)
114 ENDMACRO(CMAKE_DETERMINE_COMPILER_ID)