CheckIPOSupported.cmake 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. CheckIPOSupported
  5. -----------------
  6. .. versionadded:: 3.9
  7. Check whether the compiler supports an interprocedural optimization (IPO/LTO).
  8. Use this before enabling the :prop_tgt:`INTERPROCEDURAL_OPTIMIZATION` target
  9. property.
  10. .. command:: check_ipo_supported
  11. ::
  12. check_ipo_supported([RESULT <result>] [OUTPUT <output>]
  13. [LANGUAGES <lang>...])
  14. Options are:
  15. ``RESULT <result>``
  16. Set ``<result>`` variable to ``YES`` if IPO is supported by the
  17. compiler and ``NO`` otherwise. If this option is not given then
  18. the command will issue a fatal error if IPO is not supported.
  19. ``OUTPUT <output>``
  20. Set ``<output>`` variable with details about any error.
  21. ``LANGUAGES <lang>...``
  22. Specify languages whose compilers to check.
  23. Languages ``C``, ``CXX``, and ``Fortran`` are supported.
  24. It makes no sense to use this module when :policy:`CMP0069` is set to ``OLD`` so
  25. module will return error in this case. See policy :policy:`CMP0069` for details.
  26. .. versionadded:: 3.13
  27. Add support for Visual Studio generators.
  28. .. versionadded:: 3.24
  29. The check uses the caller's :variable:`CMAKE_<LANG>_FLAGS`
  30. and :variable:`CMAKE_<LANG>_FLAGS_<CONFIG>` values.
  31. See policy :policy:`CMP0138`.
  32. Examples
  33. ^^^^^^^^
  34. .. code-block:: cmake
  35. check_ipo_supported() # fatal error if IPO is not supported
  36. set_property(TARGET foo PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
  37. .. code-block:: cmake
  38. # Optional IPO. Do not use IPO if it's not supported by compiler.
  39. check_ipo_supported(RESULT result OUTPUT output)
  40. if(result)
  41. set_property(TARGET foo PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
  42. else()
  43. message(WARNING "IPO is not supported: ${output}")
  44. endif()
  45. #]=======================================================================]
  46. # X_RESULT - name of the final result variable
  47. # X_OUTPUT - name of the variable with information about error
  48. macro(_ipo_not_supported output)
  49. if(NOT X_RESULT)
  50. message(FATAL_ERROR "IPO is not supported (${output}).")
  51. endif()
  52. set("${X_RESULT}" NO PARENT_SCOPE)
  53. if(X_OUTPUT)
  54. set("${X_OUTPUT}" "${output}" PARENT_SCOPE)
  55. endif()
  56. endmacro()
  57. # Run IPO/LTO test
  58. macro(_ipo_run_language_check language)
  59. set(_C_ext "c")
  60. set(_CXX_ext "cpp")
  61. set(_Fortran_ext "f")
  62. string(COMPARE EQUAL "${language}" "CUDA" is_cuda)
  63. set(ext ${_${language}_ext})
  64. if(NOT "${ext}" STREQUAL "")
  65. set(copy_sources foo.${ext} main.${ext})
  66. elseif(is_cuda)
  67. if(_CMAKE_CUDA_IPO_SUPPORTED_BY_CMAKE)
  68. set("${X_RESULT}" YES PARENT_SCOPE)
  69. endif()
  70. return()
  71. else()
  72. message(FATAL_ERROR "Language not supported")
  73. endif()
  74. set(testdir "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/_CMakeLTOTest-${language}")
  75. file(REMOVE_RECURSE "${testdir}")
  76. file(MAKE_DIRECTORY "${testdir}")
  77. set(bindir "${testdir}/bin")
  78. set(srcdir "${testdir}/src")
  79. file(MAKE_DIRECTORY "${bindir}")
  80. file(MAKE_DIRECTORY "${srcdir}")
  81. set(TRY_COMPILE_PROJECT_NAME "lto-test")
  82. set(try_compile_src "${CMAKE_ROOT}/Modules/CheckIPOSupported")
  83. # Use:
  84. # * TRY_COMPILE_PROJECT_NAME
  85. # * CMAKE_VERSION
  86. configure_file(
  87. "${try_compile_src}/CMakeLists-${language}.txt.in"
  88. "${srcdir}/CMakeLists.txt"
  89. @ONLY
  90. )
  91. foreach(x ${copy_sources})
  92. configure_file(
  93. "${try_compile_src}/${x}"
  94. "${srcdir}/${x}"
  95. COPYONLY
  96. )
  97. endforeach()
  98. if(ipo_CMP0138 STREQUAL "NEW")
  99. set(CMAKE_TRY_COMPILE_CONFIGURATION Debug)
  100. set(_CMAKE_LANG_FLAGS
  101. "-DCMAKE_${language}_FLAGS:STRING=${CMAKE_${language}_FLAGS}"
  102. "-DCMAKE_${language}_FLAGS_DEBUG:STRING=${CMAKE_${language}_FLAGS_DEBUG}"
  103. )
  104. else()
  105. set(_CMAKE_LANG_FLAGS "")
  106. endif()
  107. try_compile(
  108. _IPO_LANGUAGE_CHECK_RESULT
  109. PROJECT "${TRY_COMPILE_PROJECT_NAME}"
  110. SOURCE_DIR "${srcdir}"
  111. BINARY_DIR "${bindir}"
  112. CMAKE_FLAGS
  113. "-DCMAKE_VERBOSE_MAKEFILE=ON"
  114. "-DCMAKE_INTERPROCEDURAL_OPTIMIZATION=ON"
  115. ${_CMAKE_LANG_FLAGS}
  116. OUTPUT_VARIABLE output
  117. )
  118. set(_IPO_LANGUAGE_CHECK_RESULT "${_IPO_LANGUAGE_CHECK_RESULT}")
  119. unset(_IPO_LANGUAGE_CHECK_RESULT CACHE)
  120. if(NOT _IPO_LANGUAGE_CHECK_RESULT)
  121. _ipo_not_supported("check failed to compile")
  122. if(X_OUTPUT)
  123. set("${X_OUTPUT}" "${output}" PARENT_SCOPE)
  124. endif()
  125. return()
  126. endif()
  127. endmacro()
  128. function(check_ipo_supported)
  129. cmake_policy(GET CMP0069 x)
  130. string(COMPARE EQUAL "${x}" "" not_set)
  131. if(not_set)
  132. message(FATAL_ERROR "Policy CMP0069 is not set")
  133. endif()
  134. string(COMPARE EQUAL "${x}" "OLD" is_old)
  135. if(is_old)
  136. message(FATAL_ERROR "Policy CMP0069 set to OLD")
  137. endif()
  138. # Save policy setting for condition in _ipo_run_language_check.
  139. cmake_policy(GET CMP0138 ipo_CMP0138
  140. PARENT_SCOPE # undocumented, do not use outside of CMake
  141. )
  142. set(optional)
  143. set(one RESULT OUTPUT)
  144. set(multiple LANGUAGES)
  145. # Introduce:
  146. # * X_RESULT
  147. # * X_OUTPUT
  148. # * X_LANGUAGES
  149. cmake_parse_arguments(X "${optional}" "${one}" "${multiple}" "${ARGV}")
  150. string(COMPARE NOTEQUAL "${X_UNPARSED_ARGUMENTS}" "" has_unparsed)
  151. if(has_unparsed)
  152. message(FATAL_ERROR "Unparsed arguments: ${X_UNPARSED_ARGUMENTS}")
  153. endif()
  154. string(COMPARE EQUAL "${X_LANGUAGES}" "" no_languages)
  155. if(no_languages)
  156. # User did not set any languages, use defaults
  157. get_property(enabled_languages GLOBAL PROPERTY ENABLED_LANGUAGES)
  158. string(COMPARE EQUAL "${enabled_languages}" "" no_languages)
  159. if(no_languages)
  160. _ipo_not_supported(
  161. "no languages found in ENABLED_LANGUAGES global property"
  162. )
  163. return()
  164. endif()
  165. set(languages "")
  166. list(FIND enabled_languages "CXX" result)
  167. if(NOT result EQUAL -1)
  168. list(APPEND languages "CXX")
  169. endif()
  170. list(FIND enabled_languages "C" result)
  171. if(NOT result EQUAL -1)
  172. list(APPEND languages "C")
  173. endif()
  174. list(FIND enabled_languages "CUDA" result)
  175. if(NOT result EQUAL -1)
  176. list(APPEND languages "CUDA")
  177. endif()
  178. list(FIND enabled_languages "Fortran" result)
  179. if(NOT result EQUAL -1)
  180. list(APPEND languages "Fortran")
  181. endif()
  182. string(COMPARE EQUAL "${languages}" "" no_languages)
  183. if(no_languages)
  184. _ipo_not_supported(
  185. "no C/CXX/CUDA/Fortran languages found in ENABLED_LANGUAGES global property"
  186. )
  187. return()
  188. endif()
  189. else()
  190. set(languages "${X_LANGUAGES}")
  191. set(unsupported_languages "${languages}")
  192. list(REMOVE_ITEM unsupported_languages "C" "CXX" "CUDA" "Fortran")
  193. string(COMPARE NOTEQUAL "${unsupported_languages}" "" has_unsupported)
  194. if(has_unsupported)
  195. _ipo_not_supported(
  196. "language(s) '${unsupported_languages}' not supported"
  197. )
  198. return()
  199. endif()
  200. endif()
  201. foreach(lang ${languages})
  202. if(NOT _CMAKE_${lang}_IPO_SUPPORTED_BY_CMAKE)
  203. _ipo_not_supported("CMake doesn't support IPO for current ${lang} compiler")
  204. return()
  205. endif()
  206. if(NOT _CMAKE_${lang}_IPO_MAY_BE_SUPPORTED_BY_COMPILER)
  207. _ipo_not_supported("${lang} compiler doesn't support IPO")
  208. return()
  209. endif()
  210. endforeach()
  211. foreach(x ${languages})
  212. _ipo_run_language_check(${x})
  213. endforeach()
  214. set("${X_RESULT}" YES PARENT_SCOPE)
  215. endfunction()