Skip to content

Commit d948e38

Browse files
authored
Minor cmake fix & cleanup (yhirose#1754)
* Reorder cmake docs a bit Just wanted to group the more related build options together. Also removed a pointless reference to the old reasoning for the required min ver since it's 3.14 now anyways. * Fix outdated cmake comment We don't use Git to find the version string anymore. Just updated to match what it's actually used for now. * Group options and build-tree vars in Cmake Doesn't really change anything, I just wanted to clean these up a bit. * Fix how we set HTTPLIB_IS_USING_XXX vars in Cmake Prevents us acidentally using libs when the user didn't want them actually used. This could happen if they set the option to OFF but their own project itself is using the lib, thus we'd find and use it anyways. Ref yhirose#1602 to see an example of this already happening before. This is merely apply that kind of fix to all 3 of our deps, instead of just OpenSSL. * Minor formatting/comment change to Cmake Pointless, but these things were bothering me..
1 parent 65218ce commit d948e38

File tree

1 file changed

+30
-37
lines changed

1 file changed

+30
-37
lines changed

CMakeLists.txt

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
* BUILD_SHARED_LIBS (default off) builds as a shared library (if HTTPLIB_COMPILE is ON)
44
* HTTPLIB_USE_OPENSSL_IF_AVAILABLE (default on)
55
* HTTPLIB_USE_ZLIB_IF_AVAILABLE (default on)
6+
* HTTPLIB_USE_BROTLI_IF_AVAILABLE (default on)
67
* HTTPLIB_REQUIRE_OPENSSL (default off)
78
* HTTPLIB_REQUIRE_ZLIB (default off)
8-
* HTTPLIB_USE_BROTLI_IF_AVAILABLE (default on)
9-
* HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN (default on)
109
* HTTPLIB_REQUIRE_BROTLI (default off)
10+
* HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN (default on)
1111
* HTTPLIB_COMPILE (default off)
1212
* HTTPLIB_INSTALL (default on)
1313
* HTTPLIB_TEST (default off)
@@ -59,7 +59,6 @@
5959
6060
-------------------------------------------------------------------------------
6161
62-
FindPython3 requires Cmake v3.12
6362
ARCH_INDEPENDENT option of write_basic_package_version_file() requires Cmake v3.14
6463
]]
6564
cmake_minimum_required(VERSION 3.14.0 FATAL_ERROR)
@@ -69,20 +68,19 @@ cmake_minimum_required(VERSION 3.14.0 FATAL_ERROR)
6968
# This is so the maintainer doesn't actually need to update this manually.
7069
file(STRINGS httplib.h _raw_version_string REGEX "CPPHTTPLIB_VERSION \"([0-9]+\\.[0-9]+\\.[0-9]+)\"")
7170

72-
# Needed since git tags have "v" prefixing them.
73-
# Also used if the fallback to user agent string is being used.
71+
# Extracts just the version string itself from the whole string contained in _raw_version_string
72+
# since _raw_version_string would contain the entire line of code where it found the version string
7473
string(REGEX MATCH "([0-9]+\\.?)+" _httplib_version "${_raw_version_string}")
7574

7675
project(httplib VERSION ${_httplib_version} LANGUAGES CXX)
7776

78-
# Lets you disable C++ exception during CMake configure time.
79-
# The value is used in the install CMake config file.
80-
option(HTTPLIB_NO_EXCEPTIONS "Disable the use of C++ exceptions" OFF)
81-
8277
# Change as needed to set an OpenSSL minimum version.
8378
# This is used in the installed Cmake config file.
8479
set(_HTTPLIB_OPENSSL_MIN_VER "3.0.0")
8580

