cmake_minimum_required(VERSION 3.10)
project(car 
    VERSION 1.3.11
    LANGUAGES C CXX
)

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

# 设置模式 Release 为发布模式，Debug 为调试模式。
# 平时切换只需改这一行；命令行 -DCAR_BUILD_TYPE=Debug 会优先生效。
# 1、Debug 编译（推荐，不改源码）
# cmake -S . -B build -DCAR_BUILD_TYPE=Debug
# cmake --build build -j2
# 2. Release 编译（切回发布版）
# cmake -S . -B build -DCAR_BUILD_TYPE=Release
# cmake --build build -j2

set(CAR_DEFAULT_BUILD_TYPE "Release")
if(NOT DEFINED CAR_BUILD_TYPE)
    set(CAR_BUILD_TYPE "${CAR_DEFAULT_BUILD_TYPE}")
endif()
set(CMAKE_BUILD_TYPE "${CAR_BUILD_TYPE}" CACHE STRING "Build type" FORCE)

# **** 关键修改：强制构建静态库 ****
# 告诉 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)

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

add_subdirectory(third_party)
add_subdirectory(app)
add_subdirectory(drivers)
add_subdirectory(modules)

# 创建可执行文件
add_executable(main
    ${APP_SOURCES}
    ${DRIVERS_SOURCES}
    ${MODULES_SOURCES}
    ${THIRD_PARTY_SOURCES}
)

target_include_directories(main PRIVATE
    ${CMAKE_CURRENT_BINARY_DIR}/include
    ${CMAKE_CURRENT_SOURCE_DIR}/include
    ${APP_INCLUDE_DIRS}
    ${DRIVERS_INCLUDE_DIRS}
    ${MODULES_INCLUDE_DIRS}
    ${THIRD_PARTY_INCLUDE_DIRS}
)

# 将项目根目录传入代码，消除硬编码路径
target_compile_definitions(main PRIVATE
    PROJECT_ROOT_DIR="${CMAKE_CURRENT_SOURCE_DIR}"
    GST_USE_UNSTABLE_API
)

# 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 生成的静态库目标
    LibDataChannel::LibDataChannel
    wiringPi
    cjson
    curl
    m
    ssl
    crypto
    pthread
    dl
    uuid
    ${WEBRTCPUSH_LIBRARIES}
)

# libdatachannel is C++, while the application sources use its stable C API.
set_target_properties(main PROPERTIES LINKER_LANGUAGE CXX)

target_compile_options(main PRIVATE ${WEBRTCPUSH_CFLAGS})

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

enable_testing()
