cmake_minimum_required(VERSION 3.16)
project(AQTSampleMachinePlug LANGUAGES CXX)

# 添加更多调试输出
message(STATUS "Current source dir: ${CMAKE_CURRENT_SOURCE_DIR}")
message(STATUS "FastDDS lib dir: ${fastdds_DIR}")
message(STATUS "FastCDR lib dir: ${fastcdr_DIR}")

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

find_package(Qt6 REQUIRED COMPONENTS Core Quick)

# 添加 FastDDS 依赖
if(NOT fastcdr_FOUND)
    find_package(fastcdr 2 REQUIRED)
endif()

if(NOT fastdds_FOUND)
    find_package(fastdds 3 REQUIRED)
endif()

# 查找所有 DDS 相关的源文件
file(GLOB DDS_SOURCES 
    "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp"
    "${CMAKE_CURRENT_SOURCE_DIR}/*.cxx"
)

# 创建插件库
add_library(AQTSampleMachinePlug SHARED
    ${DDS_SOURCES}
    AQTSampleMachinePlug.hpp
    AQTSampleMachinePlug.json
)

# 定义导出宏
target_compile_definitions(AQTSampleMachinePlug
    PRIVATE 
        AQTSAMPLEMACHINEPLUG_LIBRARY
        QT_PLUGIN
)

# 添加包含目录
target_include_directories(AQTSampleMachinePlug
    PRIVATE
        ${CMAKE_SOURCE_DIR}
)

target_include_directories(AQTSampleMachinePlug
    PUBLIC
        ${CMAKE_CURRENT_SOURCE_DIR}
)

target_link_libraries(AQTSampleMachinePlug
    PRIVATE 
        Qt6::Core
        Qt6::Quick
        fastdds
        fastcdr
)

# 修改属性设置
set_target_properties(AQTSampleMachinePlug PROPERTIES
    OUTPUT_NAME "AQTSampleMachinePlug"
    LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
    RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
    PREFIX ""
    SUFFIX ".so"
    POSITION_INDEPENDENT_CODE ON
    # RPATH 设置
    SKIP_BUILD_RPATH FALSE
    BUILD_WITH_INSTALL_RPATH FALSE
    INSTALL_RPATH "$ORIGIN:${CMAKE_INSTALL_PREFIX}/lib"
    INSTALL_RPATH_USE_LINK_PATH TRUE
)

# 添加运行时依赖路径
if(UNIX AND NOT APPLE)
    set_target_properties(AQTSampleMachinePlug PROPERTIES
        INSTALL_RPATH "$ORIGIN:${CMAKE_INSTALL_PREFIX}/lib:/usr/local/lib"
    )
endif()

# 如果是调试版本，添加调试符号
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
    target_compile_options(AQTSampleMachinePlug PRIVATE -g)
endif()
# 调试信息
message(STATUS "Build Type: ${CMAKE_BUILD_TYPE}")
message(STATUS "Binary Dir: ${CMAKE_BINARY_DIR}")
message(STATUS "Plugin Output Dir: ${CMAKE_BINARY_DIR}/bin")

# 添加一个消息来验证设置
message(STATUS "Plugin name will be: AQTSampleMachinePlug.so")

# 修改安装配置
install(TARGETS AQTSampleMachinePlug
    LIBRARY DESTINATION bin
    RUNTIME DESTINATION bin
) 

# 确保导出符号
if(WIN32)
    set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
endif() 
