如何在不同的 workspace/package 中引用 bazel c++ protobuf 输出 headers

How to reference bazel c++ protobuf output headers in a different workspace/package

我几天前才开始使用 Bazel,希望能有比 CMake 更好的东西。我有一个小型库,它在自己的存储库中只包含 protobuf 定义。我已经使用 bazel 构建原型类型并在 bazel-bin/proto 目录中看到它们,但我不确定如何继续使该目录成为依赖 workspaces/packages 的包含,以便我可以利用输出 header 个文件?

proto-repo: 建造

load("@rules_cc//cc:defs.bzl", "cc_proto_library")
load("@rules_proto//proto:defs.bzl", "proto_library")

cc_library(
    name = "my-protobuf-common",
    hdrs = [
        ":my-proto-lib",
    ],
    copts = ["--std=c++17"],
    includes = [
        ":my-proto-lib",
    ],
    linkstatic = True,
    visibility = ["//visibility:public"],
    deps = [":my-proto-lib"],
)

cc_proto_library(
    name = "my-proto-lib",
    visibility = ["//visibility:public"],
    deps = [":my-proto"],
)

proto_library(
    name = "my-proto",
    srcs = [
        "proto/point.proto",
        "proto/point-geodetic.proto",
        "proto/point-ned.proto",
    ],
    visibility = ["//visibility:public"],
)

依赖回购(工作区正确地作为外部拉动,我看到原型构建输出): 建造

load("@rules_cc//cc:defs.bzl", "cc_library")

cc_library(
    name = "my-service",
    srcs = [
        "app/bazel-test.cpp",
    ],
    hdrs = [
        "@mpc//:my-protobuf-common",
    ],
    copts = ["--std=c++17"],
    deps = [        
        "@mpc//:my-protobuf-common",
    ],
)

bazel-test.cpp

#include <iostream>
#include <proto/point.pb.h>

int main() {
    MyProtobufCommon::Point p;
}

构建错误:

app/bazel-test.cpp:2:10: fatal error: proto/point.pb.h: No such file or directory
    2 | #include <proto/point.pb.h>
      |          ^~~~~~~~~~~~~~~~~~

cc_library.includes 采用表示路径的字符串,而不是标签。你想在 my-protobuf-common.

中设置 includes = ["."]

同样,cc_library.hdrs 用于 headers 的源文件,依赖于此文件的目标将 #include。在 depshdrs 中列出一些东西没有意义,对于这个用例,你根本不需要 hdrs

此外,使用 cc_binary 构建带有 main 的文件。 cc_library 没有完整 link。

此外,copts = ["--std=c++17"] 很少是个好主意。这只会为 cc_library 中的文件设置标志,这可以更改其 ABI,以便 linking 到构建的其他部分不起作用。在这种情况下,cc_proto_librarycc_library 等价部分不会让标志通过。使用 bazel command-line 标志 --copt=--std=c++17 instead 将其应用于整个构建。

一般来说,你应该可以直接依赖cc_proto_library,所以一般不需要中间的cc_library my-protobuf-common。 cc 工具链使用 -iquote 添加 proto deps,所以我相信必须使用 #include "proto/point.pb.h"

proto-repo/WORKSPACE:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
    name = "rules_proto",
    sha256 = "66bfdf8782796239d3875d37e7de19b1d94301e8972b3cbd2446b332429b4df1",
    strip_prefix = "rules_proto-4.0.0",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/rules_proto/archive/refs/tags/4.0.0.tar.gz",
        "https://github.com/bazelbuild/rules_proto/archive/refs/tags/4.0.0.tar.gz",
    ],
)
load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")
rules_proto_dependencies()
rules_proto_toolchains()

proto-repo/BUILD:

cc_proto_library(
  name = "point_cc_proto",
  deps = [":point"],
  visibility = ["//visibility:public"],
)

proto_library(
  name = "point",
  srcs = ["proto/point.proto"],
)

proto-repo/proto/point.proto:

syntax = "proto3";

package my_protos.point;

message Point {
  optional int32 x = 1;
  optional int32 y = 2;
}

main-repo/WORKSPACE:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

local_repository(
  name = "my_protos",
  path = "../proto-repo",
)

http_archive(
    name = "rules_proto",
    sha256 = "66bfdf8782796239d3875d37e7de19b1d94301e8972b3cbd2446b332429b4df1",
    strip_prefix = "rules_proto-4.0.0",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/rules_proto/archive/refs/tags/4.0.0.tar.gz",
        "https://github.com/bazelbuild/rules_proto/archive/refs/tags/4.0.0.tar.gz",
    ],
)
load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")
rules_proto_dependencies()
rules_proto_toolchains()

main-repo/BUILD:

cc_binary(
  name = "main",
  srcs = ["main.cc"],
  deps = ["@my_protos//:point_cc_proto"],
)

main-repo/main.cc:

#include <iostream>
#include "proto/point.pb.h"

int main() {

  my_protos::point::Point p;
  p.set_x(123);
  p.set_y(456);
  std::cout << p.DebugString();

  return 0;
}

用法:

main-repo$ bazel run main
INFO: Analyzed target //:main (43 packages loaded, 570 targets configured).
INFO: Found 1 target...
Target //:main up-to-date:
  bazel-bin/main
INFO: Elapsed time: 6.166s, Critical Path: 5.33s
INFO: 106 processes: 4 internal, 102 linux-sandbox.
INFO: Build completed successfully, 106 total actions
INFO: Build completed successfully, 106 total actions
x: 123
y: 456