forked from openziti/ziti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linux-build.sh
executable file
·72 lines (65 loc) · 2.38 KB
/
linux-build.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env bash
#
# build the Linux artifacts for amd64, arm, arm64
#
# runs one background job per desired architecture unless there are too few CPUs
#
#
set -o pipefail -e -u
# if no architectures supplied then default list of three
if (( ${#} )); then
typeset -a JOBS=(${@})
else
typeset -a JOBS=(amd64 arm arm64)
fi
# specify the Go template used by Gox to save the artifacts
GOX_OUTPUT="release/{{.Arch}}/{{.OS}}/{{.Dir}}"
# count the number of available CPUs for time-efficient parallelism
PROC_COUNT=$(nproc --all)
# compute the number of processors available for each job, rounded down to integer
PROCS_PER_JOB=$((PROC_COUNT / ${#JOBS[@]}))
# if multiple jobs and at least one processor for each job then background, else foreground with all available CPUs-1 (gox default)
if (( ${#JOBS[@]} > 1 && ${PROCS_PER_JOB} )); then
BACKGROUND="&"
# initialize an associative array in which to map background PIDs to the ARCH being built
typeset -A BUILDS
else
BACKGROUND="" # run normally in foreground
PROCS_PER_JOB=0 # invokes gox default to use all CPUs-1
fi
for ARCH in ${JOBS[@]}; do
GOX_CMD="
gox \
-cgo \
-os=linux \
-arch=${ARCH} \
-output=${GOX_OUTPUT} \
-parallel=${PROCS_PER_JOB} \
./...
"
case ${ARCH} in
amd64) eval ${GOX_CMD} ${BACKGROUND}
(( ${PROCS_PER_JOB} )) && BUILDS[${!}]=${ARCH} # if greater than zero procs per job then map background pid->arch
;;
arm) eval CC=arm-linux-gnueabihf-gcc ${GOX_CMD} ${BACKGROUND}
(( ${PROCS_PER_JOB} )) && BUILDS[${!}]=${ARCH}
;;
arm64) eval CC=aarch64-linux-gnu-gcc ${GOX_CMD} ${BACKGROUND}
(( ${PROCS_PER_JOB} )) && BUILDS[${!}]=${ARCH}
;;
*) echo "ERROR: invalid architecture '${ARCH}', must be one of amd64, arm, arm64" >&2
exit 1
;;
esac
done
# if not background in parallel then exit now with well earned success
[[ -z "${BACKGROUND:-}" ]] || exit 0
# Wait for builds in the background and exit with an error if any fail
EXIT=0
while true; do
# "wait -p" requires BASH >=5.1 which is present in Ubuntu 20.10 and Debian Bullseye
wait -n -p JOB_PID; JOB_RESULT=$?
echo "Building for ${BUILDS[$JOB_PID]} finished with result ${JOB_RESULT}"
(( ${JOB_RESULT} )) && EXIT=1
done
exit ${EXIT}