Search code examples
linuxtensorflowpackagebazelarch

Bazel doesn't find tensorflow packages for C++ example code


I try to make this example work, but everytime I try to build the program with bazel I get this error message:

bazel build //code:label_image 
ERROR: /home/jonas/tensorflow/code/BUILD:12:1: no such package 'tensorflow': BUILD file not found on package path and referenced by '//code:label_image'.
ERROR: /home/jonas/tensorflow/code/BUILD:12:1: no such package 'tensorflow': BUILD file not found on package path and referenced by '//code:label_image'.
ERROR: /home/jonas/tensorflow/code/BUILD:12:1: no such package 'tensorflow': BUILD file not found on package path and referenced by '//code:label_image'.
ERROR: /home/jonas/tensorflow/code/BUILD:12:1: no such package 'tensorflow': BUILD file not found on package path and referenced by '//code:label_image'.
ERROR: /home/jonas/tensorflow/code/BUILD:12:1: no such package 'tensorflow': BUILD file not found on package path and referenced by '//code:label_image'.
ERROR: Analysis of target '//code:label_image' failed; build aborted.
INFO: Elapsed time: 1.261s

I saved the exact source code from github in a directory called code. I installed tensorflow in an (active) virtual environment via pip: pip3 install --upgrade tensorflow. I use arch linux.

Why doesn't bazel find the proper packages? I'm quite new to bazel/tensorflow. Where are these packages saved? Do I have to specify them explicitely somewhere?


Solution

  • Typically, extracting a subfolder from a project that uses Bazel and building it separately does not work.

    If you look into the BUILD file of the label_image folder, you will see this definition for a C++ binary:

    cc_binary(
        name = "label_image",
        srcs = [
            "main.cc",
        ],
        linkopts = select({
            "//tensorflow:android": [
                "-pie",
                "-landroid",
                "-ljnigraphics",
                "-llog",
                "-lm",
                "-z defs",
                "-s",
                "-Wl,--exclude-libs,ALL",
            ],
            "//conditions:default": ["-lm"],
        }),
        deps = select({
            "//tensorflow:android": [
                # cc:cc_ops is used to include image ops (for label_image)
                # Jpg, gif, and png related code won't be included
                "//tensorflow/cc:cc_ops",
                "//tensorflow/core:android_tensorflow_lib",
                # cc:android_tensorflow_image_op is for including jpeg/gif/png
                # decoder to enable real-image evaluation on Android
                "//tensorflow/core/kernels:android_tensorflow_image_op",
            ],
            "//conditions:default": [
                "//tensorflow/cc:cc_ops",
                "//tensorflow/core:core_cpu",
                "//tensorflow/core:framework",
                "//tensorflow/core:framework_internal",
                "//tensorflow/core:lib",
                "//tensorflow/core:protos_all_cc",
                "//tensorflow/core:tensorflow",
            ],
        }),
    )
    

    This rule tells Bazel what the label_image binary requires to be built. Notably, it has dependencies (deps) and link options (linkopts) that point to the root of the tensorflow workspace (//tensorflow, defined by the WORKSPACE file), which is missing from your extracted subfolder. This is the reason why Bazel is complaining that it cannot find the package tensorflow.

    The easiest way to build the label_image binary is to build it from within the tensorflow project, since the paths are already set up.