#
# GNU makefile for pycurl (MSVC)
#
# By <gvanem@yahoo.no> 2013.
#

#
# Change $(CURL_ROOT) to suite:
#
CURL_ROOT   = ../..
PYTHON_ROOT = $(realpath $(PYTHONHOME))

#
# Set to '' for RELEASE mode or 'd' for DEBUG.
# LDFLAGS always have '-debug' since it's nice to have a pycurl.pdb
# in RELEASE mode too.
#
DEBUG =

USE_WSOCK_TRACE = 0

VERSION   := $(shell grep.exe --max-count=1 VERSION ../setup.py | cut -d'"' -f2)
THIS_FILE := $(firstword $(MAKEFILE_LIST))

CC     = cl
CFLAGS = -nologo -MD$(DEBUG) -Zi -O2 -W3 -GF \
         -I$(PYTHON_ROOT)/include -I$(CURL_ROOT)/include \
         -D_BIND_TO_CURRENT_VCLIBS_VERSION=1 -DPYCURL_USE_LIBCURL_DLL=1 \
         -DWIN32_LEAN_AND_MEAN -D_WIN32_WINNT=0x0501 # -showIncludes

EX_LIBS += $(CURL_ROOT)/lib/libcurl$(DEBUG)_imp.lib kernel32.lib

ifeq ($(USE_WSOCK_TRACE),1)
  EX_LIBS += wsock_trace.lib
else
  EX_LIBS += ws2_32.lib
endif

LDFLAGS = -nologo -dll -manifest -debug -verbose -map -export:initpycurl

#################################################################################

all: message pycurl.pyd

message:
	@echo 'Building pycurl ver. $(VERSION)'

pycurl.pdb: pycurl.pyd

pycurl.pyd: pycurl.obj manifest.obj pycurl.res
	link $(LDFLAGS) -out:$@ $^ $(EX_LIBS) > tmp.map
	cat tmp.map >> pycurl.map
	mt -nologo -manifest $@.manifest -outputresource:"$@;1"
	rm -f tmp.map
	@echo

#
# Just a test target
#
get_manifest: pycurl.pyd
	mt -nologo -inputresource:pycurl.pyd -out:manifest.txt
	@echo

pycurl.obj: pycurl.c $(addprefix $(CURL_ROOT)/include/curl/, curl.h easy.h multi.h)
	$(CC) $(CFLAGS) -c pycurl.c
	@echo

pycurl.res: $(THIS_FILE)
	@echo "$$PYCURL_RC" > pycurl.rc
	rc -nologo $(DEBUG:d=-dDEBUG) -r pycurl.rc
	@echo

manifest.obj: $(THIS_FILE)
	@echo "$$MANIFEST_C" > manifest.c
	$(CC) $(CFLAGS) -c manifest.c
	@echo

install: pycurl.pyd pycurl.pdb
	cp --update $^ $(PYTHON_ROOT)/DLLs

clean vclean:
	rm -f pycurl.pyd pycurl.obj pycurl.pdb pycurl.exp pycurl.ilk pycurl.lib \
	      pycurl.map pycurl.rc pycurl.res pycurl.pyd.manifest manifest.* \
	      vc100.pdb .depend.MSVC

comma := ,

define PYCURL_RC
  /* This file is generated by $(realpath $(THIS_FILE)).
   * DO NOT EDIT!
   */
  #include <winver.h>

  #define RC_VERSION  $(subst .,$(comma),$(VERSION))
  #define VER_STRING  "$(VERSION)"

  #ifdef DEBUG
    #define DBG_REL   "(debug)"
  #else
    #define DBG_REL   "(release)"
  #endif

  VS_VERSION_INFO VERSIONINFO
    FILEVERSION     RC_VERSION
    PRODUCTVERSION  RC_VERSION
    FILEFLAGSMASK   0x3FL
  #ifdef DEBUG
    FILEFLAGS 1
  #else
    FILEFLAGS 0
  #endif
    FILEOS      VOS__WINDOWS32
    FILETYPE    VFT_DLL
    FILESUBTYPE 0x0L

  BEGIN
    BLOCK "StringFileInfo"
    BEGIN
      BLOCK "040904B0"
      BEGIN
        VALUE "CompanyName",      "Python lib for libcurl"
        VALUE "FileDescription",  "Python lib for libcurl " DBG_REL
        VALUE "FileVersion",      VER_STRING
        VALUE "InternalName",     "pycurl " DBG_REL
        VALUE "OriginalFilename", "pycurl.pyd"
        VALUE "ProductName",      "Python bindings for libcurl"
        VALUE "ProductVersion",   VER_STRING " (MSVC)"
        VALUE "LegalCopyright",   "LGPL and an MIT/X derivative license.\r\n"
                                  "\t\t\tCopyright (C) 2001-2008 Kjetil Jacobsen.\r\n"
                                  "\t\t\tCopyright (C) 2001-2008 Markus F.X.J. Oberhumer."
      END
    END

    BLOCK "VarFileInfo"
    BEGIN
      VALUE "Translation", 0x409, 1200
    END
  END

  #define MANIFEST_RESOURCE_ID 1
  #define RT_MANIFEST 24

  #if 0
    /* How to solve this problem: The below manifest is generated in the
     * link step. The link step also needs the .res file. Not sure a
     * manifest in a .res-file is any point.
     */
    MANIFEST_RESOURCE_ID RT_MANIFEST "pycurl.pyd.manifest"
  #endif
endef

define MANIFEST_C
  #pragma comment (linker, "/manifestdependency:type='win32'")
  #pragma comment (linker, "/manifestdependency:name='Microsoft.VC90.CRT'")
  #pragma comment (linker, "/manifestdependency:version='9.0.30411.0'")
  #pragma comment (linker, "/manifestdependency:processorArchitecture='x86'")
  #pragma comment (linker, "/manifestdependency:publicKeyToken='1fc8b3b9a1e18e3b'")
endef

export PYCURL_RC MANIFEST_C

depend: $(THIS_FILE)
	gcc -MM -I$(PYTHON_ROOT)/include -I$(CURL_ROOT)/include pycurl.c > .depend.MSVC

-include .depend.MSVC
