Clang.cmake 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. # This module is shared by multiple languages; use include blocker.
  4. if(__COMPILER_CLANG)
  5. return()
  6. endif()
  7. set(__COMPILER_CLANG 1)
  8. include(Compiler/CMakeCommonCompilerMacros)
  9. set(__pch_header_C "c-header")
  10. set(__pch_header_CXX "c++-header")
  11. set(__pch_header_OBJC "objective-c-header")
  12. set(__pch_header_OBJCXX "objective-c++-header")
  13. if("x${CMAKE_C_SIMULATE_ID}" STREQUAL "xMSVC"
  14. OR "x${CMAKE_CXX_SIMULATE_ID}" STREQUAL "xMSVC"
  15. OR "x${CMAKE_Fortran_SIMULATE_ID}" STREQUAL "xMSVC")
  16. macro(__compiler_clang lang)
  17. set(CMAKE_INCLUDE_SYSTEM_FLAG_${lang} "-imsvc ")
  18. endmacro()
  19. else()
  20. include(Compiler/GNU)
  21. macro(__compiler_clang lang)
  22. __compiler_gnu(${lang})
  23. set(CMAKE_${lang}_COMPILE_OPTIONS_PIE "-fPIE")
  24. # Link options for PIE are already set in 'Compiler/GNU.cmake'
  25. # but clang may require alternate syntax on some platforms
  26. if (APPLE)
  27. set(CMAKE_${lang}_LINK_OPTIONS_PIE ${CMAKE_${lang}_COMPILE_OPTIONS_PIE} -Xlinker -pie)
  28. set(CMAKE_${lang}_LINK_OPTIONS_NO_PIE -Xlinker -no_pie)
  29. endif()
  30. set(CMAKE_INCLUDE_SYSTEM_FLAG_${lang} "-isystem ")
  31. set(CMAKE_${lang}_COMPILE_OPTIONS_VISIBILITY "-fvisibility=")
  32. if(CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 3.4.0)
  33. set(CMAKE_${lang}_COMPILE_OPTIONS_TARGET "-target ")
  34. set(CMAKE_${lang}_COMPILE_OPTIONS_EXTERNAL_TOOLCHAIN "-gcc-toolchain ")
  35. else()
  36. set(CMAKE_${lang}_COMPILE_OPTIONS_TARGET "--target=")
  37. set(CMAKE_${lang}_COMPILE_OPTIONS_EXTERNAL_TOOLCHAIN "--gcc-toolchain=")
  38. endif()
  39. set(CMAKE_${lang}_LINKER_WRAPPER_FLAG "-Xlinker" " ")
  40. set(CMAKE_${lang}_LINKER_WRAPPER_FLAG_SEP)
  41. if(CMAKE_${lang}_COMPILER_TARGET)
  42. if(CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 3.4.0)
  43. list(APPEND CMAKE_${lang}_COMPILER_PREDEFINES_COMMAND "-target" "${CMAKE_${lang}_COMPILER_TARGET}")
  44. else()
  45. list(APPEND CMAKE_${lang}_COMPILER_PREDEFINES_COMMAND "--target=${CMAKE_${lang}_COMPILER_TARGET}")
  46. endif()
  47. endif()
  48. set(_CMAKE_${lang}_IPO_SUPPORTED_BY_CMAKE YES)
  49. set(_CMAKE_${lang}_IPO_MAY_BE_SUPPORTED_BY_COMPILER YES)
  50. string(COMPARE EQUAL "${CMAKE_${lang}_COMPILER_ID}" "AppleClang" __is_apple_clang)
  51. # '-flto=thin' available since Clang 3.9 and Xcode 8
  52. # * http://clang.llvm.org/docs/ThinLTO.html#clang-llvm
  53. # * https://trac.macports.org/wiki/XcodeVersionInfo
  54. set(_CMAKE_LTO_THIN TRUE)
  55. if(__is_apple_clang)
  56. if(CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 8.0)
  57. set(_CMAKE_LTO_THIN FALSE)
  58. endif()
  59. else()
  60. if(CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 3.9)
  61. set(_CMAKE_LTO_THIN FALSE)
  62. endif()
  63. endif()
  64. if(_CMAKE_LTO_THIN)
  65. set(CMAKE_${lang}_COMPILE_OPTIONS_IPO "-flto=thin")
  66. else()
  67. set(CMAKE_${lang}_COMPILE_OPTIONS_IPO "-flto")
  68. endif()
  69. if(ANDROID)
  70. # https://github.com/android-ndk/ndk/issues/242
  71. set(CMAKE_${lang}_LINK_OPTIONS_IPO "-fuse-ld=gold")
  72. endif()
  73. if(ANDROID OR __is_apple_clang)
  74. set(__ar "${CMAKE_AR}")
  75. set(__ranlib "${CMAKE_RANLIB}")
  76. else()
  77. set(__ar "${CMAKE_${lang}_COMPILER_AR}")
  78. set(__ranlib "${CMAKE_${lang}_COMPILER_RANLIB}")
  79. endif()
  80. set(CMAKE_${lang}_ARCHIVE_CREATE_IPO
  81. "\"${__ar}\" cr <TARGET> <LINK_FLAGS> <OBJECTS>"
  82. )
  83. set(CMAKE_${lang}_ARCHIVE_APPEND_IPO
  84. "\"${__ar}\" r <TARGET> <LINK_FLAGS> <OBJECTS>"
  85. )
  86. set(CMAKE_${lang}_ARCHIVE_FINISH_IPO
  87. "\"${__ranlib}\" <TARGET>"
  88. )
  89. set(CMAKE_PCH_EXTENSION .pch)
  90. if (NOT CMAKE_GENERATOR MATCHES "Xcode")
  91. set(CMAKE_PCH_PROLOGUE "#pragma clang system_header")
  92. endif()
  93. if(CMAKE_${lang}_COMPILER_VERSION VERSION_GREATER_EQUAL 11.0.0 AND NOT __is_apple_clang)
  94. set(CMAKE_${lang}_COMPILE_OPTIONS_INSTANTIATE_TEMPLATES_PCH -fpch-instantiate-templates)
  95. endif()
  96. set(CMAKE_${lang}_COMPILE_OPTIONS_USE_PCH -Xclang -include-pch -Xclang <PCH_FILE> -Xclang -include -Xclang <PCH_HEADER>)
  97. set(CMAKE_${lang}_COMPILE_OPTIONS_CREATE_PCH -Xclang -emit-pch -Xclang -include -Xclang <PCH_HEADER> -x ${__pch_header_${lang}})
  98. endmacro()
  99. endif()
  100. macro(__compiler_clang_cxx_standards lang)
  101. if("x${CMAKE_${lang}_COMPILER_FRONTEND_VARIANT}" STREQUAL "xGNU")
  102. if(NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 2.1)
  103. set(CMAKE_${lang}98_STANDARD_COMPILE_OPTION "-std=c++98")
  104. set(CMAKE_${lang}98_EXTENSION_COMPILE_OPTION "-std=gnu++98")
  105. endif()
  106. if(NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 3.1)
  107. set(CMAKE_${lang}98_STANDARD__HAS_FULL_SUPPORT ON)
  108. set(CMAKE_${lang}11_STANDARD_COMPILE_OPTION "-std=c++11")
  109. set(CMAKE_${lang}11_EXTENSION_COMPILE_OPTION "-std=gnu++11")
  110. set(CMAKE_${lang}11_STANDARD__HAS_FULL_SUPPORT ON)
  111. elseif(NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 2.1)
  112. set(CMAKE_${lang}11_STANDARD_COMPILE_OPTION "-std=c++0x")
  113. set(CMAKE_${lang}11_EXTENSION_COMPILE_OPTION "-std=gnu++0x")
  114. endif()
  115. if(NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 3.5)
  116. set(CMAKE_${lang}14_STANDARD_COMPILE_OPTION "-std=c++14")
  117. set(CMAKE_${lang}14_EXTENSION_COMPILE_OPTION "-std=gnu++14")
  118. set(CMAKE_${lang}14_STANDARD__HAS_FULL_SUPPORT ON)
  119. elseif(NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 3.4)
  120. set(CMAKE_${lang}14_STANDARD_COMPILE_OPTION "-std=c++1y")
  121. set(CMAKE_${lang}14_EXTENSION_COMPILE_OPTION "-std=gnu++1y")
  122. set(CMAKE_${lang}14_STANDARD__HAS_FULL_SUPPORT ON)
  123. endif()
  124. set(_clang_version_std17 5.0)
  125. if(CMAKE_SYSTEM_NAME STREQUAL "Android")
  126. set(_clang_version_std17 6.0)
  127. endif()
  128. if (NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS "${_clang_version_std17}")
  129. set(CMAKE_${lang}17_STANDARD_COMPILE_OPTION "-std=c++17")
  130. set(CMAKE_${lang}17_EXTENSION_COMPILE_OPTION "-std=gnu++17")
  131. elseif (NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 3.5)
  132. set(CMAKE_${lang}17_STANDARD_COMPILE_OPTION "-std=c++1z")
  133. set(CMAKE_${lang}17_EXTENSION_COMPILE_OPTION "-std=gnu++1z")
  134. endif()
  135. if(NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 6.0)
  136. set(CMAKE_${lang}17_STANDARD__HAS_FULL_SUPPORT ON)
  137. endif()
  138. if(NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 11.0)
  139. set(CMAKE_${lang}20_STANDARD_COMPILE_OPTION "-std=c++20")
  140. set(CMAKE_${lang}20_EXTENSION_COMPILE_OPTION "-std=gnu++20")
  141. elseif(NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS "${_clang_version_std17}")
  142. set(CMAKE_${lang}20_STANDARD_COMPILE_OPTION "-std=c++2a")
  143. set(CMAKE_${lang}20_EXTENSION_COMPILE_OPTION "-std=gnu++2a")
  144. endif()
  145. unset(_clang_version_std17)
  146. if("x${CMAKE_${lang}_SIMULATE_ID}" STREQUAL "xMSVC")
  147. # The MSVC standard library requires C++14, and MSVC itself has no
  148. # notion of operating in a mode not aware of at least that standard.
  149. set(CMAKE_${lang}98_STANDARD_COMPILE_OPTION "-std=c++14")
  150. set(CMAKE_${lang}98_EXTENSION_COMPILE_OPTION "-std=gnu++14")
  151. set(CMAKE_${lang}11_STANDARD_COMPILE_OPTION "-std=c++14")
  152. set(CMAKE_${lang}11_EXTENSION_COMPILE_OPTION "-std=gnu++14")
  153. # This clang++ is missing some features because of MSVC compatibility.
  154. unset(CMAKE_${lang}11_STANDARD__HAS_FULL_SUPPORT)
  155. unset(CMAKE_${lang}14_STANDARD__HAS_FULL_SUPPORT)
  156. unset(CMAKE_${lang}17_STANDARD__HAS_FULL_SUPPORT)
  157. unset(CMAKE_${lang}20_STANDARD__HAS_FULL_SUPPORT)
  158. endif()
  159. __compiler_check_default_language_standard(${lang} 2.1 98)
  160. elseif(CMAKE_${lang}_COMPILER_VERSION VERSION_GREATER_EQUAL 3.9
  161. AND CMAKE_${lang}_SIMULATE_VERSION VERSION_GREATER_EQUAL 19.0)
  162. # This version of clang-cl and the MSVC version it simulates have
  163. # support for -std: flags.
  164. set(CMAKE_${lang}98_STANDARD_COMPILE_OPTION "")
  165. set(CMAKE_${lang}98_EXTENSION_COMPILE_OPTION "")
  166. set(CMAKE_${lang}98_STANDARD__HAS_FULL_SUPPORT ON)
  167. set(CMAKE_${lang}11_STANDARD_COMPILE_OPTION "")
  168. set(CMAKE_${lang}11_EXTENSION_COMPILE_OPTION "")
  169. set(CMAKE_${lang}14_STANDARD_COMPILE_OPTION "-std:c++14")
  170. set(CMAKE_${lang}14_EXTENSION_COMPILE_OPTION "-std:c++14")
  171. if (CMAKE_${lang}_COMPILER_VERSION VERSION_GREATER_EQUAL 6.0)
  172. set(CMAKE_${lang}17_STANDARD_COMPILE_OPTION "-std:c++17")
  173. set(CMAKE_${lang}17_EXTENSION_COMPILE_OPTION "-std:c++17")
  174. set(CMAKE_${lang}20_STANDARD_COMPILE_OPTION "-std:c++latest")
  175. set(CMAKE_${lang}20_EXTENSION_COMPILE_OPTION "-std:c++latest")
  176. else()
  177. set(CMAKE_${lang}17_STANDARD_COMPILE_OPTION "-std:c++latest")
  178. set(CMAKE_${lang}17_EXTENSION_COMPILE_OPTION "-std:c++latest")
  179. endif()
  180. __compiler_check_default_language_standard(${lang} 3.9 14)
  181. else()
  182. # This version of clang-cl, or the MSVC version it simulates, does not have
  183. # language standards. Set these options as empty strings so the feature
  184. # test infrastructure can at least check to see if they are defined.
  185. set(CMAKE_${lang}98_STANDARD_COMPILE_OPTION "")
  186. set(CMAKE_${lang}98_EXTENSION_COMPILE_OPTION "")
  187. set(CMAKE_${lang}11_STANDARD_COMPILE_OPTION "")
  188. set(CMAKE_${lang}11_EXTENSION_COMPILE_OPTION "")
  189. set(CMAKE_${lang}14_STANDARD_COMPILE_OPTION "")
  190. set(CMAKE_${lang}14_EXTENSION_COMPILE_OPTION "")
  191. set(CMAKE_${lang}17_STANDARD_COMPILE_OPTION "")
  192. set(CMAKE_${lang}17_EXTENSION_COMPILE_OPTION "")
  193. set(CMAKE_${lang}20_STANDARD_COMPILE_OPTION "")
  194. set(CMAKE_${lang}20_EXTENSION_COMPILE_OPTION "")
  195. # There is no meaningful default for this
  196. set(CMAKE_${lang}_STANDARD_DEFAULT "")
  197. # There are no compiler modes so we only need to test features once.
  198. # Override the default macro for this special case. Pretend that
  199. # all language standards are available so that at least compilation
  200. # can be attempted.
  201. macro(cmake_record_${lang}_compile_features)
  202. list(APPEND CMAKE_${lang}_COMPILE_FEATURES
  203. cxx_std_98
  204. cxx_std_11
  205. cxx_std_14
  206. cxx_std_17
  207. cxx_std_20
  208. )
  209. _record_compiler_features(${lang} "" CMAKE_${lang}_COMPILE_FEATURES)
  210. endmacro()
  211. endif()
  212. endmacro()