aboutsummaryrefslogtreecommitdiff
path: root/cmake/module/TryAppendLinkerFlag.cmake
blob: 749120d4458dd6e605e6314b6b9bec9f39c840a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# Copyright (c) 2023-present The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or https://opensource.org/license/mit/.

include_guard(GLOBAL)
include(CheckCXXSourceCompiles)

#[=[
Usage example:

  try_append_linker_flag("-Wl,--major-subsystem-version,6" TARGET core_interface)


In configuration output, this function prints a string by the following pattern:

  -- Performing Test LINKER_SUPPORTS_[flag]
  -- Performing Test LINKER_SUPPORTS_[flag] - Success

]=]
function(try_append_linker_flag flag)
  cmake_parse_arguments(PARSE_ARGV 1
    TALF                              # prefix
    ""                                # options
    "TARGET;VAR;SOURCE;RESULT_VAR"    # one_value_keywords
    "IF_CHECK_PASSED;IF_CHECK_FAILED" # multi_value_keywords
  )

  string(MAKE_C_IDENTIFIER "${flag}" result)
  string(TOUPPER "${result}" result)
  string(PREPEND result LINKER_SUPPORTS_)

  set(source "int main() { return 0; }")
  if(DEFINED TALF_SOURCE AND NOT TALF_SOURCE STREQUAL source)
    set(source "${TALF_SOURCE}")
    string(SHA256 source_hash "${source}")
    string(SUBSTRING ${source_hash} 0 4 source_hash_head)
    string(APPEND result _${source_hash_head})
  endif()

  # This forces running a linker.
  set(CMAKE_TRY_COMPILE_TARGET_TYPE EXECUTABLE)
  set(CMAKE_REQUIRED_LINK_OPTIONS ${flag} ${working_linker_werror_flag})
  check_cxx_source_compiles("${source}" ${result})

  if(${result})
    if(DEFINED TALF_IF_CHECK_PASSED)
      if(DEFINED TALF_TARGET)
        target_link_options(${TALF_TARGET} INTERFACE ${TALF_IF_CHECK_PASSED})
      endif()
      if(DEFINED TALF_VAR)
        string(STRIP "${${TALF_VAR}} ${TALF_IF_CHECK_PASSED}" ${TALF_VAR})
      endif()
    else()
      if(DEFINED TALF_TARGET)
        target_link_options(${TALF_TARGET} INTERFACE ${flag})
      endif()
      if(DEFINED TALF_VAR)
        string(STRIP "${${TALF_VAR}} ${flag}" ${TALF_VAR})
      endif()
    endif()
  elseif(DEFINED TALF_IF_CHECK_FAILED)
    if(DEFINED TALF_TARGET)
      target_link_options(${TALF_TARGET} INTERFACE ${TACXXF_IF_CHECK_FAILED})
    endif()
    if(DEFINED TALF_VAR)
      string(STRIP "${${TALF_VAR}} ${TACXXF_IF_CHECK_FAILED}" ${TALF_VAR})
    endif()
  endif()

  if(DEFINED TALF_VAR)
    set(${TALF_VAR} "${${TALF_VAR}}" PARENT_SCOPE)
  endif()

  if(DEFINED TALF_RESULT_VAR)
    set(${TALF_RESULT_VAR} "${${result}}" PARENT_SCOPE)
  endif()
endfunction()

if(MSVC)
  try_append_linker_flag("/WX" VAR working_linker_werror_flag)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
  try_append_linker_flag("-Wl,-fatal_warnings" VAR working_linker_werror_flag)
else()
  try_append_linker_flag("-Wl,--fatal-warnings" VAR working_linker_werror_flag)
endif()