Files
Library/cmake/Module.cmake
T

294 lines
13 KiB
CMake

set_property(GLOBAL PROPERTY VOID_MODULES "")
set_property(GLOBAL PROPERTY VOID_MODULE_DEPENDENCY_RECORDS "")
set_property(GLOBAL PROPERTY VOID_PENDING_OPTIONAL_DEPS "")
set_property(GLOBAL PROPERTY VOID_ACTIVE_PUBLIC_HEADERS "")
function(void_add_module MODULE_NAME)
cmake_parse_arguments(ARG "" ""
"INTERNAL_DEPS;EXTERNAL_DEPS;OPTIONAL_DEPS;DEPS_WIN32;DEPS_UNIX;OPTIONS_WIN32;OPTIONS_UNIX;EXTRA_HEADERS;EXCLUDE_HEADERS"
${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"
"${CMAKE_CURRENT_SOURCE_DIR}/include/Void/${MODULE_NAME}/*.h")
foreach(_excl IN LISTS ARG_EXCLUDE_HEADERS)
list(REMOVE_ITEM HDR "${CMAKE_CURRENT_SOURCE_DIR}/include/${_excl}")
endforeach()
foreach(_extra IN LISTS ARG_EXTRA_HEADERS)
list(APPEND HDR "${CMAKE_CURRENT_SOURCE_DIR}/include/${_extra}")
endforeach()
if(HDR)
list(REMOVE_DUPLICATES HDR)
endif()
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})
foreach(_hdr IN LISTS HDR)
file(RELATIVE_PATH _rel_hdr "${CMAKE_CURRENT_SOURCE_DIR}/include" "${_hdr}")
set_property(GLOBAL APPEND PROPERTY VOID_ACTIVE_PUBLIC_HEADERS "${_rel_hdr}")
endforeach()
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
"$<BUILD_INTERFACE:${_build_external_target}>")
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}
"$<BUILD_INTERFACE:${_build_external_target}>"
"$<INSTALL_INTERFACE:${_install_external_dep}>")
endforeach()
endif()
endif()
foreach(_lib IN LISTS ARG_DEPS_WIN32)
target_link_libraries(${TGT} ${SCOPE} "$<$<PLATFORM_ID:Windows>:${_lib}>")
endforeach()
foreach(_lib IN LISTS ARG_DEPS_UNIX)
target_link_libraries(${TGT} ${SCOPE} "$<$<NOT:$<PLATFORM_ID:Windows>>:${_lib}>")
endforeach()
if(SRC)
foreach(_opt IN LISTS ARG_OPTIONS_WIN32)
target_compile_options(${TGT} PRIVATE "$<$<PLATFORM_ID:Windows>:${_opt}>")
endforeach()
foreach(_opt IN LISTS ARG_OPTIONS_UNIX)
target_compile_options(${TGT} PRIVATE "$<$<NOT:$<PLATFORM_ID:Windows>>:${_opt}>")
endforeach()
if(VOID_WARNINGS_AS_ERRORS)
target_compile_options(${TGT} PRIVATE
"$<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:-Wall;-Wextra;-Wpedantic;-Werror;-Wno-missing-field-initializers>"
"$<$<CXX_COMPILER_ID:Clang,AppleClang>:-Wno-missing-designated-field-initializers>"
"$<$<CXX_COMPILER_ID:MSVC>:/W4;/WX>"
)
endif()
if(VOID_ENABLE_ASAN)
target_compile_options(${TGT} PUBLIC
"$<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:-fsanitize=address;-fno-omit-frame-pointer>"
)
target_link_options(${TGT} PUBLIC
"$<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:-fsanitize=address>"
)
endif()
if(VOID_ENABLE_UBSAN)
target_compile_options(${TGT} PUBLIC
"$<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:-fsanitize=undefined>"
)
target_link_options(${TGT} PUBLIC
"$<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:-fsanitize=undefined>"
)
endif()
if(VOID_ENABLE_TSAN)
target_compile_options(${TGT} PUBLIC
"$<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:-fsanitize=thread>"
)
target_link_options(${TGT} PUBLIC
"$<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:-fsanitize=thread>"
)
endif()
if(VOID_ENABLE_COVERAGE)
target_compile_options(${TGT} PUBLIC
"$<$<CXX_COMPILER_ID:GNU>:--coverage;-fprofile-update=atomic>"
"$<$<CXX_COMPILER_ID:Clang,AppleClang>:-fprofile-instr-generate;-fcoverage-mapping>"
)
target_link_options(${TGT} PUBLIC
"$<$<CXX_COMPILER_ID:GNU>:--coverage>"
"$<$<CXX_COMPILER_ID:Clang,AppleClang>:-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_target_properties(${TGT} PROPERTIES
INSTALL_RPATH "$ORIGIN"
INSTALL_RPATH_USE_LINK_PATH TRUE
)
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.
# Propagate it on every module (including INTERFACE modules like Core and
# Data) so standalone installed consumers receive the required flag.
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 16)
target_compile_options(${TGT} ${SCOPE}
"$<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CXX_COMPILER_ID:GNU>,$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,16>>:-freflection>")
endif()
if(HDR)
target_sources(${TGT} ${SCOPE} FILE_SET HEADERS BASE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/include" FILES ${HDR})
endif()
target_include_directories(${TGT} ${SCOPE}
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
if(HDR)
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}"
)
else()
install(TARGETS ${TGT} EXPORT ${EXPORT_SET_NAME}
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
)
endif()
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_<UPPERCASE_DEP> 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()