Skip to content

Commit 2843754

Browse files
committed
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 <[email protected]> Run-TryBot: Michael Matloob <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent c906608 commit 2843754

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/cmd/go/internal/work/exec.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2709,7 +2709,7 @@ func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgo
27092709
// subtly break some cgo files that include .h files across directory
27102710
// boundaries, even though they shouldn't.
27112711
hasOverlay := false
2712-
cgoFileLists := [][]string{cgofiles, gccfiles, gxxfiles, mfiles, ffiles, hfiles}
2712+
cgoFileLists := [][]string{gccfiles, gxxfiles, mfiles, ffiles, hfiles}
27132713
OverlayLoop:
27142714
for _, fs := range cgoFileLists {
27152715
for _, f := range fs {
@@ -2732,6 +2732,20 @@ OverlayLoop:
27322732
}
27332733
}
27342734

2735+
// Rewrite overlaid paths in cgo files.
2736+
// cgo adds //line and #line pragmas in generated files with these paths.
2737+
var trimpath []string
2738+
for i := range cgofiles {
2739+
path := mkAbs(p.Dir, cgofiles[i])
2740+
if opath, ok := fsys.OverlayPath(path); ok {
2741+
cgofiles[i] = opath
2742+
trimpath = append(trimpath, opath+"=>"+path)
2743+
}
2744+
}
2745+
if len(trimpath) > 0 {
2746+
cgoflags = append(cgoflags, "-trimpath", strings.Join(trimpath, ";"))
2747+
}
2748+
27352749
if err := b.run(a, execdir, p.ImportPath, cgoenv, cfg.BuildToolexec, cgoExe, "-objdir", objdir, "-importpath", p.ImportPath, cgoflags, "--", cgoCPPFLAGS, cgoCFLAGS, cgofiles); err != nil {
27362750
return nil, nil, err
27372751
}

src/cmd/go/testdata/script/build_overlay.txt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ go build -overlay overlay.json -o main_cgo_angle$GOEXE ./cgo_hello_angle
4343
exec ./main_cgo_angle$GOEXE
4444
stdout '^hello cgo\r?\n'
4545

46+
go list -compiled -overlay overlay.json -f '{{range .CompiledGoFiles}}{{. | printf "%s\n"}}{{end}}' ./cgo_hello_replace
47+
cp stdout compiled_cgo_sources.txt
48+
go run ../print_line_comments.go compiled_cgo_sources.txt
49+
stdout $GOPATH[/\\]src[/\\]m[/\\]cgo_hello_replace[/\\]cgo_hello_replace.go
50+
! stdout $GOPATH[/\\]src[/\\]m[/\\]overlay[/\\]hello.c
51+
4652
# Run same tests but with gccgo.
4753
env GO111MODULE=off
4854
[!exec:gccgo] stop
@@ -207,3 +213,33 @@ void say_hello() { puts("hello cgo\n"); fflush(stdout); }
207213

208214
void say_hello() { puts("hello cgo\n"); fflush(stdout); }
209215

216+
-- print_line_comments.go --
217+
package main
218+
219+
import (
220+
"fmt"
221+
"io/ioutil"
222+
"log"
223+
"os"
224+
"strings"
225+
)
226+
227+
func main() {
228+
compiledGoFilesArg := os.Args[1]
229+
b, err := ioutil.ReadFile(compiledGoFilesArg)
230+
if err != nil {
231+
log.Fatal(err)
232+
}
233+
compiledGoFiles := strings.Split(strings.TrimSpace(string(b)), "\n")
234+
for _, f := range compiledGoFiles {
235+
b, err := ioutil.ReadFile(f)
236+
if err != nil {
237+
log.Fatal(err)
238+
}
239+
for _, line := range strings.Split(string(b), "\n") {
240+
if strings.HasPrefix(line, "#line") || strings.HasPrefix(line, "//line") {
241+
fmt.Println(line)
242+
}
243+
}
244+
}
245+
}

0 commit comments

Comments
 (0)