# 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.

# Music Library Makefile for DJGPP cross-compilation
#
# music.lib (see music.h) is our own sources but musicXmp.c.  The files
# that run at interrupt time (ISR_SRCS) are linked last, so musicLock pins
# one span from their first function to the end of the library's data;
# gcc must not move parts of them elsewhere or turn loops into library
# calls.  The OP2 instrument bank is copied beside the library.
#
# players/xmp.drv, the module player music.lib loads when a tracker
# module is opened (musicXmpDrv.c), is musicXmp.c and libxmp-lite
# (thirdparty/libxmp-lite, with DVX's patches from its dvx-patches
# folder, built with warnings off, as its own options say below): only
# what musicXmp.c reaches of libxmp is kept (DXE_GC).

include ../../../common.mk

XMP_DIR    = thirdparty/libxmp-lite
XMP_SRCS   = $(wildcard $(XMP_DIR)/src/*.c) $(wildcard $(XMP_DIR)/src/loaders/*.c)
XMP_HDRS   = $(wildcard $(XMP_DIR)/include/*.h $(XMP_DIR)/src/*.h $(XMP_DIR)/src/loaders/*.h)
XMP_OPTS   = -DLIBXMP_CORE_PLAYER -DLIBXMP_NO_PROWIZARD -DLIBXMP_NO_DEPACKERS -DBUILDING_STATIC -I$(XMP_DIR)/include -I$(XMP_DIR)/src
XMP_CFLAGS = $(filter-out $(WARNFLAGS) $(DEPFLAGS),$(DOS_CFLAGS)) -w -fno-analyzer $(DXE_GC_CFLAGS) $(XMP_OPTS)

ISR_SRCS   = musicCents.c musicFm.c musicModTick.c musicMpu.c musicWave.c musicSeq.c
PLAYER_SRC = musicXmp.c
MAIN_SRCS  = $(filter-out $(ISR_SRCS) $(PLAYER_SRC),$(wildcard *.c))
ISR_CFLAGS = -fno-reorder-blocks-and-partition -fno-reorder-functions -fno-tree-loop-distribute-patterns
CFLAGS     = $(DOS_CFLAGS) $(ISR_CFLAGS) -I. -I$(XMP_DIR)/include -I../audio -I../libdvx -I../libdvx/platform -I../libdvx/thirdparty -I../libtasks -I../dvxshell

OBJDIR    = ../../../../obj/music
LIBSDIR   = ../../../../bin/libs
TARGETDIR = $(LIBSDIR)/kpunch/music
TARGET    = $(TARGETDIR)/music.lib
BANK      = $(TARGETDIR)/genmidi.op2
PLAYERDIR = $(TARGETDIR)/players
PLAYER    = $(PLAYERDIR)/xmp.drv

XMP_OBJS   = $(patsubst $(XMP_DIR)/%.c,$(OBJDIR)/xmp/%.o,$(XMP_SRCS))
OBJS       = $(patsubst %.c,$(OBJDIR)/%.o,$(MAIN_SRCS) $(ISR_SRCS))
PLAYER_OBJ = $(OBJDIR)/$(PLAYER_SRC:.c=.o)
GC_OBJ     = $(OBJDIR)/xmpGc.o

.PHONY: all clean

all: $(TARGET) $(TARGETDIR)/music.dep $(BANK) $(PLAYER)

$(TARGETDIR)/music.dep: music.dep | $(TARGETDIR)
	sed 's/$$/\r/' $< > $@

$(BANK): thirdparty/genmidi/GENMIDI.OP2 | $(TARGETDIR)
	cp $< $@

$(TARGET): $(OBJS) | $(TARGETDIR)
	$(DXE3GEN) -o $(TARGETDIR)/music.dxe -U $(OBJS)
	mv $(TARGETDIR)/music.dxe $@

$(GC_OBJ): $(PLAYER_OBJ) $(XMP_OBJS)
	$(DXE_GC) $@ $(PLAYER_OBJ) -- $(PLAYER_OBJ) $(XMP_OBJS)

$(PLAYER): $(GC_OBJ) | $(PLAYERDIR)
	$(DXE3GEN) -o $(PLAYERDIR)/xmp.dxe -U $(GC_OBJ)
	mv $(PLAYERDIR)/xmp.dxe $@

$(OBJDIR)/xmp/%.o: $(XMP_DIR)/%.c $(XMP_HDRS) $(DVX_MAKEFILES)
	@mkdir -p $(dir $@)
	$(CC) $(XMP_CFLAGS) -c -o $@ $<

$(OBJDIR)/%.o: %.c $(DVX_MAKEFILES) | $(OBJDIR)
	$(CC) $(CFLAGS) -c -o $@ $<

$(OBJDIR):
	mkdir -p $(OBJDIR)

$(TARGETDIR):
	mkdir -p $(TARGETDIR)

$(PLAYERDIR):
	mkdir -p $(PLAYERDIR)

-include $(patsubst %.c,$(OBJDIR)/%.d,$(MAIN_SRCS) $(ISR_SRCS) $(PLAYER_SRC))

clean:
	rm -rf $(OBJDIR) $(TARGETDIR)
