Commit e97b3ab1 authored by Russ Cox's avatar Russ Cox

build: remove tmp dir names from objects, support GOROOT_FINAL again

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
parent fdade683
...@@ -145,6 +145,19 @@ Finally, the go command now supports packages that import Objective-C ...@@ -145,6 +145,19 @@ Finally, the go command now supports packages that import Objective-C
files (suffixed <code>.m</code>) through cgo. files (suffixed <code>.m</code>) through cgo.
</p> </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> <h3 id="misc">Miscellany</h3>
<p> <p>
......
...@@ -360,6 +360,9 @@ struct Link ...@@ -360,6 +360,9 @@ struct Link
Biobuf* bso; // for -v flag Biobuf* bso; // for -v flag
char* pathname; char* pathname;
int32 windows; int32 windows;
char* trimpath;
char* goroot;
char* goroot_final;
// hash table of all symbols // hash table of all symbols
LSym* hash[LINKHASH]; LSym* hash[LINKHASH];
......
...@@ -97,7 +97,7 @@ enum ...@@ -97,7 +97,7 @@ enum
Always = 14, Always = 14,
}; };
EXTERN char debug[256]; EXTERN int debug[256];
EXTERN Sym* hash[NHASH]; EXTERN Sym* hash[NHASH];
EXTERN char** Dlist; EXTERN char** Dlist;
EXTERN int nDlist; EXTERN int nDlist;
......
...@@ -57,11 +57,27 @@ Lconv(Fmt *fp) ...@@ -57,11 +57,27 @@ Lconv(Fmt *fp)
return linklinefmt(ctxt, 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 void
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char *p; char *p;
int c;
thechar = '5'; thechar = '5';
thestring = "arm"; thestring = "arm";
...@@ -84,49 +100,24 @@ main(int argc, char *argv[]) ...@@ -84,49 +100,24 @@ main(int argc, char *argv[])
cinit(); cinit();
outfile = 0; outfile = 0;
setinclude("."); setinclude(".");
ARGBEGIN {
default: flagfn1("D", "name[=value]: add #define", dodef);
c = ARGC(); flagfn1("I", "dir: add dir to include path", setinclude);
if(c >= 0 && c < sizeof(debug)) flagcount("S", "print assembly and machine code", &debug['S']);
debug[c] = 1; flagcount("m", "debug preprocessor macros", &debug['m']);
break; flagstr("o", "file: set output file", &outfile);
flagstr("trimpath", "prefix: remove prefix from recorded source file paths", &ctxt->trimpath);
case 'o':
outfile = ARGF(); flagparse(&argc, &argv, usage);
break; ctxt->debugasm = debug['S'];
case 'D': if(argc < 1)
p = ARGF(); usage();
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();
}
if(argc > 1){ if(argc > 1){
print("can't assemble multiple files\n"); print("can't assemble multiple files\n");
errorexit(); errorexit();
} }
if(assemble(argv[0])) if(assemble(argv[0]))
errorexit(); errorexit();
Bflush(&bstdout); Bflush(&bstdout);
......
...@@ -109,7 +109,7 @@ enum ...@@ -109,7 +109,7 @@ enum
CPREPROC, CPREPROC,
}; };
EXTERN char debug[256]; EXTERN int debug[256];
EXTERN Sym* hash[NHASH]; EXTERN Sym* hash[NHASH];
EXTERN char** Dlist; EXTERN char** Dlist;
EXTERN int nDlist; EXTERN int nDlist;
......
...@@ -63,13 +63,29 @@ Lconv(Fmt *fp) ...@@ -63,13 +63,29 @@ Lconv(Fmt *fp)
return linklinefmt(ctxt, 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; LinkArch* thelinkarch = &linkamd64;
void
usage(void)
{
print("usage: %ca [options] file.c...\n", thechar);
flagprint(1);
errorexit();
}
void void
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char *p; char *p;
int c;
thechar = '6'; thechar = '6';
thestring = "amd64"; thestring = "amd64";
...@@ -94,45 +110,24 @@ main(int argc, char *argv[]) ...@@ -94,45 +110,24 @@ main(int argc, char *argv[])
cinit(); cinit();
outfile = 0; outfile = 0;
setinclude("."); setinclude(".");
ARGBEGIN {
default: flagfn1("D", "name[=value]: add #define", dodef);
c = ARGC(); flagfn1("I", "dir: add dir to include path", setinclude);
if(c >= 0 && c < sizeof(debug)) flagcount("S", "print assembly and machine code", &debug['S']);
debug[c] = 1; flagcount("m", "debug preprocessor macros", &debug['m']);
break; flagstr("o", "file: set output file", &outfile);
flagstr("trimpath", "prefix: remove prefix from recorded source file paths", &ctxt->trimpath);
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': flagparse(&argc, &argv, usage);
ctxt->debugasm++; ctxt->debugasm = debug['S'];
break;
} ARGEND
if(*argv == 0) { if(argc < 1)
print("usage: %ca [-options] file.s\n", thechar); usage();
errorexit();
}
if(argc > 1){ if(argc > 1){
print("can't assemble multiple files\n"); print("can't assemble multiple files\n");
errorexit(); errorexit();
} }
if(assemble(argv[0])) if(assemble(argv[0]))
errorexit(); errorexit();
Bflush(&bstdout); Bflush(&bstdout);
......
...@@ -109,7 +109,7 @@ enum ...@@ -109,7 +109,7 @@ enum
CPREPROC, CPREPROC,
}; };
EXTERN char debug[256]; EXTERN int debug[256];
EXTERN Sym* hash[NHASH]; EXTERN Sym* hash[NHASH];
EXTERN char** Dlist; EXTERN char** Dlist;
EXTERN int nDlist; EXTERN int nDlist;
......
...@@ -63,11 +63,26 @@ Lconv(Fmt *fp) ...@@ -63,11 +63,26 @@ Lconv(Fmt *fp)
return linklinefmt(ctxt, 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 void
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char *p; char *p;
int c;
thechar = '8'; thechar = '8';
thestring = "386"; thestring = "386";
...@@ -90,44 +105,24 @@ main(int argc, char *argv[]) ...@@ -90,44 +105,24 @@ main(int argc, char *argv[])
cinit(); cinit();
outfile = 0; outfile = 0;
setinclude("."); setinclude(".");
ARGBEGIN {
default: flagfn1("D", "name[=value]: add #define", dodef);
c = ARGC(); flagfn1("I", "dir: add dir to include path", setinclude);
if(c >= 0 && c < sizeof(debug)) flagcount("S", "print assembly and machine code", &debug['S']);
debug[c] = 1; flagcount("m", "debug preprocessor macros", &debug['m']);
break; flagstr("o", "file: set output file", &outfile);
flagstr("trimpath", "prefix: remove prefix from recorded source file paths", &ctxt->trimpath);
case 'o':
outfile = ARGF(); flagparse(&argc, &argv, usage);
break; ctxt->debugasm = debug['S'];
case 'D': if(argc < 1)
p = ARGF(); usage();
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();
}
if(argc > 1){ if(argc > 1){
print("can't assemble multiple files\n"); print("can't assemble multiple files\n");
errorexit(); errorexit();
} }
if(assemble(argv[0])) if(assemble(argv[0]))
errorexit(); errorexit();
Bflush(&bstdout); Bflush(&bstdout);
......
...@@ -195,6 +195,7 @@ main(int argc, char *argv[]) ...@@ -195,6 +195,7 @@ main(int argc, char *argv[])
flagcount("q", "print Go definitions", &debug['q']); flagcount("q", "print Go definitions", &debug['q']);
flagcount("s", "print #define assembly offsets", &debug['s']); flagcount("s", "print #define assembly offsets", &debug['s']);
flagcount("t", "debug code generation", &debug['t']); 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("w", "enable warnings", &debug['w']);
flagcount("v", "increase debug verbosity", &debug['v']); flagcount("v", "increase debug verbosity", &debug['v']);
if(thechar == '6') if(thechar == '6')
......
...@@ -306,6 +306,7 @@ main(int argc, char *argv[]) ...@@ -306,6 +306,7 @@ main(int argc, char *argv[])
flagcount("r", "debug generated wrappers", &debug['r']); flagcount("r", "debug generated wrappers", &debug['r']);
flagcount("race", "enable race detector", &flag_race); flagcount("race", "enable race detector", &flag_race);
flagcount("s", "warn about composite literals that can be simplified", &debug['s']); 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("u", "reject unsafe code", &safemode);
flagcount("v", "increase debug verbosity", &debug['v']); flagcount("v", "increase debug verbosity", &debug['v']);
flagcount("w", "debug type checking", &debug['w']); flagcount("w", "debug type checking", &debug['w']);
......
...@@ -1599,7 +1599,7 @@ func (gcToolchain) gc(b *builder, p *Package, archive, obj string, importArgs [] ...@@ -1599,7 +1599,7 @@ func (gcToolchain) gc(b *builder, p *Package, archive, obj string, importArgs []
gcargs = append(gcargs, "-installsuffix", buildContext.InstallSuffix) 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 { if ofile == archive {
args = append(args, "-pack") args = append(args, "-pack")
} }
...@@ -1613,7 +1613,7 @@ func (gcToolchain) gc(b *builder, p *Package, archive, obj string, importArgs [] ...@@ -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 { func (gcToolchain) asm(b *builder, p *Package, obj, ofile, sfile string) error {
sfile = mkAbs(p.Dir, sfile) 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 { func (gcToolchain) pkgpath(basedir string, p *Package) string {
...@@ -1626,7 +1626,7 @@ func (gcToolchain) pack(b *builder, p *Package, objDir, afile string, ofiles []s ...@@ -1626,7 +1626,7 @@ func (gcToolchain) pack(b *builder, p *Package, objDir, afile string, ofiles []s
for _, f := range ofiles { for _, f := range ofiles {
absOfiles = append(absOfiles, mkAbs(objDir, f)) absOfiles = append(absOfiles, mkAbs(objDir, f))
} }
cmd := "grcP" cmd := "c"
absAfile := mkAbs(objDir, afile) absAfile := mkAbs(objDir, afile)
appending := false appending := false
if _, err := os.Stat(absAfile); err == nil { if _, err := os.Stat(absAfile); err == nil {
...@@ -1784,7 +1784,7 @@ func (gcToolchain) ld(b *builder, p *Package, out string, allactions []*action, ...@@ -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 { func (gcToolchain) cc(b *builder, p *Package, objdir, ofile, cfile string) error {
inc := filepath.Join(goroot, "pkg", fmt.Sprintf("%s_%s", goos, goarch)) inc := filepath.Join(goroot, "pkg", fmt.Sprintf("%s_%s", goos, goarch))
cfile = mkAbs(p.Dir, cfile) 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) return b.run(p.Dir, p.ImportPath, nil, args)
} }
......
...@@ -87,6 +87,34 @@ linklinefmt(Link *ctxt, Fmt *fp) ...@@ -87,6 +87,34 @@ linklinefmt(Link *ctxt, Fmt *fp)
return 0; 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. // 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. // It doesn't allow printing the full stack, and it returns the file name and line number separately.
// TODO: Unify with linklinefmt somehow. // TODO: Unify with linklinefmt somehow.
...@@ -103,7 +131,7 @@ linkgetline(Link *ctxt, int32 line, LSym **f, int32 *l) ...@@ -103,7 +131,7 @@ linkgetline(Link *ctxt, int32 line, LSym **f, int32 *l)
int32 lno, d, dlno; int32 lno, d, dlno;
int n; int n;
Hist *h; Hist *h;
char buf[1024], *file; char buf[1024], buf1[1024], *file;
lno = line; lno = line;
n = 0; n = 0;
...@@ -159,6 +187,22 @@ linkgetline(Link *ctxt, int32 line, LSym **f, int32 *l) ...@@ -159,6 +187,22 @@ linkgetline(Link *ctxt, int32 line, LSym **f, int32 *l)
snprint(buf, sizeof buf, "%s", file); snprint(buf, sizeof buf, "%s", file);
else else
snprint(buf, sizeof buf, "%s/%s", ctxt->pathname, file); 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; lno -= dlno;
*f = linklookup(ctxt, buf, HistVersion); *f = linklookup(ctxt, buf, HistVersion);
*l = lno; *l = lno;
......
...@@ -95,6 +95,10 @@ linknew(LinkArch *arch) ...@@ -95,6 +95,10 @@ linknew(LinkArch *arch)
ctxt = emallocz(sizeof *ctxt); ctxt = emallocz(sizeof *ctxt);
ctxt->arch = arch; ctxt->arch = arch;
ctxt->version = HistVersion; 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(); p = getgoarch();
if(strcmp(p, arch->name) != 0) if(strcmp(p, arch->name) != 0)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment