# location where the kernel binary is installed
INSTALL_FOLDER:=./binary/

# compiler and flags
CC:=/usr/cross/bin/i586-elf-gcc.exe
CPPFLAGS:=-I ./include -nostdinc++ -nostartfiles -fno-builtin -fno-exceptions -fno-rtti -O3  -Werror

CFLAGS:=-I ./include -std=c99 -fno-builtin -fno-exceptions -nostdlib -O3

# linker and flags
LD:=/usr/cross/bin/i586-elf-ld.exe
LDFLAGS:=-T ./link.ld -o

# project directories
PROJDIRS:=.
# net

# assembler
AS:=nasm
AS_SRCS:=kernel/loader.asm

# source and objects, and final binary
SRCS:=$(shell find $(PROJDIRS) -maxdepth 5 -name "*.cpp" -type f)

SRCS2:=$(shell find $(PROJDIRS) -maxdepth 5 -name "*.c" -type f)

OBJS:=$(patsubst %.asm,%.o,$(AS_SRCS)) $(patsubst %.cpp,%.o,$(SRCS)) $(patsubst %.c,%.o,$(SRCS2))

# final binary filename
FINALBIN:=$(INSTALL_FOLDER)kernel.bin

# target all builds the kernel
all: $(FINALBIN)

# install copies the binary to the install folder
install: $(FINALBIN)
	@rm $(shell find -iname "*.[oda]")

# clean up, but don't remove the binary from the install folder
clean:
	rm $(shell find -iname "*.[oda]")
#	rm $(FINALBIN)



# final binary target
$(FINALBIN) : $(OBJS)
	$(LD) $(LDFLAGS) $(FINALBIN) $(OBJS)


# setup the dependencies (so that header changes update relevant source)
#-include $(DEPFILES)

# c++ source files
%.o : %.cpp
	$(CC) $(CPPFLAGS) -c $< -o $@
	
%.o : %.c
	$(CC) $(CFLAGS) -c $< -o $@

# assembly
%.o : %.asm
	$(AS) $< -f elf -o $@
