# 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): libxmp-lite (thirdparty/libxmp-lite, with
# DVX's patches from its dvx-patches folder, built with warnings off, as
# its own options say below) and our own sources.  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.

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 $(XMP_OPTS)

ISR_SRCS   = musicCents.c musicFm.c musicModTick.c musicMpu.c musicWave.c musicSeq.c
MAIN_SRCS  = $(filter-out $(ISR_SRCS),$(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

XMP_OBJS  = $(patsubst $(XMP_DIR)/%.c,$(OBJDIR)/xmp/%.o,$(XMP_SRCS))
OBJS      = $(XMP_OBJS) $(patsubst %.c,$(OBJDIR)/%.o,$(MAIN_SRCS) $(ISR_SRCS))

.PHONY: all clean

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

$(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 $@

$(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)

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

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