Search code examples
c++tensorflowcross-compilingbazeltoolchain

"Missing dependency declarations" for system headers when cross-compiling with Bazel


I'm trying to cross-compile a project (TensorFlow C library) using Bazel with a cross-compiling toolchain. After much desperation (but that's another story), I finally managed to get the actual compilation of external dependencies (e.g. zlib or protobuf) to work with my toolchain, however, I'm now getting missing dependency declarations for all system headers (provided by the cross-compiling toolchain) that are included by the compiled rule that generates this error.

This exact error is described in Bazel's own tutorial for configuring C++ toolchains (at the very bottom in step 10). They also suggest a fix that consists of adding a feature to the CcToolchainConfigInfo that adds the system header directories via -isystem flags.

The problem that I have with that fix is that I cannot add the absolute paths of these directories as described, as that triggers this error: The include path ... references a path outside of the execution root.

Instead of using absolute paths, I've also tried declaring new local repositories in my WORKSPACE with filegroups and cc_libraries that list the paths in their srcs/hdrs/includes and referencing those filegroups/cc_libraries in the -isystem flags instead, but none of my attempts have worked. Maybe I didn't do it right but I've tried many variations.

If I cannot add absolute paths or get it to work the the local repositories, then what is the correct way of doing this?


Solution

  • For the check to know which headers you are using from your host and should be considered system headers / are considered OK even when not explicitly declared as a dependencies, you supply list of directories containing such headers to cxx_builtin_include_directories attribute of create_cc_toolchain_config_info() as you are defining your cc_toolchain. For example writing one for my system compiler and system libraries from the host, I could say:

    cc_common.create_cc_toolchain_config_info(
        ...
        cxx_builtin_include_directories = [
            "/usr/include/",
            "/usr/lib64/gcc/x86_64-slackware-linux/9.3.0/include/",
        ],
        ...
    )
    

    I am currently not aware of another way to do so for host tools that are not residing inside bazel's own tree. At least as of 3.1.0 it works and is documented.

    Note: This configures the dependency check, not search paths for the tools as such, that's where an appropriate feature you've mentioned comes to play.