Skip to content

Commit 253a9e2

Browse files
committed
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: webmproject/libwebp@bfad7ab Closes: #1399
1 parent 8afbeb6 commit 253a9e2

File tree

3 files changed

+39
-56
lines changed

3 files changed

+39
-56
lines changed

CMakeLists.txt

Lines changed: 37 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,35 @@ endif()
1616
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
1717
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
1818

19+
set(SD_LIB stable-diffusion)
20+
21+
file(GLOB SD_LIB_SOURCES
22+
"src/*.h"
23+
"src/*.cpp"
24+
"src/*.hpp"
25+
"src/vocab/*.h"
26+
"src/vocab/*.cpp"
27+
)
28+
29+
if(SD_BUILD_SHARED_LIBS)
30+
message("-- Build shared library")
31+
message(${SD_LIB_SOURCES})
32+
if(NOT SD_BUILD_SHARED_GGML_LIB)
33+
set(BUILD_SHARED_LIBS OFF)
34+
endif()
35+
add_library(${SD_LIB} SHARED ${SD_LIB_SOURCES})
36+
add_definitions(-DSD_BUILD_SHARED_LIB)
37+
target_compile_definitions(${SD_LIB} PRIVATE -DSD_BUILD_DLL)
38+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
39+
else()
40+
message("-- Build static library")
41+
if(NOT SD_BUILD_SHARED_GGML_LIB)
42+
set(BUILD_SHARED_LIBS OFF)
43+
endif()
44+
add_library(${SD_LIB} STATIC ${SD_LIB_SOURCES})
45+
endif()
46+
47+
1948
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
2049
set(SD_STANDALONE ON)
2150
else()
@@ -108,20 +137,10 @@ if(SD_WEBP)
108137
"Or link against system library:\n cmake (...) -DSD_USE_SYSTEM_WEBP=ON")
109138
endif()
110139
if(SD_USE_SYSTEM_WEBP)
111-
find_package(WebP REQUIRED)
112-
add_library(webp ALIAS WebP::webp)
113-
# libwebp CMake target naming is not consistent across versions/distros.
114-
# Some export WebP::libwebpmux, others export WebP::webpmux.
115-
if(TARGET WebP::libwebpmux)
116-
add_library(libwebpmux ALIAS WebP::libwebpmux)
117-
elseif(TARGET WebP::webpmux)
118-
add_library(libwebpmux ALIAS WebP::webpmux)
119-
else()
120-
message(FATAL_ERROR
121-
"Could not find a compatible webpmux target in system WebP package. "
122-
"Expected WebP::libwebpmux or WebP::webpmux."
123-
)
124-
endif()
140+
find_package(PkgConfig REQUIRED)
141+
pkg_check_modules(WebP REQUIRED libwebp)
142+
include_directories(${WebP_INCLUDEDIR})
143+
target_link_libraries(${SD_LIB} PUBLIC ${WebP_LINK_LIBRARIES})
125144
endif()
126145
endif()
127146

@@ -135,31 +154,13 @@ if(SD_WEBM)
135154
"Or link against system library:\n cmake (...) -DSD_USE_SYSTEM_WEBM=ON")
136155
endif()
137156
if(SD_USE_SYSTEM_WEBM)
138-
find_path(WEBM_INCLUDE_DIR
139-
NAMES mkvmuxer/mkvmuxer.h mkvparser/mkvparser.h common/webmids.h
140-
PATH_SUFFIXES webm
141-
REQUIRED)
142-
find_library(WEBM_LIBRARY
143-
NAMES webm libwebm
144-
REQUIRED)
145-
146-
add_library(webm UNKNOWN IMPORTED)
147-
set_target_properties(webm PROPERTIES
148-
IMPORTED_LOCATION "${WEBM_LIBRARY}"
149-
INTERFACE_INCLUDE_DIRECTORIES "${WEBM_INCLUDE_DIR}")
157+
find_package(PkgConfig REQUIRED)
158+
pkg_check_modules(WebM REQUIRED libwebm)
159+
include_directories(${WebM_INCLUDEDIR})
160+
target_link_libraries(${SD_LIB} PUBLIC ${WebM_LINK_LIBRARIES})
150161
endif()
151162
endif()
152163

153-
set(SD_LIB stable-diffusion)
154-
155-
file(GLOB SD_LIB_SOURCES
156-
"src/*.h"
157-
"src/*.cpp"
158-
"src/*.hpp"
159-
"src/vocab/*.h"
160-
"src/vocab/*.cpp"
161-
)
162-
163164
find_program(GIT_EXE NAMES git git.exe NO_CMAKE_FIND_ROOT_PATH)
164165
if(GIT_EXE)
165166
execute_process(COMMAND ${GIT_EXE} describe --tags --abbrev=7 --dirty=+
@@ -192,24 +193,6 @@ set_property(
192193
SDCPP_BUILD_COMMIT=${SDCPP_BUILD_COMMIT} SDCPP_BUILD_VERSION=${SDCPP_BUILD_VERSION}
193194
)
194195

195-
if(SD_BUILD_SHARED_LIBS)
196-
message("-- Build shared library")
197-
message(${SD_LIB_SOURCES})
198-
if(NOT SD_BUILD_SHARED_GGML_LIB)
199-
set(BUILD_SHARED_LIBS OFF)
200-
endif()
201-
add_library(${SD_LIB} SHARED ${SD_LIB_SOURCES})
202-
add_definitions(-DSD_BUILD_SHARED_LIB)
203-
target_compile_definitions(${SD_LIB} PRIVATE -DSD_BUILD_DLL)
204-
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
205-
else()
206-
message("-- Build static library")
207-
if(NOT SD_BUILD_SHARED_GGML_LIB)
208-
set(BUILD_SHARED_LIBS OFF)
209-
endif()
210-
add_library(${SD_LIB} STATIC ${SD_LIB_SOURCES})
211-
endif()
212-
213196
if(SD_SYCL)
214197
message("-- Use SYCL as backend stable-diffusion")
215198
set(GGML_SYCL ON)

examples/cli/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ install(TARGETS ${TARGET} RUNTIME)
1111
target_link_libraries(${TARGET} PRIVATE stable-diffusion zip ${CMAKE_THREAD_LIBS_INIT})
1212
if(SD_WEBP)
1313
target_compile_definitions(${TARGET} PRIVATE SD_USE_WEBP)
14-
target_link_libraries(${TARGET} PRIVATE webp libwebpmux)
14+
target_link_libraries(${TARGET} PRIVATE webp webpmux)
1515
endif()
1616
if(SD_WEBM)
1717
target_compile_definitions(${TARGET} PRIVATE SD_USE_WEBM)

examples/server/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ install(TARGETS ${TARGET} RUNTIME)
7777
target_link_libraries(${TARGET} PRIVATE stable-diffusion ${CMAKE_THREAD_LIBS_INIT})
7878
if(SD_WEBP)
7979
target_compile_definitions(${TARGET} PRIVATE SD_USE_WEBP)
80-
target_link_libraries(${TARGET} PRIVATE webp libwebpmux)
80+
target_link_libraries(${TARGET} PRIVATE webp webpmux)
8181
endif()
8282
if(SD_WEBM)
8383
target_compile_definitions(${TARGET} PRIVATE SD_USE_WEBM)

0 commit comments

Comments
 (0)