fix when -DLIBA_JAVASCRIPT=1 and -DLIBA_CXX=0
[liba.git] / cmake / TargetStatic.cmake
blob47fe82d7b07e554d53938adb5e76a693431170e6
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     check_c_compiler_flag(-static-libgcc ld-static-libgcc)
9   endif()
10   set(index)
11 endif()
13 if(NOT CMAKE_STATIC_LIBRARY_SUFFIX STREQUAL .lib)
14   list(FIND ENABLED_LANGUAGES CXX index)
15   if(${index} GREATER -1)
16     check_cxx_compiler_flag(-static-libstdc++ ld-static-libstdcxx)
17   endif()
18   set(index)
19 endif()
21 function(string_append var)
22   foreach(arg ${ARGN})
23     string(FIND "${${var}}" "${arg}" index)
24     if(${index} EQUAL -1)
25       string(STRIP "${${var}} ${arg}" ${var})
26     endif()
27   endforeach()
28   set(${var} "${${var}}" PARENT_SCOPE)
29 endfunction()
31 function(target_link_static_3_13)
32   foreach(target ${ARGN})
33     if(TARGET ${target})
34       get_property(LINK_FLAGS TARGET ${target} PROPERTY LINK_FLAGS)
35       if(ld-static-libgcc)
36         string_append(LINK_FLAGS -static-libgcc)
37       endif()
38       if(ld-static-libstdcxx)
39         string_append(LINK_FLAGS -static-libstdc++)
40       endif()
41       set_property(TARGET ${target} PROPERTY LINK_FLAGS "${LINK_FLAGS}")
42     endif()
43   endforeach()
44 endfunction()
46 function(target_link_static_3_15)
47   foreach(target ${ARGN})
48     if(TARGET ${target})
49       set_property(TARGET ${target} PROPERTY
50         MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>"
51       )
52     endif()
53   endforeach()
54 endfunction()
56 function(target_link_static)
57   if(CMAKE_VERSION VERSION_LESS 3.13)
58     target_link_static_3_13(${ARGN})
59     return()
60   endif()
61   if(NOT CMAKE_VERSION VERSION_LESS 3.15)
62     target_link_static_3_15(${ARGN})
63   endif()
64   set(scope PRIVATE)
65   foreach(target ${ARGN})
66     if(target MATCHES "^INTERFACE|PUBLIC|PRIVATE")
67       set(scope ${target})
68       continue()
69     endif()
70     if(ld-static-libgcc)
71       target_link_options(${target} ${scope} -static-libgcc)
72     endif()
73     if(ld-static-libstdcxx)
74       target_link_options(${target} ${scope} $<$<COMPILE_LANGUAGE:CXX>:-static-libstdc++>)
75     endif()
76   endforeach()
77 endfunction()