Skip to content

Commit 6365e81

Browse files
committed
Reapply "Create proper install target export, and find_package machinery"
This reverts commit 8660aed.
1 parent 8660aed commit 6365e81

File tree

1 file changed

+50
-3
lines changed

1 file changed

+50
-3
lines changed

CMakeLists.txt

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
# Licensed under and subject to the terms of the LICENSE file accompanying this distribution.
33
# Official repository: https://github.com/jimmyorourke/plotlypp
44

5-
cmake_minimum_required(VERSION 3.13)
6-
project(plotlypp LANGUAGES CXX)
5+
cmake_minimum_required(VERSION 3.14)
6+
project(plotlypp VERSION 0.1.0 LANGUAGES CXX)
77

88
set(MAIN_PROJECT OFF)
99
if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
@@ -33,22 +33,69 @@ else()
3333
include(FetchContent)
3434

3535
set(FETCHCONTENT_BASE_DIR ${PLOTLYPP_FETCH_CONTENT_DIR})
36+
# Enable nlohmann::json install targets. Otherwise the plotlypp install targets complain the "local" dependency
37+
# is not being exported.
38+
set(JSON_Install ON)
3639
FetchContent_Declare(json URL https://github.com/nlohmann/json/releases/download/v3.11.3/json.tar.xz)
3740
FetchContent_MakeAvailable(json)
3841
endif()
3942
set(JSON_TARGET nlohmann_json::nlohmann_json)
4043

4144
add_library(plotlypp INTERFACE)
42-
# Add a namespaced alias
45+
# Add a namespaced alias for in-tree users.
4346
add_library(plotlypp::plotlypp ALIAS plotlypp)
47+
4448
target_link_libraries(plotlypp INTERFACE ${JSON_TARGET})
49+
4550
target_include_directories(plotlypp INTERFACE
4651
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
4752
$<INSTALL_INTERFACE:include>
4853
)
54+
4955
# Require at least C++17. Depending executables can override with a newer version.
5056
target_compile_features(plotlypp INTERFACE cxx_std_17)
5157

58+
include(GNUInstallDirs)
59+
include(CMakePackageConfigHelpers)
60+
61+
# Install headers.
62+
install(DIRECTORY include/plotlypp DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
63+
64+
# Register the target with the export set.
65+
install(TARGETS plotlypp
66+
EXPORT ${PROJECT_NAME}Targets
67+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
68+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
69+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
70+
)
71+
72+
# Install/generate the cmake file for rebuilding the export set targets.
73+
install(EXPORT ${PROJECT_NAME}Targets
74+
FILE ${PROJECT_NAME}Targets.cmake
75+
NAMESPACE ${PROJECT_NAME}::
76+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
77+
)
78+
79+
# Write the find package config file.
80+
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
81+
"include(CMakeFindDependencyMacro)
82+
find_dependency(nlohmann_json)
83+
include(\"\${CMAKE_CURRENT_LIST_DIR}/${PROJECT_NAME}Targets.cmake\")"
84+
)
85+
86+
# Write the find package version file.
87+
write_basic_package_version_file(
88+
${PROJECT_NAME}ConfigVersion.cmake
89+
VERSION ${PROJECT_VERSION}
90+
COMPATIBILITY AnyNewerVersion
91+
)
92+
93+
# Install find package config files.
94+
install(FILES
95+
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
96+
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
97+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
98+
)
5299

53100
if(PLOTLYPP_BUILD_EXAMPLES)
54101
add_subdirectory(examples)

0 commit comments

Comments
 (0)