CMakeCompilerIdDetection.cmake 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. function(_readFile file)
  4. include(${file})
  5. get_filename_component(name ${file} NAME_WE)
  6. string(REGEX REPLACE "-.*" "" CompilerId ${name})
  7. set(_compiler_id_version_compute_${CompilerId} ${_compiler_id_version_compute} PARENT_SCOPE)
  8. set(_compiler_id_simulate_${CompilerId} ${_compiler_id_simulate} PARENT_SCOPE)
  9. set(_compiler_id_pp_test_${CompilerId} ${_compiler_id_pp_test} PARENT_SCOPE)
  10. endfunction()
  11. function(compiler_id_detection outvar lang)
  12. if (NOT lang STREQUAL Fortran AND NOT lang STREQUAL CSharp)
  13. file(GLOB lang_files
  14. "${CMAKE_ROOT}/Modules/Compiler/*-DetermineCompiler.cmake")
  15. set(nonlang CXX)
  16. if (lang STREQUAL CXX)
  17. set(nonlang C)
  18. endif()
  19. file(GLOB nonlang_files
  20. "${CMAKE_ROOT}/Modules/Compiler/*-${nonlang}-DetermineCompiler.cmake")
  21. list(REMOVE_ITEM lang_files ${nonlang_files})
  22. endif()
  23. set(files ${lang_files})
  24. if (files)
  25. foreach(file ${files})
  26. _readFile(${file})
  27. endforeach()
  28. set(options ID_STRING VERSION_STRINGS ID_DEFINE PLATFORM_DEFAULT_COMPILER)
  29. set(oneValueArgs PREFIX)
  30. cmake_parse_arguments(CID "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  31. if (CID_UNPARSED_ARGUMENTS)
  32. message(FATAL_ERROR "Unrecognized arguments: \"${CID_UNPARSED_ARGUMENTS}\"")
  33. endif()
  34. # Order is relevant here. For example, compilers which pretend to be
  35. # GCC must appear before the actual GCC.
  36. if (lang STREQUAL CXX)
  37. list(APPEND ordered_compilers
  38. Comeau
  39. )
  40. endif()
  41. list(APPEND ordered_compilers
  42. Intel
  43. PathScale
  44. Embarcadero
  45. Borland
  46. Watcom
  47. OpenWatcom
  48. SunPro
  49. HP
  50. Compaq
  51. zOS
  52. XL
  53. VisualAge
  54. PGI
  55. Cray
  56. TI
  57. Fujitsu
  58. )
  59. if (lang STREQUAL C)
  60. list(APPEND ordered_compilers
  61. TinyCC
  62. Bruce
  63. )
  64. endif()
  65. list(APPEND ordered_compilers
  66. SCO
  67. AppleClang
  68. Clang
  69. GNU
  70. MSVC
  71. ADSP
  72. IAR
  73. ARMCC
  74. )
  75. if (lang STREQUAL C)
  76. list(APPEND ordered_compilers
  77. SDCC
  78. )
  79. endif()
  80. list(APPEND ordered_compilers
  81. MIPSpro)
  82. #Currently the only CUDA compilers are NVIDIA
  83. if(lang STREQUAL CUDA)
  84. set(ordered_compilers NVIDIA)
  85. endif()
  86. if(CID_ID_DEFINE)
  87. foreach(Id ${ordered_compilers})
  88. string(APPEND CMAKE_${lang}_COMPILER_ID_CONTENT "# define ${CID_PREFIX}COMPILER_IS_${Id} 0\n")
  89. endforeach()
  90. endif()
  91. set(pp_if "#if")
  92. if (CID_VERSION_STRINGS)
  93. string(APPEND CMAKE_${lang}_COMPILER_ID_CONTENT "\n/* Version number components: V=Version, R=Revision, P=Patch
  94. Version date components: YYYY=Year, MM=Month, DD=Day */\n")
  95. endif()
  96. foreach(Id ${ordered_compilers})
  97. if (NOT _compiler_id_pp_test_${Id})
  98. message(FATAL_ERROR "No preprocessor test for \"${Id}\"")
  99. endif()
  100. set(id_content "${pp_if} ${_compiler_id_pp_test_${Id}}\n")
  101. if (CID_ID_STRING)
  102. set(PREFIX ${CID_PREFIX})
  103. string(CONFIGURE "${_compiler_id_simulate_${Id}}" SIMULATE_BLOCK @ONLY)
  104. string(APPEND id_content "# define ${CID_PREFIX}COMPILER_ID \"${Id}\"${SIMULATE_BLOCK}")
  105. endif()
  106. if (CID_ID_DEFINE)
  107. string(APPEND id_content "# undef ${CID_PREFIX}COMPILER_IS_${Id}\n")
  108. string(APPEND id_content "# define ${CID_PREFIX}COMPILER_IS_${Id} 1\n")
  109. endif()
  110. if (CID_VERSION_STRINGS)
  111. set(PREFIX ${CID_PREFIX})
  112. set(MACRO_DEC DEC)
  113. set(MACRO_HEX HEX)
  114. string(CONFIGURE "${_compiler_id_version_compute_${Id}}" VERSION_BLOCK @ONLY)
  115. string(APPEND id_content "${VERSION_BLOCK}\n")
  116. endif()
  117. string(APPEND CMAKE_${lang}_COMPILER_ID_CONTENT "\n${id_content}")
  118. set(pp_if "#elif")
  119. endforeach()
  120. if (CID_PLATFORM_DEFAULT_COMPILER)
  121. set(platform_compiler_detection "
  122. /* These compilers are either not known or too old to define an
  123. identification macro. Try to identify the platform and guess that
  124. it is the native compiler. */
  125. #elif defined(__sgi)
  126. # define ${CID_PREFIX}COMPILER_ID \"MIPSpro\"
  127. #elif defined(__hpux) || defined(__hpua)
  128. # define ${CID_PREFIX}COMPILER_ID \"HP\"
  129. #else /* unknown compiler */
  130. # define ${CID_PREFIX}COMPILER_ID \"\"")
  131. endif()
  132. string(APPEND CMAKE_${lang}_COMPILER_ID_CONTENT "\n${platform_compiler_detection}\n#endif")
  133. endif()
  134. set(${outvar} ${CMAKE_${lang}_COMPILER_ID_CONTENT} PARENT_SCOPE)
  135. endfunction()