# MicroGoose ESP-IDF component: the Goose runtime + POSIX shim + HAL.
# goose_runtime.c is written by `goose --emit-runtime` (mg.py does it).
idf_component_register(
    SRCS "goose_runtime.c" "mg_compat.c" "mg_hal.c" "mg_main.c"
    INCLUDE_DIRS "include" "compat"
    REQUIRES esp_driver_gpio esp_driver_rmt esp_driver_tsens esp_adc esp_timer
             esp_hw_support esp_system freertos heap pthread
)

# Same prefix for the runtime here and for the generated program (main/).
set(MG_PREFIX "${CMAKE_CURRENT_SOURCE_DIR}/compat/mg_prefix.h")
# -fno-tree-sra/-fno-ipa-sra: works around an Xtensa GCC 15.2 miscompile
# (esp-15.2.0_20251204): SRA turns a 64-bit field of a packed struct (every
# Goose array header, gs_rhdr) into a scalar that keeps align(1), and the
# Xtensa backend then passes it in an odd register pair (a11:a12 instead of
# a12:a13), so the callee reads garbage. See docs/FINDINGS.md.
target_compile_options(${COMPONENT_LIB} PUBLIC
    "SHELL:-include ${MG_PREFIX}" -fno-tree-sra -fno-ipa-sra
    -Wno-unused-function -Wno-unused-variable -Wno-unused-but-set-variable)
set_source_files_properties(goose_runtime.c PROPERTIES COMPILE_OPTIONS
    "-O2;-Wno-error;-w")

# Park instead of rebooting when a program ends; name data stack overflows.
target_link_libraries(${COMPONENT_LIB} INTERFACE
    "-Wl,--wrap=_exit" "-Wl,--wrap=_Exit" "-Wl,--wrap=exit"
    "-Wl,--wrap=esp_panic_handler")
