Skip to content

Commit 4266851

Browse files
committed
Generate and install exported targets configs
Once installed to a prefix, CMake's find_package(zsync2 ...) can be used to find libzsync2.
1 parent 1104ccb commit 4266851

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,6 @@ add_subdirectory(src)
8888

8989
# add unit tests
9090
add_subdirectory(tests)
91+
92+
# finally, generate export configs
93+
include(cmake/export.cmake)

cmake/export.cmake

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# make sure CMake won't export this package by default (valid from 3.15 onwards)
2+
cmake_minimum_required(VERSION 3.15)
3+
set(CMP0090 NEW)
4+
5+
# set up installion of exported targets (consider libzsync2 a namespace for use in CMake)
6+
install(
7+
TARGETS libzsync2
8+
DESTINATION lib
9+
EXPORT libzsync2
10+
)
11+
12+
# allow import from current build tree
13+
export(
14+
TARGETS libzsync2
15+
NAMESPACE zsync2::
16+
FILE libzsync2Targets.cmake
17+
)
18+
19+
# allow import from install tree
20+
# note that the targets must be install(... EXPORT zsync) in order for this to work (consider libzsync2 a namespace)
21+
install(
22+
EXPORT libzsync2
23+
DESTINATION lib/cmake/zsync2
24+
)
25+
26+
include(CMakePackageConfigHelpers)
27+
# generate the config file that is includes the exports
28+
configure_package_config_file(
29+
"${CMAKE_CURRENT_LIST_DIR}/zsync2Config.cmake.in"
30+
"${PROJECT_BINARY_DIR}/cmake/zsync2Config.cmake"
31+
INSTALL_DESTINATION "lib/cmake/zsync2"
32+
NO_SET_AND_CHECK_MACRO
33+
NO_CHECK_REQUIRED_COMPONENTS_MACRO
34+
)
35+
36+
write_basic_package_version_file(
37+
"${PROJECT_BINARY_DIR}/cmake/zsync2ConfigVersion.cmake"
38+
VERSION "${VERSION}"
39+
COMPATIBILITY AnyNewerVersion
40+
)
41+
42+
install(FILES
43+
"${PROJECT_BINARY_DIR}/cmake/zsync2Config.cmake"
44+
"${PROJECT_BINARY_DIR}/cmake/zsync2ConfigVersion.cmake"
45+
DESTINATION lib/cmake/zsync2
46+
)

cmake/zsync2Config.cmake.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@PACKAGE_INIT@
2+
3+
include ( "${CMAKE_CURRENT_LIST_DIR}/zsync2Targets.cmake" )

0 commit comments

Comments
 (0)