xref: /aosp_15_r20/external/cronet/third_party/libc++/src/utils/ci/vendor/android/emulator-functions.sh (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1#===----------------------------------------------------------------------===##
2#
3# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4# See https://llvm.org/LICENSE.txt for license information.
5# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6#
7#===----------------------------------------------------------------------===##
8
9# Bash functions for managing the names of emulator system images.
10
11# Parse the image name and set variables: API, TYPE, and ARCH.
12__parse_emu_img() {
13    if [[ "${1}" =~ ([0-9]+)-(def|goog|play)-(arm|arm64|x86|x86_64)$ ]]; then
14        API=${BASH_REMATCH[1]}
15        case ${BASH_REMATCH[2]} in
16            def) TYPE=default ;;
17            goog) TYPE=google_apis ;;
18            play) TYPE=google_apis_playstore ;;
19        esac
20        ARCH=${BASH_REMATCH[3]}
21        return 0
22    else
23        return 1
24    fi
25}
26
27# Check that the emulator image name has valid syntax.
28validate_emu_img_syntax() {
29    local EMU_IMG="${1}"
30    local API TYPE ARCH
31    if ! __parse_emu_img "${EMU_IMG}"; then
32        echo "\
33error: invalid emulator image name: ${EMU_IMG}
34  expected \"\${API}-\${TYPE}-\${ARCH}\" where API is a number, TYPE is one of
35  (def|goog|play), and ARCH is one of arm, arm64, x86, or x86_64." >&2
36        return 1
37    fi
38}
39
40docker_image_of_emu_img() {
41    echo "android-emulator-${1}"
42}
43
44# Check that the emulator image name has valid syntax and that the Docker image
45# is present. On failure, writes an error to stderr and exits the script.
46validate_emu_img() {
47    local EMU_IMG="${1}"
48    if ! validate_emu_img_syntax "${EMU_IMG}"; then
49        return 1
50    fi
51    # Make sure Docker is working before trusting other Docker commands.
52    # Temporarily suppress command echoing so we only show 'docker info' output
53    # on failure, and only once.
54    if (set +x; !(docker info &>/dev/null || docker info)); then
55        echo "error: Docker is required for emulator usage but 'docker info' failed" >&2
56        return 1
57    fi
58    local DOCKER_IMAGE=$(docker_image_of_emu_img ${EMU_IMG})
59    if ! docker image inspect ${DOCKER_IMAGE} &>/dev/null; then
60        echo "error: emulator Docker image (${DOCKER_IMAGE}) is not installed" >&2
61        return 1
62    fi
63}
64
65api_of_emu_img() {
66    local API TYPE ARCH
67    __parse_emu_img "${1}"
68    echo ${API}
69}
70
71type_of_emu_img() {
72    local API TYPE ARCH
73    __parse_emu_img "${1}"
74    echo ${TYPE}
75}
76
77arch_of_emu_img() {
78    local API TYPE ARCH
79    __parse_emu_img "${1}"
80    echo ${ARCH}
81}
82
83# Expand the short emu_img string into the full SDK package string identifying
84# the system image.
85sdk_package_of_emu_img() {
86    local API TYPE ARCH
87    __parse_emu_img "${1}"
88    echo "system-images;android-${API};${TYPE};$(abi_of_arch ${ARCH})"
89}
90
91# Return the Android ABI string for an architecture.
92abi_of_arch() {
93    case "${1}" in
94        arm) echo armeabi-v7a ;;
95        arm64) echo aarch64-v8a ;;
96        x86) echo x86 ;;
97        x86_64) echo x86_64 ;;
98        *) echo "error: unhandled arch ${1}" >&2; exit 1 ;;
99    esac
100}
101
102triple_of_arch() {
103    case "${1}" in
104        arm) echo armv7a-linux-androideabi ;;
105        arm64) echo aarch64-linux-android ;;
106        x86) echo i686-linux-android ;;
107        x86_64) echo x86_64-linux-android ;;
108        *) echo "error: unhandled arch ${1}" >&2; exit 1 ;;
109    esac
110}
111