New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add bazel build #1606
add bazel build #1606
Changes from all commits
60bba02
a4d13c9
79a9d00
272ebdc
6df60b0
1c98ce8
528bc96
1e99a02
c0d511e
9f81beb
5681e55
a43347e
15acf26
c8ea63a
183390c
8c20571
aafad2b
6708f22
cebb4e0
944267f
295732a
4ab991c
567a036
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -147,6 +147,84 @@ endif() | |
|
||
`thirdparty/nlohmann_json` is then a complete copy of this source tree. | ||
|
||
### Bazel | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the extensive example! What I had in mind, though, was a paragraph like for the package managers in the "Package Managers" section below. Just enough to let people knowing Bazel understand what they need to do to use this library. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since these instructions pertain more to developers, it would probably be better to just download the single header release, not build the entire thing from source. Like so:
|
||
|
||
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 <iostream> | ||
|
||
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I looked at the demo and didn't see any details about why quotation marks are needed here. Can you explain? |
||
|
||
### 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`. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
workspace(name = "nlohmann_json") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this file really needed in this repository? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes it is - otherwise, this is not recognized by Bazel - I named the file |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From the docs: "This pattern should be avoided if possible, as it can reduce build incrementality and therefore increase build times." A better approach would be to generate the list of sources and headers from CMake (that way, you don't need to repeat all of it in Bazel), i.e: https://github.com/googleapis/google-cloud-cpp/blob/8e8dd3d83ec27b7a5176f9fe43cdd93aa5ace3dc/cmake/CreateBazelConfig.cmake#L43
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed this in #3709