cmake_minimum_required(VERSION 3.20)

# 工具链路径（用于交叉编译）
set(TOOLCHAIN_PATH /opt/arm-gnu-toolchain-11.3.rel1-x86_64-aarch64-none-linux-gnu/bin)

set(CMAKE_SYSTEM_NAME Linux)     # 设置目标系统名字
set(CMAKE_SYSTEM_PROCESSOR aarch64)  # 设置目标处理器架构
# set(CMAKE_SYSROOT /opt/poky/1.4.1/sysroots/cortexa9hf-vfp-neon-poky-linux-gnueabi) # 设置 sysroot

# 设置交叉编译器
set(CMAKE_C_COMPILER ${TOOLCHAIN_PATH}/aarch64-none-linux-gnu-gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PATH}/aarch64-none-linux-gnu-g++)
# set(CMAKE_AR ${TOOLCHAIN_PATH}/aarch64-none-linux-gnu-ar)
# set(CMAKE_LINKER ${TOOLCHAIN_PATH}/aarch64-none-linux-gnu-ld)
# set(CMAKE_STRIP ${TOOLCHAIN_PATH}/aarch64-none-linux-gnu-strip)
# set(CMAKE_RANLIB ${TOOLCHAIN_PATH}/aarch64-none-linux-gnu-ranlib)


set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

project(monitor VERSION 1.0.0 LANGUAGES CXX C)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# set(CMAKE_BUILD_TYPE "Release")

message(STATUS ${CMAKE_C_COMPILER})
message(STATUS ${CMAKE_CXX_COMPILER})
message(STATUS ${CMAKE_AR})
message(STATUS ${CMAKE_LINKER})
message(STATUS ${CMAKE_STRIP})
message(STATUS ${CMAKE_RANLIB})

# 设置自定义 Qt 路径
set(CMAKE_PREFIX_PATH /opt/qt/qt5.12.12-rk3588)
set(QT5_DIR /opt/qt/qt5.12.12-rk3588/lib/cmake/Qt5)

# 获取 Git 信息（可选）
execute_process(
    COMMAND git rev-parse --short=10 HEAD
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
    OUTPUT_VARIABLE GIT_COMMIT_HASH
    OUTPUT_STRIP_TRAILING_WHITESPACE
)
execute_process(
    COMMAND git rev-parse --abbrev-ref HEAD
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
    OUTPUT_VARIABLE GIT_BRANCH
    OUTPUT_STRIP_TRAILING_WHITESPACE
)

string(TIMESTAMP BUILD_TIME "%Y-%m-%d %H:%M:%S")

# 将变量写入 version.h
configure_file(
    ${CMAKE_CURRENT_SOURCE_DIR}/include/version.h.in
    ${CMAKE_CURRENT_SOURCE_DIR}/include/version.h
    @ONLY
)

find_package(Qt5 REQUIRED COMPONENTS Widgets UiPlugin Network SerialPort)

set(PROJECT_SOURCE_DIR_LIST
    ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/cpp-httplib
    ${CMAKE_CURRENT_SOURCE_DIR}/include
    ${CMAKE_CURRENT_SOURCE_DIR}/src
)

foreach(item ${PROJECT_SOURCE_DIR_LIST})
    string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" SOURCE_DIR ${item})
    message("--------${SOURCE_DIR}--------")
    if("${item}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}")
        file(GLOB HEADERS CONFIGURE_DEPENDS ${item}/*.h ${item}/*.hpp)
    else()
        file(GLOB_RECURSE HEADERS CONFIGURE_DEPENDS ${item}/*.h ${item}/*.hpp)
    endif()
    foreach(item ${HEADERS})
        string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" HEADER_FILE ${item})
        message(STATUS ${HEADER_FILE})
        list(APPEND PROJECT_HEADERS ${HEADER_FILE})
    endforeach()

    if("${item}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}")
        file(GLOB SOURCES CONFIGURE_DEPENDS ${item}/*.c ${item}/*.cpp ${item}/*.cc)
    else()
        file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS ${item}/*.c ${item}/*.cpp ${item}/*.cc)
    endif()
    foreach(item ${SOURCES})
        string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" SOURCE_FILE ${item})
        message(STATUS ${SOURCE_FILE})
        list(APPEND PROJECT_SOURCES ${SOURCE_FILE})
    endforeach()
endforeach()

add_executable(${PROJECT_NAME}
    ${PROJECT_HEADERS}
    ${PROJECT_SOURCES}
)

target_include_directories(${PROJECT_NAME}
    PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/include
    ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/libxml2/include/libxml2
    ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/spdlog/include
    ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/cpp-httplib
)

set_target_properties(${PROJECT_NAME} PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/output/bin
)

set(PLUGIN_OUTPUT_DIR ${CMAKE_BINARY_DIR}/output/lib)
add_subdirectory(scadaplugin)
target_include_directories(${PROJECT_NAME}
    PRIVATE
    scadaplugin/basicwidgets
)

target_link_libraries(${PROJECT_NAME}
    PRIVATE
    Qt5::Widgets
    Qt5::UiPlugin
    Qt5::Network
    Qt5::SerialPort
)

target_link_directories(${PROJECT_NAME}
    PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/libxml2/lib
    ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/spdlog/lib
)

target_link_libraries(${PROJECT_NAME}
    PRIVATE 
    xml2
    spdlog
)

# ===== install =====
install(TARGETS ${PROJECT_NAME}
    RUNTIME DESTINATION ${PROJECT_NAME}/bin
)

# install plugin library
install(TARGETS scadaplugin
    LIBRARY DESTINATION ${PROJECT_NAME}/plugins
    ARCHIVE DESTINATION ${PROJECT_NAME}/plugins
)

# install monitor.xml
# install(FILES
#     ${CMAKE_CURRENT_SOURCE_DIR}/tests/monitor.xml
#     DESTINATION ${PROJECT_NAME}/conf
#     PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ
#                 GROUP_EXECUTE GROUP_READ
#                 WORLD_EXECUTE WORLD_READ
# )

# install conf
install(FILES
    ${CMAKE_CURRENT_SOURCE_DIR}/conf/monitor.service
    DESTINATION ${PROJECT_NAME}
    PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ
                GROUP_EXECUTE GROUP_READ
                WORLD_EXECUTE WORLD_READ
)

# install scripts
install(FILES
    ${CMAKE_CURRENT_SOURCE_DIR}/scripts/run.sh
    ${CMAKE_CURRENT_SOURCE_DIR}/scripts/install.sh
    DESTINATION ${PROJECT_NAME}
    PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ
                GROUP_EXECUTE GROUP_READ
                WORLD_EXECUTE WORLD_READ
)

# install third-party libraries
install(FILES
    ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/libxml2/lib/libxml2.so
    ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/libxml2/lib/libxml2.so.16
    ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/libxml2/lib/libxml2.so.16.2.0
    DESTINATION ${PROJECT_NAME}/lib
)

message(STATUS "install prefix: ${CMAKE_INSTALL_PREFIX}")
