xref: /curl/lib/Makefile.mk (revision e411c98f)
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:   make -f Makefile.mk CFG=-feat1[-feat2][-feat3][...]
28# Example: 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 ?=
43LDFLAGS ?=
44LIBS ?=
45
46CROSSPREFIX ?=
47
48ifeq ($(CC),cc)
49  CC := gcc
50endif
51CC := $(CROSSPREFIX)$(CC)
52AR := $(CROSSPREFIX)$(AR)
53
54TRIPLET ?= $(shell $(CC) -dumpmachine)
55
56BIN_EXT :=
57
58ifneq ($(findstring msdos,$(TRIPLET)),)
59  # Cross-tools: https://github.com/andrewwutw/build-djgpp
60  MSDOS := 1
61  BIN_EXT := .exe
62else ifneq ($(findstring amigaos,$(TRIPLET)),)
63  # Cross-tools: https://github.com/bebbo/amiga-gcc
64  AMIGA := 1
65endif
66
67CPPFLAGS += -I. -I$(PROOT)/include
68
69### Deprecated settings. For compatibility.
70
71ifdef WATT_ROOT
72  WATT_PATH := $(realpath $(WATT_ROOT))
73endif
74
75### Optional features
76
77ifneq ($(findstring -debug,$(CFG)),)
78  CFLAGS += -g
79  CPPFLAGS += -DDEBUGBUILD
80else
81  CPPFLAGS += -DNDEBUG
82endif
83ifneq ($(findstring -trackmem,$(CFG)),)
84  CPPFLAGS += -DCURLDEBUG
85endif
86ifneq ($(findstring -map,$(CFG)),)
87  MAP := 1
88endif
89
90# CPPFLAGS below are only necessary when building libcurl via 'lib' (see
91# comments below about exceptions). Always include them anyway to match
92# behavior of other build systems.
93
94ifneq ($(findstring -sync,$(CFG)),)
95  CPPFLAGS += -DUSE_SYNC_DNS
96else ifneq ($(findstring -ares,$(CFG)),)
97  LIBCARES_PATH ?= $(PROOT)/../c-ares
98  CPPFLAGS += -DUSE_ARES
99  CPPFLAGS += -I"$(LIBCARES_PATH)/include"
100  LDFLAGS += -L"$(LIBCARES_PATH)/lib"
101  LIBS += -lcares
102endif
103
104ifneq ($(findstring -rtmp,$(CFG)),)
105  LIBRTMP_PATH ?= $(PROOT)/../librtmp
106  CPPFLAGS += -DUSE_LIBRTMP
107  CPPFLAGS += -I"$(LIBRTMP_PATH)"
108  LDFLAGS += -L"$(LIBRTMP_PATH)/librtmp"
109  LIBS += -lrtmp
110  ZLIB := 1
111endif
112
113ifneq ($(findstring -ssh2,$(CFG)),)
114  LIBSSH2_PATH ?= $(PROOT)/../libssh2
115  CPPFLAGS += -DUSE_LIBSSH2
116  CPPFLAGS += -I"$(LIBSSH2_PATH)/include"
117  LDFLAGS += -L"$(LIBSSH2_PATH)/lib"
118  LIBS += -lssh2
119else ifneq ($(findstring -libssh,$(CFG)),)
120  LIBSSH_PATH ?= $(PROOT)/../libssh
121  CPPFLAGS += -DUSE_LIBSSH
122  CPPFLAGS += -I"$(LIBSSH_PATH)/include"
123  LDFLAGS += -L"$(LIBSSH_PATH)/lib"
124  LIBS += -lssh
125else ifneq ($(findstring -wolfssh,$(CFG)),)
126  WOLFSSH_PATH ?= $(PROOT)/../wolfssh
127  CPPFLAGS += -DUSE_WOLFSSH
128  CPPFLAGS += -I"$(WOLFSSH_PATH)/include"
129  LDFLAGS += -L"$(WOLFSSH_PATH)/lib"
130  LIBS += -lwolfssh
131endif
132
133ifneq ($(findstring -ssl,$(CFG)),)
134  OPENSSL_PATH ?= $(PROOT)/../openssl
135  CPPFLAGS += -DUSE_OPENSSL
136  CPPFLAGS += -DCURL_DISABLE_OPENSSL_AUTO_LOAD_CONFIG
137  OPENSSL_INCLUDE ?= $(OPENSSL_PATH)/include
138  OPENSSL_LIBPATH ?= $(OPENSSL_PATH)/lib
139  CPPFLAGS += -I"$(OPENSSL_INCLUDE)"
140  LDFLAGS += -L"$(OPENSSL_LIBPATH)"
141  OPENSSL_LIBS ?= -lssl -lcrypto
142  LIBS += $(OPENSSL_LIBS)
143
144  ifneq ($(findstring -srp,$(CFG)),)
145    ifneq ($(wildcard $(OPENSSL_INCLUDE)/openssl/srp.h),)
146      # OpenSSL 1.0.1 and later.
147      CPPFLAGS += -DHAVE_OPENSSL_SRP -DUSE_TLS_SRP
148    endif
149  endif
150  SSLLIBS += 1
151endif
152ifneq ($(findstring -wolfssl,$(CFG)),)
153  WOLFSSL_PATH ?= $(PROOT)/../wolfssl
154  CPPFLAGS += -DUSE_WOLFSSL
155  CPPFLAGS += -DSIZEOF_LONG_LONG=8
156  CPPFLAGS += -I"$(WOLFSSL_PATH)/include"
157  LDFLAGS += -L"$(WOLFSSL_PATH)/lib"
158  LIBS += -lwolfssl
159  SSLLIBS += 1
160endif
161ifneq ($(findstring -mbedtls,$(CFG)),)
162  MBEDTLS_PATH ?= $(PROOT)/../mbedtls
163  CPPFLAGS += -DUSE_MBEDTLS
164  CPPFLAGS += -I"$(MBEDTLS_PATH)/include"
165  LDFLAGS += -L"$(MBEDTLS_PATH)/lib"
166  LIBS += -lmbedtls -lmbedx509 -lmbedcrypto
167  SSLLIBS += 1
168endif
169
170ifneq ($(findstring -nghttp2,$(CFG)),)
171  NGHTTP2_PATH ?= $(PROOT)/../nghttp2
172  CPPFLAGS += -DUSE_NGHTTP2
173  CPPFLAGS += -I"$(NGHTTP2_PATH)/include"
174  LDFLAGS += -L"$(NGHTTP2_PATH)/lib"
175  LIBS += -lnghttp2
176endif
177
178ifeq ($(findstring -nghttp3,$(CFG))$(findstring -ngtcp2,$(CFG)),-nghttp3-ngtcp2)
179  NGHTTP3_PATH ?= $(PROOT)/../nghttp3
180  CPPFLAGS += -DUSE_NGHTTP3
181  CPPFLAGS += -I"$(NGHTTP3_PATH)/include"
182  LDFLAGS += -L"$(NGHTTP3_PATH)/lib"
183  LIBS += -lnghttp3
184
185  NGTCP2_PATH ?= $(PROOT)/../ngtcp2
186  CPPFLAGS += -DUSE_NGTCP2
187  CPPFLAGS += -I"$(NGTCP2_PATH)/include"
188  LDFLAGS += -L"$(NGTCP2_PATH)/lib"
189
190  NGTCP2_LIBS ?=
191  ifeq ($(NGTCP2_LIBS),)
192    ifneq ($(findstring -ssl,$(CFG)),)
193      ifneq ($(wildcard $(OPENSSL_INCLUDE)/openssl/aead.h),)
194        NGTCP2_LIBS := -lngtcp2_crypto_boringssl
195      else  # including libressl
196        NGTCP2_LIBS := -lngtcp2_crypto_quictls
197      endif
198    else ifneq ($(findstring -wolfssl,$(CFG)),)
199      NGTCP2_LIBS := -lngtcp2_crypto_wolfssl
200    endif
201  endif
202
203  LIBS += -lngtcp2 $(NGTCP2_LIBS)
204endif
205
206ifneq ($(findstring -zlib,$(CFG))$(ZLIB),)
207  ZLIB_PATH ?= $(PROOT)/../zlib
208  # These CPPFLAGS are also required when compiling the curl tool via 'src'.
209  CPPFLAGS += -DHAVE_LIBZ
210  CPPFLAGS += -I"$(ZLIB_PATH)/include"
211  LDFLAGS += -L"$(ZLIB_PATH)/lib"
212  ZLIB_LIBS ?= -lz
213  LIBS += $(ZLIB_LIBS)
214  ZLIB := 1
215endif
216ifneq ($(findstring -zstd,$(CFG)),)
217  ZSTD_PATH ?= $(PROOT)/../zstd
218  CPPFLAGS += -DHAVE_ZSTD
219  CPPFLAGS += -I"$(ZSTD_PATH)/include"
220  LDFLAGS += -L"$(ZSTD_PATH)/lib"
221  ZSTD_LIBS ?= -lzstd
222  LIBS += $(ZSTD_LIBS)
223endif
224ifneq ($(findstring -brotli,$(CFG)),)
225  BROTLI_PATH ?= $(PROOT)/../brotli
226  CPPFLAGS += -DHAVE_BROTLI
227  CPPFLAGS += -I"$(BROTLI_PATH)/include"
228  LDFLAGS += -L"$(BROTLI_PATH)/lib"
229  BROTLI_LIBS ?= -lbrotlidec -lbrotlicommon
230  LIBS += $(BROTLI_LIBS)
231endif
232ifneq ($(findstring -gsasl,$(CFG)),)
233  LIBGSASL_PATH ?= $(PROOT)/../gsasl
234  CPPFLAGS += -DUSE_GSASL
235  CPPFLAGS += -I"$(LIBGSASL_PATH)/include"
236  LDFLAGS += -L"$(LIBGSASL_PATH)/lib"
237  LIBS += -lgsasl
238endif
239
240ifneq ($(findstring -idn2,$(CFG)),)
241  LIBIDN2_PATH ?= $(PROOT)/../libidn2
242  CPPFLAGS += -DUSE_LIBIDN2
243  CPPFLAGS += -I"$(LIBIDN2_PATH)/include"
244  LDFLAGS += -L"$(LIBIDN2_PATH)/lib"
245  LIBS += -lidn2
246
247ifneq ($(findstring -psl,$(CFG)),)
248  LIBPSL_PATH ?= $(PROOT)/../libpsl
249  CPPFLAGS += -DUSE_LIBPSL
250  CPPFLAGS += -I"$(LIBPSL_PATH)/include"
251  LDFLAGS += -L"$(LIBPSL_PATH)/lib"
252  LIBS += -lpsl
253endif
254endif
255
256ifneq ($(findstring -ipv6,$(CFG)),)
257  CPPFLAGS += -DUSE_IPV6
258endif
259
260ifneq ($(findstring -watt,$(CFG))$(MSDOS),)
261  WATT_PATH ?= $(PROOT)/../watt
262  CPPFLAGS += -I"$(WATT_PATH)/inc"
263  LDFLAGS += -L"$(WATT_PATH)/lib"
264  LIBS += -lwatt
265endif
266
267ifneq ($(findstring 11,$(subst $(subst ,, ),,$(SSLLIBS))),)
268  CPPFLAGS += -DCURL_WITH_MULTI_SSL
269endif
270
271### Common rules
272
273OBJ_DIR := $(TRIPLET)
274
275ifneq ($(findstring /sh,$(SHELL)),)
276DEL   = rm -f $1
277COPY  = -cp -afv $1 $2
278MKDIR = mkdir -p $1
279RMDIR = rm -fr $1
280WHICH = $(SHELL) -c "command -v $1"
281else
282DEL   = -del 2>NUL /q /f $(subst /,\,$1)
283COPY  = -copy 2>NUL /y $(subst /,\,$1) $(subst /,\,$2)
284MKDIR = -md 2>NUL $(subst /,\,$1)
285RMDIR = -rd 2>NUL /q /s $(subst /,\,$1)
286WHICH = where $1
287endif
288
289all: $(TARGETS)
290
291$(OBJ_DIR):
292	-$(call MKDIR, $(OBJ_DIR))
293
294$(OBJ_DIR)/%.o: %.c
295	$(CC) -W -Wall $(CFLAGS) $(CPPFLAGS) -c $< -o $@
296
297clean:
298	@$(call DEL, $(TOCLEAN))
299	@$(RMDIR) $(OBJ_DIR)
300
301distclean vclean: clean
302	@$(call DEL, $(TARGETS) $(TOVCLEAN))
303
304### Local
305
306ifdef LOCAL
307
308CPPFLAGS += -DBUILDING_LIBCURL
309
310### Sources and targets
311
312# Provides CSOURCES, HHEADERS
313include Makefile.inc
314
315vpath %.c vauth vquic vssh vtls
316
317libcurl_a_LIBRARY := libcurl.a
318
319TARGETS := $(libcurl_a_LIBRARY)
320
321libcurl_a_OBJECTS := $(patsubst %.c,$(OBJ_DIR)/%.o,$(notdir $(strip $(CSOURCES))))
322libcurl_a_DEPENDENCIES := $(strip $(CSOURCES) $(HHEADERS))
323
324TOCLEAN :=
325TOVCLEAN :=
326
327### Rules
328
329$(libcurl_a_LIBRARY): $(libcurl_a_OBJECTS) $(libcurl_a_DEPENDENCIES)
330	@$(call DEL, $@)
331	$(AR) rcs $@ $(libcurl_a_OBJECTS)
332
333all: $(OBJ_DIR) $(TARGETS)
334endif
335