Simplify sfml_find_package

All in service of hopefully removing sfml_find_package one day since
that will simplify packaging SFML and better set us up for adopting
a package manager.
This commit is contained in:
Chris Thrasher 2023-08-14 22:41:30 -06:00
parent 62c376ffa5
commit af7d6c2589
4 changed files with 14 additions and 29 deletions

View File

@ -381,32 +381,17 @@ endfunction()
# Find the requested package and make an INTERFACE library from it
# The created INTERFACE library is tagged for export to be part of the generated SFMLConfig
# Usage: sfml_find_package(wanted_target_name
# [INCLUDE "OPENGL_INCLUDE_DIR"]
# [LINK "OPENGL_gl_LIBRARY"])
function(sfml_find_package)
list(GET ARGN 0 target)
list(REMOVE_AT ARGN 0)
# Usage: sfml_find_package(wanted_target_name [OPENGL_INCLUDE_DIR] [OPENGL_gl_LIBRARY])
function(sfml_find_package target INCLUDE_DIRS LINK_LIBRARIES)
if(TARGET ${target})
message(FATAL_ERROR "Target '${target}' is already defined")
endif()
cmake_parse_arguments(THIS "" "" "INCLUDE;LINK" ${ARGN})
if(THIS_UNPARSED_ARGUMENTS)
message(FATAL_ERROR "Unknown arguments when calling sfml_find_package: ${THIS_UNPARSED_ARGUMENTS}")
endif()
find_package(${target} REQUIRED)
add_library(${target} INTERFACE)
foreach(include_dir IN LISTS THIS_INCLUDE)
target_include_directories(${target} SYSTEM INTERFACE "$<BUILD_INTERFACE:${${include_dir}}>")
endforeach()
foreach(link_item IN LISTS THIS_LINK)
target_link_libraries(${target} INTERFACE "$<BUILD_INTERFACE:${${link_item}}>")
endforeach()
target_include_directories(${target} SYSTEM INTERFACE "$<BUILD_INTERFACE:${${INCLUDE_DIRS}}>")
target_link_libraries(${target} INTERFACE "$<BUILD_INTERFACE:${${LINK_LIBRARIES}}>")
install(TARGETS ${target} EXPORT SFMLConfigExport)
endfunction()

View File

@ -68,9 +68,9 @@ elseif(SFML_OS_ANDROID)
endif()
# find external libraries
sfml_find_package(OpenAL INCLUDE "OPENAL_INCLUDE_DIR" LINK "OPENAL_LIBRARY")
sfml_find_package(VORBIS INCLUDE "VORBIS_INCLUDE_DIRS" LINK "VORBIS_LIBRARIES")
sfml_find_package(FLAC INCLUDE "FLAC_INCLUDE_DIR" LINK "FLAC_LIBRARY")
sfml_find_package(OpenAL OPENAL_INCLUDE_DIR OPENAL_LIBRARY)
sfml_find_package(VORBIS VORBIS_INCLUDE_DIRS VORBIS_LIBRARIES)
sfml_find_package(FLAC FLAC_INCLUDE_DIR FLAC_LIBRARY)
# avoids warnings in vorbisfile.h
target_compile_definitions(VORBIS INTERFACE "OV_EXCLUDE_STATIC_CALLBACKS")

View File

@ -117,7 +117,7 @@ elseif(SFML_OS_IOS)
target_link_libraries(sfml-graphics PRIVATE z bz2)
endif()
sfml_find_package(Freetype INCLUDE "FREETYPE_INCLUDE_DIRS" LINK "FREETYPE_LIBRARY")
sfml_find_package(Freetype FREETYPE_INCLUDE_DIRS FREETYPE_LIBRARY)
target_link_libraries(sfml-graphics PRIVATE Freetype)
# on some platforms (e.g. Raspberry Pi 3 armhf), GCC requires linking libatomic to use <atomic> features

View File

@ -278,9 +278,9 @@ sfml_add_library(Window
# DRM libraries
if(SFML_OS_LINUX OR SFML_OS_FREEBSD OR SFML_OS_OPENBSD OR SFML_OS_NETBSD)
if(SFML_USE_DRM)
sfml_find_package(DRM INCLUDE "DRM_INCLUDE_DIR" LINK "DRM_LIBRARY")
sfml_find_package(DRM DRM_INCLUDE_DIR DRM_LIBRARY)
target_include_directories(sfml-window PRIVATE ${DRM_INCLUDE_DIR}/libdrm)
sfml_find_package(GBM INCLUDE "GBM_INCLUDE_DIR" LINK "GBM_LIBRARY")
sfml_find_package(GBM GBM_INCLUDE_DIR GBM_LIBRARY)
target_link_libraries(sfml-window PRIVATE drm gbm EGL)
else()
find_package(X11 REQUIRED COMPONENTS Xrandr Xcursor)
@ -311,12 +311,12 @@ endif()
if(SFML_OS_IOS)
target_link_libraries(sfml-window PRIVATE "-framework OpenGLES")
elseif(SFML_OS_ANDROID)
sfml_find_package(GLES INCLUDE "GLES_INCLUDE_DIR" LINK "GLES_LIBRARY")
sfml_find_package(EGL INCLUDE "EGL_INCLUDE_DIR" LINK "EGL_LIBRARY")
sfml_find_package(GLES GLES_INCLUDE_DIR GLES_LIBRARY)
sfml_find_package(EGL EGL_INCLUDE_DIR EGL_LIBRARY)
target_link_libraries(sfml-window PRIVATE EGL)
target_link_libraries(sfml-window PRIVATE GLES)
else()
sfml_find_package(OpenGL INCLUDE "OPENGL_INCLUDE_DIR" LINK "OPENGL_gl_LIBRARY")
sfml_find_package(OpenGL OPENGL_INCLUDE_DIR OPENGL_gl_LIBRARY)
target_link_libraries(sfml-window PRIVATE OpenGL)
endif()
@ -329,7 +329,7 @@ if(SFML_OS_WINDOWS AND NOT SFML_COMPILER_MSVC)
endif()
if(SFML_OS_LINUX)
sfml_find_package(UDev INCLUDE "UDEV_INCLUDE_DIR" LINK "UDEV_LIBRARIES")
sfml_find_package(UDev UDEV_INCLUDE_DIR UDEV_LIBRARIES)
target_link_libraries(sfml-window PRIVATE UDev dl)
elseif(SFML_OS_WINDOWS)
target_link_libraries(sfml-window PRIVATE winmm gdi32)