# 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   Master APR makefile.
# Usage:
#                 DLL=1     Build DLL version
#                 DEBUG=1   Build DEBUG version of APR
#
# Originally contributed by Mladen Turk <mturk redhat.com>
#
# ====================================================================
#

!IF !DEFINED(APR_DECLARE_STATIC) || "$(APR_DECLARE_STATIC)" == ""
TARGET=DLL
EXTRA_CFLAGS = -DAPR_DECLARE_EXPORT
PROJECT = libapr-1
!ELSE
TARGET=LIB
EXTRA_CFLAGS = -DAPR_DECLARE_STATIC
PROJECT = apr-1
!ENDIF

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

!include <..\..\NMAKEMakefile.inc>


!IF "$(TARGET)" != "LIB"
LFLAGS = $(LFLAGS) /version:1.4
!ENDIF
INCLUDES = -I$(SRCDIR)\include -I$(SRCDIR)\include\arch -I$(SRCDIR)\include\arch\win32 -I$(SRCDIR)\include\arch\unix
PDBFLAGS = -Fo$(WORKDIR)\ -Fd$(WORKDIR)\$(PROJECT)-src
OBJECTS = \
	$(WORKDIR)\apr_atomic.obj \
	$(WORKDIR)\dso.obj \
	$(WORKDIR)\apr_escape.obj \
	$(WORKDIR)\buffer.obj \
	$(WORKDIR)\copy.obj \
	$(WORKDIR)\dir.obj \
	$(WORKDIR)\fileacc.obj \
	$(WORKDIR)\filedup.obj \
	$(WORKDIR)\filepath.obj \
	$(WORKDIR)\filepath_util.obj \
	$(WORKDIR)\filestat.obj \
	$(WORKDIR)\filesys.obj \
	$(WORKDIR)\flock.obj \
	$(WORKDIR)\fullrw.obj \
	$(WORKDIR)\mktemp.obj \
	$(WORKDIR)\open.obj \
	$(WORKDIR)\pipe.obj \
	$(WORKDIR)\readwrite.obj \
	$(WORKDIR)\seek.obj \
	$(WORKDIR)\tempdir.obj \
	$(WORKDIR)\proc_mutex.obj \
	$(WORKDIR)\thread_cond.obj \
	$(WORKDIR)\thread_mutex.obj \
	$(WORKDIR)\thread_rwlock.obj \
	$(WORKDIR)\apr_pools.obj \
	$(WORKDIR)\apr_app.obj \
	$(WORKDIR)\charset.obj \
	$(WORKDIR)\env.obj \
	$(WORKDIR)\errorcodes.obj \
	$(WORKDIR)\getopt.obj \
	$(WORKDIR)\internal.obj \
	$(WORKDIR)\misc.obj \
	$(WORKDIR)\otherchild.obj \
	$(WORKDIR)\rand.obj \
	$(WORKDIR)\start.obj \
	$(WORKDIR)\utf8.obj \
	$(WORKDIR)\version.obj \
	$(WORKDIR)\common.obj \
	$(WORKDIR)\mmap.obj \
	$(WORKDIR)\inet_ntop.obj \
	$(WORKDIR)\inet_pton.obj \
	$(WORKDIR)\multicast.obj \
	$(WORKDIR)\sendrecv.obj \
	$(WORKDIR)\sockaddr.obj \
	$(WORKDIR)\sockets.obj \
	$(WORKDIR)\socket_util.obj \
	$(WORKDIR)\sockopt.obj \
	$(WORKDIR)\apr_getpass.obj \
	$(WORKDIR)\poll.obj \
	$(WORKDIR)\pollcb.obj \
	$(WORKDIR)\pollset.obj \
	$(WORKDIR)\select.obj \
	$(WORKDIR)\apr_random.obj \
	$(WORKDIR)\sha2.obj \
	$(WORKDIR)\sha2_glue.obj \
	$(WORKDIR)\shm.obj \
	$(WORKDIR)\apr_cpystrn.obj \
	$(WORKDIR)\apr_fnmatch.obj \
	$(WORKDIR)\apr_snprintf.obj \
	$(WORKDIR)\apr_strings.obj \
	$(WORKDIR)\apr_strnatcmp.obj \
	$(WORKDIR)\apr_strtok.obj \
	$(WORKDIR)\apr_hash.obj \
	$(WORKDIR)\apr_tables.obj \
	$(WORKDIR)\proc.obj \
	$(WORKDIR)\signals.obj \
	$(WORKDIR)\thread.obj \
	$(WORKDIR)\threadpriv.obj \
	$(WORKDIR)\time.obj \
	$(WORKDIR)\timestr.obj \
	$(WORKDIR)\groupinfo.obj \
	$(WORKDIR)\userinfo.obj

