Skip to content

Commit 424bad3

Browse files
authored
[v3.1.3] Enable CMake install (#16)
* Add serialize test case for escaped strings * Enable install * Fix MinGW
1 parent 2fd1318 commit 424bad3

File tree

10 files changed

+156
-15
lines changed

10 files changed

+156
-15
lines changed

.github/workflows/ci.yml

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ jobs:
1414
steps:
1515
- uses: actions/checkout@v4
1616
- name: init
17-
run: uname -m
17+
run: |
18+
uname -m
19+
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-14 10
1820
- name: configure
19-
run: cmake -S . --preset=ninja-gcc -B build -DCMAKE_C_COMPILER=gcc-14 -DCMAKE_CXX_COMPILER=g++-14
21+
run: cmake -S . --preset=ninja-gcc -B build
2022
- name: build debug
2123
run: cmake --build build --config=Debug -- -v
2224
- name: build release
@@ -25,16 +27,18 @@ jobs:
2527
run: cd build && ctest -V -C Debug
2628
- name: test release
2729
run: cd build && ctest -V -C Release
30+
- name: test install
31+
run: cmake -DBUILD_DIR=build -DPRESET=ninja-gcc -P install_test/script.cmake
2832
x64-linux-clang:
2933
runs-on: ubuntu-latest
3034
steps:
3135
- uses: actions/checkout@v4
3236
- name: init
3337
run: |
3438
uname -m
35-
sudo apt update -yqq && sudo apt install -yqq clang-19
39+
sudo apt update -yqq && sudo apt install -yqq clang-20
3640
sudo update-alternatives --remove-all clang++
37-
sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-19 10
41+
sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-20 10
3842
- name: configure
3943
run: cmake -S . --preset=ninja-clang -B build
4044
- name: build debug
@@ -45,12 +49,16 @@ jobs:
4549
run: cd build && ctest -V -C Debug
4650
- name: test release
4751
run: cd build && ctest -V -C Release
52+
- name: test install
53+
run: cmake -DBUILD_DIR=build -DPRESET=ninja-clang -P install_test/script.cmake
4854
arm64-linux-gcc:
4955
runs-on: ubuntu-24.04-arm
5056
steps:
5157
- uses: actions/checkout@v4
5258
- name: init
53-
run: uname -m
59+
run: |
60+
uname -m
61+
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-14 10
5462
- name: configure
5563
run: cmake -S . --preset=ninja-gcc -B build -DCMAKE_C_COMPILER=gcc-14 -DCMAKE_CXX_COMPILER=g++-14
5664
- name: build debug
@@ -61,16 +69,18 @@ jobs:
6169
run: cd build && ctest -V -C Debug
6270
- name: test release
6371
run: cd build && ctest -V -C Release
72+
- name: test install
73+
run: cmake -DBUILD_DIR=build -DPRESET=ninja-gcc -P install_test/script.cmake
6474
arm64-linux-clang:
6575
runs-on: ubuntu-24.04-arm
6676
steps:
6777
- uses: actions/checkout@v4
6878
- name: init
6979
run: |
7080
uname -m
71-
sudo apt update -yqq && sudo apt install -yqq clang-19
81+
sudo apt update -yqq && sudo apt install -yqq clang-20
7282
sudo update-alternatives --remove-all clang++
73-
sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-19 10
83+
sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-20 10
7484
- name: configure
7585
run: cmake -S . --preset=ninja-clang -B build
7686
- name: build debug
@@ -81,6 +91,8 @@ jobs:
8191
run: cd build && ctest -V -C Debug
8292
- name: test release
8393
run: cd build && ctest -V -C Release
94+
- name: test install
95+
run: cmake -DBUILD_DIR=build -DPRESET=ninja-clang -P install_test/script.cmake
8496
x64-windows-vs22:
8597
runs-on: windows-2025
8698
steps:
@@ -95,17 +107,37 @@ jobs:
95107
run: cd build && ctest -V -C Debug
96108
- name: test release
97109
run: cd build && ctest -V -C Release
110+
- name: test install
111+
run: cmake -DBUILD_DIR=build -DPRESET=vs22 -P install_test/script.cmake
98112
x64-windows-clang:
99113
runs-on: windows-2025
100114
steps:
101115
- uses: actions/checkout@v4
102116
- name: configure
103-
run: cmake -S . --preset=ninja-clang -B clang
117+
run: cmake -S . --preset=ninja-clang -B build
118+
- name: build debug
119+
run: cmake --build build --config=Debug -- -v
120+
- name: build release
121+
run: cmake --build build --config=Release -- -v
122+
- name: test debug
123+
run: cd build && ctest -V -C Debug
124+
- name: test release
125+
run: cd build && ctest -V -C Release
126+
- name: test install
127+
run: cmake -DBUILD_DIR=build -DPRESET=ninja-clang -P install_test/script.cmake
128+
x64-mingw-gcc:
129+
runs-on: windows-2025
130+
steps:
131+
- uses: actions/checkout@v4
132+
- name: configure
133+
run: cmake -S . --preset=ninja-gcc -B build
104134
- name: build debug
105-
run: cmake --build clang --config=Debug -- -v
135+
run: cmake --build build --config=Debug -- -v
106136
- name: build release
107-
run: cmake --build clang --config=Release -- -v
137+
run: cmake --build build --config=Release -- -v
108138
- name: test debug
109-
run: cd clang && ctest -V -C Debug
139+
run: cd build && ctest -V -C Debug
110140
- name: test release
111-
run: cd clang && ctest -V -C Release
141+
run: cd build && ctest -V -C Release
142+
- name: test install
143+
run: cmake -DBUILD_DIR=build -DPRESET=ninja-gcc -P install_test/script.cmake

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ include(version.cmake)
99

1010
project(djson VERSION ${djson_version})
1111

12+
option(DJ_INSTALL "Setup djson install" ${PROJECT_IS_TOP_LEVEL})
1213
option(DJ_BUILD_TESTS "Build djson tests" ${PROJECT_IS_TOP_LEVEL})
1314

1415
configure_file(Doxyfile.in Doxyfile @ONLY)

install_test/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
exe/CMakePresets.json
2+
/build
3+
/packages

install_test/exe/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
cmake_minimum_required(VERSION 3.23)
2+
3+
set(CMAKE_CXX_STANDARD 23)
4+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
5+
set(CMAKE_CXX_EXTENSIONS OFF)
6+
7+
project(exe)
8+
9+
add_executable(${PROJECT_NAME})
10+
11+
find_package(djson CONFIG REQUIRED)
12+
13+
target_link_libraries(${PROJECT_NAME} PRIVATE djson::djson)
14+
15+
target_sources(${PROJECT_NAME} PRIVATE main.cpp)

install_test/exe/main.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#include <djson/build_version.hpp>
2+
#include <print>
3+
4+
auto main() -> int { std::println("djson version: {}", dj::build_version_v); }

install_test/script.cmake

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
if("${PRESET}" STREQUAL "")
2+
message(FATAL_ERROR "Invalid PRESET: ${PRESET}")
3+
endif()
4+
5+
if("${BUILD_DIR}" STREQUAL "")
6+
message(FATAL_ERROR "Invalid BUILD_DIR: ${BUILD_DIR}")
7+
endif()
8+
9+
message(STATUS "djson install test")
10+
11+
execute_process(
12+
COMMAND ${CMAKE_COMMAND} --install ${BUILD_DIR} --config=Debug --prefix=install_test/packages
13+
COMMAND_ERROR_IS_FATAL ANY
14+
)
15+
16+
execute_process(
17+
COMMAND ${CMAKE_COMMAND} --install ${BUILD_DIR} --config=Release --prefix=install_test/packages
18+
COMMAND_ERROR_IS_FATAL ANY
19+
)
20+
21+
execute_process(
22+
COMMAND cp -f CMakePresets.json install_test/exe
23+
COMMAND_ERROR_IS_FATAL ANY
24+
)
25+
26+
execute_process(
27+
COMMAND ${CMAKE_COMMAND} -S install_test/exe --preset=${PRESET} -B=install_test/build -DCMAKE_INSTALL_PREFIX=install_test/packages
28+
COMMAND_ERROR_IS_FATAL ANY
29+
COMMAND_ECHO STDOUT
30+
)
31+
32+
execute_process(
33+
COMMAND ${CMAKE_COMMAND} --build install_test/build --config=Debug
34+
COMMAND_ERROR_IS_FATAL ANY
35+
)
36+
37+
execute_process(
38+
COMMAND ${CMAKE_COMMAND} --build install_test/build --config=Release
39+
COMMAND_ERROR_IS_FATAL ANY
40+
)
41+
42+
execute_process(
43+
COMMAND install_test/build/Debug/exe
44+
COMMAND_ERROR_IS_FATAL ANY
45+
)
46+
47+
execute_process(
48+
COMMAND install_test/build/Release/exe
49+
COMMAND_ERROR_IS_FATAL ANY
50+
)

lib/CMakeLists.txt

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
project(djson-lib VERSION ${djson_version})
2-
31
set(version_file "${CMAKE_CURRENT_BINARY_DIR}/include/djson/build_version.hpp")
42
configure_file(src/build_version.hpp.in "${version_file}" @ONLY)
53

@@ -27,3 +25,25 @@ target_sources(${PROJECT_NAME} PRIVATE
2725
)
2826

2927
target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_23)
28+
29+
target_link_libraries(${PROJECT_NAME} PRIVATE
30+
$<$<BOOL:${MINGW}>:stdc++exp>
31+
)
32+
33+
if(DJ_INSTALL)
34+
message(STATUS "[djson] setting up install")
35+
include(GNUInstallDirs)
36+
37+
install(
38+
TARGETS djson
39+
EXPORT djson-targets
40+
FILE_SET HEADERS
41+
)
42+
43+
install(
44+
EXPORT djson-targets
45+
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/djson"
46+
NAMESPACE djson::
47+
FILE djson-config.cmake
48+
)
49+
endif()

