CMakeDetermineCompilerId.cmake 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 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{${lang}FLAGS})
  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. ELSE(CMAKE_${lang}_COMPILER_ID_RESULT)
  51. # Compilation succeeded.
  52. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  53. "Compiling the ${lang} compiler identification source file \""
  54. "${CMAKE_${lang}_COMPILER_ID_SRC}\" succeeded with the following output:\n"
  55. "${CMAKE_${lang}_COMPILER_ID_OUTPUT}\n\n")
  56. # Find the executable produced by the compiler.
  57. SET(CMAKE_${lang}_COMPILER_ID_EXE)
  58. GET_FILENAME_COMPONENT(CMAKE_${lang}_COMPILER_ID_SRC_BASE ${CMAKE_${lang}_COMPILER_ID_SRC} NAME_WE)
  59. FOREACH(name a.out a.exe ${CMAKE_${lang}_COMPILER_ID_SRC_BASE}.exe)
  60. IF(EXISTS ${CMAKE_${lang}_COMPILER_ID_DIR}/${name})
  61. SET(CMAKE_${lang}_COMPILER_ID_EXE ${CMAKE_${lang}_COMPILER_ID_DIR}/${name})
  62. ENDIF(EXISTS ${CMAKE_${lang}_COMPILER_ID_DIR}/${name})
  63. ENDFOREACH(name)
  64. # Check if the executable was found.
  65. IF(CMAKE_${lang}_COMPILER_ID_EXE)
  66. # The executable was found.
  67. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  68. "Compilation of the ${lang} compiler identification source \""
  69. "${CMAKE_${lang}_COMPILER_ID_SRC}\" produced \""
  70. "${CMAKE_${lang}_COMPILER_ID_EXE}\"\n\n")
  71. # Read the compiler identification string from the executable file.
  72. FILE(STRINGS ${CMAKE_${lang}_COMPILER_ID_EXE}
  73. CMAKE_${lang}_COMPILER_ID_STRINGS LIMIT_COUNT 2 REGEX "INFO:")
  74. FOREACH(info ${CMAKE_${lang}_COMPILER_ID_STRINGS})
  75. IF("${info}" MATCHES ".*INFO:compiler\\[(.*)\\].*")
  76. STRING(REGEX REPLACE ".*INFO:compiler\\[(.*)\\].*" "\\1"
  77. CMAKE_${lang}_COMPILER_ID "${info}")
  78. ELSEIF("${info}" MATCHES ".*INFO:platform\\[(.*)\\].*")
  79. STRING(REGEX REPLACE ".*INFO:platform\\[(.*)\\].*" "\\1"
  80. CMAKE_${lang}_PLATFORM_ID "${info}")
  81. ENDIF("${info}" MATCHES ".*INFO:compiler\\[(.*)\\].*")
  82. ENDFOREACH(info)
  83. # Check the compiler identification string.
  84. IF(CMAKE_${lang}_COMPILER_ID)
  85. # The compiler identification was found.
  86. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  87. "The ${lang} compiler identification is ${CMAKE_${lang}_COMPILER_ID}\n\n")
  88. ELSE(CMAKE_${lang}_COMPILER_ID)
  89. # The compiler identification could not be found.
  90. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  91. "The ${lang} compiler identification could not be found in \""
  92. "${CMAKE_${lang}_COMPILER_ID_EXE}\"\n\n")
  93. ENDIF(CMAKE_${lang}_COMPILER_ID)
  94. ELSE(CMAKE_${lang}_COMPILER_ID_EXE)
  95. # The executable was not found.
  96. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  97. "Compilation of the ${lang} compiler identification source \""
  98. "${CMAKE_${lang}_COMPILER_ID_SRC}\" did not produce an executable in "
  99. "${CMAKE_${lang}_COMPILER_ID_DIR} "
  100. "with a name known to CMake.\n\n")
  101. ENDIF(CMAKE_${lang}_COMPILER_ID_EXE)
  102. ENDIF(CMAKE_${lang}_COMPILER_ID_RESULT)
  103. ENDMACRO(CMAKE_DETERMINE_COMPILER_ID)