blob: cdf48161a54662fc55f1cb71934072367cc2d3a2 [file] [log] [blame]
Don Turnerfda67012018-01-19 18:10:25 +00001# Copyright 2018 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15################################################
16# Script to build and run the Oboe tests on an attached Android device or emulator
17#
18# Prerequisites:
Don Turnerf9d26052019-03-27 16:05:50 +000019# - CMake on PATH. This is usually found in $ANDROID_HOME/cmake/<version>/bin.
Don Turnerfda67012018-01-19 18:10:25 +000020# - ANDROID_NDK environment variable is set to your Android NDK location
Don Turnerd8854b72019-03-26 18:00:14 +000021# e.g. $HOME/Library/Android/sdk/ndk-bundle
Don Turnerfda67012018-01-19 18:10:25 +000022# - Android device or emulator attached and accessible via adb
23#
Don Turnerd5a82b72018-07-12 18:09:15 +010024# Instructions:
Don Turnerae0a4772018-09-20 18:46:44 +010025# - Run this script
26# - Check the test results on your target device
Don Turnerfda67012018-01-19 18:10:25 +000027#
Don Turnerae0a4772018-09-20 18:46:44 +010028# What does the script do?
29# - Builds a test binary for the target architecture
30# - Copies the test binary into the UnitTestRunner app
31# - Builds, installs and runs the app on the target device
Don Turnerfda67012018-01-19 18:10:25 +000032#
Don Turnerae0a4772018-09-20 18:46:44 +010033# The initial run may take some time as GTest is built, subsequent runs should be much faster.
34#
35# If you want to perform a clean build just delete the 'build' folder and re-run this script. You will need to do
36# this if you change target architectures (e.g. when changing between real device and emulator)
37#
38# Why is running the tests so convoluted?
39# The tests require the RECORDING permission and on many devices (e.g Samsung) the adb user does not have this
40# permission (and `run-as` is broken). This means that the test binary must be executed by an app which has this
41# permission, hence the need for the UnitTestRunner app.
Don Turnerfda67012018-01-19 18:10:25 +000042#
43################################################
44
45#!/bin/bash
46
47# Directories, paths and filenames
48BUILD_DIR=build
49CMAKE=cmake
50TEST_BINARY_FILENAME=testOboe
Don Turnerae0a4772018-09-20 18:46:44 +010051TEST_RUNNER_DIR=UnitTestRunner
52TEST_RUNNER_PACKAGE_NAME=com.google.oboe.tests.unittestrunner
53TEST_RUNNER_ASSET_DIR=${TEST_RUNNER_DIR}/app/src/main/assets
Don Turnerfda67012018-01-19 18:10:25 +000054
55# Check prerequisites
56if [ -z "$ANDROID_NDK" ]; then
57 echo "Please set ANDROID_NDK to the Android NDK folder"
58 exit 1
59fi
60
61if [ ! $(type -P ${CMAKE}) ]; then
Don Turnerf9d26052019-03-27 16:05:50 +000062 echo "${CMAKE} was not found on your path. You can install it using Android Studio using Tools->Android->SDK Manager->SDK Tools."
63 echo "Once done you will need to add ${HOME}/Library/Android/sdk/cmake/<current_version>/bin to your path."
64 exit 1
Don Turnerfda67012018-01-19 18:10:25 +000065fi
66
67# Get the device ABI
68ABI=$(adb shell getprop ro.product.cpu.abi | tr -d '\n\r')
69
70if [ -z "$ABI" ]; then
71 echo "No device ABI was set. Please ensure a device or emulator is running"
72 exit 1
73fi
74
75echo "Device/emulator architecture is $ABI"
76
77if [ ${ABI} == "arm64-v8a" ] || [ ${ABI} == "x86_64" ]; then
78 PLATFORM=android-21
79elif [ ${ABI} == "armeabi-v7a" ] || [ ${ABI} == "x86" ]; then
80 PLATFORM=android-16
81else
82 echo "Unrecognised ABI: ${ABI}. Supported ABIs are: arm64-v8a, armeabi-v7a, x86_64, x86. If you feel ${ABI} should be supported please file an issue on github.com/google/oboe"
83 exit 1
84fi
85
86# Configure the build
87echo "Building tests for ${ABI} using ${PLATFORM}"
88
89CMAKE_ARGS="-H. \
90 -B${BUILD_DIR} \
91 -DANDROID_ABI=${ABI} \
92 -DANDROID_PLATFORM=${PLATFORM} \
93 -DCMAKE_BUILD_TYPE=RelWithDebInfo \
94 -DCMAKE_CXX_FLAGS=-std=c++11 \
95 -DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake \
96 -DCMAKE_VERBOSE_MAKEFILE=1"
97
98mkdir -p ${BUILD_DIR}
99
100cmake ${CMAKE_ARGS}
101
102# Perform the build
103pushd ${BUILD_DIR}
104 make -j5
105
106 if [ $? -eq 0 ]; then
107 echo "Tests built successfully"
108 else
109 echo "Building tests FAILED"
110 exit 1
111 fi
112
113popd
114
Don Turnerae0a4772018-09-20 18:46:44 +0100115# Copy the binary into the unit test runner app
116mkdir ${TEST_RUNNER_ASSET_DIR}/${ABI}
117DESTINATION_DIR=${TEST_RUNNER_ASSET_DIR}/${ABI}/${TEST_BINARY_FILENAME}
118echo "Copying binary to ${DESTINATION_DIR}"
119cp ${BUILD_DIR}/${TEST_BINARY_FILENAME} ${DESTINATION_DIR}
Don Turnerfda67012018-01-19 18:10:25 +0000120
Don Turnerae0a4772018-09-20 18:46:44 +0100121# Build and install the unit test runner app
122pushd ${TEST_RUNNER_DIR}
123 echo "Building test runner app"
124 ./gradlew assembleDebug
125 echo "Installing to device"
126 ./gradlew installDebug
127popd
Don Turnerff649d02018-07-04 18:35:02 +0100128
Don Turnerae0a4772018-09-20 18:46:44 +0100129echo "Starting app - Check your device for test results"
Don Turner6dd64392018-09-20 19:14:13 +0100130adb shell am start ${TEST_RUNNER_PACKAGE_NAME}/.MainActivity
131
132sleep 1
133adb logcat --pid=`adb shell pidof -s ${TEST_RUNNER_PACKAGE_NAME}`