cmake_minimum_required(VERSION 3.16)

# 添加更详细的错误输出
set(CMAKE_VERBOSE_MAKEFILE ON)
message(STATUS "Qt6_DIR is: $ENV{Qt6_DIR}")
message(STATUS "CMAKE_PREFIX_PATH is: $ENV{CMAKE_PREFIX_PATH}")

# 在project声明之前设置构建类型
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
    set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build." FORCE)
    set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
endif()

project(AuseftDDSPlugTest VERSION 0.1 LANGUAGES CXX)

# 如果没有指定构建类型，设置为 Debug
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Debug)
endif()

# 基本设置
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

# 根据构建类型设置编译选项
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -O0 -Wall -Wextra")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")

# 输出目录设置
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/bin)

# 添加 OpenGL 查找
find_package(OpenGL REQUIRED)

# Qt相关设置
list(APPEND CMAKE_PREFIX_PATH "/home/suzj/Qt/6.8.1/gcc_64")
find_package(Qt6 6.8 REQUIRED COMPONENTS 
    Quick 
    OpenGL
    Gui
)

# 添加接口目录到包含路径
include_directories(${CMAKE_SOURCE_DIR})

# 在 project 声明之后添加
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

# 设置 Qt 策略
qt_policy(SET QTP0001 NEW)

# 在 project 声明后添加
add_library(Interfaces INTERFACE)
target_include_directories(Interfaces INTERFACE ${CMAKE_SOURCE_DIR})

# 添加接口文件到 IDE 显示
add_custom_target(InterfacesIDE SOURCES
    interfaces/plugin_interface.hpp
    # 添加其他接口文件...
)

# 修改 qt_add_executable 部分
qt_add_executable(AuseftDDSPlugTest
    main.cpp
    PluginLoader.cpp
    PluginLoader.hpp
    MANUAL_FINALIZATION
)

# 修改 qt_add_qml_module 部分
qt_add_qml_module(AuseftDDSPlugTest
    URI AuseftDDSPlugTest
    VERSION 1.0
    QML_FILES
        Main.qml

)

set_target_properties(AuseftDDSPlugTest PROPERTIES
    QT_QML_LINT_ENABLED TRUE
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
)

# 链接接口库
target_link_libraries(AuseftDDSPlugTest
    PRIVATE 
    Interfaces
    Qt6::Quick
    Qt6::OpenGL
    OpenGL::GL
    fastdds
    fastcdr
)

include(GNUInstallDirs)
install(TARGETS AuseftDDSPlugTest
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

add_subdirectory(plugins/AQTSampleMachinePlug BEFORE)
add_subdirectory(plugins/AQTPackageMachinePlug)

# Windows 系统下启用调试信息
if(WIN32)
    set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
    set(CMAKE_ENABLE_EXPORTS ON)
endif()

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
    target_compile_options(AuseftDDSPlugTest PRIVATE -g)
endif()

# 在cmake_minimum_required之后添加
set(CMAKE_VERBOSE_MAKEFILE ON)
message(STATUS "CMAKE_BUILD_TYPE is: ${CMAKE_BUILD_TYPE}")
message(STATUS "Build directory is: ${CMAKE_BINARY_DIR}")

# 在文件末尾添加
qt_finalize_executable(AuseftDDSPlugTest)

target_include_directories(AuseftDDSPlugTest
    PRIVATE
        ${CMAKE_SOURCE_DIR}
        ${CMAKE_SOURCE_DIR}/plugins
)

