【RustPython】 RustPython Cargo.toml 配置文件详解
[package] 包基本信息
[package]
name = "rustpython"
description = "A python interpreter written in rust."
include = ["LICENSE", "Cargo.toml", "src/**/*.rs"]
version.workspace = true
authors.workspace = true
edition.workspace = true
rust-version.workspace = true
repository.workspace = true
license.workspace = true
解释:
- name: 包名为
rustpython
- description: 项目描述 - “用 Rust 编写的 Python 解释器”
- include: 指定发布包时包含的文件(仅包含许可证、配置文件和源代码)
.workspace = true
: 所有元数据都从工作空间统一继承,确保一致性
[features] 功能特性配置
[features]
default = ["threading", "stdlib", "stdio", "importlib"]
importlib = ["rustpython-vm/importlib"]
encodings = ["rustpython-vm/encodings"]
stdio = ["rustpython-vm/stdio"]
stdlib = ["rustpython-stdlib", "rustpython-pylib", "encodings"]
flame-it = ["rustpython-vm/flame-it", "flame", "flamescope"]
freeze-stdlib = ["stdlib", "rustpython-vm/freeze-stdlib", "rustpython-pylib?/freeze-stdlib"]
jit = ["rustpython-vm/jit"]
threading = ["rustpython-vm/threading", "rustpython-stdlib/threading"]
sqlite = ["rustpython-stdlib/sqlite"]
ssl = ["rustpython-stdlib/ssl"]
ssl-vendor = ["ssl", "rustpython-stdlib/ssl-vendor"]
tkinter = ["rustpython-stdlib/tkinter"]
核心特性说明:
- default: 默认包含线程支持、标准库、标准IO和导入库功能
- stdlib: 启用完整的 Python 标准库支持
- jit: 启用实验性的即时编译功能
- ssl: 启用 HTTPS/SSL 支持
- freeze-stdlib: 将标准库编译进二进制文件(用于 WASM 部署)
[dependencies] 依赖关系
核心内部依赖
rustpython-compiler = { workspace = true }
rustpython-pylib = { workspace = true, optional = true }
rustpython-stdlib = { workspace = true, optional = true, features = ["compiler"] }
rustpython-vm = { workspace = true, features = ["compiler"] }
ruff_python_parser = { workspace = true }
模块化架构:
- rustpython-vm: 虚拟机核心(必需)
- rustpython-compiler: 编译器模块
- rustpython-stdlib: 标准库实现(可选)
- rustpython-pylib: Python 库支持(可选)
- ruff_python_parser: 使用 Ruff 的高性能解析器
条件依赖
[target.'cfg(windows)'.dependencies]
libc = { workspace = true }[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
rustyline = { workspace = true }
- Windows: 需要
libc
库 - 非WASM平台: 包含
rustyline
用于交互式命令行编辑
[workspace] 工作空间配置
[workspace]
resolver = "2"
members = ["compiler","compiler/core", "compiler/codegen","compiler/literal",".","common","derive","jit","vm","vm/sre_engine","pylib","stdlib","derive-impl","wtf8","wasm/lib",
]
项目模块结构:
- compiler/: 编译器相关模块(核心、代码生成、字面量处理)
- vm/: 虚拟机核心(包含正则表达式引擎)
- stdlib/: 标准库实现
- pylib/: Python 库支持
- jit/: 即时编译功能
性能优化配置
[profile.dev.package."*"]
opt-level = 3[profile.test]
opt-level = 3[profile.bench]
lto = "thin"
codegen-units = 1
opt-level = 3[profile.release]
lto = "thin"
优化策略:
- 开发模式: 对所有依赖包启用 O3 优化级别
- 测试模式: 启用优化以保证测试性能
- 发布模式: 使用 ThinLTO 链接时优化
- 基准测试: 最大程度优化(单代码生成单元 + ThinLTO)
特殊配置
包发布元数据
[package.metadata.packager]
product-name = "RustPython"
identifier = "com.rustpython.rustpython"
description = "An open source Python 3 interpreter written in Rust"
Windows 特定配置
[package.metadata.vcpkg]
git = "https://github.com/microsoft/vcpkg"
rev = "2024.02.14"
使用 vcpkg 管理 Windows 上的原生依赖(如 OpenSSL)
代码质量保证
[workspace.lints.rust]
unsafe_code = "allow"
unsafe_op_in_unsafe_fn = "deny"
elided_lifetimes_in_paths = "warn"[workspace.lints.clippy]
perf = "warn"
style = "warn"
complexity = "warn"
suspicious = "warn"
correctness = "warn"
代码检查策略:
- 允许 unsafe: 解释器底层需要 unsafe 代码
- 严格检查: 对性能、代码风格、复杂性进行全面警告
- 安全性: 禁止在 unsafe 函数中不安全操作
总结
这个配置文件展示了 RustPython 的现代化 Rust 项目架构:
- 模块化设计: 清晰的内部模块划分
- 灵活的特性系统: 按需启用功能
- 跨平台支持: 针对不同平台的优化配置
- 性能优先: 全方位的编译优化设置
- 代码质量: 严格的静态检查规则