##
 # Makefile
 #
 # Yet Another (WA)SAPI (YASAPI) NT Output Plugin for Winamp (out_yasapi-nt)
 # Copyright (C) 2018  Peter Belkner (pbelkner@snafu.de)
 #
 # out_yasapi-nt is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
 # the Free Software Foundation, either version 3 of the License, or
 # (at your option) any later version.
 #
 # out_yasapi-nt is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
 #
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
 ##
###############################################################################
include config.mak
include $(srcdir)/sources.mak
include $(srcdir)/version.mak
builddir=$(PWD)

.PHONY: all install uninstall release-gtk release FORCE

###############################################################################
VPATH=$(srcdir)

ifneq (ms,$(tools)) # [
CC=gcc
CFLAGS+=-O2
#CFLAGS+=-m64
CFLAGS+=-shared
LDFLAGS+=-mwindows
#LDFLAGS+=-L$(PWD)	# Not needed because there's a dependency to the delayload
										# import libraries and hence they will be linked without
										# searching.
EXTO=.o
EXTA=.a
EXTDLLA=.dll.a
CFLAGS+=-Wall
CFLAGS+=-Werror
#CPPFLAGS+='-I$(srcdir)/mingw32'
LDLIBS+=-lwinmm
#LDLIBS+=-loleaut32
LDLIBS+=-lshell32
LDLIBS+=-lavrt
LDLIBS+=-lksguid
LDLIBS+=-lole32
else # ] [
PATH+=:$(msvc)/bin
CC=cl
#LIB=lib -nologo -wx
LIB=lib -nologo
EXTO=.obj
EXTA=.lib
EXTDLLA=.lib
CFLAGS+=-nologo
CFLAGS+=-O2
#CFLAGS+=-MT	# Causes the application to use the multithread, static
					# version of the run-time library. Defines _MT and causes
					# the compiler to place the library name LIBCMT.lib into
					# the .obj file so that the linker will use LIBCMT.lib to
					# resolve external symbols.
CFLAGS+=-MD
          # Causes the application to use the multithread-specific and
					# DLL-specific version of the run-time library. Defines _MT
					# and _DLL and causes the compiler to place the library name
					# MSVCRT.lib into the .obj file.
CPPFLAGS+=-D_USING_V110_SDK71_
CPPFLAGS+='-I$(mssdk)/include'
CPPFLAGS+='-I$(msvc)/include'
LDFLAGS+='-libpath:$(mssdk)/lib'
LDFLAGS+='-libpath:$(msvc)/lib'
LDFLAGS+='-delayload:libgtk-3-0.dll'
LDFLAGS+='-delayload:libgdk-3-0.dll'
LDFLAGS+='-delayload:libgobject-2.0-0.dll'
LDFLAGS+='-delayload:libglib-2.0-0.dll'
LDLIBS+=delayimp.lib
LDLIBS+=winmm.lib
LDLIBS+=shell32.lib
LDLIBS+=kernel32.lib
LDLIBS+=user32.lib
LDLIBS+=avrt.lib
LDLIBS+=ole32.lib
LDLIBS+=ksguid.lib
endif # ]

ifneq (,$(APPVER_MAJOR)) # [
CPPFLAGS+=-DYASAPI_MAJOR=$(APPVER_MAJOR)
endif # ]
ifneq (,$(APPVER_MINOR)) # [
CPPFLAGS+=-DYASAPI_MINOR=$(APPVER_MINOR)
endif # ]
ifneq (,$(APPVER_BUILD)) # [
CPPFLAGS+=-DYASAPI_BUILD=$(APPVER_BUILD)
endif # ]
ifneq (,$(APPVER_BETA)) # [
CPPFLAGS+=-DYASAPI_BETA=$(APPVER_BETA)
endif # ]
ifneq (,$(APPVER_DEBUG)) # [
CPPFLAGS+=-DYASAPI_DEBUG=$(APPVER_DEBUG)
endif # ]
CPPFLAGS+='-I$(srcdir)'
CPPFLAGS+='-I$(wasdk)'

