diff --git a/cmake/modules/InstallationHelper.cmake b/cmake/modules/InstallationHelper.cmake --- a/cmake/modules/InstallationHelper.cmake +++ b/cmake/modules/InstallationHelper.cmake @@ -2,11 +2,72 @@ include(GNUInstallDirs) -macro(install_target _target) +function(install_target _target) + set(RUNTIME_DESTINATION "${CMAKE_INSTALL_BINDIR}") + # CMake installs Windows shared libraries to the RUNTIME destination folder, + # but autotools install them into the LIBRARY destination folder. + # This special case only purpose is to provide identical installation trees + # between CMake and autotools. + get_target_property(_target_type ${_target} TYPE) + if(${CMAKE_SYSTEM_NAME} MATCHES "Windows" AND _target_type STREQUAL "SHARED_LIBRARY") + set(RUNTIME_DESTINATION "${CMAKE_INSTALL_LIBDIR}") + endif() + install( TARGETS ${_target} - RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" + RUNTIME DESTINATION "${RUNTIME_DESTINATION}" LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" ) -endmacro() +endfunction() + +function(install_shared_library NAME) + cmake_parse_arguments(ARG + "" + "" + "PUBLIC_HEADER" + ${ARGN} + ) + + set(_sources ${ARG_UNPARSED_ARGUMENTS}) + + get_target_property(_target_type ${NAME} TYPE) + if(_target_type STREQUAL "SHARED_LIBRARY") + set(_shared_name "${NAME}") + target_sources(${NAME} PRIVATE ${_sources}) + else() + set(_shared_name "${NAME}-shared") + add_library(${_shared_name} SHARED ${_sources}) + target_link_libraries(${_shared_name} ${NAME}) + endif() + + if(ARG_PUBLIC_HEADER) + list(APPEND _properties PUBLIC_HEADER ${ARG_PUBLIC_HEADER}) + endif() + + if(${CMAKE_SYSTEM_NAME} MATCHES "Linux") + # FIXME For compatibility reason with autotools, the version is set + # to 0.0.0 (major being actually 0). This is obviously wrong and the + # version of the library should reflect the version of the release. + # On platforms other than linux, only the major version (0) is used. + # Replace the VERSION line with the statement below to set the + # correct version: + # set(_properties VERSION "${bitcoin-abc_VERSION}") + list(APPEND _properties VERSION "${bitcoin-abc_VERSION_MAJOR}.0.0") + else() + list(APPEND _properties VERSION "${bitcoin-abc_VERSION_MAJOR}") + endif() + + # For autotools compatibility, rename the library to ${OUTPUT_NAME}-0.dll + if(${CMAKE_SYSTEM_NAME} MATCHES "Windows") + list(APPEND _properties OUTPUT_NAME "${NAME}-${bitcoin-abc_VERSION_MAJOR}") + else() + list(APPEND _properties OUTPUT_NAME "${NAME}") + endif() + + list(APPEND _properties SOVERSION "${bitcoin-abc_VERSION_MAJOR}") + + set_target_properties(${_shared_name} PROPERTIES ${_properties}) + + install_target(${_shared_name}) +endfunction() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -15,6 +15,7 @@ option(BUILD_BITCOIN_CLI "Build bitcoin-cli" ON) option(BUILD_BITCOIN_TX "Build bitcoin-tx" ON) option(BUILD_BITCOIN_QT "Build bitcoin-qt" ON) +option(BUILD_LIBBITCOINCONSENSUS "Build the bitcoinconsenus shared library" ON) option(ENABLE_HARDENING "Harden the executables" ON) option(ENABLE_REDUCE_EXPORTS "Reduce the amount of exported symbols" OFF) option(ENABLE_STATIC_LIBSTDCXX "Statically link libstdc++" OFF) @@ -374,7 +375,6 @@ # script library add_library(script - script/bitcoinconsensus.cpp script/bitfield.cpp script/interpreter.cpp script/ismine.cpp @@ -399,6 +399,20 @@ target_link_libraries(bitcoinconsensus script) +if(BUILD_LIBBITCOINCONSENSUS) + target_compile_definitions(bitcoinconsensus + PUBLIC + BUILD_BITCOIN_INTERNAL + HAVE_CONSENSUS_LIB + ) + + include(InstallationHelper) + install_shared_library(bitcoinconsensus + script/bitcoinconsensus.cpp + PUBLIC_HEADER script/bitcoinconsensus.h + ) +endif() + # Bitcoin server facilities add_library(server addrdb.cpp @@ -510,7 +524,6 @@ endif() include(BinaryTest) -include(InstallationHelper) # bitcoin-cli if(BUILD_BITCOIN_CLI) diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -154,6 +154,11 @@ find_package(Boost 1.58 REQUIRED unit_test_framework) target_link_libraries(test_bitcoin Boost::unit_test_framework rpcclient server) +if(TARGET bitcoinconsensus-shared) + target_link_libraries(test_bitcoin bitcoinconsensus-shared) +else() + target_link_libraries(test_bitcoin bitcoinconsensus) +endif() # We need to detect if the BOOST_TEST_DYN_LINK flag is required. set(CMAKE_REQUIRED_LIBRARIES Boost::unit_test_framework)