cmake_minimum_required(VERSION 3.16)
project(voice_control)

if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 17)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# 寻找依赖包
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)

# 科大讯飞SDK路径配置 - 关键修改
set(XUNFEI_SDK_LIB_DIRS 
  ${CMAKE_CURRENT_SOURCE_DIR}/libs
)

# 添加头文件路径
include_directories(
  include
  ${CMAKE_CURRENT_SOURCE_DIR}/include
)

# 添加库文件路径
link_directories(
  ${XUNFEI_SDK_LIB_DIRS}
)

# 打印库路径用于调试
message(STATUS "寻找libmsc.so的路径: ${XUNFEI_SDK_LIB_DIRS}")

# 检查libmsc.so是否存在
foreach(LIB_DIR ${XUNFEI_SDK_LIB_DIRS})
  if(EXISTS "${LIB_DIR}/libmsc.so")
    message(STATUS "找到libmsc.so: ${LIB_DIR}/libmsc.so")
  endif()
endforeach()

add_executable(iat_publish 
  src/iat_publish.cpp 
  src/speech_recognizer.c 
  src/linuxrec.c
)

ament_target_dependencies(iat_publish
  rclcpp
  std_msgs
)

# 链接库
target_link_libraries(iat_publish
  -L${XUNFEI_SDK_LIB_DIRS}  # 显式指定库路径
  msc  # 科大讯飞主库
  dl pthread m rt asound
)

# 安装可执行文件
install(TARGETS
  iat_publish
  DESTINATION lib/${PROJECT_NAME}
)

# 安装SDK库文件到系统能找到的地方
install(DIRECTORY ${XUNFEI_SDK_LIB_DIRS}/
  DESTINATION lib
  FILES_MATCHING PATTERN "*.so*"
)

install(DIRECTORY include/
  DESTINATION include
)

if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/config)
  install(DIRECTORY config/
    DESTINATION share/${PROJECT_NAME}/config
  )
endif()

if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/launch)
  install(DIRECTORY launch/
    DESTINATION share/${PROJECT_NAME}/launch
  )
endif()

ament_export_include_directories(include)
ament_export_dependencies(rclcpp std_msgs)

ament_package()
    