Skip to content

Commit

Permalink
Enable Link Time Optimisation
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
deece committed Mar 15, 2024
1 parent 7b366d4 commit f7754eb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
27 changes: 27 additions & 0 deletions pio-scripts/lto.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Import('env')

env.Replace(AR=env['AR'].replace('elf-ar', 'elf-gcc-ar'))
env.Replace(RANLIB=env['RANLIB'].replace('elf-ranlib', 'elf-gcc-ranlib'))

# Something later clobbers AR & RANLIB, so until https://github.com/platformio/platform-espressif32/pull/1329
# is available, wrap the replace function to protect them

# Save a reference to the original env.Replace()
original_replace = env.Replace

def create_replace_wrapper(env):
def replace_wrapper(**kw):
if 'AR' in kw:
kw.pop("AR")
if 'RANLIB' in kw:
kw.pop("RANLIB")

original_replace(**kw)

return replace_wrapper

# Replace the env.Replace with the wrapper
env.Replace = create_replace_wrapper(env)

print(f"Updated AR: {env['AR']}")
print(f"Updated RANLIB: {env['RANLIB']}")
4 changes: 4 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ debug_flags = -D DEBUG=1 -D WLED_DEBUG
# This reduces the OTA size with ~45KB, so it's especially useful on low memory boards (512k/1m).
# ------------------------------------------------------------------------------
build_flags =
-flto
-Wno-attributes
-DMQTT_MAX_PACKET_SIZE=1024
-DSECURE_CLIENT=SECURE_CLIENT_BEARSSL
Expand All @@ -103,6 +104,8 @@ build_flags =
-D DECODE_LG=true
-DWLED_USE_MY_CONFIG



build_unflags =

build_flags_esp8266 = ${common.build_flags} ${esp8266.build_flags}
Expand All @@ -121,6 +124,7 @@ extra_scripts =
post:pio-scripts/strip-floats.py
pre:pio-scripts/user_config_copy.py
pre:pio-scripts/build_ui.py
pre:pio-scripts/lto.py

# ------------------------------------------------------------------------------
# COMMON SETTINGS:
Expand Down
2 changes: 2 additions & 0 deletions wled00/wled00.ino
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
*/
#include "wled.h"

void setup() __attribute__((used));
void setup() {
WLED::instance().setup();
}

void loop() __attribute__((used));
void loop() {
WLED::instance().loop();
}

0 comments on commit f7754eb

Please sign in to comment.