如何向 Bazel 中的 C 工具链提供 py_binary 的运行文件

问题描述 投票:0回答:1

我正在尝试编写一个编译器,嗯,一个 Clang 的 Python 包装器,它在 Bazel 中执行一些特定于平台的操作。我创建了一个 MRE,可以在这里重现我的问题:https://github.com/allsey87/custom_cc_toolchain

工作区应该使用以下命令构建:

bazel build --platforms=//:custom_platform //:test

根据 Bazel 的以下输出,我认为我的

py_binary
已创建并被发现,但是,Bazel 的 Python 包装器代码中有一个失败的断言,如下所示:

$ bazel clean --expunge
INFO: Starting clean (this may take a while). Consider using --async if the clean takes more than several minutes.
$ bazel build --verbose_failures --sandbox_debug --platforms=//:custom_platform //:test 
Starting local Bazel server and connecting to it...
WARNING: --enable_bzlmod is set, but no MODULE.bazel file was found at the workspace root. Bazel will create an empty MODULE.bazel file. Please consider migrating your external dependencies from WORKSPACE to MODULE.bazel. For more details, please refer to https://github.com/bazelbuild/bazel/issues/18958.
INFO: Analyzed target //:test (87 packages loaded, 754 targets configured).
ERROR: /workspaces/bazel_test/BUILD:23:10: Compiling empty.c failed: (Exit 1): process-wrapper failed: error executing CppCompile command 
  (cd /home/developer/.cache/bazel/_bazel_developer/e4895a8da4e216f782d166038e1bbca2/sandbox/processwrapper-sandbox/1/execroot/_main && \
  exec env - \
    PATH=/vscode/vscode-server/bin/linux-x64/5437499feb04f7a586f677b155b039bc2b3669eb/bin/remote-cli:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \
    PWD=/proc/self/cwd \
    TMPDIR=/tmp \
  /home/developer/.cache/bazel/_bazel_developer/install/937e6e0d806e40997135c14c1d61532a/process-wrapper '--timeout=0' '--kill_delay=15' '--stats=/home/developer/.cache/bazel/_bazel_developer/e4895a8da4e216f782d166038e1bbca2/sandbox/processwrapper-sandbox/1/stats.out' bazel-out/k8-opt-exec-ST-d57f47055a04/bin/clang_wrapper -MD -MF bazel-out/k8-fastbuild/bin/_objs/test/empty.d '-frandom-seed=bazel-out/k8-fastbuild/bin/_objs/test/empty.o' -iquote . -iquote bazel-out/k8-fastbuild/bin -iquote external/bazel_tools -iquote bazel-out/k8-fastbuild/bin/external/bazel_tools -c empty.c -o bazel-out/k8-fastbuild/bin/_objs/test/empty.o)
Traceback (most recent call last):
  File "/home/developer/.cache/bazel/_bazel_developer/e4895a8da4e216f782d166038e1bbca2/sandbox/processwrapper-sandbox/1/execroot/_main/bazel-out/k8-opt-exec-ST-d57f47055a04/bin/clang_wrapper", line 559, in <module>
    Main()
  File "/home/developer/.cache/bazel/_bazel_developer/e4895a8da4e216f782d166038e1bbca2/sandbox/processwrapper-sandbox/1/execroot/_main/bazel-out/k8-opt-exec-ST-d57f47055a04/bin/clang_wrapper", line 457, in Main
    module_space = FindModuleSpace(main_rel_path)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/developer/.cache/bazel/_bazel_developer/e4895a8da4e216f782d166038e1bbca2/sandbox/processwrapper-sandbox/1/execroot/_main/bazel-out/k8-opt-exec-ST-d57f47055a04/bin/clang_wrapper", line 172, in FindModuleSpace
    raise AssertionError('Cannot find .runfiles directory for %s' % sys.argv[0])
AssertionError: Cannot find .runfiles directory for bazel-out/k8-opt-exec-ST-d57f47055a04/bin/clang_wrapper
Target //:test failed to build
INFO: Elapsed time: 5.067s, Critical Path: 0.05s
INFO: 7 processes: 7 internal.
ERROR: Build did NOT complete successfully

如何将此 .runfiles 目录提供给工具链?

cross-compiling bazel bazel-python
1个回答
0
投票

查看https://github.com/bazelbuild/bazel/blob/674e67bdc3c08b5ac72d5f1ce456bb52766a0f84/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainConfigInfo.java#L56我不是确保 cc 工具链设置为接受具有运行文件的目标。看起来它需要自包含/预编译的二进制文件,因为我没有在 ActionConfig 或 Tool 类中看到任何跟踪运行文件的内容。

因此,最方便的做法就是使您的

py_binary
成为一个独立的可执行文件。
py_binary
中有一个名为
ptyhon_zip_file
的输出组,它应该制作一个可自执行的 python zip 文件,但看起来存在一个问题,即
#!/usr/bin/env python3
未添加到 zip 文件中:https://github .com/bazelbuild/bazel/issues/17629 一种解决方法是在 genrule 中添加 hashbang:

py_binary(
  name = "clang_wrapper",
  srcs = ["clang_wrapper.py"],
)
filegroup(
  name = "clang_wrapper_zip",
  srcs = [":clang_wrapper"],
  output_group = "python_zip_file",
)
genrule(
  name = "clang_wrapper_zip_py_executable",
  srcs = [":clang_wrapper_zip"],
  outs = ["clang_wrapper_zip_py_executable.zip"],
  cmd = "echo '#!/usr/bin/env python3' | cat - $< >$@",
  executable = True,
)

custom_cc_toolchain_config_rule(
  name = "custom_toolchain_config",
  compiler = ":clang_wrapper_zip_py_executable"
)

filegroup(name = "empty")

cc_toolchain(
    name = "custom_cc_toolchain",
    all_files = ":empty",
    ar_files = ":empty",
    as_files = ":empty",
    compiler_files = ":clang_wrapper_zip_py_executable",
    dwp_files = ":empty",
    linker_files = ":empty",
    objcopy_files = ":empty",
    strip_files = ":empty",
    toolchain_config = "custom_toolchain_config",
    toolchain_identifier = "custom_toolchain_identifier",
)

请注意,这可能仅适用于 Linux 和 Mac——在 Windows 上,采用不同的代码路径来创建

exe
,并且 genrule 可能会使这一切在 Windows 上失败。

© www.soinside.com 2019 - 2024. All rights reserved.