public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* libgo patch committed: Make cgo generate consistent results
@ 2017-06-14 13:59 Ian Lance Taylor
  0 siblings, 0 replies; only message in thread
From: Ian Lance Taylor @ 2017-06-14 13:59 UTC (permalink / raw)
  To: gcc-patches, gofrontend-dev

[-- Attachment #1: Type: text/plain, Size: 744 bytes --]

This libgo patch changes the cgo tool to generate consistent results
across different runs.

The go tool will pass -I objdir as one of the flags, where objdir is
the temporary build directory. Remove that from _cgo_flags: we don't
need it, and it will be different each time.

Sort the flags to avoid the unpredictable map iteration order.n

The contents of the flags file matters for gccgo because for a package
that uses cgo, the go tool when building for gccgo will store the
_cgo_flags file in the archive. That means that we want to generate
identical _cgo_flags for every run.

The test for this is the cmd/go testsuite, to follow in a future patch.

Bootstrapped and ran Go testsuite on x86_64-pc-linux-gnu.  Committed
to mainline.

Ian

[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 2645 bytes --]

Index: gcc/go/gofrontend/MERGE
===================================================================
--- gcc/go/gofrontend/MERGE	(revision 249198)
+++ gcc/go/gofrontend/MERGE	(working copy)
@@ -1,4 +1,4 @@
-bc785455a35bfa7d4b0a66781c7c3ef08a24a845
+372e75503c1dc9a38d9978aa6b67631283d5d6dd
 
 The first line of this file holds the git revision number of the last
 merge done from the gofrontend repository.
Index: libgo/go/cmd/cgo/gcc.go
===================================================================
--- libgo/go/cmd/cgo/gcc.go	(revision 249125)
+++ libgo/go/cmd/cgo/gcc.go	(working copy)
@@ -86,11 +86,29 @@ func (f *File) DiscardCgoDirectives() {
 // addToFlag appends args to flag. All flags are later written out onto the
 // _cgo_flags file for the build system to use.
 func (p *Package) addToFlag(flag string, args []string) {
-	p.CgoFlags[flag] = append(p.CgoFlags[flag], args...)
 	if flag == "CFLAGS" {
-		// We'll also need these when preprocessing for dwarf information.
+		// We'll need these when preprocessing for dwarf information.
 		p.GccOptions = append(p.GccOptions, args...)
 	}
+
+	skip := false
+	for i, arg := range args {
+		// The go tool will pass us a -I option pointing to objdir;
+		// we don't need to record that for later, as the objdir
+		// will disappear anyhow.
+		if skip {
+			// Discard argument in "-I objdir" case.
+			skip = false
+		} else if strings.HasPrefix(arg, "-I") && strings.HasPrefix(arg[2:], *objDir) {
+			// This is -Iobjdir. Don't save this argument.
+		} else if arg == "-I" && i+1 < len(args) && strings.HasPrefix(args[i+1], *objDir) {
+			// This is -I objdir. Don't save this argument
+			// or the next one.
+			skip = true
+		} else {
+			p.CgoFlags[flag] = append(p.CgoFlags[flag], arg)
+		}
+	}
 }
 
 // splitQuoted splits the string s around each instance of one or more consecutive
Index: libgo/go/cmd/cgo/out.go
===================================================================
--- libgo/go/cmd/cgo/out.go	(revision 249125)
+++ libgo/go/cmd/cgo/out.go	(working copy)
@@ -40,14 +40,19 @@ func (p *Package) writeDefs() {
 	var gccgoInit bytes.Buffer
 
 	fflg := creat(*objDir + "_cgo_flags")
+	var flags []string
 	for k, v := range p.CgoFlags {
-		fmt.Fprintf(fflg, "_CGO_%s=%s\n", k, strings.Join(v, " "))
+		flags = append(flags, fmt.Sprintf("_CGO_%s=%s", k, strings.Join(v, " ")))
 		if k == "LDFLAGS" && !*gccgo {
 			for _, arg := range v {
 				fmt.Fprintf(fgo2, "//go:cgo_ldflag %q\n", arg)
 			}
 		}
 	}
+	sort.Strings(flags)
+	for _, flag := range flags {
+		fmt.Fprintln(fflg, flag)
+	}
 	fflg.Close()
 
 	// Write C main file for using gcc to resolve imports.

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2017-06-14 13:59 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-06-14 13:59 libgo patch committed: Make cgo generate consistent results Ian Lance Taylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).