# This build script uses some GNU make specific extensions.

include config.mk

CFLAGS += -ggdb

# Kernel sources which are specific to the x86_64 architecture.
# Add new source files (C or Assembler) here,
# preferentially in alphabetical order.
VISOR_SOURCES_x86_64 := \
	src/vintel.c \
	src/proc.c \
	# end of x86_64 specific sources list

# Architecture-agnostic kernel sources.
# Add new source files (C or Assembler) here,
# preferentially in alphabetical order.
VISOR_SOURCES :=  \
	src/alloc.c   \
	src/efi.c     \
	src/strlcpy.c \
	$(VISOR_SOURCES_$(ARCH)) \
	# end of sources list

VISOR_OBJECTS  := $(VISOR_SOURCES:.c=.o)
VISOR_OBJECTS  := $(VISOR_OBJECTS:.S=.o)
VISOR_OBJECTS  := $(VISOR_OBJECTS:%.o=build/%.o)
VISOR_DEPFILES := $(VISOR_OBJECTS:.o=.d)
VISOR_TARGET   := build/visor

.PHONY: all clean

all: $(VISOR_TARGET)

clean:
	rm -f $(VISOR_OBJECTS)
	rm -f $(VISOR_DEPFILES)
	rm -f build/visor.so
	rm -f $(VISOR_TARGET)

build/src/%.o: src/%.[cS] | build/src
	@printf "CC %s\n" $@
	@"$(CC)" $(CFLAGS) -MMD -MP -c -o $@ $< $(CPPFLAGS)

build/visor.so: $(VISOR_OBJECTS) gnuefi/crt0-efi-$(ARCH).o | build
	@printf "LD %s\n" $@
	@"$(LD)" $(LDFLAGS) -o $@ gnuefi/crt0-efi-$(ARCH).o $(VISOR_OBJECTS) $(LIBS)

$(VISOR_TARGET): build/visor.so | build
	@printf "ELF->PE %s\n" $@
	@objcopy -j .text -j .sdata -j .data -j .rodata -j .dynamic -j .dynsym -j .rel -j .rela -j .rel.* -j .rela.* -j .reloc --target efi-app-x86_64 --subsystem 10 build/visor.so $@

build:
	mkdir -p $@

build/src:
	mkdir -p $@

config.mk: | config.default.mk
	cp config.default.mk $@

-include $(VISOR_DEPFILES)
