]> Cypherpunks repositories - gostls13.git/commitdiff
build: remove tmp dir names from objects, support GOROOT_FINAL again
authorRuss Cox <rsc@golang.org>
Wed, 16 Apr 2014 00:46:46 +0000 (20:46 -0400)
committerRuss Cox <rsc@golang.org>
Wed, 16 Apr 2014 00:46:46 +0000 (20:46 -0400)
If we compile a generated file stored in a temporary
directory - let's say /tmp/12345/work/x.c - then by default
6c stores the full path and then the pcln table in the
final binary includes the full path. This makes repeated builds
(using different temporary directories) produce different
binaries, even if the inputs are the same.

In the old 'go tool pack', the P flag specified a prefix to remove
from all stored paths (if present), and cmd/go invoked
'go tool pack grcP $WORK' to remove references to the
temporary work directory.

We've changed the build to avoid pack as much as possible,
under the theory that instead of making pack convert from
.6 to .a, the tools should just write the .a directly and save a
round of I/O.

Instead of going back to invoking pack always, define a common
flag -trimpath in the assemblers, C compilers, and Go compilers,
implemented in liblink, and arrange for cmd/go to use the flag.
Then the object files being written out have the shortened paths
from the start.

While we are here, reimplement pcln support for GOROOT_FINAL.
A build in /tmp/go uses GOROOT=/tmp/go, but if GOROOT_FINAL=/usr/local/go
is set, then a source file named /tmp/go/x.go is recorded instead as
/usr/local/go/x.go. We use this so that we can prepare distributions
to be installed in /usr/local/go without actually working in that
directory. The conversion to liblink deleted all the old file name
handling code, including the GOROOT_FINAL translation.
Bring the GOROOT_FINAL translation back.

Before this CL, using GOROOT_FINAL=/goroot make.bash:

        g% strings $(which go) | grep -c $TMPDIR
        6
        g% strings $(which go) | grep -c $GOROOT
        793
        g%

After this CL:

        g% strings $(which go) | grep -c $TMPDIR
        0
        g% strings $(which go) | grep -c $GOROOT
        0
        g%

(The references to $TMPDIR tend to be cgo-generated source files.)

Adding the -trimpath flag to the assemblers required converting
them to the new Go-semantics flag parser. The text in go1.3.html
is copied and adjusted from go1.1.html, which is when we applied
that conversion to the compilers and linkers.

Fixes #6989.

LGTM=iant
R=r, iant
CC=golang-codereviews
https://golang.org/cl/88300045

13 files changed:
doc/go1.3.html
include/link.h
src/cmd/5a/a.h
src/cmd/5a/lex.c
src/cmd/6a/a.h
src/cmd/6a/lex.c
src/cmd/8a/a.h
src/cmd/8a/lex.c
src/cmd/cc/lex.c
src/cmd/gc/lex.c
src/cmd/go/build.go
src/liblink/obj.c
src/liblink/sym.c

index 10073200c473126d8970eb8e9c0844c6104aba11..916ed04d25ae6fd8c4124bec0d702f5cb3e4b52c 100644 (file)
@@ -145,6 +145,19 @@ Finally, the go command now supports packages that import Objective-C
 files (suffixed <code>.m</code>) through cgo.
 </p>
 
+<h3 id="gc_flag">Command-line flag parsing</h3>
+
+<p>
+In the gc tool chain, the assemblers now use the
+same command-line flag parsing rules as the Go flag package, a departure
+from the traditional Unix flag parsing. This may affect scripts that invoke
+the tool directly.
+For example,
+<code>go tool 6a -SDfoo</code> must now be written
+<code>go tool 6a -S -D foo</code>.
+(The same change was made to the compilers and linkers in <a href="/doc/go1.1#gc_flag">Go 1.1</a>.)
+</p>
+
 <h3 id="misc">Miscellany</h3>
 
 <p>
index 9a6fca2ab004f931586ae640395e3437bb81e82c..200a503cce31cef9ee04fc0990ef65259344bdfb 100644 (file)
@@ -360,6 +360,9 @@ struct      Link
        Biobuf* bso;    // for -v flag
        char*   pathname;
        int32   windows;
+       char*   trimpath;
+       char*   goroot;
+       char*   goroot_final;
 
        // hash table of all symbols
        LSym*   hash[LINKHASH];
