bazel icon indicating copy to clipboard operation
bazel copied to clipboard

CROSSTOOL questions

Open ghost opened this issue 9 years ago • 4 comments

I am attempting to adapt Bazel to a project that needs to cross-compile to various embedded platforms.

After following the guide and examining the sample project I have things tentatively working. However, there are a few small issues and I have some questions.

An extra cc_toolchain rule is present in the toolchain BUILD file; I could not get my build to work without it (bazel build invocations ignored my CPU specification and tried to build with cpu=k8) but yet it goes unexplained in the guide. Why is this needed? What is its purpose?

If I omit a --cpu= specification when specifying my CROSSTOOL in a blaze build command, it tries to default to using the aforementioned cc-compiler-k8 target, and this fails with a visibility issue:

scott.minor-local-> bazel build --crosstool_top=//tools/ti_arm_compiler:toolchain hello_world:hello_world
ERROR: /home/scott.minor-local/.cache/bazel/_bazel_scott.minor-local/0d573361e307428d3d42ae2e893c15a9/external/bazel_tools/tools/cpp/BUILD:7:1: Target '//tools/ti_arm_compiler:cc-compiler-k8' is not visible from target '@bazel_tools//tools/cpp:stl'. Check the visibility declaration of the former target if you think the dependency is legitimate.
ERROR: Analysis of target '//hello_world:hello_world' failed; build aborted.
INFO: Elapsed time: 0.353s

Fixing this visibility issue in //tools/ti_arm_compiler/BUILD by allowing visibility to @bazel_tools//tools/cpp:stl causes a dependency cycle:

scott.minor-local-> bazel build --crosstool_top=//tools/ti_arm_compiler:toolchain hello_world:hello_world
ERROR: /home/scott.minor-local/.cache/bazel/_bazel_scott.minor-local/0d573361e307428d3d42ae2e893c15a9/external/bazel_tools/tools/cpp/BUILD:7:1: in cc_library rule @bazel_tools//tools/cpp:stl: cycle in dependency graph:
    //hello_world:hello_world
    @bazel_tools//tools/cpp:stl
    //tools/ti_arm_compiler:cc-compiler-k8
.-> @bazel_tools//tools/cpp:stl (host)
|   //tools/ti_arm_compiler:cc-compiler-k8 (host)
`-- @bazel_tools//tools/cpp:stl (host)
This cycle occurred because of a configuration option.
ERROR: Analysis of target '//hello_world:hello_world' failed; build aborted.
INFO: Elapsed time: 0.370s

Fixing this visibility issue by allowing public visibility fixes the dependency cycle. Why is this? Is this expected behavior?

Thanks in advance for your help, -Scott

ghost avatar May 08 '17 19:05 ghost

When you declare visibility, you have to say visibility = ['@bazel_tools//tools/cpp:__pkg__'], not specify the exact target. Otherwise :stl needs to resolve the target :cc-compiler-k8 (because it's a dependency) and :cc-compiler-k8 needs to resolve the target :stl (to figure out visibility) and you end up with a circular dep. Bazel avoids this by having visibility declared in terms of packages or subpackages (__subpackages__), so that the rule itself doesn't have to be resolved.

I'm not sure what your first question meant... are you saying that when you build with --cpu=armeabi-v7a (or whatever) Bazel still tried to use the k8 toolchain?

kchodorow avatar May 08 '17 20:05 kchodorow

Hi Scott, You've got to include --cpu=armeabi-v7a otherwise bazel doesn't know what target you're cross compiling for and defaults to k8.

I think the "extra" cc_toolchain is for any code that needs to be compiled for host instead of target, eg protobufs or host scripts...

jemdiggity avatar May 09 '17 03:05 jemdiggity

https://github.com/jemdiggity/bazel_arm_none.git

jemdiggity avatar May 09 '17 03:05 jemdiggity

https://docs.bazel.build/versions/master/tutorial/cc-toolchain-config.html

oquenchil avatar May 11 '20 13:05 oquenchil