Files
VSAG_SENTRY_SIM_27RMUL/docs/构建体系与代码分层.md
T

211 lines
9.0 KiB
Markdown
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.
# 构建体系与代码分层
> **状态**:✅ 已落地(全工程只用 xmake,colcon / CMake / package.xml 已删除)
> **最后更新**:2026-11-17
> **涉及**:仓库根 `xmake.lua`、`src/sentry_nav/`、`src/sentry_sim/`(纯数据包)
## 1. 一句话结论
> **C++ 标准用 23**(下限 C++17,被依赖强制);
> **整个工程用 xmake**,仓库根一条 `xmake`,增量 0.5 秒,不需要 source ROS;
> **没有 colcon / ament 注册**,所以也没有 `ros2 run`;
> **纯算法放在 `src/core/`**,保留毫秒级的单测快速回路。
```
仓库根 xmake.lua
└── includes("src/sentry_nav")
├── sentry_nav_core (static) src/core/*.cpp 零 ROS、零 PCL
├── self_filter (binary) ROS 胶水
├── patrol (binary) ROS 胶水
└── test_* (binary) 223 项检查,5 ms
src/sentry_sim/ 纯数据包:SDF / STL / YAML / py / sh —— 没有源码,不编译
```
---
## 2. C++ 标准:为什么是 23
用 `g++ -std=xxx -fsyntax-only` 编译「rclcpp + nav2_msgs + tf2 + PCL」的最小文件:
| 标准 | 结果 | 首条报错 |
|---|---|---|
| **C++11** | ❌ | `'make_unique' is not a member of 'std'`(要 14) |
| **C++14** | ❌ | `'is_same_v' is not a member of 'std'`(要 17) |
| **C++17** | ✅ | — |
| C++20 / **C++23** | ✅ | — ← 当前用 23 |
PCL 更直接,在头文件里 `#error` 硬拦:
```
/usr/include/pcl-1.15/pcl/pcl_config.h:7: #error C++ standard too low (PCL requires 201703L or above)
```
**结论:C++17 是硬下限。** 「改成 C++11」不是风格问题,是编译不过。
改标准只有一处:仓库根 `xmake.lua` 的 `set_languages("c++23")`。
---
## 3. 构建体系:为什么全用 xmake
### 3.1 现状
| 能力 | 现在(xmake) | 以前(colcon + CMake) |
|---|---|---|
| 编出 `self_filter` / `patrol` | ✅ | ✅ |
| **增量编译** | **0.52 秒** | 3.84 秒 |
| 需要 `source /opt/ros/...` | ❌ | ✅ |
| 跑单测 | **`xmake test`,5 ms** | 要起 Gazebo |
| 编译数据库 | **构建时自动重写** | 要手工合并 |
| 一个工程几个构建系统 | **1 个** | 2 个 |
| `ros2 run` / ament 注册 | ❌ **没有了** | ✅ |
### 3.2 关键技术点:靠 linkgroup 解决链接顺序
最初手写 xmake 链接时报:
```
undefined reference to symbol 'rcl_timer_call_with_info'
librcl.so: error adding symbols: DSO missing from command line
```
原因是 **GNU ld 单遍扫描**:`bar` 依赖 `foo` 而 `foo` 排在前面时,`foo` 里的符号就丢了。
ament 会自动展开依赖链并排好序,手写就得自己排。
**解法**(xmake 官方 skill `xmake-link-order`):
```lua
add_linkgroups(libs, {group = true}) -- → -Wl,--start-group … -Wl,--end-group
```
链接器在组内反复扫描,顺序就不重要了。副作用是只链 **约 40 个库**
(ament 会链 158 个),`patrol` 产物 657 KB vs 1029 KB。
另外两个细节:header-only 的 ROS 包(如 `rosidl_runtime_cpp`)写成 `-l` 会报
`cannot find`,所以用 `os.isfile()` 先过滤;公共配置函数必须**无参**,
因为 xmake 的 API 只在 `target()` 块内部有效(写成 `ros_common(t)` 会报
`attempt to index a nil value`)。
### 3.3 去 colcon 的完整记账
`CMakeLists.txt`、`package.xml`、`src/sentry_sim/launch/`、`install/`、`log/`、
`tools/gen_compdb.sh` 全部删除。
| 失去的东西 | 现在的替代做法 |
|---|---|
| `ros2 run sentry_nav self_filter` | 直接跑 `build/bin/self_filter` |
| `ros2 run sentry_sim odom_tf.py` | `build/bin/odom_tf` |
| `ros2 pkg prefix sentry_sim` | `SENTRY_SIM_SHARE` 环境变量(默认 `<仓库>/src/sentry_sim`) |
| `xacro` 的 `$(find sentry_sim)` | 改成 `$(env SENTRY_SIM_SHARE)`;不设变量会明确报错 |
| `ros2 launch sentry_sim …`(备用入口) | 只保留 `scripts/sentry_up.sh`(本来就是主入口) |
| 自定义 `.msg` / `.srv` 代码生成 | **没有替代方案 —— 触发回 colcon 的唯一信号** |
**仍然正常工作的**:`ros2 topic` / `node` / `action` / `ros2 launch nav2_bringup`
等所有**系统包**命令。它们来自 `/opt/ros/lyrical` 的 apt 安装,从来与 colcon 无关。
### 3.4 `sentry_sim` 为什么根本没有构建系统
45 个文件全是 `.sdf` / `.stl` / `.yaml` / `.py` / `.sh` / `.md`(146 MB 是网格),
**一行 C/C++ 都没有**。以前用 colcon 只是为了让 `ros2 pkg prefix` 返回一个路径,
现在 `sentry_up.sh` 里一行搞定:
```bash
PKG_SHARE="${SENTRY_SIM_SHARE:-${WS}/src/sentry_sim}"
```
---
## 4. 代码分层
### 4.1 职责边界
| 层 | 放什么 | **不许**放什么 |
|---|---|---|
| `src/sentry_nav/src/core/` | 纯数学、纯状态逻辑、参数校验 | ❌ 任何 `#include <rclcpp/...>`、`<pcl/...>`、`<tf2/...>` |
| `src/sentry_nav/src/` | 节点、话题、参数、TF、动作客户端、日志 | 尽量少放算法(能下沉就下沉) |
**判据**:这段逻辑需要 `#include <rclcpp/...>` 吗?不需要 → 进 `core/`。
### 4.2 抽出来的内容
| 逻辑 | 原来在哪 | 现在在哪 |
|---|---|---|
| 点云四道过滤(NaN / 距离 / 高度 / 车身盒) | `self_filter.cpp` 的 for 循环 | `SelfFilterCore::Classify()`,**额外返回丢弃原因** |
| 航点参数校验(偶数、≥2) | `patrol.cpp` 构造函数 | `WaypointSequencer::Parse()`,错误信息更具体 |
| 索引取模回绕 / 走完判断 / 结果计数 | `patrol.cpp` | `WaypointSequencer`,**成功/中止/取消分开计数** |
这份下沉是有回报的:`patrol` 初版用 `index_` 判断「走完没有」,而它一直在
`(index+1) % total` 里回绕、永远小于 `total`,所以**节点永远不退出**——
Gazebo 里要盯日志很久,单测 5 毫秒就报出来。
### 4.3 一份源码,一个构建系统
```
src/sentry_nav/
├── src/core/*.cpp ──┬─→ sentry_nav_core (静态库) ─┬─→ self_filter
│ │ └─→ patrol
│ └─→ test_self_filter_core / test_waypoint_sequencer
└── src/*.cpp ─────────────────────────────→ 两个节点
```
没有复制粘贴,不存在「两套构建系统各编一遍」。`src/core/*.cpp` 是通配,
加新实现文件不用改 `xmake.lua`。
---
## 5. 日常工作流
| 改了什么 | 命令 |
|---|---|
| `src/core/` 纯算法 | `xmake test`(5 ms,不用 source ROS)→ `xmake` |
| `src/` ROS 胶水 | `xmake`(增量 0.5 秒) |
| URDF | `./tools/gen_sdf.sh` → 重启仿真 |
| 模型 / 世界 / Nav2 参数 / 脚本 | 重启仿真(数据包不用编译) |
| 完整联调 | `xmake && HEADLESS=0 scripts/sentry_up.sh` |
---
## 6. 加新算法(比如决策状态机)
1. `include/sentry_nav/` 加头文件
2. `src/core/` 加实现(**别 include ROS 头**)
3. `tests/` 加测试 → `xmake test`
4. **不用改任何构建文件**(`src/core/*.cpp` 是通配)
—— 只有加**新节点**才要照抄一段 `target("xxx") … end`
5. 在 ROS 节点里调用它(只写话题/参数/生命周期这些胶水)
**先在 1~3 步把逻辑跑对,再接 ROS** —— 调试不用等 Gazebo。
---
## 7. xmake 特有的坑
| 现象 | 原因 | 处理 |
|---|---|---|
| `DSO missing from command line` | 新加的库没进 linkgroup | 库名加到 `xmake.lua` 的 `libs` 列表 |
| `cannot find -lxxx` | 那个包是 header-only | 用 `os.isfile()` 过滤,别直接 `add_links` |
| `xmake test` 报 `invalid task: run_tests` | 老写法 | 用 **`xmake test`** |
| `xmake test xxx` 显示 `nothing to test` | 用例名是 `<target>/<testname>` | **`xmake test test_xxx/default`** |
| `attempt to index a nil value (local 't')` | 公共配置函数带了参数,或想在 `target()` 块**外面**拿目标实例 | xmake 的 API **只在 `target()` 块内部有效**。`target(name)` 也**不返回实例**(返回 nil),所以写不出 `set_targetdir` 的包装函数 |
| VS Code 报 `xxx.hpp file not found` | 根 `compile_commands.json` 过期 | 跑一次 `xmake`,再重启 clangd |
### 7.1 产物目录固定成 `build/bin/`
**结论**:每个 target 里写一行 `set_targetdir("$(builddir)/bin")`,**不用 xmake 默认的
`build/<平台>/<架构>/<模式>/`**。
**为什么**:那串平台三元组(`build/linux/x86_64/release/…`)会被抄进文档、脚本、
报错提示里。换架构 / 换 OS / 换 release↔debug,**全仓 29 处引用一起失效** ——
本工程已经在这上面翻过一次车。固定成 `build/bin/` 后,文档里可以永远写
`build/bin/nav_client`。
**代价**:release 和 debug 落同一层、互相覆盖。要并行保留就用 `xmake -o <目录>`。
**⚠️ 新增 target 时必须自己补那一行**,否则它会掉回默认目录、和其它的分家。
`tools/project_env.py --bin-dir` 和 `scripts/env_common.sh` 的 `env_bin_dir()`
都按「找某个已知产物的实际位置」定位,所以**新老两套布局都能兼容**。
更多见 `排错速查.md` §E。