| 1 | #!/usr/bin/env bash |
| 2 | # |
| 3 | # Build Debian binary packages via dpkg-buildpackage, using |
| 4 | # packaging/debian/debian/ as the packaging directory. |
| 5 | # |
| 6 | # Usage: |
| 7 | # bash scripts/build-deb-packages.sh |
| 8 | # |
| 9 | # Environment variables: |
| 10 | # DIST_DIR Directory to write .deb files (default: <repo>/dist) |
| 11 | # BUILD_MODE "all" = arch:any + arch:all packages (default; use for native builds) |
| 12 | # "any-only" = arch:any packages only (use for secondary architectures |
| 13 | # to avoid duplicating arch:all packages) |
| 14 | # TARGET_ARCH Debian architecture to cross-compile for (e.g. arm64). |
| 15 | # When set, dpkg-buildpackage -a <arch> is used and DEB_BUILD_OPTIONS |
| 16 | # defaults to "noautodbgsym nocheck" (test binaries are compiled for the |
| 17 | # target arch and cannot run on the build host). |
| 18 | # DEB_BUILD_OPTIONS Forwarded to dpkg-buildpackage. If unset, defaults to |
| 19 | # "noautodbgsym" (native) or "noautodbgsym nocheck" (cross). |
| 20 | |
| 21 | set -euo pipefail |
| 22 | |
| 23 | ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 24 | DIST_DIR="${DIST_DIR:-$ROOT_DIR/dist}" |
| 25 | BUILD_MODE="${BUILD_MODE:-all}" |
| 26 | TARGET_ARCH="${TARGET_ARCH:-}" |
| 27 | |
| 28 | main() { |
| 29 | if [ -e "$ROOT_DIR/debian" ]; then |
| 30 | printf 'Error: %s/debian already exists. Remove it before running this script.\n' \ |
| 31 | "$ROOT_DIR" >&2 |
| 32 | exit 1 |
| 33 | fi |
| 34 | |
| 35 | cp -r "$ROOT_DIR/packaging/debian/debian" "$ROOT_DIR/debian" |
| 36 | trap 'rm -rf "$ROOT_DIR/debian"' EXIT |
| 37 | |
| 38 | local args=(-us -uc) |
| 39 | [ -n "$TARGET_ARCH" ] && args+=(-a "$TARGET_ARCH") |
| 40 | case "$BUILD_MODE" in |
| 41 | all) args+=(-b) ;; |
| 42 | any-only) args+=(-B) ;; |
| 43 | *) |
| 44 | printf 'Unknown BUILD_MODE: %s (expected "all" or "any-only")\n' \ |
| 45 | "$BUILD_MODE" >&2 |
| 46 | exit 1 |
| 47 | ;; |
| 48 | esac |
| 49 | |
| 50 | if [ -z "${DEB_BUILD_OPTIONS:-}" ]; then |
| 51 | if [ -n "$TARGET_ARCH" ]; then |
| 52 | export DEB_BUILD_OPTIONS="noautodbgsym nocheck" |
| 53 | else |
| 54 | export DEB_BUILD_OPTIONS="noautodbgsym" |
| 55 | fi |
| 56 | fi |
| 57 | |
| 58 | # Use a temp file as a timestamp marker so we can identify freshly produced |
| 59 | # .deb files in the parent directory without accidentally grabbing stale ones. |
| 60 | local marker |
| 61 | marker="$(mktemp)" |
| 62 | |
| 63 | (cd "$ROOT_DIR" && dpkg-buildpackage "${args[@]}") |
| 64 | |
| 65 | # dpkg-buildpackage writes output into the parent of the source directory. |
| 66 | local out_dir |
| 67 | out_dir="$(dirname "$ROOT_DIR")" |
| 68 | mkdir -p "$DIST_DIR" |
| 69 | find "$out_dir" -maxdepth 1 -name '*.deb' -newer "$marker" \ |
| 70 | -exec mv {} "$DIST_DIR/" \; |
| 71 | find "$out_dir" -maxdepth 1 \( -name '*.changes' -o -name '*.buildinfo' \) \ |
| 72 | -newer "$marker" -delete |
| 73 | rm -f "$marker" |
| 74 | |
| 75 | printf '\nPackages written to %s:\n' "$DIST_DIR" |
| 76 | ls -1 "$DIST_DIR"/*.deb |
| 77 | } |
| 78 | |
| 79 | main "$@" |