From 1a5d49b7f61e6dbf707ff02e860edb563fc88801 Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Sun, 5 Oct 2025 16:03:59 +0100 Subject: [PATCH] Replace calculate_libraries with inline CMake code. --- CMakeLists.txt | 3 +- cmake/Anope.cmake | 110 ++++++++-------------------------- modules/CMakeLists.txt | 40 +++---------- modules/extra/enc_argon2.cpp | 10 +++- modules/extra/enc_posix.cpp | 2 - modules/extra/ldap.cpp | 7 ++- modules/extra/mysql.cpp | 10 +++- modules/extra/regex_pcre2.cpp | 10 +++- modules/extra/regex_tre.cpp | 5 +- modules/extra/sqlite.cpp | 6 +- modules/extra/ssl_gnutls.cpp | 6 +- modules/extra/ssl_openssl.cpp | 6 +- modules/extra/xmlrpc.cpp | 7 ++- src/CMakeLists.txt | 6 +- 14 files changed, 89 insertions(+), 139 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5ce846ab9..c0b0e540d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -107,6 +107,7 @@ include(CheckFunctionExists) include(CheckTypeSize) include(CheckLibraryExists) include(CheckCXXCompilerFlag) +include(FindPkgConfig) # If extra include directories were specified, tell cmake about them. if(EXTRA_INCLUDE) @@ -121,7 +122,7 @@ endif() # setup conan if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/conanbuildinfo.cmake") include("${CMAKE_CURRENT_SOURCE_DIR}/conanbuildinfo.cmake") - conan_basic_setup() + conan_basic_setup(TARGETS) endif() # Find gettext diff --git a/cmake/Anope.cmake b/cmake/Anope.cmake index 6dd136421..d445e1e83 100644 --- a/cmake/Anope.cmake +++ b/cmake/Anope.cmake @@ -1,87 +1,3 @@ -############################################################################### -# calculate_libraries( ) -# -# This macro is used in most of the module (sub)directories to calculate the -# library dependencies for the given source file. -############################################################################### -macro(calculate_libraries SRC SRC_LDFLAGS EXTRA_DEPENDS) - # Set up a temporary LDFLAGS for this file - set(THIS_LDFLAGS "${LDFLAGS}") - # Reset extra dependencies - set(EXTRA_DEPENDENCIES) - # Reset library paths - set(LIBRARY_PATHS) - # Reset libraries - set(LIBRARIES) - # Check to see if there are any lines matching: /* RequiredLibraries: [something] */ - if(WIN32) - file(STRINGS ${SRC} REQUIRED_LIBRARIES REGEX "/\\*[ \t]*RequiredWindowsLibraries:[ \t]*.*[ \t]*\\*/") - else() - file(STRINGS ${SRC} REQUIRED_LIBRARIES REGEX "/\\*[ \t]*RequiredLibraries:[ \t]*.*[ \t]*\\*/") - endif() - # Iterate through those lines - foreach(REQUIRED_LIBRARY ${REQUIRED_LIBRARIES}) - # Strip off the /* RequiredLibraries: and */ from the line - string(REGEX REPLACE "/\\*[ \t]*Required.*Libraries:[ \t]*([^ \t]*)[ \t]*\\*/" "\\1" REQUIRED_LIBRARY ${REQUIRED_LIBRARY}) - # Replace all commas with semicolons - string(REGEX REPLACE "," ";" REQUIRED_LIBRARY ${REQUIRED_LIBRARY}) - # Iterate through the libraries given - foreach(LIBRARY ${REQUIRED_LIBRARY}) - # If the library has multiple names extract the alternate. - unset(LIBRARY_ALT) - if (${LIBRARY} MATCHES "^.+\\|.+$") - string(REGEX REPLACE ".+\\|(.*)" "\\1" LIBRARY_ALT ${LIBRARY}) - string(REGEX REPLACE "(.+)\\|.*" "\\1" LIBRARY ${LIBRARY}) - endif() - # Locate the library to see if it exists - if(DEFAULT_LIBRARY_DIRS OR DEFINED $ENV{VCINSTALLDIR}) - find_library(FOUND_${LIBRARY}_LIBRARY NAMES ${LIBRARY} ${LIBRARY_ALT} PATHS ${DEFAULT_LIBRARY_DIRS} $ENV{VCINSTALLDIR}/lib ${EXTRA_INCLUDE} ${EXTRA_LIBS}) - else() - find_library(FOUND_${LIBRARY}_LIBRARY NAMES ${LIBRARY} ${LIBRARY_ALT} PATHS ${EXTRA_INCLUDE} ${EXTRA_LIBS} NO_DEFAULT_PATH) - find_library(FOUND_${LIBRARY}_LIBRARY NAMES ${LIBRARY} ${LIBRARY_ALT} PATHS ${EXTRA_INCLUDE} ${EXTRA_LIBS}) - endif() - # If the library was found, we will add it to the linker flags - if(FOUND_${LIBRARY}_LIBRARY) - if(MSVC) - # For Visual Studio, instead of editing the linker flags, we'll add the library to a separate list of extra dependencies - list(APPEND EXTRA_DEPENDENCIES "${FOUND_${LIBRARY}_LIBRARY}") - else() - # Get the path only of the library, to add it to library paths. - get_filename_component(LIBRARY_PATH ${FOUND_${LIBRARY}_LIBRARY} PATH) - list(APPEND LIBRARY_PATHS "${LIBRARY_PATH}") - # Extract the library short name, add it to the library path - get_filename_component(LIBRARY_NAME ${FOUND_${LIBRARY}_LIBRARY} NAME_WE) - string(REGEX REPLACE "^lib" "" LIBRARY_NAME ${LIBRARY_NAME}) - list(APPEND LIBRARIES ${LIBRARY_NAME}) - endif() - else() - # In the case of the library not being found, we fatally error so CMake stops trying to generate - message(FATAL_ERROR "${SRC} needs library ${LIBRARY} but we were unable to locate that library! Check that the library is within the search path of your OS.") - endif() - endforeach() - endforeach() - # Remove duplicates from the library paths - if(LIBRARY_PATHS) - list(REMOVE_DUPLICATES LIBRARY_PATHS) - endif() - # Remove diplicates from the libraries - if(LIBRARIES) - list(REMOVE_DUPLICATES LIBRARIES) - endif() - # Iterate through library paths and add them to the linker flags - foreach(LIBRARY_PATH ${LIBRARY_PATHS}) - if(NOT "${LIBRARY_PATH}" IN_LIST DEFAULT_LIBRARY_DIRS) - set(THIS_LDFLAGS "${THIS_LDFLAGS} -L${LIBRARY_PATH}") - endif() - endforeach() - # Iterate through libraries and add them to the linker flags - foreach(LIBRARY ${LIBRARIES}) - list(APPEND EXTRA_DEPENDENCIES "${LIBRARY}") - endforeach() - set(${SRC_LDFLAGS} "${THIS_LDFLAGS}") - set(${EXTRA_DEPENDS} "${EXTRA_DEPENDENCIES}") -endmacro() - ############################################################################### # add_to_cpack_ignored_files( [TRUE]) # @@ -104,3 +20,29 @@ macro(add_to_cpack_ignored_files ITEM) set(ENV{CPACK_IGNORED_FILES} "${REAL_ITEM}") endif() endmacro() + +############################################################################### +# inline_cmake(TARGET FILE) +# +# A macro to execute inline CMake instructions from within a module source. +############################################################################### +macro(inline_cmake TARGET FILE) + file(STRINGS ${FILE} SRC) + set(CODE "") + set(IN_CODE OFF) + foreach(LINE IN LISTS SRC) + if(IN_CODE) + string(REGEX REPLACE "/// " "" CLEAN_LINE ${LINE}) + if(CLEAN_LINE MATCHES "^END CMAKE$") + cmake_language(EVAL CODE "${CODE}") + set(CODE "") + set(IN_CODE OFF) + else() + set(CODE "${CODE}\n${CLEAN_LINE}") + endif() + elseif(LINE MATCHES "^/// BEGIN CMAKE$") + message(STATUS "Executing inline CMake code for ${TARGET}") + set(IN_CODE ON) + endif() + endforeach() +endmacro() diff --git a/modules/CMakeLists.txt b/modules/CMakeLists.txt index 777f080f0..76757a2e0 100644 --- a/modules/CMakeLists.txt +++ b/modules/CMakeLists.txt @@ -52,12 +52,6 @@ macro(build_modules SRC) file(RELATIVE_PATH FNAME ${SRC} ${MODULE_SRC}) # Convert the real source file extension to have a library extension string(REGEX REPLACE "\\.cpp$" "${CMAKE_SHARED_LIBRARY_SUFFIX}" SO ${FNAME}) - # Reset linker flags - set(TEMP_LDFLAGS) - # Reset extra dependencies - set(TEMP_DEPENDENCIES) - # Calculate the library dependencies for the given source file - calculate_libraries(${MODULE_SRC} TEMP_LDFLAGS TEMP_DEPENDENCIES) # For Visual Studio only, include win32_memory static library, required to override Visual Studio's overrides of the new/delete operators if(MSVC) set(WIN32_MEMORY win32_memory) @@ -66,6 +60,8 @@ macro(build_modules SRC) endif() # Generate the module and set its linker flags, also set it to depend on the main Anope executable to be built beforehand add_library(${SO} MODULE ${MODULE_SRC}) + # Execute inline CMake code for the module + inline_cmake(${SO} ${MODULE_SRC}) # Windows requires this because it's weird if(WIN32) set(WIN32_NO_LIBS "/nodefaultlib:\"libcmt.lib\" /OPT:NOREF") @@ -77,7 +73,7 @@ macro(build_modules SRC) FOLDER "Modules" INSTALL_RPATH_USE_LINK_PATH ON LINKER_LANGUAGE CXX - LINK_FLAGS "${TEMP_LDFLAGS} ${WIN32_NO_LIBS}" + LINK_FLAGS "${WIN32_NO_LIBS}" PREFIX "" SUFFIX "" ) @@ -85,13 +81,12 @@ macro(build_modules SRC) if(HAVE_LOCALIZATION) add_dependencies(${SO} module_language) endif() - target_link_libraries(${SO} ${TEMP_DEPENDENCIES}) # For Windows only, have the module link to the export library of Anope as well as wsock32 and Ws2_32 libraries (most of the modules probably don't need this, but this is to be on the safe side), also set its version if(WIN32) - target_link_libraries(${SO} ${PROGRAM_NAME} wsock32 Ws2_32 ${WIN32_MEMORY}) + target_link_libraries(${SO} PUBLIC ${PROGRAM_NAME} wsock32 Ws2_32 ${WIN32_MEMORY}) set_target_properties(${PROGRAM_NAME} PROPERTIES VERSION "${VERSION_DOTTED}") elseif(APPLE) - target_link_libraries(${SO} ${PROGRAM_NAME}) + target_link_libraries(${SO} PUBLIC ${PROGRAM_NAME}) endif() # Set the module to be installed to the module directory under the data directory install(TARGETS ${SO} @@ -113,31 +108,11 @@ macro(build_subdir) # Set all the files to use C++ as well as set their compile flags (use the module-specific compile flags, though) set_source_files_properties(${MODULES_SUBDIR_SRCS} PROPERTIES LANGUAGE CXX COMPILE_FLAGS "${CXXFLAGS}") - # Iterate through the source files in the subdirectory - foreach(SRC ${MODULES_SUBDIR_SRCS}) - # Reset linker flags - set(TEMP_LDFLAGS) - # Reset extra dependencies - set(TEMP_DEPENDENCIES) - # Calculate the library dependencies for the given source file - calculate_libraries(${SRC} SKIP_LIBRARIES MODULE TEMP_LDFLAGS TEMP_DEPENDENCIES) - - # Append this source file's linker flags to the subdirectoy's linker flags, if there are any to append - if(TEMP_DEPENDENCIES) - list(APPEND SUBDIR_EXTRA_DEPENDS ${TEMP_DEPENDENCIES}) - endif() - endforeach() - # Remove duplicates from the linker flags if(SUBDIR_LDFLAGS) list(REMOVE_DUPLICATES SUBDIR_LDFLAGS) endif() - # Remove duplicates from the extra dependencies - if(SUBDIR_EXTRA_DEPENDS) - list(REMOVE_DUPLICATES SUBDIR_EXTRA_DEPENDS) - endif() - # For Visual Studio only, include win32_memory static library, required to override Visual Studio's overrides of the new/delete operators if(MSVC) set(WIN32_MEMORY win32_memory) @@ -160,13 +135,12 @@ macro(build_subdir) if(HAVE_LOCALIZATION) add_dependencies(${SO} module_language) endif() - target_link_libraries(${SO} ${SUBDIR_EXTRA_DEPENDS}) # For Windows only, have the module link to the export library of Anope as well as wsock32 and Ws2_32 libraries (most of the modules probably don't need this, but this is to be on the safe side), also set it's version if(WIN32) - target_link_libraries(${SO} ${PROGRAM_NAME} wsock32 Ws2_32 ${WIN32_MEMORY}) + target_link_libraries(${SO} PUBLIC ${PROGRAM_NAME} PUBLIC wsock32 Ws2_32 ${WIN32_MEMORY}) set_target_properties(${PROGRAM_NAME} PROPERTIES VERSION "${VERSION_DOTTED}") elseif(APPLE) - target_link_libraries(${SO} ${PROGRAM_NAME}) + target_link_libraries(${SO} PUBLIC ${PROGRAM_NAME}) endif() # Set the module to be installed to the module directory under the data directory diff --git a/modules/extra/enc_argon2.cpp b/modules/extra/enc_argon2.cpp index 81d052c0c..ecfd94d4b 100644 --- a/modules/extra/enc_argon2.cpp +++ b/modules/extra/enc_argon2.cpp @@ -8,8 +8,14 @@ * */ -/* RequiredLibraries: argon2 */ -/* RequiredWindowsLibraries: argon2 */ +/// BEGIN CMAKE +/// if(WIN32) +/// target_link_libraries(${SO} PRIVATE CONAN_PKG::argon2) +/// else() +/// pkg_check_modules("ARGON2" IMPORTED_TARGET REQUIRED "libargon2") +/// target_link_libraries(${SO} PRIVATE PkgConfig::ARGON2) +/// endif() +/// END CMAKE #include #include diff --git a/modules/extra/enc_posix.cpp b/modules/extra/enc_posix.cpp index acf65d95b..7bcfc1fea 100644 --- a/modules/extra/enc_posix.cpp +++ b/modules/extra/enc_posix.cpp @@ -8,8 +8,6 @@ * */ -/* RequiredLibraries: crypt */ - #include "module.h" class EPOSIX final diff --git a/modules/extra/ldap.cpp b/modules/extra/ldap.cpp index 3a26281d9..0c5289efe 100644 --- a/modules/extra/ldap.cpp +++ b/modules/extra/ldap.cpp @@ -9,7 +9,12 @@ * Based on the original code of Services by Andy Church. */ -/* RequiredLibraries: ldap_r|ldap,lber */ +/// BEGIN CMAKE +/// if(NOT WIN32) +/// pkg_check_modules("OPENLDAP" IMPORTED_TARGET REQUIRED "lber" "ldap") +/// target_link_libraries(${SO} PRIVATE PkgConfig::OPENLDAP) +/// endif() +/// END CMAKE #include "module.h" #include "modules/ldap.h" diff --git a/modules/extra/mysql.cpp b/modules/extra/mysql.cpp index a7e2699f9..2b1922aa3 100644 --- a/modules/extra/mysql.cpp +++ b/modules/extra/mysql.cpp @@ -6,8 +6,14 @@ * Please read COPYING and README for further details. */ -/* RequiredLibraries: mysqlclient */ -/* RequiredWindowsLibraries: libmysql */ +/// BEGIN CMAKE +/// if(WIN32) +/// target_link_libraries(${SO} PRIVATE CONAN_PKG::libmysqlclient) +/// else() +/// pkg_search_module("MYSQL" IMPORTED_TARGET REQUIRED "mysqlclient" "mariadb") +/// target_link_libraries(${SO} PRIVATE PkgConfig::MYSQL) +/// endif() +/// END CMAKE #include "module.h" #include "modules/sql.h" diff --git a/modules/extra/regex_pcre2.cpp b/modules/extra/regex_pcre2.cpp index 03a93202b..02c573528 100644 --- a/modules/extra/regex_pcre2.cpp +++ b/modules/extra/regex_pcre2.cpp @@ -6,8 +6,14 @@ * Please read COPYING and README for further details. */ -/* RequiredLibraries: pcre2-8 */ -/* RequiredWindowsLibraries: pcre2-8 */ +/// BEGIN CMAKE +/// if(WIN32) +/// target_link_libraries(${SO} PRIVATE CONAN_PKG::pcre2) +/// else() +/// pkg_check_modules("PCRE2" IMPORTED_TARGET REQUIRED "libpcre2-8") +/// target_link_libraries(${SO} PRIVATE PkgConfig::PCRE2) +/// endif() +/// END CMAKE #include "module.h" diff --git a/modules/extra/regex_tre.cpp b/modules/extra/regex_tre.cpp index 1d11e1e09..eff30cecd 100644 --- a/modules/extra/regex_tre.cpp +++ b/modules/extra/regex_tre.cpp @@ -6,7 +6,10 @@ * Please read COPYING and README for further details. */ -/* RequiredLibraries: tre */ +/// BEGIN CMAKE +/// pkg_check_modules("TRE" IMPORTED_TARGET REQUIRED "tre") +/// target_link_libraries(${SO} PRIVATE PkgConfig::TRE) +/// END CMAKE #include "module.h" #include diff --git a/modules/extra/sqlite.cpp b/modules/extra/sqlite.cpp index 524ad9af1..985a415ed 100644 --- a/modules/extra/sqlite.cpp +++ b/modules/extra/sqlite.cpp @@ -6,8 +6,10 @@ * Please read COPYING and README for further details. */ -/* RequiredLibraries: sqlite3 */ -/* RequiredWindowsLibraries: sqlite3 */ +/// BEGIN CMAKE +/// find_package("SQLite3" REQUIRED) +/// target_link_libraries(${SO} PRIVATE SQLite::SQLite3) +/// END CMAKE #include "module.h" #include "modules/sql.h" diff --git a/modules/extra/ssl_gnutls.cpp b/modules/extra/ssl_gnutls.cpp index 0d77b8edc..5120b8830 100644 --- a/modules/extra/ssl_gnutls.cpp +++ b/modules/extra/ssl_gnutls.cpp @@ -7,8 +7,10 @@ * Please read COPYING and README for further details. */ -/* RequiredLibraries: gnutls */ -/* RequiredWindowsLibraries: libgnutls-30 */ +/// BEGIN CMAKE +/// find_package("GnuTLS" REQUIRED) +/// target_link_libraries(${SO} PRIVATE GnuTLS::GnuTLS) +/// END CMAKE #include "module.h" #include "modules/ssl.h" diff --git a/modules/extra/ssl_openssl.cpp b/modules/extra/ssl_openssl.cpp index 59db4dba3..2f07cfe5e 100644 --- a/modules/extra/ssl_openssl.cpp +++ b/modules/extra/ssl_openssl.cpp @@ -6,8 +6,10 @@ * Please read COPYING and README for further details. */ -/* RequiredLibraries: ssl,crypto */ -/* RequiredWindowsLibraries: libssl,libcrypto */ +/// BEGIN CMAKE +/// find_package("OpenSSL" REQUIRED) +/// target_link_libraries(${SO} PRIVATE OpenSSL::Crypto OpenSSL::SSL) +/// END CMAKE #include "module.h" #include "modules/ssl.h" diff --git a/modules/extra/xmlrpc.cpp b/modules/extra/xmlrpc.cpp index 0ea771698..f0288bc45 100644 --- a/modules/extra/xmlrpc.cpp +++ b/modules/extra/xmlrpc.cpp @@ -6,8 +6,11 @@ * Please read COPYING and README for further details. */ - -/* RequiredLibraries: xmlrpc */ +/// BEGIN CMAKE +/// find_library("XMLRPC" "xmlrpc" REQUIRED) +/// message(STATUS "Found XMLRPC: ${XMLRPC}") +/// target_link_libraries(${SO} PRIVATE ${XMLRPC}) +/// END CMAKE #include diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 79d10e39c..1c4d24c4d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -50,15 +50,15 @@ set_target_properties(${PROGRAM_NAME} PROPERTIES LINKER_LANGUAGE CXX LINK_FLAGS # On Windows, also link Anope to the wsock32 and Ws2_32 library, as well as set the version if(WIN32) - target_link_libraries(${PROGRAM_NAME} wsock32 Ws2_32 ${LINK_LIBS} ${WIN32_MEMORY}) + target_link_libraries(${PROGRAM_NAME} PUBLIC wsock32 Ws2_32 ${LINK_LIBS} ${WIN32_MEMORY}) set_target_properties(${PROGRAM_NAME} PROPERTIES VERSION "${VERSION_DOTTED}") else() - target_link_libraries(${PROGRAM_NAME} ${LINK_LIBS}) + target_link_libraries(${PROGRAM_NAME} PUBLIC ${LINK_LIBS}) endif() # If being built with localisation we might need to link against libintl. if(HAVE_LOCALIZATION AND Intl_LIBRARY) - target_link_libraries(${PROGRAM_NAME} ${Intl_LIBRARY}) + target_link_libraries(${PROGRAM_NAME} PUBLIC ${Intl_LIBRARY}) endif() # Building the Anope executable requires the version.h header to be generated