From 48e1f72d2390d7dd9a2556ca15a8f49f4862bd9c Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Mon, 22 Apr 2019 12:06:16 +0200 Subject: [PATCH] Split off a library --- src/lib.rs | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 36 +++-------------------------------- 2 files changed, 58 insertions(+), 33 deletions(-) create mode 100644 src/lib.rs diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 00000000..96db9bcb --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,55 @@ +#![no_std] +#![cfg_attr(test, no_main)] +#![feature(custom_test_frameworks)] +#![test_runner(crate::test_runner)] +#![reexport_test_harness_main = "test_main"] + +use core::panic::PanicInfo; + +pub mod serial; +pub mod vga_buffer; + +pub fn test_runner(tests: &[&dyn Fn()]) { + serial_println!("Running {} tests", tests.len()); + for test in tests { + test(); + } + unsafe { exit_qemu(QemuExitCode::Success) }; +} + +pub fn test_panic_handler(info: &PanicInfo) -> ! { + serial_println!("[failed]\n"); + serial_println!("Error: {}\n", info); + unsafe { + exit_qemu(QemuExitCode::Failed); + } + loop {} +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(u32)] +pub enum QemuExitCode { + Success = 0x10, + Failed = 0x11, +} + +pub unsafe fn exit_qemu(exit_code: QemuExitCode) { + use x86_64::instructions::port::Port; + + let mut port = Port::new(0xf4); + port.write(exit_code as u32); +} + +/// Entry point for `cargo xtest` +#[cfg(test)] +#[no_mangle] +pub extern "C" fn _start() -> ! { + test_main(); + loop {} +} + +#[cfg(test)] +#[panic_handler] +fn panic(info: &PanicInfo) -> ! { + test_panic_handler(info) +} diff --git a/src/main.rs b/src/main.rs index e76ebb8e..6e7f82d2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,12 @@ #![no_std] #![no_main] #![feature(custom_test_frameworks)] -#![test_runner(crate::test_runner)] +#![test_runner(blog_os::test_runner)] #![reexport_test_harness_main = "test_main"] +use blog_os::println; use core::panic::PanicInfo; -mod serial; -mod vga_buffer; - #[no_mangle] pub extern "C" fn _start() -> ! { println!("Hello World{}", "!"); @@ -19,15 +17,6 @@ pub extern "C" fn _start() -> ! { loop {} } -#[cfg(test)] -fn test_runner(tests: &[&dyn Fn()]) { - serial_println!("Running {} tests", tests.len()); - for test in tests { - test(); - } - unsafe { exit_qemu(QemuExitCode::Success) }; -} - /// This function is called on panic. #[cfg(not(test))] #[panic_handler] @@ -39,24 +28,5 @@ fn panic(info: &PanicInfo) -> ! { #[cfg(test)] #[panic_handler] fn panic(info: &PanicInfo) -> ! { - serial_println!("[failed]\n"); - serial_println!("Error: {}\n", info); - unsafe { - exit_qemu(QemuExitCode::Failed); - } - loop {} -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -#[repr(u32)] -pub enum QemuExitCode { - Success = 0x10, - Failed = 0x11, -} - -pub unsafe fn exit_qemu(exit_code: QemuExitCode) { - use x86_64::instructions::port::Port; - - let mut port = Port::new(0xf4); - port.write(exit_code as u32); + blog_os::test_panic_handler(info) }