# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ====================================================================
#
# NMAKEmakefile   Tomcat Native makefile.
# Usage:
#                 APR_DECLARE_STATIC=1  Use static version of the APR
#                 ENABLE_OCSP=1  	    Enable OpenSSL OCSP code
#                 OPENSSL_NEW_LIBS=1    Use new OpenSSL lib file names
#                 DEBUG=1   		    Build DEBUG version of TCN
#
# Originally contributed by Mladen Turk <mturk redhat.com>
#
# ====================================================================
#

!IF !DEFINED(APR_DECLARE_STATIC) || "$(APR_DECLARE_STATIC)" == ""
PROJECT = libtcnative-1
!ELSE
PROJECT = tcnative-1
!ENDIF
TARGET = DLL

!include <NMAKEmakefile.inc>

!IF !DEFINED(APR_DECLARE_STATIC) || "$(APR_DECLARE_STATIC)" == ""
APR_LIB = libapr-1.lib
!ELSE
CFLAGS = $(CFLAGS) -DAPR_DECLARE_STATIC
APR_LIB = apr-1.lib
!ENDIF
!IF !DEFINED(ENABLE_OCSP)
CFLAGS = $(CFLAGS) -DOPENSSL_NO_OCSP
!ENDIF

!IF !DEFINED(SRCDIR) || "$(SRCDIR)" == ""
SRCDIR = .
!ENDIF

INCLUDES = -I$(SRCDIR)\include $(JAVA_INCLUDES)

!IFNDEF WITH_APR
WITH_APR = $(SRCDIR)\srclib\apr
LDIRS = /libpath:"$(WITH_APR)\$(WORKDIR)"
!ELSE
LDIRS = /libpath:"$(WITH_APR)\lib"
!ENDIF

INCLUDES = $(INCLUDES) -I$(WITH_APR)\include -I$(WITH_APR)\include\arch\win32
!IF DEFINED(WITH_OPENSSL)
INCLUDES = $(INCLUDES) -I$(WITH_OPENSSL)\include
LDIRS = $(LDIRS) /libpath:"$(WITH_OPENSSL)\lib"
!ELSE
WITH_OPENSSL = $(SRCDIR)\srclib\openssl
INCLUDES = $(INCLUDES) -I$(WITH_OPENSSL)\inc32
LDIRS = $(LDIRS) /libpath:"$(WITH_OPENSSL)\out32"
!ENDIF

LFLAGS = $(LFLAGS) /version:1.1
LFLAGS = $(LFLAGS) user32.lib psapi.lib gdi32.lib shlwapi.lib wldap32.lib ole32.lib
LFLAGS = $(LFLAGS) $(APR_LIB)
!IF DEFINED(WITH_FIPS)
LFLAGS = $(LFLAGS) libeayfips32.lib libeaycompat32.lib ssleay32.lib /NODEFAULTLIB:LIBCMT
!ELSE
!IF DEFINED(OPENSSL_NEW_LIBS)
LFLAGS = $(LFLAGS) libssl.lib libcrypto.lib crypt32.lib
!ELSE
LFLAGS = $(LFLAGS) libeay32.lib ssleay32.lib 
!ENDIF
!ENDIF

CFLAGS = $(CFLAGS) -DZLIB_WINAPI -DNO_IDEA -DNO_RC5 -DNO_MDC2 -DOPENSSL_NO_IDEA \
	-DOPENSSL_NO_RC5 -DOPENSSL_NO_MDC2 -DHAVE_OPENSSL -DHAVE_SSL_SET_STATE=1


