cmd/link: use "CC --print-prog-name" to locate tools

When building for macOS with external linking, we currently use
"xcrun" to invoke "dsymutil" and "strip" tools. That doesn't work
well for cross compilation. Use "CC --print-prog-name" to find the
tool path instead.

Fixes #47316.

Change-Id: Ib30c6494c48bfb6a505dc26fe644ef543d777076
Reviewed-on: https://go-review.googlesource.com/c/go/+/336769
Trust: Cherry Mui <cherryyz@google.com>
Run-TryBot: Cherry Mui <cherryyz@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
This commit is contained in:
Cherry Mui 2021-07-22 18:57:52 -04:00
parent 205640ed7b
commit eb9f090d1b

View file

@ -1644,13 +1644,31 @@ func (ctxt *Link) hostlink() {
}
if combineDwarf {
// Find "dsymutils" and "strip" tools using CC --print-prog-name.
var cc []string
cc = append(cc, ctxt.extld()...)
cc = append(cc, hostlinkArchArgs(ctxt.Arch)...)
cc = append(cc, "--print-prog-name", "dsymutil")
out, err := exec.Command(cc[0], cc[1:]...).CombinedOutput()
if err != nil {
Exitf("%s: finding dsymutil failed: %v\n%s", os.Args[0], err, out)
}
dsymutilCmd := strings.TrimSuffix(string(out), "\n")
cc[len(cc)-1] = "strip"
out, err = exec.Command(cc[0], cc[1:]...).CombinedOutput()
if err != nil {
Exitf("%s: finding strip failed: %v\n%s", os.Args[0], err, out)
}
stripCmd := strings.TrimSuffix(string(out), "\n")
dsym := filepath.Join(*flagTmpdir, "go.dwarf")
if out, err := exec.Command("xcrun", "dsymutil", "-f", *flagOutfile, "-o", dsym).CombinedOutput(); err != nil {
if out, err := exec.Command(dsymutilCmd, "-f", *flagOutfile, "-o", dsym).CombinedOutput(); err != nil {
Exitf("%s: running dsymutil failed: %v\n%s", os.Args[0], err, out)
}
// Remove STAB (symbolic debugging) symbols after we are done with them (by dsymutil).
// They contain temporary file paths and make the build not reproducible.
if out, err := exec.Command("xcrun", "strip", "-S", *flagOutfile).CombinedOutput(); err != nil {
if out, err := exec.Command(stripCmd, "-S", *flagOutfile).CombinedOutput(); err != nil {
Exitf("%s: running strip failed: %v\n%s", os.Args[0], err, out)
}
// Skip combining if `dsymutil` didn't generate a file. See #11994.