From 5a4d47eb7dbcc6303a3d0be955d4da9a65539f7f Mon Sep 17 00:00:00 2001 From: Vertexwahn Date: Mon, 22 Aug 2022 23:48:38 +0200 Subject: [PATCH] Add Bazel build support Change workspace name Fix typo in WORKSPACE name Try to generate Bazel BUILD file via CMake Update README.md Update README.md Update CMakeLists.txt Still trying to get CMake to generte a list of all JSON sourcfiles ... Remove generated source file Can generate a list of all files now... Some progress Update create_bazel_build_file.cmake Delete nlohmann_json.bzl fgd --- BUILD.bazel | 53 +++++++++++++++++++++++++++++ CMakeLists.txt | 14 ++++++-- README.md | 4 ++- WORKSPACE.bazel | 1 + cmake/create_bazel_build_file.cmake | 21 ++++++++++++ 5 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 BUILD.bazel create mode 100644 WORKSPACE.bazel create mode 100644 cmake/create_bazel_build_file.cmake diff --git a/BUILD.bazel b/BUILD.bazel new file mode 100644 index 000000000..4485ede47 --- /dev/null +++ b/BUILD.bazel @@ -0,0 +1,53 @@ +cc_library( + name = "json", + hdrs = glob([ + "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 d88feb5c2..654ba7dd6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,11 +40,12 @@ endif() option(JSON_BuildTests "Build the unit tests when BUILD_TESTING is enabled." ${JSON_BuildTests_INIT}) option(JSON_CI "Enable CI build targets." OFF) option(JSON_Diagnostics "Use extended diagnostic messages." OFF) +option(JSON_DisableEnumSerialization "Disable default integer enum serialization." OFF) +option(JSON_GenerateBazelBuildFiles "Generate Bazel Build files." OFF) 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_Install "Install CMake targets during install step." ${MAIN_PROJECT}) +option(JSON_LegacyDiscardedValueComparison "Enable legacy discarded value comparison." OFF) option(JSON_MultipleHeaders "Use non-amalgamated version of the library." ON) option(JSON_SystemInclude "Include as system headers (skip for clang-tidy)." OFF) @@ -153,6 +154,15 @@ if (JSON_BuildTests) add_subdirectory(tests) endif() +## +## BAZEL +## create Bazel BUILD file +## +if (JSON_BuildTests) + include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/create_bazel_build_file.cmake) + create_bazel_build_file(${NLOHMANN_JSON_INCLUDE_BUILD_DIR}) +endif() + ## ## INSTALL ## install header files, generate and install cmake config files for find_package() diff --git a/README.md b/README.md index a05e9672e..8c2e2ea67 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 000000000..2b2ae9dba --- /dev/null +++ b/WORKSPACE.bazel @@ -0,0 +1 @@ +workspace(name = "nlohmann_json") diff --git a/cmake/create_bazel_build_file.cmake b/cmake/create_bazel_build_file.cmake new file mode 100644 index 000000000..2b0c8507a --- /dev/null +++ b/cmake/create_bazel_build_file.cmake @@ -0,0 +1,21 @@ +function (create_bazel_build_file NLOHMANN_JSON_INCLUDE_BUILD_DIR) + message(STATUS "Generating Bazel BUILD file") + + file(GLOB_RECURSE NLOHMANN_JSON_HEADERS "${NLOHMANN_JSON_INCLUDE_BUILD_DIR}/*.hpp") + + set(filename "BUILD.bazel") + file(WRITE "${filename}" "cc_library(\n") + file(APPEND "${filename}" " name = \"json\",\n") + file(APPEND "${filename}" " hdrs = glob([\n") + + foreach(_header ${NLOHMANN_JSON_HEADERS}) + file(RELATIVE_PATH _header_rel ${PROJECT_SOURCE_DIR} ${_header}) + file(APPEND "${filename}" " \"${_header_rel}\",\n") + endforeach() + + file(APPEND "${filename}" " ]),\n") + file(APPEND "${filename}" " includes = [\"include\"],\n") + file(APPEND "${filename}" " visibility = [\"//visibility:public\"],\n") + file(APPEND "${filename}" " alwayslink = True,\n") + file(APPEND "${filename}" ")\n") +endfunction ()