lib/src/djson.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ auto Parser::from_operator(token::Operator const op) -> Json {
217217
template <typename T>
218218
auto Parser::make_number(token::Number const in) -> Json {
219219
auto value = T{};
220+
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
220221
auto const* end = in.raw_str.data() + in.raw_str.size();
221222
auto const [ptr, ec] = std::from_chars(in.raw_str.data(), end, value);
222223
if (ec != std::errc{} || ptr != end) { throw make_error(Error::Type::InvalidNumber); }

tests/test_serialize.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,19 @@ TEST(serialize_object) {
7171
std::println("serialized: {}", str);
7272
EXPECT(str == expected);
7373
}
74+
75+
TEST(serialize_escape) {
76+
std::string_view text = R"(test "quoted" text)";
77+
std::string_view expected = R"("test \"quoted\" text")";
78+
auto json = Json{};
79+
json.set_string(text);
80+
auto str = json.serialize(SerializeOptions{.flags = SerializeFlag::NoSpaces});
81+
std::println("serialized:\n{}\nexpected:\n{}", str, expected);
82+
EXPECT(str == expected);
83+
84+
auto result = Json::parse(str);
85+
ASSERT(result);
86+
json = std::move(*result);
87+
EXPECT(json.as_string() == text);
88+
}
7489
} // namespace

version.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
set(djson_version "3.1.2")
1+
set(djson_version "3.1.3")

0 commit comments

Comments
 (0)