# The MIT License (MIT)
#
# Copyright (C) 2026 Scott Duensing
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.


# Form-runtime test suite (T5): formrt + basrt on the host backend.
#
#     make -C src/test/formrt          # build + run (SAN=1 default)
#     make -C src/test/formrt SAN=0
#
# Links obj/host/basrt.a (compiler + runtime + formrt, built by the
# dvxbasic Makefile's host-basrt target) with libdvx.a, libdvxtest.a
# and the widget .so files.

.DEFAULT_GOAL := all

include ../dvxtest/dvxtest.mk

DVXBASIC   = $(DVXTEST_BASICDIR)
BINDIR     = $(DVXTEST_BINDIR)/formrt
TARGET     = $(BINDIR)/testFormRt
STUBS_SO   = $(BINDIR)/frtStubs.so
# Every .c here except the stub library is part of the test binary.
SRCS       = $(filter-out frtStubs.c,$(wildcard *.c))
SEC_DIR    = $(DVXTEST_ROOT)/src/libs/kpunch/serial/security
SEC_OBJS   = $(patsubst $(SEC_DIR)/%.c,$(BINDIR)/security/%.o,$(wildcard $(SEC_DIR)/*.c))
OBJS       = $(patsubst %.c,$(BINDIR)/%.o,$(SRCS)) $(BINDIR)/basBuild.o $(BINDIR)/game.o $(SEC_OBJS)
GAME_DIR   = $(DVXTEST_ROOT)/src/libs/kpunch/game
CFLAGS     = $(DVXTEST_CFLAGS) -I$(DVXBASIC) -I$(DVXTEST_ROOT)/src/libs/kpunch/dvxshell -DFRT_FIXTURE_DIR='"$(abspath .)/fixtures"' -DFRT_INCLUDE_DIR='"$(DVXTEST_ROOT)/src/include/basic"' -DFRT_STUBS_SO='"$(abspath $(STUBS_SO))"'

.PHONY: all run clean

all: run

$(BINDIR)/%.o: %.c $(DVX_MAKEFILES)
	@mkdir -p $(BINDIR)
	$(HOSTCC) $(CFLAGS) -c -o $@ $<

# The build pipeline is not part of basrt.a; compile it for caseBuildProject.
$(BINDIR)/basBuild.o: $(DVXBASIC)/basBuild.c $(DVX_MAKEFILES)
	@mkdir -p $(BINDIR)
	$(HOSTCC) $(CFLAGS) -c -o $@ $<

# The game library's frame pacer, so FRAMEWAIT runs (FrameMissed test).
$(BINDIR)/game.o: $(GAME_DIR)/game.c $(DVX_MAKEFILES)
	@mkdir -p $(BINDIR)
	$(HOSTCC) $(CFLAGS) -I$(GAME_DIR) -c -o $@ $<

# security.bas calls the security library for real
$(BINDIR)/security/%.o: $(SEC_DIR)/%.c $(DVX_MAKEFILES)
	@mkdir -p $(dir $@)
	$(HOSTCC) $(CFLAGS) -c -o $@ $<

$(STUBS_SO): frtStubs.c $(DVX_MAKEFILES)
	@mkdir -p $(BINDIR)
	$(HOSTCC) $(CFLAGS) -fPIC -shared -o $@ frtStubs.c

$(TARGET): $(OBJS) $(STUBS_SO) $(DVXTEST_BASRT_A) $(DVXTEST_TASKS_A) $(DVXTEST_ARCHIVES) | dvxtest-libs
	$(HOSTCC) -o $@ $(OBJS) $(DVXTEST_BASRT_A) $(DVXTEST_TASKS_A) $(DVXTEST_LINK)

-include $(OBJS:.o=.d) $(STUBS_SO:.so=.d)

# Runs from its object directory so the log and scratch files it writes
# never land in the source tree; the fonts are linked in there (the Font
# property cases draw with them).
run: $(TARGET) $(DVXTEST_HELP_HLP) dvxtest-fonts
	ln -sfn $(DVXTEST_FONTS) $(BINDIR)/$(DVXTEST_FONTS_DIR)
	cd $(BINDIR) && ASAN_OPTIONS=detect_leaks=1 ./testFormRt

clean:
	rm -rf $(BINDIR)
