1
0
mirror of https://github.com/golang/go synced 2024-11-11 19:51:37 -07:00

cmd/go: don't copy cgo files to objdir when overlay is present

This cl is a roll-forward of golang.org/cl/265758, which was rolled back
in golang.org/cl/268900. The changes made are removing cgofiles
from the list of files that are copied to objdir (because the cgofiles
themselves aren't actually provided to the compiler) and fixing test
cases to properly provide the overlay flag and to allow for paths with
backslashes (as in Windows).

The previous cl (golang.org/cl/262618) copied non-overlaid cgo files
to objdir, mostly to get around the issue that otherwise cgo-generated
files were written out with the wrong names (they'd get the base path
of the overlay file containing the replaced contents, instead of the
base path of the path whose contents are being replaced). So that CL
it would copy the files to objdir with the base path of the file
being replaced to circumvent that.

This CL changes cmd/go and cmd/cgo so that instead of copying
files, it passes the actual path of the file on disk either of
the original file (if it is not overlaid) or its replacement
file (if it is) as well as a flag --path_rewrite, newly added to
cmd/cgo, that specifies the actual original file path that corresponds
to the replaced files.

Updates #39958

Change-Id: Ia45b022f9d27cfce0f9ec6da5f3a9f53654c67b8
Reviewed-on: https://go-review.googlesource.com/c/go/+/269017
Trust: Michael Matloob <matloob@golang.org>
Run-TryBot: Michael Matloob <matloob@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Reviewed-by: Jay Conrod <jayconrod@google.com>
This commit is contained in:
Michael Matloob 2020-11-10 13:59:48 -05:00
parent c906608406
commit 28437546f4
2 changed files with 51 additions and 1 deletions

View File

@ -2709,7 +2709,7 @@ func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgo
// subtly break some cgo files that include .h files across directory
// boundaries, even though they shouldn't.
hasOverlay := false
cgoFileLists := [][]string{cgofiles, gccfiles, gxxfiles, mfiles, ffiles, hfiles}
cgoFileLists := [][]string{gccfiles, gxxfiles, mfiles, ffiles, hfiles}
OverlayLoop:
for _, fs := range cgoFileLists {
for _, f := range fs {
@ -2732,6 +2732,20 @@ OverlayLoop:
}
}
// Rewrite overlaid paths in cgo files.
// cgo adds //line and #line pragmas in generated files with these paths.
var trimpath []string
for i := range cgofiles {
path := mkAbs(p.Dir, cgofiles[i])
if opath, ok := fsys.OverlayPath(path); ok {
cgofiles[i] = opath
trimpath = append(trimpath, opath+"=>"+path)
}
}
if len(trimpath) > 0 {
cgoflags = append(cgoflags, "-trimpath", strings.Join(trimpath, ";"))
}
if err := b.run(a, execdir, p.ImportPath, cgoenv, cfg.BuildToolexec, cgoExe, "-objdir", objdir, "-importpath", p.ImportPath, cgoflags, "--", cgoCPPFLAGS, cgoCFLAGS, cgofiles); err != nil {
return nil, nil, err
}

View File

@ -43,6 +43,12 @@ go build -overlay overlay.json -o main_cgo_angle$GOEXE ./cgo_hello_angle
exec ./main_cgo_angle$GOEXE
stdout '^hello cgo\r?\n'
go list -compiled -overlay overlay.json -f '{{range .CompiledGoFiles}}{{. | printf "%s\n"}}{{end}}' ./cgo_hello_replace
cp stdout compiled_cgo_sources.txt
go run ../print_line_comments.go compiled_cgo_sources.txt
stdout $GOPATH[/\\]src[/\\]m[/\\]cgo_hello_replace[/\\]cgo_hello_replace.go
! stdout $GOPATH[/\\]src[/\\]m[/\\]overlay[/\\]hello.c
# Run same tests but with gccgo.
env GO111MODULE=off
[!exec:gccgo] stop
@ -207,3 +213,33 @@ void say_hello() { puts("hello cgo\n"); fflush(stdout); }
void say_hello() { puts("hello cgo\n"); fflush(stdout); }
-- print_line_comments.go --
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"strings"
)
func main() {
compiledGoFilesArg := os.Args[1]
b, err := ioutil.ReadFile(compiledGoFilesArg)
if err != nil {
log.Fatal(err)
}
compiledGoFiles := strings.Split(strings.TrimSpace(string(b)), "\n")
for _, f := range compiledGoFiles {
b, err := ioutil.ReadFile(f)
if err != nil {
log.Fatal(err)
}
for _, line := range strings.Split(string(b), "\n") {
if strings.HasPrefix(line, "#line") || strings.HasPrefix(line, "//line") {
fmt.Println(line)
}
}
}
}