cmake: allow find_package from build directory (requires CMake 3.26)

main
Anonymous Maarten 2023-02-04 23:20:03 +01:00 committed by Sam Lantinga
parent b8adf90aa3
commit ff82341ce9
2 changed files with 105 additions and 80 deletions

View File

@ -78,6 +78,12 @@ if (LIBC_IS_GLIBC AND CMAKE_SIZEOF_VOID_P EQUAL 4)
target_compile_definitions(sdl-build-options INTERFACE "_FILE_OFFSET_BITS=64") target_compile_definitions(sdl-build-options INTERFACE "_FILE_OFFSET_BITS=64")
endif() endif()
if(CMAKE_VERSION VERSION_LESS "3.26")
set(build_local_interface "BUILD_INTERFACE")
else()
set(build_local_interface "BUILD_LOCAL_INTERFACE")
endif()
# Increment this if there is an incompatible change - but if that happens, # Increment this if there is an incompatible change - but if that happens,
# we should rename the library from SDL3 to SDL4, at which point this would # we should rename the library from SDL3 to SDL4, at which point this would
# reset to 0 anyway. # reset to 0 anyway.
@ -3231,9 +3237,9 @@ if(SDL_SHARED)
) )
target_link_libraries(SDL3 PUBLIC $<TARGET_NAME:SDL3::Headers>) target_link_libraries(SDL3 PUBLIC $<TARGET_NAME:SDL3::Headers>)
# This picks up all the compiler options and such we've accumulated up to here. # 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_local_interface}:sdl-build-options>)
target_link_libraries(SDL3 PRIVATE $<BUILD_INTERFACE:sdl-shared-build-options>) target_link_libraries(SDL3 PRIVATE $<${build_local_interface}:sdl-shared-build-options>)
target_link_libraries(SDL3 PRIVATE $<BUILD_INTERFACE:sdl-global-options>) target_link_libraries(SDL3 PRIVATE $<${build_local_interface}:sdl-global-options>)
if(MINGW OR CYGWIN) if(MINGW OR CYGWIN)
if(NOT CMAKE_VERSION VERSION_LESS "3.13") if(NOT CMAKE_VERSION VERSION_LESS "3.13")
target_link_options(SDL3 PRIVATE -static-libgcc) target_link_options(SDL3 PRIVATE -static-libgcc)
@ -3275,8 +3281,8 @@ if(SDL_STATIC)
) )
target_link_libraries(SDL3-static PUBLIC $<TARGET_NAME:SDL3::Headers>) 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. # 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_local_interface}:sdl-build-options>)
target_link_libraries(SDL3-static PRIVATE $<BUILD_INTERFACE:sdl-global-options>) target_link_libraries(SDL3-static PRIVATE $<${build_local_interface}:sdl-global-options>)
# Use `Compatible Interface Properties` to allow consumers to enforce a shared/static library # Use `Compatible Interface Properties` to allow consumers to enforce a shared/static library
set_property(TARGET SDL3-static PROPERTY INTERFACE_SDL3_SHARED FALSE) set_property(TARGET SDL3-static PROPERTY INTERFACE_SDL3_SHARED FALSE)
set_property(TARGET SDL3-static APPEND PROPERTY COMPATIBLE_INTERFACE_BOOL SDL3_SHARED) set_property(TARGET SDL3-static APPEND PROPERTY COMPATIBLE_INTERFACE_BOOL SDL3_SHARED)
@ -3297,7 +3303,7 @@ if(SDL_TEST)
file(GLOB TEST_SOURCES ${SDL3_SOURCE_DIR}/src/test/*.c) file(GLOB TEST_SOURCES ${SDL3_SOURCE_DIR}/src/test/*.c)
add_library(SDL3_test STATIC ${TEST_SOURCES}) add_library(SDL3_test STATIC ${TEST_SOURCES})
add_library(SDL3::SDL3_test ALIAS SDL3_test) add_library(SDL3::SDL3_test ALIAS SDL3_test)
target_link_libraries(SDL3_test PRIVATE $<BUILD_INTERFACE:sdl-global-options>) target_link_libraries(SDL3_test PRIVATE $<${build_local_interface}:sdl-global-options>)
set_target_properties(SDL3_test PROPERTIES set_target_properties(SDL3_test PROPERTIES
EXPORT_NAME SDL3_test) EXPORT_NAME SDL3_test)
if(APPLE) if(APPLE)
@ -3320,8 +3326,6 @@ if(SDL_TEST)
target_link_libraries(SDL3_test PRIVATE ${EXTRA_TEST_LIBS}) target_link_libraries(SDL3_test PRIVATE ${EXTRA_TEST_LIBS})
endif() endif()
if(NOT SDL_DISABLE_INSTALL)
##### Configure installation folders ##### ##### Configure installation folders #####
if(WINDOWS AND NOT MINGW) if(WINDOWS AND NOT MINGW)
@ -3382,6 +3386,37 @@ if(NOT SDL_DISABLE_INSTALL)
set(SDL_SDLtest_INSTALL_CMAKEFILENAME "SDL3testTargets.cmake") set(SDL_SDLtest_INSTALL_CMAKEFILENAME "SDL3testTargets.cmake")
endif() endif()
if(CMAKE_VERSION VERSION_LESS "3.26")
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/SDL3Targets.cmake" [[message(FATAL_ERROR "find_package(SDL3) using the SDL3 build directory not supported for CMake versions older then 3.26.")]])
else()
export(TARGETS SDL3_Headers NAMESPACE "SDL3::" FILE "SDL3headersTargets.cmake")
if(SDL_SHARED)
export(TARGETS SDL3 NAMESPACE "SDL3::" FILE "SDL3Targets.cmake")
endif()
if(SDL_STATIC)
export(TARGETS SDL3-static NAMESPACE "SDL3::" FILE "SDL3staticTargets.cmake")
endif()
if(SDL_TEST)
export(TARGETS SDL3_test NAMESPACE "SDL3::" FILE "SDL3testTargets.cmake")
endif()
endif()
configure_file("cmake/sdlfind.cmake" "sdlfind.cmake" COPYONLY)
include(CMakePackageConfigHelpers)
configure_package_config_file(cmake/SDL3Config.cmake.in SDL3Config.cmake
PATH_VARS CMAKE_INSTALL_PREFIX
INSTALL_DESTINATION "${SDL_SDL_INSTALL_CMAKEDIR}"
)
write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/SDL3ConfigVersion.cmake"
COMPATIBILITY AnyNewerVersion
)
if(NOT SDL_DISABLE_INSTALL)
##### sdl3.pc ##### ##### sdl3.pc #####
# Clean up variables for sdl3.pc # Clean up variables for sdl3.pc
@ -3452,16 +3487,7 @@ if(NOT SDL_DISABLE_INSTALL)
) )
endif() endif()
##### CMake Export files ##### ##### Install CMake Targets #####
include(CMakePackageConfigHelpers)
configure_package_config_file(cmake/SDL3Config.cmake.in SDL3Config.cmake
PATH_VARS CMAKE_INSTALL_PREFIX
INSTALL_DESTINATION "${SDL_SDL_INSTALL_CMAKEDIR}"
)
write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/SDL3ConfigVersion.cmake"
COMPATIBILITY AnyNewerVersion
)
install(EXPORT SDL3headersTargets install(EXPORT SDL3headersTargets
FILE "SDL3headersTargets.cmake" FILE "SDL3headersTargets.cmake"

View File

@ -1,9 +1,8 @@
macro(sdlFindALSA) macro(sdlFindALSA)
find_package(ALSA MODULE) find_package(ALSA MODULE)
if(ALSA_FOUND AND (NOT TARGET ALSA::ALSA) ) if(ALSA_FOUND AND NOT TARGET ALSA::ALSA)
add_Library(ALSA::ALSA UNKNOWN IMPORTED) add_Library(ALSA::ALSA UNKNOWN IMPORTED)
set_property(TARGET ALSA::ALSA PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${ALSA_INCLUDE_DIRS}) set_property(TARGET ALSA::ALSA PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${ALSA_INCLUDE_DIRS}")
set_property(TARGET ALSA::ALSA APPEND PROPERTY IMPORTED_LOCATION ${ALSA_LIBRARY}) set_property(TARGET ALSA::ALSA APPEND PROPERTY IMPORTED_LOCATION "${ALSA_LIBRARY}")
endif() endif()
endmacro() endmacro()