c++ - Bazel 使用 ccache 构建 C++ 示例失败

标签 c++ bazel ccache

我一直遵循 OSX here 的说明。我已经安装了 bazel,克隆了示例并尝试使用 bazel build//main:hello-world 来构建它。但这里出了问题。

INFO: Analysed target //main:hello-world (0 packages loaded). INFO:
Found 1 target... ERROR:
/Users/myuser/work/temp/bazel-sample/examples/cpp-tutorial/stage1/main/BUILD:1:1:
Linking of rule '//main:hello-world' failed (Exit 1) ccache: error:
Failed to create temporary file for
/Users/myuser/.ccache/6/stats.tmp: Operation not
permitted Target //main:hello-world failed to build Use
--verbose_failures to see the command lines of failed build steps. INFO: Elapsed time: 0.295s, Critical Path: 0.07s INFO: 0 processes.
FAILED: Build did NOT complete successfully

我安装的 ccache 似乎有问题。使用 --verbose_failures

运行相同的命令后
INFO: Analysed target //main:hello-world (0 packages loaded). INFO:
Found 1 target... ERROR:
/Users/myuser/work/temp/bazel-sample/examples/cpp-tutorial/stage1/main/BUILD:1:1:
Linking of rule '//main:hello-world' failed (Exit 1): cc_wrapper.sh
failed: error executing command    (cd
/private/var/tmp/_bazel_myuser/550c05da61518b4bbbb0ffdfd033154f/execroot/__main__
&& \   exec env - \
    APPLE_SDK_PLATFORM='' \
    APPLE_SDK_VERSION_OVERRIDE='' \
    PATH=/Users/myuser/Library/Android/sdk/platform-tools:/Users/myuser/Library/Android/sdk/tools:/Users/myuser/Library/Android/sdk/build-tools/25.0.0:/Users/myuser/Library/Android/ndk/android-ndk-r10c:/Applications/CMake.app/Contents/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/Users/myuser/bin
\
    XCODE_VERSION_OVERRIDE=9.3.0 \   external/local_config_cc/cc_wrapper.sh -fobjc-link-runtime -Wl,-S -o
bazel-out/darwin-fastbuild/bin/main/hello-world
bazel-out/darwin-fastbuild/bin/main/_objs/hello-world/main/hello-world.o
-headerpad_max_install_names -lc++ -no-canonical-prefixes)

Use --sandbox_debug to see verbose messages from the sandbox ccache:
error: Failed to create temporary file for
/Users/myuser/.ccache/0/stats.tmp: Operation not permitted Target
//main:hello-world failed to build INFO: Elapsed time: 0.294s,
Critical Path: 0.07s INFO: 0 processes. FAILED: Build did NOT complete
successfully

我发现沙箱有问题。添加 --sandbox_debug 打印:

INFO: Analysed target //main:hello-world (0 packages loaded).
INFO: Found 1 target...
ERROR: /Users/myuser/work/temp/bazel-sample/examples/cpp-tutorial/stage1/main/BUILD:1:1: Linking of rule '//main:hello-world' failed (Exit 1): sandbox-exec failed: error executing command 
  (cd /private/var/tmp/_bazel_myuser/550c05da61518b4bbbb0ffdfd033154f/execroot/__main__ && \
  exec env - \
    APPLE_SDK_PLATFORM='' \
    APPLE_SDK_VERSION_OVERRIDE='' \
    DEVELOPER_DIR=/Applications/Xcode.app/Contents/Developer \
    PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/myuser/bin \
    SDKROOT=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk \
    TMPDIR=/var/folders/x3/r5tf7wfx3_q0xnyx95s3r5400000gq/T/ \
    XCODE_VERSION_OVERRIDE=9.3.0 \
  /usr/bin/sandbox-exec -f /private/var/tmp/_bazel_myuser/550c05da61518b4bbbb0ffdfd033154f/sandbox/darwin-sandbox/1/sandbox.sb /private/var/tmp/_bazel_myuser/550c05da61518b4bbbb0ffdfd033154f/execroot/__main__/_bin/process-wrapper '--timeout=0' '--kill_delay=15' external/local_config_cc/cc_wrapper.sh -fobjc-link-runtime -Wl,-S -o bazel-out/darwin-fastbuild/bin/main/hello-world bazel-out/darwin-fastbuild/bin/main/_objs/hello-world/main/hello-world.o -headerpad_max_install_names -lc++ -no-canonical-prefixes)
ccache: error: Failed to create temporary file for /Users/myuser/.ccache/b/stats.tmp: Operation not permitted
Target //main:hello-world failed to build
INFO: Elapsed time: 0.268s, Critical Path: 0.06s
INFO: 0 processes.
FAILED: Build did NOT complete successfully

沙箱文件sandbox.sb的内容是:

(version 1)
(debug deny)
(allow default)
(deny file-write*)
(allow file-write*
    (subpath "/dev")
    (subpath "/Users/myuser/Library/Logs")
    (subpath "/Users/myuser/Library/Developer")
    (subpath "/private/var/folders/x3/r5tf7wfx3_q0xnyx95s3r5400000gq/T")
    (subpath "/private/var/tmp/_bazel_myuser/550c05da61518b4bbbb0ffdfd033154f/sandbox/darwin-sandbox/1/execroot/__main__")
    (subpath "/private/var/tmp")
    (subpath "/private/var/folders/x3/r5tf7wfx3_q0xnyx95s3r5400000gq/C")
    (subpath "/private/tmp")
)

它显然缺少写入 /Users/myuser/.ccache 目录的权限。我尝试使用 export CCACHE_DISABLE=1 禁用 ccache,但没有任何变化。如何在不删除 ccache 的情况下修复它?

最佳答案

是否可以选择设置编译器的绝对路径并将其传递给 CC 环境变量?如果您安装了 Xcode,您可能需要使用 BAZEL_USE_CPP_ONLY_TOOLCHAIN=1

关于c++ - Bazel 使用 ccache 构建 C++ 示例失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50858954/

相关文章:

c++ - 为什么这两个模板函数的输出不同?

c++ - 线程通信的 myDesign 是否可以接受?

java - 当我尝试在Jetson TK1上使用协议(protocol)3.0.0 alpha 3和jdk 1.8编译Bazel 0.1.0时,遇到了此JNI链接错误

path - 如何让ccache不将编译器的完整路径传递给distcc

c++ - 将结构体成员声明为 uint32_t 时的额外字节

c++ - 如何在 C++ 中使用 [[noreturn]] 属性?

bazel - 运行测试直到失败

c++ - 使用包中工作空间导入的线性代数库Eigen

docker - 在 docker 内部构建时使用 ccache

c++ - 如何在多个架构中将 ccache 与 gcc -march=native 一起使用?