blob: e49c6da609120ffa9a13ee33c169f590b67c5dee [file] [log] [blame]
Kojo Acquah2b774d92021-10-05 17:08:19 -07001#!/bin/bash
2# Copyright 2021 The IREE Authors
3#
4# Licensed under the Apache License v2.0 with LLVM Exceptions.
5# See https://llvm.org/LICENSE.txt for license information.
6# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7
8# Cross-compile IREE's TFLite Java Bindings with Gradle and CMake. Produces an
9# iree-tflite-bindings-debug.aar and an iree-tflite-bindings-release.aar
10# library under bindings/tflite/java/output. Designed for CI, but can be run
11# manually.
12
13set -x
14set -e
15set -o pipefail
16
17# --------------------------------------------------------------------------- #
18ROOT_DIR=$(git rev-parse --show-toplevel)
19
20CMAKE_BIN=${CMAKE_BIN:-$(which cmake)}
21
22"${CMAKE_BIN?}" --version
23ninja --version
24gradle --version
25
26cd ${ROOT_DIR?}
27
28# --------------------------------------------------------------------------- #
29# Build the host libraries
30
31HOST_BUILD_DIR=${ROOT_DIR?}/build-host
32HOST_INSTALL_DIR=${HOST_BUILD_DIR}/install
33
34cmake -G Ninja -B ${HOST_BUILD_DIR} \
35 -DIREE_BUILD_COMPILER=OFF \
36 -DIREE_BUILD_TESTS=OFF \
37 -DIREE_BUILD_SAMPLES=OFF \
38 -DCMAKE_C_COMPILER=clang \
39 -DCMAKE_CXX_COMPILER=clang++ \
40 -DCMAKE_INSTALL_PREFIX=${HOST_INSTALL_DIR} .
41
42cmake --build ${HOST_BUILD_DIR} --target install
43
44# --------------------------------------------------------------------------- #
45# Build native libraries
46
47ANDROID_BUILD_DIR=${ROOT_DIR?}/build-android
48
49# Todo: Support multiple ABIs. For now we build a singe abi: arm64.
50ANDROID_ABI=arm64-v8a
51
52cmake -G Ninja -B ${ANDROID_BUILD_DIR} \
53 -DCMAKE_TOOLCHAIN_FILE="${ANDROID_NDK}/build/cmake/android.toolchain.cmake" \
54 -DIREE_HOST_BINARY_ROOT="${HOST_INSTALL_DIR}" \
55 -DANDROID_ABI="${ANDROID_ABI}" \
56 -DANDROID_PLATFORM="android-29" \
57 -DIREE_BUILD_COMPILER=OFF \
58 -DIREE_BUILD_BINDINGS_TFLITE=ON \
59 -DIREE_BUILD_BINDINGS_TFLITE_JAVA=ON \
60 -DIREE_BUILD_TESTS=OFF \
61 -DIREE_BUILD_SAMPLES=OFF \
62 -DIREE_BUILD_PYTHON_BINDINGS=OFF .
63
64cmake --build ${ANDROID_BUILD_DIR} --target iree-tflite-bindings
65
66NATIVE_LIBRARY=${ANDROID_BUILD_DIR}/bindings/tflite/java/org/tensorflow/lite/native/libiree-tflite-bindings.so
67
68# --------------------------------------------------------------------------- #
69# Setup the gradle build with native libraries
70
71BINDINGS_DIR=${ROOT_DIR?}/bindings/tflite/java
72cd ${BINDINGS_DIR}
73
74# Copy the native library(s) to the jniLibs folder
75mkdir -p jniLibs/${ANDROID_ABI}
76cp ${NATIVE_LIBRARY} jniLibs/${ANDROID_ABI}
77
78# --------------------------------------------------------------------------- #
79# Build the Android library
80
81gradle wrapper
82
83# Note: since we're providing the native libraries, we omit the tasks that
84# generate them from the build.gradle.
85./gradlew build \
86 -x externalNativeBuildDebug \
87 -x externalNativeBuildCleanDebug \
88 -x externalNativeBuildRelease \
89 -x externalNativeBuildCleanRelease \
90 -x cmakeConfigureHost \
91 -x cmakeBuildHost
92
93echo "Android Library Artifacts:"
94ls build/outputs/aar/