release 0.1.15
[liba.git] / cmake / TargetStatic.cmake
blob58815506386181bd05bdee82be8e8172460951a5
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)
11   endif()
12 endif()
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)
20   endif()
21 endif()
23 function(target_link_static_3_13)
24   function(string_append var)
25     foreach(arg ${ARGN})
26       string(FIND "${${var}}" "${arg}" found)
27       if(${found} EQUAL -1)
28         string(STRIP "${${var}} ${arg}" ${var})
29       endif()
30     endforeach()
31     set(${var} "${${var}}" PARENT_SCOPE)
32   endfunction()
33   foreach(target ${ARGN})
34     if(TARGET ${target})
35       get_property(LINK_FLAGS TARGET ${target} PROPERTY LINK_FLAGS)
36       if(ld-static-libgcc)
37         string_append(LINK_FLAGS -static-libgcc)
38       endif()
39       if(ld-static-libstdcxx)
40         string_append(LINK_FLAGS -static-libstdc++)
41       endif()
42       set_property(TARGET ${target} PROPERTY LINK_FLAGS "${LINK_FLAGS}")
43     endif()
44   endforeach()
45 endfunction()
47 function(target_link_static_3_15)
48   foreach(target ${ARGN})
49     if(TARGET ${target})
50       set_property(TARGET ${target} PROPERTY
51         MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>"
52       )
53     endif()
54   endforeach()
55 endfunction()
57 function(target_link_static)
58   if(CMAKE_VERSION VERSION_LESS 3.13)
59     target_link_static_3_13(${ARGN})
60     return()
61   endif()
62   if(NOT CMAKE_VERSION VERSION_LESS 3.15)
63     target_link_static_3_15(${ARGN})
64   endif()
65   set(scope PRIVATE)
66   foreach(target ${ARGN})
67     if(target MATCHES "^INTERFACE|PUBLIC|PRIVATE")
68       set(scope ${target})
69       continue()
70     endif()
71     if(ld-static-libgcc)
72       target_link_options(${target} ${scope} -static-libgcc)
73     endif()
74     if(ld-static-libstdcxx)
75       target_link_options(${target} ${scope} $<$<COMPILE_LANGUAGE:CXX>:-static-libstdc++>)
76     endif()
77   endforeach()
78 endfunction()