Using custom clang with libc++

1,242 views
Skip to first unread message

Henrique Bucher

unread,
May 15, 2017, 3:56:33 PM5/15/17
to bazel-discuss
Hello folks, 
We a custom clang with additional homegrown optimization passes for our application. We are incorporating tensorflow as a shared library and for that we had to customize the build, which has been achieved. Bazel compiles tensorflow with our custom clang and generates the .so library as expected.
The problem now is that when we try to create the library using libc++ instead of the default GCC libstdc++, it spews out this error:

$ bazel build --cxxopt=-std=c++11 --cxxopt=-stdlib=libc++ tensorflow:libtensorflow_all.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'
  '/home/hbucher/install/include/c++/v1/__nullptr'
  '/home/hbucher/install/include/c++/v1/stdint.h'
  '/home/hbucher/install/include/c++/v1/string.h'
  '/home/hbucher/install/include/c++/v1/stdio.h'

I am struggling with the path of creating a custom toolchain so my question to you is:

Is there any way to just tweak the zipped bazel distribution (bazel-4.5.0-dist.zip) such that I can get through this without creating a custom toolchain?

Alternatively, would you have a ready example of files for a custom clang/x86_64 I can use with --crosstool_top?

Thank you.

Robert Tsai (Verb Ext.)

unread,
May 15, 2017, 4:22:43 PM5/15/17
to Henrique Bucher, bazel-discuss
The most easily-accessible "ready example" is:
  1. bazel build //something
  2. cp $HOME/bazel-<workspace_name>/external/local_config_cc/{BUILD,CROSSTOOL} $HOME/tmp
Use those BUILD and CROSSTOOL files as a template for your own toolchain.

--Rob

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discuss+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/20533346-7eac-41e6-a785-62660d75a116%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Austin Schuh

unread,
May 15, 2017, 11:17:13 PM5/15/17
to Robert Tsai (Verb Ext.), Henrique Bucher, bazel-discuss
You want something like the following in your CROSSTOOL file.

cxx_builtin_include_directory: "%package(@clang_3_7_repo//lib/clang/3.7.1/include)%"

The %package(@...)% stanza computes the path to the package with the compiler in it.  You can also just do

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

if you want to require everyone to install the compiler at that path.

Austin

To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/CACJAxRzvDQ08WF4wbKE9mscfqG2GkG%2BxZ36n14GSk8SFyNMJvQ%40mail.gmail.com.

Henrique Bucher

unread,
May 16, 2017, 12:34:02 AM5/16/17
to Austin Schuh, Robert Tsai (Verb Ext.), bazel-discuss
Thanks for your answers!

I added the section below to tensorflor/WORKSPACE 

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

where /home/hbucher/BazelCustomToolchain points to the git checkout of 

$ ls /home/hbucher/BazelCustomToolchain/ -l
-rwxrwxr-x 1 hbucher hbucher   896 May 15 21:46 BUILD
-rwxrwxr-x 1 hbucher hbucher   867 May 15 15:42 cc_wrapper.sh
-rwxrwxr-x 1 hbucher hbucher  2869 May 15 23:21 CROSSTOOL
-rw-rw-r-- 1 hbucher hbucher 35141 May 15 23:17 LICENSE
-rw-rw-r-- 1 hbucher hbucher    80 May 15 23:17 README.md
-rw-rw-r-- 1 hbucher hbucher   111 May 15 15:39 WORKSPACE

But when I run the build I get this:

$ /home/hbucher/build-ts/bazel/output/bazel build --crosstool_top=//hbclang:toolchain tensorflow:libtensorflow_all.so
ERROR: no such package 'hbclang': BUILD file not found on package path.

I am very confused how the wrapping of all these files work. I went through the tutorial 

but that only made everything more confusing. 

Would you be able to lend me a hand here?

Thank you.



To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discuss+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discuss+unsubscribe@googlegroups.com.

Austin Schuh

unread,
May 16, 2017, 1:11:51 PM5/16/17
to hbu...@gmail.com, Robert Tsai (Verb Ext.), bazel-discuss
On Mon, May 15, 2017 at 9:34 PM Henrique Bucher <hbu...@gmail.com> wrote:
Thanks for your answers!

I added the section below to tensorflor/WORKSPACE 

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

where /home/hbucher/BazelCustomToolchain points to the git checkout of 

Good
 
$ ls /home/hbucher/BazelCustomToolchain/ -l
-rwxrwxr-x 1 hbucher hbucher   896 May 15 21:46 BUILD
-rwxrwxr-x 1 hbucher hbucher   867 May 15 15:42 cc_wrapper.sh
-rwxrwxr-x 1 hbucher hbucher  2869 May 15 23:21 CROSSTOOL
-rw-rw-r-- 1 hbucher hbucher 35141 May 15 23:17 LICENSE
-rw-rw-r-- 1 hbucher hbucher    80 May 15 23:17 README.md
-rw-rw-r-- 1 hbucher hbucher   111 May 15 15:39 WORKSPACE

But when I run the build I get this:

$ /home/hbucher/build-ts/bazel/output/bazel build --crosstool_top=//hbclang:toolchain tensorflow:libtensorflow_all.so
ERROR: no such package 'hbclang': BUILD file not found on package path.

I am very confused how the wrapping of all these files work. I went through the tutorial 

but that only made everything more confusing. 

Would you be able to lend me a hand here?

Thank you.


Try  

$ /home/hbucher/build-ts/bazel/output/bazel build --crosstool_top=@hbclang//:toolchain tensorflow:libtensorflow_all.so

That will at least get you to the next error.  ;)

I was looking for the documentation to point you to for that, but I had to instead file https://github.com/bazelbuild/bazel/issues/3013

https://bazel.build/versions/master/docs/external.html talks about the syntax of the label, but not very directly.

