CMakeDetermineCompilerId.cmake 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.
  63. SET(CMAKE_${lang}_COMPILER_ID_EXE)
  64. GET_FILENAME_COMPONENT(CMAKE_${lang}_COMPILER_ID_SRC_BASE ${CMAKE_${lang}_COMPILER_ID_SRC} NAME_WE)
  65. FOREACH(name a.out a.exe ${CMAKE_${lang}_COMPILER_ID_SRC_BASE}.exe)
  66. IF(EXISTS ${CMAKE_${lang}_COMPILER_ID_DIR}/${name})
  67. SET(CMAKE_${lang}_COMPILER_ID_EXE ${CMAKE_${lang}_COMPILER_ID_DIR}/${name})
  68. ENDIF(EXISTS ${CMAKE_${lang}_COMPILER_ID_DIR}/${name})
  69. ENDFOREACH(name)
  70. # Check if the executable was found.
  71. IF(CMAKE_${lang}_COMPILER_ID_EXE)
  72. # The executable was found.
  73. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  74. "Compilation of the ${lang} compiler identification source \""
  75. "${CMAKE_${lang}_COMPILER_ID_SRC}\" produced \""
  76. "${CMAKE_${lang}_COMPILER_ID_EXE}\"\n\n")
  77. # Read the compiler identification string from the executable file.
  78. FILE(STRINGS ${CMAKE_${lang}_COMPILER_ID_EXE}
  79. CMAKE_${lang}_COMPILER_ID_STRINGS LIMIT_COUNT 2 REGEX "INFO:")
  80. FOREACH(info ${CMAKE_${lang}_COMPILER_ID_STRINGS})
  81. IF("${info}" MATCHES ".*INFO:compiler\\[([^]]*)\\].*")
  82. STRING(REGEX REPLACE ".*INFO:compiler\\[([^]]*)\\].*" "\\1"
  83. CMAKE_${lang}_COMPILER_ID "${info}")
  84. ENDIF("${info}" MATCHES ".*INFO:compiler\\[([^]]*)\\].*")
  85. IF("${info}" MATCHES ".*INFO:platform\\[([^]]*)\\].*")
  86. STRING(REGEX REPLACE ".*INFO:platform\\[([^]]*)\\].*" "\\1"
  87. CMAKE_${lang}_PLATFORM_ID "${info}")
  88. ENDIF("${info}" MATCHES ".*INFO:platform\\[([^]]*)\\].*")
  89. ENDFOREACH(info)
  90. # Check the compiler identification string.
  91. IF(CMAKE_${lang}_COMPILER_ID)
  92. # The compiler identification was found.
  93. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  94. "The ${lang} compiler identification is ${CMAKE_${lang}_COMPILER_ID}\n\n")
  95. ELSE(CMAKE_${lang}_COMPILER_ID)
  96. # The compiler identification could not be found.
  97. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  98. "The ${lang} compiler identification could not be found in \""
  99. "${CMAKE_${lang}_COMPILER_ID_EXE}\"\n\n")
  100. ENDIF(CMAKE_${lang}_COMPILER_ID)
  101. ELSE(CMAKE_${lang}_COMPILER_ID_EXE)
  102. # The executable was not 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} "
  107. "with a name known to CMake.\n\n")
  108. ENDIF(CMAKE_${lang}_COMPILER_ID_EXE)
  109. IF(CMAKE_${lang}_COMPILER_ID)
  110. MESSAGE(STATUS "The ${lang} compiler identification is "
  111. "${CMAKE_${lang}_COMPILER_ID}")
  112. ELSE(CMAKE_${lang}_COMPILER_ID)
  113. MESSAGE(STATUS "The ${lang} compiler identification is unknown")
  114. ENDIF(CMAKE_${lang}_COMPILER_ID)
  115. ENDIF(CMAKE_${lang}_COMPILER_ID_RESULT)
  116. ENDMACRO(CMAKE_DETERMINE_COMPILER_ID)