#!/usr/bin/env bash # Run a command inside the DarkRoom Windows cross-build container. # # ./docker/windows/build.sh cargo build --release --target x86_64-pc-windows-gnu -p darkroom-desktop # ./docker/windows/build.sh cargo clippy --target x86_64-pc-windows-gnu -p darkroom-desktop # ./docker/windows/build.sh # interactive shell # # Builds the image on first use. Rebuild after editing the Dockerfile with: # ./docker/windows/build.sh --rebuild set -euo pipefail IMAGE="darkroom-windows: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-windows" # `home` is the container's $HOME: Wine keeps its prefix there for the smoke # tests, and refuses one it does not own — which rules out anything the image # could have created, since the container runs as the host user. mkdir -p "${CACHE}/registry" "${CACHE}/target" "${CACHE}/home" ARGS=( --rm -v "${REPO}:/work:z" -v "${CACHE}/registry:/opt/cargo/registry:z" -v "${CACHE}/target:/work/target-windows:z" -v "${CACHE}/home:/tmp/home:z" -e CARGO_TARGET_DIR=/work/target-windows -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}" "$@"