# ##############################################################################
# Target

add_library(pj_speex
  libspeex/bits.c
  libspeex/cb_search.c
  libspeex/exc_10_16_table.c
  libspeex/exc_10_32_table.c
  libspeex/exc_20_32_table.c
  libspeex/exc_5_256_table.c
  libspeex/exc_5_64_table.c
  libspeex/exc_8_128_table.c
  libspeex/fftwrap.c
  libspeex/filterbank.c
  libspeex/filters.c
  libspeex/gain_table.c
  libspeex/gain_table_lbr.c
  libspeex/hexc_10_32_table.c
  libspeex/hexc_table.c
  libspeex/high_lsp_tables.c
  libspeex/kiss_fft.c
  libspeex/kiss_fftr.c
  libspeex/lpc.c
  libspeex/lsp.c
  libspeex/lsp_tables_nb.c
  libspeex/ltp.c
  libspeex/mdf.c
  libspeex/modes.c
  libspeex/modes_wb.c
  libspeex/nb_celp.c
  libspeex/preprocess.c
  libspeex/quant_lsp.c
  libspeex/resample.c
  libspeex/sb_celp.c
  libspeex/smallft.c
  libspeex/speex.c
  libspeex/speex_callbacks.c
  libspeex/speex_header.c
  libspeex/scal.c
  libspeex/stereo.c
  libspeex/vbr.c
  libspeex/vq.c
  libspeex/window.c
)

# headers
target_sources(pj_speex
  PUBLIC
    FILE_SET HEADERS
      BASE_DIRS
        include
        "${CMAKE_CURRENT_SOURCE_DIR}/../build/speex"
      FILES
        include/speex/speex.h
        include/speex/speex_bits.h
        include/speex/speex_buffer.h
        include/speex/speex_callbacks.h
        include/speex/speex_echo.h
        include/speex/speex_header.h
        include/speex/speex_jitter.h
        include/speex/speex_preprocess.h
        include/speex/speex_resampler.h
        include/speex/speex_stereo.h
        include/speex/speex_types.h
        include/speex/speexdsp_types.h

        "${CMAKE_CURRENT_SOURCE_DIR}/../build/speex/speex/speex_config_types.h"
)

# Warnings in the bundled code, which is kept as upstream has it
target_compile_options(pj_speex
  PRIVATE
    $<$<C_COMPILER_ID:GNU>:-Wno-maybe-uninitialized>
    $<$<C_COMPILER_ID:Clang,AppleClang>:-Wno-shorten-64-to-32>
)

# ##############################################################################
# Dependencies

# pjlib - for config & log overrides
target_link_libraries(pj_speex PRIVATE pjlib)

if (NOT (WIN32 OR CYGWIN OR MINGW))
  # Speex calls libm functions (exp(), pow(), ...), a separate library
  # on these systems that the final link needs. Linked by name so that a
  # cross toolchain picks the libm matching its CPU flags; a Generic one
  # resolves -lm through its multilib specs, where find_library() does
  # not look.
  find_library(MATH_LIBRARY m)
  if(MATH_LIBRARY OR CMAKE_SYSTEM_NAME STREQUAL "Generic")
    target_link_libraries(pj_speex PUBLIC m)
  endif()
endif()

# ##############################################################################
# Configuration

target_include_directories(pj_speex BEFORE
  PRIVATE
    "${CMAKE_CURRENT_SOURCE_DIR}/../build/speex"
)

target_compile_definitions(pj_speex PRIVATE HAVE_CONFIG_H=1)
