#!/bin/bash
#
# This file is part of MagiskOnWSALocal.
#
# MagiskOnWSALocal is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# MagiskOnWSALocal is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with MagiskOnWSALocal. If not, see .
#
# Copyright (C) 2022 LSPosed Contributors
#
if [ ! "$BASH_VERSION" ]; then
echo "Please do not use sh to run this script, just execute it directly" 1>&2
exit 1
fi
HOST_ARCH=$(uname -m)
if [ "$HOST_ARCH" != "x86_64" ] && [ "$HOST_ARCH" != "aarch64" ]; then
echo "Unsupported architectures: $HOST_ARCH"
exit 1
fi
cd "$(dirname "$0")" || exit 1
trap umount_clean EXIT
# export TMPDIR=$(dirname "$PWD")/WORK_DIR_
if [ "$TMPDIR" ] && [ ! -d "$TMPDIR" ]; then
mkdir -p "$TMPDIR"
fi
WORK_DIR=$(mktemp -d -t wsa-build-XXXXXXXXXX_) || exit 1
DOWNLOAD_DIR=../download
DOWNLOAD_CONF_NAME=download.list
OUTPUT_DIR=../output
MOUNT_DIR="$WORK_DIR"/system
SUDO="$(which sudo 2>/dev/null)"
if [ -z "$SUDO" ]; then
unset SUDO
fi
umount_clean() {
if [ -d "$MOUNT_DIR" ]; then
echo "Cleanup Work Directory"
if [ -d "$MOUNT_DIR/vendor" ]; then
$SUDO umount "$MOUNT_DIR"/vendor
fi
if [ -d "$MOUNT_DIR/product" ]; then
$SUDO umount "$MOUNT_DIR"/product
fi
if [ -d "$MOUNT_DIR/system_ext" ]; then
$SUDO umount "$MOUNT_DIR"/system_ext
fi
$SUDO umount "$MOUNT_DIR"
$SUDO rm -rf "${WORK_DIR:?}"
else
rm -rf "${WORK_DIR:?}"
fi
if [ "$TMPDIR" ] && [ -d "$TMPDIR" ]; then
rm -rf "${TMPDIR:?}"
unset TMPDIR
fi
}
clean_download() {
if [ -d "$DOWNLOAD_DIR" ]; then
echo "Cleanup Download Directory"
if [ "$CLEAN_DOWNLOAD_WSA" ]; then
rm -f "${WSA_ZIP_PATH:?}"
fi
if [ "$CLEAN_DOWNLOAD_MAGISK" ]; then
rm -f "${MAGISK_PATH:?}"
fi
if [ "$CLEAN_DOWNLOAD_GAPPS" ]; then
rm -f "${GAPPS_PATH:?}"
fi
fi
}
abort() {
echo "Build: an error has occurred, exit"
if [ -d "$WORK_DIR" ]; then
umount_clean
fi
clean_download
exit 1
}
trap abort INT TERM
function Gen_Rand_Str {
tr -dc 'a-zA-Z0-9' ' -e '/print/i \ \ \ \ ' "$MOUNT_DIR"/vendor/etc/permissions/windows.permissions.xml
$SUDO setfattr -n security.selinux -v "u:object_r:vendor_configs_file:s0" "$MOUNT_DIR"/vendor/etc/permissions/windows.permissions.xml || abort
echo -e "done\n"
if [ "$ROOT_SOL" = 'magisk' ] || [ "$ROOT_SOL" = '' ]; then
echo "Integrate Magisk"
$SUDO mkdir "$MOUNT_DIR"/sbin
$SUDO setfattr -n security.selinux -v "u:object_r:rootfs:s0" "$MOUNT_DIR"/sbin || abort
$SUDO chown root:root "$MOUNT_DIR"/sbin
$SUDO chmod 0700 "$MOUNT_DIR"/sbin
$SUDO cp "$WORK_DIR"/magisk/magisk/* "$MOUNT_DIR"/sbin/
$SUDO cp "$MAGISK_PATH" "$MOUNT_DIR"/sbin/magisk.apk
$SUDO tee -a "$MOUNT_DIR"/sbin/loadpolicy.sh <
EOF
wine64 ../wine/"$HOST_ARCH"/makepri.exe new /pr "$WORK_DIR"/wsa/pri /in MicrosoftCorporationII.WindowsSubsystemForAndroid /cf "$WORK_DIR"/wsa/priconfig.xml /of "$WORK_DIR"/wsa/"$ARCH"/resources.pri /o
sed -i -zE "s//\n$(cat "$WORK_DIR"/wsa/xml/* | grep -Po ']*/>' | sed ':a;N;$!ba;s/\n/\\n/g' | sed 's/\$/\\$/g' | sed 's/\//\\\//g')\n<\/Resources>/g" "$WORK_DIR"/wsa/"$ARCH"/AppxManifest.xml
echo -e "Merge Language Resources done\n"
echo "Add extra packages"
$SUDO cp -r ../"$ARCH"/system/* "$MOUNT_DIR" || abort
find ../"$ARCH"/system/system/priv-app/ -maxdepth 1 -mindepth 1 -printf '%P\n' | xargs -I placeholder $SUDO find "$MOUNT_DIR"/system/priv-app/placeholder -type d -exec chmod 0755 {} \;
find ../"$ARCH"/system/system/priv-app/ -maxdepth 1 -mindepth 1 -printf '%P\n' | xargs -I placeholder $SUDO find "$MOUNT_DIR"/system/priv-app/placeholder -type f -exec chmod 0644 {} \;
find ../"$ARCH"/system/system/priv-app/ -maxdepth 1 -mindepth 1 -printf '%P\n' | xargs -I placeholder $SUDO find "$MOUNT_DIR"/system/priv-app/placeholder -exec chown root:root {} \;
find ../"$ARCH"/system/system/priv-app/ -maxdepth 1 -mindepth 1 -printf '%P\n' | xargs -I placeholder $SUDO find "$MOUNT_DIR"/system/priv-app/placeholder -exec setfattr -n security.selinux -v "u:object_r:system_file:s0" {} \; || abort
echo -e "Add extra packages done\n"
if [ "$GAPPS_BRAND" != 'none' ]; then
echo "Integrate $GAPPS_BRAND"
find "$WORK_DIR/gapps/" -mindepth 1 -type d -exec $SUDO chmod 0755 {} \;
find "$WORK_DIR/gapps/" -mindepth 1 -type d -exec $SUDO chown root:root {} \;
file_list="$(find "$WORK_DIR/gapps/" -mindepth 1 -type f | cut -d/ -f5-)"
for file in $file_list; do
$SUDO chown root:root "$WORK_DIR/gapps/${file}"
$SUDO chmod 0644 "$WORK_DIR/gapps/${file}"
done
if [ "$GAPPS_BRAND" = "OpenGApps" ]; then
find "$WORK_DIR"/gapps/ -maxdepth 1 -mindepth 1 -type d -not -path '*product' -exec $SUDO cp --preserve=all -r {} "$MOUNT_DIR"/system \; || abort
elif [ "$GAPPS_BRAND" = "MindTheGapps" ]; then
$SUDO cp --preserve=all -r "$WORK_DIR"/gapps/system_ext/* "$MOUNT_DIR"/system_ext/ || abort
if [ -e "$MOUNT_DIR"/system_ext/priv-app/SetupWizard ]; then
rm -rf "${MOUNT_DIR:?}/system_ext/priv-app/Provision"
fi
fi
$SUDO cp --preserve=all -r "$WORK_DIR"/gapps/product/* "$MOUNT_DIR"/product || abort
find "$WORK_DIR"/gapps/product/overlay -maxdepth 1 -mindepth 1 -printf '%P\n' | xargs -I placeholder $SUDO find "$MOUNT_DIR"/product/overlay/placeholder -type f -exec setfattr -n security.selinux -v "u:object_r:vendor_overlay_file:s0" {} \; || abort
if [ "$GAPPS_BRAND" = "OpenGApps" ]; then
find "$WORK_DIR"/gapps/app/ -maxdepth 1 -mindepth 1 -printf '%P\n' | xargs -I placeholder $SUDO find "$MOUNT_DIR"/system/app/placeholder -type d -exec setfattr -n security.selinux -v "u:object_r:system_file:s0" {} \; || abort
find "$WORK_DIR"/gapps/framework/ -maxdepth 1 -mindepth 1 -printf '%P\n' | xargs -I placeholder $SUDO find "$MOUNT_DIR"/system/framework/placeholder -type d -exec setfattr -n security.selinux -v "u:object_r:system_file:s0" {} \; || abort
find "$WORK_DIR"/gapps/priv-app/ -maxdepth 1 -mindepth 1 -printf '%P\n' | xargs -I placeholder $SUDO find "$MOUNT_DIR"/system/priv-app/placeholder -type d -exec setfattr -n security.selinux -v "u:object_r:system_file:s0" {} \; || abort
find "$WORK_DIR"/gapps/app/ -maxdepth 1 -mindepth 1 -printf '%P\n' | xargs -I placeholder $SUDO find "$MOUNT_DIR"/system/app/placeholder -type f -exec setfattr -n security.selinux -v "u:object_r:system_file:s0" {} \; || abort
find "$WORK_DIR"/gapps/framework/ -maxdepth 1 -mindepth 1 -printf '%P\n' | xargs -I placeholder $SUDO find "$MOUNT_DIR"/system/framework/placeholder -type f -exec setfattr -n security.selinux -v "u:object_r:system_file:s0" {} \; || abort
find "$WORK_DIR"/gapps/priv-app/ -maxdepth 1 -mindepth 1 -printf '%P\n' | xargs -I placeholder $SUDO find "$MOUNT_DIR"/system/priv-app/placeholder -type f -exec setfattr -n security.selinux -v "u:object_r:system_file:s0" {} \; || abort
find "$WORK_DIR"/gapps/etc/ -maxdepth 1 -mindepth 1 -printf '%P\n' | xargs -I placeholder $SUDO find "$MOUNT_DIR"/system/etc/placeholder -type d -exec setfattr -n security.selinux -v "u:object_r:system_file:s0" {} \; || abort
find "$WORK_DIR"/gapps/etc/ -maxdepth 1 -mindepth 1 -printf '%P\n' | xargs -I placeholder $SUDO find "$MOUNT_DIR"/system/etc/placeholder -type f -exec setfattr -n security.selinux -v "u:object_r:system_file:s0" {} \; || abort
else
find "$WORK_DIR"/gapps/product/app/ -maxdepth 1 -mindepth 1 -printf '%P\n' | xargs -I placeholder $SUDO find "$MOUNT_DIR"/product/app/placeholder -type d -exec setfattr -n security.selinux -v "u:object_r:system_file:s0" {} \; || abort
find "$WORK_DIR"/gapps/product/etc/ -maxdepth 1 -mindepth 1 -printf '%P\n' | xargs -I placeholder $SUDO find "$MOUNT_DIR"/product/etc/placeholder -type d -exec setfattr -n security.selinux -v "u:object_r:system_file:s0" {} \; || abort
find "$WORK_DIR"/gapps/product/priv-app/ -maxdepth 1 -mindepth 1 -printf '%P\n' | xargs -I placeholder $SUDO find "$MOUNT_DIR"/product/priv-app/placeholder -type d -exec setfattr -n security.selinux -v "u:object_r:system_file:s0" {} \; || abort
find "$WORK_DIR"/gapps/product/framework/ -maxdepth 1 -mindepth 1 -printf '%P\n' | xargs -I placeholder $SUDO find "$MOUNT_DIR"/product/framework/placeholder -type d -exec setfattr -n security.selinux -v "u:object_r:system_file:s0" {} \; || abort
find "$WORK_DIR"/gapps/product/app/ -maxdepth 1 -mindepth 1 -printf '%P\n' | xargs -I placeholder $SUDO find "$MOUNT_DIR"/product/app/placeholder -type f -exec setfattr -n security.selinux -v "u:object_r:system_file:s0" {} \; || abort
find "$WORK_DIR"/gapps/product/etc/ -maxdepth 1 -mindepth 1 -printf '%P\n' | xargs -I placeholder $SUDO find "$MOUNT_DIR"/product/etc/placeholder -type f -exec setfattr -n security.selinux -v "u:object_r:system_file:s0" {} \; || abort
find "$WORK_DIR"/gapps/product/priv-app/ -maxdepth 1 -mindepth 1 -printf '%P\n' | xargs -I placeholder $SUDO find "$MOUNT_DIR"/product/priv-app/placeholder -type f -exec setfattr -n security.selinux -v "u:object_r:system_file:s0" {} \; || abort
find "$WORK_DIR"/gapps/product/framework/ -maxdepth 1 -mindepth 1 -printf '%P\n' | xargs -I placeholder $SUDO find "$MOUNT_DIR"/product/framework/placeholder -type f -exec setfattr -n security.selinux -v "u:object_r:system_file:s0" {} \; || abort
find "$WORK_DIR"/gapps/system_ext/etc/permissions/ -maxdepth 1 -mindepth 1 -printf '%P\n' | xargs -I placeholder $SUDO find "$MOUNT_DIR"/system_ext/etc/permissions/placeholder -type f -exec setfattr -n security.selinux -v "u:object_r:system_file:s0" {} \; || abort
$SUDO setfattr -n security.selinux -v "u:object_r:system_lib_file:s0" "$MOUNT_DIR"/product/lib || abort
find "$WORK_DIR"/gapps/product/lib/ -maxdepth 1 -mindepth 1 -printf '%P\n' | xargs -I placeholder $SUDO find "$MOUNT_DIR"/product/lib/placeholder -exec setfattr -n security.selinux -v "u:object_r:system_lib_file:s0" {} \; || abort
find "$WORK_DIR"/gapps/product/lib64/ -maxdepth 1 -mindepth 1 -printf '%P\n' | xargs -I placeholder $SUDO find "$MOUNT_DIR"/product/lib64/placeholder -type f -exec setfattr -n security.selinux -v "u:object_r:system_lib_file:s0" {} \; || abort
find "$WORK_DIR"/gapps/system_ext/priv-app/ -maxdepth 1 -mindepth 1 -printf '%P\n' | xargs -I placeholder $SUDO find "$MOUNT_DIR"/system_ext/priv-app/placeholder -type d -exec setfattr -n security.selinux -v "u:object_r:system_file:s0" {} \; || abort
find "$WORK_DIR"/gapps/system_ext/etc/ -maxdepth 1 -mindepth 1 -printf '%P\n' | xargs -I placeholder $SUDO find "$MOUNT_DIR"/system_ext/etc/placeholder -type d -exec setfattr -n security.selinux -v "u:object_r:system_file:s0" {} \; || abort
find "$WORK_DIR"/gapps/system_ext/priv-app/ -maxdepth 1 -mindepth 1 -printf '%P\n' | xargs -I placeholder $SUDO find "$MOUNT_DIR"/system_ext/priv-app/placeholder -type f -exec setfattr -n security.selinux -v "u:object_r:system_file:s0" {} \; || abort
fi
$SUDO "$WORK_DIR"/magisk/magiskpolicy --load "$MOUNT_DIR"/vendor/etc/selinux/precompiled_sepolicy --save "$MOUNT_DIR"/vendor/etc/selinux/precompiled_sepolicy "allow gmscore_app gmscore_app vsock_socket { create connect write read }" "allow gmscore_app device_config_runtime_native_boot_prop file read" "allow gmscore_app system_server_tmpfs dir search" "allow gmscore_app system_server_tmpfs file open" "allow gmscore_app system_server_tmpfs filesystem getattr" "allow gmscore_app gpu_device dir search" || abort
echo -e "Integrate $GAPPS_BRAND done\n"
fi
if [ "$GAPPS_BRAND" != 'none' ]; then
echo "Fix $GAPPS_BRAND prop"
$SUDO python3 fixGappsProp.py "$MOUNT_DIR" || abort
echo -e "done\n"
fi
echo "Umount images"
$SUDO find "$MOUNT_DIR" -exec touch -hamt 200901010000.00 {} \;
$SUDO umount "$MOUNT_DIR"/vendor
$SUDO umount "$MOUNT_DIR"/product
$SUDO umount "$MOUNT_DIR"/system_ext
$SUDO umount "$MOUNT_DIR"
echo -e "done\n"
echo "Shrink images"
e2fsck -pf "$WORK_DIR"/wsa/"$ARCH"/system.img || abort
resize2fs -M "$WORK_DIR"/wsa/"$ARCH"/system.img || abort
e2fsck -pf "$WORK_DIR"/wsa/"$ARCH"/vendor.img || abort
resize2fs -M "$WORK_DIR"/wsa/"$ARCH"/vendor.img || abort
e2fsck -pf "$WORK_DIR"/wsa/"$ARCH"/product.img || abort
resize2fs -M "$WORK_DIR"/wsa/"$ARCH"/product.img || abort
e2fsck -pf "$WORK_DIR"/wsa/"$ARCH"/system_ext.img || abort
resize2fs -M "$WORK_DIR"/wsa/"$ARCH"/system_ext.img || abort
echo -e "Shrink images done\n"
echo "Remove signature and add scripts"
$SUDO rm -rf "${WORK_DIR:?}"/wsa/"$ARCH"/\[Content_Types\].xml "$WORK_DIR"/wsa/"$ARCH"/AppxBlockMap.xml "$WORK_DIR"/wsa/"$ARCH"/AppxSignature.p7x "$WORK_DIR"/wsa/"$ARCH"/AppxMetadata || abort
cp "$vclibs_PATH" "$xaml_PATH" "$WORK_DIR"/wsa/"$ARCH" || abort
tee "$WORK_DIR"/wsa/"$ARCH"/Install.ps1 <nul
exit 1
) else (
start powershell.exe -ExecutionPolicy Bypass -File .\Install.ps1
exit
)
EOF
echo -e "Remove signature and add scripts done\n"
echo "Generate info"
if [[ "$ROOT_SOL" = "none" ]]; then
name1=""
elif [[ "$ROOT_SOL" = "" ]]; then
name1="-with-magisk-$MAGISK_VER"
else
name1="-with-$ROOT_SOL-$MAGISK_VER"
fi
if [ "$GAPPS_BRAND" = "none" ]; then
name2="-NoGApps"
else
if [ "$GAPPS_BRAND" = "OpenGApps" ]; then
name2="-$GAPPS_BRAND-${GAPPS_VARIANT}"
else
name2="-$GAPPS_BRAND"
fi
if [ "$GAPPS_BRAND" = "OpenGApps" ] && [ "$DEBUG" ]; then
echo ":warning: Since OpenGApps doesn't officially support Android 12.1 yet, lock the variant to pico!"
fi
fi
artifact_name="WSA${name1}${name2}_${WSA_VER}_${ARCH}_${WSA_REL}"
echo "$artifact_name"
echo -e "\nFinishing building...."
if [ -f "$OUTPUT_DIR" ]; then
$SUDO rm -rf "${OUTPUT_DIR:?}"
fi
if [ ! -d "$OUTPUT_DIR" ]; then
mkdir -p "$OUTPUT_DIR"
fi
OUTPUT_PATH="${OUTPUT_DIR:?}/$artifact_name"
if [ "$COMPRESS_OUTPUT" ] || [ -n "$COMPRESS_FORMAT" ]; then
mv "$WORK_DIR/wsa/$ARCH" "$WORK_DIR/wsa/$artifact_name"
if [ -z "$COMPRESS_FORMAT" ]; then
COMPRESS_FORMAT="7z"
fi
if [ -n "$COMPRESS_FORMAT" ]; then
FILE_EXT=".$COMPRESS_FORMAT"
if [ "$FILE_EXT" = ".xz" ]; then
FILE_EXT=".tar$FILE_EXT"
fi
OUTPUT_PATH="$OUTPUT_PATH$FILE_EXT"
fi
rm -f "${OUTPUT_PATH:?}" || abort
if [ "$COMPRESS_FORMAT" = "7z" ]; then
echo "Compressing with 7z"
7z a "${OUTPUT_PATH:?}" "$WORK_DIR/wsa/$artifact_name" || abort
elif [ "$COMPRESS_FORMAT" = "xz" ]; then
echo "Compressing with tar xz"
if ! (tar -cP -I 'xz -9 -T0' -f "${OUTPUT_PATH:?}" "$WORK_DIR/wsa/$artifact_name"); then
echo "Out of memory? Trying again with single threads..."
tar -cPJvf "${OUTPUT_PATH:?}" "$WORK_DIR/wsa/$artifact_name" || abort
fi
elif [ "$COMPRESS_FORMAT" = "zip" ]; then
7z -tzip a "${OUTPUT_PATH:?}" "$WORK_DIR/wsa/$artifact_name" || abort
fi
else
rm -rf "${OUTPUT_PATH:?}" || abort
cp -r "$WORK_DIR"/wsa/"$ARCH" "$OUTPUT_PATH" || abort
fi
echo -e "done\n"
echo "Cleanup Work Directory"
$SUDO rm -rf "${WORK_DIR:?}"
echo "done"