Skip to content

Instantly share code, notes, and snippets.

@Razuuu
Last active November 15, 2023 19:02
Show Gist options
  • Save Razuuu/be0119c53f80e7c7c05b546f14a72720 to your computer and use it in GitHub Desktop.
Save Razuuu/be0119c53f80e7c7c05b546f14a72720 to your computer and use it in GitHub Desktop.
Basic script for building LineageOS. Drop this script to you're android/lineage/ folder
#!/bin/bash
#
# This Script is by Razuuu
#
# Initialize variables with default values
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
DEVICE=""
BUILD_TARGET=""
BUILD_TYPE=""
THREADS="$(nproc)"
CLEAN_FLAG=""
CCACHE_SIZE=""
# Display help message
function print_help() {
cat <<EOF
Usage: $(basename "$0") [OPTIONS]
Options:
--help, -h Display this help message.
--device, -d DEVICE Specify the device.
--build-target, -btt TARGET Specify the build target, e.g. bacon, recoveryimage, bootimage.
--build-type, -bte [eng|user|userdebug] Specify the build type.
--threads, -t THREADS Specify the number of threads. (Default: ${THREADS})
--clean, -c Clean folders. Device is needed.
--ccache, -cc CCACHE_SIZE Enable and specify the ccache size in GB.
EOF
exit 1
}
# Function to print messages with appropriate prefixes
function print_msg() {
local message="$1"
local prefix="[INFO]"
if [[ "$message" == *"fail"* || "$message" == *"missing"* ]]; then
prefix="[ERROR]"
fi
echo "$prefix $message"
}
function clean_files() {
if [ -n "${DEVICE}" ]; then
rm -rf out/target/product/${DEVICE}/system*
rm -rf out/target/product/${DEVICE}/vendor*
rm -rf out/target/product/${DEVICE}/*img
rm -rf out/target/product/${DEVICE}/root*
rm -rf out/target/product/${DEVICE}/ramdisk*
rm -rf out/target/product/${DEVICE}/recovery*
rm -rf out/target/product/${DEVICE}/odm*
rm -rf out/target/product/${DEVICE}/product*
rm -rf out/target/product/${DEVICE}/obj/PACKAGING
rm -rf out/target/product/${DEVICE}/obj/KERNEL_OBJ
rm -rf out/target/product/${DEVICE}/lineage*
rm -rf out/target/product/${DEVICE}/kernel
print_msg "Files successfully cleaned"
else
print_msg "Device is missing!"
print_help
fi
}
# Loop through the command line arguments
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
--device|-d)
DEVICE="$2"
shift 2
;;
--build-target|-btt)
BUILD_TARGET="$2"
shift 2
;;
--build-type|-bte)
BUILD_TYPE="$2"
shift 2
;;
--threads|-t)
THREADS="$2"
shift 2
;;
--clean|-c)
CLEAN_FLAG="true"
shift 1
;;
--ccache|-cc)
CCACHE_SIZE="$2"
shift 2
;;
--help|-h|*)
print_help
;;
esac
done
# Clean folders if CLEAN_FLAG is set
if [ "${CLEAN_FLAG}" == "true" ]; then
clean_files
fi
# Check if required options are missing
if [ -z "${DEVICE}" ] || [ -z "${BUILD_TARGET}" ] || [ -z "${BUILD_TYPE}" ]; then
print_msg "Either Device, Build target, or build type is missing."
print_help
fi
# Set up ccache if CCACHE_SIZE is specified
if [ -n "${CCACHE_SIZE}" ]; then
USE_CCACHE=1
CCACHE_EXEC=/usr/bin/ccache
ccache -M "${CCACHE_SIZE}"
fi
# Error handling for sourcing envsetup.sh
source "${SCRIPT_DIR}/build/envsetup.sh" || {
print_msg "Failed to source envsetup.sh"
exit 1
}
# Init Device
lunch lineage_${DEVICE}-${BUILD_TYPE}
# Print variables
for VAR in DEVICE BUILD_TARGET BUILD_TYPE THREADS CCACHE_SIZE; do
echo "${VAR}=${!VAR}"
done
# Build device
m -j"${THREADS}" "${BUILD_TARGET}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment