Skip to content

Commit a6ff433

Browse files
committed
cmd/go: pass -gcflags after other flags generated by the go command
Otherwise, any gc flags set by user using "-gcflags" won't have effect. Fixes #47682 Change-Id: Icd365577cba1f64f6c7b8320d0c9019de9f062f6 Reviewed-on: https://go-review.googlesource.com/c/go/+/344909 Trust: Cuong Manh Le <[email protected]> Run-TryBot: Cuong Manh Le <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent 4f26202 commit a6ff433

File tree

4 files changed

+44
-24
lines changed

4 files changed

+44
-24
lines changed

src/cmd/go/internal/work/gc.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (gcToolchain) gc(b *Builder, a *Action, archive string, importcfg, embedcfg
7575
}
7676

7777
pkgpath := pkgPath(a)
78-
gcargs := []string{"-p", pkgpath}
78+
gcflags := []string{"-p", pkgpath}
7979
if p.Module != nil {
8080
v := p.Module.GoVersion
8181
if v == "" {
@@ -94,19 +94,19 @@ func (gcToolchain) gc(b *Builder, a *Action, archive string, importcfg, embedcfg
9494
v = "1.16"
9595
}
9696
if allowedVersion(v) {
97-
gcargs = append(gcargs, "-lang=go"+v)
97+
gcflags = append(gcflags, "-lang=go"+v)
9898
}
9999
}
100100
if p.Standard {
101-
gcargs = append(gcargs, "-std")
101+
gcflags = append(gcflags, "-std")
102102
}
103103
_, compilingRuntime := runtimePackages[p.ImportPath]
104104
compilingRuntime = compilingRuntime && p.Standard
105105
if compilingRuntime {
106106
// runtime compiles with a special gc flag to check for
107107
// memory allocations that are invalid in the runtime package,
108108
// and to implement some special compiler pragmas.
109-
gcargs = append(gcargs, "-+")
109+
gcflags = append(gcflags, "-+")
110110
}
111111

112112
// If we're giving the compiler the entire package (no C etc files), tell it that,
@@ -125,25 +125,25 @@ func (gcToolchain) gc(b *Builder, a *Action, archive string, importcfg, embedcfg
125125
}
126126
}
127127
if extFiles == 0 {
128-
gcargs = append(gcargs, "-complete")
128+
gcflags = append(gcflags, "-complete")
129129
}
130130
if cfg.BuildContext.InstallSuffix != "" {
131-
gcargs = append(gcargs, "-installsuffix", cfg.BuildContext.InstallSuffix)
131+
gcflags = append(gcflags, "-installsuffix", cfg.BuildContext.InstallSuffix)
132132
}
133133
if a.buildID != "" {
134-
gcargs = append(gcargs, "-buildid", a.buildID)
134+
gcflags = append(gcflags, "-buildid", a.buildID)
135135
}
136136
if p.Internal.OmitDebug || cfg.Goos == "plan9" || cfg.Goarch == "wasm" {
137-
gcargs = append(gcargs, "-dwarf=false")
137+
gcflags = append(gcflags, "-dwarf=false")
138138
}
139139
if strings.HasPrefix(runtimeVersion, "go1") && !strings.Contains(os.Args[0], "go_bootstrap") {
140-
gcargs = append(gcargs, "-goversion", runtimeVersion)
140+
gcflags = append(gcflags, "-goversion", runtimeVersion)
141141
}
142142
if symabis != "" {
143-
gcargs = append(gcargs, "-symabis", symabis)
143+
gcflags = append(gcflags, "-symabis", symabis)
144144
}
145145

146-
gcflags := str.StringList(forcedGcflags, p.Internal.Gcflags)
146+
gcflags = append(gcflags, str.StringList(forcedGcflags, p.Internal.Gcflags)...)
147147
if compilingRuntime {
148148
// Remove -N, if present.
149149
// It is not possible to build the runtime with no optimizations,
@@ -157,7 +157,7 @@ func (gcToolchain) gc(b *Builder, a *Action, archive string, importcfg, embedcfg
157157
}
158158
}
159159

160-
args := []interface{}{cfg.BuildToolexec, base.Tool("compile"), "-o", ofile, "-trimpath", a.trimpath(), gcflags, gcargs}
160+
args := []interface{}{cfg.BuildToolexec, base.Tool("compile"), "-o", ofile, "-trimpath", a.trimpath(), gcflags}
161161
if p.Internal.LocalPrefix != "" {
162162
// Workaround #43883.
163163
args = append(args, "-D", p.Internal.LocalPrefix)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Tests golang.org/issue/47682
2+
# Flags specified with -gcflags should appear after other flags generated by cmd/go.
3+
4+
cd m
5+
go build -n -gcflags=-lang=go1.17
6+
stderr ' -lang=go1.16.* -lang=go1.17'
7+
8+
-- m/go.mod --
9+
module example.com
10+
11+
go 1.16
12+
13+
-- m/main.go --
14+
package main
15+
16+
func main() {
17+
var s = []int{1, 2, 3}
18+
var pa = (*[2]int)(s[1:])
19+
println(pa[1])
20+
}

src/cmd/go/testdata/script/build_runtime_gcflags.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ mkdir $GOCACHE
88
# Verify the standard library (specifically runtime/internal/atomic) can be
99
# built with -gcflags when -n is given. See golang.org/issue/29346.
1010
go build -n -gcflags=all='-l' std
11-
stderr 'compile.* -l .* runtime/internal/atomic'
11+
stderr 'compile.* runtime/internal/atomic .* -l'

src/cmd/go/testdata/script/gcflags_patterns.txt

+11-11
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,28 @@ env GOCACHE=$WORK/gocache # Looking for compile commands, so need a clean cache
77

88
# -gcflags=-e applies to named packages, not dependencies
99
go build -n -v -gcflags=-e z1 z2
10-
stderr 'compile.* -e.* -p z1'
11-
stderr 'compile.* -e.* -p z2'
10+
stderr 'compile.* -p z1.* -e'
11+
stderr 'compile.* -p z2.* -e'
1212
stderr 'compile.* -p y'
13-
! stderr 'compile.* -e.* -p [^z]'
13+
! stderr 'compile.* -p [^z].* -e'
1414

1515
# -gcflags can specify package=flags, and can be repeated; last match wins
1616
go build -n -v -gcflags=-e -gcflags=z1=-N z1 z2
17-
stderr 'compile.* -N.* -p z1'
18-
! stderr 'compile.* -e.* -p z1'
19-
! stderr 'compile.* -N.* -p z2'
20-
stderr 'compile.* -e.* -p z2'
17+
stderr 'compile.* -p z1.* -N'
18+
! stderr 'compile.* -p z1.* -e'
19+
! stderr 'compile.* -p z2.* -N'
20+
stderr 'compile.* -p z2.* -e'
2121
stderr 'compile.* -p y'
22-
! stderr 'compile.* -e.* -p [^z]'
23-
! stderr 'compile.* -N.* -p [^z]'
22+
! stderr 'compile.* -p [^z].* -e'
23+
! stderr 'compile.* -p [^z].* -N'
2424

2525
# -gcflags can have arbitrary spaces around the flags
2626
go build -n -v -gcflags=' z1 = -e ' z1
27-
stderr 'compile.* -e.* -p z1'
27+
stderr 'compile.* -p z1.* -e'
2828

2929
# -gcflags='all=-e' should apply to all packages, even with go test
3030
go test -c -n -gcflags='all=-e' z1
31-
stderr 'compile.* -e.* -p z3 '
31+
stderr 'compile.* -p z3.* -e '
3232

3333
# this particular -gcflags argument made the compiler crash
3434
! go build -gcflags=-d=ssa/ z1

0 commit comments

Comments
 (0)