GTK=gtk+-3.0
ifneq (,$(GTK)) # [
CPPFLAGS+=$(shell pkg-config --cflags-only-I $(GTK))
endif # ]

ifeq (ms,$(tools)) # [
yasapi_uuid$(EXTO): yasapi_uuid.c
	$(CC) $(CFLAGS) $(CPPFLAGS) -Tp $< -c -Fo$@
else # ] [
###############################################################################
ifneq (,$(GTK)) # [
CFLAGS+=$(shell pkg-config --cflags-only-other $(GTK))
#CPPFLAGS+=$(shell pkg-config --cflags-only-I $(GTK))
#LDFLAGS+=$(shell pkg-config --libs-only-L $(GTK))
#LDLIBS+=$(shell pkg-config --libs-only-l $(GTK))
endif # ]
endif # ]

###############################################################################
plugins=$(winamp)/Plugins
ALL+=$(app).dll
#ifneq (ms,$(tools)) # [
ALL+=x7
#x7: x7$(EXTO)
#ALL+=x6
ALL+=x5
ALL+=x4
ALL+=x3
ALL+=x2
ALL+=x1
#endif # ]
#$(info $(ALL))
all: $(ALL)
#ifneq (ms,$(tools)) # [
#install: gtk-installed.stamp
#endif # ]
install: installed.stamp
installed.stamp: $(app).dll
	mkdir -p '$(plugins)'
	cp -p $< '$(plugins)/$(app).dll'
	touch $@
#gtk-installed.stamp: gtk-3.0-rt.stamp
#	mkdir -p '$(plugins)'
#	rsync -av ./gtk-3.0/ '$(plugins)/gtk-3.0/'
#	touch $@
uninstall:
	rm -f $< '$(plugins)/$(app).dll'
release: all
ifeq (,$(APPVER_DEBUG)) # [
relpfx=$(reldir)/$(APPVER)/$(app)-$(APPVER)
else # ] [
relpfx=$(reldir)/$(APPVER)/debug$(APPVER_DEBUG)/$(app)-$(APPVER)-debug$(APPVER_DEBUG)
endif # ]
ifeq (ms,$(tools)) # [
relpfx-tools=$(relpfx)-msc
else # ] [
relpfx-tools=$(relpfx)-gcc
endif # ]
release-gtk: release
release-gtk: $(relpfx-tools)-gtk-bundle.exe
$(relpfx-tools)-gtk-bundle.exe: ./$(app)-$(APPVER)-gtk-bundle.nsi
	mkdir -p $(@D)
	'$(nsis)/makensis.exe' $<
ifeq (,$(APPVER_DEBUG)) # [
release: $(reldir)/$(APPVER)/README
else # ] [
release: $(reldir)/$(APPVER)/debug$(APPVER_DEBUG)/README
endif # ]
release: $(relpfx)-src.tar.gz
release: $(relpfx-tools).7z
release: $(relpfx-tools).exe
$(relpfx-tools).exe: ./$(app)-$(APPVER).nsi $(relpfx-tools).7z
	mkdir -p $(@D)
	'$(nsis)/makensis.exe' $<
ifeq (ms,$(tools)) # [
nsi-opts='-DYASAPI_VERSION=$(APPVER) (msc)'
else # ] [
nsi-opts='-DYASAPI_VERSION=$(APPVER) (gcc)'
endif # ]
relpfx-tools-win1=$(shell cygpath -m $(relpfx-tools))
# For whatever reason "cygpath" may add some extra ".exe" extension we've
# to remove.
relpfx-tools-win2=$(patsubst %.exe,%,$(relpfx-tools-win1))
nsi-opts+='-DYASAPI_RELPFX=$(relpfx-tools-win2)'
nsi-opts+='-DYASAPI_APP=$(app)'
$(app)-$(APPVER).nsi: $(srcdir)/$(app).nsi ./Makefile
	m4 $(nsi-opts) $< > $@
