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
21. "$(dirname $0)/buildbot-utils.sh"
22
23if [[ -z "$ART_TEST_ON_VM" ]]; then
24  # Setup as root, as some actions performed here require it.
25  adb root
26  adb wait-for-device
27fi
28
29if [[ -z "$ANDROID_BUILD_TOP" ]]; then
30  msgfatal 'ANDROID_BUILD_TOP environment variable is empty; did you forget to run `lunch`?'
31elif [[ -z "$ANDROID_PRODUCT_OUT" ]]; then
32  msgfatal 'ANDROID_PRODUCT_OUT environment variable is empty; did you forget to run `lunch`?'
33elif [[ -z "$ART_TEST_CHROOT" ]]; then
34  msgfatal 'ART_TEST_CHROOT environment variable is empty; ' \
35      'please set it before running this script.'
36fi
37
38# Sync relevant product directories
39# ---------------------------------
40
41(
42  cd $ANDROID_PRODUCT_OUT
43  for dir in system/* linkerconfig data; do
44    [ -d $dir ] || continue
45    if [ $dir == system/apex ]; then
46      # We sync the APEXes later.
47      continue
48    fi
49    msginfo "Syncing $dir directory..."
50    if [[ -n "$ART_TEST_ON_VM" ]]; then
51      $ART_RSYNC_CMD -R $dir "$ART_TEST_SSH_USER@$ART_TEST_SSH_HOST:$ART_TEST_CHROOT"
52    else
53      adb shell mkdir -p "$ART_TEST_CHROOT/$dir"
54      adb push $dir "$ART_TEST_CHROOT/$(dirname $dir)"
55    fi
56  done
57)
58
59# Overwrite the default public.libraries.txt file with a smaller one that
60# contains only the public libraries pushed to the chroot directory.
61if [[ -n "$ART_TEST_ON_VM" ]]; then
62  $ART_RSYNC_CMD "$ANDROID_BUILD_TOP/art/tools/public.libraries.buildbot.txt" \
63    "$ART_TEST_SSH_USER@$ART_TEST_SSH_HOST:$ART_TEST_CHROOT/system/etc/public.libraries.txt"
64else
65  adb push "$ANDROID_BUILD_TOP/art/tools/public.libraries.buildbot.txt" \
66    "$ART_TEST_CHROOT/system/etc/public.libraries.txt"
67fi
68
69# Create the framework directory if it doesn't exist. Some gtests need it.
70if [[ -n "$ART_TEST_ON_VM" ]]; then
71  $ART_SSH_CMD "$ART_CHROOT_CMD mkdir -p $ART_TEST_CHROOT/system/framework"
72else
73  adb shell mkdir -p "$ART_TEST_CHROOT/system/framework"
74fi
75
76# APEX packages activation.
77# -------------------------
78
79if [[ -n "$ART_TEST_ON_VM" ]]; then
80  $ART_SSH_CMD "$ART_CHROOT_CMD mkdir -p $ART_TEST_CHROOT/apex"
81else
82  adb shell mkdir -p "$ART_TEST_CHROOT/apex"
83fi
84
85# Manually "activate" the flattened APEX $1 by syncing it to /apex/$2 in the
86# chroot. $2 defaults to $1.
87activate_apex() {
88  local src_apex=${1}
89  local dst_apex=${2:-${src_apex}}
90
91  # Unpack the .apex or .capex file in the product directory, but if we already
92  # see a directory we assume buildbot-build.sh has already done it for us and
93  # just use it.
94  src_apex_path=$ANDROID_PRODUCT_OUT/system/apex/${src_apex}
95  if [ ! -d $src_apex_path ]; then
96    unset src_apex_file
97    if [ -f "${src_apex_path}.apex" ]; then
98      src_apex_file="${src_apex_path}.apex"
99    elif [ -f "${src_apex_path}.capex" ]; then
100      src_apex_file="${src_apex_path}.capex"
101    fi
102    if [ -z "${src_apex_file}" ]; then
103      msgerror "Failed to find .apex or .capex file to extract for ${src_apex_path}"
104      exit 1
105    fi
106    msginfo "Extracting APEX ${src_apex_file}..."
107    mkdir -p $src_apex_path
108    $ANDROID_HOST_OUT/bin/deapexer --debugfs_path $ANDROID_HOST_OUT/bin/debugfs_static \
109      --fsckerofs_path $ANDROID_HOST_OUT/bin/fsck.erofs \
110      extract ${src_apex_file} $src_apex_path
111  fi
112
113  msginfo "Activating APEX ${src_apex} as ${dst_apex}..."
114  if [[ -n "$ART_TEST_ON_VM" ]]; then
115    $ART_RSYNC_CMD $src_apex_path/* \
116      "$ART_TEST_SSH_USER@$ART_TEST_SSH_HOST:$ART_TEST_CHROOT/apex/${dst_apex}"
117  else
118    adb shell rm -rf "$ART_TEST_CHROOT/apex/${dst_apex}"
119    adb push $src_apex_path "$ART_TEST_CHROOT/apex/${dst_apex}"
120  fi
121}
122
123# "Activate" the required APEX modules.
124activate_apex com.android.art.testing com.android.art
125activate_apex com.android.i18n
126activate_apex com.android.runtime
127activate_apex com.android.tzdata
128activate_apex com.android.conscrypt
129activate_apex com.android.os.statsd
130
131# Generate primary boot images on device for testing.
132for b in {32,64}; do
133  basename="generate-boot-image$b"
134  bin_on_host="$ANDROID_PRODUCT_OUT/system/bin/$basename"
135  bin_on_device="/data/local/tmp/$basename"
136  output_dir="/system/framework/art_boot_images"
137  if [ -f $bin_on_host ]; then
138    msginfo "Generating the primary boot image ($b-bit)..."
139    if [[ -n "$ART_TEST_ON_VM" ]]; then
140      $ART_RSYNC_CMD "$bin_on_host" \
141        "$ART_TEST_SSH_USER@$ART_TEST_SSH_HOST:$ART_TEST_CHROOT$bin_on_device"
142      $ART_SSH_CMD "mkdir -p $ART_TEST_CHROOT$output_dir"
143    else
144      adb push "$bin_on_host" "$ART_TEST_CHROOT$bin_on_device"
145      adb shell mkdir -p "$ART_TEST_CHROOT$output_dir"
146    fi
147    # `compiler-filter=speed-profile` is required because OatDumpTest checks the compiled code in
148    # the boot image.
149    if [[ -n "$ART_TEST_ON_VM" ]]; then
150      $ART_SSH_CMD \
151        "$ART_CHROOT_CMD $bin_on_device --output-dir=$output_dir --compiler-filter=speed-profile"
152    else
153      adb shell chroot "$ART_TEST_CHROOT" \
154        "$bin_on_device" --output-dir=$output_dir --compiler-filter=speed-profile
155    fi
156  fi
157done
158