Skip to content

Commit 365645d

Browse files
committed
CMake: Handle multiple libraries being returned for Brotli
Brotli is built as three libraries: libbrotlienc, libbrotlidec, and libbrotlicommon. When requesting the linker flags using pkg-config for e.g. libbrotlidec, it will return both libbrotlidec and libbrotlicommon. The FindBrotli*.cmake files ignore the names of the libraries returned by pkg-config, and hardcode only the libbrotli{enc,dec} names. While this is fine when using shared libraries (they contain an entry for libbrotlicommon as required library in their headers), it will cause linker errors when Brotli has been built as static libraries, due to the missing symbols from libbrotlicommon being unknown to the linker. This fixes FindBrotli*.cmake files by using the library names reported by pkg-config, and applying find_library() to each of the libraries to find their absolute paths. The well-known names of the libraries are still used as fallback in case pkg-config is not available or in case it does not provide the names. If any of the libraries is missing, the corresponding BROTLI{ENC,DEC}_LIBRARIES is unset to let find_package_handle_standard_args() report an error.
1 parent 5e5f2cf commit 365645d

2 files changed

Lines changed: 31 additions & 8 deletions

File tree

cmake/FindBrotliDec.cmake

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,26 @@ find_package(PkgConfig)
1313

1414
pkg_check_modules(PC_BROTLIDEC libbrotlidec)
1515

16+
if(NOT PC_BROTLIDEC_LIBRARIES)
17+
# Fall-back for systems without pkg-config; both libraries must
18+
# be present, otherwise linking will likely fail for static builds.
19+
list(APPEND PC_BROTLIDEC_LIBRARIES libbrotlidec libbrotlicommon)
20+
endif()
21+
1622
find_path(BROTLIDEC_INCLUDE_DIRS
1723
NAMES brotli/decode.h
1824
HINTS ${PC_BROTLIDEC_INCLUDEDIR}
1925
)
2026

21-
find_library(BROTLIDEC_LIBRARIES
22-
NAMES brotlidec
23-
HINTS ${PC_BROTLIDEC_LIBDIR}
24-
)
27+
set(BROTLIDEC_LIBRARIES "")
28+
foreach(_lib ${PC_BROTLIDEC_LIBRARIES})
29+
find_library(BROTLIDEC_PATH_${_lib} ${_lib} HINTS ${PC_BROTLIDEC_LIBRARY_DIRS})
30+
if(NOT BROTLIDEC_PATH_${_lib})
31+
unset(BROTLIDEC_LIBRARIES)
32+
break()
33+
endif()
34+
list(APPEND BROTLIDEC_LIBRARIES "${BROTLIDEC_PATH_${_lib}}")
35+
endforeach()
2536

2637
include(FindPackageHandleStandardArgs)
2738
find_package_handle_standard_args(BrotliDec

cmake/FindBrotliEnc.cmake

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,27 @@ find_package(PkgConfig)
1313

1414
pkg_check_modules(PC_BROTLIENC libbrotlienc)
1515

16+
if(NOT PC_BROTLIENC_LIBRARIES)
17+
# Fall-back for systems without pkg-config; both libraries must
18+
# be present, otherwise linking will likely fail for static builds.
19+
list(APPEND PC_BROTLIENC_LIBRARIES libbrotlienc libbrotlicommon)
20+
endif()
21+
1622
find_path(BROTLIENC_INCLUDE_DIRS
1723
NAMES brotli/encode.h
1824
HINTS ${PC_BROTLIENC_INCLUDEDIR}
1925
)
2026

21-
find_library(BROTLIENC_LIBRARIES
22-
NAMES brotlienc
23-
HINTS ${PC_BROTLIENC_LIBDIR}
24-
)
27+
set(BROTLIENC_LIBRARIES "")
28+
foreach(_lib ${PC_BROTLIENC_LIBRARIES})
29+
find_library(BROTLIENC_PATH_${_lib} ${_lib}
30+
HINTS ${PC_BROTLIENC_LIBRARY_DIRS})
31+
if(NOT BROTLIENC_PATH_${_lib})
32+
unset(BROTLIENC_LIBRARIES)
33+
break()
34+
endif()
35+
list(APPEND BROTLIENC_LIBRARIES "${BROTLIENC_PATH_${_lib}}")
36+
endforeach()
2537

2638
include(FindPackageHandleStandardArgs)
2739
find_package_handle_standard_args(BrotliEnc

0 commit comments

Comments
 (0)