Skip to content

Commit

Permalink
Build: Remove SDL requirement for packaging
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nickelpro committed Jun 21, 2023
1 parent b52e2ec commit e05f742
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 24 deletions.
47 changes: 28 additions & 19 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
$<IF:$<TARGET_EXISTS:SDL2::SDL2>,SDL2::SDL2,SDL2::SDL2-static>
$<IF:$<TARGET_EXISTS:SDL2_image::SDL2_image>,SDL2_image::SDL2_image,SDL2_image::SDL2_image-static>
$<IF:$<TARGET_EXISTS:SDL2_mixer::SDL2_mixer>,SDL2_mixer::SDL2_mixer,SDL2_mixer::SDL2_mixer-static>
$<IF:$<TARGET_EXISTS:SDL2_ttf::SDL2_ttf>,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
$<IF:$<TARGET_EXISTS:SDL2::SDL2>,SDL2::SDL2,SDL2::SDL2-static>
$<IF:$<TARGET_EXISTS:SDL2_image::SDL2_image>,SDL2_image::SDL2_image,SDL2_image::SDL2_image-static>
$<IF:$<TARGET_EXISTS:SDL2_mixer::SDL2_mixer>,SDL2_mixer::SDL2_mixer,SDL2_mixer::SDL2_mixer-static>
$<IF:$<TARGET_EXISTS:SDL2_ttf::SDL2_ttf>,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
Expand All @@ -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)
Expand Down Expand Up @@ -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::
Expand Down
8 changes: 4 additions & 4 deletions cmake/centurion-config.cmake
Original file line number Diff line number Diff line change
@@ -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")
36 changes: 35 additions & 1 deletion cmake/centurion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,38 @@ function(cen_set_basic_compiler_options target)
target_compile_options(${target} PRIVATE -Werror)
endif ()
endif ()
endfunction()
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()

0 comments on commit e05f742

Please sign in to comment.