1#!/bin/bash
2#
3# android_setup.sh: Sets environment variables used by other Android scripts.
4
5# Fail-fast if anything in the script fails.
6set -e
7
8BUILDTYPE=${BUILDTYPE-Debug}
9
10while (( "$#" )); do
11  if [[ "$1" == "-d" ]]; then
12    DEVICE_ID=$2
13    shift
14  elif [[ "$1" == "-i" || "$1" == "--resourcePath" ]]; then
15    RESOURCE_PATH=$2
16    APP_ARGS=("${APP_ARGS[@]}" "${1}" "${2}")
17    shift
18  elif [[ "$1" == "-s" ]]; then
19    DEVICE_SERIAL="-s $2"
20    shift
21  elif [[ "$1" == "-t" ]]; then
22    BUILDTYPE=$2
23    shift
24  elif [[ "$1" == "--release" ]]; then
25    BUILDTYPE=Release
26  else
27    APP_ARGS=("${APP_ARGS[@]}" "${1}")
28  fi
29  shift
30done
31
32function verbose {
33  if [[ -n $VERBOSE ]]; then
34    echo $@
35  fi
36}
37
38function exportVar {
39  NAME=$1
40  VALUE=$2
41  verbose export $NAME=\"$VALUE\"
42  export $NAME="$VALUE"
43}
44
45function absPath {
46  (cd $1; pwd)
47}
48
49SCRIPT_DIR=$(absPath "$(dirname "$BASH_SOURCE[0]}")")
50
51if [ -z "$ANDROID_SDK_ROOT" ]; then
52  if ANDROID_SDK_ROOT="$(dirname $(which android))/.."; then
53    exportVar ANDROID_SDK_ROOT $ANDROID_SDK_ROOT
54  else
55     echo "No ANDROID_SDK_ROOT set and can't auto detect it from location of android binary."
56     exit 1
57  fi
58fi
59
60# check to see that gclient sync ran successfully
61THIRD_PARTY_EXTERNAL_DIR=${SCRIPT_DIR}/../third_party/externals
62if [ ! -d "$THIRD_PARTY_EXTERNAL_DIR" ]; then
63	echo ""
64	echo "ERROR: Unable to find the required third_party dependencies needed to build."
65	echo "       To fix this add the following line to your .gclient file and run 'gclient sync'"
66	echo "        target_os = ['android']"
67	echo ""
68	exit 1;
69fi
70
71# Helper function to configure the GYP defines to the appropriate values
72# based on the target device.
73setup_device() {
74  DEFINES="OS=android"
75  DEFINES="${DEFINES} host_os=$(uname -s | sed -e 's/Linux/linux/;s/Darwin/mac/')"
76  DEFINES="${DEFINES} skia_os=android"
77  DEFINES="${DEFINES} android_base=$(absPath ${SCRIPT_DIR}/..)"
78  if [[ "$GYP_DEFINES" != *skia_shared_lib=* ]]; then
79      DEFINES="${DEFINES} skia_shared_lib=1"
80  fi
81
82  # Setup the build variation depending on the target device
83  TARGET_DEVICE="$1"
84
85  if [ -z "$TARGET_DEVICE" ]; then
86    if [ -f .android_config ]; then
87      TARGET_DEVICE=$(cat .android_config)
88      verbose "no target device (-d), using ${TARGET_DEVICE} from most recent build"
89    else
90      TARGET_DEVICE="arm_v7_thumb"
91      verbose "no target device (-d), using ${TARGET_DEVICE}"
92    fi
93  fi
94
95  case $TARGET_DEVICE in
96    nexus_s)
97      DEFINES="${DEFINES} skia_arch_type=arm arm_neon=1 arm_version=7 arm_thumb=1"
98      DEFINES="${DEFINES} skia_resource_cache_mb_limit=24"
99      ANDROID_ARCH="arm"
100      ;;
101    nexus_4 | nexus_7 | nexus_10)
102      DEFINES="${DEFINES} skia_arch_type=arm arm_neon=1 arm_version=7 arm_thumb=1"
103      ANDROID_ARCH="arm"
104      ;;
105    xoom)
106      DEFINES="${DEFINES} skia_arch_type=arm arm_neon=0 arm_version=7 arm_thumb=1"
107      ANDROID_ARCH="arm"
108      ;;
109    galaxy_nexus)
110      DEFINES="${DEFINES} skia_arch_type=arm arm_neon=1 arm_version=7 arm_thumb=1"
111      DEFINES="${DEFINES} skia_resource_cache_mb_limit=32"
112      ANDROID_ARCH="arm"
113      ;;
114    intel_rhb | razr_i)
115      DEFINES="${DEFINES} skia_arch_type=x86 skia_arch_width=32"
116      DEFINES="${DEFINES} skia_resource_cache_mb_limit=32"
117      ANDROID_ARCH="x86"
118      ;;
119    x86)
120      DEFINES="${DEFINES} skia_arch_type=x86 skia_arch_width=32"
121      ANDROID_ARCH="x86"
122      ;;
123    x86_64 | x64)
124      DEFINES="${DEFINES} skia_arch_type=x86 skia_arch_width=64"
125      ANDROID_ARCH="x86_64"
126      ;;
127    arm_v7)
128      DEFINES="${DEFINES} skia_arch_type=arm arm_neon_optional=1 arm_version=7 arm_thumb=0"
129      ANDROID_ARCH="arm"
130      ;;
131    arm_v7_thumb | nvidia_logan | nexus_5)
132      DEFINES="${DEFINES} skia_arch_type=arm arm_neon_optional=1 arm_version=7 arm_thumb=1"
133      ANDROID_ARCH="arm"
134      ;;
135    arm)
136      DEFINES="${DEFINES} skia_arch_type=arm arm_neon=0 arm_thumb=0"
137      ANDROID_ARCH="arm"
138      ;;
139    arm_thumb)
140      DEFINES="${DEFINES} skia_arch_type=arm arm_neon=0 arm_thumb=1"
141      ANDROID_ARCH="arm"
142      ;;
143    arm64)
144      DEFINES="${DEFINES} skia_arch_type=arm64 skia_arch_width=64"
145      ANDROID_ARCH="arm64"
146      ;;
147    mips)
148      DEFINES="${DEFINES} skia_arch_type=mips skia_arch_width=32"
149      DEFINES="${DEFINES} skia_resource_cache_mb_limit=32"
150      ANDROID_ARCH="mips"
151      ;;
152    mips_dsp2)
153      DEFINES="${DEFINES} skia_arch_type=mips skia_arch_width=32"
154      DEFINES="${DEFINES} mips_arch_variant=mips32r2 mips_dsp=2"
155      ANDROID_ARCH="mips"
156      ;;
157    mips64)
158      DEFINES="${DEFINES} skia_arch_type=mips skia_arch_width=64"
159      ANDROID_ARCH="mips64"
160      ;;
161    *)
162      if [ -z "$ANDROID_IGNORE_UNKNOWN_DEVICE" ]; then
163          echo "ERROR: unknown device $TARGET_DEVICE"
164          exit 1
165      fi
166      # If ANDROID_IGNORE_UNKNOWN_DEVICE is set, then ANDROID_TOOLCHAIN
167      # or ANDROID_ARCH should be set; Otherwise, ANDROID_ARCH
168      # defaults to 'arm' and the default ARM toolchain is used.
169      DEFINES="${DEFINES} skia_arch_type=${ANDROID_ARCH-arm}"
170      # If ANDROID_IGNORE_UNKNOWN_DEVICE is set, extra gyp defines can be
171      # added via ANDROID_GYP_DEFINES
172      DEFINES="${DEFINES} ${ANDROID_GYP_DEFINES}"
173      ;;
174  esac
175
176  verbose "The build is targeting the device: $TARGET_DEVICE"
177  exportVar DEVICE_ID $TARGET_DEVICE
178
179  # setup the appropriate cross compiling toolchains
180  source $SCRIPT_DIR/utils/setup_toolchain.sh
181
182  DEFINES="${DEFINES} android_toolchain=${TOOLCHAIN_TYPE}"
183  exportVar GYP_DEFINES "$DEFINES $GYP_DEFINES"
184
185  SKIA_SRC_DIR=$(cd "${SCRIPT_DIR}/../../.."; pwd)
186  DEFAULT_SKIA_OUT="${SKIA_SRC_DIR}/out/config/android-${TARGET_DEVICE}"
187  exportVar SKIA_OUT "${SKIA_OUT:-${DEFAULT_SKIA_OUT}}"
188}
189
190# adb_pull_if_needed(android_src, host_dst)
191adb_pull_if_needed() {
192
193  # get adb location
194  source $SCRIPT_DIR/utils/setup_adb.sh
195
196  # read input params
197  ANDROID_SRC="$1"
198  HOST_DST="$2"
199
200  if [ -f $HOST_DST ];
201  then
202    #get the MD5 for dst and src
203    ANDROID_MD5=`$ADB $DEVICE_SERIAL shell md5 $ANDROID_SRC`
204    if [ $(uname) == "Darwin" ]; then
205      HOST_MD5=`md5 -q $HOST_DST`
206    else
207      HOST_MD5=`md5sum $HOST_DST`
208    fi
209
210    if [ "${ANDROID_MD5:0:32}" != "${HOST_MD5:0:32}" ]; then
211      echo -n "$HOST_DST "
212      $ADB $DEVICE_SERIAL pull $ANDROID_SRC $HOST_DST
213    fi
214  else
215    echo -n "$HOST_DST "
216    $ADB $DEVICE_SERIAL pull $ANDROID_SRC $HOST_DST
217  fi
218}
219
220# adb_push_if_needed(host_src, android_dst)
221adb_push_if_needed() {
222
223  # get adb location
224  source $SCRIPT_DIR/utils/setup_adb.sh
225
226  # read input params
227  local HOST_SRC="$1"
228  local ANDROID_DST="$2"
229
230  ANDROID_LS=`$ADB $DEVICE_SERIAL shell ls -ld $ANDROID_DST`
231  HOST_LS=`ls -ld $HOST_SRC`
232  if [ "${ANDROID_LS:0:1}" == "d" -a "${HOST_LS:0:1}" == "-" ];
233  then
234    ANDROID_DST="${ANDROID_DST}/$(basename ${HOST_SRC})"
235  fi
236
237
238  ANDROID_LS=`$ADB $DEVICE_SERIAL shell ls -ld $ANDROID_DST`
239  if [ "${ANDROID_LS:0:1}" == "-" ]; then
240    #get the MD5 for dst and src
241    ANDROID_MD5=`$ADB $DEVICE_SERIAL shell md5 $ANDROID_DST`
242    if [ $(uname) == "Darwin" ]; then
243      HOST_MD5=`md5 -q $HOST_SRC`
244    else
245      HOST_MD5=`md5sum $HOST_SRC`
246    fi
247
248    if [ "${ANDROID_MD5:0:32}" != "${HOST_MD5:0:32}" ]; then
249      echo -n "$ANDROID_DST "
250      $ADB $DEVICE_SERIAL push $HOST_SRC $ANDROID_DST
251    fi
252  elif [ "${ANDROID_LS:0:1}" == "d" ]; then
253    for FILE_ITEM in `ls $HOST_SRC`; do
254      adb_push_if_needed "${HOST_SRC}/${FILE_ITEM}" "${ANDROID_DST}/${FILE_ITEM}"
255    done
256  else
257    HOST_LS=`ls -ld $HOST_SRC`
258    if [ "${HOST_LS:0:1}" == "d" ]; then
259      $ADB $DEVICE_SERIAL shell mkdir -p $ANDROID_DST
260      adb_push_if_needed $HOST_SRC $ANDROID_DST
261    else
262      echo -n "$ANDROID_DST "
263      $ADB $DEVICE_SERIAL shell mkdir -p "$(dirname "$ANDROID_DST")"
264      $ADB $DEVICE_SERIAL push $HOST_SRC $ANDROID_DST
265    fi
266  fi
267}
268
269setup_device "${DEVICE_ID}"
270