aboutsummaryrefslogtreecommitdiffhomepage
path: root/compileopts/config.go
diff options
context:
space:
mode:
authorAyke van Laethem <[email protected]>2021-11-01 19:11:04 +0100
committerRon Evans <[email protected]>2021-11-03 23:03:44 +0100
commit29206cf0a4a8efcdabe50306090fe6125f6b2d0c (patch)
tree2df5e3376a0a8b026d6f819f35e2a80880c7b039 /compileopts/config.go
parentc2165f74d81a2ed1b1649e710b73f1c613a54185 (diff)
downloadtinygo-29206cf0a4a8efcdabe50306090fe6125f6b2d0c.tar.gz
tinygo-29206cf0a4a8efcdabe50306090fe6125f6b2d0c.zip
targets: add CPU property everywhere
This is for consistency with Clang, which always adds a CPU flag even if it's not specified in CFLAGS. This commit also adds some tests to make sure the Clang target-cpu matches the CPU property in the JSON files. This does have an effect on the generated binaries. The effect is very small though: on average just 0.2% increase in binary size, apparently because Cortex-M3 and Cortex-M4 are compiled a bit differently. However, when rebased on top of https://github.com/tinygo-org/tinygo/pull/2218 (minsize), the difference drops to -0.1% (a slight decrease on average).
Diffstat (limited to 'compileopts/config.go')
-rw-r--r--compileopts/config.go13
1 files changed, 13 insertions, 0 deletions
diff --git a/compileopts/config.go b/compileopts/config.go
index 7da0323df..a084403c7 100644
--- a/compileopts/config.go
+++ b/compileopts/config.go
@@ -220,6 +220,19 @@ func (c *Config) CFlags() []string {
cflags = append(cflags, "-O"+c.Options.Opt)
// Set the LLVM target triple.
cflags = append(cflags, "--target="+c.Triple())
+ // Set the -mcpu (or similar) flag.
+ if c.Target.CPU != "" {
+ if c.GOARCH() == "amd64" || c.GOARCH() == "386" {
+ // x86 prefers the -march flag (-mcpu is deprecated there).
+ cflags = append(cflags, "-march="+c.Target.CPU)
+ } else if strings.HasPrefix(c.Triple(), "avr") {
+ // AVR MCUs use -mmcu instead of -mcpu.
+ cflags = append(cflags, "-mmcu="+c.Target.CPU)
+ } else {
+ // The rest just uses -mcpu.
+ cflags = append(cflags, "-mcpu="+c.Target.CPU)
+ }
+ }
return cflags
}