#----------------------------------------------------------------------------------------------
#   makefile to build MPXPLAY-FFmpeg program for Win32 console
# notes:
#   required tools and libs: 
#    MinGWw64 12.2.0 x86
#    JWasm : http://www.japheth.de/JWasm.html (v2.06e)
#    FFmpeg 8.x (built in c:\CPP\QT\5.6.3\qt_static_audio)
#----------------------------------------------------------------------------------------------

# project root defs
PROJECT_ROOTDIR = $(CURDIR)
PROJECT_LIBDIR = $(PROJECT_ROOTDIR)
BUILDDIR = $(PROJECT_ROOTDIR)/build
EXE_MPXPLAY = $(PROJECT_ROOTDIR)/mpxplayf.exe

# mingw tool/compiler defs
TOOLS_ROOT_DIR = c:/CPP
QTDIR = $(TOOLS_ROOT_DIR)/QT
QTVERSION = 5.6.3
TOOLVERSION = 12.2.0
TOOLDIR = $(QTDIR)/Tools/mingw1220_32
MINGW_INCDIRS = -I$(TOOLDIR)/include -I$(TOOLDIR)/i686-w64-mingw32/include
MINGW_LIBDIRS = -L$(TOOLDIR)/lib -L$(TOOLDIR)/i686-w64-mingw32/lib -L$(TOOLDIR)/lib/gcc/i686-w64-mingw32/$(TOOLVERSION)

BINDIR = $(TOOLDIR)/bin
AR = $(BINDIR)/ar
CC = $(BINDIR)/gcc
CXX = $(BINDIR)/g++
RANLIB = $(BINDIR)/ranlib
STRIP = $(BINDIR)/strip
MSYS_BIN_DIR = $(TOOLS_ROOT_DIR)/msys2_x64/usr/bin
RM = $(MSYS_BIN_DIR)/rm

DEL_LIB_OBJS = $(RM) $(addsuffix /*.o, $(dir $<))

ARFLAGS = rcs

# compiler options
MPX_DEFINES = -DMPXPLAY $(MAKE_CFLAGS) -D_WIN32_WINNT=0x0500
CFLAGS_BASE = -pipe -funsigned-char -I. $(MINGW_INCDIRS) -ffast-math $(MPX_DEFINES)
CFLAGS_OPTI = -s -Os -Wno-deprecated-declarations
CFLAGS = $(CFLAGS_BASE) $(CFLAGS_OPTI) -DINITGUID
LFLAGS = -static -lwinmm -ldsound
LFLAGS += $(MINGW_LIBDIRS)
#LFLAGS +=-Wl,-M -Wl,-Map=$(BUILDDIR)\mpxplay.map

#--- QT compiler/build defs -------------------------------------------------
QT_DIR_ROOT = $(QTDIR)/$(QTVERSION)/qt_static_audio
QT_DIR_H = $(QT_DIR_ROOT)/include
QT_DIR_LIB = $(QT_DIR_ROOT)/lib

CXXFLAGS = -std=c++11 -frtti $(CFLAGS)
CFLAGS += -I'$(QT_DIR_H)'
LFLAGS += -L$(QT_DIR_LIB) -DINITGUID
LFLAGS += -lavformat -lavcodec -lavutil -lswresample
LFLAGS += -lstrmiids -loleaut32 -lole32 -luuid
#LFLAGS += -Wl,-s -Wl,-subsystem,windows

#--- main lib---- ------------------------------------------------------------
LIB_MAIN = mpx_main.a
DIR_MAIN = $(PROJECT_ROOTDIR)
SRC_MAIN = MPXPLAY.C IN_FILE.C MPXINBUF.C
SRCS_MAIN = $(addprefix $(DIR_MAIN)/, $(notdir $(SRC_MAIN)))
OBJS_MAIN := $(SRCS_MAIN:.C=.o)
VPATH = $(DIR_MAIN)
LIB_ALL = $(LIB_MAIN)

#--- AU_CARDS lib ------------------------------------------------------------
LIB_AUCARDS = au_cards.a
DIR_AUCARDS = $(PROJECT_LIBDIR)/AU_CARDS
SRC_AUCARDS = AU_CARDS.C DMAIRQ.C SC_WAV.C SC_NULL.C SC_WINDS.C SC_WINWO.C
SRCS_AUCARDS = $(addprefix $(DIR_AUCARDS)/, $(notdir $(SRC_AUCARDS)))
OBJS_AUCARDS := $(SRCS_AUCARDS:.C=.o)
VPATH += $(DIR_AUCARDS)
LIB_ALL += $(LIB_AUCARDS)

#--- AU_MIXER lib -------------------------------------------------------------
LIB_AUMIXER = au_mixer.a
DIR_AUMIXER = $(PROJECT_LIBDIR)/AU_MIXER
SRC_AUMIXER = ANALISER.C AU_MIXER.C CUTSILEN.C CV_BITS.C CV_CHAN.C CV_FREQ.C FX_SURR.C FX_TONE.C MX_AVOL.C MX_CROSF.C MX_VOLUM.C
SRCS_AUMIXER = $(addprefix $(DIR_AUMIXER)/, $(notdir $(SRC_AUMIXER)))
OBJS_AUMIXER := $(SRCS_AUMIXER:.C=.o)
VPATH += $(DIR_AUMIXER)
LIB_ALL += $(LIB_AUMIXER)

#--- DISPLAY lib --------------------------------------------------------------
LIB_DISPLAY = display.a
DIR_DISPLAY = $(PROJECT_LIBDIR)/DISPLAY
SRC_DISPLAY = DISPLAY.C BUTTONS.C LCD.C TEXTWIN.C VISUALPI.C
SRCS_DISPLAY = $(addprefix $(DIR_DISPLAY)/, $(notdir $(SRC_DISPLAY)))
OBJS_DISPLAY := $(SRCS_DISPLAY:.C=.o)
VPATH += $(DIR_DISPLAY)
LIB_ALL += $(LIB_DISPLAY)

#--- CONTROL lib ---------------------------------------------------------------
LIB_CONTROL = control.a
DIR_CONTROL = $(PROJECT_LIBDIR)/CONTROL
SRC_CONTROL = CONTROL.C FASTLIST.C JOY.C KEYCTRL.C KEYGROUP.C MOUSE.C SERIAL.C STARTUP.C
SRCS_CONTROL = $(addprefix $(DIR_CONTROL)/, $(notdir $(SRC_CONTROL)))
OBJS_CONTROL := $(SRCS_CONTROL:.C=.o)
VPATH += $(DIR_CONTROL)
LIB_ALL += $(LIB_CONTROL)

#--- DECODERS lib --------------------------------------------------------------
LIB_DECODERS = decoders.a
DIR_DECODERS = $(PROJECT_LIBDIR)/DECODERS
SRC_DECODERS = DECODERS.C AD_PCM.C
SRCS_DECODERS = $(addprefix $(DIR_DECODERS)/, $(notdir $(SRC_DECODERS)))
OBJS_DECODERS := $(SRCS_DECODERS:.C=.o)
VPATH += $(DIR_DECODERS)
LIB_ALL += $(LIB_DECODERS)

#--- DEMUXERS lib --------------------------------------------------------------
LIB_DEMUXERS = demuxers.a
DIR_DEMUXERS = $(PROJECT_LIBDIR)/DEMUXERS
SRC_DEMUXERS = IN_ASF.C IN_MP4.C IN_OGG.C
SRCS_DEMUXERS = $(addprefix $(DIR_DEMUXERS)/, $(notdir $(SRC_DEMUXERS)))
OBJS_DEMUXERS := $(SRCS_DEMUXERS:.C=.o)
VPATH += $(DIR_DEMUXERS)
LIB_ALL += $(LIB_DEMUXERS)

#--- DEPARSER lib --------------------------------------------------------------
LIB_DEPARSER = deparser.a
DIR_DEPARSER = $(PROJECT_LIBDIR)/DEPARSER
SRC_DEPARSER = TAGGING.C IN_FLAC.C IN_FMPEG.C
SRC_DEPARSER += FFMPADEC.C FFMPDMUX.C FFMPEPG.C FFMPFILE.C FFMPGDEC.C FFMPLANG.C FFMPQUEU.C FFMPPARS.C FFMPSTRM.C
SRCS_DEPARSER = $(addprefix $(DIR_DEPARSER)/, $(notdir $(SRC_DEPARSER)))
OBJS_DEPARSER := $(SRCS_DEPARSER:.C=.o)
VPATH += $(DIR_DEPARSER)
LIB_ALL += $(LIB_DEPARSER)
LIB_ALL += $(LIB_DEMUXERS)

#--- DISKDRIV lib --------------------------------------------------------------
LIB_DISKDRIV = diskdriv.a
DIR_DISKDRIV = $(PROJECT_LIBDIR)/DISKDRIV
SRC_DISKDRIV_C = DISKDRIV.C DRV_CD.C DRV_HD.C DRV_FTP.C DRV_HTTP.C DRV_ICE.C DRV_DTV.C CD_ASPI.C
SRC_DISKDRIV_C += DTVDBASE.C DTVSCAN.C OPEN_FTP.C TCPCOMON.C
SRC_DISKDRIV_CPP = DTV_BDA.CPP
SRCS_DISKDRIV_C = $(addprefix $(DIR_DISKDRIV)/, $(notdir $(SRC_DISKDRIV_C)))
SRCS_DISKDRIV_CPP = $(addprefix $(DIR_DISKDRIV)/, $(notdir $(SRC_DISKDRIV_CPP)))
OBJS_DISKDRIV := $(SRCS_DISKDRIV_C:.C=.o)
OBJS_DISKDRIV_CPP := $(SRCS_DISKDRIV_CPP:.CPP=.o)
VPATH += $(DIR_DISKDRIV)
LIB_ALL += $(LIB_DISKDRIV)

#--- NEWFUNC lib --------------------------------------------------------------
LIB_NEWFUNC = newfunc.a
DIR_NEWFUNC = $(PROJECT_LIBDIR)/NEWFUNC
SRC_NEWFUNC = NEWFUNC.C BITSTRM.C CPU.C DLL_LOAD.C DPMI.C DRIVEHND.C ERRORHND.C FILEHAND.C FPU.C
SRC_NEWFUNC += KEYBOARD.C MEMORY.C MIXED.C STRING.C TEXTDISP.C TIME.C THREADS.C TIMER.C
SRCS_NEWFUNC = $(addprefix $(DIR_NEWFUNC)/, $(notdir $(SRC_NEWFUNC)))
OBJS_NEWFUNC := $(SRCS_NEWFUNC:.C=.o)
VPATH += $(DIR_NEWFUNC)
LIB_ALL += $(LIB_NEWFUNC)

#--- PLAYLIST lib --------------------------------------------------------------
LIB_PLAYLIST = playlist.a
DIR_PLAYLIST = $(PROJECT_LIBDIR)/PLAYLIST
SRC_PLAYLIST = PLAYLIST.C CHKENTRY.C DISKFILE.C EDITLIST.C FILEINFO.C ID3LIST.C JUKEBOX.C LOADDIR.C
SRC_PLAYLIST += LOADLIST.C LOADSUB.C RANDLIST.C SAVELIST.C SKIPLIST.C SORTLIST.C TEXTCONV.C
SRCS_PLAYLIST = $(addprefix $(DIR_PLAYLIST)/, $(notdir $(SRC_PLAYLIST)))
OBJS_PLAYLIST := $(SRCS_PLAYLIST:.C=.o)
VPATH += $(DIR_PLAYLIST)
LIB_ALL += $(LIB_PLAYLIST)

#--- VIDEOOUT lib --------------------------------------------------------------
LIB_VIDEOOUT = videoout.a
DIR_VIDEOOUT = $(PROJECT_LIBDIR)/VIDEOOUT
SRC_VIDEOOUT = VIDEOOUT.C VO_VESA.C
SRCS_VIDEOOUT = $(addprefix $(DIR_VIDEOOUT)/, $(notdir $(SRC_VIDEOOUT)))
OBJS_VIDEOOUT := $(SRCS_VIDEOOUT:.C=.o)
VPATH += $(DIR_VIDEOOUT)
LIB_ALL += $(LIB_VIDEOOUT)

#################################################################################
#--- AD_VORBI lib --------------------------------------------------------------
LIB_AD_VORBI = ad_vorbi.a
DIR_AD_VORBI = $(DIR_DECODERS)/AD_VORBI
SRC_AD_VORBI  = BITWISE.C BLOCK.C CODEBOOK.C FLOOR0.C FLOOR1.C FRAMING.C INFO.C MAPPING0.C RES0.C SHARBOOK.C SYNTHESI.C WINDOW.C LSP.C MDCT_VOR.C
SRCS_AD_VORBI = $(addprefix $(DIR_AD_VORBI)/, $(notdir $(SRC_AD_VORBI)))
OBJS_AD_VORBI := $(SRCS_AD_VORBI:.C=.o)
VPATH += $(DIR_AD_VORBI)
LIB_ALL += $(LIB_AD_VORBI)


#################################################################################
LIBS_MPXPLAY := $(addprefix $(BUILDDIR)/, $(notdir $(LIB_ALL:.a=.a)))

all: $(EXE_MPXPLAY)

$(BUILDDIR)/$(LIB_MAIN): $(OBJS_MAIN)
	$(AR) $(ARFLAGS) $@ $(OBJS_MAIN)
	$(DEL_LIB_OBJS)

$(BUILDDIR)/$(LIB_AUCARDS): $(OBJS_AUCARDS)
	$(AR) $(ARFLAGS) $@ $(OBJS_AUCARDS)
	$(DEL_LIB_OBJS)

$(BUILDDIR)/$(LIB_AUMIXER): $(OBJS_AUMIXER)
	JWasm -coff -Fo$(DIR_AUMIXER)/ANALIASM.o $(DIR_AUMIXER)/ANALIASM.ASM
	$(AR) $(ARFLAGS) $@ $(DIR_AUMIXER)/ANALIASM.o $(OBJS_AUMIXER)
	$(DEL_LIB_OBJS)

$(BUILDDIR)/$(LIB_CONTROL): $(OBJS_CONTROL)
	$(AR) $(ARFLAGS) $@ $(OBJS_CONTROL)
	$(DEL_LIB_OBJS)

$(BUILDDIR)/$(LIB_DECODERS): $(OBJS_DECODERS)
	$(AR) $(ARFLAGS) $@ $(OBJS_DECODERS)
	$(DEL_LIB_OBJS)

$(BUILDDIR)/$(LIB_DEMUXERS): $(OBJS_DEMUXERS)
	$(AR) $(ARFLAGS) $@ $(OBJS_DEMUXERS)
	$(DEL_LIB_OBJS)

$(BUILDDIR)/$(LIB_DEPARSER): $(OBJS_DEPARSER)
	$(AR) $(ARFLAGS) $@ $(OBJS_DEPARSER)
	$(DEL_LIB_OBJS)

$(BUILDDIR)/$(LIB_DISKDRIV): $(OBJS_DISKDRIV) $(OBJS_DISKDRIV_CPP)
	$(AR) $(ARFLAGS) $@ $(OBJS_DISKDRIV) $(OBJS_DISKDRIV_CPP)
	$(DEL_LIB_OBJS)

$(BUILDDIR)/$(LIB_DISPLAY): $(OBJS_DISPLAY)
	$(AR) $(ARFLAGS) $@ $(OBJS_DISPLAY)
	$(DEL_LIB_OBJS)

$(BUILDDIR)/$(LIB_NEWFUNC): $(OBJS_NEWFUNC)
	$(AR) $(ARFLAGS) $@ $(OBJS_NEWFUNC)
	$(DEL_LIB_OBJS)

$(BUILDDIR)/$(LIB_PLAYLIST): $(OBJS_PLAYLIST)
	$(AR) $(ARFLAGS) $@ $(OBJS_PLAYLIST)
	$(DEL_LIB_OBJS)

$(BUILDDIR)/$(LIB_VIDEOOUT): $(OBJS_VIDEOOUT)
	$(AR) $(ARFLAGS) $@ $(OBJS_VIDEOOUT)
	$(DEL_LIB_OBJS)

#---------------------------------------------------------------------------------
$(BUILDDIR)/$(LIB_AD_VORBI): $(OBJS_AD_VORBI)
	$(AR) $(ARFLAGS) $@ $(OBJS_AD_VORBI)
	$(DEL_LIB_OBJS)

#------------------------------------------------------------------------------------
ranlib: $(LIBS_MPXPLAY)
	$(RANLIB) $(LIBS_MPXPLAY)

$(EXE_MPXPLAY): ranlib
	$(CXX) -o $@ $(LIBS_MPXPLAY) $(LFLAGS)
	$(STRIP) -s $(EXE_MPXPLAY)

%.o: %.c
	$(CC) -c -x c $(CFLAGS) -o $@ $<

%.o: %.C
	$(CC) -c -x c $(CFLAGS) -o $@ $<

%.o: %.CPP
	$(CXX) -c $(CXXFLAGS) -I'$(QT_DIR_H)' -o $@ $<

clean distclean:
	$(RM) $(BUILDDIR)/*.a
