##############################################################################
#
# Open Watcom makefile for SDL 1.2.15
#
# This makefile builds Win32 versions of the SDL library, only.
# It has been tested on Windows XP SP2, Home and Professional versions.
#
# Author:	Marc Peter <macpete@gmx.de>
#
# Prerequisites:
#
#  Requires Open Watcom 1.4 or later. (See http://www.openwatcom.org)
#
#  The environment variable DXDIR must be set and contain the base directory
#  of a DirectX SDK. Tested were DX8 and the minimal DirectX 7 SDK available
#  from the Allegro download site.
#
# wmake targets are:
#
#  sdl    Builds the SDL library (default target)
#  clean  Cleans up files generated by previous runs of wmake
#  tests  Builds sdl and puts all test executables in the directory ../test
#
# Optional build modifiers:
#
#  build=debug   Choose Debug build instead of Release.
#
#  tgt=dll       Build DLL version of SDL instead of the static library.
#                NOTE: Client applications must be compiled using the -bm
#                      switch of Open Watcom.
#
# Examples:
#
#  Default build: Release SDL static library in subdirectory Release
#     wmake
#       or
#     wmake sdl
#
#  Default Release build with all test executables:
#     wmake tests
#
#  Release build of the library and two selcted tests (see TESTS macro):
#     wmake testdyngl testsem
#
#  Debug build of DLL library version and all tests:
#     wmake tests build=debug tgt=dll
#
#  Clean up Release build of DLL version and tests:
#     wmake clean tgt=dll
#     (Note: the target clean does NOT remove all possible build targets, only
#     the tests and the library directory selected with build=... and tgt=...)
#
##############################################################################

CC  = wcc386
CPP = wpp386
RSC = wrc
NASM= nasm

SRCDIR  = ../src
DXDIR  = dx-wine

INCDIRS = -I"$(DXDIR)/include" -I"../include"

#ENABLE_ASM=1

DEFINES = -D_WIN32_WINNT=0x0400
# -DNO_STDIO_REDIRECT
!ifdef ENABLE_ASM
DEFINES+= -DSDL_HERMES_BLITTERS=1 -DSDL_ASSEMBLY_ROUTINES=1
!endif

LIBS = kernel32.lib user32.lib winmm.lib


CFLAGS = -zq -5r -fp5 -fpi87 -bt=nt -bm -s -ei -j -w3 $(DEFINES) $(INCDIRS)
NASMFLAGS = -f win32 -I ../src/hermes/

#
# Names of the test executables
#
TESTS1 = checkkeys.exe graywin.exe loopwave.exe testalpha.exe testbitmap.exe testblitspeed.exe &
	testdyngl.exe testerror.exe testfile.exe testgamma.exe testhread.exe testiconv.exe &
	testkeys.exe testlock.exe testoverlay.exe testoverlay2.exe testpalette.exe testplatform.exe &
	testsem.exe testsprite.exe testtimer.exe testver.exe testvidinfo.exe testwin.exe testwm.exe &
	torturethread.exe testcdrom.exe testjoystick.exe threadwin.exe testcursor.exe
TESTS2 = testgl.exe
TESTS = $(TESTS1) $(TESTS2)

!ifndef build
bld = release
!else
bld = $(build)
!endif


!ifeq bld debug

OUTDIR	= Debug
DEFINES	+= -D_DEBUG
CFLAGS	+= -od -d2
LDOPTS	= DEBUG all OP symf

!else

OUTDIR	= Release
DEFINES	+= -DNDEBUG
CFLAGS	+= -d0 -oaxt
LDOPTS	=

!endif

LDOPTS += OP quiet LIBR {$(LIBS)}


!ifndef tgt
tgt = dll
!endif

