如何使用 shell 脚本在后续 Bazel 构建目标之前生成输出文件
How to use shell script that generates output files prior to subsequent Bazel build targets
我有一些使用 shell 脚本构建的 protobuf 文件,如下所示:
proto.sh
#!/usr/bin/env bash
rm -rf ./proto-out
mkdir -p proto-out
protoc -Iproto --cpp_out=./proto-out ./proto/*.proto
但是我不知道如何在我的 BUILD 文件中获取输出 c++ 类 和 headers。稍后我还需要能够在其他依赖库中使用 headers。我试过像这样的 genrule:
genrule(
name = "proto-build",
srcs = glob(["proto/*.proto"]),
outs = [
"proto-out/point.pb.h",
"proto-out/point-geodetic.pb.h",
"proto-out/point-ned.pb.h",
"proto-out/point.pb.cc",
"proto-out/point-geodetic.pb.cc",
"proto-out/point-ned.pb.cc",
],
cmd = "$(location proto.sh)",
tools = ["proto.sh"],
visibility = ["//visibility:public"],
)
cc_library(
name = "protobuf-common",
srcs = [
"proto-out/point.pb.cc",
"proto-out/point-geodetic.pb.cc",
"proto-out/point-ned.pb.cc",
],
hdrs = [
"proto-out/point.pb.h",
"proto-out/point-geodetic.pb.h",
"proto-out/point-ned.pb.h",
],
copts = ["--std=c++17"],
data = [":proto"],
includes = ["proto-out"],
visibility = ["//visibility:public"],
)
但是执行此操作从来没有任何输出,cc_library 构建当然会失败。这是我第一天在我的团队中评估 Bazel 作为 CMake 的潜在替代品,因此我们将不胜感激。
谢谢
虽然您肯定可以使 genrule + cc_library 方法起作用,但有一些 proto 规则集可以为您完成所有这些工作:
cc_library(
name = "your_code",
srcs = ["your_code.cc"],
deps = [":foo_cc_proto"],
)
cc_proto_library(
name = "foo_cc_proto",
deps = [":foo_proto"],
)
proto_library(
name = "foo_proto",
)
参见:
https://bazel-contrib.github.io/SIG-rules-authors/proto-grpc.html
https://docs.bazel.build/versions/main/be/c-cpp.html#cc_proto_library
我有一些使用 shell 脚本构建的 protobuf 文件,如下所示:
proto.sh
#!/usr/bin/env bash
rm -rf ./proto-out
mkdir -p proto-out
protoc -Iproto --cpp_out=./proto-out ./proto/*.proto
但是我不知道如何在我的 BUILD 文件中获取输出 c++ 类 和 headers。稍后我还需要能够在其他依赖库中使用 headers。我试过像这样的 genrule:
genrule(
name = "proto-build",
srcs = glob(["proto/*.proto"]),
outs = [
"proto-out/point.pb.h",
"proto-out/point-geodetic.pb.h",
"proto-out/point-ned.pb.h",
"proto-out/point.pb.cc",
"proto-out/point-geodetic.pb.cc",
"proto-out/point-ned.pb.cc",
],
cmd = "$(location proto.sh)",
tools = ["proto.sh"],
visibility = ["//visibility:public"],
)
cc_library(
name = "protobuf-common",
srcs = [
"proto-out/point.pb.cc",
"proto-out/point-geodetic.pb.cc",
"proto-out/point-ned.pb.cc",
],
hdrs = [
"proto-out/point.pb.h",
"proto-out/point-geodetic.pb.h",
"proto-out/point-ned.pb.h",
],
copts = ["--std=c++17"],
data = [":proto"],
includes = ["proto-out"],
visibility = ["//visibility:public"],
)
但是执行此操作从来没有任何输出,cc_library 构建当然会失败。这是我第一天在我的团队中评估 Bazel 作为 CMake 的潜在替代品,因此我们将不胜感激。
谢谢
虽然您肯定可以使 genrule + cc_library 方法起作用,但有一些 proto 规则集可以为您完成所有这些工作:
cc_library(
name = "your_code",
srcs = ["your_code.cc"],
deps = [":foo_cc_proto"],
)
cc_proto_library(
name = "foo_cc_proto",
deps = [":foo_proto"],
)
proto_library(
name = "foo_proto",
)
参见:
https://bazel-contrib.github.io/SIG-rules-authors/proto-grpc.html
https://docs.bazel.build/versions/main/be/c-cpp.html#cc_proto_library