Skip to content
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

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions BUILD.bazel
@@ -0,0 +1,13 @@
licenses(["notice"]) # 3-Clause BSD

exports_files(["LICENSE.MIT"])

cc_library(
name = "json",
hdrs = glob([
"include/**/*.hpp",
Copy link

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

Copy link
Contributor

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

]),
includes = ["include"],
visibility = ["//visibility:public"],
alwayslink = 1,
)
78 changes: 78 additions & 0 deletions README.md
Expand Up @@ -147,6 +147,84 @@ endif()

`thirdparty/nlohmann_json` is then a complete copy of this source tree.

### Bazel
Copy link
Owner

Choose a reason for hiding this comment

The 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.

Copy link

Choose a reason for hiding this comment

The 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:

        http_file(
            name = "com_github_nlohmann_json_single_header",
            urls = [
                "https://github.com/nlohmann/json/releases/download/v3.6.1/json.hpp",
            ],
            sha256 = "blah_blah_blah",
        )


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.
Copy link

Choose a reason for hiding this comment

The 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`.
Expand Down
1 change: 1 addition & 0 deletions WORKSPACE
@@ -0,0 +1 @@
workspace(name = "nlohmann_json")
Copy link
Owner

Choose a reason for hiding this comment

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

Is this file really needed in this repository?

Copy link
Contributor

@Vertexwahn Vertexwahn Sep 4, 2022

Choose a reason for hiding this comment

The 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 WORKSPCE.bazel (which is an alternative to WORKSPACE only) to make it more clear that this is a Bazel thing -> see my PR: #3709