From bc3153d7673ba003a474c1c441a354e6e810a073 Mon Sep 17 00:00:00 2001 From: Brecht Van Lommel Date: Tue, 14 Nov 2023 15:05:44 +0100 Subject: [PATCH 1/3] Support cmake config and shared library for libdeflate When not using the internal deflate, the only option was pkgconfig which is not well supported on Windows. This adds support for using the libdeflate cmake config files when available. This change also affects the generated OpenEXR cmake config files. For a static OpenEXR library, INTERFACE_LINK_LIBRARIES now appropriately includes libdeflate along with the existing dependencies like Imath. It also turns the internal libdeflate into a target for consistency. Resolves #1588 Signed-off-by: Brecht Van Lommel --- cmake/LibraryDefine.cmake | 3 -- cmake/OpenEXRSetup.cmake | 58 ++++++++++++++++++++---------- src/lib/OpenEXRCore/CMakeLists.txt | 6 ++-- 3 files changed, 43 insertions(+), 24 deletions(-) diff --git a/cmake/LibraryDefine.cmake b/cmake/LibraryDefine.cmake index a242ec8519..0e2dbf6c17 100644 --- a/cmake/LibraryDefine.cmake +++ b/cmake/LibraryDefine.cmake @@ -27,9 +27,6 @@ function(OPENEXR_DEFINE_LIBRARY libname) PRIVATE cxx_std_${OPENEXR_CXX_STANDARD} INTERFACE cxx_std_11 ) - # we are embedding libdeflate - target_include_directories(${objlib} PRIVATE ${EXR_DEFLATE_INCLUDE_DIR}) - if(OPENEXR_CURLIB_PRIV_EXPORT AND BUILD_SHARED_LIBS) target_compile_definitions(${objlib} PRIVATE ${OPENEXR_CURLIB_PRIV_EXPORT}) if(WIN32) diff --git a/cmake/OpenEXRSetup.cmake b/cmake/OpenEXRSetup.cmake index 7d01362c84..3a446cd157 100644 --- a/cmake/OpenEXRSetup.cmake +++ b/cmake/OpenEXRSetup.cmake @@ -160,15 +160,45 @@ set(OPENEXR_DEFLATE_TAG "v1.18" CACHE STRING "Tag to use for libdeflate source r if(NOT OPENEXR_FORCE_INTERNAL_DEFLATE) #TODO: ^^ Release should not clone from main, this is a place holder set(CMAKE_IGNORE_PATH "${CMAKE_CURRENT_BINARY_DIR}/_deps/deflate-src/config;${CMAKE_CURRENT_BINARY_DIR}/_deps/deflate-build/config") - include(FindPkgConfig) - pkg_check_modules(deflate IMPORTED_TARGET GLOBAL libdeflate) - set(CMAKE_IGNORE_PATH) - if (deflate_FOUND) - message(STATUS "Using libdeflate from ${deflate_LINK_LIBRARIES}") + # First try cmake config + find_package(libdeflate CONFIG) + if(libdeflate_FOUND) + if(TARGET libdeflate::libdeflate_shared) + set(EXR_DEFLATE_TARGET libdeflate::libdeflate_shared) + else() + set(EXR_DEFLATE_TARGET libdeflate::libdeflate_static) + endif() + set(EXR_DEFLATE_SOURCES) + set(EXR_DEFLATE_LDFLAGS) + # Set EXR_DEFATE_LDFLAGS for OpenEXR.pc.in for static build + if(NOT BUILD_SHARED_LIBS) + get_target_property(deflate_LIBRARIES ${EXR_DEFLATE_TARGET} LOCATION) + set(EXR_DEFLATE_LDFLAGS "-l${deflate_LIBRARIES}") + endif() + message(STATUS "Using libdeflate from ${libdeflate_DIR}") + else() + # If not found, try pkgconfig + find_package(PkgConfig) + if(PKG_CONFIG_FOUND) + include(FindPkgConfig) + pkg_check_modules(deflate IMPORTED_TARGET GLOBAL libdeflate) + if(deflate_FOUND) + set(EXR_DEFLATE_TARGET PkgConfig::deflate) + set(EXR_DEFLATE_SOURCES) + set(EXR_DEFLATE_LDFLAGS) + # Set EXR_DEFATE_LDFLAGS for OpenEXR.pc.in for static build + if(NOT BUILD_SHARED_LIBS) + set(EXR_DEFLATE_LDFLAGS "-l${deflate_LIBRARIES}") + endif() + message(STATUS "Using libdeflate from ${deflate_LINK_LIBRARIES}") + endif() + endif() endif() + set(CMAKE_IGNORE_PATH) endif() -if(NOT TARGET PkgConfig::deflate AND NOT deflate_FOUND) +if(NOT EXR_DEFLATE_TARGET) + # Using internal deflate. if(OPENEXR_FORCE_INTERNAL_DEFLATE) message(STATUS "libdeflate forced internal, installing from ${OPENEXR_DEFLATE_REPO} (${OPENEXR_DEFLATE_TAG})") else() @@ -211,18 +241,10 @@ if(NOT TARGET PkgConfig::deflate AND NOT deflate_FOUND) # cmake makes fetch content name lowercase for the properties (to deflate) list(TRANSFORM EXR_DEFLATE_SOURCES PREPEND ${deflate_SOURCE_DIR}/) - set(EXR_DEFLATE_INCLUDE_DIR ${deflate_SOURCE_DIR}) - set(EXR_DEFLATE_LIB) -else() - set(EXR_DEFLATE_INCLUDE_DIR) - set(EXR_DEFLATE_LIB ${deflate_LIBRARIES}) - # set EXR_DEFATE_LDFLAGS for OpenEXR.pc.in for static build - if (BUILD_SHARED_LIBS) - set(EXR_DEFLATE_LDFLAGS "") - else() - set(EXR_DEFLATE_LDFLAGS "-l${deflate_LIBRARIES}") - endif() - set(EXR_DEFLATE_SOURCES) + + add_library(libdeflate_internal_static STATIC IMPORTED) + set_target_properties(libdeflate_internal_static PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${deflate_SOURCE_DIR}") + set(EXR_DEFLATE_TARGET libdeflate_internal_static) endif() ####################################### diff --git a/src/lib/OpenEXRCore/CMakeLists.txt b/src/lib/OpenEXRCore/CMakeLists.txt index f4bd54efb3..3b209a3a63 100644 --- a/src/lib/OpenEXRCore/CMakeLists.txt +++ b/src/lib/OpenEXRCore/CMakeLists.txt @@ -104,10 +104,10 @@ openexr_define_library(OpenEXRCore Imath::Imath ) -if (DEFINED EXR_DEFLATE_LIB) +if (DEFINED EXR_DEFLATE_TARGET) if (BUILD_SHARED_LIBS) - target_link_libraries(OpenEXRCore PRIVATE ${EXR_DEFLATE_LIB}) + target_link_libraries(OpenEXRCore PRIVATE ${EXR_DEFLATE_TARGET}) else() - target_link_libraries(OpenEXRCore PUBLIC ${EXR_DEFLATE_LIB}) + target_link_libraries(OpenEXRCore PUBLIC ${EXR_DEFLATE_TARGET}) endif() endif() From 0924c0b74a7d913bf8c117c83a1b991b1713c141 Mon Sep 17 00:00:00 2001 From: Brecht Van Lommel Date: Wed, 10 Jan 2024 22:28:39 +0100 Subject: [PATCH 2/3] Back out of adding a target for internal libdeflate Signed-off-by: Brecht Van Lommel --- cmake/LibraryDefine.cmake | 3 +++ cmake/OpenEXRSetup.cmake | 18 +++++++++--------- src/lib/OpenEXRCore/CMakeLists.txt | 6 +++--- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/cmake/LibraryDefine.cmake b/cmake/LibraryDefine.cmake index 0e2dbf6c17..a242ec8519 100644 --- a/cmake/LibraryDefine.cmake +++ b/cmake/LibraryDefine.cmake @@ -27,6 +27,9 @@ function(OPENEXR_DEFINE_LIBRARY libname) PRIVATE cxx_std_${OPENEXR_CXX_STANDARD} INTERFACE cxx_std_11 ) + # we are embedding libdeflate + target_include_directories(${objlib} PRIVATE ${EXR_DEFLATE_INCLUDE_DIR}) + if(OPENEXR_CURLIB_PRIV_EXPORT AND BUILD_SHARED_LIBS) target_compile_definitions(${objlib} PRIVATE ${OPENEXR_CURLIB_PRIV_EXPORT}) if(WIN32) diff --git a/cmake/OpenEXRSetup.cmake b/cmake/OpenEXRSetup.cmake index 3a446cd157..f92d0af52a 100644 --- a/cmake/OpenEXRSetup.cmake +++ b/cmake/OpenEXRSetup.cmake @@ -164,15 +164,16 @@ if(NOT OPENEXR_FORCE_INTERNAL_DEFLATE) find_package(libdeflate CONFIG) if(libdeflate_FOUND) if(TARGET libdeflate::libdeflate_shared) - set(EXR_DEFLATE_TARGET libdeflate::libdeflate_shared) + set(EXR_DEFLATE_LIB libdeflate::libdeflate_shared) else() - set(EXR_DEFLATE_TARGET libdeflate::libdeflate_static) + set(EXR_DEFLATE_LIB libdeflate::libdeflate_static) endif() set(EXR_DEFLATE_SOURCES) + set(EXR_DEFLATE_INCLUDE_DIR) set(EXR_DEFLATE_LDFLAGS) # Set EXR_DEFATE_LDFLAGS for OpenEXR.pc.in for static build if(NOT BUILD_SHARED_LIBS) - get_target_property(deflate_LIBRARIES ${EXR_DEFLATE_TARGET} LOCATION) + get_target_property(deflate_LIBRARIES ${EXR_DEFLATE_LIB} LOCATION) set(EXR_DEFLATE_LDFLAGS "-l${deflate_LIBRARIES}") endif() message(STATUS "Using libdeflate from ${libdeflate_DIR}") @@ -183,8 +184,9 @@ if(NOT OPENEXR_FORCE_INTERNAL_DEFLATE) include(FindPkgConfig) pkg_check_modules(deflate IMPORTED_TARGET GLOBAL libdeflate) if(deflate_FOUND) - set(EXR_DEFLATE_TARGET PkgConfig::deflate) + set(EXR_DEFLATE_LIB PkgConfig::deflate) set(EXR_DEFLATE_SOURCES) + set(EXR_DEFLATE_INCLUDE_DIR) set(EXR_DEFLATE_LDFLAGS) # Set EXR_DEFATE_LDFLAGS for OpenEXR.pc.in for static build if(NOT BUILD_SHARED_LIBS) @@ -197,7 +199,7 @@ if(NOT OPENEXR_FORCE_INTERNAL_DEFLATE) set(CMAKE_IGNORE_PATH) endif() -if(NOT EXR_DEFLATE_TARGET) +if(NOT EXR_DEFLATE_LIB) # Using internal deflate. if(OPENEXR_FORCE_INTERNAL_DEFLATE) message(STATUS "libdeflate forced internal, installing from ${OPENEXR_DEFLATE_REPO} (${OPENEXR_DEFLATE_TAG})") @@ -241,10 +243,8 @@ if(NOT EXR_DEFLATE_TARGET) # cmake makes fetch content name lowercase for the properties (to deflate) list(TRANSFORM EXR_DEFLATE_SOURCES PREPEND ${deflate_SOURCE_DIR}/) - - add_library(libdeflate_internal_static STATIC IMPORTED) - set_target_properties(libdeflate_internal_static PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${deflate_SOURCE_DIR}") - set(EXR_DEFLATE_TARGET libdeflate_internal_static) + set(EXR_DEFLATE_INCLUDE_DIR ${deflate_SOURCE_DIR}) + set(EXR_DEFLATE_LIB) endif() ####################################### diff --git a/src/lib/OpenEXRCore/CMakeLists.txt b/src/lib/OpenEXRCore/CMakeLists.txt index 3b209a3a63..f4bd54efb3 100644 --- a/src/lib/OpenEXRCore/CMakeLists.txt +++ b/src/lib/OpenEXRCore/CMakeLists.txt @@ -104,10 +104,10 @@ openexr_define_library(OpenEXRCore Imath::Imath ) -if (DEFINED EXR_DEFLATE_TARGET) +if (DEFINED EXR_DEFLATE_LIB) if (BUILD_SHARED_LIBS) - target_link_libraries(OpenEXRCore PRIVATE ${EXR_DEFLATE_TARGET}) + target_link_libraries(OpenEXRCore PRIVATE ${EXR_DEFLATE_LIB}) else() - target_link_libraries(OpenEXRCore PUBLIC ${EXR_DEFLATE_TARGET}) + target_link_libraries(OpenEXRCore PUBLIC ${EXR_DEFLATE_LIB}) endif() endif() From 80fd751083ad54ccd7277245d767c852703366db Mon Sep 17 00:00:00 2001 From: Brecht Van Lommel Date: Thu, 11 Jan 2024 14:12:45 +0100 Subject: [PATCH 3/3] Use Requires.private for generated pkgconfig file Signed-off-by: Brecht Van Lommel --- cmake/OpenEXR.pc.in | 3 ++- cmake/OpenEXRSetup.cmake | 29 +++++++++++------------------ 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/cmake/OpenEXR.pc.in b/cmake/OpenEXR.pc.in index bce35c2b1f..68d71c3c84 100644 --- a/cmake/OpenEXR.pc.in +++ b/cmake/OpenEXR.pc.in @@ -14,7 +14,8 @@ Name: OpenEXR Description: OpenEXR image library Version: @OPENEXR_VERSION@ -Libs: @exr_pthread_libs@ -L${libdir} -lOpenEXR${libsuffix} -lOpenEXRUtil${libsuffix} -lOpenEXRCore${libsuffix} -lIex${libsuffix} -lIlmThread${libsuffix} @EXR_DEFLATE_LDFLAGS@ +Libs: @exr_pthread_libs@ -L${libdir} -lOpenEXR${libsuffix} -lOpenEXRUtil${libsuffix} -lOpenEXRCore${libsuffix} -lIex${libsuffix} -lIlmThread${libsuffix} Cflags: -I${includedir} -I${OpenEXR_includedir} @exr_pthread_cflags@ Requires: Imath +Requires.private: @EXR_DEFLATE_PKGCONFIG_REQUIRES@ diff --git a/cmake/OpenEXRSetup.cmake b/cmake/OpenEXRSetup.cmake index f92d0af52a..a70f86d2f4 100644 --- a/cmake/OpenEXRSetup.cmake +++ b/cmake/OpenEXRSetup.cmake @@ -161,21 +161,14 @@ if(NOT OPENEXR_FORCE_INTERNAL_DEFLATE) #TODO: ^^ Release should not clone from main, this is a place holder set(CMAKE_IGNORE_PATH "${CMAKE_CURRENT_BINARY_DIR}/_deps/deflate-src/config;${CMAKE_CURRENT_BINARY_DIR}/_deps/deflate-build/config") # First try cmake config - find_package(libdeflate CONFIG) + find_package(libdeflate CONFIG QUIET) if(libdeflate_FOUND) if(TARGET libdeflate::libdeflate_shared) set(EXR_DEFLATE_LIB libdeflate::libdeflate_shared) else() set(EXR_DEFLATE_LIB libdeflate::libdeflate_static) endif() - set(EXR_DEFLATE_SOURCES) - set(EXR_DEFLATE_INCLUDE_DIR) - set(EXR_DEFLATE_LDFLAGS) - # Set EXR_DEFATE_LDFLAGS for OpenEXR.pc.in for static build - if(NOT BUILD_SHARED_LIBS) - get_target_property(deflate_LIBRARIES ${EXR_DEFLATE_LIB} LOCATION) - set(EXR_DEFLATE_LDFLAGS "-l${deflate_LIBRARIES}") - endif() + set(EXR_DEFLATE_VERSION ${libdeflate_VERSION}) message(STATUS "Using libdeflate from ${libdeflate_DIR}") else() # If not found, try pkgconfig @@ -185,13 +178,7 @@ if(NOT OPENEXR_FORCE_INTERNAL_DEFLATE) pkg_check_modules(deflate IMPORTED_TARGET GLOBAL libdeflate) if(deflate_FOUND) set(EXR_DEFLATE_LIB PkgConfig::deflate) - set(EXR_DEFLATE_SOURCES) - set(EXR_DEFLATE_INCLUDE_DIR) - set(EXR_DEFLATE_LDFLAGS) - # Set EXR_DEFATE_LDFLAGS for OpenEXR.pc.in for static build - if(NOT BUILD_SHARED_LIBS) - set(EXR_DEFLATE_LDFLAGS "-l${deflate_LIBRARIES}") - endif() + set(EXR_DEFLATE_VERSION ${deflate_VERSION}) message(STATUS "Using libdeflate from ${deflate_LINK_LIBRARIES}") endif() endif() @@ -199,8 +186,14 @@ if(NOT OPENEXR_FORCE_INTERNAL_DEFLATE) set(CMAKE_IGNORE_PATH) endif() -if(NOT EXR_DEFLATE_LIB) - # Using internal deflate. +if(EXR_DEFLATE_LIB) + # Using external library + set(EXR_DEFLATE_SOURCES) + set(EXR_DEFLATE_INCLUDE_DIR) + # For OpenEXR.pc.in for static build + set(EXR_DEFLATE_PKGCONFIG_REQUIRES "libdeflate >= ${EXR_DEFLATE_VERSION}") +else() + # Using internal deflate if(OPENEXR_FORCE_INTERNAL_DEFLATE) message(STATUS "libdeflate forced internal, installing from ${OPENEXR_DEFLATE_REPO} (${OPENEXR_DEFLATE_TAG})") else()