]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: pass -gcflags after other flags generated by the go command
authorCuong Manh Le <cuong.manhle.vn@gmail.com>
Wed, 25 Aug 2021 07:56:01 +0000 (14:56 +0700)
committerCuong Manh Le <cuong.manhle.vn@gmail.com>
Thu, 26 Aug 2021 02:06:43 +0000 (02:06 +0000)
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 <cuong.manhle.vn@gmail.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
src/cmd/go/internal/work/gc.go
src/cmd/go/testdata/script/build_gcflags_order.txt [new file with mode: 0644]
src/cmd/go/testdata/script/build_runtime_gcflags.txt
src/cmd/go/testdata/script/gcflags_patterns.txt

index eee8adca94d8bca54436324922ad99c957422034..1cce5d4dd5d3be34b157c2dcb43eebca75e4d848 100644 (file)
@@ -75,7 +75,7 @@ func (gcToolchain) gc(b *Builder, a *Action, archive string, importcfg, embedcfg
        }
 
        pkgpath := pkgPath(a)
-       gcargs := []string{"-p", pkgpath}
+       gcflags := []string{"-p", pkgpath}
        if p.Module != nil {
                v := p.Module.GoVersion
                if v == "" {
@@ -94,11 +94,11 @@ func (gcToolchain) gc(b *Builder, a *Action, archive string, importcfg, embedcfg
                        v = "1.16"
                }
                if allowedVersion(v) {
-                       gcargs = append(gcargs, "-lang=go"+v)
+                       gcflags = append(gcflags, "-lang=go"+v)
                }
        }
        if p.Standard {
-               gcargs = append(gcargs, "-std")
+               gcflags = append(gcflags, "-std")
        }
        _, compilingRuntime := runtimePackages[p.ImportPath]
        compilingRuntime = compilingRuntime && p.Standard
@@ -106,7 +106,7 @@ func (gcToolchain) gc(b *Builder, a *Action, archive string, importcfg, embedcfg
                // runtime compiles with a special gc flag to check for
                // memory allocations that are invalid in the runtime package,
                // and to implement some special compiler pragmas.
-               gcargs = append(gcargs, "-+")
+               gcflags = append(gcflags, "-+")
        }
 
        // 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
                }
        }
        if extFiles == 0 {
-               gcargs = append(gcargs, "-complete")
+               gcflags = append(gcflags, "-complete")
        }
        if cfg.BuildContext.InstallSuffix != "" {
-               gcargs = append(gcargs, "-installsuffix", cfg.BuildContext.InstallSuffix)
+               gcflags = append(gcflags, "-installsuffix", cfg.BuildContext.InstallSuffix)
        }
        if a.buildID != "" {
-               gcargs = append(gcargs, "-buildid", a.buildID)
+               gcflags = append(gcflags, "-buildid", a.buildID)
        }
        if p.Internal.OmitDebug || cfg.Goos == "plan9" || cfg.Goarch == "wasm" {
-               gcargs = append(gcargs, "-dwarf=false")
+               gcflags = append(gcflags, "-dwarf=false")
        }
        if strings.HasPrefix(runtimeVersion, "go1") && !strings.Contains(os.Args[0], "go_bootstrap") {
-               gcargs = append(gcargs, "-goversion", runtimeVersion)
+               gcflags = append(gcflags, "-goversion", runtimeVersion)
        }
        if symabis != "" {
-               gcargs = append(gcargs, "-symabis", symabis)
+               gcflags = append(gcflags, "-symabis", symabis)
        }
 
-       gcflags := str.StringList(forcedGcflags, p.Internal.Gcflags)
+       gcflags = append(gcflags, str.StringList(forcedGcflags, p.Internal.Gcflags)...)
        if compilingRuntime {
                // Remove -N, if present.
                // 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
                }
        }
 
-       args := []interface{}{cfg.BuildToolexec, base.Tool("compile"), "-o", ofile, "-trimpath", a.trimpath(), gcflags, gcargs}
+       args := []interface{}{cfg.BuildToolexec, base.Tool("compile"), "-o", ofile, "-trimpath", a.trimpath(), gcflags}
        if p.Internal.LocalPrefix != "" {
                // Workaround #43883.
                args = append(args, "-D", p.Internal.LocalPrefix)
diff --git a/src/cmd/go/testdata/script/build_gcflags_order.txt b/src/cmd/go/testdata/script/build_gcflags_order.txt
new file mode 100644 (file)
index 0000000..0ffe157
--- /dev/null
@@ -0,0 +1,20 @@
+# Tests golang.org/issue/47682
+# Flags specified with -gcflags should appear after other flags generated by cmd/go.
+
+cd m
+go build -n -gcflags=-lang=go1.17
+stderr ' -lang=go1.16.* -lang=go1.17'
+
+-- m/go.mod --
+module example.com
+
+go 1.16
+
+-- m/main.go --
+package main
+
+func main() {
+    var s = []int{1, 2, 3}
+    var pa = (*[2]int)(s[1:])
+    println(pa[1])
+}
index da1b65f06c849788fbf7b7b73a50ee0f40979892..c87e480911933a6211fd31991cbf17ec2e75da44 100644 (file)
@@ -8,4 +8,4 @@ mkdir $GOCACHE
 # Verify the standard library (specifically runtime/internal/atomic) can be
 # built with -gcflags when -n is given. See golang.org/issue/29346.
 go build -n -gcflags=all='-l' std
-stderr 'compile.* -l .* runtime/internal/atomic'
+stderr 'compile.* runtime/internal/atomic .* -l'
index f23cecefd3aa30e87ef45d26b15949c2f88d3f28..e9521c2fb27a11142c2474883b873e7077ee95e0 100644 (file)
@@ -7,28 +7,28 @@ env GOCACHE=$WORK/gocache  # Looking for compile commands, so need a clean cache
 
 # -gcflags=-e applies to named packages, not dependencies
 go build -n -v -gcflags=-e z1 z2
-stderr 'compile.* -e.* -p z1'
-stderr 'compile.* -e.* -p z2'
+stderr 'compile.* -p z1.* -e'
+stderr 'compile.* -p z2.* -e'
 stderr 'compile.* -p y'
-! stderr 'compile.* -e.* -p [^z]'
+! stderr 'compile.* -p [^z].* -e'
 
 # -gcflags can specify package=flags, and can be repeated; last match wins
 go build -n -v -gcflags=-e -gcflags=z1=-N z1 z2
-stderr 'compile.* -N.* -p z1'
-! stderr 'compile.* -e.* -p z1'
-! stderr 'compile.* -N.* -p z2'
-stderr 'compile.* -e.* -p z2'
+stderr 'compile.* -p z1.* -N'
+! stderr 'compile.* -p z1.* -e'
+! stderr 'compile.* -p z2.* -N'
+stderr 'compile.* -p z2.* -e'
 stderr 'compile.* -p y'
-! stderr 'compile.* -e.* -p [^z]'
-! stderr 'compile.* -N.* -p [^z]'
+! stderr 'compile.* -p [^z].* -e'
+! stderr 'compile.* -p [^z].* -N'
 
 # -gcflags can have arbitrary spaces around the flags
 go build -n -v -gcflags='  z1 =        -e      ' z1
-stderr 'compile.* -e.* -p z1'
+stderr 'compile.* -p z1.* -e'
 
 # -gcflags='all=-e' should apply to all packages, even with go test
 go test -c -n -gcflags='all=-e' z1
-stderr 'compile.* -e.* -p z3 '
+stderr 'compile.* -p z3.* -e '
 
 # this particular -gcflags argument made the compiler crash
 ! go build -gcflags=-d=ssa/ z1