Skip to content

Commit e3de852

Browse files
committed
cmd/go: don't copy cgo files to objdir when overlay is present
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: Ic4aae5ef77fe405011fcdce7f6c162488d13daa2 Reviewed-on: https://go-review.googlesource.com/c/go/+/265758 Trust: Michael Matloob <[email protected]> Run-TryBot: Michael Matloob <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Jay Conrod <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent 1899312 commit e3de852

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

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

+14
Original file line numberDiff line numberDiff line change
@@ -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

+36
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 -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)