# SPDX-License-Identifier: GPL-2.0-or-later
# SPDX-FileCopyrightText: Copyright (C) 2026 Max A. Vilkki a.k.a. codise
#
# libebur128 (MIT) from lib/libebur128 (v1.2.6): EBU R128 loudness and true peak, for
# auto-normalize and recording level checks.

set(ebur128Dir "${FLIPSID_LIB_DIR}/libebur128/ebur128")

add_library(ext_ebur128 STATIC "${ebur128Dir}/ebur128.c")
add_library(ext::ebur128 ALIAS ext_ebur128)

target_include_directories(ext_ebur128 SYSTEM PUBLIC "${ebur128Dir}")

# ebur128.c needs <sys/queue.h>, which MSVC lacks. Upstream probes for the system one;
# we always use the bundled copy so every platform builds the same code.
target_include_directories(ext_ebur128 SYSTEM PRIVATE "${ebur128Dir}/queue")

if(MSVC)
    target_compile_definitions(ext_ebur128 PRIVATE _USE_MATH_DEFINES)
endif()

# Without SSE2 (arm64), ebur128.c flushes denormals by hand, which is correct, but it
# also says so with a #warning in every build.
if(CMAKE_C_COMPILER_ID MATCHES "Clang")
    target_compile_options(ext_ebur128 PRIVATE "-Wno-#warnings")
elseif(CMAKE_C_COMPILER_ID STREQUAL "GNU")
    target_compile_options(ext_ebur128 PRIVATE -Wno-cpp)
endif()
if(UNIX AND NOT APPLE)
    target_link_libraries(ext_ebur128 PUBLIC m)
endif()

flipsid_configure_third_party(ext_ebur128)
