Files
builders_rubenslte/build_twrp.sh
T

154 lines
4.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# Build a TWRP recovery image for the Samsung Galaxy Tab Active SM-T365
# (codename rubenslte, msm8226).
#
# Usage:
# ./build_twrp.sh build recovery.img (default)
# ./build_twrp.sh --no-sync skip repo sync / tree updates (fast iteration)
# ./build_twrp.sh clean remove twrp-build/ and recovery.img
# ./build_twrp.sh env drop into the interactive nix-shell build env
#
# The build runs inside the nix-shell FHS environment defined in shell.nix.
# `nix-shell --run` cannot be relied on here, so when invoked from the host the
# script re-executes itself *inside* the env via a stdin pipe
# (RUBENSLTE_BUILD_IN_ENV prevents infinite recursion).
set -euo pipefail
SCRIPT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT"
RECOVERY_PARTITION_SIZE=10485248
MANIFEST_URL="https://github.com/minimal-manifest-twrp/platform_manifest_twrp_omni.git"
MANIFEST_BRANCH="twrp-5.1"
DEVICE_TREE_URL="https://git.cfpi-fpsi.eu/rubenslte/twrp_device_samsung_rubenslte"
KERNEL_URL="https://git.cfpi-fpsi.eu/rubenslte/android_kernel_samsung_rubenslte"
TOOLCHAIN_URL="https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/arm/arm-eabi-4.8/+archive/refs/tags/android-5.1.1_r38.tar.gz"
TOOLCHAIN_DIR="prebuilts/gcc/linux-x86/arm/arm-eabi-4.8"
usage() {
sed -n '2,9p' "$0" | sed 's/^# \{0,1\}//'
}
reenter_build_env() {
if [ -z "${RUBENSLTE_BUILD_IN_ENV:-}" ]; then
echo "[*] Re-entering build inside the nix-shell FHS environment ..."
printf 'export RUBENSLTE_BUILD_IN_ENV=1\nbash "%s"\n' "$SCRIPT/build_twrp.sh" \
| nix-shell shell.nix
exit $?
fi
}
clone_or_update() {
local name="$1" url="$2" dest="$3"
if [ ! -d "$dest/.git" ]; then
echo "[*] Cloning $name ..."
git clone -q --depth 1 "$url" "$dest"
else
echo "[*] Updating $name ..."
local branch
branch="$(git -C "$dest" rev-parse --abbrev-ref HEAD)"
git -C "$dest" fetch -q --depth 1 origin "$branch"
git -C "$dest" reset -q --hard "origin/$branch"
fi
}
apply_patches() {
local p log
for p in "$SCRIPT"/patches/*.patch; do
log="$(mktemp)"
if patch -p1 -s < "$p" >"$log" 2>&1; then
echo "[*] Applied patch: $(basename "$p")"
elif grep -q -E 'Reversed|already applied' "$log"; then
echo "[*] Patch already applied: $(basename "$p")"
else
echo "[!] WARNING: patch failed: $(basename "$p")"
cat "$log"
fi
rm -f "$log"
done
}
fetch_toolchain() {
if [ -x "$TOOLCHAIN_DIR/bin/arm-eabi-gcc" ]; then
return
fi
echo "[*] Downloading arm-eabi-4.8 prebuilt toolchain ..."
mkdir -p "$TOOLCHAIN_DIR"
curl -L --fail "$TOOLCHAIN_URL" | tar -xz -C "$TOOLCHAIN_DIR"
}
verify_image() {
local img="$1"
[ -f "$img" ] || { echo "ERROR: $img was not produced" >&2; exit 1; }
local size
size="$(stat -c%s "$img")"
echo "[*] $img: $size bytes (partition budget: $RECOVERY_PARTITION_SIZE)"
if [ "$size" -gt "$RECOVERY_PARTITION_SIZE" ]; then
echo "ERROR: image exceeds the recovery partition" >&2
exit 1
fi
sha256sum "$img"
echo "Done: $img"
}
do_build() {
export LC_ALL=C LANG=C LC_CTYPE=C
mkdir -p twrp-build
cd twrp-build
if [ ! -d .repo ]; then
repo init --depth 1 -u "$MANIFEST_URL" -b "$MANIFEST_BRANCH"
fi
if [ "$NO_SYNC" -eq 0 ]; then
repo sync -c -j"$(nproc)"
clone_or_update "device tree" "$DEVICE_TREE_URL" device/samsung/rubenslte
clone_or_update "kernel" "$KERNEL_URL" kernel/samsung/rubenslte
apply_patches
fi
fetch_toolchain
export PATH="$PWD/$TOOLCHAIN_DIR/bin:$PATH"
export TOP="$PWD"
set +u
source build/envsetup.sh
lunch omni_rubenslte-userdebug
set -u
mka recoveryimage
cp out/target/product/rubenslte/recovery.img "$SCRIPT/"
verify_image "$SCRIPT/recovery.img"
}
do_clean() {
rm -rf twrp-build recovery.img
}
do_env() {
exec nix-shell shell.nix
}
cmd="${1:-build}"
case "$cmd" in
clean) shift 2>/dev/null || true ;;
env) shift 2>/dev/null || true ;;
build) shift 2>/dev/null || true ;;
--*) cmd=build ;;
*) usage >&2; exit 2 ;;
esac
NO_SYNC=0
for a in "$@"; do
case "$a" in
--no-sync) NO_SYNC=1 ;;
*) usage >&2; exit 2 ;;
esac
done
case "$cmd" in
build) reenter_build_env; do_build ;;
clean) do_clean ;;
env) do_env ;;
esac