!INCLUDE ..\win32\common.mak

SCRAM=1

!IF "$(NTLM)" == "1"
PLUGINS_EXT=saslNTLM.dll
!ELSE 
PLUGINS_EXT=
!ENDIF 

!IF "$(GSSAPI)" == "CyberSafe"
PLUGINS_EXT=$(PLUGINS_EXT) saslGSSAPI.dll
!ENDIF 

!IF "$(SRP)" == "1"
PLUGINS_EXT=$(PLUGINS_EXT) saslSRP.dll
!IF "$(DO_SRP_SETPASS)" == "1"
SRP_FLAGS=/DDO_SRP_SETPASS=1
!ENDIF 
!ENDIF 

!IF "$(OTP)" == "1"
PLUGINS_EXT=$(PLUGINS_EXT) saslOTP.dll
!ENDIF

!IF "$(LDAP)" == "1"
PLUGINS_EXT=$(PLUGINS_EXT) saslLDAPDB.dll

# NB: linking to libsasl itself!!!
LDAP_FLAGS = /I $(LDAP_INCLUDE)
LDAP_LIBS = $(LDAP_LIB_BASE)\olber32.lib $(LDAP_LIB_BASE)\oldap32.lib ..\lib\libsasl.lib
!ENDIF 

!IF "$(SQL)" == "SQLITE"
PLUGINS_EXT=$(PLUGINS_EXT) saslSQLITE.dll
SQL_FLAGS= $(SQLITE_INCLUDES) /DHAVE_SQLITE=1
SQLITE_LIBS = "/libpath:$(SQLITE_LIBPATH)" libsqlite.lib
!ENDIF
!IF "$(SQL)" == "SQLITE3"
PLUGINS_EXT=$(PLUGINS_EXT) saslSQLITE.dll
SQL_FLAGS= $(SQLITE_INCLUDES3) /DHAVE_SQLITE3=1
SQLITE_LIBS = "/libpath:$(SQLITE_LIBPATH3)" libsqlite3.lib
!ENDIF

PLUGINS=saslANONYMOUS.dll \
	saslPLAIN.dll \
	saslCRAMMD5.dll \
	saslDIGESTMD5.dll \
	saslLOGIN.dll \
	saslSCRAM.dll \
	$(PLUGINS_EXT) \
	saslSASLDB.dll

generated_rc=saslANONYMOUS.rc saslPLAIN.rc saslCRAMMD5.rc saslDIGESTMD5.rc saslLOGIN.rc saslNTLM.rc saslSCRAM.rc saslGSSAPI.rc saslSRP.rc saslOTP.rc saslSASLDB.rc saslSQLITE.rc saslLDAPDB.rc

# WS2tcpip.h included in Visual Studio 7 provides getaddrinfo, ...
# emulation on Windows, so there is no need to build getaddrinfo.c

!IF "$(VCVER)" == "6"
compat_objs = getaddrinfo.obj getnameinfo.obj
!ENDIF

common_objs = plugin_common.obj $(compat_objs)

saslANONYMOUS_objs = anonymous.obj anonymous_init.obj $(common_objs)
saslANONYMOUS_out = saslANONYMOUS.dll saslANONYMOUS.exp saslANONYMOUS.lib

saslPLAIN_objs = plain.obj plain_init.obj $(common_objs)
saslPLAIN_out = saslPLAIN.dll saslPLAIN.exp saslPLAIN.lib

saslCRAMMD5_objs = cram.obj crammd5_init.obj $(common_objs)
saslCRAMMD5_out = saslCRAMMD5.dll saslCRAMMD5.exp saslCRAMMD5.lib

saslDIGESTMD5_objs = digestmd5.obj digestmd5_init.obj $(common_objs)
saslDIGESTMD5_out = saslDIGESTMD5.dll saslDIGESTMD5.exp saslDIGESTMD5.lib

