CMakeCompilerIdDetection.cmake 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 "x${lang}" STREQUAL "xFortran" AND NOT "x${lang}" STREQUAL "xCSharp"
  13. AND NOT "x${lang}" STREQUAL "xISPC")
  14. file(GLOB lang_files
  15. "${CMAKE_ROOT}/Modules/Compiler/*-DetermineCompiler.cmake")
  16. set(nonlang CXX)
  17. if ("x${lang}" STREQUAL "xCXX")
  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 ("x${lang}" STREQUAL "xCXX")
  38. list(APPEND ordered_compilers
  39. Comeau
  40. )
  41. endif()
  42. list(APPEND ordered_compilers
  43. Intel
  44. IntelLLVM
  45. PathScale
  46. Embarcadero
  47. Borland
  48. Watcom
  49. OpenWatcom
  50. SunPro
  51. HP
  52. Compaq
  53. zOS
  54. XLClang
  55. XL
  56. VisualAge
  57. NVHPC
  58. PGI
  59. Cray
  60. TI
  61. FujitsuClang
  62. Fujitsu
  63. GHS
  64. )
  65. if ("x${lang}" STREQUAL "xC")
  66. list(APPEND ordered_compilers
  67. TinyCC
  68. Bruce
  69. )
  70. endif()
  71. list(APPEND ordered_compilers
  72. SCO
  73. ARMCC
  74. AppleClang
  75. ARMClang
  76. )
  77. list(APPEND ordered_compilers
  78. Clang
  79. LCC
  80. GNU
  81. MSVC
  82. ADSP
  83. IAR
  84. )
  85. if ("x${lang}" STREQUAL "xC")
  86. list(APPEND ordered_compilers
  87. SDCC
  88. )
  89. endif()
  90. if("x${lang}" STREQUAL "xCUDA")
  91. set(ordered_compilers NVIDIA Clang)
  92. endif()
  93. if(CID_ID_DEFINE)
  94. foreach(Id ${ordered_compilers})
  95. string(APPEND CMAKE_${lang}_COMPILER_ID_CONTENT "# define ${CID_PREFIX}COMPILER_IS_${Id} 0\n")
  96. endforeach()
  97. # Hard-code definitions for compilers that are no longer supported.
  98. string(APPEND CMAKE_${lang}_COMPILER_ID_CONTENT "# define ${CID_PREFIX}COMPILER_IS_MIPSpro 0\n")
  99. endif()
  100. set(pp_if "#if")
  101. if (CID_VERSION_STRINGS)
  102. string(APPEND CMAKE_${lang}_COMPILER_ID_CONTENT "\n/* Version number components: V=Version, R=Revision, P=Patch
  103. Version date components: YYYY=Year, MM=Month, DD=Day */\n")
  104. endif()
  105. foreach(Id ${ordered_compilers})
  106. if (NOT _compiler_id_pp_test_${Id})
  107. message(FATAL_ERROR "No preprocessor test for \"${Id}\"")
  108. endif()
  109. set(id_content "${pp_if} ${_compiler_id_pp_test_${Id}}\n")
  110. if (CID_ID_STRING)
  111. set(PREFIX ${CID_PREFIX})
  112. string(CONFIGURE "${_compiler_id_simulate_${Id}}" SIMULATE_BLOCK @ONLY)
  113. string(APPEND id_content "# define ${CID_PREFIX}COMPILER_ID \"${Id}\"${SIMULATE_BLOCK}")
  114. endif()
  115. if (CID_ID_DEFINE)
  116. string(APPEND id_content "# undef ${CID_PREFIX}COMPILER_IS_${Id}\n")
  117. string(APPEND id_content "# define ${CID_PREFIX}COMPILER_IS_${Id} 1\n")
  118. endif()
  119. if (CID_VERSION_STRINGS)
  120. set(PREFIX ${CID_PREFIX})
  121. set(MACRO_DEC DEC)
  122. set(MACRO_HEX HEX)
  123. string(CONFIGURE "${_compiler_id_version_compute_${Id}}" VERSION_BLOCK @ONLY)
  124. string(APPEND id_content "${VERSION_BLOCK}\n")
  125. endif()
  126. string(APPEND CMAKE_${lang}_COMPILER_ID_CONTENT "\n${id_content}")
  127. set(pp_if "#elif")
  128. endforeach()
  129. if (CID_PLATFORM_DEFAULT_COMPILER)
  130. set(platform_compiler_detection "
  131. /* These compilers are either not known or too old to define an
  132. identification macro. Try to identify the platform and guess that
  133. it is the native compiler. */
  134. #elif defined(__hpux) || defined(__hpua)
  135. # define ${CID_PREFIX}COMPILER_ID \"HP\"
  136. #else /* unknown compiler */
  137. # define ${CID_PREFIX}COMPILER_ID \"\"")
  138. endif()
  139. string(APPEND CMAKE_${lang}_COMPILER_ID_CONTENT "\n${platform_compiler_detection}\n#endif")
  140. endif()
  141. set(${outvar} ${CMAKE_${lang}_COMPILER_ID_CONTENT} PARENT_SCOPE)
  142. endfunction()