CC = gcc
LD = ld

BUILD = build

EXT2_ROOT = ../ext2_root

CFLAGS = \
	-m64 \
	-O2 \
	-ffreestanding \
	-fno-pie \
	-fno-pic \
	-fno-stack-protector \
	-nostdlib \
	-nodefaultlibs \
	-fno-builtin

LDFLAGS = \
	-m elf_x86_64 \
	-T linker.ld \
	-nostdlib \
	-z max-page-size=0x1000

SRC_C = programs/init.c
SRC_S = crt0.S

OBJ = \
	$(BUILD)/init.o \
	$(BUILD)/crt0.o

all: $(BUILD)/init.elf

$(BUILD):
	mkdir -p $(BUILD)

$(BUILD)/init.o: programs/init.c | $(BUILD)
	$(CC) $(CFLAGS) -c $< -o $@

$(BUILD)/crt0.o: crt0.S | $(BUILD)
	$(CC) $(CFLAGS) -c $< -o $@

$(BUILD)/init.elf: $(OBJ)
	$(LD) $(LDFLAGS) $^ -o $@

install: $(BUILD)/init.elf
	cp $(BUILD)/init.elf $(EXT2_ROOT)/init.elf

clean:
	rm -rf $(BUILD)