Skip to content

Commit

Permalink
cmake: don't install static vendored libraries when SDL3_image is bui…
Browse files Browse the repository at this point in the history
…lt as a shared library

The library is already built-in. No need for an extra static library.
  • Loading branch information
madebr committed Sep 28, 2023
1 parent 252bab2 commit 330bc90
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 49 deletions.
70 changes: 48 additions & 22 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ endif()

# Use `Compatible Interface Properties` to ensure a shared SDL3_image is built with a shared SDL3
if(SDL3IMAGE_BUILD_SHARED_LIBS)
set_property(TARGET ${sdl3_image_target_name} PROPERTY INTERFACE_SDL3_SHARED ${SDL3IMAGE_BUILD_SHARED_LIBS})
set_property(TARGET ${sdl3_image_target_name} PROPERTY INTERFACE_SDL3_SHARED TRUE)
set_property(TARGET ${sdl3_image_target_name} APPEND PROPERTY COMPATIBLE_INTERFACE_BOOL SDL3_SHARED)
endif()

Expand All @@ -313,6 +313,7 @@ if(SDL3IMAGE_BUILD_SHARED_LIBS)
endif()

set(INSTALL_EXTRA_TARGETS)
set(INSTALL_EXTRA_CMAKE_MODULES)
set(PC_LIBS)
set(PC_REQUIRES)

