AUXFILES := Makefile
PROJDIRS := General Boot IDTManager Exceptions
SRCFILES := $(shell find $(PROJDIRS) -mindepth 1 -maxdepth 3 -name "*.c")
HDRFILES := $(shell find $(PROJDIRS) -mindepth 1 -maxdepth 3 -name "*.h")
ASMFILES := $(shell find $(PROJDIRS) -mindepth 1 -maxdepth 3 -name "*.S")
OBJFILES := $(patsubst %.S,%.o,$(ASMFILES)) $(patsubst %.c,%.o,$(SRCFILES))
DEPFILES := $(patsubst %.c,%.d,$(SRCFILES))

ALLFILES := $(SRCFILES) $(HDRFILES) $(AUXFILES) $(ASMFILES)

.PHONY: clean all

CC := i386-elf-gcc

CFLAGS := -Wall -Wextra -pedantic -Wshadow -Wpointer-arith -Wcast-align \
          -Wwrite-strings -Wmissing-prototypes -Wmissing-declarations \
          -Wredundant-decls -Wnested-externs -Winline -Wno-long-long \
          -Wconversion -Wstrict-prototypes

AS := nasm
LD := i386-elf-ld

all: kernel

kernel: $(OBJFILES) General/linker.ld
	@$(LD) -o kernel -Map kernelmap $(OBJFILES) General/linker.ld

clean:
	-@for file in $(OBJFILES) $(DEPFILES) kernel; do if [ -f $$file ]; then rm $$file; fi;

-include $(DEPFILES)

todolist:
	-@for file in $(ALLFILES); do grep -H TODO $$file; done; true

print_asmfiles:
	echo $(ASMFILES)

print_asmobj:
	echo $(patsubst %.S,%.o,$(ASMFILES))

print_objfiles:
	echo $(OBJFILES) 

%.o: %.c Makefile
	@$(CC) $(CFLAGS) -DNDEBUG -MD -MP -std=c99 -c $< -o $@
%.o: %.S Makefile
	@$(AS) -f elf -o $@ $<