This is like a follow up question from Build an android app using Tensorflow. I'd like to separate the android example project from tensorflow git repo and be able to build it separately with tensorflow as a dependency. This is my folder structure
my_project
|-- WORKSPACE
|-- android
| |-- BUILD
| `-- ...
|-- tensorflow
| |-- tensorflow
| | | |-- workspace.bzl
| | | |-- tensorflow.bzl
| | | `-- ...
| |-- WORKSPACE
| |-- BUILD
. `-- ...
where android app is just a copy of Tensorflow Android example. The root WORKSPACE file has following contents:
workspace(name = "my_android_app")
local_repository(
name = "org_tensorflow",
path = "tensorflow", # Relative path to the tensorflow workspace
)
load('//android:workspace.bzl', 'android_workspace')
android_workspace()
# Specify the minimum required bazel version.
load("@org_tensorflow//tensorflow:tensorflow.bzl", "check_version")
check_version("0.3.1")
android/workspace.bzl looks like this
load('@org_tensorflow//tensorflow:workspace.bzl', 'tf_workspace')
def android_workspace():
tf_workspace()
and android/BUILD has the same contents as Tensorflow Android example BUILD except that I've prefixed @org_tensorflow to //tensorflow everywhere, for example
"@org_tensorflow//tensorflow:tensorflow.bzl"
"@org_tensorflow//tensorflow/contrib/android:android_tensorflow_inference_jni",
"@org_tensorflow//tensorflow/core:android_tensorflow_lib",
when I try to build the main target tensorflow_demo, it gives this error
no such package 'tensorflow': Package crosses into repository @org_tensorflow and referenced by '//android:libtensorflow_demo.so'.
ERROR: Analysis of target '//android:tensorflow_demo' failed; build aborted.
EDIT:
I was able to decouple Tensorflow Android demo from Tensorflow source, thanks to Kristina. You can use the following git as a template for your Tensorflow Android projects. https://github.com/devinsaini/tensorflow_android
Ah, okay, I see what you're doing. Thanks for elaborating.
The problem here is a bit subtle:
copts = tf_copts()
calls a function in @org_tensorflow that looks like this:Basically a switch statement to use different flags on different platforms. However, because
load()
s are evaluated beforeselect()
s, your BUILD file suddenly "contains" references to//tensorflow
.To fix, the easiest options are to either hard-code the copts you need or redefine tf_copts in your local repository.
How to track down incorrect references in general
I was actually pretty baffled by the error you were getting, so here's a brain dump of how one usually goes about debugging this kind of thing.
The usual way to track down this kind of cross reference is 1) to examine your BUILD file (maybe the deps are just wrong) or 2) print out how Bazel actually "sees" the BUILD file. For example, if you wanted to print the evaluated build rule in this case, you could do:
However, this doesn't actually help you in this case, because it doesn't show the copts!
So, after running the query, I ended up "binary searching" the BUILD rule: I commented out everything but the srcs, tried building, then gradually uncommented parts until I got the error.