Skip to content

Commit c26e047

Browse files
FtZPetruskajbeder
andauthored
CMake: Fixes for generated config (#1212)
* CMake: Add option to set the package install dir. * CMake: Fix generated config. - `YAML_CPP_SHARED_LIBS_BUILT` should not be set with a `PATH_VAR` as it would always evaluate to true. - `YAML_CPP_LIBRARIES` should used the exported target name including the namespace, but `check_required_components` shouldn't. - Use `CMAKE_CURRENT_LIST_DIR` to find the target file, instead of a `PATH_VAR`. Package managers such as vcpkg move CMake configs after installing. * CI: Test the generated CMake package. * CMake: Create add a deprecated yaml-cpp target. This target is meant to provide compatibility with versions prior to 0.8.0. * CMake: mark the yaml-cpp target as IMPORTED. --------- Co-authored-by: Jesse Beder <[email protected]>
1 parent ac144ed commit c26e047

File tree

5 files changed

+89
-22
lines changed

5 files changed

+89
-22
lines changed

.github/workflows/build.yml

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ on:
66
branches: [ master ]
77
workflow_dispatch:
88
permissions: read-all
9+
defaults:
10+
run:
11+
shell: bash
912
jobs:
1013
cmake-build:
1114
strategy:
@@ -26,40 +29,74 @@ jobs:
2629
YAML_BUILD_SHARED_LIBS: ${{ matrix.build == 'shared' && 'ON' || 'OFF' }}
2730
CMAKE_GENERATOR: >-
2831
${{format(matrix.generator != 'Default Generator' && '-G "{0}"' || '', matrix.generator)}}
32+
CMAKE_INSTALL_PREFIX: "${{ github.workspace }}/install-prefix"
33+
CMAKE_BUILD_TYPE: Debug
2934
runs-on: ${{ matrix.os }}
3035
steps:
31-
- uses: actions/checkout@v2
36+
- uses: actions/checkout@v3
3237

33-
- name: Get number of CPU cores
34-
uses: SimenB/github-actions-cpu-cores@v1
38+
- name: Configure
39+
run: |
40+
cmake \
41+
${{ env.CMAKE_GENERATOR }} \
42+
-S "${{ github.workspace }}" \
43+
-B build \
44+
-D CMAKE_CXX_STANDARD=${{ matrix.cxx_standard }} \
45+
-D CMAKE_INSTALL_PREFIX="${{ env.CMAKE_INSTALL_PREFIX }}" \
46+
-D CMAKE_BUILD_TYPE=${{ env.CMAKE_BUILD_TYPE }} \
47+
-D YAML_BUILD_SHARED_LIBS=${{ env.YAML_BUILD_SHARED_LIBS }} \
48+
-D YAML_CPP_BUILD_TESTS=ON
3549
36-
- name: Build Tests
37-
shell: bash
50+
- name: Build
3851
run: |
39-
cmake ${{ env.CMAKE_GENERATOR }} -S "${{ github.workspace }}" -B build -DCMAKE_CXX_STANDARD=${{ matrix.cxx_standard }} -DYAML_BUILD_SHARED_LIBS=${{ env.YAML_BUILD_SHARED_LIBS }} -DYAML_CPP_BUILD_TESTS=ON
40-
cd build && cmake --build . --parallel ${{ steps.cpu-cores.outputs.count }}
52+
cmake \
53+
--build build \
54+
--config ${{ env.CMAKE_BUILD_TYPE }} \
55+
--verbose \
56+
--parallel
4157
4258
- name: Run Tests
4359
shell: bash
4460
run: |
45-
cd build && ctest -C Debug --output-on-failure --verbose
61+
ctest \
62+
--test-dir build \
63+
--build-config ${{ env.CMAKE_BUILD_TYPE }} \
64+
--output-on-failure \
65+
--verbose
66+
67+
- name: Install
68+
run: cmake --install build --config ${{ env.CMAKE_BUILD_TYPE }}
69+
70+
- name: Configure CMake package test
71+
run: |
72+
cmake \
73+
${{ env.CMAKE_GENERATOR }} \
74+
-S "${{ github.workspace }}/test/cmake" \
75+
-B consumer-build \
76+
-D CMAKE_BUILD_TYPE=${{ env.CMAKE_BUILD_TYPE }} \
77+
-D CMAKE_PREFIX_PATH="${{ env.CMAKE_INSTALL_PREFIX }}"
78+
79+
- name: Build CMake package test
80+
run: |
81+
cmake \
82+
--build consumer-build \
83+
--config ${{ env.CMAKE_BUILD_TYPE }} \
84+
--verbose
4685
4786
bazel-build:
4887
strategy:
4988
matrix:
5089
os: [ubuntu-latest, windows-latest, macos-latest]
5190
runs-on: ${{ matrix.os }}
5291
steps:
53-
- uses: actions/checkout@v2
92+
- uses: actions/checkout@v3
5493

5594
- name: Build
56-
shell: bash
5795
run: |
5896
cd "${{ github.workspace }}"
5997
bazel build :all
6098
6199
- name: Test
62-
shell: bash
63100
run: |
64101
cd "${{ github.workspace }}"
65102
bazel test test

CMakeLists.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ cmake_dependent_option(YAML_CPP_BUILD_TESTS
3434
cmake_dependent_option(YAML_MSVC_SHARED_RT
3535
"MSVC: Build yaml-cpp with shared runtime libs (/MD)" ON
3636
"CMAKE_SYSTEM_NAME MATCHES Windows" OFF)
37-
37+
set(YAML_CPP_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake/yaml-cpp"
38+
CACHE STRING "Path to install the CMake package to")
39+
3840
if (YAML_CPP_FORMAT_SOURCE)
3941
find_program(YAML_CPP_CLANG_FORMAT_EXE NAMES clang-format)
4042
endif()
@@ -146,13 +148,12 @@ set_target_properties(yaml-cpp PROPERTIES
146148
PROJECT_LABEL "yaml-cpp ${yaml-cpp-label-postfix}"
147149
DEBUG_POSTFIX "${CMAKE_DEBUG_POSTFIX}")
148150

149-
set(CONFIG_EXPORT_DIR "${CMAKE_INSTALL_LIBDIR}/cmake/yaml-cpp")
150-
set(EXPORT_TARGETS yaml-cpp)
151+
set(EXPORT_TARGETS yaml-cpp::yaml-cpp)
151152
configure_package_config_file(
152153
"${PROJECT_SOURCE_DIR}/yaml-cpp-config.cmake.in"
153154
"${PROJECT_BINARY_DIR}/yaml-cpp-config.cmake"
154-
INSTALL_DESTINATION "${CONFIG_EXPORT_DIR}"
155-
PATH_VARS CMAKE_INSTALL_INCLUDEDIR CMAKE_INSTALL_LIBDIR CONFIG_EXPORT_DIR YAML_BUILD_SHARED_LIBS)
155+
INSTALL_DESTINATION "${YAML_CPP_INSTALL_CMAKEDIR}"
156+
PATH_VARS CMAKE_INSTALL_INCLUDEDIR CMAKE_INSTALL_LIBDIR)
156157
unset(EXPORT_TARGETS)
157158

158159
write_basic_package_version_file(
@@ -172,15 +173,14 @@ if (YAML_CPP_INSTALL)
172173
FILES_MATCHING PATTERN "*.h")
173174
install(EXPORT yaml-cpp-targets
174175
NAMESPACE yaml-cpp::
175-
DESTINATION "${CONFIG_EXPORT_DIR}")
176+
DESTINATION "${YAML_CPP_INSTALL_CMAKEDIR}")
176177
install(FILES
177178
"${PROJECT_BINARY_DIR}/yaml-cpp-config.cmake"
178179
"${PROJECT_BINARY_DIR}/yaml-cpp-config-version.cmake"
179-
DESTINATION "${CONFIG_EXPORT_DIR}")
180+
DESTINATION "${YAML_CPP_INSTALL_CMAKEDIR}")
180181
install(FILES "${PROJECT_BINARY_DIR}/yaml-cpp.pc"
181182
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
182183
endif()
183-
unset(CONFIG_EXPORT_DIR)
184184

185185
if(YAML_CPP_BUILD_TESTS)
186186
add_subdirectory(test)

test/cmake/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
project(yaml-cpp-consumer LANGUAGES CXX)
3+
4+
find_package(yaml-cpp CONFIG REQUIRED)
5+
get_target_property(LIBRARY_TYPE yaml-cpp::yaml-cpp TYPE)
6+
7+
if(LIBRARY_TYPE STREQUAL "SHARED_LIBRARY")
8+
if(NOT YAML_CPP_SHARED_LIBS_BUILT)
9+
message(FATAL_ERROR "Library type (${LIBRARY_TYPE}) contradicts config: ${YAML_CPP_SHARED_LIBS_BUILT}")
10+
endif()
11+
else()
12+
if(YAML_CPP_SHARED_LIBS_BUILT)
13+
message(FATAL_ERROR "Library type (${LIBRARY_TYPE}) contradicts config: ${YAML_CPP_SHARED_LIBS_BUILT}")
14+
endif()
15+
endif()
16+
17+
add_executable(main main.cpp)
18+
set_target_properties(main PROPERTIES CXX_STANDARD 11)
19+
target_link_libraries(main PRIVATE ${YAML_CPP_LIBRARIES})

test/cmake/main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include "yaml-cpp/yaml.h"
2+
3+
int main(int, char**) { YAML::Parser foo{}; }

yaml-cpp-config.cmake.in

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,20 @@ set_and_check(YAML_CPP_INCLUDE_DIR "@PACKAGE_CMAKE_INSTALL_INCLUDEDIR@")
1111
set_and_check(YAML_CPP_LIBRARY_DIR "@PACKAGE_CMAKE_INSTALL_LIBDIR@")
1212

1313
# Are we building shared libraries?
14-
set(YAML_CPP_SHARED_LIBS_BUILT "@PACKAGE_YAML_BUILD_SHARED_LIBS@")
14+
set(YAML_CPP_SHARED_LIBS_BUILT @YAML_BUILD_SHARED_LIBS@)
1515

1616
# Our library dependencies (contains definitions for IMPORTED targets)
17-
include(@PACKAGE_CONFIG_EXPORT_DIR@/yaml-cpp-targets.cmake)
17+
include("${CMAKE_CURRENT_LIST_DIR}/yaml-cpp-targets.cmake")
1818

1919
# These are IMPORTED targets created by yaml-cpp-targets.cmake
2020
set(YAML_CPP_LIBRARIES "@EXPORT_TARGETS@")
2121

22-
check_required_components(@EXPORT_TARGETS@)
22+
add_library(yaml-cpp INTERFACE IMPORTED)
23+
target_link_libraries(yaml-cpp INTERFACE yaml-cpp::yaml-cpp)
24+
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.17)
25+
set_target_properties(yaml-cpp PROPERTIES
26+
DEPRECATION "The target yaml-cpp is deprecated and will be removed in version 0.10.0. Use the yaml-cpp::yaml-cpp target instead."
27+
)
28+
endif()
29+
30+
check_required_components(yaml-cpp)

0 commit comments

Comments
 (0)