Expand Down Expand Up @@ -358,7 +359,9 @@ if(SDL3IMAGE_ZLIB)
else()
set(ZLIB_LIBRARY zlibstatic)
endif()
list(APPEND INSTALL_EXTRA_TARGETS ${ZLIB_LIBRARY})
if(SDL3IMAGE_ZLIB_SHARED OR NOT SDL3IMAGE_BUILD_SHARED_LIBS)
list(APPEND INSTALL_EXTRA_TARGETS ${ZLIB_LIBRARY})
endif()
set_target_properties(${ZLIB_LIBRARY} PROPERTIES EXPORT_NAME external_zlib)
add_library(SDL3_image::external_zlib ALIAS ${ZLIB_LIBRARY})
else()
Expand All @@ -370,9 +373,12 @@ endif()
if(SDL3IMAGE_DAV1D)
if(SDL3IMAGE_DAV1D_VENDORED)
message(STATUS "${PROJECT_NAME}: Using vendored dav1d")
set(BUILD_SHARED_LIBS ${SDL3IMAGE_DAV1D_SHARED})
add_subdirectory(external/dav1d)
set(DAV1D_LIBRARY dav1d)
list(APPEND INSTALL_EXTRA_TARGETS dav1d)
if(SDL3IMAGE_DAV1D_SHARED OR NOT SDL3IMAGE_BUILD_SHARED_LIBS)
list(APPEND INSTALL_EXTRA_TARGETS dav1d)
endif()
else()
message(FATAL_ERROR "Not implemented")
endif()
Expand All @@ -387,8 +393,11 @@ if(SDL3IMAGE_AVIF)
set(AVIF_CODEC_DAV1D ON CACHE BOOL "Use dav1d codec for decoding" FORCE)
set(AVIF_LOCAL_DAV1D OFF CACHE BOOL "Build dav1d by libaf" FORCE)
set(LIBAVIF_WITH_SHARPYUV_SDLIMAGE ${SDL3IMAGE_WEBP} CACHE BOOL "Build libavif with sharpyuv support (re-use sharpyuv built by libwebp)" FORCE)
set(BUILD_SHARED_LIBS ${SDL3IMAGE_AVIF_SHARED})
add_subdirectory(external/libavif EXCLUDE_FROM_ALL)
list(APPEND INSTALL_EXTRA_TARGETS avif)
if(SDL3IMAGE_AVIF_SHARED OR NOT SDL3IMAGE_BUILD_SHARED_LIBS)
list(APPEND INSTALL_EXTRA_TARGETS avif)
endif()
set_target_properties(avif PROPERTIES EXPORT_NAME external_libavif)
add_library(SDL3_image::external_libavif ALIAS avif)
if(NOT SDL3IMAGE_AVIF_SHARED)
Expand Down Expand Up @@ -453,7 +462,9 @@ if(SDL3IMAGE_JPG)
sdl_check_project_in_subfolder(external/jpeg libjpeg SDL3IMAGE_VENDORED)
set(BUILD_SHARED_LIBS ${SDL3IMAGE_JPG_SHARED})
add_subdirectory(external/jpeg EXCLUDE_FROM_ALL)
list(APPEND INSTALL_EXTRA_TARGETS jpeg)
if(SDL3IMAGE_JPG_SHARED OR NOT SDL3IMAGE_BUILD_SHARED_LIBS)
list(APPEND INSTALL_EXTRA_TARGETS jpeg)
endif()
set_target_properties(jpeg PROPERTIES EXPORT_NAME external_libjpeg)
add_library(SDL3_image::external_libjpeg ALIAS jpeg)
if(NOT SDL3IMAGE_JPG_SHARED)
Expand Down Expand Up @@ -491,6 +502,8 @@ if(SDL3IMAGE_JXL)
message(STATUS "${PROJECT_NAME}: Using vendored libjxl")
# BUILD_TESTING variable is used by libjxl
set(BUILD_TESTING OFF CACHE BOOL "build testing")
# JPEGXL_ENABLE_BENCHMARK variable is used by libjxl
set(JPEGXL_ENABLE_BENCHMARK OFF CACHE BOOL "libjpegxl benchmark" FORCE)
# JPEGXL_ENABLE_TOOLS variable is used by libjxl
set(JPEGXL_ENABLE_JNI OFF CACHE BOOL "build jpegxl fni")
# JPEGXL_ENABLE_MANPAGES variable is used by libjxl
Expand All @@ -504,18 +517,19 @@ if(SDL3IMAGE_JXL)
sdl_check_project_in_subfolder(external/libjxl libjxl SDL3IMAGE_VENDORED)
set(BUILD_SHARED_LIBS ${SDL3IMAGE_JXL_SHARED})
add_subdirectory(external/libjxl EXCLUDE_FROM_ALL)
if(BUILD_SHARED_LIBS)
if(SDL3IMAGE_JXL_SHARED)
set(jxl_lib jxl)
list(APPEND INSTALL_EXTRA_TARGETS brotlidec brotlicommon brotlienc ${jxl_lib})
if(NOT SDL3IMAGE_JXL_SHARED)
list(APPEND PC_LIBS
-l$<TARGET_FILE_BASE_NAME:brotlidec> -l$<TARGET_FILE_BASE_NAME:brotlicommon>
-l$<TARGET_FILE_BASE_NAME:brotlienc> -l$<TARGET_FILE_BASE_NAME:${jxl_lib}>
)
endif()
set(jxl_install_libs brotlidec brotlicommon brotlienc jxl)
else()
set(jxl_lib jxl_dec-static)
list(APPEND INSTALL_EXTRA_TARGETS brotlidec-static brotlicommon-static hwy ${jxl_lib})
set(jxl_install_libs brotlidec-static brotlicommon-static hwy jxl_dec-static)
list(APPEND PC_LIBS
-l$<TARGET_FILE_BASE_NAME:brotlidec> -l$<TARGET_FILE_BASE_NAME:brotlicommon>
-l$<TARGET_FILE_BASE_NAME:brotlienc> -l$<TARGET_FILE_BASE_NAME:jxl>
)
endif()
if(SDL3IMAGE_JXL_SHARED OR NOT SDL3IMAGE_BUILD_SHARED_LIBS)
list(APPEND INSTALL_EXTRA_TARGETS ${jxl_install_libs})
endif()
set_target_properties(${jxl_lib} PROPERTIES EXPORT_NAME external_libjxl)
add_library(SDL3_image::external_libjxl ALIAS ${jxl_lib})
Expand All @@ -527,6 +541,7 @@ if(SDL3IMAGE_JXL)
find_package(libjxl REQUIRED)
if(NOT SDL3IMAGE_JXL_SHARED)
list(APPEND PC_REQUIRES libjxl)
list(APPEND INSTALL_EXTRA_CMAKE_MODULES cmake/Findlibjxl.cmake)
endif()
endif()
if(SDL3IMAGE_JXL_SHARED)
Expand Down Expand Up @@ -571,8 +586,11 @@ if(SDL3IMAGE_PNG)
set(PNG_LIBRARY png_static)
endif()
add_library(PNG::PNG ALIAS ${PNG_LIBRARY})
set_property(TARGET ${PNG_LIBRARY} PROPERTY DEBUG_POSTFIX "")
target_include_directories(${sdl3_image_target_name} PRIVATE external/libpng)
list(APPEND INSTALL_EXTRA_TARGETS ${PNG_LIBRARY})
if(SDL3IMAGE_PNG_SHARED OR NOT SDL3IMAGE_BUILD_SHARED_LIBS)
list(APPEND INSTALL_EXTRA_TARGETS ${PNG_LIBRARY})
endif()
set_target_properties(${PNG_LIBRARY} PROPERTIES EXPORT_NAME external_libpng)
add_library(SDL3_image::external_libpng ALIAS ${PNG_LIBRARY})
if(NOT SDL3IMAGE_PNG_SHARED)
Expand Down Expand Up @@ -654,7 +672,9 @@ if(SDL3IMAGE_TIF)
set(BUILD_SHARED_LIBS ${SDL3IMAGE_TIF_SHARED})
add_subdirectory(external/libtiff EXCLUDE_FROM_ALL)
add_library(TIFF::TIFF ALIAS tiff)
list(APPEND INSTALL_EXTRA_TARGETS tiff)
if(SDL3IMAGE_TIF_SHARED OR NOT SDL3IMAGE_BUILD_SHARED_LIBS)
list(APPEND INSTALL_EXTRA_TARGETS tiff)
endif()
set_target_properties(tiff PROPERTIES EXPORT_NAME external_libtiff)
add_library(SDL3_image::external_libtiff ALIAS tiff)
if(NOT SDL3IMAGE_TIF_SHARED)
Expand Down Expand Up @@ -695,9 +715,11 @@ if(SDL3IMAGE_WEBP)
target_include_directories(${sdl3_image_target_name} PRIVATE external/libwebp/src)
add_library(WebP::webp ALIAS webp)
add_library(WebP::webpdemux ALIAS webpdemux)
list(APPEND INSTALL_EXTRA_TARGETS webp webpdemux)
if(NOT BUILD_SHARED_LIBS)
list(APPEND INSTALL_EXTRA_TARGETS sharpyuv)
if(SDL3IMAGE_WEBP_SHARED OR NOT SDL3IMAGE_BUILD_SHARED_LIBS)
list(APPEND INSTALL_EXTRA_TARGETS webp webpdemux)
if(NOT SDL3IMAGE_WEBP_SHARED)
list(APPEND INSTALL_EXTRA_TARGETS sharpyuv)
endif()
endif()
set_target_properties(webp PROPERTIES EXPORT_NAME "external_libwebp")
set_target_properties(webpdemux PROPERTIES EXPORT_NAME "external_webpdemux")
Expand All @@ -708,6 +730,7 @@ if(SDL3IMAGE_WEBP)
find_package(webp REQUIRED)
if(NOT SDL3IMAGE_WEBP_SHARED)
list(APPEND PC_REQUIRES libwebp)
list(APPEND INSTALL_EXTRA_CMAKE_MODULES cmake/Findwebp.cmake)
endif()
endif()
if(SDL3IMAGE_WEBP_SHARED)
Expand Down Expand Up @@ -763,10 +786,14 @@ if(SDL3IMAGE_INSTALL)

