1 get_cmake_property(ENABLED_LANGUAGES ENABLED_LANGUAGES)
2 include(CheckCXXCompilerFlag)
3 include(CheckCCompilerFlag)
5 if(NOT CMAKE_STATIC_LIBRARY_SUFFIX STREQUAL .lib)
6 list(FIND ENABLED_LANGUAGES C found)
7 if(${found} GREATER -1)
8 set(CMAKE_REQUIRED_FLAGS -static-libgcc)
9 check_c_compiler_flag(-static-libgcc ld-static-libgcc)
10 set(CMAKE_REQUIRED_FLAGS)
14 if(NOT CMAKE_STATIC_LIBRARY_SUFFIX STREQUAL .lib)
15 list(FIND ENABLED_LANGUAGES CXX found)
16 if(${found} GREATER -1)
17 set(CMAKE_REQUIRED_FLAGS -static-libstdc++)
18 check_cxx_compiler_flag(-static-libstdc++ ld-static-libstdcxx)
19 set(CMAKE_REQUIRED_FLAGS)
23 function(target_link_static_3_13)
24 function(string_append var)
26 string(FIND "${${var}}" "${arg}" found)
28 string(STRIP "${${var}} ${arg}" ${var})
31 set(${var} "${${var}}" PARENT_SCOPE)
33 foreach(target ${ARGN})
35 get_property(LINK_FLAGS TARGET ${target} PROPERTY LINK_FLAGS)
37 string_append(LINK_FLAGS -static-libgcc)
39 if(ld-static-libstdcxx)
40 string_append(LINK_FLAGS -static-libstdc++)
42 set_property(TARGET ${target} PROPERTY LINK_FLAGS "${LINK_FLAGS}")
47 function(target_link_static_3_15)
48 foreach(target ${ARGN})
50 set_property(TARGET ${target} PROPERTY
51 MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>"
57 function(target_link_static)
58 if(CMAKE_VERSION VERSION_LESS 3.13)
59 target_link_static_3_13(${ARGN})
62 if(NOT CMAKE_VERSION VERSION_LESS 3.15)
63 target_link_static_3_15(${ARGN})
66 foreach(target ${ARGN})
67 if(target MATCHES "^INTERFACE|PUBLIC|PRIVATE")
72 target_link_options(${target} ${scope} -static-libgcc)
74 if(ld-static-libstdcxx)
75 target_link_options(${target} ${scope} $<$<COMPILE_LANGUAGE:CXX>:-static-libstdc++>)