Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make android tools scripts Python3-compatible for building android apps #4097

Closed
angerson opened this issue Nov 15, 2017 · 7 comments
Closed

Comments

@angerson
Copy link
Contributor

Description of the problem / feature request / question:

Google's TFLite team has discovered that the TF Lite Android demo application cannot build via Bazel with Python 3, because the android tools Bazel uses aren't Python-3 compatible. The first breaking point looks like third_party/gflags, but past that there seem to be additional version incompatibilities.

If possible, provide a minimal example to reproduce the problem:

Here's a very fast example:

$ git clone http://github.com/bazelbuild/bazel /tmp/bazel
$ cd /tmp/bazel
$ python3
>>> import tools.android.aar_embedded_jars_extractor

I'm not actually sure how to do the same as the above in python 2, but that sequence of commands results in the same errors we see when trying to build the demo app (No module named 'gflags'). I can sort of fix this with $ pip install python-gflags, which makes Python not even try to use the local files (this seems like an ultimately buggy workaround). The import is fixed, but the build (below) is still busted with a str-to-buffer message problem which I've included at the very bottom of the issue.

Here's how I ran the TF Lite app build. The build requires the SDK setup steps for Tensorflow's WORKSPACE.

$ virtualenv --python=python3 --system-site-packages /tmp/py3
$ git clone http://github.com/tensorflow/tensorflow /tmp/tf
$ cd /tmp/tf
$ source /tmp/py3/bin/activate
# prepare WORKSPACE, see link above
$ ./configure      # should default to /tmp/py3 python path
$ bazel build //tensorflow/contrib/lite/java/demo/app/src/main:TfLiteCameraDemo --cxxopt='--std=c++11' --config=android_arm 

Environment info

  • Operating System: Ubuntu 14.04 LTS
  • Bazel version (output of bazel info release): release 0.7.0

Have you found anything relevant by searching the web?

Anything else, information or logs or outputs that would be helpful?

This incompatibility means that the TF Lite app can't be built (afaik) unless users install Python 2. Not a tremendous problem, but pretty inconvenient.

From the build commands above, I get something like this:

(py3)❯ bazel build //tensorflow/contrib/lite/java/demo/app/src/main:TfLiteCameraDemo --cxxopt='--std=c++11' --config=android_arm --logging=0
WARNING: The major revision of the Android NDK referenced by android_ndk_repository rule 'androidndk' is 15. The major revisions supported by Bazel are [10, 11, 12, 13, 14]. Defaulting to revision 14.
INFO: Analysed target //tensorflow/contrib/lite/java/demo/app/src/main:TfLiteCameraDemo (0 packages loaded).
INFO: Found 1 target...
ERROR: /usr/local/google/home/angerson/.cache/bazel/_bazel_angerson/7b7f16644e8d0eb6d37d6401f95d6305/external/androidsdk/com.android.support/BUILD:4563:1: Filtering AAR native libs by architecture failed (Exit 1)
Traceback (most recent call last):
  File "/usr/local/google/home/angerson/.cache/bazel/_bazel_angerson/7b7f16644e8d0eb6d37d6401f95d6305/execroot/org_tensorflow/bazel-out/host/bin/external/bazel_tools/tools/android/aar_native_libs_zip_creator.runfiles/org_tensorflow/../bazel_tools/tools/android/aar_native_libs_zip_creator.py", line 26, in <module>
    from third_party.py import gflags
  File "/usr/local/google/home/angerson/.cache/bazel/_bazel_angerson/7b7f16644e8d0eb6d37d6401f95d6305/execroot/org_tensorflow/bazel-out/host/bin/external/bazel_tools/tools/android/aar_native_libs_zip_creator.runfiles/bazel_tools/third_party/py/gflags/__init__.py", line 1, in <module>
    from gflags import *
ImportError: No module named 'gflags'
Target //tensorflow/contrib/lite/java/demo/app/src/main:TfLiteCameraDemo failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 0.440s, Critical Path: 0.11s
FAILED: Build did NOT complete successfully

And here's the str-to-buffer problem after installing pip install python-gflags:

