OXYGENHUB
◆ job-waits oxygenhub / scripts / dist.sh
6.0 KiB · Bourne Again Shell (bash) · executable · last changed deb: name the machine's architecture in the apt source 27 days ago source blame history raw
1
#!/usr/bin/env bash
2
# Build release archives of one binary for every platform from Linux:
3
#   Linux x86_64, arm64 and riscv64 (glibc 2.28 and later): .tar.gz and .deb
4
#   macOS x86_64 and arm64: .tar.gz
5
#   Windows x86_64: .zip
6
# plus SHA256SUMS, in target/dist/VERSION.
7
#
8
#   scripts/dist.sh [VERSION]    (default: the package version)
9
#   ONLY=linux-x86_64 scripts/dist.sh   builds one platform (a glob)
10
#
11
# Needs: rustup targets x86_64-pc-windows-gnu, aarch64-unknown-linux-gnu,
12
# riscv64gc-unknown-linux-gnu,
13
# x86_64-apple-darwin and aarch64-apple-darwin; cargo-zigbuild and zig on
14
# PATH (Linux with an old glibc, and macOS without Apple's SDK); the
15
# MinGW-w64 gcc (Windows); dpkg-deb and zip.
16
set -euo pipefail
17
18
PKG=oh                   # the cargo package
19
BIN=oh                   # the binary it builds
20
DEB_NAME=oh              # the Debian package
21
OTHER_DEB=oxy            # the other package sharing the apt repository files
22
SUMMARY="OxygenHub from the command line"
23
DESCRIPTION="oh talks to an OxygenHub (such as https://oxygenhub.io): issues,
24
 proposals and their reviews, releases, Flow runs and checks, from the
25
 terminal. It works alongside oxy, the Oxygen version control client."
26
HOMEPAGE="https://oxygenhub.io/andymccall/oxygenhub"
27
MAINTAINER="Andy McCall <[email protected]>"
28
DOCS=(README.md LICENSE docs/oh.md)
29
30
cd "$(dirname "$0")/.."
31
VERSION="${1:-$(cargo pkgid -p "$PKG" | sed 's/.*[#@]//')}"
32
VERSION="${VERSION#v}"
33
OUT="target/dist/$VERSION"
34
rm -rf "$OUT" && mkdir -p "$OUT"
35
export PATH="$HOME/.local/opt/zig:$PATH"
36
# Stripped, with no debug info: these are for people, not for debugging.
37
export CARGO_PROFILE_RELEASE_DEBUG=0 CARGO_PROFILE_RELEASE_STRIP=true CARGO_INCREMENTAL=0
38
# The version the binary reports.
39
export OXY_BUILD="v$VERSION"
40
41
# target triple | platform name | build tool | Debian architecture
42
PLATFORMS=(
43
  "x86_64-unknown-linux-gnu.2.28|linux-x86_64|zig|amd64"
44
  "aarch64-unknown-linux-gnu.2.28|linux-arm64|zig|arm64"
45
  "riscv64gc-unknown-linux-gnu.2.28|linux-riscv64|zig|riscv64"
46
  "x86_64-apple-darwin|macos-x86_64|zig|"
47
  "aarch64-apple-darwin|macos-arm64|zig|"
48
  "x86_64-pc-windows-gnu|windows-x86_64|cargo|"
49
)
50
51
for p in "${PLATFORMS[@]}"; do
52
  IFS='|' read -r target platform tool deb <<<"$p"
53
  triple="${target%%.2.*}"
54
  # ONLY=linux-x86_64 (a glob) builds just the platforms it matches.
55
  if [ -n "${ONLY:-}" ] && [[ "$platform" != $ONLY ]]; then continue; fi
56
  echo "== $platform ($target)"
57
  if [ "$tool" = zig ]; then
58
    cargo zigbuild -q --release --locked --target "$target" -p "$PKG"
59
  else
60
    cargo build -q --release --locked --target "$target" -p "$PKG"
61
  fi
62
  exe="$BIN"
63
  [[ "$triple" == *windows* ]] && exe="$BIN.exe"
64
  built="target/$triple/release/$exe"
65
  name="$BIN-$VERSION-$platform"
66
  stage="$OUT/stage/$name"
67
  mkdir -p "$stage"
68
  cp "$built" "$stage/"
69
  cp "${DOCS[@]}" "$stage/"
70
  if [[ "$triple" == *windows* ]]; then
71
    (cd "$OUT/stage" && zip -qr "../$name.zip" "$name")
72
  else
73
    tar -C "$OUT/stage" -czf "$OUT/$name.tar.gz" "$name"
74
  fi
75
  if [ -n "$deb" ]; then
