为什么BAZEL HTTP_ARCHIVER规则未下载存档?
我是Bazel Newbie,正在此处通过C ++指南,试图包括外部测试库(GTEST): https://bazel.build/tutorials/cpp-use-cass#include-external-libraries
这是我的文件结构和工作空间和构建文件内容:
$ tree
.
├── gtest.BUILD
├── lib
│ ├── BUILD
│ ├── hello-time.cc
│ └── hello-time.h
├── main
│ ├── BUILD
│ ├── hello-greet.cc
│ ├── hello-greet.h
│ └── hello-world.cc
├── README.md
├── test
│ ├── BUILD
│ └── hello-test.cc
└── WORKSPACE
工作空间的内容: :
$ cat WORKSPACE
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "gtest",
build_file = "@//:gtest.BUILD",
url = "https://github.com/google/googletest/archive/release-1.10.0.zip",
sha256 = "94c634d499558a76fa649edb13721dce6e98fb1e7018dfaeba3cd7a083945e91",
)
gtest.build的内容:
$ cat gtest.BUILD
cc_library(
name = "main",
srcs = glob(
["src/*.cc"],
exclude = ["src/gtest-all.cc"]
),
hdrs = glob([
"include/**/*.h",
"src/*.h"
]),
copts = ["-Iexternal/gtest/include"],
linkopts = ["-pthread"],
visibility = ["//visibility:public"],
)
测试/构建的内容:
$ cat test/BUILD
cc_test(
name = "hello-test",
srcs = ["hello-test.cc"],
copts = ["-Iexternal/gtest/include"],
deps = [
"@gtest//:main",
"//main:hello-greet",
],
)
然后,我尝试运行“ Bazel Test:Hello-Test”,但它引发了一个问题,这些问题抱怨缺少一个“构建”文件:
ERROR: An error occurred during the fetch of repository 'gtest':
Traceback (most recent call last):
...
Error in read: Unable to load package for //:gtest.BUILD: BUILD file not found in any of the following directories. Add a BUILD file to a directory to mark it as a package.
- /home/user/code/bazelbuild_examples/cpp-tutorial/stage4
然后,我在此处跑了“ touch build”顶级目录在找到带有类似错误消息的GitHub问题之后,它摆脱了该错误。
Bazel现在正在下载GTEST库(可以在“ Bazel stage4/ofternal/gtest”下看到它),但它似乎并没有使其可用于测试目标:
ERROR: /home/user/code/bazelbuild_examples/cpp-tutorial/stage4/test/BUILD:1:8: Compiling test/hello-test.cc failed: (Exit 1): gcc failed: error executing command /usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer '-std=c++0x' -MD -MF ... (remaining 25 arguments skipped)
Use --sandbox_debug to see verbose messages from the sandbox
test/hello-test.cc:1:10: fatal error: gtest/gtest.h: No such file or directory
1 | #include "gtest/gtest.h"
| ^~~~~~~~~~~~~~~
compilation terminated.
为什么它找不到GTEST标题/库?当您运行“ Bazel测试”时,目录布局如何工作? (即测试代码目录相对于第三方库目录在哪里?)
I'm a Bazel newbie and am working through the C++ guide here, trying to include an external testing library (gtest): https://bazel.build/tutorials/cpp-use-cases#include-external-libraries
This is my file structure and WORKSPACE and BUILD file contents:
$ tree
.
├── gtest.BUILD
├── lib
│ ├── BUILD
│ ├── hello-time.cc
│ └── hello-time.h
├── main
│ ├── BUILD
│ ├── hello-greet.cc
│ ├── hello-greet.h
│ └── hello-world.cc
├── README.md
├── test
│ ├── BUILD
│ └── hello-test.cc
└── WORKSPACE
Contents of WORKSPACE:
$ cat WORKSPACE
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "gtest",
build_file = "@//:gtest.BUILD",
url = "https://github.com/google/googletest/archive/release-1.10.0.zip",
sha256 = "94c634d499558a76fa649edb13721dce6e98fb1e7018dfaeba3cd7a083945e91",
)
Contents of gtest.BUILD:
$ cat gtest.BUILD
cc_library(
name = "main",
srcs = glob(
["src/*.cc"],
exclude = ["src/gtest-all.cc"]
),
hdrs = glob([
"include/**/*.h",
"src/*.h"
]),
copts = ["-Iexternal/gtest/include"],
linkopts = ["-pthread"],
visibility = ["//visibility:public"],
)
Contents of test/BUILD:
$ cat test/BUILD
cc_test(
name = "hello-test",
srcs = ["hello-test.cc"],
copts = ["-Iexternal/gtest/include"],
deps = [
"@gtest//:main",
"//main:hello-greet",
],
)
I then try to run "bazel test test:hello-test" but it throws an issue complaining about a missing "BUILD" file:
ERROR: An error occurred during the fetch of repository 'gtest':
Traceback (most recent call last):
...
Error in read: Unable to load package for //:gtest.BUILD: BUILD file not found in any of the following directories. Add a BUILD file to a directory to mark it as a package.
- /home/user/code/bazelbuild_examples/cpp-tutorial/stage4
I then ran "touch BUILD" in the top level directory, after finding a GitHub issue with a similar error message, which got rid of that error.
Bazel is now downloading gtest library (can see it under "bazel-stage4/external/gtest") but it doesn't seem to be making it available to the test target:
ERROR: /home/user/code/bazelbuild_examples/cpp-tutorial/stage4/test/BUILD:1:8: Compiling test/hello-test.cc failed: (Exit 1): gcc failed: error executing command /usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer '-std=c++0x' -MD -MF ... (remaining 25 arguments skipped)
Use --sandbox_debug to see verbose messages from the sandbox
test/hello-test.cc:1:10: fatal error: gtest/gtest.h: No such file or directory
1 | #include "gtest/gtest.h"
| ^~~~~~~~~~~~~~~
compilation terminated.
Why is it unable to find the gtest headers/library? How does the directory layout work when you're running "bazel test"? (i.e. where is the test code directory relative to the 3rd party library directory?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为问题与您的GTEST构建文件有关。首先, Google Test 已经带有支持的Bazel Build File,那么为什么要编写自己的而不是使用其使用?
第二:
C ++源代码必须在此规则中为标头#inccy的路径是“ Include /.../*/。H”。那是不合适的。首先,
copts
会影响此规则,但没有其他取决于它的目标。通常,编译器选项应成为工具链的一部分,而不是这样的规则。其次, cc_library 规则具有incept = []
专门用于修复的参数包括剥离领先前缀的途径。而不是copts
您应该使用incold
来修复路径-------但您应该使用官方的Google Test构建文件,而不是编写自己的文件处理这样的问题。I think the problem has to do with your gtest build file. First, google test comes with a supported Bazel BUILD file already, so why write your own instead of using theirs?
Second:
The path that c++ source code would have to #include for headers in this rule is "include/.../*.h". That's not proper. For one,
copts
affects this rule, but not other targets that depend on it. In general, compiler options should be part of the toolchain, not the rule like this. Second, cc_library rules have anincludes = []
argument specifically for fixing include paths to strip leading prefixes. Instead ofcopts
you should useincludes
to fix the path-------BUT you should use the official google test BUILD file instead of writing your own and not have to deal with problems like this.