diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/cmd/go/internal/work/exec.go | 46 | ||||
| -rw-r--r-- | src/cmd/go/testdata/script/cgo_trimpath_macro.txt | 71 |
2 files changed, 106 insertions, 11 deletions
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 |
