# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.10.2)

if (${CMAKE_HOST_APPLE})
    set(HOST_TAG "darwin-x86_64")
elseif (${CMAKE_HOST_UNIX})
    set(HOST_TAG "linux-x86_64")
endif()

# Add standalone toolchain and toolchain host to the path. The toolchain host is not the same
# as the provided ABI, but has a 1:1 mapping that we do here
if (${ANDROID_ABI} STREQUAL "x86")
    set(ANDROID_TOOLCHAIN_HOST "i686-linux-android")
elseif (${ANDROID_ABI} STREQUAL "x86_64")
    set(ANDROID_TOOLCHAIN_HOST "x86_64-linux-android")
elseif (${ANDROID_ABI} STREQUAL "armeabi-v7a")
    set(ANDROID_TOOLCHAIN_HOST "arm-linux-androideabi")
elseif (${ANDROID_ABI} STREQUAL "arm64-v8a")
    set(ANDROID_TOOLCHAIN_HOST "aarch64-linux-android")
else()
    set(ANDROID_TOOLCHAIN_HOST "UNKNOWN")
endif()
set(
    TOOLCHAIN_PATH
        "${ANDROID_NDK}/toolchains/llvm/prebuilt/${HOST_TAG}/bin")
set(ANDROID_MIN_SDK_VERSION "24")

# Specify link directories for our externally built libraries before defining add_library so that it
# is available at link time.
link_directories(
        ../../iZFrameworks/ThirdPartyLibs/zeromq/built_products/${ANDROID_TOOLCHAIN_HOST}/lib/
        ../../iZFrameworks/ThirdPartyLibs/FLAC/built_products/${ANDROID_TOOLCHAIN_HOST}/lib/
)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
        hook-lib

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source files
        src/main/cpp/ArmingAndColorUtilsJNIWrapper.cpp
        src/main/cpp/NativeBounceUtils.h
        src/main/cpp/NativeBounceUtils.cpp
        src/main/cpp/JNIUtils.cpp
        src/main/cpp/JNIContext.cpp
        src/main/cpp/OboeDeviceWrapper.h
        src/main/cpp/OboeDeviceWrapper.cpp
        src/main/cpp/ProjectControlHelper.h
        src/main/cpp/ProjectControlHelper.cpp
        src/main/cpp/ProjectControlJNIWrapper.cpp
        src/main/cpp/ProjectSerializationUtils.h
        src/main/cpp/ProjectSerializationUtils.cpp
        src/main/cpp/RemoteJNIWrapper.cpp
        src/main/cpp/SpireBaseJNICommon.h
        src/main/cpp/SpireBaseJNICommon.cpp
        src/main/cpp/SpireWireAudioEngineHelper.h
        src/main/cpp/SpireWireAudioEngineHelper.cpp
        src/main/cpp/SpireWireAudioEngineJNIWrapper.cpp
        src/main/cpp/SubscriberJNIWrapper.cpp
        src/main/cpp/TestUtils/FakeGamutResponseEndpointJNIWrapper.cpp
        src/main/cpp/TestUtils/FakePublisherJNIWrapper.cpp
        src/main/cpp/TestUtils/JNITestUtils.cpp
        src/main/cpp/Waveform/WaveformControllerJNIWrapper.cpp
        src/main/cpp/Waveform/WaveformControllerHelper.h
        src/main/cpp/Waveform/WaveformControllerHelper.cpp)
target_compile_options(hook-lib PRIVATE
        --std=c++17
        -O3
        -Wall
        -Wno-strict-aliasing
        -Wno-unused-local-typedefs
        -Wno-deprecated-declarations
        -Wno-multichar
        -Werror=return-type
        -Werror=attributes
        -Werror=unused-result)

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
        log-lib

        # Specifies the name of the NDK library that
        # you want CMake to locate.
        log )

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

# add_subdirectory should be paths to cmake files to be included by
# this cmake file
set(lib_SpireBase_CMAKELISTS_DIR ../../spirebase/build/out.android/${CMAKE_BUILD_TYPE})
set(lib_SpireBase_BUILD_DIR ${lib_SpireBase_CMAKELISTS_DIR}/${ANDROID_ABI})
set(lib_SpireBase_BUILD_OUTPUT_DIR ${lib_SpireBase_BUILD_DIR}/obj.target/spirebase/build)

list(APPEND build_exclude
        "iZBase Unit Tests"
        "iZBase Network Unit Tests"
        "SpireBaseUnitTests")
add_subdirectory(${lib_SpireBase_CMAKELISTS_DIR} ${lib_SpireBase_BUILD_DIR})

# Create library with appropriate properties
add_library(lib_SpireBase STATIC IMPORTED)
set_target_properties(
        lib_SpireBase
        PROPERTIES
        IMPORTED_LOCATION ${lib_SpireBase_BUILD_OUTPUT_DIR}/libSpireBase.a)

list(APPEND build_exclude
        "MasterAssistantUnitTests"
        "SpireWireCLI"
        "SpireWireUnitTests"
        "OperationChainUnitTests")
