• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2###############################################################################
3# Copyright 2015 Google Inc.
4#
5# Use of this source code is governed by a BSD-style license that can be
6# found in the LICENSE file.
7###############################################################################
8#
9# android_setup.sh: Sets environment variables used by other Android scripts.
10
11# Fail-fast if anything in the script fails.
12set -e
13
14IS_DEBUG="false"
15
16while (( "$#" )); do
17  if [[ "$1" == "-C" ]]; then
18    SKIA_OUT=$2
19    shift
20  elif [[ "$1" == "-i" || "$1" == "--resourcePath" ]]; then
21    RESOURCE_PATH=$2
22    APP_ARGS=("${APP_ARGS[@]}" "${1}" "${2}")
23    shift
24  elif [[ "$1" == "-s" ]]; then
25    DEVICE_SERIAL="-s $2"
26    shift
27  elif [[ "$1" == "--logcat" ]]; then
28    LOGCAT=1
29  elif [[ "$1" == "--verbose" ]]; then
30    VERBOSE="true"
31  else
32    APP_ARGS=("${APP_ARGS[@]}" "${1}")
33  fi
34  shift
35done
36
37function verbose {
38  if [[ -n $VERBOSE ]]; then
39    echo $@
40  fi
41}
42
43function exportVar {
44  NAME=$1
45  VALUE=$2
46  verbose export $NAME=\"$VALUE\"
47  export $NAME="$VALUE"
48}
49
50function absPath {
51  (cd $1; pwd)
52}
53
54UTIL_DIR=$(absPath "$(dirname "$BASH_SOURCE[0]}")")
55
56if [ -z "$ANDROID_SDK_ROOT" ]; then
57  if ANDROID_SDK_ROOT="$(dirname $(which android))/.."; then
58    exportVar ANDROID_SDK_ROOT $ANDROID_SDK_ROOT
59  else
60     echo "No ANDROID_SDK_ROOT set and can't auto detect it from location of android binary."
61     exit 1
62  fi
63fi
64
65if [ -z "$ANDROID_NDK_ROOT" ]; then
66  if [ -d "${ANDROID_SDK_ROOT}/ndk-bundle" ]; then
67    exportVar ANDROID_NDK_ROOT ${ANDROID_SDK_ROOT}/ndk-bundle
68  else
69     echo "No ANDROID_NDK_ROOT set and can't auto detect it from location of SDK."
70     exit 1
71  fi
72fi
73
74# adb_pull_if_needed(android_src, host_dst)
75adb_pull_if_needed() {
76
77  # get adb location
78  source $SCRIPT_DIR/utils/setup_adb.sh
79
80  # read input params
81  ANDROID_SRC="$1"
82  HOST_DST="$2"
83
84  if [ -f $HOST_DST ];
85  then
86    #get the MD5 for dst and src depending on OS and/or OS revision
87    ANDROID_MD5_SUPPORT=`$ADB $DEVICE_SERIAL shell ls -ld /system/bin/md5`
88    if [ "${ANDROID_MD5_SUPPORT:0:15}" != "/system/bin/md5" ]; then
89      ANDROID_MD5=`$ADB $DEVICE_SERIAL shell md5 $ANDROID_DST`
90    else
91      ANDROID_MD5=`$ADB $DEVICE_SERIAL shell md5sum $ANDROID_DST`
92    fi
93    if [ $(uname) == "Darwin" ]; then
94      HOST_MD5=`md5 -q $HOST_DST`
95    else
96      HOST_MD5=`md5sum $HOST_DST`
97    fi
98
99    if [ "${ANDROID_MD5:0:32}" != "${HOST_MD5:0:32}" ]; then
100      echo -n "$HOST_DST "
101      $ADB $DEVICE_SERIAL pull $ANDROID_SRC $HOST_DST
102    fi
103  else
104    echo -n "$HOST_DST "
105    $ADB $DEVICE_SERIAL pull $ANDROID_SRC $HOST_DST
106  fi
107}
108
109# adb_push_if_needed(host_src, android_dst)
110adb_push_if_needed() {
111
112  # get adb location
113  source $SCRIPT_DIR/utils/setup_adb.sh
114
115  # read input params
116  local HOST_SRC="$1"
117  local ANDROID_DST="$2"
118
119  # disable crashing on failed commands since newer (N+) versions of Android
120  # return an error when attempting to run ls on a directory or file that does
121  # not exist.
122  set +e
123
124  ANDROID_LS=`$ADB $DEVICE_SERIAL shell ls -ld $ANDROID_DST`
125  HOST_LS=`ls -ld $HOST_SRC`
126  if [ "${ANDROID_LS:0:1}" == "d" -a "${HOST_LS:0:1}" == "-" ];
127  then
128    ANDROID_DST="${ANDROID_DST}/$(basename ${HOST_SRC})"
129  fi
130
131  ANDROID_LS=`$ADB $DEVICE_SERIAL shell ls -ld $ANDROID_DST 2> /dev/null`
132  if [ "${ANDROID_LS:0:1}" == "-" ]; then
133    #get the MD5 for dst and src depending on OS and/or OS revision
134    ANDROID_MD5_SUPPORT=`$ADB $DEVICE_SERIAL shell ls -ld /system/bin/md5 2> /dev/null`
135    if [ "${ANDROID_MD5_SUPPORT:0:1}" == "-" ]; then
136      ANDROID_MD5=`$ADB $DEVICE_SERIAL shell md5 $ANDROID_DST`
137    else
138      ANDROID_MD5=`$ADB $DEVICE_SERIAL shell md5sum $ANDROID_DST`
139    fi
140
141    if [ $(uname) == "Darwin" ]; then
142      HOST_MD5=`md5 -q $HOST_SRC`
143    else
144      HOST_MD5=`md5sum $HOST_SRC`
145    fi
146
147    if [ "${ANDROID_MD5:0:32}" != "${HOST_MD5:0:32}" ]; then
148      echo -n "$ANDROID_DST "
149      $ADB $DEVICE_SERIAL push $HOST_SRC $ANDROID_DST
150    fi
151  elif [ "${ANDROID_LS:0:1}" == "d" ]; then
152    for FILE_ITEM in `ls $HOST_SRC`; do
153      adb_push_if_needed "${HOST_SRC}/${FILE_ITEM}" "${ANDROID_DST}/${FILE_ITEM}"
154    done
155  else
156    HOST_LS=`ls -ld $HOST_SRC`
157    if [ "${HOST_LS:0:1}" == "d" ]; then
158      $ADB $DEVICE_SERIAL shell mkdir -p $ANDROID_DST
159      adb_push_if_needed $HOST_SRC $ANDROID_DST
160    else
161      echo -n "$ANDROID_DST "
162      $ADB $DEVICE_SERIAL shell mkdir -p "$(dirname "$ANDROID_DST")"
163      $ADB $DEVICE_SERIAL push $HOST_SRC $ANDROID_DST
164    fi
165  fi
166
167  # turn error checking back on
168  set -e
169}
170