FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
stackrox/scripts/go-tool.sh at master · stackrox/stackrox · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
stackrox
/
stackrox
Public
Notifications
You must be signed in to change notification settings
Fork
191
Star
1.3k
Code
Issues
54
Pull requests
604
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
stackrox
/
scripts
/
go-tool.sh
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
159 lines (132 loc) · 3.84 KB
Breadcrumbs
stackrox
/
scripts
/
go-tool.sh
Copy path
File metadata and controls
executable file
·
159 lines (132 loc) · 3.84 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#!
/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
function
invoke_go()
{
local
tool=
"
${1
:?
"
invoke_go tool argument required
"
}
"
shift
local
args=()
local
cgo_ldflags=(
"
${ldflags[@]}
"
)
local
cc_compiler=
"
"
local
cgo_enabled=
"
${CGO_ENABLED
:-
0}
"
args+=(
"
-buildvcs=false
"
)
args+=(-tags
"
$(
tr ,
'
'
<<<
"
$GOTAGS
"
)
"
)
if
[[
"
$RACE
"
==
"
true
"
]]
;
then
echo
>&2
"
RACE==true, forcing CGO_ENABLED=1
"
cgo_enabled=1
args+=(
"
-race
"
)
if
command
-v musl-gcc
&
>
/dev/null
;
then
echo
>&2
"
Using musl-gcc for static linking to avoid GLIBC dependencies
"
cc_compiler=
"
musl-gcc
"
cgo_ldflags+=(
'
-extldflags=-static
'
)
else
echo
>&2
"
musl-gcc not found, using default cc and linker (auto)
"
fi
fi
#
-linkmode=external must be set for all CGO builds so the external linker
#
(gcc or musl-gcc) is used. This check is inside invoke_go rather than at
#
the top level because race builds override cgo_enabled to 1 after the
#
environment is read.
if
[[
"
$cgo_enabled
"
!=
0 ]]
;
then
echo
>&2
"
CGO_ENABLED=
$cgo_enabled
, adding -linkmode=external
"
cgo_ldflags+=(
'
-linkmode=external
'
)
fi
args+=(-ldflags=
"
${cgo_ldflags[*]}
"
)
export
CGO_ENABLED=
"
$cgo_enabled
"
[[
-n
"
$cc_compiler
"
]]
&&
export
CC=
"
$cc_compiler
"
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
Back
|
FazBrowse Home
|
New Git URL