-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.sh
executable file
·157 lines (131 loc) · 5.47 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/bin/sh -e
# Copyright 2021 Adobe. All rights reserved.
# This file is licensed to you under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. You may obtain a copy
# of the License at http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software distributed under
# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
# OF ANY KIND, either express or implied. See the License for the specific language
# governing permissions and limitations under the License.
MAXJOBS=$1
[ "x$MAXJOBS" != 'x' ] || MAXJOBS=4
root_dir=$(cd "$(dirname "$0")"; echo "$PWD")
echo "ROOT DIR: ${root_dir}"
cd "${root_dir}"
# Disabling cgo in order to not link with libc and utilize static linkage binaries
# which will help to not relay on glibc on linux and be truely independend from OS
export CGO_ENABLED=0
echo "--- GENERATE CODE FOR AQUARIUM-FISH ---"
# Install oapi-codegen if it's not available or version is not the same with go.mod
gopath=$(go env GOPATH)
req_ver=$(grep -F 'github.com/oapi-codegen/oapi-codegen/v2' go.mod | cut -d' ' -f 2)
curr_ver="$(PATH="$gopath/bin:$PATH" oapi-codegen --version 2>/dev/null | tail -1 || true)"
if [ "$curr_ver" != "$req_ver" ]; then
go install "github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen@$req_ver"
fi
# Cleanup the old generated files
find ./lib -name '*.gen.go' -delete
# Run code generation
PATH="$gopath/bin:$PATH" go generate -v ./lib/...
# Making LabelDefinitions an actual type to attach GORM-needed Scanner/Valuer functions to it to
# make the array a json document and store in the DB row as one item
# TODO: https://github.com/deepmap/oapi-codegen/issues/859
sed -i.bak 's/^type LabelDefinitions = /type LabelDefinitions /' lib/openapi/types/types.gen.go
rm -f lib/openapi/types/types.gen.go.bak
# If ONLYGEN is specified - skip the build
[ -z "$ONLYGEN" ] || exit 0
# Prepare version number as overrides during link
mod_name=$(grep '^module' "${root_dir}/go.mod" | cut -d' ' -f 2)
git_version="$(git describe --tags --match 'v*')$([ "$(git diff)" = '' ] || echo '-dirty')"
version_flags="-X '$mod_name/lib/build.Version=${git_version}' -X '$mod_name/lib/build.Time=$(date -u +%y%m%d.%H%M%S)'"
BINARY_NAME="aquarium-fish-$git_version"
# Doing check after generation because generated sources requires additional modules
./check.sh
echo
echo "--- RUN UNIT TESTS ---"
go test -ldflags="$version_flags" -v ./lib/...
echo
echo "--- BUILD ${BINARY_NAME} ($MAXJOBS in parallel) ---"
if [ "x${RELEASE}" != "x" ]; then
export GIN_MODE=release
os_list='linux darwin windows freebsd openbsd'
arch_list='amd64 arm64'
else
echo "--- WARNING: build DEBUG mode ---"
os_list="$(go env GOOS)"
arch_list="$(go env GOARCH)"
fi
# Run parallel builds but no more than limit (gox doesn't support all the os/archs we need)
pwait() {
# Note: Dash really don't like jobs to be executed in a pipe or in other shell, soooo...
# Using "jobs -p" to show only PIDs (because it could be multiline)
# Unfortunately "jobs -r" is not supported in dash, not a big problem with sleep for 1 sec
while jobs -p > /tmp/jobs_list.tmp; do
# Cleanup jobs list, otherwise "jobs -p" will stay the same forever
jobs > /dev/null 2>&1
[ $(cat /tmp/jobs_list.tmp | wc -l) -ge $1 ] || break
sleep 1
done
}
# If use it directly - it causes "go tool dist: signal: broken pipe"
go tool dist list > /tmp/go_tool_dist_list.txt
for GOOS in $os_list; do
for GOARCH in $arch_list; do
name="$BINARY_NAME.${GOOS}_${GOARCH}"
if ! grep -q "^${GOOS}/${GOARCH}$" /tmp/go_tool_dist_list.txt; then
echo "Skipping: $name as not supported by go"
continue
fi
echo "Building: $name ..."
rm -f "$name" "$name.log" "$name.zip" "$name.tar.xz"
GOOS=$GOOS GOARCH=$GOARCH go build -ldflags="-s -w $version_flags" -o "$name" ./cmd/fish > "$name.log" 2>&1 &
pwait $MAXJOBS
done
done
wait
# Check build logs for errors
errorcount=0
for GOOS in $os_list; do
for GOARCH in $arch_list; do
name="$BINARY_NAME.${GOOS}_${GOARCH}"
# Log file is not here - build was skipped
[ -f "$name.log" ] || continue
# Binary is not here - build error happened
if [ ! -f "$name" ]; then
echo
echo "--- ERROR: $name ---"
cat "$name.log"
errorcount=$(($errorcount+1))
elif [ -s "$name.log" ]; then
echo
echo "--- WARNING: $name ---"
cat "$name.log"
fi
rm -f "$name.log"
done
done
[ $errorcount -eq 0 ] || exit $errorcount
if [ "x${RELEASE}" != "x" ]; then
echo
echo "--- ARCHIVE ${BINARY_NAME} ($MAXJOBS in parallel) ---"
# Pack the artifact archives
for GOOS in $os_list; do
for GOARCH in $arch_list; do
name="$BINARY_NAME.${GOOS}_${GOARCH}"
[ -f "$name" ] || continue
echo "Archiving: $(du -h "$name") ..."
mkdir "$name.dir"
bin_name='aquarium-fish'
[ "$GOOS" != "windows" ] || bin_name="$bin_name.exe"
cp -a "$name" "$name.dir/$bin_name"
$(
cd "$name.dir"
tar -cJf "../$name.tar.xz" "$bin_name" >/dev/null 2>&1
zip "../$name.zip" "$bin_name" >/dev/null 2>&1
cd .. && rm -rf "$name.dir"
) &
pwait $MAXJOBS
done
done
wait
fi