cmake_minimum_required(VERSION 3.14)
project(HapticsCarriage VERSION 0.0.1)

find_package(Git)
include(cmake/haptics_utils.cmake)

# default HapticsCarriage options

option(HAPTICS_CARRIAGE_TESTS "Build Haptics Carriage unit tests" ON)
option(HAPTICS_SERIALIZER_TESTS "Build Haptics Serializer unit tests" ON)
option(HAPTICS_HMPEG_TESTS "Build Haptics hmpg unit tests" ON)
option(HAPTICS_UPDATE_GIT_SUBMODULES "Update submodules at first build" ON)
option(HAPTICS_APPLY_SUBMODULES_PATCHES "Apply patches to submodules. Warning: for robustness, reset updates on submodules before applying patches." ON)

# ISO/IEC 23090-32 vs 23090-31 channel_id size option
# By default, follows ISO/IEC 23090-32 (8 bits for channel_id in MP4 boxes)
# Enable this option to use 16 bits (following ISO/IEC 23090-31 and IOHaptics reference implementation)
option(MIHS_USE_16BIT_CHANNEL_ID "Use 16-bit channel_id in MP4 boxes (ISO/IEC 23090-31 compliance)" OFF)

# set global variables

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)

# Configure output directories based on build configuration
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin/$<CONFIG>)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib/$<CONFIG>)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib/$<CONFIG>)

if (HAPTICS_CARRIAGE_TESTS OR HAPTICS_SERIALIZER_TESTS OR HAPTICS_HMPEG_TESTS)
    set(HAPTICS_TESTS ON)
endif()

# Apply channel_id size option
if(MIHS_USE_16BIT_CHANNEL_ID)
    add_compile_definitions(MIHS_USE_16BIT_CHANNEL_ID)
    message(STATUS "MIHS: Using 16-bit channel_id in MP4 boxes (ISO/IEC 23090-31 compliance)")
else()
    message(STATUS "MIHS: Using 8-bit channel_id in MP4 boxes (ISO/IEC 23090-32 compliance - default)")
endif()

# import external libs

option(RAPIDJSON_BUILD_DOC OFF)
option(RAPIDJSON_BUILD_EXAMPLES OFF)
option(RAPIDJSON_BUILD_TESTS OFF)
option(IIR_BUILD_DEMO OFF)
option(IIR_BUILD_TESTS OFF)
option(BUILD_DECODER "Enable building Decoder" OFF)
option(BUILD_ENCODER "Enable building Encoder" OFF)
option(BUILD_SYNTHESIZER "Enable building Synthesizer" OFF)
option(BUILD_CONFORMANCE "Enable building Conformance" OFF)


set(BUILD_CATCH2 OFF) # disable hapticsMpeg unit tests

haptics_utils_reset_default_compile_options()
message(STATUS "Update submodules. It would TAKE TIME!!!")
haptics_utils_update_git_submodules()
haptics_assert_file_exists(external/Catch2/CMakeLists.txt)
haptics_assert_file_exists(external/cxxopts/CMakeLists.txt)
haptics_assert_file_exists(external/hapticsMpeg/CMakeLists.txt)
haptics_assert_file_exists(external/isobmff/CMakeLists.txt)
haptics_assert_file_exists(external/json/CMakeLists.txt)
haptics_assert_file_exists(external/spdlog/CMakeLists.txt)

if (BUILD_DECODER OR BUILD_ENCODER OR BUILD_SYNTHESIZER OR BUILD_CONFORMANCE)
## preload hapticsMpeg dependencies to apply patch to fix warning and avoid unused make targets
include(cmake/pugixml.cmake)
include(cmake/iir.cmake)
include(cmake/rapidjson.cmake)
endif()

## patches
haptics_utils_apply_external_patch(isobmff)
haptics_utils_apply_external_patch(hapticsMpeg)

## haptics carriage dependencies
add_subdirectory(external/isobmff/IsoLib/libisomediafile)
add_subdirectory(external/spdlog)
add_subdirectory(external/cxxopts)
add_subdirectory(external/hapticsMpeg)
add_definitions(-DJSON_SCHEMA_PATH="${CMAKE_BINARY_DIR}/_deps/hjif_specs-src/Specifications")

include_directories(external/hapticsMpeg/source)

# build options

if(HAPTICS_TESTS)
    add_definitions(-DTEST_DATA_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}/data")
    add_subdirectory(external/Catch2)
    include(CTest)
    include(Catch)
    enable_testing()
endif()

if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
    add_compile_options(-Wall -Wextra -Wpedantic)
endif()

add_subdirectory(src/utils)
add_subdirectory(src/lib)
add_subdirectory(src/app)
