Skip to content

Commit 342c3ab

Browse files
authored
Add Brotli Cmake support (yhirose#584)
Had to create a custom FindBrotli package, as not all users have PkgConfig installed (which Brotli uses). This file gets installed alongside httplibConfig.cmake for the end-users convenience. Set BROTLI_USE_STATIC_LIBS to ON if you want to find the static libs instead of default shared. Adds the HTTPLIB_REQUIRE_BROTLI (default off) and HTTPLIB_USE_BROTLI_IF_AVAILABLE (default on) options, which work in the same manner as the other optional/required dependency options. Moved the scattered linking and definitions to a single call. Updated some documentation about the new options. Improved the in-tree support by setting the HTTPLIB_IS_USING_XYZ variables in the main CMakeLists (as well as having them in the httplibConfig.cmake file). Fixes yhirose#582
1 parent 6cce795 commit 342c3ab

File tree

3 files changed

+247
-27
lines changed

3 files changed

+247
-27
lines changed

CMakeLists.txt

+53-27
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
#[[
22
Build options:
3+
* BUILD_SHARED_LIBS (default off) builds as a static library (if HTTPLIB_COMPILE is ON)
34
* HTTPLIB_USE_OPENSSL_IF_AVAILABLE (default on)
45
* HTTPLIB_USE_ZLIB_IF_AVAILABLE (default on)
56
* HTTPLIB_REQUIRE_OPENSSL (default off)
67
* HTTPLIB_REQUIRE_ZLIB (default off)
8+
* HTTPLIB_USE_BROTLI_IF_AVAILABLE (default on)
9+
* HTTPLIB_REQUIRE_BROTLI (default off)
710
* HTTPLIB_COMPILE (default off)
11+
* BROTLI_USE_STATIC_LIBS - tells Cmake to use the static Brotli libs (only works if you have them installed).
12+
* OPENSSL_USE_STATIC_LIBS - tells Cmake to use the static OpenSSL libs (only works if you have them installed).
813
914
-------------------------------------------------------------------------------
1015
@@ -36,10 +41,11 @@
3641
* HTTPLIB_HEADER_PATH - this is the full path to the installed header (e.g. /usr/include/httplib.h).
3742
* HTTPLIB_IS_USING_OPENSSL - a bool for if OpenSSL support is enabled.
3843
* HTTPLIB_IS_USING_ZLIB - a bool for if ZLIB support is enabled.
44+
* HTTPLIB_IS_USING_BROTLI - a bool for if Brotli support is enabled.
3945
* HTTPLIB_IS_COMPILED - a bool for if the library is compiled, or otherwise header-only.
4046
* HTTPLIB_INCLUDE_DIR - the root path to httplib's header (e.g. /usr/include).
4147
* HTTPLIB_LIBRARY - the full path to the library if compiled (e.g. /usr/lib/libhttplib.so).
42-
* HTTPLIB_VERSION - the project's version string.
48+
* httplib_VERSION or HTTPLIB_VERSION - the project's version string.
4349
* HTTPLIB_FOUND - a bool for if the target was found.
4450
4551
Want to use precompiled headers (Cmake feature since v3.16)?
@@ -86,9 +92,16 @@ option(HTTPLIB_REQUIRE_ZLIB "Requires ZLIB to be found & linked, or fails build.
8692
# Allow for a build to casually enable OpenSSL/ZLIB support, but silenty continue if not found.
8793
# Make these options so their automatic use can be specifically disabled (as needed)
8894
option(HTTPLIB_USE_OPENSSL_IF_AVAILABLE "Uses OpenSSL (if available) to enable HTTPS support." ON)
89-
option(HTTPLIB_USE_ZLIB_IF_AVAILABLE "Uses ZLIB (if available) to enable compression support." ON)
95+
option(HTTPLIB_USE_ZLIB_IF_AVAILABLE "Uses ZLIB (if available) to enable Zlib compression support." ON)
9096
# Lets you compile the program as a regular library instead of header-only
9197
option(HTTPLIB_COMPILE "If ON, uses a Python script to split the header into a compilable header & source file (requires Python v3)." OFF)
98+
# Just setting this variable here for people building in-tree
99+
if(HTTPLIB_COMPILE)
100+
set(HTTPLIB_IS_COMPILED TRUE)
101+
endif()
102+
103+
option(HTTPLIB_REQUIRE_BROTLI "Requires Brotli to be found & linked, or fails build." OFF)
104+
option(HTTPLIB_USE_BROTLI_IF_AVAILABLE "Uses Brotli (if available) to enable Brotli compression support." ON)
92105
# Defaults to static library
93106
option(BUILD_SHARED_LIBS "Build the library as a shared library instead of static. Has no effect if using header-only." OFF)
94107
if (BUILD_SHARED_LIBS AND WIN32 AND HTTPLIB_COMPILE)
@@ -105,11 +118,33 @@ if(HTTPLIB_REQUIRE_OPENSSL)
105118
elseif(HTTPLIB_USE_OPENSSL_IF_AVAILABLE)
106119
find_package(OpenSSL ${_HTTPLIB_OPENSSL_MIN_VER} COMPONENTS Crypto SSL QUIET)
107120
endif()
121+
# Just setting this variable here for people building in-tree
122+
if(OPENSSL_FOUND)
123+
set(HTTPLIB_IS_USING_OPENSSL TRUE)
124+
endif()
125+
108126
if(HTTPLIB_REQUIRE_ZLIB)
109127
find_package(ZLIB REQUIRED)
110128
elseif(HTTPLIB_USE_ZLIB_IF_AVAILABLE)
111129
find_package(ZLIB QUIET)
112130
endif()
131+
# Just setting this variable here for people building in-tree
132+
# FindZLIB doesn't have a ZLIB_FOUND variable, so check the target.
133+
if(TARGET ZLIB::ZLIB)
134+
set(HTTPLIB_IS_USING_ZLIB TRUE)
135+
endif()
136+
137+
# Adds our cmake folder to the search path for find_package
138+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
139+
if(HTTPLIB_REQUIRE_BROTLI)
140+
find_package(Brotli COMPONENTS encoder decoder common REQUIRED)
141+
elseif(HTTPLIB_USE_BROTLI_IF_AVAILABLE)
142+
find_package(Brotli COMPONENTS encoder decoder common QUIET)
143+
endif()
144+
# Just setting this variable here for people building in-tree
145+
if(Brotli_FOUND)
146+
set(HTTPLIB_IS_USING_BROTLI TRUE)
147+
endif()
113148

114149
# Used for default, common dirs that the end-user can change (if needed)
115150
# like CMAKE_INSTALL_INCLUDEDIR or CMAKE_INSTALL_DATADIR
@@ -185,33 +220,21 @@ target_link_libraries(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC}
185220
$<$<PLATFORM_ID:Windows>:ws2_32>
186221
$<$<PLATFORM_ID:Windows>:crypt32>
187222
$<$<PLATFORM_ID:Windows>:cryptui>
223+
# Can't put multiple targets in a single generator expression or it bugs out.
224+
$<$<BOOL:${HTTPLIB_IS_USING_BROTLI}>:Brotli::common>
225+
$<$<BOOL:${HTTPLIB_IS_USING_BROTLI}>:Brotli::encoder>
226+
$<$<BOOL:${HTTPLIB_IS_USING_BROTLI}>:Brotli::decoder>
227+
$<$<BOOL:${HTTPLIB_IS_USING_ZLIB}>:ZLIB::ZLIB>
228+
$<$<BOOL:${HTTPLIB_IS_USING_OPENSSL}>:OpenSSL::SSL>
229+
$<$<BOOL:${HTTPLIB_IS_USING_OPENSSL}>:OpenSSL::Crypto>
188230
)
189231

190-
# We check for the target when using IF_AVAILABLE since it's possible we didn't find it.
191-
if(HTTPLIB_USE_OPENSSL_IF_AVAILABLE AND TARGET OpenSSL::SSL AND TARGET OpenSSL::Crypto OR HTTPLIB_REQUIRE_OPENSSL)
192-
target_link_libraries(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC}
193-
OpenSSL::SSL OpenSSL::Crypto
194-
)
195-
target_compile_definitions(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC}
196-
CPPHTTPLIB_OPENSSL_SUPPORT
197-
)
198-
set(HTTPLIB_IS_USING_OPENSSL TRUE)
199-
else()
200-
set(HTTPLIB_IS_USING_OPENSSL FALSE)
201-
endif()
202-
203-
# We check for the target when using IF_AVAILABLE since it's possible we didn't find it.
204-
if(HTTPLIB_USE_ZLIB_IF_AVAILABLE AND TARGET ZLIB::ZLIB OR HTTPLIB_REQUIRE_ZLIB)
205-
target_link_libraries(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC}
206-
ZLIB::ZLIB
207-
)
208-
target_compile_definitions(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC}
209-
CPPHTTPLIB_ZLIB_SUPPORT
210-
)
211-
set(HTTPLIB_IS_USING_ZLIB TRUE)
212-
else()
213-
set(HTTPLIB_IS_USING_ZLIB FALSE)
214-
endif()
232+
# Set the definitions to enable optional features
233+
target_compile_definitions(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC}
234+
$<$<BOOL:${HTTPLIB_IS_USING_BROTLI}>:"CPPHTTPLIB_BROTLI_SUPPORT">
235+
$<$<BOOL:${HTTPLIB_IS_USING_ZLIB}>:"CPPHTTPLIB_ZLIB_SUPPORT">
236+
$<$<BOOL:${HTTPLIB_IS_USING_OPENSSL}>:"CPPHTTPLIB_OPENSSL_SUPPORT">
237+
)
215238

