From 989223d5b2f21eac892dbe357c2c56be30aa5623 Mon Sep 17 00:00:00 2001 From: Adrian Perez de Castro Date: Tue, 27 Mar 2018 19:59:23 +0100 Subject: [PATCH] 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. --- cmake/FindBrotliDec.cmake | 19 +++++++++++++++---- cmake/FindBrotliEnc.cmake | 20 ++++++++++++++++---- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/cmake/FindBrotliDec.cmake b/cmake/FindBrotliDec.cmake index abb06f4..5518f45 100644 --- a/cmake/FindBrotliDec.cmake +++ b/cmake/FindBrotliDec.cmake @@ -13,15 +13,26 @@ find_package(PkgConfig) pkg_check_modules(PC_BROTLIDEC libbrotlidec) +if(NOT PC_BROTLIDEC_LIBRARIES) + # Fall-back for systems without pkg-config; both libraries must + # be present, otherwise linking will likely fail for static builds. + list(APPEND PC_BROTLIDEC_LIBRARIES brotlidec brotlicommon) +endif() + find_path(BROTLIDEC_INCLUDE_DIRS NAMES brotli/decode.h HINTS ${PC_BROTLIDEC_INCLUDEDIR} ) -find_library(BROTLIDEC_LIBRARIES - NAMES brotlidec - HINTS ${PC_BROTLIDEC_LIBDIR} -) +set(BROTLIDEC_LIBRARIES "") +foreach(_lib ${PC_BROTLIDEC_LIBRARIES}) + find_library(BROTLIDEC_PATH_${_lib} ${_lib} HINTS ${PC_BROTLIDEC_LIBRARY_DIRS}) + if(NOT BROTLIDEC_PATH_${_lib}) + unset(BROTLIDEC_LIBRARIES) + break() + endif() + list(APPEND BROTLIDEC_LIBRARIES "${BROTLIDEC_PATH_${_lib}}") +endforeach() include(FindPackageHandleStandardArgs) find_package_handle_standard_args(BrotliDec diff --git a/cmake/FindBrotliEnc.cmake b/cmake/FindBrotliEnc.cmake index 4be347d..92eb1d5 100644 --- a/cmake/FindBrotliEnc.cmake +++ b/cmake/FindBrotliEnc.cmake @@ -13,15 +13,27 @@ find_package(PkgConfig) pkg_check_modules(PC_BROTLIENC libbrotlienc) +if(NOT PC_BROTLIENC_LIBRARIES) + # Fall-back for systems without pkg-config; both libraries must + # be present, otherwise linking will likely fail for static builds. + list(APPEND PC_BROTLIENC_LIBRARIES brotlienc brotlicommon) +endif() + find_path(BROTLIENC_INCLUDE_DIRS NAMES brotli/encode.h HINTS ${PC_BROTLIENC_INCLUDEDIR} ) -find_library(BROTLIENC_LIBRARIES - NAMES brotlienc - HINTS ${PC_BROTLIENC_LIBDIR} -) +set(BROTLIENC_LIBRARIES "") +foreach(_lib ${PC_BROTLIENC_LIBRARIES}) + find_library(BROTLIENC_PATH_${_lib} ${_lib} + HINTS ${PC_BROTLIENC_LIBRARY_DIRS}) + if(NOT BROTLIENC_PATH_${_lib}) + unset(BROTLIENC_LIBRARIES) + break() + endif() + list(APPEND BROTLIENC_LIBRARIES "${BROTLIENC_PATH_${_lib}}") +endforeach() include(FindPackageHandleStandardArgs) find_package_handle_standard_args(BrotliEnc