asterinas/docs/src/osdk/guide/create-project.md

120 lines
2.8 KiB
Markdown
Raw Normal View History

# Creating an OS Project
2024-02-27 09:30:26 +00:00
2024-03-01 03:23:47 +00:00
OSDK can be used to create a new kernel project
or a new library project.
A kernel project defines the entry point of the kernel
and can be run with QEMU.
A library project can provide certain OS functionalities
and be imported by other OSes.
2024-02-27 09:30:26 +00:00
## Creating a new kernel project
2024-03-01 03:23:47 +00:00
Creating a new kernel project is simple.
You only need to execute the following command:
2024-02-27 09:30:26 +00:00
```bash
cargo osdk new --kernel myos
2024-02-27 09:30:26 +00:00
```
## Creating a new library project
Creating a new library project requires just one command:
```bash
cargo osdk new mylib
```
## Generated files
2024-03-01 03:23:47 +00:00
Next, we will introduce
the contents of the generated project in detail.
If you don't wish to delve into the details,
you can skip the following sections.
2024-02-27 09:30:26 +00:00
### Overview
2024-03-01 03:23:47 +00:00
The generated directory
for both the kernel project and library project
contains the following contents:
2024-02-27 09:30:26 +00:00
```text
myos/
2024-03-11 07:21:23 +00:00
├── Cargo.toml
├── OSDK.toml
├── rust-toolchain.toml
└── src/
└── lib.rs
2024-02-27 09:30:26 +00:00
```
### `src/lib.rs`
#### Kernel project
2024-03-01 03:23:47 +00:00
The `src/lib.rs` file contains the code for a simple kernel.
2024-06-20 06:16:04 +00:00
The function marked with the `#[ostd::main]` macro
2024-03-01 03:23:47 +00:00
is considered the kernel entry point by OSDK.
The kernel
will print `Hello world from the guest kernel!`to the console
and then abort.
2024-02-27 09:30:26 +00:00
```rust
2024-03-01 03:23:47 +00:00
{{#include ../../../../osdk/src/commands/new/kernel.template}}
2024-02-27 09:30:26 +00:00
```
#### Library project
2024-03-01 03:23:47 +00:00
The `src/lib.rs` of library project only contains
a simple kernel mode unit test.
2024-05-22 06:04:36 +00:00
It follows a similar code pattern as user mode unit tests.
The test module is marked with the `#[cfg(ktest)]` macro,
and each test case is marked with `#[ktest]`.
2024-02-27 09:30:26 +00:00
```rust
2024-03-01 03:23:47 +00:00
{{#include ../../../../osdk/src/commands/new/lib.template}}
2024-02-27 09:30:26 +00:00
```
### `Cargo.toml`
2024-03-01 03:23:47 +00:00
The `Cargo.toml` file is the Rust project manifest.
In addition to the contents of a normal Rust project,
2024-06-19 08:18:39 +00:00
OSDK will add the dependencies of the Asterinas OSTD to the file.
2024-03-01 03:23:47 +00:00
The dependency version may change over time.
2024-02-27 09:30:26 +00:00
```toml
2024-06-19 08:18:39 +00:00
[dependencies.ostd]
2024-02-27 09:30:26 +00:00
git = "https://github.com/asterinas/asterinas"
branch = "main"
[dependencies.ktest]
git = "https://github.com/asterinas/asterinas"
branch = "main"
```
2024-05-22 06:04:36 +00:00
OSDK will also exclude the directory
which is used to generate temporary files.
```toml
[workspace]
exclude = ["target/osdk/base"]
```
2024-02-27 09:30:26 +00:00
### `OSDK.toml`
2024-03-01 03:23:47 +00:00
The `OSDK.toml` file is a manifest
that defines the exact behavior of OSDK.
2024-05-22 06:04:36 +00:00
By default, it includes settings on how to start QEMU to run a kernel.
2024-03-01 03:23:47 +00:00
The meaning of each key can be found
in the [manifest documentation](../reference/manifest.md).
Please avoid changing the default settings
unless you know what you are doing.
2024-02-27 09:30:26 +00:00
2024-05-22 06:04:36 +00:00
The default manifest of a kernel project:
2024-02-27 09:30:26 +00:00
```toml
2024-05-22 06:04:36 +00:00
{{#include ../../../../osdk/src/commands/new/kernel.OSDK.toml.template}}
2024-02-27 09:30:26 +00:00
```
### `rust-toolchain.toml`
2024-03-01 03:23:47 +00:00
The Rust toolchain for the kernel.
2024-06-19 08:18:39 +00:00
It aligns with the toolchain of the Asterinas OSTD.