CROSS   = aarch64-linux-gnu-
AS      = $(CROSS)gcc
CC      = $(CROSS)gcc
LD      = $(CROSS)ld

CFLAGS = -Wall -Wextra -O2 -ffreestanding -nostdlib -mcpu=cortex-a53 -Iinclude

SRC_C = $(wildcard kernel/*.c)
SRC_S = $(wildcard arch/arm64/*.s)

OBJ_C = $(patsubst %.c,build/%.o,$(SRC_C))
OBJ_S = $(patsubst %.s,build/%.o,$(SRC_S))
OBJ   = $(OBJ_C) $(OBJ_S)

# Compile C source files
build/%.o: %.c
	@mkdir -p $(dir $@)
	$(CC) $(CFLAGS) -c $< -o $@

# Compile assembly source files
build/%.o: %.s
	@mkdir -p $(dir $@)
	$(AS) $(CFLAGS) -c $< -o $@

# Link final kernel ELF
build/kernel.elf: $(OBJ) linker.ld
	$(LD) -T linker.ld -o $@ $(OBJ)

# Targets
all: build/kernel.elf

clean:
	rm -rf build

.PHONY: all clean
