CheckIPOSupported.cmake 6.2 KB

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