Split off a library

This commit is contained in:
Philipp Oppermann
2019-04-22 12:06:16 +02:00
parent 3b13211579
commit 48e1f72d23
2 changed files with 58 additions and 33 deletions

55
src/lib.rs Normal file
View File

@@ -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)
}

View File

@@ -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)
}