saslLOGIN_objs = login.obj login_init.obj $(common_objs)
saslLOGIN_out = saslLOGIN.dll saslLOGIN.exp saslLOGIN.lib

saslSCRAM_objs = scram.obj scram_init.obj $(common_objs)
saslSCRAM_out = saslSCRAM.dll saslSCRAM.exp saslSCRAM.lib

saslNTLM_objs = ntlm.obj ntlm_init.obj $(common_objs)
saslNTLM_out = saslNTLM.dll saslNTLM.exp saslNTLM.lib

saslGSSAPI_objs = gssapi.obj gssapiv2_init.obj $(common_objs)
saslGSSAPI_out = saslGSSAPI.dll saslGSSAPI.exp saslGSSAPI.lib

saslSRP_objs = srp.obj srp_init.obj $(common_objs)
saslSRP_out = saslSRP.dll saslSRP.exp saslSRP.lib

saslOTP_objs = otp.obj otp_init.obj $(common_objs)
saslOTP_out = saslOTP.dll saslOTP.exp saslOTP.lib

saslSQL_objs = sql.obj sql_init.obj $(common_objs)
# saslSQL_out is an agregation of all generated files for all SQL plugins
saslSQL_out = saslSQLITE.dll saslSQLITE.exp saslSQLITE.lib

saslLDAPDB_objs = ldapdb.obj $(common_objs)
saslLDAPDB_out = saslLDAPDB.dll saslLDAPDB.exp saslLDAPDB.lib

!IF "$(NTLM)" == "1" || "$(SRP)" == "1" || "$(OTP)" == "1" || "$(SCRAM)" == "1"
OPENSSL_FLAGS= /I "$(OPENSSL_INCLUDE)"
!ELSE 
OPENSSL_FLAGS=
!ENDIF 

!IF "$(GSSAPILIB)" == ""
!IF EXISTS($(GSSAPI_LIBPATH)\gssapi64.lib)
GSSAPILIB=gssapi64.lib
!ELSE
GSSAPILIB=gssapi32.lib
!ENDIF
!ENDIF

!IF "$(GSSAPI)" == "CyberSafe"
GSS_FLAGS= /I "$(GSSAPI_INCLUDE)" /D "HAVE_GSSAPI_GSSAPI_H" /D "HAVE_GSSAPI_GSSAPI_KRB5_H" /D "HAVE_GSS_C_NT_HOSTBASED_SERVICE" /D "HAVE_GSS_C_NT_USER_NAME"
GSS_LIBS="/libpath:$(GSSAPI_LIBPATH)" $(GSSAPILIB)
!ELSE IF "$(GSSAPI)" == "MITKerberos"
GSS_FLAGS= /I "$(GSSAPI_INCLUDE)" /D "HAVE_GSSAPI_GSSAPI_H" /D "HAVE_GSSAPI_GSSAPI_KRB5_H" /D "HAVE_GSS_C_NT_HOSTBASED_SERVICE" /D "HAVE_GSS_C_NT_USER_NAME"
GSS_LIBS="/libpath:$(GSSAPI_LIBPATH)" $(GSSAPILIB)
!ELSE 
GSS_FLAGS=
GSS_LIBS=
!ENDIF

!IF "$(SASLDB)" == "LMDB"
DB_LIBS="/libpath:$(LMDB_LIBPATH)" lmdb.lib

# Auxprop Plugin
libsasldb_objs = allockey.obj db_lmdb.obj
!ELSE
DB_LIBS="/libpath:$(DB_LIBPATH)" "$(DB_LIB)"

# Auxprop Plugin
libsasldb_objs = allockey.obj db_berkeley.obj
!ENDIF

CRAM_FLAGS=/DOBSOLETE_CRAM_ATTR=1

DIGEST_FLAGS=/D "WITH_RC4" /DOBSOLETE_DIGEST_ATTR=1

SCRAM_FLAGS=/DHAVE_SHA512=1

