From e91fae57568df9d9f7073d9f32558f5ba4d9d3ff Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Wed, 21 Sep 2022 13:14:20 +0800 Subject: [PATCH] compileopts: silently succeed when there's no debug info to strip Before, on the baremetal target or MacOS, we errored if the user provided configuration to strip debug info. Ex. ```bash $ $ tinygo build -o main.go -scheduler=none --no-debug main.go error: cannot remove debug information: MacOS doesn't store debug info in the executable by default ``` This is a poor experience which results in having OS-specific CLI behavior. Silently succeeding is good keeping with the Linux philosophy and less distracting than logging the same without failing. Signed-off-by: Adrian Cole --- builder/build.go | 32 ++++++++++++++++++-------------- compileopts/config.go | 4 ++-- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/builder/build.go b/builder/build.go index 990d00b8..5dfb5ea8 100644 --- a/builder/build.go +++ b/builder/build.go @@ -700,21 +700,25 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil // Add embedded files. linkerDependencies = append(linkerDependencies, embedFileObjects...) - // Strip debug information with -no-debug. - if !config.Debug() { - for _, tag := range config.BuildTags() { - if tag == "baremetal" { - // Don't use -no-debug on baremetal targets. It makes no sense: - // the debug information isn't flashed to the device anyway. - return fmt.Errorf("stripping debug information is unnecessary for baremetal targets") - } - } - if config.GOOS() == "darwin" { - // Debug information isn't stored in the binary itself on MacOS but - // is left in the object files by default. The binary does store the - // path to these object files though. - return errors.New("cannot remove debug information: MacOS doesn't store debug info in the executable by default") + // Determine whether the compilation configuration would result in debug + // (DWARF) information in the object files. + var hasDebug = true + for _, tag := range config.BuildTags() { + if tag == "baremetal" { + // Don't use -no-debug on baremetal targets. It makes no sense: + // the debug information isn't flashed to the device anyway. + hasDebug = false } + } + if config.GOOS() == "darwin" { + // Debug information isn't stored in the binary itself on MacOS but + // is left in the object files by default. The binary does store the + // path to these object files though. + hasDebug = false + } + + // Strip debug information with -no-debug. + if hasDebug && !config.Debug() { if config.Target.Linker == "wasm-ld" { // Don't just strip debug information, also compress relocations // while we're at it. Relocations can only be compressed when debug diff --git a/compileopts/config.go b/compileopts/config.go index e1d1a311..ebfc5082 100644 --- a/compileopts/config.go +++ b/compileopts/config.go @@ -377,8 +377,8 @@ func (c *Config) VerifyIR() bool { } // Debug returns whether debug (DWARF) information should be retained by the -// linker. By default, debug information is retained but it can be removed with -// the -no-debug flag. +// linker. By default, debug information is retained, but it can be removed +// with the -no-debug flag. func (c *Config) Debug() bool { return c.Options.Debug }