From 9d5be4d677f9143efdd5cf65c7ae9cd2876efe0f Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 31 Jan 2026 23:05:28 -0600 Subject: [PATCH] wip --- esphome/components/esp8266/__init__.py | 6 +++ .../esp8266/remove_float_printf.py.script | 45 +++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 esphome/components/esp8266/remove_float_printf.py.script diff --git a/esphome/components/esp8266/__init__.py b/esphome/components/esp8266/__init__.py index c7b5d5c130d..2a32c13862f 100644 --- a/esphome/components/esp8266/__init__.py +++ b/esphome/components/esp8266/__init__.py @@ -205,6 +205,7 @@ async def to_code(config): "pre:testing_mode.py", "pre:exclude_updater.py", "pre:exclude_waveform.py", + "pre:remove_float_printf.py", "post:post_build.py", ], ) @@ -342,3 +343,8 @@ def copy_files() -> None: exclude_waveform_file, CORE.relative_build_path("exclude_waveform.py"), ) + remove_float_printf_file = dir / "remove_float_printf.py.script" + copy_file_if_changed( + remove_float_printf_file, + CORE.relative_build_path("remove_float_printf.py"), + ) diff --git a/esphome/components/esp8266/remove_float_printf.py.script b/esphome/components/esp8266/remove_float_printf.py.script new file mode 100644 index 00000000000..2824f0c3f31 --- /dev/null +++ b/esphome/components/esp8266/remove_float_printf.py.script @@ -0,0 +1,45 @@ +# pylint: disable=E0602 +Import("env") # noqa + +# Remove float printf/scanf support from linker flags +# The Arduino ESP8266 framework unconditionally adds: +# -u _printf_float -u _scanf_float +# This forces inclusion of float formatting code (~7KB) even when not used. +# +# ESPHome avoids %f format specifiers in logging to not require this code. +# This script removes those flags to save flash space. +# +# Savings: +# - _dtoa_r: ~3.4KB (double-to-ASCII conversion) +# - _strtod_l: ~3.7KB (string-to-double conversion) +# - _printf_float: ~1.3KB +# - _scanf_float: ~1.3KB +# - Additional float math helpers + + +def remove_float_printf_flags(source, target, env): + """Remove -u _printf_float and -u _scanf_float from linker flags. + + This is called as a pre-action before the link step. + """ + linkflags = env.get("LINKFLAGS", []) + new_linkflags = [] + i = 0 + + while i < len(linkflags): + flag = linkflags[i] + if flag == "-u" and i + 1 < len(linkflags): + next_flag = linkflags[i + 1] + if next_flag in ("_printf_float", "_scanf_float"): + print(f"ESPHome: Removing float printf support ({next_flag})") + i += 2 # Skip both -u and the symbol + continue + new_linkflags.append(flag) + i += 1 + + env.Replace(LINKFLAGS=new_linkflags) + + +# Register the callback to run before the link step +# This ensures it runs after the framework has added its flags +env.AddPreAction("$BUILD_DIR/${PROGNAME}.elf", remove_float_printf_flags)