saslSASLDB_objs = sasldb.obj sasldb_init.obj $(libsasldb_objs) $(common_objs)
saslSASLDB_out = saslSASLDB.dll saslSASLDB.exp saslSASLDB.lib

all_objs = $(saslANONYMOUS_objs) $(saslPLAIN_objs) $(saslCRAMMD5_objs) $(saslDIGESTMD5_objs) $(saslLOGIN_objs) $(saslSCRAM_objs) $(saslNTLM_objs) $(saslGSSAPI_objs) $(saslSRP_objs) $(saslOTP_objs) $(saslSASLDB_objs) $(saslSQL_objs) $(saslLDAPDB_objs)
all_out = $(saslANONYMOUS_out) $(saslPLAIN_out) $(saslCRAMMD5_out) $(saslDIGESTMD5_out) $(saslLOGIN_out) $(saslSCRAM_out) $(saslNTLM_out) $(saslGSSAPI_out) $(saslSRP_out) $(saslOTP_out) $(saslSASLDB_out) $(saslSQL_out) $(saslLDAPDB_out)

# LIBSASL_EXPORTS is required to export additional DB routines from sasldb
DB_FLAGS = /I "$(DB_INCLUDE)" /I "..\sasldb" /D "LIBSASL_EXPORTS" /D "KEEP_DB_OPEN"

!IF $(TARGET_WIN_SYSTEM) >= 51
EXTRA_FLAGS = /D TARGET_WIN_SYSTEM=$(TARGET_WIN_SYSTEM) $(EXTRA_FLAGS)
!ENDIF

EXTRA_FLAGS=$(EXTRA_FLAGS) $(DB_FLAGS) $(OPENSSL_FLAGS) $(GSS_FLAGS) $(SRP_FLAGS) $(SQL_FLAGS) $(DIGEST_FLAGS) $(CRAM_FLAGS) $(SCRAM_FLAGS) $(LDAP_FLAGS)
CPPFLAGS = /I "..\win32\include" /I "." /I "..\include" /I "..\common" $(EXTRA_FLAGS) /D "_WIN32" $(COMMON_CPPFLAGS)

OPENSSL_LIBS="/libpath:$(OPENSSL_LIBPATH)"
!IF      EXISTS($(OPENSSL_LIBPATH)\libcrypto.lib)
# OpenSSL >= 1.1
OPENSSL_LIBS=$(OPENSSL_LIBS) libcrypto.lib
!ELSE IF EXISTS($(OPENSSL_LIBPATH)\libeay32.lib)
# OpenSSL <= 1.0
OPENSSL_LIBS=$(OPENSSL_LIBS) libeay32.lib
!ELSE
# Don't error out because during 'nmake install', variable 'OPENSSL_LIBS'
# can be missing but it's not used anyway.
OPENSSL_LIBS=$(OPENSSL_LIBS) failed_to_identify_openssl.lib
!ENDIF

# Where to install files from this directory
libdir = $(prefix)\lib
bindir = $(prefix)\bin\sasl2

all : all-recursive

#
# /I flag to xcopy tells to treat the last parameter as directory and create all missing levels
#
# In order to force xcopy not to confirm if the second parameter is file or directory,
# the first parameter has to contain a wildcard character. For example, we use libsasl.l*,
# instead of libsasl.lib. Ugly, but works!
#
# Note, that we will copy all dlls here, not just $(PLUGINS). This is a bug, but it allows
# us to copy GSSAPI plugin, which might not be in $(PLUGINS).
#
install: $(PLUGINS)
	@xcopy *.dll "$(bindir)" /I /F /Y

all-recursive : $(PLUGINS)

#Add /pdb: option?

saslANONYMOUS.dll: $(saslANONYMOUS_objs) saslANONYMOUS.res
	$(LINK32DLL) @<< $(LINK32DLL_FLAGS) /out:"saslANONYMOUS.dll" /implib:"saslANONYMOUS.lib" $(saslANONYMOUS_objs) saslANONYMOUS.res
