summaryrefslogtreecommitdiffstats
path: root/Tests/RunCMake/target_link_libraries-LINK_LIBRARY/load_archive.cmake
blob: a0bbb436cf21f95e973e6a992585352b26be1c0c (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

enable_language(C)

set(CMAKE_C_LINK_LIBRARY_USING_load_archive_SUPPORTED TRUE)
if(CMAKE_C_COMPILER_ID STREQUAL "AppleClang")
  set(CMAKE_C_LINK_LIBRARY_USING_load_archive "-force_load <LIB_ITEM>")
elseif(CMAKE_C_COMPILER_ID STREQUAL "GNU" AND CMAKE_SYSTEM_NAME STREQUAL "Linux")
  execute_process(COMMAND "${CMAKE_LINKER}" --help
                          OUTPUT_VARIABLE linker_help
                          ERROR_VARIABLE linker_help)
  if(linker_help MATCHES "--push-state" AND linker_help MATCHES "--pop-state")
    set(CMAKE_C_LINK_LIBRARY_USING_load_archive "LINKER:--push-state,--whole-archive"
                                                "<LINK_ITEM>"
                                                "LINKER:--pop-state")
  else()
    set(CMAKE_C_LINK_LIBRARY_USING_load_archive "LINKER:--whole-archive"
                                                "<LINK_ITEM>"
                                                "LINKER:--no-whole-archive")
  endif()
elseif(CMAKE_C_COMPILER_ID STREQUAL "MSVC")
  set(CMAKE_C_LINK_LIBRARY_USING_load_archive "/WHOLEARCHIVE:<LIBRARY>")
else()
  # feature not yet supported for the other environments
  set(CMAKE_C_LINK_LIBRARY_USING_load_archive_SUPPORTED FALSE)
endif()

add_library(base STATIC base.c unref.c)
target_compile_definitions(base PUBLIC STATIC_BASE)

add_library(lib SHARED lib.c)
target_link_libraries(lib PRIVATE "$<LINK_LIBRARY:load_archive,base>")

add_executable(main main.c)
target_link_libraries(main PRIVATE lib)