1#! /bin/sh
2
3# Copyright 2018 Google Inc.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7make_gn() {
8    #TODO: make this more configurable
9    cat << EOF
10    target_cpu = "$ARCH"
11    is_debug = false
12    ndk = "$ANDROID_NDK"
13    ndk_api = 26
14EOF
15}
16
17main() {
18    APP="$1"
19    LIB="$2"
20
21    ANDROID_NDK="$(cd "$ANDROID_NDK"; pwd)"
22    ANDROID_HOME="$(cd "$ANDROID_HOME"; pwd)"
23
24    cd "$(dirname "$0")/../../.."
25
26    python tools/git-sync-deps
27    git clean -fxd platform_tools/android/apps/$APP
28    mkdir -p platform_tools/android/apps/${APP}/src/main/assets
29    cp -a resources platform_tools/android/apps/${APP}/src/main/assets/
30
31    for ARCH in arm arm64 x86 x64; do
32        BUILD=out/${APP}-$ARCH
33        mkdir -p "$BUILD"
34        make_gn > "${BUILD}/args.gn"
35        bin/gn gen $BUILD
36        ninja -C $BUILD $LIB
37        case $ARCH in
38            arm)    NATIVE=armeabi-v7a ;;
39            arm64)  NATIVE=arm64-v8a   ;;
40            x86)    NATIVE=x86         ;;
41            x64)    NATIVE=x86_64      ;;
42            *)      usage              ;;
43        esac
44        DST=platform_tools/android/apps/$APP/src/main/libs/$NATIVE
45        mkdir -p $DST
46        cp -a $BUILD/$LIB $DST/$LIB
47    done
48    (
49        cd platform_tools/android
50        apps/gradlew --daemon -p apps/$APP -P suppressNativeBuild :${APP}:assembleUniversalDebug
51    )
52
53    mkdir -p out/${APP}-universal
54    cp platform_tools/android/apps/$APP/build/outputs/apk/${APP}-universal-debug.apk \
55        out/${APP}-universal/$APP-universal-debug.apk
56    ls -l out/${APP}-universal/$APP-universal-debug.apk
57}
58
59usage() {
60    cat >&2 <<EOM
61The environment variables ANDROID_NDK and ANDROID_HOME must be set to the
62locations of the Android NDK and SDK.  Current values:
63
64    ANDROID_NDK="$ANDROID_NDK"
65    ANDROID_HOME="$ANDROID_HOME"
66
67Additionally, \`python\` and \`ninja\` should be in your path.
68
69EOM
70    exit 1
71}
72
73[ -d "$ANDROID_NDK"  ] || usage
74[ -d "$ANDROID_HOME" ] || usage
75command -v ninja  > /dev/null || usage
76command -v python > /dev/null || usage
77
78set -x # Verbose
79set -e # Exit immediately
80
81main viewer libviewer.so
82