cmake_minimum_required(VERSION 3.16)
project(cx_cpp LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

option(USE_K4A "Enable Azure Kinect capture" ON)
option(ENABLE_LZ4 "Enable LZ4 frame compression for FRM2" ON)

find_package(Threads REQUIRED)
find_package(OpenGL REQUIRED)
find_package(PkgConfig REQUIRED)
find_package(GLEW QUIET)
if(NOT GLEW_FOUND)
  pkg_check_modules(GLEW REQUIRED glew)
endif()
find_package(glfw3 QUIET)
if(NOT glfw3_FOUND)
  pkg_check_modules(GLFW3 REQUIRED glfw3)
endif()

find_package(nlohmann_json QUIET)
if(NOT nlohmann_json_FOUND)
  include(CheckIncludeFileCXX)
  check_include_file_cxx("nlohmann/json.hpp" HAVE_NLOHMANN_JSON_HEADER)
  if(HAVE_NLOHMANN_JSON_HEADER)
    add_library(nlohmann_json INTERFACE)
    add_library(nlohmann_json::nlohmann_json ALIAS nlohmann_json)
  else()
    message(FATAL_ERROR "nlohmann/json.hpp not found. Please install package: sudo apt install -y nlohmann-json-dev")
  endif()
endif()

if(ENABLE_LZ4)
  pkg_check_modules(LZ4 REQUIRED liblz4)
endif()

if(USE_K4A)
  # pkg_check_modules(K4A REQUIRED k4a)
  
  # 手动指定 Orbbec Wrapper 的头文件和替身库路径
  set(K4A_INCLUDE_DIRS "/home/jetson/Desktop/Orbbec_K4A/include")
  set(K4A_LIBRARIES "/home/jetson/Desktop/Orbbec_K4A/lib/libk4a.so")
endif()

add_executable(cx_cpp main.cpp)
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
  target_compile_options(cx_cpp PRIVATE -g -fno-omit-frame-pointer)
  target_link_options(cx_cpp PRIVATE -rdynamic)
endif()
target_link_libraries(cx_cpp PRIVATE
  Threads::Threads
  OpenGL::GL
  nlohmann_json::nlohmann_json
  asound   # <--- 新增：ALSA 音频库，用于读取声卡
  ltc      # <--- 新增：LTC 库，用于解码时间码
)

if(GLEW_FOUND)
  target_link_libraries(cx_cpp PRIVATE GLEW::GLEW)
else()
  target_include_directories(cx_cpp PRIVATE ${GLEW_INCLUDE_DIRS})
  target_link_libraries(cx_cpp PRIVATE ${GLEW_LIBRARIES})
endif()

if(glfw3_FOUND)
  target_link_libraries(cx_cpp PRIVATE glfw)
else()
  target_include_directories(cx_cpp PRIVATE ${GLFW3_INCLUDE_DIRS})
  target_link_libraries(cx_cpp PRIVATE ${GLFW3_LIBRARIES})
endif()

if(ENABLE_LZ4)
  target_compile_definitions(cx_cpp PRIVATE ENABLE_LZ4=1)
  target_include_directories(cx_cpp PRIVATE ${LZ4_INCLUDE_DIRS})
  target_link_libraries(cx_cpp PRIVATE ${LZ4_LIBRARIES})
endif()

if(USE_K4A)
  target_compile_definitions(cx_cpp PRIVATE USE_K4A=1)
  target_include_directories(cx_cpp PRIVATE ${K4A_INCLUDE_DIRS})
  target_link_libraries(cx_cpp PRIVATE ${K4A_LIBRARIES})
endif()

if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9)
  target_link_libraries(cx_cpp PRIVATE stdc++fs)
endif()
