main: add -debug flag that controls debuginfo stripping

Debug information is useful in many case, but in the case of WebAssembly
it increases the binary size by a large amount. It also inhibits
compression of relocations. Therefore I've made debug information
optional and disabled by default.

(Debug information is still generated, but stripped from the binary at
the link step).
This commit is contained in:
Ayke van Laethem
2021-07-11 15:12:46 +02:00
parent 0565b7c0e0
commit 2930c44bfc
5 changed files with 77 additions and 9 deletions
+27 -4
View File
@@ -209,7 +209,7 @@ func (c *Config) CFlags() []string {
cflags = append(cflags, "-nostdlibinc", "-Xclang", "-internal-isystem", "-Xclang", filepath.Join(root, "lib", "picolibc", "newlib", "libc", "include"))
cflags = append(cflags, "-I"+filepath.Join(root, "lib/picolibc-include"))
}
if c.Debug() {
if c.EmitDWARF() {
cflags = append(cflags, "-g")
}
return cflags
@@ -250,10 +250,33 @@ func (c *Config) VerifyIR() bool {
return c.Options.VerifyIR
}
// Debug returns whether to add debug symbols to the IR, for debugging with GDB
// and similar.
// EmitDWARF returns whether to add debug symbols to the IR, for debugging with
// GDB and similar.
func (c *Config) EmitDWARF() bool {
return c.Options.EmitDWARF
}
// Debug returns whether debug (DWARF) information should be retained by the
// linker. The default varies by target but can be controlled with the -debug
// command line flag.
func (c *Config) Debug() bool {
return c.Options.Debug
switch c.Options.Debug {
case "true":
return true
case "false":
return false
case "auto":
// Emit debug information everywhere by default except on WebAssembly.
for _, tag := range c.BuildTags() {
if tag == "tinygo.wasm" {
return false
}
}
return true
default:
// This is already checked so shouldn't happen.
panic("unknown -debug flag")
}
}
// BinaryFormat returns an appropriate binary format, based on the file
+9 -1
View File
@@ -9,6 +9,7 @@ import (
var (
validGCOptions = []string{"none", "leaking", "extalloc", "conservative"}
validSchedulerOptions = []string{"none", "tasks", "coroutines"}
validDebugOptions = []string{"auto", "true", "false"}
validSerialOptions = []string{"none", "uart", "usb"}
validPrintSizeOptions = []string{"none", "short", "full"}
validPanicStrategyOptions = []string{"print", "trap"}
@@ -28,7 +29,8 @@ type Options struct {
DumpSSA bool
VerifyIR bool
PrintCommands func(cmd string, args ...string)
Debug bool
EmitDWARF bool
Debug string
PrintSizes string
PrintAllocs *regexp.Regexp // regexp string
PrintStacks bool
@@ -70,6 +72,12 @@ func (o *Options) Verify() error {
}
}
if !isInArray(validDebugOptions, o.Debug) {
return fmt.Errorf(`invalid debug option '%s': valid values are %s`,
o.Debug,
strings.Join(validDebugOptions, ", "))
}
if o.PrintSizes != "" {
valid := isInArray(validPrintSizeOptions, o.PrintSizes)
if !valid {