michael@0: # Copyright (c) 2012 Google Inc. michael@0: # All rights reserved. michael@0: # michael@0: # Redistribution and use in source and binary forms, with or without michael@0: # modification, are permitted provided that the following conditions are michael@0: # met: michael@0: # michael@0: # * Redistributions of source code must retain the above copyright michael@0: # notice, this list of conditions and the following disclaimer. michael@0: # * Redistributions in binary form must reproduce the above michael@0: # copyright notice, this list of conditions and the following disclaimer michael@0: # in the documentation and/or other materials provided with the michael@0: # distribution. michael@0: # * Neither the name of Google Inc. nor the names of its michael@0: # contributors may be used to endorse or promote products derived from michael@0: # this software without specific prior written permission. michael@0: # michael@0: # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: # Collection of common shell functions for 'run-checks.sh' et 'test-shell.sh' michael@0: michael@0: # All internal variables and functions use an underscore as a prefix michael@0: # (e.g. _VERBOSE, _ALL_CLEANUPS, etc..). michael@0: michael@0: # Sanitize the environment michael@0: export LANG=C michael@0: export LC_ALL=C michael@0: michael@0: if [ "$BASH_VERSION" ]; then michael@0: set -o posix michael@0: fi michael@0: michael@0: # Utility functions michael@0: michael@0: _ALL_CLEANUPS= michael@0: michael@0: # Register a function to be called when the script exits, even in case of michael@0: # Ctrl-C, logout, etc. michael@0: # $1: function name. michael@0: atexit () { michael@0: if [ -z "$_ALL_CLEANUPS" ]; then michael@0: _ALL_CLEANUPS=$1 michael@0: # Ensure a clean exit when the script is: michael@0: # - Exiting normally (EXIT) michael@0: # - Interrupted by Ctrl-C (INT) michael@0: # - Interrupted by log out (HUP) michael@0: # - Being asked to quit nicely (TERM) michael@0: # - Being asked to quit and dump core (QUIT) michael@0: trap "_exit_cleanups \$?" EXIT INT HUP QUIT TERM michael@0: else michael@0: _ALL_CLEANUPS="$_ALL_CLEANUPS $1" michael@0: fi michael@0: } michael@0: michael@0: # Called on exit if at least one function was registered with atexit michael@0: # $1: final exit status code michael@0: _exit_cleanups () { michael@0: local CLEANUP CLEANUPS michael@0: # Ignore calls to atexit during cleanups michael@0: CLEANUPS=$_ALL_CLEANUPS michael@0: _ALL_CLEANUPS= michael@0: for CLEANUP in $CLEANUPS; do michael@0: ($CLEANUP) michael@0: done michael@0: exit "$@" michael@0: } michael@0: michael@0: michael@0: michael@0: michael@0: # Dump a panic message then exit. michael@0: # $1+: message michael@0: panic () { michael@0: echo "ERROR: $@" >&2 michael@0: exit 1 michael@0: } michael@0: michael@0: # If the previous command failed, dump a panic message then exit. michael@0: # $1+: message. michael@0: fail_panic () { michael@0: if [ $? != 0 ]; then michael@0: panic "$@" michael@0: fi; michael@0: } michael@0: michael@0: _VERBOSE=0 michael@0: michael@0: # Increase verbosity for dump/log/run/run2 functions michael@0: increase_verbosity () { michael@0: _VERBOSE=$(( $_VERBOSE + 1 )) michael@0: } michael@0: michael@0: # Decrease verbosity michael@0: decrease_verbosity () { michael@0: _VERBOSE=$(( $_VERBOSE - 1 )) michael@0: } michael@0: michael@0: # Returns success iff verbosity level is higher than a specific value michael@0: # $1: verbosity level michael@0: verbosity_is_higher_than () { michael@0: [ "$_VERBOSE" -gt "$1" ] michael@0: } michael@0: michael@0: # Returns success iff verbosity level is lower than a specific value michael@0: # $1: verbosity level michael@0: verbosity_is_lower_than () { michael@0: [ "$_VERBOSE" -le "$1" ] michael@0: } michael@0: michael@0: # Dump message to stdout, unless verbosity is < 0, i.e. --quiet was called michael@0: # $1+: message michael@0: dump () { michael@0: if [ "$_VERBOSE" -ge 0 ]; then michael@0: printf "%s\n" "$*" michael@0: fi michael@0: } michael@0: michael@0: # If --verbose was used, dump a message to stdout. michael@0: # $1+: message michael@0: log () { michael@0: if [ "$_VERBOSE" -ge 1 ]; then michael@0: printf "%s\n" "$*" michael@0: fi michael@0: } michael@0: michael@0: _RUN_LOG= michael@0: michael@0: # Set a run log file that can be used to collect the output of commands that michael@0: # are not displayed. michael@0: set_run_log () { michael@0: _RUN_LOG=$1 michael@0: } michael@0: michael@0: # Run a command. Output depends on $_VERBOSE: michael@0: # $_VERBOSE <= 0: Run command, store output into the run log michael@0: # $_VERBOSE >= 1: Dump command, run it, output goest to stdout michael@0: # Note: Ideally, the command's output would go to the run log for $_VERBOSE >= 1 michael@0: # but the 'tee' tool doesn't preserve the status code of its input pipe michael@0: # in case of error. michael@0: run () { michael@0: local LOGILE michael@0: if [ "$_RUN_LOG" ]; then michael@0: LOGFILE=$_RUN_LOG michael@0: else michael@0: LOGFILE=/dev/null michael@0: fi michael@0: michael@0: if [ "$_VERBOSE" -ge 1 ]; then michael@0: echo "COMMAND: $@" michael@0: "$@" michael@0: else michael@0: "$@" >>$LOGFILE 2>&1 michael@0: fi michael@0: } michael@0: michael@0: # Same as run(), but only dump command output for $_VERBOSE >= 2 michael@0: run2 () { michael@0: local LOGILE michael@0: if [ "$_RUN_LOG" ]; then michael@0: LOGFILE=$_RUN_LOG michael@0: else michael@0: LOGFILE=/dev/null michael@0: fi michael@0: michael@0: if [ "$_VERBOSE" -ge 1 ]; then michael@0: echo "COMMAND: $@" michael@0: fi michael@0: if [ "$_VERBOSE" -ge 2 ]; then michael@0: "$@" michael@0: else michael@0: "$@" >>$LOGFILE 2>&1 michael@0: fi michael@0: } michael@0: michael@0: # Extract number of cores to speed up the builds michael@0: # Out: number of CPU cores michael@0: get_core_count () { michael@0: case $(uname -s) in michael@0: Linux) michael@0: grep -c -e '^processor' /proc/cpuinfo michael@0: ;; michael@0: Darwin) michael@0: sysctl -n hw.ncpu michael@0: ;; michael@0: CYGWIN*|*_NT-*) michael@0: echo $NUMBER_OF_PROCESSORS michael@0: ;; michael@0: *) michael@0: echo 1 michael@0: ;; michael@0: esac michael@0: } michael@0: michael@0: michael@0: # Check for the Android ADB program. michael@0: # michael@0: # On success, return nothing, but updates internal variables so later calls to michael@0: # adb_shell, adb_push, etc.. will work. You can get the path to the ADB program michael@0: # with adb_get_program if needed. michael@0: # michael@0: # On failure, returns 1, and updates the internal adb error message, which can michael@0: # be retrieved with adb_get_error. michael@0: # michael@0: # $1: optional ADB program path. michael@0: # Return: success or failure. michael@0: _ADB= michael@0: _ADB_STATUS= michael@0: _ADB_ERROR= michael@0: michael@0: adb_check () { michael@0: # First, try to find the executable in the path, or the SDK install dir. michael@0: _ADB=$1 michael@0: if [ -z "$_ADB" ]; then michael@0: _ADB=$(which adb 2>/dev/null) michael@0: if [ -z "$_ADB" -a "$ANDROID_SDK_ROOT" ]; then michael@0: _ADB=$ANDROID_SDK_ROOT/platform-tools/adb michael@0: if [ ! -f "$_ADB" ]; then michael@0: _ADB= michael@0: fi michael@0: fi michael@0: if [ -z "$_ADB" ]; then michael@0: _ADB_STATUS=1 michael@0: _ADB_ERROR="The Android 'adb' tool is not in your path." michael@0: return 1 michael@0: fi michael@0: fi michael@0: michael@0: log "Found ADB program: $_ADB" michael@0: michael@0: # Check that it works correctly michael@0: local ADB_VERSION michael@0: ADB_VERSION=$("$_ADB" version 2>/dev/null) michael@0: case $ADB_VERSION in michael@0: "Android Debug Bridge "*) # Pass michael@0: log "Found ADB version: $ADB_VERSION" michael@0: ;; michael@0: *) # Fail michael@0: _ADB_ERROR="Your ADB binary reports a bad version ($ADB_VERSION): $_ADB" michael@0: _ADB_STATUS=1 michael@0: return 1 michael@0: esac michael@0: michael@0: _ADB_STATUS=0 michael@0: return 0 michael@0: } michael@0: michael@0: michael@0: # Return the path to the Android ADB program, if correctly detected. michael@0: # On failure, return the empty string. michael@0: # Out: ADB program path (or empty on failure) michael@0: # Return: success or failure. michael@0: adb_get_program () { michael@0: # Return cached value as soon as possible. michael@0: if [ -z "$_ADB_STATUS" ]; then michael@0: adb_check $1 michael@0: fi michael@0: echo "$_ADB" michael@0: return $_ADB_STATUS michael@0: } michael@0: michael@0: # Return the error corresponding to the last ADB function failure. michael@0: adb_get_error () { michael@0: echo "$_ADB_ERROR" michael@0: } michael@0: michael@0: # Check that there is one device connected through ADB. michael@0: # In case of failure, use adb_get_error to know why this failed. michael@0: # $1: Optional adb program path michael@0: # Return: success or failure. michael@0: _ADB_DEVICE= michael@0: _ADB_DEVICE_STATUS= michael@0: adb_check_device () { michael@0: if [ "$_ADB_DEVICE_STATUS" ]; then michael@0: return $_ADB_DEVICE_STATUS michael@0: fi michael@0: michael@0: # Check for ADB. michael@0: if ! adb_check $1; then michael@0: _ADB_DEVICE_STATUS=$_ADB_STATUS michael@0: return 1 michael@0: fi michael@0: michael@0: local ADB_DEVICES NUM_DEVICES FINGERPRINT michael@0: michael@0: # Count the number of connected devices. michael@0: ADB_DEVICES=$("$_ADB" devices 2>/dev/null | awk '$2 == "device" { print $1; }') michael@0: NUM_DEVICES=$(echo "$ADB_DEVICES" | wc -l) michael@0: case $NUM_DEVICES in michael@0: 0) michael@0: _ADB_ERROR="No Android device connected. Please connect one to your machine." michael@0: _ADB_DEVICE_STATUS=1 michael@0: return 1 michael@0: ;; michael@0: 1) # Pass michael@0: # Ensure the same device will be called in later adb_shell calls. michael@0: export ANDROID_SERIAL=$ADB_DEVICES michael@0: ;; michael@0: *) # 2 or more devices. michael@0: if [ "$ANDROID_SERIAL" ]; then michael@0: ADB_DEVICES=$ANDROID_SERIAL michael@0: NUM_DEVICES=1 michael@0: else michael@0: _ADB_ERROR="More than one Android device connected. \ michael@0: Please define ANDROID_SERIAL in your environment" michael@0: _ADB_DEVICE_STATUS=1 michael@0: return 1 michael@0: fi michael@0: ;; michael@0: esac michael@0: michael@0: _ADB_DEVICE_STATUS=0 michael@0: _ADB_DEVICE=$ADB_DEVICES michael@0: michael@0: FINGERPRINT=$(adb_shell getprop ro.build.fingerprint) michael@0: log "Using ADB device: $ANDROID_SERIAL ($FINGERPRINT)" michael@0: return 0 michael@0: } michael@0: michael@0: # The 'adb shell' command is pretty hopeless, try to make sense of it by: michael@0: # 1/ Removing trailing \r from line endings. michael@0: # 2/ Ensuring the function returns the command's status code. michael@0: # michael@0: # $1+: Command michael@0: # Out: command output (stdout + stderr combined) michael@0: # Return: command exit status michael@0: adb_shell () { michael@0: local RET ADB_LOG michael@0: # Check for ADB device. michael@0: adb_check_device || return 1 michael@0: ADB_LOG=$(mktemp "${TMPDIR:-/tmp}/adb-XXXXXXXX") michael@0: "$_ADB" shell "$@" ";" echo \$? > "$ADB_LOG" 2>&1 michael@0: sed -i -e 's![[:cntrl:]]!!g' "$ADB_LOG" # Remove \r. michael@0: RET=$(sed -e '$!d' "$ADB_LOG") # Last line contains status code. michael@0: sed -e '$d' "$ADB_LOG" # Print everything except last line. michael@0: rm -f "$ADB_LOG" michael@0: return $RET michael@0: } michael@0: michael@0: # Push a file to a device. michael@0: # $1: source file path michael@0: # $2: device target file path michael@0: # Return: success or failure. michael@0: adb_push () { michael@0: adb_check_device || return 1 michael@0: run "$_ADB" push "$1" "$2" michael@0: } michael@0: michael@0: # Pull a file from a device michael@0: # $1: device file path michael@0: # $2: target host file path michael@0: # Return: success or failure. michael@0: adb_pull () { michael@0: adb_check_device || return 1 michael@0: run "$_ADB" pull "$1" "$2" michael@0: } michael@0: michael@0: # Same as adb_push, but will panic if the operations didn't succeed. michael@0: adb_install () { michael@0: adb_push "$@" michael@0: fail_panic "Failed to install $1 to the Android device at $2" michael@0: } michael@0: