# Makefile for targetting win32/win64
# Requires a native or cross MinGW or MinGW-w64 compiler-set
# and GNU make (gmake or mingw32-make or whatever)

ifeq ($(CROSS),)
CC=gcc
AS=as
AR=ar
RANLIB=ranlib
else
CC=$(CROSS)-gcc
AS=$(CROSS)-as
AR=$(CROSS)-ar
RANLIB=$(CROSS)-ranlib
endif
#RM=del
RM=rm -f

ARFLAGS = cr
CPPFLAGS=-DMIKMOD_BUILD
LDLIBS=

# SSE2 support (for x86 builds. for x86_64 it doesn't hurt.)
# SIMD code is unstable at present: *not* recommended...
SSE2FLAGS=-msse2

# support for WinMM sound output:
LDLIBS+=-lwinmm
CPPFLAGS+=-DDRV_WIN

# support for Direct Sound output:
LDLIBS+=-ldsound
CPPFLAGS+=-DDRV_DS

# disable support for module depackers
#CPPFLAGS+=-DNO_DEPACKERS
# disable the high quality mixer (build only with the standart mixer)
#CPPFLAGS+=-DNO_HQMIXER

# support for output through OpenAL (see drv_openal.c
# for notes about OpenAL header location issues.)
#LDLIBS+=-lOpenAL32
#CPPFLAGS+=-DDRV_OPENAL

# support for output through SDL:
# NO_SDL_CONFIG define is for including SDL.h as <SDL/SDL.h>.
#LDLIBS+=-lSDL
#CPPFLAGS+=-DDRV_SDL -DNO_SDL_CONFIG

# support for XAudio2 output: need dxsdk / xaudio2.h (xaudio2_7)
#LDLIBS+=-lole32
#CPPFLAGS+=-DDRV_XAUDIO2
# the following are *additionally* needed with xaudio2_8 for Windows8:
#LDLIBS+=-lxaudio2_8
#CPPFLAGS+=-DDRV_XAUDIO28

# support for aiff file output:
CPPFLAGS+=-DDRV_AIFF
# support for wav file output:
CPPFLAGS+=-DDRV_WAV
# support for output raw data to a file:
CPPFLAGS+=-DDRV_RAW

# support for output to stdout:
#CPPFLAGS+=-DDRV_STDOUT

# Compiler flags
CPPFLAGS+=-DHAVE_FCNTL_H -DHAVE_MALLOC_H -DHAVE_LIMITS_H
CFLAGS=-O2 -Wall
CFLAGS+=-ffast-math
#CFLAGS+=-g
LDFLAGS=

COMPILE=$(CC) -c $(CFLAGS) $(CPPFLAGS) -I../win32 -I../include

.SUFFIXES:
.SUFFIXES: .o .c

DLL_VER=3

CPPFLAGS_DLL=-DDLL_EXPORT=1
CPPFLAGS_STATIC=-DMIKMOD_STATIC=1

LIBNAME=libmikmod.dll.a
LIBSTATIC=libmikmod.a
DLLNAME=libmikmod-$(DLL_VER).dll
DEFNAME=libmikmod-$(DLL_VER).def

LIBS=$(LIBNAME) $(DLLNAME)

OBJ=drv_ds.o drv_xaudio2.o drv_win.o drv_openal.o drv_sdl.o \
    drv_raw.o drv_aiff.o drv_wav.o \
    drv_nos.o drv_stdout.o \
    load_669.o load_amf.o load_asy.o load_dsm.o load_far.o load_gdm.o load_gt2.o \
    load_it.o load_imf.o load_m15.o load_med.o load_mod.o load_mtm.o load_okt.o \
    load_s3m.o load_stm.o load_stx.o load_ult.o load_umx.o load_uni.o load_xm.o \
    mmalloc.o mmerror.o mmio.o mdriver.o mdreg.o \
    mmcmp.o pp20.o s404.o xpk.o strcasecmp.o \
    mloader.o mlreg.o mlutil.o mplayer.o munitrk.o mwav.o \
    npertab.o sloader.o virtch.o virtch2.o virtch_common.o
#   mdulaw.o

help:
	@echo ""
	@echo "Valid targets:"
	@echo ""
	@echo "  $(MAKE) dll        : build dll and import library"
	@echo "  $(MAKE) static     : build static library"
	@echo ""
	@echo "Same as above, but with SIMD (SSE2) mixer optimizations:"
	@echo "(SIMD code is unstable at present: *not* recommended...)"
	@echo ""
	@echo "  $(MAKE) dll-sse2   : dll and import lib with sse2 support"
	@echo "  $(MAKE) static-sse2: static library with sse2 support"
	@echo ""

clean:
	$(RM) $(DLLNAME) $(LIBNAME) $(LIBSTATIC) $(DEFNAME) *.o

