Skip to content

Commit 6791a83

Browse files
authored
FindBrotli cleanup & fixes (#1786)
* Unimportant cleanup of FindBrotli Some whitespace, removal of an unused var, and add a comment about the missing version support * Fix incorrect var in FindBrotli Not much to say, it was just the wrong one. Not sure how that happened, maybe a holdover from when I did an overhaul at some point.. * Tiny useless edit to FindBrotli Just a little tweak to how I was postfixing those -static strings to the lib names. Mostly pointless.. * Simplify some FindBrotli code Nothing much, just reducing redundant stuff & a cleanup of a comment. * Ignore PkgConf in FindBrotli if looking for static As per the issue mentioned in this comment, static PkgConf support is broken upstream. After testing, I found it'll just accept shared even if you try to find static, so I'm merely disabling that feature for this FindBrotli module. That said, you can still get the static libs if you have them installed, but PkgConf will be ignored even if found in favor of a regular find_library call. Otherwise you'd end up with shared libs even if you ask for static (with PkgConf installed). TLDR: actually fail correctly when static libs aren't found when PkgConf thought they were.
1 parent d1a1c8a commit 6791a83

File tree

1 file changed

+18
-28
lines changed

1 file changed

+18
-28
lines changed

cmake/FindBrotli.cmake

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# A simple FindBrotli package for Cmake's find_package function.
22
# Note: This find package doesn't have version support, as the version file doesn't seem to be installed on most systems.
3-
#
3+
#
44
# If you want to find the static packages instead of shared (the default), define BROTLI_USE_STATIC_LIBS as TRUE.
55
# The targets will have the same names, but it will use the static libs.
66
#
@@ -14,7 +14,7 @@
1414

1515
# If they asked for a specific version, warn/fail since we don't support it.
1616
# TODO: if they start distributing the version somewhere, implement finding it.
17-
# But currently there's a version header that doesn't seem to get installed.
17+
# See https://github.com/google/brotli/issues/773#issuecomment-579133187
1818
if(Brotli_FIND_VERSION)
1919
set(_brotli_version_error_msg "FindBrotli.cmake doesn't have version support!")
2020
# If the package is required, throw a fatal error
@@ -68,29 +68,24 @@ if(BROTLI_USE_STATIC_LIBS)
6868
set(_brotli_stat_str "_STATIC")
6969
endif()
7070

71-
# Lets us know we are using the PkgConfig libraries
72-
# Will be set false if any non-pkgconf vars are used
73-
set(_brotli_using_pkgconf TRUE)
74-
7571
# Each string here is "ComponentName;LiteralName" (the semi-colon is a delimiter)
7672
foreach(_listvar "common;common" "decoder;dec" "encoder;enc")
7773
# Split the component name and literal library name from the listvar
7874
list(GET _listvar 0 _component_name)
7975
list(GET _listvar 1 _libname)
8076

81-
if(PKG_CONFIG_FOUND)
77+
# NOTE: We can't rely on PkgConf for static libs since the upstream static lib support is broken
78+
# See https://github.com/google/brotli/issues/795
79+
# TODO: whenever their issue is fixed upstream, remove this "AND NOT BROTLI_USE_STATIC_LIBS" check
80+
if(PKG_CONFIG_FOUND AND NOT BROTLI_USE_STATIC_LIBS)
8281
# These need to be GLOBAL for MinGW when making ALIAS libraries against them.
83-
if(BROTLI_USE_STATIC_LIBS)
84-
# Have to use _STATIC to tell PkgConfig to find the static libs.
85-
pkg_check_modules(Brotli_${_component_name}_STATIC QUIET GLOBAL IMPORTED_TARGET libbrotli${_libname})
86-
else()
87-
pkg_check_modules(Brotli_${_component_name} QUIET GLOBAL IMPORTED_TARGET libbrotli${_libname})
88-
endif()
82+
# Have to postfix _STATIC on the name to tell PkgConfig to find the static libs.
83+
pkg_check_modules(Brotli_${_component_name}${_brotli_stat_str} QUIET GLOBAL IMPORTED_TARGET libbrotli${_libname})
8984
endif()
9085

91-
# Check if the target was already found by Pkgconf
92-
if(TARGET PkgConfig::Brotli_${_component_name} OR TARGET PkgConfig::Brotli_${_component_name}_STATIC)
93-
# Can't use generators for ALIAS targets, so you get this jank
86+
# Check if the target was already found by Pkgconf
87+
if(TARGET PkgConfig::Brotli_${_component_name}${_brotli_stat_str})
88+
# ALIAS since we don't want the PkgConfig namespace on the Cmake library (for end-users)
9489
add_library(Brotli::${_component_name} ALIAS PkgConfig::Brotli_${_component_name}${_brotli_stat_str})
9590

9691
# Tells HANDLE_COMPONENTS we found the component
@@ -109,23 +104,18 @@ foreach(_listvar "common;common" "decoder;dec" "encoder;enc")
109104
continue()
110105
endif()
111106

112-
# Lets us know we aren't using the PkgConfig libraries
113-
set(_brotli_using_pkgconf FALSE)
114107
if(Brotli_FIND_REQUIRED_${_component_name})
115108
# If it's required, we can set the name used in find_library as a required var for FindPackageHandleStandardArgs
116109
list(APPEND _brotli_req_vars "Brotli_${_component_name}")
117110
endif()
118111

112+
list(APPEND _brotli_lib_names
113+
"brotli${_libname}"
114+
"libbrotli${_libname}"
115+
)
119116
if(BROTLI_USE_STATIC_LIBS)
120-
list(APPEND _brotli_lib_names
121-
"brotli${_libname}-static"
122-
"libbrotli${_libname}-static"
123-
)
124-
else()
125-
list(APPEND _brotli_lib_names
126-
"brotli${_libname}"
127-
"libbrotli${_libname}"
128-
)
117+
# Postfix "-static" to the libnames since we're looking for static libs
118+
list(TRANSFORM _brotli_lib_names APPEND "-static")
129119
endif()
130120

131121
find_library(Brotli_${_component_name}
@@ -168,7 +158,7 @@ find_package_handle_standard_args(Brotli
168158
Brotli_FOUND
169159
REQUIRED_VARS
170160
Brotli_INCLUDE_DIR
171-
${_brotli_required_targets}
161+
${_brotli_req_vars}
172162
HANDLE_COMPONENTS
173163
)
174164

0 commit comments

Comments
 (0)