Skip to content
This repository was archived by the owner on Jun 12, 2025. It is now read-only.

Commit f198d02

Browse files
committed
Merge branch 'master' into merge-key-support
2 parents c7bf224 + 1d8ca1f commit f198d02

16 files changed

Lines changed: 215 additions & 38 deletions

File tree

.github/dependabot.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: "github-actions"
4+
directory: "/"
5+
schedule:
6+
interval: "monthly"
7+
groups:
8+
github-actions:
9+
patterns:
10+
- "*"
11+

.github/workflows/build.yml

Lines changed: 66 additions & 12 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:
@@ -14,6 +17,7 @@ jobs:
1417
os: [ubuntu-latest, windows-latest, macos-latest]
1518
cxx_standard: [11, 17, 20]
1619
build: [static, shared]
20+
googletest: [build, system]
1721
generator: ["Default Generator", "MinGW Makefiles"]
1822
exclude:
1923
- os: macos-latest
@@ -22,44 +26,94 @@ jobs:
2226
generator: "MinGW Makefiles"
2327
- os: ubuntu-latest
2428
generator: "MinGW Makefiles"
29+
- os: macos-latest
30+
googletest: system
31+
- os: windows-latest
32+
googletest: system
2533
env:
2634
YAML_BUILD_SHARED_LIBS: ${{ matrix.build == 'shared' && 'ON' || 'OFF' }}
35+
YAML_USE_SYSTEM_GTEST: ${{ matrix.googletest == 'system' && 'ON' || 'OFF' }}
2736
CMAKE_GENERATOR: >-
2837
${{format(matrix.generator != 'Default Generator' && '-G "{0}"' || '', matrix.generator)}}
38+
CMAKE_INSTALL_PREFIX: "${{ github.workspace }}/install-prefix"
39+
CMAKE_BUILD_TYPE: Debug
40+
CMAKE_CXX_FLAGS_DEBUG: ${{ matrix.googletest == 'build' && '-g -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC' || '-g' }}
2941
runs-on: ${{ matrix.os }}
3042
steps:
31-
- uses: actions/checkout@v2
3243

33-
- name: Get number of CPU cores
34-
uses: SimenB/github-actions-cpu-cores@v1
44+
- uses: awalsh128/cache-apt-pkgs-action@latest
45+
if: matrix.os == 'ubuntu-latest'
46+
with:
47+
packages: googletest libgmock-dev libgtest-dev
48+
version: 1.0
3549

36-
- name: Build Tests
37-
shell: bash
50+
- uses: actions/checkout@v4
51+
52+
- name: Configure
3853
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 -DYAML_CPP_SUPPORT_MERGE_KEYS=ON
40-
cd build && cmake --build . --parallel ${{ steps.cpu-cores.outputs.count }}
54+
cmake \
55+
${{ env.CMAKE_GENERATOR }} \
56+
-S "${{ github.workspace }}" \
57+
-B build \
58+
-D CMAKE_CXX_STANDARD=${{ matrix.cxx_standard }} \
59+
-D CMAKE_INSTALL_PREFIX="${{ env.CMAKE_INSTALL_PREFIX }}" \
60+
-D CMAKE_BUILD_TYPE=${{ env.CMAKE_BUILD_TYPE }} \
61+
-D CMAKE_CXX_FLAGS_DEBUG="${{ env.CMAKE_CXX_FLAGS_DEBUG }}" \
62+
-D YAML_BUILD_SHARED_LIBS=${{ env.YAML_BUILD_SHARED_LIBS }} \
63+
-D YAML_USE_SYSTEM_GTEST=${{ env.YAML_USE_SYSTEM_GTEST }} \
64+
-D YAML_CPP_BUILD_TESTS=ON \
65+
-DYAML_CPP_SUPPORT_MERGE_KEYS=ON
66+
67+
- name: Build
68+
run: |
69+
cmake \
70+
--build build \
71+
--config ${{ env.CMAKE_BUILD_TYPE }} \
72+
--verbose \
73+
--parallel
4174
4275
- name: Run Tests
4376
shell: bash
4477
run: |
45-
cd build && ctest -C Debug --output-on-failure --verbose
78+
ctest \
79+
--test-dir build \
80+
--build-config ${{ env.CMAKE_BUILD_TYPE }} \
81+
--output-on-failure \
82+
--verbose
83+
84+
- name: Install
85+
run: cmake --install build --config ${{ env.CMAKE_BUILD_TYPE }}
86+
87+
- name: Configure CMake package test
88+
run: |
89+
cmake \
90+
${{ env.CMAKE_GENERATOR }} \
91+
-S "${{ github.workspace }}/test/cmake" \
92+
-B consumer-build \
93+
-D CMAKE_BUILD_TYPE=${{ env.CMAKE_BUILD_TYPE }} \
94+
-D CMAKE_PREFIX_PATH="${{ env.CMAKE_INSTALL_PREFIX }}"
95+
96+
- name: Build CMake package test
97+
run: |
98+
cmake \
99+
--build consumer-build \
100+
--config ${{ env.CMAKE_BUILD_TYPE }} \
101+
--verbose
46102
47103
bazel-build:
48104
strategy:
49105
matrix:
50106
os: [ubuntu-latest, windows-latest, macos-latest]
51107
runs-on: ${{ matrix.os }}
52108
steps:
53-
- uses: actions/checkout@v2
109+
- uses: actions/checkout@v4
54110

