##############################################################################
#
# Open Watcom makefile for SDL 1.2.10
#
# 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 and -br switches 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
MTL = midl
RSC = wrc


!ifndef %DXDIR
!  error DXDIR is not set! (Set it to the path of the DirectX SDK base dir)
!endif


!ifdef __LOADDLL__
!  loaddll wcc386   wccd386
!  loaddll wpp386   wppd386
!  loaddll wlink    wlink
!  loaddll wlib     wlibd
!endif

SRCDIR  = ../src

INCDIRS = &
	-I"$(SRCDIR)" -I"../include" -I"$(SRCDIR)/audio" -I"$(SRCDIR)/video" &
	-I"$(SRCDIR)/video/windx5" -I"$(SRCDIR)/video/wincommon" &
	-I"$(SRCDIR)/events" -I"$(SRCDIR)/joystick" -I"$(%DXDIR)/include" &
	-I"$(SRCDIR)/cdrom" -I"$(SRCDIR)/thread" -I"$(SRCDIR)/thread/win32" &
	-I"$(SRCDIR)/timer" -I"../include/SDL"

DEFINES = &
	-DWIN32 -D_WINDOWS -DENABLE_DIRECTX -DNO_STDIO_REDIRECT&
	-D_WIN32_WINNT=0x0400

LIBS = kernel32.lib user32.lib winmm.lib opengl32.lib


CFLAGS = -zq -6r -s $(DEFINES) $(INCDIRS) -zq -w3 -ei -j -fp6 -fpi87 -bt=nt -bm

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


!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)}


!ifeq tgt dll
OUTDIR		= $+ $(OUTDIR)_DLL $-
CFLAGS_DLL_OPT	= -bd -DBUILD_DLL
CFLAGS		+= -br
LDOPTS_DLL_OPT	= initinstance
COPY_DLL_OPT	= copy_dll
SDL_LIB_DEP	= $(OUTDIR)/SDL.dll
MK_SDL_LIB	= wlib -q -b -n $@ +$[@
!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

COMPILE_C = $(CC) $(CFLAGS) -DBUILD_SDL -fo="$@" "$]@" $(CFLAGS_DLL_OPT)


$(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="$@" "$]@"

#.c.obj :
#    $(OMPILE_C)


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


$(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
	-cp $(OUTDIR)/SDL.sym ../test

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

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

# vim: ts=8
