From 692217d1fec9fb0a5d3add6fe03b473eaf61ed75 Mon Sep 17 00:00:00 2001 From: staphen Date: Wed, 3 Nov 2021 22:38:08 -0400 Subject: [PATCH] Add CMake option to use system SDL_image --- 3rdParty/SDL_image/CMakeLists.txt | 8 ++++++- CMake/FindSDL2_image.cmake | 26 +++++++++++++++++++++++ CMakeLists.txt | 35 +++++++++++++++++++++++++++---- vcpkg.json | 3 ++- 4 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 CMake/FindSDL2_image.cmake diff --git a/3rdParty/SDL_image/CMakeLists.txt b/3rdParty/SDL_image/CMakeLists.txt index 4cdeef000..b96650747 100644 --- a/3rdParty/SDL_image/CMakeLists.txt +++ b/3rdParty/SDL_image/CMakeLists.txt @@ -14,7 +14,12 @@ else() endif() FetchContent_MakeAvailableExcludeFromAll(SDL_image) -add_library(SDL_image STATIC ${CMAKE_CURRENT_LIST_DIR}/IMG.c ${sdl_image_SOURCE_DIR}/IMG_png.c) +if(DEVILUTIONX_STATIC_SDL_IMAGE) + add_library(SDL_image STATIC ${CMAKE_CURRENT_LIST_DIR}/IMG.c ${sdl_image_SOURCE_DIR}/IMG_png.c) +else() + add_library(SDL_image SHARED ${CMAKE_CURRENT_LIST_DIR}/IMG.c ${sdl_image_SOURCE_DIR}/IMG_png.c) +endif() + target_include_directories(SDL_image PRIVATE ${sdl_image_SOURCE_DIR}) target_compile_definitions(SDL_image PRIVATE LOAD_PNG SDL_IMAGE_USE_COMMON_BACKEND) target_link_libraries(SDL_image PNG::PNG) @@ -23,4 +28,5 @@ if(USE_SDL1) target_link_libraries(SDL_image ${SDL_LIBRARY}) else() target_link_libraries(SDL_image SDL2::SDL2) + add_library(SDL2::SDL2_image ALIAS SDL_image) endif() diff --git a/CMake/FindSDL2_image.cmake b/CMake/FindSDL2_image.cmake new file mode 100644 index 000000000..dd7900286 --- /dev/null +++ b/CMake/FindSDL2_image.cmake @@ -0,0 +1,26 @@ +find_package(PkgConfig) +pkg_check_modules(PC_SDL2_image QUIET SDL2_image>=2.0.0) + +find_path(SDL2_image_INCLUDE_DIR SDL_image.h + HINTS ${PC_SDL2_image_INCLUDEDIR} ${PC_SDL2_image_INCLUDE_DIRS}) + +find_library(SDL2_image_LIBRARY + NAMES SDL2_image libSDL2_image + HINTS ${PC_SDL2_image_LIBDIR} ${PC_SDL2_image_LIBRARY_DIRS}) + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(SDL2_image DEFAULT_MSG + SDL2_image_INCLUDE_DIR SDL2_image_LIBRARY) + +if(SDL2_image_FOUND AND NOT TARGET SDL2::SDL2_image) + add_library(SDL2::SDL2_image UNKNOWN IMPORTED) + set_target_properties(SDL2::SDL2_image PROPERTIES + IMPORTED_LOCATION ${SDL2_image_LIBRARY} + INTERFACE_INCLUDE_DIRECTORIES ${SDL2_image_INCLUDE_DIR}) +endif() + +if(SDL2_image_FOUND) + mark_as_advanced(SDL2_image_INCLUDE_DIR SDL2_image_LIBRARY) + set(SDL2_image_LIBRARIES ${SDL2_image_LIBRARY}) + set(SDL2_image_INCLUDE_DIRS ${SDL2_image_INCLUDE_DIR}) +endif() diff --git a/CMakeLists.txt b/CMakeLists.txt index 2121409e9..116da573f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -93,6 +93,10 @@ option(DEVILUTIONX_SYSTEM_LIBPNG "Use system-provided libpng" ON) cmake_dependent_option(DEVILUTIONX_STATIC_LIBPNG "Link static libpng" OFF "DEVILUTIONX_SYSTEM_LIBPNG AND NOT DIST" ON) +option(DEVILUTIONX_SYSTEM_SDL_IMAGE "Use system-provided SDL_image" ON) +cmake_dependent_option(DEVILUTIONX_STATIC_SDL_IMAGE "Link static SDL_image" OFF + "DEVILUTIONX_SYSTEM_SDL_IMAGE AND NOT DIST" ON) + if(NOT VERSION_NUM) include(CMake/git.cmake) get_git_tag(VERSION_NUM) @@ -313,7 +317,29 @@ if(NOT NONET AND NOT DISABLE_TCP) endif() endif() -add_subdirectory(3rdParty/SDL_image) +if(DEVILUTIONX_SYSTEM_SDL_IMAGE) + if(USE_SDL1) + find_package(SDL_image QUIET) + else() + # vcpkg uses sdl2-image as the package name + find_package(sdl2-image QUIET) + set(SDL_image_FOUND ${sdl2-image_FOUND}) + + if(NOT SDL_image_FOUND) + # Fall back on PkgConfig via FindSDL2_image.cmake + find_package(SDL2_image QUIET) + set(SDL_image_FOUND ${SDL2_image_FOUND}) + endif() + endif() + if(SDL_image_FOUND) + message("-- Found SDL_image") + else() + message("-- Suitable system SDL_image package not found, will use SDL_image from source") + endif() +endif() +if(NOT SDL_image_FOUND) + add_subdirectory(3rdParty/SDL_image) +endif() if(NOT NOSOUND) if(DEVILUTIONX_SYSTEM_SDL_AUDIOLIB) @@ -986,14 +1012,15 @@ endif() if(USE_SDL1) target_link_libraries(libdevilutionx PUBLIC - ${SDL_LIBRARY}) + ${SDL_LIBRARY} + SDL_image) target_compile_definitions(libdevilutionx PUBLIC USE_SDL1) else() target_link_libraries(libdevilutionx PUBLIC SDL2::SDL2 - ${SDL2_MAIN}) + ${SDL2_MAIN} + SDL2::SDL2_image) endif() -target_link_libraries(libdevilutionx PUBLIC SDL_image) if(NOT NOSOUND) target_link_libraries(libdevilutionx PUBLIC SDL_audiolib) diff --git a/vcpkg.json b/vcpkg.json index 0d2243d09..5719e58fe 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -4,7 +4,8 @@ "dependencies": [ "fmt", "libpng", - "sdl2" + "sdl2", + "sdl2-image" ], "features": { "encryption": {