BUILDINC = $(PREFIX)\include\apr-1
BUILDLIB = $(PREFIX)\lib
!IF "$(TARGET)" == "DLL"
BUILDBIN = $(WORKDIR)\$(PROJECT).dll
BUILDPDB = $(WORKDIR)\$(PROJECT).pdb
BUILDRES = $(WORKDIR)\$(PROJECT).res
BUILDMAN = $(BUILDBIN).manifest
!ELSE
BUILDBIN = $(WORKDIR)\$(PROJECT).lib
!ENDIF

all : $(WORKDIR) $(BUILDBIN)

$(BUILDINC) :
	@if not exist "$(BUILDINC)\arch\win32\$(NULL)" mkdir "$(BUILDINC)\arch\win32"

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

$(WORKDIR) :
	@$(MAKEWORKDIR)

$(SRCDIR)\include\apr.h: $(SRCDIR)\include\apr.hw
	-@copy $(SRCDIR)\include\apr.hw $(SRCDIR)\include\apr.h

$(WORKDIR)\apr_escape_test_char.h: $(WORKDIR)\gen_test_char.exe
	$(WORKDIR)\gen_test_char.exe > $(WORKDIR)\apr_escape_test_char.h

$(WORKDIR)\gen_test_char.exe: $(WORKDIR)\gen_test_char.obj
	$(LINK) $(WORKDIR)\gen_test_char.obj /out:$(WORKDIR)\gen_test_char.exe

$(WORKDIR)\gen_test_char.obj: $(SRCDIR)\tools\gen_test_char.c
	$(CC) $(CFLAGS) $(PDBFLAGS) $(SRCDIR)\tools\gen_test_char.c

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

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

# Needs additional dynamically generated header file.
# It is created in $(WORKDIR), so add it to header search dirs.
{$(SRCDIR)\encoding}.c{$(WORKDIR)}.obj:
	$(CC) $(CFLAGS) $(INCLUDES) -I$(WORKDIR) $(PDBFLAGS) $<

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

$(OBJECTS): $(SRCDIR)\include\apr.h

$(WORKDIR)\apr_escape.obj: $(WORKDIR)\apr_escape_test_char.h

!IF "$(TARGET)" == "DLL"
$(BUILDRES): libapr.rc $(SRCDIR)\include\apr.h
	$(RC) $(RCFLAGS) /i "$(SRCDIR)\include" /d "APR_VERSION_ONLY" /fo $(BUILDRES) libapr.rc
$(BUILDBIN): $(WORKDIR) $(OBJECTS) $(BUILDRES)
	$(LINK) $(LFLAGS) $(OBJECTS) $(BUILDRES) $(LIBS) /pdb:$(BUILDPDB) /out:$(BUILDBIN)
	IF EXIST $(BUILDMAN) \
		mt -nologo -manifest $(BUILDMAN) -outputresource:$(BUILDBIN);2
!ELSE
$(BUILDBIN): $(WORKDIR) $(OBJECTS)
	$(LINK) $(LFLAGS) $(OBJECTS) /out:$(BUILDBIN)
!ENDIF

clean:
	@$(CLEANTARGET)

install: $(BUILDINC) $(BUILDLIB) $(WORKDIR) $(BUILDBIN)
	@xcopy "$(SRCDIR)\include\*.h" "$(BUILDINC)" /Y /Q
	@xcopy "$(SRCDIR)\include\arch\apr_private_common.h" "$(BUILDINC)\arch" /Y /Q
	@xcopy "$(SRCDIR)\include\arch\win32\*.h" "$(BUILDINC)\arch\win32" /Y /Q
	@xcopy "$(WORKDIR)\*.lib" "$(BUILDLIB)" /Y /Q
	@xcopy "$(WORKDIR)\*.dll" "$(BUILDLIB)" /Y /Q 2>NUL

