-
Notifications
You must be signed in to change notification settings - Fork 4.1k
docs: add Bazel build guide and bzlmod example #3468
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,100 @@ | ||
| ## bRPC 作为Bazel第三方依赖 | ||
| 1. bRPC 依赖于一些开源库, 但这些库并没有提供bazel支持, 所以需要你手动将一部分依赖加入到你的构建项目中. | ||
| 2. 将 /example/build_with_bazel/*.BUILD 和 brpc_workspace.bzl 该文件移动到你的项目根目录下, 将 | ||
| ```c++ | ||
| load("@//:brpc_workspace.bzl", "brpc_workspace") | ||
| brpc_workspace(); | ||
| ## bRPC 作为 Bazel 第三方依赖 | ||
|
|
||
| 推荐在 Bazel 项目中使用 bzlmod(`MODULE.bazel`)依赖本地 bRPC 源码。 | ||
| `example/build_with_bazel_module` 中有一个包含 server 和 client 的可运行示例: | ||
|
|
||
| ```shell | ||
| $ cd example/build_with_bazel_module | ||
| $ bazel build //:echo_c++_server //:echo_c++_client | ||
| $ bazel run //:echo_c++_server & | ||
| $ bazel run //:echo_c++_client | ||
| ``` | ||
|
|
||
| 先在你的 `.bazelrc` 中添加 bRPC 使用的 registry: | ||
|
|
||
| ```shell | ||
| common --registry=https://bcr.bazel.build | ||
| common --registry=https://baidu.github.io/babylon/registry | ||
| common --registry=https://raw.githubusercontent.com/apache/brpc/master/registry | ||
| ``` | ||
| 内容添加到你的WORKSPACE中. | ||
|
|
||
| 3. 链接请使用 | ||
| ```c++ | ||
| ... | ||
| deps = [ | ||
| 然后在 `MODULE.bazel` 中添加 bRPC,并指向本地 bRPC 源码: | ||
|
|
||
| ```python | ||
| module( | ||
| name = "my_brpc_app", | ||
| version = "0.1.0", | ||
| ) | ||
|
|
||
| bazel_dep(name = "protobuf", version = "27.3", repo_name = "com_google_protobuf") | ||
| bazel_dep(name = "brpc", version = "1.17.0", repo_name = "apache_brpc") | ||
|
|
||
| local_path_override( | ||
| module_name = "brpc", | ||
| path = "/path/to/brpc", | ||
| ) | ||
|
|
||
| single_version_override( | ||
| module_name = "leveldb", | ||
| registry = "https://raw.githubusercontent.com/secretflow/bazel-registry/main", | ||
| ) | ||
|
|
||
| single_version_override( | ||
| module_name = "openssl", | ||
| version = "3.3.2.bcr.1", | ||
| registry = "https://raw.githubusercontent.com/secretflow/bazel-registry/main", | ||
| ) | ||
| ``` | ||
|
|
||
| `bazel_dep` 用来声明模块名和仓库映射,`local_path_override` 让 Bazel 使用本地源码,而不是从 registry 解析 bRPC。 | ||
|
|
||
| bzlmod 不会传递依赖模块中的 override。把 bRPC 当依赖时,需要在自己项目的根 | ||
| `MODULE.bazel` 里重复上面的 `single_version_override`;它们在 aarch64 上是必需的,否则 leveldb toolchain 解析会失败。 | ||
|
|
||
| 之后在目标中链接 bRPC: | ||
|
|
||
| ```python | ||
| cc_binary( | ||
| name = "server", | ||
| srcs = ["server.cpp"], | ||
| deps = [ | ||
| "@apache_brpc//:brpc", | ||
| ], | ||
| ) | ||
| ``` | ||
|
|
||
| 如果服务使用 protobuf,可以从 bRPC 加载 `brpc_proto_library`: | ||
|
|
||
| ```python | ||
| load("@apache_brpc//bazel/tools:brpc_proto_library.bzl", "brpc_proto_library") | ||
|
|
||
| brpc_proto_library( | ||
| name = "cc_echo_proto", | ||
| srcs = ["echo.proto"], | ||
| ) | ||
| ``` | ||
|
|
||
| ## 旧版 WORKSPACE 用法 | ||
|
|
||
| 仍在使用 `WORKSPACE` 的项目可以参考 `example/build_with_bazel`。 | ||
|
|
||
| 1. 将 `example/build_with_bazel/*.BUILD` 和 | ||
| `example/build_with_bazel/brpc_workspace.bzl` 移动到你的项目根目录下。 | ||
| 2. 在 `WORKSPACE` 中添加: | ||
|
|
||
| ```python | ||
| load("@//:brpc_workspace.bzl", "brpc_workspace") | ||
|
|
||
| brpc_workspace() | ||
| ``` | ||
|
|
||
| 3. 在目标中链接 `apache_brpc`: | ||
|
|
||
| ```python | ||
| deps = [ | ||
| "@apache_brpc//:bthread", | ||
| "@apache_brpc//:brpc", | ||
| "@apache_brpc//:butil", | ||
| "@apache_brpc//:bvar", | ||
| ] | ||
| ... | ||
| ``` | ||
| ] | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,105 @@ | ||
| ## bRPC as a Bazel third-party dependency | ||
| 1. bRPC relies on a number of open source libraries that do not provide bazel support, so you will need to manually add some of these dependencies to your build project. | ||
| 2. Move the BUILD file /example/build_with_bazel/*.BUILD and brpc_workspace.bzl to the root of your project, and add the contents of | ||
| ```c++ | ||
| load("@//:brpc_workspace.bzl", "brpc_workspace") | ||
| brpc_workspace(); | ||
|
|
||
| The recommended way to depend on a local bRPC checkout from a Bazel project is | ||
| to use bzlmod (`MODULE.bazel`). See `example/build_with_bazel_module` for a | ||
| runnable example with both a server and a client: | ||
|
|
||
| ```shell | ||
| $ cd example/build_with_bazel_module | ||
| $ bazel build //:echo_c++_server //:echo_c++_client | ||
| $ bazel run //:echo_c++_server & | ||
| $ bazel run //:echo_c++_client | ||
| ``` | ||
|
|
||
| Add the registries used by bRPC to your `.bazelrc`: | ||
|
|
||
| ```shell | ||
| common --registry=https://bcr.bazel.build | ||
| common --registry=https://baidu.github.io/babylon/registry | ||
| common --registry=https://raw.githubusercontent.com/apache/brpc/master/registry | ||
| ``` | ||
| to your WORKSPACE | ||
|
|
||
| 3. link apache_brpc like: | ||
| ```c++ | ||
| ... | ||
| deps = [ | ||
| Add bRPC to your `MODULE.bazel`, and point it to your local bRPC checkout: | ||
|
|
||
| ```python | ||
| module( | ||
| name = "my_brpc_app", | ||
| version = "0.1.0", | ||
| ) | ||
|
|
||
| bazel_dep(name = "protobuf", version = "27.3", repo_name = "com_google_protobuf") | ||
| bazel_dep(name = "brpc", version = "1.17.0", repo_name = "apache_brpc") | ||
|
|
||
| local_path_override( | ||
| module_name = "brpc", | ||
| path = "/path/to/brpc", | ||
| ) | ||
|
|
||
| single_version_override( | ||
| module_name = "leveldb", | ||
| registry = "https://raw.githubusercontent.com/secretflow/bazel-registry/main", | ||
| ) | ||
|
|
||
| single_version_override( | ||
| module_name = "openssl", | ||
| version = "3.3.2.bcr.1", | ||
| registry = "https://raw.githubusercontent.com/secretflow/bazel-registry/main", | ||
| ) | ||
| ``` | ||
|
|
||
| The `bazel_dep` keeps the module name and repository mapping, while | ||
| `local_path_override` makes Bazel use the local checkout instead of resolving | ||
| bRPC from a registry. | ||
|
|
||
| bzlmod does not propagate overrides from dependency modules. When using bRPC as | ||
| a dependency, repeat the `single_version_override` entries above in your root | ||
| `MODULE.bazel`; they are required on aarch64 because leveldb toolchain | ||
| resolution otherwise fails. | ||
|
|
||
| Then link bRPC from your targets: | ||
|
|
||
| ```python | ||
| cc_binary( | ||
| name = "server", | ||
| srcs = ["server.cpp"], | ||
| deps = [ | ||
| "@apache_brpc//:brpc", | ||
| ], | ||
| ) | ||
| ``` | ||
|
|
||
| If your service uses protobuf, load `brpc_proto_library` from bRPC: | ||
|
|
||
| ```python | ||
| load("@apache_brpc//bazel/tools:brpc_proto_library.bzl", "brpc_proto_library") | ||
|
|
||
| brpc_proto_library( | ||
| name = "cc_echo_proto", | ||
| srcs = ["echo.proto"], | ||
| ) | ||
| ``` | ||
|
|
||
| ## Legacy WORKSPACE usage | ||
|
|
||
| For projects that still use `WORKSPACE`, see `example/build_with_bazel`. | ||
|
|
||
| 1. Move `example/build_with_bazel/*.BUILD` and | ||
| `example/build_with_bazel/brpc_workspace.bzl` to the root of your project. | ||
| 2. Add the following to your `WORKSPACE`: | ||
|
|
||
| ```python | ||
| load("@//:brpc_workspace.bzl", "brpc_workspace") | ||
|
|
||
| brpc_workspace() | ||
| ``` | ||
|
|
||
| 3. Link `apache_brpc` from your targets: | ||
|
|
||
| ```python | ||
| deps = [ | ||
| "@apache_brpc//:bthread", | ||
| "@apache_brpc//:brpc", | ||
| "@apache_brpc//:butil", | ||
| "@apache_brpc//:bvar", | ||
| ] | ||
| ... | ||
| ``` | ||
| ] | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.