# 微内核 P0（P0-6）：开发者 CLI 工具
#
# vde_plugin_tool —— 插件/清单/ABI 诊断：
#   vde_plugin_tool --list <dir>       扫描目录（递归 2 层）列出 vde-plugin.json
#   vde_plugin_tool --inspect <file>   解析并打印单个插件清单
#   vde_plugin_tool --check <dir|manifest>
#                                       自检：清单必填字段/kind/ABI 兼容/query 一致性/
#                                       artifacts SHA-256（P3-12；退出码同 verify）
#   vde_plugin_tool --build-index <plugins_dir> -o <index.json> [--sign <seed>]
#                                       P3-13：扫描插件包生成仓库索引（artifact SHA-256，
#                                       可选 Ed25519 签名）
#   vde_plugin_tool --verify-index <index.json> [--pubkey] [--allow-unsigned]
#                                       P3-13：校验索引 schema/签名/工件哈希/ABI 兼容
#   vde_plugin_tool --install <index.json|plugin_dir> --dest <dir>
#                                       P3-13：校验后安装插件包到 <dest>/<id>/
#   vde_plugin_tool scaffold <kind> <name> --out <dir> [--templates <dir>] [--force]
#                                       P3-17：从 plugins/templates 生成可构建插件工程
#   vde_plugin_tool --fetch-index <url|file> -o <index.json> [--pubkey] [--allow-unsigned]
#                                       P3-17：获取远程/本地仓库索引并校验（Windows WinHTTP）
#   vde_plugin_tool --abi              打印宿主 ABI / 内核版本 / C 结构体尺寸
#
# 该工具依赖宿主侧插件管理器（vde_plugin）；-DVDE_BUILD_PLUGIN=OFF 时跳过。

if(NOT VDE_ENABLE_plugin)
    message(STATUS "VDE P0-6: tools/vde_plugin_tool skipped (VDE_BUILD_PLUGIN=OFF)")
