#
# GNU Makefile for nghttp2 / MSVC.
#
# By G. Vanem <gvanem@yahoo.no> 2013
# Updated 3/2015 by Remo Eichenberger @remoe
# The MIT License apply.
#

#
# Choose your weapons:
#  Set 'USE_CYTHON=1' to build and install the 'nghttp2.pyd' Python extension.
#
THIS_MAKEFILE := $(lastword $(MAKEFILE_LIST))

USE_CYTHON := 0
#USE_CYTHON := 1

_VERSION	:= $(shell grep AC_INIT ../configure.ac | cut -d'[' -f3 | sed -e 's/-DEV//g' -e 's/], //g')
_VERSION   	:= $(subst ., ,$(_VERSION))
VER_MAJOR  	:= $(word 1,$(_VERSION))
VER_MINOR  	:= $(word 2,$(_VERSION))
VER_MICRO   := $(word 3,$(_VERSION))
VERSION     := $(VER_MAJOR).$(VER_MINOR).$(VER_MICRO)
VERSION_NUM := (($(VER_MAJOR) << 16) + ($(VER_MINOR) << 8) + $(VER_MICRO))

GENERATED   := 'Generated by $(realpath Makefile.MSVC)'

OBJ_DIR := MSVC_obj
#SUFFIX :=-vc90-mt-x86

#
# Where to copy nghttp2.dll + lib + headers to.
# Note: 'make install' is not in default targets. Do it explicitly.
#
TARGET_DIR  ?= ../_VC_ROOT
VC_ROOT     := $(abspath $(TARGET_DIR))
INSTALL_BIN := $(VC_ROOT)/bin
INSTALL_LIB := $(VC_ROOT)/lib
INSTALL_HDR := $(VC_ROOT)/include
DLL_R	:= $(OBJ_DIR)/nghttp2$(SUFFIX).dll
DLL_D	:= $(OBJ_DIR)/nghttp2d$(SUFFIX).dll
LIB_R	:= $(OBJ_DIR)/nghttp2-static.lib
LIB_D	:= $(OBJ_DIR)/nghttp2d-static.lib
IMP_R	:= $(OBJ_DIR)/nghttp2.lib
IMP_D	:= $(OBJ_DIR)/nghttp2d.lib

#
# Build for DEBUG-model and RELEASE at the same time.
#
TARGETS := $(LIB_R) $(DLL_R) $(IMP_R) \
          $(LIB_D) $(DLL_D) $(IMP_D)

EXT_LIBS = 

NGHTTP2_PDB_R := $(OBJ_DIR)/nghttp2.pdb
NGHTTP2_PDB_D := $(OBJ_DIR)/nghttp2d.pdb

CC       = cl
LD		 := link
AR		 := lib
#CC       := icl
#LD		 := xilink
#AR		 := xilib
RC		 := rc
CFLAGS   := -I./includes -Dssize_t=long

CFLAGS_R := -nologo -MD  -W3 -Z7 -DBUILDING_NGHTTP2
CFLAGS_D := -nologo -MDd -W3 -Z7 -DBUILDING_NGHTTP2 \
           -Ot -D_DEBUG -GF -RTCs -RTCu # -RTCc -GS

LDFLAGS := -nologo -MAP -debug -incremental:no -opt:ref,icf -MANIFEST # -verbose


NGHTTP2_SRC := nghttp2_pq.c \
  nghttp2_map.c \
  nghttp2_queue.c \
  nghttp2_frame.c \
  nghttp2_buf.c \
  nghttp2_stream.c \
  nghttp2_outbound_item.c \
  nghttp2_session.c \
  nghttp2_submit.c \
  nghttp2_helper.c \
  nghttp2_npn.c \
  nghttp2_hd.c \
  nghttp2_hd_huffman.c \
  nghttp2_hd_huffman_data.c \
  nghttp2_version.c \
  nghttp2_priority_spec.c \
  nghttp2_option.c \
  nghttp2_callbacks.c \
  nghttp2_mem.c \
  nghttp2_http.c \
  nghttp2_rcbuf.c

NGHTTP2_OBJ_R := $(addprefix $(OBJ_DIR)/r_, $(notdir $(NGHTTP2_SRC:.c=.obj)))
NGHTTP2_OBJ_D := $(addprefix $(OBJ_DIR)/d_, $(notdir $(NGHTTP2_SRC:.c=.obj)))

