cmake_minimum_required(VERSION 3.10)
project(car 
    VERSION 1.2.0
    LANGUAGES C
)

# 在CMakeLists.txt开头添加调试信息
message(STATUS "Current source dir: ${CMAKE_CURRENT_SOURCE_DIR}")
message(STATUS "Mosquitto path: ${CMAKE_CURRENT_SOURCE_DIR}/third_party/mosquitto")

# 验证目录是否存在
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/third_party/mosquitto")
    message(STATUS "Mosquitto directory exists")
else()
    message(FATAL_ERROR "Mosquitto directory MISSING at: ${CMAKE_CURRENT_SOURCE_DIR}/third_party/mosquitto")
endif()

# 设置 mosquitto 静态编译选项
set(WITH_STATIC_LIBRARIES ON CACHE BOOL "Build static library" FORCE)
set(WITH_SHARED_LIBRARIES OFF CACHE BOOL "Disable shared library" FORCE)
set(WITH_DOCS OFF CACHE BOOL "Disable documentation" FORCE)

# 添加 mosquitto 子项目
add_subdirectory(third_party/mosquitto)

add_library(cjson STATIC
    third_party/cjson/cJSON.c  # 假设cJSON源码在项目cjson目录下
)

# 版本文件配置
configure_file(
    ${CMAKE_CURRENT_SOURCE_DIR}/include/version.h.in
    ${CMAKE_CURRENT_BINARY_DIR}/include/version.h
)

# 包含目录
include_directories(
    ${CMAKE_CURRENT_BINARY_DIR}/include
    include
    app/device_identity
    app/main
    app/device_change
    drivers/gpio
    drivers/network
    drivers/sensors
    drivers/devicecontrol
    modules/logger
    modules/delay
    modules/thread_pool
    modules/mqtt
    modules/http
    modules/browser
    third_party/zlog/src
    third_party/mosquitto/include
    third_party/mosquitto/lib
)

# 源文件收集
file(GLOB_RECURSE SOURCES
    drivers/gpio/*.c
    drivers/network/*.c
    drivers/sensors/*.c
    drivers/devicecontrol/*c
    modules/logger/*.c
    modules/delay/*.c
    modules/thread_pool/*.c
    modules/mqtt/*.c
    modules/http/*.c
    modules/browser/*.c
    app/device_identity/*.c
    app/device_change/*.c
    app/main/*.c
    third_party/zlog/src/*.c
)

list(FILTER SOURCES EXCLUDE REGEX "third_party/zlog/src/zlog-chk-conf.c")
list(FILTER SOURCES EXCLUDE REGEX "third_party/zlog/src/zlog_win.c")
list(FILTER SOURCES EXCLUDE REGEX "third_party/cjson/cJSON.c") 

# 创建可执行文件
add_executable(main ${SOURCES})

# 正确链接静态库
target_link_libraries(main PRIVATE
    libmosquitto_static  # 使用 mosquitto 生成的静态库目标
    wiringPi
    cjson
    curl
    m
    ssl
    crypto
    pthread
    dl
)

# 安装规则
install(TARGETS main DESTINATION bin)
enable_testing()