cmake: SDL3-shared target will always be a shared target

This commit is contained in:
Anonymous Maarten
2023-02-16 23:30:20 +01:00
committed by Anonymous Maarten
parent 6ae1578691
commit dc138ee3d4
6 changed files with 83 additions and 70 deletions

View File

@@ -17,10 +17,10 @@ if(NOT TARGET SDL3::Headers)
endif()
set(SDL3_Headers_FOUND TRUE)
# Find SDL3::SDL3
if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/SDL3Targets.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/SDL3Targets.cmake")
set(SDL3_SDL3_FOUND TRUE)
# Find SDL3::SDL3-shared
if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/SDL3sharedTargets.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/SDL3sharedTargets.cmake")
set(SDL3_SDL3-shared_FOUND TRUE)
endif()
# Find SDL3::SDL3-static
@@ -39,6 +39,10 @@ else()
endif()
endif()
if(SDL3_SDL3-shared_FOUND OR SDL3_SDL3-static_FOUND)
set(SDL3_SDL3_FOUND TRUE)
endif()
# Find SDL3::SDL3_test
if(_sdl3_framework)
find_package(SDL3_test CONFIG)
@@ -65,14 +69,22 @@ unset(SDL_ALSA_SHARED)
check_required_components(SDL3)
# Create SDL3::SDL3 alias for static-only builds
if(TARGET SDL3::SDL3-static AND NOT TARGET SDL3::SDL3)
function(_sdl_create_target_alias_compat NEW_TARGET TARGET)
if(CMAKE_VERSION VERSION_LESS "3.18")
# FIXME: Aliasing local targets is not supported on CMake < 3.18, so make it global.
add_library(SDL3::SDL3 INTERFACE IMPORTED)
set_target_properties(SDL3::SDL3 PROPERTIES INTERFACE_LINK_LIBRARIES "SDL3::SDL3-static")
add_library(${NEW_TARGET} INTERFACE IMPORTED)
set_target_properties(${NEW_TARGET} PROPERTIES INTERFACE_LINK_LIBRARIES "${TARGET}")
else()
add_library(SDL3::SDL3 ALIAS SDL3::SDL3-static)
add_library(${NEW_TARGET} ALIAS ${TARGET})
endif()
endfunction()
# Make sure SDL3::SDL3 always exists
if(NOT TARGET SDL3::SDL3)
if(TARGET SDL3::SDL3-shared)
_sdl_create_target_alias_compat(SDL3::SDL3 SDL3::SDL3-shared)
else()
_sdl_create_target_alias_compat(SDL3::SDL3 SDL3::SDL3-static)
endif()
endif()

View File

@@ -35,32 +35,32 @@ add_library(headers_test OBJECT inc_sdl_slash.c inc_sdl_noslash.c)
target_link_libraries(headers_test PRIVATE SDL3::Headers)
if(TEST_SHARED)
find_package(SDL3 REQUIRED CONFIG COMPONENTS SDL3)
find_package(SDL3 REQUIRED CONFIG COMPONENTS SDL3-shared)
add_executable(gui-shared WIN32 main_gui.c)
target_link_libraries(gui-shared PRIVATE SDL3::SDL3)
target_link_libraries(gui-shared PRIVATE SDL3::SDL3-shared)
if(WIN32)
add_custom_command(TARGET gui-shared POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different "$<TARGET_FILE:SDL3::SDL3>" "$<TARGET_FILE_DIR:gui-shared>"
COMMAND ${CMAKE_COMMAND} -E copy_if_different "$<TARGET_FILE:SDL3::SDL3-shared>" "$<TARGET_FILE_DIR:gui-shared>"
)
endif()
add_library(sharedlib-shared SHARED main_lib.c)
target_link_libraries(sharedlib-shared PRIVATE SDL3::SDL3)
target_link_libraries(sharedlib-shared PRIVATE SDL3::SDL3-shared)
generate_export_header(sharedlib-shared EXPORT_MACRO_NAME MYLIBRARY_EXPORT)
target_compile_definitions(sharedlib-shared PRIVATE "EXPORT_HEADER=\"${CMAKE_CURRENT_BINARY_DIR}/sharedlib-shared_export.h\"")
set_target_properties(sharedlib-shared PROPERTIES C_VISIBILITY_PRESET "hidden")
add_executable(cli-shared main_cli.c)
target_link_libraries(cli-shared PRIVATE SDL3::SDL3)
target_link_libraries(cli-shared PRIVATE SDL3::SDL3-shared)
if(WIN32)
add_custom_command(TARGET cli-shared POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different "$<TARGET_FILE:SDL3::SDL3>" "$<TARGET_FILE_DIR:cli-shared>"
COMMAND ${CMAKE_COMMAND} -E copy_if_different "$<TARGET_FILE:SDL3::SDL3-shared>" "$<TARGET_FILE_DIR:cli-shared>"
)
endif()
if(TEST_TEST)
add_executable(sdltest-shared sdltest.c)
target_link_libraries(sdltest-shared PRIVATE SDL3::SDL3_test SDL3::SDL3)
target_link_libraries(sdltest-shared PRIVATE SDL3::SDL3_test SDL3::SDL3-shared)
endif()
endif()
@@ -84,4 +84,8 @@ if(TEST_STATIC)
endif()
endif()
find_package(SDL3 REQUIRED CONFIG COMPONENTS SDL3)
add_executable(gui-whatever WIN32 main_gui.c)
target_link_libraries(gui-whatever PRIVATE SDL3::SDL3)
feature_summary(WHAT ALL)