Feat: better screen formating & better project resstructure

This commit is contained in:
AfonsoCMSousa
2026-08-05 13:40:42 +01:00
parent 7a4394feb6
commit f80dafd03d
14 changed files with 277 additions and 170 deletions
+61 -11
View File
@@ -14,6 +14,32 @@ BLUE='\033[0;34m'
YELLOW='\033[1;33m'
NC='\033[0m'
# Shared compiler flags (single source of truth for both real compiles AND compile_commands.json)
CFLAGS="-ffreestanding -mcmodel=large -mno-red-zone -fno-pic -fno-pie -static -fno-stack-protector -nostdlib -mno-mmx -mno-sse -mno-sse2 -m64"
INCLUDE="-I./include"
# compile_commands.json setup (for clangd/nvim LSP support)
CCJ_OUTPUT="compile_commands.json"
PROJECT_DIR="$(pwd)"
append_ccj_entry() {
local src_file="$1"
local full_cmd="$2"
if [ "$ccj_first" = true ]; then
ccj_first=false
else
echo "," >> "$CCJ_OUTPUT"
fi
printf ' {\n' >> "$CCJ_OUTPUT"
printf ' "directory": "%s",\n' "$PROJECT_DIR" >> "$CCJ_OUTPUT"
printf ' "file": "%s",\n' "$src_file" >> "$CCJ_OUTPUT"
printf ' "command": "%s"\n' "$full_cmd" >> "$CCJ_OUTPUT"
printf ' }' >> "$CCJ_OUTPUT"
}
echo "[" > "$CCJ_OUTPUT"
ccj_first=true
# Prepare build environment
echo -e "Preparing build environment...${NC}"
echo -e "Creating Folders${NC}"
@@ -21,22 +47,45 @@ mkdir -p bin obj boot include
echo -e "${GREEN}${NC} Folders created.${NC}"
# Step 1: Assemble Stage 1 bootloader (MBR - 512 bytes)
echo -e "${BLUE}[1/6]${NC} Assembling Stage 1 bootloader (MBR)..."
echo -e "${BLUE}[1/7]${NC} Assembling Stage 1 bootloader (MBR)..."
nasm -f bin ./boot/boot.asm -o ./bin/boot.bin
# Step 1.5: Compile C source files
echo -e "${BLUE}[2/7]${NC} Compiling C source files..."
COUNT=$(ls src/*.c 2>/dev/null | wc -l)
if [ "$COUNT" -eq 0 ]; then
echo -e "${YELLOW}No C source files found in src/. Skipping compilation.${NC}"
else
COUNTER=1
for file in src/*.c; do
echo -e "${BLUE}[$COUNTER/$COUNT]${NC} Compiling $file..."
((COUNTER++))
name=$(basename "$file")
gcc $CFLAGS $INCLUDE -c "$file" -o "obj/${name}.o"
append_ccj_entry "$file" "gcc $CFLAGS $INCLUDE -c $file -o obj/${name}.o"
done
echo -e "${GREEN}${NC} C source files compiled.${NC}"
fi
# Step 2: Compile kernel C code
echo -e "${BLUE}[2/6]${NC} Compiling kernel..."
gcc -ffreestanding -c kernel.c -o ./obj/kernel.c.o -mcmodel=large -mno-red-zone -fno-pic -fno-pie -static -fno-stack-protector -nostdlib -mno-mmx -mno-sse -mno-sse2 -m64
echo -e "${BLUE}[3/7]${NC} Compiling kernel..."
gcc $CFLAGS $INCLUDE -c kernel.c -o ./obj/kernel.c.o
append_ccj_entry "kernel.c" "gcc $CFLAGS $INCLUDE -c kernel.c -o ./obj/kernel.c.o"
# Close compile_commands.json now that all C compiling is done
echo "" >> "$CCJ_OUTPUT"
echo "]" >> "$CCJ_OUTPUT"
# Step 3: Assemble kernel entry point
echo -e "${BLUE}[3/6]${NC} Assembling kernel entry..."
echo -e "${BLUE}[4/7]${NC} Assembling kernel entry..."
nasm -f elf64 ./boot/kernel.asm -o ./obj/kernel.asm.o
# Step 4: Link kernel
echo -e "${BLUE}[4/6]${NC} Linking kernel..."
ld -T linker.ld -o ./bin/kernel.bin ./obj/kernel.asm.o ./obj/kernel.c.o --oformat binary --nostdlib
echo -e "${BLUE}[5/7]${NC} Linking kernel..."
ld -T linker.ld -o ./bin/kernel.bin ./obj/*.o --oformat binary --nostdlib
# Debug build (optional)
ld -T linker.ld -o ./bin/kernel_debug.elf ./obj/kernel.asm.o ./obj/kernel.c.o --oformat elf64-x86-64 --nostdlib
ld -T linker.ld -o ./bin/kernel_debug.elf ./obj/*.o --oformat elf64-x86-64 --nostdlib
# Step 4.5: Calculate kernel size in sectors
KERNEL_SIZE=$(stat -f%z ./bin/kernel.bin 2>/dev/null || stat -c%s ./bin/kernel.bin 2>/dev/null)
@@ -44,16 +93,18 @@ SECTORS=$(( (KERNEL_SIZE + 511) / 512 )) #Round up to nearest sector
echo -e "${YELLOW}Kernel size: ${KERNEL_SIZE} bytes, which is ${SECTORS} sectors.${NC}"
# Step 5: Assemble Stage 2 bootloader (extended loader)
echo -e "${BLUE}[5/6]${NC} Assembling Stage 2 bootloader..."
echo -e "${BLUE}[6/7]${NC} Assembling Stage 2 bootloader..."
nasm -f bin ./boot/elevator.asm -D KERNEL_SECTORS=$SECTORS -o ./bin/elevator.bin
# Step 6: Create OS image
echo -e "${BLUE}[6/6]${NC} Creating OS image..."
echo -e "${BLUE}[7/7]${NC} Creating OS image..."
cat ./bin/boot.bin ./bin/elevator.bin ./bin/kernel.bin > os.bin
# Pad to floppy size (1.44MB)
# FIX: Uncomment the line below if you want to ensure the image is exactly 1.44MB
#truncate -s 1440K os.bin
truncate -s 1440K os.bin
echo ""
echo -e "${GREEN}✓ Build complete!${NC}"
@@ -83,5 +134,4 @@ echo ""
qemu-system-x86_64 -drive format=raw,file=os.bin -no-reboot
echo -e "${GREEN}Done!${NC}"