Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not print to stderr if cgo linking succeeds after retry #3187

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions go/tools/builders/cgo2.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -196,7 +197,8 @@ func cgo2(goenv *env, goSrcs, cgoSrcs, cSrcs, cxxSrcs, objcSrcs, objcxxSrcs, sSr
mainBin := filepath.Join(workDir, "_cgo_.o") // .o is a lie; it's an executable
args = append([]string{cc, "-o", mainBin, mainObj}, cObjs...)
args = append(args, combinedLdFlags...)
if err := goenv.runCommand(args); err != nil {
var originalErrBuf bytes.Buffer
if err := goenv.runCommandToFile(os.Stdout, &originalErrBuf, args); err != nil {
// If linking the binary for cgo fails, this is usually because the
// object files reference external symbols that can't be resolved yet.
// Since the binary is only produced to have its symbols read by the cgo
Expand All @@ -214,13 +216,20 @@ func cgo2(goenv *env, goSrcs, cgoSrcs, cSrcs, cxxSrcs, objcSrcs, objcxxSrcs, sSr
default:
allowUnresolvedSymbolsLdFlag = "-Wl,--unresolved-symbols=ignore-all"
}
if err2 := goenv.runCommand(append(args, allowUnresolvedSymbolsLdFlag)); err2 != nil {
// Return the original error if we can't link the binary with the
// additional linker flags as they may simply be incorrect for the
// particular compiler/linker pair and would obscure the true reason
// for the failure of the original command.
// Print and return the original error if we can't link the binary with
// the additional linker flags as they may simply be incorrect for the
// particular compiler/linker pair and would obscure the true reason for
// the failure of the original command.
if err2 := goenv.runCommandToFile(
os.Stdout,
ioutil.Discard,
append(args, allowUnresolvedSymbolsLdFlag),
); err2 != nil {
os.Stderr.Write(relativizePaths(originalErrBuf.Bytes()))
return "", nil, nil, err
}
// Do not print the original error - rerunning the command with the
// additional linker flag fixed it.
}

cgoImportsGo := filepath.Join(workDir, "_cgo_imports.go")
Expand Down
10 changes: 5 additions & 5 deletions go/tools/builders/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@ func (e *env) runCommand(args []string) error {
return err
}

// runCommandToFile executes a subprocess and writes the output to the given
// writer.
func (e *env) runCommandToFile(w io.Writer, args []string) error {
// runCommandToFile executes a subprocess and writes stdout/stderr to the given
// writers.
func (e *env) runCommandToFile(out, err io.Writer, args []string) error {
cmd := exec.Command(args[0], args[1:]...)
cmd.Stdout = w
cmd.Stderr = os.Stderr
cmd.Stdout = out
cmd.Stderr = err
return runAndLogCommand(cmd, e.verbose)
}

Expand Down
4 changes: 2 additions & 2 deletions go/tools/builders/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ func run(args []string) error {
}
defer f.Close()
}
if err := goenv.runCommandToFile(f, goenv.goCmd("version")); err != nil {
if err := goenv.runCommandToFile(f, os.Stderr, goenv.goCmd("version")); err != nil {
return err
}
if err := goenv.runCommandToFile(f, goenv.goCmd("env")); err != nil {
if err := goenv.runCommandToFile(f, os.Stderr, goenv.goCmd("env")); err != nil {
return err
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion go/tools/builders/stdliblist.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func stdliblist(args []string) error {
defer jsonFile.Close()

jsonData := &bytes.Buffer{}
if err := goenv.runCommandToFile(jsonData, listArgs); err != nil {
if err := goenv.runCommandToFile(jsonData, os.Stderr, listArgs); err != nil {
return err
}
encoder := json.NewEncoder(jsonFile)
Expand Down