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 <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Michael Matloob <matloob@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
This commit is contained in:
Michael Matloob 2023-03-21 16:00:18 -04:00
parent 48d61a46e2
commit 4d9beb2052
2 changed files with 106 additions and 11 deletions

View File

@ -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

View File

@ -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__);
}