nsi-opts-gtk=$(nsi-opts)
nsi-opts-gtk+='-DYASAPI_GTK=$(shell cygpath -w $(gtk))'
$(app)-$(APPVER)-gtk-bundle.nsi: $(srcdir)/$(app)-gtk-bundle.nsi ./Makefile
	m4 $(nsi-opts-gtk) $< > $@
$(relpfx-tools).7z: $(relpfx)-src.tar.gz
$(relpfx-tools).7z: $(app).dll
	mkdir -p $(@D)
	rm -f $@
	7za a $@ $<
	sleep 1
$(relpfx)-src.tar.gz: $(reldir)/$(APPVER)/README
$(relpfx)-src.tar.gz: $(shell find $(srcdir) -type f)
	mkdir -p $(@D)
	tar cfvz $@ -C $(srcdir) ./ '--transform=s,./,./$(app)-$(APPVER)/g,'
	sleep 1
readme-opts+='-DYASAPI_APP=$(app)'
readme-opts+='-DYASAPI_VERSION=$(APPVER)'
ifeq (,$(APPVER_DEBUG)) # [
$(reldir)/$(APPVER)/README: $(srcdir)/README
else # ] [
$(reldir)/$(APPVER)/debug$(APPVER_DEBUG)/README: $(srcdir)/README
endif # ]
	mkdir -p $(@D)
	m4 $(readme-opts) $< > $@
	sleep 1
ifneq (x,x$(RELEASE-ACTIVATE)) # [
#install release: gtk-3.0-rt.stamp
release: gtk-3.0-rt.stamp
gtk-3.0-rt.stamp: gtk-3.0/bin/gspawn-win32-helper-console.exe
gtk-3.0-rt.stamp: gtk-3.0/bin/gspawn-win32-helper.exe
gtk-3.0-rt.stamp: gtk-3.0/bin/libgtk-3-0.dll
gtk-3.0-rt.stamp: gtk-3.0/bin/libgdk-3-0.dll
gtk-3.0-rt.stamp: gtk-3.0/bin/libgcc_s_dw2-1.dll
gtk-3.0-rt.stamp: gtk-3.0/bin/libwinpthread-1.dll
gtk-3.0-rt.stamp: gtk-3.0/bin/libatk-1.0-0.dll
gtk-3.0-rt.stamp: gtk-3.0/bin/libcairo-gobject-2.dll
gtk-3.0-rt.stamp: gtk-3.0/bin/libcairo-2.dll
gtk-3.0-rt.stamp: gtk-3.0/bin/libepoxy-0.dll
gtk-3.0-rt.stamp: gtk-3.0/bin/libgdk_pixbuf-2.0-0.dll
gtk-3.0-rt.stamp: gtk-3.0/bin/libgio-2.0-0.dll
gtk-3.0-rt.stamp: gtk-3.0/bin/libglib-2.0-0.dll
gtk-3.0-rt.stamp: gtk-3.0/bin/libgmodule-2.0-0.dll
gtk-3.0-rt.stamp: gtk-3.0/bin/libgobject-2.0-0.dll
gtk-3.0-rt.stamp: gtk-3.0/bin/libintl-8.dll
gtk-3.0-rt.stamp: gtk-3.0/bin/libpango-1.0-0.dll
gtk-3.0-rt.stamp: gtk-3.0/bin/libpangocairo-1.0-0.dll
gtk-3.0-rt.stamp: gtk-3.0/bin/libpangowin32-1.0-0.dll
gtk-3.0-rt.stamp: gtk-3.0/bin/libfontconfig-1.dll
gtk-3.0-rt.stamp: gtk-3.0/bin/libfreetype-6.dll
gtk-3.0-rt.stamp: gtk-3.0/bin/libpixman-1-0.dll
gtk-3.0-rt.stamp: gtk-3.0/bin/libpng16-16.dll
gtk-3.0-rt.stamp: gtk-3.0/bin/zlib1.dll
gtk-3.0-rt.stamp: gtk-3.0/bin/libexpat-1.dll
gtk-3.0-rt.stamp: gtk-3.0/bin/libiconv-2.dll
gtk-3.0-rt.stamp: gtk-3.0/bin/libbz2-1.dll
gtk-3.0-rt.stamp: gtk-3.0/bin/libharfbuzz-0.dll
gtk-3.0-rt.stamp: gtk-3.0/bin/libpcre-1.dll
gtk-3.0-rt.stamp: gtk-3.0/bin/libgraphite2.dll
gtk-3.0-rt.stamp: gtk-3.0/bin/libffi-6.dll
gtk-3.0-rt.stamp: gtk-3.0/bin/libpangoft2-1.0-0.dll
gtk-3.0-rt.stamp: gtk-3.0/bin/libstdc++-6.dll
gtk-3.0-rt.stamp: gtk-3.0/share/icons/hicolor
gtk-3.0-rt.stamp: gtk-3.0/share/icons/Adwaita
gtk-3.0-rt.stamp:
	touch $@
