Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add external header files during bazel/tensorflow build

I am trying to add external header file (like OpenCL header file) for some experimentation for tensorflow. I tried to add this into BUILD file under tensorflow/core/BUILD file:

# This includes implementations of all kernels built into TensorFlow.
cc_library(
    name = "all_kernels",
    visibility = ["//visibility:public"],
    copts = tf_copts() + ["-Ithird_party/include"],    <==== this is the line I added

I have also created a softlink in this directory to the location of these header files from OpenCL driver (under tensorflow/third_party) too (like ln -s /opt/opencl/ ) but it still complains that it has not found that header file.

If I add external header file directly (like /opt/opencl/CL/) it complains that external files cannot be included (or some such thing).

I do not have root password to copy these header files into /usr/include/ too.

Can someone explain how exactly to do external header files into tensorflow for building?

Thanks for any quick help.

like image 522
Prakash Raghavendra Avatar asked Jun 11 '16 08:06

Prakash Raghavendra


People also ask

How do I add a header file to a compiler?

You can either add a -I option to the command line to tell the compiler to look there for header files. If you have header files in include/ directory, then this command should work for you. There shouldn't be any space between -I compiler option and directory location.

How do I add an external header file in Visual Studio?

Then, I right-click on Solution Explorer, choose to add an existing item, and then browse to the location of my file myheader. hpp . Once this is added, I see it appears under Solution Items .

Can you include header files in header files?

Yes, this will work. Note, however, that if you include a lot of headers in this file and don't need all of them in each of your source files, it will likely increase your compilation time.

What are build Bazel files?

Bazel builds software from source code organized in a directory called a workspace. Source files in the workspace are organized in a nested hierarchy of packages, where each package is a directory that contains a set of related source files and one BUILD file.


1 Answers

I've faced with the similar problem when I built TensorFlow with Intel MKL and had to add MKL headers. My solution is the following:

  1. Create symlink to your headers into third_party folder, like:

    <your tensorflow folder>/third_party/opencl/include -> /opt/OpenCL/include
    

    with command:

    ln -s /opt/OpenCL/include <your tensorflow folder>/third_party/opencl
    
  2. Create simple BUILD file into <your tensorflow folder>/third_party/opencl folder:

    cc_library(
        name = "opencl",
        hdrs = glob(["include/CL/*.h"]),
        visibility = ["//visibility:public"],
    )
    
  3. Add deps into target library:

    cc_library(
        name = "all_kernels",
        visibility = ["//visibility:public"],
        copts = tf_copts() + ["-Ithird_party/opencl/include"],
        deps = [
            "//third_party/opencl", 
            ...
        ],
    )
    
  4. Don't forget to add compiler options either into target library as shown above or just as a flag to bazel:

     bazel build --copt="-Ithird_party/opencl/include" ...
    
like image 131
Anton V. Gorshkov Avatar answered Oct 14 '22 09:10

Anton V. Gorshkov