111 lines · bash
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-exception6#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)$ ]]; then14 API=${BASH_REMATCH[1]}15 case ${BASH_REMATCH[2]} in16 def) TYPE=default ;;17 goog) TYPE=google_apis ;;18 play) TYPE=google_apis_playstore ;;19 esac20 ARCH=${BASH_REMATCH[3]}21 return 022 else23 return 124 fi25}26 27# Check that the emulator image name has valid syntax.28validate_emu_img_syntax() {29 local EMU_IMG="${1}"30 local API TYPE ARCH31 if ! __parse_emu_img "${EMU_IMG}"; then32 echo "\33error: invalid emulator image name: ${EMU_IMG}34 expected \"\${API}-\${TYPE}-\${ARCH}\" where API is a number, TYPE is one of35 (def|goog|play), and ARCH is one of arm, arm64, x86, or x86_64." >&236 return 137 fi38}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 image45# 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}"; then49 return 150 fi51 # Make sure Docker is working before trusting other Docker commands.52 # Temporarily suppress command echoing so we only show 'docker info' output53 # on failure, and only once.54 if (set +x; !(docker info &>/dev/null || docker info)); then55 echo "error: Docker is required for emulator usage but 'docker info' failed" >&256 return 157 fi58 local DOCKER_IMAGE=$(docker_image_of_emu_img ${EMU_IMG})59 if ! docker image inspect ${DOCKER_IMAGE} &>/dev/null; then60 echo "error: emulator Docker image (${DOCKER_IMAGE}) is not installed" >&261 return 162 fi63}64 65api_of_emu_img() {66 local API TYPE ARCH67 __parse_emu_img "${1}"68 echo ${API}69}70 71type_of_emu_img() {72 local API TYPE ARCH73 __parse_emu_img "${1}"74 echo ${TYPE}75}76 77arch_of_emu_img() {78 local API TYPE ARCH79 __parse_emu_img "${1}"80 echo ${ARCH}81}82 83# Expand the short emu_img string into the full SDK package string identifying84# the system image.85sdk_package_of_emu_img() {86 local API TYPE ARCH87 __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}" in94 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 esac100}101 102triple_of_arch() {103 case "${1}" in104 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 esac110}111