#release: gtk-3.0/ieshims.dll
endif # ]

###############################################################################
libname=yasapi
libsources=$(filter $(libname)_%.c,$(out_yasapi_srcs))
libobjects=$(patsubst %.c,%$(EXTO),$(libsources))
libmembers=$(patsubst %,lib$(libname)$(EXTA)(%),$(libobjects))
lib$(libname)$(EXTA): $(libmembers)

headers=$(filter %.h,$(out_yasapi_srcs))
headers+=$(srcdir)/version.mak
objects=$(patsubst %.c,%$(EXTO),$(filter %.c,$(out_yasapi_srcs)))
$(objects): $(headers)
.PRECIOUS: lib$(libname)$(EXTA)
.INTERMEDIATE: $(libobjects)

###############################################################################
$(app).dll: lib$(libname)$(EXTA)
x7: lib$(libname)$(EXTA)
$(ALL): libgtk-3-0$(EXTDLLA)
$(ALL): libgdk-3-0$(EXTDLLA)
$(ALL): libgobject-2.0-0$(EXTDLLA)
$(ALL): libglib-2.0-0$(EXTDLLA)
out_yasapi.dll: out_yasapi$(EXTO)

###############################################################################
ifeq (ms,$(tools)) # [
(%$(EXTO)): %$(EXTO)
	if test -e $@; then $(LIB) -out:$@ $@ $<; else $(LIB) -wx -out:$@ $<; fi
%$(EXTO): %.c
	$(CC) $(CFLAGS) $(CPPFLAGS) $< -c -Fo$@
endif # ]
.PRECIOUS: %.dll
ifeq (ms,$(tools)) # [
%: %$(EXTO)
	$(CC) $(CFLAGS) $(CPPFLAGS) -Fe$@.exe $^ -link $(LDFLAGS) $(LDLIBS)
%.dll: %$(EXTO)
	$(CC) $(CFLAGS) -LD $(CPPFLAGS) -Fe$@ $^ -link $(LDFLAGS) $(LDLIBS)
.PRECIOUS: %.lib
%$(EXTDLLA): %.def
	$(LIB) -machine:x86 -def:$< -out:$@
else # ] [
#yasapi_ring_conv.o: yasapi_ring_conv.c
#yasapi_ring_conv.c: $(srcdir)/yasapi_ring_conv.c
#	gcc $(CPPFLAGS) -E $< > $@
%.dll: %$(EXTO)
#  $(LINK.o) -shared $^ $(LOADLIBES) $(LDLIBS) -o $@
	$(CC) $(CFLAGS) $(CPPFLAGS) $^ $(LDFLAGS) $(LDLIBS) -o $@
.PRECIOUS: %.dll.a
%$(EXTDLLA): %.def
	dlltool -d $< --output-delaylib $@
endif # ]
.PRECIOUS: %.def
%.def:  /mingw32/bin/%.dll
	gendef $<
ifneq (x,x$(RELEASE-ACTIVATE)) # [
gtk-3.0/bin/%: /mingw32/bin/%
	mkdir -p $(@D)
	cp -p $< $(@D)
gtk-3.0/share/%: /mingw32/share/%
	mkdir -p $(@D)
	cp -rp $< $(@D)
endif # ]
FORCE:
