From c4b6dceb035a4bebb4269bcf203a2aaec61c8a73 Mon Sep 17 00:00:00 2001 From: David Bears Date: Sat, 22 Aug 2026 13:21:59 -0400 Subject: [PATCH] polish CMake-aware installation --- CMakeLists.txt | 78 +++++++++++++++++------ mk/cmake/bitdht-config.cmake.in | 27 ++++++++ mk/cmake/bitdht-config_redirects.cmake.in | 24 +++++++ src/CMakeLists.txt | 5 ++ src/bitdht/CMakeLists.txt | 37 ++++++----- src/udp/CMakeLists.txt | 9 ++- src/util/CMakeLists.txt | 15 +++-- 7 files changed, 149 insertions(+), 46 deletions(-) create mode 100644 mk/cmake/bitdht-config.cmake.in create mode 100644 mk/cmake/bitdht-config_redirects.cmake.in diff --git a/CMakeLists.txt b/CMakeLists.txt index ec6175d..88c0024 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,20 +8,17 @@ cmake_minimum_required(VERSION 3.15) project(bitdht) -if(NOT DEFINED RS_DATA_DIR) - include(GNUInstallDirs) - set(RS_DATA_DIR "${CMAKE_INSTALL_DATADIR}/retroshare") -endif() +include(GNUInstallDirs) +set(BD_INSTALL_DATADIR "${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}") +set(BD_INSTALL_INCLUDEDIR "${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}") add_library(${PROJECT_NAME}) +add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME}) -target_include_directories( - ${PROJECT_NAME} - PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src -) +set(BD_BOOT_FILE "${PROJECT_SOURCE_DIR}/src/bitdht/bdboot.txt") if(WIN32) - target_compile_definitions(bitdht PRIVATE UNICODE _UNICODE) + target_compile_definitions(${PROJECT_NAME} PRIVATE UNICODE _UNICODE) # Some files trip strict GCC >= 15 diagnostics that are now errors by # default; downgrade them to warnings for now. @@ -29,7 +26,7 @@ if(WIN32) CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang" ) - target_compile_options(bitdht PRIVATE + target_compile_options(${PROJECT_NAME} PRIVATE -Wno-error=incompatible-pointer-types -Wno-error=int-conversion -Wno-error=implicit-function-declaration @@ -38,18 +35,59 @@ if(WIN32) endif() -add_library(bitdht:bitdht ALIAS bitdht) - add_subdirectory(src) -# TODO: some things to polish the cmake integration -# - install the library itself to support dynamic linking -# - install headers -# - install a config file -# - create a build-tree config file -# - config version files for install and build trees + +### installation +install(TARGETS ${PROJECT_NAME} + EXPORT ${PROJECT_NAME}_export + COMPONENT ${PROJECT_NAME} + FILE_SET HEADERS DESTINATION "${BD_INSTALL_INCLUDEDIR}" +) install( - FILES src/bitdht/bdboot.txt - DESTINATION ${RS_DATA_DIR} + FILES "${BD_BOOT_FILE}" + DESTINATION "${BD_INSTALL_DATADIR}" + COMPONENT ${PROJECT_NAME} +) + +# config package for install tree +set(CONFIG_DIR "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}") +install(EXPORT ${PROJECT_NAME}_export + DESTINATION "${CONFIG_DIR}" + FILE ${PROJECT_NAME}-targets.cmake + NAMESPACE ${PROJECT_NAME}:: + COMPONENT ${PROJECT_NAME} +) +include(CMakePackageConfigHelpers) +set(BD_INSTALL_BOOT_FILE "${BD_INSTALL_DATADIR}/bdboot.txt") +configure_package_config_file(mk/cmake/${PROJECT_NAME}-config.cmake.in + cmake/${PROJECT_NAME}-config_installed.cmake + INSTALL_DESTINATION "${CONFIG_DIR}" + PATH_VARS BD_INSTALL_BOOT_FILE +) +install(FILES + "${CMAKE_CURRENT_BINARY_DIR}/cmake/${PROJECT_NAME}-config_installed.cmake" + DESTINATION "${CONFIG_DIR}" + RENAME ${PROJECT_NAME}-config.cmake + COMPONENT ${PROJECT_NAME} +) + +# config package for build tree +export(EXPORT ${PROJECT_NAME}_export + FILE cmake/${PROJECT_NAME}-targets.cmake + NAMESPACE ${PROJECT_NAME}:: +) +include(CMakePackageConfigHelpers) +set(PACKAGE_BD_INSTALL_BOOT_FILE "${BD_BOOT_FILE}") +configure_package_config_file(mk/cmake/${PROJECT_NAME}-config.cmake.in + cmake/${PROJECT_NAME}-config.cmake + INSTALL_DESTINATION cmake +) + +# config package for CMAKE_FIND_PACKAGE_REDIRECTS_DIR +configure_file( + mk/cmake/${PROJECT_NAME}-config_redirects.cmake.in + "${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/${PROJECT_NAME}-config.cmake" + @ONLY ) diff --git a/mk/cmake/bitdht-config.cmake.in b/mk/cmake/bitdht-config.cmake.in new file mode 100644 index 0000000..a59be45 --- /dev/null +++ b/mk/cmake/bitdht-config.cmake.in @@ -0,0 +1,27 @@ +# ------------------------------------------------------------------------ *\ +# mk/cmake/bitdht-config.cmake.in +# This file is part of BitDHT +# +# Copyright (C) 2026 David Bears +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program. If not, see . +# ------------------------------------------------------------------------ */ + +@PACKAGE_INIT@ + +include("${CMAKE_CURRENT_LIST_DIR}/bitdht-targets.cmake") + +set_and_check(BD_BOOT_FILE "@PACKAGE_BD_INSTALL_BOOT_FILE@") + +check_required_components("${CMAKE_FIND_PACKAGE_NAME}") diff --git a/mk/cmake/bitdht-config_redirects.cmake.in b/mk/cmake/bitdht-config_redirects.cmake.in new file mode 100644 index 0000000..c98b631 --- /dev/null +++ b/mk/cmake/bitdht-config_redirects.cmake.in @@ -0,0 +1,24 @@ +# ------------------------------------------------------------------------ *\ +# mk/cmake/bitdht-config_redirects.cmake.in +# This file is part of BitDHT +# +# Copyright (C) 2026 David Bears +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program. If not, see . +# ------------------------------------------------------------------------ */ + +set(BD_BOOT_FILE "@BD_BOOT_FILE@") + +include("${CMAKE_CURRENT_LIST_DIR}/bitdht-extra.cmake" OPTIONAL) +include("${CMAKE_CURRENT_LIST_DIR}/bitdhtExtra.cmake" OPTIONAL) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4911b96..6605fd2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -18,6 +18,11 @@ # along with this program. If not, see . # ------------------------------------------------------------------------ */ +target_sources(${PROJECT_NAME} + PUBLIC FILE_SET HEADERS + BASE_DIRS ${CMAKE_CURRENT_SOURCE_DIR} +) + add_subdirectory(bitdht) add_subdirectory(udp) add_subdirectory(util) diff --git a/src/bitdht/CMakeLists.txt b/src/bitdht/CMakeLists.txt index 5234cd9..190d43a 100644 --- a/src/bitdht/CMakeLists.txt +++ b/src/bitdht/CMakeLists.txt @@ -18,38 +18,41 @@ # along with this program. If not, see . # ------------------------------------------------------------------------ */ -target_sources(${PROJECT_NAME} PRIVATE - bdaccount.cc +target_sources(${PROJECT_NAME} + PUBLIC FILE_SET HEADERS FILES bdaccount.h - bdconnection.cc bdconnection.h - bdfilter.cc bdfilter.h - bdfriendlist.cc bdfriendlist.h - bdhash.cc bdhash.h - bdhistory.cc bdhistory.h bdiface.h - bdmanager.cc bdmanager.h - bdmsgs.cc bdmsgs.h - bdnode.cc bdnode.h - bdobj.cc bdobj.h - bdpeer.cc bdpeer.h - bdquery.cc bdquery.h - bdquerymgr.cc bdquerymgr.h - bdstddht.cc bdstddht.h - bdstore.cc bdstore.h - bencode.c bencode.h + + PRIVATE + bdaccount.cc + bdconnection.cc + bdfilter.cc + bdfriendlist.cc + bdhash.cc + bdhistory.cc + bdmanager.cc + bdmsgs.cc + bdnode.cc + bdobj.cc + bdpeer.cc + bdquery.cc + bdquerymgr.cc + bdstddht.cc + bdstore.cc + bencode.c ) diff --git a/src/udp/CMakeLists.txt b/src/udp/CMakeLists.txt index 7f4c3f5..34110dc 100644 --- a/src/udp/CMakeLists.txt +++ b/src/udp/CMakeLists.txt @@ -19,11 +19,14 @@ # ------------------------------------------------------------------------ */ target_sources(${PROJECT_NAME} PRIVATE - udpbitdht.cc + PUBLIC FILE_SET HEADERS FILES udpbitdht.h - udplayer.cc udplayer.h udpproxylayer.h - udpstack.cc udpstack.h + + PRIVATE + udpbitdht.cc + udplayer.cc + udpstack.cc ) diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt index 0d1516f..464cf15 100644 --- a/src/util/CMakeLists.txt +++ b/src/util/CMakeLists.txt @@ -19,16 +19,19 @@ # ------------------------------------------------------------------------ */ target_sources(${PROJECT_NAME} PRIVATE - bdbloom.cc + PUBLIC FILE_SET HEADERS FILES bdbloom.h - bdfile.cc bdfile.h - bdnet.cc bdnet.h - bdrandom.cc bdrandom.h - bdstring.cc bdstring.h - bdthreads.cc bdthreads.h + + PRIVATE + bdbloom.cc + bdfile.cc + bdnet.cc + bdrandom.cc + bdstring.cc + bdthreads.cc )