summaryrefslogtreecommitdiff
path: root/kernel/run
blob: df258d86d4b509005408d31a49916214f208f392 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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=<str>          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"