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