Skip to content

Commit e0a4e2a

Browse files
committed
Intercept stdout/stderr, and prefix them with the target
Otherwise it's impossible to interpret the source of output. At the same time, add a differences section to the docs.
1 parent 446cde9 commit e0a4e2a

2 files changed

Lines changed: 35 additions & 4 deletions

File tree

README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,30 @@ The `GOARCH` placeholder expands to the `GOARCH` under build.
9090

9191
Only a single `output` directive may be found in a package.
9292

93+
# Differences to `go build`
94+
95+
As multibuild is a wrapper around `go build`, most of the behaviour you will see come from there.
96+
This section is an attempt to document the areas where there are differences, and why.
97+
98+
## Verbose
99+
100+
multibuild adds its own verbose output indicating when different targets start/finish if you pass `-v`.
101+
102+
## Output Prefixing
103+
104+
Output from all builds is prefixed with `GOOS/GOARCH: `, e.g. instead of `go build saying stuff`,
105+
you will see `linux/arm64: go build saying stuff`.
106+
107+
I think this is generally useful, and the only way to get sane output you can act on,
108+
so there is no configuration knob to disable it at this time.
109+
93110
## Cgo
94111

95112
Since the primary purpose of `multibuild` is to cross compile, the use of cgo isn't really
96113
something I have thought about or focused on: I personally just switch it off and call it a day,
97-
so that my binaries run in more places. This is one place I have chosen to differ from `go build`:
98-
`multibuild` will force `CGO_ENABLED=0` by default.
114+
so that my binaries run in more places.
115+
116+
So `multibuild` forces `CGO_ENABLED=0` by default.
99117

100118
This choice might not be for everyone, though, so `multibuild` will not complain if you explicitly
101119
choose to enable it, e.g. by running `CGO_ENABLED=1 go tool multibuild`.

cmd/multibuild/multibuild.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ package main
88
//go:multibuild:output=bin/${TARGET}-${GOOS}-${GOARCH}
99

1010
import (
11+
"bufio"
1112
"bytes"
1213
"encoding/json"
1314
"fmt"
15+
"io"
1416
"log"
1517
"os"
1618
"os/exec"
@@ -228,9 +230,20 @@ func main() {
228230

229231
func runBuild(args []string, goos, goarch string) {
230232
cmd := exec.Command("go", append([]string{"build"}, args...)...)
231-
cmd.Stdout = os.Stdout
232-
cmd.Stderr = os.Stderr
233233
cmd.Env = os.Environ()
234+
stdout, _ := cmd.StdoutPipe()
235+
stderr, _ := cmd.StderrPipe()
236+
237+
interceptor := func(source io.ReadCloser, dest io.Writer) {
238+
scanner := bufio.NewScanner(source)
239+
for scanner.Scan() {
240+
line := fmt.Sprintf("%s/%s: %s", goos, goarch, scanner.Text())
241+
fmt.Fprintln(dest, line)
242+
}
243+
}
244+
245+
go interceptor(stdout, os.Stdout)
246+
go interceptor(stderr, os.Stderr)
234247

235248
if goos != "" {
236249
cmd.Env = append(cmd.Env,

0 commit comments

Comments
 (0)