-
Notifications
You must be signed in to change notification settings - Fork 127
/
build.sh
65 lines (51 loc) · 1.59 KB
/
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
#!/usr/bin/env bash
set -e
# Environment variable options:
# - PLATFORMS: Platforms to build for (e.g. "windows/amd64,linux/amd64,darwin/amd64")
export CLI_VERSION=$(git describe --tags 2>/dev/null || git rev-parse --short HEAD)
export LC_ALL=C
export LC_DATE=C
make_ldflags() {
local ldflags="-s -w -X 'github.com/yomorun/yomo/cli.Version=$CLI_VERSION'"
echo "$ldflags"
}
build_for_platform() {
local platform="$1"
local ldflags="$2"
local GOOS="${platform%/*}"
local GOARCH="${platform#*/}"
if [[ -z "$GOOS" || -z "$GOARCH" ]]; then
echo "Invalid platform $platform" >&2
return 1
fi
echo "Building $GOOS/$GOARCH"
local output="yomo"
if [[ "$GOOS" = "windows" ]]; then
output="$output.exe"
fi
# compress to .tar.gz file
local binfile="build/yomo-$GOARCH-$GOOS.tar.gz"
local exit_val=0
CGO_ENABLED=0 GOOS=$GOOS GOARCH=$GOARCH go build -o "build/$output" -ldflags "$ldflags" -trimpath ./cmd/yomo/main.go || exit_val=$?
# compress compiled binary to .tar.gz
# zip -r -j "$binfile" "$output"
tar -C build -czvf "$binfile" "$output"
rm -rf $output
if [[ "$exit_val" -ne 0 ]]; then
echo "Error: failed to build $GOOS/$GOARCH" >&2
return $exit_val
fi
}
if [ -z "$PLATFORMS" ]; then
PLATFORMS="$(go env GOOS)/$(go env GOARCH)"
fi
platforms=(${PLATFORMS//,/ })
ldflags="$(make_ldflags)"
mkdir -p build
rm -rf build/*
echo "Starting build..."
for platform in "${platforms[@]}"; do
build_for_platform "$platform" "$ldflags"
done
echo "Build complete."
ls -lh build/ | awk '{print $9, $5}'