Cap both halves of the container build: CARGO_BUILD_JOBS limits how many rustc processes cargo starts, while --cpus limits what the container gets regardless of what nested build scripts spawn — cc, cmake, and ring's asm build all parallelise on their own account and do not consult cargo. Without both, a full cross-compile takes every thread on the host and makes the machine unusable for the length of a background build. Assisted-by: LLM
73 lines
2.4 KiB
Bash
Executable File
73 lines
2.4 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Run a command inside the DarkRoom Android build container.
|
|
#
|
|
# ./docker/android/build.sh cargo ndk -t arm64-v8a build --release
|
|
# ./docker/android/build.sh cargo check --target aarch64-linux-android
|
|
# ./docker/android/build.sh # interactive shell
|
|
#
|
|
# Builds the image on first use. Rebuild after editing the Dockerfile with:
|
|
# ./docker/android/build.sh --rebuild
|
|
set -euo pipefail
|
|
|
|
IMAGE="darkroom-android:latest"
|
|
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
REPO="$(cd "${HERE}/../.." && pwd)"
|
|
|
|
# Prefer podman (rootless by default); fall back to docker.
|
|
if command -v podman >/dev/null 2>&1; then
|
|
ENGINE=podman
|
|
elif command -v docker >/dev/null 2>&1; then
|
|
ENGINE=docker
|
|
else
|
|
echo "error: neither podman nor docker found" >&2
|
|
exit 1
|
|
fi
|
|
|
|
if [[ "${1:-}" == "--rebuild" ]]; then
|
|
shift
|
|
"${ENGINE}" build -t "${IMAGE}" "${HERE}"
|
|
elif ! "${ENGINE}" image exists "${IMAGE}" 2>/dev/null && \
|
|
! "${ENGINE}" image inspect "${IMAGE}" >/dev/null 2>&1; then
|
|
echo "==> building ${IMAGE} (first run; several minutes)"
|
|
"${ENGINE}" build -t "${IMAGE}" "${HERE}"
|
|
fi
|
|
|
|
# Persist the cargo registry and target dir across runs, or every build
|
|
# re-downloads the crate index.
|
|
CACHE="${XDG_CACHE_HOME:-${HOME}/.cache}/darkroom-android"
|
|
mkdir -p "${CACHE}/registry" "${CACHE}/target"
|
|
|
|
ARGS=(
|
|
--rm
|
|
-v "${REPO}:/work:z"
|
|
-v "${CACHE}/registry:/opt/cargo/registry:z"
|
|
-v "${CACHE}/target:/work/target-android:z"
|
|
-e CARGO_TARGET_DIR=/work/target-android
|
|
-w /work
|
|
)
|
|
|
|
# Build parallelism. A full cross-compile will otherwise take every thread on
|
|
# the host and make the machine unusable for the length of the build, which is
|
|
# a poor trade when it is running in the background.
|
|
#
|
|
# Both halves are needed: CARGO_BUILD_JOBS caps how many rustc processes cargo
|
|
# starts, while --cpus caps what the container gets no matter what any nested
|
|
# build script decides to spawn (cc, cmake, and ring's asm build all parallelise
|
|
# on their own account and do not consult cargo).
|
|
JOBS="${DARKROOM_BUILD_JOBS:-8}"
|
|
if [[ "${JOBS}" != "0" ]]; then
|
|
ARGS+=(--cpus "${JOBS}" -e "CARGO_BUILD_JOBS=${JOBS}")
|
|
fi
|
|
|
|
# Rootless podman already maps the host user; docker needs it stated.
|
|
if [[ "${ENGINE}" == "docker" ]]; then
|
|
ARGS+=(--user "$(id -u):$(id -g)")
|
|
fi
|
|
|
|
if [[ $# -eq 0 ]]; then
|
|
ARGS+=(-it)
|
|
set -- /bin/bash
|
|
fi
|
|
|
|
exec "${ENGINE}" run "${ARGS[@]}" "${IMAGE}" "$@"
|