CMakeDetermineCompilerId.cmake 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # Macro 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. MACRO(CMAKE_DETERMINE_COMPILER_ID lang flagvar src)
  5. # Store the compiler identification source file.
  6. SET(CMAKE_${lang}_COMPILER_ID_SRC "${src}")
  7. IF(WIN32 AND NOT CYGWIN)
  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(WIN32 AND NOT CYGWIN)
  14. # Make sure user-specified compiler flags are used.
  15. IF(CMAKE_${lang}_FLAGS)
  16. SET(CMAKE_${lang}_COMPILER_ID_FLAGS ${CMAKE_${lang}_FLAGS})
  17. ELSE(CMAKE_${lang}_FLAGS)
  18. SET(CMAKE_${lang}_COMPILER_ID_FLAGS $ENV{${flagvar}})
  19. ENDIF(CMAKE_${lang}_FLAGS)
  20. # Create an empty directory in which to run the test.
  21. SET(CMAKE_${lang}_COMPILER_ID_DIR ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CompilerId${lang})
  22. FILE(REMOVE_RECURSE ${CMAKE_${lang}_COMPILER_ID_DIR})
  23. FILE(MAKE_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR})
  24. # Compile the compiler identification source.
  25. STRING(REGEX REPLACE " " ";" CMAKE_${lang}_COMPILER_ID_FLAGS_LIST "${CMAKE_${lang}_COMPILER_ID_FLAGS}")
  26. IF(COMMAND EXECUTE_PROCESS)
  27. EXECUTE_PROCESS(
  28. COMMAND ${CMAKE_${lang}_COMPILER} ${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST} ${CMAKE_${lang}_COMPILER_ID_SRC}
  29. WORKING_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR}
  30. OUTPUT_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  31. ERROR_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  32. RESULT_VARIABLE CMAKE_${lang}_COMPILER_ID_RESULT
  33. )
  34. ELSE(COMMAND EXECUTE_PROCESS)
  35. EXEC_PROGRAM(
  36. ${CMAKE_${lang}_COMPILER} ${CMAKE_${lang}_COMPILER_ID_DIR}
  37. ARGS ${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST} \"${CMAKE_${lang}_COMPILER_ID_SRC}\"
  38. OUTPUT_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  39. RETURN_VALUE CMAKE_${lang}_COMPILER_ID_RESULT
  40. )
  41. ENDIF(COMMAND EXECUTE_PROCESS)
  42. # Check the result of compilation.
  43. IF(CMAKE_${lang}_COMPILER_ID_RESULT)
  44. # Compilation failed.
  45. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  46. "Compiling the ${lang} compiler identification source file \""
  47. "${CMAKE_${lang}_COMPILER_ID_SRC}\" failed with the following output:\n"
  48. "${CMAKE_${lang}_COMPILER_ID_RESULT}\n"
  49. "${CMAKE_${lang}_COMPILER_ID_OUTPUT}\n\n")
  50. IF(NOT CMAKE_${lang}_COMPILER_ID_ALLOW_FAIL)
  51. MESSAGE(FATAL_ERROR "Compiling the ${lang} compiler identification source file \""
  52. "${CMAKE_${lang}_COMPILER_ID_SRC}\" failed with the following output:\n"
  53. "${CMAKE_${lang}_COMPILER_ID_RESULT}\n"
  54. "${CMAKE_${lang}_COMPILER_ID_OUTPUT}\n\n")
  55. ENDIF(NOT CMAKE_${lang}_COMPILER_ID_ALLOW_FAIL)
  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")
  62. # Find the executable produced by the compiler, try all files in the binary dir
  63. SET(CMAKE_${lang}_COMPILER_ID)
  64. FILE(GLOB COMPILER_${lang}_PRODUCED_FILES ${CMAKE_${lang}_COMPILER_ID_DIR}/*)
  65. FOREACH(CMAKE_${lang}_COMPILER_ID_EXE ${COMPILER_${lang}_PRODUCED_FILES})
  66. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  67. "Compilation of the ${lang} compiler identification source \""
  68. "${CMAKE_${lang}_COMPILER_ID_SRC}\" produced \""
  69. "${CMAKE_${lang}_COMPILER_ID_EXE}\"\n\n")
  70. # only check if we don't have it yet
  71. IF(NOT CMAKE_${lang}_COMPILER_ID)
  72. # SET(CMAKE_${lang}_COMPILER_ID_EXE "${CMAKE_${lang}_COMPILER_ID_DIR}/ConvertedToBinary")
  73. # FILE(HEX_TO_BIN "${CMAKE_${lang}_COMPILER_ID_EXE_TRY}" "${CMAKE_${lang}_COMPILER_ID_EXE}")
  74. # Read the compiler identification string from the executable file.
  75. FILE(STRINGS ${CMAKE_${lang}_COMPILER_ID_EXE}
  76. CMAKE_${lang}_COMPILER_ID_STRINGS LIMIT_COUNT 2 REGEX "INFO:")
  77. FOREACH(info ${CMAKE_${lang}_COMPILER_ID_STRINGS})
  78. IF("${info}" MATCHES ".*INFO:compiler\\[([^]]*)\\].*")
  79. STRING(REGEX REPLACE ".*INFO:compiler\\[([^]]*)\\].*" "\\1"
  80. CMAKE_${lang}_COMPILER_ID "${info}")
  81. ENDIF("${info}" MATCHES ".*INFO:compiler\\[([^]]*)\\].*")
  82. IF("${info}" MATCHES ".*INFO:platform\\[([^]]*)\\].*")
  83. STRING(REGEX REPLACE ".*INFO:platform\\[([^]]*)\\].*" "\\1"
  84. CMAKE_${lang}_PLATFORM_ID "${info}")
  85. ENDIF("${info}" MATCHES ".*INFO:platform\\[([^]]*)\\].*")
  86. ENDFOREACH(info)
  87. # Check the compiler identification string.
  88. IF(CMAKE_${lang}_COMPILER_ID)
  89. # The compiler identification was found.
  90. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  91. "The ${lang} compiler identification is ${CMAKE_${lang}_COMPILER_ID}, found in \""
  92. "${CMAKE_${lang}_COMPILER_ID_EXE}\"\n\n")
  93. ELSE(CMAKE_${lang}_COMPILER_ID)
  94. # The compiler identification could not be found.
  95. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  96. "The ${lang} compiler identification could not be found in \""
  97. "${CMAKE_${lang}_COMPILER_ID_EXE}\"\n\n")
  98. ENDIF(CMAKE_${lang}_COMPILER_ID)
  99. ENDIF(NOT CMAKE_${lang}_COMPILER_ID)
  100. ENDFOREACH(CMAKE_${lang}_COMPILER_ID_EXE)
  101. IF(NOT COMPILER_${lang}_PRODUCED_FILES)
  102. # No executable was found.
  103. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  104. "Compilation of the ${lang} compiler identification source \""
  105. "${CMAKE_${lang}_COMPILER_ID_SRC}\" did not produce an executable in "
  106. "${CMAKE_${lang}_COMPILER_ID_DIR} .\n\n")
  107. ENDIF(NOT COMPILER_${lang}_PRODUCED_FILES)
  108. IF(CMAKE_${lang}_COMPILER_ID)
  109. MESSAGE(STATUS "The ${lang} compiler identification is "
  110. "${CMAKE_${lang}_COMPILER_ID}")
  111. ELSE(CMAKE_${lang}_COMPILER_ID)
  112. MESSAGE(STATUS "The ${lang} compiler identification is unknown")
  113. ENDIF(CMAKE_${lang}_COMPILER_ID)
  114. ENDIF(CMAKE_${lang}_COMPILER_ID_RESULT)
  115. ENDMACRO(CMAKE_DETERMINE_COMPILER_ID)