Build the window backends as separate libraries

VoxelGLFW, VoxelWindowBase (the input state both backends share) and
VoxelWayland own their sources, dependencies and protocol generation,
while VoxelCoreSrc only sees the Window/Input interfaces and the
factories declared in detail/WindowBackends.hpp.

VOXELCORE_WAYLAND now only selects the subdirectory to add and the
library to link, and the wayland library defines the macro for whoever
links it, so linking the backend is what enables it. The protocol sources
are generated inside build/src/window/wayland, and missing dependencies
only turn the backend off instead of breaking the configuration.
This commit is contained in:
alexei-zebra 2026-10-10 14:15:40 +03:00
parent b558beeea3
commit aac0bfa44a
No known key found for this signature in database
GPG key ID: D19D450AC3D9A80E
3 changed files with 155 additions and 93 deletions

View file

@ -4,9 +4,12 @@ file(GLOB_RECURSE headers ${CMAKE_CURRENT_SOURCE_DIR}/*.hpp)
file(GLOB_RECURSE sources ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
list(REMOVE_ITEM sources ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)
file(GLOB_RECURSE wayland_sources
# The window backends are separate libraries: the engine only talks to the
# Window/Input interfaces and to the factories in detail/WindowBackends.hpp
file(GLOB_RECURSE backend_sources
${CMAKE_CURRENT_SOURCE_DIR}/window/detail/*.cpp
${CMAKE_CURRENT_SOURCE_DIR}/window/wayland/*.cpp)
list(REMOVE_ITEM sources ${wayland_sources})
list(REMOVE_ITEM sources ${backend_sources})
add_library(VoxelCoreSrc STATIC ${sources} ${headers})
@ -76,97 +79,8 @@ endif()
target_include_directories(VoxelCoreSrc PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
option(VOXELCORE_WAYLAND "Build the native Wayland window backend" ON)
if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND VOXELCORE_WAYLAND)
find_package(PkgConfig REQUIRED)
pkg_check_modules(wayland-client IMPORTED_TARGET wayland-client)
pkg_check_modules(xkbcommon IMPORTED_TARGET xkbcommon)
pkg_check_modules(egl IMPORTED_TARGET egl)
find_library(WAYLAND_EGL_LIBRARY NAMES wayland-egl libwayland-egl.so.1)
find_program(WAYLAND_SCANNER wayland-scanner)
pkg_check_modules(libsystemd IMPORTED_TARGET libsystemd)
if(NOT wayland-client_FOUND OR NOT xkbcommon_FOUND OR NOT egl_FOUND
OR NOT WAYLAND_EGL_LIBRARY OR NOT WAYLAND_SCANNER)
message(STATUS "Wayland backend is off: dependencies are not found")
else()
set(WAYLAND_PROTOCOLS ${CMAKE_CURRENT_SOURCE_DIR}/window/wayland/protocols)
set(WAYLAND_GENERATED ${CMAKE_CURRENT_BINARY_DIR}/wayland-protocols)
file(MAKE_DIRECTORY ${WAYLAND_GENERATED})
set(WAYLAND_SOURCES)
function(voxel_wayland_protocol XML NAME)
add_custom_command(
OUTPUT ${WAYLAND_GENERATED}/${NAME}-client-protocol.h
${WAYLAND_GENERATED}/${NAME}-protocol.c
COMMAND ${WAYLAND_SCANNER} client-header
${WAYLAND_PROTOCOLS}/${XML}
${WAYLAND_GENERATED}/${NAME}-client-protocol.h
COMMAND ${WAYLAND_SCANNER} private-code
${WAYLAND_PROTOCOLS}/${XML}
${WAYLAND_GENERATED}/${NAME}-protocol.c
DEPENDS ${WAYLAND_PROTOCOLS}/${XML}
VERBATIM)
set(WAYLAND_SOURCES ${WAYLAND_SOURCES}
${WAYLAND_GENERATED}/${NAME}-protocol.c
PARENT_SCOPE)
endfunction()
voxel_wayland_protocol(xdg-shell.xml xdg-shell)
voxel_wayland_protocol(cursor-shape-v1.xml cursor-shape-v1)
voxel_wayland_protocol(idle-inhibit-unstable-v1.xml
idle-inhibit-unstable-v1)
voxel_wayland_protocol(pointer-constraints-unstable-v1.xml
pointer-constraints-unstable-v1)
voxel_wayland_protocol(relative-pointer-unstable-v1.xml
relative-pointer-unstable-v1)
voxel_wayland_protocol(xdg-decoration-unstable-v1.xml
xdg-decoration-unstable-v1)
# tablet is referenced by cursor-shape-v1
voxel_wayland_protocol(tablet-unstable-v2.xml tablet-unstable-v2)
target_sources(
VoxelCoreSrc PRIVATE ${WAYLAND_SOURCES} ${wayland_sources})
target_include_directories(VoxelCoreSrc PRIVATE ${WAYLAND_GENERATED})
target_compile_definitions(VoxelCoreSrc PRIVATE VOXELCORE_WAYLAND=1)
if(libsystemd_FOUND)
target_compile_definitions(VoxelCoreSrc PUBLIC VOXELCORE_SDBUS=1)
target_link_libraries(VoxelCoreSrc PRIVATE PkgConfig::libsystemd)
message(STATUS "Wayland backend: sd-bus settings are on")
else()
message(STATUS "Wayland backend: sd-bus settings are off")
endif()
target_link_libraries(
VoxelCoreSrc
PRIVATE PkgConfig::wayland-client
PkgConfig::xkbcommon
PkgConfig::egl
${WAYLAND_EGL_LIBRARY})
message(STATUS "Wayland backend is on")
endif()
endif()
target_link_libraries(
VoxelCoreSrc
PRIVATE glfw
OpenGL::GL
GLEW::GLEW
ZLIB::ZLIB
PNG::PNG
CURL::libcurl
OpenSSL::Crypto
OpenAL::OpenAL
Vorbis::vorbis
Vorbis::vorbisfile
Freetype::Freetype
PUBLIC luajit::luajit
glm::glm # Need public for src/delegates.hpp, which including to
# main.cpp
)
target_compile_options(
VoxelCoreSrc
PUBLIC $<$<CXX_COMPILER_ID:MSVC>:
set(VOXEL_COMPILE_OPTIONS
$<$<CXX_COMPILER_ID:MSVC>:
/utf-8
/MP
/D_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR
@ -202,6 +116,38 @@ target_compile_options(
-Wduplicated-cond
>)
option(VOXELCORE_WAYLAND "Build the native Wayland window backend" ON)
add_subdirectory(window/detail)
if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND VOXELCORE_WAYLAND)
add_subdirectory(window/wayland)
endif()
target_link_libraries(
VoxelCoreSrc
PRIVATE VoxelGLFW
glfw
OpenGL::GL
GLEW::GLEW
ZLIB::ZLIB
PNG::PNG
CURL::libcurl
OpenSSL::Crypto
OpenAL::OpenAL
Vorbis::vorbis
Vorbis::vorbisfile
Freetype::Freetype
PUBLIC luajit::luajit
glm::glm # Need public for src/delegates.hpp, which including to
# main.cpp
)
if(TARGET VoxelWayland)
target_link_libraries(VoxelCoreSrc PRIVATE VoxelWayland)
endif()
target_compile_options(VoxelCoreSrc PUBLIC ${VOXEL_COMPILE_OPTIONS})
target_link_options(
VoxelCoreSrc
PUBLIC

View file

@ -0,0 +1,22 @@
project(VoxelWindowBackends)
# Input state shared by the window backends.
add_library(VoxelWindowBase STATIC BaseInput.cpp)
target_include_directories(VoxelWindowBase PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_compile_options(VoxelWindowBase PRIVATE ${VOXEL_COMPILE_OPTIONS})
add_library(VoxelGLFW STATIC GLFWWindow.cpp)
target_include_directories(VoxelGLFW PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_compile_options(VoxelGLFW PRIVATE ${VOXEL_COMPILE_OPTIONS})
target_link_libraries(
VoxelGLFW
PRIVATE VoxelWindowBase
glfw
OpenGL::GL
GLEW::GLEW
glm::glm)

View file

@ -0,0 +1,94 @@
project(VoxelWayland)
# The library is linked from the parent directory, so the imported targets of
# its dependencies have to be visible outside of this directory too.
function(voxel_wayland_pkg_config MODULE)
pkg_check_modules(${MODULE} IMPORTED_TARGET ${ARGN})
if(TARGET PkgConfig::${MODULE})
set_target_properties(
PkgConfig::${MODULE} PROPERTIES IMPORTED_GLOBAL TRUE)
endif()
endfunction()
macro(voxel_wayland_protocol XML NAME)
add_custom_command(
OUTPUT ${WAYLAND_GENERATED}/${NAME}-client-protocol.h
${WAYLAND_GENERATED}/${NAME}-protocol.c
COMMAND ${WAYLAND_SCANNER} client-header ${WAYLAND_PROTOCOLS}/${XML}
${WAYLAND_GENERATED}/${NAME}-client-protocol.h
COMMAND ${WAYLAND_SCANNER} private-code ${WAYLAND_PROTOCOLS}/${XML}
${WAYLAND_GENERATED}/${NAME}-protocol.c
DEPENDS ${WAYLAND_PROTOCOLS}/${XML}
VERBATIM)
list(APPEND WAYLAND_SOURCES ${WAYLAND_GENERATED}/${NAME}-protocol.c)
endmacro()
function(voxel_add_wayland_backend)
find_package(PkgConfig REQUIRED)
voxel_wayland_pkg_config(wayland-client wayland-client)
voxel_wayland_pkg_config(xkbcommon xkbcommon)
voxel_wayland_pkg_config(egl egl)
voxel_wayland_pkg_config(libsystemd libsystemd)
find_library(WAYLAND_EGL_LIBRARY NAMES wayland-egl libwayland-egl.so.1)
find_program(WAYLAND_SCANNER wayland-scanner)
if(NOT wayland-client_FOUND OR NOT xkbcommon_FOUND OR NOT egl_FOUND
OR NOT WAYLAND_EGL_LIBRARY OR NOT WAYLAND_SCANNER)
message(STATUS "Wayland backend is off: dependencies are not found")
return()
endif()
set(WAYLAND_PROTOCOLS ${CMAKE_CURRENT_SOURCE_DIR}/protocols)
set(WAYLAND_GENERATED ${CMAKE_CURRENT_BINARY_DIR}/protocols)
file(MAKE_DIRECTORY ${WAYLAND_GENERATED})
set(WAYLAND_SOURCES)
voxel_wayland_protocol(xdg-shell.xml xdg-shell)
voxel_wayland_protocol(cursor-shape-v1.xml cursor-shape-v1)
voxel_wayland_protocol(idle-inhibit-unstable-v1.xml
idle-inhibit-unstable-v1)
voxel_wayland_protocol(pointer-constraints-unstable-v1.xml
pointer-constraints-unstable-v1)
voxel_wayland_protocol(relative-pointer-unstable-v1.xml
relative-pointer-unstable-v1)
voxel_wayland_protocol(xdg-decoration-unstable-v1.xml
xdg-decoration-unstable-v1)
# tablet is referenced by cursor-shape-v1
voxel_wayland_protocol(tablet-unstable-v2.xml tablet-unstable-v2)
file(GLOB wayland_sources ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
add_library(VoxelWayland STATIC ${wayland_sources} ${WAYLAND_SOURCES})
target_include_directories(VoxelWayland
PRIVATE ${CMAKE_SOURCE_DIR}/src ${WAYLAND_GENERATED})
target_compile_options(VoxelWayland PRIVATE ${VOXEL_COMPILE_OPTIONS})
# Linking this library is what enables the backend in the engine code.
target_compile_definitions(VoxelWayland PUBLIC VOXELCORE_WAYLAND=1)
target_link_libraries(
VoxelWayland
PRIVATE VoxelWindowBase
PkgConfig::wayland-client
PkgConfig::xkbcommon
PkgConfig::egl
${WAYLAND_EGL_LIBRARY}
glfw
OpenGL::GL
GLEW::GLEW
glm::glm)
if(libsystemd_FOUND)
target_compile_definitions(VoxelWayland PRIVATE VOXELCORE_SDBUS=1)
target_link_libraries(VoxelWayland PRIVATE PkgConfig::libsystemd)
message(STATUS "Wayland backend: sd-bus settings are on")
else()
message(STATUS "Wayland backend: sd-bus settings are off")
endif()
message(STATUS "Wayland backend is on")
endfunction()
voxel_add_wayland_backend()