From 18cce571a21ba23e4a281f3da26a618c7d146752 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Sun, 3 Nov 2019 11:55:51 +0100 Subject: [PATCH] main: move ldflags to compileopts --- compileopts/config.go | 21 +++++++++++++++++++++ main.go | 14 +------------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/compileopts/config.go b/compileopts/config.go index 4f55945c..c9557861 100644 --- a/compileopts/config.go +++ b/compileopts/config.go @@ -4,6 +4,7 @@ package compileopts import ( "fmt" + "strconv" "strings" "github.com/tinygo-org/tinygo/goenv" @@ -104,6 +105,26 @@ func (c *Config) CFlags() []string { return cflags } +// LDFlags returns the flags to pass to the linker. A few more flags are needed +// (like the one for the compiler runtime), but this represents the majority of +// the flags. +func (c *Config) LDFlags() []string { + root := goenv.Get("TINYGOROOT") + // Merge and adjust LDFlags. + ldflags := append([]string{}, c.Options.LDFlags...) + for _, flag := range c.Target.LDFlags { + ldflags = append(ldflags, strings.Replace(flag, "{root}", root, -1)) + } + ldflags = append(ldflags, "-L", root) + if c.Target.GOARCH == "wasm" { + // Round heap size to next multiple of 65536 (the WebAssembly page + // size). + heapSize := (c.Options.HeapSize + (65536 - 1)) &^ (65536 - 1) + ldflags = append(ldflags, "--initial-memory="+strconv.FormatInt(heapSize, 10)) + } + return ldflags +} + // DumpSSA returns whether to dump Go SSA while compiling (-dumpssa flag). Only // enable this for debugging. func (c *Config) DumpSSA() bool { diff --git a/main.go b/main.go index 2e5aae97..f2de471a 100644 --- a/main.go +++ b/main.go @@ -187,25 +187,13 @@ func Compile(pkgName, outpath string, spec *compileopts.TargetSpec, options *com } } - // Merge and adjust LDFlags. - ldflags := append([]string{}, options.LDFlags...) - for _, flag := range spec.LDFlags { - ldflags = append(ldflags, strings.Replace(flag, "{root}", root, -1)) - } - // Prepare link command. executable := filepath.Join(dir, "main") tmppath := executable // final file - ldflags = append(ldflags, "-o", executable, objfile, "-L", root) + ldflags := append(compilerConfig.LDFlags(), "-o", executable, objfile) if spec.RTLib == "compiler-rt" { ldflags = append(ldflags, librt) } - if spec.GOARCH == "wasm" { - // Round heap size to next multiple of 65536 (the WebAssembly page - // size). - heapSize := (options.HeapSize + (65536 - 1)) &^ (65536 - 1) - ldflags = append(ldflags, "--initial-memory="+strconv.FormatInt(heapSize, 10)) - } // Compile extra files. for i, path := range spec.ExtraFiles {