Skip to content

Commit

Permalink
Generate and install a cmake configuration file, which can be automat…
Browse files Browse the repository at this point in the history
…ically

picked up by downstream cmake projects.

Downstream projects can link to Clipper2 or Clipper2Z by adding
'Clipper2::Clipper2' or 'Clipper2::Clipper2Z' as target dependency, and
don't need to implement a FindPackage module.
  • Loading branch information
mmassing committed Mar 17, 2023
1 parent 4990053 commit 4168fb5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
51 changes: 46 additions & 5 deletions CPP/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ set(PCFILEZ "${CMAKE_CURRENT_BINARY_DIR}/Clipper2Z.pc")
add_library(Clipper2 ${CLIPPER2_INC} ${CLIPPER2_SRC})

target_include_directories(Clipper2
PUBLIC Clipper2Lib/include
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/Clipper2Lib/include>
$<INSTALL_INTERFACE:include>
)

# Clipper2 but with USINGZ defined
Expand All @@ -47,7 +49,9 @@ add_library(Clipper2Z ${CLIPPER2_INC} ${CLIPPER2_SRC})
target_compile_definitions(Clipper2Z PUBLIC USINGZ)

target_include_directories(Clipper2Z
PUBLIC Clipper2Lib/include
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/Clipper2Lib/include>
$<INSTALL_INTERFACE:include>
)

if (MSVC)
Expand Down Expand Up @@ -212,15 +216,52 @@ endif()
file(COPY ../Tests/Polygons.txt DESTINATION ${CMAKE_BINARY_DIR} FILE_PERMISSIONS OWNER_READ GROUP_READ WORLD_READ )
endif()

# create package config file
include(CMakePackageConfigHelpers)
include(GNUInstallDirs)

write_basic_package_version_file("${PROJECT_BINARY_DIR}/Clipper2ConfigVersion.cmake" COMPATIBILITY AnyNewerVersion)

configure_package_config_file(
"Clipper2Config.cmake.in"
"${PROJECT_BINARY_DIR}/Clipper2Config.cmake"
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/clipper2
)

export(TARGETS Clipper2 Clipper2Z FILE "${PROJECT_BINARY_DIR}/Clipper2Targets.cmake")

# installation
install(TARGETS Clipper2 Clipper2Z EXPORT Clipper2-targets
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/clipper2
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
)

install(
FILES
${PROJECT_BINARY_DIR}/Clipper2Config.cmake
${PROJECT_BINARY_DIR}/Clipper2ConfigVersion.cmake
DESTINATION
${CMAKE_INSTALL_LIBDIR}/cmake/clipper2
)

install(
EXPORT
Clipper2-targets
NAMESPACE
Clipper2::
FILE
Clipper2Targets.cmake
DESTINATION
${CMAKE_INSTALL_LIBDIR}/cmake/clipper2
)

CONFIGURE_FILE(Clipper2.pc.cmakein "${PCFILE}" @ONLY)
set(PCFILE_LIB_SUFFIX "Z")
CONFIGURE_FILE(Clipper2.pc.cmakein "${PCFILEZ}" @ONLY)

install(TARGETS Clipper2 Clipper2Z
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/clipper2
)
install(FILES ${PCFILE} ${PCFILEZ} DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)


# disable exceptions
#string(REGEX REPLACE "/W[3|4]" "/w" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
#add_definitions(-D_HAS_EXCEPTIONS=0) # for STL
6 changes: 6 additions & 0 deletions CPP/Clipper2Config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@PACKAGE_INIT@

# Provide all our library targets to users.
include("${CMAKE_CURRENT_LIST_DIR}/Clipper2Targets.cmake")

check_required_components(Clipper2)

0 comments on commit 4168fb5

Please sign in to comment.