# EDuke32 Makefile for Microsoft NMake

obj=obj

ENGINE_ROOT=build
ENGINE_SRC=$(ENGINE_ROOT)\src
ENGINE_INC=$(ENGINE_ROOT)\include
ENGINE_OBJ=$(ENGINE_ROOT)\$(obj)

DUKE3D_SRC=source
DUKE3D_OBJ=$(DUKE3D_SRC)\$(obj)
DUKE3D_INC=$(DUKE3D_SRC)
DUKE3D_RSRC=rsrc

MACT_ROOT=$(DUKE3D_SRC)\jmact
MACT_SRC=$(MACT_ROOT)
MACT_INC=$(MACT_ROOT)
MACT_OBJ=$(MACT_ROOT)\$(obj)

AUDIOLIB_ROOT=$(DUKE3D_SRC)\jaudiolib
AUDIOLIB_OBJ=$(AUDIOLIB_ROOT)\$(obj)
AUDIOLIB_INC=$(AUDIOLIB_ROOT)\include
AUDIOLIB_SRC=$(AUDIOLIB_ROOT)\src

ENET_ROOT=$(DUKE3D_SRC)\enet
ENET_OBJ=$(ENET_ROOT)\$(obj)
ENET_INC=$(ENET_ROOT)\include
ENET_SRC=$(ENET_ROOT)\src

o=obj
res=res
asm=masm




CPLUSPLUS=1

!ifndef WINBITS
WINBITS=32
!endif

WINLIB=\$(WINBITS)

!if ($(WINBITS)==32)
WINMACHINE=/MACHINE:X86
!elseif ($(WINBITS)==64)
WINMACHINE=/MACHINE:X64
!endif

# the WDK allows us to link against msvcrt.dll instead of msvcrxxx.dll
# this path should match build\Makefile.msvc
# WDKROOT="H:\WinDDK\7600.16385.1"
PLATFORM=platform\Windows
AUDIOINC=source\jaudiolib\third-party\common
AUDIOPLATFORM=source\jaudiolib\third-party\Windows

!ifndef RENDERTYPE
RENDERTYPE=WIN
!endif
!ifndef MIXERTYPE
MIXERTYPE=WIN
!endif

!ifdef DEBUG
# debugging options
flags_cl=/Od /Zi # /analyze
flags_link=/DEBUG
!else
# release options
flags_cl=/O2 /GL /MP # /I$(WDKROOT)\inc\crt /I$(WDKROOT)\inc\api
!if ($(WINBITS)!=64)
flags_cl=$(flags_cl) /arch:SSE
!endif
flags_link=/RELEASE /LTCG # /LIBPATH:$(WDKROOT)\lib\wxp\i386 /LIBPATH:$(WDKROOT)\lib\Crt\i386
!endif

ENGINEOPTS=/DUSE_OPENGL /DPOLYMER /DUSE_LIBPNG /DUSE_LIBVPX /DHAVE_VORBIS #/DHAVE_FLAC

!ifdef CPLUSPLUS
ENGINEOPTS=$(ENGINEOPTS) /TP
!endif

!if ($(WINBITS)==64)
NOASM=1
!endif

CC=cl
AS=ml
LINK=link /nologo /opt:ref 
MT=mt
CFLAGS= /MT /J /nologo $(flags_cl)  \
	/I$(DUKE3D_INC) /I$(ENGINE_INC)\msvc /I$(ENGINE_INC) /I$(MACT_ROOT) /I$(AUDIOLIB_ROOT)\include /I$(ENET_ROOT)\include \
 	/W2 $(ENGINEOPTS) \
 	/I$(PLATFORM)\include /I$(AUDIOINC)\include /DRENDERTYPE$(RENDERTYPE)=1 /DMIXERTYPE$(MIXERTYPE)=1 /DSDL_FRAMEWORK /DSDL_TARGET=2 

ENET_CFLAGS=/I$(ENET_INC) /I$(ENET_SRC)
AUDIOLIB_CFLAGS=/I$(AUDIOLIB_INC) /I$(AUDIOLIB_SRC) 

LIBS=user32.lib gdi32.lib shell32.lib winmm.lib ws2_32.lib comctl32.lib shlwapi.lib oleaut32.lib ole32.lib imm32.lib version.lib \
     libogg.a libvorbis.a libvorbisfile.a libvpx.a libpng_mini.a libz_mini.a \
     dxguid.lib dsound.lib advapi32.lib libcompat-to-msvc.a

