1
0
mirror of https://github.com/anope/anope.git synced 2026-06-12 19:14:47 +02:00

Replace calculate_libraries with inline CMake code.

This commit is contained in:
Sadie Powell
2025-10-05 16:03:59 +01:00
parent 0b3e55ed8f
commit 1a5d49b7f6
14 changed files with 89 additions and 139 deletions
+2 -1
View File
@@ -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
+26 -84
View File
@@ -1,87 +1,3 @@
###############################################################################
# calculate_libraries(<source filename> <output variable for linker flags> <output variable for extra depends>)
#
# 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(<item> [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()
+7 -33
View File
@@ -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
+8 -2
View File
@@ -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 <climits>
#include <random>
-2
View File
@@ -8,8 +8,6 @@
*
*/
/* RequiredLibraries: crypt */
#include "module.h"
class EPOSIX final
+6 -1
View File
@@ -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"
+8 -2
View File
@@ -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"
+8 -2
View File
@@ -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"
+4 -1
View File
@@ -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 <tre/regex.h>
+4 -2
View File
@@ -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"
+4 -2
View File
@@ -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"
+4 -2
View File
@@ -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"
+5 -2
View File
@@ -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 <xmlrpc-c/base.h>
+3 -3
View File
@@ -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