From 3f1216779ee2ec0d734ea9210efb435c81dec823 Mon Sep 17 00:00:00 2001 From: cloudy Date: Sun, 19 Jul 2026 14:44:07 +0700 Subject: [PATCH] build: add auto-build script for OrangeFox with json config Got tired of typing the same sync/clone/build commands for every device, so now everything lives in config.json (codename, vendor, tree repo, fox branch, build target, env vars). The script handles: - syncing fox sources via the official orangefox_sync.sh - cloning/updating the device tree into device// - exporting env vars from the json and running mka Also supports -s/-c/-b flags to run each step separately. Signed-off-by: cloudy --- ofox-build.sh | 146 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 ofox-build.sh diff --git a/ofox-build.sh b/ofox-build.sh new file mode 100644 index 0000000..6dd97e6 --- /dev/null +++ b/ofox-build.sh @@ -0,0 +1,146 @@ +#!/bin/bash +# ofox-build.sh +# quick script to build OrangeFox without typing the same 20 commands every time +# reads everything from config.json so I dont have to edit the script per device +# +# usage: ./ofox-build.sh [config.json] +# -s sync only +# -c clone tree only +# -b build only + +set -e + +CONFIG="config.json" +MODE="all" + +# arg parsing, nothing fancy +while getopts "scbh" opt; do + case $opt in + s) MODE="sync";; + c) MODE="clone";; + b) MODE="build";; + h) sed -n '2,9p' "$0"; exit 0;; + esac +done +shift $((OPTIND-1)) +[ -n "$1" ] && CONFIG="$1" + +if ! command -v jq >/dev/null; then + echo "need jq installed (sudo apt install jq)" + exit 1 +fi + +if [ ! -f "$CONFIG" ]; then + # no config.json? maybe theres a .json laying around + FOUND=$(ls *.json 2>/dev/null | head -n1) + if [ -n "$FOUND" ]; then + echo "using $FOUND" + CONFIG="$FOUND" + else + echo "cant find $CONFIG (or any .json here)" + exit 1 + fi +fi + +# pull stuff out of the json +FOX_BRANCH=$(jq -r '.fox.branch' "$CONFIG") +SYNC_DIR=$(eval echo "$(jq -r '.fox.sync_dir' "$CONFIG")") +BUILD_DIR=$(eval echo "$(jq -r '.fox.build_dir' "$CONFIG")") + +CODENAME=$(jq -r '.device.codename' "$CONFIG") +VENDOR=$(jq -r '.device.vendor' "$CONFIG") +TREE_REPO=$(jq -r '.device.tree_repo' "$CONFIG") +TREE_BRANCH=$(jq -r '.device.tree_branch // "main"' "$CONFIG") + +TARGET=$(jq -r '.build.target // "recoveryimage"' "$CONFIG") +JOBS=$(jq -r '.build.jobs // 0' "$CONFIG") +CLEAN=$(jq -r '.build.clean_before_build // false' "$CONFIG") +CCACHE_ON=$(jq -r '.build.ccache // true' "$CONFIG") +CCACHE_SIZE=$(jq -r '.build.ccache_size // "50G"' "$CONFIG") + +# 0 = use all cores +[ "$JOBS" = "0" ] && JOBS=$(nproc) + +if [ "$CODENAME" = "null" ] || [ -z "$CODENAME" ]; then + echo "no codename in config??" + exit 1 +fi + +echo "==========================================" +echo " OrangeFox $FOX_BRANCH for $VENDOR/$CODENAME" +echo " build dir: $BUILD_DIR" +echo " jobs: $JOBS" +echo "==========================================" + +do_sync() { + # dont resync if we already have sources, takes forever + if [ -d "$BUILD_DIR/.repo" ]; then + echo "sources already there, skipping sync" + return + fi + + echo ">> syncing fox sources, go grab a coffee..." + mkdir -p "$SYNC_DIR" + if [ ! -d "$SYNC_DIR/sync" ]; then + git clone https://gitlab.com/OrangeFox/sync.git "$SYNC_DIR/sync" + fi + cd "$SYNC_DIR/sync" + ./orangefox_sync.sh --branch "$FOX_BRANCH" --path "$BUILD_DIR" +} + +do_clone() { + DEST="$BUILD_DIR/device/$VENDOR/$CODENAME" + echo ">> device tree -> $DEST" + + if [ -d "$DEST/.git" ]; then + # already cloned, just update it + cd "$DEST" + git fetch origin "$TREE_BRANCH" + git checkout "$TREE_BRANCH" + git pull origin "$TREE_BRANCH" || true + else + git clone -b "$TREE_BRANCH" "$TREE_REPO" "$DEST" + fi +} + +do_build() { + cd "$BUILD_DIR" + + # export whatever is in the env section of the json + for kv in $(jq -r '.env // {} | to_entries[] | "\(.key)=\(.value)"' "$CONFIG"); do + export "$kv" + echo " $kv" + done + + if [ "$CCACHE_ON" = "true" ] && command -v ccache >/dev/null; then + export USE_CCACHE=1 + export CCACHE_EXEC=$(which ccache) + ccache -M "$CCACHE_SIZE" >/dev/null + fi + + export LC_ALL=C + source build/envsetup.sh + + [ "$CLEAN" = "true" ] && make clean + + # newer trees use twrp_ prefix, older ones omni_ + lunch twrp_${CODENAME}-eng 2>/dev/null || lunch omni_${CODENAME}-eng || { + echo "lunch failed, check the tree makefile name" + exit 1 + } + + echo ">> building, this will take a while" + mka $TARGET -j$JOBS + + OUT="$BUILD_DIR/out/target/product/$CODENAME" + echo "" + echo ">> done! output files:" + ls -lh "$OUT"/OrangeFox* 2>/dev/null || ls -lh "$OUT"/*.img +} + +case $MODE in + sync) do_sync;; + clone) do_clone;; + build) do_build;; + all) do_sync && do_clone && do_build;; +esac