CMakeDetermineCompilerId.cmake 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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(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 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_ARG1} ${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_ARG1} ${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. # Read the compiler identification string from the executable file.
  73. FILE(STRINGS ${CMAKE_${lang}_COMPILER_ID_EXE}
  74. CMAKE_${lang}_COMPILER_ID_STRINGS LIMIT_COUNT 2 REGEX "INFO:")
  75. FOREACH(info ${CMAKE_${lang}_COMPILER_ID_STRINGS})
  76. IF("${info}" MATCHES ".*INFO:compiler\\[([^]]*)\\].*")
  77. STRING(REGEX REPLACE ".*INFO:compiler\\[([^]]*)\\].*" "\\1"
  78. CMAKE_${lang}_COMPILER_ID "${info}")
  79. ENDIF("${info}" MATCHES ".*INFO:compiler\\[([^]]*)\\].*")
  80. IF("${info}" MATCHES ".*INFO:platform\\[([^]]*)\\].*")
  81. STRING(REGEX REPLACE ".*INFO:platform\\[([^]]*)\\].*" "\\1"
  82. CMAKE_${lang}_PLATFORM_ID "${info}")
  83. ENDIF("${info}" MATCHES ".*INFO:platform\\[([^]]*)\\].*")
  84. ENDFOREACH(info)
  85. # Check the compiler identification string.
  86. IF(CMAKE_${lang}_COMPILER_ID)
  87. # The compiler identification was found.
  88. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  89. "The ${lang} compiler identification is ${CMAKE_${lang}_COMPILER_ID}, found in \""
  90. "${CMAKE_${lang}_COMPILER_ID_EXE}\"\n\n")
  91. ELSE(CMAKE_${lang}_COMPILER_ID)
  92. # The compiler identification could not be found.
  93. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  94. "The ${lang} compiler identification could not be found in \""
  95. "${CMAKE_${lang}_COMPILER_ID_EXE}\"\n\n")
  96. ENDIF(CMAKE_${lang}_COMPILER_ID)
  97. ENDIF(NOT CMAKE_${lang}_COMPILER_ID)
  98. ENDFOREACH(CMAKE_${lang}_COMPILER_ID_EXE)
  99. IF(NOT COMPILER_${lang}_PRODUCED_FILES)
  100. # No executable was found.
  101. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  102. "Compilation of the ${lang} compiler identification source \""
  103. "${CMAKE_${lang}_COMPILER_ID_SRC}\" did not produce an executable in "
  104. "${CMAKE_${lang}_COMPILER_ID_DIR} .\n\n")
  105. ENDIF(NOT COMPILER_${lang}_PRODUCED_FILES)
  106. IF(CMAKE_${lang}_COMPILER_ID)
  107. MESSAGE(STATUS "The ${lang} compiler identification is "
  108. "${CMAKE_${lang}_COMPILER_ID}")
  109. ELSE(CMAKE_${lang}_COMPILER_ID)
  110. MESSAGE(STATUS "The ${lang} compiler identification is unknown")
  111. ENDIF(CMAKE_${lang}_COMPILER_ID)
  112. ENDIF(CMAKE_${lang}_COMPILER_ID_RESULT)
  113. ENDMACRO(CMAKE_DETERMINE_COMPILER_ID)