From 51aebb6ee35e099cc9bf0e3f3fadde3a5c6af7be Mon Sep 17 00:00:00 2001 From: David Bears Date: Sat, 1 Aug 2026 15:31:52 -0400 Subject: [PATCH 1/2] improve cmake build - avoid globbing sources - move platform-dependent compile options for bitdht from other projects to this one - create an alias target for use by superproject - avoid messing with RS_DATA_DIR globally --- CMakeLists.txt | 53 ++++++++++++++++++++++++++++--------- src/CMakeLists.txt | 23 ++++++++++++++++ src/bitdht/CMakeLists.txt | 55 +++++++++++++++++++++++++++++++++++++++ src/udp/CMakeLists.txt | 29 +++++++++++++++++++++ src/util/CMakeLists.txt | 34 ++++++++++++++++++++++++ 5 files changed, 181 insertions(+), 13 deletions(-) create mode 100644 src/CMakeLists.txt create mode 100644 src/bitdht/CMakeLists.txt create mode 100644 src/udp/CMakeLists.txt create mode 100644 src/util/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 4b4e8e534c..ec6175d7d9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,28 +1,55 @@ # RetroShare decentralized communication platform # # Copyright (C) 2021-2022 Gioacchino Mazzurco +# Copyright (C) 2026 David Bears # # SPDX-License-Identifier: CC0-1.0 -cmake_minimum_required (VERSION 3.15) +cmake_minimum_required(VERSION 3.15) project(bitdht) -set( - RS_DATA_DIR - "${CMAKE_INSTALL_PREFIX}/share/retroshare" - CACHE STRING - "Path where to install RetroShare system wide data" ) +if(NOT DEFINED RS_DATA_DIR) + include(GNUInstallDirs) + set(RS_DATA_DIR "${CMAKE_INSTALL_DATADIR}/retroshare") +endif() -file( - GLOB BITDHT_SOURCES - src/bitdht/*.c src/bitdht/*.cc src/udp/*.cc src/util/*.cc ) - -add_library(${PROJECT_NAME} ${BITDHT_SOURCES}) +add_library(${PROJECT_NAME}) target_include_directories( ${PROJECT_NAME} - PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src ) + PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src +) + +if(WIN32) + target_compile_definitions(bitdht PRIVATE UNICODE _UNICODE) + + # Some files trip strict GCC >= 15 diagnostics that are now errors by + # default; downgrade them to warnings for now. + if( + CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR + CMAKE_CXX_COMPILER_ID STREQUAL "Clang" + ) + target_compile_options(bitdht PRIVATE + -Wno-error=incompatible-pointer-types + -Wno-error=int-conversion + -Wno-error=implicit-function-declaration + ) + endif() +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 install( FILES src/bitdht/bdboot.txt - DESTINATION ${RS_DATA_DIR} ) + DESTINATION ${RS_DATA_DIR} +) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000000..4911b96633 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,23 @@ +# ------------------------------------------------------------------------ *\ +# src/CMakeLists.txt +# 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 . +# ------------------------------------------------------------------------ */ + +add_subdirectory(bitdht) +add_subdirectory(udp) +add_subdirectory(util) diff --git a/src/bitdht/CMakeLists.txt b/src/bitdht/CMakeLists.txt new file mode 100644 index 0000000000..5234cd9f7f --- /dev/null +++ b/src/bitdht/CMakeLists.txt @@ -0,0 +1,55 @@ +# ------------------------------------------------------------------------ *\ +# src/bitdht/CMakeLists.txt +# 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 . +# ------------------------------------------------------------------------ */ + +target_sources(${PROJECT_NAME} PRIVATE + bdaccount.cc + 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 +) diff --git a/src/udp/CMakeLists.txt b/src/udp/CMakeLists.txt new file mode 100644 index 0000000000..7f4c3f5f1d --- /dev/null +++ b/src/udp/CMakeLists.txt @@ -0,0 +1,29 @@ +# ------------------------------------------------------------------------ *\ +# src/udp/CMakeLists.txt +# 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 . +# ------------------------------------------------------------------------ */ + +target_sources(${PROJECT_NAME} PRIVATE + udpbitdht.cc + udpbitdht.h + udplayer.cc + udplayer.h + udpproxylayer.h + udpstack.cc + udpstack.h +) diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt new file mode 100644 index 0000000000..0d1516f07e --- /dev/null +++ b/src/util/CMakeLists.txt @@ -0,0 +1,34 @@ +# ------------------------------------------------------------------------ *\ +# src/util/CMakeLists.txt +# 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 . +# ------------------------------------------------------------------------ */ + +target_sources(${PROJECT_NAME} PRIVATE + bdbloom.cc + bdbloom.h + bdfile.cc + bdfile.h + bdnet.cc + bdnet.h + bdrandom.cc + bdrandom.h + bdstring.cc + bdstring.h + bdthreads.cc + bdthreads.h +) From c4b6dceb035a4bebb4269bcf203a2aaec61c8a73 Mon Sep 17 00:00:00 2001 From: David Bears Date: Sat, 22 Aug 2026 13:21:59 -0400 Subject: [PATCH 2/2] 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 ec6175d7d9..88c0024c2a 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 0000000000..a59be4540f --- /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 0000000000..c98b631ded --- /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 4911b96633..6605fd2350 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 5234cd9f7f..190d43a2ac 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 7f4c3f5f1d..34110dc262 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 0d1516f07e..464cf15827 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 )