From: Josh Bleecher Snyder Date: Wed, 22 Feb 2017 08:05:18 +0000 (-0800) Subject: cmd/compile: add -dolinkobj flag X-Git-Tag: go1.9beta1~1459 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=005c77dde89d6a062c021a3ed0e180a6848d82b4;p=gostls13.git cmd/compile: add -dolinkobj flag When set to false, the -dolinkobj flag instructs the compiler not to generate or emit linker information. This is handy when you need the compiler's export data, e.g. for use with go/importer, but you want to avoid the cost of full compilation. This must be used with care, since the resulting files are unusable for linking. This CL interacts with #18369, where adding gcflags and ldflags to buildid has been mooted. On the one hand, adding gcflags would make safe use of this flag easier, since if the full object files were needed, a simple 'go install' would fix it. On the other hand, this would mean that 'go install -gcflags=-dolinkobj=false' would rebuild the object files, although any existing object files would probably suffice. Change-Id: I8dc75ab5a40095c785c1a4d2260aeb63c4d10f73 Reviewed-on: https://go-review.googlesource.com/37384 Run-TryBot: Josh Bleecher Snyder TryBot-Result: Gobot Gobot Reviewed-by: Matthew Dempsky --- diff --git a/src/cmd/compile/internal/gc/go.go b/src/cmd/compile/internal/gc/go.go index 9e5d1843d0..95c1124f9e 100644 --- a/src/cmd/compile/internal/gc/go.go +++ b/src/cmd/compile/internal/gc/go.go @@ -118,6 +118,7 @@ var pragcgobuf string var outfile string var linkobj string +var dolinkobj bool var bout *bio.Writer diff --git a/src/cmd/compile/internal/gc/main.go b/src/cmd/compile/internal/gc/main.go index d1dbe45c71..2b1ae860d6 100644 --- a/src/cmd/compile/internal/gc/main.go +++ b/src/cmd/compile/internal/gc/main.go @@ -181,6 +181,7 @@ func Main() { obj.Flagcount("live", "debug liveness analysis", &debuglive) obj.Flagcount("m", "print optimization decisions", &Debug['m']) flag.BoolVar(&flag_msan, "msan", false, "build code compatible with C/C++ memory sanitizer") + flag.BoolVar(&dolinkobj, "dolinkobj", true, "generate linker-specific objects; if false, some invalid code may compile") flag.BoolVar(&nolocalimports, "nolocalimports", false, "reject local (relative) imports") flag.StringVar(&outfile, "o", "", "write output to `file`") flag.StringVar(&myimportpath, "p", "", "set expected package import `path`") @@ -450,38 +451,40 @@ func Main() { timings.Start("fe", "escapes") escapes(xtop) - // Phase 7: Transform closure bodies to properly reference captured variables. - // This needs to happen before walk, because closures must be transformed - // before walk reaches a call of a closure. - timings.Start("fe", "xclosures") - for _, n := range xtop { - if n.Op == ODCLFUNC && n.Func.Closure != nil { - Curfn = n - transformclosure(n) + if dolinkobj { + // Phase 7: Transform closure bodies to properly reference captured variables. + // This needs to happen before walk, because closures must be transformed + // before walk reaches a call of a closure. + timings.Start("fe", "xclosures") + for _, n := range xtop { + if n.Op == ODCLFUNC && n.Func.Closure != nil { + Curfn = n + transformclosure(n) + } } - } - Curfn = nil + Curfn = nil - // Phase 8: Compile top level functions. - // Don't use range--walk can add functions to xtop. - timings.Start("be", "compilefuncs") - fcount = 0 - for i := 0; i < len(xtop); i++ { - n := xtop[i] - if n.Op == ODCLFUNC { - funccompile(n) - fcount++ + // Phase 8: Compile top level functions. + // Don't use range--walk can add functions to xtop. + timings.Start("be", "compilefuncs") + fcount = 0 + for i := 0; i < len(xtop); i++ { + n := xtop[i] + if n.Op == ODCLFUNC { + funccompile(n) + fcount++ + } } - } - timings.AddEvent(fcount, "funcs") + timings.AddEvent(fcount, "funcs") - if nsavederrors+nerrors == 0 { - fninit(xtop) - } + if nsavederrors+nerrors == 0 { + fninit(xtop) + } - if compiling_runtime { - checknowritebarrierrec() + if compiling_runtime { + checknowritebarrierrec() + } } // Phase 9: Check external declarations. diff --git a/src/cmd/compile/internal/gc/obj.go b/src/cmd/compile/internal/gc/obj.go index 6d5f2aa208..bec5d89d75 100644 --- a/src/cmd/compile/internal/gc/obj.go +++ b/src/cmd/compile/internal/gc/obj.go @@ -41,12 +41,16 @@ const ( ) func dumpobj() { + if !dolinkobj { + dumpobj1(outfile, modeCompilerObj) + return + } if linkobj == "" { dumpobj1(outfile, modeCompilerObj|modeLinkerObj) - } else { - dumpobj1(outfile, modeCompilerObj) - dumpobj1(linkobj, modeLinkerObj) + return } + dumpobj1(outfile, modeCompilerObj) + dumpobj1(linkobj, modeLinkerObj) } func dumpobj1(outfile string, mode int) {