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

Compiling Tensorflow with a custom Clang + Libc++ (instead of stdlibc++)

本文关键字:stdlibc++ Tensorflow 编译 自定义 Clang Libc++      更新时间:2023-10-16

我正在尝试使用自定义的clang/llvm工具链编译tensorflow,并使用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'

我试图在 bazel 中入侵 tools/cpp/CROSSTOOL,因为一些帖子建议添加该行

cxx_builtin_include_directory: "/home/hbucher/install/include/c++/v1"

但无济于事,这似乎没有任何区别。

然后我尝试按照 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  

我已经将这些行附加到我的张量流/工作区

new_local_repository(                                                                                                                                                                                                                                                                                                                           
name="hbclang",                                                                                                                                                                                                                                                                                                                                 
path="/home/hbucher/BazelCustomToolchain",                                                                                                                                                                                                                                                                                     
build_file = "/home/hbucher/BazelCustomToolchain/BUILD",      
)

我在 bazel 的谷歌群组上问过这个问题,但他们将我重定向到 stackoverflow。在这一点上,我即将放弃。

有人试图这样做还是我在这里破土动工?

谢谢。

已解决。不是以预期的方式,但它对我有用。

export INSTALL_DIR="$HOME/install"
export CC=$INSTALL_DIR/bin/clang
export CXX=$INSTALL_DIR/bin/clang++
export CXXFLAGS="-stdlib=libc++ -L$INSTALL_DIR/lib"
export LDFLAGS="-L$INSTALL_DIR/lib -lm -lrt"
export LD_LIBRARY_PATH="/usr/lib:/lib/x86_64-linux-gnu/:$INSTALL_DIR/lib"
git clone https://github.com/tensorflow/tensorflow.git tensorflow-github
cd tensorflow-github
mkdir build-tmp && cd build-tmp
cmake ../tensorflow/contrib/cmake/
make -j4

像 1-2-3 一样简单,带 cmake

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

TLDR:要使用特定的 Clang 二进制文件和 libc++ 使用 Bazel 构建项目,这对我有用(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 //...

背景:

您可以使用--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 中的文档。