else()
    add_executable(vde_plugin_tool vde_plugin_tool/main.cpp)
    target_link_libraries(vde_plugin_tool PRIVATE vde_plugin vde_foundation
        nlohmann_json::nlohmann_json)
    target_compile_features(vde_plugin_tool PRIVATE cxx_std_20)
    # P3-1：verify 命令的 Ed25519 验签实现由宿主（本工具）注入 vde_license；
    # 内核 vde_plugin 不依赖 vde_license（分层要求）。license 模块关闭时
    # verify 仍可用（仅结构/策略检查，加密校验返回退出码 4）。
    if(TARGET vde_license)
        target_link_libraries(vde_plugin_tool PRIVATE vde_license)
        target_compile_definitions(vde_plugin_tool PRIVATE VDE_TOOL_HAS_ED25519=1)
    endif()
    # P3-17：--fetch-index 的 http(s) 获取在 Windows 上使用 WinHTTP
    if(WIN32)
        target_link_libraries(vde_plugin_tool PRIVATE winhttp)
    endif()
    if(VDE_INSTALL_SDK)
        include(GNUInstallDirs)
        install(TARGETS vde_plugin_tool RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
    endif()
    # P3-17b：scaffold 端到端 CTest 注册在 tests/plugin/CMakeLists.txt（enable_testing 之后；
    # 脚本本身位于 tools/vde_plugin_tool/scaffold_smoke.cmake）。
endif()

# B-Rep 健康/解析诊断 CLI：validate + diagnose + STEP 报告 + 可选 heal/deep
if(TARGET vde_brep)
    add_executable(vde_brep_check vde_brep_check.cpp)
    target_link_libraries(vde_brep_check PRIVATE vde)
    target_compile_features(vde_brep_check PRIVATE cxx_std_17)
    if(MSVC)
        target_link_options(vde_brep_check PRIVATE
            "/MAP:${CMAKE_BINARY_DIR}/vde_brep_check.map")
    endif()
    if(VDE_INSTALL_SDK)
        include(GNUInstallDirs)
        install(TARGETS vde_brep_check RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
    endif()
endif()

# v1.2: .vde inspect / validate / text<->binary CLI
if(TARGET vde_brep)
    add_executable(vde_format_tool vde_format_tool.cpp)
    target_link_libraries(vde_format_tool PRIVATE vde_brep)
    target_compile_features(vde_format_tool PRIVATE cxx_std_17)
    if(VDE_INSTALL_SDK)
        include(GNUInstallDirs)
        install(TARGETS vde_format_tool RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
    endif()
    if(BUILD_TESTS)
        add_test(NAME vde_format_tool_selftest
                 COMMAND vde_format_tool selftest "${CMAKE_BINARY_DIR}")
    endif()
endif()


# 说明：本仓库用 BUILD_TESTS（VDE 自有选项，默认 ON）控制测试构建；
# BUILD_TESTING 是 CTest 模块变量，本工程未 include(CTest)，恒为 OFF。
# 工具自检此前误用 BUILD_TESTING，导致 add_test 从未注册（1.0.142 修正）。

# GPU/CPU 计算后端探测与基准（OpenCL/CUDA 动态后端）
# VDE-1278：可选 --mode cloud，测点云 kNN 法线/SOR 的 CPU vs GPU 路径。
if(TARGET vde_compute)
    add_executable(vde_gpu_probe vde_gpu_probe.cpp)
    target_link_libraries(vde_gpu_probe PRIVATE vde_compute)
    target_compile_features(vde_gpu_probe PRIVATE cxx_std_17)
    if(TARGET vde_mesh)
        target_link_libraries(vde_gpu_probe PRIVATE vde_mesh)
        target_compile_definitions(vde_gpu_probe PRIVATE VDE_GPU_PROBE_HAVE_MESH=1)
    endif()
    # --file（真实 STL 采样点云）需要 foundation 的 read_stl
    if(TARGET vde_foundation)
        target_link_libraries(vde_gpu_probe PRIVATE vde_foundation)
        target_compile_definitions(vde_gpu_probe PRIVATE VDE_GPU_PROBE_HAVE_IO=1)
    endif()
    if(TARGET vde_mesh)
        if(BUILD_TESTS)
            # 小点云冒烟（无 GPU 时产品代码如实回退 CPU，命令仍返回 0）。
            add_test(NAME vde_gpu_probe_cloud_selftest
                     COMMAND vde_gpu_probe --mode cloud --points 2000 --json)
        endif()
    endif()
    # VDE-1296：--mode sdf 对比旧 O(F) 暴力 / CPU LBVH / GPU 批量 mesh→SDF 采样
    if(TARGET vde_sdf)
        target_link_libraries(vde_gpu_probe PRIVATE vde_sdf)
        target_compile_definitions(vde_gpu_probe PRIVATE VDE_GPU_PROBE_HAVE_SDF=1)
        if(BUILD_TESTS)
            add_test(NAME vde_gpu_probe_sdf_selftest
                     COMMAND vde_gpu_probe --mode sdf --tri 2000 --points 512 --json)
        endif()
    endif()
    if(BUILD_TESTS)
        # VDE-1284：GPU BVH 射线批量查询冒烟（无 GPU 时如实回退 CPU，返回 0）。
        add_test(NAME vde_gpu_probe_rays_selftest
                 COMMAND vde_gpu_probe --mode rays --tri 20000 --rays 256 --json)
        add_test(NAME vde_gpu_probe_closest_selftest
                 COMMAND vde_gpu_probe --mode closest --tri 20000 --points 512 --json)
    endif()
endif()
# 文件 IO 微基准（STL 二进制/ASCII、OBJ、PLY 读写吞吐 + 大文件阶段分解）
# VDE-1277：为 IO 性能优化与 GPU 后处理提供可复现基线。
if(TARGET vde_io)
    add_executable(vde_io_bench vde_io_bench.cpp)
    target_link_libraries(vde_io_bench PRIVATE vde)
    target_compile_features(vde_io_bench PRIVATE cxx_std_17)
    if(VDE_INSTALL_SDK)
        include(GNUInstallDirs)
        install(TARGETS vde_io_bench RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
    endif()
    if(BUILD_TESTS)
        # 极小合成网格冒烟：覆盖 STL(二进制/ASCII)/OBJ/PLY 的写→读往返。
        add_test(NAME vde_io_bench_selftest
                 COMMAND vde_io_bench --triangles 2000 --repeat 1
                         --formats stl,stl_ascii,obj,ply --json)
    endif()
endif()
