ksh.max
ksh.max

Reputation: 439

What is a proper way to build C++ target with android ndk toolchain using bazel

I have a single .cpp code with tflite model inference. This source code file is a part of bazel workspace. I want to build it with default desktop compiler and with arm64 custom toolchain from ndk, then run it on PC and mobile and compare result. How can I specify a custom compiler from ndk toolchain (someandroidndkpath/toolchains/arm64/bin/clang) ?

I have a simple target into my BUILD file:

cc_binary(
    name = "Evaluation",
    srcs = ["evaluation.cpp"],
    visibility = ["//visibility:public"],
    deps = [
        "@org_tensorflow//tensorflow/lite:framework",
        "@org_tensorflow//tensorflow/lite/kernels:builtin_ops",
    ],
)

Edit: Thanks to @ahumesky, it works. I would like to clarify how to configure android_ndk_repository rule.

  1. Go to tf repo tensorflow/third_party/android and put those files to your project.

  2. Set path and version of android sdk into android_configure.bzl. (It is the simplest way but you can do it with .bazelrc variables)

Example:

_ANDROID_NDK_HOME = "~/Android/Sdk/ndk/20.1.5948944/"
_ANDROID_SDK_HOME = "~/Android/Sdk/"
_ANDROID_NDK_API_VERSION = "29"
_ANDROID_SDK_API_VERSION = "29"
_ANDROID_BUILD_TOOLS_VERSION = "29.0.2"
  1. Add to WORKSPACE.

Example:

load("//third_party/android:android_configure.bzl", "android_configure")
android_configure(name = "local_config_android")
load("@local_config_android//:android.bzl", "android_workspace")
android_workspace()
  1. Build target with following flags, push it to android device and run.

Upvotes: 2

Views: 1249

Answers (1)

ahumesky
ahumesky

Reputation: 5016

First make sure that you have an android_ndk_repository rule set up in your WORKSPACE file (either one you set up manually, or through TensorFlow's configure script), then try these bazel flags:

--crosstool_top=//external:android/crosstool
--host_crosstool_top=@bazel_tools//tools/cpp:toolchain
--cpu=arm64-v8a

With the Android crosstool, --cpu can be one of arm64-v8a, armeabi-v7a, x86, or x86_64

Note that these flags are needed only if you're building a cc_binary. If you're building an android_binary, cc_library rules in the dependencies of that android_binary will automatically use the Android crosstool.

Upvotes: 3

Related Questions