# PhoneRail 宿主机构建 —— 不依赖 ESP-IDF，用来跑单元测试（CTest）。
#
#   bash host/test.sh            配置 + 编译 + ctest
#
# 与 IDF 目标共享的源码只有 ../main 里的与硬件无关部分。
# 新增一个既要上板又要被测的 .c，必须同时加进：
#   ../main/CMakeLists.txt 的 SRCS   和   下面的 PHONERAIL_CORE_SRCS
# HAL 里新增一路实现则要同时加进：
#   ../components/phonerail_hal/CMakeLists.txt 的 SRCS（esp 那份）
#   和下面的 PHONERAIL_HAL_SIM_SRCS（sim 那份）
# 新增一个器件驱动则是：
#   ../components/drv/CMakeLists.txt 的 SRCS   和   下面的 PHONERAIL_DRV_SRCS
cmake_minimum_required(VERSION 3.16)
project(phonerail_host C)

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
add_compile_options(-Wall -Wextra -Werror)

set(FW_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/..)

# ---- 被测代码 -------------------------------------------------------------
set(PHONERAIL_CORE_SRCS
    ${FW_ROOT}/main/version.c
    ${FW_ROOT}/main/rail.c
    ${FW_ROOT}/main/rail_cfg.c
    ${FW_ROOT}/main/i2cbus.c
    ${FW_ROOT}/main/slot.c
    ${FW_ROOT}/main/sched.c
    ${FW_ROOT}/main/tasks.c
    ${FW_ROOT}/main/chgmode.c
    ${FW_ROOT}/main/hubctl.c
    ${FW_ROOT}/main/pdmon.c
    ${FW_ROOT}/main/t_safety.c
    ${FW_ROOT}/main/t_actuator.c
    ${FW_ROOT}/main/t_thermal.c
    ${FW_ROOT}/main/evlog.c
    ${FW_ROOT}/main/cmd_json.c
    ${FW_ROOT}/main/cmd.c
    ${FW_ROOT}/main/cmd_exec.c
    ${FW_ROOT}/main/cmd_report.c
    ${FW_ROOT}/main/ulink.c
    ${FW_ROOT}/main/cmd_net.c
    ${FW_ROOT}/main/netcfg.c
    ${FW_ROOT}/main/mlink.c
    ${FW_ROOT}/main/prov.c
    ${FW_ROOT}/main/bringup.c
    ${FW_ROOT}/main/ntc.c
    ${FW_ROOT}/main/ota.c
)

# HAL：接口在 include/，宿主机链 sim/ 的实现（esp/ 那份只有 IDF 目标编）。
set(PHONERAIL_HAL_DIR ${FW_ROOT}/components/phonerail_hal)
set(PHONERAIL_HAL_SIM_SRCS
    ${PHONERAIL_HAL_DIR}/hal_err.c
    ${PHONERAIL_HAL_DIR}/hal_i2c_acct.c
    ${PHONERAIL_HAL_DIR}/sim/hal_lock_sim.c
    ${PHONERAIL_HAL_DIR}/sim/sim_hal.c
    ${PHONERAIL_HAL_DIR}/sim/hal_i2c_sim.c
    ${PHONERAIL_HAL_DIR}/sim/hal_gpio_sim.c
    ${PHONERAIL_HAL_DIR}/sim/sim_sr595.c
    ${PHONERAIL_HAL_DIR}/sim/sim_pca9555.c
    ${PHONERAIL_HAL_DIR}/sim/hal_pwm_sim.c
    ${PHONERAIL_HAL_DIR}/sim/hal_time_sim.c
    ${PHONERAIL_HAL_DIR}/sim/hal_store_sim.c
)

# 器件驱动：与平台无关（只经 hal_i2c_*），两个目标编的是同一份源码。
set(PHONERAIL_DRV_DIR ${FW_ROOT}/components/drv)
set(PHONERAIL_DRV_SRCS
    ${PHONERAIL_DRV_DIR}/ina226.c
    ${PHONERAIL_DRV_DIR}/dac63204.c
    ${PHONERAIL_DRV_DIR}/sr595.c
    ${PHONERAIL_DRV_DIR}/pca9555.c
    ${PHONERAIL_DRV_DIR}/tca9548a.c
    ${PHONERAIL_DRV_DIR}/ch224a.c
)

add_library(phonerail_core STATIC
    ${PHONERAIL_CORE_SRCS} ${PHONERAIL_HAL_SIM_SRCS} ${PHONERAIL_DRV_SRCS})
target_include_directories(phonerail_core PUBLIC
    ${FW_ROOT}/main
    ${PHONERAIL_HAL_DIR}/include
    ${PHONERAIL_HAL_DIR}/sim
    ${PHONERAIL_DRV_DIR}/include)

# ---- 测试框架 Unity（从 ESP-IDF v5.4 原样复制到 third_party/，与上板测试同一套）
add_library(unity STATIC ${FW_ROOT}/third_party/unity/unity.c)
target_include_directories(unity PUBLIC ${FW_ROOT}/third_party/unity)
target_compile_definitions(unity PUBLIC UNITY_OUTPUT_COLOR)
# 第三方源码不吃本工程的 -Werror
target_compile_options(unity PRIVATE -w)

# ---- 测试用例：test/test_*.c 一个文件一个可执行 + 一条 ctest ---------------
enable_testing()

file(GLOB PHONERAIL_TESTS ${FW_ROOT}/test/test_*.c)
foreach(test_src ${PHONERAIL_TESTS})
    get_filename_component(test_name ${test_src} NAME_WE)
    add_executable(${test_name} ${test_src})
    target_link_libraries(${test_name} PRIVATE phonerail_core unity)
    add_test(NAME ${test_name} COMMAND ${test_name})
endforeach()
