From: Michael Matloob Date: Tue, 21 Mar 2023 20:00:18 +0000 (-0400) Subject: cmd/go: use --ffile-prefix-map instead of --debug-prefix-map X-Git-Tag: go1.21rc1~1182 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=4d9beb2052f9ee22901afe7ad0776e375ff581e7;p=gostls13.git cmd/go: use --ffile-prefix-map instead of --debug-prefix-map Also add code to replace the vendor directory in the prefix-map in vendored modules. We weren't doing that before because in vendored modules, the module's Dir field was set to empty, so nothing was being replaced. Instead when Dir is not set, so we are in vendor mode, replace the entire vendor directory's path. Change-Id: I910499c74237699fd36d18049909a72e2b6705d9 Reviewed-on: https://go-review.googlesource.com/c/go/+/478455 Reviewed-by: Bryan Mills TryBot-Result: Gopher Robot Run-TryBot: Michael Matloob Reviewed-by: Michael Matloob --- diff --git a/src/cmd/go/internal/work/exec.go b/src/cmd/go/internal/work/exec.go index 55cd30bbd1..86738e233d 100644 --- a/src/cmd/go/internal/work/exec.go +++ b/src/cmd/go/internal/work/exec.go @@ -2583,27 +2583,47 @@ func (b *Builder) ccompile(a *Action, p *load.Package, outfile string, flags []s // when -trimpath is enabled. if b.gccSupportsFlag(compiler, "-fdebug-prefix-map=a=b") { if cfg.BuildTrimpath || p.Goroot { + prefixMapFlag := "-fdebug-prefix-map" + if b.gccSupportsFlag(compiler, "-ffile-prefix-map=a=b") { + prefixMapFlag = "-ffile-prefix-map" + } // Keep in sync with Action.trimpath. - // The trimmed paths are a little different, but we need to trim in the + // The trimmed paths are a little different, but we need to trim in mostly the // same situations. var from, toPath string - if m := p.Module; m != nil { - from = m.Dir - toPath = m.Path + "@" + m.Version + if m := p.Module; m == nil { + if p.Root == "" { // command-line-arguments in GOPATH mode, maybe? + from = p.Dir + toPath = p.ImportPath + } else if p.Goroot { + from = p.Root + toPath = "GOROOT" + } else { + from = p.Root + toPath = "GOPATH" + } + } else if m.Dir == "" { + // The module is in the vendor directory. Replace the entire vendor + // directory path, because the module's Dir is not filled in. + from = modload.VendorDir() + toPath = "vendor" } else { - from = p.Dir - toPath = p.ImportPath + from = m.Dir + toPath = m.Path + if m.Version != "" { + m.Path += "@" + m.Version + } } - // -fdebug-prefix-map requires an absolute "to" path (or it joins the path - // with the working directory). Pick something that makes sense for the - // target platform. + // -fdebug-prefix-map (or -ffile-prefix-map) requires an absolute "to" + // path (or it joins the path with the working directory). Pick something + // that makes sense for the target platform. var to string if cfg.BuildContext.GOOS == "windows" { to = filepath.Join(`\\_\_`, toPath) } else { to = filepath.Join("/_", toPath) } - flags = append(slices.Clip(flags), "-fdebug-prefix-map="+from+"="+to) + flags = append(slices.Clip(flags), prefixMapFlag+"="+from+"="+to) } } @@ -2786,7 +2806,11 @@ func (b *Builder) compilerCmd(compiler []string, incdir, workdir string) []strin workdir = b.WorkDir } workdir = strings.TrimSuffix(workdir, string(filepath.Separator)) - a = append(a, "-fdebug-prefix-map="+workdir+"=/tmp/go-build") + if b.gccSupportsFlag(compiler, "-ffile-prefix-map=a=b") { + a = append(a, "-ffile-prefix-map="+workdir+"=/tmp/go-build") + } else { + a = append(a, "-fdebug-prefix-map="+workdir+"=/tmp/go-build") + } } // Tell gcc not to include flags in object files, which defeats the diff --git a/src/cmd/go/testdata/script/cgo_trimpath_macro.txt b/src/cmd/go/testdata/script/cgo_trimpath_macro.txt new file mode 100644 index 0000000000..b5cc1167cb --- /dev/null +++ b/src/cmd/go/testdata/script/cgo_trimpath_macro.txt @@ -0,0 +1,71 @@ +# This is a test that -trimpath trims the paths of every directory +# of Cgo dependencies in the module, and trims file paths included +# through the __FILE__ macro using --file-prefix-map. + +[!cgo] skip +[short] skip 'links and runs binaries' + +# Test in main module. +go run -trimpath -mod=vendor ./main +stdout '(\\_\\_|/_)[\\/]m[\\/]c[\\/]bar.h' + +# Test in vendored module. +go run -trimpath -mod=vendor v.com/main +stdout '(\\_\\_|/_)[\\/]vendor[\\/]v.com[\\/]c[\\/]bar.h' + +# Test in GOPATH mode. +env GO111MODULE=off +go run -trimpath ./main +stdout '(\\_\\_|/_)[\\/]GOPATH[\\/]src[\\/]c[\\/]bar.h' + +-- go.mod -- +module m + +require v.com v1.0.0 +-- go.sum -- +v.com v1.0.0 h1:xxx +v.com v1.0.0/go.mod h1:xxx +-- vendor/modules.txt -- +# v.com v1.0.0 +## explicit; go 1.20 +v.com/main +-- vendor/v.com/main/main.go -- +package main + +// #cgo CFLAGS: -I../c +// #include "stdio.h" +// void printfile(); +import "C" + +func main() { + C.printfile() + C.fflush(C.stdout) +} +-- vendor/v.com/main/foo.c -- +#include "bar.h" +-- vendor/v.com/c/bar.h -- +#include "stdio.h" + +void printfile() { + printf("%s\n", __FILE__); +} +-- main/main.go -- +package main + +// #cgo CFLAGS: -I../c +// #include "stdio.h" +// void printfile(); +import "C" + +func main() { + C.printfile() + C.fflush(C.stdout) +} +-- main/foo.c -- +#include "bar.h" +-- c/bar.h -- +#include "stdio.h" + +void printfile() { + printf("%s\n", __FILE__); +} \ No newline at end of file