# Voicegram for Android — native Ogg Vorbis encoder
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Android has no Vorbis encoder of its own (MediaRecorder writes Ogg only with Opus),
# so the app builds libogg and libvorbis from the Xiph.Org release sources.
#
# The sources are downloaded at configure time. To build offline, unpack them and pass
#   -DFETCHCONTENT_SOURCE_DIR_XIPH_OGG=/path/to/ogg-1.3.5
#   -DFETCHCONTENT_SOURCE_DIR_XIPH_VORBIS=/path/to/vorbis-1.3.7
# (in app/build.gradle.kts, externalNativeBuild.cmake.arguments).

cmake_minimum_required(VERSION 3.18)
project(voicegram C)

include(FetchContent)
if(POLICY CMP0135)
    cmake_policy(SET CMP0135 NEW) # extracted files get the extraction time
endif()

# SOURCE_SUBDIR points at a directory without a CMakeLists.txt, so FetchContent only
# downloads; the libraries are compiled below with an explicit, minimal source list.
FetchContent_Declare(xiph_ogg
    URL https://github.com/xiph/ogg/archive/refs/tags/v1.3.5.tar.gz
    SOURCE_SUBDIR do-not-configure)
FetchContent_Declare(xiph_vorbis
    URL https://github.com/xiph/vorbis/archive/refs/tags/v1.3.7.tar.gz
    SOURCE_SUBDIR do-not-configure)
FetchContent_MakeAvailable(xiph_ogg xiph_vorbis)

set(OGG_DIR ${xiph_ogg_SOURCE_DIR})
set(VORBIS_DIR ${xiph_vorbis_SOURCE_DIR})

add_library(xiph_ogg_static STATIC
    ${OGG_DIR}/src/bitwise.c
    ${OGG_DIR}/src/framing.c)
target_include_directories(xiph_ogg_static PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}/ogg-config
    ${OGG_DIR}/include)
set_target_properties(xiph_ogg_static PROPERTIES POSITION_INDEPENDENT_CODE ON)

add_library(xiph_vorbis_static STATIC
    ${VORBIS_DIR}/lib/analysis.c
    ${VORBIS_DIR}/lib/bitrate.c
    ${VORBIS_DIR}/lib/block.c
    ${VORBIS_DIR}/lib/codebook.c
    ${VORBIS_DIR}/lib/envelope.c
    ${VORBIS_DIR}/lib/floor0.c
    ${VORBIS_DIR}/lib/floor1.c
    ${VORBIS_DIR}/lib/info.c
    ${VORBIS_DIR}/lib/lookup.c
    ${VORBIS_DIR}/lib/lpc.c
    ${VORBIS_DIR}/lib/lsp.c
    ${VORBIS_DIR}/lib/mapping0.c
    ${VORBIS_DIR}/lib/mdct.c
    ${VORBIS_DIR}/lib/psy.c
    ${VORBIS_DIR}/lib/registry.c
    ${VORBIS_DIR}/lib/res0.c
    ${VORBIS_DIR}/lib/sharedbook.c
    ${VORBIS_DIR}/lib/smallft.c
    ${VORBIS_DIR}/lib/synthesis.c
    ${VORBIS_DIR}/lib/vorbisenc.c
    ${VORBIS_DIR}/lib/window.c)
target_include_directories(xiph_vorbis_static
    PUBLIC ${VORBIS_DIR}/include
    PRIVATE ${VORBIS_DIR}/lib)
target_link_libraries(xiph_vorbis_static PUBLIC xiph_ogg_static)
set_target_properties(xiph_vorbis_static PROPERTIES POSITION_INDEPENDENT_CODE ON)
if(CMAKE_C_COMPILER_ID MATCHES "Clang|GNU")
    target_compile_options(xiph_vorbis_static PRIVATE -O2 -w)
    target_compile_options(xiph_ogg_static PRIVATE -O2 -w)
endif()

add_library(voicegram SHARED vg_encoder.c vg_jni.c)
target_link_libraries(voicegram PRIVATE xiph_vorbis_static m)
if(ANDROID)
    target_link_libraries(voicegram PRIVATE log)
    # 16 KB page size support (required for new apps on Android 15+ devices)
    target_link_options(voicegram PRIVATE "-Wl,-z,max-page-size=16384")
endif()