.PHONY: all intro test_ver install copy_headers_and_libs \
	install_nghttp2_pyd_0 install_nghttp2_pyd_1 \
	build_nghttp2_pyd_0 build_nghttp2_pyd_1 \
	clean_nghttp2_pyd_0 clean_nghttp2_pyd_1


all: intro includes/nghttp2/nghttp2ver.h $(OBJ_DIR) $(TARGETS) build_nghttp2_pyd_$(USE_CYTHON)
	@echo 'Welcome to NgHTTP2 (release + debug).'
	@echo 'Do a "make -f Makefile.MSVC install" at own risk!'

intro:
	@echo 'Building NgHTTP (MSVC) ver. "$(VERSION)".'

test_ver:
	@echo '$$(VERSION):   "$(VERSION)".'
	@echo '$$(_VERSION):  "$(_VERSION)".'
	@echo '$$(VER_MAJOR): "$(VER_MAJOR)".'
	@echo '$$(VER_MINOR): "$(VER_MINOR)".'
	@echo '$$(VER_MICRO): "$(VER_MICRO)".'

$(OBJ_DIR):
	- mkdir $(OBJ_DIR)

install: includes/nghttp2/nghttp2.h includes/nghttp2/nghttp2ver.h \
         $(TARGETS) \
         copy_headers_and_libs install_nghttp2_pyd_$(USE_CYTHON) 

#
# This MUST be done before using the 'install_nghttp2_pyd_1' rule.
#
copy_headers_and_libs:
	- mkdir -p $(INSTALL_HDR)/nghttp2 $(INSTALL_BIN) $(INSTALL_LIB)
	cp --update $(addprefix includes/nghttp2/, nghttp2.h nghttp2ver.h)     $(INSTALL_HDR)/nghttp2
	cp --update $(DLL_R) $(DLL_D) $(NGHTTP2_PDB_R) $(NGHTTP2_PDB_D) $(INSTALL_BIN)
	cp --update $(IMP_R) $(IMP_D) $(LIB_R) $(LIB_D) $(INSTALL_LIB)
	@echo

$(LIB_R): $(NGHTTP2_OBJ_R)
	$(AR) -nologo -out:$@ $^
	@echo

$(LIB_D): $(NGHTTP2_OBJ_D)
	$(AR) -nologo -out:$@ $^
	@echo


$(IMP_R): $(DLL_R)

$(DLL_R): $(NGHTTP2_OBJ_R) $(OBJ_DIR)/r_nghttp2.res 
	$(LD) $(LDFLAGS) -dll -out:$@ -implib:$(IMP_R) $(NGHTTP2_OBJ_R) -PDB:$(NGHTTP2_PDB_R) $(OBJ_DIR)/r_nghttp2.res $(EXT_LIBS)
	mt -nologo -manifest $@.manifest -outputresource:$@\;2
	@echo

$(IMP_D): $(DLL_D)
	
$(DLL_D): $(NGHTTP2_OBJ_D) $(OBJ_DIR)/d_nghttp2.res 
	$(LD) $(LDFLAGS) -dll -out:$@ -implib:$(IMP_D) $(NGHTTP2_OBJ_D) -PDB:$(NGHTTP2_PDB_D) $(OBJ_DIR)/d_nghttp2.res $(EXT_LIBS)
	mt -nologo -manifest $@.manifest -outputresource:$@\;2
	@echo


WIN_OBJDIR:=$(shell cygpath -w $(abspath $(OBJ_DIR)))
WIN_OBJDIR:=$(subst \,/,$(WIN_OBJDIR))

../python/setup.py: ../python/setup.py.in $(THIS_MAKEFILE)
	cd ../python ; \
	echo '# $(GENERATED). DO NOT EDIT.' > setup.py ; \
	sed -e 's/@top_srcdir@/../'   \
	    -e 's%@top_builddir@%$(WIN_OBJDIR)%' \
	    -e 's/@PACKAGE_VERSION@/$(VERSION)/' setup.py.in >> setup.py ;

build_nghttp2_pyd_0: ;

build_nghttp2_pyd_1: $(addprefix ../python/, setup.py nghttp2.pyx)
	cd ../python ; \
	python setup.py build_ext -i -f bdist_wininst

install_nghttp2_pyd_0: ;
		
install_nghttp2_pyd_1: $(addprefix ../python/, setup.py nghttp2.pyx)
	cd ../python ; \
	pip install .

clean_nghttp2_pyd_0: ;

clean_nghttp2_pyd_1:
	cd ../python ; \
	rm -fR build dist

