This repository has been archived by the owner on Aug 6, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcross-build.sh
executable file
·111 lines (92 loc) · 3.14 KB
/
cross-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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env bash
# Adapted from https://gist.github.com/makeworld-the-better-one/e1bb127979ae4195f43aaa3ad46b1097/e8abbae0ce5af35a227ffaeb1c589ac071738fd2
type setopt >/dev/null 2>&1
NOT_ALLOWED_OS="windows js android ios illumos aix"
NOT_ALLOWED_ARCH="mips mips64 mips64le mipsle ppc64 ppc64le riscv64 s390x"
FAILURES=""
BASE_DIR="$(pwd)"
contains() {
# Source: https://stackoverflow.com/a/8063398/7361270
[[ $1 =~ (^|[[:space:]])$2($|[[:space:]]) ]]
}
export GO111MODULES=on
export CGO_ENABLED=0
rm -rf build
mkdir build
# Get all targets
while IFS= read -r target; do
GOOS=${target%/*}
GOARCH=${target#*/}
if contains "$NOT_ALLOWED_OS" "$GOOS" ; then
continue
fi
if contains "$NOT_ALLOWED_ARCH" "$GOARCH" ; then
continue
fi
if [[ $GOOS == "darwin" ]] && [[ $GOARCH = "arm64" ]]; then
continue
fi
for binary in view like add-comment; do
BIN_FILENAME="$binary"
cd "$BASE_DIR/$binary"
# Check for arm and set arm version
if [[ $GOARCH == "arm" ]]; then
# Set what arm versions each platform supports
if [[ $GOOS == "darwin" ]]; then
arms="7"
elif [[ $GOOS == "windows" ]]; then
# This is a guess, it's not clear what Windows supports from the docs
# But I was able to build all these on my machine
arms="5 6 7"
elif [[ $GOOS == *"bsd" ]]; then
arms="6 7"
else
# Linux goes here
arms="5 6 7"
fi
# Now do the arm build
for GOARM in $arms; do
if [[ "${GOOS}" == "windows" ]]; then BIN_FILENAME="${BIN_FILENAME}.exe"; fi
CMD="GOARM=${GOARM} GOOS=${GOOS} GOARCH=${GOARCH} go build -o ${BIN_FILENAME} $@"
echo "${CMD}"
eval "${CMD}"
status=$?
if [ $status -eq 0 ]; then
# Move binarIES
mkdir -p "../build/$GOOS-arm$GOARM"
mv "$BIN_FILENAME" "../build/$GOOS-arm$GOARM"
else
FAILURES="${FAILURES} ${GOOS}/${GOARCH}${GOARM}"
fi
done
continue # Skip the non-arm building done below
fi
# Build non-arm here
if [[ "${GOOS}" == "windows" ]]; then BIN_FILENAME="${BIN_FILENAME}.exe"; fi
CMD="GOOS=${GOOS} GOARCH=${GOARCH} go build -o ${BIN_FILENAME} $@"
echo "${CMD}"
eval "${CMD}"
status=$?
if [ $status -eq 0 ]; then
# Move binary
mkdir -p "../build/$GOOS-$GOARCH"
mv "$BIN_FILENAME" "../build/$GOOS-$GOARCH"
else
FAILURES="${FAILURES} ${GOOS}/${GOARCH}"
fi
done
done <<< "$(go tool dist list)"
# Create .tar.gz of each folder
cd "$BASE_DIR/build"
echo "Creating tar archives..."
for dir in */; do
dir=${dir%*/} # remove the trailing "/"
tar czf "$dir.tar.gz" "$dir"
rm -r "$dir"
done
echo "Done all."
if [[ "${FAILURES}" != "" ]]; then
echo ""
echo "${SCRIPT_NAME} failed on: ${FAILURES}"
exit 1
fi