Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions cmake/nlohmann_jsonTargets.cmake
Original file line number Diff line number Diff line change
@@ -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"
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Respect Meson includedir in imported target

The Meson-installed CMake target hard-codes the header location to ${prefix}/include, but Meson can install headers to a different path when --includedir (or an includedir option in packaging) is set. In that case, find_package(nlohmann_json) succeeds yet consumers get an imported target with a wrong INTERFACE_INCLUDE_DIRECTORIES, which breaks includes at compile time. The target path should be generated from the actual Meson includedir instead of a fixed ../../../include assumption.

Useful? React with 👍 / 👎.

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()
31 changes: 31 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading