#***************************************************************************
#                                  _   _ ____  _
#  Project                     ___| | | |  _ \| |
#                             / __| | | | |_) | |
#                            | (__| |_| |  _ <| |___
#                             \___|\___/|_| \_\_____|
#
# Copyright (C) 1999 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at https://curl.haxx.se/docs/copyright.html.
#
# You may opt to use, copy, modify, merge, publish, distribute and/or sell
# copies of the Software, and permit persons to whom the Software is
# furnished to do so, under the terms of the COPYING file.
#
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
# KIND, either express or implied.
#
#***************************************************************************

###########################################################################
#
## Makefile for building libcurl.a with MingW (GCC-3.2 or later or LLVM/Clang)
## and optionally OpenSSL (1.0.2a), libssh2 (1.5), zlib (1.2.8), librtmp (2.4),
## brotli (1.0.1)
##
## Usage:   mingw32-make -f Makefile.m32 CFG=-feature1[-feature2][-feature3][...]
## Example: mingw32-make -f Makefile.m32 CFG=-zlib-ssl-sspi-winidn
##
## Hint: you can also set environment vars to control the build, f.e.:
## set ZLIB_PATH=c:/zlib-1.2.8
## set ZLIB=1
#
###########################################################################

# Edit the path below to point to the base of your Zlib sources.
ifndef ZLIB_PATH
ZLIB_PATH = ../../zlib-1.2.8
endif
# Edit the path below to point to the base of your Brotli sources.
ifndef BROTLI_PATH
BROTLI_PATH = ../../brotli-1.0.1
endif
# Edit the path below to point to the base of your OpenSSL package.
ifndef OPENSSL_PATH
OPENSSL_PATH = ../../openssl-1.0.2a
endif
# Edit the path below to point to the base of your LibSSH2 package.
ifndef LIBSSH2_PATH
LIBSSH2_PATH = ../../libssh2-1.5.0
endif
# Edit the path below to point to the base of your librtmp package.
ifndef LIBRTMP_PATH
LIBRTMP_PATH = ../../librtmp-2.4
endif
# Edit the path below to point to the base of your libidn2 package.
ifndef LIBIDN2_PATH
LIBIDN2_PATH = ../../libidn2-2.0.3
endif
# Edit the path below to point to the base of your MS IDN package.
# Microsoft Internationalized Domain Names (IDN) Mitigation APIs 1.1
# https://www.microsoft.com/en-us/download/details.aspx?id=734
ifndef WINIDN_PATH
WINIDN_PATH = ../../Microsoft IDN Mitigation APIs
endif
# Edit the path below to point to the base of your Novell LDAP NDK.
ifndef LDAP_SDK
LDAP_SDK = c:/novell/ndk/cldapsdk/win32
endif
# Edit the path below to point to the base of your nghttp2 package.
ifndef NGHTTP2_PATH
NGHTTP2_PATH = ../../nghttp2-1.0.0
endif

PROOT = ..

# Edit the path below to point to the base of your c-ares package.
ifndef LIBCARES_PATH
LIBCARES_PATH = $(PROOT)/ares
endif

ifeq ($(CURL_CC),)
CURL_CC := $(CROSSPREFIX)gcc
endif
ifeq ($(CURL_AR),)
CURL_AR := $(CROSSPREFIX)ar
endif
ifeq ($(CURL_RANLIB),)
CURL_RANLIB := $(CROSSPREFIX)ranlib
endif

CC = $(CURL_CC)
CFLAGS = $(CURL_CFLAG_EXTRAS) -g -O2 -Wall -W
CFLAGS += -fno-strict-aliasing
# comment LDFLAGS below to keep debug info
LDFLAGS = $(CURL_LDFLAG_EXTRAS) $(CURL_LDFLAG_EXTRAS_DLL) -s
AR = $(CURL_AR)
RANLIB = $(CURL_RANLIB)
RC = $(CROSSPREFIX)windres
RCFLAGS = --include-dir=$(PROOT)/include -DDEBUGBUILD=0 -O COFF
STRIP   = $(CROSSPREFIX)strip -g

# Set environment var ARCH to your architecture to override autodetection.
ifndef ARCH
ifeq ($(findstring x86_64,$(shell $(CC) -dumpmachine)),x86_64)
ARCH = w64
else
ARCH = w32
endif
endif

ifeq ($(ARCH),w64)
CFLAGS  += -m64 -D_AMD64_
LDFLAGS += -m64
RCFLAGS += -F pe-x86-64
else
CFLAGS  += -m32
LDFLAGS += -m32
RCFLAGS += -F pe-i386
endif

# Platform-dependent helper tool macros
ifeq ($(findstring /sh,$(SHELL)),/sh)
DEL     = rm -f $1
RMDIR   = rm -fr $1
MKDIR   = mkdir -p $1
COPY    = -cp -afv $1 $2
#COPYR  = -cp -afr $1/* $2
COPYR   = -rsync -aC $1/* $2
TOUCH   = touch $1
CAT     = cat
ECHONL  = echo ""
DL = '
else
ifeq "$(OS)" "Windows_NT"
DEL     = -del 2>NUL /q /f $(subst /,\,$1)
RMDIR   = -rd 2>NUL /q /s $(subst /,\,$1)
else
DEL     = -del 2>NUL $(subst /,\,$1)
RMDIR   = -deltree 2>NUL /y $(subst /,\,$1)
endif
MKDIR   = -md 2>NUL $(subst /,\,$1)
COPY    = -copy 2>NUL /y $(subst /,\,$1) $(subst /,\,$2)
COPYR   = -xcopy 2>NUL /q /y /e $(subst /,\,$1) $(subst /,\,$2)
TOUCH   = copy 2>&1>NUL /b $(subst /,\,$1) +,,
CAT     = type
ECHONL  = $(ComSpec) /c echo.
endif

########################################################
## Nothing more to do below this line!

ifeq ($(findstring -dyn,$(CFG)),-dyn)
DYN = 1
endif
ifeq ($(findstring -ares,$(CFG)),-ares)
ARES = 1
endif
ifeq ($(findstring -sync,$(CFG)),-sync)
SYNC = 1
endif
ifeq ($(findstring -rtmp,$(CFG)),-rtmp)
RTMP = 1
SSL = 1
ZLIB = 1
endif
ifeq ($(findstring -ssh2,$(CFG)),-ssh2)
SSH2 = 1
SSL = 1
ZLIB = 1
endif
ifeq ($(findstring -ssl,$(CFG)),-ssl)
SSL = 1
endif
ifeq ($(findstring -srp,$(CFG)),-srp)
SRP = 1
endif
ifeq ($(findstring -zlib,$(CFG)),-zlib)
ZLIB = 1
endif
ifeq ($(findstring -brotli,$(CFG)),-brotli)
BROTLI = 1
endif
ifeq ($(findstring -idn2,$(CFG)),-idn2)
IDN2 = 1
endif
ifeq ($(findstring -winidn,$(CFG)),-winidn)
WINIDN = 1
endif
ifeq ($(findstring -sspi,$(CFG)),-sspi)
SSPI = 1
endif
ifeq ($(findstring -ldaps,$(CFG)),-ldaps)
LDAPS = 1
endif
ifeq ($(findstring -ipv6,$(CFG)),-ipv6)
IPV6 = 1
endif
ifeq ($(findstring -winssl,$(CFG)),-winssl)
WINSSL = 1
SSPI = 1
endif
ifeq ($(findstring -nghttp2,$(CFG)),-nghttp2)
NGHTTP2 = 1
endif

INCLUDES = -I. -I../include
CFLAGS += -DBUILDING_LIBCURL
ifdef SSL
  ifdef WINSSL
    CFLAGS += -DCURL_WITH_MULTI_SSL
  endif
endif

ifdef SYNC
  CFLAGS += -DUSE_SYNC_DNS
else
  ifdef ARES
    INCLUDES += -I"$(LIBCARES_PATH)"
    CFLAGS += -DUSE_ARES -DCARES_STATICLIB
    DLL_LIBS += -L"$(LIBCARES_PATH)" -lcares
    libcurl_dll_DEPENDENCIES = $(LIBCARES_PATH)/libcares.a
  endif
endif
ifdef RTMP
  INCLUDES += -I"$(LIBRTMP_PATH)"
  CFLAGS += -DUSE_LIBRTMP
  DLL_LIBS += -L"$(LIBRTMP_PATH)/librtmp" -lrtmp -lwinmm
endif
ifdef NGHTTP2
  INCLUDES += -I"$(NGHTTP2_PATH)/include"
  CFLAGS += -DUSE_NGHTTP2
  DLL_LIBS += -L"$(NGHTTP2_PATH)/lib" -lnghttp2
endif
ifdef SSH2
  INCLUDES += -I"$(LIBSSH2_PATH)/include" -I"$(LIBSSH2_PATH)/win32"
  CFLAGS += -DUSE_LIBSSH2 -DHAVE_LIBSSH2_H
  DLL_LIBS += -L"$(LIBSSH2_PATH)/win32" -lssh2
  ifdef WINSSL
    ifndef DYN
      DLL_LIBS += -lbcrypt -lcrypt32
    endif
  endif
endif
ifdef SSL
  ifndef OPENSSL_INCLUDE
    ifeq "$(wildcard $(OPENSSL_PATH)/outinc)" "$(OPENSSL_PATH)/outinc"
      OPENSSL_INCLUDE = $(OPENSSL_PATH)/outinc
    endif
    ifeq "$(wildcard $(OPENSSL_PATH)/include)" "$(OPENSSL_PATH)/include"
      OPENSSL_INCLUDE = $(OPENSSL_PATH)/include
    endif
  endif
  ifneq "$(wildcard $(OPENSSL_INCLUDE)/openssl/opensslv.h)" "$(OPENSSL_INCLUDE)/openssl/opensslv.h"
  $(error Invalid path to OpenSSL package: $(OPENSSL_PATH))
  endif
  ifndef OPENSSL_LIBPATH
    ifeq "$(wildcard $(OPENSSL_PATH)/out)" "$(OPENSSL_PATH)/out"
      OPENSSL_LIBPATH = $(OPENSSL_PATH)/out
      OPENSSL_LIBS = -leay32 -lssl32
    endif
    ifeq "$(wildcard $(OPENSSL_PATH)/lib)" "$(OPENSSL_PATH)/lib"
      OPENSSL_LIBPATH = $(OPENSSL_PATH)/lib
      OPENSSL_LIBS = -lcrypto -lssl
    endif
  endif
  ifndef DYN
    OPENSSL_LIBS += -lgdi32 -lcrypt32
  endif
  INCLUDES += -I"$(OPENSSL_INCLUDE)"
  CFLAGS += -DUSE_OPENSSL -DHAVE_OPENSSL_ENGINE_H -DHAVE_OPENSSL_PKCS12_H \
            -DHAVE_ENGINE_LOAD_BUILTIN_ENGINES -DOPENSSL_NO_KRB5
  DLL_LIBS += -L"$(OPENSSL_LIBPATH)" $(OPENSSL_LIBS)
  ifdef SRP
    ifeq "$(wildcard $(OPENSSL_INCLUDE)/openssl/srp.h)" "$(OPENSSL_INCLUDE)/openssl/srp.h"
      CFLAGS += -DHAVE_OPENSSL_SRP -DUSE_TLS_SRP
    endif
  endif
endif
ifdef WINSSL
  CFLAGS += -DUSE_SCHANNEL
  DLL_LIBS += -lcrypt32
endif
ifdef ZLIB
  INCLUDES += -I"$(ZLIB_PATH)"
  CFLAGS += -DHAVE_LIBZ -DHAVE_ZLIB_H
  DLL_LIBS += -L"$(ZLIB_PATH)" -lz
endif
ifdef BROTLI
  INCLUDES += -I"$(BROTLI_PATH)/include"
  CFLAGS += -DHAVE_BROTLI
  DLL_LIBS += -L"$(BROTLI_PATH)/lib"
  ifdef BROTLI_LIBS
    DLL_LIBS += $(BROTLI_LIBS)
  else
    DLL_LIBS += -lbrotlidec
  endif
endif
ifdef IDN2
  INCLUDES += -I"$(LIBIDN2_PATH)/include"
  CFLAGS += -DUSE_LIBIDN2
  DLL_LIBS += -L"$(LIBIDN2_PATH)/lib" -lidn2
else
ifdef WINIDN
  CFLAGS += -DUSE_WIN32_IDN
  CFLAGS += -DWANT_IDN_PROTOTYPES
  DLL_LIBS += -L"$(WINIDN_PATH)" -lnormaliz
endif
endif
ifdef SSPI
  CFLAGS += -DUSE_WINDOWS_SSPI
endif
ifdef SPNEGO
  CFLAGS += -DHAVE_SPNEGO
endif
ifdef IPV6
  CFLAGS += -DENABLE_IPV6 -D_WIN32_WINNT=0x0501
endif
ifdef LDAPS
  CFLAGS += -DHAVE_LDAP_SSL
endif
ifdef USE_LDAP_NOVELL
  INCLUDES += -I"$(LDAP_SDK)/inc"
  CFLAGS += -DCURL_HAS_NOVELL_LDAPSDK
  DLL_LIBS += -L"$(LDAP_SDK)/lib/mscvc" -lldapsdk -lldapssl -lldapx
endif
ifdef USE_LDAP_OPENLDAP
  INCLUDES += -I"$(LDAP_SDK)/include"
  CFLAGS += -DCURL_HAS_OPENLDAP_LDAPSDK
  DLL_LIBS += -L"$(LDAP_SDK)/lib" -lldap -llber
endif
ifndef USE_LDAP_NOVELL
ifndef USE_LDAP_OPENLDAP
  DLL_LIBS += -lwldap32
endif
endif
DLL_LIBS += -lws2_32

# Makefile.inc provides the CSOURCES and HHEADERS defines
include Makefile.inc

ifeq ($(CURL_DLL_A_SUFFIX),)
CURL_DLL_A_SUFFIX := dll
endif

libcurl_dll_LIBRARY = libcurl$(CURL_DLL_SUFFIX).dll
libcurl_dll_a_LIBRARY = libcurl$(CURL_DLL_A_SUFFIX).a
libcurl_a_LIBRARY = libcurl.a

libcurl_a_OBJECTS := $(patsubst %.c,%.o,$(strip $(CSOURCES)))
libcurl_a_DEPENDENCIES := $(strip $(CSOURCES) $(HHEADERS))

RESOURCE = libcurl.res


all: $(libcurl_a_LIBRARY) $(libcurl_dll_LIBRARY)

$(libcurl_a_LIBRARY): $(libcurl_a_OBJECTS) $(libcurl_a_DEPENDENCIES)
	@$(call DEL, $@)
	$(AR) cru $@ $(libcurl_a_OBJECTS)
	$(RANLIB) $@
	$(STRIP) $@

# remove the last line above to keep debug info

$(libcurl_dll_LIBRARY): $(libcurl_a_OBJECTS) $(RESOURCE) $(libcurl_dll_DEPENDENCIES)
	@$(call DEL, $@)
	$(CC) $(LDFLAGS) -shared -o $@ \
	  -Wl,--output-def,$(@:.dll=.def),--out-implib,$(libcurl_dll_a_LIBRARY) \
	  $(libcurl_a_OBJECTS) $(RESOURCE) $(DLL_LIBS)

%.o: %.c
	$(CC) $(INCLUDES) $(CFLAGS) -c $< -o $@

%.res: %.rc
	$(RC) $(RCFLAGS) -i $< -o $@

clean:
	@$(call DEL, $(libcurl_a_OBJECTS) $(RESOURCE))

distclean vclean: clean
	@$(call DEL, $(libcurl_a_LIBRARY) $(libcurl_dll_LIBRARY) $(libcurl_dll_LIBRARY:.dll=.def) $(libcurl_dll_a_LIBRARY))

$(LIBCARES_PATH)/libcares.a:
	$(MAKE) -C $(LIBCARES_PATH) -f Makefile.m32