1
0

CMakeDetermineCompilerId.cmake 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 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. # Create an empty directory in which to run the test.
  23. SET(CMAKE_${lang}_COMPILER_ID_DIR ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CompilerId${lang})
  24. FILE(REMOVE_RECURSE ${CMAKE_${lang}_COMPILER_ID_DIR})
  25. FILE(MAKE_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR})
  26. # Compile the compiler identification source.
  27. STRING(REGEX REPLACE " " ";" CMAKE_${lang}_COMPILER_ID_FLAGS_LIST "${CMAKE_${lang}_COMPILER_ID_FLAGS}")
  28. IF(COMMAND EXECUTE_PROCESS)
  29. EXECUTE_PROCESS(
  30. COMMAND ${CMAKE_${lang}_COMPILER} ${CMAKE_${lang}_COMPILER_ID_ARG1} ${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST} ${CMAKE_${lang}_COMPILER_ID_SRC}
  31. WORKING_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR}
  32. OUTPUT_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  33. ERROR_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  34. RESULT_VARIABLE CMAKE_${lang}_COMPILER_ID_RESULT
  35. )
  36. ELSE(COMMAND EXECUTE_PROCESS)
  37. EXEC_PROGRAM(
  38. ${CMAKE_${lang}_COMPILER} ${CMAKE_${lang}_COMPILER_ID_DIR}
  39. ARGS ${CMAKE_${lang}_COMPILER_ID_ARG1} ${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST} \"${CMAKE_${lang}_COMPILER_ID_SRC}\"
  40. OUTPUT_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  41. RETURN_VALUE CMAKE_${lang}_COMPILER_ID_RESULT
  42. )
  43. ENDIF(COMMAND EXECUTE_PROCESS)
  44. # Check the result of compilation.
  45. IF(CMAKE_${lang}_COMPILER_ID_RESULT)
  46. # Compilation failed.
  47. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  48. "Compiling the ${lang} compiler identification source file \""
  49. "${CMAKE_${lang}_COMPILER_ID_SRC}\" failed with the following output:\n"
  50. "${CMAKE_${lang}_COMPILER_ID_RESULT}\n"
  51. "${CMAKE_${lang}_COMPILER_ID_OUTPUT}\n\n")
  52. IF(NOT CMAKE_${lang}_COMPILER_ID_ALLOW_FAIL)
  53. MESSAGE(FATAL_ERROR "Compiling the ${lang} compiler identification source file \""
  54. "${CMAKE_${lang}_COMPILER_ID_SRC}\" failed with the following output:\n"
  55. "${CMAKE_${lang}_COMPILER_ID_RESULT}\n"
  56. "${CMAKE_${lang}_COMPILER_ID_OUTPUT}\n\n")
  57. ENDIF(NOT CMAKE_${lang}_COMPILER_ID_ALLOW_FAIL)
  58. ELSE(CMAKE_${lang}_COMPILER_ID_RESULT)
  59. # Compilation succeeded.
  60. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  61. "Compiling the ${lang} compiler identification source file \""
  62. "${CMAKE_${lang}_COMPILER_ID_SRC}\" succeeded with the following output:\n"
  63. "${CMAKE_${lang}_COMPILER_ID_OUTPUT}\n\n")
  64. # Find the executable produced by the compiler, try all files in the binary dir
  65. SET(CMAKE_${lang}_COMPILER_ID)
  66. FILE(GLOB COMPILER_${lang}_PRODUCED_FILES ${CMAKE_${lang}_COMPILER_ID_DIR}/*)
  67. FOREACH(CMAKE_${lang}_COMPILER_ID_EXE ${COMPILER_${lang}_PRODUCED_FILES})
  68. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  69. "Compilation of the ${lang} compiler identification source \""
  70. "${CMAKE_${lang}_COMPILER_ID_SRC}\" produced \""
  71. "${CMAKE_${lang}_COMPILER_ID_EXE}\"\n\n")
  72. # only check if we don't have it yet
  73. IF(NOT CMAKE_${lang}_COMPILER_ID)
  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)