Files
ViewDesignEngine/cmake/check_dependency_direction.cmake
hm 88653eff2f feat(plugin): P1 微内核 — 官方格式插件 + C API 拆分 + 模块模式/依赖守卫(1.0.46)
- P1-1:VDE_BUILD_IO 开关;C API 拆分 vde_kernel_capi(不链接 vde_io)/vde_io_capi/旧 vde_capi 聚合;
  kernel SDK 导出 vde::kernel_capi + vde::kernel::capi 别名;kernel-only 配置/构建/install/consumer 验证;
  src/capi/vde_capi_io.cpp、tests/capi/test_capi_split.cpp 新增;
- P1-2:插件 ABI 1.1(VdePluginHost 尾部追加 body_from_mesh,旧宿主/旧插件按 struct_size 兼容);
  官方格式插件 plugins/official/{stl,obj,ply}(纯 C ABI、不链接内核;STL 读写、OBJ/PLY 只读;与内置导入
  面数/体积/bbox 一致到 1e-9);import_cad 插件回退(内置优先 + VDE_IMPORT_CAD_PREFER_PLUGIN 显式);
  新增 tests/plugin/test_plugin_format_official.cpp 10 用例;
- P1-3:VDE_MODULES_MODE(static/hybrid 生效;shared/plugin 预留 WARNING);6 个业务模块独立开关;
  vde::io/vde::app 聚合 + vde::engine deprecated;cmake/check_dependency_direction.cmake +
  VDE_DependencyDirection_Guard CTest(六配置 PASS、负向可检出);
- 文档:plugin-development §10/§11、architecture §2.2–2.4/§7、design §7/§8 P1 进展;
- 版本 1.0.46(CMake/version.h/test_version/README/docs index/module-manual/CHANGELOG 同步)。

验证:全量构建 0 error;全量 ctest 7369 注册、7274/7274 PASS(0 failed,25 skipped,300.51s,-E Benchmark);
插件 ctest 54/54、官方插件 10/10、test_capi_split 10/10(kernel 7+io 3);kernel-only / no-cam / 业务全关 /
static / shared 配置均通过;依赖守卫 PASS。
2026-10-28 14:46:31 +08:00

319 lines
13 KiB
CMake
Raw Permalink 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.
# ═══════════════════════════════════════════════════════════════════════════
# VDE 微内核 P1-3:依赖方向 CI 守卫(architecture guard,CMake 脚本模式)
#
# 用法(configure 时由 BUILD_TESTS 自动注册为 CTest 用例):
# cmake -DSOURCE_DIR=<repo-root> -P cmake/check_dependency_direction.cmake
#
# 检查内容(架构硬规则,见 docs/guides/architecture.md §2.3/§7):
# 1. VDE_DEPS 表:所有模块有依赖条目、依赖模块都存在、无依赖环;
# 2. 内核模块闭包只含内核模块(+ platform 基础模块),不得包含
# io / cam / cae / capi / render / ui / drafting / pdm / mechanism /
# license / expression / param / event / infra / sketch 等业务模块;
# 3. src/CMakeLists.txt 的 target_link_libraries(vde_<m> ...) 无反向依赖:
# kernel 层目标只可链接 kernel;io 层目标只可链接 kernel/io(io → kernel 单向);
# 应用层(其余模块)可链接任意下层(不在此守卫检查)。
# 4. vde_kernel 聚合列表 set(_vde_kernel_modules ...) ⊆ 内核模块集合。
#
# 退出码:0 通过;有违规时 message(FATAL_ERROR) 输出具体条目(ctest 失败)。
# ═══════════════════════════════════════════════════════════════════════════
cmake_minimum_required(VERSION 3.16)
if(NOT DEFINED SOURCE_DIR)
message(FATAL_ERROR "[dep-guard] 缺少 -DSOURCE_DIR=<repo-root>")
endif()
set(_root_cmake "${SOURCE_DIR}/CMakeLists.txt")
set(_src_cmake "${SOURCE_DIR}/src/CMakeLists.txt")
if(NOT EXISTS "${_root_cmake}" OR NOT EXISTS "${_src_cmake}")
message(FATAL_ERROR "[dep-guard] 找不到 ${_root_cmake} 或 ${_src_cmake}")
endif()
# ── 基准集合(与 docs/guides/architecture.md §2/§7 一致)────────────────
# 内核模块(vde::kernel 聚合):几何内核 + 插件管理器
set(_kernel_modules foundation plugin core compute curves mesh spatial boolean collision sdf brep)
# 基础模块 platform 被 foundation 以 PUBLIC 依赖,属内核闭包的一部分
set(_kernel_base_modules platform)
set(_kernel_allowed ${_kernel_modules} ${_kernel_base_modules})
# IO/集成层(只能单向依赖内核)
set(_io_modules io)
string(ASCII 9 _TAB)
string(ASCII 13 _CR)
# ── 文本工具 -------------------------------------------------------------
function(_vde_tokens text out)
string(REPLACE " " ";" _t "${text}")
string(REPLACE "${_TAB}" ";" _t "${_t}")
string(REPLACE "${_CR}" ";" _t "${_t}")
set(_clean "")
foreach(_tok IN LISTS _t)
if(_tok STREQUAL "")
continue()
endif()
string(REPLACE "(" "" _tok "${_tok}")
string(REPLACE ")" "" _tok "${_tok}")
string(REPLACE "\"" "" _tok "${_tok}")
if(_tok STREQUAL "")
continue()
endif()
list(APPEND _clean "${_tok}")
endforeach()
set(${out} "${_clean}" PARENT_SCOPE)
endfunction()
function(_vde_lstrip text out)
set(_s "${text}")
string(LENGTH "${_s}" _len)
set(_idx 0)
while(_idx LESS _len)
string(SUBSTRING "${_s}" ${_idx} 1 _ch)
if(NOT _ch STREQUAL " ")
break()
endif()
math(EXPR _idx "${_idx} + 1")
endwhile()
string(SUBSTRING "${_s}" ${_idx} -1 _s)
set(${out} "${_s}" PARENT_SCOPE)
endfunction()
# 解析 set(<marker> ... ) 列表(marker 含 "set(" 前缀;支持跨行,遇 ')' 结束)
function(_vde_parse_set_list lines marker out found)
set(_acc "")
set(_in FALSE)
set(_seen FALSE)
foreach(_line IN LISTS ${lines})
if(NOT _in)
string(FIND "${_line}" "${marker}" _pos)
if(_pos EQUAL 0)
set(_in TRUE)
set(_seen TRUE)
string(LENGTH "${marker}" _mlen)
string(SUBSTRING "${_line}" ${_mlen} -1 _acc)
endif()
else()
string(APPEND _acc " " "${_line}")
endif()
if(_in)
string(FIND "${_acc}" ")" _q)
if(NOT _q EQUAL -1)
string(SUBSTRING "${_acc}" 0 ${_q} _acc)
set(_in FALSE)
endif()
endif()
endforeach()
if(_seen)
_vde_tokens("${_acc}" _toks)
set(${out} "${_toks}" PARENT_SCOPE)
else()
set(${out} "" PARENT_SCOPE)
endif()
set(${found} ${_seen} PARENT_SCOPE)
endfunction()
# 模块 → 层级:1=kernel(+platform/kernel_capi),2=io,3=应用层,0=未知/聚合目标/基础工具
# 说明:kernel_capi 属内核层(P1-1 拆分后要求它不链接 vde_io),io_capi/capi 属应用层。
function(_vde_layer module out)
set(_layer 0)
if("${module}" STREQUAL "kernel_capi")
set(_layer 1)
elseif("${module}" IN_LIST _kernel_allowed)
set(_layer 1)
elseif("${module}" STREQUAL "io")
set(_layer 2)
elseif("${module}" STREQUAL "io_capi")
set(_layer 3)
elseif("${module}" STREQUAL "capi" OR "${module}" STREQUAL "capi_shared")
set(_layer 3)
elseif("${module}" IN_LIST _modules)
set(_layer 3)
endif()
set(${out} ${_layer} PARENT_SCOPE)
endfunction()
file(STRINGS "${_root_cmake}" _root_lines)
file(STRINGS "${_src_cmake}" _src_lines)
set(_violations "")
# ── 1) 解析 VDE_MODULES / VDE_DEPS 表 ────────────────────────────────────
_vde_parse_set_list(_root_lines "set(VDE_MODULES" _modules _mods_found)
if(NOT _mods_found OR NOT _modules)
message(FATAL_ERROR "[dep-guard] 根 CMakeLists 中未解析到 set(VDE_MODULES ...)")
endif()
set(_deps_keys "")
foreach(_line IN LISTS _root_lines)
string(FIND "${_line}" "set(VDE_DEPS_" _p)
if(NOT _p EQUAL 0)
continue()
endif()
string(LENGTH "set(VDE_DEPS_" _plen)
string(SUBSTRING "${_line}" ${_plen} -1 _rest)
string(FIND "${_rest}" " " _sp)
if(NOT _sp GREATER 0)
continue()
endif()
string(SUBSTRING "${_rest}" 0 ${_sp} _m)
string(FIND "${_rest}" "\"" _q1)
string(FIND "${_rest}" "\"" _q2 REVERSE)
if(_q1 EQUAL -1 OR _q2 EQUAL -1 OR NOT _q2 GREATER _q1)
list(APPEND _violations "VDE_DEPS_${_m} 行格式非法: ${_line}")
continue()
endif()
math(EXPR _vlen "${_q2} - ${_q1} - 1")
if(_vlen GREATER 0)
math(EXPR _vstart "${_q1} + 1")
string(SUBSTRING "${_rest}" ${_vstart} ${_vlen} _deps_text)
else()
set(_deps_text "")
endif()
_vde_tokens("${_deps_text}" _ds)
set(_deps_${_m} "${_ds}")
list(APPEND _deps_keys "${_m}")
endforeach()
# 模块 ↔ 依赖条目完整性
foreach(_m IN LISTS _modules)
if(NOT DEFINED _deps_${_m})
list(APPEND _violations "VDE_DEPS 缺少模块 '${_m}' 的依赖条目")
set(_deps_${_m} "")
endif()
endforeach()
foreach(_m IN LISTS _deps_keys)
if(NOT _m IN_LIST _modules)
list(APPEND _violations "VDE_DEPS_${_m} 不在 VDE_MODULES 中")
endif()
endforeach()
# 依赖存在性 / 无环 / 闭包
foreach(_start IN LISTS _modules)
set(_visited "")
set(_queue ${_start})
while(_queue)
list(POP_FRONT _queue _cur)
set(_cur_deps "${_deps_${_cur}}")
foreach(_d IN LISTS _cur_deps)
if(NOT _d IN_LIST _modules)
list(APPEND _violations "VDE_DEPS_${_cur} 依赖未知模块 '${_d}'")
continue()
endif()
if(NOT _d IN_LIST _visited)
list(APPEND _visited "${_d}")
list(APPEND _queue "${_d}")
endif()
endforeach()
endwhile()
if(_start IN_LIST _visited)
list(APPEND _violations "VDE_DEPS 存在依赖环: '${_start}' 的闭包含自身")
endif()
set(_closure_${_start} "${_visited}")
endforeach()
# ── 2) 内核闭包纯净性 ───────────────────────────────────────────────────
foreach(_k IN LISTS _kernel_modules)
if(NOT _k IN_LIST _modules)
list(APPEND _violations "内核模块 '${_k}' 不在 VDE_MODULES 中")
continue()
endif()
set(_k_closure "${_closure_${_k}}")
foreach(_c IN LISTS _k_closure)
if(NOT _c IN_LIST _kernel_allowed)
list(APPEND _violations "kernel 闭包违规: ${_k} -> ${_c}(业务模块进入内核依赖闭包)")
endif()
endforeach()
endforeach()
# ── 3) vde_kernel 聚合列表纯净性 ────────────────────────────────────────
_vde_parse_set_list(_src_lines "set(_vde_kernel_modules" _kernel_agg _kagg_found)
if(NOT _kagg_found OR NOT _kernel_agg)
list(APPEND _violations "src/CMakeLists.txt 未找到 set(_vde_kernel_modules ...) 聚合列表")
else()
foreach(_t IN LISTS _kernel_agg)
if(NOT _t IN_LIST _kernel_allowed)
list(APPEND _violations "vde::kernel 聚合包含非内核模块 '${_t}'")
endif()
endforeach()
endif()
# ── 4) target_link_libraries 反向依赖扫描 ───────────────────────────────
list(LENGTH _src_lines _src_n)
math(EXPR _src_last "${_src_n} - 1")
set(_i 0)
set(_edge_count 0)
while(_i LESS _src_last)
list(GET _src_lines ${_i} _raw)
_vde_lstrip("${_raw}" _line)
string(FIND "${_line}" "target_link_libraries(" _tll)
if(_tll EQUAL 0)
string(LENGTH "target_link_libraries(" _plen)
string(SUBSTRING "${_line}" ${_plen} -1 _block)
string(FIND "${_block}" ")" _q)
set(_j ${_i})
while(_q EQUAL -1 AND _j LESS _src_last)
math(EXPR _j "${_j} + 1")
list(GET _src_lines ${_j} _next)
string(APPEND _block " " "${_next}")
string(FIND "${_block}" ")" _q)
endwhile()
if(NOT _q EQUAL -1)
string(SUBSTRING "${_block}" 0 ${_q} _block)
endif()
_vde_tokens("${_block}" _toks)
list(LENGTH _toks _toks_n)
if(_toks_n GREATER 0)
list(GET _toks 0 _consumer)
set(_cmod "")
string(FIND "${_consumer}" "vde_" _vpos)
if(_vpos EQUAL 0)
string(SUBSTRING "${_consumer}" 4 -1 _cmod)
endif()
_vde_layer("${_cmod}" _clayer)
if(_clayer EQUAL 1 OR _clayer EQUAL 2)
set(_ti 1)
while(_ti LESS _toks_n)
list(GET _toks ${_ti} _dep)
math(EXPR _ti "${_ti} + 1")
string(FIND "${_dep}" "vde_" _dpos)
if(NOT _dpos EQUAL 0)
continue()
endif()
if(_dep STREQUAL "vde_kernel" OR _dep STREQUAL "vde_app"
OR _dep STREQUAL "vde" OR _dep STREQUAL "vde_compile_options")
continue()
endif()
string(SUBSTRING "${_dep}" 4 -1 _dmod)
if(_dmod STREQUAL "capi_shared")
set(_dmod "capi")
endif()
_vde_layer("${_dmod}" _dlayer)
if(_dlayer EQUAL 0)
continue()
endif()
math(EXPR _edge_count "${_edge_count} + 1")
if(_clayer EQUAL 1 AND NOT _dlayer EQUAL 1)
list(APPEND _violations
"反向依赖: ${_consumer}(kernel 层)-> ${_dep}(层 ${_dlayer})")
elseif(_clayer EQUAL 2 AND _dlayer GREATER 2)
list(APPEND _violations
"反向依赖: ${_consumer}(io 层)-> ${_dep}(应用层)")
endif()
endwhile()
endif()
endif()
set(_i ${_j})
endif()
math(EXPR _i "${_i} + 1")
endwhile()
# ── 结果 ────────────────────────────────────────────────────────────────
list(LENGTH _modules _mod_count)
list(LENGTH _deps_keys _dep_count)
list(LENGTH _violations _viol_count)
message(STATUS "[dep-guard] VDE_MODULES=${_mod_count} 模块,VDE_DEPS=${_dep_count} 条依赖表")
message(STATUS "[dep-guard] 内核聚合: ${_kernel_modules}")
message(STATUS "[dep-guard] target_link_libraries 扫描:${_edge_count} 条 vde_* 依赖边")
if(_viol_count GREATER 0)
string(REPLACE ";" "\n - " _vtext "${_violations}")
message(FATAL_ERROR "[dep-guard] 发现 ${_viol_count} 处架构违规:\n - ${_vtext}")
endif()
message(STATUS "[dep-guard] kernel/io/app 依赖方向符合微内核分层规则(PASS)")