diff options
author | Russ Cox <rsc@golang.org> | 2012-06-13 16:24:54 -0400 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2012-06-13 16:24:54 -0400 |
commit | 2c396a8f4af5d6ef031bd6627a780b2e85733357 (patch) | |
tree | d621d93e5c817e3cd09445f352370d0135d17e4f | |
parent | 5169191e96002811775c1badc621446fd833a964 (diff) | |
download | go-2c396a8f4af5d6ef031bd6627a780b2e85733357.tar.gz go-2c396a8f4af5d6ef031bd6627a780b2e85733357.zip |
[release-branch.go1] cmd/cgo: make Go code order deterministic
««« backport 319a9f3330d0
cmd/cgo: make Go code order deterministic
The type declarations were being generated using
a range over a map, which meant that successive
runs produced different orders. This will make sure
successive runs produce the same files.
Fixes #3707.
R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/6300062
»»»
-rw-r--r-- | src/cmd/cgo/main.go | 16 | ||||
-rw-r--r-- | src/cmd/cgo/out.go | 12 |
2 files changed, 20 insertions, 8 deletions
diff --git a/src/cmd/cgo/main.go b/src/cmd/cgo/main.go index 7449f04c4c..60165961a1 100644 --- a/src/cmd/cgo/main.go +++ b/src/cmd/cgo/main.go @@ -22,6 +22,7 @@ import ( "path/filepath" "reflect" "runtime" + "sort" "strings" ) @@ -33,9 +34,8 @@ type Package struct { GccOptions []string CgoFlags map[string]string // #cgo flags (CFLAGS, LDFLAGS) Written map[string]bool - Name map[string]*Name // accumulated Name from Files - Typedef map[string]ast.Expr // accumulated Typedef from Files - ExpFunc []*ExpFunc // accumulated ExpFunc from Files + Name map[string]*Name // accumulated Name from Files + ExpFunc []*ExpFunc // accumulated ExpFunc from Files Decl []ast.Decl GoFiles []string // list of Go files GccFiles []string // list of gcc output files @@ -51,7 +51,15 @@ type File struct { Ref []*Ref // all references to C.xxx in AST ExpFunc []*ExpFunc // exported functions for this file Name map[string]*Name // map from Go name to Name - Typedef map[string]ast.Expr // translations of all necessary types from C +} + +func nameKeys(m map[string]*Name) []string { + var ks []string + for k := range m { + ks = append(ks, k) + } + sort.Strings(ks) + return ks } // A Ref refers to an expression of the form C.xxx in the AST. diff --git a/src/cmd/cgo/out.go b/src/cmd/cgo/out.go index acd1e0b438..5dfc16a020 100644 --- a/src/cmd/cgo/out.go +++ b/src/cmd/cgo/out.go @@ -71,7 +71,8 @@ func (p *Package) writeDefs() { } cVars := make(map[string]bool) - for _, n := range p.Name { + for _, key := range nameKeys(p.Name) { + n := p.Name[key] if n.Kind != "var" { continue } @@ -94,14 +95,16 @@ func (p *Package) writeDefs() { } fmt.Fprintf(fc, "\n") - for _, n := range p.Name { + for _, key := range nameKeys(p.Name) { + n := p.Name[key] if n.Const != "" { fmt.Fprintf(fgo2, "const _Cconst_%s = %s\n", n.Go, n.Const) } } fmt.Fprintf(fgo2, "\n") - for _, n := range p.Name { + for _, key := range nameKeys(p.Name) { + n := p.Name[key] if n.FuncType != nil { p.writeDefsFunc(fc, fgo2, n) } @@ -372,7 +375,8 @@ func (p *Package) writeOutput(f *File, srcfile string) { fmt.Fprintf(fgcc, "%s\n", f.Preamble) fmt.Fprintf(fgcc, "%s\n", gccProlog) - for _, n := range f.Name { + for _, key := range nameKeys(f.Name) { + n := f.Name[key] if n.FuncType != nil { p.writeOutputFunc(fgcc, n) } |