From 4ab991c41bc93211c7c6a1ebf660ef34167bcfad Mon Sep 17 00:00:00 2001 From: horance Date: Thu, 23 May 2019 06:45:40 +0800 Subject: [PATCH 1/2] user can deps nlohmann json using bazel directly --- BUILD.bazel | 13 +++++++++++++ WORKSPACE | 1 + 2 files changed, 14 insertions(+) create mode 100644 BUILD.bazel create mode 100644 WORKSPACE diff --git a/BUILD.bazel b/BUILD.bazel new file mode 100644 index 0000000000..b8c097308c --- /dev/null +++ b/BUILD.bazel @@ -0,0 +1,13 @@ +licenses(["notice"]) # 3-Clause BSD + +exports_files(["LICENSE.MIT"]) + +cc_library( + name = "json", + hdrs = glob([ + "include/**/*.hpp", + ]), + includes = ["include"], + visibility = ["//visibility:public"], + alwayslink = 1, +) diff --git a/WORKSPACE b/WORKSPACE new file mode 100644 index 0000000000..2b2ae9dba7 --- /dev/null +++ b/WORKSPACE @@ -0,0 +1 @@ +workspace(name = "nlohmann_json") From 567a0366a0dc96fafb4be1bd89be5fd196fb1a7e Mon Sep 17 00:00:00 2001 From: horance Date: Sat, 25 May 2019 22:33:45 +0800 Subject: [PATCH 2/2] add bazel build demo --- README.md | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/README.md b/README.md index 08fbb14a62..d7d5af416b 100644 --- a/README.md +++ b/README.md @@ -147,6 +147,84 @@ endif() `thirdparty/nlohmann_json` is then a complete copy of this source tree. +### Bazel + +You can also use the `nlohmann/json` in Bazel. + +#### Add External Dependency in WORKSPACE + +You can write external dependency `github_nlohmann_json` at your WORKSPACE file. + +```python +workspace(name = "nlohmann_json_demo") + +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") + +http_archive( + name = "github_nlohmann_json", + sha256 = "69cc88207ce91347ea530b227ff0776db82dcb8de6704e1a3d74f4841bc651cf", + urls = [ + "https://github.com/nlohmann/json/releases/download/v3.6.1/include.zip", + ], + build_file = "//third_party:nlohmann_json.BUILD", +) +``` + +`nlohmann_json.BUILD` define the bazel rule for the `nlohmann/json` library. + +```python +licenses(["notice"]) + +exports_files(["LICENSE.MIT"]) + +cc_library( + name = "json", + hdrs = glob([ + "include/**/*.hpp", + ]), + includes = ["include"], + visibility = ["//visibility:public"], + alwayslink = 1, +) +``` + +NOTE: sha256 is public by the release, and you can also get `sha256` as follow command: + +``` +curl -L https://github.com/nlohmann/json/releases/download/v3.6.1/include.zip | sha256sum +``` + +#### Usage + +You can write one simple c++ binary rule and depend `@github_nlohmann_json//:json` target. + +``` +cc_binary( + name = "json_test", + srcs = ["json_test.cc"], + deps = [ + "@github_nlohmann_json//:json", + ], +) +``` + +After it, you can include `nlohmann/json.hpp` file. + +```cpp +#include "nlohmann/json.hpp" +#include + +int main(int argc, char** argv) { + nlohmann::json obj = { + {"bazel", "https://bazel.build"}, + {"cmake", "https://cmake.org/"}, + }; + std::cout << obj.dump(4) << std::endl; +} +``` + +NOTE: you should use double quotation marks instead of angle brackets. You can see details at [horance-liu/nlohmann_json_demo](https://github.com/horance-liu/nlohmann_json_demo) project. + ### Package Managers :beer: If you are using OS X and [Homebrew](http://brew.sh), just type `brew tap nlohmann/json` and `brew install nlohmann_json` and you're set. If you want the bleeding edge rather than the latest release, use `brew install nlohmann_json --HEAD`.