mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 14:27:49 +00:00
Remove all the extern crate definitions
This commit is contained in:
@@ -332,12 +332,6 @@ Instead of writing our own bootloader, which is a project on its own, we use the
|
|||||||
bootloader = "0.3.4"
|
bootloader = "0.3.4"
|
||||||
```
|
```
|
||||||
|
|
||||||
```rust
|
|
||||||
// in main.rs
|
|
||||||
|
|
||||||
extern crate bootloader;
|
|
||||||
```
|
|
||||||
|
|
||||||
Adding the bootloader as dependency is not enough to actually create a bootable disk image. The problem is that we need to combine the bootloader with the kernel after it has been compiled, but cargo has no support for additional build steps after successful compilation (see [this issue][post-build script] for more information).
|
Adding the bootloader as dependency is not enough to actually create a bootable disk image. The problem is that we need to combine the bootloader with the kernel after it has been compiled, but cargo has no support for additional build steps after successful compilation (see [this issue][post-build script] for more information).
|
||||||
|
|
||||||
[post-build script]: https://github.com/rust-lang/cargo/issues/545
|
[post-build script]: https://github.com/rust-lang/cargo/issues/545
|
||||||
|
|||||||
@@ -285,14 +285,6 @@ The `0.2.3` is the [semantic] version number. For more information, see the [Spe
|
|||||||
[semantic]: http://semver.org/
|
[semantic]: http://semver.org/
|
||||||
[Specifying Dependencies]: http://doc.crates.io/specifying-dependencies.html
|
[Specifying Dependencies]: http://doc.crates.io/specifying-dependencies.html
|
||||||
|
|
||||||
Now we've declared that our project depends on the `volatile` crate and are able to import it in `src/main.rs`:
|
|
||||||
|
|
||||||
```rust
|
|
||||||
// in src/main.rs
|
|
||||||
|
|
||||||
extern crate volatile;
|
|
||||||
```
|
|
||||||
|
|
||||||
Let's use it to make writes to the VGA buffer volatile. We update our `Buffer` type as follows:
|
Let's use it to make writes to the VGA buffer volatile. We update our `Buffer` type as follows:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
@@ -475,12 +467,6 @@ The one-time initialization of statics with non-const functions is a common prob
|
|||||||
|
|
||||||
Let's add the `lazy_static` crate to our project:
|
Let's add the `lazy_static` crate to our project:
|
||||||
|
|
||||||
```rust
|
|
||||||
// in src/main.rs
|
|
||||||
|
|
||||||
extern crate lazy_static;
|
|
||||||
```
|
|
||||||
|
|
||||||
```toml
|
```toml
|
||||||
# in Cargo.toml
|
# in Cargo.toml
|
||||||
|
|
||||||
@@ -532,11 +518,6 @@ To use a spinning mutex, we can add the [spin crate] as a dependency:
|
|||||||
spin = "0.4.9"
|
spin = "0.4.9"
|
||||||
```
|
```
|
||||||
|
|
||||||
```rust
|
|
||||||
// in src/main.rs
|
|
||||||
extern crate spin;
|
|
||||||
```
|
|
||||||
|
|
||||||
Then we can use the spinning Mutex to add safe [interior mutability] to our static `WRITER`:
|
Then we can use the spinning Mutex to add safe [interior mutability] to our static `WRITER`:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
|
|||||||
@@ -244,18 +244,10 @@ To use that crate, we add the following to our `Cargo.toml`:
|
|||||||
array-init = "0.0.3"
|
array-init = "0.0.3"
|
||||||
```
|
```
|
||||||
|
|
||||||
Note that we're using the [`dev-dependencies`] table instead of the `dependencies` table, because we only need the crate for `cargo test` and not for a normal build. Consequently, we also add a `#[cfg(test)]` attribute to the `extern crate` declaration in `main.rs`:
|
Note that we're using the [`dev-dependencies`] table instead of the `dependencies` table, because we only need the crate for `cargo test` and not for a normal build.
|
||||||
|
|
||||||
[`dev-dependencies`]: https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#development-dependencies
|
[`dev-dependencies`]: https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#development-dependencies
|
||||||
|
|
||||||
|
|
||||||
```rust
|
|
||||||
// in main.rs
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
extern crate array_init;
|
|
||||||
```
|
|
||||||
|
|
||||||
Now we can fix our `construct_buffer` function:
|
Now we can fix our `construct_buffer` function:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
|
|||||||
@@ -66,12 +66,6 @@ We will use the [`uart_16550`] crate to initialize the UART and send data over t
|
|||||||
uart_16550 = "0.1.0"
|
uart_16550 = "0.1.0"
|
||||||
```
|
```
|
||||||
|
|
||||||
```rust
|
|
||||||
// in src/main.rs
|
|
||||||
|
|
||||||
extern crate uart_16550;
|
|
||||||
```
|
|
||||||
|
|
||||||
The `uart_16550` crate contains a `SerialPort` struct that represents the UART registers, but we still need to construct an instance of it ourselves. For that we create a new `serial` module with the following content:
|
The `uart_16550` crate contains a `SerialPort` struct that represents the UART registers, but we still need to construct an instance of it ourselves. For that we create a new `serial` module with the following content:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
@@ -210,8 +204,6 @@ x86_64 = "0.2.8"
|
|||||||
```rust
|
```rust
|
||||||
// in src/main.rs
|
// in src/main.rs
|
||||||
|
|
||||||
extern crate x86_64;
|
|
||||||
|
|
||||||
pub unsafe fn exit_qemu() {
|
pub unsafe fn exit_qemu() {
|
||||||
use x86_64::instructions::port::Port;
|
use x86_64::instructions::port::Port;
|
||||||
|
|
||||||
@@ -376,16 +368,6 @@ Cargo supports hybrid projects that are both a library and a binary. We only nee
|
|||||||
|
|
||||||
#![cfg_attr(not(test), no_std)] // don't link the Rust standard library
|
#![cfg_attr(not(test), no_std)] // don't link the Rust standard library
|
||||||
|
|
||||||
extern crate bootloader;
|
|
||||||
extern crate spin;
|
|
||||||
extern crate volatile;
|
|
||||||
extern crate lazy_static;
|
|
||||||
extern crate uart_16550;
|
|
||||||
extern crate x86_64;
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
extern crate array_init;
|
|
||||||
|
|
||||||
// NEW: We need to add `pub` here to make them accessible from the outside
|
// NEW: We need to add `pub` here to make them accessible from the outside
|
||||||
pub mod vga_buffer;
|
pub mod vga_buffer;
|
||||||
pub mod serial;
|
pub mod serial;
|
||||||
@@ -405,9 +387,6 @@ pub unsafe fn exit_qemu() {
|
|||||||
#![cfg_attr(not(test), no_main)] // disable all Rust-level entry points
|
#![cfg_attr(not(test), no_main)] // disable all Rust-level entry points
|
||||||
#![cfg_attr(test, allow(dead_code, unused_macros, unused_imports))]
|
#![cfg_attr(test, allow(dead_code, unused_macros, unused_imports))]
|
||||||
|
|
||||||
// NEW: Add the library as dependency (same crate name as executable)
|
|
||||||
extern crate blog_os;
|
|
||||||
|
|
||||||
use core::panic::PanicInfo;
|
use core::panic::PanicInfo;
|
||||||
use blog_os::println;
|
use blog_os::println;
|
||||||
|
|
||||||
@@ -430,7 +409,7 @@ fn panic(info: &PanicInfo) -> ! {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
So we move everything except `_start` and `panic` to `lib.rs`, make the `vga_buffer` and `serial` modules public, and add an `extern crate` definition to our `main.rs`. Everything should work exactly as before, including `bootimage run` and `cargo test`.
|
So we move everything except `_start` and `panic` to `lib.rs` and make the `vga_buffer` and `serial` modules public. Everything should work exactly as before, including `bootimage run` and `cargo test`.
|
||||||
|
|
||||||
### Test Basic Boot
|
### Test Basic Boot
|
||||||
|
|
||||||
@@ -443,9 +422,6 @@ We are finally able to create our first integration test executable. We start si
|
|||||||
#![cfg_attr(not(test), no_main)] // disable all Rust-level entry points
|
#![cfg_attr(not(test), no_main)] // disable all Rust-level entry points
|
||||||
#![cfg_attr(test, allow(dead_code, unused_macros, unused_imports))]
|
#![cfg_attr(test, allow(dead_code, unused_macros, unused_imports))]
|
||||||
|
|
||||||
// add the library as dependency (same crate name as executable)
|
|
||||||
extern crate blog_os;
|
|
||||||
|
|
||||||
use core::panic::PanicInfo;
|
use core::panic::PanicInfo;
|
||||||
use blog_os::{exit_qemu, serial_println};
|
use blog_os::{exit_qemu, serial_println};
|
||||||
|
|
||||||
@@ -507,8 +483,6 @@ To test that our panic handler is really invoked on a panic, we create a `test-p
|
|||||||
#![cfg_attr(not(test), no_main)]
|
#![cfg_attr(not(test), no_main)]
|
||||||
#![cfg_attr(test, allow(dead_code, unused_macros, unused_imports))]
|
#![cfg_attr(test, allow(dead_code, unused_macros, unused_imports))]
|
||||||
|
|
||||||
extern crate blog_os;
|
|
||||||
|
|
||||||
use core::panic::PanicInfo;
|
use core::panic::PanicInfo;
|
||||||
use blog_os::{exit_qemu, serial_println};
|
use blog_os::{exit_qemu, serial_println};
|
||||||
|
|
||||||
|
|||||||
@@ -85,12 +85,6 @@ To add the crate as dependency, we add the following to our project:
|
|||||||
pic8259_simple = "0.1.1"
|
pic8259_simple = "0.1.1"
|
||||||
```
|
```
|
||||||
|
|
||||||
```rust
|
|
||||||
// in src/lib.rs
|
|
||||||
|
|
||||||
extern crate pic8259_simple;
|
|
||||||
```
|
|
||||||
|
|
||||||
The main abstraction provided by the crate is the [`ChainedPics`] struct that represents the primary/secondary PIC layout we saw above. It is designed to be used in the following way:
|
The main abstraction provided by the crate is the [`ChainedPics`] struct that represents the primary/secondary PIC layout we saw above. It is designed to be used in the following way:
|
||||||
|
|
||||||
[`ChainedPics`]: https://docs.rs/pic8259_simple/0.1.1/pic8259_simple/struct.ChainedPics.html
|
[`ChainedPics`]: https://docs.rs/pic8259_simple/0.1.1/pic8259_simple/struct.ChainedPics.html
|
||||||
@@ -543,12 +537,6 @@ Translating the other keys works in the same way. Fortunately there is a crate n
|
|||||||
pc-keyboard = "0.3.1"
|
pc-keyboard = "0.3.1"
|
||||||
```
|
```
|
||||||
|
|
||||||
```rust
|
|
||||||
// in src/lib.rs
|
|
||||||
|
|
||||||
extern crate pc_keyboard;
|
|
||||||
```
|
|
||||||
|
|
||||||
Now we can use this crate to rewrite our `keyboard_interrupt_handler`:
|
Now we can use this crate to rewrite our `keyboard_interrupt_handler`:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
|
|||||||
Reference in New Issue
Block a user