# Modern ESP-IDF (idf_component_register) wrapper around the vendored
# libsmb2 checkout at E:\esp32\src\third_party\libsmb2 (upstream
# sahlberg/libsmb2, LGPL-2.1). Vendored outside components/ deliberately:
# ESP-IDF auto-scans every immediate subdirectory of components/ as a
# candidate component, and libsmb2's own CMakeLists.txt predates
# idf_component_register (calls add_definitions() at the top level, which
# ESP-IDF 6.1's two-pass component requirement scan rejects with
# "add_definitions command is not scriptable") — so it must never be
# auto-discovered directly. Source list and compile definitions below are
# copied from the upstream lib/CMakeLists.txt ESP_PLATFORM branch, minus
# aes_apple.c/krb5-wrapper.c (excluded there too for this platform).

set(LIBSMB2_DIR "${CMAKE_CURRENT_LIST_DIR}/../../third_party/libsmb2")
set(LIBSMB2_LIB_DIR "${LIBSMB2_DIR}/lib")

set(LIBSMB2_SOURCES
    aes.c
    aes_reference.c
    aes128ccm.c
    alloc.c
    asn1-ber.c
    compat.c
    libsmb2-dcerpc.c
    libsmb2-dcerpc-srvsvc.c
    errors.c
    hmac.c
    hmac-md5.c
    init.c
    libsmb2.c
    md4c.c
    md5.c
    ntlmssp.c
    pdu.c
    sha1.c
    sha224-256.c
    sha384-512.c
    smb2-cmd-close.c
    smb2-cmd-create.c
    smb2-cmd-echo.c
    smb2-cmd-error.c
    smb2-cmd-flush.c
    smb2-cmd-ioctl.c
    smb2-cmd-lock.c
    smb2-cmd-logoff.c
    smb2-cmd-negotiate.c
    smb2-cmd-notify-change.c
    smb2-cmd-oplock-break.c
    smb2-cmd-query-directory.c
    smb2-cmd-query-info.c
    smb2-cmd-read.c
    smb2-cmd-session-setup.c
    smb2-cmd-set-info.c
    smb2-cmd-tree-connect.c
    smb2-cmd-tree-disconnect.c
    smb2-cmd-write.c
    smb2-data-file-info.c
    smb2-data-filesystem-info.c
    smb2-data-security-descriptor.c
    smb2-data-reparse-point.c
    smb2-share-enum.c
    smb3-seal.c
    smb2-signing.c
    socket.c
    spnego-wrapper.c
    sync.c
    timestamps.c
    unicode.c
    usha.c
)

list(TRANSFORM LIBSMB2_SOURCES PREPEND "${LIBSMB2_LIB_DIR}/")

idf_component_register(
    SRCS ${LIBSMB2_SOURCES}
    INCLUDE_DIRS
        "${LIBSMB2_DIR}/include"
        "${LIBSMB2_DIR}/include/smb2"
        "${LIBSMB2_DIR}/include/esp"
    PRIV_INCLUDE_DIRS
        "${LIBSMB2_LIB_DIR}"
    REQUIRES
        lwip
        mbedtls
        sock_utils
)

target_compile_definitions(${COMPONENT_LIB} PRIVATE
    HAVE_CONFIG_H
    NEED_READV
    NEED_WRITEV
    NEED_GETLOGIN_R
    NEED_RANDOM
    NEED_SRANDOM
    "_U_=__attribute__((unused))"
)

# Vendored upstream code, built as-is: relax -Werror for warning classes that
# are harmless in practice (e.g. a tautological unsigned comparison in
# libsmb2.c) rather than patching third-party sources to satisfy this
# project's stricter default warning flags.
target_compile_options(${COMPONENT_LIB} PRIVATE
    -Wno-error=type-limits
)