216239
# Cmake's find_package search path is different based on the system
217240
# See https://cmake.org/cmake/help/latest/command/find_package.html for the list
@@ -266,6 +289,9 @@ install(FILES "${_httplib_build_includedir}/httplib.h" DESTINATION ${CMAKE_INSTA
266289
install(FILES
267290
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
268291
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
292+
# Install it so it can be used later by the httplibConfig.cmake file.
293+
# Put it in the same dir as our config file instead of a global path so we don't potentially stomp on other packages.
294+
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/FindBrotli.cmake"
269295
DESTINATION ${_TARGET_INSTALL_CMAKEDIR}
270296
)
271297

cmake/FindBrotli.cmake

+185
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
# A simple FindBrotli package for Cmake's find_package function.
2+
# Note: This find package doesn't have version support, as the version file doesn't seem to be installed on most systems.
3+
#
4+
# If you want to find the static packages instead of shared (the default), define BROTLI_USE_STATIC_LIBS as TRUE.
5+
# The targets will have the same names, but it will use the static libs.
6+
#
7+
# Valid find_package COMPONENTS names: "decoder", "encoder", and "common"
8+
#
9+
# Defines the libraries (if found): Brotli::decoder, Brotli::encoder, Brotli::common
10+
# and the includes path variable: Brotli_INCLUDE_DIR
11+
12+
function(brotli_err_msg _err_msg)
13+
# If the package is required, throw a fatal error
14+
# Otherwise, if not running quietly, we throw a warning
15+
if(Brotli_FIND_REQUIRED)
16+
message(FATAL_ERROR "${_err_msg}")
17+
elseif(NOT Brotli_FIND_QUIETLY)
18+
message(WARNING "${_err_msg}")
19+
endif()
20+
endfunction()
21+
22+
# If they asked for a specific version, warn/fail since we don't support it.
23+
if(Brotli_FIND_VERSION)
24+
brotli_err_msg("FindBrotli.cmake doesn't have version support!")
25+
endif()
26+
27+
# Since both decoder & encoder require the common lib (I think), force its requirement..
28+
# if the user is requiring either of those other libs.
29+
if(Brotli_FIND_REQUIRED_decoder OR Brotli_FIND_REQUIRED_encoder)
30+
set(Brotli_FIND_REQUIRED_common TRUE)
31+
endif()
32+
33+
# Make PkgConfig optional, since some users (mainly Windows) don't have it.
34+
# But it's a lot more clean than manually using find_library.
35+
find_package(PkgConfig QUIET)
36+
if(PKG_CONFIG_FOUND)
37+
if(BROTLI_USE_STATIC_LIBS)
38+
pkg_check_modules(Brotli_common_STATIC QUIET IMPORTED_TARGET libbrotlicommon)
39+
pkg_check_modules(Brotli_decoder_STATIC QUIET IMPORTED_TARGET libbrotlidec)
40+
pkg_check_modules(Brotli_encoder_STATIC QUIET IMPORTED_TARGET libbrotlienc)
41+
else()
42+
pkg_check_modules(Brotli_common QUIET IMPORTED_TARGET libbrotlicommon)
43+
pkg_check_modules(Brotli_decoder QUIET IMPORTED_TARGET libbrotlidec)
44+
pkg_check_modules(Brotli_encoder QUIET IMPORTED_TARGET libbrotlienc)
45+
endif()
46+
endif()
47+
48+
# Only used if the PkgConfig libraries aren't used.
49+
find_path(Brotli_INCLUDE_DIR
50+
NAMES "brotli/decode.h" "brotli/encode.h"
51+
PATH_SUFFIXES "include" "includes"
52+
DOC "The path to Brotli's include directory."
53+
)
54+
55+
# Also check if Brotli_decoder was defined, as it can be passed by the end-user
56+
if(NOT TARGET PkgConfig::Brotli_decoder AND NOT Brotli_decoder)
57+
if(BROTLI_USE_STATIC_LIBS)
58+
list(APPEND _brotli_decoder_lib_names
59+
"brotlidec-static"
60+
"libbrotlidec-static"
61+
)
62+
else()
63+
list(APPEND _brotli_decoder_lib_names
64+
"brotlidec"
65+
"libbrotlidec"
66+
)
67+
endif()
68+
find_library(Brotli_decoder
69+
NAMES ${_brotli_decoder_lib_names}
70+
PATH_SUFFIXES
71+
"lib"
72+
"lib64"
73+
"libs"
74+
"libs64"
75+
"lib/x86_64-linux-gnu"
76+
)
77+
endif()
78+
79+
# Also check if Brotli_encoder was defined, as it can be passed by the end-user
80+
if(NOT TARGET PkgConfig::Brotli_encoder AND NOT Brotli_encoder)
81+
if(BROTLI_USE_STATIC_LIBS)
82+
list(APPEND _brotli_encoder_lib_names
83+
"brotlienc-static"
84+
"libbrotlienc-static"
85+
)
86+
else()
87+
list(APPEND _brotli_encoder_lib_names
88+
"brotlienc"
89+
"libbrotlienc"
90+
)
91+
endif()
92+
find_library(Brotli_encoder
93+
NAMES ${_brotli_encoder_lib_names}
94+
PATH_SUFFIXES
95+
"lib"
96+
"lib64"
97+
"libs"
98+
"libs64"
99+
"lib/x86_64-linux-gnu"
100+
)
101+
endif()
102+
103+
# Also check if Brotli_common was defined, as it can be passed by the end-user
104+
if(NOT TARGET PkgConfig::Brotli_common AND NOT Brotli_common)
105+
if(BROTLI_USE_STATIC_LIBS)
106+
list(APPEND _brotli_common_lib_names
107+
"brotlicommon-static"
108+
"libbrotlicommon-static"
109+
)
110+
else()
111+
list(APPEND _brotli_common_lib_names
112+
"brotlicommon"
113+
"libbrotlicommon"
114+
)
115+
endif()
116+
find_library(Brotli_common
117+
NAMES ${_brotli_common_lib_names}
118+
PATH_SUFFIXES
119+
"lib"
120+
"lib64"
121+
"libs"
122+
"libs64"
123+
"lib/x86_64-linux-gnu"
124+
)
125+
endif()
126+
127+
set(_brotli_req_vars "")
128+
# Generic loop to either create all the aliases for the end-user, or throw errors/warnings.
129+
# Note that the case here needs to match the case we used elsewhere in this file.
130+
foreach(_target_name "common" "decoder" "encoder")
131+
# The PkgConfig IMPORTED_TARGET has PkgConfig:: prefixed to it.
132+
if(TARGET PkgConfig::Brotli_${_target_name})
133+
add_library(Brotli::${_target_name} ALIAS PkgConfig::Brotli_${_target_name})
134+
135+
if(Brotli_FIND_REQUIRED_${_target_name})
136+
# The PkgConfig version of the library has a slightly different path to its lib.
137+
if(BROTLI_USE_STATIC_LIBS)
138+
list(APPEND _brotli_req_vars "Brotli_${_target_name}_STATIC_LINK_LIBRARIES")
139+
else()
140+
list(APPEND _brotli_req_vars "Brotli_${_target_name}_LINK_LIBRARIES")
141+
endif()
142+
endif()
143+
# This will only trigger for libraries we found using find_library
144+
elseif(Brotli_${_target_name})
145+
add_library("Brotli::${_target_name}" UNKNOWN IMPORTED)
146+
# Safety-check the includes dir
147+
if(NOT Brotli_INCLUDE_DIR)
148+
brotli_err_msg("Failed to find Brotli's includes directory. Try manually defining \"Brotli_INCLUDE_DIR\" to Brotli's header path on your system.")
149+
endif()
150+
# Attach the literal library and include dir to the IMPORTED target for the end-user
151+
set_target_properties("Brotli::${_target_name}" PROPERTIES
152+
INTERFACE_INCLUDE_DIRECTORIES "${Brotli_INCLUDE_DIR}"
153+
IMPORTED_LOCATION "${Brotli_${_target_name}}"
154+
)
155+
# Attach the library from find_library to our required vars (if it's required)
156+
if(Brotli_FIND_REQUIRED_${_target_name})
157+
list(APPEND _brotli_req_vars "Brotli_${_target_name}")
158+
endif()
159+
# This will only happen if it's a required library but we didn't find it.
160+
elseif(Brotli_FIND_REQUIRED_${_target_name})
161+
# Only bother with an error/failure if they actually required the lib.
162+
brotli_err_msg("Failed to find Brotli's ${_target_name} library. Try manually defining \"Brotli_${_target_name}\" to its path on your system.")
163+
endif()
164+
endforeach()
165+
166+
include(FindPackageHandleStandardArgs)
167+
find_package_handle_standard_args(Brotli
168+
FOUND_VAR Brotli_FOUND
169+
REQUIRED_VARS ${_brotli_req_vars}
170+
)
171+
172+
if(Brotli_FOUND)
173+
include(FindPackageMessage)
174+
foreach(_lib_name ${_brotli_req_vars})
175+
# TODO: remove this if/when The Cmake PkgConfig file fixes the non-quiet message about libbrotlicommon being found.
176+
if(${_lib_name} MATCHES "common")
177+
# This avoids a duplicate "Found Brotli: /usr/lib/libbrotlicommon.so" type message.
178+
continue()
179+
endif()
180+
# Double-expand the var to get the actual path instead of the variable's name.
181+
find_package_message(Brotli "Found Brotli: ${${_lib_name}}"
182+
"[${${_lib_name}}][${Brotli_INCLUDE_DIR}]"
183+
)
184+
endforeach()
185+
endif()

httplibConfig.cmake.in

+9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
set(HTTPLIB_IS_USING_OPENSSL @HTTPLIB_IS_USING_OPENSSL@)
77
set(HTTPLIB_IS_USING_ZLIB @HTTPLIB_IS_USING_ZLIB@)
88
set(HTTPLIB_IS_COMPILED @HTTPLIB_COMPILE@)
9+
set(HTTPLIB_IS_USING_BROTLI @HTTPLIB_IS_USING_BROTLI@)
910
set(HTTPLIB_VERSION @PROJECT_VERSION@)
1011

1112
include(CMakeFindDependencyMacro)
@@ -26,6 +27,14 @@ if(@HTTPLIB_IS_USING_ZLIB@)
2627
find_dependency(ZLIB REQUIRED)
2728
endif()
2829

30+
if(@HTTPLIB_IS_USING_BROTLI@)
31+
# Needed so we can use our own FindBrotli.cmake in this file.
32+
# Note that the FindBrotli.cmake file is installed in the same dir as this file.
33+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
34+
set(BROTLI_USE_STATIC_LIBS @BROTLI_USE_STATIC_LIBS@)
35+
find_dependency(Brotli COMPONENTS common encoder decoder REQUIRED)
36+
endif()
37+
2938
# Mildly useful for end-users
3039
# Not really recommended to be used though
3140
set_and_check(HTTPLIB_INCLUDE_DIR "@PACKAGE_CMAKE_INSTALL_FULL_INCLUDEDIR@")

0 commit comments

Comments
 (0)