# Smoke test for the ports sysroot.
#
# Links one program against every library port and exercises a little of each,
# printing PASS/FAIL per library. Building it proves the sysroot's headers and
# archives resolve; RUNNING it on MontaukOS is the only way to prove the code
# is correct, since none of this can execute on the build host.
#
#   make -C test              build out/test/porttest.elf
#   make -C test install      copy it into the MontaukOS ramdisk as a system
#                             program, then rebuild the image and run
#                             `porttest.elf` from the terminal
#   make -C test uninstall    remove it again (do this before cutting a
#                             release -- it is not an app bundle, so it
#                             carries no .port marker and the release step
#                             will NOT prune it automatically)

PORT_NAME := porttest

include ../mk/port.mk

# Link order matters: dependents before their dependencies.
SYSROOT_LIBS := \
    $(PORT_SYSROOT_LIB)/libdom.a \
    $(PORT_SYSROOT_LIB)/libcss.a \
    $(PORT_SYSROOT_LIB)/libhubbub.a \
    $(PORT_SYSROOT_LIB)/libparserutils.a \
    $(PORT_SYSROOT_LIB)/libwapcaplet.a \
    $(PORT_SYSROOT_LIB)/libnsutils.a \
    $(PORT_SYSROOT_LIB)/libnslog.a \
    $(PORT_SYSROOT_LIB)/libnspsl.a \
    $(PORT_SYSROOT_LIB)/libnsgif.a \
    $(PORT_SYSROOT_LIB)/libnsbmp.a \
    $(PORT_SYSROOT_LIB)/libutf8proc.a \
    $(PORT_SYSROOT_LIB)/libz.a

TEST_ELF := $(OUTDIR)/test/porttest.elf

CFLAGS := -std=gnu11 $(PORT_CFLAGS_COMMON) -Wall

.PHONY: all clean install uninstall
all: $(TEST_ELF)

$(TEST_ELF): porttest.c $(SYSROOT_LIBS) $(LIBC_A) Makefile
	@mkdir -p $(dir $@)
	$(CC) $(CFLAGS) $(PORT_LDFLAGS_COMMON) porttest.c \
	    $(SDK_LIB)/libc/crt1.o $(SYSROOT_LIBS) $(LIBC_A) -o $@
	@echo "Built: $@"

install: $(TEST_ELF)
	cp $(TEST_ELF) $(MONTAUKOS)/programs/bin/os/porttest.elf
	@echo "installed: 0:/os/porttest.elf - rebuild the image, then run 'porttest.elf'"
	@echo "REMINDER: 'make -C test uninstall' before cutting a release ISO."

uninstall:
	rm -f $(MONTAUKOS)/programs/bin/os/porttest.elf
	@echo "removed: $(MONTAUKOS)/programs/bin/os/porttest.elf"

clean:
	rm -rf $(OUTDIR)/test
