##############################################################################
#
# Open Watcom makefile for SDL 1.3.xxx
#
# This makefile builds Win32 versions of the SDL library, only.
# It has been tested on Windows XP SP3, Professional versions.
#
# Author:	Marc Peter <macpete@gmx.de>
#
# Modification for SDL 1.3 / Open Watcom 1.8:
#           Daniele Forghieri   ow_sdl at digitalfantasy dot it
#
# Prerequisites:
#
#  Requires Open Watcom 1.8 or later. (See http://www.openwatcom.org)
#
#  The environment variable DXDIR must be set and contain the base directory
#  of a DirectX SDK. Tested with DX10 sdk available from the Microsoft site
#  (DXSDX_Aug09, 'd3d9.h' is required).
#
# 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=...)
#
##############################################################################

# programs
CC  = wcc386
CPP = wpp386
MTL = midl
RSC = wrc

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

# faster compile ?
!ifdef __LOADDLL__
!  loaddll wcc386   wccd386
!  loaddll wpp386   wppd386
!  loaddll wlink    wlinkd
!  loaddll wlib     wlibd
!endif

# where the source are located
SRCDIR  = ../src

# include dir, main one (SDL) and Directx only
INCDIRS = -I"../include" -I"$(%DXDIR)/include"

# defines
DEFINES = -DWIN32 -D_WINDOWS -DENABLE_DIRECTX -DNO_STDIO_REDIRECT -D_WIN32_WINNT=0x0400

# libraries
LIBS = kernel32.lib user32.lib winmm.lib opengl32.lib msimg32.lib

# compile flags (* = required)
#   -zq     Operate quietly
#   -6r     Pentium Pro, register call
#   -s      Remove Stack overflow
#   -w3     Warning level
#   -ei   * Force enum to be of size int
#   -j      Default char is unsigned
#   -fp6    Floating point code for Pentium pro
#   -fpi87  Inline 387  instructions
#   -bt=nt  Build target is nt
#   -bm     Multi thread
#   -ecc  * Default calling convention is __cdecl
#   -aa   * Permit non const inizializer
CFLAGS = $(DEFINES) $(INCDIRS) -zq -6r -s -w3 -ei -j -fp6 -fpi87 -bt=nt &
        -bm -ecc -aa

# Names of the test executables not working (yet)
TESTS_TO_CHECK =        &
    common              &
    testdraw2           &
    testatomic          &
    testerror           &
    testgl2             &
    testhread           &
    testintersections   &
    testlock            &
    testmultiaudio      &
    testnative          &
    testnativew32       &
    testnativex11       &
    testresample        &
    testsem             &
    testsprite2         &
    testwm2             &
    threadwin           &
    torturethread	&
    end_list

# Names of the test executables (working ...)
TESTS =                 &
    checkkeys           &
    graywin             &
    loopwave            &
    testalpha           &
    testaudioinfo       &
    testbitmap          &
    testblitspeed       &
    testcursor          &
    testdyngl           &
    testdyngles         &
    testfile            &
    testgamma           &
    testgl              &
    testgles            &
    testhaptic          &
    testiconv           &
    testime             &
    testjoystick        &
    testkeys            &
    testloadso          &
    testmmousetablet    &
    testoverlay         &
    testoverlay2        &
    testpalette         &
    testplatform        &
    testpower           &
    testsprite          &
    testtimer           &
    testver             &
    testvidinfo         &
    testwin             &
    testwm

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

!ifeq bld debug

OUTDIR	= Debug
DEFINES	+= -D_DEBUG
#   -od     Disable optimization
#   -d2     Debug info, full
CFLAGS  += -od -d2
#   All debug info, sym file, map file
LDOPTS  = DEBUG all OP symf map

!else

OUTDIR	= Release
DEFINES	+= -DNDEBUG
#   -d0     No debug info
#   -oaxt   Optimization!
CFLAGS  += -d0 -oaxt
#   map file
LDOPTS  = OP map

!endif

# Added linker options
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)

# SDLmain, needed for watcom ?
$(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 target
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

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

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

# dll copy, to test the tests ;)
copy_dll : .SYMBOLIC
    copy $(OUTDIR)\SDL.dll ..\test
!ifeq bld debug
    copy $(OUTDIR)\SDL.sym ..\test
!endif

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

# Tests!
$(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
