-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
build-tests.sh
executable file
·86 lines (74 loc) · 2.35 KB
/
build-tests.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
#!/usr/bin/env bash
set -exo pipefail
function build_linux () {
local tags=osusergo,netgo,sqlite_foreign_keys,sqlite_json,static_build
local cc
case $(go env GOARCH) in
amd64)
cc=$(xcc linux x86_64)
;;
arm64)
cc=$(xcc linux aarch64)
tags="$tags,noasm"
;;
*)
>&2 echo Error: Unknown arch $(go env GOARCH)
exit 1
;;
esac
local -r extld="-fno-PIC -static -Wl,-z,stack-size=8388608"
CGO_ENABLED=1 PKG_CONFIG=$(which pkg-config) CC="${cc}" go-test-compile \
-tags "$tags" -o "${1}/" -ldflags "-extldflags '$extld'" ./...
}
function build_mac () {
CGO_ENABLED=1 PKG_CONFIG=$(which pkg-config) CC="$(xcc darwin)" go-test-compile \
-tags sqlite_foreign_keys,sqlite_json -o "${1}/" ./...
}
function build_windows () {
CGO_ENABLED=1 PKG_CONFIG=$(which pkg-config) CC="$(xcc windows)" go-test-compile \
-tags sqlite_foreign_keys,sqlite_json,timetzdata -o "${1}/" ./...
}
function build_test_tools () {
# Copy pre-built gotestsum out of the cross-builder.
local ext=""
if [ "$(go env GOOS)" = windows ]; then
ext=".exe"
fi
cp "/usr/local/bin/gotestsum_$(go env GOOS)_$(go env GOARCH)${ext}" "$1/gotestsum${ext}"
# Build test2json from the installed Go distribution.
CGO_ENABLED=0 go build -o "${1}/" -ldflags="-s -w" cmd/test2json
}
function write_test_metadata () {
# Write version that should be reported in test results.
echo "$(go env GOVERSION) $(go env GOOS)/$(go env GOARCH)" > "${1}/go.version"
# Write list of all packages.
go list ./... > "${1}/tests.list"
}
function main () {
if [[ $# != 1 ]]; then
>&2 echo Usage: $0 '<output-dir>'
exit 1
fi
local -r out_dir="$1"
mkdir -p "$out_dir"
case $(go env GOOS) in
linux)
build_linux "$out_dir"
;;
darwin)
build_mac "$out_dir"
;;
windows)
build_windows "$out_dir"
;;
*)
>&2 echo Error: unknown OS $(go env GOOS)
exit 1
;;
esac
# Build gotestsum and test2json so downstream jobs can use it without needing `go`.
build_test_tools "$out_dir"
# Write other metadata needed for testing.
write_test_metadata "$out_dir"
}
main ${@}