# Vendored libmysofa 1.3.5 (https://github.com/hoene/libmysofa), BSD-3-Clause, see LICENSE.
# Only the library sources are kept; tests, data and the upstream build are dropped.

# libmysofa needs zlib. macOS ships it; on Windows it's built here from the official release, from
# its sources directly (zlib's own CMake file is too old for CMake 4).
if(WIN32)
    include(FetchContent)
    FetchContent_Declare(
        zlib
        URL https://github.com/madler/zlib/releases/download/v1.3.1/zlib-1.3.1.tar.gz
        URL_HASH SHA256=9a93b2b7dfdac77ceba5a558a580e74667dd6fede4585b91eefb60f03b72df23
        SOURCE_SUBDIR do-not-use-zlibs-cmake)
    FetchContent_MakeAvailable(zlib)

    set(zlib_sources adler32 compress crc32 deflate gzclose gzlib gzread gzwrite infback inffast
        inflate inftrees trees uncompr zutil)
    list(TRANSFORM zlib_sources PREPEND "${zlib_SOURCE_DIR}/")
    list(TRANSFORM zlib_sources APPEND ".c")
    add_library(orbit_zlib STATIC ${zlib_sources})
    target_include_directories(orbit_zlib PUBLIC "${zlib_SOURCE_DIR}")
    target_compile_definitions(orbit_zlib PRIVATE _CRT_SECURE_NO_WARNINGS _CRT_NONSTDC_NO_DEPRECATE)
    target_compile_options(orbit_zlib PRIVATE /w)
    add_library(ZLIB::ZLIB ALIAS orbit_zlib)
else()
    find_package(ZLIB REQUIRED)
endif()

add_library(mysofa STATIC
    src/hrtf/reader.c
    src/hdf/superblock.c
    src/hdf/dataobject.c
    src/hdf/btree.c
    src/hdf/fractalhead.c
    src/hdf/gunzip.c
    src/hdf/gcol.c
    src/hrtf/check.c
    src/hrtf/spherical.c
    src/hrtf/lookup.c
    src/hrtf/tools.c
    src/hrtf/kdtree.c
    src/hrtf/neighbors.c
    src/hrtf/interpolate.c
    src/hrtf/resample.c
    src/hrtf/loudness.c
    src/hrtf/minphase.c
    src/hrtf/easy.c
    src/hrtf/cache.c
    src/resampler/speex_resampler.c)

set_target_properties(mysofa PROPERTIES C_STANDARD 99 POSITION_INDEPENDENT_CODE ON)
target_include_directories(mysofa PUBLIC src/hrtf PRIVATE src)
target_link_libraries(mysofa PRIVATE ZLIB::ZLIB)
if(NOT WIN32)
    target_link_libraries(mysofa PRIVATE m)
endif()
# Third-party code: keep its warnings out of our build output.
if(MSVC)
    target_compile_options(mysofa PRIVATE /w)
    target_compile_definitions(mysofa PRIVATE _CRT_SECURE_NO_WARNINGS _USE_MATH_DEFINES)
else()
    target_compile_options(mysofa PRIVATE -w)
endif()
