Skip to content

Commit

Permalink
Merge bitcoin#30814: kernel: Create usable static kernel library
Browse files Browse the repository at this point in the history
0dd16d7 build: Add a pkg-config file for libbitcoinkernel (TheCharlatan)
45be32f build: Produce a usable static kernel library (TheCharlatan)

Pull request description:

  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.

  Fix this by explicitly installing all the required internal static libraries. To make usage of these installed libraries easy, add a pkg-config file that can be used during linking.

  This patch can be tested with:

  ```
  cmake -B build -DBUILD_SHARED_LIBS=OFF -DBUILD_KERNEL_LIB=ON
  cmake --build build
  cmake --install build
  g++ -std=c++20 -o test_chainstate src/bitcoin-chainstate.cpp -I/home/drgrid/bitcoin/src $(pkg-config --libs --static libbitcoinkernel)
  ```

  Attempts to solve bitcoin#30801

ACKs for top commit:
  hebasto:
    ACK 0dd16d7.
  fanquake:
    ACK 0dd16d7 - this looks like a good place to start.
  ryanofsky:
    Code review ACK 0dd16d7

Tree-SHA512: 92f7bc959584bdc595f4aa6d0ab133355481075fe8564224fd7ac122fd7bdd75f98cf26ef0a6a7d84fd552d2258ddca1b674eca91122469a58bacc5f0a0ec2ef
  • Loading branch information
fanquake committed Sep 12, 2024
2 parents 7d43bca + 0dd16d7 commit 24817e8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
11 changes: 11 additions & 0 deletions libbitcoinkernel.pc.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
prefix=@CMAKE_INSTALL_PREFIX@
exec_prefix=${prefix}
libdir=${prefix}/@CMAKE_INSTALL_LIBDIR@
includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@

Name: @PACKAGE_NAME@ kernel library
Description: Experimental library for the Bitcoin Core validation engine.
Version: @PACKAGE_VERSION@
Libs: -L${libdir} -lbitcoinkernel
Libs.private: -L${libdir} @LIBS_PRIVATE@
Cflags: -I${includedir}
34 changes: 34 additions & 0 deletions src/kernel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,40 @@ 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)

# LIBS_PRIVATE is substituted in the pkg-config file.
set(LIBS_PRIVATE "")
foreach(lib ${all_kernel_static_link_libs})
install(TARGETS ${lib} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
string(APPEND LIBS_PRIVATE " -l${lib}")
endforeach()

string(STRIP "${LIBS_PRIVATE}" LIBS_PRIVATE)
endif()

configure_file(${PROJECT_SOURCE_DIR}/libbitcoinkernel.pc.in ${PROJECT_BINARY_DIR}/libbitcoinkernel.pc @ONLY)
install(FILES ${PROJECT_BINARY_DIR}/libbitcoinkernel.pc DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")

include(GNUInstallDirs)
install(TARGETS bitcoinkernel
RUNTIME
Expand Down

0 comments on commit 24817e8

Please sign in to comment.