1#! /bin/bash
2#
3# Copyright (C) 2018 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# Push ART artifacts and its dependencies to a chroot directory for on-device testing.
18
19set -e
20
21if [ -t 1 ]; then
22  # Color sequences if terminal is a tty.
23  red='\033[0;31m'
24  green='\033[0;32m'
25  yellow='\033[0;33m'
26  magenta='\033[0;35m'
27  nc='\033[0m'
28fi
29
30# Setup as root, as some actions performed here require it.
31adb root
32adb wait-for-device
33
34if [[ -z "$ANDROID_BUILD_TOP" ]]; then
35  echo 'ANDROID_BUILD_TOP environment variable is empty; did you forget to run `lunch`?'
36  exit 1
37fi
38
39if [[ -z "$ANDROID_PRODUCT_OUT" ]]; then
40  echo 'ANDROID_PRODUCT_OUT environment variable is empty; did you forget to run `lunch`?'
41  exit 1
42fi
43
44if [[ -z "$ART_TEST_CHROOT" ]]; then
45  echo 'ART_TEST_CHROOT environment variable is empty; please set it before running this script.'
46  exit 1
47fi
48
49
50# Sync relevant product directories
51# ---------------------------------
52
53(
54  cd $ANDROID_PRODUCT_OUT
55  for dir in system/* linkerconfig data; do
56    [ -d $dir ] || continue
57    if [ $dir == system/apex ]; then
58      # We sync the APEXes later.
59      continue
60    fi
61    echo -e "${green}Syncing $dir directory...${nc}"
62    adb shell mkdir -p "$ART_TEST_CHROOT/$dir"
63    adb push $dir "$ART_TEST_CHROOT/$(dirname $dir)"
64  done
65)
66
67# Overwrite the default public.libraries.txt file with a smaller one that
68# contains only the public libraries pushed to the chroot directory.
69adb push "$ANDROID_BUILD_TOP/art/tools/public.libraries.buildbot.txt" \
70  "$ART_TEST_CHROOT/system/etc/public.libraries.txt"
71
72# Create the framework directory if it doesn't exist. Some gtests need it.
73adb shell mkdir -p "$ART_TEST_CHROOT/system/framework"
74
75# APEX packages activation.
76# -------------------------
77
78adb shell mkdir -p "$ART_TEST_CHROOT/apex"
79
80# Manually "activate" the flattened APEX $1 by syncing it to /apex/$2 in the
81# chroot. $2 defaults to $1.
82activate_apex() {
83  local src_apex=${1}
84  local dst_apex=${2:-${src_apex}}
85
86  # Unpack the .apex file in the product directory, but if we already see a
87  # directory we assume buildbot-build.sh has already done it for us and just
88  # use it.
89  src_apex_path=$ANDROID_PRODUCT_OUT/system/apex/${src_apex}
90  if [ ! -d $src_apex_path ]; then
91    echo -e "${green}Extracting APEX ${src_apex}.apex...${nc}"
92    mkdir -p $src_apex_path
93    $ANDROID_HOST_OUT/bin/deapexer --debugfs_path $ANDROID_HOST_OUT/bin/debugfs_static \
94      extract ${src_apex_path}.apex $src_apex_path
95  fi
96
97  echo -e "${green}Activating APEX ${src_apex} as ${dst_apex}...${nc}"
98  adb shell rm -rf "$ART_TEST_CHROOT/apex/${dst_apex}"
99  adb push $src_apex_path "$ART_TEST_CHROOT/apex/${dst_apex}"
100}
101
102# "Activate" the required APEX modules.
103activate_apex com.android.art.testing com.android.art
104activate_apex com.android.i18n
105activate_apex com.android.runtime
106activate_apex com.android.tzdata
107activate_apex com.android.conscrypt
108activate_apex com.android.os.statsd
109