如何添加非 bazel 项目作为当前 bazel 项目的构建目标?

发布于 2025-01-14 09:45:05 字数 1770 浏览 5 评论 0原文

背景: 我有一个 C++ 头文件库,例如:mpack mpack 使用 cmake 构建系统。但是,我想在我的项目(my_project)中使用它的一些功能,该项目使用 bazel 构建系统。

我正在按照以下步骤进行操作 https://docs.bazel.build/versions/ 1.2.0/external.html#non-bazel-projects

目标: 尝试在 sni_filter.cc 中包含 mpack.hpp

我的更改:

a) 在 WORKSPACE 文件中添加了以下代码段

new_local_repository(
    name = "mpack-c",
    path = "mpack-c",
    build_file = "BUILD.mpack-c",
)

b) 添加了 BUILD.mpack-c

cc_library(
    name = "mpack-lib",
    srcs = glob(["**"]),
    visibility = ["//visibility:public"],
)

c) 在 sni_filter 的 BUILD 文件中添加了 mpack-lib 目标

envoy_cc_library(
    name = "sni_filter_lib",
    srcs = ["sni_filter.cc"],
    hdrs = ["sni_filter.h"],    
    deps = [       
        "@mpack-c//:mpack-lib",
    ],
)

当我尝试运行bazel 构建,它抛出一个错误:

ERROR: Error fetching repository: /xoxo/xyz/abc/repo/src/engine/WORKSPACE:28:21: In new_local_repository rule //external:mpack-c the 'build_file' attribute does not specify an existing file (/xoxo/xyz/abc/repo/src/engine/BUILD.mpack-c does not exist)
ERROR: /xoxo/xyz/abc/repo/src/engine/my_proj/sni_filter/BUILD:11:17: //my_proj/sni_filter:sni_filter_lib depends on @mpack-c//:mpack-lib in repository @mpack-c which failed to fetch. no such package '@mpack-c//': In new_local_repository rule //external:mpack-c the 'build_file' attribute does not specify an existing file (/xoxo/xyz/abc/repo/src/engine/BUILD.mpack-c does not exist)

附加目录结构 输入图片此处描述

Background:
I have a C++ header-only library eg : mpack
mpack uses cmake build system. However, I want to use some of its functions in my project (my_project) which uses bazel build system.

I was following the steps from
https://docs.bazel.build/versions/1.2.0/external.html#non-bazel-projects

Goal:
Trying to include mpack.hpp in sni_filter.cc

My changes:

a) Added the following snippet in WORKSPACE file

new_local_repository(
    name = "mpack-c",
    path = "mpack-c",
    build_file = "BUILD.mpack-c",
)

b) Added BUILD.mpack-c

cc_library(
    name = "mpack-lib",
    srcs = glob(["**"]),
    visibility = ["//visibility:public"],
)

c) Added the mpack-lib target in BUILD file of sni_filter

envoy_cc_library(
    name = "sni_filter_lib",
    srcs = ["sni_filter.cc"],
    hdrs = ["sni_filter.h"],    
    deps = [       
        "@mpack-c//:mpack-lib",
    ],
)

When I am trying to run a bazel build, its throwing an error :

ERROR: Error fetching repository: /xoxo/xyz/abc/repo/src/engine/WORKSPACE:28:21: In new_local_repository rule //external:mpack-c the 'build_file' attribute does not specify an existing file (/xoxo/xyz/abc/repo/src/engine/BUILD.mpack-c does not exist)
ERROR: /xoxo/xyz/abc/repo/src/engine/my_proj/sni_filter/BUILD:11:17: //my_proj/sni_filter:sni_filter_lib depends on @mpack-c//:mpack-lib in repository @mpack-c which failed to fetch. no such package '@mpack-c//': In new_local_repository rule //external:mpack-c the 'build_file' attribute does not specify an existing file (/xoxo/xyz/abc/repo/src/engine/BUILD.mpack-c does not exist)

Attached the directory struct
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

合久必婚 2025-01-21 09:45:05

new_local_repository 属性中的路径必须是绝对路径,或者相对于 WORKSPACE 文件所在目录的相对路径。此外,build_file 中的标签> 属性需要相对于工作空间根指定。

因此,在您的 WORKSPACE 文件中将: 替换

new_local_repository(
    name = "mpack-c",
    path = "mpack-c",
    build_file = "BUILD.mpack-c",
)

为:

new_local_repository(
    name = "mpack-c",
    path = "mpack",
    build_file = "mpack/BUILD.mpack-c",
)

作为参考,请参阅 文档

The path in the new_local_repository attribute needs to either be absolute or relative to the directory the WORKSPACE file is located in. Also, the label in the build_file attribute needs to specified relative to the workspace root.

So, in your WORKSPACE file replace:

new_local_repository(
    name = "mpack-c",
    path = "mpack-c",
    build_file = "BUILD.mpack-c",
)

with:

new_local_repository(
    name = "mpack-c",
    path = "mpack",
    build_file = "mpack/BUILD.mpack-c",
)

For reference, see the documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文