Skip to content

Commit c214f51

Browse files
authored
cmake find_package() (#84)
1 parent 9bbcd31 commit c214f51

4 files changed

Lines changed: 77 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1010
- linting for C++ and bazel files.
1111
- Added CI status badges.
1212
- Added test coverage
13+
- Added support for cmake `FetchContent`.
14+
See README for details. [#75](https://github.com/tzaeschke/phtree-cpp/issues/75)
15+
- Added support for cmake `find_packet()` and direct import via `add_sub_directory()`.
16+
See README for details. [#83](https://github.com/tzaeschke/phtree-cpp/issues/83)
1317

1418
### Changed
1519
- Cleaned up build scripts. [#53](https://github.com/tzaeschke/phtree-cpp/issues/53)

CMakeLists.txt

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ endif ()
3434
# bench options
3535
option(PHTREE_BUILD_BENCHMARKS "Build benchmarks (Requires https://github.com/google/benchmark.git to be installed)" OFF)
3636

37+
# install options
38+
option(PHTREE_INSTALL "Generate the install target" OFF)
39+
3740

3841
# ---------------------------------------------------------------------------------------
3942
# Compiler config
@@ -82,7 +85,7 @@ else ()
8285
endif ()
8386
if (PHTREE_CODE_COVERAGE)
8487
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage") # -Wa,-mbig-obj")
85-
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
88+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
8689
endif ()
8790
endif ()
8891

@@ -98,8 +101,8 @@ add_library(phtree::phtree ALIAS phtree)
98101
target_compile_features(phtree INTERFACE cxx_std_17)
99102

100103
target_include_directories(phtree INTERFACE
101-
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
102-
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
104+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
105+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
103106

104107
if (PHTREE_BUILD_EXAMPLES OR PHTREE_BUILD_ALL)
105108
message(STATUS "Generating examples")
@@ -117,3 +120,42 @@ if (PHTREE_BUILD_TESTS OR PHTREE_BUILD_ALL)
117120
include(GoogleTest)
118121
add_subdirectory(test)
119122
endif ()
123+
124+
# ---------------------------------------------------------------------------------------
125+
# Install
126+
# ---------------------------------------------------------------------------------------
127+
if (PHTREE_INSTALL)
128+
include(GNUInstallDirs)
129+
130+
install(TARGETS phtree
131+
EXPORT ${PROJECT_NAME}_Targets
132+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
133+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
134+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
135+
136+
include(CMakePackageConfigHelpers)
137+
write_basic_package_version_file("phtreeConfigVersion.cmake"
138+
VERSION ${PROJECT_VERSION}
139+
COMPATIBILITY SameMajorVersion)
140+
141+
configure_package_config_file(
142+
"${PROJECT_SOURCE_DIR}/cmake/${PROJECT_NAME}Config.cmake.in"
143+
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
144+
INSTALL_DESTINATION
145+
${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake)
146+
147+
install(EXPORT ${PROJECT_NAME}_Targets
148+
FILE ${PROJECT_NAME}Targets.cmake
149+
NAMESPACE phtree::
150+
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake)
151+
152+
install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
153+
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
154+
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake)
155+
156+
install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/phtree
157+
DESTINATION include
158+
PATTERN "BUILD" EXCLUDE
159+
PATTERN "*.md" EXCLUDE)
160+
161+
endif ()

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,11 @@ bazel run //benchmark:update_mm_d_benchmark --config=benchmark -- --benchmark_c
594594
<a id="cmake"></a>
595595

596596
### cmake
597-
<!--With `FetchContent_...()`: ***NOTE This will only work once v1.4.0 has been released!***
597+
<!--
598+
The library supports three types of cmake dependency management, `FetchContent`, `find_package()` and `add_subfolder()`.
599+
All three approaches are used in [this example project](https://github.com/tzaeschke/test-phtree-cpp-cmake).
600+
#### FetchContent
601+
With `FetchContent_...()`: ***NOTE This will only work once v1.4.0 has been released!***
598602
```
599603
include(FetchContent)
600604
FetchContent_Declare(
@@ -605,6 +609,25 @@ FetchContent_Declare(
605609
)
606610
FetchContent_MakeAvailable(phtree)
607611
```
612+
613+
#### find_package()
614+
You need to build the library with:
615+
```
616+
mkdir out && cd out
617+
cmake .. -DPHTREE_INSTALL=on
618+
sudo cmake --build . --config Release --target install -- -j <number of cores>
619+
```
620+
Note that the option `CMAKE_INSTALL_PREFIX:PATH=...` does _not_ work.
621+
The library can then be included with:
622+
```
623+
find_package(phtree CONFIG REQUIRED)
624+
add_executable(ExampleProject example.cc)
625+
target_link_libraries(ExampleProject phtree::phtree)
626+
```
627+
628+
#### add_subfolder()
629+
For this you can simply copy the PH-Tree source code into your project (you can skip `benchmark` and `test`) and
630+
then include the folder with `add_subdirectory(phtree-cpp)`.
608631
-->
609632

610633
`cmake` uses `ccache` when available.

cmake/phtreeConfig.cmake.in

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

0 commit comments

Comments
 (0)