From 6a6255ec67f335639948db8c62385b136809140d Mon Sep 17 00:00:00 2001 From: natrixaeria Date: Sat, 2 Nov 2019 21:55:24 +0100 Subject: Rewrite build script --- kernel/.cargo/config | 1 - kernel/build.sh | 122 --------------------------------------------------- kernel/run | 96 ++++++++++++++++++++++++++++++++++++++++ kernel/src/lib.rs | 49 +++++++++++++++------ 4 files changed, 131 insertions(+), 137 deletions(-) delete mode 100755 kernel/build.sh create mode 100755 kernel/run diff --git a/kernel/.cargo/config b/kernel/.cargo/config index e698526..a0b86ef 100644 --- a/kernel/.cargo/config +++ b/kernel/.cargo/config @@ -1,3 +1,2 @@ - [build] target = "x86_64-uff.json" diff --git a/kernel/build.sh b/kernel/build.sh deleted file mode 100755 index d8086b6..0000000 --- a/kernel/build.sh +++ /dev/null @@ -1,122 +0,0 @@ -#!/usr/bin/sh - -ASM_PATH="src/" -TARGET_PATH="target" -TMP_PATH="$TARGET_PATH/temp/" -OBJ_PATH="$TMP_PATH" -LINK_SCRIPT_PATH="src/" -GRUB_CFG_PATH="src/grub.cfg" -ARCH="x86_64" -TARGET="$ARCH-uff" -RUST_BIN="$TARGET_PATH/$TARGET/debug/libkernel.a" - -remove_target() { - rm -rf $TMP_PATH/* &> /dev/null - rm -f $TARGET_PATH/uff.iso &> /dev/null -} - -create_target() { - mkdir $TARGET_PATH &> /dev/null - mkdir $TMP_PATH &> /dev/null -} - -cleanup_temp() { - rm -f $TMP_PATH/* &> /dev/null -} - -# --------------------------------------------------- - -build_kernel_elf() { - create_target - for f in $ASM_PATH/*.asm; do - nasm -felf64 "$f" -o $OBJ_PATH/"$(basename -s .asm $f)".o - done - ld -n -o $TARGET_PATH/kernel.bin -gc-sections -T $LINK_SCRIPT_PATH/linker.ld $OBJ_PATH/*.o $RUST_BIN - cleanup_temp -} - -build_kernel_iso() { - if test ! -e $TARGET_PATH/kernel.bin; then - build_kernel_elf - fi - create_target - mkdir $TMP_PATH/isofiles &> /dev/null - mkdir $TMP_PATH/isofiles/boot &> /dev/null - mkdir $TMP_PATH/isofiles/boot/grub &> /dev/null - cp $TARGET_PATH/kernel.bin $TMP_PATH/isofiles/boot &> /dev/null - cp $GRUB_CFG_PATH $TMP_PATH/isofiles/boot/grub/grub.cfg &> /dev/null - grub-mkrescue -d /usr/lib/grub/i386-pc -o $TARGET_PATH/uff.iso $TMP_PATH/isofiles - cleanup_temp -} - -build_init() { - create_target -} - -build_launch() { - qemu-system-x86_64 -cdrom $TARGET_PATH/uff.iso -} - -build_run() { - if test ! -e $TARGET_PATH/uff.iso; then - build_kernel_iso - fi - build_launch -} - -build_clean() { - remove_target -} - -build_build() { - cargo xbuild - rm -rf $TARGET_PATH/sysroot - rm -rf $TARGET_PATH/kernel.bin - rm -rf $TARGET_PATH/uff.iso - build_run -} - -build_all() { - build_kernel_elf -} - -not_exec=true - -for arg in "$@"; do - case "$arg" in - "kernel-elf") build_kernel_elf;; - "kernel-iso") build_kernel_iso;; - "init") build_init;; - "launch") build_launch;; - "run") build_run;; - "clean") build_clean;; - "build") build_build;; - "all") build_all;; - *) - echo "warn: ignoring unknown option '$arg'" - continue;; - esac - not_exec=false - break -done - -print_help() { - echo "usage: $0 option" - echo "options: " - echo " init initialise directory" - echo " clean cleanup binaries" - echo " launch launch iso in qemu" - echo " run creates iso if necessary and runs it in qemu" - echo - echo " kernel-elf build the kernel-elf" - echo " kernel-iso build the kernel-elf if necessary and the kernel-iso" - echo " build build the kernel-iso with rust binaries" - echo " all build kernel" -} - -if $not_exec; then - echo "error: please specify an argument" - echo - print_help - exit -1 -fi diff --git a/kernel/run b/kernel/run new file mode 100755 index 0000000..df258d8 --- /dev/null +++ b/kernel/run @@ -0,0 +1,96 @@ +#!/bin/sh + +onerr() { + echo -e "\x1b[1;31merror: '$action' failed\x1b[m" + exit 1 +} + +trap onerr ERR + +name="uff" +build_mode=debug +action=help +target=x86_64 +target_name="$target-$name" +target_path="target/" +rust_target_path="$target_path/$target_name/$build_mode/" +iso_path="$target_path/iso/" +obj_path="$iso_path/obj/" +src_path="src/" +asm_path="$src_path/" +link_script="$src_path/linker.ld" +grub_cfg="$src_path/grub.cfg" + +print_help() { + echo "usage: $0 (options) [action]" + echo "options:" + echo " -mode= set build mode (standard: debug)" + exit +} + +get_rust_bin() { + case "$action" in + "test") echo "$rust_target_path/$(ls -t1 $rust_target_path | grep -P '^kernel-[a-fA-F0-9]+$' | head -n1)";; + *) echo "$rust_target_path/libkernel.a";; + esac +} + +prepare_iso() { + mkdir "$target_path" &> /dev/null + mkdir "$iso_path" &> /dev/null + mkdir "$obj_path" &> /dev/null + mkdir "$iso_path/isofiles" &> /dev/null + mkdir "$iso_path/isofiles/boot" &> /dev/null + mkdir "$iso_path/isofiles/boot/grub" &> /dev/null + for f in "$asm_path"/*.asm; do + nasm -felf64 "$f" -o "$obj_path/$(basename -s .asm $f).o" + done + cp "$grub_cfg" "$iso_path/isofiles/boot/grub/grub.cfg" +} + +build_iso() { + ld -n -o "$iso_path/isofiles/boot/kernel.bin" -gc-sections -T "$link_script" "$obj_path"/*.o "$(get_rust_bin)" + grub-mkrescue -d /usr/lib/grub/i386-pc -o "$iso_path/uff.iso" "$iso_path/isofiles" +} + +build() { + if test ! -d "$iso_path/isofiles"; then + prepare_iso + fi + cargo xbuild + build_iso +} + +build_test() { + if test ! -d "$iso_path/isofiles"; then + prepare_iso + fi + RUSTFLAGS="-Clink-arg=-r -Clink-dead-code" cargo xtest --no-run + build_iso +} + +run() { + qemu-system-x86_64 -cdrom "$iso_path/uff.iso" +} + +for arg in "$@"; do + case "$arg" in + -mode=*) + build_mode="$(echo $arg | sed "s/^-mode=//")";; + "run") action=run;; + "build") action=build;; + "test") action=test;; + "help") action=help;; + *) + echo "warn: ignoring unknown option '$arg'";; + esac +done + +case "$action" in + "help") print_help;; + "build") build;; + "test") build_test; run;; + "run") build; run;; +esac + +echo -e "\x1b[1;32mdone\x1b[m" diff --git a/kernel/src/lib.rs b/kernel/src/lib.rs index 92ce010..08059ea 100644 --- a/kernel/src/lib.rs +++ b/kernel/src/lib.rs @@ -1,24 +1,52 @@ #![no_main] #![feature(compiler_builtins_lib)] +#![feature(custom_test_frameworks)] +#![test_runner(crate::test_runner)] +#![reexport_test_harness_main = "test_main"] #![no_std] extern crate compiler_builtins; mod vga_text; +use vga_text::OStream; use core::fmt::Write; +#[cfg(test)] +pub fn test_runner(tests: &[&dyn Fn(&mut OStream)]) { + let mut stdout = OStream::new(); + stdout.clear(); + write!(&mut stdout, "running {} tests\n", tests.len()); + for test in tests { + test(&mut stdout) + } +} + +#[test_case] +fn test01(stdout: &mut OStream) { + write!(stdout, "running test01 . . . OK"); +} + #[no_mangle] pub extern "C" fn _start() -> ! { + + if cfg!(test) { + #[cfg(test)] + test_main(); + loop {} + } + let mut stdout = vga_text::OStream::new(); stdout.clear(); write!(&mut stdout, "hello world!"); - panic!("i has panicing"); + // panic!("i has panicing"); core::iter::successors(Some(0), |n| Some(n + 1)) - .for_each(|n| write!(&mut stdout, "hello world {}!", n).unwrap()); + .for_each(|n| + write!(&mut stdout, "hello world {}!", n).unwrap() + ); loop {} } @@ -28,26 +56,19 @@ pub extern "C" fn _start() -> ! { pub extern "C" fn panic_handler(_info: &core::panic::PanicInfo) -> ! { let mut stderr = vga_text::OStream::new(); stderr.set_state(vga_text::CharState::from_colors( - vga_text::Color::LightRed, - vga_text::Color::Red, - )); + vga_text::Color::LightRed, vga_text::Color::Red)); stderr.clear(); stderr.print(b"uff-os"); stderr.set_row(10); stderr.set_state(vga_text::CharState::from_colors( - vga_text::Color::White, - vga_text::Color::Red, - )); + vga_text::Color::White, vga_text::Color::Red)); stderr.print_centered(b""); stderr.set_row(14); - let text = _info - .payload() + let text = _info.payload() .downcast_ref::<&str>() - .unwrap_or(&"no panic information is obtainable"); + .unwrap_or(&"no panic informations are obtainable"); stderr.set_state(vga_text::CharState::from_colors( - vga_text::Color::Cyan, - vga_text::Color::Red, - )); + vga_text::Color::Cyan, vga_text::Color::Red)); stderr.print_centered(text.as_bytes()); loop {} } -- cgit v1.2.3-54-g00ecf