CMakeDetermineCompilerId.cmake 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. # Function to compile a source file to identify the compiler. This is
  2. # used internally by CMake and should not be included by user code.
  3. # If successful, sets CMAKE_<lang>_COMPILER_ID and CMAKE_<lang>_PLATFORM_ID
  4. FUNCTION(CMAKE_DETERMINE_COMPILER_ID lang flagvar src)
  5. # Store the compiler identification source file.
  6. SET(CMAKE_${lang}_COMPILER_ID_SRC "${src}")
  7. IF(CMAKE_HOST_WIN32 AND NOT CMAKE_HOST_UNIX)
  8. # This seems to escape spaces:
  9. #FILE(TO_NATIVE_PATH "${CMAKE_${lang}_COMPILER_ID_SRC}"
  10. # CMAKE_${lang}_COMPILER_ID_SRC)
  11. STRING(REGEX REPLACE "/" "\\\\" CMAKE_${lang}_COMPILER_ID_SRC
  12. "${CMAKE_${lang}_COMPILER_ID_SRC}")
  13. ENDIF(CMAKE_HOST_WIN32 AND NOT CMAKE_HOST_UNIX)
  14. # Make sure the compiler arguments are clean.
  15. STRING(STRIP "${CMAKE_${lang}_COMPILER_ARG1}" CMAKE_${lang}_COMPILER_ID_ARG1)
  16. # Make sure user-specified compiler flags are used.
  17. IF(CMAKE_${lang}_FLAGS)
  18. SET(CMAKE_${lang}_COMPILER_ID_FLAGS ${CMAKE_${lang}_FLAGS})
  19. ELSE(CMAKE_${lang}_FLAGS)
  20. SET(CMAKE_${lang}_COMPILER_ID_FLAGS $ENV{${flagvar}})
  21. ENDIF(CMAKE_${lang}_FLAGS)
  22. STRING(REGEX REPLACE " " ";" CMAKE_${lang}_COMPILER_ID_FLAGS_LIST "${CMAKE_${lang}_COMPILER_ID_FLAGS}")
  23. # Compute the directory in which to run the test.
  24. SET(CMAKE_${lang}_COMPILER_ID_DIR ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CompilerId${lang})
  25. # Try building with no extra flags and then try each set
  26. # of helper flags. Stop when the compiler is identified.
  27. FOREACH(flags "" ${CMAKE_${lang}_COMPILER_ID_TEST_FLAGS})
  28. IF(NOT CMAKE_${lang}_COMPILER_ID)
  29. CMAKE_DETERMINE_COMPILER_ID_BUILD("${lang}" "${flags}")
  30. FOREACH(file ${COMPILER_${lang}_PRODUCED_FILES})
  31. CMAKE_DETERMINE_COMPILER_ID_CHECK("${lang}" "${file}")
  32. ENDFOREACH(file)
  33. ENDIF(NOT CMAKE_${lang}_COMPILER_ID)
  34. ENDFOREACH(flags)
  35. # if the format is unknown after all files have been checked, put "Unknown" in the cache
  36. IF(NOT CMAKE_EXECUTABLE_FORMAT)
  37. SET(CMAKE_EXECUTABLE_FORMAT "Unknown" CACHE INTERNAL "Executable file format")
  38. ENDIF(NOT CMAKE_EXECUTABLE_FORMAT)
  39. # Display the final identification result.
  40. IF(CMAKE_${lang}_COMPILER_ID)
  41. MESSAGE(STATUS "The ${lang} compiler identification is "
  42. "${CMAKE_${lang}_COMPILER_ID}")
  43. ELSE(CMAKE_${lang}_COMPILER_ID)
  44. MESSAGE(STATUS "The ${lang} compiler identification is unknown")
  45. ENDIF(CMAKE_${lang}_COMPILER_ID)
  46. SET(CMAKE_${lang}_COMPILER_ID "${CMAKE_${lang}_COMPILER_ID}" PARENT_SCOPE)
  47. SET(CMAKE_${lang}_PLATFORM_ID "${CMAKE_${lang}_PLATFORM_ID}" PARENT_SCOPE)
  48. ENDFUNCTION(CMAKE_DETERMINE_COMPILER_ID)
  49. #-----------------------------------------------------------------------------
  50. # Function to build the compiler id source file and look for output
  51. # files.
  52. FUNCTION(CMAKE_DETERMINE_COMPILER_ID_BUILD lang testflags)
  53. # Create a clean working directory.
  54. FILE(REMOVE_RECURSE ${CMAKE_${lang}_COMPILER_ID_DIR})
  55. FILE(MAKE_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR})
  56. # Construct a description of this test case.
  57. SET(COMPILER_DESCRIPTION
  58. "Compiler: ${CMAKE_${lang}_COMPILER} ${CMAKE_${lang}_COMPILER_ID_ARG1}\n"
  59. "Build flags: ${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST}\n"
  60. "Id flags: ${testflags}\n"
  61. )
  62. # Compile the compiler identification source.
  63. IF(COMMAND EXECUTE_PROCESS)
  64. EXECUTE_PROCESS(
  65. COMMAND ${CMAKE_${lang}_COMPILER}
  66. ${CMAKE_${lang}_COMPILER_ID_ARG1}
  67. ${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST}
  68. ${testflags}
  69. ${CMAKE_${lang}_COMPILER_ID_SRC}
  70. WORKING_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR}
  71. OUTPUT_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  72. ERROR_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  73. RESULT_VARIABLE CMAKE_${lang}_COMPILER_ID_RESULT
  74. )
  75. ELSE(COMMAND EXECUTE_PROCESS)
  76. EXEC_PROGRAM(
  77. ${CMAKE_${lang}_COMPILER} ${CMAKE_${lang}_COMPILER_ID_DIR}
  78. ARGS ${CMAKE_${lang}_COMPILER_ID_ARG1}
  79. ${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST}
  80. ${testflags}
  81. \"${CMAKE_${lang}_COMPILER_ID_SRC}\"
  82. OUTPUT_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  83. RETURN_VALUE CMAKE_${lang}_COMPILER_ID_RESULT
  84. )
  85. ENDIF(COMMAND EXECUTE_PROCESS)
  86. # Check the result of compilation.
  87. IF(CMAKE_${lang}_COMPILER_ID_RESULT)
  88. # Compilation failed.
  89. SET(MSG
  90. "Compiling the ${lang} compiler identification source file \""
  91. "${CMAKE_${lang}_COMPILER_ID_SRC}\" failed.\n"
  92. ${COMPILER_DESCRIPTION}
  93. "The output was:\n"
  94. "${CMAKE_${lang}_COMPILER_ID_RESULT}\n"
  95. "${CMAKE_${lang}_COMPILER_ID_OUTPUT}\n\n"
  96. )
  97. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log "${MSG}")
  98. #IF(NOT CMAKE_${lang}_COMPILER_ID_ALLOW_FAIL)
  99. # MESSAGE(FATAL_ERROR "${MSG}")
  100. #ENDIF(NOT CMAKE_${lang}_COMPILER_ID_ALLOW_FAIL)
  101. # No output files should be inspected.
  102. SET(COMPILER_${lang}_PRODUCED_FILES)
  103. ELSE(CMAKE_${lang}_COMPILER_ID_RESULT)
  104. # Compilation succeeded.
  105. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  106. "Compiling the ${lang} compiler identification source file \""
  107. "${CMAKE_${lang}_COMPILER_ID_SRC}\" succeeded.\n"
  108. ${COMPILER_DESCRIPTION}
  109. "The output was:\n"
  110. "${CMAKE_${lang}_COMPILER_ID_RESULT}\n"
  111. "${CMAKE_${lang}_COMPILER_ID_OUTPUT}\n\n"
  112. )
  113. # Find the executable produced by the compiler, try all files in the
  114. # binary dir.
  115. FILE(GLOB COMPILER_${lang}_PRODUCED_FILES ${CMAKE_${lang}_COMPILER_ID_DIR}/*)
  116. FOREACH(file ${COMPILER_${lang}_PRODUCED_FILES})
  117. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  118. "Compilation of the ${lang} compiler identification source \""
  119. "${CMAKE_${lang}_COMPILER_ID_SRC}\" produced \"${file}\"\n\n")
  120. ENDFOREACH(file)
  121. IF(NOT COMPILER_${lang}_PRODUCED_FILES)
  122. # No executable was found.
  123. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  124. "Compilation of the ${lang} compiler identification source \""
  125. "${CMAKE_${lang}_COMPILER_ID_SRC}\" did not produce an executable in \""
  126. "${CMAKE_${lang}_COMPILER_ID_DIR}\".\n\n")
  127. ENDIF(NOT COMPILER_${lang}_PRODUCED_FILES)
  128. ENDIF(CMAKE_${lang}_COMPILER_ID_RESULT)
  129. # Return the files produced by the compilation.
  130. SET(COMPILER_${lang}_PRODUCED_FILES "${COMPILER_${lang}_PRODUCED_FILES}" PARENT_SCOPE)
  131. ENDFUNCTION(CMAKE_DETERMINE_COMPILER_ID_BUILD lang testflags)
  132. #-----------------------------------------------------------------------------
  133. # Function to extract the compiler id from an executable.
  134. FUNCTION(CMAKE_DETERMINE_COMPILER_ID_CHECK lang file)
  135. # Look for a compiler id if not yet known.
  136. IF(NOT CMAKE_${lang}_COMPILER_ID)
  137. # Read the compiler identification string from the executable file.
  138. SET(COMPILER_ID)
  139. SET(PLATFORM_ID)
  140. FILE(STRINGS ${file}
  141. CMAKE_${lang}_COMPILER_ID_STRINGS LIMIT_COUNT 2 REGEX "INFO:")
  142. SET(HAVE_COMPILER_TWICE 0)
  143. FOREACH(info ${CMAKE_${lang}_COMPILER_ID_STRINGS})
  144. IF("${info}" MATCHES ".*INFO:compiler\\[([^]]*)\\].*")
  145. IF(COMPILER_ID)
  146. SET(COMPILER_ID_TWICE 1)
  147. ENDIF(COMPILER_ID)
  148. STRING(REGEX REPLACE ".*INFO:compiler\\[([^]]*)\\].*" "\\1"
  149. COMPILER_ID "${info}")
  150. ENDIF("${info}" MATCHES ".*INFO:compiler\\[([^]]*)\\].*")
  151. IF("${info}" MATCHES ".*INFO:platform\\[([^]]*)\\].*")
  152. STRING(REGEX REPLACE ".*INFO:platform\\[([^]]*)\\].*" "\\1"
  153. PLATFORM_ID "${info}")
  154. ENDIF("${info}" MATCHES ".*INFO:platform\\[([^]]*)\\].*")
  155. ENDFOREACH(info)
  156. # Check if a valid compiler and platform were found.
  157. IF(COMPILER_ID AND NOT COMPILER_ID_TWICE)
  158. SET(CMAKE_${lang}_COMPILER_ID "${COMPILER_ID}")
  159. SET(CMAKE_${lang}_PLATFORM_ID "${PLATFORM_ID}")
  160. ENDIF(COMPILER_ID AND NOT COMPILER_ID_TWICE)
  161. # Check the compiler identification string.
  162. IF(CMAKE_${lang}_COMPILER_ID)
  163. # The compiler identification was found.
  164. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  165. "The ${lang} compiler identification is ${CMAKE_${lang}_COMPILER_ID}, found in \""
  166. "${file}\"\n\n")
  167. ELSE(CMAKE_${lang}_COMPILER_ID)
  168. # The compiler identification could not be found.
  169. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  170. "The ${lang} compiler identification could not be found in \""
  171. "${file}\"\n\n")
  172. ENDIF(CMAKE_${lang}_COMPILER_ID)
  173. ENDIF(NOT CMAKE_${lang}_COMPILER_ID)
  174. # try to figure out the executable format: ELF, COFF, Mach-O
  175. IF(NOT CMAKE_EXECUTABLE_FORMAT)
  176. FILE(READ ${file} CMAKE_EXECUTABLE_MAGIC LIMIT 4 HEX)
  177. # ELF files start with 0x7f"ELF"
  178. IF("${CMAKE_EXECUTABLE_MAGIC}" STREQUAL "7f454c46")
  179. SET(CMAKE_EXECUTABLE_FORMAT "ELF" CACHE INTERNAL "Executable file format")
  180. ENDIF("${CMAKE_EXECUTABLE_MAGIC}" STREQUAL "7f454c46")
  181. # # COFF (.exe) files start with "MZ"
  182. # IF("${CMAKE_EXECUTABLE_MAGIC}" MATCHES "4d5a....")
  183. # SET(CMAKE_EXECUTABLE_FORMAT "COFF" CACHE STRING "Executable file format")
  184. # ENDIF("${CMAKE_EXECUTABLE_MAGIC}" MATCHES "4d5a....")
  185. #
  186. # # Mach-O files start with CAFEBABE or FEEDFACE, according to http://radio.weblogs.com/0100490/2003/01/28.html
  187. # IF("${CMAKE_EXECUTABLE_MAGIC}" MATCHES "cafebabe")
  188. # SET(CMAKE_EXECUTABLE_FORMAT "MACHO" CACHE STRING "Executable file format")
  189. # ENDIF("${CMAKE_EXECUTABLE_MAGIC}" MATCHES "cafebabe")
  190. # IF("${CMAKE_EXECUTABLE_MAGIC}" MATCHES "feedface")
  191. # SET(CMAKE_EXECUTABLE_FORMAT "MACHO" CACHE STRING "Executable file format")
  192. # ENDIF("${CMAKE_EXECUTABLE_MAGIC}" MATCHES "feedface")
  193. ENDIF(NOT CMAKE_EXECUTABLE_FORMAT)
  194. # Return the information extracted.
  195. SET(CMAKE_${lang}_COMPILER_ID "${CMAKE_${lang}_COMPILER_ID}" PARENT_SCOPE)
  196. SET(CMAKE_${lang}_PLATFORM_ID "${CMAKE_${lang}_PLATFORM_ID}" PARENT_SCOPE)
  197. SET(CMAKE_EXECUTABLE_FORMAT "${CMAKE_EXECUTABLE_FORMAT}" PARENT_SCOPE)
  198. ENDFUNCTION(CMAKE_DETERMINE_COMPILER_ID_CHECK lang)