From 85fec73da7c9512858ac551ee2adc441fa92c782 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 30 Sep 2026 09:40:26 +0200 Subject: [PATCH] Make the json.tar.xz release archive configure standalone The json.tar.xz rule packed neither src/modules nor tests, but the top-level CMakeLists.txt needs src/modules for NLOHMANN_JSON_BUILD_MODULES=ON and defaulted JSON_BuildTests to ON for a main project. A standalone configure of the extracted archive failed at add_subdirectory(tests), and enabling the module failed at add_subdirectory(src/modules). Add src/modules to the archive, and default JSON_BuildTests to ON only if tests/CMakeLists.txt exists, so a repository checkout still builds the tests by default while the archive configures out of the box. Use GNU tar via a TAR variable (gtar if present, else tar), the same way SED is found, instead of hard-coding gtar. The library, its public API, and its ABI are unchanged. Part of #5716 Signed-off-by: Niels Lohmann --- CMakeLists.txt | 5 ++++- Makefile | 7 +++++-- docs/mkdocs/docs/integration/cmake.md | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4c43c23ff..18c6891c9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -42,8 +42,11 @@ endif() ## OPTIONS ## +# Build the tests by default only for the main project and only if the tests +# directory exists (the release archive json.tar.xz does not contain it). # VERSION_GREATER_EQUAL is not available in older CMake (< 3.7) -if(${MAIN_PROJECT} AND (${CMAKE_VERSION} VERSION_EQUAL 3.13 OR ${CMAKE_VERSION} VERSION_GREATER 3.13)) +if(${MAIN_PROJECT} AND (${CMAKE_VERSION} VERSION_EQUAL 3.13 OR ${CMAKE_VERSION} VERSION_GREATER 3.13) + AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tests/CMakeLists.txt") set(JSON_BuildTests_INIT ON) else() set(JSON_BuildTests_INIT OFF) diff --git a/Makefile b/Makefile index 196f87243..cf0d3c714 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,9 @@ # find GNU sed to use `-i` parameter SED:=$(shell command -v gsed || which sed) +# find GNU tar to use `--sort` and `--pax-option` parameters +TAR:=$(shell command -v gtar || which tar) + ########################################################################## # source files @@ -215,8 +218,8 @@ ChangeLog.md: # archive is created according to the advices of . json.tar.xz: mkdir json - rsync -R $(shell find LICENSE.MIT nlohmann_json.natvis CMakeLists.txt cmake/*.in include single_include -type f) json - gtar --sort=name --mtime="@$(shell git log -1 --pretty=%ct)" --owner=0 --group=0 --numeric-owner --pax-option=exthdr.name=%d/PaxHeaders/%f,delete=atime,delete=ctime --create --file - json | xz --compress -9e --threads=2 - > json.tar.xz + rsync -R $(shell find LICENSE.MIT nlohmann_json.natvis CMakeLists.txt cmake/*.in include single_include src/modules -type f) json + $(TAR) --sort=name --mtime="@$(shell git log -1 --pretty=%ct)" --owner=0 --group=0 --numeric-owner --pax-option=exthdr.name=%d/PaxHeaders/%f,delete=atime,delete=ctime --create --file - json | xz --compress -9e --threads=2 - > json.tar.xz rm -fr json # We use `-X` to make the resulting ZIP file reproducible, see diff --git a/docs/mkdocs/docs/integration/cmake.md b/docs/mkdocs/docs/integration/cmake.md index 71512cbd5..336ab07a1 100644 --- a/docs/mkdocs/docs/integration/cmake.md +++ b/docs/mkdocs/docs/integration/cmake.md @@ -125,7 +125,7 @@ automatically download a release as a dependency at configure time. ### `JSON_BuildTests` -Build the unit tests when [`BUILD_TESTING`](https://cmake.org/cmake/help/latest/command/enable_testing.html) is enabled. This option is `ON` by default if the library's CMake project is the top project. That is, when integrating the library as described above, the test suite is not built unless explicitly switched on with this option. +Build the unit tests when [`BUILD_TESTING`](https://cmake.org/cmake/help/latest/command/enable_testing.html) is enabled. This option is `ON` by default if the library's CMake project is the top project and the `tests` directory exists (the release archive `json.tar.xz` does not contain it). That is, when integrating the library as described above, the test suite is not built unless explicitly switched on with this option. ### `JSON_CI`