!if ("$(RENDERTYPE)"=="SDL")
LIBS=libSDL2main.a libSDL2.a libSDL2_mixer.a $(LIBS)
!endif

LIBS=/NODEFAULTLIB:glu32.lib /NODEFAULTLIB:msvcrt.lib /NODEFAULTLIB:msvcrtd.lib /NODEFAULTLIB:libcmt.lib \
    /NODEFAULTLIB:libcmtd.lib $(LIBS)

# NOASM     When defined, uses C instead of assembly code
!ifdef NOASM
CFLAGS=$(CFLAGS) /DNOASM
!endif

ASFLAGS=/nologo /coff /c
EXESUFFIX=.exe
!ifdef DEBUG
CFLAGS=$(CFLAGS) /DDEBUGGINGAIDS /D "_CRT_SECURE_NO_DEPRECATE"
LIBS=$(LIBS)  msvcrtd.lib
!else
# comment msvcrt_winxp.obj if not using the WDK
LIBS=$(LIBS)  msvcrt.lib # msvcrt_winxp.obj
!endif

ENGINE_OBJS= \
!ifdef NOASM
        $(ENGINE_OBJ)\a-c.$o \
!else
        $(ENGINE_OBJ)\a.$o \
!endif
	$(ENGINE_OBJ)\baselayer.$o \
	$(ENGINE_OBJ)\cache1d.$o \
	$(ENGINE_OBJ)\common.$o \
	$(ENGINE_OBJ)\compat.$o \
	$(ENGINE_OBJ)\crc32.$o \
	$(ENGINE_OBJ)\defs.$o \
	$(ENGINE_OBJ)\colmatch.$o \
	$(ENGINE_OBJ)\engine.$o \
        $(ENGINE_OBJ)\glbuild.$o \
        $(ENGINE_OBJ)\texcache.$o \
        $(ENGINE_OBJ)\kplib.$o \
        $(ENGINE_OBJ)\hightile.$o \
	$(ENGINE_OBJ)\polymost.$o \
        $(ENGINE_OBJ)\polymer.$o \
        $(ENGINE_OBJ)\mdsprite.$o \
        $(ENGINE_OBJ)\voxmodel.$o \
	$(ENGINE_OBJ)\dxtfilter.$o \
	$(ENGINE_OBJ)\textfont.$o \
	$(ENGINE_OBJ)\smalltextfont.$o \
	$(ENGINE_OBJ)\lz4.$o \
	$(ENGINE_OBJ)\md4.$o \
	$(ENGINE_OBJ)\mmulti_null.$o \
	$(ENGINE_OBJ)\osd.$o \
	$(ENGINE_OBJ)\pragmas.$o \
	$(ENGINE_OBJ)\scriptfile.$o \
	$(ENGINE_OBJ)\mutex.$o \
	$(ENGINE_OBJ)\winbits.$o \
	$(ENGINE_OBJ)\xxhash.$o

ENGINE_EDITOR_OBJS=$(ENGINE_OBJ)\build.$o \
	$(ENGINE_OBJ)\startwin.editor.$o \
	$(ENGINE_OBJ)\config.$o

ENET_OBJS=$(ENET_OBJ)\callbacks.$o \
	$(ENET_OBJ)\host.$o \
	$(ENET_OBJ)\list.$o \
	$(ENET_OBJ)\packet.$o \
	$(ENET_OBJ)\peer.$o \
	$(ENET_OBJ)\protocol.$o \
	$(ENET_OBJ)\win32.$o \
	$(ENET_OBJ)\compress.$o

AUDIOLIB_OBJS=$(AUDIOLIB_OBJ)\drivers.$o \
	$(AUDIOLIB_OBJ)\fx_man.$o \
	$(AUDIOLIB_OBJ)\multivoc.$o \
	$(AUDIOLIB_OBJ)\mix.$o \
	$(AUDIOLIB_OBJ)\mixst.$o \
	$(AUDIOLIB_OBJ)\pitch.$o \
	$(AUDIOLIB_OBJ)\formats.$o \
	$(AUDIOLIB_OBJ)\vorbis.$o \
	$(AUDIOLIB_OBJ)\flac.$o \
	$(AUDIOLIB_OBJ)\xa.$o \
	$(AUDIOLIB_OBJ)\driver_nosound.$o

