#
# Copyright (c) 2026, Chloe M.
# Provided under the BSD-3 clause
#
# Description:  AMD64 port build script
# Author:		Chloe M.
#

.SILENT:
include ../../../mk/ptos.mk

ASMFILES = $(shell find . -name "*.S")
ASMOFILES = $(ASMFILES:.S=.S.o)

CFILES = $(shell find . -name "*.c")
OFILES = $(CFILES:.c=.o)
CDFILES = $(OFILES:.o.d)
ASMDFILES = $(ASMOFILES:.S.o=.S.d)

MISC_OFILES = $(shell find $(PT_PROJECT_ROOT)/service/ptos/ -name "*.o")
MISC_OFILES += $(shell find $(PT_PROJECT_ROOT)/service/spkg/ -name "*.o")

KERNEL_PATH =       \
	$(PT_PROJECT_ROOT)/artifacts/ptsv.sys

CFLAGS =				\
	$(SYS_CFLAGS)		\
	-I../../target		\
	-I../../head/		\
	-D_KERNEL			\
	-MMD				\
	-I$(PT_PROJECT_ROOT)/sdk/head \
	-I$(PT_PROJECT_ROOT)/service/spkg/head/

.PHONY: all
all: bin

.PHONY: bin
bin: $(OFILES) $(ASMOFILES)
	$(PROMPT) "LD" $(KERNEL_PATH)
	$(SYS_LD) -Tdev/link.ld $(MISC_OFILES) -o $(KERNEL_PATH)

-include $(CDFILES)
%.o: %.c
	$(PROMPT) "CC" $<
	$(SYS_CC) -c $< $(CFLAGS) -o $@

-include $(ASMDFILES)
%.S.o: %.S
	$(PROMPT) "AS" $<
	$(SYS_CC) -c $< $(CFLAGS) -o $@
