From c67a27bf7520ae9440a665e80c7c3891bafb33d4 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Wed, 8 Mar 2023 14:48:41 -0500 Subject: [PATCH] cmd/dist: omit DWARF in build release toolchain binaries The vast majority of users of Go toolchains have no need for binaries like the go command and compiler to include DWARF information, and the DWARF information is 34% of the size of the overall Go toolchain zip files (14% when the toolchain is unzipped on disk, because other parts get bigger). To save network and disk, disable DWARF in build release binaries. DWARF remains enabled when developing in the main branch (signaled by no VERSION file existing), for better debuggability when actually working on the compiler and go command. Note that removing DWARF does not break the backtraces shown when a binary panics, nor does it break other uses of stack traces from within a Go program, such as runtime.Callers. To build a release toolchain with DWARF included, people can use GO_LDFLAGS=-w=0 ./make.bash Change-Id: Ib0bbe1446adca4599066b2fb2f2734e6825c1106 Reviewed-on: https://go-review.googlesource.com/c/go/+/475378 Auto-Submit: Russ Cox Run-TryBot: Russ Cox Reviewed-by: Ian Lance Taylor TryBot-Result: Gopher Robot --- src/cmd/compile/internal/ssa/stmtlines_test.go | 9 ++++++++- src/cmd/dist/build.go | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/cmd/compile/internal/ssa/stmtlines_test.go b/src/cmd/compile/internal/ssa/stmtlines_test.go index 4dadfe86307..dd3ce7c1d87 100644 --- a/src/cmd/compile/internal/ssa/stmtlines_test.go +++ b/src/cmd/compile/internal/ssa/stmtlines_test.go @@ -75,8 +75,15 @@ func TestStmtLines(t *testing.T) { } } + // Build cmd/go forcing DWARF enabled, as a large test case. + dir := t.TempDir() + out, err := testenv.Command(t, testenv.GoToolPath(t), "build", "-ldflags=-w=0", "-o", dir+"/test.exe", "cmd/go").CombinedOutput() + if err != nil { + t.Fatalf("go build: %v\n%s", err, out) + } + lines := map[Line]bool{} - dw, err := open(testenv.GoToolPath(t)) + dw, err := open(dir + "/test.exe") must(err) rdr := dw.Reader() rdr.Seek(0) diff --git a/src/cmd/dist/build.go b/src/cmd/dist/build.go index 343f0ce332f..25f75804e04 100644 --- a/src/cmd/dist/build.go +++ b/src/cmd/dist/build.go @@ -1328,7 +1328,7 @@ func toolenv() []string { // Do not include local development, so that people working in the // main branch for day-to-day work on the Go toolchain itself can // still have full paths for stack traces for compiler crashes and the like. - env = append(env, "GOFLAGS=-trimpath") + env = append(env, "GOFLAGS=-trimpath -ldflags=-w -gcflags=cmd/...=-dwarf=false") } return env }