From e05f7429d5dc54ea1228e127857c2d6d0d2c0d41 Mon Sep 17 00:00:00 2001 From: Vito Gamberini Date: Wed, 21 Jun 2023 15:33:18 -0400 Subject: [PATCH] Build: Remove SDL requirement for packaging This allows the "headers-only" target to be installed even if SDL wasn't found during configuration, enabling a "bring your own SDL" style of development for devs who vendor specific SDL versions or binaries in their codebase. --- CMakeLists.txt | 47 +++++++++++++++++++++--------------- cmake/centurion-config.cmake | 8 +++--- cmake/centurion.cmake | 36 ++++++++++++++++++++++++++- 3 files changed, 67 insertions(+), 24 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e060642d1..a2e453d3e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,29 +38,36 @@ set(CENTURION_TEST_TARGET centurion-tests) set(CENTURION_MOCK_TARGET centurion-mocks) # System dependencies -find_package(SDL2 CONFIG REQUIRED) -find_package(SDL2_image CONFIG REQUIRED) -find_package(SDL2_mixer CONFIG REQUIRED) -find_package(SDL2_ttf CONFIG REQUIRED) +find_package(SDL2 CONFIG) +find_package(SDL2_image CONFIG) +find_package(SDL2_mixer CONFIG) +find_package(SDL2_ttf CONFIG) # CMake Targets add_library(centurion-headers-only INTERFACE) add_library(centurion::centurion-headers-only ALIAS centurion-headers-only) +set(CEN_TARGETS centurion-headers-only) add_subdirectory("${CEN_SOURCE_DIR}") -add_library(centurion INTERFACE) -add_library(centurion::centurion ALIAS centurion) -target_link_libraries(centurion INTERFACE - centurion-headers-only - $,SDL2::SDL2,SDL2::SDL2-static> - $,SDL2_image::SDL2_image,SDL2_image::SDL2_image-static> - $,SDL2_mixer::SDL2_mixer,SDL2_mixer::SDL2_mixer-static> - $,SDL2_ttf::SDL2_ttf,SDL2_ttf::SDL2_ttf-static> -) +cen_has_SDL(SDL_SHARED SDL_STATIC) + +if (SDL_SHARED OR SDL_STATIC) + add_library(centurion INTERFACE) + add_library(centurion::centurion ALIAS centurion) + list(APPEND CEN_TARGETS centurion) + target_link_libraries(centurion INTERFACE + centurion-headers-only + $,SDL2::SDL2,SDL2::SDL2-static> + $,SDL2_image::SDL2_image,SDL2_image::SDL2_image-static> + $,SDL2_mixer::SDL2_mixer,SDL2_mixer::SDL2_mixer-static> + $,SDL2_ttf::SDL2_ttf,SDL2_ttf::SDL2_ttf-static> + ) +endif () -if (TARGET SDL2::SDL2-static) +if (SDL_STATIC) add_library(centurion-static INTERFACE) add_library(centurion::centurion-static ALIAS centurion-static) + list(APPEND CEN_TARGETS centurion-static) target_link_libraries(centurion-static INTERFACE centurion-headers-only SDL2::SDL2-static @@ -70,6 +77,12 @@ if (TARGET SDL2::SDL2-static) ) endif () +if (BUILD_TESTS OR BUILD_EXAMPLES) + if (NOT TARGET centurion::centurion) + message(FATAL_ERROR "Unable to find required SDL libraries") + endif() +endif () + if (BUILD_TESTS) # Vcpkg test dependencies find_package(GTest CONFIG REQUIRED) @@ -111,15 +124,11 @@ install(FILES ) install( - TARGETS centurion-headers-only centurion + TARGETS ${CEN_TARGETS} EXPORT centurionTargets FILE_SET HEADERS ) -if(TARGET centurion-static) - install(TARGETS centurion-static EXPORT centurionTargets) -endif() - install( EXPORT centurionTargets NAMESPACE centurion:: diff --git a/cmake/centurion-config.cmake b/cmake/centurion-config.cmake index 9564c2133..4ed69d198 100644 --- a/cmake/centurion-config.cmake +++ b/cmake/centurion-config.cmake @@ -1,6 +1,6 @@ -find_package(SDL2 CONFIG REQUIRED) -find_package(SDL2_image CONFIG REQUIRED) -find_package(SDL2_mixer CONFIG REQUIRED) -find_package(SDL2_ttf CONFIG REQUIRED) +find_package(SDL2 CONFIG) +find_package(SDL2_image CONFIG) +find_package(SDL2_mixer CONFIG) +find_package(SDL2_ttf CONFIG) include("${CMAKE_CURRENT_LIST_DIR}/centurionTargets.cmake") diff --git a/cmake/centurion.cmake b/cmake/centurion.cmake index 56831ac30..21aa3cad3 100644 --- a/cmake/centurion.cmake +++ b/cmake/centurion.cmake @@ -52,4 +52,38 @@ function(cen_set_basic_compiler_options target) target_compile_options(${target} PRIVATE -Werror) endif () endif () -endfunction() \ No newline at end of file +endfunction() + +# Checks if targets exists and stores the result in a variable. +# var: the name of the result variable +# ARGN: list of targets to check the existence of +function(cen_has_targets var) + set(${var} FALSE PARENT_SCOPE) + foreach(target IN LISTS ARGN) + if (NOT TARGET ${target}) + return() + endif () + endforeach () + set(${var} TRUE PARENT_SCOPE) +endfunction() + +# Checks if the relevant SDL targets exist and stores the result in the +# corresponding variable +# shared: the name of the shared libraries variable +# static: the name of the static libraries variable +function(cen_has_SDL shared static) + cen_has_targets(_shared + SDL2::SDL2 + SDL2_image::SDL2_image + SDL2_mixer::SDL2_mixer + SDL2_ttf::SDL2_ttf + ) + set(${shared} ${_shared} PARENT_SCOPE) + cen_has_targets(_static + SDL2::SDL2-static + SDL2_image::SDL2_image-static + SDL2_mixer::SDL2_mixer-static + SDL2_ttf::SDL2_ttf-static + ) + set(${static} ${_static} PARENT_SCOPE) +endfunction()