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

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

#设置模式Release为发布模式，debug为调试模式
set(CMAKE_BUILD_TYPE Release)

# **** 关键修改：强制构建静态库 ****
# 告诉 CMake 不构建动态库，只构建静态库
set(BUILD_SHARED_LIBS OFF CACHE BOOL "Build shared libraries" FORCE)

# 设置 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
    drivers/selfcontrol
    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
    third_party/cjson
)

# 源文件收集
file(GLOB_RECURSE SOURCES
    drivers/gpio/*.c
    drivers/network/*.c
    drivers/sensors/*.c
    drivers/devicecontrol/*.c
    drivers/selfcontrol/*.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
    third_party/cjson/cJSON.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
    uuid
)

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