Repository navigation
Expand file tree
/
Copy pathgo-tool.sh
More file actions
executable file
·139 lines (116 loc) · 3.09 KB
/
Copy pathgo-tool.sh
File metadata and controls
executable file
·139 lines (116 loc) · 3.09 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
#!/usr/bin/env bash
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
TOOL="$(basename "$0" ".sh" | sed -E 's/^go-//g')"
set -e
die() {
echo >&2 "$@"
exit 1
}
RACE="${RACE:-false}"
x_defs=()
x_def_errors=()
while read -r line || [[ -n "$line" ]]; do
if [[ "$line" =~ ^[[:space:]]*$ ]]; then
continue
elif [[ "$line" =~ ^([^[:space:]]+)[[:space:]]+(.*)[[:space:]]*$ ]]; then
var="${BASH_REMATCH[1]}"
def="${BASH_REMATCH[2]}"
eval "status_${var}=$(printf '%q' "$def")"
else
die "Malformed status.sh output line ${line}"
fi
done < <(cd "${SCRIPT_DIR}/.."; ./status.sh)
while read -r line || [[ -n "$line" ]]; do
if [[ "$line" =~ ^[[:space:]]*$ ]]; then
continue
elif [[ "$line" =~ ^([^:]+):([[:digit:]]+):[[:space:]]*(var[[:space:]]+)?([^[:space:]]+)[[:space:]].*//XDef:([^[:space:]]+)[[:space:]]*$ ]]; then
go_file="${BASH_REMATCH[1]}"
go_line="${BASH_REMATCH[2]}"
go_var="${BASH_REMATCH[4]}"
status_var="${BASH_REMATCH[5]}"
varname="status_${status_var}"
[[ -n "${!varname}" ]] || x_def_errors+=(
"Variable ${go_var} defined in ${go_file}:${go_line} references status var ${status_var} that is not part of the status.sh output"
)
go_package="$(cd "${SCRIPT_DIR}/.."; go list -e "./$(dirname "$go_file")")"
x_defs+=(-X "\"${go_package}.${go_var}=${!varname}\"")
fi
done < <(git -C "${SCRIPT_DIR}/.." grep -n '//XDef:' -- '*.go')
if [[ "${#x_def_errors[@]}" -gt 0 ]]; then
printf >&2 "%s\n" "${x_def_errors[@]}"
exit 1
fi
ldflags=("${x_defs[@]}")
if [[ "$DEBUG_BUILD" != "yes" ]]; then
ldflags+=(-s -w)
fi
if [[ "${CGO_ENABLED}" != 0 ]]; then
echo >&2 "CGO_ENABLED is not 0. Compiling with -linkmode=external"
ldflags+=('-linkmode=external')
fi
function invoke_go() {
local tool="${1:?"invoke_go tool argument required"}"
shift
local args=()
local CGO_ENABLED
args+=("-buildvcs=false")
args+=(-ldflags="${ldflags[*]}")
args+=(-tags "$(tr , ' ' <<<"$GOTAGS")")
if [[ "$RACE" == "true" ]]; then
export CGO_ENABLED=1
args+=("-race")
fi
go "$tool" "${args[@]}" "$@"
}
function go_build() (
export GOOS="${GOOS:-${DEFAULT_GOOS}}"
[[ -n "$GOOS" ]] || die "GOOS must be set"
[[ -n "$GOARCH" ]] || die "GOARCH must be set"
dirs=()
for main_srcdir in "$@"; do
if ! [[ "$main_srcdir" =~ ^\.?/ ]]; then
main_srcdir="./$main_srcdir"
fi
dirs+=("$main_srcdir")
done
output="bin/${GOOS}_${GOARCH}"
mkdir -p "$output"
echo >&2 "Compiling Go source in ${dirs[*]} to ${output}"
invoke_go_build -o "$output" "${dirs[@]}"
)
function go_build_file() {
local input_file="$1"
local output_file="$2"
invoke_go_build -o "${output_file}" "${input_file}"
}
function invoke_go_build() {
local gcflags=()
if [[ "$DEBUG_BUILD" == "yes" ]]; then
gcflags+=(-gcflags "all=-N -l")
fi
invoke_go build -trimpath "${gcflags[@]}" "$@"
}
function go_run() (
invoke_go run "$@"
)
function go_test() (
unset GOOS
invoke_go test "$@"
)
case "$TOOL" in
build)
go_build "$@"
;;
build-file)
go_build_file "$1" "$2"
;;
test)
go_test "$@"
;;
run)
go_run "$@"
;;
*)
die "Unknown go tool '${TOOL}'"
;;
esac