idf_component_register(SRCS "radio.c"
    PRIV_REQUIRES bt nvs_flash esp_hw_support esp_timer esp_driver_gpio freertos)

if(NOT IDF_TARGET STREQUAL "esp32c6")
    message(FATAL_ERROR "This board requires idf.py set-target esp32c6")
endif()

if(NOT CONFIG_BT_NIMBLE_NVS_PERSIST OR NOT CONFIG_BT_NIMBLE_SM_SC
        OR CONFIG_BT_NIMBLE_SM_LEGACY OR CONFIG_BT_NIMBLE_SM_SC_DEBUG_KEYS
        OR NOT CONFIG_BT_NIMBLE_SM_SC_ONLY EQUAL 1 OR NOT CONFIG_BT_NIMBLE_SM_LVL EQUAL 4)
    message(FATAL_ERROR "Use the bonded, authenticated Secure Connections settings from sdkconfig.defaults")
endif()

find_program(SWIFTC NAMES swiftc REQUIRED)
set(swift_flags -target riscv32-none-none-eabi
    -enable-experimental-feature Embedded -wmo -Osize -parse-as-library
    -Xcc -march=rv32imac_zicsr_zifencei -Xcc -mabi=ilp32 -Xcc -fno-pic -Xcc -fno-pie)
set(device_source "${CMAKE_CURRENT_LIST_DIR}/../Sources/TransponderDevice/DeviceSession.swift")
set(device_module "${CMAKE_CURRENT_BINARY_DIR}/TransponderDevice.swiftmodule")
add_custom_command(
    OUTPUT "${device_module}"
    COMMAND "${SWIFTC}" ${swift_flags} -module-name TransponderDevice
        -emit-module -emit-module-path "${device_module}" "${device_source}"
    DEPENDS "${device_source}" "${SWIFTC}"
    VERBATIM)

# Embedded Swift emits imported implementations and the runtime in the client's object.
set(swift_source "${CMAKE_CURRENT_LIST_DIR}/Indicator.swift")
set(swift_object "${CMAKE_CURRENT_BINARY_DIR}/Indicator.o")
add_custom_command(
    OUTPUT "${swift_object}"
    COMMAND "${SWIFTC}" ${swift_flags} -I "${CMAKE_CURRENT_BINARY_DIR}"
        -Xfrontend -function-sections -c "${swift_source}" -o "${swift_object}"
    # Place Swift module metadata in flash alongside the other read-only data.
    COMMAND "${CMAKE_OBJCOPY}" --rename-section .swift_modhash=.rodata.swift_modhash "${swift_object}"
    DEPENDS "${swift_source}" "${device_module}" "${SWIFTC}"
    VERBATIM)
set_source_files_properties("${swift_object}" PROPERTIES EXTERNAL_OBJECT TRUE GENERATED TRUE)
target_sources(${COMPONENT_LIB} PRIVATE "${swift_object}")