55111
- name: Build
56-
shell: bash
57112
run: |
58113
cd "${{ github.workspace }}"
59114
bazel build :all
60115
61116
- name: Test
62-
shell: bash
63117
run: |
64118
cd "${{ github.workspace }}"
65119
bazel test test
@@ -70,7 +124,7 @@ jobs:
70124
os: [ubuntu-latest, windows-latest, macos-latest]
71125
runs-on: ${{ matrix.os }}
72126
steps:
73-
- uses: actions/checkout@v2
127+
- uses: actions/checkout@v4
74128

75129
- name: Build
76130
shell: bash
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Bazel Release
2+
3+
on:
4+
release:
5+
types: [published]
6+
7+
jobs:
8+
# A release archive is required for bzlmod
9+
# See: https://blog.bazel.build/2023/02/15/github-archive-checksum.html
10+
bazel-release-archive:
11+
runs-on: ubuntu-latest
12+
permissions:
13+
contents: write
14+
steps:
15+
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
16+
- run: git archive $GITHUB_REF -o "yaml-cpp-${GITHUB_REF:10}.tar.gz"
17+
- run: gh release upload ${GITHUB_REF:10} "yaml-cpp-${GITHUB_REF:10}.tar.gz"
18+
env:
19+
GH_TOKEN: ${{ github.token }}

CMakeLists.txt

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,20 @@ option(YAML_CPP_BUILD_CONTRIB "Enable yaml-cpp contrib in library" ON)
2525
option(YAML_CPP_BUILD_TOOLS "Enable parse tools" ON)
2626
option(YAML_BUILD_SHARED_LIBS "Build yaml-cpp shared library" ${BUILD_SHARED_LIBS})
2727
option(YAML_CPP_INSTALL "Enable generation of yaml-cpp install targets" ${YAML_CPP_MAIN_PROJECT})
28-
option(YAML_CPP_FORMAT_SOURCE "Format source" ON)
28+
option(YAML_CPP_FORMAT_SOURCE "Format source" ${YAML_CPP_MAIN_PROJECT})
29+
option(YAML_CPP_DISABLE_UNINSTALL "Disable uninstallation of yaml-cpp" OFF)
30+
option(YAML_USE_SYSTEM_GTEST "Use system googletest if found" OFF)
31+
option(YAML_ENABLE_PIC "Use Position-Independent Code " ON)
2932
option(YAML_CPP_SUPPORT_MERGE_KEYS "Support YAML merge keys ('<<') in yaml-cpp's executable targets. Use '#define YAML_CPP_SUPPORT_MERGE_KEYS' instead when linking from another project." OFF)
33+
3034
cmake_dependent_option(YAML_CPP_BUILD_TESTS
3135
"Enable yaml-cpp tests" OFF
3236
"BUILD_TESTING;YAML_CPP_MAIN_PROJECT" OFF)
3337
cmake_dependent_option(YAML_MSVC_SHARED_RT
3438
"MSVC: Build yaml-cpp with shared runtime libs (/MD)" ON
3539
"CMAKE_SYSTEM_NAME MATCHES Windows" OFF)
40+
set(YAML_CPP_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake/yaml-cpp"
41+
CACHE STRING "Path to install the CMake package to")
3642

3743
if (YAML_CPP_FORMAT_SOURCE)
3844
find_program(YAML_CPP_CLANG_FORMAT_EXE NAMES clang-format)
@@ -87,7 +93,7 @@ set_property(TARGET yaml-cpp
8793
CXX_STANDARD_REQUIRED ON)
8894

8995
if (NOT YAML_BUILD_SHARED_LIBS)
90-
set_property(TARGET yaml-cpp PROPERTY POSITION_INDEPENDENT_CODE ON)
96+
set_property(TARGET yaml-cpp PROPERTY POSITION_INDEPENDENT_CODE ${YAML_ENABLE_PIC})
9197
endif()
9298

