# 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.
#
PROJECT = mod_jk
# Tools
CC = cl.exe
LINK = link.exe
RC = rc.exe
MT = mt.exe

!IF !DEFINED(APACHE2_HOME) || "$(APACHE2_HOME)" == ""
!ERROR Cannot find APACHE2_HOME. Use nmake -f Makefile.vc APACHE2_HOME=dir
!ENDIF
!IF !DEFINED(APACHE2_LDIR) || "$(APACHE2_LDIR)" == ""
APACHE2_LDIR=$(APACHE2_HOME)\lib
!ENDIF
!IF !DEFINED(APACHE2_IDIR) || "$(APACHE2_IDIR)" == ""
APACHE2_IDIR=$(APACHE2_HOME)\include
!ENDIF
!IF !DEFINED(PREFIX) || "$PREFIX)" == ""
PREFIX=$(APACHE2_HOME)\modules
!ENDIF

LIB = $(LIB);$(APACHE2_LDIR)
INCLUDE = $(INCLUDE);$(APACHE2_IDIR)

!IF !DEFINED(BUILD_CPU) || "$(BUILD_CPU)" == ""
!ERROR Must specify BUILD_CPU matching compiler x86 or x64
!ENDIF
!IF "$(BUILD_CPU)" == "x86"
CPUFLAGS = -D_X86_=1
MACHINE = X86
!ELSE
CPUFLAGS = -DWIN64 -D_WIN64
MACHINE = AMD64
!ENDIF
!IF !DEFINED(WINVER) || "$(WINVER)" == ""
WINVER = 0x0601
!ENDIF

# Modify library names or list them when calling nmake
APACHE2_LIBS = libhttpd.lib libapr-1.lib libaprutil-1.lib

BUILDBIN = $(WORKDIR)\$(PROJECT).so
BUILDRES = $(WORKDIR)\$(PROJECT).res
BUILDPDB = $(WORKDIR)\$(PROJECT).pdb
BUILDMFT = $(BUILDBIN).manifest

WORKDIR = $(BUILD_CPU)_RELEASE
CLEANTARGET = rd /s /q $(WORKDIR)
MAKEWORKDIR = md $(WORKDIR)

CFLAGS = $(CFLAGS) -DNDEBUG -DWIN32 -D_WINNT -DWINNT -D_WIN32_WINNT=$(WINVER) -DWINVER=$(WINVER) $(CPUFLAGS)
CFLAGS = $(CFLAGS) $(EXTRA_CFLAGS)
CLOPTS = /c /nologo -MD -W3 -O2 -Ob2 -Zi -EHsc
PDBFLAGS = -Fo$(WORKDIR)\ -Fd$(WORKDIR)\$(PROJECT)
LFLAGS = /nologo /DLL /INCREMENTAL:NO /DEBUG /OPT:REF /SUBSYSTEM:WINDOWS /MACHINE:$(MACHINE) $(EXTRA_LDLAGS)
LDLIBS = kernel32.lib user32.lib advapi32.lib ws2_32.lib mswsock.lib $(APACHE2_LIBS) $(EXTRA_LIBS)
INCDIR = -I..\common

OBJECTS = \
	$(WORKDIR)\jk_ajp12_worker.obj \
	$(WORKDIR)\jk_ajp13.obj \
	$(WORKDIR)\jk_ajp13_worker.obj \
	$(WORKDIR)\jk_ajp14.obj \
	$(WORKDIR)\jk_ajp14_worker.obj \
	$(WORKDIR)\jk_ajp_common.obj \
	$(WORKDIR)\jk_connect.obj \
	$(WORKDIR)\jk_context.obj \
	$(WORKDIR)\jk_lb_worker.obj \
	$(WORKDIR)\jk_map.obj \
	$(WORKDIR)\jk_md5.obj \
	$(WORKDIR)\jk_msg_buff.obj \
	$(WORKDIR)\jk_pool.obj \
	$(WORKDIR)\jk_shm.obj \
	$(WORKDIR)\jk_sockbuf.obj \
	$(WORKDIR)\jk_status.obj \
	$(WORKDIR)\jk_uri_worker_map.obj \
	$(WORKDIR)\jk_url.obj \
	$(WORKDIR)\jk_util.obj \
	$(WORKDIR)\jk_worker.obj \
	$(WORKDIR)\mod_jk.obj

all : $(WORKDIR) $(BUILDBIN)

$(WORKDIR) :
	@$(MAKEWORKDIR)

.c{$(WORKDIR)}.obj:
	$(CC) $(CLOPTS) $(CFLAGS) $(INCDIR) $(PDBFLAGS) $<

{..\common}.c{$(WORKDIR)}.obj:
	$(CC) $(CLOPTS) $(CFLAGS) $(INCDIR) $(PDBFLAGS) $<

$(BUILDRES): ..\common\jk.rc
	$(RC) /l 0x409 /n /i ..\common /d NDEBUG /fo $(BUILDRES) ..\common\jk.rc

$(BUILDBIN): $(WORKDIR) $(OBJECTS) $(BUILDRES)
	$(LINK) $(LFLAGS) $(OBJECTS) $(BUILDRES) $(LDLIBS) /out:$(BUILDBIN) /pdb:$(BUILDPDB)
	@if exist $(BUILDMFT) \
		$(MT) -nologo -manifest $(BUILDMFT) -outputresource:$(BUILDBIN);2

install : all
	@-xcopy /I /Y /Q "$(WORKDIR)\*.so"  "$(PREFIX)"
	@-xcopy /I /Y /Q "$(WORKDIR)\*.pdb" "$(PREFIX)"

clean:
	@-$(CLEANTARGET) 2>NUL