(py3)❯ bazel build //tensorflow/contrib/lite/java/demo/app/src/main:TfLiteCameraDemo --cxxopt='--std=c++11' --config=android_arm --logging=0
WARNING: The major revision of the Android NDK referenced by android_ndk_repository rule 'androidndk' is 15. The major revisions supported by Bazel are [10, 11, 12, 13, 14]. Defaulting to revision 14.
INFO: Analysed target //tensorflow/contrib/lite/java/demo/app/src/main:TfLiteCameraDemo (50 packages loaded).
INFO: Found 1 target...
ERROR: /usr/local/google/home/angerson/.cache/bazel/_bazel_angerson/7b7f16644e8d0eb6d37d6401f95d6305/external/androidsdk/com.android.support/BUILD:4563:1: Extracting classes.jar and libs/*.jar from support-fragment-25.2.0.aar failed (Exit 1)
Traceback (most recent call last):
  File "/usr/local/google/home/angerson/.cache/bazel/_bazel_angerson/7b7f16644e8d0eb6d37d6401f95d6305/execroot/org_tensorflow/bazel-out/host/bin/external/bazel_tools/tools/android/aar_embedded_jars_extractor.runfiles/org_tensorflow/../bazel_tools/tools/android/aar_embedded_jars_extractor.py", line 92, in <module>
    main()
  File "/usr/local/google/home/angerson/.cache/bazel/_bazel_angerson/7b7f16644e8d0eb6d37d6401f95d6305/execroot/org_tensorflow/bazel-out/host/bin/external/bazel_tools/tools/android/aar_embedded_jars_extractor.runfiles/org_tensorflow/../bazel_tools/tools/android/aar_embedded_jars_extractor.py", line 87, in main
    _Main(FLAGS.input_aar, FLAGS.output_singlejar_param_file, FLAGS.output_dir)
  File "/usr/local/google/home/angerson/.cache/bazel/_bazel_angerson/7b7f16644e8d0eb6d37d6401f95d6305/execroot/org_tensorflow/bazel-out/host/bin/external/bazel_tools/tools/android/aar_embedded_jars_extractor.runfiles/org_tensorflow/../bazel_tools/tools/android/aar_embedded_jars_extractor.py", line 67, in _Main
    output_dir_orig)
  File "/usr/local/google/home/angerson/.cache/bazel/_bazel_angerson/7b7f16644e8d0eb6d37d6401f95d6305/execroot/org_tensorflow/bazel-out/host/bin/external/bazel_tools/tools/android/aar_embedded_jars_extractor.runfiles/org_tensorflow/../bazel_tools/tools/android/aar_embedded_jars_extractor.py", line 48, in ExtractEmbeddedJars
    singlejar_param_file.write("--exclude_build_data\n")
TypeError: 'str' does not support the buffer interface
Target //tensorflow/contrib/lite/java/demo/app/src/main:TfLiteCameraDemo failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 10.214s, Critical Path: 0.54s
FAILED: Build did NOT complete successfully
@akirabaruah
Copy link
Contributor

I think that #3855 should resolve this issue, but I'll test it to confirm.

@aj-michael
Copy link
Contributor

Hey thanks for bringing this to our attention, ccing some people that can help
@jin @dkelmer @ahumesky

@akirabaruah
Copy link
Contributor

akirabaruah commented Nov 21, 2017

Thanks @aj-michael - I was able to reproduce @angersson's errors and can confirm that #3855 resolves the first one.

However, the second error persists. It can probably be resolved by updating modules in tools/android and tools/build_defs for compatibility with python3-style strings. Should be relatively simple, so I can probably make those changes soon.

@akirabaruah
Copy link
Contributor

akirabaruah commented Nov 22, 2017

As promised, I've created a branch containing python 3 compatibility updates to bazel tools.

@angersson @aj-michael @jin @dkelmer @ahumesky you can test the fix by running:

git clone https://github.com/akira-baruah/bazel.git /tmp/bazel-akira
cd /tmp/bazel-akira
git checkout python3-tools  # branch with bugfixes
bazel build //src:bazel

cd <tensorflow repo>
/tmp/bazel-akira/bazel-bin/src/bazel build //tensorflow/contrib/lite/java/demo/app/src/main:TfLiteCameraDemo --cxxopt='--std=c++11' --force_python=py3 --python_path=$(which python3) --config=android_arm --logging=0

If it looks good to you, I'll make a PR. Please note that this branch depends on PR #3855, so we would probably want to have that merged first.

Thanks!

@angerson
Copy link
Contributor Author

I was able to replicate the fixes, but I'll need to fetch NDK 15 to fully test building the app in a clean environment due to #4068.

@angerson
Copy link
Contributor Author

I verified build success with nvidia-docker, gcr.io/tensorflow/tensorflow:latest-devel-gpu, and this... awful script, run inside the container:

#!/usr/bin/env bash

set -euxo pipefail

TMPDIR=$(mktemp -d)
trap '' PIPE

git clone https://github.com/akira-baruah/bazel.git ${TMPDIR}/bazel-akira
cd ${TMPDIR}/bazel-akira
git checkout python3-tools  # branch with bugfixes
bazel build //src:bazel

cd ${TMPDIR}

curl -o ${TMPDIR}/sdk-tools.zip https://dl.google.com/android/repository/sdk-tools-linux-3859397.zip
unzip ${TMPDIR}/sdk-tools.zip -d ${TMPDIR}/sdk
echo "y" | ${TMPDIR}/sdk/tools/bin/sdkmanager "build-tools;26.0.1" "platforms;android-26" "extras;android;m2repository"

pip install virtualenv
virtualenv --python=python3 --system-site-packages ${TMPDIR}/py3
PS1=""  # Avoids `set -u`-related error with bin/activate
source ${TMPDIR}/py3/bin/activate

git clone http://github.com/tensorflow/tensorflow ${TMPDIR}/tf
cd ${TMPDIR}/tf

curl -o ${TMPDIR}/ndk.zip https://dl.google.com/android/repository/android-ndk-r14b-linux-x86_64.zip
unzip ${TMPDIR}/ndk.zip -d ${TMPDIR}/ndk/

cat >> ${TMPDIR}/tf/WORKSPACE <<HEREDOC
android_sdk_repository(
    name = "androidsdk",
    api_level = 26,
    build_tools_version = "26.0.1",
    path = "${TMPDIR}/sdk",
)
android_ndk_repository(
    name="androidndk",
    path="${TMPDIR}/ndk/android-ndk-r14b",
    api_level=14)
HEREDOC

set +o pipefail
yes '' | ${TMPDIR}/tf/configure
set -o pipefail
cd ${TMPDIR}/tf
${TMPDIR}/bazel-akira/bazel-bin/src/bazel build //tensorflow/contrib/lite/java/demo/app/src/main:TfLiteCameraDemo --cxxopt='--std=c++11' --config=android_arm

Sweet.

@akirabaruah
Copy link
Contributor

@angersson now that gflags supports python 3 imports (see commits fb15f0f, d926bc4, and 4d0e626), I've created PR #4265 to resolve this issue.

akirabaruah added a commit to akirabaruah/bazel that referenced this issue Dec 11, 2017
akirabaruah added a commit to akirabaruah/bazel that referenced this issue Dec 16, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants