Commit a1fb024a authored by Russ Cox's avatar Russ Cox

cmd/go: hide work subdirectory names in gcc/clang object files

Until now the subdirectories under $WORK have had predictable
names, so it was OK to strip just $WORK from the file names that
end up in object files. In the future, those predictable names would
cause predictable collisions when compiling one package in two
different ways, so we're moving toward arbitrary, unpredictable
subdirectory names instead. When we do that, if the names appear
in the object files we won't get reproducible builds.

Take the subdirectory names out now, to make the later change safe.

Change-Id: I8057d1cc73f6e35c98b7718c9789c161dcbd87c0
Reviewed-on: https://go-review.googlesource.com/67251
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 1409c287
...@@ -73,10 +73,10 @@ func MkEnv() []cfg.EnvVar { ...@@ -73,10 +73,10 @@ func MkEnv() []cfg.EnvVar {
env = append(env, cfg.EnvVar{Name: "GO386", Value: cfg.GO386}) env = append(env, cfg.EnvVar{Name: "GO386", Value: cfg.GO386})
} }
cmd := b.GccCmd(".") cmd := b.GccCmd(".", "")
env = append(env, cfg.EnvVar{Name: "CC", Value: cmd[0]}) env = append(env, cfg.EnvVar{Name: "CC", Value: cmd[0]})
env = append(env, cfg.EnvVar{Name: "GOGCCFLAGS", Value: strings.Join(cmd[3:], " ")}) env = append(env, cfg.EnvVar{Name: "GOGCCFLAGS", Value: strings.Join(cmd[3:], " ")})
cmd = b.GxxCmd(".") cmd = b.GxxCmd(".", "")
env = append(env, cfg.EnvVar{Name: "CXX", Value: cmd[0]}) env = append(env, cfg.EnvVar{Name: "CXX", Value: cmd[0]})
if cfg.BuildContext.CgoEnabled { if cfg.BuildContext.CgoEnabled {
......
...@@ -3157,18 +3157,18 @@ func gccgoCleanPkgpath(p *load.Package) string { ...@@ -3157,18 +3157,18 @@ func gccgoCleanPkgpath(p *load.Package) string {
} }
// gcc runs the gcc C compiler to create an object from a single C file. // gcc runs the gcc C compiler to create an object from a single C file.
func (b *Builder) gcc(p *load.Package, out string, flags []string, cfile string) error { func (b *Builder) gcc(p *load.Package, workdir, out string, flags []string, cfile string) error {
return b.ccompile(p, out, flags, cfile, b.GccCmd(p.Dir)) return b.ccompile(p, out, flags, cfile, b.GccCmd(p.Dir, workdir))
} }
// gxx runs the g++ C++ compiler to create an object from a single C++ file. // gxx runs the g++ C++ compiler to create an object from a single C++ file.
func (b *Builder) gxx(p *load.Package, out string, flags []string, cxxfile string) error { func (b *Builder) gxx(p *load.Package, workdir, out string, flags []string, cxxfile string) error {
return b.ccompile(p, out, flags, cxxfile, b.GxxCmd(p.Dir)) return b.ccompile(p, out, flags, cxxfile, b.GxxCmd(p.Dir, workdir))
} }
// gfortran runs the gfortran Fortran compiler to create an object from a single Fortran file. // gfortran runs the gfortran Fortran compiler to create an object from a single Fortran file.
func (b *Builder) gfortran(p *load.Package, out string, flags []string, ffile string) error { func (b *Builder) gfortran(p *load.Package, workdir, out string, flags []string, ffile string) error {
return b.ccompile(p, out, flags, ffile, b.gfortranCmd(p.Dir)) return b.ccompile(p, out, flags, ffile, b.gfortranCmd(p.Dir, workdir))
} }
// ccompile runs the given C or C++ compiler and creates an object from a single source file. // ccompile runs the given C or C++ compiler and creates an object from a single source file.
...@@ -3211,41 +3211,41 @@ func (b *Builder) ccompile(p *load.Package, outfile string, flags []string, file ...@@ -3211,41 +3211,41 @@ func (b *Builder) ccompile(p *load.Package, outfile string, flags []string, file
} }
// gccld runs the gcc linker to create an executable from a set of object files. // gccld runs the gcc linker to create an executable from a set of object files.
func (b *Builder) gccld(p *load.Package, out string, flags []string, objs []string) error { func (b *Builder) gccld(p *load.Package, objdir, out string, flags []string, objs []string) error {
var cmd []string var cmd []string
if len(p.CXXFiles) > 0 || len(p.SwigCXXFiles) > 0 { if len(p.CXXFiles) > 0 || len(p.SwigCXXFiles) > 0 {
cmd = b.GxxCmd(p.Dir) cmd = b.GxxCmd(p.Dir, objdir)
} else { } else {
cmd = b.GccCmd(p.Dir) cmd = b.GccCmd(p.Dir, objdir)
} }
return b.run(p.Dir, p.ImportPath, nil, cmd, "-o", out, objs, flags) return b.run(p.Dir, p.ImportPath, nil, cmd, "-o", out, objs, flags)
} }
// gccCmd returns a gcc command line prefix // gccCmd returns a gcc command line prefix
// defaultCC is defined in zdefaultcc.go, written by cmd/dist. // defaultCC is defined in zdefaultcc.go, written by cmd/dist.
func (b *Builder) GccCmd(objdir string) []string { func (b *Builder) GccCmd(incdir, workdir string) []string {
return b.compilerCmd("CC", cfg.DefaultCC, objdir) return b.compilerCmd("CC", cfg.DefaultCC, incdir, workdir)
} }
// gxxCmd returns a g++ command line prefix // gxxCmd returns a g++ command line prefix
// defaultCXX is defined in zdefaultcc.go, written by cmd/dist. // defaultCXX is defined in zdefaultcc.go, written by cmd/dist.
func (b *Builder) GxxCmd(objdir string) []string { func (b *Builder) GxxCmd(incdir, workdir string) []string {
return b.compilerCmd("CXX", cfg.DefaultCXX, objdir) return b.compilerCmd("CXX", cfg.DefaultCXX, incdir, workdir)
} }
// gfortranCmd returns a gfortran command line prefix. // gfortranCmd returns a gfortran command line prefix.
func (b *Builder) gfortranCmd(objdir string) []string { func (b *Builder) gfortranCmd(incdir, workdir string) []string {
return b.compilerCmd("FC", "gfortran", objdir) return b.compilerCmd("FC", "gfortran", incdir, workdir)
} }
// compilerCmd returns a command line prefix for the given environment // compilerCmd returns a command line prefix for the given environment
// variable and using the default command when the variable is empty. // variable and using the default command when the variable is empty.
func (b *Builder) compilerCmd(envvar, defcmd, objdir string) []string { func (b *Builder) compilerCmd(envvar, defcmd, incdir, workdir string) []string {
// NOTE: env.go's mkEnv knows that the first three // NOTE: env.go's mkEnv knows that the first three
// strings returned are "gcc", "-I", objdir (and cuts them off). // strings returned are "gcc", "-I", incdir (and cuts them off).
compiler := envList(envvar, defcmd) compiler := envList(envvar, defcmd)
a := []string{compiler[0], "-I", objdir} a := []string{compiler[0], "-I", incdir}
a = append(a, compiler[1:]...) a = append(a, compiler[1:]...)
// Definitely want -fPIC but on Windows gcc complains // Definitely want -fPIC but on Windows gcc complains
...@@ -3279,7 +3279,11 @@ func (b *Builder) compilerCmd(envvar, defcmd, objdir string) []string { ...@@ -3279,7 +3279,11 @@ func (b *Builder) compilerCmd(envvar, defcmd, objdir string) []string {
// Tell gcc not to include the work directory in object files. // Tell gcc not to include the work directory in object files.
if b.gccSupportsFlag(compiler, "-fdebug-prefix-map=a=b") { if b.gccSupportsFlag(compiler, "-fdebug-prefix-map=a=b") {
a = append(a, "-fdebug-prefix-map="+b.WorkDir+"=/tmp/go-build") if workdir == "" {
workdir = b.WorkDir
}
workdir = strings.TrimSuffix(workdir, string(filepath.Separator))
a = append(a, "-fdebug-prefix-map="+workdir+"=/tmp/go-build")
} }
// Tell gcc not to include flags in object files, which defeats the // Tell gcc not to include flags in object files, which defeats the
...@@ -3508,7 +3512,7 @@ func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgo ...@@ -3508,7 +3512,7 @@ func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgo
cflags := str.StringList(cgoCPPFLAGS, cgoCFLAGS) cflags := str.StringList(cgoCPPFLAGS, cgoCFLAGS)
for _, cfile := range cfiles { for _, cfile := range cfiles {
ofile := nextOfile() ofile := nextOfile()
if err := b.gcc(p, ofile, cflags, objdir+cfile); err != nil { if err := b.gcc(p, a.Objdir, ofile, cflags, objdir+cfile); err != nil {
return nil, nil, err return nil, nil, err
} }
outObj = append(outObj, ofile) outObj = append(outObj, ofile)
...@@ -3516,7 +3520,7 @@ func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgo ...@@ -3516,7 +3520,7 @@ func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgo
for _, file := range gccfiles { for _, file := range gccfiles {
ofile := nextOfile() ofile := nextOfile()
if err := b.gcc(p, ofile, cflags, file); err != nil { if err := b.gcc(p, a.Objdir, ofile, cflags, file); err != nil {
return nil, nil, err return nil, nil, err
} }
outObj = append(outObj, ofile) outObj = append(outObj, ofile)
...@@ -3525,7 +3529,7 @@ func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgo ...@@ -3525,7 +3529,7 @@ func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgo
cxxflags := str.StringList(cgoCPPFLAGS, cgoCXXFLAGS) cxxflags := str.StringList(cgoCPPFLAGS, cgoCXXFLAGS)
for _, file := range gxxfiles { for _, file := range gxxfiles {
ofile := nextOfile() ofile := nextOfile()
if err := b.gxx(p, ofile, cxxflags, file); err != nil { if err := b.gxx(p, a.Objdir, ofile, cxxflags, file); err != nil {
return nil, nil, err return nil, nil, err
} }
outObj = append(outObj, ofile) outObj = append(outObj, ofile)
...@@ -3533,7 +3537,7 @@ func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgo ...@@ -3533,7 +3537,7 @@ func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgo
for _, file := range mfiles { for _, file := range mfiles {
ofile := nextOfile() ofile := nextOfile()
if err := b.gcc(p, ofile, cflags, file); err != nil { if err := b.gcc(p, a.Objdir, ofile, cflags, file); err != nil {
return nil, nil, err return nil, nil, err
} }
outObj = append(outObj, ofile) outObj = append(outObj, ofile)
...@@ -3542,7 +3546,7 @@ func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgo ...@@ -3542,7 +3546,7 @@ func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgo
fflags := str.StringList(cgoCPPFLAGS, cgoFFLAGS) fflags := str.StringList(cgoCPPFLAGS, cgoFFLAGS)
for _, file := range ffiles { for _, file := range ffiles {
ofile := nextOfile() ofile := nextOfile()
if err := b.gfortran(p, ofile, fflags, file); err != nil { if err := b.gfortran(p, a.Objdir, ofile, fflags, file); err != nil {
return nil, nil, err return nil, nil, err
} }
outObj = append(outObj, ofile) outObj = append(outObj, ofile)
...@@ -3577,7 +3581,7 @@ func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgo ...@@ -3577,7 +3581,7 @@ func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgo
func (b *Builder) dynimport(p *load.Package, objdir, importGo, cgoExe string, cflags, cgoLDFLAGS, outObj []string) error { func (b *Builder) dynimport(p *load.Package, objdir, importGo, cgoExe string, cflags, cgoLDFLAGS, outObj []string) error {
cfile := objdir + "_cgo_main.c" cfile := objdir + "_cgo_main.c"
ofile := objdir + "_cgo_main.o" ofile := objdir + "_cgo_main.o"
if err := b.gcc(p, ofile, cflags, cfile); err != nil { if err := b.gcc(p, objdir, ofile, cflags, cfile); err != nil {
return err return err
} }
...@@ -3589,7 +3593,7 @@ func (b *Builder) dynimport(p *load.Package, objdir, importGo, cgoExe string, cf ...@@ -3589,7 +3593,7 @@ func (b *Builder) dynimport(p *load.Package, objdir, importGo, cgoExe string, cf
if (cfg.Goarch == "arm" && cfg.Goos == "linux") || cfg.Goos == "android" { if (cfg.Goarch == "arm" && cfg.Goos == "linux") || cfg.Goos == "android" {
ldflags = append(ldflags, "-pie") ldflags = append(ldflags, "-pie")
} }
if err := b.gccld(p, dynobj, ldflags, linkobj); err != nil { if err := b.gccld(p, objdir, dynobj, ldflags, linkobj); err != nil {
return err return err
} }
......
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