Files
smart-home-control-system/CMakeLists.txt
T
sambu 578a770737 chore(M0): 建立 CLion/CMake 工程骨架与测试框架
- CMake 多目标工程:shs_core 静态库 + smart_home 可执行 + shs_tests 测试
- CMakePresets.json:debug/release/asan/coverage 四个预设(debug 告警即错误)
- 自研零依赖测试框架 tests/framework/test_framework.h + 运行器(套件过滤、CTest 集成)
- 工具:tools/header_check.sh 头文件独立编译检查(接口冻结门)
- 已实现公共层:errors / string_utils / num_utils,main 启动自检
- 实测:debug 预设 0 告警、ctest 3/3 通过(45 项断言)
2026-09-14 19:34:18 +08:00

112 lines
4.7 KiB
CMake
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# ============================================================================
# 智能家居控制系统 (Smart Home Control System)
# 《C语言程序设计项目化教程——智能家居控制系统开发实战》配套工程
# 工程格式:CLion (CMake) · 语言标准:C17 · 目标依赖:零第三方库
# ============================================================================
cmake_minimum_required(VERSION 3.20)
project(smart_home_control_system
VERSION 1.0.0
DESCRIPTION "智能家居控制系统 - C语言项目化教学配套工程"
LANGUAGES C)
# ---------------------------------------------------------------- 语言与编译选项
set(CMAKE_C_STANDARD 17)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
set(CMAKE_BUILD_TYPE Debug CACHE STRING "构建类型" FORCE)
endif()
option(SHS_WERROR "把编译告警视为错误" OFF)
option(SHS_ENABLE_SANITIZERS "启用 ASan/UBSan 内存检查" OFF)
option(SHS_ENABLE_COVERAGE "启用 gcov 覆盖率插桩" OFF)
option(SHS_BUILD_TESTS "构建测试目标" ON)
# 统一告警策略接口库:所有目标都链接它
add_library(shs_warnings INTERFACE)
if(MSVC)
target_compile_options(shs_warnings INTERFACE /W4)
else()
target_compile_options(shs_warnings INTERFACE -Wall -Wextra -Wpedantic)
if(SHS_WERROR)
target_compile_options(shs_warnings INTERFACE -Werror)
endif()
if(SHS_ENABLE_SANITIZERS)
target_compile_options(shs_warnings INTERFACE -fsanitize=address,undefined -fno-omit-frame-pointer)
target_link_options(shs_warnings INTERFACE -fsanitize=address,undefined)
endif()
if(SHS_ENABLE_COVERAGE)
target_compile_options(shs_warnings INTERFACE --coverage -O0 -g)
target_link_options(shs_warnings INTERFACE --coverage)
endif()
endif()
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
# ---------------------------------------------------------------- 源码清单
# 公共层(项目二 数据类型与运算符、项目五 字符串)
set(SHS_COMMON_SOURCES
src/common/errors.c
src/common/string_utils.c
src/common/num_utils.c)
# 核心库:业务层 + 抽象层 + 协议层 + 存储层 + 公共层(后续里程碑逐模块补齐)
set(SHS_CORE_SOURCES ${SHS_COMMON_SOURCES})
add_library(shs_core STATIC ${SHS_CORE_SOURCES})
target_include_directories(shs_core PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_link_libraries(shs_core PRIVATE shs_warnings)
set_target_properties(shs_core PROPERTIES OUTPUT_NAME shs_core)
# ---------------------------------------------------------------- 主程序
add_executable(smart_home src/main.c)
target_link_libraries(smart_home PRIVATE shs_core shs_warnings)
if(WIN32)
target_link_libraries(smart_home PRIVATE ws2_32)
endif()
# ---------------------------------------------------------------- 测试
if(SHS_BUILD_TESTS)
enable_testing()
set(SHS_TEST_SOURCES
tests/framework/test_framework.c
tests/test_runner.c
tests/unit/test_string.c
tests/unit/test_num.c)
add_executable(shs_tests ${SHS_TEST_SOURCES})
target_link_libraries(shs_tests PRIVATE shs_core shs_warnings)
target_include_directories(shs_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR}/tests)
# 每个测试套件在 CTest 中独立注册(ctest -R unit.string)
set(SHS_TEST_SUITES
unit.string
unit.num)
foreach(suite IN LISTS SHS_TEST_SUITES)
add_test(NAME ${suite} COMMAND shs_tests --suite ${suite})
endforeach()
add_test(NAME unit.list COMMAND shs_tests --list)
endif()
# ---------------------------------------------------------------- 头文件检查
add_custom_target(headers-check
COMMAND ${CMAKE_COMMAND} -E env CC=${CMAKE_C_COMPILER}
bash ${CMAKE_CURRENT_SOURCE_DIR}/tools/header_check.sh
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMENT "检查 include/shs/*.h 是否可独立编译(接口冻结门)")
# ---------------------------------------------------------------- 打印摘要
message(STATUS "=====================================================================")
message(STATUS " 智能家居控制系统 - 智能家居控制系统开发实战配套工程")
message(STATUS " 版本 : ${PROJECT_VERSION}")
message(STATUS " 构建类型 : ${CMAKE_BUILD_TYPE}")
message(STATUS " C 标准 : C${CMAKE_C_STANDARD}")
message(STATUS " 告警即错误: ${SHS_WERROR}")
message(STATUS " 内存检查 : ${SHS_ENABLE_SANITIZERS}")
message(STATUS " 覆盖率 : ${SHS_ENABLE_COVERAGE}")
message(STATUS "=====================================================================")