<<
	IF EXIST $@.manifest mt -manifest $@.manifest -outputresource:$@;2

saslPLAIN.dll: $(saslPLAIN_objs) saslPLAIN.res
	$(LINK32DLL) @<< $(LINK32DLL_FLAGS) /out:"saslPLAIN.dll" /implib:"saslPLAIN.lib" $(saslPLAIN_objs) saslPLAIN.res
<<
	IF EXIST $@.manifest mt -manifest $@.manifest -outputresource:$@;2

saslCRAMMD5.dll: $(saslCRAMMD5_objs) saslCRAMMD5.res
	$(LINK32DLL) @<< $(LINK32DLL_FLAGS) /out:"saslCRAMMD5.dll" /implib:"saslCRAMMD5.lib" $(saslCRAMMD5_objs) saslCRAMMD5.res
<<
	IF EXIST $@.manifest mt -manifest $@.manifest -outputresource:$@;2

saslDIGESTMD5.dll: $(saslDIGESTMD5_objs) saslDIGESTMD5.res
	$(LINK32DLL) @<< $(OPENSSL_LIBS) $(LINK32DLL_FLAGS) /out:"saslDIGESTMD5.dll" /implib:"saslDIGESTMD5.lib" $(saslDIGESTMD5_objs) saslDIGESTMD5.res
<<
	IF EXIST $@.manifest mt -manifest $@.manifest -outputresource:$@;2

saslLOGIN.dll: $(saslLOGIN_objs) saslLOGIN.res
	$(LINK32DLL) @<< $(LINK32DLL_FLAGS) /out:"saslLOGIN.dll" /implib:"saslLOGIN.lib" $(saslLOGIN_objs) saslLOGIN.res
<<
	IF EXIST $@.manifest mt -manifest $@.manifest -outputresource:$@;2

saslSCRAM.dll: $(saslSCRAM_objs) saslSCRAM.res
	$(LINK32DLL) @<< $(OPENSSL_LIBS) $(LINK32DLL_FLAGS) /out:"saslSCRAM.dll" /implib:"saslSCRAM.lib" $(saslSCRAM_objs) saslSCRAM.res
<<
	IF EXIST $@.manifest mt -manifest $@.manifest -outputresource:$@;2

saslNTLM.dll: $(saslNTLM_objs) saslNTLM.res
	$(LINK32DLL) @<< $(OPENSSL_LIBS) $(LINK32DLL_FLAGS) /out:"saslNTLM.dll" /implib:"saslNTLM.lib" $(saslNTLM_objs) saslNTLM.res
<<
	IF EXIST $@.manifest mt -manifest $@.manifest -outputresource:$@;2

saslGSSAPI.dll: $(saslGSSAPI_objs) saslGSSAPI.res
	$(LINK32DLL) @<< $(GSS_LIBS) $(LINK32DLL_FLAGS) /out:"saslGSSAPI.dll" /implib:"saslGSSAPI.lib" $(saslGSSAPI_objs) saslGSSAPI.res
<<
	IF EXIST $@.manifest mt -manifest $@.manifest -outputresource:$@;2

saslSRP.dll: $(saslSRP_objs) saslSRP.res
	$(LINK32DLL) @<< $(OPENSSL_LIBS) $(LINK32DLL_FLAGS) /out:"saslSRP.dll" /implib:"saslSRP.lib" $(saslSRP_objs) saslSRP.res
<<
	IF EXIST $@.manifest mt -manifest $@.manifest -outputresource:$@;2

saslOTP.dll: $(saslOTP_objs) saslOTP.res
	$(LINK32DLL) @<< $(OPENSSL_LIBS) $(LINK32DLL_FLAGS) /out:"saslOTP.dll" /implib:"saslOTP.lib" $(saslOTP_objs) saslOTP.res
<<
	IF EXIST $@.manifest mt -manifest $@.manifest -outputresource:$@;2

