1 | | From 4aad2b1666a4742743b04e765a34742512915674 Mon Sep 17 00:00:00 2001 |
2 | | From: Felix Schwitzer <flx107809@gmail.com> |
3 | | Date: Fri, 1 Apr 2022 05:26:47 +0200 |
4 | | Subject: [PATCH] Fix CMake export files (#1077) |
5 | | |
6 | | After configuring the file `yaml-cpp-config.cmake.in`, the result ends up with |
7 | | empty variables. (see also the discussion in #774). |
8 | | |
9 | | Rework this file and the call to `configure_package_config_file` according the |
10 | | cmake documentation |
11 | | (https://cmake.org/cmake/help/v3.22/module/CMakePackageConfigHelpers.html?highlight=configure_package_config#command:configure_package_config_file) |
12 | | to overcome this issue and allow a simple `find_package` after install. |
13 | | |
14 | | As there was some discussion about the place where to install the |
15 | | `yaml-cpp-config.cmake` file, e.g. #1055, factor out the install location into |
16 | | an extra variable to make it easier changing this location in the future. |
17 | | |
18 | | Also untabify CMakeLists.txt in some places to align with the other code parts in this file. |
19 | | --- |
20 | | CMakeLists.txt | 29 ++++++++++++++++++----------- |
21 | | yaml-cpp-config.cmake.in | 10 ++++++---- |
22 | | 2 files changed, 24 insertions(+), 15 deletions(-) |
23 | | |
24 | | diff --git CMakeLists.txt CMakeLists.txt |
25 | | index 564b7c8d1..ccc1964ea 100644 |
26 | | --- CMakeLists.txt |
27 | | +++ CMakeLists.txt |
28 | | @@ -133,10 +133,16 @@ set_target_properties(yaml-cpp PROPERTIES |
29 | | PROJECT_LABEL "yaml-cpp ${yaml-cpp-label-postfix}" |
30 | | DEBUG_POSTFIX "${CMAKE_DEBUG_POSTFIX}") |
31 | | |
32 | | +# FIXME(felix2012): A more common place for the cmake export would be |
33 | | +# `CMAKE_INSTALL_LIBDIR`, as e.g. done in ubuntu or in this project for GTest |
34 | | +set(CONFIG_EXPORT_DIR "${CMAKE_INSTALL_DATADIR}/cmake/yaml-cpp") |
35 | | +set(EXPORT_TARGETS yaml-cpp) |
36 | | configure_package_config_file( |
37 | | "${PROJECT_SOURCE_DIR}/yaml-cpp-config.cmake.in" |
38 | | "${PROJECT_BINARY_DIR}/yaml-cpp-config.cmake" |
39 | | - INSTALL_DESTINATION "${CMAKE_INSTALL_DATADIR}/cmake/yaml-cpp") |
40 | | + INSTALL_DESTINATION "${CONFIG_EXPORT_DIR}" |
41 | | + PATH_VARS CMAKE_INSTALL_INCLUDEDIR CONFIG_EXPORT_DIR) |
42 | | +unset(EXPORT_TARGETS) |
43 | | |
44 | | write_basic_package_version_file( |
45 | | "${PROJECT_BINARY_DIR}/yaml-cpp-config-version.cmake" |
46 | | @@ -145,30 +151,31 @@ write_basic_package_version_file( |
47 | | configure_file(yaml-cpp.pc.in yaml-cpp.pc @ONLY) |
48 | | |
49 | | if (YAML_CPP_INSTALL) |
50 | | - install(TARGETS yaml-cpp |
51 | | + install(TARGETS yaml-cpp |
52 | | EXPORT yaml-cpp-targets |
53 | | RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} |
54 | | LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} |
55 | | ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) |
56 | | - install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/ |
57 | | + install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/ |
58 | | DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} |
59 | | - FILES_MATCHING PATTERN "*.h") |
60 | | + FILES_MATCHING PATTERN "*.h") |
61 | | install(EXPORT yaml-cpp-targets |
62 | | - DESTINATION "${CMAKE_INSTALL_DATADIR}/cmake/yaml-cpp") |
63 | | - install(FILES |
64 | | - "${PROJECT_BINARY_DIR}/yaml-cpp-config.cmake" |
65 | | - "${PROJECT_BINARY_DIR}/yaml-cpp-config-version.cmake" |
66 | | - DESTINATION "${CMAKE_INSTALL_DATADIR}/cmake/yaml-cpp") |
67 | | + DESTINATION "${CONFIG_EXPORT_DIR}") |
68 | | + install(FILES |
69 | | + "${PROJECT_BINARY_DIR}/yaml-cpp-config.cmake" |
70 | | + "${PROJECT_BINARY_DIR}/yaml-cpp-config-version.cmake" |
71 | | + DESTINATION "${CONFIG_EXPORT_DIR}") |
72 | | install(FILES "${PROJECT_BINARY_DIR}/yaml-cpp.pc" |
73 | | DESTINATION ${CMAKE_INSTALL_DATADIR}/pkgconfig) |
74 | | endif() |
75 | | +unset(CONFIG_EXPORT_DIR) |
76 | | |
77 | | if(YAML_CPP_BUILD_TESTS) |
78 | | - add_subdirectory(test) |
79 | | + add_subdirectory(test) |
80 | | endif() |
81 | | |
82 | | if(YAML_CPP_BUILD_TOOLS) |
83 | | - add_subdirectory(util) |
84 | | + add_subdirectory(util) |
85 | | endif() |
86 | | |
87 | | if (YAML_CPP_CLANG_FORMAT_EXE) |
88 | | diff --git yaml-cpp-config.cmake.in yaml-cpp-config.cmake.in |
89 | | index 7b41e3f30..a7ace3dc0 100644 |
90 | | --- yaml-cpp-config.cmake.in |
91 | | +++ yaml-cpp-config.cmake.in |
92 | | @@ -3,12 +3,14 @@ |
93 | | # YAML_CPP_INCLUDE_DIR - include directory |
94 | | # YAML_CPP_LIBRARIES - libraries to link against |
95 | | |
96 | | -# Compute paths |
97 | | -get_filename_component(YAML_CPP_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH) |
98 | | -set(YAML_CPP_INCLUDE_DIR "@CONFIG_INCLUDE_DIRS@") |
99 | | +@PACKAGE_INIT@ |
100 | | + |
101 | | +set_and_check(YAML_CPP_INCLUDE_DIR "@PACKAGE_CMAKE_INSTALL_INCLUDEDIR@") |
102 | | |
103 | | # Our library dependencies (contains definitions for IMPORTED targets) |
104 | | -include("${YAML_CPP_CMAKE_DIR}/yaml-cpp-targets.cmake") |
105 | | +include(@PACKAGE_CONFIG_EXPORT_DIR@/yaml-cpp-targets.cmake) |
106 | | |
107 | | # These are IMPORTED targets created by yaml-cpp-targets.cmake |
108 | | set(YAML_CPP_LIBRARIES "@EXPORT_TARGETS@") |
109 | | + |
110 | | +check_required_components(@EXPORT_TARGETS@) |