#
# mingw makefile generics
#

CC      = ${MINGWBASE}gcc
CXX     = ${MINGWBASE}g++
AR      = ${MINGWBASE}ar
OBJCOPY = ${MINGWBASE}objcopy
STRIP   = ${MINGWBASE}strip --strip-unneeded
WINDRES = ${MINGWBASE}windres
PEFIX   = ${pefix}

BINEXT  = .exe

DSOLDFLAGS = -shared
DSOPRE     =
DSOPOST    = .dll
DSORPATH   =
DSOSONAME  = -Wl,-soname,

# Use MSVCRT directly for stdio; it's faster
ARCH_CFLAGS   += -D__USE_MINGW_ANSI_STDIO=0
ARCH_CXXFLAGS += -D__USE_MINGW_ANSI_STDIO=0

# Fix MINGW's shared library fanaticism
ARCH_LDFLAGS += -static-libstdc++ -static-libgcc

# OpenGL must be linked to MZX
core_ldflags += -lopengl32

# Windows needs PNG to be statically linked (inc. zlib)
LIBPNG_LDFLAGS = $(shell libpng-config --static --ldflags)

ifeq (${BUILD_LIBSDL2},)
SDL_PREFIX := ${shell sdl-config --prefix}
SDL_DLL := SDL.dll
else
SDL_PREFIX := ${shell sdl2-config --prefix}
SDL_DLL := SDL2.dll
endif

ifdef DEBUG
ARCH_LDFLAGS += -mconsole
endif

pefix := arch/mingw/pefix${BINEXT}

-include arch/mingw/pefix.d

arch/mingw/pefix.o: arch/mingw/pefix.c
	$(if ${V},,@echo "  CC      " $<)
	${HOST_CC} -MD ${CFLAGS} -c $< -o $@

${pefix}: arch/mingw/pefix.o
	$(if ${V},,@echo "  LINK    " ${pefix})
	${HOST_CC} arch/mingw/pefix.o -o ${pefix}

all: ${pefix}

clean:
	${RM} -f ${pefix} arch/mingw/pefix.o arch/mingw/pefix.d

#
# Windows builds must copy $SDL_DLL
# For SDL 1.2 builds, we want directx.bat too
#

build: ${build}
ifdef CUSTOM_SDL_DLL_DIR
	${CP} ${CUSTOM_SDL_DLL_DIR}/${SDL_DLL} ${build}
else
	${CP} ${SDL_PREFIX}/bin/${SDL_DLL} ${build}
endif
	${PEFIX} ${build}/${SDL_DLL}
ifeq (${LIBSDL2},0)
	${CP} arch/mingw/directx.bat ${build}
endif
ifeq (${BUILD_UTILS},1)
	${CP} arch/mingw/checkres* ${build}/utils
endif
	@arch/manifest.sh ${build}

SUBARCH := ${shell echo ${SUBPLATFORM} | sed 's,windows-,,'}
include arch/zip.inc 
