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
19if [ -t 1 ]; then
20  # Color sequences if terminal is a tty.
21  red='\033[0;31m'
22  green='\033[0;32m'
23  yellow='\033[0;33m'
24  magenta='\033[0;35m'
25  nc='\033[0m'
26fi
27
28# Setup as root, as some actions performed here require it.
29adb root
30adb wait-for-device
31
32if [[ -z "$ANDROID_BUILD_TOP" ]]; then
33  echo 'ANDROID_BUILD_TOP environment variable is empty; did you forget to run `lunch`?'
34  exit 1
35fi
36
37if [[ -z "$ANDROID_PRODUCT_OUT" ]]; then
38  echo 'ANDROID_PRODUCT_OUT environment variable is empty; did you forget to run `lunch`?'
39  exit 1
40fi
41
42if [[ -z "$ART_TEST_CHROOT" ]]; then
43  echo 'ART_TEST_CHROOT environment variable is empty; please set it before running this script.'
44  exit 1
45fi
46
47if [[ "$(build/soong/soong_ui.bash --dumpvar-mode TARGET_FLATTEN_APEX)" != "true" ]]; then
48  echo -e "${red}This script only works when  APEX packages are flattened, but the build" \
49    "configuration is set up to use non-flattened APEX packages.${nc}"
50  echo -e "${magenta}You can force APEX flattening by setting the environment variable" \
51    "\`OVERRIDE_TARGET_FLATTEN_APEX\` to \"true\" before starting the build and running this" \
52    "script.${nc}"
53  exit 1
54fi
55
56
57# `/system` "partition" synchronization.
58# --------------------------------------
59
60# Sync the system directory to the chroot.
61echo -e "${green}Syncing system directory...${nc}"
62adb shell mkdir -p "$ART_TEST_CHROOT/system"
63adb push "$ANDROID_PRODUCT_OUT/system" "$ART_TEST_CHROOT/"
64# Overwrite the default public.libraries.txt file with a smaller one that
65# contains only the public libraries pushed to the chroot directory.
66adb push "$ANDROID_BUILD_TOP/art/tools/public.libraries.buildbot.txt" \
67  "$ART_TEST_CHROOT/system/etc/public.libraries.txt"
68
69
70# APEX packages activation.
71# -------------------------
72
73# Manually "activate" the flattened APEX $1 by syncing it to /apex/$2 in the
74# chroot. $2 defaults to $1.
75#
76# TODO: Handle the case of build targets using non-flatted APEX packages.
77# As a workaround, one can run `export OVERRIDE_TARGET_FLATTEN_APEX=true` before building
78# a target to have its APEX packages flattened.
79activate_apex() {
80  local src_apex=${1}
81  local dst_apex=${2:-${src_apex}}
82  echo -e "${green}Activating APEX ${src_apex} as ${dst_apex}...${nc}"
83  # We move the files from `/system/apex/${src_apex}` to `/apex/${dst_apex}` in
84  # the chroot directory, instead of simply using a symlink, as Bionic's linker
85  # relies on the real path name of a binary (e.g.
86  # `/apex/com.android.art/bin/dex2oat`) to select the linker configuration.
87  adb shell mkdir -p "$ART_TEST_CHROOT/apex"
88  adb shell rm -rf "$ART_TEST_CHROOT/apex/${dst_apex}"
89  # Use use mv instead of cp, as cp has a bug on fugu NRD90R where symbolic
90  # links get copied with odd names, eg: libcrypto.so -> /system/lib/libcrypto.soe.sort.so
91  adb shell mv "$ART_TEST_CHROOT/system/apex/${src_apex}" "$ART_TEST_CHROOT/apex/${dst_apex}" \
92    || exit 1
93}
94
95# "Activate" the required APEX modules.
96activate_apex com.android.art.testing com.android.art
97activate_apex com.android.i18n
98activate_apex com.android.runtime
99activate_apex com.android.tzdata
100activate_apex com.android.conscrypt
101
102
103# Linker configuration.
104# ---------------------
105
106# Statically linked `linkerconfig` binary.
107linkerconfig_binary="/system/bin/linkerconfig"
108# Generated linker configuration file path (since Android R).
109ld_generated_config_file_path="/linkerconfig/ld.config.txt"
110# Location of the generated linker configuration file.
111ld_generated_config_file_location=$(dirname "$ld_generated_config_file_path")
112
113# Generate linker configuration files on device.
114echo -e "${green}Generating linker configuration files on device in" \
115  "\`$ld_generated_config_file_path\`${nc}..."
116adb shell chroot "$ART_TEST_CHROOT" \
117  "$linkerconfig_binary" --target "$ld_generated_config_file_location" || exit 1
118ld_generated_config_files=$(adb shell find $ART_TEST_CHROOT/linkerconfig ! -type d | sed 's/^/  /')
119echo -e "${green}Generated linker configuration files on device:${nc}"
120echo -e "${green}$ld_generated_config_files${nc}"
121
122
123# `/data` "partition" synchronization.
124# ------------------------------------
125
126# Sync the data directory to the chroot.
127echo -e "${green}Syncing data directory...${nc}"
128adb shell mkdir -p "$ART_TEST_CHROOT/data"
129adb push "$ANDROID_PRODUCT_OUT/data" "$ART_TEST_CHROOT/"
130