MACT_OBJS=$(DUKE3D_OBJ)\file_lib.$o \
	$(DUKE3D_OBJ)\control.$o \
	$(DUKE3D_OBJ)\keyboard.$o \
	$(DUKE3D_OBJ)\mouse.$o \
	$(DUKE3D_OBJ)\joystick.$o \
	$(DUKE3D_OBJ)\scriplib.$o
 
DUKE3D_OBJS=$(DUKE3D_OBJ)\game.$o \
	$(DUKE3D_OBJ)\actors.$o \
	$(DUKE3D_OBJ)\anim.$o \
	$(DUKE3D_OBJ)\animsounds.$o \
	$(DUKE3D_OBJ)\animvpx.$o \
        $(DUKE3D_OBJ)\cheats.$o \
        $(DUKE3D_OBJ)\sbar.$o \
        $(DUKE3D_OBJ)\screentext.$o \
	$(DUKE3D_OBJ)\common.$o \
	$(DUKE3D_OBJ)\demo.$o \
	$(DUKE3D_OBJ)\gamedef.$o \
	$(DUKE3D_OBJ)\gameexec.$o \
	$(DUKE3D_OBJ)\gamevars.$o \
	$(DUKE3D_OBJ)\global.$o \
	$(DUKE3D_OBJ)\input.$o \
	$(DUKE3D_OBJ)\menus.$o \
	$(DUKE3D_OBJ)\namesdyn.$o \
    $(DUKE3D_OBJ)\net.$o \
	$(DUKE3D_OBJ)\player.$o \
	$(DUKE3D_OBJ)\premap.$o \
	$(DUKE3D_OBJ)\savegame.$o \
	$(DUKE3D_OBJ)\sector.$o \
	$(DUKE3D_OBJ)\rev.$o \
	$(DUKE3D_OBJ)\rts.$o \
	$(DUKE3D_OBJ)\config.$o \
	$(DUKE3D_OBJ)\animlib.$o\
	$(DUKE3D_OBJ)\osdfuncs.$o \
	$(DUKE3D_OBJ)\osdcmds.$o \
	$(DUKE3D_OBJ)\grpscan.$o \
	$(DUKE3D_OBJ)\winbits.$o \
	$(DUKE3D_OBJ)\gameres.$(res) \
	$(DUKE3D_OBJ)\startwin.game.$o \
	$(MACT_OBJS) \
	$(DUKE3D_OBJ)\sounds.$o \
	$(DUKE3D_OBJ)\soundsdyn.$o \
!ifdef DEBUG
	$(DUKE3D_OBJ)\mdump.$o
!endif

DUKE3D_EDITOR_OBJS=$(DUKE3D_OBJ)\astub.$o \
	$(DUKE3D_OBJ)\common.$o \
	$(DUKE3D_OBJ)\grpscan.$o \
	$(DUKE3D_OBJ)\m32common.$o \
	$(DUKE3D_OBJ)\m32def.$o \
	$(DUKE3D_OBJ)\m32vars.$o \
	$(DUKE3D_OBJ)\m32exec.$o \
	$(DUKE3D_OBJ)\sounds_mapster32.$o \
	$(DUKE3D_OBJ)\rev.$o \
	$(DUKE3D_OBJ)\buildres.$(res) \
!ifdef DEBUG
	$(DUKE3D_OBJ)\mdump.$o
!endif

!if ("$(RENDERTYPE)"=="WIN")
ENGINE_OBJS=$(ENGINE_OBJS) $(ENGINE_OBJ)\winlayer.$o $(ENGINE_OBJ)\rawinput.$o
!endif
!if ("$(RENDERTYPE)"=="SDL")
ENGINE_OBJS=$(ENGINE_OBJS) $(ENGINE_OBJ)\sdlayer.$o
DUKE3D_OBJS=$(DUKE3D_OBJS) $(DUKE3D_OBJ)\game_icon.$o
DUKE3D_EDITOR_OBJS=$(DUKE3D_EDITOR_OBJS) $(DUKE3D_OBJ)\build_icon.$o
!endif