if(INSTALL_EXTRA_TARGETS)
set_property(TARGET ${INSTALL_EXTRA_TARGETS} PROPERTY PUBLIC_HEADER "")
set(archive_destination)
if(NOT BUILD_SHARED_LIBS)
set(archive_destination ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT devel)
endif()
install(
TARGETS ${INSTALL_EXTRA_TARGETS}
EXPORT SDL3ImageExports
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT devel
${archive_destination}
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT library
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT library
)
Expand Down Expand Up @@ -798,8 +825,7 @@ if(SDL3IMAGE_INSTALL)
FILES
"${CMAKE_CURRENT_BINARY_DIR}/SDL3_imageConfig.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/SDL3_imageConfigVersion.cmake"
cmake/Findlibjxl.cmake
cmake/Findwebp.cmake
${INSTALL_EXTRA_CMAKE_MODULES}
DESTINATION "${SDL3IMAGE_INSTALL_CMAKEDIR}"
COMPONENT devel
)
Expand Down
63 changes: 40 additions & 23 deletions cmake/SDL3_imageConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,28 @@ set_package_properties(SDL3_image PROPERTIES

set(SDL3_image_FOUND ON)

set(SDL3IMAGE_AVIF @SDL3IMAGE_AVIF@)
set(SDL3IMAGE_BMP @SDL3IMAGE_BMP@)
set(SDL3IMAGE_GIF @SDL3IMAGE_GIF@)
set(SDL3IMAGE_JPG @SDL3IMAGE_JPG@)
set(SDL3IMAGE_JXL @SDL3IMAGE_JXL@)
set(SDL3IMAGE_LBM @SDL3IMAGE_LBM@)
set(SDL3IMAGE_PCX @SDL3IMAGE_PCX@)
set(SDL3IMAGE_PNG @SDL3IMAGE_PNG@)
set(SDL3IMAGE_PNM @SDL3IMAGE_PNM@)
set(SDL3IMAGE_QOI @SDL3IMAGE_QOI@)
set(SDL3IMAGE_SVG @SDL3IMAGE_SVG@)
set(SDL3IMAGE_TGA @SDL3IMAGE_TGA@)
set(SDL3IMAGE_TIF @SDL3IMAGE_TIF@)
set(SDL3IMAGE_XCF @SDL3IMAGE_XCF@)
set(SDL3IMAGE_XPM @SDL3IMAGE_XPM@)
set(SDL3IMAGE_XV @SDL3IMAGE_XV@)
set(SDL3IMAGE_WEBP @SDL3IMAGE_WEBP@)
set(SDL3IMAGE_AVIF @SDL3IMAGE_AVIF@)
set(SDL3IMAGE_AVIF_SHARED @SDL3IMAGE_AVIF_SHARED@)
set(SDL3IMAGE_BMP @SDL3IMAGE_BMP@)
set(SDL3IMAGE_GIF @SDL3IMAGE_GIF@)
set(SDL3IMAGE_JPG @SDL3IMAGE_JPG@)
set(SDL3IMAGE_JPG_SHARED @SDL3IMAGE_JPG_SHARED@)
set(SDL3IMAGE_JXL @SDL3IMAGE_JXL@)
set(SDL3IMAGE_JXL_SHARED @SDL3IMAGE_JXL_SHARED@)
set(SDL3IMAGE_LBM @SDL3IMAGE_LBM@)
set(SDL3IMAGE_PCX @SDL3IMAGE_PCX@)
set(SDL3IMAGE_PNG @SDL3IMAGE_PNG@)
set(SDL3IMAGE_PNG_SHARED @SDL3IMAGE_PNG_SHARED@)
set(SDL3IMAGE_PNM @SDL3IMAGE_PNM@)
set(SDL3IMAGE_QOI @SDL3IMAGE_QOI@)
set(SDL3IMAGE_SVG @SDL3IMAGE_SVG@)
set(SDL3IMAGE_TGA @SDL3IMAGE_TGA@)
set(SDL3IMAGE_TIF @SDL3IMAGE_TIF@)
set(SDL3IMAGE_TIF_SHARED @SDL3IMAGE_TIF_SHARED@)
set(SDL3IMAGE_XCF @SDL3IMAGE_XCF@)
set(SDL3IMAGE_XPM @SDL3IMAGE_XPM@)
set(SDL3IMAGE_XV @SDL3IMAGE_XV@)
set(SDL3IMAGE_WEBP_SHARED @SDL3IMAGE_WEBP_SHARED@)

set(SDL3IMAGE_JPG_SAVE @SDL3IMAGE_JPG_SAVE@)
set(SDL3IMAGE_PNG_SAVE @SDL3IMAGE_PNG_SAVE@)
Expand All @@ -44,23 +49,35 @@ endif()
if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/SDL3_image-static-targets.cmake")

if(SDL3IMAGE_VENDORED)
if(SDL3IMAGE_WEBP)
if(SDL3IMAGE_AVIF AND NOT SDL3IMAGE_AVIF_SHARED)
find_package(Threads)
endif()
if(SDL3IMAGE_JXL)
if(SDL3IMAGE_JXL AND NOT SDL3IMAGE_JXL_SHARED)
include(CheckLanguage)
check_language(CXX)
if(NOT CMAKE_CXX_COMPILER AND NOT _sdl3image_nowarning)
message(WARNING "CXX language not enabled. Linking to SDL3_image::SDL3_image-static might fail.")
endif()
endif()
if(SDL3IMAGE_TIF AND NOT SDL3IMAGE_TIF_SHARED)
if(NOT TARGET CMath::CMath)
add_library(CMath::CMath INTERFACE IMPORTED)
find_library(CMATH_LIBRARY NAMES m)
if(CMATH_LIBRARY)
set_property(TARGET CMath::CMath PROPERTY INTERFACE_LINK_LIBRARIES "${CMAKE_LIBRARY}")
endif()
endif()
endif()
if(SDL3IMAGE_WEBP AND NOT SDL3IMAGE_WEBP_SHARED)
find_package(Threads)
endif()
else()
set(_sdl_cmake_module_path "${CMAKE_MODULE_PATH}")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")

include(CMakeFindDependencyMacro)

if(SDL3IMAGE_AVIF AND NOT TARGET avif)
if(SDL3IMAGE_AVIF AND NOT TARGET avif AND NOT SDL3IMAGE_AVIF_SHARED)
find_package(libavif 1.0 QUIET)
if(NOT libavif_FOUND)
find_package(libavif @LIBAVIF_MINIMUM_VERSION@ QUIET)
Expand All @@ -71,7 +88,7 @@ if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/SDL3_image-static-targets.cmake")
endif()
endif()

if(SDL3IMAGE_JPG AND NOT TARGET JPEG::JPEG)
if(SDL3IMAGE_JPG AND NOT TARGET JPEG::JPEG AND NOT SDL3IMAGE_JPG_SHARED)
find_dependency(JPEG)
endif()

Expand All @@ -80,15 +97,15 @@ if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/SDL3_image-static-targets.cmake")
find_dependency(libjxl)
endif()

if(SDL3IMAGE_PNG AND NOT TARGET PNG::PNG)
if(SDL3IMAGE_PNG AND NOT TARGET PNG::PNG AND NOT SDL3IMAGE_PNG_SHARED)
find_dependency(PNG)
endif()

if(SDL3IMAGE_TIF AND NOT TARGET TIFF::TIFF)
find_dependency(TIFF)
endif()

if(SDL3IMAGE_WEBP AND NOT TARGET WebP::webp)
if(SDL3IMAGE_WEBP AND NOT TARGET WebP::webp AND NOT SDL3IMAGE_WEBP_SHARED)
list(APPEND webp_ROOT "${CMAKE_CURRENT_LIST_DIR}")
find_dependency(webp)
endif()
Expand Down
24 changes: 20 additions & 4 deletions cmake/test/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,31 @@
#include <SDL3/SDL_main.h>
#include <SDL3_image/SDL_image.h>

#define TEST_INIT_FLAG(FLAG) do { \
if ((IMG_Init(FLAG) & FLAG) == FLAG) { \
SDL_Log("IMG_Init("#FLAG") succeeded"); \
} else { \
SDL_Log("IMG_Init("#FLAG") failed"); \
} \
} while (0);

#define FOREACH_INIT_FLAGS(X) \
X(IMG_INIT_JPG) \
X(IMG_INIT_PNG) \
X(IMG_INIT_TIF) \
X(IMG_INIT_WEBP) \
X(IMG_INIT_JXL) \
X(IMG_INIT_AVIF) \

int main(int argc, char *argv[])
{
if (SDL_Init(0) < 0) {
SDL_Log("Could not initialize SDL: %s\n", SDL_GetError());
SDL_Log("SDL_Init(0) failed: %s\n", SDL_GetError());
return 1;
}
if (IMG_Init(0) == 0) {
SDL_Log("No image formats supported\n");
}

FOREACH_INIT_FLAGS(TEST_INIT_FLAG)

IMG_Quit();
SDL_Quit();
return 0;
Expand Down
2 changes: 2 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

enable_testing()

set(CMAKE_POSITION_INDEPENDENT_CODE OFF)

set(RESOURCE_FILES
palette.bmp
palette.gif
Expand Down

0 comments on commit 330bc90

Please sign in to comment.