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