saslSASLDB.dll: $(saslSASLDB_objs) saslSASLDB.res
	$(LINK32DLL) @<< $(DB_LIBS) $(LINK32DLL_FLAGS) /out:"saslSASLDB.dll" /implib:"saslSASLDB.lib" $(saslSASLDB_objs) saslSASLDB.res
<<
	IF EXIST $@.manifest mt -manifest $@.manifest -outputresource:$@;2

saslSQLITE.dll: $(saslSQL_objs) saslSQLITE.res
	$(LINK32DLL) @<< $(SQLITE_LIBS) $(LINK32DLL_FLAGS) /out:"saslSQLITE.dll" /implib:"saslSQLITE.lib" $(saslSQL_objs) saslSQLITE.res
<<
	IF EXIST $@.manifest mt -manifest $@.manifest -outputresource:$@;2

saslLDAPDB.dll: $(saslLDAPDB_objs) saslLDAPDB.res
	$(LINK32DLL) @<< $(LDAP_LIBS) $(OPENSSL_LIBS) $(LINK32DLL_FLAGS) /out:"saslLDAPDB.dll" /implib:"saslLDAPDB.lib" $(saslLDAPDB_objs) saslLDAPDB.res
<<
	IF EXIST $@.manifest mt -manifest $@.manifest -outputresource:$@;2

CLEAN :
	-@erase $(all_objs)
	-@erase "*.idb"
	-@erase "*.pdb"
	-@erase "*.manifest"
	-@erase getaddrinfo.c
	-@erase allockey.c
	-@erase db_berkeley.c
	-@erase db_lmdb.c
	-@erase getnameinfo.c
	-@erase plugin_common.h
	-@erase plugin_common.c
	-@erase $(generated_rc)
	-@erase "*.res"
	-@erase $(all_out)

.c.obj::
   $(CPP) @<<
   $(CPP_PROJ) $< 
<<

{..\lib}.c.obj::
   $(CPP) @<<
   $(CPP_PROJ) $< 
<<

{..\common}.c.obj::
   $(CPP) @<<
   $(CPP_PROJ) $< 
<<

{..\sasldb}.c.obj::
   $(CPP) @<<
   $(CPP_PROJ) $< 
<<

.rc.res:
	rc $<

$(generated_rc):
	copy <<temp.rc $@
#include "windows.h"

VS_VERSION_INFO VERSIONINFO
 FILEVERSION $(SASL_VERSION_MAJOR),$(SASL_VERSION_MINOR),$(SASL_VERSION_STEP),0
 PRODUCTVERSION $(SASL_VERSION_MAJOR),$(SASL_VERSION_MINOR),$(SASL_VERSION_STEP),0
 FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
 FILEFLAGS 0x1L
#else
 FILEFLAGS 0x0L
#endif
 FILEOS 0x40004L
 FILETYPE 0x1L
 FILESUBTYPE 0x0L
BEGIN
    BLOCK "StringFileInfo"
    BEGIN
        BLOCK "040904b0"
        BEGIN
            VALUE "CompanyName", "Carnegie Mellon University\0"
            VALUE "FileDescription", "CMU SASL $(@B) plugin\0"
            VALUE "FileVersion", "$(SASL_VERSION_MAJOR).$(SASL_VERSION_MINOR).$(SASL_VERSION_STEP).0\0"
            VALUE "InternalName", "$(@B)\0"
            VALUE "LegalCopyright", "Copyright (c) Carnegie Mellon University 2002-2017\0"
            VALUE "OriginalFilename", "$(@B).dll\0"
            VALUE "ProductName", "Carnegie Mellon University SASL\0"
            VALUE "ProductVersion", "$(SASL_VERSION_MAJOR).$(SASL_VERSION_MINOR).$(SASL_VERSION_STEP)-0"
        END
    END
    BLOCK "VarFileInfo"
    BEGIN
        VALUE "Translation", 0x409, 1200
    END
END
<<
