## 通用配置：不重复定义目标，仅补充包含与源文件（按需）

cmake_minimum_required(VERSION 3.7.2)    # Minimum CMake Version 
project(sdk_test VERSION 1.0)     # Project Definition

# C++ Standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)


# 全局包含根目录，允许使用 "inspire_hand/inspire_hand_driver/rh56_frame.h" 这种包含写法
include_directories(
	${CMAKE_SOURCE_DIR}
	${CMAKE_SOURCE_DIR}/c&c++/inc_of_c++
	${CMAKE_SOURCE_DIR}/jaka_robot
	${CMAKE_SOURCE_DIR}/tools
	${CMAKE_SOURCE_DIR}/inspire_hand
	${CMAKE_SOURCE_DIR}/robot_control_hand
)

# 若存在 hand_tio_example 目标，则为其追加手驱动源与包含路径（不重复创建目标）
if (TARGET hand_tio_example)
	target_sources(hand_tio_example PRIVATE ${CMAKE_SOURCE_DIR}/inspire_hand/inspire_hand_driver/rh56_frame.c)
	target_include_directories(hand_tio_example PUBLIC ${CMAKE_SOURCE_DIR}/inspire_hand/inspire_hand_driver)
endif()


# Source Directory Information
message(${CMAKE_SOURCE_DIR})

file(GLOB SRC_JAKA "${CMAKE_SOURCE_DIR}/jaka_robot/*.cpp")
file(GLOB SRC_HAND "${CMAKE_SOURCE_DIR}/inspire_hand/*.cpp")
file(GLOB SRC_BRIDGE "${CMAKE_SOURCE_DIR}/robot_control_hand/*.cpp")
file(GLOB SRC_COORD "${CMAKE_SOURCE_DIR}/coordinated_actions/*.cpp")
set(SRC_FILES ${SRC_JAKA} ${SRC_HAND} ${SRC_BRIDGE} ${SRC_COORD})
message(STATUS "Source files: ${SRC_FILES}")

# Inspire hand driver source
set(HAND_DRIVER_SRC "${CMAKE_SOURCE_DIR}/inspire_hand/inspire_hand_driver/rh56_frame.c")
if(NOT EXISTS "${HAND_DRIVER_SRC}")
	message(FATAL_ERROR "Hand driver not found at ${HAND_DRIVER_SRC}")
endif()

# Object library for shared robot logic
add_library(robot_core OBJECT ${SRC_FILES} ${CMAKE_SOURCE_DIR}/tools/tools.cpp ${CMAKE_SOURCE_DIR}/tools/config_loader.cpp ${HAND_DRIVER_SRC})

# Example 1: robot menu (use example/main.cpp)
add_executable(robot_menu
	${CMAKE_SOURCE_DIR}/example/main.cpp
	$<TARGET_OBJECTS:robot_core>
)

# Example 2: TIO RS485 hand frame sender
add_executable(hand_tio_example
	${CMAKE_SOURCE_DIR}/example/test.cpp
	${HAND_DRIVER_SRC}
	${CMAKE_SOURCE_DIR}/tools/tools.cpp
)

# Example 3: TIO RS485 signal-demo (semaphore-style read of RH56 forces)
add_executable(test_semo
	${CMAKE_SOURCE_DIR}/example/test_semo.cpp
	$<TARGET_OBJECTS:robot_core>
)

add_executable(fixed_grasp_demo
    ${CMAKE_SOURCE_DIR}/example/fixed_grasp_demo.cpp
    $<TARGET_OBJECTS:robot_core>
)

# Set the header file path of the SDK included in the project
target_include_directories(robot_menu PUBLIC ${CMAKE_SOURCE_DIR}/c&c++/inc_of_c++ ${CMAKE_SOURCE_DIR}/jaka_robot)
target_include_directories(hand_tio_example PUBLIC
	${CMAKE_SOURCE_DIR}/c&c++/inc_of_c++
	${CMAKE_SOURCE_DIR}/jaka_robot
	${CMAKE_SOURCE_DIR}/inspire_hand
	${CMAKE_SOURCE_DIR}/inspire_hand/inspire_hand_driver
	${CMAKE_SOURCE_DIR}/tools
)
target_include_directories(test_semo PUBLIC
	${CMAKE_SOURCE_DIR}/c&c++/inc_of_c++
	${CMAKE_SOURCE_DIR}/tools
)

target_include_directories(fixed_grasp_demo PUBLIC
    ${CMAKE_SOURCE_DIR}
    ${CMAKE_SOURCE_DIR}/c&c++/inc_of_c++
    ${CMAKE_SOURCE_DIR}/jaka_robot
    ${CMAKE_SOURCE_DIR}/tools
    ${CMAKE_SOURCE_DIR}/inspire_hand
    ${CMAKE_SOURCE_DIR}/robot_control_hand
    ${CMAKE_SOURCE_DIR}/coordinated_actions
    ${CMAKE_SOURCE_DIR}/inspire_hand/inspire_hand_driver
)


# 对于旧版 GCC (<=8) 使用 filesystem 需要链接 stdc++fs
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
	if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9)
		target_link_libraries(robot_menu stdc++fs)
		target_link_libraries(hand_tio_example stdc++fs)
		target_link_libraries(test_semo stdc++fs)
		target_link_libraries(fixed_grasp_demo stdc++fs)
	endif()
endif()

# Try to link the SDK library if it exists (path provided in repo for linux builds)
set(JAKA_SDK_LIB ${CMAKE_SOURCE_DIR}/c&c++/x86_64-linux-gnu/shared/libjakaAPI.so)
if(EXISTS "${JAKA_SDK_LIB}")
	message(STATUS "Found JAKA SDK library: ${JAKA_SDK_LIB}")
	target_link_libraries(robot_menu "${JAKA_SDK_LIB}" pthread)
	target_link_libraries(hand_tio_example "${JAKA_SDK_LIB}" pthread)
	target_link_libraries(test_semo "${JAKA_SDK_LIB}" pthread)
	target_link_libraries(fixed_grasp_demo "${JAKA_SDK_LIB}" pthread)
else()
	message(WARNING "JAKA SDK library not found at ${JAKA_SDK_LIB}. Please update the path for your platform (Windows .lib or Linux .so). Build may fail if SDK library is not available.")
endif()
