185 lines
7.3 KiB
CMake
185 lines
7.3 KiB
CMake
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()
|
|
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)
|
|
string(REGEX REPLACE "::.*" "" BASE_DEP ${ext_dep})
|
|
list(APPEND _module_external_deps ${BASE_DEP})
|
|
include(cmake/Dependencies.cmake)
|
|
void_fetch_dependency(${BASE_DEP})
|
|
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)
|
|
target_link_libraries(${TGT} ${SCOPE} ${ARG_EXTERNAL_DEPS})
|
|
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_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}
|
|
)
|
|
|
|
# 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}
|
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
|
|
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
|
|
)
|
|
|
|
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_<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()
|