@@ -532,6 +532,7 @@ def safe_remove_sdkconfig_files():
532532flag_custom_component_remove = False
533533flag_custom_component_add = False
534534flag_lib_ignore = False
535+ flag_lto = False
535536
536537if mcu == "esp32c2" :
537538 flag_custom_sdkconfig = True
@@ -600,6 +601,18 @@ def has_psram_config():
600601
601602 new_build_unflags = build_unflags .split ()
602603 env .Replace (BUILD_UNFLAGS = new_build_unflags )
604+
605+ # Check if BUILD_FLAGS contains -flto=auto
606+ build_flags = env .get ('BUILD_FLAGS' , [])
607+ if isinstance (build_flags , str ):
608+ build_flags = build_flags .split ()
609+
610+ if '-flto=auto' in build_flags :
611+ # Remove -flto=auto from BUILD_FLAGS
612+ build_flags = [flag for flag in build_flags if flag != '-flto=auto' ]
613+ env .Replace (BUILD_FLAGS = build_flags )
614+ flag_lto = True
615+ print ("*** Detected -flto=auto in BUILD_FLAGS, will apply LTO management ***" )
603616
604617
605618def get_MD5_hash (phrase ):
@@ -916,6 +929,13 @@ def get_frameworks_in_current_env():
916929 from component_manager import ComponentManager
917930 component_manager = ComponentManager (env )
918931 component_manager .handle_component_settings ()
932+
933+ # Handle LTO flags if flag_lto is set
934+ if flag_lto :
935+ # First remove existing -fno-lto flags, then add LTO flags
936+ component_manager .remove_lto_flags ()
937+ component_manager .add_lto_flags ()
938+
919939 silent_action = env .Action (component_manager .restore_pioarduino_build_py )
920940 # silence scons command output
921941 silent_action .strfunction = lambda target , source , env : ''
0 commit comments