Skip to content

Commit 4aad2b1

Browse files
authored
Fix CMake export files (#1077)
After configuring the file `yaml-cpp-config.cmake.in`, the result ends up with empty variables. (see also the discussion in #774). Rework this file and the call to `configure_package_config_file` according the cmake documentation (https://cmake.org/cmake/help/v3.22/module/CMakePackageConfigHelpers.html?highlight=configure_package_config#command:configure_package_config_file) to overcome this issue and allow a simple `find_package` after install. As there was some discussion about the place where to install the `yaml-cpp-config.cmake` file, e.g. #1055, factor out the install location into an extra variable to make it easier changing this location in the future. Also untabify CMakeLists.txt in some places to align with the other code parts in this file.
1 parent a6db7e3 commit 4aad2b1

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

CMakeLists.txt

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,16 @@ set_target_properties(yaml-cpp PROPERTIES
133133
PROJECT_LABEL "yaml-cpp ${yaml-cpp-label-postfix}"
134134
DEBUG_POSTFIX "${CMAKE_DEBUG_POSTFIX}")
135135

136+
# FIXME(felix2012): A more common place for the cmake export would be
137+
# `CMAKE_INSTALL_LIBDIR`, as e.g. done in ubuntu or in this project for GTest
138+
set(CONFIG_EXPORT_DIR "${CMAKE_INSTALL_DATADIR}/cmake/yaml-cpp")
139+
set(EXPORT_TARGETS yaml-cpp)
136140
configure_package_config_file(
137141
"${PROJECT_SOURCE_DIR}/yaml-cpp-config.cmake.in"
138142
"${PROJECT_BINARY_DIR}/yaml-cpp-config.cmake"
139-
INSTALL_DESTINATION "${CMAKE_INSTALL_DATADIR}/cmake/yaml-cpp")
143+
INSTALL_DESTINATION "${CONFIG_EXPORT_DIR}"
144+
PATH_VARS CMAKE_INSTALL_INCLUDEDIR CONFIG_EXPORT_DIR)
145+
unset(EXPORT_TARGETS)
140146

141147
write_basic_package_version_file(
142148
"${PROJECT_BINARY_DIR}/yaml-cpp-config-version.cmake"
@@ -145,30 +151,31 @@ write_basic_package_version_file(
145151
configure_file(yaml-cpp.pc.in yaml-cpp.pc @ONLY)
146152

147153
if (YAML_CPP_INSTALL)
148-
install(TARGETS yaml-cpp
154+
install(TARGETS yaml-cpp
149155
EXPORT yaml-cpp-targets
150156
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
151157
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
152158
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
153-
install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/
159+
install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/
154160
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
155-
FILES_MATCHING PATTERN "*.h")
161+
FILES_MATCHING PATTERN "*.h")
156162
install(EXPORT yaml-cpp-targets
157-
DESTINATION "${CMAKE_INSTALL_DATADIR}/cmake/yaml-cpp")
158-
install(FILES
159-
"${PROJECT_BINARY_DIR}/yaml-cpp-config.cmake"
160-
"${PROJECT_BINARY_DIR}/yaml-cpp-config-version.cmake"
161-
DESTINATION "${CMAKE_INSTALL_DATADIR}/cmake/yaml-cpp")
163+
DESTINATION "${CONFIG_EXPORT_DIR}")
164+
install(FILES
165+
"${PROJECT_BINARY_DIR}/yaml-cpp-config.cmake"
166+
"${PROJECT_BINARY_DIR}/yaml-cpp-config-version.cmake"
167+
DESTINATION "${CONFIG_EXPORT_DIR}")
162168
install(FILES "${PROJECT_BINARY_DIR}/yaml-cpp.pc"
163169
DESTINATION ${CMAKE_INSTALL_DATADIR}/pkgconfig)
164170
endif()
171+
unset(CONFIG_EXPORT_DIR)
165172

166173
if(YAML_CPP_BUILD_TESTS)
167-
add_subdirectory(test)
174+
add_subdirectory(test)
168175
endif()
169176

170177
if(YAML_CPP_BUILD_TOOLS)
171-
add_subdirectory(util)
178+
add_subdirectory(util)
172179
endif()
173180

174181
if (YAML_CPP_CLANG_FORMAT_EXE)

yaml-cpp-config.cmake.in

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
# YAML_CPP_INCLUDE_DIR - include directory
44
# YAML_CPP_LIBRARIES - libraries to link against
55

6-
# Compute paths
7-
get_filename_component(YAML_CPP_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
8-
set(YAML_CPP_INCLUDE_DIR "@CONFIG_INCLUDE_DIRS@")
6+
@PACKAGE_INIT@
7+
8+
set_and_check(YAML_CPP_INCLUDE_DIR "@PACKAGE_CMAKE_INSTALL_INCLUDEDIR@")
99

1010
# Our library dependencies (contains definitions for IMPORTED targets)
11-
include("${YAML_CPP_CMAKE_DIR}/yaml-cpp-targets.cmake")
11+
include(@PACKAGE_CONFIG_EXPORT_DIR@/yaml-cpp-targets.cmake)
1212

1313
# These are IMPORTED targets created by yaml-cpp-targets.cmake
1414
set(YAML_CPP_LIBRARIES "@EXPORT_TARGETS@")
15+
16+
check_required_components(@EXPORT_TARGETS@)

0 commit comments

Comments
 (0)