# Copyright (c) Orbbec Inc. / French Touch Factory. All Rights Reserved.
# Licensed under the MIT License.

cmake_minimum_required(VERSION 3.10)
project(ob_stream_depth_color_k4arec_pipeline)

if(WIN32)
    set(K4A_WRAPPER_DIR
        "C:/Ithaca_Projects/RGBD/Clone K4A Depot V2/OrbbecSDK-K4A-Wrapper"
        CACHE PATH "Path to OrbbecSDK-K4A-Wrapper root directory")
    # Windows: the wrapper's own out-of-source build dir is conventionally "Build",
    # producing MSVC import libs (k4a.lib/k4arecord.lib) alongside the DLLs in bin/.
    set(K4A_WRAPPER_BUILD_SUBDIR "Build")
else()
    set(K4A_WRAPPER_DIR
        "$ENV{HOME}/Downloads/OrbbecSDK-K4A-Wrapper"
        CACHE PATH "Path to OrbbecSDK-K4A-Wrapper root directory")
    # Linux: built with a lowercase "build" dir; shared libs land in lib/, not bin/.
    set(K4A_WRAPPER_BUILD_SUBDIR "build")
endif()

if(NOT EXISTS "${K4A_WRAPPER_DIR}/include/k4a/k4a.h")
    message(WARNING
        "${PROJECT_NAME}: K4A wrapper not found at '${K4A_WRAPPER_DIR}'. "
        "Set K4A_WRAPPER_DIR to the OrbbecSDK-K4A-Wrapper root. Skipping.")
    return()
endif()

add_executable(${PROJECT_NAME} depth_color_k4arec_pipeline.cpp)
# C++14: the source uses digit-separator literals (2'000'000ULL); C++11 rejects them.
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 14)

# rightW/rightH (Spout) and pinCpu/pinCore (SetProcessAffinityMask) are only read
# inside #ifdef _WIN32 blocks, so non-Windows builds see them as set-but-unused —
# harmless, but this SDK's own linux_config.cmake enables -Werror globally.
if(NOT WIN32)
    target_compile_options(${PROJECT_NAME} PRIVATE -Wno-unused-but-set-variable)
endif()

# SYSTEM: the K4A wrapper's public headers have GCC-pedantic issues (stray ';'
# after struct/enum decls) that -Wpedantic -Werror (set globally by this SDK's
# own linux_config.cmake) would otherwise turn into hard errors. -isystem makes
# GCC/Clang suppress warnings from headers found there.
target_include_directories(${PROJECT_NAME} SYSTEM PRIVATE
    "${K4A_WRAPPER_DIR}/include"
    "${K4A_WRAPPER_DIR}/${K4A_WRAPPER_BUILD_SUBDIR}/src/orbbec/include"
    "${K4A_WRAPPER_DIR}/${K4A_WRAPPER_BUILD_SUBDIR}/src/orbbec/record/sdk/include"
)

if(WIN32)
    set(K4A_LIB          "${K4A_WRAPPER_DIR}/${K4A_WRAPPER_BUILD_SUBDIR}/bin/k4a.lib")
    set(K4ARECORD_LIB    "${K4A_WRAPPER_DIR}/${K4A_WRAPPER_BUILD_SUBDIR}/bin/k4arecord.lib")
endif()

# The SDK's library-target name. v2.8.6 (the Nano) sets OB_SDK_LIB_NAME in the
# example scope; v2.4.3 (JetPack 6 Orin) does not, leaving "ob::" — a CMake error.
# The stock examples link ob::OrbbecSDK, so default to that when it is unset.
if(NOT OB_SDK_LIB_NAME)
    set(OB_SDK_LIB_NAME OrbbecSDK)
endif()

target_link_libraries(${PROJECT_NAME}
    ob::${OB_SDK_LIB_NAME}
    ob::examples::utils
    ${OpenCV_LIBS}
    $<$<BOOL:${WIN32}>:${K4A_LIB}>
    $<$<BOOL:${WIN32}>:${K4ARECORD_LIB}>
    $<$<BOOL:${WIN32}>:SpoutDX>
    # Linux: libk4a.so/libk4arecord.so are dlopen'd (RTLD_DEEPBIND) at runtime
    # instead of link-time linked — see the dlopen comment block near the top
    # of depth_color_k4arec_pipeline.cpp for why. Needs libdl for dlopen/dlsym.
    $<$<NOT:$<BOOL:${WIN32}>>:dl>
)

set_target_properties(${PROJECT_NAME} PROPERTIES FOLDER "examples")
if(MSVC)
    set_target_properties(${PROJECT_NAME} PROPERTIES
        VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
endif()

if(NOT WIN32)
    # Linux: libk4a.so/libk4arecord.so aren't on the system search path — rpath
    # the wrapper's build/lib so the binary finds them without LD_LIBRARY_PATH.
    # BUILD_RPATH (not INSTALL_RPATH+BUILD_WITH_INSTALL_RPATH) so this ADDS to
    # CMake's automatic build-tree rpath instead of replacing it — the automatic
    # one is what makes libOrbbecSDK.so (built earlier in this same tree) found.
    set_target_properties(${PROJECT_NAME} PROPERTIES
        BUILD_RPATH "${K4A_WRAPPER_DIR}/${K4A_WRAPPER_BUILD_SUBDIR}/lib")
endif()

if(WIN32)
    add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_if_different
            "${K4A_WRAPPER_DIR}/${K4A_WRAPPER_BUILD_SUBDIR}/bin/k4a.dll"
            "${K4A_WRAPPER_DIR}/${K4A_WRAPPER_BUILD_SUBDIR}/bin/k4arecord.dll"
            "${K4A_WRAPPER_DIR}/${K4A_WRAPPER_BUILD_SUBDIR}/bin/depthengine_2_0.dll"
            $<TARGET_FILE_DIR:${PROJECT_NAME}>
        COMMENT "Copying K4A wrapper DLLs"
    )
endif()

if(WIN32 AND TARGET opencv_world)
    add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_if_different
            "$<TARGET_FILE:opencv_world>"
            $<TARGET_FILE_DIR:${PROJECT_NAME}>
        COMMENT "Copying OpenCV DLL"
    )
endif()

install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION bin)
if(WIN32)
    install(FILES
        "${K4A_WRAPPER_DIR}/${K4A_WRAPPER_BUILD_SUBDIR}/bin/k4a.dll"
        "${K4A_WRAPPER_DIR}/${K4A_WRAPPER_BUILD_SUBDIR}/bin/k4arecord.dll"
        "${K4A_WRAPPER_DIR}/${K4A_WRAPPER_BUILD_SUBDIR}/bin/depthengine_2_0.dll"
        DESTINATION bin
    )
endif()
if(WIN32 AND TARGET opencv_world)
    install(FILES "$<TARGET_FILE:opencv_world>" DESTINATION bin)
endif()
