cmake: create SDL3::headers for include path + no exported CMake variables

This commit is contained in:
Anonymous Maarten 2023-01-21 00:55:44 +01:00 committed by Anonymous Maarten
parent 5690dfb65b
commit 93c25e650c
7 changed files with 102 additions and 85 deletions

View File

@ -3075,9 +3075,32 @@ endif()
# Disable precompiled headers on SDL_dynapi.c to avoid applying dynapi overrides
set_source_files_properties(src/dynapi/SDL_dynapi.c PROPERTIES SKIP_PRECOMPILE_HEADERS 1)
add_library(SDL3_headers INTERFACE)
add_library(SDL3::headers ALIAS SDL3_headers)
set_target_properties(SDL3_headers PROPERTIES
EXPORT_NAME "headers"
)
target_include_directories(SDL3_headers
INTERFACE
"$<BUILD_INTERFACE:${SDL3_BINARY_DIR}/include>"
"$<BUILD_INTERFACE:${SDL3_SOURCE_DIR}/include>"
"$<BUILD_INTERFACE:${SDL3_SOURCE_DIR}/include/SDL3>"
)
if(SDL_FRAMEWORK)
target_include_directories(SDL3_headers
INTERFACE
"$<INSTALL_INTERFACE:SDL3.framework/Headers>"
)
else()
target_include_directories(SDL3_headers
INTERFACE
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/SDL3>"
)
endif()
if(SDL_SHARED)
add_library(SDL3 SHARED ${SOURCE_FILES} ${VERSION_SOURCES})
# alias target for in-tree builds
add_library(SDL3::SDL3 ALIAS SDL3)
set_target_properties(SDL3 PROPERTIES
POSITION_INDEPENDENT_CODE TRUE
@ -3132,13 +3155,8 @@ if(SDL_SHARED)
PRIVATE
"$<BUILD_INTERFACE:${SDL3_BINARY_DIR}/include-config-$<LOWER_CASE:$<CONFIG>>>"
"$<BUILD_INTERFACE:${SDL3_SOURCE_DIR}/src>"
PUBLIC
"$<BUILD_INTERFACE:${SDL3_BINARY_DIR}/include>"
"$<BUILD_INTERFACE:${SDL3_SOURCE_DIR}/include>"
"$<BUILD_INTERFACE:${SDL3_SOURCE_DIR}/include/SDL3>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/SDL3>"
)
target_link_libraries(SDL3 PUBLIC $<TARGET_NAME:SDL3::headers>)
# This picks up all the compiler options and such we've accumulated up to here.
target_link_libraries(SDL3 PRIVATE $<BUILD_INTERFACE:sdl-build-options>)
target_link_libraries(SDL3 PRIVATE $<BUILD_INTERFACE:sdl-shared-build-options>)
@ -3158,7 +3176,6 @@ endif()
if(SDL_STATIC)
add_library(SDL3-static STATIC ${SOURCE_FILES})
# alias target for in-tree builds
add_library(SDL3::SDL3-static ALIAS SDL3-static)
set_target_properties(SDL3-static PROPERTIES
OUTPUT_NAME "${sdl_static_libname}"
@ -3170,13 +3187,8 @@ if(SDL_STATIC)
PRIVATE
"$<BUILD_INTERFACE:${SDL3_BINARY_DIR}/include-config-$<LOWER_CASE:$<CONFIG>>>"
"$<BUILD_INTERFACE:${SDL3_SOURCE_DIR}/src>"
PUBLIC
"$<BUILD_INTERFACE:${SDL3_BINARY_DIR}/include>"
"$<BUILD_INTERFACE:${SDL3_SOURCE_DIR}/include>"
"$<BUILD_INTERFACE:${SDL3_SOURCE_DIR}/include/SDL3>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/SDL3>"
)
target_link_libraries(SDL3-static PUBLIC $<TARGET_NAME:SDL3::headers>)
# This picks up all the compiler options and such we've accumulated up to here.
target_link_libraries(SDL3-static PRIVATE $<BUILD_INTERFACE:sdl-build-options>)
target_link_libraries(SDL3-static PRIVATE $<BUILD_INTERFACE:sdl-global-options>)
@ -3206,13 +3218,8 @@ if(SDL_TEST)
target_include_directories(SDL3_test
PRIVATE
"$<BUILD_INTERFACE:${SDL3_BINARY_DIR}/include-config-$<LOWER_CASE:$<CONFIG>>>"
PUBLIC
"$<BUILD_INTERFACE:${SDL3_BINARY_DIR}/include>"
"$<BUILD_INTERFACE:${SDL3_SOURCE_DIR}/include>"
"$<BUILD_INTERFACE:${SDL3_SOURCE_DIR}/include/SDL3>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/SDL3>"
)
target_link_libraries(SDL3_test PUBLIC $<TARGET_NAME:SDL3::headers>)
target_link_libraries(SDL3_test PRIVATE ${EXTRA_TEST_LIBS})
endif()
@ -3244,6 +3251,8 @@ if(NOT SDL_DISABLE_INSTALL)
##### Installation targets #####
install(TARGETS SDL3_headers EXPORT SDL3headersTargets)
if(SDL_SHARED)
install(TARGETS SDL3 EXPORT SDL3Targets
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
@ -3313,6 +3322,12 @@ if(NOT SDL_DISABLE_INSTALL)
COMPATIBILITY AnyNewerVersion
)
install(EXPORT SDL3headersTargets
FILE SDL3headersTargets.cmake
NAMESPACE SDL3::
DESTINATION "${SDL_INSTALL_CMAKEDIR}"
)
if(SDL_SHARED)
install(EXPORT SDL3Targets
FILE SDL3Targets.cmake

View File

@ -39,14 +39,9 @@ else()
return()
endif()
# For compatibility with autotools sdl3-config.cmake, provide SDL3_* variables.
set_and_check(SDL3_PREFIX "${CMAKE_CURRENT_LIST_DIR}/..")
set_and_check(SDL3_EXEC_PREFIX "${CMAKE_CURRENT_LIST_DIR}/..")
set_and_check(SDL3_INCLUDE_DIR "${SDL3_PREFIX}/include")
set(SDL3_INCLUDE_DIRS "${SDL3_INCLUDE_DIR};${SDL3_INCLUDE_DIR}/SDL3")
set_and_check(SDL3_BINDIR "${SDL3_PREFIX}/lib/${_sdl_arch_subdir}")
set_and_check(SDL3_LIBDIR "${SDL3_PREFIX}/lib/${_sdl_arch_subdir}")
set_and_check(_sdl3_prefix "${CMAKE_CURRENT_LIST_DIR}/..")
set(_sdl3_include_dirs "${_sdl3_prefix}/include;${_sdl3_prefix}/include/SDL3")
unset(_sdl3_prefix)
set(SDL3_LIBRARIES SDL3::SDL3)
set(SDL3TEST_LIBRARY SDL3::SDL3_test)
@ -55,6 +50,16 @@ set(SDL3TEST_LIBRARY SDL3::SDL3_test)
# All targets are created, even when some might not be requested though COMPONENTS.
# This is done for compatibility with CMake generated SDL3-target.cmake files.
if(NOT TARGET SDL3::headers)
add_library(SDL3::headers INTERFACE IMPORTED)
set_target_properties(SDL3::SDL3
PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${_sdl3_include_dirs}"
)
endif()
set(SDL3_headers_FOUND TRUE)
unset(_sdl3_include_dirs)
set(_sdl3_library "${SDL3_LIBDIR}/SDL3.lib")
set(_sdl3_dll_library "${SDL3_BINDIR}/SDL3.dll")
if(EXISTS "${_sdl3_library}" AND EXISTS "${_sdl3_dll_library}")
@ -62,7 +67,7 @@ if(EXISTS "${_sdl3_library}" AND EXISTS "${_sdl3_dll_library}")
add_library(SDL3::SDL3 SHARED IMPORTED)
set_target_properties(SDL3::SDL3
PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${SDL3_INCLUDE_DIRS}"
INTERFACE_LINK_LIBRARIES "SDL3::headers"
IMPORTED_IMPLIB "${_sdl3_library}"
IMPORTED_LOCATION "${_sdl3_dll_library}"
COMPATIBLE_INTERFACE_BOOL "SDL3_SHARED"
@ -82,7 +87,7 @@ if(EXISTS "${_sdl3test_library}")
add_library(SDL3::SDL3_test STATIC IMPORTED)
set_target_properties(SDL3::SDL3_test
PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${SDL3_INCLUDE_DIRS}"
INTERFACE_LINK_LIBRARIES "SDL3::headers"
IMPORTED_LOCATION "${_sdl3test_library}"
)
endif()

View File

@ -31,34 +31,45 @@ endmacro()
set(SDL3_FOUND TRUE)
string(REGEX REPLACE "SDL3\\.framework.*" "SDL3.framework" SDL3_FRAMEWORK_PATH "${CMAKE_CURRENT_LIST_DIR}")
string(REGEX REPLACE "SDL3\\.framework.*" "" SDL3_FRAMEWORK_PARENT_PATH "${CMAKE_CURRENT_LIST_DIR}")
# Compute the installation prefix relative to this file.
get_filename_component(_sdl3_framework_path "${CMAKE_CURRENT_LIST_FILE}" PATH) # /SDL3.framework/Resources/CMake/
get_filename_component(_sdl3_framework_path "${_IMPORT_PREFIX}" PATH) # /SDL3.framework/Resources/
get_filename_component(_sdl3_framework_path "${_IMPORT_PREFIX}" PATH) # /SDL3.framework/
get_filename_component(_sdl3_framework_parent_path "${_sdl3_framework_path}" PATH) # /
# For compatibility with autotools sdl3-config.cmake, provide SDL3_* variables.
set_and_check(SDL3_PREFIX "${SDL3_FRAMEWORK_PATH}")
set_and_check(SDL3_EXEC_PREFIX "${SDL3_FRAMEWORK_PATH}")
set_and_check(SDL3_INCLUDE_DIR "${SDL3_FRAMEWORK_PATH}/Headers")
set(SDL3_INCLUDE_DIRS "${SDL3_INCLUDE_DIR}")
set_and_check(SDL3_BINDIR "${SDL3_FRAMEWORK_PATH}")
set_and_check(SDL3_LIBDIR "${SDL3_FRAMEWORK_PATH}")
set_and_check(_sdl3_include_dirs "${_sdl3_framework_path}/Headers")
set(SDL3_LIBRARIES "SDL3::SDL3")
# All targets are created, even when some might not be requested though COMPONENTS.
# This is done for compatibility with CMake generated SDL3-target.cmake files.
if(NOT TARGET SDL3::headers)
add_library(SDL3::headers INTERFACE IMPORTED)
set_target_properties(SDL3::headers
PROPERTIES
INTERFACE_COMPILE_OPTIONS "SHELL:-F \"${_sdl3_framework_parent_path}\""
INTERFACE_INCLUDE_DIRECTORIES "${_sdl3_include_dirs}"
)
endif()
set(SDL3_headers_FOUND TRUE)
unset(_sdl3_include_dirs)
if(NOT TARGET SDL3::SDL3)
add_library(SDL3::SDL3 INTERFACE IMPORTED)
add_library(SDL3::SDL3 SHARED IMPORTED)
set_target_properties(SDL3::SDL3
PROPERTIES
INTERFACE_COMPILE_OPTIONS "SHELL:-F \"${SDL3_FRAMEWORK_PARENT_PATH}\""
INTERFACE_INCLUDE_DIRECTORIES "${SDL3_INCLUDE_DIRS}"
INTERFACE_LINK_OPTIONS "SHELL:-F \"${SDL3_FRAMEWORK_PARENT_PATH}\";SHELL:-framework SDL3"
FRAMEWORK "TRUE"
INTERFACE_LINK_LIBRARIES "SDL3::headers"
IMPORTED_LOCATION "${_sdl3_framework_path}/SDL3"
IMPORTED_SONAME "${_sdl3_framework_path}/SDL3"
COMPATIBLE_INTERFACE_BOOL "SDL3_SHARED"
INTERFACE_SDL3_SHARED "ON"
)
endif()
set(SDL3_SDL3_FOUND TRUE)
unset(_sdl3_framework_parent_path)
unset(_sdl3_framework_path)
check_required_components(SDL3)

View File

@ -10,6 +10,13 @@ set_package_properties(SDL3 PROPERTIES
set(SDL3_FOUND TRUE)
# Find SDL3::headers
if(NOT TARGET SDL3::headers)
include("${CMAKE_CURRENT_LIST_DIR}/SDL3headersTargets.cmake")
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)
@ -49,14 +56,6 @@ if(TARGET SDL3::SDL3-static AND NOT TARGET SDL3::SDL3)
endif()
endif()
# For compatibility with autotools sdl3-config.cmake, provide SDL3_* variables.
set(SDL3_PREFIX "@PACKAGE_CMAKE_INSTALL_PREFIX@")
set(SDL3_EXEC_PREFIX "@PACKAGE_CMAKE_INSTALL_PREFIX@")
set(SDL3_INCLUDE_DIR "@PACKAGE_CMAKE_INSTALL_FULL_INCLUDEDIR@")
set(SDL3_INCLUDE_DIRS "${SDL3_INCLUDE_DIR}")
set(SDL3_BINDIR "@PACKAGE_CMAKE_INSTALL_FULL_BINDIR@")
set(SDL3_LIBDIR "@PACKAGE_CMAKE_INSTALL_FULL_LIBDIR@")
set(SDL3_LIBRARIES SDL3::SDL3)
set(SDL3_STATIC_LIBRARIES SDL3::SDL3-static)
set(SDL3_STATIC_PRIVATE_LIBS)

View File

@ -27,6 +27,10 @@ add_feature_info("TEST_SHARED" TEST_SHARED "Test linking with shared library")
option(TEST_STATIC "Test linking to static SDL3 library" ON)
add_feature_info("TEST_STATIC" TEST_STATIC "Test linking with static library")
find_package(SDL3 REQUIRED CONFIG COMPONENTS headers)
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)
add_executable(gui-shared WIN32 main_gui.c)
@ -43,10 +47,6 @@ if(TEST_SHARED)
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(gui-shared-vars WIN32 main_gui.c)
target_link_libraries(gui-shared-vars PRIVATE ${SDL3_LIBRARIES})
target_include_directories(gui-shared-vars PRIVATE ${SDL3_INCLUDE_DIRS})
add_executable(cli-shared main_cli.c)
target_link_libraries(cli-shared PRIVATE SDL3::SDL3)
if(WIN32)
@ -54,17 +54,6 @@ if(TEST_SHARED)
COMMAND ${CMAKE_COMMAND} -E copy_if_different "$<TARGET_FILE:SDL3::SDL3>" "$<TARGET_FILE_DIR:cli-shared>"
)
endif()
add_executable(cli-shared-vars main_cli.c)
target_link_libraries(cli-shared-vars PRIVATE ${SDL3_LIBRARIES})
target_include_directories(cli-shared-vars PRIVATE ${SDL3_INCLUDE_DIRS})
add_library(sharedlib-shared-vars SHARED main_lib.c)
target_link_libraries(sharedlib-shared-vars PRIVATE ${SDL3_LIBRARIES})
target_include_directories(sharedlib-shared-vars PRIVATE ${SDL3_INCLUDE_DIRS})
generate_export_header(sharedlib-shared-vars EXPORT_MACRO_NAME MYLIBRARY_EXPORT)
target_compile_definitions(sharedlib-shared-vars PRIVATE "EXPORT_HEADER=\"${CMAKE_CURRENT_BINARY_DIR}/sharedlib-shared-vars_export.h\"")
set_target_properties(sharedlib-shared-vars PROPERTIES C_VISIBILITY_PRESET "hidden")
endif()
if(TEST_STATIC)
@ -81,24 +70,6 @@ if(TEST_STATIC)
set_target_properties(sharedlib-static PROPERTIES C_VISIBILITY_PRESET "hidden")
endif()
add_executable(gui-static-vars WIN32 main_gui.c)
target_link_libraries(gui-static-vars PRIVATE ${SDL3_STATIC_LIBRARIES})
target_include_directories(gui-static-vars PRIVATE ${SDL3_INCLUDE_DIRS})
add_executable(cli-static main_cli.c)
target_link_libraries(cli-static PRIVATE SDL3::SDL3-static)
add_executable(cli-static-vars main_cli.c)
target_link_libraries(cli-static-vars PRIVATE ${SDL3_STATIC_LIBRARIES})
target_include_directories(cli-static-vars PRIVATE ${SDL3_INCLUDE_DIRS})
endif()
message(STATUS "SDL3_PREFIX: ${SDL3_PREFIX}")
message(STATUS "SDL3_INCLUDE_DIR: ${SDL3_INCLUDE_DIR}")
message(STATUS "SDL3_INCLUDE_DIRS: ${SDL3_INCLUDE_DIRS}")
message(STATUS "SDL3_LIBRARIES: ${SDL3_LIBRARIES}")
message(STATUS "SDL3_STATIC_LIBRARIES: ${SDL3_STATIC_LIBRARIES}")
message(STATUS "SDL3TEST_LIBRARY: ${SDL3TEST_LIBRARY}")
feature_summary(WHAT ALL)

View File

@ -0,0 +1,8 @@
#include "SDL.h"
#include "SDL_main.h"
void inc_sdl_noslash(void) {
SDL_SetMainReady();
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Quit();
}

View File

@ -0,0 +1,8 @@
#include "SDL3/SDL.h"
#include "SDL3/SDL_main.h"
void inc_sdl_slash(void) {
SDL_SetMainReady();
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Quit();
}