1
0
mirror of https://github.com/golang/go synced 2024-11-18 23:44:43 -07:00

cmd/cgo: only write _cgo_flags for gccgo

We only use it for gccgo. Also only write out LDFLAGS,
as that is all that cmd/go uses.

Fixes #60642

Change-Id: I6ccc419a17a433583d9868dd63aa7ec41c2b22c4
Reviewed-on: https://go-review.googlesource.com/c/go/+/524556
Auto-Submit: Ian Lance Taylor <iant@google.com>
Commit-Queue: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
This commit is contained in:
Ian Lance Taylor 2023-08-30 14:06:45 -07:00 committed by Gopher Robot
parent 51ed3cb702
commit bdccd923e9
4 changed files with 14 additions and 15 deletions

View File

@ -710,7 +710,7 @@ files:
_cgo_export.c # for gcc _cgo_export.c # for gcc
_cgo_export.h # for gcc _cgo_export.h # for gcc
_cgo_main.c # for gcc _cgo_main.c # for gcc
_cgo_flags # for alternative build tools _cgo_flags # for build tool (if -gccgo)
The file x.cgo1.go is a copy of x.go with the import "C" removed and The file x.cgo1.go is a copy of x.go with the import "C" removed and
references to C.xxx replaced with names like _Cfunc_xxx or _Ctype_xxx. references to C.xxx replaced with names like _Cfunc_xxx or _Ctype_xxx.

View File

@ -104,10 +104,8 @@ func (f *File) ProcessCgoDirectives() {
f.Preamble = strings.Join(linesOut, "\n") f.Preamble = strings.Join(linesOut, "\n")
} }
// addToFlag appends args to flag. All flags are later written out onto the // addToFlag appends args to flag.
// _cgo_flags file for the build system to use.
func (p *Package) addToFlag(flag string, args []string) { func (p *Package) addToFlag(flag string, args []string) {
p.CgoFlags[flag] = append(p.CgoFlags[flag], args...)
if flag == "CFLAGS" { if flag == "CFLAGS" {
// We'll also need these when preprocessing for dwarf information. // We'll also need these when preprocessing for dwarf information.
// However, discard any -g options: we need to be able // However, discard any -g options: we need to be able
@ -118,6 +116,9 @@ func (p *Package) addToFlag(flag string, args []string) {
} }
} }
} }
if flag == "LDFLAGS" {
p.LdFlags = append(p.LdFlags, args...)
}
} }
// splitQuoted splits the string s around each instance of one or more consecutive // splitQuoted splits the string s around each instance of one or more consecutive

View File

@ -38,7 +38,7 @@ type Package struct {
IntSize int64 IntSize int64
GccOptions []string GccOptions []string
GccIsClang bool GccIsClang bool
CgoFlags map[string][]string // #cgo flags (CFLAGS, LDFLAGS) LdFlags []string // #cgo LDFLAGS
Written map[string]bool Written map[string]bool
Name map[string]*Name // accumulated Name from Files Name map[string]*Name // accumulated Name from Files
ExpFunc []*ExpFunc // accumulated ExpFunc from Files ExpFunc []*ExpFunc // accumulated ExpFunc from Files
@ -475,7 +475,6 @@ func newPackage(args []string) *Package {
p := &Package{ p := &Package{
PtrSize: ptrSize, PtrSize: ptrSize,
IntSize: intSize, IntSize: intSize,
CgoFlags: make(map[string][]string),
Written: make(map[string]bool), Written: make(map[string]bool),
noCallbacks: make(map[string]bool), noCallbacks: make(map[string]bool),
noEscapes: make(map[string]bool), noEscapes: make(map[string]bool),

View File

@ -45,18 +45,17 @@ func (p *Package) writeDefs() {
var gccgoInit strings.Builder var gccgoInit strings.Builder
fflg := creat(*objDir + "_cgo_flags") if !*gccgo {
for k, v := range p.CgoFlags { for _, arg := range p.LdFlags {
for _, arg := range v { fmt.Fprintf(fgo2, "//go:cgo_ldflag %q\n", arg)
fmt.Fprintf(fflg, "_CGO_%s=%s\n", k, arg)
} }
if k == "LDFLAGS" && !*gccgo { } else {
for _, arg := range v { fflg := creat(*objDir + "_cgo_flags")
fmt.Fprintf(fgo2, "//go:cgo_ldflag %q\n", arg) for _, arg := range p.LdFlags {
} fmt.Fprintf(fflg, "_CGO_LDFLAGS=%s\n", arg)
} }
fflg.Close()
} }
fflg.Close()
// Write C main file for using gcc to resolve imports. // Write C main file for using gcc to resolve imports.
fmt.Fprintf(fm, "#include <stddef.h>\n") // For size_t below. fmt.Fprintf(fm, "#include <stddef.h>\n") // For size_t below.