-
Notifications
You must be signed in to change notification settings - Fork 116
/
build.sh
executable file
·122 lines (102 loc) · 2.65 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
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
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env bash
#
# build.sh compiles dnote binary for target platforms. It is resonsible for creating
# distributable files that can be released by a human or a script.
#
# It can either cross-compile for different platforms using xgo, or simply target a specific
# platform. Set GOOS and GOARCH environment variables to disable xgo and instead
# compile locally for a specific platform.
#
# use:
# ./scripts/build.sh 0.4.8
# GOOS=linux GOARCH=amd64 ./scripts/build.sh 0.4.8
set -ex
dir=$(dirname "${BASH_SOURCE[0]}")
version=$1
projectDir="$dir/../.."
basedir="$projectDir/pkg/cli"
outputDir="$projectDir/build/cli"
command_exists () {
command -v "$1" >/dev/null 2>&1;
}
if ! command_exists shasum; then
echo "please install shasum"
exit 1
fi
if [ $# -eq 0 ]; then
echo "no version specified."
exit 1
fi
if [[ $1 == v* ]]; then
echo "do not prefix version with v"
exit 1
fi
goVersion=go-1.21.x
get_binary_name() {
platform=$1
if [ "$platform" == "windows" ]; then
echo "dnote.exe"
else
echo "dnote"
fi
}
build() {
platform=$1
arch=$2
# native indicates if the compilation is to take place natively on the host platform
# if not true, use xgo with Docker to cross-compile
native=$3
# build binary
destDir="$outputDir/$platform-$arch"
ldflags="-X main.apiEndpoint=https://api.getdnote.com -X main.versionTag=$version"
tags="fts5"
pushd "$projectDir"
mkdir -p "$destDir"
if [ "$native" == true ]; then
GOOS="$platform" GOARCH="$arch" \
go build \
-ldflags "$ldflags" \
--tags "$tags" \
-o="$destDir/cli-$platform-$arch" \
"$basedir"
else
flags=()
if [ "$platform" == "windows" ]; then
flags+=("-buildmode=exe")
fi
xgo \
-go "$goVersion" \
-targets="$platform/$arch" \
-ldflags "$ldflags" \
-dest="$destDir" \
-out="cli" \
"${flags[@]}" \
-tags "$tags" \
-pkg pkg/cli \
.
fi
popd
binaryName=$(get_binary_name "$platform")
mv "$destDir/cli-${platform}-"* "$destDir/$binaryName"
# build tarball
tarballName="dnote_${version}_${platform}_${arch}.tar.gz"
tarballPath="$outputDir/$tarballName"
cp "$projectDir/licenses/GPLv3.txt" "$destDir"
cp "$basedir/README.md" "$destDir"
tar -C "$destDir" -zcvf "$tarballPath" "."
rm -rf "$destDir"
# calculate checksum
pushd "$outputDir"
shasum -a 256 "$tarballName" >> "$outputDir/dnote_${version}_checksums.txt"
popd
}
if [ -z "$GOOS" ] && [ -z "$GOARCH" ]; then
# install the tool
go install src.techknowlogick.com/xgo@latest
build linux amd64
build linux arm64
build darwin amd64
build windows amd64
else
build "$GOOS" "$GOARCH" true
fi