# -*- Makefile -*-
!IF "$(OS)" == "Windows_NT"
NULL=
rmdir=rd /s /q
!ELSE
NULL=nul
rmdir=deltree /y
!ENDIF

SILENT=@

# Default build and bind modes
BUILD_MODE = release
BIND_MODE  = shared

!IF "$(BUILD_MODE)" == "release" || "$(BUILD_MODE)" == "Release"
!IF "$(BIND_MODE)" == "shared"
# release shared
APR_LIB_PFX = $(APR_SOURCE)\Release\lib
API_LIB_PFX = $(API_SOURCE)\Release\lib
CFG_CFLAGS  = /MD /Zi /O2
CFG_DEFINES = /D "NDEBUG"
CFG_LDFLAGS = /debug /opt:ref
CFG_OUTPUT  = Release\iconv

!ELSE
!IF "$(BIND_MODE)" == "static"
# release static
APR_LIB_PFX = $(APR_SOURCE)\LibR\	# no line continuation
API_LIB_PFX = $(API_SOURCE)\LibR\	# no line continuation
CFG_CFLAGS  = /MD /Zi /O2
CFG_DEFINES = /D "NDEBUG" /D "APR_DECLARE_STATIC" /D "API_DECLARE_STATIC"
CFG_LDFLAGS = /debug /opt:ref
CFG_OUTPUT  = LibR\iconv

!ELSE
!ERROR Unknown bind mode "$(BIND_MODE)"
!ENDIF
!ENDIF

!ELSE
!IF "$(BUILD_MODE)" == "debug" || "$(BUILD_MODE)" == "Debug"
!IF "$(BIND_MODE)" == "shared"
# debug shared
APR_LIB_PFX = $(APR_SOURCE)\Debug\lib
API_LIB_PFX = $(API_SOURCE)\Debug\lib
CFG_CFLAGS  = /MDd /Zi /Od
CFG_DEFINES = /D "_DEBUG"
CFG_LDFLAGS = /debug
CFG_OUTPUT  = Debug\iconv

!ELSE
!IF "$(BIND_MODE)" == "static"
# debug static
APR_LIB_PFX = $(APR_SOURCE)\LibD\	# no line continuation
API_LIB_PFX = $(API_SOURCE)\LibD\	# no line continuation
CFG_CFLAGS  = /MDd /Zi /Od
CFG_DEFINES = /D "_DEBUG" /D "APR_DECLARE_STATIC" /D "API_DECLARE_STATIC"
CFG_LDFLAGS = /debug
CFG_OUTPUT  = LibD\iconv

!ELSE
!ERROR Unknown bind mode "$(BIND_MODE)"
!ENDIF
!ENDIF

!ELSE
!ERROR Unknown build mode "$(BUILD_MODE)"
!ENDIF
!ENDIF


APR_SOURCE   = ..\..\apr
API_SOURCE   = ..
OUTPUT_DIR   = $(API_SOURCE)\$(CFG_OUTPUT)
OBJ_DIR   = $(OUTPUT_DIR)\objs

INT_CFLAGS   = /nologo $(CFG_CFLAGS) /Fp"$(OUTPUT_DIR)\iconv.pch" /YX"iconv.h"
INT_INCLUDES = /I "$(API_SOURCE)\include" /I "$(API_SOURCE)\lib" \
		/I "$(APR_SOURCE)\include"
INT_DEFINES  = /D "WIN32" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" $(CFG_DEFINES)
INT_LDFLAGS  = /nologo /dll /incremental:no /subsystem:windows \
		/export:iconv_module,DATA $(CFG_LDFLAGS)

CFLAGS       = /W3
ALL_CFLAGS   = $(INT_CFLAGS) $(INT_INCLUDES) $(INT_DEFINES) $(CFLAGS)

LDFLAGS      = /WARN:0
ALL_LDFLAGS  = $(INT_LDFLAGS) $(LDFLAGS)

.c{$(OUTPUT_DIR)}.so:
	-$(SILENT)if not exist "$(OUTPUT_DIR)\$(NULL)" mkdir "$(OUTPUT_DIR)"
	-$(SILENT)if not exist "$(OBJ_DIR)\$(NULL)" mkdir "$(OBJ_DIR)"
	$(SILENT)echo Compiling $<... via $(OBJ_DIR)\$(@B).obj to $@
	$(SILENT)cl $(ALL_CFLAGS) /Fo"$(OBJ_DIR)\$(@B).obj" \
		/Fd"$(OBJ_DIR)\$(@B)_src" $< \
		/link $(ALL_LDFLAGS) /out:$@ \
		/base:@"..\build\BaseAddr.ref",$(@F) \
		"$(API_LIB_PFX)apriconv.lib" "$(APR_LIB_PFX)apr.lib" \
		kernel32.lib advapi32.lib ws2_32.lib mswsock.lib
	$(SILENT)echo.
	$(SILENT)del "$*.exp"
	$(SILENT)del "$*.lib"

all: $(ALL_TARGETS)


# The rebase target below is used only to *regenerate* the BaseAddr.ref file
# if the module list or sizes change considerably.  Use the rebase target to

# to create the BaseAddr.0xNNNNNNNN.txt file of the rebase results, 
# then remerge it back into the explicit build/BaseAddr.ref file.
#
BASE_REF = $(OUTPUT_DIR)\BaseAddr.$(REBASE_TOP).txt
ALL_REBASE = -d -p -b $(REBASE_TOP) -C $(BASE_REF) 

rebase: $(ALL_TARGETS) 
	$(SILENT)echo Extracting .dbg symbols and rebasing all modules...
	$(SILENT)rebase $(ALL_REBASE) $(ALL_TARGETS)
	$(SILENT)echo.


clean:
	-$(SILENT)if exist "$(OUTPUT_DIR)/$(NULL)" $(rmdir) $(OUTPUT_DIR)

install:
	@echo Copying apr-iconv modules from $(OUTPUT_DIR) to $(INSTALL_DIR)
	-$(SILENT)mkdir "$(INSTALL_DIR)"
	$(SILENT)copy $(OUTPUT_DIR)\*.so "$(INSTALL_DIR)" < <<
A
<<
	@echo Copying apr-iconv pdb debugging symbols from $(OUTPUT_DIR) to $(INSTALL_DIR)
	$(SILENT)copy $(OUTPUT_DIR)\*.pdb "$(INSTALL_DIR)" < <<
A
<<
