aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/cgo/main.go
diff options
context:
space:
mode:
authorMichael Matloob <matloob@golang.org>2020-10-27 17:51:58 -0400
committerMichael Matloob <matloob@golang.org>2020-10-31 00:35:18 +0000
commit79fb187be4d1a1a93c01b3a6be66ea529311ea84 (patch)
tree821cab961132a52e17ff11e514ed83c7cbcc053d /src/cmd/cgo/main.go
parent07e4f0fd4b0f215cdfa7d6ea50f3e6402762a1a9 (diff)
downloadgo-79fb187be4d1a1a93c01b3a6be66ea529311ea84.tar.xz
cmd/cgo: add -trimpath flag allowing paths to be rewritten in outputs
cmd/cgo now has a -trimpath flag that behaves the same as the -trimpath flag to cmd/compile. This will be used to correct paths to cgo files that are overlaid. The code that processes trimpath in internal/objapi has been slightly refactored because it's currently only accessible via AbsFile, which does some additional processing to the path names. Now an ApplyRewrites function is exported that just applies the trimpath rewrites. Also remove unused srcfile argument to cmd/cgo.(*Package).godefs. For #39958 Change-Id: I497d48d0bc2fe1f6ab2b5835cbe79f15b839ee59 Reviewed-on: https://go-review.googlesource.com/c/go/+/266358 Trust: Michael Matloob <matloob@golang.org> Run-TryBot: Michael Matloob <matloob@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Jay Conrod <jayconrod@google.com> Reviewed-by: Bryan C. Mills <bcmills@google.com>
Diffstat (limited to 'src/cmd/cgo/main.go')
-rw-r--r--src/cmd/cgo/main.go15
1 files changed, 14 insertions, 1 deletions
diff --git a/src/cmd/cgo/main.go b/src/cmd/cgo/main.go
index 7d02ac3c54..c1116e28ec 100644
--- a/src/cmd/cgo/main.go
+++ b/src/cmd/cgo/main.go
@@ -243,6 +243,8 @@ var gccgopkgpath = flag.String("gccgopkgpath", "", "-fgo-pkgpath option used wit
var gccgoMangler func(string) string
var importRuntimeCgo = flag.Bool("import_runtime_cgo", true, "import runtime/cgo in generated code")
var importSyscall = flag.Bool("import_syscall", true, "import syscall in generated code")
+var trimpath = flag.String("trimpath", "", "applies supplied rewrites or trims prefixes to recorded source file paths")
+
var goarch, goos string
func main() {
@@ -322,6 +324,13 @@ func main() {
input = filepath.Join(*srcDir, input)
}
+ // Create absolute path for file, so that it will be used in error
+ // messages and recorded in debug line number information.
+ // This matches the rest of the toolchain. See golang.org/issue/5122.
+ if aname, err := filepath.Abs(input); err == nil {
+ input = aname
+ }
+
b, err := ioutil.ReadFile(input)
if err != nil {
fatalf("%s", err)
@@ -330,6 +339,10 @@ func main() {
fatalf("%s", err)
}
+ // Apply trimpath to the file path. The path won't be read from after this point.
+ input, _ = objabi.ApplyRewrites(input, *trimpath)
+ goFiles[i] = input
+
f := new(File)
f.Edit = edit.NewBuffer(b)
f.ParseGo(input, b)
@@ -367,7 +380,7 @@ func main() {
p.PackagePath = f.Package
p.Record(f)
if *godefs {
- os.Stdout.WriteString(p.godefs(f, input))
+ os.Stdout.WriteString(p.godefs(f))
} else {
p.writeOutput(f, input)
}