# SPDX-License-Identifier: GPL-2.0-or-later
# SPDX-FileCopyrightText: Copyright (C) 2026 Max A. Vilkki a.k.a. codise
#
# miniaudio (public domain or MIT-0) from lib/miniaudio (0.11.25): audio output and
# capture devices, device enumeration, and the data converter/resampler (DESIGN §2.1).

set(miniaudioDir "${FLIPSID_LIB_DIR}/miniaudio")

# miniaudio.c defines MINIAUDIO_IMPLEMENTATION and includes miniaudio.h.
add_library(ext_miniaudio STATIC "${miniaudioDir}/miniaudio.c")
add_library(ext::miniaudio ALIAS ext_miniaudio)

target_include_directories(ext_miniaudio SYSTEM PUBLIC "${miniaudioDir}")

# Leave out the decoders, encoders, generators, engine, node graph and resource manager;
# we only move samples between our player and a device. PUBLIC because these change what
# miniaudio.h declares, and every user of it has to agree with the implementation.
target_compile_definitions(ext_miniaudio PUBLIC
    MA_NO_DECODING
    MA_NO_ENCODING
    MA_NO_GENERATION
    MA_NO_ENGINE
    MA_NO_NODE_GRAPH
    MA_NO_RESOURCE_MANAGER
)

if(APPLE)
    # By default miniaudio dlopens CoreAudio at run time, which can fail notarization
    # (miniaudio docs §2.2). Linking the frameworks costs nothing in size: they're part
    # of the OS.
    target_compile_definitions(ext_miniaudio PUBLIC MA_NO_RUNTIME_LINKING)
    target_link_libraries(ext_miniaudio PUBLIC
        "-framework CoreFoundation"
        "-framework CoreAudio"
        "-framework AudioToolbox"
    )
elseif(UNIX)
    # ALSA, PulseAudio and JACK stay dlopened at run time, so there are no link-time
    # audio dependencies and the binary runs on systems that lack some of them.
    find_package(Threads REQUIRED)
    target_link_libraries(ext_miniaudio PUBLIC ${CMAKE_DL_LIBS} Threads::Threads m)
endif()
# Windows needs nothing: WASAPI and friends are loaded at run time.

flipsid_configure_third_party(ext_miniaudio)
