#!/usr/bin/env bash set -euo pipefail if [[ $# -ne 4 ]]; then echo "usage: $0 CONFIG ARCHITECTURE VERSION OUTPUT_DIR" >&2 exit 2 fi config=$1 architecture=$2 version=$3 output_dir=$4 repo_root=$(CDPATH= cd -- "$(dirname -- "$0")/../.." && pwd) [[ $architecture =~ ^[a-zA-Z0-9_-]+$ ]] || { echo "invalid architecture name: $architecture" >&2; exit 2; } [[ $version =~ ^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$ ]] || { echo "invalid semantic version: $version" >&2; exit 2; } matches=$(jq --arg architecture "$architecture" '[.targets[] | select(.architecture == $architecture)] | length' "$config") [[ $matches -eq 1 ]] || { echo "architecture must occur exactly once in $config: $architecture" >&2; exit 2; } openwrt_release=$(jq -er '.openwrt_release' "$config") target=$(jq -er --arg architecture "$architecture" '.targets[] | select(.architecture == $architecture) | .target' "$config") subtarget=$(jq -er --arg architecture "$architecture" '.targets[] | select(.architecture == $architecture) | .subtarget' "$config") download_base="https://downloads.openwrt.org/releases/${openwrt_release}/targets/${target}/${subtarget}" sdk_archive=$(curl -fsSL "${download_base}/" | grep -o "openwrt-sdk-${openwrt_release}-[^\"]*\.tar\.zst" | head -n 1) [[ -n $sdk_archive ]] || { echo "no SDK found at $download_base" >&2; exit 1; } work_dir="${RUNNER_TEMP:-/tmp}/openunifi-${architecture}-${version}" rm -rf -- "$work_dir" mkdir -p -- "$work_dir" "$repo_root/$output_dir" curl -fL --retry 3 --output "$work_dir/$sdk_archive" "$download_base/$sdk_archive" expected_sha=$(curl -fsSL "$download_base/sha256sums" | awk -v archive="$sdk_archive" '{ name=$2; sub(/^\*/, "", name); if (name == archive) { print $1; exit } }') [[ -n $expected_sha ]] || { echo "no checksum found for $sdk_archive" >&2; exit 1; } printf '%s %s\n' "$expected_sha" "$work_dir/$sdk_archive" | sha256sum --check tar --zstd -xf "$work_dir/$sdk_archive" -C "$work_dir" sdk_dir=$(find "$work_dir" -mindepth 1 -maxdepth 1 -type d -name 'openwrt-sdk-*' -print -quit) [[ -n $sdk_dir ]] || { echo "SDK directory was not extracted" >&2; exit 1; } cd "$sdk_dir" ./scripts/feeds update base packages ./scripts/feeds install mbedtls uci usteer mkdir -p package/openuf (cd "$repo_root" && tar --exclude=.git --exclude="$output_dir" -cf - .) | (cd package/openuf && tar -xf -) sed -i -E "s/^(PKG_VERSION[[:space:]]*:?=[[:space:]]*).*/\\1${version}/" package/openuf/Makefile # Release SDKs remember the package set used to create the SDK. Neutralize # those baked-in defaults so this job builds openuf and its dependency closure, # rather than every package and kernel module available for the target. sed -i -E '/^config PACKAGE_/,/^$/ s/^([[:space:]]*)default [ym]$/\1default n/' Config-build.in for symbol in TARGET_MULTI_PROFILE TARGET_ALL_PROFILES TARGET_PER_DEVICE_ROOTFS ALL_NONSHARED ALL_KMODS ALL BUILDBOT; do sed -i -E "/^config ${symbol}$/,/^$/ s/^([[:space:]]*)default y$/\\1default n/" Config.in Config-build.in done printf '%s\n' \ '# CONFIG_ALL is not set' \ '# CONFIG_ALL_KMODS is not set' \ '# CONFIG_ALL_NONSHARED is not set' \ '# CONFIG_TARGET_MULTI_PROFILE is not set' \ '# CONFIG_TARGET_ALL_PROFILES is not set' \ 'CONFIG_PACKAGE_openuf=m' >.config make defconfig make -j"$(nproc)" package/openuf/compile mapfile -t packages < <(find bin/packages -type f -name "openuf-${version}-*.apk") if [[ ${#packages[@]} -ne 1 ]]; then echo "expected one openuf package, found ${#packages[@]}" >&2 printf '%s\n' "${packages[@]}" >&2 exit 1 fi asset="$repo_root/$output_dir/openUniFi-${architecture}-${version}.apk" cp -- "${packages[0]}" "$asset" sha256sum "$asset"