Imported Upstream version 3.23.2
[platform/upstream/cmake.git] / Modules / Internal / CheckLinkerFlag.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 include_guard(GLOBAL)
5 include(Internal/CheckFlagCommonConfig)
6 include(Internal/CheckSourceCompiles)
7 include(CMakeCheckCompilerFlagCommonPatterns)
8
9 cmake_policy(PUSH)
10 cmake_policy(SET CMP0054 NEW) # if() quoted variables not dereferenced
11
12 function(CMAKE_CHECK_LINKER_FLAG _lang _flag _var)
13   # link step supports less languages than the compiler
14   # so do a first check about the requested language
15   if (_lang STREQUAL "ISPC")
16     message (SEND_ERROR "check_linker_flag: ${_lang}: unsupported language.")
17     return()
18   endif()
19
20   # Parse extra arguments
21   cmake_parse_arguments(PARSE_ARGV 3 CHECK_LINKER_FLAG "" "OUTPUT_VARIABLE" "")
22
23   cmake_check_flag_common_init("check_linker_flag" ${_lang} _lang_src _lang_fail_regex)
24
25   set(CMAKE_REQUIRED_LINK_OPTIONS "${_flag}")
26
27   check_compiler_flag_common_patterns(_common_patterns)
28
29   # Match linker warnings if the exact flag is ignored.
30   foreach(flag IN LISTS _flag)
31     string(REGEX REPLACE "([][+.*?()^$])" [[\\\1]] _flag_regex "${flag}")
32     list(APPEND _common_patterns
33       FAIL_REGEX "warning: .*${_flag_regex}.* ignored"
34       )
35   endforeach()
36
37   cmake_check_source_compiles(${_lang}
38     "${_lang_src}"
39     ${_var}
40     ${_lang_fail_regex}
41     ${_common_patterns}
42     OUTPUT_VARIABLE _output
43     )
44
45   if (CHECK_LINKER_FLAG_OUTPUT_VARIABLE)
46     set(${CHECK_LINKER_FLAG_OUTPUT_VARIABLE} "${_output}" PARENT_SCOPE)
47   endif()
48
49   cmake_check_flag_common_finish()
50 endfunction()
51
52 cmake_policy(POP)