Skip to content
Open
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
17 changes: 16 additions & 1 deletion FILES.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,22 @@ The file can be updated by calling
make BUILD.bazel
```

### `meson.build`
### `meson.build` and `meson_options.txt`

Meson build definitions suitable for use as a subproject ("wrap" in Meson terminology).

Projects wishing to use the wrap can execute:
```sh
meson wrap install nlohmann_json
```

Which allows Meson to build from source when a system provided dependency isn't available.

To build directly:
```sh
meson setup builddir
ninja -C builddir
```

### `Package.swift`

Expand Down
40 changes: 28 additions & 12 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,39 @@ project('nlohmann_json',
'cpp',
version : '3.12.0',
license : 'MIT',
meson_version : '>= 0.64',
default_options: ['cpp_std=c++11'],
)

nlohmann_json_dep = declare_dependency(
include_directories: include_directories('single_include')
)
if get_option('MultipleHeaders')
incdir = 'include'
else
incdir = 'single_include'
endif

nlohmann_json_multiple_headers = declare_dependency(
include_directories: include_directories('include')
cpp_args = [
'-DJSON_USE_GLOBAL_UDLS=@0@'.format(
(not get_option('GlobalUDLs')).to_int()),
'-DJSON_USE_IMPLICIT_CONVERSIONS=@0@'.format(
(not get_option('ImplicitConversions')).to_int()),
]

nlohmann_json_dep = declare_dependency(
compile_args: cpp_args,
include_directories: include_directories(incdir)
)
meson.override_dependency('nlohmann_json', nlohmann_json_dep)

if not meson.is_subproject()
install_headers('single_include/nlohmann/json.hpp', subdir: 'nlohmann')
install_headers('single_include/nlohmann/json_fwd.hpp', subdir: 'nlohmann')
install_subdir(
incdir / 'nlohmann',
install_dir: get_option('includedir'),
install_tag: 'devel',
)

pkgc = import('pkgconfig')
pkgc.generate(name: 'nlohmann_json',
version: meson.project_version(),
description: 'JSON for Modern C++'
)
pkgc = import('pkgconfig')
pkgc.generate(name: 'nlohmann_json',
version: meson.project_version(),
description: 'JSON for Modern C++'
)
endif
18 changes: 18 additions & 0 deletions meson_options.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
option(
'MultipleHeaders',
type: 'boolean',
value: true,
description: 'Use non-amalgamated version of the library',
)
option(
'GlobalUDLs',
type: 'boolean',
value: true,
description: 'Place user-defined string literals in the global namespace',
)
option(
'ImplicitConversions',
type: 'boolean',
value: true,
description: 'Enable implicit conversions',
)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

There are more flags, see https://json.nlohmann.me/integration/cmake/#cmake-options for example. Though not are equally interesting, I wonder why here only theres three have been selected.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's been a while, I remember for sure that the MultipleHeaders option is essential to get the same behavior out of Meson and CMake, especially when using the Meson definitions in an embedded project. I don't remember why I added the other two.

@dcbaker dcbaker Jan 27, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@nlohmann: I looked more, and I'm pretty sure that the reason that I added those two options in particular is that the related defines are exposed publicly in the pkg-config file. So for Meson and CMake to generate equivalent .pc files we'd need that.

Now that I'm looking at the generated CMake Target, it looks like JSON_DISABLE_ENUM_SERIALIZATION, JSON_DIAGNOSTICS, JSON_DIAGNOSTIC_POSITIONS, and JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON are exposed in CMake, so maybe we should update the pkg-config CMake generates to expose those as well?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Sounds plausible, but I would not know how :)

Loading