idf_build_get_property(target IDF_TARGET)

set(target_folder "${target}")

set(srcs)

# On Linux the soc component is a simple wrapper, without much functionality
if(NOT ${target} STREQUAL "linux")
    set(srcs "lldesc.c"
            "dport_access_common.c"
            "${target_folder}/interrupts.c"
            "${target_folder}/gpio_periph.c")
endif()

set(includes "include" "${target_folder}")

if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${target_folder}/include")
    # miscellaneous headers, like definitions, man-made register headers, wrappers, etc.
    list(APPEND includes "${target_folder}/include")
endif()

# register headers that generated by script from CSV
if(CONFIG_IDF_TARGET_ESP32P4)
    if(CONFIG_ESP32P4_SELECTS_REV_LESS_V3)
        list(APPEND includes "${target_folder}/register/hw_ver1")
    else()
        list(APPEND includes "${target_folder}/register/hw_ver3")
    endif()
elseif(CONFIG_IDF_TARGET_ESP32H21) # TODO: ESP32H21 IDF-13923
    list(APPEND includes "${target_folder}/register")
    if(CONFIG_ESP32H21_SELECTS_REV_MP)
        list(APPEND includes "${target_folder}/register/hw_ver_mp")
    else()
        list(APPEND includes "${target_folder}/register/hw_ver_beta1")
    endif()
else()
    if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${target_folder}/register")
        list(APPEND includes "${target_folder}/register")
    endif()
endif()

if(target STREQUAL "esp32")
    list(APPEND srcs "${target_folder}/dport_access.c")
endif()

if(CONFIG_SOC_DEBUG_PROBE_SUPPORTED)
    list(APPEND srcs "${target_folder}/debug_probe_periph.c")
endif()

if(CONFIG_SOC_MPI_SUPPORTED)
    list(APPEND srcs "${target_folder}/mpi_periph.c")
endif()

if(CONFIG_SOC_BOD_SUPPORTED)
    list(APPEND srcs "${target_folder}/power_supply_periph.c")
endif()

idf_component_register(SRCS ${srcs}
                       INCLUDE_DIRS ${includes}
                       LDFRAGMENTS "linker.lf")

# For an embedded system, the MMU page size should always be defined statically
# For IDF, we define it according to the Flash size that user selects
# Replace this value in an adaptive way, if Kconfig isn't available on your platform
target_compile_definitions(${COMPONENT_LIB} INTERFACE SOC_MMU_PAGE_SIZE=CONFIG_MMU_PAGE_SIZE)

target_compile_definitions(${COMPONENT_LIB} INTERFACE SOC_XTAL_FREQ_MHZ=CONFIG_XTAL_FREQ)

if(target STREQUAL "esp32")
    # esp_dport_access_reg_read is added as an undefined symbol because otherwise
    # the linker can ignore dport_access.c as it would no other files depending on any symbols in it.
    set_property(TARGET ${COMPONENT_LIB} APPEND PROPERTY INTERFACE_LINK_LIBRARIES "-u esp_dport_access_reg_read")
endif()

if(NOT CONFIG_IDF_TARGET_LINUX)
    target_linker_script(${COMPONENT_LIB} INTERFACE "${target_folder}/ld/${target}.peripherals.ld")
endif()
