# flext - C++ layer for Max/MSP and pd (pure data) externals
# Copyright (c) 2001-2003 Thomas Grill (xovo@gmx.net)
#
# ----- for internal use ----------------------
#

!include config-max-msvc.txt

NAME=flext

# source files
SOURCE=source

# includes
INCPATH=/I$(MAXSDKPATH)\max-includes /I$(MAXSDKPATH)\msp-includes /I$(SOURCE)
LDFLAGS=

!ifdef MSVCPATH
INCPATH=$(INCPATH) /I$(MSVCPATH)\include 
LDFLAGS=$(LDFLAGS) /LIBPATH:$(MSVCPATH)\lib
!endif

# compiler definitions and flags
DEFS=/DFLEXT_SYS=1 $(UFLAGS)

CFLAGS=/GX /GD
OFLAGS=/Ox
DFLAGS=/Od /Zi

TARGET=maxwin  # appendix to lib name



!ifdef FLEXT_SHARED
DEFS=$(DEFS) /DFLEXT_SHARED /DFLEXT_EXPORTS 

!ifdef _DEBUG
LIBS=$(LIBS) $(MAXSDKPATH)\max-includes\win-includes\debug\maxapi.lib $(MAXSDKPATH)\msp-includes\win-includes\debug\maxaudio.lib
!else
LIBS=$(LIBS) $(MAXSDKPATH)\max-includes\win-includes\release\maxapi.lib $(MAXSDKPATH)\msp-includes\win-includes\release\maxaudio.lib
!endif

EXT=dll

!ifndef _DEBUG
CFLAGS=$(CFLAGS) $(OFLAGS) /MT /LD
OBJPATH=l
!else
CFLAGS=$(CFLAGS) $(DFLAGS) /MTd /LDd
OBJPATH=ld
!endif

!else

EXT=lib

!ifdef FLEXT_THREADS
DEFS=$(DEFS) /DFLEXT_THREADS

!ifndef _DEBUG
CFLAGS=$(CFLAGS) $(OFLAGS) /MT
OBJPATH=t
!else
CFLAGS=$(CFLAGS) $(DFLAGS) /MTd
OBJPATH=td
!endif

!else

!ifndef _DEBUG
CFLAGS=$(CFLAGS) $(OFLAGS) /ML
OBJPATH=s
!else
CFLAGS=$(CFLAGS) $(DFLAGS) /MLd
OBJPATH=sd
!endif

!endif # FLEXT_THREADS

!endif # FLEXT_SHARED


# the rest can stay untouched
# ----------------------------------------------

# all the source files from the package
!include make-files.txt

!ifdef SNDOBJ
SRCS=$(SRCS) $(SRCS_SNDOBJ)
HDRS=$(HDRS) $(HDRS_SNDOBJ)
INCPATH=$(INCPATH) /I$(SNDOBJ)/include
!endif

!ifdef STK
SRCS=$(SRCS) $(SRCS_STK)
HDRS=$(HDRS) $(HDRS_STK)
INCPATH=$(INCPATH) /I$(STK)/include 
!endif

#default target
all: $(OUTPATH)\$(NAME)-$(TARGET).$(EXT)

# remove build
clean:
	-cd $(OUTPATH)
	-del /s /q $(OBJPATH) > nul
	-rmdir $(OBJPATH)
	-cd ..
	-del /q $(OUTPATH)\$(NAME)-$(TARGET).$(EXT) > nul
	-rmdir $(OUTPATH)

# -----------------------------------------------

OBJS= $(SRCS:.cpp=.obj)

{$(SOURCE)\}.cpp.obj:
	@-if not exist $(OUTPATH) mkdir $(OUTPATH)
	@cd $(OUTPATH)
	@-if not exist $(OBJPATH) mkdir $(OBJPATH)
	@cd ..
	cl /c $(CFLAGS) $(DEFS) $(INCPATH) /Fo$(OUTPATH)\$(OBJPATH)\$@ $<



$(OUTPATH)\$(NAME)-$(TARGET).lib: $(OBJS)
	@cd $(OUTPATH)\$(OBJPATH)
	lib /OUT:..\$(NAME)-$(TARGET).lib $(OBJS)
	@cd ..\..
!ifdef INSTDIR
	@-if not exist $(OBJPATH) mkdir $(INSTDIR)
	copy $(OUTPATH)\$(NAME)-$(TARGET).lib $(INSTDIR) > nul
	copy $(SOURCE)\*.h $(INSTDIR) > nul
!endif


$(OUTPATH)\$(NAME)-$(TARGET).dll: $(OBJS)
	@cd $(OUTPATH)\$(OBJPATH)
	link /DLL /NOLOGO $(LDFLAGS) /OUT:..\$(NAME)-$(TARGET).dll $(LIBS) $(OBJS)
	@cd ..\..
!ifdef INSTDIR
	@-if not exist $(OBJPATH) mkdir $(INSTDIR)
	copy $(OUTPATH)\$(NAME)-$(TARGET).dll $(INSTDIR) > nul
	copy $(SOURCE)\*.h $(INSTDIR) > nul
!endif