set_property(GLOBAL PROPERTY VOID_MODULES "") set_property(GLOBAL PROPERTY VOID_MODULE_DEPENDENCY_RECORDS "") set_property(GLOBAL PROPERTY VOID_PENDING_OPTIONAL_DEPS "") function(void_add_module MODULE_NAME) cmake_parse_arguments(ARG "" "" "INTERNAL_DEPS;EXTERNAL_DEPS;OPTIONAL_DEPS;DEPS_WIN32;DEPS_UNIX;OPTIONS_WIN32;OPTIONS_UNIX" ${ARGN}) set(TGT ${PROJECT_NAME}_${MODULE_NAME}) set(ALIAS ${PROJECT_NAME}::${MODULE_NAME}) file(GLOB_RECURSE SRC CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/${MODULE_NAME}/*.cpp") file(GLOB_RECURSE HDR CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/include/Void/${MODULE_NAME}/*.hpp") if(NOT SRC AND NOT HDR) return() endif() if(NOT SRC) set(LIB_TYPE "INTERFACE") set(SCOPE "INTERFACE") add_library(${TGT} INTERFACE) target_compile_features(${TGT} INTERFACE cxx_std_26) else() set(SCOPE "PUBLIC") if (MODULE_NAME STREQUAL "Testing") add_library(${TGT} STATIC) else() # Honour the standard BUILD_SHARED_LIBS policy. Consumers that ship a # single validated executable must be able to embed Void modules, # while Void's standalone default remains shared. add_library(${TGT}) endif() target_sources(${TGT} PRIVATE ${SRC}) target_compile_features(${TGT} PUBLIC cxx_std_26) endif() get_target_property(_module_target_type ${TGT} TYPE) if(_module_target_type STREQUAL "SHARED_LIBRARY" OR _module_target_type STREQUAL "MODULE_LIBRARY") # External packages are implementation details of compiled shared # modules: Void's installed public headers do not expose their types. # Keeping these links private also lets a shared Void SDK carry a # fetched runtime dependency without requiring that dependency's CMake # development package on every consumer machine. set(_external_scope PRIVATE) set(_record_external_dependencies FALSE) else() set(_external_scope ${SCOPE}) set(_record_external_dependencies TRUE) endif() add_library(${ALIAS} ALIAS ${TGT}) set_property(GLOBAL APPEND PROPERTY VOID_MODULES ${MODULE_NAME}) set(_module_external_deps "") foreach(ext_dep IN LISTS ARG_EXTERNAL_DEPS) # FindSQLite3's package name and imported-target namespace differ. # Preserve the package name in exported dependency metadata. if(ext_dep MATCHES "^SQLite::") set(BASE_DEP SQLite3) else() string(REGEX REPLACE "::.*" "" BASE_DEP ${ext_dep}) endif() if(_record_external_dependencies) list(APPEND _module_external_deps ${BASE_DEP}) endif() include(cmake/Dependencies.cmake) void_fetch_dependency(${BASE_DEP}) if(NOT TARGET ${ext_dep}) message(FATAL_ERROR "[Void] Dependency '${ext_dep}' required by module " "'${MODULE_NAME}' was not provided") endif() endforeach() set(_module_internal_deps ${ARG_INTERNAL_DEPS} ${ARG_OPTIONAL_DEPS}) if(_module_internal_deps) list(REMOVE_DUPLICATES _module_internal_deps) string(REPLACE ";" "," _module_internal_record "${_module_internal_deps}") else() set(_module_internal_record "-") endif() if(_module_external_deps) list(REMOVE_DUPLICATES _module_external_deps) string(REPLACE ";" "," _module_external_record "${_module_external_deps}") else() set(_module_external_record "-") endif() set_property(GLOBAL APPEND PROPERTY VOID_MODULE_DEPENDENCY_RECORDS "${MODULE_NAME}|${_module_internal_record}|${_module_external_record}") foreach(int_dep IN LISTS ARG_INTERNAL_DEPS) target_link_libraries(${TGT} ${SCOPE} ${PROJECT_NAME}::${int_dep}) endforeach() # Optional deps are resolved in void_finalize_modules() after every # module has been discovered, so resolution does not depend on the # alphabetical order in which src/ subdirectories are processed. if(ARG_OPTIONAL_DEPS) string(REPLACE ";" "," _opt_list "${ARG_OPTIONAL_DEPS}") set_property(GLOBAL APPEND PROPERTY VOID_PENDING_OPTIONAL_DEPS "${TGT}|${SCOPE}|${_opt_list}") endif() if(ARG_EXTERNAL_DEPS) if(_external_scope STREQUAL "PRIVATE") foreach(_external_dep IN LISTS ARG_EXTERNAL_DEPS) string(MAKE_C_IDENTIFIER "${_external_dep}" _external_dep_identifier) set(_build_external_target "_void_${MODULE_NAME}_${_external_dep_identifier}") add_library(${_build_external_target} INTERFACE IMPORTED) set_property(TARGET ${_build_external_target} PROPERTY INTERFACE_LINK_LIBRARIES "${_external_dep}") target_link_libraries(${TGT} PRIVATE "$") endforeach() else() # Keep fetched dependency build targets out of Void's install # export. Static and interface consumers still receive the same # canonical targets after find_dependency(). foreach(_external_dep IN LISTS ARG_EXTERNAL_DEPS) string(MAKE_C_IDENTIFIER "${_external_dep}" _external_dep_identifier) set(_build_external_target "_void_${MODULE_NAME}_${_external_dep_identifier}") add_library(${_build_external_target} INTERFACE IMPORTED) set_property(TARGET ${_build_external_target} PROPERTY INTERFACE_LINK_LIBRARIES "${_external_dep}") # Defer namespace reconstruction until a consumer evaluates # the installed target. Otherwise CMake canonicalizes aliases # from a FetchContent dependency (Boost::url -> boost_url) and # requires the third-party target in Void's export set. string(REPLACE "::" "$<1:::>" _install_external_dep "${_external_dep}") target_link_libraries(${TGT} ${_external_scope} "$" "$") endforeach() endif() endif() foreach(_lib IN LISTS ARG_DEPS_WIN32) target_link_libraries(${TGT} ${SCOPE} "$<$:${_lib}>") endforeach() foreach(_lib IN LISTS ARG_DEPS_UNIX) target_link_libraries(${TGT} ${SCOPE} "$<$>:${_lib}>") endforeach() if(SRC) foreach(_opt IN LISTS ARG_OPTIONS_WIN32) target_compile_options(${TGT} PRIVATE "$<$:${_opt}>") endforeach() foreach(_opt IN LISTS ARG_OPTIONS_UNIX) target_compile_options(${TGT} PRIVATE "$<$>:${_opt}>") endforeach() if(VOID_WARNINGS_AS_ERRORS) target_compile_options(${TGT} PRIVATE "$<$:-Wall;-Wextra;-Wpedantic;-Werror;-Wno-missing-field-initializers>" "$<$:-Wno-missing-designated-field-initializers>" "$<$:/W4;/WX>" ) endif() if(VOID_ENABLE_ASAN) target_compile_options(${TGT} PUBLIC "$<$:-fsanitize=address;-fno-omit-frame-pointer>" ) target_link_options(${TGT} PUBLIC "$<$:-fsanitize=address>" ) endif() if(VOID_ENABLE_UBSAN) target_compile_options(${TGT} PUBLIC "$<$:-fsanitize=undefined>" ) target_link_options(${TGT} PUBLIC "$<$:-fsanitize=undefined>" ) endif() if(VOID_ENABLE_COVERAGE) target_compile_options(${TGT} PUBLIC "$<$:--coverage;-fprofile-update=atomic>" "$<$:-fprofile-instr-generate;-fcoverage-mapping>" ) target_link_options(${TGT} PUBLIC "$<$:--coverage>" "$<$:-fprofile-instr-generate>" ) endif() endif() set_target_properties(${TGT} PROPERTIES CXX_STANDARD 26 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF EXPORT_NAME ${MODULE_NAME} ) if(UNIX AND _module_target_type STREQUAL "SHARED_LIBRARY") set_property(TARGET ${TGT} PROPERTY INSTALL_RPATH "$ORIGIN") endif() if(WIN32 AND _module_target_type STREQUAL "SHARED_LIBRARY") # Void's public headers do not require consumers to opt into a DLL # import macro. Generate the export table for compiled shared modules # so the same installed targets remain usable on Windows. set_property(TARGET ${TGT} PROPERTY WINDOWS_EXPORT_ALL_SYMBOLS ON) endif() # GCC 16+ exposes experimental C++26 reflection (P2996) via -freflection. # Apply it to every module unconditionally so any module is free to use # ^^T / std::meta::* without per-module wiring. if(SRC AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 16) target_compile_options(${TGT} PUBLIC -freflection) endif() target_sources(${TGT} ${SCOPE} FILE_SET HEADERS BASE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/include" FILES ${HDR}) target_include_directories(${TGT} ${SCOPE} $ $ ) install(TARGETS ${TGT} EXPORT ${EXPORT_SET_NAME} FILE_SET HEADERS DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" ) endfunction() # Resolve OPTIONAL_DEPS recorded by void_add_module(). Must be called once # after all modules have been discovered. Each present dep is linked and # defines VOID_HAS_ on the dependent target. function(void_finalize_modules) get_property(_modules GLOBAL PROPERTY VOID_MODULES) get_property(_pending GLOBAL PROPERTY VOID_PENDING_OPTIONAL_DEPS) foreach(_entry IN LISTS _pending) string(REPLACE "|" ";" _parts "${_entry}") list(GET _parts 0 _tgt) list(GET _parts 1 _scope) list(GET _parts 2 _deps_packed) string(REPLACE "," ";" _deps "${_deps_packed}") foreach(_dep IN LISTS _deps) if(_dep IN_LIST _modules) target_link_libraries(${_tgt} ${_scope} ${PROJECT_NAME}::${_dep}) string(TOUPPER ${_dep} _upper) target_compile_definitions(${_tgt} ${_scope} VOID_HAS_${_upper}) endif() endforeach() endforeach() endfunction()