Debian Codegraph @

libcxx/utils/ci/vendor/android/emulator-entrypoint.sh

raw
1 #!/usr/bin/env bash
2 # ===----------------------------------------------------------------------===##
3 #
4 # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 # See https://llvm.org/LICENSE.txt for license information.
6 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 #
8 # ===----------------------------------------------------------------------===##
9
10 # This script is the entrypoint of an Android Emulator Docker container.
11
12 set -e
13
14 # The container's /dev/kvm has the same UID+GID as the host device. Changing the
15 # ownership inside the container doesn't affect the UID+GID on the host.
16 sudo chown emulator:emulator /dev/kvm
17
18 # Always use a copy of platform-tools provided by the host to ensure that the
19 # versions of adb match between the host and the emulator.
20 if [ ! -x /mnt/android-platform-tools/platform-tools/adb ]; then
21 echo "error: This image requires platform-tools mounted at" \
22 "/mnt/android-platform-tools containing platform-tools/adb" >&2
23 exit 1
24 fi
25 sudo cp -r /mnt/android-platform-tools/platform-tools /opt/android/sdk
26
27 # Start an adb host server. `adb start-server` blocks until the port is ready.
28 # Use ADB_REJECT_KILL_SERVER=1 to ensure that an adb protocol version mismatch
29 # doesn't kill the adb server.
30 ADB_REJECT_KILL_SERVER=1 adb -a start-server
31
32 # This syntax (using an IP address of 127.0.0.1 rather than localhost) seems to
33 # prevent the adb client from ever spawning an adb host server.
34 export ADB_SERVER_SOCKET=tcp:127.0.0.1:5037
35
36 # The AVD could already exist if the Docker container were stopped and then
37 # restarted.
38 if [ ! -d ~/.android/avd/emulator.avd ]; then
39 # N.B. AVD creation takes a few seconds and creates a mostly-empty
40 # multi-gigabyte userdata disk image. (It's not useful to create the AVDs in
41 # advance.)
42 avdmanager --verbose create avd --name emulator \
43 --package "${EMU_PACKAGE_NAME}" --device pixel_5
44 fi
45
46 # Use exec so that the emulator is PID 1, so that `docker stop` kills the
47 # emulator.
48 exec emulator @emulator -no-audio -no-window -no-metrics \
49 -partition-size "${EMU_PARTITION_SIZE}"