!ifeq tgt dll
OUTDIR		= $+ $(OUTDIR)_DLL $-
CFLAGS_DLL_OPT	= -bd -DBUILD_DLL
LDOPTS_DLL_OPT	= initinstance
# override the DECLSPEC definition in begin_code.h,
# because we are using an exports file for the dll
# to remove the '_' prefix from the symbol names.
CFLAGS_DLL_OPT	+= -DDECLSPEC=
LDOPTS_DLL_OPT	+=  EXPORT=SDL.exports OPTION IMPF=sdl.lbc
COPY_DLL_OPT	= copy_dll
SDL_LIB_DEP	= $(OUTDIR)/SDL.dll
# must use the lbc file to create a proper import library (see above.)
MK_SDL_LIB	= wlib -q -b -n -c -pa -s -t -zld -ii -io $@ @sdl.lbc
!else
SDL_LIB_DEP	= $(OUTDIR)/sdl.lnk $(OBJS)
MK_SDL_LIB	= wlib -q -b -n $@ @$[@
!endif

!message Doing $(OUTDIR) build

.before
	@if not exist "$(OUTDIR)/$(NULL)" mkdir "$(OUTDIR)"

#
# default target: SDL libraries only (without tests)
#
sdl : .SYMBOLIC $(OUTDIR)/SDL.lib $(OUTDIR)/SDLmain.lib


#
# target to build all SDL tests
#
tests : .SYMBOLIC $(TESTS)
	@echo Test executables have been put into directory ../test

#
# include definition of object files
#
!include objdef.inc
!ifdef ENABLE_ASM
OBJS+= $(ASMOBJS)
!endif

COMPILE_C = $(CC) $(CFLAGS) -DBUILD_SDL -fo="$@" "$]@" $(CFLAGS_DLL_OPT)
COMPILE_ASM = $(NASM) $(NASMFLAGS) -o "$@" "$]@"


$(OUTDIR)/SDLmain.lib : $(OUTDIR)/SDL_win32_main.obj
	wlib -q -b -n $@ -+$(OUTDIR)/SDL_win32_main.obj

$(OUTDIR)/SDL_win32_main.obj : $(SRCDIR)/main/win32/SDL_win32_main.c
	$(CC) $(CFLAGS) -DBUILD_SDL -fo="$@" "$]@"


clean : .SYMBOLIC
	-@rm -f $(OUTDIR)/*.obj
	-@rm -f $(OUTDIR)/*.lib
	-@rm -f $(OUTDIR)/*.map
	-@rm -f $(OUTDIR)/*.lnk
	-@rm -f $(OUTDIR)/*.dll
	-@rm -rf $(OUTDIR)
	-@rm -f *.err *.lbc ../test/*.exe ../test/*.dll


$(OUTDIR)/SDL.lib : $(SDL_LIB_DEP)
	$(MK_SDL_LIB)

$(OUTDIR)/SDL.dll : $(OBJS)
	wlink NAM $@ SYS nt_dll $(LDOPTS_DLL_OPT) $(LDOPTS) FIL {$(OBJS)}

copy_dll : .SYMBOLIC
	-cp $(OUTDIR)/SDL.dll ../test

$(OUTDIR)/sdl.lnk : objdef.inc
	%create $^@
	@for %i in ($(OBJS)) do @%append $^@ +-%i

$(TESTS1) : .SYMBOLIC makefile sdl $(COPY_DLL_OPT)
	wcc386 $(CFLAGS) -fo="$(OUTDIR)/$*.obj" "../test/$*.c" -DHAVE_OPENGL -DSCREENSHOT
	wlink SYS nt NAM ../test/$@ $(LDOPTS) OP M=$(OUTDIR)/$*.map &
	      FIL $(OUTDIR)/$*.obj &
	      LIBF $(OUTDIR)/SDLmain.lib LIBR $(OUTDIR)/SDL.lib
$(TESTS2): .SYMBOLIC makefile sdl $(COPY_DLL_OPT)
	wcc386 $(CFLAGS) -fo="$(OUTDIR)/$*.obj" "../test/$*.c" -DHAVE_OPENGL -DSCREENSHOT
	wlink SYS nt NAM ../test/$@ $(LDOPTS) OP M=$(OUTDIR)/$*.map &
	      FIL $(OUTDIR)/$*.obj &
	      LIBF $(OUTDIR)/SDLmain.lib LIBR $(OUTDIR)/SDL.lib LIBR opengl32.lib

# vim: ts=8