$(OBJ_DIR)/r_%.obj: %.c $(THIS_MAKEFILE)
	$(CC) $(CFLAGS_R) $(CFLAGS) -Fo$@ -c $<
	@echo

$(OBJ_DIR)/d_%.obj: %.c $(THIS_MAKEFILE)
	$(CC) $(CFLAGS_D) $(CFLAGS) -Fo$@ -c $<
	@echo

$(OBJ_DIR)/r_nghttp2.res: $(OBJ_DIR)/nghttp2.rc $(THIS_MAKEFILE)
	$(RC) -D_RELEASE -Fo $@ $<
	@echo

$(OBJ_DIR)/d_nghttp2.res: $(OBJ_DIR)/nghttp2.rc $(THIS_MAKEFILE)
	$(RC) -D_DEBUG -Fo $@ $<
	@echo

includes/nghttp2/nghttp2ver.h: includes/nghttp2/nghttp2ver.h.in $(THIS_MAKEFILE)
	sed < includes/nghttp2/nghttp2ver.h.in     \
	     -e 's/@PACKAGE_VERSION@/$(VERSION)/g' \
	     -e 's/@PACKAGE_VERSION_NUM@/$(VERSION_NUM)/g' > $@
	touch --reference=includes/nghttp2/nghttp2ver.h.in $@


define RES_FILE
  #include <winver.h>

  VS_VERSION_INFO VERSIONINFO
    FILEVERSION    $(VER_MAJOR), $(VER_MINOR), $(VER_MICRO), 0
    PRODUCTVERSION $(VER_MAJOR), $(VER_MINOR), $(VER_MICRO), 0
    FILEFLAGSMASK  0x3fL
    FILEOS         0x40004L
    FILETYPE       0x2L
    FILESUBTYPE    0x0L
  #ifdef _DEBUG
    #define        VER_STR  "$(VERSION).0 (MSVC debug)"
    #define        DBG      "d"
    FILEFLAGS      0x1L
  #else
    #define        VER_STR  "$(VERSION).0 (MSVC release)"
    #define        DBG      ""
    FILEFLAGS      0x0L
  #endif
  BEGIN
    BLOCK "StringFileInfo"
    BEGIN
      BLOCK "040904b0"
      BEGIN
        VALUE "CompanyName",      "http://tatsuhiro-t.github.io/nghttp2/"
        VALUE "FileDescription",  "nghttp2; HTTP/2 C library"
        VALUE "FileVersion",      VER_STR
        VALUE "InternalName",     "nghttp2" DBG
        VALUE "LegalCopyright",   "The MIT License"
        VALUE "LegalTrademarks",  ""
        VALUE "OriginalFilename", "nghttp2" DBG ".dll"
        VALUE "ProductName",      "NGHTTP2."
        VALUE "ProductVersion",   VER_STR
      END
    END
  BLOCK "VarFileInfo"
  BEGIN
    VALUE "Translation", 0x409, 1200
  END
  END
endef

export RES_FILE

$(OBJ_DIR)/nghttp2.rc: Makefile.MSVC
	@echo 'Generating $@...'
	@echo ' /* $(GENERATED). DO NOT EDIT.' > $@
	@echo '  */'                          >> $@
	@echo "$$RES_FILE"                    >> $@

clean:
	rm -f $(OBJ_DIR)/* includes/nghttp2/nghttp2ver.h
	@echo

vclean realclean: clean clean_nghttp2_pyd_$(USE_CYTHON)
	- rm -rf $(OBJ_DIR)
	- rm -f .depend.MSVC

#
# Use gcc to generated the dependencies. No MSVC specific args please!
#
REPLACE_R = 's/\(.*\)\.o: /\n$$(OBJ_DIR)\/r_\1.obj: /'
REPLACE_D = 's/\(.*\)\.o: /\n$$(OBJ_DIR)\/d_\1.obj: /'

depend: includes/nghttp2/nghttp2ver.h
	@echo '# $(GENERATED). DO NOT EDIT.' > .depend.MSVC
	gcc -MM $(CFLAGS) $(NGHTTP2_SRC)    >> .depend.tmp
	@echo '#'                           >> .depend.MSVC
	@echo '# Release lib objects:'      >> .depend.MSVC
	sed -e $(REPLACE_R) .depend.tmp     >> .depend.MSVC
	@echo '#'                           >> .depend.MSVC
	@echo '# Debug lib objects:'        >> .depend.MSVC
	sed -e $(REPLACE_D) .depend.tmp     >> .depend.MSVC
	rm -f .depend.tmp

-include .depend.MSVC