dll-sse2: CFLAGS+=$(SSE2FLAGS)
dll-sse2: CPPFLAGS+=-DMIKMOD_SIMD
dll-sse2: dll

static-sse2: CFLAGS+=$(SSE2FLAGS)
static-sse2: CPPFLAGS+=-DMIKMOD_SIMD
static-sse2: static

dll: $(DLLNAME) $(LIBNAME)
static: $(LIBSTATIC)

$(DLLNAME): CPPFLAGS+=$(CPPFLAGS_DLL)
$(LIBNAME): $(DLLNAME)
$(DLLNAME): $(OBJ)
	$(CC) -shared -o $(DLLNAME) $(OBJ) $(LDFLAGS) $(LDLIBS) \
		-Wl,--enable-auto-image-base -Wl,--no-undefined \
		-Xlinker --output-def -Xlinker $(DEFNAME) \
		-Xlinker --out-implib -Xlinker $(LIBNAME)

$(LIBSTATIC): CPPFLAGS+=$(CPPFLAGS_STATIC)
$(LIBSTATIC): $(OBJ)
	$(AR) $(ARFLAGS) $@ $(OBJ)
	$(RANLIB) $@

HEADER_DEPS=../include/mikmod.h ../include/mikmod_internals.h ../include/mikmod_ctype.h
drv_ds.o: ../drivers/drv_ds.c $(HEADER_DEPS)
	$(COMPILE) ../drivers/drv_ds.c -o drv_ds.o
drv_nos.o: ../drivers/drv_nos.c $(HEADER_DEPS)
	$(COMPILE) ../drivers/drv_nos.c -o drv_nos.o
drv_openal.o: ../drivers/drv_openal.c $(HEADER_DEPS)
	$(COMPILE) ../drivers/drv_openal.c -o drv_openal.o
drv_sdl.o: ../drivers/drv_sdl.c $(HEADER_DEPS)
	$(COMPILE) ../drivers/drv_sdl.c -o drv_sdl.o
drv_raw.o: ../drivers/drv_raw.c $(HEADER_DEPS)
	$(COMPILE) ../drivers/drv_raw.c -o drv_raw.o
drv_stdout.o: ../drivers/drv_stdout.c $(HEADER_DEPS)
	$(COMPILE) ../drivers/drv_stdout.c -o drv_stdout.o
drv_aiff.o: ../drivers/drv_aiff.c $(HEADER_DEPS)
	$(COMPILE) ../drivers/drv_aiff.c -o drv_aiff.o
drv_wav.o: ../drivers/drv_wav.c $(HEADER_DEPS)
	$(COMPILE) ../drivers/drv_wav.c -o drv_wav.o
drv_win.o: ../drivers/drv_win.c $(HEADER_DEPS)
	$(COMPILE) ../drivers/drv_win.c -o drv_win.o
drv_xaudio2.o: ../drivers/drv_xaudio2.c $(HEADER_DEPS)
	$(COMPILE) ../drivers/drv_xaudio2.c -o drv_xaudio2.o
load_669.o: ../loaders/load_669.c $(HEADER_DEPS)
	$(COMPILE) ../loaders/load_669.c -o load_669.o
load_amf.o: ../loaders/load_amf.c $(HEADER_DEPS)
	$(COMPILE) ../loaders/load_amf.c -o load_amf.o
load_asy.o: ../loaders/load_asy.c $(HEADER_DEPS)
	$(COMPILE) ../loaders/load_asy.c -o load_asy.o
load_dsm.o: ../loaders/load_dsm.c $(HEADER_DEPS)
	$(COMPILE) ../loaders/load_dsm.c -o load_dsm.o
load_far.o: ../loaders/load_far.c $(HEADER_DEPS)
	$(COMPILE) ../loaders/load_far.c -o load_far.o
load_gdm.o: ../loaders/load_gdm.c $(HEADER_DEPS)
	$(COMPILE) ../loaders/load_gdm.c -o load_gdm.o
load_gt2.o: ../loaders/load_gt2.c $(HEADER_DEPS)
	$(COMPILE) ../loaders/load_gt2.c -o load_gt2.o
load_it.o: ../loaders/load_it.c $(HEADER_DEPS)
	$(COMPILE) ../loaders/load_it.c -o load_it.o
load_imf.o: ../loaders/load_imf.c $(HEADER_DEPS)
	$(COMPILE) ../loaders/load_imf.c -o load_imf.o
load_m15.o: ../loaders/load_m15.c $(HEADER_DEPS)
	$(COMPILE) ../loaders/load_m15.c -o load_m15.o
load_med.o: ../loaders/load_med.c $(HEADER_DEPS)
	$(COMPILE) ../loaders/load_med.c -o load_med.o
load_mod.o: ../loaders/load_mod.c $(HEADER_DEPS)
	$(COMPILE) ../loaders/load_mod.c -o load_mod.o
