CheckIPOSupported.cmake 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file LICENSE.rst or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. CheckIPOSupported
  5. -----------------
  6. .. versionadded:: 3.9
  7. This module provides a command to check whether the compiler supports
  8. interprocedural optimization (IPO/LTO).
  9. Load this module in a CMake project with:
  10. .. code-block:: cmake
  11. include(CheckIPOSupported)
  12. Interprocedural optimization is a compiler technique that performs
  13. optimizations across translation units (i.e., across source files), allowing
  14. the compiler to analyze and optimize the entire program as a whole rather
  15. than file-by-file. This can improve performance by enabling more aggressive
  16. inlining and dead code elimination. When these optimizations are applied at
  17. link time, the process is typically referred to as link-time optimization
  18. (LTO), which is a common form of IPO.
  19. In CMake, interprocedural optimization can be enabled on a per-target basis
  20. using the :prop_tgt:`INTERPROCEDURAL_OPTIMIZATION` target property, or
  21. for all targets in the current scope using the
  22. :variable:`CMAKE_INTERPROCEDURAL_OPTIMIZATION` variable.
  23. Use this module before enabling the interprocedural optimization on targets
  24. to ensure the compiler supports IPO/LTO.
  25. Commands
  26. ^^^^^^^^
  27. This module provides the following command:
  28. .. command:: check_ipo_supported
  29. Checks whether the compiler supports interprocedural optimization (IPO/LTO):
  30. .. code-block:: cmake
  31. check_ipo_supported(
  32. [RESULT <result-var>]
  33. [OUTPUT <output-var>]
  34. [LANGUAGES <lang>...]
  35. )
  36. Options are:
  37. ``RESULT <result-var>``
  38. Set ``<result-var>`` variable to ``YES`` if IPO is supported by the
  39. compiler and ``NO`` otherwise. If this option is not given then
  40. the command will issue a fatal error if IPO is not supported.
  41. ``OUTPUT <output-var>``
  42. Set ``<output-var>`` variable with details about any error.
  43. ``LANGUAGES <lang>...``
  44. Specify languages whose compilers to check.
  45. The following languages are supported:
  46. * ``C``
  47. * ``CXX``
  48. * ``CUDA``
  49. .. versionadded:: 3.25
  50. * ``Fortran``
  51. If this option is not given, the default languages are picked from
  52. the current :prop_gbl:`ENABLED_LANGUAGES` global property.
  53. .. note::
  54. To use ``check_ipo_supported()``, policy :policy:`CMP0069` must be set to
  55. ``NEW``; otherwise, a fatal error will occur.
  56. .. versionadded:: 3.13
  57. Support for :ref:`Visual Studio Generators`.
  58. .. versionadded:: 3.24
  59. The check uses the caller's :variable:`CMAKE_<LANG>_FLAGS`
  60. and :variable:`CMAKE_<LANG>_FLAGS_<CONFIG>` values.
  61. See policy :policy:`CMP0138`.
  62. Examples
  63. ^^^^^^^^
  64. Checking whether the compiler supports IPO and emitting a fatal error if it is
  65. not supported:
  66. .. code-block:: cmake
  67. include(CheckIPOSupported)
  68. check_ipo_supported() # fatal error if IPO is not supported
  69. set_property(TARGET foo PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
  70. The following example demonstrates how to use this module to enable IPO for
  71. the target only when supported by the compiler and to issue a warning if it
  72. is not. Additionally, projects may want to provide a configuration option
  73. to control when IPO is enabled. For example:
  74. .. code-block:: cmake
  75. option(FOO_ENABLE_IPO "Enable IPO/LTO")
  76. if(FOO_ENABLE_IPO)
  77. include(CheckIPOSupported)
  78. check_ipo_supported(RESULT result OUTPUT output)
  79. if(result)
  80. set_property(TARGET foo PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
  81. else()
  82. message(WARNING "IPO is not supported: ${output}")
  83. endif()
  84. endif()
  85. #]=======================================================================]
  86. # X_RESULT - name of the final result variable
  87. # X_OUTPUT - name of the variable with information about error
  88. macro(_ipo_not_supported output)
  89. if(NOT X_RESULT)
  90. message(FATAL_ERROR "IPO is not supported (${output}).")
  91. endif()
  92. set("${X_RESULT}" NO PARENT_SCOPE)
  93. if(X_OUTPUT)
  94. set("${X_OUTPUT}" "${output}" PARENT_SCOPE)
  95. endif()
  96. endmacro()
  97. # Run IPO/LTO test
  98. macro(_ipo_run_language_check language)
  99. set(_C_ext "c")
  100. set(_CXX_ext "cpp")
  101. set(_Fortran_ext "f")
  102. string(COMPARE EQUAL "${language}" "CUDA" is_cuda)
  103. set(ext ${_${language}_ext})
  104. if(NOT "${ext}" STREQUAL "")
  105. set(copy_sources foo.${ext} main.${ext})
  106. elseif(is_cuda)
  107. if(_CMAKE_CUDA_IPO_SUPPORTED_BY_CMAKE)
  108. set("${X_RESULT}" YES PARENT_SCOPE)
  109. endif()
  110. return()
  111. else()
  112. message(FATAL_ERROR "Language not supported")
  113. endif()
  114. set(testdir "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/_CMakeLTOTest-${language}")
  115. file(REMOVE_RECURSE "${testdir}")
  116. file(MAKE_DIRECTORY "${testdir}")
  117. set(bindir "${testdir}/bin")
  118. set(srcdir "${testdir}/src")
  119. file(MAKE_DIRECTORY "${bindir}")
  120. file(MAKE_DIRECTORY "${srcdir}")
  121. set(TRY_COMPILE_PROJECT_NAME "lto-test")
  122. set(try_compile_src "${CMAKE_ROOT}/Modules/CheckIPOSupported")
  123. # Use:
  124. # * TRY_COMPILE_PROJECT_NAME
  125. # * CMAKE_VERSION
  126. configure_file(
  127. "${try_compile_src}/CMakeLists-${language}.txt.in"
  128. "${srcdir}/CMakeLists.txt"
  129. @ONLY
  130. )
  131. foreach(x ${copy_sources})
  132. configure_file(
  133. "${try_compile_src}/${x}"
  134. "${srcdir}/${x}"
  135. COPYONLY
  136. )
  137. endforeach()
  138. if(ipo_CMP0138 STREQUAL "NEW")
  139. set(CMAKE_TRY_COMPILE_CONFIGURATION Debug)
  140. set(_CMAKE_LANG_FLAGS
  141. "-DCMAKE_${language}_FLAGS:STRING=${CMAKE_${language}_FLAGS}"
  142. "-DCMAKE_${language}_FLAGS_DEBUG:STRING=${CMAKE_${language}_FLAGS_DEBUG}"
  143. )
  144. else()
  145. set(_CMAKE_LANG_FLAGS "")
  146. endif()
  147. try_compile(
  148. _IPO_LANGUAGE_CHECK_RESULT
  149. PROJECT "${TRY_COMPILE_PROJECT_NAME}"
  150. SOURCE_DIR "${srcdir}"
  151. BINARY_DIR "${bindir}"
  152. CMAKE_FLAGS
  153. "-DCMAKE_VERBOSE_MAKEFILE=ON"
  154. "-DCMAKE_INTERPROCEDURAL_OPTIMIZATION=ON"
  155. ${_CMAKE_LANG_FLAGS}
  156. OUTPUT_VARIABLE output
  157. )
  158. set(_IPO_LANGUAGE_CHECK_RESULT "${_IPO_LANGUAGE_CHECK_RESULT}")
  159. unset(_IPO_LANGUAGE_CHECK_RESULT CACHE)
  160. if(NOT _IPO_LANGUAGE_CHECK_RESULT)
  161. _ipo_not_supported("check failed to compile")
  162. if(X_OUTPUT)
  163. set("${X_OUTPUT}" "${output}" PARENT_SCOPE)
  164. endif()
  165. return()
  166. endif()
  167. endmacro()
  168. function(check_ipo_supported)
  169. cmake_policy(GET CMP0069 x)
  170. string(COMPARE EQUAL "${x}" "" not_set)
  171. if(not_set)
  172. message(FATAL_ERROR "Policy CMP0069 is not set")
  173. endif()
  174. string(COMPARE EQUAL "${x}" "OLD" is_old)
  175. if(is_old)
  176. message(FATAL_ERROR "Policy CMP0069 set to OLD")
  177. endif()
  178. # Save policy setting for condition in _ipo_run_language_check.
  179. cmake_policy(GET CMP0138 ipo_CMP0138
  180. PARENT_SCOPE # undocumented, do not use outside of CMake
  181. )
  182. set(optional)
  183. set(one RESULT OUTPUT)
  184. set(multiple LANGUAGES)
  185. # Introduce:
  186. # * X_RESULT
  187. # * X_OUTPUT
  188. # * X_LANGUAGES
  189. cmake_parse_arguments(X "${optional}" "${one}" "${multiple}" "${ARGV}")
  190. string(COMPARE NOTEQUAL "${X_UNPARSED_ARGUMENTS}" "" has_unparsed)
  191. if(has_unparsed)
  192. message(FATAL_ERROR "Unparsed arguments: ${X_UNPARSED_ARGUMENTS}")
  193. endif()
  194. string(COMPARE EQUAL "${X_LANGUAGES}" "" no_languages)
  195. if(no_languages)
  196. # User did not set any languages, use defaults
  197. get_property(enabled_languages GLOBAL PROPERTY ENABLED_LANGUAGES)
  198. string(COMPARE EQUAL "${enabled_languages}" "" no_languages)
  199. if(no_languages)
  200. _ipo_not_supported(
  201. "no languages found in ENABLED_LANGUAGES global property"
  202. )
  203. return()
  204. endif()
  205. set(languages "")
  206. list(FIND enabled_languages "CXX" result)
  207. if(NOT result EQUAL -1)
  208. list(APPEND languages "CXX")
  209. endif()
  210. list(FIND enabled_languages "C" result)
  211. if(NOT result EQUAL -1)
  212. list(APPEND languages "C")
  213. endif()
  214. list(FIND enabled_languages "CUDA" result)
  215. if(NOT result EQUAL -1)
  216. list(APPEND languages "CUDA")
  217. endif()
  218. list(FIND enabled_languages "Fortran" result)
  219. if(NOT result EQUAL -1)
  220. list(APPEND languages "Fortran")
  221. endif()
  222. string(COMPARE EQUAL "${languages}" "" no_languages)
  223. if(no_languages)
  224. _ipo_not_supported(
  225. "no C/CXX/CUDA/Fortran languages found in ENABLED_LANGUAGES global property"
  226. )
  227. return()
  228. endif()
  229. else()
  230. set(languages "${X_LANGUAGES}")
  231. set(unsupported_languages "${languages}")
  232. list(REMOVE_ITEM unsupported_languages "C" "CXX" "CUDA" "Fortran")
  233. string(COMPARE NOTEQUAL "${unsupported_languages}" "" has_unsupported)
  234. if(has_unsupported)
  235. _ipo_not_supported(
  236. "language(s) '${unsupported_languages}' not supported"
  237. )
  238. return()
  239. endif()
  240. endif()
  241. foreach(lang ${languages})
  242. if(NOT _CMAKE_${lang}_IPO_SUPPORTED_BY_CMAKE)
  243. _ipo_not_supported("CMake doesn't support IPO for current ${lang} compiler")
  244. return()
  245. endif()
  246. if(NOT _CMAKE_${lang}_IPO_MAY_BE_SUPPORTED_BY_COMPILER)
  247. _ipo_not_supported("${lang} compiler doesn't support IPO")
  248. return()
  249. endif()
  250. endforeach()
  251. foreach(x ${languages})
  252. _ipo_run_language_check(${x})
  253. endforeach()
  254. set("${X_RESULT}" YES PARENT_SCOPE)
  255. endfunction()