...
1env GO111MODULE=off
2
3[!compiler:gc] skip 'using -gcflags and -ldflags'
4[short] skip
5
6# -gcflags=-e applies to named packages, not dependencies
7go build -a -n -v -gcflags=-e z1 z2
8stderr 'compile.* -p z1.* -e '
9stderr 'compile.* -p z2.* -e '
10stderr 'compile.* -p y'
11! stderr 'compile.* -p [^z].* -e '
12
13# -gcflags can specify package=flags, and can be repeated; last match wins
14go build -a -n -v -gcflags=-e -gcflags=z1=-N z1 z2
15stderr 'compile.* -p z1.* -N '
16! stderr 'compile.* -p z1.* -e '
17! stderr 'compile.* -p z2.* -N '
18stderr 'compile.* -p z2.* -e '
19stderr 'compile.* -p y'
20! stderr 'compile.* -p [^z].* -e '
21! stderr 'compile.* -p [^z].* -N '
22
23# -gcflags can have arbitrary spaces around the flags
24go build -a -n -v -gcflags=' z1 = -e ' z1
25stderr 'compile.* -p z1.* -e '
26
27# -gcflags='all=-e' should apply to all packages, even with go test
28go test -a -c -n -gcflags='all=-e' z1
29stderr 'compile.* -p z3.* -e '
30
31# this particular -gcflags argument made the compiler crash
32! go build -gcflags=-d=ssa/ z1
33stderr 'PhaseOptions usage'
34
35# check for valid -ldflags parameter
36! go build '-ldflags="-X main.X=Hello"'
37stderr 'invalid value'
38
39# -ldflags for implicit test package applies to test binary
40go test -a -c -n -gcflags=-N -ldflags=-X=x.y=z z1
41stderr 'compile.* -N .*z_test.go'
42stderr 'link.* -X=x.y=z'
43
44# -ldflags for explicit test package applies to test binary
45go test -a -c -n -gcflags=z1=-N -ldflags=z1=-X=x.y=z z1
46stderr 'compile.* -N .*z_test.go'
47stderr 'link.* -X=x.y=z'
48
49# -ldflags applies to link of command
50go build -a -n -ldflags=-X=math.pi=3 my/cmd/prog
51stderr 'link.* -X=math.pi=3'
52
53# -ldflags applies to link of command even with strange directory name
54go build -a -n -ldflags=-X=math.pi=3 my/cmd/prog/
55stderr 'link.* -X=math.pi=3'
56
57# -ldflags applies to current directory
58cd my/cmd/prog
59go build -a -n -ldflags=-X=math.pi=3
60stderr 'link.* -X=math.pi=3'
61
62# -ldflags applies to current directory even if GOPATH is funny
63[!case-sensitive] cd $WORK/GoPath/src/my/cmd/prog
64go build -a -n -ldflags=-X=math.pi=3
65stderr 'link.* -X=math.pi=3'
66
67# cgo.a should not be a dependency of internally-linked go package
68go build -ldflags='-linkmode=external -linkmode=internal' -n prog.go
69! stderr 'packagefile .*runtime/cgo.a'
70
71-- z1/z.go --
72package z1
73import _ "y"
74import _ "z2"
75
76-- z1/z_test.go --
77package z1_test
78import "testing"
79import _ "z3"
80func Test(t *testing.T) {}
81
82-- z2/z.go --
83package z2
84
85-- z3/z.go --
86package z3
87
88-- y/y.go --
89package y
90
91-- my/cmd/prog/prog.go --
92package main
93func main() {}
View as plain text