81+
# Lets you disable C++ exception during CMake configure time.
82+
# The value is used in the install CMake config file.
83+
option(HTTPLIB_NO_EXCEPTIONS "Disable the use of C++ exceptions" OFF)
8684
# Allow for a build to require OpenSSL to pass, instead of just being optional
8785
option(HTTPLIB_REQUIRE_OPENSSL "Requires OpenSSL to be found & linked, or fails build." OFF)
8886
option(HTTPLIB_REQUIRE_ZLIB "Requires ZLIB to be found & linked, or fails build." OFF)
@@ -94,10 +92,6 @@ option(HTTPLIB_USE_ZLIB_IF_AVAILABLE "Uses ZLIB (if available) to enable Zlib co
9492
option(HTTPLIB_COMPILE "If ON, uses a Python script to split the header into a compilable header & source file (requires Python v3)." OFF)
9593
# Lets you disable the installation (useful when fetched from another CMake project)
9694
option(HTTPLIB_INSTALL "Enables the installation target" ON)
97-
# Just setting this variable here for people building in-tree
98-
if(HTTPLIB_COMPILE)
99-
set(HTTPLIB_IS_COMPILED TRUE)
100-
endif()
10195
option(HTTPLIB_TEST "Enables testing and builds tests" OFF)
10296
option(HTTPLIB_REQUIRE_BROTLI "Requires Brotli to be found & linked, or fails build." OFF)
10397
option(HTTPLIB_USE_BROTLI_IF_AVAILABLE "Uses Brotli (if available) to enable Brotli decompression support." ON)
@@ -110,45 +104,48 @@ if (BUILD_SHARED_LIBS AND WIN32 AND HTTPLIB_COMPILE)
110104
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
111105
endif()
112106

107+
# Set some variables that are used in-tree and while building based on our options
108+
set(HTTPLIB_IS_COMPILED ${HTTPLIB_COMPILE})
109+
set(HTTPLIB_IS_USING_CERTS_FROM_MACOSX_KEYCHAIN ${HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN})
110+
113111
# Threads needed for <thread> on some systems, and for <pthread.h> on Linux
114-
set(THREADS_PREFER_PTHREAD_FLAG true)
112+
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
115113
find_package(Threads REQUIRED)
116114
# Since Cmake v3.11, Crypto & SSL became optional when not specified as COMPONENTS.
117115
if(HTTPLIB_REQUIRE_OPENSSL)
118116
find_package(OpenSSL ${_HTTPLIB_OPENSSL_MIN_VER} COMPONENTS Crypto SSL REQUIRED)
117+
set(HTTPLIB_IS_USING_OPENSSL TRUE)
119118
elseif(HTTPLIB_USE_OPENSSL_IF_AVAILABLE)
120119
find_package(OpenSSL ${_HTTPLIB_OPENSSL_MIN_VER} COMPONENTS Crypto SSL QUIET)
121-
endif()
122-
# Just setting this variable here for people building in-tree
123-
if(OPENSSL_FOUND AND NOT DEFINED HTTPLIB_IS_USING_OPENSSL)
124-
set(HTTPLIB_IS_USING_OPENSSL TRUE)
120+
# Avoid a rare circumstance of not finding all components but the end-user did their
121+
# own call for OpenSSL, which might trick us into thinking we'd otherwise have what we wanted
122+
if (TARGET OpenSSL::SSL AND TARGET OpenSSL::Crypto)
123+
set(HTTPLIB_IS_USING_OPENSSL ${OPENSSL_FOUND})
124+
else()
125+
set(HTTPLIB_IS_USING_OPENSSL FALSE)
126+
endif()
125127
endif()
126128

127129
if(HTTPLIB_REQUIRE_ZLIB)
128130
find_package(ZLIB REQUIRED)
131+
set(HTTPLIB_IS_USING_ZLIB TRUE)
129132
elseif(HTTPLIB_USE_ZLIB_IF_AVAILABLE)
130133
find_package(ZLIB QUIET)
131-
endif()
132-
# Just setting this variable here for people building in-tree
133-
# FindZLIB doesn't have a ZLIB_FOUND variable, so check the target.
134-
if(TARGET ZLIB::ZLIB)
135-
set(HTTPLIB_IS_USING_ZLIB TRUE)
134+
# FindZLIB doesn't have a ZLIB_FOUND variable, so check the target.
135+
if(TARGET ZLIB::ZLIB)
136+
set(HTTPLIB_IS_USING_ZLIB TRUE)
137+
endif()
136138
endif()
137139

138140
# Adds our cmake folder to the search path for find_package
141+
# This is so we can use our custom FindBrotli.cmake
139142
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
140143
if(HTTPLIB_REQUIRE_BROTLI)
141144
find_package(Brotli COMPONENTS encoder decoder common REQUIRED)
145+
set(HTTPLIB_IS_USING_BROTLI TRUE)
142146
elseif(HTTPLIB_USE_BROTLI_IF_AVAILABLE)
143147
find_package(Brotli COMPONENTS encoder decoder common QUIET)
144-
endif()
145-
# Just setting this variable here for people building in-tree
146-
if(Brotli_FOUND)
147-
set(HTTPLIB_IS_USING_BROTLI TRUE)
148-
endif()
149-
150-
if(HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN)
151-
set(HTTPLIB_IS_USING_CERTS_FROM_MACOSX_KEYCHAIN TRUE)
148+
set(HTTPLIB_IS_USING_BROTLI ${Brotli_FOUND})
152149
endif()
153150

154151
# Used for default, common dirs that the end-user can change (if needed)
@@ -204,9 +201,7 @@ endif()
204201
add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
205202

206203
# Require C++11
207-
target_compile_features(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC}
208-
cxx_std_11
209-
)
204+
target_compile_features(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC} cxx_std_11)
210205

211206
target_include_directories(${PROJECT_NAME} SYSTEM ${_INTERFACE_OR_PUBLIC}
212207
$<BUILD_INTERFACE:${_httplib_build_includedir}>
@@ -273,9 +268,7 @@ if(HTTPLIB_INSTALL)
273268
# Creates the export httplibTargets.cmake
274269
# This is strictly what holds compilation requirements
275270
# and linkage information (doesn't find deps though).
276-
install(TARGETS ${PROJECT_NAME}
277-
EXPORT httplibTargets
278-
)
271+
install(TARGETS ${PROJECT_NAME} EXPORT httplibTargets)
279272

280273
install(FILES "${_httplib_build_includedir}/httplib.h" TYPE INCLUDE)
281274

0 commit comments

Comments
 (0)