【发布时间】:2018-03-19 01:25:41
【问题描述】:
我正在尝试使用Bazel's Protocol Buffer Rules 来编译(生成)Python 语言绑定和任何依赖项。我的项目布局很简单,只有一个目录proto,其中包含.proto 文件和BUILD 文件。
WORKSPACE
BUILD.six
|-- proto
| |-- example.proto
| |-- BUILD
我的WORKSPACE 文件:
workspace(name = "com_example")
http_archive(
name = "com_google_protobuf",
strip_prefix = "protobuf-3.4.1",
urls = ["https://github.com/google/protobuf/archive/v3.4.1.zip"],
)
new_http_archive(
name = "six_archive",
build_file = "six.BUILD",
url = "https://pypi.python.org/packages/source/s/six/six-1.10.0.tar.gz",
)
bind(
name = "six",
actual = "@six_archive//:six",
)
在我的WORKSPACE 文件中,为了便于阅读,已省略下载文件的预期 SHA-256 哈希值。 http_archive WORKSPACE 规则用于因为 ProtoBuf GitHub repo 包含 Bazel WORKSPACE 和 BUILD 文件。
new_http_archive 必须用于六库,因为它不是 Bazel 工作区。另外值得注意的是,Bazel transitive dependencies 必须在我的WORKSPACE 文件中提供(来自 Bazel 文档):
Bazel 仅读取 WORKSPACE 文件中列出的依赖项。如果你的 项目(A)依赖于另一个项目(B),其中列出了依赖项 第三个项目 (C) 在其 WORKSPACE 文件中,您必须同时添加 B 和 C 到您项目的 WORKSPACE 文件中。
six.BUILD 直接取自 repo 并保存在本地:
我的BUILD 文件
load("@com_google_protobuf//:protobuf.bzl", "py_proto_library")
py_proto_library(
name = "py",
use_grpc_plugin = True,
deps = [
"@com_google_protobuf//:protobuf_python",
":example_proto",
],
visibility = ["//visibility:public"],
# protoc = "@com_google_protobuf//:protoc",
)
proto_library(
name = "example_proto",
srcs = ["example.proto"],
)
构建时出现问题:
bazel build //proto:py
输出(为便于阅读而格式化):
proto/BUILD:3:1:
no such target '//:protobuf_python':
target 'protobuf_python' not declared in package '' defined by BUILD and referenced by '//proto:py'.
ERROR: Analysis of target '//proto:py' failed; build aborted.
但是,从我的命令行构建外部依赖项是可行的:
bazel build @com_google_protobuf//:protobuf_python
输出(为了便于阅读而截断):
INFO: Found 1 target...
...
INFO: Elapsed time: 51.577s, Critical Path: 8.63s
protobuf_python 目标已明确定义并公开:
【问题讨论】:
标签: git protocol-buffers bazel