Austin

Henrique Bucher

unread,
May 17, 2017, 10:14:25 AM5/17/17
to Austin Schuh, Robert Tsai (Verb Ext.), bazel-discuss
I ultimately gave up on this. It is impossible to get bazel to do anything that is not mainstream. Never seen such cryptic system. Throwing the towel and feeling miserable.

Irina Iancu

unread,
May 17, 2017, 10:51:33 AM5/17/17
to bazel-discuss, austin...@gmail.com, rober...@verbsurgical.com, hbu...@gmail.com, Marcel Hlopko
I'm adding Marcel, who knows a lot about crosstool, to take a look at this.

Marcel Hlopko

unread,
May 17, 2017, 10:53:26 AM5/17/17
to Irina Iancu, bazel-discuss, austin...@gmail.com, rober...@verbsurgical.com, hbu...@gmail.com
Hi Henrique, can you share a git repo with your toolchain (or what you have so far) and a hello world cc file? I'll take it from there.
--
-- 
Marcel Hlopko | Software Engineer | hlo...@google.com | 

Google Germany GmbH | Erika-Mann-Str. 33  | 80636 München | Germany | Geschäftsführer: Matthew Scott Sucherman, Paul Terence Manicle | Registergericht und -nummer: Hamburg, HRB 86891

Henrique Bucher

unread,
May 17, 2017, 11:25:05 AM5/17/17
to Marcel Hlopko, Irina Iancu, bazel-discuss, Austin Schuh, Robert Tsai (Verb Ext.)
The link is already in the email. 

Marcel Hlopko

unread,
May 17, 2017, 11:27:26 PM5/17/17
to hbu...@gmail.com, Irina Iancu, bazel-discuss, Austin Schuh, Robert Tsai (Verb Ext.)
Uploaded https://github.com/HFTrader/BazelCustomToolchain/pull/1. Let us know if it did or didn't help, or where you got stuck next. Also, consider asking on Stack Overflow, we monitor questions tagged "Bazel" in the same way we check bazel-discuss, but SO feels like is a better platform for building knowledge base than google groups.

All in all, we are very well aware how hard and frustrating writing Crosstool is and I'm sorry you had to suffer. This is one of our highest priorities on the C++ front so there is a hope it will get simpler and saner. In the meantime, do not hesitate to ask. Thanks!

Henrique Bucher

unread,
May 18, 2017, 2:04:52 AM5/18/17
to Marcel Hlopko, Irina Iancu, bazel-discuss, Austin Schuh, Robert Tsai (Verb Ext.)
I will post there. Meanwhile, here is what I get now:

hbucher@vbox-ubuntu-server:~/build-ts/tensorflow-github$ /home/hbucher/build-ts/bazel/output/bazel build --crosstool_top=@hbclang//:toolchain tensorflow:libtensorflow_all.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  


Henrique Bucher

unread,
May 18, 2017, 2:10:50 AM5/18/17
to Marcel Hlopko, Irina Iancu, bazel-discuss, Austin Schuh, Robert Tsai (Verb Ext.)
I have appended these lines to my tensorflow/WORKSPACE

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

Henrique Bucher

unread,
May 18, 2017, 5:57:34 PM5/18/17
to Marcel Hlopko, Irina Iancu, bazel-discuss, Austin Schuh, Robert Tsai (Verb Ext.)

On Wed, May 17, 2017 at 10:27 PM, Marcel Hlopko <hlo...@google.com> wrote:

Marcel Hlopko

unread,
May 19, 2017, 12:41:30 AM5/19/17
to hbu...@gmail.com, Irina Iancu, bazel-discuss, Austin Schuh, Robert Tsai (Verb Ext.)
Replied to the SO question.

Henrique Bucher

unread,
May 19, 2017, 12:58:30 AM5/19/17
to Marcel Hlopko, Irina Iancu, bazel-discuss, Austin Schuh, Robert Tsai (Verb Ext.)
Seriously, your reply did not bring any new information.

I give up. Bazel is a piece of shit.

Pedro Kiefer

unread,
May 19, 2017, 9:39:03 AM5/19/17
to hbu...@gmail.com, Marcel Hlopko, Irina Iancu, bazel-discuss, Austin Schuh, Robert Tsai (Verb Ext.)
How rude you are. There's no need for that, just simply don't use it.

I use it to all my embedded projects, using different crosstools for arm-none, arm-linux, stm8, blackfin, etc. It works great, but I'll give you that the learning curve for writing your own CROSSTOOL and custom rules is a steep one.

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discuss+unsubscribe@googlegroups.com.

Henrique Bucher

unread,
May 19, 2017, 9:44:55 AM5/19/17
to Pedro Kiefer, Marcel Hlopko, Irina Iancu, bazel-discuss, Austin Schuh, Robert Tsai (Verb Ext.)
Rude? ROFL. Are we prioritizing feelings now? That might explain why this crap is so half baked.
We have a business and I am almost losing a client because of this tool. 
How about writing some software that works, well documented and without hardcoded paths everywhere?  

Henrique Bucher

unread,
May 20, 2017, 12:32:03 AM5/20/17
to Pedro Kiefer, Marcel Hlopko, Irina Iancu, bazel-discuss, Austin Schuh, Robert Tsai (Verb Ext.)
Sorry it was indeed rude. 
It has been a mad week in here - I'm trying to get this startup off the ground.
Apologies.

Henrique Bucher

unread,
May 20, 2017, 1:47:36 PM5/20/17
to Marcel Hlopko, Irina Iancu, bazel-discuss, Austin Schuh, Robert Tsai (Verb Ext.)
Solved

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-tp && cd build-tmp
cmake ../tensorflow/contrib/cmake/
make -j4
Reply all
Reply to author
Forward
0 new messages