BlueGit

timfa posprint

Files

posprint / scripts / build-arch.sh

build-arch.sh

build-arch.sh 4.6 KiB Download Raw
1#!/usr/bin/env bash
2# Build an Arch Linux pacman package (.pkg.tar.zst) for posprint.
3set -euo pipefail
4
5ROOT="$(cd "$(dirname "$0")/.." && pwd)"
6cd "$ROOT"
7
8SKIP_TESTS=false
9AS_ROOT=false
10INSTALL=false
11
12for arg in "$@"; do
13 case "$arg" in
14 --skip-tests) SKIP_TESTS=true ;;
15 --asroot) AS_ROOT=true ;;
16 --install) INSTALL=true ;;
17 -h|--help)
18 echo "Usage: $0 [--skip-tests] [--asroot] [--install]"
19 echo "Builds packaging/arch/posprint-<ver>-<rel>-x86_64.pkg.tar.zst"
20 echo " --install install the package with pacman after building"
21 echo "On a TTY, missing build dependencies are offered for automatic install."
22 echo "Without --install, prompts interactively when stdin is a TTY."
23 echo "Uses rustup/cargo on PATH; runs makepkg --nodeps if pacman rust/cargo are absent."
24 exit 0
25 ;;
26 *) echo "Unknown option: $arg"; exit 1 ;;
27 esac
28done
29
30is_interactive() {
31 [[ -t 0 ]]
32}
33
34ask_yes_no() {
35 local prompt="$1"
36 if ! is_interactive; then
37 return 1
38 fi
39 local answer
40 read -r -p "$prompt [y/N] " answer
41 [[ "$answer" =~ ^[Yy]([Ee][Ss])?$ ]]
42}
43
44want_install() {
45 if [[ "$INSTALL" == "true" ]]; then
46 return 0
47 fi
48 if ! is_interactive; then
49 return 1
50 fi
51 ask_yes_no "Install the package now with pacman?"
52}
53
54# Ensure rustup toolchains are visible to makepkg's build().
55if [[ -f "$HOME/.cargo/env" ]]; then
56 # shellcheck source=/dev/null
57 source "$HOME/.cargo/env"
58fi
59
60ensure_makepkg() {
61 if command -v makepkg &>/dev/null; then
62 return 0
63 fi
64
65 echo "makepkg not found (base-devel)."
66 if ask_yes_no "Install base-devel with pacman now?"; then
67 sudo pacman -S --needed --noconfirm base-devel
68 else
69 echo "Install manually: sudo pacman -S base-devel"
70 exit 1
71 fi
72
73 if ! command -v makepkg &>/dev/null; then
74 echo "makepkg still not found after install."
75 exit 1
76 fi
77}
78
79ensure_rust() {
80 if command -v cargo &>/dev/null && command -v rustc &>/dev/null; then
81 return 0
82 fi
83
84 echo "cargo/rustc not found."
85 if ask_yes_no "Install Rust via rustup now?"; then
86 curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
87 # shellcheck source=/dev/null
88 source "$HOME/.cargo/env"
89 elif ask_yes_no "Install pacman rust/cargo packages instead?"; then
90 sudo pacman -S --needed --noconfirm rust
91 else
92 echo "Install via rustup (https://rustup.rs) or: sudo pacman -S rust"
93 exit 1
94 fi
95
96 if ! command -v cargo &>/dev/null || ! command -v rustc &>/dev/null; then
97 echo "Rust install finished but cargo/rustc still not on PATH. Open a new shell or: source \"\$HOME/.cargo/env\""
98 exit 1
99 fi
100}
101
102ensure_arch_build_deps() {
103 if ! command -v pacman &>/dev/null; then
104 return 0
105 fi
106
107 # Runtime/build libs commonly needed for egui + serialport (beyond base-devel).
108 local pkgs=(
109 pkgconf
110 openssl
111 libxkbcommon
112 libxcb
113 dbus
114 wayland
115 libx11
116 fontconfig
117 freetype2
118 libglvnd
119 git
120 rsync
121 )
122 local missing=()
123 local pkg
124 for pkg in "${pkgs[@]}"; do
125 if ! pacman -Q "$pkg" &>/dev/null; then
126 missing+=("$pkg")
127 fi
128 done
129
130 if [[ ${#missing[@]} -eq 0 ]]; then
131 return 0
132 fi
133
134 echo "Missing pacman packages: ${missing[*]}"
135 if ask_yes_no "Install them with pacman now?"; then
136 sudo pacman -S --needed --noconfirm "${missing[@]}"
137 else
138 echo "Install manually, then re-run:"
139 echo " sudo pacman -S --needed ${missing[*]}"
140 exit 1
141 fi
142}
143
144ensure_makepkg
145ensure_rust
146ensure_arch_build_deps
147
148# PKGBUILD lists pacman rust/cargo as makedepends. rustup installs don't register
149# those packages, so skip dep resolution when the toolchain is already on PATH.
150MAKEPKG_NODEPS=false
151if ! pacman -Q rust &>/dev/null || ! pacman -Q cargo &>/dev/null; then
152 echo "Note: pacman rust/cargo not installed; using toolchain on PATH ($(rustc --version))."
153 echo " makepkg will run with --nodeps."
154 MAKEPKG_NODEPS=true
155fi
156
157if [[ "$SKIP_TESTS" != "true" ]]; then
158 cargo test
159fi
160
161PKGVER="$(grep '^version' Cargo.toml | head -1 | sed -E 's/.*"([^"]+)".*/\1/')"
162# makepkg refuses PKGBUILDs with Windows (CRLF) line endings.
163sed -i "s/\r$//; s/^pkgver=.*/pkgver=${PKGVER}/" packaging/arch/PKGBUILD
164
165cd packaging/arch
166MAKEPKG_FLAGS=(-f --noconfirm)
167if [[ "$AS_ROOT" == "true" ]]; then
168 MAKEPKG_FLAGS+=(--asroot)
169fi
170if [[ "$MAKEPKG_NODEPS" == "true" ]]; then
171 MAKEPKG_FLAGS+=(--nodeps)
172fi
173makepkg "${MAKEPKG_FLAGS[@]}"
174
175shopt -s nullglob
176PACKAGES=(posprint-"${PKGVER}"-*-x86_64.pkg.tar.zst)
177shopt -u nullglob
178if [[ ${#PACKAGES[@]} -eq 0 ]]; then
179 echo "No package produced."
180 exit 1
181fi
182
183echo "Arch package:"
184ls -lh "${PACKAGES[@]}"
185
186if want_install; then
187 echo "Installing with pacman..."
188 sudo pacman -U --noconfirm "${PACKAGES[@]}"
189 echo "Installed."
190fi