Debian Codegraph @

mg-static/generate-mg-static

raw
1 #!/bin/sh
2
3 # This Makefile has been written by Han Boetes
4 # <[email protected]> and is released in Public Domain.
5
6 isinpath () {
7 command -v $1 >&/dev/null
8 }
9
10 for i in podman buildah; do
11 if ! isinpath $i; then
12 echo "$0: $i is a required dependency for this script" >&2
13 EXIT=true
14 fi
15 done
16
17 if [ "$EXIT" = true ]; then
18 exit 1
19 fi
20
21 usage() {
22 name=${0##*/}
23 cat << EOF >&2
24 This script can build a static binary for mg, like this:
25 ./$name [amd64|i386|arm64v8|arm32v7] [tag|latest]
26 Make sure your architecture is compatible.
27 EOF
28 exit 1
29 }
30
31 case $1 in
32 amd64)
33 ext=$1
34 ;;
35 arm64v8)
36 ext=$1
37 ;;
38 arm32v7)
39 ext=$1
40 ;;
41 i386)
42 ext=$1
43 ;;
44 *)
45 usage
46 ;;
47 esac
48
49 case $2 in
50 latest)
51 TCMD='date +latest%Y%m%d'
52 ;;
53 tag)
54 TCMD='git tag|tail -n 1'
55 ;;
56 *)
57 usage
58 ;;
59 esac
60
61 # Create the Containerfile
62 cat << EOF > Containerfile
63 FROM docker.io/$ext/alpine
64 RUN apk upgrade
65 RUN apk add git libbsd-static libbsd-dev ncurses-dev musl-dev ncurses-static gcc make
66 RUN git clone https://github.com/hboetes/mg.git
67 WORKDIR mg
68 RUN TAG=\$($TCMD); \
69 git checkout \$TAG; \
70 make STATIC=1; \
71 strip mg; \
72 ln mg mg-\$TAG-static-$ext
73 EOF
74
75
76 cat << EOF > ./helper-script
77 #!/bin/sh
78 mnt=\$(podman image mount localhost/mg-static)
79 cp \$mnt/mg/mg-*static-* .
80 EOF
81 chmod 755 ./helper-script
82
83 podman image rm localhost/mg-static
84 buildah build -f Containerfile -t mg-static
85 podman unshare ./helper-script
86
87 # Clean up the mess
88 rm -f Containerfile helper-script
89 podman image rm localhost/mg-static