build: Produce a usable static kernel library

Since the move to cmake, the kernel static library that is installed
after a cmake --install build is unusable. It lacks symbols for the
internal libraries, besides those defined in the kernel library target.

This is because cmake, unlike the libtool archiver, does not combine
multiple static libraries into a single static library on installation.
This is likely an intentional design choice, since there were a bunch of
caveats to the way libtool calculated these libraries.

Fix this problem by installing all the required libraries. The user must
then link all of them along with the bitcoin kernel library.
This commit is contained in:
TheCharlatan 2024-09-04 21:46:31 +02:00
parent f640b323bd
commit 45be32f838
No known key found for this signature in database
GPG key ID: 9B79B45691DB4173

View file

@ -98,6 +98,32 @@ set_target_properties(bitcoinkernel PROPERTIES
CXX_VISIBILITY_PRESET default
)
# When building the static library, install all static libraries the
# bitcoinkernel depends on.
if(NOT BUILD_SHARED_LIBS)
# Recursively get all the static libraries a target depends on and put them in libs_out
function(get_target_static_link_libs target libs_out)
get_target_property(linked_libraries ${target} LINK_LIBRARIES)
foreach(dep ${linked_libraries})
if(TARGET ${dep})
get_target_property(dep_type ${dep} TYPE)
if(dep_type STREQUAL "STATIC_LIBRARY")
list(APPEND ${libs_out} ${dep})
get_target_static_link_libs(${dep} ${libs_out})
endif()
endif()
endforeach()
set(${libs_out} ${${libs_out}} PARENT_SCOPE)
endfunction()
set(all_kernel_static_link_libs "")
get_target_static_link_libs(bitcoinkernel all_kernel_static_link_libs)
foreach(lib ${all_kernel_static_link_libs})
install(TARGETS ${lib} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
endforeach()
endif()
include(GNUInstallDirs)
install(TARGETS bitcoinkernel
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}