From 8b7fb6cddeb1ac8c4e4526c2a46749771683d3ec Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Thu, 15 May 2025 02:28:25 -0700 Subject: [PATCH] Improve scripts --- .github/workflows/build.yml | 4 +- build.py | 12 +- scripts/{avd_test.sh => avd.sh} | 225 +++++++++++++---------- scripts/{avd_patch.sh => host_patch.sh} | 0 scripts/{avd_magisk.sh => live_setup.sh} | 0 5 files changed, 138 insertions(+), 103 deletions(-) rename scripts/{avd_test.sh => avd.sh} (55%) rename scripts/{avd_patch.sh => host_patch.sh} (100%) rename scripts/{avd_magisk.sh => live_setup.sh} (100%) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index f0df79ac5..488be5b4d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -110,7 +110,7 @@ jobs: timeout-minutes: 10 env: AVD_TEST_LOG: 1 - run: scripts/avd_test.sh ${{ matrix.version }} ${{ matrix.type }} + run: scripts/avd.sh test ${{ matrix.version }} ${{ matrix.type }} - name: Upload logs on error if: ${{ failure() }} @@ -152,7 +152,7 @@ jobs: env: FORCE_32_BIT: 1 AVD_TEST_LOG: 1 - run: scripts/avd_test.sh ${{ matrix.version }} + run: scripts/avd.sh test ${{ matrix.version }} - name: Upload logs on error if: ${{ failure() }} diff --git a/build.py b/build.py index 15e558534..8364ba2f7 100755 --- a/build.py +++ b/build.py @@ -642,11 +642,11 @@ def push_files(script): def setup_avd(): header("* Setting up emulator") - push_files(Path("scripts", "avd_magisk.sh")) + push_files(Path("scripts", "live_setup.sh")) - proc = execv([adb_path, "shell", "sh", "/data/local/tmp/avd_magisk.sh"]) + proc = execv([adb_path, "shell", "sh", "/data/local/tmp/live_setup.sh"]) if proc.returncode != 0: - error("avd_magisk.sh failed!") + error("live_setup.sh failed!") def patch_avd_file(): @@ -655,7 +655,7 @@ def patch_avd_file(): header(f"* Patching {input.name}") - push_files(Path("scripts", "avd_patch.sh")) + push_files(Path("scripts", "host_patch.sh")) proc = execv([adb_path, "push", input, "/data/local/tmp"]) if proc.returncode != 0: @@ -664,9 +664,9 @@ def patch_avd_file(): src_file = f"/data/local/tmp/{input.name}" out_file = f"{src_file}.magisk" - proc = execv([adb_path, "shell", "sh", "/data/local/tmp/avd_patch.sh", src_file]) + proc = execv([adb_path, "shell", "sh", "/data/local/tmp/host_patch.sh", src_file]) if proc.returncode != 0: - error("avd_patch.sh failed!") + error("host_patch.sh failed!") proc = execv([adb_path, "pull", out_file, output]) if proc.returncode != 0: diff --git a/scripts/avd_test.sh b/scripts/avd.sh similarity index 55% rename from scripts/avd_test.sh rename to scripts/avd.sh index 197337687..78db799ea 100755 --- a/scripts/avd_test.sh +++ b/scripts/avd.sh @@ -1,29 +1,52 @@ #!/usr/bin/env bash -set -xe +set -e . scripts/test_common.sh emu_port=5682 export ANDROID_SERIAL="emulator-$emu_port" emu_args_base="-no-window -no-audio -no-boot-anim -gpu swiftshader_indirect -read-only -no-snapshot -port $emu_port -cores $core_count" +log_args="-show-kernel -logcat '' -logcat-output logcat.log" +emu_args= emu_pid= atd_min_api=30 atd_max_api=35 huge_ram_min_api=26 -cleanup() { - print_error "! An error occurred" +case $(uname -m) in + 'arm64'|'aarch64') + if [ -n "$FORCE_32_BIT" ]; then + echo "! ARM32 is not supported" + exit 1 + fi + arch=arm64-v8a + ;; + *) + if [ -n "$FORCE_32_BIT" ]; then + arch=x86 + else + arch=x86_64 + fi - rm -f magisk_patched.img - "$avd" delete avd -n test + ;; +esac + +cleanup() { pkill -INT -P $$ wait trap - EXIT + rm -f magisk_*.img + "$avd" delete avd -n test exit 1 } +test_error() { + print_error "! An error occurred" + cleanup +} + wait_for_boot() { set -e adb wait-for-device @@ -49,33 +72,18 @@ wait_emu() { [ $which_pid -eq $wait_pid ] } -test_emu() { - local variant=$1 - local api=$2 - - print_title "* Testing $avd_pkg ($variant)" - - if [ -n "$AVD_TEST_LOG" ]; then - rm -f logcat.log - "$emu" @test $emu_args > kernel.log 2>&1 & - else - "$emu" @test $emu_args > /dev/null 2>&1 & - fi - - emu_pid=$! - wait_emu - - run_setup $variant - - adb reboot - wait_emu - - run_tests +dump_vars() { + local val + for name in $@; do + eval val=\$$name + echo $name=\"$val\"\; + done } -test_main() { - local ver=$1 - local type=$2 +resolve_vars() { + local arg_list="$1" + local ver=$2 + local type=$3 # Determine API level local api @@ -91,21 +99,19 @@ test_main() { ;; esac - # Determine image type + # Determine default image type if [ -z $type ]; then if [ $api -ge $atd_min_api -a $api -le $atd_max_api ]; then # Use the lightweight ATD images if possible type='aosp_atd' + elif [ $api -gt $atd_max_api ]; then + # Preview/beta release, no AOSP version available + type='google_apis' else type='default' fi fi - # System image variable and paths - local avd_pkg="system-images;android-$ver;$type;$arch" - local sys_img_dir="$ANDROID_HOME/system-images/android-$ver/$type/$arch" - local ramdisk="$sys_img_dir/ramdisk.img" - # Old Linux kernels will not boot with memory larger than 3GB local memory if [ $api -lt $huge_ram_min_api ]; then @@ -116,9 +122,58 @@ test_main() { emu_args="$emu_args_base -memory $memory" - # Setup emulator - "$sdk" --channel=3 $avd_pkg + # System image variable and paths + local avd_pkg="system-images;android-$ver;$type;$arch" + local sys_img_dir="$ANDROID_HOME/system-images/android-$ver/$type/$arch" + local ramdisk="$sys_img_dir/ramdisk.img" + + # Dump variables to output + dump_vars $arg_list +} + +setup_emu() { + local avd_pkg=$1 + + yes | "$sdk" --licenses > /dev/null 2>&1 + "$sdk" --channel=3 platform-tools emulator $avd_pkg echo no | "$avd" create avd -f -n test -k $avd_pkg +} + +test_emu() { + local variant=$1 + + local magisk_args="-ramdisk magisk_${variant}.img -feature -SystemAsRoot" + + if [ -n "$AVD_TEST_LOG" ]; then + rm -f logcat.log + "$emu" @test $emu_args $log_args $magisk_args > kernel.log 2>&1 & + else + "$emu" @test $emu_args $magisk_args > /dev/null 2>&1 & + fi + + emu_pid=$! + wait_emu + + run_setup $variant + + adb reboot + wait_emu + + run_tests + + kill -INT $emu_pid + wait $emu_pid +} + +test_main() { + local avd_pkg ramdisk + eval $(resolve_vars "emu_args avd_pkg ramdisk" $1 $2) + + setup_emu "$avd_pkg" + + # Restart ADB daemon just in case + adb kill-server + adb start-server # Launch stock emulator print_title "* Launching $avd_pkg" @@ -126,77 +181,57 @@ test_main() { emu_pid=$! wait_emu - # Update arguments for Magisk runs - emu_args="$emu_args -ramdisk magisk_patched.img -feature -SystemAsRoot" - if [ -n "$AVD_TEST_LOG" ]; then - emu_args="$emu_args -show-kernel -logcat '' -logcat-output logcat.log" + # Patch images + if [ -z "$AVD_TEST_SKIP_DEBUG" ]; then + ./build.py -v avd_patch "$ramdisk" magisk_debug.img + fi + if [ -z "$AVD_TEST_SKIP_RELEASE" ]; then + ./build.py -vr avd_patch "$ramdisk" magisk_release.img fi + kill -INT $emu_pid + wait $emu_pid + if [ -z "$AVD_TEST_SKIP_DEBUG" ]; then - # Patch and test debug build - ./build.py -v avd_patch "$ramdisk" magisk_patched.img - kill -INT $emu_pid - wait $emu_pid - test_emu debug $api + print_title "* Testing $avd_pkg (debug)" + test_emu debug fi if [ -z "$AVD_TEST_SKIP_RELEASE" ]; then - # Patch and test release build - ./build.py -vr avd_patch "$ramdisk" magisk_patched.img - kill -INT $emu_pid - wait $emu_pid - test_emu release $api + print_title "* Testing $avd_pkg (release)" + test_emu release fi # Cleanup - kill -INT $emu_pid - wait $emu_pid - rm -f magisk_patched.img + rm -f magisk_*.img + "$avd" delete avd -n test } -trap cleanup EXIT -export -f wait_for_boot +run_main() { + local avd_pkg + eval $(resolve_vars "emu_args avd_pkg" $1 $2) + setup_emu "$avd_pkg" + "$emu" @test $emu_args 2>/dev/null +} -case $(uname -m) in - 'arm64'|'aarch64') - arch=arm64-v8a +case "$1" in + test ) + shift + trap test_error EXIT + export -f wait_for_boot + set -x + test_main "$@" ;; - *) - arch=x86_64 + run ) + shift + trap cleanup EXIT + run_main "$@" + ;; + * ) + print_error "Unknown argument '$1'" + exit 1 ;; esac -if [ -n "$FORCE_32_BIT" ]; then - case $arch in - 'arm64-v8a') - echo "! ARM32 is not supported" - exit 1 - ;; - 'x86_64') - arch=x86 - max_api=$i386_max_api - ;; - esac -fi - -yes | "$sdk" --licenses > /dev/null -"$sdk" --channel=3 platform-tools emulator - -adb kill-server -adb start-server - -if [ -n "$1" ]; then - test_main $1 $2 -else - for api in $(seq 23 35); do - test_main $api - done - # Android 16 Beta - test_main 36 google_apis - # Run 16k page tests - test_main 36 google_apis_ps16k -fi - -"$avd" delete avd -n test - +# Exit normally, don't run through cleanup again trap - EXIT diff --git a/scripts/avd_patch.sh b/scripts/host_patch.sh similarity index 100% rename from scripts/avd_patch.sh rename to scripts/host_patch.sh diff --git a/scripts/avd_magisk.sh b/scripts/live_setup.sh similarity index 100% rename from scripts/avd_magisk.sh rename to scripts/live_setup.sh