-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.sh
executable file
·92 lines (78 loc) · 2.78 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
#!/bin/bash
# Copyright 2021 RELEX Oy
# Licensed 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 CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Environments:
# CGO_ENABLED: enable CGO or not; default 0
# GO_LDFLAGS: to be passed in -ldflags
#
# Command-line arguments:
# passed to go build
set -o pipefail
GOPATH=$(go env GOPATH) || exit 1
CGO_ENABLED=${CGO_ENABLED:-0}
test -d BUILD || { echo "missing BUILD dir"; exit 1; }
# Generate and check go inline reports by special "xx:inline" comment
# e.g. func (s LogSchema) GetFieldName(index int) string { // xx:inline
INLINE_REPORT_FILE="BUILD/go-inline.txt"
truncate -s0 $INLINE_REPORT_FILE
# Fix inconsistent filepaths
normalize() {
# e.g. /home/guest/github/relex/slog-agent/util/slices.go:8:6: can inline util.CopySlice[go.shape.uint8_0]
# e.g. ./main.go:16:11: inlining call to rand.Seed
cat | sed "s+^\./++g" | sed "s+^$PWD/++g" | grep -E -v '^<autogenerated>'
return 0
}
collect_inline_hints() {
# e.g. base/logschema.go:84:6: can inline LogSchema.GetFieldName
# e.g. main.go:16:11: inlining call to rand.Seed
#
cat | grep -E ': can inline ' > $INLINE_REPORT_FILE
return 0
}
hide_known_hints() {
cat | grep -E -v '(can inline|devirtualizing|escapes|ignoring self-assignment|index bounds check|inlining call|leaking param|moved to heap|not escape)' | grep -E -v '^# '
return 0
}
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
if [[ $OS == "darwin" ]]
then
GO_LDFLAGS_FULL="$GO_LDFLAGS" # "-static" is not supported on OS/X when linker is used
else
GO_LDFLAGS_FULL="$GO_LDFLAGS -extldflags -static"
fi
GO111MODULE=on CGO_ENABLED=$CGO_ENABLED go build -gcflags "./...=-m -l=4" -ldflags "$GO_LDFLAGS_FULL" -o BUILD "$@" 2>&1 | normalize | tee >(collect_inline_hints) | hide_known_hints
GOBUILD_EXIT=$?
if [[ "$GOBUILD_EXIT" != "0" ]]
then
exit $GOBUILD_EXIT
fi
INLINE_FAIL=0
IFS=$'\n'
for LINE in $(find . -name "*.go" -exec grep -Hn "// xx:inline" '{}' ';')
do
# e.g. ./base/logschema.go:84:func (s *xLogSchema) GetFieldName(index int) string { // xx:inline
LINE=${LINE#./}
LINE=${LINE%(*}
LHEADER=${LINE%:*}
if $(grep -q "^$LHEADER" $INLINE_REPORT_FILE)
then
echo $LINE is inlined
else
echo -e $LINE is NOT inlined "\t<<<<<<<<<< <<<<<<<<<< <<<<<<<<<<" >&2
INLINE_FAIL=1
fi
done
if [[ $INLINE_FAIL != "0" ]]
then
exit 1
fi