-
Notifications
You must be signed in to change notification settings - Fork 6
/
make
executable file
·178 lines (136 loc) · 7.41 KB
/
make
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/bin/bash
#=================================================================================
# Copyright (C) Andrzej Adamczyk (at https://blackdev.org/). All rights reserved.
#=================================================================================
# coloristics
green=$(tput setaf 2)
default=$(tput sgr0)
# default optimization
OPT="${1}"
if [ -z "${OPT}" ]; then OPT="fast"; fi
# we use clang, as no cross-compiler needed, include std.h header as default for all
CC="clang"
C="${CC} -include ./library/std.h -g"
LD="ld.lld"
ASM="nasm"
ISO="xorriso"
V="qemu-system-x86_64"
ARCH="x86-64"
FLAGS="-march=${ARCH} -mtune=generic -O${OPT} -m64 -ffreestanding -nostdlib -fno-stack-protector -fno-builtin -mno-red-zone ${DEBUG}"
# for clear view
clear
# check environment software, required!
ENV=true
echo -n "Foton environment: "
type -a ${CC} &> /dev/null || (echo -en "\e[38;5;196m${C}\e[0m" && ENV=false)
type -a ${LD} &> /dev/null || (echo -en "\e[38;5;196m${LD}\e[0m" && ENV=false)
type -a ${ASM} &> /dev/null || (echo -en "\e[38;5;196m${ASM}\e[0m" && ENV=false)
type -a ${ISO} &> /dev/null || (echo -en "\e[38;5;196m${ISO}\e[0m" && ENV=false)
if [ "${ENV}" = false ]; then echo -e "\n[please install missing software]"; exit 1; else echo -e "\e[38;5;2m\xE2\x9C\x94\e[0m"; fi
# optional
type -a qemu-system-x86_64 &> /dev/null || echo -e "Optional \e[38;5;11mqemu\e[0m not installed. ISO file will be generated regardless of that."
# external resources initialization
git submodule update --init
# remove all obsolete files, which could interference
rm -rf build && mkdir -p build/iso
rm -f bx_enh_dbg.ini # just to make clean directory, if you executed bochs.sh
# git deosn't allow empty folder
mkdir -p root/system/{bin,lib}
# copy default filesystem structure
cp -rf root build
# build subroutines required by kernel
EXT=""
${ASM} -f elf64 kernel/init/gdt.asm -o build/gdt.o || exit 1; EXT="${EXT} build/gdt.o"
${ASM} -f elf64 kernel/idt.asm -o build/idt.o || exit 1; EXT="${EXT} build/idt.o"
${ASM} -f elf64 kernel/task.asm -o build/task.o || exit 1; EXT="${EXT} build/task.o"
${ASM} -f elf64 kernel/driver/rtc.asm -o build/rtc.o || exit 1; EXT="${EXT} build/rtc.o"
${ASM} -f elf64 kernel/syscall.asm -o build/syscall.o || exit 1; EXT="${EXT} build/syscall.o"
# default configuration of clang for kernel making
if [ ! -z "${2}" ]; then DEBUG="-DDEBUG"; fi
CFLAGS="${FLAGS} -mno-mmx -mno-sse -mno-sse2 -mno-3dnow ${DEBUG}"
CFLAGS_SOFTWARE="${FLAGS} ${DEBUG}"
LDFLAGS="-nostdlib -static -no-dynamic-linker"
# build kernel file
${C} -c kernel/init.c -o build/kernel.o ${CFLAGS} || exit 1;
${LD} ${EXT} build/kernel.o -o build/kernel -T tools/kernel.ld ${LDFLAGS} || exit 1;
strip -s build/kernel
# information
kernel_size=`ls -lh build/kernel | cut -d ' ' -f 5`
echo -e "${green}\xE2\x9C\x94${default}|Kernel|${kernel_size}" | awk -F "|" '{printf "%s %-30s %s\n", $1, $2, $3 }'
# copy kernel file and limine files onto destined iso folder
gzip -k build/kernel
#===============================================================================
for submodules in `(cd module && ls *.asm)`; do
# module name
submodule=${submodules%.*}
# build
${ASM} -f elf64 module/${submodule}.asm -o build/${submodule}.ao
# information
submodule_size=`ls -lh build/${submodule}.ao | cut -d ' ' -f 5`
echo -e "${green}\xE2\x9C\x94${default}|[submodule of ${submodule}.ko]|${submodule_size}" | awk -F "|" '{printf "%s %-30s %s\n", $1, $2, $3 }'
done
for modules in `(cd module && ls *.c)`; do
# module name
module=${modules%.*}
# build
${C} -c -fpic -DMODULE module/${module}.c -o build/${module}.o ${CFLAGS} || exit 1
# connect with libraries (if necessery)
SUB=""
if [ -f build/${module}.ao ]; then SUB="build/${module}.ao"; fi
${LD} ${SUB} build/${module}.o -o build/root/system/lib/modules/${module}.ko -T tools/module.ld ${LDFLAGS}
# we do not need any additional information
strip -s build/root/system/lib/modules/${module}.ko
# information
module_size=`ls -lh build/root/system/lib/modules/${module}.ko | cut -d ' ' -f 5`
echo -e "${green}\xE2\x9C\x94${default}|Module: ${module}.ko|${module_size}" | awk -F "|" '{printf "%s %-30s %s\n", $1, $2, $3 }'
done
#===============================================================================
lib="" # include list of libraries
# keep parsing libraries by. dependencies and alphabetically
for library in path color elf integer string network input math json font std image interface random rgl terminal; do
# build
${C} -c -fpic library/${library}.c -o build/${library}.o ${CFLAGS_SOFTWARE} || exit 1
# convert to shared
${C} -shared build/${library}.o -o build/root/system/lib/lib${library}.so ${CFLAGS_SOFTWARE} -Wl,--as-needed,-T./tools/library.ld -L./build/root/system/lib/ ${lib} || exit 1
# we do not need any additional information
strip -s build/root/system/lib/lib${library}.so
# update libraries list
lib="${lib} -l${library}"
# information
library_size=`ls -lh build/root/system/lib/lib${library}.so | cut -d ' ' -f 5`
echo -e "${green}\xE2\x9C\x94${default}|Library: lib${library}.so|${library_size}" | awk -F "|" '{printf "%s %-30s %s\n", $1, $2, $3 }'
done
#===============================================================================
for software in `(cd software && ls *.c)`; do
# program name
name=${software::$(expr ${#software} - 2)}
# build
${C} -DSOFTWARE -c software/${name}.c -o build/${name}.o ${CFLAGS_SOFTWARE} || exit 1
# connect with libraries (if necessery)
${LD} --as-needed -L./build/root/system/lib build/${name}.o -o build/root/system/bin/${name} ${lib} -T tools/software.ld ${LDFLAGS}
# we do not need any additional information
strip -s build/root/system/bin/${name} > /dev/null 2>&1
# information
software_size=`ls -lh build/root/system/bin/${name} | cut -d ' ' -f 5`
echo -e "${green}\xE2\x9C\x94${default}|Software: ${name}|${software_size}" | awk -F "|" '{printf "%s %-30s %s\n", $1, $2, $3 }'
done
#===============================================================================
# prepare virtual file system with content of all available software, libraries, files
(cd build && clang ../tools/vfs.c -o vfs && find root -name '.keep' -delete && ./vfs root && find root -name '*.vfs' -delete && gzip -k root.vfs)
#cp build/kernel build/root.vfs tools/limine.cfg limine/limine-bios.sys limine/limine-bios-cd.bin limine/limine-uefi-cd.bin build/iso
cp build/kernel.gz build/root.vfs.gz tools/limine.cfg limine/limine-bios.sys limine/limine-bios-cd.bin limine/limine-uefi-cd.bin build/iso
# information
echo -e "\nOverall ------------"
kernel_size=`ls -lh build/kernel | cut -d ' ' -f 5`
echo -e "|kernel|${kernel_size}" | awk -F "|" '{printf "%s %-30s %s\n", $1, $2, $3 }'
root_size=`ls -lh build/root.vfs 2>&1 | cut -d ' ' -f 5`
echo -e "|root.vfs|${root_size}" | awk -F "|" '{printf "%s %-30s %s\n", $1, $2, $3 }'
echo -e "\nCompressed ---------"
kernel_size=`ls -lh build/kernel.gz | cut -d ' ' -f 5`
echo -e "|kernel.gz|${kernel_size}" | awk -F "|" '{printf "%s %-30s %s\n", $1, $2, $3 }'
root_size=`ls -lh build/root.vfs.gz 2>&1 | cut -d ' ' -f 5`
echo -e "|root.vfs.gz|${root_size}" | awk -F "|" '{printf "%s %-30s %s\n", $1, $2, $3 }'
# convert iso directory to iso file
xorriso -as mkisofs -b limine-bios-cd.bin -no-emul-boot -boot-load-size 4 -boot-info-table --efi-boot limine-uefi-cd.bin -efi-boot-part --efi-boot-image --protective-msdos-label build/iso -o build/foton.iso > /dev/null 2>&1
# install bootloader limine inside created
limine/limine bios-install build/foton.iso > /dev/null 2>&1