使用自定义 Clang + Libc++(而不是 stdlibc++)编译 Tensorflow

6 c++ llvm clang++ bazel tensorflow

我正在尝试使用自定义 clang/llvm 工具链并使用 clang 的本机 libc++ (而不是借用 Gcc 的 stdlibc++)来编译张量流。

看起来 bazel plain 假设每个 clang 都会使用 Gcc 的库,因为我收到以下错误:

$ bazel build --cxxopt=-std=c++11 --cxxopt=-stdlib=libc++ tensorflow:libtensorflow.so
INFO: Found 1 target...
INFO: From Compiling 
external/protobuf/src/google/protobuf/compiler/js/embed.cc [for host]:
external/protobuf/src/google/protobuf/compiler/js/embed.cc:37:12: 
warning: unused variable 'output_file' [-Wunused-const-variable]
const char output_file[] = "well_known_types_embed.cc";
            ^
1 warning generated.
ERROR: /home/hbucher/.cache/bazel/_bazel_hbucher/ad427c7fddd5b68de5e1cfaa7cd8c8cc/external/com_googlesource_code_re2/BUILD:11:1: undeclared inclusion(s) in rule '@com_googlesource_code_re2//:re2':
this rule is missing dependency declarations for the following files included by 'external/com_googlesource_code_re2/re2/bitstate.cc':
  '/home/hbucher/install/include/c++/v1/stddef.h'
  '/home/hbucher/install/include/c++/v1/__config'
Run Code Online (Sandbox Code Playgroud)

我尝试侵入 bazel 内的 tools/cpp/CROSSTOOL,因为一些帖子建议添加该行

cxx_builtin_include_directory: "/home/hbucher/install/include/c++/v1"
Run Code Online (Sandbox Code Playgroud)

但无济于事,似乎也没有什么区别。

然后我尝试按照 bazel 教程创建自定义工具链。文本没有多大帮助,因为他们实际上正在编写一个交叉工具,而我想做的是调整现有的主机规则,并且 bazel 似乎以某种方式撤消了我尝试调整其参数的每一次尝试。

我已经到达了当前在我的 github 存储库中的点https://github.com/HFTrader/BazelCustomToolchain

但是它无法编译,我什至不知道如何开始调试此消息。

$  bazel build --crosstool_top=@hbclang//:toolchain tensorflow:libtensorflow.so                                                                                             
.....................                                                                                                                                                                                                                                                                                                                                   
ERROR: The crosstool_top you specified was resolved to 
'@hbclang//:toolchain', which does not contain a CROSSTOOL file. You can 
use a crosstool from the depot by specifying its label.
INFO: Elapsed time: 2.216s  
Run Code Online (Sandbox Code Playgroud)

我已将这些行附加到我的张量流/工作空间中

new_local_repository(                                                                                                                                                                                                                                                                                                                           
  name="hbclang",                                                                                                                                                                                                                                                                                                                                 
  path="/home/hbucher/BazelCustomToolchain",                                                                                                                                                                                                                                                                                     
  build_file = "/home/hbucher/BazelCustomToolchain/BUILD",      
)
Run Code Online (Sandbox Code Playgroud)

我在 bazel 的 google groups 上问过这个问题,但他们将我重定向到 stackoverflow。此时我已经快要放弃了。

有人尝试这样做吗?或者我正在这里破土动工?

谢谢。

hlo*_*pko 2

[2020-05-24:编辑以使答案更新。]

TLDR:要使用带有特定 Clang 二进制文件的 Bazel 和 libc++ 构建项目,这对我有用(这INSTALL_DIR是我安装 llvm 的位置):

CC="$INSTALL_DIR/bin/clang" \
BAZEL_CXXOPTS="-stdlib=libc++:-isystem$INSTALL_DIR/include" \
BAZEL_LINKOPTS="-stdlib=libc++" \
BAZEL_LINKLIBS="-L$INSTALL_DIR/lib:-Wl,-rpath,$INSTALL_DIR/lib:-lc++:-lm" \
bazel test //...
Run Code Online (Sandbox Code Playgroud)

背景:

您可以使用--repo_env选项,例如--repo_env=CC=clang,将这些默认值放入项目或系统范围的 .bazelrc 中。

这种方法使用 Bazel 的 C++ 工具链自动配置,它不会尝试在 BUILD 文件中声明所有工具链输入。这是为了简化用户的配置。因此,每当您以 Bazel 无法知道的方式修改 C++ 工具链(重建 llvm 等)时,您都必须运行bazel clean --expunge以刷新缓存并在下次重新运行自动配置。

在 Bazel 中指定 C++ 工具链的可靠解决方案是使用 CcToolchainConfigInfo。请参阅https://docs.bazel.build/versions/master/tutorial/cc-toolchain-config.html和https://docs.bazel.build/versions/master/cc-toolchain-config-reference中的文档。 html