• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#***************************************************************************
2#                                  _   _ ____  _
3#  Project                     ___| | | |  _ \| |
4#                             / __| | | | |_) | |
5#                            | (__| |_| |  _ <| |___
6#                             \___|\___/|_| \_\_____|
7#
8# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9#
10# This software is licensed as described in the file COPYING, which
11# you should have received as part of this distribution. The terms
12# are also available at https://curl.se/docs/copyright.html.
13#
14# You may opt to use, copy, modify, merge, publish, distribute and/or sell
15# copies of the Software, and permit persons to whom the Software is
16# furnished to do so, under the terms of the COPYING file.
17#
18# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19# KIND, either express or implied.
20#
21# SPDX-License-Identifier: curl
22#
23#***************************************************************************
24
25# Makefile to build curl parts with GCC-like toolchains and optional features.
26#
27# Usage:   [mingw32-]make -f Makefile.mk CFG=-feat1[-feat2][-feat3][...]
28# Example: [mingw32-]make -f Makefile.mk CFG=-zlib-ssl-libssh2-ipv6
29#
30# Look for ' ?=' to find all accepted customization variables.
31
32# This script is reused by 'src' and 'docs/examples' Makefile.mk scripts.
33
34ifndef PROOT
35  PROOT := ..
36  LOCAL := 1
37endif
38
39### Common
40
41CFLAGS ?=
42CPPFLAGS ?=
43RCFLAGS ?=
44LDFLAGS ?=
45CURL_LDFLAGS_BIN ?=
46CURL_LDFLAGS_LIB ?=
47LIBS ?=
48
49CROSSPREFIX ?=
50
51ifeq ($(CC),cc)
52  CC := gcc
53endif
54CC := $(CROSSPREFIX)$(CC)
55AR := $(CROSSPREFIX)$(AR)
56RC ?= $(CROSSPREFIX)windres
57
58# For compatibility
59ARCH ?=
60ifeq ($(ARCH),w64)
61  TRIPLET := x86_64-w64-mingw32
62  CFLAGS  += -m64
63  LDFLAGS += -m64
64  RCFLAGS += --target=pe-x86-64
65else ifdef ARCH
66  TRIPLET := i686-w64-mingw32
67  CFLAGS  += -m32
68  LDFLAGS += -m32
69  RCFLAGS += --target=pe-i386
70else
71  TRIPLET ?= $(shell $(CC) -dumpmachine)
72endif
73
74BIN_EXT := .exe
75
76ifneq ($(findstring -w,$(TRIPLET)),)
77  WIN32 := 1
78else ifneq ($(findstring msdos,$(TRIPLET)),)
79  # Cross-tools: https://github.com/andrewwutw/build-djgpp
80  MSDOS := 1
81else ifneq ($(findstring amigaos,$(TRIPLET)),)
82  # Cross-tools: https://github.com/bebbo/amiga-gcc
83  AMIGA := 1
84endif
85
86CPPFLAGS += -I. -I$(PROOT)/include
87RCFLAGS  += -I$(PROOT)/include
88
89ifndef WIN32
90  DYN :=
91endif
92
93ifdef AMIGA
94  BIN_EXT :=
95endif
96
97### Deprecated settings. For compatibility.
98
99ifdef WATT_ROOT
100  WATT_PATH := $(realpath $(WATT_ROOT))
101endif
102
103### Optional features
104
105ifneq ($(findstring -debug,$(CFG)),)
106  CFLAGS += -g
107  CPPFLAGS += -DDEBUGBUILD
108else
109  CPPFLAGS += -DNDEBUG
110endif
111ifneq ($(findstring -trackmem,$(CFG)),)
112  CPPFLAGS += -DCURLDEBUG
113endif
114ifneq ($(findstring -map,$(CFG)),)
115  MAP := 1
116endif
117
118ifdef WIN32
119  ifneq ($(findstring -unicode,$(CFG)),)
120    CPPFLAGS += -DUNICODE -D_UNICODE
121    CURL_LDFLAGS_BIN += -municode
122  endif
123endif
124
125# CPPFLAGS below are only necessary when building libcurl via 'lib' (see
126# comments below about exceptions). Always include them anyway to match
127# behavior of other build systems.
128
129# Linker options to exclude for shared mode executables.
130_LDFLAGS :=
131_LIBS :=
132
133ifneq ($(findstring -sync,$(CFG)),)
134  CPPFLAGS += -DUSE_SYNC_DNS
135else ifneq ($(findstring -ares,$(CFG)),)
136  LIBCARES_PATH ?= $(PROOT)/../c-ares
137  CPPFLAGS += -DUSE_ARES
138  CPPFLAGS += -I"$(LIBCARES_PATH)/include"
139  _LDFLAGS += -L"$(LIBCARES_PATH)/lib"
140  _LIBS += -lcares
141endif
142
143ifneq ($(findstring -rtmp,$(CFG)),)
144  LIBRTMP_PATH ?= $(PROOT)/../librtmp
145  CPPFLAGS += -DUSE_LIBRTMP
146  CPPFLAGS += -I"$(LIBRTMP_PATH)"
147  _LDFLAGS += -L"$(LIBRTMP_PATH)/librtmp"
148  _LIBS += -lrtmp -lwinmm
149  ZLIB := 1
150endif
151
152ifneq ($(findstring -ssh2,$(CFG)),)
153  LIBSSH2_PATH ?= $(PROOT)/../libssh2
154  CPPFLAGS += -DUSE_LIBSSH2
155  CPPFLAGS += -I"$(LIBSSH2_PATH)/include"
156  _LDFLAGS += -L"$(LIBSSH2_PATH)/lib"
157  ifdef WIN32
158    _LDFLAGS += -L"$(LIBSSH2_PATH)/win32"
159  endif
160  _LIBS += -lssh2
161else ifneq ($(findstring -libssh,$(CFG)),)
162  LIBSSH_PATH ?= $(PROOT)/../libssh
163  CPPFLAGS += -DUSE_LIBSSH
164  CPPFLAGS += -I"$(LIBSSH_PATH)/include"
165  _LDFLAGS += -L"$(LIBSSH_PATH)/lib"
166  _LIBS += -lssh
167else ifneq ($(findstring -wolfssh,$(CFG)),)
168  WOLFSSH_PATH ?= $(PROOT)/../wolfssh
169  CPPFLAGS += -DUSE_WOLFSSH
170  CPPFLAGS += -I"$(WOLFSSH_PATH)/include"
171  _LDFLAGS += -L"$(WOLFSSH_PATH)/lib"
172  _LIBS += -lwolfssh
173endif
174
175ifneq ($(findstring -ssl,$(CFG)),)
176  OPENSSL_PATH ?= $(PROOT)/../openssl
177  CPPFLAGS += -DUSE_OPENSSL
178  CPPFLAGS += -DCURL_DISABLE_OPENSSL_AUTO_LOAD_CONFIG
179  OPENSSL_INCLUDE ?= $(OPENSSL_PATH)/include
180  OPENSSL_LIBPATH ?= $(OPENSSL_PATH)/lib
181  CPPFLAGS += -I"$(OPENSSL_INCLUDE)"
182  _LDFLAGS += -L"$(OPENSSL_LIBPATH)"
183  OPENSSL_LIBS ?= -lssl -lcrypto
184  _LIBS += $(OPENSSL_LIBS)
185
186  ifneq ($(findstring -srp,$(CFG)),)
187    ifneq ($(wildcard $(OPENSSL_INCLUDE)/openssl/srp.h),)
188      # OpenSSL 1.0.1 and later.
189      CPPFLAGS += -DHAVE_OPENSSL_SRP -DUSE_TLS_SRP
190    endif
191  endif
192  SSLLIBS += 1
193endif
194ifneq ($(findstring -wolfssl,$(CFG)),)
195  WOLFSSL_PATH ?= $(PROOT)/../wolfssl
196  CPPFLAGS += -DUSE_WOLFSSL
197  CPPFLAGS += -DSIZEOF_LONG_LONG=8
198  CPPFLAGS += -I"$(WOLFSSL_PATH)/include"
199  _LDFLAGS += -L"$(WOLFSSL_PATH)/lib"
200  _LIBS += -lwolfssl
201  SSLLIBS += 1
202endif
203ifneq ($(findstring -mbedtls,$(CFG)),)
204  MBEDTLS_PATH ?= $(PROOT)/../mbedtls
205  CPPFLAGS += -DUSE_MBEDTLS
206  CPPFLAGS += -I"$(MBEDTLS_PATH)/include"
207  _LDFLAGS += -L"$(MBEDTLS_PATH)/lib"
208  _LIBS += -lmbedtls -lmbedx509 -lmbedcrypto
209  SSLLIBS += 1
210endif
211ifneq ($(findstring -schannel,$(CFG)),)
212  CPPFLAGS += -DUSE_SCHANNEL
213  SSLLIBS += 1
214endif
215
216ifneq ($(findstring -nghttp2,$(CFG)),)
217  NGHTTP2_PATH ?= $(PROOT)/../nghttp2
218  CPPFLAGS += -DUSE_NGHTTP2
219  CPPFLAGS += -I"$(NGHTTP2_PATH)/include"
220  _LDFLAGS += -L"$(NGHTTP2_PATH)/lib"
221  _LIBS += -lnghttp2
222endif
223
224ifeq ($(findstring -nghttp3,$(CFG))$(findstring -ngtcp2,$(CFG)),-nghttp3-ngtcp2)
225  NGHTTP3_PATH ?= $(PROOT)/../nghttp3
226  CPPFLAGS += -DUSE_NGHTTP3
227  CPPFLAGS += -I"$(NGHTTP3_PATH)/include"
228  _LDFLAGS += -L"$(NGHTTP3_PATH)/lib"
229  _LIBS += -lnghttp3
230
231  NGTCP2_PATH ?= $(PROOT)/../ngtcp2
232  CPPFLAGS += -DUSE_NGTCP2
233  CPPFLAGS += -I"$(NGTCP2_PATH)/include"
234  _LDFLAGS += -L"$(NGTCP2_PATH)/lib"
235
236  NGTCP2_LIBS ?=
237  ifeq ($(NGTCP2_LIBS),)
238    ifneq ($(findstring -ssl,$(CFG)),)
239      ifneq ($(wildcard $(OPENSSL_INCLUDE)/openssl/aead.h),)
240        NGTCP2_LIBS := -lngtcp2_crypto_boringssl
241      else  # including libressl
242        NGTCP2_LIBS := -lngtcp2_crypto_quictls
243      endif
244    else ifneq ($(findstring -wolfssl,$(CFG)),)
245      NGTCP2_LIBS := -lngtcp2_crypto_wolfssl
246    endif
247  endif
248
249  _LIBS += -lngtcp2 $(NGTCP2_LIBS)
250endif
251
252ifneq ($(findstring -zlib,$(CFG))$(ZLIB),)
253  ZLIB_PATH ?= $(PROOT)/../zlib
254  # These CPPFLAGS are also required when compiling the curl tool via 'src'.
255  CPPFLAGS += -DHAVE_LIBZ
256  CPPFLAGS += -I"$(ZLIB_PATH)/include"
257  _LDFLAGS += -L"$(ZLIB_PATH)/lib"
258  ZLIB_LIBS ?= -lz
259  _LIBS += $(ZLIB_LIBS)
260  ZLIB := 1
261endif
262ifneq ($(findstring -zstd,$(CFG)),)
263  ZSTD_PATH ?= $(PROOT)/../zstd
264  CPPFLAGS += -DHAVE_ZSTD
265  CPPFLAGS += -I"$(ZSTD_PATH)/include"
266  _LDFLAGS += -L"$(ZSTD_PATH)/lib"
267  ZSTD_LIBS ?= -lzstd
268  _LIBS += $(ZSTD_LIBS)
269endif
270ifneq ($(findstring -brotli,$(CFG)),)
271  BROTLI_PATH ?= $(PROOT)/../brotli
272  CPPFLAGS += -DHAVE_BROTLI
273  CPPFLAGS += -I"$(BROTLI_PATH)/include"
274  _LDFLAGS += -L"$(BROTLI_PATH)/lib"
275  BROTLI_LIBS ?= -lbrotlidec -lbrotlicommon
276  _LIBS += $(BROTLI_LIBS)
277endif
278ifneq ($(findstring -gsasl,$(CFG)),)
279  LIBGSASL_PATH ?= $(PROOT)/../gsasl
280  CPPFLAGS += -DUSE_GSASL
281  CPPFLAGS += -I"$(LIBGSASL_PATH)/include"
282  _LDFLAGS += -L"$(LIBGSASL_PATH)/lib"
283  _LIBS += -lgsasl
284endif
285
286ifneq ($(findstring -idn2,$(CFG)),)
287  LIBIDN2_PATH ?= $(PROOT)/../libidn2
288  CPPFLAGS += -DUSE_LIBIDN2
289  CPPFLAGS += -I"$(LIBIDN2_PATH)/include"
290  _LDFLAGS += -L"$(LIBIDN2_PATH)/lib"
291  _LIBS += -lidn2
292
293ifneq ($(findstring -psl,$(CFG)),)
294  LIBPSL_PATH ?= $(PROOT)/../libpsl
295  CPPFLAGS += -DUSE_LIBPSL
296  CPPFLAGS += -I"$(LIBPSL_PATH)/include"
297  _LDFLAGS += -L"$(LIBPSL_PATH)/lib"
298  _LIBS += -lpsl
299endif
300else ifneq ($(findstring -winidn,$(CFG)),)
301  CPPFLAGS += -DUSE_WIN32_IDN
302  _LIBS += -lnormaliz
303endif
304
305ifneq ($(findstring -sspi,$(CFG)),)
306  ifdef WIN32
307    CPPFLAGS += -DUSE_WINDOWS_SSPI
308  endif
309endif
310ifneq ($(findstring -ipv6,$(CFG)),)
311  CPPFLAGS += -DENABLE_IPV6
312endif
313
314ifneq ($(findstring -watt,$(CFG))$(MSDOS),)
315  WATT_PATH ?= $(PROOT)/../watt
316  CPPFLAGS += -I"$(WATT_PATH)/inc"
317  _LDFLAGS += -L"$(WATT_PATH)/lib"
318  _LIBS += -lwatt
319endif
320
321ifdef WIN32
322  ifeq ($(findstring -lldap,$(LIBS)),)
323    _LIBS += -lwldap32
324  endif
325  _LIBS += -lws2_32 -lcrypt32 -lbcrypt
326endif
327
328ifneq ($(findstring 11,$(subst $(subst ,, ),,$(SSLLIBS))),)
329  CPPFLAGS += -DCURL_WITH_MULTI_SSL
330endif
331
332ifndef DYN
333  LDFLAGS += $(_LDFLAGS)
334  LIBS += $(_LIBS)
335endif
336
337### Common rules
338
339OBJ_DIR := $(TRIPLET)
340
341ifneq ($(findstring /sh,$(SHELL)),)
342DEL   = rm -f $1
343COPY  = -cp -afv $1 $2
344MKDIR = mkdir -p $1
345RMDIR = rm -fr $1
346WHICH = $(SHELL) -c "command -v $1"
347else
348DEL   = -del 2>NUL /q /f $(subst /,\,$1)
349COPY  = -copy 2>NUL /y $(subst /,\,$1) $(subst /,\,$2)
350MKDIR = -md 2>NUL $(subst /,\,$1)
351RMDIR = -rd 2>NUL /q /s $(subst /,\,$1)
352WHICH = where $1
353endif
354
355all: $(TARGETS)
356
357$(OBJ_DIR):
358	-$(call MKDIR, $(OBJ_DIR))
359
360$(OBJ_DIR)/%.o: %.c
361	$(CC) -W -Wall $(CFLAGS) $(CPPFLAGS) -c $< -o $@
362
363$(OBJ_DIR)/%.res: %.rc
364	$(RC) -O coff $(RCFLAGS) -i $< -o $@
365
366clean:
367	@$(call DEL, $(TOCLEAN))
368	@$(RMDIR) $(OBJ_DIR)
369
370distclean vclean: clean
371	@$(call DEL, $(TARGETS) $(TOVCLEAN))
372
373### Local
374
375ifdef LOCAL
376
377CPPFLAGS += -DBUILDING_LIBCURL
378ifdef WIN32
379CPPFLAGS += -DCURL_STATICLIB
380endif
381
382### Sources and targets
383
384# Provides CSOURCES, HHEADERS, LIB_RCFILES
385include Makefile.inc
386
387vpath %.c vauth vquic vssh vtls
388
389libcurl_a_LIBRARY := libcurl.a
390ifdef WIN32
391CURL_DLL_SUFFIX ?=
392libcurl_dll_LIBRARY := libcurl$(CURL_DLL_SUFFIX).dll
393libcurl_dll_a_LIBRARY := libcurl.dll.a
394ifeq ($(findstring -trackmem,$(CFG)),)
395CURL_LDFLAGS_LIB += $(PROOT)/libcurl.def
396endif
397ifdef MAP
398libcurl_map_LIBRARY := libcurl$(CURL_DLL_SUFFIX).map
399CURL_LDFLAGS_LIB += -Wl,-Map,$(libcurl_map_LIBRARY)
400endif
401endif
402
403TARGETS := $(libcurl_a_LIBRARY) $(libcurl_dll_LIBRARY)
404
405libcurl_a_OBJECTS := $(patsubst %.c,$(OBJ_DIR)/%.o,$(notdir $(strip $(CSOURCES))))
406libcurl_a_DEPENDENCIES := $(strip $(CSOURCES) $(HHEADERS))
407ifdef WIN32
408libcurl_dll_OBJECTS := $(libcurl_a_OBJECTS)
409libcurl_dll_OBJECTS += $(patsubst %.rc,$(OBJ_DIR)/%.res,$(strip $(LIB_RCFILES)))
410endif
411
412TOCLEAN := $(libcurl_dll_OBJECTS)
413TOVCLEAN := $(libcurl_dll_LIBRARY:.dll=.def) $(libcurl_dll_a_LIBRARY) $(libcurl_map_LIBRARY)
414
415### Rules
416
417$(libcurl_a_LIBRARY): $(libcurl_a_OBJECTS) $(libcurl_a_DEPENDENCIES)
418	@$(call DEL, $@)
419	$(AR) rcs $@ $(libcurl_a_OBJECTS)
420
421$(libcurl_dll_LIBRARY): $(libcurl_dll_OBJECTS)
422	$(CC) $(LDFLAGS) -shared $(CURL_LDFLAGS_LIB) -o $@ $(libcurl_dll_OBJECTS) $(LIBS) \
423	  -Wl,--output-def,$(@:.dll=.def),--out-implib,$(libcurl_dll_a_LIBRARY)
424
425all: $(OBJ_DIR) $(TARGETS)
426endif
427