File: emulator-functions.sh

package info (click to toggle)
llvm-toolchain-18 1%3A18.1.8-18
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,908,340 kB
  • sloc: cpp: 6,667,937; ansic: 1,440,452; asm: 883,619; python: 230,549; objc: 76,880; f90: 74,238; lisp: 35,989; pascal: 16,571; sh: 10,229; perl: 7,459; ml: 5,047; awk: 3,523; makefile: 2,987; javascript: 2,149; xml: 892; fortran: 649; cs: 573
file content (110 lines) | stat: -rw-r--r-- 3,321 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#===----------------------------------------------------------------------===##
#
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
#===----------------------------------------------------------------------===##

# Bash functions for managing the names of emulator system images.

# Parse the image name and set variables: API, TYPE, and ARCH.
__parse_emu_img() {
    if [[ "${1}" =~ ([0-9]+)-(def|goog|play)-(arm|arm64|x86|x86_64)$ ]]; then
        API=${BASH_REMATCH[1]}
        case ${BASH_REMATCH[2]} in
            def) TYPE=default ;;
            goog) TYPE=google_apis ;;
            play) TYPE=google_apis_playstore ;;
        esac
        ARCH=${BASH_REMATCH[3]}
        return 0
    else
        return 1
    fi
}

# Check that the emulator image name has valid syntax.
validate_emu_img_syntax() {
    local EMU_IMG="${1}"
    local API TYPE ARCH
    if ! __parse_emu_img "${EMU_IMG}"; then
        echo "\
error: invalid emulator image name: ${EMU_IMG}
  expected \"\${API}-\${TYPE}-\${ARCH}\" where API is a number, TYPE is one of
  (def|goog|play), and ARCH is one of arm, arm64, x86, or x86_64." >&2
        return 1
    fi
}

docker_image_of_emu_img() {
    echo "android-emulator-${1}"
}

# Check that the emulator image name has valid syntax and that the Docker image
# is present. On failure, writes an error to stderr and exits the script.
validate_emu_img() {
    local EMU_IMG="${1}"
    if ! validate_emu_img_syntax "${EMU_IMG}"; then
        return 1
    fi
    # Make sure Docker is working before trusting other Docker commands.
    # Temporarily suppress command echoing so we only show 'docker info' output
    # on failure, and only once.
    if (set +x; !(docker info &>/dev/null || docker info)); then
        echo "error: Docker is required for emulator usage but 'docker info' failed" >&2
        return 1
    fi
    local DOCKER_IMAGE=$(docker_image_of_emu_img ${EMU_IMG})
    if ! docker image inspect ${DOCKER_IMAGE} &>/dev/null; then
        echo "error: emulator Docker image (${DOCKER_IMAGE}) is not installed" >&2
        return 1
    fi
}

api_of_emu_img() {
    local API TYPE ARCH
    __parse_emu_img "${1}"
    echo ${API}
}

type_of_emu_img() {
    local API TYPE ARCH
    __parse_emu_img "${1}"
    echo ${TYPE}
}

arch_of_emu_img() {
    local API TYPE ARCH
    __parse_emu_img "${1}"
    echo ${ARCH}
}

# Expand the short emu_img string into the full SDK package string identifying
# the system image.
sdk_package_of_emu_img() {
    local API TYPE ARCH
    __parse_emu_img "${1}"
    echo "system-images;android-${API};${TYPE};$(abi_of_arch ${ARCH})"
}

# Return the Android ABI string for an architecture.
abi_of_arch() {
    case "${1}" in
        arm) echo armeabi-v7a ;;
        arm64) echo aarch64-v8a ;;
        x86) echo x86 ;;
        x86_64) echo x86_64 ;;
        *) echo "error: unhandled arch ${1}" >&2; exit 1 ;;
    esac
}

triple_of_arch() {
    case "${1}" in
        arm) echo armv7a-linux-androideabi ;;
        arm64) echo aarch64-linux-android ;;
        x86) echo i686-linux-android ;;
        x86_64) echo x86_64-linux-android ;;
        *) echo "error: unhandled arch ${1}" >&2; exit 1 ;;
    esac
}