include $(ROOT)/mk/grabsrc.mk

.PHONY: all clean

PUTCHAR_ ?= fb

CFLAGS := -ffreestanding -Wall -Wextra -g -fcommon -nostdinc

CFLAGS += -I. \
          -I$(ROOT)/limine \
          -I$(ROOT)/std/include \
          -I./std \
          -I./flanterm/src \
					-I$(ROOT)/share \
					-I./picotcp/include \
					-I./picotcp/modules \
					-I./port_picotcp \
          -DPRINTF_INCLUDE_CONFIG_H=1 \
          -DLFS_NO_ASSERT \
          -DLFS_NO_DEBUG \
          -DLFS_NO_WARN \
          -DLFS_NO_ERROR \
					-DPICO_MOP2 \
					-DPICO_SUPPORT_TCP \
					-DPICO_SUPPORT_UDP \
					-DPICO_SUPPORT_ETH \
					-DPICO_SUPPORT_ICMP4 \
					-DPICO_SUPPORT_IPV4 \
					-DPICO_SUPPORT_MUTEX \

ifeq ($(PUTCHAR_),fb)
CFLAGS += -DPUTCHAR_=PUTCHAR_FB
else
CFLAGS += -DPUTCHAR_=PUTCHAR_SERIAL
endif

include arch/$(ARCH)/$(ARCH).mk
include extconf/extra.mk

LDFLAGS += -nostdlib -static -T arch/$(ARCH)/link.ld $(shell $(CC) -print-libgcc-file-name)

SRCFILES :=

SRCFILES += $(call GRABSRC, \
						  . \
              printf \
              bitmap \
              pmm \
              bootinfo \
              spinlock \
              term \
              dlmalloc \
              vfs \
              storedev \
              util \
              fs/kvfs \
              fs/littlefs \
              fs/portlfs \
              baseimg \
              proc \
              hal \
              hal/$(ARCH) \
              std \
              flanterm/src \
              flanterm/src/flanterm_backends \
              syscall \
							path \
							rbuf \
							ipc/pipe \
							ipc/netsock \
							dev \
							randcrypto \
							time \
							diskpart \
							netdev \
							port_picotcp \
							port_picotcp/modules \
							picotcp/stack \
            )

CFILES  := $(call GET_CFILES, $(SRCFILES))
ASFILES := $(call GET_ASFILES, $(SRCFILES))
OBJ := $(call GET_OBJ, $(SRCFILES))

%.o: %.c
	$(CC) $(CFLAGS) -c $< -o $@

%.o: %.S
	$(CC) $(CFLAGS) -c $< -o $@

all: mop2

mop2: $(OBJ)
	$(LD) $^ $(LDFLAGS) -o $@

clean:
	rm -f $(OBJ) mop2