!if ("$(MIXERTYPE)"=="WIN")
DUKE3D_OBJS=$(DUKE3D_OBJS) $(DUKE3D_OBJ)\midi.$o $(DUKE3D_OBJ)\music.$o $(DUKE3D_OBJ)\mpu401.$o
AUDIOLIB_OBJS=$(AUDIOLIB_OBJS) $(AUDIOLIB_OBJ)\driver_directsound.$o
AUDIOLIB_CFLAGS=$(AUDIOLIB_CFLAGS) /DHAVE_DS
!endif
!if ("$(MIXERTYPE)"=="SDL")
DUKE3D_OBJS=$(DUKE3D_OBJS) $(DUKE3D_OBJ)\sdlmusic.$o
AUDIOLIB_OBJS=$(AUDIOLIB_OBJS) $(AUDIOLIB_OBJ)/driver_sdl.$o
AUDIOLIB_CFLAGS=$(AUDIOLIB_CFLAGS) /DHAVE_SDL
!endif

DUKE3D_OBJS=$(DUKE3D_OBJS) $(MUSICOBJ)
DUKE3D_EDITOR_OBJS=$(DUKE3D_EDITOR_OBJS) $(MUSICOBJ)


CHECKDIR_ENGINE=@if not exist "$(ENGINE_OBJ)" mkdir "$(ENGINE_OBJ)"
CHECKDIR_DUKE3D=@if not exist "$(DUKE3D_OBJ)" mkdir "$(DUKE3D_OBJ)"
CHECKDIR_ENET=@if not exist "$(ENET_OBJ)" mkdir "$(ENET_OBJ)"
CHECKDIR_AUDIOLIB=@if not exist "$(AUDIOLIB_OBJ)" mkdir "$(AUDIOLIB_OBJ)"


# RULES
.SUFFIXES: .masm

{$(ENGINE_SRC)}.masm{$(ENGINE_OBJ)}.$o:
	$(CHECKDIR_ENGINE)
	$(AS) /c $(ASFLAGS) /Fo$@ $<

{$(ENGINE_SRC)\util}.c{$(ENGINE_OBJ)}.$o:
	$(CHECKDIR_ENGINE)
	$(CC) /c $(CFLAGS) /Fo$@ $<

{$(ENGINE_SRC)\misc}.rc{$(ENGINE_OBJ)}.$(res):
	$(CHECKDIR_ENGINE)
	$(RC) /i$(ENGINE_INC)\ /fo$@ /r $<

{$(ENGINE_SRC)}.c{$(ENGINE_OBJ)}.$o:
	$(CHECKDIR_ENGINE)
	$(CC) /c $(CFLAGS) /Fo$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DUKE3D)
	$(RC) /i$(ENGINE_INC)\ /i$(DUKE3D_SRC)\ /i$(DUKE3D_RSRC)\ /DPOLYMER /fo$@ /r $<


# TARGETS
all: eduke32$(EXESUFFIX) mapster32$(EXESUFFIX)

eduke32$(EXESUFFIX): $(DUKE3D_OBJS) $(ENGINE_OBJS) $(AUDIOLIB_OBJS) $(ENET_OBJS)
	$(LINK) /OUT:$@ /SUBSYSTEM:WINDOWS $(WINMACHINE) /LIBPATH:$(PLATFORM)\lib$(WINLIB) /LIBPATH:$(AUDIOPLATFORM)\lib$(WINLIB) $(flags_link) /MAP $** $(LIBS)
	$(MT) -manifest $(DUKE3D_RSRC)$(WINLIB)\manifest.game.xml -hashupdate -outputresource:$@ -out:$@.manifest
	
mapster32$(EXESUFFIX): $(DUKE3D_EDITOR_OBJS) $(ENGINE_OBJS) $(ENGINE_EDITOR_OBJS) $(AUDIOLIB_OBJS)
	$(LINK) /OUT:$@ /SUBSYSTEM:WINDOWS $(WINMACHINE) /LIBPATH:$(PLATFORM)\lib$(WINLIB) /LIBPATH:$(AUDIOPLATFORM)\lib$(WINLIB) $(flags_link) /MAP $** $(LIBS)
	$(MT) -manifest $(DUKE3D_RSRC)$(WINLIB)\manifest.build.xml -hashupdate -outputresource:$@ -out:$@.manifest
	
!include Makefile.deps
!include $(ENGINE_ROOT)\Makefile.deps

# PHONIES

clean:
	-del /Q eduke32$(EXESUFFIX) mapster32$(EXESUFFIX) $(DUKE3D_OBJS) $(DUKE3D_EDITOR_OBJS) *.pdb *.map *.manifest
	
veryclean: clean
	-del /Q $(ENGINE_OBJS) $(ENGINE_EDITOR_OBJS) $(ENET_OBJS) $(AUDIOLIB_OBJS)
