# BearSSL static library (vendored, MIT license)
# All .c files compiled as a single translation unit for best optimization

add_library(bearssl STATIC)

target_include_directories(bearssl
    PUBLIC inc
    PRIVATE src
)

# Collect all C source files
file(GLOB_RECURSE BEARSSL_SOURCES "src/*.c")
target_sources(bearssl PRIVATE ${BEARSSL_SOURCES})

# x86_64 Windows optimizations
if(WIN32 AND CMAKE_SIZEOF_VOID_P EQUAL 8)
    target_compile_definitions(bearssl PRIVATE
        BR_64
        BR_INT128
        BR_LE_UNALIGNED
        BR_USE_WIN32_RAND
        BR_USE_WIN32_TIME
    )
    # AES-NI and SSE2 are auto-detected by BearSSL on x86
    if(CMAKE_C_COMPILER_ID MATCHES "Clang|GNU")
        target_compile_options(bearssl PRIVATE -maes -msse2)
    endif()
elseif(UNIX)
    target_compile_definitions(bearssl PRIVATE
        BR_USE_URANDOM
        BR_USE_UNIX_TIME
    )
    if(CMAKE_SIZEOF_VOID_P EQUAL 8)
        target_compile_definitions(bearssl PRIVATE BR_64 BR_INT128 BR_LE_UNALIGNED)
    endif()
endif()

# Suppress warnings for vendored code
if(MSVC)
    target_compile_options(bearssl PRIVATE /W0)
else()
    target_compile_options(bearssl PRIVATE -w)
endif()
