贡献完整示例
TGOSKits 的顶层 examples/ 目录用于放置可运行的场景示例,而不是通用
workspace 组件模板。系统级或组件级示例仍应优先放在对应子系 统目录中,例如
apps/arceos/、components/*/examples/ 或 test-suit/。
StarryOS 板端示例
1. 获取仓库并创建分支
1.1 Fork 仓库
- 打开 https://github.com/rcore-os/tgoskits
- 点击页面右上角的 Fork 按钮
- 选择你的 GitHub 账户作为 Fork 目标
1.2 克隆到本地
# 将 <YOUR_USERNAME> 替换为你的 GitHub 用户名
git clone https://github.com/<YOUR_USERNAME>/tgoskits.git
cd tgoskits
1.3 添加上游仓库
git remote add upstream https://github.com/rcore-os/tgoskits.git
验证远程仓库配置:
git remote -v
# 预期输出:
# origin https://github.com/<YOUR_USERNAME>/tgoskits.git (fetch)
# origin https://github.com/<YOUR_USERNAME>/tgoskits.git (push)
# upstream https://github.com/rcore-os/tgoskits.git (fetch)
# upstream https://github.com/rcore-os/tgoskits.git (push)
1.4 同步上游最新代码
git fetch upstream
git checkout dev
git merge upstream/dev
1.5 创建功能分支
TGOSKits 使用 main / dev / 功能分支 三层策略(详见 仓库管理):
main:稳定发布分支,禁止直接 pushdev:集成分支,所有开发通过 PR 合入- 功能分支:开发者基于
dev创建的个人开发分支
# 从最新的 dev 创建功能分支
# 分支命名约定:feat/<功能名> 或 fix/<修复名>
git checkout -b feat/add-tgmath-component # 场景 A
git checkout -b feat/tgmath-add-lcm # 场景 B
2. 场景 A:新增组件
2.1 确定改动位置
TGOSKits 的组件按职责分布在不同目录中,正式添加组件应该放到对应的目录下。但是,作为演示示例,我们将示例添加的组件放在 examples/ 目录中!
| 你的目标 | 修改位置 |
|---|---|
| 通用基础能力(错误、锁、容器等) | components/ |
| ArceOS 内核模块 | os/arceos/modules/ |
| ArceOS API / 用户库 | os/arceos/api/ 或 os/arceos/ulib/ |
| StarryOS 内核 | os/StarryOS/kernel/ |
| Axvisor 运行时 | os/axvisor/src/ |
| 平台适配 | platforms/ |
为了同时演示新增和修改组件,examples/tgmath 本身已经存在了。对于新增组件的演示,请先删除 examples/tgmath 后执行后续步骤!
2.2 创建组件目录
根据 组件开发指南 第 5.2 节定义的标准目录结构,一个完整的组件应包含以下文件:
apps/starry/<case>/
如果组件仅作为 TGOSKits 内部组件(非独立仓库),不需要立即添加 .github/、scripts/ 等 CI 文件。下文所有路径均以 examples/tgmath/ 为例。
# 本演示放在 examples/ 下;正式贡献请替换为对应目录(如 components/)
mkdir -p examples/tgmath/src
mkdir -p examples/tgmath/tests
mkdir -p examples/tgmath/scripts
mkdir -p examples/tgmath/.github/workflows
2.3 编写 Cargo.toml
创建 examples/tgmath/Cargo.toml:
[package]
name = "tgmath"
version = "0.1.0"
edition = "2024"
authors = ["TGOSKits Contributor <example@example.com>"]
description = "A tiny math utility crate for TGOSKits demo."
license = "GPL-3.0-or-later OR Apache-2.0 OR MulanPSL-2.0"
[dependencies]
2.4 编写库代码
创建 examples/tgmath/src/lib.rs:
#![no_std]
/// Add two numbers.
pub fn add(a: i64, b: i64) -> i64 {
a + b
}
/// Subtract `b` from `a`.
pub fn sub(a: i64, b: i64) -> i64 {
a - b
}
/// Clamp a value within a range `[lo, hi]`.
pub fn clamp(val: i64, lo: i64, hi: i64) -> i64 {
if val < lo {
lo
} else if val > hi {
hi
} else {
val
}
}
/// Compute the greatest common divisor.
pub fn gcd(a: u64, b: u64) -> u64 {
let mut a = a;
let mut b = b;
while b != 0 {
let t = b;
b = a % b;
a = t;
}
a
}
/// Compute the least common multiple.
pub fn lcm(a: u64, b: u64) -> u64 {
if a == 0 || b == 0 {
0
} else {
a / gcd(a, b) * b
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_add() {
assert_eq!(add(2, 3), 5);
assert_eq!(add(-1, 1), 0);
}
#[test]
fn test_sub() {
assert_eq!(sub(5, 3), 2);
}
#[test]
fn test_clamp() {
assert_eq!(clamp(5, 0, 10), 5);
assert_eq!(clamp(-1, 0, 10), 0);
assert_eq!(clamp(15, 0, 10), 10);
}
#[test]
fn test_gcd() {
assert_eq!(gcd(12, 8), 4);
assert_eq!(gcd(7, 0), 7);
}
#[test]
fn test_lcm() {
assert_eq!(lcm(4, 6), 12);
assert_eq!(lcm(0, 5), 0);
assert_eq!(lcm(7, 0), 0);
assert_eq!(lcm(3, 7), 21);
}
}
2.5 编写集成测试
创建 examples/tgmath/tests/integration.rs:
use tgmath::{add, clamp, gcd, lcm, sub};
#[test]
fn integration_add_sub() {
assert_eq!(add(100, 200), 300);
assert_eq!(sub(300, 200), 100);
}
#[test]
fn integration_clamp_boundary() {
assert_eq!(clamp(0, 0, 100), 0);
assert_eq!(clamp(100, 0, 100), 100);
}
#[test]
fn integration_gcd_coprime() {
assert_eq!(gcd(13, 7), 1);
}
#[test]
fn integration_lcm() {
assert_eq!(lcm(12, 8), 24);
assert_eq!(lcm(3, 7), 21);
}
2.6 注册到 Workspace
新增 crate 后,必须手动将其添加到根 Cargo.toml 的 [workspace] members 列表中。TGOSKits 的 workspace members 是显式枚举的,不会通过 glob 模式自动包含。因此,编辑根 Cargo.toml,在 members 数组末尾添加新行:
[workspace]
members = [
# ... 已有成员 ...
# 新增组件(本演示放在 examples/ 下)
"examples/tgmath", # ← 添加这一行
# 正式组件则使用对应路径,如:
# "components/tgmath",
]
添加后可以验证 workspace 是否识别到新 crate:
cargo test -p tgmath
2.7 Subtree 组件(可选)
如果新组件有独立仓库,则需要使用 scripts/repo/repo.py 工具,将独立仓库的组件显示添加到当前主仓库中。
python3 scripts/repo/repo.py add \
--url https://github.com/<org>/tgmath \
--target examples/tgmath \
--branch dev \
--category ArceOS
本例中
tgmath仅作为演示,不需要注册 subtree。
3. 场景 B:修改已有 demo
本场景演示修改 examples/tgmath/——为其添加一个 lcm(最小公倍数)函数。