CMakeCompilerIdDetection.cmake 4.9 KB

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