# Create library with appropriate properties
add_library(lib_SpireWire STATIC IMPORTED)
set_target_properties(
        lib_SpireWire
        PROPERTIES
        IMPORTED_LOCATION ${lib_SpireBase_BUILD_OUTPUT_DIR}/libSpireWire.a)
add_library(lib_MasterAssistant STATIC IMPORTED)
set_target_properties(
        lib_MasterAssistant
        PROPERTIES
        IMPORTED_LOCATION ${lib_SpireBase_BUILD_OUTPUT_DIR}/libMasterAssistant.a)
add_library(lib_GamutRemote STATIC IMPORTED)
set_target_properties(
        lib_GamutRemote
        PROPERTIES
        IMPORTED_LOCATION ${lib_SpireBase_BUILD_OUTPUT_DIR}/libGamutRemote.a)
add_library(lib_ProjectControl STATIC IMPORTED)
set_target_properties(
        lib_ProjectControl
        PROPERTIES
        IMPORTED_LOCATION ${lib_SpireBase_BUILD_OUTPUT_DIR}/libProjectControl.a)
add_library(lib_Waveform STATIC IMPORTED)
set_target_properties(
        lib_Waveform
        PROPERTIES
        IMPORTED_LOCATION ${lib_SpireBase_BUILD_OUTPUT_DIR}/libWaveform.a)
add_library(lib_SomaDrivers STATIC IMPORTED)
set_target_properties(
        lib_SomaDrivers
        PROPERTIES
        IMPORTED_LOCATION ${lib_SpireBase_BUILD_OUTPUT_DIR}/libSomaDrivers.a)
add_library(lib_OperationChain STATIC IMPORTED)
set_target_properties(
        lib_OperationChain
        PROPERTIES
        IMPORTED_LOCATION ${lib_SpireBase_BUILD_OUTPUT_DIR}/libOperationChain.a)
add_library(lib_DynamicEQ STATIC IMPORTED)
set_target_properties(
        lib_DynamicEQ
        PROPERTIES
        IMPORTED_LOCATION ${lib_SpireBase_BUILD_DIR}/obj.target/DynamicEQ/build/libDynamicEQ.a)
add_library(lib_IRC STATIC IMPORTED)
set_target_properties(
        lib_IRC
        PROPERTIES
        IMPORTED_LOCATION ${lib_SpireBase_BUILD_DIR}/obj.target/IRC/build/libIRC.a)

add_library(lib_oboe STATIC IMPORTED)
set(lib_oboe_CMAKELISTS_DIR ../../oboe)
set(lib_oboe_BUILD_DIR ${lib_oboe_CMAKELISTS_DIR}/${ANDROID_ABI})
add_subdirectory(${lib_oboe_CMAKELISTS_DIR} ${lib_oboe_BUILD_DIR})
set_target_properties(
        lib_oboe
        PROPERTIES
        IMPORTED_LOCATION ${lib_oboe_BUILD_DIR})

# Add include directories for any header files we need to access from this library
list(APPEND include_dirs
        src/main/cpp
        ../../spirebase/src
        ../../spirebase/GamutRemote/src
        ../../iZFrameworks/iZBase/src
        ../../iZFrameworks/ThirdPartyLibs/better-enums
        ../../iZFrameworks/ThirdPartyLibs/boost/include
        ../../iZFrameworks/ThirdPartyLibs/expected/include
        ../../iZFrameworks/ThirdPartyLibs/ranges/ranges-v3/include
        ../../iZFrameworks/ThirdPartyLibs/jsoncpp/jsoncpp/include
        ../../iZFrameworks/ThirdPartyLibs/zeromq/cppzmq
        ../../iZFrameworks/ThirdPartyLibs/zeromq/libzmq/include
        ../../iZFrameworks/ThirdPartyLibs/fmt/fmt/include
        ../../iZFrameworks/ThirdPartyLibs/spdlog/spdlog/include
        ../../soma/src
        ../../soma/SomaDrivers/src
        ../../operationchain/src
        ../../iZFrameworks/iZDSPBase/src
        ../../iZFrameworks/ThirdPartyLibs/msgpack2/msgpack2/include
        ../../iZFrameworks/ThirdPartyLibs/gsl/gsl/include
        ../../iZFrameworks/iZAudio/src
        ../../iZFrameworks/ThirdPartyLibs/ranges/ranges-v3/include
        ../../iZFrameworks/iZBase/libs/vectorize/include
        ../../iZFrameworks/ThirdPartyLibs/FLAC/built_products/${ANDROID_TOOLCHAIN_HOST}/include
        ${lib_oboe_CMAKELISTS_DIR}/include)
include_directories(${include_dirs})

target_link_libraries( # Specifies the target library.
        hook-lib
        -Wl,--start-group

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib}

        #iZotope libraries
        lib_SpireBase
        lib_SpireWire
        lib_MasterAssistant
        lib_GamutRemote
        lib_ProjectControl
        lib_Waveform
        iZBase
        iZDSPBase
        iZAudio_Full
        Soma
        SomaDrivers
        OperationChain
        lib_DynamicEQ
        lib_IRC

        json
        fmt
        oboe
        -lFLAC
        -lFLAC++
        -Wl,--end-group
        -lzmq)