PDBFLAGS = -Fo$(WORKDIR)\ -Fd$(WORKDIR)\$(PROJECT)-src
OBJECTS = \
	$(WORKDIR)\address.obj \
	$(WORKDIR)\bb.obj \
	$(WORKDIR)\dir.obj \
	$(WORKDIR)\error.obj \
	$(WORKDIR)\file.obj \
	$(WORKDIR)\info.obj \
	$(WORKDIR)\jnilib.obj \
	$(WORKDIR)\lock.obj \
	$(WORKDIR)\misc.obj \
	$(WORKDIR)\mmap.obj \
	$(WORKDIR)\multicast.obj \
	$(WORKDIR)\network.obj \
	$(WORKDIR)\os.obj \
	$(WORKDIR)\poll.obj \
	$(WORKDIR)\pool.obj \
	$(WORKDIR)\proc.obj \
	$(WORKDIR)\shm.obj \
	$(WORKDIR)\ssl.obj \
	$(WORKDIR)\sslcontext.obj \
	$(WORKDIR)\sslconf.obj \
	$(WORKDIR)\sslinfo.obj \
	$(WORKDIR)\sslnetwork.obj \
	$(WORKDIR)\sslutils.obj \
	$(WORKDIR)\stdlib.obj \
	$(WORKDIR)\user.obj \
	$(WORKDIR)\thread.obj \
	$(WORKDIR)\ntpipe.obj \
	$(WORKDIR)\registry.obj \
	$(WORKDIR)\system.obj
!IF DEFINED(WITH_FIPS)
OBJECTS = $(OBJECTS) srclib\openssl\tmp32\fips_premain.obj
!ENDIF

!IF DEFINED(WITH_FIPS)
FIPS_OPTS = /map
LINK_WRAPPER = perl $(FIPSDIR)\bin\fipslink.pl
!ELSE
LINK_WRAPPER = $(LINK)
!ENDIF

HEADERS = \
	$(SRCDIR)\include\tcn.h \
	$(SRCDIR)\include\tcn_api.h \
	$(SRCDIR)\include\tcn_version.h \
	$(SRCDIR)\include\ssl_private.h

BUILDLIB = $(PREFIX)\lib
BUILDBIN = $(WORKDIR)\$(PROJECT).dll
BUILDPDB = $(WORKDIR)\$(PROJECT).pdb
BUILDRES = $(WORKDIR)\$(PROJECT).res
BUILDMAN = $(BUILDBIN).manifest

all : $(WORKDIR) $(BUILDBIN)

$(BUILDLIB) :
	@if not exist "$(BUILDLIB)\$(NULL)" mkdir "$(BUILDLIB)"

$(WORKDIR) :
	@$(MAKEWORKDIR)

{$(SRCDIR)\src}.c{$(WORKDIR)}.obj:
	$(CC) $(CFLAGS) $(INCLUDES) $(PDBFLAGS) $<

{$(SRCDIR)\os\win32}.c{$(WORKDIR)}.obj:
	$(CC) $(CFLAGS) $(INCLUDES) $(PDBFLAGS) $<

$(OBJECTS): $(HEADERS)

$(BUILDRES): $(SRCDIR)\os\win32\libtcnative.rc $(HEADERS)
	$(RC) $(RCFLAGS) /i "$(SRCDIR)\include" /fo $(BUILDRES) $(SRCDIR)\os\win32\libtcnative.rc

$(BUILDBIN): $(WORKDIR) $(OBJECTS) $(BUILDRES)
  SET FIPSLINK=perl $(FIPSDIR)\bin\fipslink.pl
  SET FIPS_LINK=$(LINK)
  SET FIPS_CC=$(CC)
  SET FIPS_CC_ARGS=/Fosrclib\openssl\tmp32\fips_premain.obj /MD -c
  SET PREMAIN_DSO_EXE=srclib\openssl\\out32\fips_premain_dso.exe
  SET FIPS_TARGET=$(BUILDBIN)
  SET FIPS_SHA1_EXE=$(FIPSDIR)\bin\fips_standalone_sha1.exe
  SET FIPSLIB_D=$(FIPSDIR)\lib
  $(LINK_WRAPPER) $(LFLAGS) $(OBJECTS) $(BUILDRES) $(LIBS) $(LDIRS) /pdb:$(BUILDPDB) $(FIPS_OPTS) /out:$(BUILDBIN)
	IF EXIST $(BUILDMAN) \
		mt -nologo -manifest $(BUILDMAN) -outputresource:$(BUILDBIN);2

clean:
	@$(CLEANTARGET)

install: $(BUILDLIB) $(WORKDIR) $(BUILDBIN)
	@xcopy "$(WORKDIR)\*.lib" "$(BUILDLIB)" /Y /Q
	@xcopy "$(WORKDIR)\*.dll" "$(BUILDLIB)" /Y /Q