load_mtm.o: ../loaders/load_mtm.c $(HEADER_DEPS)
	$(COMPILE) ../loaders/load_mtm.c -o load_mtm.o
load_okt.o: ../loaders/load_okt.c $(HEADER_DEPS)
	$(COMPILE) ../loaders/load_okt.c -o load_okt.o
load_s3m.o: ../loaders/load_s3m.c $(HEADER_DEPS)
	$(COMPILE) ../loaders/load_s3m.c -o load_s3m.o
load_stm.o: ../loaders/load_stm.c $(HEADER_DEPS)
	$(COMPILE) ../loaders/load_stm.c -o load_stm.o
load_stx.o: ../loaders/load_stx.c $(HEADER_DEPS)
	$(COMPILE) ../loaders/load_stx.c -o load_stx.o
load_ult.o: ../loaders/load_ult.c $(HEADER_DEPS)
	$(COMPILE) ../loaders/load_ult.c -o load_ult.o
load_umx.o: ../loaders/load_umx.c $(HEADER_DEPS)
	$(COMPILE) ../loaders/load_umx.c -o load_umx.o
load_uni.o: ../loaders/load_uni.c $(HEADER_DEPS)
	$(COMPILE) ../loaders/load_uni.c -o load_uni.o
load_xm.o: ../loaders/load_xm.c $(HEADER_DEPS)
	$(COMPILE) ../loaders/load_xm.c -o load_xm.o
mmalloc.o: ../mmio/mmalloc.c $(HEADER_DEPS)
	$(COMPILE) ../mmio/mmalloc.c -o mmalloc.o
mmerror.o: ../mmio/mmerror.c $(HEADER_DEPS)
	$(COMPILE) ../mmio/mmerror.c -o mmerror.o
mmio.o: ../mmio/mmio.c $(HEADER_DEPS)
	$(COMPILE) ../mmio/mmio.c -o mmio.o
mmcmp.o: ../depackers/mmcmp.c $(HEADER_DEPS)
	$(COMPILE) ../depackers/mmcmp.c -o mmcmp.o
pp20.o: ../depackers/pp20.c $(HEADER_DEPS)
	$(COMPILE) ../depackers/pp20.c -o pp20.o
s404.o: ../depackers/s404.c $(HEADER_DEPS)
	$(COMPILE) ../depackers/s404.c -o s404.o
xpk.o: ../depackers/xpk.c $(HEADER_DEPS)
	$(COMPILE) ../depackers/xpk.c -o xpk.o
mdriver.o: ../playercode/mdriver.c $(HEADER_DEPS)
	$(COMPILE) ../playercode/mdriver.c -o mdriver.o
mdreg.o: ../playercode/mdreg.c $(HEADER_DEPS)
	$(COMPILE) ../playercode/mdreg.c -o mdreg.o
mdulaw.o: ../playercode/mdulaw.c $(HEADER_DEPS)
	$(COMPILE) ../playercode/mdulaw.c -o mdulaw.o
mloader.o: ../playercode/mloader.c $(HEADER_DEPS)
	$(COMPILE) ../playercode/mloader.c -o mloader.o
mlreg.o: ../playercode/mlreg.c $(HEADER_DEPS)
	$(COMPILE) ../playercode/mlreg.c -o mlreg.o
mlutil.o: ../playercode/mlutil.c $(HEADER_DEPS)
	$(COMPILE) ../playercode/mlutil.c -o mlutil.o
mplayer.o: ../playercode/mplayer.c $(HEADER_DEPS)
	$(COMPILE) ../playercode/mplayer.c -o mplayer.o
munitrk.o: ../playercode/munitrk.c $(HEADER_DEPS)
	$(COMPILE) ../playercode/munitrk.c -o munitrk.o
mwav.o: ../playercode/mwav.c $(HEADER_DEPS)
	$(COMPILE) ../playercode/mwav.c -o mwav.o
npertab.o: ../playercode/npertab.c $(HEADER_DEPS)
	$(COMPILE) ../playercode/npertab.c -o npertab.o
sloader.o: ../playercode/sloader.c $(HEADER_DEPS)
	$(COMPILE) ../playercode/sloader.c -o sloader.o
virtch.o: ../playercode/virtch.c ../playercode/virtch_common.c $(HEADER_DEPS)
	$(COMPILE) ../playercode/virtch.c -o virtch.o
virtch2.o: ../playercode/virtch2.c ../playercode/virtch_common.c $(HEADER_DEPS)
	$(COMPILE) ../playercode/virtch2.c -o virtch2.o
virtch_common.o: ../playercode/virtch_common.c $(HEADER_DEPS)
	$(COMPILE) ../playercode/virtch_common.c -o virtch_common.o
strcasecmp.o: ../posix/strcasecmp.c $(HEADER_DEPS)
	$(COMPILE) ../posix/strcasecmp.c -o strcasecmp.o
