blob: d845470f2f125961494960be222892abaab4e6f9 [file]
#!/usr/bin/env bash
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) Opinsys Oy 2026
set -euo pipefail
case "${1:-}" in
configure|build|savedefconfig|linux-update-defconfig)
command=$1
;;
*)
echo "usage: $0 <configure|build|savedefconfig|linux-update-defconfig>" >&2
exit 1
;;
esac
if (($# != 1)); then
echo "usage: $0 <configure|build|savedefconfig|linux-update-defconfig>" >&2
exit 1
fi
cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.."
: "${BOARD:?BOARD is required}"
: "${BOARD_DIR:?BOARD_DIR is required}"
: "${TPMDD_ARCH:?TPMDD_ARCH is required}"
: "${TPMDD_VERSION:?TPMDD_VERSION is required}"
: "${BUILDROOT_JOBS:?BUILDROOT_JOBS is required}"
: "${BUILDROOT_SRC_DIR:?BUILDROOT_SRC_DIR is required}"
: "${BUILDROOT_DL_DIR:?BUILDROOT_DL_DIR is required}"
: "${BUILDROOT_TARBALL:?BUILDROOT_TARBALL is required}"
: "${BUILDROOT_HASH:?BUILDROOT_HASH is required}"
: "${BUILDROOT_PATCH:?BUILDROOT_PATCH is required}"
: "${BUILDROOT_PATCH_STAMP:?BUILDROOT_PATCH_STAMP is required}"
: "${BUILDROOT_PATCH_LOCK:?BUILDROOT_PATCH_LOCK is required}"
: "${SDK_INSTALL_DIR:?SDK_INSTALL_DIR is required}"
: "${SDK_INSTALL_STAMP:?SDK_INSTALL_STAMP is required}"
: "${CCACHE_DIR:?CCACHE_DIR is required}"
: "${SOURCE_DATE_EPOCH:?SOURCE_DATE_EPOCH is required}"
[[ "${FORCE:-0}" == 0 || "${FORCE:-0}" == 1 ]] || {
echo 'FORCE must be 0 or 1' >&2
exit 1
}
mkdir -p "$(dirname "${BUILDROOT_PATCH_LOCK}")"
exec 9>"${BUILDROOT_PATCH_LOCK%.patch}.build"
flock 9
./scripts/buildroot-setup.sh "${BUILDROOT_PATCH_LOCK}" "${BUILDROOT_TARBALL}" \
"${BUILDROOT_HASH}" "${BOARD_DIR}" "${BUILDROOT_SRC_DIR}" \
"${BUILDROOT_PATCH_STAMP}" "${BUILDROOT_PATCH}"
state=(python3 "${PWD}/scripts/state.py")
state_dir="${BOARD_DIR}/stamp/orchestration"
mkdir -p "${state_dir}"
make_args=(
-j"${BUILDROOT_JOBS}" -C "${BUILDROOT_SRC_DIR}"
O="${BOARD_DIR}" BR2_EXTERNAL="${PWD}/br2-external" BR2_CCACHE_DIR="${CCACHE_DIR}"
BR2_DL_DIR="${BUILDROOT_DL_DIR}" SDK_INSTALL_DIR="${SDK_INSTALL_DIR}"
TPMDD_VERSION="${TPMDD_VERSION}" SOURCE_DATE_EPOCH="${SOURCE_DATE_EPOCH}"
BR2_CCACHE=y
)
configure_args=(
key
--path "br2-external/configs/${BOARD}_defconfig"
--path br2-external/Config.in
--path br2-external/external.desc
--path "${BUILDROOT_PATCH_STAMP}"
--path "${SDK_INSTALL_STAMP}"
--path scripts/buildroot-board.sh
--value "source=${BUILDROOT_SRC_DIR}"
--value "sdk=${SDK_INSTALL_DIR}"
--value "board=${BOARD_DIR}"
--value "epoch=${SOURCE_DATE_EPOCH}"
)
while IFS= read -r -d '' path; do
configure_args+=(--path "${path}")
done < <(find br2-external/package -name Config.in \( -type f -o -type l \) -print0 | sort -z)
if [[ "${command}" == savedefconfig ]]; then
if [[ ! -f "${BOARD_DIR}/.config" ]]; then
make "${make_args[@]}" "${BOARD}_defconfig"
fi
make "${make_args[@]}" BR2_DEFCONFIG="${PWD}/br2-external/configs/${BOARD}_defconfig" savedefconfig
configure_key=$("${state[@]}" "${configure_args[@]}")
"${state[@]}" save "${state_dir}/configure.json" --key "${configure_key}" --path "${BOARD_DIR}/.config"
exit 0
fi
configure_key=$("${state[@]}" "${configure_args[@]}")
if [[ "${command}" == configure ]] || ! "${state[@]}" check "${state_dir}/configure.json" \
--key "${configure_key}" --path "${BOARD_DIR}/.config"; then
echo "${BOARD}: applying configuration"
rm -f "${state_dir}/build.json"
make "${make_args[@]}" "${BOARD}_defconfig"
"${state[@]}" save "${state_dir}/configure.json" --key "${configure_key}" --path "${BOARD_DIR}/.config"
fi
if [[ "${command}" == linux-update-defconfig ]]; then
linux_defconfig="br2-external/linux/${BOARD}_defconfig"
linux_dir=
for candidate in "${BOARD_DIR}/build"/linux-*; do
[[ -d "${candidate}" ]] || continue
case "$(basename "${candidate}")" in
linux-firmware*|linux-tools*|linux-pam*) continue ;;
esac
linux_dir="${candidate}"
break
done
if [[ -z "${linux_dir}" ]]; then
make "${make_args[@]}" linux-patch
for candidate in "${BOARD_DIR}/build"/linux-*; do
[[ -d "${candidate}" ]] || continue
case "$(basename "${candidate}")" in
linux-firmware*|linux-tools*|linux-pam*) continue ;;
esac
linux_dir="${candidate}"
break
done
fi
test -n "${linux_dir}"
if [[ ! -f "${linux_dir}/.stamp_kconfig_fixup_done" || ! -f "${linux_dir}/.stamp_dotconfig" || \
"${linux_defconfig}" -nt "${linux_dir}/.stamp_dotconfig" ]]; then
make "${make_args[@]}" "${linux_dir}/.stamp_kconfig_fixup_done"
fi
make "${make_args[@]}" linux-update-defconfig
exit 0
fi
[[ "${command}" == build ]] || exit 0
export TARGET_DIR="${BOARD_DIR}/target" BR2_EXTERNAL_TPMDD_PATH="${PWD}/br2-external"
input_args=(
key
--exclude __pycache__
--exclude '*.pyc'
--path Makefile
--path br2-external
--path sdk
--path scripts
--path buildroot.patch
--value "source=${BUILDROOT_SRC_DIR}"
--value "sdk=${SDK_INSTALL_DIR}"
--value "board=${BOARD_DIR}"
--value "epoch=${SOURCE_DATE_EPOCH}"
)
has_override_env=false
while IFS= read -r name; do
case "${name}" in
*PASSWORD*|*TOKEN*|*SECRET*) continue ;;
*_OVERRIDE_SRCDIR) has_override_env=true ;;
esac
input_args+=(--value "${name}=${!name}")
done < <(compgen -e | LC_ALL=C sort | grep -E '^(TPMDD_|BR2_|[A-Za-z0-9_]+_OVERRIDE_SRCDIR$)' || true)
input_key=$("${state[@]}" "${input_args[@]}")
configuration_key=$("${state[@]}" key --path "${BOARD_DIR}/.config")
. scripts/image-lib.sh
outputs=(
--metadata-path "${BOARD_DIR}/images/${BOARD}.${TPMDD_MEDIA_EXT}"
--metadata-path "${BOARD_DIR}/images/rootfs.squashfs"
--metadata-path "${BOARD_DIR}/images/rootfs-oci-latest-${TPMDD_ARCH}-linux.oci-image.tar"
--metadata-path "${BOARD_DIR}/images/image.tar"
--path "${BOARD_DIR}/images/image.manifest"
--path "${BOARD_DIR}/images/image-${TPMDD_VERSION}.tar"
--path "${BOARD_DIR}/images/image-${TPMDD_VERSION}.manifest"
--path "${BOARD_DIR}/images/ssh_config"
)
if [[ "${TPMDD_ARCH}" == amd64 ]]; then
outputs+=(
--metadata-path "${BOARD_DIR}/images/rootfs-live.squashfs"
--path "${BOARD_DIR}/images/live-ssh/id-rsa"
--path "${BOARD_DIR}/images/live-ssh/id-rsa.pub"
)
else
outputs+=(
--path "${TARGET_DIR}/var/roothome/.ssh/id-rsa"
--path "${TARGET_DIR}/var/roothome/.ssh/id-rsa.pub"
--path "${TARGET_DIR}/var/roothome/.ssh/authorized_keys"
)
fi
if [[ "${FORCE:-0}" == 0 && "${has_override_env}" == false && ! -e "${BOARD_DIR}/local.mk" && \
-f "${state_dir}/packages.json" && ! -e "${state_dir}/packages.pending.json" ]]; then
build_tree=$("${state[@]}" build-tree "${BOARD_DIR}")
build_key=$("${state[@]}" key \
--value "inputs=${input_key}" \
--value "packages=${build_tree}" \
--path "${BOARD_DIR}/.config" \
--path "${state_dir}/packages.json")
if "${state[@]}" check "${state_dir}/build.json" --key "${build_key}" "${outputs[@]}"; then
echo "${BOARD}: up to date"
exit 0
fi
fi
packages=(python3 "${PWD}/scripts/package-state.py")
"${packages[@]}" prepare --root "${PWD}" --board "${BOARD_DIR}" \
--source "${BUILDROOT_SRC_DIR}" -- "${make_args[@]}"
if [[ "${FORCE:-0}" == 0 && -f "${state_dir}/packages.json" && ! -e "${state_dir}/packages.pending.json" ]]; then
build_tree=$("${state[@]}" build-tree "${BOARD_DIR}")
build_key=$("${state[@]}" key \
--value "inputs=${input_key}" \
--value "packages=${build_tree}" \
--path "${BOARD_DIR}/.config" \
--path "${state_dir}/packages.json")
if "${state[@]}" check "${state_dir}/build.json" --key "${build_key}" "${outputs[@]}"; then
rm -f "${state_dir}/packages.next.json"
echo "${BOARD}: up to date"
exit 0
fi
fi
if [[ "${FORCE:-0}" == 1 ]]; then
echo "${BOARD}: forced build"
else
echo "${BOARD}: inputs, package state or outputs changed; running incremental build"
fi
rm -f "${state_dir}/build.json"
"${packages[@]}" assemble --board "${BOARD_DIR}"
make "${make_args[@]}"
export BINARIES_DIR="${BOARD_DIR}/images" HOST_DIR="${BOARD_DIR}/host"
./scripts/image-manifest.sh
./scripts/image-live-rootfs.sh
./scripts/image-live-media.sh
current_input_key=$("${state[@]}" "${input_args[@]}")
current_configuration_key=$("${state[@]}" key --path "${BOARD_DIR}/.config")
if [[ "${input_key}" != "${current_input_key}" || "${configuration_key}" != "${current_configuration_key}" ]]; then
echo "${BOARD}: inputs changed during the build; not recording an up-to-date result" >&2
exit 1
fi
"${packages[@]}" commit --root "${PWD}" --board "${BOARD_DIR}" \
--source "${BUILDROOT_SRC_DIR}" -- "${make_args[@]}"
build_tree=$("${state[@]}" build-tree "${BOARD_DIR}")
build_key=$("${state[@]}" key \
--value "inputs=${input_key}" \
--value "packages=${build_tree}" \
--path "${BOARD_DIR}/.config" \
--path "${state_dir}/packages.json")
"${state[@]}" save "${state_dir}/configure.json" --key "${configure_key}" --path "${BOARD_DIR}/.config"
"${state[@]}" save "${state_dir}/build.json" --key "${build_key}" "${outputs[@]}"