Skip to content

Commit 477572e

Browse files
committed
Improve installed Cmake Config
Adds automatic dependency finding (if they were used). Adds a way to require a specific version in the find_package(httplib) call. You should link against the httplib::httplib IMPORTED target, which is created automatically.
1 parent 7169750 commit 477572e

File tree

2 files changed

+68
-8
lines changed

2 files changed

+68
-8
lines changed

CMakeLists.txt

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,58 @@ target_compile_definitions(${PROJECT_NAME} INTERFACE
5858
$<$<BOOL:${OPENSSL_FOUND}>:CPPHTTPLIB_OPENSSL_SUPPORT>
5959
)
6060

61+
# Cmake's find_package search path is different based on the system
62+
# See https://cmake.org/cmake/help/latest/command/find_package.html for the list
63+
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
64+
set(_TARGET_INSTALL_CMAKEDIR "${CMAKE_INSTALL_PREFIX}/cmake/${PROJECT_NAME}")
65+
else()
66+
# On Non-Windows, it should be /usr/lib/cmake/<name>/<name>Config.cmake
67+
# NOTE: This may or may not work for macOS...
68+
set(_TARGET_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
69+
endif()
70+
71+
include(CMakePackageConfigHelpers)
72+
73+
# Configures the meta-file httplibConfig.cmake.in to replace variables with paths/values/etc.
74+
configure_package_config_file("${PROJECT_NAME}Config.cmake.in"
75+
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
76+
INSTALL_DESTINATION "${_TARGET_INSTALL_CMAKEDIR}"
77+
# Passes the includedir install path
78+
PATH_VARS CMAKE_INSTALL_FULL_INCLUDEDIR
79+
# There aren't any components, so don't use the macro
80+
NO_CHECK_REQUIRED_COMPONENTS_MACRO
81+
)
6182

62-
install(TARGETS ${PROJECT_NAME} EXPORT httplibConfig
63-
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
64-
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
65-
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
83+
# Creates the version file for version-checking with find_package(httplib)
84+
write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
85+
VERSION ${CMAKE_PROJECT_VERSION}
86+
# Controls how the version-checking works.
87+
# AnyNewerVersion just accepts any version >= the target version.
88+
# If SemVer is instead desired behaviour, use SameMajorVersion
89+
COMPATIBILITY AnyNewerVersion
90+
)
6691

92+
# Creates the export httplibTargets.cmake
93+
# This is strictly what holds compilation requirements
94+
# and linkage information (doesn't find deps though).
95+
install(TARGETS ${PROJECT_NAME}
96+
EXPORT httplibTargets
97+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
98+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
99+
)
67100

68-
install(EXPORT httplibConfig DESTINATION share/httplib/cmake)
69101
install(FILES httplib.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
70102

71-
export(TARGETS ${PROJECT_NAME} FILE httplibConfig.cmake)
103+
install(FILES
104+
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
105+
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
106+
DESTINATION ${_TARGET_INSTALL_CMAKEDIR}
107+
)
72108

73-
#add_subdirectory(example)
74-
#add_subdirectory(test)
109+
# NOTE: This path changes depending on if it's on Windows or Linux
110+
install(EXPORT httplibTargets
111+
# Puts the targets into the httplib namespace
112+
# So this makes httplib::httplib linkable after doing find_package(httplib)
113+
NAMESPACE ${PROJECT_NAME}::
114+
DESTINATION ${_TARGET_INSTALL_CMAKEDIR}
115+
)

httplibConfig.cmake.in

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Generates a macro to auto-configure everything
2+
@PACKAGE_INIT@
3+
4+
include(CMakeFindDependencyMacro)
5+
6+
# We add find_dependency calls here to not make the end-user have to call them.
7+
find_dependency(Threads REQUIRED)
8+
find_dependency(OpenSSL @_HTTPLIB_OPENSSL_MIN_VER@ QUIET)
9+
find_dependency(ZLIB QUIET)
10+
11+
# Lets the end-user find the header path if needed
12+
# This is helpful if you're using Cmake's pre-compiled header feature
13+
set_and_check(HTTPLIB_HEADER_PATH "@PACKAGE_CMAKE_INSTALL_FULL_INCLUDEDIR@/httplib.h")
14+
# Lets you check if these options were correctly enabled for your install
15+
set(HTTPLIB_IS_USING_OPENSSL @OPENSSL_FOUND@)
16+
set(HTTPLIB_IS_USING_ZLIB @ZLIB_FOUND@)
17+
18+
# Brings in the target library
19+
include("${CMAKE_CURRENT_LIST_DIR}/httplibTargets.cmake")

0 commit comments

Comments
 (0)