From 7f0ca894e87ef8044a03e98ea262112cac060f9e Mon Sep 17 00:00:00 2001 From: wuyangfan <1102042793@qq.com> Date: Sun, 17 May 2026 23:51:56 +0800 Subject: [PATCH] fix(meson): install CMake package config files Install nlohmann_jsonConfig.cmake, nlohmann_jsonConfigVersion.cmake, and nlohmann_jsonTargets.cmake when building with Meson so consumers can use find_package(nlohmann_json) after a Meson install. Fixes #3885 --- cmake/nlohmann_jsonTargets.cmake | 13 +++++++++++++ meson.build | 31 +++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 cmake/nlohmann_jsonTargets.cmake diff --git a/cmake/nlohmann_jsonTargets.cmake b/cmake/nlohmann_jsonTargets.cmake new file mode 100644 index 0000000000..1e6709d9a2 --- /dev/null +++ b/cmake/nlohmann_jsonTargets.cmake @@ -0,0 +1,13 @@ +# Installed by Meson for find_package(nlohmann_json) compatibility with the CMake build. + +if(NOT TARGET nlohmann_json::nlohmann_json) + get_filename_component( + _nlohmann_json_include_dir + "${CMAKE_CURRENT_LIST_DIR}/../../../include" + ABSOLUTE) + add_library(nlohmann_json::nlohmann_json INTERFACE IMPORTED) + set_target_properties(nlohmann_json::nlohmann_json PROPERTIES + INTERFACE_COMPILE_FEATURES "cxx_std_11" + INTERFACE_INCLUDE_DIRECTORIES "${_nlohmann_json_include_dir}" + ) +endif() diff --git a/meson.build b/meson.build index a2d6e31a25..a31daa8ea4 100644 --- a/meson.build +++ b/meson.build @@ -21,4 +21,35 @@ pkgc.generate(name: 'nlohmann_json', version: meson.project_version(), description: 'JSON for Modern C++' ) + +cmake_dir = get_option('datadir') / 'cmake' / meson.project_name() +version_parts = meson.project_version().split('.') + +cmake_conf = configuration_data() +cmake_conf.set('PROJECT_NAME', meson.project_name()) +cmake_conf.set('NLOHMANN_JSON_TARGET_NAME', meson.project_name()) +cmake_conf.set('NLOHMANN_JSON_TARGETS_EXPORT_NAME', meson.project_name() + 'Targets') +cmake_conf.set('PROJECT_VERSION', meson.project_version()) +cmake_conf.set('PROJECT_VERSION_MAJOR', version_parts[0]) + +configure_file( + input: 'cmake/config.cmake.in', + output: meson.project_name() + 'Config.cmake', + configuration: cmake_conf, + install: true, + install_dir: cmake_dir, +) + +configure_file( + input: 'cmake/nlohmann_jsonConfigVersion.cmake.in', + output: meson.project_name() + 'ConfigVersion.cmake', + configuration: cmake_conf, + install: true, + install_dir: cmake_dir, +) + +install_data( + 'cmake/nlohmann_jsonTargets.cmake', + install_dir: cmake_dir, +) endif