index 4300dd8621fa9dda58547f33a4d0e0d9c8ae6cc6..bb60fe7de2505ec8ad46b6c43180c71bb06dd68a 100644 (file)
@@ -97,7 +97,7 @@ enum
        Always  = 14,
 };
 
-EXTERN char    debug[256];
+EXTERN int     debug[256];
 EXTERN Sym*    hash[NHASH];
 EXTERN char**  Dlist;
 EXTERN int     nDlist;
index 211f7538b5a3308260dcb6efee74c3a8e312f12e..906eee641af90df60004e4b6478d2c7a1d7912a6 100644 (file)
@@ -57,11 +57,27 @@ Lconv(Fmt *fp)
        return linklinefmt(ctxt, fp);
 }
 
+void
+dodef(char *p)
+{
+       if(nDlist%8 == 0)
+               Dlist = allocn(Dlist, nDlist*sizeof(char *),
+                       8*sizeof(char *));
+       Dlist[nDlist++] = p;
+}
+
+void
+usage(void)
+{
+       print("usage: %ca [options] file.c...\n", thechar);
+       flagprint(1);
+       errorexit();
+}
+
 void
 main(int argc, char *argv[])
 {
        char *p;
-       int c;
 
        thechar = '5';
        thestring = "arm";
@@ -84,49 +100,24 @@ main(int argc, char *argv[])
        cinit();
        outfile = 0;
        setinclude(".");
-       ARGBEGIN {
-       default:
-               c = ARGC();
-               if(c >= 0 && c < sizeof(debug))
-                       debug[c] = 1;
-               break;
-
-       case 'o':
-               outfile = ARGF();
-               break;
-
-       case 'D':
-               p = ARGF();
-               if(p) {
-                       if (nDlist%8 == 0) 
-                               Dlist = allocn(Dlist, nDlist*sizeof(char *), 
-                                       8*sizeof(char *));
-                       Dlist[nDlist++] = p;
-               }
-               break;
-
-       case 'I':
-               p = ARGF();
-               setinclude(p);
-               break;
-
-       case 't':
-               thechar = 't';
-               thestring = "thumb";
-               break;
-
-       case 'S':
-               ctxt->debugasm++;
-               break;
-       } ARGEND
-       if(*argv == 0) {
-               print("usage: %ca [-options] file.s\n", thechar);
-               errorexit();
-       }
+       
+       flagfn1("D", "name[=value]: add #define", dodef);
+       flagfn1("I", "dir: add dir to include path", setinclude);
+       flagcount("S", "print assembly and machine code", &debug['S']);
+       flagcount("m", "debug preprocessor macros", &debug['m']);
+       flagstr("o", "file: set output file", &outfile);
+       flagstr("trimpath", "prefix: remove prefix from recorded source file paths", &ctxt->trimpath);
+
+       flagparse(&argc, &argv, usage);
+       ctxt->debugasm = debug['S'];
+
+       if(argc < 1)
+               usage();
        if(argc > 1){
                print("can't assemble multiple files\n");
                errorexit();
        }
+
        if(assemble(argv[0]))
                errorexit();
        Bflush(&bstdout);
index da12b32986a173049fb2c77c858ac8f981cc8eef..b3fb0bb19f3db7e5b372a9eb79902fc59889ac90 100644 (file)
@@ -109,7 +109,7 @@ enum
        CPREPROC,
 };
 
-EXTERN char    debug[256];
+EXTERN int     debug[256];
 EXTERN Sym*    hash[NHASH];
 EXTERN char**  Dlist;
 EXTERN int     nDlist;
index 2a1c4b8e1fb6b61b0d89f25859a23ac3b02df070..4ebcc175c3518919e4c8edaedc129e037c208320 100644 (file)
@@ -63,13 +63,29 @@ Lconv(Fmt *fp)
        return linklinefmt(ctxt, fp);
 }
 
+void
+dodef(char *p)
+{
+       if(nDlist%8 == 0)
+               Dlist = allocn(Dlist, nDlist*sizeof(char *),
+                       8*sizeof(char *));
+       Dlist[nDlist++] = p;
+}
+
 LinkArch*       thelinkarch = &linkamd64;
 
+void
+usage(void)
+{
+       print("usage: %ca [options] file.c...\n", thechar);
+       flagprint(1);
+       errorexit();
+}
+
 void
 main(int argc, char *argv[])
 {
        char *p;
-       int c;
 
        thechar = '6';
        thestring = "amd64";
@@ -94,45 +110,24 @@ main(int argc, char *argv[])
        cinit();
        outfile = 0;
        setinclude(".");
-       ARGBEGIN {
-       default:
-               c = ARGC();
-               if(c >= 0 && c < sizeof(debug))
-                       debug[c] = 1;
-               break;
-
-       case 'o':
-               outfile = ARGF();
-               break;
-
-       case 'D':
-               p = ARGF();
-               if(p) {
-                       if (nDlist%8 == 0)
-                               Dlist = allocn(Dlist, nDlist*sizeof(char *), 
-                                       8*sizeof(char *));
-                       Dlist[nDlist++] = p;
-               }
-               break;
-
-       case 'I':
-               p = ARGF();
-               setinclude(p);
-               break;
+       
+       flagfn1("D", "name[=value]: add #define", dodef);
+       flagfn1("I", "dir: add dir to include path", setinclude);
+       flagcount("S", "print assembly and machine code", &debug['S']);
+       flagcount("m", "debug preprocessor macros", &debug['m']);
+       flagstr("o", "file: set output file", &outfile);
+       flagstr("trimpath", "prefix: remove prefix from recorded source file paths", &ctxt->trimpath);
 
-       case 'S':
-               ctxt->debugasm++;
-               break;
-       } ARGEND
+       flagparse(&argc, &argv, usage);
+       ctxt->debugasm = debug['S'];
 
-       if(*argv == 0) {
-               print("usage: %ca [-options] file.s\n", thechar);
-               errorexit();
-       }
+       if(argc < 1)
+               usage();
        if(argc > 1){
                print("can't assemble multiple files\n");
                errorexit();
        }
+
        if(assemble(argv[0]))
                errorexit();
        Bflush(&bstdout);
index 8c023c3ec61c05d3ba13dfc70be8cf90a75126b1..adc388ca9f4d0226fa76c8186930738636b6afc9 100644 (file)
@@ -109,7 +109,7 @@ enum
        CPREPROC,
 };
 
-EXTERN char    debug[256];
+EXTERN int     debug[256];
 EXTERN Sym*    hash[NHASH];
 EXTERN char**  Dlist;
 EXTERN int     nDlist;
index 49a105da6acb325268bca27ca71af8317c7f62f7..79a9488e4bfdecb1a82a2e1b35c6b75f888b2969 100644 (file)
@@ -63,11 +63,26 @@ Lconv(Fmt *fp)
        return linklinefmt(ctxt, fp);
 }
 
+void
+dodef(char *p)
+{
+       if(nDlist%8 == 0)
+               Dlist = allocn(Dlist, nDlist*sizeof(char *),
+                       8*sizeof(char *));
+       Dlist[nDlist++] = p;
+}
+
+void
+usage(void)
+{
+       print("usage: %ca [options] file.c...\n", thechar);
+       flagprint(1);
+       errorexit();
+}
 void
 main(int argc, char *argv[])
 {
        char *p;
-       int c;
 
        thechar = '8';
        thestring = "386";
@@ -90,44 +105,24 @@ main(int argc, char *argv[])
        cinit();
        outfile = 0;
        setinclude(".");
-       ARGBEGIN {
-       default:
-               c = ARGC();
-               if(c >= 0 && c < sizeof(debug))
-                       debug[c] = 1;
-               break;
-
-       case 'o':
-               outfile = ARGF();
-               break;
-
-       case 'D':
-               p = ARGF();
-               if(p) {
-                       if (nDlist%8 == 0)
-                               Dlist = allocn(Dlist, nDlist*sizeof(char *), 
-                                       8*sizeof(char *));
-                       Dlist[nDlist++] = p;
-               }
-               break;
-
-       case 'I':
-               p = ARGF();
-               setinclude(p);
-               break;
-
-       case 'S':
-               ctxt->debugasm++;
-               break;
-       } ARGEND
-       if(*argv == 0) {
-               print("usage: %ca [-options] file.s\n", thechar);
-               errorexit();
-       }
+       
+       flagfn1("D", "name[=value]: add #define", dodef);
+       flagfn1("I", "dir: add dir to include path", setinclude);
+       flagcount("S", "print assembly and machine code", &debug['S']);
+       flagcount("m", "debug preprocessor macros", &debug['m']);
+       flagstr("o", "file: set output file", &outfile);
+       flagstr("trimpath", "prefix: remove prefix from recorded source file paths", &ctxt->trimpath);
+
+       flagparse(&argc, &argv, usage);
+       ctxt->debugasm = debug['S'];
+
+       if(argc < 1)
+               usage();
        if(argc > 1){
                print("can't assemble multiple files\n");
                errorexit();
        }
+
        if(assemble(argv[0]))
                errorexit();
        Bflush(&bstdout);
index a8ece212f055a42f8afa090b52da1a23ec41920c..4248437643890bedbd6defc2d0584f2da0d64677 100644 (file)
@@ -195,6 +195,7 @@ main(int argc, char *argv[])
        flagcount("q", "print Go definitions", &debug['q']);
        flagcount("s", "print #define assembly offsets", &debug['s']);
        flagcount("t", "debug code generation", &debug['t']);
+       flagstr("trimpath", "prefix: remove prefix from recorded source file paths", &ctxt->trimpath);
        flagcount("w", "enable warnings", &debug['w']);
        flagcount("v", "increase debug verbosity", &debug['v']);        
        if(thechar == '6')
index f372581aaea565a89525f9b83f3668462bb7adc4..7e28205656579875af3d9044489892b7c833017e 100644 (file)
@@ -306,6 +306,7 @@ main(int argc, char *argv[])
        flagcount("r", "debug generated wrappers", &debug['r']);
        flagcount("race", "enable race detector", &flag_race);
        flagcount("s", "warn about composite literals that can be simplified", &debug['s']);
+       flagstr("trimpath", "prefix: remove prefix from recorded source file paths", &ctxt->trimpath);
        flagcount("u", "reject unsafe code", &safemode);
        flagcount("v", "increase debug verbosity", &debug['v']);
        flagcount("w", "debug type checking", &debug['w']);
index d7a1d2182829a3627b36955110a3c633f8583878..03300555deb0da2004da84917e3be61d48f2a136 100644 (file)
@@ -1599,7 +1599,7 @@ func (gcToolchain) gc(b *builder, p *Package, archive, obj string, importArgs []
                gcargs = append(gcargs, "-installsuffix", buildContext.InstallSuffix)
        }
 
-       args := stringList(tool(archChar+"g"), "-o", ofile, buildGcflags, gcargs, "-D", p.localPrefix, importArgs)
+       args := stringList(tool(archChar+"g"), "-o", ofile, "-trimpath", b.work, buildGcflags, gcargs, "-D", p.localPrefix, importArgs)
        if ofile == archive {
                args = append(args, "-pack")
        }
@@ -1613,7 +1613,7 @@ func (gcToolchain) gc(b *builder, p *Package, archive, obj string, importArgs []
 
 func (gcToolchain) asm(b *builder, p *Package, obj, ofile, sfile string) error {
        sfile = mkAbs(p.Dir, sfile)
-       return b.run(p.Dir, p.ImportPath, nil, tool(archChar+"a"), "-I", obj, "-o", ofile, "-D", "GOOS_"+goos, "-D", "GOARCH_"+goarch, sfile)
+       return b.run(p.Dir, p.ImportPath, nil, tool(archChar+"a"), "-trimpath", b.work, "-I", obj, "-o", ofile, "-D", "GOOS_"+goos, "-D", "GOARCH_"+goarch, sfile)
 }
 
 func (gcToolchain) pkgpath(basedir string, p *Package) string {
@@ -1626,7 +1626,7 @@ func (gcToolchain) pack(b *builder, p *Package, objDir, afile string, ofiles []s
        for _, f := range ofiles {
                absOfiles = append(absOfiles, mkAbs(objDir, f))
        }
-       cmd := "grcP"
+       cmd := "c"
        absAfile := mkAbs(objDir, afile)
        appending := false
        if _, err := os.Stat(absAfile); err == nil {
@@ -1784,7 +1784,7 @@ func (gcToolchain) ld(b *builder, p *Package, out string, allactions []*action,
 func (gcToolchain) cc(b *builder, p *Package, objdir, ofile, cfile string) error {
        inc := filepath.Join(goroot, "pkg", fmt.Sprintf("%s_%s", goos, goarch))
        cfile = mkAbs(p.Dir, cfile)
-       args := stringList(tool(archChar+"c"), "-F", "-V", "-w", "-I", objdir, "-I", inc, "-o", ofile, buildCcflags, "-D", "GOOS_"+goos, "-D", "GOARCH_"+goarch, cfile)
+       args := stringList(tool(archChar+"c"), "-F", "-V", "-w", "-trimpath", b.work, "-I", objdir, "-I", inc, "-o", ofile, buildCcflags, "-D", "GOOS_"+goos, "-D", "GOARCH_"+goarch, cfile)
        return b.run(p.Dir, p.ImportPath, nil, args)
 }
 
index 856227fe831d6a777e6996940062e1f6347e7552..53ae47035424d0a45d9e55d2df13f16a1f749c76 100644 (file)
@@ -87,6 +87,34 @@ linklinefmt(Link *ctxt, Fmt *fp)
        return 0;
 }
 
+// Does s have t as a path prefix?
+// That is, does s == t or does s begin with t followed by a slash?
+// For portability, we allow ASCII case folding, so that haspathprefix("a/b/c", "A/B") is true.
+// Similarly, we allow slash folding, so that haspathprefix("a/b/c", "a\\b") is true.
+static int
+haspathprefix(char *s, char *t)
+{
+       int i, cs, ct;
+
+       if(t == nil)
+               return 0;
+       for(i=0; t[i]; i++) {
+               cs = s[i];
+               ct = t[i];
+               if('A' <= cs && cs <= 'Z')
+                       cs += 'a' - 'A';
+               if('A' <= ct && ct <= 'Z')
+                       ct += 'a' - 'A';
+               if(cs == '\\')
+                       cs = '/';
+               if(ct == '\\')
+                       ct = '/';
+               if(cs != ct)
+                       return 0;
+       }
+       return s[i] == '\0' || s[i] == '/' || s[i] == '\\';
+}
+
 // This is a simplified copy of linklinefmt above.
 // It doesn't allow printing the full stack, and it returns the file name and line number separately.
 // TODO: Unify with linklinefmt somehow.
@@ -103,7 +131,7 @@ linkgetline(Link *ctxt, int32 line, LSym **f, int32 *l)
        int32 lno, d, dlno;
        int n;
        Hist *h;
-       char buf[1024], *file;
+       char buf[1024], buf1[1024], *file;
 
        lno = line;
        n = 0;
@@ -159,6 +187,22 @@ linkgetline(Link *ctxt, int32 line, LSym **f, int32 *l)
                snprint(buf, sizeof buf, "%s", file);
        else
                snprint(buf, sizeof buf, "%s/%s", ctxt->pathname, file);
+
+       // Remove leading ctxt->trimpath, or else rewrite $GOROOT to $GOROOT_FINAL.
+       if(haspathprefix(buf, ctxt->trimpath)) {
+               if(strlen(buf) == strlen(ctxt->trimpath))
+                       strcpy(buf, "??");
+               else {
+                       snprint(buf1, sizeof buf1, "%s", buf+strlen(ctxt->trimpath)+1);
+                       if(buf1[0] == '\0')
+                               strcpy(buf1, "??");
+                       strcpy(buf, buf1);
+               }
+       } else if(ctxt->goroot_final != nil && haspathprefix(buf, ctxt->goroot)) {
+               snprint(buf1, sizeof buf1, "%s%s", ctxt->goroot_final, buf+strlen(ctxt->goroot));
+               strcpy(buf, buf1);
+       }
+
        lno -= dlno;
        *f = linklookup(ctxt, buf, HistVersion);
        *l = lno;
index 29fc036bcb7cfc37d6f3a293ca53850239cc915d..ff51b3df89e76b9a662100b3bb4d0588402d7feb 100644 (file)
@@ -95,6 +95,10 @@ linknew(LinkArch *arch)
        ctxt = emallocz(sizeof *ctxt);
        ctxt->arch = arch;
        ctxt->version = HistVersion;
+       ctxt->goroot = getgoroot();
+       ctxt->goroot_final = getenv("GOROOT_FINAL");
+       if(ctxt->goroot_final != nil && ctxt->goroot_final[0] == '\0')
+               ctxt->goroot_final = nil;
 
        p = getgoarch();
        if(strcmp(p, arch->name) != 0)