#!/bin/bash # Automatically prepare OpenWRT build dependencies for Debian/Ubuntu # Supports both x86_64 and arm64 # Detects Ubuntu 22.04, 24.04, 26.04, Debian 12/13 and newer # Author: Zhe Yuan set -e umask 022 echo "========================================" echo " OpenWRT Build Environment Preparation" echo "========================================" # --- Detect architecture --- ARCH=$(uname -m) echo "[INFO] Detected architecture: $ARCH" # --- Detect OS version --- if [ -f /etc/os-release ]; then . /etc/os-release DISTRO="$ID" VERSION_ID="${VERSION_ID:-unknown}" else echo "[ERROR] Cannot detect system version (no /etc/os-release)" exit 1 fi echo "[INFO] Detected distribution: $DISTRO $VERSION_ID" # --- Version helpers --- version_ge() { local current="${1:-unknown}" local minimum="$2" if [[ "$current" == "unknown" ]]; then return 1 fi if command -v dpkg >/dev/null 2>&1; then dpkg --compare-versions "$current" ge "$minimum" else [[ "$(printf '%s\n%s\n' "$minimum" "$current" | sort -V | head -n1)" == "$minimum" ]] fi } IS_UBUNTU_24_OR_NEWER=false IS_UBUNTU_26_OR_NEWER=false IS_DEBIAN_13_OR_NEWER=false if [[ "$DISTRO" == "ubuntu" ]] && version_ge "$VERSION_ID" "24.04"; then IS_UBUNTU_24_OR_NEWER=true fi if [[ "$DISTRO" == "ubuntu" ]] && version_ge "$VERSION_ID" "26.04"; then IS_UBUNTU_26_OR_NEWER=true fi if [[ "$DISTRO" == "debian" ]] && version_ge "$VERSION_ID" "13"; then IS_DEBIAN_13_OR_NEWER=true fi # --- Base packages (common to all) --- COMMON_PACKAGES="build-essential clang flex bison g++ gawk \ gettext git libssl-dev rsync swig unzip zlib1g-dev file wget bzip2" # --- Determine specific package set --- if [[ "$ARCH" == "x86_64" || "$ARCH" == "amd64" ]]; then echo "[INFO] Preparing environment for x86_64." if [[ "$IS_UBUNTU_26_OR_NEWER" == "true" ]]; then echo "[INFO] Detected Ubuntu 26.04+ (no python3-distutils; OpenWrt docs use libncurses-dev)." EXTRA_PACKAGES="gcc-multilib g++-multilib glibc-source libncurses-dev python3-setuptools" elif [[ "$IS_UBUNTU_24_OR_NEWER" == "true" ]]; then echo "[INFO] Detected Ubuntu 24.04+ (Python 3.12+, no python3-distutils)." EXTRA_PACKAGES="gcc-multilib g++-multilib libncurses5-dev python3-setuptools" elif [[ "$IS_DEBIAN_13_OR_NEWER" == "true" ]]; then echo "[INFO] Detected Debian 13+ (no python3-distutils)." EXTRA_PACKAGES="gcc-multilib g++-multilib libncurses-dev python3-setuptools" else echo "[INFO] Detected older Ubuntu/Debian (with python3-distutils)." EXTRA_PACKAGES="gcc-multilib g++-multilib libncurses-dev python3-distutils python3-setuptools" fi INSTALL_PACKAGES="$COMMON_PACKAGES $EXTRA_PACKAGES" elif [[ "$ARCH" == "aarch64" || "$ARCH" == "arm64" ]]; then echo "[INFO] Preparing environment for ARM64." # Note: arm64 has no native multilib; install cross multilib packages MULTILIB_PACKAGES=" gcc-multilib-i686-linux-gnu gcc-multilib-s390x-linux-gnu \ gcc-multilib-x86-64-linux-gnu gcc-multilib-x86-64-linux-gnux32 \ g++-multilib-i686-linux-gnu g++-multilib-s390x-linux-gnu \ g++-multilib-x86-64-linux-gnu g++-multilib-x86-64-linux-gnux32 \ libc6-dev-i386-amd64-cross libc6-dev-i386-cross libc6-dev-i386-x32-cross" if [[ "$IS_UBUNTU_26_OR_NEWER" == "true" ]]; then EXTRA_PACKAGES="glibc-source libncurses-dev python3-setuptools" elif [[ "$IS_UBUNTU_24_OR_NEWER" == "true" ]]; then EXTRA_PACKAGES="libncurses5-dev python3-setuptools" elif [[ "$IS_DEBIAN_13_OR_NEWER" == "true" ]]; then EXTRA_PACKAGES="libncurses-dev python3-setuptools" else EXTRA_PACKAGES="libncurses-dev python3-distutils python3-setuptools" fi INSTALL_PACKAGES="$COMMON_PACKAGES $EXTRA_PACKAGES $MULTILIB_PACKAGES golang" else echo "[WARN] Unknown architecture: $ARCH" echo "[WARN] Attempting to use x86_64 package list." if [[ "$IS_UBUNTU_26_OR_NEWER" == "true" ]]; then INSTALL_PACKAGES="$COMMON_PACKAGES gcc-multilib g++-multilib glibc-source libncurses-dev python3-setuptools" else INSTALL_PACKAGES="$COMMON_PACKAGES gcc-multilib g++-multilib libncurses-dev python3-setuptools" fi fi # --- Display package list --- echo echo "========================================" echo "📦 Packages to install:" echo "$INSTALL_PACKAGES" echo "========================================" echo echo "[INFO] Updating APT repositories..." sudo apt update -y echo "[INFO] Installing packages..." sudo apt install -y $INSTALL_PACKAGES # --- Post installation notice --- echo echo "========================================" echo "✅ OpenWRT build environment setup complete!" echo "========================================" echo if [[ "$ARCH" == "x86_64" || "$ARCH" == "amd64" ]]; then echo "👉 [NOTE] Go language is optional on x86_64." echo " If you plan to compile packages requiring Go (e.g. firewall4, netifd), install it manually:" echo " sudo apt install golang -y" else echo "👉 [INFO] Go language installed for ARM64 cross compilation." fi echo echo "⚙️ After running 'make menuconfig', set Go toolchain path if required:" echo " Example: /usr/lib/go-1.18" echo echo "👉 Next step:" echo " ./prepare-openwrt.sh # to clone and set up OpenWRT source" echo