Files
2026-11-03 03:32:07 +00:00

75 lines
3.9 KiB
Markdown

# 编译器开发
编译路线为 Dever → 具体类型 Rust → Cargo/rustc → 原生程序。语法、检查、代码生成与运行库各自负责一层。
## 目录
| 路径 | 内容 |
| --- | --- |
| `src/syntax/` | 源码位置、词法、语法和诊断 |
| `src/compiler/project/` | 项目、配置与依赖加载 |
| `src/compiler/check/`、`model/` | 类型检查和已检查的程序 |
| `src/compiler/emit/`、`build/` | Rust 生成与原生构建 |
| `src/compiler/inspect/` | 符号、说明、调用关系和流程图 |
| `src/runtime/` | 运行库,复用 Rust 标准库及已有依赖 |
| `src/process/` | 子进程启动、等待与取消 |
| `src/cli/` | 命令行入口 |
| `../lib/`、`../sdk/` | Dever 标准库与外部 Worker SDK |
| `../test/rust/` | 检查器、原生执行、运行库和性能测试 |
只有本目录及 `src/runtime/` 两份 Cargo 清单。CLI 不单独拆包,生成程序只依赖运行库。
## 构建
需要 Rust 1.95.0 和 C 编译工具,版本见 `rust-toolchain.toml`。在本目录执行;平台开发使用已有 dev 容器。
```sh
cargo +1.95.0 fetch --locked
cargo +1.95.0 build --locked --offline --features postgres --bin dever
compiler="${CARGO_TARGET_DIR:-target}/debug/dever"
"$compiler" new ../target/hello-example
"$compiler" check ../target/hello-example
"$compiler" test ../target/hello-example
"$compiler" run ../target/hello-example -- hello.greeting.greet '{"name":"Dever"}'
```
`CARGO_TARGET_DIR` 可指定构建缓存。已有依赖缓存时省略 fetch;锁文件不随日常构建更新。项目和显式输出文件须使用新路径。
## 检查与测试
```sh
cargo +1.95.0 fmt --all --check
cargo +1.95.0 clippy --locked --offline --workspace --all-targets --all-features --no-deps -- -D warnings
cargo +1.95.0 test --locked --offline --test syntax --test compiler
cargo +1.95.0 test --locked --offline --test native inspection::
cargo +1.95.0 test --locked --offline --test native toolchain::
```
按改动选择现有测试:运行库测试位于 `../test/rust/runtime/`,应用原生测试位于 `../test/rust/native/`。数据库测试使用独立配置,不能连接业务数据库。测试通过数以真实执行结果为准,ignored 不计为通过。
语法和运行行为以 [语言规范](../LANGUAGE.md)及相关测试为准;实现约束见 [AGENTS](../AGENTS.md)。性能测试见 [测试方法](../test/performance/README.md)。
## 打包
从同一份源码构建发行编译器,再在仓库根组装:
```sh
cargo +1.95.0 build --locked --offline --release --features postgres --bin dever
compiler="$(realpath "${CARGO_TARGET_DIR:-target}/release/dever")"
cd ..
python3 build/online.py --compiler "$compiler" --work /tmp/dever-online --output target/releases/0.0.1/online
python3 build/development.py --compiler "$compiler" --work /tmp/dever-offline --output target/releases/0.0.1/dever-0.0.1-linux-x86_64-development.tar.xz
```
组装环境为 Debian 13,工作目录和输出须不存在。在线包与离线包共用 `build/development.py` 的输入准备;底层 `dever release` 只组装已提供的编译器、源码和清单。
脚本保留许可证、锁定依赖及链接所需文件,输出 SHA-256。归档不带构建缓存、用户配置或凭据;只裁剪复制进包的工具,不修改系统安装。
发布前在独立目录验证创建、检查、测试、构建和运行。模板、标准库说明嵌入编译器,修改后须重建编译器和安装包。打包不自动上传、安装或切换开发环境。
## 目标平台
`dever build --target` 支持 `linux-x86_64` 和 `linux-aarch64`。交叉构建须预备目标标准库、链接器和原生依赖;`run/test` 在本机执行。当前安装包只提供已验证的 Linux x86_64 环境。
发行构建使用 release、fat LTO、单代码生成单元、溢出检查和符号裁剪。业务程序仍保留 Dever 源码位置诊断。