76
    root="$OUT/stage/deb-$deb"
77
    mkdir -p "$root/DEBIAN" "$root/usr/bin" "$root/usr/share/doc/$DEB_NAME"
78
    install -m 0755 "$built" "$root/usr/bin/$BIN"
79
    cp README.md docs/oh.md "$root/usr/share/doc/$DEB_NAME/"
80
    cp LICENSE "$root/usr/share/doc/$DEB_NAME/copyright"
81
    # The apt repository at oxygenhub.io (oxygenhub#85): the package ships
82
    # the archive's key and, on its first install, adds the repository, so
83
    # apt upgrade keeps it current. oxy and oh share the files they write.
84
    mkdir -p "$root/usr/share/$DEB_NAME"
85
    install -m 0644 packaging/oxygenhub-archive-keyring.gpg "$root/usr/share/$DEB_NAME/"
86
    sed "s/@PKG@/$DEB_NAME/" >"$root/DEBIAN/postinst" <<'EOF'
87
#!/bin/sh
88
set -e
89
if [ "$1" = configure ]; then
90
  # The key is refreshed on every install, so a new one reaches everyone.
91
  install -m 0644 /usr/share/@PKG@/oxygenhub-archive-keyring.gpg /usr/share/keyrings/oxygenhub-archive-keyring.gpg
92
  # The repository is added on the first install only: removing it
93
  # keeps it removed.
94
  L=/etc/apt/sources.list.d/oxygenhub.sources
95
  if [ -z "$2" ] && [ ! -e "$L" ] && ! grep -rqs "oxygenhub.io/apt" /etc/apt/sources.list /etc/apt/sources.list.d; then
96
    cat >"$L" <<SRC
97
Types: deb
98
URIs: https://oxygenhub.io/apt
99
Suites: stable
100
Components: main
101
Architectures: $(dpkg --print-architecture)
102
Signed-By: /usr/share/keyrings/oxygenhub-archive-keyring.gpg
103
SRC
104
  fi
105
  # Only this machine's architecture: with foreign ones enabled (i386 on
106
  # most amd64 systems) apt otherwise asks for packages the repository
107
  # has not got, and says so on every update. Sources written before
108
  # this get the line too.
109
  if [ -e "$L" ] && ! grep -q "^Architectures:" "$L"; then
110
    sed -i "/^Components:/a Architectures: $(dpkg --print-architecture)" "$L"
111
  fi
112
fi
113
EOF
114
    sed "s/@OTHER@/$OTHER_DEB/" >"$root/DEBIAN/postrm" <<'EOF'
115
#!/bin/sh
116
set -e
117
if [ "$1" = purge ] && ! dpkg-query -W -f='${Status}' @OTHER@ 2>/dev/null | grep -q "ok installed"; then
118
  rm -f /etc/apt/sources.list.d/oxygenhub.sources /usr/share/keyrings/oxygenhub-archive-keyring.gpg
119
fi
120
EOF
121
    chmod 0755 "$root/DEBIAN/postinst" "$root/DEBIAN/postrm"
122
    cat >"$root/DEBIAN/control" <<EOF
123
Package: $DEB_NAME
124
Version: $VERSION
125
Architecture: $deb
126
Maintainer: $MAINTAINER
127
Depends: libc6 (>= 2.28)
128
Section: vcs
129
Priority: optional
130
Homepage: $HOMEPAGE
131
Installed-Size: $(du -sk "$root/usr" | cut -f1)
132
Description: $SUMMARY
133
 $DESCRIPTION
134
EOF
135
    dpkg-deb --build --root-owner-group -Zxz "$root" "$OUT/${DEB_NAME}_${VERSION}_$deb.deb" >/dev/null
136
  fi
137
done
138
139
rm -rf "$OUT/stage"
140
(cd "$OUT" && shopt -s nullglob && sha256sum -- *.tar.gz *.zip *.deb >SHA256SUMS)
141
# DIST_FLAT=DIR: the same files again under names without the version, for
142
# a pipeline whose artifacts are fixed paths (the OxygenFlow dist job); the
143
# bytes are the release's, so its assets show as built by that job.
144
if [ -n "${DIST_FLAT:-}" ]; then
145
  mkdir -p "$DIST_FLAT"
146
  for f in "$OUT"/*.tar.gz "$OUT"/*.zip "$OUT"/*.deb "$OUT"/SHA256SUMS; do
147
    [ -e "$f" ] || continue
148
    b=$(basename "$f"); b=${b/-$VERSION-/-}; b=${b/_${VERSION}_/_}
149
    cp "$f" "$DIST_FLAT/$b"
150
  done
151
fi
152
echo "== $OUT"
153
ls -l "$OUT"