From 4e9ed68ee7ef2c80dcfd038c54e937646cc0ece2 Mon Sep 17 00:00:00 2001
From: Ian Lance Taylor <iant@golang.org>
Date: Thu, 20 Jul 2023 11:21:13 -0700
Subject: [PATCH] cmd/go: don't collect package CGOLDFLAGS when using gccgo

They are already collected via cmd/cgo.

The gccgo_link_c test is tweaked to do real linking as with this
change the cgo ldflags are not fully reflected in go build -n output,
since they now only come from the built archive.

This is a backport of https://go.dev/cl/497117 from the main repo.

For golang/go#60287

Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/511675
---
 gcc/go/gofrontend/MERGE                          |  2 +-
 libgo/go/cmd/go/internal/work/gccgo.go           | 10 ----------
 libgo/go/cmd/go/testdata/script/gccgo_link_c.txt |  3 ++-
 3 files changed, 3 insertions(+), 12 deletions(-)

diff --git a/gcc/go/gofrontend/MERGE b/gcc/go/gofrontend/MERGE
index c44cdc2baac7..83ab3e2d64cb 100644
--- a/gcc/go/gofrontend/MERGE
+++ b/gcc/go/gofrontend/MERGE
@@ -1,4 +1,4 @@
-92152c88ea8e2dd9e8c67e91bf4ae5e3edf1b506
+d04b024021bb7dbaa434a6d902bd12beb08e315f
 
 The first line of this file holds the git revision number of the last
 merge done from the gofrontend repository.
diff --git a/libgo/go/cmd/go/internal/work/gccgo.go b/libgo/go/cmd/go/internal/work/gccgo.go
index 1e8250002eec..c1026c71e013 100644
--- a/libgo/go/cmd/go/internal/work/gccgo.go
+++ b/libgo/go/cmd/go/internal/work/gccgo.go
@@ -413,16 +413,9 @@ func (tools gccgoToolchain) link(b *Builder, root *Action, out, importcfg string
 	}
 
 	for _, a := range allactions {
-		// Gather CgoLDFLAGS, but not from standard packages.
-		// The go tool can dig up runtime/cgo from GOROOT and
-		// think that it should use its CgoLDFLAGS, but gccgo
-		// doesn't use runtime/cgo.
 		if a.Package == nil {
 			continue
 		}
-		if !a.Package.Standard {
-			cgoldflags = append(cgoldflags, a.Package.CgoLDFLAGS...)
-		}
 		if len(a.Package.CgoFiles) > 0 {
 			usesCgo = true
 		}
@@ -452,9 +445,6 @@ func (tools gccgoToolchain) link(b *Builder, root *Action, out, importcfg string
 
 	ldflags = append(ldflags, cgoldflags...)
 	ldflags = append(ldflags, envList("CGO_LDFLAGS", "")...)
-	if root.Package != nil {
-		ldflags = append(ldflags, root.Package.CgoLDFLAGS...)
-	}
 	if cfg.Goos != "aix" {
 		ldflags = str.StringList("-Wl,-(", ldflags, "-Wl,-)")
 	}
diff --git a/libgo/go/cmd/go/testdata/script/gccgo_link_c.txt b/libgo/go/cmd/go/testdata/script/gccgo_link_c.txt
index db2a29128b2d..8d67ae2bc7ed 100644
--- a/libgo/go/cmd/go/testdata/script/gccgo_link_c.txt
+++ b/libgo/go/cmd/go/testdata/script/gccgo_link_c.txt
@@ -4,8 +4,9 @@
 [!cgo] skip
 [!exec:gccgo] skip
 
-go build -n -compiler gccgo cgoref
+! go build -x -compiler gccgo cgoref
 stderr 'gccgo.*\-L [^ ]*alibpath \-lalib' # make sure that Go-inline "#cgo LDFLAGS:" ("-L alibpath -lalib") passed to gccgo linking stage
+! stderr 'gccgo.*-lalib.*-lalib' # make sure -lalib is only passed once
 
 -- cgoref/cgoref.go --
 package main
-- 
GitLab