cmake_minimum_required(VERSION 3.10)
project(car 
    VERSION 1.3.11
    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为调试模式
#切换环境只需改这一行：Release=生产环境(只打印WARN及以上)，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
    modules/ipc
    modules/go_deploy
    modules/tailscale_deploy
    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
    modules/ipc/*.c
    modules/go_deploy/*.c
    modules/tailscale_deploy/*.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_compile_definitions(main PRIVATE
    PROJECT_ROOT_DIR="${CMAKE_CURRENT_SOURCE_DIR}"
)

# Release模式下定义LOG_PRODUCTION宏，编译时剔除debug和info日志
if(CMAKE_BUILD_TYPE STREQUAL "Release")
    target_compile_definitions(main PRIVATE LOG_PRODUCTION)
endif()

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

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