From f0e72be28518590ccfb0d98a65eab2898b93a07f Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Tue, 15 Aug 2023 01:39:53 -0600 Subject: [PATCH] Prefer using `IMPORTED` targets where possible My goal is to reduce our need on sfml_find_package until it can finally be removed. It's preferred to simply use find_package to find 3rd party projects. I had to add IMPORTED targets to some of our find modules so that we could get away from using INTERFACE libraries for external code. That also implied that our find moduels need to be installed so that users have access to them when processing SFML's config module. --- CMakeLists.txt | 3 +++ cmake/Modules/FindDRM.cmake | 5 +++++ cmake/Modules/FindEGL.cmake | 5 +++++ cmake/Modules/FindGBM.cmake | 5 +++++ cmake/Modules/FindGLES.cmake | 5 +++++ cmake/Modules/FindUDev.cmake | 8 ++++++++ cmake/SFMLConfigDependencies.cmake.in | 17 ++++++++++------- src/SFML/Window/CMakeLists.txt | 22 ++++++++++------------ 8 files changed, 51 insertions(+), 19 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 05d19c28..9902cb4d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -259,6 +259,9 @@ if(NOT SFML_BUILD_FRAMEWORKS) DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} COMPONENT devel FILES_MATCHING PATTERN "*.hpp" PATTERN "*.inl") + install(DIRECTORY cmake/Modules/ + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/SFML + COMPONENT devel) if(SFML_GENERATE_PDB) install(DIRECTORY ${PROJECT_BINARY_DIR}/lib/ diff --git a/cmake/Modules/FindDRM.cmake b/cmake/Modules/FindDRM.cmake index 23b38b11..547e9d51 100644 --- a/cmake/Modules/FindDRM.cmake +++ b/cmake/Modules/FindDRM.cmake @@ -16,3 +16,8 @@ find_library(DRM_LIBRARY NAMES drm) include(FindPackageHandleStandardArgs) find_package_handle_standard_args(DRM DEFAULT_MSG DRM_LIBRARY DRM_INCLUDE_DIR) + +add_library(DRM::DRM IMPORTED UNKNOWN) +set_target_properties(DRM::DRM PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES ${DRM_INCLUDE_DIR}/libdrm + IMPORTED_LOCATION ${DRM_LIBRARY}) diff --git a/cmake/Modules/FindEGL.cmake b/cmake/Modules/FindEGL.cmake index cde632ae..3488642c 100644 --- a/cmake/Modules/FindEGL.cmake +++ b/cmake/Modules/FindEGL.cmake @@ -12,3 +12,8 @@ find_library(EGL_LIBRARY NAMES EGL) include(FindPackageHandleStandardArgs) find_package_handle_standard_args(EGL DEFAULT_MSG EGL_LIBRARY EGL_INCLUDE_DIR) + +add_library(EGL::EGL IMPORTED UNKNOWN) +set_target_properties(EGL::EGL PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES ${EGL_INCLUDE_DIR} + IMPORTED_LOCATION ${EGL_LIBRARY}) diff --git a/cmake/Modules/FindGBM.cmake b/cmake/Modules/FindGBM.cmake index 4deb5eb6..138d8f43 100644 --- a/cmake/Modules/FindGBM.cmake +++ b/cmake/Modules/FindGBM.cmake @@ -16,3 +16,8 @@ find_library(GBM_LIBRARY NAMES gbm) include(FindPackageHandleStandardArgs) find_package_handle_standard_args(GBM DEFAULT_MSG GBM_LIBRARY GBM_INCLUDE_DIR) + +add_library(GBM::GBM IMPORTED UNKNOWN) +set_target_properties(GBM::GBM PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES ${GBM_INCLUDE_DIR} + IMPORTED_LOCATION ${GBM_LIBRARY}) diff --git a/cmake/Modules/FindGLES.cmake b/cmake/Modules/FindGLES.cmake index f34f2cd0..ec12b987 100644 --- a/cmake/Modules/FindGLES.cmake +++ b/cmake/Modules/FindGLES.cmake @@ -12,3 +12,8 @@ find_library(GLES_LIBRARY NAMES GLESv1_CM) include(FindPackageHandleStandardArgs) find_package_handle_standard_args(GLES DEFAULT_MSG GLES_LIBRARY GLES_INCLUDE_DIR) + +add_library(GLES::GLES IMPORTED UNKNOWN) +set_target_properties(GLES::GLES PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES ${GLES_INCLUDE_DIR} + IMPORTED_LOCATION ${GLES_LIBRARY}) diff --git a/cmake/Modules/FindUDev.cmake b/cmake/Modules/FindUDev.cmake index 57f02460..d4fb2133 100644 --- a/cmake/Modules/FindUDev.cmake +++ b/cmake/Modules/FindUDev.cmake @@ -50,4 +50,12 @@ else() endif() endif() +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(UDev DEFAULT_MSG UDEV_INCLUDE_DIR UDEV_LIBRARIES) + mark_as_advanced(UDEV_INCLUDE_DIR UDEV_LIBRARIES) + +add_library(UDev::UDev IMPORTED UNKNOWN) +set_target_properties(UDev::UDev PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES ${UDEV_INCLUDE_DIR} + IMPORTED_LOCATION ${UDEV_LIBRARIES}) diff --git a/cmake/SFMLConfigDependencies.cmake.in b/cmake/SFMLConfigDependencies.cmake.in index 532ffe2f..2666d195 100644 --- a/cmake/SFMLConfigDependencies.cmake.in +++ b/cmake/SFMLConfigDependencies.cmake.in @@ -1,5 +1,7 @@ # in case of static linking, we must also define the list of all the dependencies of SFML libraries if(SFML_STATIC_LIBRARIES) + list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}) + include(CMakeFindDependencyMacro) # detect the OS @@ -44,20 +46,21 @@ if(SFML_STATIC_LIBRARIES) list(FIND SFML_FIND_COMPONENTS "Window" FIND_SFML_WINDOW_COMPONENT_INDEX) if(FIND_SFML_WINDOW_COMPONENT_INDEX GREATER -1) if(FIND_SFML_USE_DRM) - sfml_bind_dependency(TARGET DRM FRIENDLY_NAME "drm" SEARCH_NAMES "drm") - sfml_bind_dependency(TARGET GBM FRIENDLY_NAME "gbm" SEARCH_NAMES "gbm") + find_dependency(DRM) + find_dependency(GBM) elseif(FIND_SFML_OS_LINUX OR FIND_SFML_OS_FREEBSD) find_dependency(X11 REQUIRED COMPONENTS Xrandr Xcursor) endif() if(FIND_SFML_OS_LINUX) - sfml_bind_dependency(TARGET UDev FRIENDLY_NAME "UDev" SEARCH_NAMES "udev" "libudev") + find_dependency(UDev) endif() - if(FIND_SFML_OS_WINDOWS) - set_property(TARGET OpenGL APPEND PROPERTY INTERFACE_LINK_LIBRARIES "OpenGL32") - elseif(NOT FIND_SFML_OS_IOS) - sfml_bind_dependency(TARGET OpenGL FRIENDLY_NAME "OpenGL" SEARCH_NAMES "OpenGL" "GL") + if(NOT FIND_SFML_OS_IOS) + if(NOT OpenGL_GL_PREFERENCE) + set(OpenGL_GL_PREFERENCE "LEGACY") + endif() + find_dependency(OpenGL COMPONENTS OpenGL) endif() endif() diff --git a/src/SFML/Window/CMakeLists.txt b/src/SFML/Window/CMakeLists.txt index 97656ebb..d91b1913 100644 --- a/src/SFML/Window/CMakeLists.txt +++ b/src/SFML/Window/CMakeLists.txt @@ -278,10 +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 DRM_INCLUDE_DIR DRM_LIBRARY) - target_include_directories(sfml-window PRIVATE ${DRM_INCLUDE_DIR}/libdrm) - sfml_find_package(GBM GBM_INCLUDE_DIR GBM_LIBRARY) - target_link_libraries(sfml-window PRIVATE drm gbm EGL) + find_package(DRM REQUIRED) + find_package(GBM REQUIRED) + target_link_libraries(sfml-window PRIVATE DRM::DRM GBM::GBM EGL) else() find_package(X11 REQUIRED COMPONENTS Xrandr Xcursor) target_link_libraries(sfml-window PRIVATE X11::X11 X11::Xrandr X11::Xcursor) @@ -311,13 +310,12 @@ endif() if(SFML_OS_IOS) target_link_libraries(sfml-window PRIVATE "-framework OpenGLES") elseif(SFML_OS_ANDROID) - 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) + find_package(EGL REQUIRED) + find_package(GLES REQUIRED) + target_link_libraries(sfml-window PRIVATE EGL::EGL GLES::GLES) else() - sfml_find_package(OpenGL OPENGL_INCLUDE_DIR OPENGL_gl_LIBRARY) - target_link_libraries(sfml-window PRIVATE OpenGL) + find_package(OpenGL REQUIRED COMPONENTS OpenGL) + target_link_libraries(sfml-window PRIVATE OpenGL::GL) endif() if(SFML_OS_WINDOWS AND NOT SFML_COMPILER_MSVC) @@ -329,8 +327,8 @@ if(SFML_OS_WINDOWS AND NOT SFML_COMPILER_MSVC) endif() if(SFML_OS_LINUX) - sfml_find_package(UDev UDEV_INCLUDE_DIR UDEV_LIBRARIES) - target_link_libraries(sfml-window PRIVATE UDev dl) + find_package(UDev REQUIRED) + target_link_libraries(sfml-window PRIVATE UDev::UDev dl) elseif(SFML_OS_WINDOWS) target_link_libraries(sfml-window PRIVATE winmm gdi32) elseif(SFML_OS_FREEBSD)