From b763e33863f35ddcbb69408e206437a767ed3c65 Mon Sep 17 00:00:00 2001 From: Craig Andrews Date: Tue, 7 Apr 2026 17:09:03 -0400 Subject: [PATCH 1/2] fix: Use PkgConfig for WebP and WebM PkgConfig is the standard approach to locating and using system libraries. It does not rely on the library having been built with CMake, improving compatibility. webpmux is the library name; it should not be prefixed with lib. See: https://github.com/webmproject/libwebp/commit/bfad7ab589a997a69ba568a7c5d9696474f917fc Closes: https://github.com/leejet/stable-diffusion.cpp/issues/1399 --- CMakeLists.txt | 60 ++++++++++++++++++++-------------- examples/cli/CMakeLists.txt | 2 +- examples/server/CMakeLists.txt | 2 +- 3 files changed, 38 insertions(+), 26 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a43c99f90..b7e0f51aa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,6 +13,8 @@ if (MSVC) add_compile_definitions(_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING) endif() +find_package(PkgConfig) + set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) @@ -108,19 +110,24 @@ if(SD_WEBP) "Or link against system library:\n cmake (...) -DSD_USE_SYSTEM_WEBP=ON") endif() if(SD_USE_SYSTEM_WEBP) - find_package(WebP REQUIRED) - add_library(webp ALIAS WebP::webp) - # libwebp CMake target naming is not consistent across versions/distros. - # Some export WebP::libwebpmux, others export WebP::webpmux. - if(TARGET WebP::libwebpmux) - add_library(libwebpmux ALIAS WebP::libwebpmux) - elseif(TARGET WebP::webpmux) - add_library(libwebpmux ALIAS WebP::webpmux) + if(PkgConfig_FOUND) + pkg_check_modules(WebP REQUIRED IMPORTED_TARGET GLOBAL libwebp) + link_libraries(PkgConfig::WebP) else() - message(FATAL_ERROR - "Could not find a compatible webpmux target in system WebP package. " - "Expected WebP::libwebpmux or WebP::webpmux." - ) + find_package(WebP REQUIRED) + add_library(webp ALIAS WebP::webp) + # libwebp CMake target naming is not consistent across versions/distros. + # Some export WebP::libwebpmux, others export WebP::webpmux. + if(TARGET WebP::libwebpmux) + add_library(libwebpmux ALIAS WebP::libwebpmux) + elseif(TARGET WebP::webpmux) + add_library(libwebpmux ALIAS WebP::webpmux) + else() + message(FATAL_ERROR + "Could not find a compatible webpmux target in system WebP package. " + "Expected WebP::libwebpmux or WebP::webpmux." + ) + endif() endif() endif() endif() @@ -135,18 +142,23 @@ if(SD_WEBM) "Or link against system library:\n cmake (...) -DSD_USE_SYSTEM_WEBM=ON") endif() if(SD_USE_SYSTEM_WEBM) - find_path(WEBM_INCLUDE_DIR - NAMES mkvmuxer/mkvmuxer.h mkvparser/mkvparser.h common/webmids.h - PATH_SUFFIXES webm - REQUIRED) - find_library(WEBM_LIBRARY - NAMES webm libwebm - REQUIRED) - - add_library(webm UNKNOWN IMPORTED) - set_target_properties(webm PROPERTIES - IMPORTED_LOCATION "${WEBM_LIBRARY}" - INTERFACE_INCLUDE_DIRECTORIES "${WEBM_INCLUDE_DIR}") + if(PkgConfig_FOUND) + pkg_check_modules(WebM REQUIRED IMPORTED_TARGET GLOBAL libwebm) + link_libraries(PkgConfig::WebM) + else() + find_path(WEBM_INCLUDE_DIR + NAMES mkvmuxer/mkvmuxer.h mkvparser/mkvparser.h common/webmids.h + PATH_SUFFIXES webm + REQUIRED) + find_library(WEBM_LIBRARY + NAMES webm libwebm + REQUIRED) + + add_library(webm UNKNOWN IMPORTED) + set_target_properties(webm PROPERTIES + IMPORTED_LOCATION "${WEBM_LIBRARY}" + INTERFACE_INCLUDE_DIRECTORIES "${WEBM_INCLUDE_DIR}") + endif() endif() endif() diff --git a/examples/cli/CMakeLists.txt b/examples/cli/CMakeLists.txt index db1f4ca37..8e6e47e14 100644 --- a/examples/cli/CMakeLists.txt +++ b/examples/cli/CMakeLists.txt @@ -11,7 +11,7 @@ install(TARGETS ${TARGET} RUNTIME) target_link_libraries(${TARGET} PRIVATE stable-diffusion zip ${CMAKE_THREAD_LIBS_INIT}) if(SD_WEBP) target_compile_definitions(${TARGET} PRIVATE SD_USE_WEBP) - target_link_libraries(${TARGET} PRIVATE webp libwebpmux) + target_link_libraries(${TARGET} PRIVATE webp webpmux) endif() if(SD_WEBM) target_compile_definitions(${TARGET} PRIVATE SD_USE_WEBM) diff --git a/examples/server/CMakeLists.txt b/examples/server/CMakeLists.txt index 99fc93444..162c2c827 100644 --- a/examples/server/CMakeLists.txt +++ b/examples/server/CMakeLists.txt @@ -77,7 +77,7 @@ install(TARGETS ${TARGET} RUNTIME) target_link_libraries(${TARGET} PRIVATE stable-diffusion ${CMAKE_THREAD_LIBS_INIT}) if(SD_WEBP) target_compile_definitions(${TARGET} PRIVATE SD_USE_WEBP) - target_link_libraries(${TARGET} PRIVATE webp libwebpmux) + target_link_libraries(${TARGET} PRIVATE webp webpmux) endif() if(SD_WEBM) target_compile_definitions(${TARGET} PRIVATE SD_USE_WEBM) From 6bfa3b3958c88a65e91c549d9da4d9ffa868c662 Mon Sep 17 00:00:00 2001 From: Craig Andrews Date: Wed, 8 Apr 2026 21:54:05 -0400 Subject: [PATCH 2/2] Use cmake_pkg_config to eliminate dependency on pkgconfig, fallback to cmake if pc not found --- CMakeLists.txt | 23 ++++++++++++----------- examples/cli/CMakeLists.txt | 2 +- examples/server/CMakeLists.txt | 2 +- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b7e0f51aa..7a7e82cec 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.12) +cmake_minimum_required(VERSION 4.1) project("stable-diffusion") set(CMAKE_EXPORT_COMPILE_COMMANDS ON) @@ -13,8 +13,6 @@ if (MSVC) add_compile_definitions(_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING) endif() -find_package(PkgConfig) - set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) @@ -110,11 +108,8 @@ if(SD_WEBP) "Or link against system library:\n cmake (...) -DSD_USE_SYSTEM_WEBP=ON") endif() if(SD_USE_SYSTEM_WEBP) - if(PkgConfig_FOUND) - pkg_check_modules(WebP REQUIRED IMPORTED_TARGET GLOBAL libwebp) - link_libraries(PkgConfig::WebP) - else() - find_package(WebP REQUIRED) + find_package(WebP) + if(WebP_FOUND) add_library(webp ALIAS WebP::webp) # libwebp CMake target naming is not consistent across versions/distros. # Some export WebP::libwebpmux, others export WebP::webpmux. @@ -128,6 +123,12 @@ if(SD_WEBP) "Expected WebP::libwebpmux or WebP::webpmux." ) endif() + else() + cmake_pkg_config(IMPORT libwebp REQUIRED) + link_libraries(PkgConfig::libwebp) + cmake_pkg_config(IMPORT libwebpmux REQUIRED) + link_libraries(PkgConfig::libwebpmux) + add_library(libwebpmux ALIAS PkgConfig::libwebpmux) endif() endif() endif() @@ -142,9 +143,9 @@ if(SD_WEBM) "Or link against system library:\n cmake (...) -DSD_USE_SYSTEM_WEBM=ON") endif() if(SD_USE_SYSTEM_WEBM) - if(PkgConfig_FOUND) - pkg_check_modules(WebM REQUIRED IMPORTED_TARGET GLOBAL libwebm) - link_libraries(PkgConfig::WebM) + cmake_pkg_config(IMPORT libwebm) + if(PKGCONFIG_libwebm_FOUND) + link_libraries(PkgConfig::libwebm) else() find_path(WEBM_INCLUDE_DIR NAMES mkvmuxer/mkvmuxer.h mkvparser/mkvparser.h common/webmids.h diff --git a/examples/cli/CMakeLists.txt b/examples/cli/CMakeLists.txt index 8e6e47e14..db1f4ca37 100644 --- a/examples/cli/CMakeLists.txt +++ b/examples/cli/CMakeLists.txt @@ -11,7 +11,7 @@ install(TARGETS ${TARGET} RUNTIME) target_link_libraries(${TARGET} PRIVATE stable-diffusion zip ${CMAKE_THREAD_LIBS_INIT}) if(SD_WEBP) target_compile_definitions(${TARGET} PRIVATE SD_USE_WEBP) - target_link_libraries(${TARGET} PRIVATE webp webpmux) + target_link_libraries(${TARGET} PRIVATE webp libwebpmux) endif() if(SD_WEBM) target_compile_definitions(${TARGET} PRIVATE SD_USE_WEBM) diff --git a/examples/server/CMakeLists.txt b/examples/server/CMakeLists.txt index 162c2c827..99fc93444 100644 --- a/examples/server/CMakeLists.txt +++ b/examples/server/CMakeLists.txt @@ -77,7 +77,7 @@ install(TARGETS ${TARGET} RUNTIME) target_link_libraries(${TARGET} PRIVATE stable-diffusion ${CMAKE_THREAD_LIBS_INIT}) if(SD_WEBP) target_compile_definitions(${TARGET} PRIVATE SD_USE_WEBP) - target_link_libraries(${TARGET} PRIVATE webp webpmux) + target_link_libraries(${TARGET} PRIVATE webp libwebpmux) endif() if(SD_WEBM) target_compile_definitions(${TARGET} PRIVATE SD_USE_WEBM)