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: 5CheckLinkerFlag 6--------------- 7 8.. versionadded:: 3.18 9 10Check whether the compiler supports a given link flag. 11 12.. command:: check_linker_flag 13 14 .. code-block:: cmake 15 16 check_linker_flag(<lang> <flag> <var>) 17 18Check that the link ``<flag>`` is accepted by the ``<lang>`` compiler without 19a diagnostic. Stores the result in an internal cache entry named ``<var>``. 20 21This command temporarily sets the ``CMAKE_REQUIRED_LINK_OPTIONS`` variable 22and calls the :command:`check_source_compiles` command from the 23:module:`CheckSourceCompiles` module. See that module's documentation 24for a listing of variables that can otherwise modify the build. 25 26The underlying implementation relies on the :prop_tgt:`LINK_OPTIONS` property 27to check the specified flag. The ``LINKER:`` prefix, as described in the 28:command:`target_link_options` command, can be used as well. 29 30A positive result from this check indicates only that the compiler did not 31issue a diagnostic message when given the link flag. Whether the flag has any 32effect or even a specific one is beyond the scope of this module. 33 34.. note:: 35 Since the :command:`try_compile` command forwards flags from variables 36 like :variable:`CMAKE_<LANG>_FLAGS`, unknown flags in such variables may 37 cause a false negative for this check. 38#]=======================================================================] 39 40include_guard(GLOBAL) 41 42include(CMakeCheckCompilerFlagCommonPatterns) 43 44cmake_policy(PUSH) 45cmake_policy(SET CMP0054 NEW) # if() quoted variables not dereferenced 46cmake_policy(SET CMP0057 NEW) # if() supports IN_LIST 47 48function(CHECK_LINKER_FLAG _lang _flag _var) 49 get_property (_supported_languages GLOBAL PROPERTY ENABLED_LANGUAGES) 50 if (NOT _lang IN_LIST _supported_languages) 51 message (SEND_ERROR "check_linker_flag: ${_lang}: unknown language.") 52 return() 53 endif() 54 55 include (CheckSourceCompiles) 56 57 set(CMAKE_REQUIRED_LINK_OPTIONS "${_flag}") 58 59 # Normalize locale during test compilation. 60 set(_locale_vars LC_ALL LC_MESSAGES LANG) 61 foreach(v IN LISTS _locale_vars) 62 set(_locale_vars_saved_${v} "$ENV{${v}}") 63 set(ENV{${v}} C) 64 endforeach() 65 66 if (_lang MATCHES "^(C|CXX)$") 67 set (_source "int main() { return 0; }") 68 elseif (_lang STREQUAL "Fortran") 69 set (_source " program test\n stop\n end program") 70 elseif (_lang MATCHES "CUDA") 71 set (_source "__host__ int main() { return 0; }") 72 elseif (_lang MATCHES "HIP") 73 set (_source "__host__ int main() { return 0; }") 74 elseif (_lang MATCHES "^(OBJC|OBJCXX)$") 75 set (_source "#ifndef __OBJC__\n# error \"Not an Objective-C++ compiler\"\n#endif\nint main(void) { return 0; }") 76 else() 77 message (SEND_ERROR "check_linker_flag: ${_lang}: unsupported language.") 78 return() 79 endif() 80 check_compiler_flag_common_patterns(_common_patterns) 81 82 check_source_compiles(${_lang} "${_source}" ${_var} ${_common_patterns}) 83 84 foreach(v IN LISTS _locale_vars) 85 set(ENV{${v}} ${_locale_vars_saved_${v}}) 86 endforeach() 87 set(${_var} "${${_var}}" PARENT_SCOPE) 88endfunction() 89 90cmake_policy(POP) 91