CMakeCompilerIdDetection.cmake 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. GHS
  59. )
  60. if (lang STREQUAL C)
  61. list(APPEND ordered_compilers
  62. TinyCC
  63. Bruce
  64. )
  65. endif()
  66. list(APPEND ordered_compilers
  67. SCO
  68. AppleClang
  69. Clang
  70. GNU
  71. MSVC
  72. ADSP
  73. IAR
  74. ARMCC
  75. )
  76. if (lang STREQUAL C)
  77. list(APPEND ordered_compilers
  78. SDCC
  79. )
  80. endif()
  81. #Currently the only CUDA compilers are NVIDIA
  82. if(lang STREQUAL CUDA)
  83. set(ordered_compilers NVIDIA)
  84. endif()
  85. if(CID_ID_DEFINE)
  86. foreach(Id ${ordered_compilers})
  87. string(APPEND CMAKE_${lang}_COMPILER_ID_CONTENT "# define ${CID_PREFIX}COMPILER_IS_${Id} 0\n")
  88. endforeach()
  89. # Hard-code definitions for compilers that are no longer supported.
  90. string(APPEND CMAKE_${lang}_COMPILER_ID_CONTENT "# define ${CID_PREFIX}COMPILER_IS_MIPSpro 0\n")
  91. endif()
  92. set(pp_if "#if")
  93. if (CID_VERSION_STRINGS)
  94. string(APPEND CMAKE_${lang}_COMPILER_ID_CONTENT "\n/* Version number components: V=Version, R=Revision, P=Patch
  95. Version date components: YYYY=Year, MM=Month, DD=Day */\n")
  96. endif()
  97. foreach(Id ${ordered_compilers})
  98. if (NOT _compiler_id_pp_test_${Id})
  99. message(FATAL_ERROR "No preprocessor test for \"${Id}\"")
  100. endif()
  101. set(id_content "${pp_if} ${_compiler_id_pp_test_${Id}}\n")
  102. if (CID_ID_STRING)
  103. set(PREFIX ${CID_PREFIX})
  104. string(CONFIGURE "${_compiler_id_simulate_${Id}}" SIMULATE_BLOCK @ONLY)
  105. string(APPEND id_content "# define ${CID_PREFIX}COMPILER_ID \"${Id}\"${SIMULATE_BLOCK}")
  106. endif()
  107. if (CID_ID_DEFINE)
  108. string(APPEND id_content "# undef ${CID_PREFIX}COMPILER_IS_${Id}\n")
  109. string(APPEND id_content "# define ${CID_PREFIX}COMPILER_IS_${Id} 1\n")
  110. endif()
  111. if (CID_VERSION_STRINGS)
  112. set(PREFIX ${CID_PREFIX})
  113. set(MACRO_DEC DEC)
  114. set(MACRO_HEX HEX)
  115. string(CONFIGURE "${_compiler_id_version_compute_${Id}}" VERSION_BLOCK @ONLY)
  116. string(APPEND id_content "${VERSION_BLOCK}\n")
  117. endif()
  118. string(APPEND CMAKE_${lang}_COMPILER_ID_CONTENT "\n${id_content}")
  119. set(pp_if "#elif")
  120. endforeach()
  121. if (CID_PLATFORM_DEFAULT_COMPILER)
  122. set(platform_compiler_detection "
  123. /* These compilers are either not known or too old to define an
  124. identification macro. Try to identify the platform and guess that
  125. it is the native compiler. */
  126. #elif defined(__hpux) || defined(__hpua)
  127. # define ${CID_PREFIX}COMPILER_ID \"HP\"
  128. #else /* unknown compiler */
  129. # define ${CID_PREFIX}COMPILER_ID \"\"")
  130. endif()
  131. string(APPEND CMAKE_${lang}_COMPILER_ID_CONTENT "\n${platform_compiler_detection}\n#endif")
  132. endif()
  133. set(${outvar} ${CMAKE_${lang}_COMPILER_ID_CONTENT} PARENT_SCOPE)
  134. endfunction()