WriteCompilerDetectionHeader.cmake 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. WriteCompilerDetectionHeader
  5. ----------------------------
  6. This module provides the function ``write_compiler_detection_header()``.
  7. This function can be used to generate a file suitable for preprocessor
  8. inclusion which contains macros to be used in source code::
  9. write_compiler_detection_header(
  10. FILE <file>
  11. PREFIX <prefix>
  12. [OUTPUT_FILES_VAR <output_files_var> OUTPUT_DIR <output_dir>]
  13. COMPILERS <compiler> [...]
  14. FEATURES <feature> [...]
  15. [BARE_FEATURES <feature> [...]]
  16. [VERSION <version>]
  17. [PROLOG <prolog>]
  18. [EPILOG <epilog>]
  19. [ALLOW_UNKNOWN_COMPILERS]
  20. [ALLOW_UNKNOWN_COMPILER_VERSIONS]
  21. )
  22. This generates the file ``<file>`` with macros which all have the prefix
  23. ``<prefix>``.
  24. By default, all content is written directly to the ``<file>``. The
  25. ``OUTPUT_FILES_VAR`` may be specified to cause the compiler-specific
  26. content to be written to separate files. The separate files are then
  27. available in the ``<output_files_var>`` and may be consumed by the caller
  28. for installation for example. The ``OUTPUT_DIR`` specifies a relative
  29. path from the main ``<file>`` to the compiler-specific files. For example:
  30. .. code-block:: cmake
  31. write_compiler_detection_header(
  32. FILE climbingstats_compiler_detection.h
  33. PREFIX ClimbingStats
  34. OUTPUT_FILES_VAR support_files
  35. OUTPUT_DIR compilers
  36. COMPILERS GNU Clang MSVC Intel
  37. FEATURES cxx_variadic_templates
  38. )
  39. install(FILES
  40. ${CMAKE_CURRENT_BINARY_DIR}/climbingstats_compiler_detection.h
  41. DESTINATION include
  42. )
  43. install(FILES
  44. ${support_files}
  45. DESTINATION include/compilers
  46. )
  47. ``VERSION`` may be used to specify the API version to be generated.
  48. Future versions of CMake may introduce alternative APIs. A given
  49. API is selected by any ``<version>`` value greater than or equal
  50. to the version of CMake that introduced the given API and less
  51. than the version of CMake that introduced its succeeding API.
  52. The value of the :variable:`CMAKE_MINIMUM_REQUIRED_VERSION`
  53. variable is used if no explicit version is specified.
  54. (As of CMake version |release| there is only one API version.)
  55. ``PROLOG`` may be specified as text content to write at the start of the
  56. header. ``EPILOG`` may be specified as text content to write at the end
  57. of the header
  58. At least one ``<compiler>`` and one ``<feature>`` must be listed. Compilers
  59. which are known to CMake, but not specified are detected and a preprocessor
  60. ``#error`` is generated for them. A preprocessor macro matching
  61. ``<PREFIX>_COMPILER_IS_<compiler>`` is generated for each compiler
  62. known to CMake to contain the value ``0`` or ``1``.
  63. Possible compiler identifiers are documented with the
  64. :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  65. Available features in this version of CMake are listed in the
  66. :prop_gbl:`CMAKE_C_KNOWN_FEATURES` and
  67. :prop_gbl:`CMAKE_CXX_KNOWN_FEATURES` global properties.
  68. The ``{c,cxx}_std_*`` meta-features are ignored if requested.
  69. See the :manual:`cmake-compile-features(7)` manual for information on
  70. compile features.
  71. ``BARE_FEATURES`` will define the compatibility macros with the name used in
  72. newer versions of the language standard, so the code can use the new feature
  73. name unconditionally.
  74. ``ALLOW_UNKNOWN_COMPILERS`` and ``ALLOW_UNKNOWN_COMPILER_VERSIONS`` cause
  75. the module to generate conditions that treat unknown compilers as simply
  76. lacking all features. Without these options the default behavior is to
  77. generate a ``#error`` for unknown compilers and versions.
  78. Feature Test Macros
  79. ===================
  80. For each compiler, a preprocessor macro is generated matching
  81. ``<PREFIX>_COMPILER_IS_<compiler>`` which has the content either ``0``
  82. or ``1``, depending on the compiler in use. Preprocessor macros for
  83. compiler version components are generated matching
  84. ``<PREFIX>_COMPILER_VERSION_MAJOR`` ``<PREFIX>_COMPILER_VERSION_MINOR``
  85. and ``<PREFIX>_COMPILER_VERSION_PATCH`` containing decimal values
  86. for the corresponding compiler version components, if defined.
  87. A preprocessor test is generated based on the compiler version
  88. denoting whether each feature is enabled. A preprocessor macro
  89. matching ``<PREFIX>_COMPILER_<FEATURE>``, where ``<FEATURE>`` is the
  90. upper-case ``<feature>`` name, is generated to contain the value
  91. ``0`` or ``1`` depending on whether the compiler in use supports the
  92. feature:
  93. .. code-block:: cmake
  94. write_compiler_detection_header(
  95. FILE climbingstats_compiler_detection.h
  96. PREFIX ClimbingStats
  97. COMPILERS GNU Clang AppleClang MSVC Intel
  98. FEATURES cxx_variadic_templates
  99. )
  100. .. code-block:: c++
  101. #if ClimbingStats_COMPILER_CXX_VARIADIC_TEMPLATES
  102. template<typename... T>
  103. void someInterface(T t...) { /* ... */ }
  104. #else
  105. // Compatibility versions
  106. template<typename T1>
  107. void someInterface(T1 t1) { /* ... */ }
  108. template<typename T1, typename T2>
  109. void someInterface(T1 t1, T2 t2) { /* ... */ }
  110. template<typename T1, typename T2, typename T3>
  111. void someInterface(T1 t1, T2 t2, T3 t3) { /* ... */ }
  112. #endif
  113. Symbol Macros
  114. =============
  115. Some additional symbol-defines are created for particular features for
  116. use as symbols which may be conditionally defined empty:
  117. .. code-block:: c++
  118. class MyClass ClimbingStats_FINAL
  119. {
  120. ClimbingStats_CONSTEXPR int someInterface() { return 42; }
  121. };
  122. The ``ClimbingStats_FINAL`` macro will expand to ``final`` if the
  123. compiler (and its flags) support the ``cxx_final`` feature, and the
  124. ``ClimbingStats_CONSTEXPR`` macro will expand to ``constexpr``
  125. if ``cxx_constexpr`` is supported.
  126. If ``BARE_FEATURES cxx_final`` was given as argument the ``final`` keyword
  127. will be defined for old compilers, too.
  128. The following features generate corresponding symbol defines and if they
  129. are available as ``BARE_FEATURES``:
  130. ========================== =================================== ================= ======
  131. Feature Define Symbol bare
  132. ========================== =================================== ================= ======
  133. ``c_restrict`` ``<PREFIX>_RESTRICT`` ``restrict`` yes
  134. ``cxx_constexpr`` ``<PREFIX>_CONSTEXPR`` ``constexpr`` yes
  135. ``cxx_deleted_functions`` ``<PREFIX>_DELETED_FUNCTION`` ``= delete``
  136. ``cxx_extern_templates`` ``<PREFIX>_EXTERN_TEMPLATE`` ``extern``
  137. ``cxx_final`` ``<PREFIX>_FINAL`` ``final`` yes
  138. ``cxx_noexcept`` ``<PREFIX>_NOEXCEPT`` ``noexcept`` yes
  139. ``cxx_noexcept`` ``<PREFIX>_NOEXCEPT_EXPR(X)`` ``noexcept(X)``
  140. ``cxx_override`` ``<PREFIX>_OVERRIDE`` ``override`` yes
  141. ========================== =================================== ================= ======
  142. Compatibility Implementation Macros
  143. ===================================
  144. Some features are suitable for wrapping in a macro with a backward
  145. compatibility implementation if the compiler does not support the feature.
  146. When the ``cxx_static_assert`` feature is not provided by the compiler,
  147. a compatibility implementation is available via the
  148. ``<PREFIX>_STATIC_ASSERT(COND)`` and
  149. ``<PREFIX>_STATIC_ASSERT_MSG(COND, MSG)`` function-like macros. The macros
  150. expand to ``static_assert`` where that compiler feature is available, and
  151. to a compatibility implementation otherwise. In the first form, the
  152. condition is stringified in the message field of ``static_assert``. In
  153. the second form, the message ``MSG`` is passed to the message field of
  154. ``static_assert``, or ignored if using the backward compatibility
  155. implementation.
  156. The ``cxx_attribute_deprecated`` feature provides a macro definition
  157. ``<PREFIX>_DEPRECATED``, which expands to either the standard
  158. ``[[deprecated]]`` attribute or a compiler-specific decorator such
  159. as ``__attribute__((__deprecated__))`` used by GNU compilers.
  160. The ``cxx_alignas`` feature provides a macro definition
  161. ``<PREFIX>_ALIGNAS`` which expands to either the standard ``alignas``
  162. decorator or a compiler-specific decorator such as
  163. ``__attribute__ ((__aligned__))`` used by GNU compilers.
  164. The ``cxx_alignof`` feature provides a macro definition
  165. ``<PREFIX>_ALIGNOF`` which expands to either the standard ``alignof``
  166. decorator or a compiler-specific decorator such as ``__alignof__``
  167. used by GNU compilers.
  168. ============================= ================================ ===================== ======
  169. Feature Define Symbol bare
  170. ============================= ================================ ===================== ======
  171. ``cxx_alignas`` ``<PREFIX>_ALIGNAS`` ``alignas``
  172. ``cxx_alignof`` ``<PREFIX>_ALIGNOF`` ``alignof``
  173. ``cxx_nullptr`` ``<PREFIX>_NULLPTR`` ``nullptr`` yes
  174. ``cxx_static_assert`` ``<PREFIX>_STATIC_ASSERT`` ``static_assert``
  175. ``cxx_static_assert`` ``<PREFIX>_STATIC_ASSERT_MSG`` ``static_assert``
  176. ``cxx_attribute_deprecated`` ``<PREFIX>_DEPRECATED`` ``[[deprecated]]``
  177. ``cxx_attribute_deprecated`` ``<PREFIX>_DEPRECATED_MSG`` ``[[deprecated]]``
  178. ``cxx_thread_local`` ``<PREFIX>_THREAD_LOCAL`` ``thread_local``
  179. ============================= ================================ ===================== ======
  180. A use-case which arises with such deprecation macros is the deprecation
  181. of an entire library. In that case, all public API in the library may
  182. be decorated with the ``<PREFIX>_DEPRECATED`` macro. This results in
  183. very noisy build output when building the library itself, so the macro
  184. may be may be defined to empty in that case when building the deprecated
  185. library:
  186. .. code-block:: cmake
  187. add_library(compat_support ${srcs})
  188. target_compile_definitions(compat_support
  189. PRIVATE
  190. CompatSupport_DEPRECATED=
  191. )
  192. #]=======================================================================]
  193. include(${CMAKE_CURRENT_LIST_DIR}/CMakeCompilerIdDetection.cmake)
  194. function(_load_compiler_variables CompilerId lang)
  195. include("${CMAKE_ROOT}/Modules/Compiler/${CompilerId}-${lang}-FeatureTests.cmake" OPTIONAL)
  196. set(_cmake_oldestSupported_${CompilerId} ${_cmake_oldestSupported} PARENT_SCOPE)
  197. foreach(feature ${ARGN})
  198. set(_cmake_feature_test_${CompilerId}_${feature} ${_cmake_feature_test_${feature}} PARENT_SCOPE)
  199. endforeach()
  200. include("${CMAKE_ROOT}/Modules/Compiler/${CompilerId}-${lang}-DetermineCompiler.cmake" OPTIONAL
  201. RESULT_VARIABLE determinedCompiler)
  202. if (NOT determinedCompiler)
  203. include("${CMAKE_ROOT}/Modules/Compiler/${CompilerId}-DetermineCompiler.cmake" OPTIONAL)
  204. endif()
  205. set(_compiler_id_version_compute_${CompilerId} ${_compiler_id_version_compute} PARENT_SCOPE)
  206. endfunction()
  207. macro(_simpledefine FEATURE_NAME FEATURE_TESTNAME FEATURE_STRING FEATURE_DEFAULT_STRING)
  208. if (feature STREQUAL "${FEATURE_NAME}")
  209. set(def_value "${prefix_arg}_${FEATURE_TESTNAME}")
  210. string(APPEND file_content "
  211. # if defined(${def_name}) && ${def_name}
  212. # define ${def_value} ${FEATURE_STRING}
  213. # else
  214. # define ${def_value} ${FEATURE_DEFAULT_STRING}
  215. # endif
  216. \n")
  217. endif()
  218. endmacro()
  219. macro(_simplebaredefine FEATURE_NAME FEATURE_STRING FEATURE_DEFAULT_STRING)
  220. if (feature STREQUAL "${FEATURE_NAME}")
  221. string(APPEND file_content "
  222. # if !(defined(${def_name}) && ${def_name})
  223. # define ${FEATURE_STRING} ${FEATURE_DEFAULT_STRING}
  224. # endif
  225. \n")
  226. endif()
  227. endmacro()
  228. function(_check_feature_lists C_FEATURE_VAR CXX_FEATURE_VAR)
  229. foreach(feature ${ARGN})
  230. if (feature MATCHES "^c_std_")
  231. # ignored
  232. elseif (feature MATCHES "^cxx_std_")
  233. # ignored
  234. elseif (feature MATCHES "^cxx_")
  235. list(APPEND _langs CXX)
  236. list(APPEND ${CXX_FEATURE_VAR} ${feature})
  237. elseif (feature MATCHES "^c_")
  238. list(APPEND _langs C)
  239. list(APPEND ${C_FEATURE_VAR} ${feature})
  240. else()
  241. message(FATAL_ERROR "Unsupported feature ${feature}.")
  242. endif()
  243. endforeach()
  244. set(${C_FEATURE_VAR} ${${C_FEATURE_VAR}} PARENT_SCOPE)
  245. set(${CXX_FEATURE_VAR} ${${CXX_FEATURE_VAR}} PARENT_SCOPE)
  246. set(_langs ${_langs} PARENT_SCOPE)
  247. endfunction()
  248. function(write_compiler_detection_header
  249. file_keyword file_arg
  250. prefix_keyword prefix_arg
  251. )
  252. if (NOT "x${file_keyword}" STREQUAL "xFILE")
  253. message(FATAL_ERROR "write_compiler_detection_header: FILE parameter missing.")
  254. endif()
  255. if (NOT "x${prefix_keyword}" STREQUAL "xPREFIX")
  256. message(FATAL_ERROR "write_compiler_detection_header: PREFIX parameter missing.")
  257. endif()
  258. set(options ALLOW_UNKNOWN_COMPILERS ALLOW_UNKNOWN_COMPILER_VERSIONS)
  259. set(oneValueArgs VERSION EPILOG PROLOG OUTPUT_FILES_VAR OUTPUT_DIR)
  260. set(multiValueArgs COMPILERS FEATURES BARE_FEATURES)
  261. cmake_parse_arguments(_WCD "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  262. if (NOT _WCD_COMPILERS)
  263. message(FATAL_ERROR "Invalid arguments. write_compiler_detection_header requires at least one compiler.")
  264. endif()
  265. if (NOT _WCD_FEATURES AND NOT _WCD_BARE_FEATURES)
  266. message(FATAL_ERROR "Invalid arguments. write_compiler_detection_header requires at least one feature.")
  267. endif()
  268. if(_WCD_UNPARSED_ARGUMENTS)
  269. message(FATAL_ERROR "Unparsed arguments: ${_WCD_UNPARSED_ARGUMENTS}")
  270. endif()
  271. if (prefix_arg STREQUAL "")
  272. message(FATAL_ERROR "A prefix must be specified")
  273. endif()
  274. string(MAKE_C_IDENTIFIER ${prefix_arg} cleaned_prefix)
  275. if (NOT prefix_arg STREQUAL cleaned_prefix)
  276. message(FATAL_ERROR "The prefix must be a valid C identifier.")
  277. endif()
  278. if(NOT _WCD_VERSION)
  279. set(_WCD_VERSION ${CMAKE_MINIMUM_REQUIRED_VERSION})
  280. endif()
  281. set(_min_version 3.1.0) # Version which introduced this function
  282. if (_WCD_VERSION VERSION_LESS _min_version)
  283. set(err "VERSION compatibility for write_compiler_detection_header is set to ${_WCD_VERSION}, which is too low.")
  284. string(APPEND err " It must be set to at least ${_min_version}. ")
  285. string(APPEND err " Either set the VERSION parameter to the write_compiler_detection_header function, or update")
  286. string(APPEND err " your minimum required CMake version with the cmake_minimum_required command.")
  287. message(FATAL_ERROR "${err}")
  288. endif()
  289. if(_WCD_OUTPUT_FILES_VAR)
  290. if(NOT _WCD_OUTPUT_DIR)
  291. message(FATAL_ERROR "If OUTPUT_FILES_VAR is specified, then OUTPUT_DIR must also be specified.")
  292. endif()
  293. endif()
  294. if(_WCD_OUTPUT_DIR)
  295. if(NOT _WCD_OUTPUT_FILES_VAR)
  296. message(FATAL_ERROR "If OUTPUT_DIR is specified, then OUTPUT_FILES_VAR must also be specified.")
  297. endif()
  298. get_filename_component(main_file_dir ${file_arg} DIRECTORY)
  299. if (NOT IS_ABSOLUTE ${main_file_dir})
  300. set(main_file_dir "${CMAKE_CURRENT_BINARY_DIR}/${main_file_dir}")
  301. endif()
  302. if (NOT IS_ABSOLUTE ${_WCD_OUTPUT_DIR})
  303. set(_WCD_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/${_WCD_OUTPUT_DIR}")
  304. endif()
  305. get_filename_component(out_file_dir ${_WCD_OUTPUT_DIR} ABSOLUTE)
  306. string(FIND ${out_file_dir} ${main_file_dir} idx)
  307. if (NOT idx EQUAL 0)
  308. message(FATAL_ERROR "The compiler-specific output directory must be within the same directory as the main file.")
  309. endif()
  310. if (main_file_dir STREQUAL out_file_dir)
  311. unset(_WCD_OUTPUT_DIR)
  312. else()
  313. string(REPLACE "${main_file_dir}/" "" _WCD_OUTPUT_DIR "${out_file_dir}/")
  314. endif()
  315. endif()
  316. set(compilers
  317. GNU
  318. Clang
  319. AppleClang
  320. MSVC
  321. SunPro
  322. Intel
  323. )
  324. set(_hex_compilers ADSP Borland Embarcadero SunPro)
  325. foreach(_comp ${_WCD_COMPILERS})
  326. list(FIND compilers ${_comp} idx)
  327. if (idx EQUAL -1)
  328. message(FATAL_ERROR "Unsupported compiler ${_comp}.")
  329. endif()
  330. if (NOT _need_hex_conversion)
  331. list(FIND _hex_compilers ${_comp} idx)
  332. if (NOT idx EQUAL -1)
  333. set(_need_hex_conversion TRUE)
  334. endif()
  335. endif()
  336. endforeach()
  337. set(file_content "
  338. // This is a generated file. Do not edit!
  339. #ifndef ${prefix_arg}_COMPILER_DETECTION_H
  340. #define ${prefix_arg}_COMPILER_DETECTION_H
  341. ")
  342. if (_WCD_PROLOG)
  343. string(APPEND file_content "\n${_WCD_PROLOG}\n")
  344. endif()
  345. if (_need_hex_conversion)
  346. string(APPEND file_content "
  347. #define ${prefix_arg}_DEC(X) (X)
  348. #define ${prefix_arg}_HEX(X) ( \\
  349. ((X)>>28 & 0xF) * 10000000 + \\
  350. ((X)>>24 & 0xF) * 1000000 + \\
  351. ((X)>>20 & 0xF) * 100000 + \\
  352. ((X)>>16 & 0xF) * 10000 + \\
  353. ((X)>>12 & 0xF) * 1000 + \\
  354. ((X)>>8 & 0xF) * 100 + \\
  355. ((X)>>4 & 0xF) * 10 + \\
  356. ((X) & 0xF) \\
  357. )\n")
  358. endif()
  359. _check_feature_lists(C_features CXX_features ${_WCD_FEATURES})
  360. _check_feature_lists(C_bare_features CXX_bare_features ${_WCD_BARE_FEATURES})
  361. list(REMOVE_DUPLICATES _langs)
  362. if(_WCD_OUTPUT_FILES_VAR)
  363. get_filename_component(main_file_name ${file_arg} NAME)
  364. set(compiler_file_content_
  365. "#ifndef ${prefix_arg}_COMPILER_DETECTION_H
  366. # error This file may only be included from ${main_file_name}
  367. #endif\n")
  368. endif()
  369. foreach(_lang ${_langs})
  370. set(target_compilers)
  371. foreach(compiler ${_WCD_COMPILERS})
  372. _load_compiler_variables(${compiler} ${_lang} ${${_lang}_features})
  373. if(_cmake_oldestSupported_${compiler})
  374. list(APPEND target_compilers ${compiler})
  375. endif()
  376. endforeach()
  377. get_property(known_features GLOBAL PROPERTY CMAKE_${_lang}_KNOWN_FEATURES)
  378. foreach(feature ${${_lang}_features})
  379. list(FIND known_features ${feature} idx)
  380. if (idx EQUAL -1)
  381. message(FATAL_ERROR "Unsupported feature ${feature}.")
  382. endif()
  383. endforeach()
  384. if(_lang STREQUAL CXX)
  385. string(APPEND file_content "\n#ifdef __cplusplus\n")
  386. else()
  387. string(APPEND file_content "\n#ifndef __cplusplus\n")
  388. endif()
  389. compiler_id_detection(ID_CONTENT ${_lang} PREFIX ${prefix_arg}_
  390. ID_DEFINE
  391. )
  392. string(APPEND file_content "${ID_CONTENT}\n")
  393. set(pp_if "if")
  394. foreach(compiler ${target_compilers})
  395. string(APPEND file_content "\n# ${pp_if} ${prefix_arg}_COMPILER_IS_${compiler}\n")
  396. if(_WCD_OUTPUT_FILES_VAR)
  397. set(compile_file_name "${_WCD_OUTPUT_DIR}${prefix_arg}_COMPILER_INFO_${compiler}_${_lang}.h")
  398. string(APPEND file_content "\n# include \"${compile_file_name}\"\n")
  399. endif()
  400. if(_WCD_OUTPUT_FILES_VAR)
  401. set(compiler_file_content compiler_file_content_${compiler}_${_lang})
  402. else()
  403. set(compiler_file_content file_content)
  404. endif()
  405. if(NOT _WCD_ALLOW_UNKNOWN_COMPILER_VERSIONS)
  406. string(APPEND ${compiler_file_content} "
  407. # if !(${_cmake_oldestSupported_${compiler}})
  408. # error Unsupported compiler version
  409. # endif\n")
  410. endif()
  411. set(PREFIX ${prefix_arg}_)
  412. if (_need_hex_conversion)
  413. set(MACRO_DEC ${prefix_arg}_DEC)
  414. set(MACRO_HEX ${prefix_arg}_HEX)
  415. else()
  416. set(MACRO_DEC)
  417. set(MACRO_HEX)
  418. endif()
  419. string(CONFIGURE "${_compiler_id_version_compute_${compiler}}" VERSION_BLOCK @ONLY)
  420. string(APPEND ${compiler_file_content} "${VERSION_BLOCK}\n")
  421. set(PREFIX)
  422. set(MACRO_DEC)
  423. set(MACRO_HEX)
  424. set(pp_if "elif")
  425. foreach(feature ${${_lang}_features})
  426. string(TOUPPER ${feature} feature_upper)
  427. set(feature_PP "COMPILER_${feature_upper}")
  428. set(_define_item "\n# define ${prefix_arg}_${feature_PP} 0\n")
  429. if (_cmake_feature_test_${compiler}_${feature} STREQUAL "1")
  430. set(_define_item "\n# define ${prefix_arg}_${feature_PP} 1\n")
  431. elseif (_cmake_feature_test_${compiler}_${feature})
  432. set(_define_item "\n# define ${prefix_arg}_${feature_PP} 0\n")
  433. set(_define_item "\n# if ${_cmake_feature_test_${compiler}_${feature}}\n# define ${prefix_arg}_${feature_PP} 1\n# else${_define_item}# endif\n")
  434. endif()
  435. string(APPEND ${compiler_file_content} "${_define_item}")
  436. endforeach()
  437. endforeach()
  438. if(pp_if STREQUAL "elif")
  439. if(_WCD_ALLOW_UNKNOWN_COMPILERS)
  440. string(APPEND file_content "
  441. # endif\n")
  442. else()
  443. string(APPEND file_content "
  444. # else
  445. # error Unsupported compiler
  446. # endif\n")
  447. endif()
  448. endif()
  449. foreach(feature ${${_lang}_features})
  450. string(TOUPPER ${feature} feature_upper)
  451. set(feature_PP "COMPILER_${feature_upper}")
  452. set(def_name ${prefix_arg}_${feature_PP})
  453. _simpledefine(c_restrict RESTRICT restrict "")
  454. _simpledefine(cxx_constexpr CONSTEXPR constexpr "")
  455. _simpledefine(cxx_final FINAL final "")
  456. _simpledefine(cxx_override OVERRIDE override "")
  457. if (feature STREQUAL cxx_static_assert)
  458. set(def_value "${prefix_arg}_STATIC_ASSERT(X)")
  459. set(def_value_msg "${prefix_arg}_STATIC_ASSERT_MSG(X, MSG)")
  460. set(def_fallback "enum { ${prefix_arg}_STATIC_ASSERT_JOIN(${prefix_arg}StaticAssertEnum, __LINE__) = sizeof(${prefix_arg}StaticAssert<X>) }")
  461. string(APPEND file_content "# if defined(${def_name}) && ${def_name}
  462. # define ${def_value} static_assert(X, #X)
  463. # define ${def_value_msg} static_assert(X, MSG)
  464. # else
  465. # define ${prefix_arg}_STATIC_ASSERT_JOIN(X, Y) ${prefix_arg}_STATIC_ASSERT_JOIN_IMPL(X, Y)
  466. # define ${prefix_arg}_STATIC_ASSERT_JOIN_IMPL(X, Y) X##Y
  467. template<bool> struct ${prefix_arg}StaticAssert;
  468. template<> struct ${prefix_arg}StaticAssert<true>{};
  469. # define ${def_value} ${def_fallback}
  470. # define ${def_value_msg} ${def_fallback}
  471. # endif
  472. \n")
  473. endif()
  474. if (feature STREQUAL cxx_alignas)
  475. set(def_value "${prefix_arg}_ALIGNAS(X)")
  476. string(APPEND file_content "
  477. # if defined(${def_name}) && ${def_name}
  478. # define ${def_value} alignas(X)
  479. # elif ${prefix_arg}_COMPILER_IS_GNU || ${prefix_arg}_COMPILER_IS_Clang || ${prefix_arg}_COMPILER_IS_AppleClang
  480. # define ${def_value} __attribute__ ((__aligned__(X)))
  481. # elif ${prefix_arg}_COMPILER_IS_MSVC
  482. # define ${def_value} __declspec(align(X))
  483. # else
  484. # define ${def_value}
  485. # endif
  486. \n")
  487. endif()
  488. if (feature STREQUAL cxx_alignof)
  489. set(def_value "${prefix_arg}_ALIGNOF(X)")
  490. string(APPEND file_content "
  491. # if defined(${def_name}) && ${def_name}
  492. # define ${def_value} alignof(X)
  493. # elif ${prefix_arg}_COMPILER_IS_GNU || ${prefix_arg}_COMPILER_IS_Clang || ${prefix_arg}_COMPILER_IS_AppleClang
  494. # define ${def_value} __alignof__(X)
  495. # elif ${prefix_arg}_COMPILER_IS_MSVC
  496. # define ${def_value} __alignof(X)
  497. # endif
  498. \n")
  499. endif()
  500. _simpledefine(cxx_deleted_functions DELETED_FUNCTION "= delete" "")
  501. _simpledefine(cxx_extern_templates EXTERN_TEMPLATE extern "")
  502. if (feature STREQUAL cxx_noexcept)
  503. set(def_value "${prefix_arg}_NOEXCEPT")
  504. string(APPEND file_content "
  505. # if defined(${def_name}) && ${def_name}
  506. # define ${def_value} noexcept
  507. # define ${def_value}_EXPR(X) noexcept(X)
  508. # else
  509. # define ${def_value}
  510. # define ${def_value}_EXPR(X)
  511. # endif
  512. \n")
  513. endif()
  514. if (feature STREQUAL cxx_nullptr)
  515. set(def_value "${prefix_arg}_NULLPTR")
  516. string(APPEND file_content "
  517. # if defined(${def_name}) && ${def_name}
  518. # define ${def_value} nullptr
  519. # elif ${prefix_arg}_COMPILER_IS_GNU
  520. # define ${def_value} __null
  521. # else
  522. # define ${def_value} 0
  523. # endif
  524. \n")
  525. endif()
  526. if (feature STREQUAL cxx_thread_local)
  527. set(def_value "${prefix_arg}_THREAD_LOCAL")
  528. string(APPEND file_content "
  529. # if defined(${def_name}) && ${def_name}
  530. # define ${def_value} thread_local
  531. # elif ${prefix_arg}_COMPILER_IS_GNU || ${prefix_arg}_COMPILER_IS_Clang || ${prefix_arg}_COMPILER_IS_AppleClang
  532. # define ${def_value} __thread
  533. # elif ${prefix_arg}_COMPILER_IS_MSVC
  534. # define ${def_value} __declspec(thread)
  535. # else
  536. // ${def_value} not defined for this configuration.
  537. # endif
  538. \n")
  539. endif()
  540. if (feature STREQUAL cxx_attribute_deprecated)
  541. set(def_name ${prefix_arg}_${feature_PP})
  542. set(def_value "${prefix_arg}_DEPRECATED")
  543. string(APPEND file_content "
  544. # ifndef ${def_value}
  545. # if defined(${def_name}) && ${def_name}
  546. # define ${def_value} [[deprecated]]
  547. # define ${def_value}_MSG(MSG) [[deprecated(MSG)]]
  548. # elif ${prefix_arg}_COMPILER_IS_GNU || ${prefix_arg}_COMPILER_IS_Clang
  549. # define ${def_value} __attribute__((__deprecated__))
  550. # define ${def_value}_MSG(MSG) __attribute__((__deprecated__(MSG)))
  551. # elif ${prefix_arg}_COMPILER_IS_MSVC
  552. # define ${def_value} __declspec(deprecated)
  553. # define ${def_value}_MSG(MSG) __declspec(deprecated(MSG))
  554. # else
  555. # define ${def_value}
  556. # define ${def_value}_MSG(MSG)
  557. # endif
  558. # endif
  559. \n")
  560. endif()
  561. endforeach()
  562. foreach(feature ${${_lang}_bare_features})
  563. string(TOUPPER ${feature} feature_upper)
  564. set(feature_PP "COMPILER_${feature_upper}")
  565. set(def_name ${prefix_arg}_${feature_PP})
  566. _simplebaredefine(c_restrict restrict "")
  567. _simplebaredefine(cxx_constexpr constexpr "")
  568. _simplebaredefine(cxx_final final "")
  569. _simplebaredefine(cxx_override override "")
  570. if (feature STREQUAL cxx_nullptr)
  571. set(def_value "nullptr")
  572. string(APPEND file_content "
  573. # if !(defined(${def_name}) && ${def_name})
  574. # if ${prefix_arg}_COMPILER_IS_GNU
  575. # define ${def_value} __null
  576. # else
  577. # define ${def_value} 0
  578. # endif
  579. # endif
  580. \n")
  581. endif()
  582. _simplebaredefine(cxx_noexcept noexcept "")
  583. endforeach()
  584. string(APPEND file_content "#endif\n")
  585. endforeach()
  586. if(_WCD_OUTPUT_FILES_VAR)
  587. foreach(compiler ${_WCD_COMPILERS})
  588. foreach(_lang ${_langs})
  589. if(compiler_file_content_${compiler}_${_lang})
  590. set(CMAKE_CONFIGURABLE_FILE_CONTENT "${compiler_file_content_}")
  591. string(APPEND CMAKE_CONFIGURABLE_FILE_CONTENT "${compiler_file_content_${compiler}_${_lang}}")
  592. set(compile_file_name "${_WCD_OUTPUT_DIR}${prefix_arg}_COMPILER_INFO_${compiler}_${_lang}.h")
  593. set(full_path "${main_file_dir}/${compile_file_name}")
  594. list(APPEND ${_WCD_OUTPUT_FILES_VAR} ${full_path})
  595. configure_file("${CMAKE_ROOT}/Modules/CMakeConfigurableFile.in"
  596. "${full_path}"
  597. @ONLY
  598. )
  599. endif()
  600. endforeach()
  601. endforeach()
  602. set(${_WCD_OUTPUT_FILES_VAR} ${${_WCD_OUTPUT_FILES_VAR}} PARENT_SCOPE)
  603. endif()
  604. if (_WCD_EPILOG)
  605. string(APPEND file_content "\n${_WCD_EPILOG}\n")
  606. endif()
  607. string(APPEND file_content "\n#endif")
  608. set(CMAKE_CONFIGURABLE_FILE_CONTENT ${file_content})
  609. configure_file("${CMAKE_ROOT}/Modules/CMakeConfigurableFile.in"
  610. "${file_arg}"
  611. @ONLY
  612. )
  613. endfunction()