Skip to content

Commit

Permalink
Add modern CMake config targets (#339)
Browse files Browse the repository at this point in the history
The current vcpkg version of s2geometry has to patch the CMakeLists.txt and generate a Config.cmake.in file in order to create a modern CMake target file that can then be found by other packages via find_package.

Because this is a vcpkg addition, they mandate that this be placed in the unofficial namespace, in order to prevent any potential conflict with an eventual upstream config that might be introduced. This PR introduces the upstream config so that it can be used by anyone who builds the package and so that the vcpkg version no longer requires any patching at all.

The changes here are essentially duplicating what the vcpkg patch does, except that they drop the unofficial prefix to everything, since the config would now be coming from the primary source.
  • Loading branch information
jherico authored Jan 26, 2024
1 parent 1ff1671 commit 0f6bd97
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
25 changes: 24 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,9 @@ endif()
if (S2_USE_SYSTEM_INCLUDES)
target_include_directories(s2 SYSTEM PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)
else ()
target_include_directories(s2 PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)
target_include_directories(s2 PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
$<INSTALL_INTERFACE:include>)
endif ()

# Add version information to the target
Expand Down Expand Up @@ -432,6 +434,17 @@ install(TARGETS ${S2_TARGETS}
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}")

# Create an export type "s2Targets" detailing the targetable artifacts created
# by this project.
install(TARGETS s2 EXPORT s2Targets)
# Install the export targets as a CMake config file in the share/s2 folder so
# that they can referenced by downstream projects as `s2::s2` after a
# successful `find_package` call.
install(EXPORT s2Targets
NAMESPACE s2
FILE s2Targets.cmake
DESTINATION share/s2/)

if (BUILD_TESTS)
if (NOT GOOGLETEST_ROOT)
message(FATAL_ERROR "BUILD_TESTS requires GOOGLETEST_ROOT")
Expand Down Expand Up @@ -586,3 +599,13 @@ endif()
if (${SWIG_FOUND} AND ${Python3_FOUND})
add_subdirectory("src/python" python)
endif()

include(CMakePackageConfigHelpers)

# Generate the config file that includes the exports.
configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in
"${CMAKE_CURRENT_BINARY_DIR}/s2Config.cmake"
INSTALL_DESTINATION "share/s2/"
NO_SET_AND_CHECK_MACRO
NO_CHECK_REQUIRED_COMPONENTS_MACRO)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/s2Config.cmake DESTINATION "share/s2/")
3 changes: 3 additions & 0 deletions Config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@PACKAGE_INIT@

include("${CMAKE_CURRENT_LIST_DIR}/s2Targets.cmake")

0 comments on commit 0f6bd97

Please sign in to comment.