diff --git a/esphome/components/libretiny/__init__.py b/esphome/components/libretiny/__init__.py index 5d87dee07a..5d630fec6c 100644 --- a/esphome/components/libretiny/__init__.py +++ b/esphome/components/libretiny/__init__.py @@ -526,6 +526,10 @@ async def component_to_code(config): # RAM-executable output section and prints a post-link placement summary. if FAMILY_COMPONENT[config[CONF_FAMILY]] != COMPONENT_BK72XX: cg.add_platformio_option("extra_scripts", ["pre:patch_linker.py"]) + # Match the 1-byte enums of the LN882H SDK's prebuilt WiFi library. + if FAMILY_COMPONENT[config[CONF_FAMILY]] == COMPONENT_LN882X: + cg.add_build_unflag("-fno-short-enums") + cg.add_build_flag("-fshort-enums") # dummy version code cg.add_define("USE_ARDUINO_VERSION_CODE", cg.RawExpression("VERSION_CODE(0, 0, 0)")) # decrease web server stack size (16k words -> 4k words) diff --git a/esphome/components/libretiny/hal.cpp b/esphome/components/libretiny/hal.cpp index 01b276005d..f97a5a19a7 100644 --- a/esphome/components/libretiny/hal.cpp +++ b/esphome/components/libretiny/hal.cpp @@ -12,6 +12,12 @@ // libretiny component's API. namespace esphome::libretiny {} // namespace esphome::libretiny +#if defined(USE_LN882X) && !defined(CLANG_TIDY) // clang-tidy runs without the PlatformIO flags +// Fail the build if the -fshort-enums swap in __init__.py stops working. +enum Ln882xEnumSizeCheck { LN882X_ENUM_SIZE_CHECK }; +static_assert(sizeof(Ln882xEnumSizeCheck) == 1, "LN882x must build with -fshort-enums to match the SDK"); +#endif + namespace esphome { // yield(), delay(), micros(), millis(), millis_64(), delayMicroseconds(), diff --git a/tests/component_tests/libretiny/__init__.py b/tests/component_tests/libretiny/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/component_tests/libretiny/config/bk72xx.yaml b/tests/component_tests/libretiny/config/bk72xx.yaml new file mode 100644 index 0000000000..35450f1039 --- /dev/null +++ b/tests/component_tests/libretiny/config/bk72xx.yaml @@ -0,0 +1,5 @@ +esphome: + name: test + +bk72xx: + board: generic-bk7231n-qfn32-tuya diff --git a/tests/component_tests/libretiny/config/ln882x.yaml b/tests/component_tests/libretiny/config/ln882x.yaml new file mode 100644 index 0000000000..4d0381737a --- /dev/null +++ b/tests/component_tests/libretiny/config/ln882x.yaml @@ -0,0 +1,5 @@ +esphome: + name: test + +ln882x: + board: generic-ln882h diff --git a/tests/component_tests/libretiny/test_short_enums.py b/tests/component_tests/libretiny/test_short_enums.py new file mode 100644 index 0000000000..185c47c506 --- /dev/null +++ b/tests/component_tests/libretiny/test_short_enums.py @@ -0,0 +1,27 @@ +"""LN882x builds with 1-byte enums to match the SDK's prebuilt libraries; +the other LibreTiny families keep LibreTiny's own enum size.""" + +from __future__ import annotations + +from collections.abc import Callable +from pathlib import Path + +from esphome.core import CORE + + +def test_ln882x_swaps_in_short_enums( + generate_main: Callable[[str | Path], str], + component_config_path: Callable[[str], Path], +) -> None: + generate_main(component_config_path("ln882x.yaml")) + assert "-fno-short-enums" in CORE.build_unflags + assert "-fshort-enums" in CORE.build_flags + + +def test_bk72xx_keeps_libretiny_enums( + generate_main: Callable[[str | Path], str], + component_config_path: Callable[[str], Path], +) -> None: + generate_main(component_config_path("bk72xx.yaml")) + assert "-fno-short-enums" not in CORE.build_unflags + assert "-fshort-enums" not in CORE.build_flags