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