I want to build tensorflow for my android smartphone. So I´ve installed bazel and get the sources for tensorflow.
git clone https://github.com/tensorflow/tensorflow
I modify the WORKSPACE file with my paths for the NDK and the SDK
android_sdk_repository(
name = "androidsdk",
api_level = 27,
build_tools_version = "27.0.3",
# Replace with path to Android SDK on your system
path = ".../AppData/Local/Android/Sdk",
)
android_ndk_repository(
name = "androidndk",
path = .../AppData/Local/Android/Sdk/ndk-bundle",
api_level = 27
)
And now I run bazel
bazel build -c opt //tensorflow/contrib/android:libtensorflow_inference.so \
--crosstool_top=//external:android/crosstool \
--host_crosstool_top=#bazel_tools//tools/cpp:toolchain \
--cpu=armeabi-v7a
And got this error
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
Target //tensorflow/contrib/android:libtensorflow_inference.so failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 72,096s, Critical Path: 14,55s
INFO: 213 processes, local.
FAILED: Build did NOT complete successfully
FAILED: Build did NOT complete successfully
How can I compile tensorflow for android with windows (or maybe ubuntu)?
Related
I am trying to make an app for object detection using tensorflow and I am following the instructions as listed in this website:
https://www.skcript.com/svr/realtime-object-and-face-detection-in-android-using-tensorflow-object-detection-api/
But I have run into build errors.
I am making an android application for detecting objects using tensorflow API and I have followed all steps as mentioned in the above link. I am using Windows 10 for coding, not any Linux distro. I tried building the app using bazel but there are build errors.
Here's the command as instructed from the above website:
bazel build -c opt //tensorflow/contrib/android:libtensorflow_inference.so --crosstool_top=//external:android/crosstool --host_crosstool_top=#bazel_tools//tools/cpp:toolchain --cpu=armeabi-v7a
After running, it starts compiling and does 1069 processes, but after reaching 1068/1069 it displays the following:
ERROR: C:/sri/sritrain/tensorflow-master/tensorflow/contrib/android/BUILD:60:1: Linking of rule '//tensorflow/contrib/android:libtensorflow_inference.so' failed (Exit 1)
external/androidndk/ndk/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin\ld: fatal error: bazel-out/armeabi-v7a-opt/bin/tensorflow/core/kernels/libandroid_tensorflow_kernels.lo: pread failed: Invalid argument
clang.exe: error: linker command failed with exit code 1 (use -v to see invocation)
Target //tensorflow/contrib/android:libtensorflow_inference.so failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 3148.512s, Critical Path: 443.26s
INFO: 1045 processes: 1045 local.
FAILED: Build did NOT complete successfully
I scoured through the internet and found a small modification so I typed:
bazel build -c opt //tensorflow/contrib/android:libtensorflow_inference.so --crosstool_top=//external:android/crosstool --host_crosstool_top=#bazel_tools//tools/cpp:toolchain --cpu=armeabi-v7a --cxxopt=-std=c++11
However this returns an error even before the previous command did:
ERROR: C:/sri/sritrain/tensorflow/tensorflow/contrib/android/BUILD:60:1: Linking of rule '//tensorflow/contrib/android:libtensorflow_inference.so' failed (Exit 1)
external/androidndk/ndk/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin\ld: fatal error: bazel-out/armeabi-v7a-opt/bin/tensorflow/core/kernels/libandroid_tensorflow_kernels.lo: pread failed: Invalid argument
clang.exe: error: linker command failed with exit code 1 (use -v to see invocation)
Target //tensorflow/contrib/android:libtensorflow_inference.so failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 2787.155s, Critical Path: 244.57s
INFO: 795 processes: 795 local.
FAILED: Build did NOT complete successfully
It is supposed to create a .so file on my computer but it doesn't.
I SOLVED IT!
I found the problem was that i was using the ndk-bundle from under Android Studio's folder and it was the latest ndk. I downloaded an older ndk version android_ndk_r15c and ran the command:
bazel build -c opt //tensorflow/contrib/android:libtensorflow_inference.so --crosstool_top=//external:android/crosstool --host_crosstool_top=#bazel_tools//tools/cpp:toolchain --cpu=armeabi-v7a --cxxopt=-std=c++11
So the build was completed successfully!
I am trying to compile tensorflow-lite for Android running the building script, but the building process stops with this error:
/system/bin/linker: No such file or directory
I can understand how the building process works (compilation + linking) but I can not figure out why this '/system/bin/linker' is needed. This linker is not present in the SDK or NDK folder, and it is not present in the folder tree of the host computer (I am using Linux for the building process).
It looks like part of the Android file structure, but the building process should not depend on the final system structure.
The element I am trying to build is 'schema_fbs', which compiles part of the code using flatbuffers (a 3rd party dependency). The complete sentence I am using is:
bazel build \
--cxxopt='--std=c++11' \
--crosstool_top=//external:android/crosstool \
--host_crosstool_top=#bazel_tools//tools/cpp:toolchain \
--cpu=armeabi-v7a \
--verbose_failures \
--subcommands \
//tensorflow/contrib/lite/schema:schema_fbs
It could be caused by a testing case inside the Bazel building script (I have commented all the tests I found), but why is the linker needed? Is there something I have to do to define this 'system' folder in the compilation process?
Notes:
Target OS: Android
Host OS: Ubuntu 16.04
Used NDK: v16b (tested with v17 but it is not compatible)
Full error messages:
INFO: Analysed target //tensorflow/contrib/lite/schema:schema_fbs_srcs (0 packages loaded).
INFO: Found 1 target...
SUBCOMMAND: # //tensorflow/contrib/lite/schema:schema_fbs_srcs [action 'Generating flatbuffer files for schema_fbs_srcs: //tensorflow/contrib/lite/schema:schema_fbs_srcs']
(cd /home/user/.cache/bazel/_bazel_user/73606864f5ec4cce18dd83a6cbcd2bc2/execroot/org_tensorflow && \
exec env - \
LD_LIBRARY_PATH=/usr/local/lib:/home/user/Libraries/llvm-4.0.0.src/build/lib: \
PATH=/home/user/Software/git-sizer:/home/user/Android/Sdk/platform-tools:/home/user/anaconda3/bin:/home/user/Libraries/llvm-4.0.0.src/build/bin:/home/user/bin:/home/user/repo/caffe/build/install/lib:/home/user/Software/cmake-3.10.3-Linux-x86_64/bin:::::/home/user/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin \
/bin/bash -c 'source external/bazel_tools/tools/genrule/genrule-setup.sh; for f in tensorflow/contrib/lite/schema/schema.fbs; do bazel-out/armeabi-v7a-opt/bin/external/flatbuffers/flatc --no-union-value-namespacing --gen-object-api -c -o bazel-out/armeabi-v7a-opt/genfiles/tensorflow/contrib/lite/schema $f; done')
ERROR: /home/user/Repositories/git/tensorflow/tensorflow/contrib/lite/schema/BUILD:57:1: Generating flatbuffer files for schema_fbs_srcs: //tensorflow/contrib/lite/schema:schema_fbs_srcs failed (Exit 255): bash failed: error executing command
(cd /home/user/.cache/bazel/_bazel_user/73606864f5ec4cce18dd83a6cbcd2bc2/execroot/org_tensorflow && \
exec env - \
LD_LIBRARY_PATH=/usr/local/lib:/home/user/Libraries/llvm-4.0.0.src/build/lib: \
PATH=/home/user/Software/git-sizer:/home/user/Android/Sdk/platform-tools:/home/user/anaconda3/bin:/home/user/Libraries/llvm-4.0.0.src/build/bin:/home/user/bin:/home/user/repo/caffe/build/install/lib:/home/user/Software/cmake-3.10.3-Linux-x86_64/bin:::::/home/user/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin \
/bin/bash -c 'source external/bazel_tools/tools/genrule/genrule-setup.sh; for f in tensorflow/contrib/lite/schema/schema.fbs; do bazel-out/armeabi-v7a-opt/bin/external/flatbuffers/flatc --no-union-value-namespacing --gen-object-api -c -o bazel-out/armeabi-v7a-opt/genfiles/tensorflow/contrib/lite/schema $f; done')
/system/bin/linker: No such file or directory
Target //tensorflow/contrib/lite/schema:schema_fbs_srcs failed to build
INFO: Elapsed time: 0.572s, Critical Path: 0.02s
INFO: 0 processes.
FAILED: Build did NOT complete successfully
The TensorFlow Lite AAR can also be built using:
bazel build --cxxopt='--std=c++11' -c opt --fat_apk_cpu=x86,x86_64,arm64-v8a,armeabi-v7a tensorflow/contrib/lite/java:tensorflow-lite
And you must make sure you ran ./configure and let it configure SDK and NDK for your.
I am trying to build Android app using the facebook's buck build system but I am getting Build Failed error.
I am getting this same error on both MAC as well as WINDOWS
buckconfig file
[cache]
mode = dir
[cxx]
default_platform = iphonesimulator-x86_64
cflags = -g -std=c11
cxxflags = -g -std=c++14
# Many ARC APIs don't compile with separate preprocess and compile.
combined_preprocess_and_compile = true
[alias]
demo_app_android = //android:demo-app
demo_app_ios = //ios:BuckDemoApp
[apple]
xctool_zip_target = //third-party/ios/xctool:xctool-minimal-zip
[ndk]
ndk_version = 16.1.4479499
app_platform = android-26
[android]
build_tools_version = 26.0.2
target = Google Inc.:Google APIs:26
[java]
src_roots = /android/java/
source_level = 7
target_level = 7
[project]
default_android_manifest = //android/AndroidManifest.xml
ignore = .buckd, \
.hg, \
.git, \
.idea, \
buck-cache, \
buck-out, \
Environment Variables:
$ env | grep ANDROID_
ANDROID_HOME=<path>/Android/sdk
ANDROID_NDK_REPOSITORY=<path>/Android/sdk/ndk-bundle
ANDROID_SDK=<path>/Library/Android/sdk
Error Stack Trace:
Building: finished in 0.0 sec
Total time: 0.0 sec
BUILD FAILED: No native platforms detected. Probably Android NDK is not configured properly.
I am not getting that what I am missing. Appreciate your help.
Thanks
Try older version of NDK (15 or older) and make sure /Android/sdk/ndk-bundle contains a directory with that version so it looks like /Android/sdk/ndk-bundle/android-ndk-r15.
I had the same error when trying to build the sample Android APP. I tried several things to clearly understand the behaviour of the .buckconfig settings and environment variables.
Set ndk_path
you can set ndk path directly. I've set this variable instead of setting ndk_version(such as 'r10e') Keep in mind that
The environment variables ANDROID_NDK and NDK_HOME both supercede
the buckconfig setting.
Create a repo folder (recommended)
Download ndk bundle and unzip it into a folder that serves as a repository folder and set it to ANDROID_NDK_REPOSITORY env var. So the path can be used to get the correct ndk version. In this case only thing you have to do is setting the version of NDK bundle and buck will automatically get the right version for you.
Currently there is error with Buck with the Java9.
Just to add more.
If you are using java9 then downgrade to java8 as buck uses the sun/misc/BASE64Encoder which is removed in java9.
To keep versions of java8 and java9 in mac. I have implemented below command lines in .bash_profile.
export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA_9_HOME=$(/usr/libexec/java_home -v9)
alias java8='export JAVA_HOME=$JAVA_8_HOME'
alias java9='export JAVA_HOME=$JAVA_9_HOME'
#default java8
export JAVA_HOME=$JAVA_8_HOME
There are some questions about this issue. But the answers are not helpful. I try to generate D8 for android. The v8 guide says that gyp is deprecated. My steps are here :
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
export PATH='pwd'/depot_tools:"$PATH"
Download ndk-r14b
fetch v8
cd v8
echo "target_os = ['android']" >> ../.gclient && gclient sync --nohooks
tools/dev/v8gen.py arm.release
gn gen out.gn/arm.release --args='android_ndk_root="/path/android-ndk-r14b" android_ndk_version="r14" v8_static_library=true android_sdk_root="/path/android-sdk-r25" target_os = "android" target_cpu = "arm" v8_target_cpu = "arm" is_component_build = false'
ninja -C out.gn/arm.release d8
Compilation throws some errors.
In file included from ../../third_party/icu/source/common/unicode/unistr.h:33:0,
from ../../third_party/icu/source/common/unicode/locid.h:36,
from ../../third_party/icu/source/i18n/unicode/dcfmtsym.h:36,
from ../../third_party/icu/source/i18n/unicode/decimfmt.h:38,
from ../../third_party/icu/source/i18n/digitlst.h:32,
from ../../third_party/icu/source/i18n/visibledigits.cpp:18:
../../third_party/icu/source/common/unicode/std_string.h:35:18: fatal error: string: No such file or directory
#include <string>
^
compilation terminated.
[6/939] CC obj/third_party/icu/icui18n/utmscale.o
ninja: build stopped: subcommand failed.
How can I fix?
You forgot
android_ndk_major_version=14
when i run the tensorflow android demo.
i have installed the bazel for a long time to build the environment.
and then when all tool is done.then run the demo in Android Studio.
the gradle console show me this:
the error image
and the error occured when the task of buildNative executed.
the full command line display in the gradle console:
Starting process 'command '/usr/local/bin/bazel''. Working directory: /Users/colarking/Workspace/tensorflow Command: /usr/local/bin/bazel build -c opt tensorflow/examples/android:tensorflow_native_libs --crosstool_top=//external:android/crosstool --cpu=armeabi-v7a --verbose_failures --host_crosstool_top=#bazel_tools//tools/cpp:toolchain
Successfully started process 'command '/usr/local/bin/bazel''
Did you build native libs with these commands?
CPU=armeabi-v7a
bazel build //tensorflow/examples/android:tensorflow_native_libs --crosstool_top=//external:android/crosstool --cpu=$CPU --host_crosstool_top=#bazel_tools//tools/cpp:toolchain
NATIVE_FOLDER=tensorflow/examples/android/libs/$CPU
mkdir -p $NATIVE_FOLDER
cp bazel-bin/tensorflow/examples/android/libtensorflow_demo.so $NATIVE_FOLDER
Source: https://github.com/tensorflow/tensorflow/issues/3444
Tensorflow cannot see the path to the NDK. You need to update the WORKSPACE file in the projects root folder with the NDK and sdk paths. you also need to install the correct NDK for your machine. Follow the instructions here