Skip to content

Commit fd76029

Browse files
authored
Merge pull request #452 from sej7278/master
Added LTO flags etc. to provide smaller/faster AVR code.
2 parents 7c28446 + 0b9ba23 commit fd76029

File tree

2 files changed

+38
-20
lines changed

2 files changed

+38
-20
lines changed

Arduino.mk

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -392,10 +392,6 @@ ifndef OBJDUMP_NAME
392392
OBJDUMP_NAME = avr-objdump
393393
endif
394394

395-
ifndef AR_NAME
396-
AR_NAME = avr-ar
397-
endif
398-
399395
ifndef SIZE_NAME
400396
SIZE_NAME = avr-size
401397
endif
@@ -1031,16 +1027,32 @@ ifneq ($(CATERINA),)
10311027
CPPFLAGS += -DUSB_VID=$(USB_VID) -DUSB_PID=$(USB_PID)
10321028
endif
10331029

1030+
# avr-gcc version that we can do maths on
1031+
CC_VERNUM = $(shell $(CC) -dumpversion | sed 's/\.//g')
1032+
1033+
# moved from above so we can find version-dependant ar
1034+
ifndef AR_NAME
1035+
ifeq ($(shell expr $(CC_VERNUM) '>' 480), 1)
1036+
AR_NAME = avr-gcc-ar
1037+
else
1038+
AR_NAME = avr-ar
1039+
endif
1040+
endif
1041+
10341042
ifndef CFLAGS_STD
1035-
CFLAGS_STD =
1043+
ifeq ($(shell expr $(CC_VERNUM) '>' 480), 1)
1044+
CFLAGS_STD = -std=gnu11 -flto -fno-fat-lto-objects
1045+
else
1046+
CFLAGS_STD =
1047+
endif
10361048
$(call show_config_variable,CFLAGS_STD,[DEFAULT])
10371049
else
10381050
$(call show_config_variable,CFLAGS_STD,[USER])
10391051
endif
10401052

10411053
ifndef CXXFLAGS_STD
1042-
ifeq ($(shell expr $(ARDUINO_VERSION) '>' 150), 1)
1043-
CXXFLAGS_STD = -std=gnu++11 -fno-threadsafe-statics
1054+
ifeq ($(shell expr $(CC_VERNUM) '>' 480), 1)
1055+
CXXFLAGS_STD = -std=gnu++11 -fno-threadsafe-statics -flto
10441056
else
10451057
CXXFLAGS_STD =
10461058
endif
@@ -1050,9 +1062,15 @@ else
10501062
endif
10511063

10521064
CFLAGS += $(CFLAGS_STD)
1053-
CXXFLAGS += -fno-exceptions $(CXXFLAGS_STD)
1065+
CXXFLAGS += -fpermissive -fno-exceptions $(CXXFLAGS_STD)
10541066
ASFLAGS += -x assembler-with-cpp
1067+
ifeq ($(shell expr $(CC_VERNUM) '>' 480), 1)
1068+
ASFLAGS += -flto
1069+
endif
10551070
LDFLAGS += -$(MCU_FLAG_NAME)=$(MCU) -Wl,--gc-sections -O$(OPTIMIZATION_LEVEL)
1071+
ifeq ($(shell expr $(CC_VERNUM) '>' 480), 1)
1072+
LDFLAGS += -flto -fuse-linker-plugin
1073+
endif
10561074
SIZEFLAGS ?= --mcu=$(MCU) -C
10571075

10581076
# for backwards compatibility, grab ARDUINO_PORT if the user has it set

arduino-mk-vars.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ OBJDUMP_NAME = pic32-objdump
822822

823823
Archive utility.
824824

825-
Defaults to `avr-ar`
825+
Defaults to `avr-ar` unless you're using toolchain > 4.8.0 in which case we use avr-gcc-ar.
826826

827827
**Example:**
828828

@@ -894,22 +894,22 @@ OPTIMIZATION_LEVEL = 3
894894

895895
Controls, *exclusively*, which C standard is to be used for compilation.
896896

897-
Defaults to `undefined`
897+
Defaults to `undefined` on 1.0.x or `-std=gnu11 -flto -fno-fat-lto-objects` on 1.5+ or if you install AVR toolchain > 4.8.0
898898

899899
Possible values:
900900

901-
* With `avr-gcc 4.3`, shipped with the Arduino IDE:
901+
* With `avr-gcc 4.3`, shipped with the 1.0 Arduino IDE:
902902
* `undefined`
903903
* `-std=c99`
904904
* `-std=gnu89` - This is the default for C code
905905
* `-std=gnu99`
906-
* With `avr-gcc 4.7, 4.8 or 4.9`, installed by you
906+
* With `avr-gcc 4.7, 4.8 or 4.9`, installed by you or 1.5+ IDE:
907907
* `undefined`
908908
* `-std=c99`
909909
* `-std=c11`
910-
* `-std=gnu89` - This is the default for C code
910+
* `-std=gnu89`
911911
* `-std=gnu99`
912-
* `-std=gnu11`
912+
* `-std=gnu11 -flto -fno-fat-lto-objects` - This is the default for C code
913913

914914
For more information, please refer to the [Options Controlling C Dialect](https://gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html)
915915

@@ -929,24 +929,24 @@ CFLAGS_STD = = -std=gnu89
929929

930930
Controls, *exclusively*, which C++ standard is to be used for compilation.
931931

932-
Defaults to `undefined` on 1.0 or `-std=gnu++11 -fno-threadsafe-statics` on 1.5+
932+
Defaults to `undefined` on 1.0 or `-std=gnu++11 -fno-threadsafe-statics -flto` on AVR toolchain > 4.8.0 (e.g. IDE 1.5+)
933933

934934
Possible values:
935935

936-
* With `avr-gcc 4.3`, shipped with the Arduino IDE:
936+
* With `avr-gcc 4.3`, shipped with the 1.0 Arduino IDE:
937937
* `undefined`
938938
* `-std=c++98`
939939
* `-std=c++0x`
940940
* `-std=gnu++98` - This is the default for C code
941941
* `-std=gnu++0x`
942-
* With `avr-gcc 4.7, 4.8 or 4.9`, installed by you
942+
* With `avr-gcc 4.7, 4.8 or 4.9`, installed by you or 1.5+ IDE:
943943
* `undefined`
944944
* `-std=c++98`
945945
* `-std=c++11`
946946
* `-std=c++1y`
947947
* `-std=c++14`
948-
* `-std=gnu++98` - This is the default for C++ code
949-
* `-std=gnu++11`
948+
* `-std=gnu++98`
949+
* `-std=gnu++11 -fno-threadsafe-statics -flto` - This is the default for C++ code
950950
* `-std=gnu++1y`
951951
* `-std=gnu++14`
952952

@@ -988,7 +988,7 @@ CFLAGS += -my-c-only-flag
988988
Flags passed to the compiler for files compiled as C++. Add more flags to this
989989
variable using `+=`.
990990

991-
Defaults to all flags required for a typical build.
991+
Defaults to `-fpermissive -fno-exceptions`
992992

993993
**Example:**
994994

0 commit comments

Comments
 (0)