Skip to content

Commit f7754eb

Browse files
committed
Enable Link Time Optimisation
ref: https://gcc.gnu.org/wiki/LinkTimeOptimization Link Time Optimisation defers final compilation until the link stage, which allows more aggressive inlining and optimisations to occur, as more information is known at link time vs when each object is compiled. For the esp32dev platform, I have the following size reductions: Without LTO: 1313024 .pio/build/esp32dev/firmware.bin With LTO: 1214256 .pio/build/esp32dev/firmware.bin Signed-off-by: Alastair D'Silva <alastair@d-silva.org>
1 parent 7b366d4 commit f7754eb

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

pio-scripts/lto.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Import('env')
2+
3+
env.Replace(AR=env['AR'].replace('elf-ar', 'elf-gcc-ar'))
4+
env.Replace(RANLIB=env['RANLIB'].replace('elf-ranlib', 'elf-gcc-ranlib'))
5+
6+
# Something later clobbers AR & RANLIB, so until https://github.com/platformio/platform-espressif32/pull/1329
7+
# is available, wrap the replace function to protect them
8+
9+
# Save a reference to the original env.Replace()
10+
original_replace = env.Replace
11+
12+
def create_replace_wrapper(env):
13+
def replace_wrapper(**kw):
14+
if 'AR' in kw:
15+
kw.pop("AR")
16+
if 'RANLIB' in kw:
17+
kw.pop("RANLIB")
18+
19+
original_replace(**kw)
20+
21+
return replace_wrapper
22+
23+
# Replace the env.Replace with the wrapper
24+
env.Replace = create_replace_wrapper(env)
25+
26+
print(f"Updated AR: {env['AR']}")
27+
print(f"Updated RANLIB: {env['RANLIB']}")

platformio.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ debug_flags = -D DEBUG=1 -D WLED_DEBUG
8787
# This reduces the OTA size with ~45KB, so it's especially useful on low memory boards (512k/1m).
8888
# ------------------------------------------------------------------------------
8989
build_flags =
90+
-flto
9091
-Wno-attributes
9192
-DMQTT_MAX_PACKET_SIZE=1024
9293
-DSECURE_CLIENT=SECURE_CLIENT_BEARSSL
@@ -103,6 +104,8 @@ build_flags =
103104
-D DECODE_LG=true
104105
-DWLED_USE_MY_CONFIG
105106

107+
108+
106109
build_unflags =
107110

108111
build_flags_esp8266 = ${common.build_flags} ${esp8266.build_flags}
@@ -121,6 +124,7 @@ extra_scripts =
121124
post:pio-scripts/strip-floats.py
122125
pre:pio-scripts/user_config_copy.py
123126
pre:pio-scripts/build_ui.py
127+
pre:pio-scripts/lto.py
124128

125129
# ------------------------------------------------------------------------------
126130
# COMMON SETTINGS:

wled00/wled00.ino

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
*/
1313
#include "wled.h"
1414

15+
void setup() __attribute__((used));
1516
void setup() {
1617
WLED::instance().setup();
1718
}
1819

20+
void loop() __attribute__((used));
1921
void loop() {
2022
WLED::instance().loop();
2123
}

0 commit comments

Comments
 (0)