Skip to content

Commit 5fcd8f7

Browse files
authored
Add automatic versioning to Cmake (#505)
It pulls the version from the user-agent string in the header, so it will not need to be manually adjusted. This version file is installed so that you can check for a specific version with find_package(httplib) Also added HTTPLIB_INCLUDE_DIR (root path without header name), and HTTPLIB_LIBRARY (only if compiled). Added HTTPLIB_VERSION, and HTTPLIB_FOUND (although it's recommended to check if the target exists). Updated CMakeLists documentation for all this.
1 parent d9fe3fa commit 5fcd8f7

File tree

2 files changed

+68
-5
lines changed

2 files changed

+68
-5
lines changed

CMakeLists.txt

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,15 @@
3232
3333
-------------------------------------------------------------------------------
3434
35-
These three variables are available after you run find_package(httplib)
36-
* HTTPLIB_HEADER_PATH - this is the full path to the installed header.
35+
These variables are available after you run find_package(httplib)
36+
* HTTPLIB_HEADER_PATH - this is the full path to the installed header (e.g. /usr/include/httplib.h).
3737
* HTTPLIB_IS_USING_OPENSSL - a bool for if OpenSSL support is enabled.
3838
* HTTPLIB_IS_USING_ZLIB - a bool for if ZLIB support is enabled.
39-
* HTTPLIB_IS_COMPILED - a bool for if the library is header-only or compiled.
39+
* HTTPLIB_IS_COMPILED - a bool for if the library is compiled, or otherwise header-only.
40+
* HTTPLIB_INCLUDE_DIR - the root path to httplib's header (e.g. /usr/include).
41+
* HTTPLIB_LIBRARY - the full path to the library if compiled (e.g. /usr/lib/libhttplib.so).
42+
* HTTPLIB_VERSION - the project's version string.
43+
* HTTPLIB_FOUND - a bool for if the target was found.
4044
4145
Want to use precompiled headers (Cmake feature since v3.16)?
4246
It's as simple as doing the following (before linking):
@@ -48,7 +52,15 @@
4852
FindPython3 requires Cmake v3.12
4953
]]
5054
cmake_minimum_required(VERSION 3.12.0 FATAL_ERROR)
51-
project(httplib LANGUAGES CXX)
55+
56+
# Get the user agent and use it as a version
57+
# This gets the string with the user agent from the header.
58+
# This is so the maintainer doesn't actually need to update this manually.
59+
file(STRINGS httplib.h _user_agent_match REGEX "User\-Agent.*cpp\-httplib/([0-9]+\.?)+")
60+
# Need to pull out just the version for use
61+
string(REGEX MATCH "([0-9]+\\.?)+" _httplib_version "${_user_agent_match}")
62+
63+
project(httplib VERSION ${_httplib_version} LANGUAGES CXX)
5264

5365
# Change as needed to set an OpenSSL minimum version.
5466
# This is used in the installed Cmake config file.
@@ -205,6 +217,23 @@ configure_package_config_file("${PROJECT_NAME}Config.cmake.in"
205217
NO_CHECK_REQUIRED_COMPONENTS_MACRO
206218
)
207219

220+
if(HTTPLIB_COMPILE)
221+
write_basic_package_version_file("${PROJECT_NAME}ConfigVersion.cmake"
222+
# Example: if you find_package(httplib 0.5.4)
223+
# then anything >= 0.5 and <= 1.0 is accepted
224+
COMPATIBILITY SameMajorVersion
225+
)
226+
else()
227+
write_basic_package_version_file("${PROJECT_NAME}ConfigVersion.cmake"
228+
# Example: if you find_package(httplib 0.5.4)
229+
# then anything >= 0.5 and <= 1.0 is accepted
230+
COMPATIBILITY SameMajorVersion
231+
# Tells Cmake that it's a header-only lib
232+
# Mildly useful for end-users :)
233+
ARCH_INDEPENDENT
234+
)
235+
endif()
236+
208237
# Creates the export httplibTargets.cmake
209238
# This is strictly what holds compilation requirements
210239
# and linkage information (doesn't find deps though).
@@ -218,6 +247,7 @@ install(FILES "${_httplib_build_includedir}/httplib.h" DESTINATION ${CMAKE_INSTA
218247

219248
install(FILES
220249
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
250+
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
221251
DESTINATION ${_TARGET_INSTALL_CMAKEDIR}
222252
)
223253

httplibConfig.cmake.in

Lines changed: 34 additions & 1 deletion
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_VERSION @PROJECT_VERSION@)
910

1011
include(CMakeFindDependencyMacro)
1112

@@ -25,9 +26,41 @@ if(@HTTPLIB_IS_USING_ZLIB@)
2526
find_dependency(ZLIB REQUIRED)
2627
endif()
2728

28-
# Lets the end-user find the header path if needed
29+
# Mildly useful for end-users
30+
# Not really recommended to be used though
31+
set_and_check(HTTPLIB_INCLUDE_DIR "@PACKAGE_CMAKE_INSTALL_FULL_INCLUDEDIR@")
32+
# Lets the end-user find the header path with the header appended
2933
# This is helpful if you're using Cmake's pre-compiled header feature
3034
set_and_check(HTTPLIB_HEADER_PATH "@PACKAGE_CMAKE_INSTALL_FULL_INCLUDEDIR@/httplib.h")
3135

3236
# Brings in the target library
3337
include("${CMAKE_CURRENT_LIST_DIR}/httplibTargets.cmake")
38+
39+
# Ouputs a "found httplib /usr/include/httplib.h" message when using find_package(httplib)
40+
include(FindPackageMessage)
41+
if(TARGET httplib::httplib)
42+
set(HTTPLIB_FOUND TRUE)
43+
44+
# Since the compiled version has a lib, show that in the message
45+
if(@HTTPLIB_COMPILE@)
46+
# The list of configurations is most likely just 1 unless they installed a debug & release
47+
get_target_property(_httplib_configs httplib::httplib "IMPORTED_CONFIGURATIONS")
48+
# Need to loop since the "IMPORTED_LOCATION" property isn't want we want.
49+
# Instead, we need to find the IMPORTED_LOCATION_RELEASE or IMPORTED_LOCATION_DEBUG which has the lib path.
50+
foreach(_httplib_conf "${_httplib_configs}")
51+
# Grab the path to the lib and sets it to HTTPLIB_LIBRARY
52+
get_target_property(HTTPLIB_LIBRARY httplib::httplib "IMPORTED_LOCATION_${_httplib_conf}")
53+
# Check if we found it
54+
if(HTTPLIB_LIBRARY)
55+
break()
56+
endif()
57+
endforeach()
58+
59+
unset(_httplib_configs)
60+
unset(_httplib_conf)
61+
62+
find_package_message(httplib "Found httplib: ${HTTPLIB_LIBRARY} (found version \"${HTTPLIB_VERSION}\")" "[${HTTPLIB_LIBRARY}][${HTTPLIB_HEADER_PATH}]")
63+
else()
64+
find_package_message(httplib "Found httplib: ${HTTPLIB_HEADER_PATH} (found version \"${HTTPLIB_VERSION}\")" "[${HTTPLIB_HEADER_PATH}]")
65+
endif()
66+
endif()

0 commit comments

Comments
 (0)