forked from build-trust/ockam
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild
More file actions
executable file
·345 lines (312 loc) · 8.14 KB
/
Copy pathbuild
File metadata and controls
executable file
·345 lines (312 loc) · 8.14 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/usr/bin/env bash
##
## This script is used to build ockam code.
##
## The most common use is to run it with no arguments, for example:
## ./build
##
## This will run the clean, lint, test, release commands in sequence
## and place the resulting binaries in the .build directory.
##
## On Linux and OSX you can run:
## ./build install
##
## This will copy the operating system specific built binary
## from .build/ to /usr/local/bin/ockam
##
## USAGE:
##
## ./build [COMMAND] [ARGS]
##
[[ "${TRACE}" ]] && set -x
# Set Bash Options.
#
# -e option will cause the script to exit immediately when a command fails.
#
# -o pipefail option sets the exit code of a pipeline to that of the rightmost command to exit with a
# non-zero status, or zero if all commands of the pipeline exit successfully.
set -eo pipefail
# The name of this package.
PKG_NAME=ockam
# The version of this package, scraped from ockam.go file.
PKG_VERSION="$(awk -F\" '/version :=/ { print $2; exit }' < ockam.go)"
# The build directory used for this project.
PKG_BUILDDIR=".build"
# Extract and print help
help() {
if [[ "$#" = 0 ]]; then
# Extract all lines in this file that start with a double ##,
# Concatenate them and display as help.
# Lines starting with a single # are not included.
grep "^##" "${BASH_SOURCE[0]}" | sed "s/^##//" | sed "s/^##//"
else
# If an argument is passed, look for help just for that command
# Must start with '## command' and end in '####'
echo -e "\\n"
grep "^##" "${BASH_SOURCE[0]}" | sed -nE "/^## ${1}/,/^####/p" | sed "s/^##//" | sed "s/^##//"
fi
}
# Build and run a tool image that is defined in the Dockerfile, as a stage, in the same directory as this script
# The image that is built is tagged as ockam/tool/TOOLNAME:latest
# The image is run with the current directory mounted at /project
run_tool() {
local interactive
if [ "$1" == "--interactive" ] || [ "$1" == "-i" ]; then
interactive="-it"
shift
fi
# The first argument to this function should be the name of the tool to run
# All remaining arguments are teated as arguments to the image run.
local toolname="$1"
shift
local imagename="ockam/tool/$toolname:latest"
local projectdir="/project"
local o buildkit
# Docker Buildkit improves the proformance of docker image builds.
# Docker Buildkit is enabled by default, set DOCKER_BUILDKIT=0 to disable
buildkit='1'
[ -n "$DOCKER_BUILDKIT" ] && buildkit="$DOCKER_BUILDKIT"
if [[ "$TRACE" ]]; then
DOCKER_BUILDKIT=$buildkit docker build --target "$toolname" --tag "$imagename" .
else
o=$(DOCKER_BUILDKIT=$buildkit docker build --quiet --target "$toolname" --tag "$imagename" . 2>&1) || echo "$o"
fi
case "$toolname" in
go) envs="--env GOOS --env GOARCH";;
*) envs="";;
esac
if [[ "$TRACE" ]] || ! [[ "$OCKAM_TOOL_QUIET" ]] || [[ "$interactive" ]]; then
docker run $envs $interactive --rm --volume "$(pwd):$projectdir" "$imagename" "$@"
else
o=$(docker run $envs --rm --volume "$(pwd):$projectdir" "$imagename" "$@" 2>&1) || echo "$o"
fi
}
# if vendor directory doesn't exist, vendor the dependencies based on go mod.
ensure_vendored() {
if [ ! -d "vendor" ]; then
run_tool go mod vendor
fi
}
# Run linter tools
lint() {
ensure_vendored
OCKAM_TOOL_QUIET=1
if [[ "$#" = 0 ]]; then
# if no args, then run all linters
run_tool eclint
run_tool commitlint
run_tool shellcheck
# run_tool gometalinter
else
for var in "$@"; do
run_tool "$var"
done
fi
OCKAM_TOOL_QUIET=0
}
# If GOOS is not explicitly set try to guess it
guess_goos() {
if [[ -z "$GOOS" ]]; then
uname -s | tr '[:upper:]' '[:lower:]'
else
echo "$GOOS"
fi
}
# If GOARCH is not explicitly set try to guess it
guess_goarch() {
if [[ -z "$GOARCH" ]]; then
#
case $(uname -m) in
amd64|x86_64) echo "amd64";;
i386|i686) echo "386";;
arm) echo "arm";;
*) echo "GOARCH must be set"; exit 1;;
esac
else
echo "$GOARCH"
fi
}
# Compile "cmd/ockam/main.go" to the ockam binary
binary() {
local os arch target
# try to guess GOOS/GOARCH, this could fail.
os=$(guess_goos)
arch=$(guess_goarch)
ensure_vendored
target="$PKG_BUILDDIR/${PKG_NAME}_${PKG_VERSION}_${os}_${arch}"
if [[ "$os" = "windows" ]]; then
target+='.exe'
fi
echo -n "Building $target ... "
# make the build directory exists
mkdir -p "$PKG_BUILDDIR"
# -mod=vendor uses the vendor directory.
# -ldflags="-s -w" strips debug info.
# -o "$target" outputs the binary at the target path
GOOS=$os GOARCH=$arch run_tool go build -mod=vendor -ldflags="-s -w" -o "$target" cmd/ockam/*.go
echo "Done."
}
# Run tests
test() {
run_tool go test -v -mod=vendor -cover ./...
}
# build ockam binaries for multiple GOOS/GOARCH combination
release() {
local platforms=(
"linux/amd64"
"linux/arm64"
"darwin/amd64"
"windows/amd64"
)
for platform in "${platforms[@]}"; do
GOOS="${platform%/*}" GOARCH="${platform#*/}" binary
done
}
# Try to guess the current GOOS/GOARCH and then copy .build/ockam_VERSION_GOOS_GOARCH to
# /usr/local/bin/ockam
install() {
local os arch target
os=$(guess_goos)
arch=$(guess_goarch)
target="$PKG_BUILDDIR/${PKG_NAME}_${PKG_VERSION}_${os}_${arch}"
if [[ -d "/usr/local/bin" ]]; then
cp "$target" "/usr/local/bin/${PKG_NAME}"
else
"Error: install command is only supported if /usr/local/bin directory exists."
exit 1
fi
}
# Remove the /usr/local/bin/ockam file, if present.
uninstall() {
if [[ -d "/usr/local/bin" ]]; then
rm -f "/usr/local/bin/${PKG_NAME}"
else
"Error: uninstall command is only supported if /usr/local/bin directory exists."
exit 1
fi
}
# Cleanup, delete the package build directory
clean() {
rm -rf "$PKG_BUILDDIR"
}
if [[ "$#" == 0 ]]; then
clean && lint && test && release && exit 0
fi
##
## Optionally, you may specify one of the below commands to run.
##
## COMMANDS:
##
case "$1" in
## binary Build the ockam binary for the current GOOS/GOARCH.
##
## If GOOS or GOARCH is not set, this script will try to guess
## using `uname`.
##
## Examples:
## ./build binary
## GOOS=windows GOARCH=amd64 ./build binary
##
####
binary) shift; binary;;
## clean Remove the project build directory.
##
## Examples:
## ./build clean
##
####
clean) shift; clean;;
##
## help [COMMAND] Display help.
##
## Displays all help by default.
##
## Optionally, you may pass in a specific command to see help
## about just that command.
##
## Examples:
## ./build help
## ./build help lint
##
####
help) shift; help "$@";;
## install Copy ockam binary to /usr/local/bin/ockam, for OSX
## or Linux environments.
##
## Examples:
## ./build install
##
####
install) shift; install;;
## lint [LINTERS] Run linters.
##
## Runs all linters by default:
## eclint, commitlint, shellcheck & gometalinter
##
## Optionally, you may pass in exactly which linters to run as
## arguments.
##
## Examples:
## ./build lint
## ./build lint shellcheck gometalinter
##
####
lint) shift; lint "$@";;
## release Build the ockam binaries for release.
##
## Builds for the following GOOS/GOARCH platforms:
## "linux/amd64"
## "linux/arm64"
## "darwin/amd64"
## "windows/amd64"
##
## Examples:
## ./build release
##
####
release) shift; release;;
##
## run [OPTIONS] TOOL [ARGS] Run a build tool.
##
## Build and run a tool image that is defined in the multi-stage
## Dockerfile as a stage
##
## OPTIONS:
## -i | --interactive Run the image in interactive mode
##
## Examples:
## ./build run --interactive go
## ./build run -i go
##
## The above example would build and run the image
## 'ockam/tool/go:latest' based on the Dockerfile
## stage 'go' with '-it' options set for 'docker run'
##
####
run) shift; run_tool "$@";;
## test Run tests.
##
## Examples:
## ./build test
##
####
test) shift; test;;
## uninstall Remove ockam binary from /usr/local/bin/ockam.
##
## Examples:
## ./build uninstall
##
####
uninstall) shift; uninstall;;
# If the command is unknown, print help and exit with exit code 1
*) help; exit 1;;
esac
##
##
## TRACING:
##
## If the TRACE environment variable is truthy turn on tracing using the
## bash -x option. This is useful when debugging this script.
##
## TRACE=1 ./build
##