Makefile refactorin'

This commit is contained in:
2024-10-18 17:18:24 +02:00
parent 63ecaec569
commit 958625b798
17 changed files with 175 additions and 150 deletions
-100
View File
@@ -1,100 +0,0 @@
# Variables
CC := i686-elf-gcc
LD := i686-elf-ld
AS := nasm
CCFLAGS := -g -std=c17 -ffreestanding -fno-builtin -fno-stack-protector -fno-pie \
-Os -nostdinc -mgeneral-regs-only -m32 -march=i386 -Wall
LDFLAGS := -z notext -melf_i386 -nostdlib
ASFLAGS :=
include make.config
BOOT := ./boot
SRC := ./src
BIN := ./bin
LIBK_INC := ./libk/include
LIBC_INC := ./libc/include
LIBC_DIR := ./libc
LIBK_DIR := ./libk
FONT_DIR := ./usr/fonts
LIBK_SRC := $(shell find $(LIBK_DIR) -type f -name "**.c")
FONTS := testfont term16n
BOOTLOADER := $(BIN)/stage1.bin $(BIN)/stage2.bin $(BIN)/stage3.bin
KERNEL := $(BIN)/kernel.bin
LIBC := $(BIN)/libc.o
LIBK := $(BIN)/libk.o
ISO := kern.iso
.PHONY: os clean run
# Main compilation target
os: $(ISO)
$(ISO): $(BIN) $(BOOTLOADER) $(KERNEL) $(C_FILES) $(FONTS)
cc ./makedisk.c -o ./makedisk
./makedisk # TODO: List of files
@echo "\nDone!\n"
$(BIN):
[ -d $(BIN) ] || mkdir $(BIN)
rm -rf $(BIN)/*
# Bootloader: This target is responsible for creating stage?.bin (1,2,3)
$(BOOTLOADER): $(BOOT)/stage1.asm $(BOOT)/stage2.asm $(BOOT)/stage3.c $(LIBC) $(LIBK)
@echo "Assembling $(BOOT)/stage1.asm..."
$(AS) -fbin -o $(BIN)/stage1.bin $(BOOT)/stage1.asm $(ASFLAGS)
@echo "Assembling $(BOOT)/stage2.asm..."
$(AS) -fbin -o $(BIN)/stage2.bin $(BOOT)/stage2.asm $(ASFLAGS)
@echo "Compiling $(BOOT)/stage3.c..."
$(CC) $(CCFLAGS) -I$(LIBK_INC) -I$(LIBC_INC) -o $(BIN)/stage3.o -c $(BOOT)/stage3.c
$(LD) $(LDFLAGS) -T$(BOOT)/stage3.ld --oformat binary -o $(BIN)/stage3.bin $(BIN)/stage3.o $(LIBC) $(LIBK)
rm $(BIN)/stage3.o
# Kernel: This target is responsible for creating kernel.bin
$(KERNEL): $(SRC)/kernel.c $(LIBC) #TODO: $(LIBK)
@echo "Compiling $(SRC)/kernel.c..."
$(CC) $(CCFLAGS) -I$(LIBK_INC) -I$(LIBC_INC) -o $(BIN)/kernel.o -c $(SRC)/kernel.c
$(LD) $(LDFLAGS) -T$(SRC)/kernel.ld --oformat binary -o $(BIN)/kernel.bin $(BIN)/kernel.o $(LIBC) $(LIBK)
rm $(BIN)/kernel.o
# TODO: user programs
$(LIBC): $(LIBC_DIR)/**.c
@echo "Compiling $(LIBC)..."
mkdir -p $(BIN)/libc
for f in libc/*.c; do\
filename=$$(basename $$f .c);\
$(CC) $(CCFLAGS) -I$(LIBC_INC) -o $(BIN)/libc/$$filename.o -c $$f;\
done
$(LD) $(LDFLAGS) -r -o $(LIBC) $(BIN)/libc/**.o
$(LIBK): $(LIBK_SRC)
@echo "Compiling $(LIBK)..."
mkdir -p $(BIN)/libk
for f in $(LIBK_SRC); do\
filename=$$(basename $$f .c);\
$(CC) $(CCFLAGS) -I$(LIBC_INC) -I$(LIBK_INC) -o $(BIN)/libk/$$filename.o -c $$f;\
done
$(LD) $(LDFLAGS) -r -o $@ $(BIN)/libk/**.o
# This target retrieves all fonts inside src/fonts
$(FONTS):
cp $(FONT_DIR)/$@.fnt $(BIN)/$@.fnt
# Run QEMU
run:
qemu-system-x86_64\
-drive format=raw,file=$(ISO),index=0,media=disk\
-m 256M -accel tcg -monitor stdio\
-rtc base=localtime,clock=host,driftfix=slew\
-audiodev coreaudio,id=audio0 -machine pcspk-audiodev=audio0
# Clean all build files
clean:
rm -f kern.iso
rm -rf $(BIN)
rm -f makedisk
+15
View File
@@ -0,0 +1,15 @@
#
# Bootloader
#
include make.defaults
bootloader: $(BOOT_DIR)/stage1.asm $(BOOT_DIR)/stage2.asm $(BOOT_DIR)/stage3.c $(LIBC) $(LIBK)
@echo "Assembling $(BOOT_DIR)/stage1.asm..."
$(AS) -fbin -o $(BIN)/stage1.bin $(BOOT_DIR)/stage1.asm $(ASFLAGS)
@echo "Assembling $(BOOT_DIR)/stage2.asm..."
$(AS) -fbin -o $(BIN)/stage2.bin $(BOOT_DIR)/stage2.asm $(ASFLAGS)
@echo "Compiling $(BOOT_DIR)/stage3.c..."
$(CC) $(CFLAGS) -I$(LIBK_INC) -I$(LIBC_INC) -o $(BIN)/boot/stage3.o -c $(BOOT_DIR)/stage3.c
$(LD) $(LDFLAGS) -T$(BOOT_DIR)/stage3.ld --oformat binary -o $(BIN)/stage3.bin $(BIN)/boot/stage3.o $(LIBC) $(LIBK)
+10 -6
View File
@@ -2,11 +2,15 @@
Root contains the Makefile that is used to build kern and some documents. Root contains the Makefile that is used to build kern and some documents.
It also contains these directories: It also contains these directories:
src/ - The source code of kern src/ - Kernel source code
include/ - C header files that can be included by the OS. include/c/ can be used by any programs, but other headers shall only be included in the kernel boot/ - Bootloader code
build/ - This directory is created by the Makefile and contains intermediary built obj file and binaries libc/ - C Standard library
other/ - Things that are not nescessarly part of the OS libk/ - Kernel library
docs/ - Some basic documentation. Mostly for me so that i don't forget stuff docs/ - Some documentation (mainly for me)
other/ - Things like screenshot, etc.
usr/ - Userspace files like fonts and programs
bin/ - Intermediary binaries
After running the Makefile and succesfully building the OS a 'kern.iso' file will be created in the root directory After running the Makefile and succesfully building the OS
a 'kern.iso' file will be created in the root directory
Binary file not shown.
+2 -2
View File
@@ -57,7 +57,7 @@ void main() {
setupTimer(); setupTimer();
setupRtc(); setupRtc();
setupKeyboard(); setupKeyboard();
if (setupSerial(PORT_COM1) != 0) { printf("\e[1MFailed to initialize serial interface\e[8M\n"); } if (setupSerial(PORT_COM1) != 0) { printf("\x1b[1MFailed to initialize serial interface\x1b[8M\n"); }
else { else {
serialPrint(PORT_COM1, "\x1b[H\x1b[J"); serialPrint(PORT_COM1, "\x1b[H\x1b[J");
serialPrint(PORT_COM1, "kern. \x1b[36m(Serial console)\x1b[0m\n\n"); serialPrint(PORT_COM1, "kern. \x1b[36m(Serial console)\x1b[0m\n\n");
@@ -98,7 +98,7 @@ void main() {
// Run Interactive Shell Program // Run Interactive Shell Program
// TODO: Make it in another file // TODO: Make it in another file
while (1) { while (1) {
printf("\e[6M\e[8N%s \e[2M%s \e[8M", cwd, PROMPT); printf("\x1b[6M\x1b[8N%s \x1b[2M%s \x1b[8M", cwd, PROMPT);
char input[256]; char input[256];
char* inputPtr = input; char* inputPtr = input;
View File
+11
View File
@@ -0,0 +1,11 @@
#
# Kernel makefile
#
include make.defaults
$(KERNEL): $(KERNEL_SRC) $(LIBC) $(LIBK)
@echo "Compiling $(KERNEL_SRC)..."
$(CC) $(CFLAGS) -I$(LIBK_INC) -I$(LIBC_INC) -o $(BIN)/kernel/kernel.o -c $(KERNEL_SRC)
$(LD) $(LDFLAGS) -T$(KERNEL_DIR)/kernel.ld --oformat binary -o $(BIN)/kernel.bin $(BIN)/kernel/kernel.o $(LIBC) $(LIBK)
+2 -2
View File
@@ -30,7 +30,7 @@ size_t fread(void* ptr, size_t size, size_t count, FILE* stream) {
int fd = stream->_file; int fd = stream->_file;
while (readEl != count) { while (readEl != count) {
if (read(fd, (void*)(ptr + offset), size) != size) break; if (read(fd, (void*)(ptr + offset), size) != (int)size) break;
offset += size; offset += size;
readEl++; readEl++;
} }
@@ -44,7 +44,7 @@ size_t fwrite(const void* ptr, size_t size, size_t count, FILE* stream) {
int fd = stream->_file; int fd = stream->_file;
while (writtenEl != count) { while (writtenEl != count) {
if (write(fd, (void*)(ptr + offset), size) != size) break; if (write(fd, (void*)(ptr + offset), size) != (int)size) break;
offset += size; offset += size;
writtenEl++; writtenEl++;
} }
+14
View File
@@ -0,0 +1,14 @@
#
# C Standard library
#
include make.defaults
$(LIBC): $(LIBC_SRC)
@echo "Building $(LIBC)..."
for f in $(LIBC_SRC); do\
filename=$$(basename $$f .c);\
$(CC) $(CFLAGS) -I$(LIBC_INC) -o $(BIN)/libc/$$filename.o -c $$f;\
done
$(LD) $(LDFLAGS) -r -o $(LIBC) $(BIN)/libc/**.o
+2 -14
View File
@@ -1,16 +1,4 @@
#ifndef MATH_H #include <math.h>
#define MATH_H
// Constants /* TODO */
#define PI 3.141592653589 // Should be good enough
#define E 2.718281828459
// Macros
#define abs(x) ( (x) > 0 ? (x) : -(x) )
#define sign(x) ( (x) == 0 ? 0 : ((x) < 0 ? -1 : 1 ) )
#define floor(x) ( (int)(x) ) // C floors floats when converting to ints
#define round(x) ( (int)((x) + 0.5) ) // Normal rounding
#define ceil(x) ( (int)((x) + 1) ) // Always rounds up
#endif
-8
View File
@@ -1,8 +0,0 @@
#ifndef BOOL_H
#define BOOL_H
#define bool _Bool
#define true 1
#define false 0
#endif
+14
View File
@@ -0,0 +1,14 @@
#
# Kernel library
#
include make.defaults
$(LIBK): $(LIBK_SRC)
@echo "Building $(LIBK)..."
for f in $(LIBK_SRC); do\
filename=$$(basename $$f .c);\
$(CC) $(CFLAGS) -I$(LIBC_INC) -I$(LIBK_INC) -o $(BIN)/libk/$$filename.o -c $$f;\
done
$(LD) $(LDFLAGS) -r -o $(LIBK) $(BIN)/libk/**.o
-9
View File
@@ -1,9 +0,0 @@
# Kern Config
# Uncomment a line to enable it
# You may also change values
#ASFLAGS := $(ASFLAGS) -D DEF_RES # Use default resolution variables
ASFLAGS := $(ASFLAGS) -D DEF_W=1920 # Default width
ASFLAGS := $(ASFLAGS) -D DEF_H=1080 # Default height
ASFLAGS := $(ASFLAGS) -D DEF_BPP=32 # Default bpp
+51
View File
@@ -0,0 +1,51 @@
#
# Makfile variables
#
# Tools
HOSTCC = cc
CC = i686-elf-gcc
LD = i686-elf-ld
AS = nasm
MAKE = make
# Flags
CFLAGS = -Wall -Wextra -Wpedantic -Wno-pointer-sign -Wno-pointer-arith\
-Os -std=c17 -nostdinc -g\
-ffreestanding -fno-stack-protector -fno-builtin -fno-pie\
-m32 -mgeneral-regs-only -march=i386
LDFLAGS = -z notext -melf_i386 -nostdlib
ASFLAGS =
# Directories
# Outputs
BIN = ./bin
BOOTLOADER = $(BIN)/stage1.bin $(BIN)/stage2.bin $(BIN)/stage3.bin
KERNEL = $(BIN)/kernel.bin
LIBC = $(BIN)/libc.o
LIBK = $(BIN)/libk.o
IMG = kern.img
# Kernel
KERNEL_DIR = ./kernel
KERNEL_SRC = $(KERNEL_DIR)/kernel.c
# Bootloader
BOOT_DIR = ./boot
# Libc
LIBC_DIR = ./libc
LIBC_SRC = $(shell find $(LIBC_DIR) -type f -name "**.c")
# Libk
LIBK_DIR = ./libk
LIBK_SRC = $(shell find $(LIBK_DIR) -type f -name "**.c")
# Userspace
FONT_DIR = ./usr/fonts
FONTS = testfont term16n
# Include directories
LIBC_INC = $(LIBC_DIR)/include
LIBK_INC = $(LIBK_DIR)/include
+2 -2
View File
@@ -5,9 +5,9 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "libk/include/fs/fs.h" #include <fs.h>
#define IMAGE_NAME "kern.iso" #define IMAGE_NAME "kern.img"
#define BIN_DIR "./bin/" #define BIN_DIR "./bin/"
#define DISK_SIZE (2880 * 512) // 1.44MB #define DISK_SIZE (2880 * 512) // 1.44MB
+45
View File
@@ -0,0 +1,45 @@
#
# Main kern makefile
#
# Version
export VERSION = 0.2.0
# Configurable variables (eg. CC, CFLAGS, ...)
include make.defaults
# The directories containing other makefiles
SUBDIRS = libc libk boot kernel
all: msg $(IMG)
msg:
@echo\
"\x1b[36mBuilding \x1b[1mkern.\x1b[22m V$(VERSION)\x1b[0m\n\x1b[32m (c) Tom-on 2024\x1b[0m\n"
# Build the final disk image
$(IMG): $(SUBDIRS) $(FONTS)
$(HOSTCC) ./makedisk.c -I$(LIBK_INC)/fs -o ./makedisk
./makedisk
@echo "\n\x1b[32mDone!\x1b[0m\n"
$(SUBDIRS):
mkdir -p $(BIN)/$@
$(MAKE) -C . -f $@/makefile
$(FONTS):
cp $(FONT_DIR)/$@.fnt $(BIN)/$@.fnt
.PHONY: all $(SUBDIRS) fonts clean qemu
clean:
rm -rf $(BIN)
rm -rf $(IMG)
rm -f makedisk
qemu:
qemu-system-x86_64\
-drive format=raw,file=$(IMG),index=0,media=disk\
-m 256M -accel tcg -monitor stdio\
-rtc base=localtime,clock=host,driftfix=slew\
-audiodev coreaudio,id=audio0 -machine pcspk-audiodev=audio0
+7 -7
View File
@@ -8,9 +8,9 @@ Legend:
- [?] Optional - [?] Optional
- [!] The Big Refactor - [!] The Big Refactor
- [?] Use GRUB - [?] Use UEFI
- [ ] Multiboot - [ ] 32-bit (probably)
- [ ] boot.s - [ ] Partitioned disk
- [ ] project structure - [ ] project structure
- [ ] system root - [ ] system root
- [ ] arch/ - [ ] arch/
@@ -19,17 +19,17 @@ Legend:
- [ ] cross compiler setup - [ ] cross compiler setup
- [x] libc - User space - [x] libc - User space
- [x] libk - Kernel libc - [x] libk - Kernel libc
- [ ] kernel - [x] kernel
- [x] use libk - [x] use libk
- [ ] makefile - [x] makefile
- [ ] probably a full rewrite - [?] probably a full rewrite
- [ ] userspace - [ ] userspace
- [ ] shell - [ ] shell
- [ ] calc - [ ] calc
- [ ] editor - [ ] editor
- [ ] filesystem - [ ] filesystem
- [ ] proper file organization - [ ] proper file organization
- [_] Makefile refactorin' - [X] Makefile refactorin' (finally done!!!)
- [x] Multistage Bootloader - [x] Multistage Bootloader
- [x] Bootloader reads filesystem to find files - [x] Bootloader reads filesystem to find files