From 5cc5d0effd255d2784f2c643d98cfcc13d879b22 Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Tue, 18 Nov 2025 01:41:11 +0000 Subject: [PATCH] Extract build_module from build_modules. --- modules/CMakeLists.txt | 108 +++++++++++++++++++++-------------------- 1 file changed, 56 insertions(+), 52 deletions(-) diff --git a/modules/CMakeLists.txt b/modules/CMakeLists.txt index 2ceaa9025..f92aada54 100644 --- a/modules/CMakeLists.txt +++ b/modules/CMakeLists.txt @@ -32,6 +32,61 @@ if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../conanbuildinfo.cmake") install(FILES ${EXTRA_DLLS} DESTINATION ${BIN_DIR}) endif() +macro(build_module SRC MODULE_SRC) + string(REGEX MATCH "\\.c$" ANOPE18MODULE ${MODULE_SRC}) + if(ANOPE18MODULE) + message(FATAL_ERROR "Anope 1 modules are not compatible with Anope 2!\nOffending module: ${MODULE_SRC}") + endif() + string(REGEX MATCH "\\.cpp$" CPP ${MODULE_SRC}) + if(CPP) + set_source_files_properties(${MODULE_SRC} PROPERTIES LANGUAGE CXX COMPILE_FLAGS "${CXXFLAGS}") + + 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}) + # 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) + else() + set(WIN32_MEMORY) + 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") + else() + set(WIN32_NO_LIBS) + endif() + set_target_properties(${SO} PROPERTIES + BUILD_WITH_INSTALL_RPATH ON + FOLDER "Modules" + INSTALL_RPATH_USE_LINK_PATH ON + LINKER_LANGUAGE CXX + LINK_FLAGS "${WIN32_NO_LIBS}" + PREFIX "" + SUFFIX "" + ) + add_dependencies(${SO} ${PROGRAM_NAME}) + if(HAVE_LOCALIZATION) + add_dependencies(${SO} module_language) + endif() + # 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} PUBLIC ${PROGRAM_NAME} wsock32 Ws2_32 ${WIN32_MEMORY}) + set_target_properties(${PROGRAM_NAME} PROPERTIES VERSION "${VERSION_DOTTED}") + elseif(APPLE) + 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} + DESTINATION ${MODULE_DIR} + LIBRARY) + endif() +endmacro() + macro(build_modules SRC) if(NOT ${SRC} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR} AND EXISTS "${SRC}/CMakeLists.txt") add_subdirectory("${SRC}") @@ -41,58 +96,7 @@ macro(build_modules SRC) if(IS_DIRECTORY "${MODULE_SRC}") build_modules("${MODULE_SRC}") else() - string(REGEX MATCH "\\.c$" ANOPE18MODULE ${MODULE_SRC}) - if(ANOPE18MODULE) - message(FATAL_ERROR "Anope 1 modules are not compatible with Anope 2!\nOffending module: ${MODULE_SRC}") - endif() - string(REGEX MATCH "\\.cpp$" CPP ${MODULE_SRC}) - if(CPP) - set_source_files_properties(${MODULE_SRC} PROPERTIES LANGUAGE CXX COMPILE_FLAGS "${CXXFLAGS}") - - 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}) - # 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) - else() - set(WIN32_MEMORY) - 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") - else() - set(WIN32_NO_LIBS) - endif() - set_target_properties(${SO} PROPERTIES - BUILD_WITH_INSTALL_RPATH ON - FOLDER "Modules" - INSTALL_RPATH_USE_LINK_PATH ON - LINKER_LANGUAGE CXX - LINK_FLAGS "${WIN32_NO_LIBS}" - PREFIX "" - SUFFIX "" - ) - add_dependencies(${SO} ${PROGRAM_NAME}) - if(HAVE_LOCALIZATION) - add_dependencies(${SO} module_language) - endif() - # 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} PUBLIC ${PROGRAM_NAME} wsock32 Ws2_32 ${WIN32_MEMORY}) - set_target_properties(${PROGRAM_NAME} PROPERTIES VERSION "${VERSION_DOTTED}") - elseif(APPLE) - 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} - DESTINATION ${MODULE_DIR} - LIBRARY) - endif() + build_module("${SRC}" "${MODULE_SRC}") endif() endforeach() endif()