From 2d1f9b6a7d94f97377f9968334a9ae6c56d21ee0 Mon Sep 17 00:00:00 2001 From: Vertexwahn Date: Sun, 18 Sep 2022 08:15:12 +0200 Subject: [PATCH] Add Bazel build support (#3709) --- BUILD.bazel | 53 +++++++++++++++++++ CMakeLists.txt | 2 +- Makefile | 2 + README.md | 4 +- WORKSPACE.bazel | 1 + cmake/scripts/gen_bazel_build_file.cmake | 24 +++++++++ .../docs/integration/package_managers.md | 4 ++ 7 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 BUILD.bazel create mode 100644 WORKSPACE.bazel create mode 100644 cmake/scripts/gen_bazel_build_file.cmake diff --git a/BUILD.bazel b/BUILD.bazel new file mode 100644 index 0000000000..15d84f16be --- /dev/null +++ b/BUILD.bazel @@ -0,0 +1,53 @@ +cc_library( + name = "json", + hdrs = [ + "include/nlohmann/adl_serializer.hpp", + "include/nlohmann/byte_container_with_subtype.hpp", + "include/nlohmann/detail/abi_macros.hpp", + "include/nlohmann/detail/conversions/from_json.hpp", + "include/nlohmann/detail/conversions/to_chars.hpp", + "include/nlohmann/detail/conversions/to_json.hpp", + "include/nlohmann/detail/exceptions.hpp", + "include/nlohmann/detail/hash.hpp", + "include/nlohmann/detail/input/binary_reader.hpp", + "include/nlohmann/detail/input/input_adapters.hpp", + "include/nlohmann/detail/input/json_sax.hpp", + "include/nlohmann/detail/input/lexer.hpp", + "include/nlohmann/detail/input/parser.hpp", + "include/nlohmann/detail/input/position_t.hpp", + "include/nlohmann/detail/iterators/internal_iterator.hpp", + "include/nlohmann/detail/iterators/iter_impl.hpp", + "include/nlohmann/detail/iterators/iteration_proxy.hpp", + "include/nlohmann/detail/iterators/iterator_traits.hpp", + "include/nlohmann/detail/iterators/json_reverse_iterator.hpp", + "include/nlohmann/detail/iterators/primitive_iterator.hpp", + "include/nlohmann/detail/json_custom_base_class.hpp", + "include/nlohmann/detail/json_pointer.hpp", + "include/nlohmann/detail/json_ref.hpp", + "include/nlohmann/detail/macro_scope.hpp", + "include/nlohmann/detail/macro_unscope.hpp", + "include/nlohmann/detail/meta/call_std/begin.hpp", + "include/nlohmann/detail/meta/call_std/end.hpp", + "include/nlohmann/detail/meta/cpp_future.hpp", + "include/nlohmann/detail/meta/detected.hpp", + "include/nlohmann/detail/meta/identity_tag.hpp", + "include/nlohmann/detail/meta/is_sax.hpp", + "include/nlohmann/detail/meta/std_fs.hpp", + "include/nlohmann/detail/meta/type_traits.hpp", + "include/nlohmann/detail/meta/void_t.hpp", + "include/nlohmann/detail/output/binary_writer.hpp", + "include/nlohmann/detail/output/output_adapters.hpp", + "include/nlohmann/detail/output/serializer.hpp", + "include/nlohmann/detail/string_concat.hpp", + "include/nlohmann/detail/string_escape.hpp", + "include/nlohmann/detail/value_t.hpp", + "include/nlohmann/json.hpp", + "include/nlohmann/json_fwd.hpp", + "include/nlohmann/ordered_map.hpp", + "include/nlohmann/thirdparty/hedley/hedley.hpp", + "include/nlohmann/thirdparty/hedley/hedley_undef.hpp", + ], + includes = ["include"], + visibility = ["//visibility:public"], + alwayslink = True, +) diff --git a/CMakeLists.txt b/CMakeLists.txt index d88feb5c2f..f942e04ab1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,7 +43,7 @@ option(JSON_Diagnostics "Use extended diagnostic messages." O option(JSON_GlobalUDLs "Place use-defined string literals in the global namespace." ON) option(JSON_ImplicitConversions "Enable implicit conversions." ON) option(JSON_DisableEnumSerialization "Disable default integer enum serialization." OFF) -option(JSON_LegacyDiscardedValueComparison "Enable legacy discarded value comparison." OFF) +option(JSON_LegacyDiscardedValueComparison "Enable legacy discarded value comparison." OFF) option(JSON_Install "Install CMake targets during install step." ${MAIN_PROJECT}) option(JSON_MultipleHeaders "Use non-amalgamated version of the library." ON) option(JSON_SystemInclude "Include as system headers (skip for clang-tidy)." OFF) diff --git a/Makefile b/Makefile index 18b08a51ab..e03b49eee8 100644 --- a/Makefile +++ b/Makefile @@ -192,6 +192,8 @@ check-amalgamation: @mv $(AMALGAMATED_FILE)~ $(AMALGAMATED_FILE) @mv $(AMALGAMATED_FWD_FILE)~ $(AMALGAMATED_FWD_FILE) +BUILD.bazel: $(SRCS) + cmake -P cmake/scripts/gen_bazel_build_file.cmake ########################################################################## # ChangeLog diff --git a/README.md b/README.md index 43d5a4686b..ddb3308d12 100644 --- a/README.md +++ b/README.md @@ -1310,7 +1310,9 @@ endif() If you are using the [Meson Build System](https://mesonbuild.com), add this source tree as a [meson subproject](https://mesonbuild.com/Subprojects.html#using-a-subproject). You may also use the `include.zip` published in this project's [Releases](https://github.com/nlohmann/json/releases) to reduce the size of the vendored source tree. Alternatively, you can get a wrap file by downloading it from [Meson WrapDB](https://wrapdb.mesonbuild.com/nlohmann_json), or simply use `meson wrap install nlohmann_json`. Please see the meson project for any issues regarding the packaging. -The provided `meson.build` can also be used as an alternative to cmake for installing `nlohmann_json` system-wide in which case a pkg-config file is installed. To use it, simply have your build system require the `nlohmann_json` pkg-config dependency. In Meson, it is preferred to use the [`dependency()`](https://mesonbuild.com/Reference-manual.html#dependency) object with a subproject fallback, rather than using the subproject directly. +The provided `meson.build` can also be used as an alternative to CMake for installing `nlohmann_json` system-wide in which case a pkg-config file is installed. To use it, simply have your build system require the `nlohmann_json` pkg-config dependency. In Meson, it is preferred to use the [`dependency()`](https://mesonbuild.com/Reference-manual.html#dependency) object with a subproject fallback, rather than using the subproject directly. + +If you are using [Bazel](https://bazel.build/) you can simply reference this repository using `http_archive` or `git_repository` and depend on `@nlohmann_json//:json`. If you are using [Conan](https://www.conan.io/) to manage your dependencies, merely add [`nlohmann_json/x.y.z`](https://conan.io/center/nlohmann_json) to your `conanfile`'s requires, where `x.y.z` is the release version you want to use. Please file issues [here](https://github.com/conan-io/conan-center-index/issues) if you experience problems with the packages. diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel new file mode 100644 index 0000000000..2b2ae9dba7 --- /dev/null +++ b/WORKSPACE.bazel @@ -0,0 +1 @@ +workspace(name = "nlohmann_json") diff --git a/cmake/scripts/gen_bazel_build_file.cmake b/cmake/scripts/gen_bazel_build_file.cmake new file mode 100644 index 0000000000..e754d387d9 --- /dev/null +++ b/cmake/scripts/gen_bazel_build_file.cmake @@ -0,0 +1,24 @@ +# generate Bazel BUILD file + +set(PROJECT_ROOT "${CMAKE_CURRENT_LIST_DIR}/../..") +set(BUILD_FILE "${PROJECT_ROOT}/BUILD.bazel") + +file(GLOB_RECURSE HEADERS LIST_DIRECTORIES false RELATIVE "${PROJECT_ROOT}" "include/*.hpp") + +file(WRITE "${BUILD_FILE}" [=[ +cc_library( + name = "json", + hdrs = [ +]=]) + +foreach(header ${HEADERS}) + file(APPEND "${BUILD_FILE}" " \"${header}\",\n") +endforeach() + +file(APPEND "${BUILD_FILE}" [=[ + ], + includes = ["include"], + visibility = ["//visibility:public"], + alwayslink = True, +) +]=]) diff --git a/docs/mkdocs/docs/integration/package_managers.md b/docs/mkdocs/docs/integration/package_managers.md index b9ebf075f1..46d7a8549a 100644 --- a/docs/mkdocs/docs/integration/package_managers.md +++ b/docs/mkdocs/docs/integration/package_managers.md @@ -62,6 +62,10 @@ If you are using the [Meson Build System](http://mesonbuild.com), add this sourc The provided `meson.build` can also be used as an alternative to cmake for installing `nlohmann_json` system-wide in which case a pkg-config file is installed. To use it, simply have your build system require the `nlohmann_json` pkg-config dependency. In Meson, it is preferred to use the [`dependency()`](https://mesonbuild.com/Reference-manual.html#dependency) object with a subproject fallback, rather than using the subproject directly. +## Bazel + +This repository provides a [Bazel](https://bazel.build/) `WORKSPACE.bazel` and a corresponding `BUILD.bazel` file. Therefore, this repository can be referenced by workspace rules such as `http_archive`, `git_repository`, or `local_repository` from other Bazel workspaces. To use the library you only need to depend on the target `@nlohmann_json//:json` (e.g. via `deps` attribute). + ## Conan If you are using [Conan](https://www.conan.io/) to manage your dependencies, merely add `nlohmann_json/x.y.z` to your `conanfile`'s requires, where `x.y.z` is the release version you want to use. Please file issues [here](https://github.com/conan-io/conan-center-index/issues) if you experience problems with the packages.