cmake_minimum_required(VERSION 3.18...4.0)

project(baresip-wear)

add_link_options("LINKER:--build-id=none")

set(distribution_DIR ${CMAKE_SOURCE_DIR}/../../../../distribution)

add_library(lib_crypto STATIC IMPORTED)
set_target_properties(lib_crypto PROPERTIES IMPORTED_LOCATION
        ${distribution_DIR}/openssl/lib/${ANDROID_ABI}/libcrypto.a)

add_library(lib_ssl STATIC IMPORTED)
set_target_properties(lib_ssl PROPERTIES IMPORTED_LOCATION
        ${distribution_DIR}/openssl/lib/${ANDROID_ABI}/libssl.a)

add_library(lib_re STATIC IMPORTED)
set_target_properties(lib_re PROPERTIES IMPORTED_LOCATION
        ${distribution_DIR}/re/lib/${ANDROID_ABI}/libre.a)

add_library(lib_opus STATIC IMPORTED)
set_target_properties(lib_opus PROPERTIES IMPORTED_LOCATION
        ${distribution_DIR}/opus/lib/${ANDROID_ABI}/libopus.a)

add_library(lib_g722 STATIC IMPORTED)
set_target_properties(lib_g722 PROPERTIES IMPORTED_LOCATION
        ${distribution_DIR}/g722/lib/${ANDROID_ABI}/libg722.a)

add_library(lib_g722_1 STATIC IMPORTED)
set_target_properties(lib_g722_1 PROPERTIES IMPORTED_LOCATION
        ${distribution_DIR}/g7221/lib/${ANDROID_ABI}/libg722_1.a)

add_library(lib_g729 STATIC IMPORTED)
set_target_properties(lib_g729 PROPERTIES IMPORTED_LOCATION
        ${distribution_DIR}/g729/lib/${ANDROID_ABI}/libbcg729.a)

add_library(lib_ilbc STATIC IMPORTED)
set_target_properties(lib_ilbc PROPERTIES IMPORTED_LOCATION
        ${distribution_DIR}/ilbc/lib/${ANDROID_ABI}/libilbc.a)

add_library(lib_codec2 STATIC IMPORTED)
set_target_properties(lib_codec2 PROPERTIES IMPORTED_LOCATION
        ${distribution_DIR}/codec2/lib/${ANDROID_ABI}/libcodec2.a)

add_library(lib_amrnb STATIC IMPORTED)
set_target_properties(lib_amrnb PROPERTIES IMPORTED_LOCATION
        ${distribution_DIR}/amr/lib/${ANDROID_ABI}/libamrnb.a)

add_library(lib_amrwb STATIC IMPORTED)
set_target_properties(lib_amrwb PROPERTIES IMPORTED_LOCATION
        ${distribution_DIR}/amr/lib/${ANDROID_ABI}/libamrwb.a)

add_library(lib_amrwbenc STATIC IMPORTED)
set_target_properties(lib_amrwbenc PROPERTIES IMPORTED_LOCATION
        ${distribution_DIR}/amr/lib/${ANDROID_ABI}/libamrwbenc.a)

add_library(lib_zrtpcppcore STATIC IMPORTED)
set_target_properties(lib_zrtpcppcore PROPERTIES IMPORTED_LOCATION
        ${distribution_DIR}/gzrtp/lib/${ANDROID_ABI}/libzrtpcppcore.a)

add_library(lib_sndfile STATIC IMPORTED)
set_target_properties(lib_sndfile PROPERTIES IMPORTED_LOCATION
        ${distribution_DIR}/sndfile/lib/${ANDROID_ABI}/libsndfile.a)

add_library(lib_baresip STATIC IMPORTED)
set_target_properties(lib_baresip PROPERTIES IMPORTED_LOCATION
        ${distribution_DIR}/baresip/lib/${ANDROID_ABI}/libbaresip.a)

add_library(wearbaresip SHARED ${CMAKE_SOURCE_DIR}/baresip.c)

# Export all symbols from libwearbaresip.so. baresip/re are linked in as
# static archives with hidden visibility, so by default NONE of their
# symbols reach the dynamic symbol table. Loadable application modules
# (account.so) dlopen() against this lib and need ua_alloc/conf_parse/
# uag_list etc. -- without --export-dynamic those are unresolved and the
# module silently fails to load, so no SIP account is ever populated.
target_link_options(wearbaresip PRIVATE -Wl,--export-dynamic)

target_include_directories(wearbaresip PRIVATE
        ${distribution_DIR}/openssl/include
        ${distribution_DIR}/re/include
        ${distribution_DIR}/baresip/include)

add_definitions(-DHAVE_PTHREAD)

target_link_libraries(
        wearbaresip
        android
        aaudio
        lib_baresip
        lib_re
        lib_ssl
        lib_crypto
        lib_opus
        lib_g722
        lib_g722_1
        lib_g729
        lib_ilbc
        lib_codec2
        lib_amrnb
        lib_amrwb
        lib_amrwbenc
        lib_zrtpcppcore
        lib_sndfile
        z
        log)

# Build the 'account' application module (.so) from the baresip submodule
# source. CRITICAL: it must reference the SAME baresip/re instance that is
# already loaded inside libwearbaresip.so -- NOT link libbaresip.a/libre.a
# statically (that would create a second, independent copy of baresip's
# static state -- separate uag_list/mod registry/global ctors -- and the
# module silently fails to dlopen, so no account is ever populated). Linking
# the MODULE against the SHARED wearbaresip lib makes it resolve every
# baresip/re symbol from the one live instance.
set(account_SRC
        ${CMAKE_SOURCE_DIR}/../../../../libbaresip-android/baresip/modules/account/account.c)

add_library(account MODULE ${account_SRC})

# baresip's module loader opens "<module_path>/<name>" where <name> comes
# from the config line "module_app account.so". It expects the file to be
# named exactly "account.so" (no "lib" prefix). CMake's default MODULE
# output is "libaccount.so", so force the bare name.
set_target_properties(account PROPERTIES PREFIX "" OUTPUT_NAME account)

target_include_directories(account PRIVATE
        ${distribution_DIR}/openssl/include
        ${distribution_DIR}/re/include
        ${distribution_DIR}/baresip/include
        ${CMAKE_SOURCE_DIR}/../../../../libbaresip-android/baresip/include)

add_definitions(-DHAVE_PTHREAD)

# Reference the shared baresip lib so there is exactly one baresip instance.
target_link_libraries(account wearbaresip log)
add_dependencies(account wearbaresip)