9399
target_include_directories(yaml-cpp
@@ -146,13 +152,12 @@ set_target_properties(yaml-cpp PROPERTIES
146152
PROJECT_LABEL "yaml-cpp ${yaml-cpp-label-postfix}"
147153
DEBUG_POSTFIX "${CMAKE_DEBUG_POSTFIX}")
148154

149-
set(CONFIG_EXPORT_DIR "${CMAKE_INSTALL_LIBDIR}/cmake/yaml-cpp")
150-
set(EXPORT_TARGETS yaml-cpp)
155+
set(EXPORT_TARGETS yaml-cpp::yaml-cpp)
151156
configure_package_config_file(
152157
"${PROJECT_SOURCE_DIR}/yaml-cpp-config.cmake.in"
153158
"${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)
159+
INSTALL_DESTINATION "${YAML_CPP_INSTALL_CMAKEDIR}"
160+
PATH_VARS CMAKE_INSTALL_INCLUDEDIR CMAKE_INSTALL_LIBDIR)
156161
unset(EXPORT_TARGETS)
157162

158163
write_basic_package_version_file(
@@ -172,15 +177,14 @@ if (YAML_CPP_INSTALL)
172177
FILES_MATCHING PATTERN "*.h")
173178
install(EXPORT yaml-cpp-targets
174179
NAMESPACE yaml-cpp::
175-
DESTINATION "${CONFIG_EXPORT_DIR}")
180+
DESTINATION "${YAML_CPP_INSTALL_CMAKEDIR}")
176181
install(FILES
177182
"${PROJECT_BINARY_DIR}/yaml-cpp-config.cmake"
178183
"${PROJECT_BINARY_DIR}/yaml-cpp-config-version.cmake"
179-
DESTINATION "${CONFIG_EXPORT_DIR}")
184+
DESTINATION "${YAML_CPP_INSTALL_CMAKEDIR}")
180185
install(FILES "${PROJECT_BINARY_DIR}/yaml-cpp.pc"
181186
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
182187
endif()
183-
unset(CONFIG_EXPORT_DIR)
184188

185189
if(YAML_CPP_BUILD_TESTS)
186190
add_subdirectory(test)
@@ -199,7 +203,7 @@ if (YAML_CPP_FORMAT_SOURCE AND YAML_CPP_CLANG_FORMAT_EXE)
199203
endif()
200204

201205
# uninstall target
202-
if(NOT TARGET uninstall)
206+
if(YAML_CPP_INSTALL AND NOT YAML_CPP_DISABLE_UNINSTALL AND NOT TARGET uninstall)
203207
configure_file(
204208
"${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
205209
"${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,47 @@ cmake [-G generator] [-DYAML_BUILD_SHARED_LIBS=on|OFF] ..
3131

3232
* `yaml-cpp` builds a static library by default, you may want to build a shared library by specifying `-DYAML_BUILD_SHARED_LIBS=ON`.
3333

34+
* [Debug mode of the GNU standard C++
35+
library](https://gcc.gnu.org/onlinedocs/libstdc++/manual/debug_mode.html)
36+
can be used when both `yaml-cpp` and client code is compiled with the
37+
`_GLIBCXX_DEBUG` flag (e.g. by calling CMake with `-D
38+
CMAKE_CXX_FLAGS_DEBUG='-g -D_GLIBCXX_DEBUG'` option).
39+
40+
Note that for `yaml-cpp` unit tests to run successfully, the _GoogleTest_
41+
library also must be built with this flag, i.e. the system one cannot be
42+
used (the _YAML_USE_SYSTEM_GTEST_ CMake option must be _OFF_, which is the
43+
default).
44+
3445
* For more options on customizing the build, see the [CMakeLists.txt](https://github.com/jbeder/yaml-cpp/blob/master/CMakeLists.txt) file.
3546

3647
#### 2. Build it!
3748
* The command you'll need to run depends on the generator you chose earlier.
3849

3950
**Note:** To clean up, just remove the `build` directory.
4051

52+
## How to Integrate it within your project using CMake
53+
54+
You can use for example FetchContent :
55+
56+
```cmake
57+
include(FetchContent)
58+
59+
FetchContent_Declare(
60+
yaml-cpp
61+
GIT_REPOSITORY https://github.com/jbeder/yaml-cpp.git
62+
GIT_TAG <tag_name> # Can be a tag (yaml-cpp-x.x.x), a commit hash, or a branch name (master)
63+
)
64+
FetchContent_GetProperties(yaml-cpp)
65+
66+
if(NOT yaml-cpp_POPULATED)
67+
message(STATUS "Fetching yaml-cpp...")
68+
FetchContent_Populate(yaml-cpp)
69+
add_subdirectory(${yaml-cpp_SOURCE_DIR} ${yaml-cpp_BINARY_DIR})
70+
endif()
71+
72+
target_link_libraries(YOUR_LIBRARY PUBLIC yaml-cpp::yaml-cpp) # The library or executable that require yaml-cpp library
73+
```
74+
4175
## Recent Releases
4276

4377
[yaml-cpp 0.6.0](https://github.com/jbeder/yaml-cpp/releases/tag/yaml-cpp-0.6.0) released! This release requires C++11, and no longer depends on Boost.

include/yaml-cpp/emitfromevents.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Emitter;
2323
class EmitFromEvents : public EventHandler {
2424
public:
2525
EmitFromEvents(Emitter& emitter);
26+
~EmitFromEvents() override = default;
2627

2728
void OnDocumentStart(const Mark& mark) override;
2829
void OnDocumentEnd() override;

include/yaml-cpp/node/impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ struct as_if {
9797
if (!node.m_pNode)
9898
return fallback;
9999

100-
T t;
100+
T t = fallback;
101101
if (convert<T>::decode(node, t))
102102
return t;
103103
return fallback;

src/emitter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,8 @@ void Emitter::EmitEndSeq() {
213213
if (m_pState->CurGroupFlowType() == FlowType::Flow) {
214214
if (m_stream.comment())
215215
m_stream << "\n";
216-
m_stream << IndentTo(m_pState->CurIndent());
216+
if (originalType == FlowType::Block || m_pState->HasBegunNode())
217+
m_stream << IndentTo(m_pState->CurIndent());
217218
if (originalType == FlowType::Block) {
218219
m_stream << "[";
219220
} else {

src/emitterutils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,11 @@ bool IsValidPlainScalar(const std::string& str, FlowType::value flowType,
173173
}
174174

175175
// then check until something is disallowed
176-
static const RegEx& disallowed_flow =
176+
static const RegEx disallowed_flow =
177177
Exp::EndScalarInFlow() | (Exp::BlankOrBreak() + Exp::Comment()) |
178178
Exp::NotPrintable() | Exp::Utf8_ByteOrderMark() | Exp::Break() |
179179
Exp::Tab() | Exp::Ampersand();
180-
static const RegEx& disallowed_block =
180+
static const RegEx disallowed_block =
181181
Exp::EndScalar() | (Exp::BlankOrBreak() + Exp::Comment()) |
182182
Exp::NotPrintable() | Exp::Utf8_ByteOrderMark() | Exp::Break() |
183183
Exp::Tab() | Exp::Ampersand();

test/CMakeLists.txt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@ set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
44
set(BUILD_MOCK ON CACHE BOOL "" FORCE)
55
set(CMAKE_POLICY_DEFAULT_CMP0048 NEW)
66

7-
add_subdirectory(
8-
"${CMAKE_CURRENT_SOURCE_DIR}/gtest-1.11.0"
9-
"${CMAKE_CURRENT_BINARY_DIR}/prefix")
10-
11-
include_directories(SYSTEM "${CMAKE_CURRENT_SOURCE_DIR}/gtest-1.11.0/googletest/include")
7+
if(YAML_USE_SYSTEM_GTEST)
8+
find_package(GTest)
9+
if (NOT GTEST_FOUND)
10+
message(FATAL_ERROR "system googletest was requested but not found")
11+
endif()
12+
else()
13+
add_subdirectory(
14+
"${CMAKE_CURRENT_SOURCE_DIR}/gtest-1.11.0"
15+
"${CMAKE_CURRENT_BINARY_DIR}/prefix")
16+
include_directories(SYSTEM "${CMAKE_CURRENT_SOURCE_DIR}/gtest-1.11.0/googletest/include")
17+
endif()
1218

1319
set(test-new-api-pattern "new-api/*.cpp")
1420
set(test-source-pattern "*.cpp" "integration/*.cpp" "node/*.cpp")
@@ -41,6 +47,7 @@ target_link_libraries(yaml-cpp-tests
4147
PRIVATE
4248
Threads::Threads
4349
yaml-cpp
50+
gtest
4451
gmock)
4552

4653
set_property(TARGET yaml-cpp-tests PROPERTY CXX_STANDARD_REQUIRED ON)

0 commit comments

Comments
 (0)