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 index)
7 if(${index} GREATER -1)
8 set(CMAKE_REQUIRED_FLAGS -static-libgcc)
9 check_c_compiler_flag(-static-libgcc ld-static-libgcc)
10 set(CMAKE_REQUIRED_FLAGS)
15 if(NOT CMAKE_STATIC_LIBRARY_SUFFIX STREQUAL .lib)
16 list(FIND ENABLED_LANGUAGES CXX index)
17 if(${index} GREATER -1)
18 set(CMAKE_REQUIRED_FLAGS -static-libstdc++)
19 check_cxx_compiler_flag(-static-libstdc++ ld-static-libstdcxx)
20 set(CMAKE_REQUIRED_FLAGS)
25 function(string_append var)
27 string(FIND "${${var}}" "${arg}" index)
29 string(STRIP "${${var}} ${arg}" ${var})
32 set(${var} "${${var}}" PARENT_SCOPE)
35 function(target_link_static_3_13)
36 foreach(target ${ARGN})
38 get_property(LINK_FLAGS TARGET ${target} PROPERTY LINK_FLAGS)
40 string_append(LINK_FLAGS -static-libgcc)
42 if(ld-static-libstdcxx)
43 string_append(LINK_FLAGS -static-libstdc++)
45 set_property(TARGET ${target} PROPERTY LINK_FLAGS "${LINK_FLAGS}")
50 function(target_link_static_3_15)
51 foreach(target ${ARGN})
53 set_property(TARGET ${target} PROPERTY
54 MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>"
60 function(target_link_static)
61 if(CMAKE_VERSION VERSION_LESS 3.13)
62 target_link_static_3_13(${ARGN})
65 if(NOT CMAKE_VERSION VERSION_LESS 3.15)
66 target_link_static_3_15(${ARGN})
69 foreach(target ${ARGN})
70 if(target MATCHES "^INTERFACE|PUBLIC|PRIVATE")
75 target_link_options(${target} ${scope} -static-libgcc)
77 if(ld-static-libstdcxx)
78 target_link_options(${target} ${scope} $<$<COMPILE_LANGUAGE:CXX>:-static-libstdc++>)