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