xref: /curl/lib/Makefile.mk (revision 445fb812)
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 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 += -isystem "$(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 += -isystem "$(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 += -isystem "$(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 += -isystem "$(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 += -isystem "$(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 += -isystem "$(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 += -isystem "$(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 += -isystem "$(MBEDTLS_PATH)/include"
165  LDFLAGS += -L"$(MBEDTLS_PATH)/lib"
166  LIBS += -lmbedtls -lmbedx509 -lmbedcrypto
167  SSLLIBS += 1
168endif
169ifneq ($(findstring -bearssl,$(CFG)),)
170  BEARSSL_PATH ?= $(PROOT)/../bearssl
171  CPPFLAGS += -DUSE_BEARSSL
172  CPPFLAGS += -isystem "$(BEARSSL_PATH)/inc"
173  LDFLAGS += -L"$(BEARSSL_PATH)/build"
174  LIBS += -lbearssl
175  SSLLIBS += 1
176endif
177
178ifneq ($(findstring -nghttp2,$(CFG)),)
179  NGHTTP2_PATH ?= $(PROOT)/../nghttp2
180  CPPFLAGS += -DUSE_NGHTTP2
181  CPPFLAGS += -isystem "$(NGHTTP2_PATH)/include"
182  LDFLAGS += -L"$(NGHTTP2_PATH)/lib"
183  LIBS += -lnghttp2
184endif
185
186ifeq ($(findstring -nghttp3,$(CFG))$(findstring -ngtcp2,$(CFG)),-nghttp3-ngtcp2)
187  NGHTTP3_PATH ?= $(PROOT)/../nghttp3
188  CPPFLAGS += -DUSE_NGHTTP3
189  CPPFLAGS += -isystem "$(NGHTTP3_PATH)/include"
190  LDFLAGS += -L"$(NGHTTP3_PATH)/lib"
191  LIBS += -lnghttp3
192
193  NGTCP2_PATH ?= $(PROOT)/../ngtcp2
194  CPPFLAGS += -DUSE_NGTCP2
195  CPPFLAGS += -isystem "$(NGTCP2_PATH)/include"
196  LDFLAGS += -L"$(NGTCP2_PATH)/lib"
197
198  NGTCP2_LIBS ?=
199  ifeq ($(NGTCP2_LIBS),)
200    ifneq ($(findstring -ssl,$(CFG)),)
201      ifneq ($(wildcard $(OPENSSL_INCLUDE)/openssl/aead.h),)
202        NGTCP2_LIBS := -lngtcp2_crypto_boringssl
203      else  # including libressl
204        NGTCP2_LIBS := -lngtcp2_crypto_quictls
205      endif
206    else ifneq ($(findstring -wolfssl,$(CFG)),)
207      NGTCP2_LIBS := -lngtcp2_crypto_wolfssl
208    endif
209  endif
210
211  LIBS += -lngtcp2 $(NGTCP2_LIBS)
212endif
213
214ifneq ($(findstring -zlib,$(CFG))$(ZLIB),)
215  ZLIB_PATH ?= $(PROOT)/../zlib
216  # These CPPFLAGS are also required when compiling the curl tool via 'src'.
217  CPPFLAGS += -DHAVE_LIBZ
218  CPPFLAGS += -isystem "$(ZLIB_PATH)/include"
219  LDFLAGS += -L"$(ZLIB_PATH)/lib"
220  ZLIB_LIBS ?= -lz
221  LIBS += $(ZLIB_LIBS)
222  ZLIB := 1
223endif
224ifneq ($(findstring -zstd,$(CFG)),)
225  ZSTD_PATH ?= $(PROOT)/../zstd
226  CPPFLAGS += -DHAVE_ZSTD
227  CPPFLAGS += -isystem "$(ZSTD_PATH)/include"
228  LDFLAGS += -L"$(ZSTD_PATH)/lib"
229  ZSTD_LIBS ?= -lzstd
230  LIBS += $(ZSTD_LIBS)
231endif
232ifneq ($(findstring -brotli,$(CFG)),)
233  BROTLI_PATH ?= $(PROOT)/../brotli
234  CPPFLAGS += -DHAVE_BROTLI
235  CPPFLAGS += -isystem "$(BROTLI_PATH)/include"
236  LDFLAGS += -L"$(BROTLI_PATH)/lib"
237  BROTLI_LIBS ?= -lbrotlidec -lbrotlicommon
238  LIBS += $(BROTLI_LIBS)
239endif
240ifneq ($(findstring -gsasl,$(CFG)),)
241  LIBGSASL_PATH ?= $(PROOT)/../gsasl
242  CPPFLAGS += -DUSE_GSASL
243  CPPFLAGS += -isystem "$(LIBGSASL_PATH)/include"
244  LDFLAGS += -L"$(LIBGSASL_PATH)/lib"
245  LIBS += -lgsasl
246endif
247
248ifneq ($(findstring -idn2,$(CFG)),)
249  LIBIDN2_PATH ?= $(PROOT)/../libidn2
250  CPPFLAGS += -DHAVE_LIBIDN2 -DHAVE_IDN2_H
251  CPPFLAGS += -isystem "$(LIBIDN2_PATH)/include"
252  LDFLAGS += -L"$(LIBIDN2_PATH)/lib"
253  LIBS += -lidn2
254
255ifneq ($(findstring -psl,$(CFG)),)
256  LIBPSL_PATH ?= $(PROOT)/../libpsl
257  CPPFLAGS += -DUSE_LIBPSL
258  CPPFLAGS += -isystem "$(LIBPSL_PATH)/include"
259  LDFLAGS += -L"$(LIBPSL_PATH)/lib"
260  LIBS += -lpsl
261endif
262endif
263
264ifneq ($(findstring -ipv6,$(CFG)),)
265  CPPFLAGS += -DUSE_IPV6
266endif
267
268ifneq ($(findstring -watt,$(CFG))$(MSDOS),)
269  WATT_PATH ?= $(PROOT)/../watt
270  CPPFLAGS += -isystem "$(WATT_PATH)/inc"
271  LDFLAGS += -L"$(WATT_PATH)/lib"
272  LIBS += -lwatt
273endif
274
275ifneq ($(findstring 11,$(subst $(subst ,, ),,$(SSLLIBS))),)
276  CPPFLAGS += -DCURL_WITH_MULTI_SSL
277endif
278
279### Common rules
280
281OBJ_DIR := $(TRIPLET)
282
283ifneq ($(findstring /sh,$(SHELL)),)
284DEL   = rm -f $1
285COPY  = -cp -afv $1 $2
286MKDIR = mkdir -p $1
287RMDIR = rm -fr $1
288else
289DEL   = -del 2>NUL /q /f $(subst /,\,$1)
290COPY  = -copy 2>NUL /y $(subst /,\,$1) $(subst /,\,$2)
291MKDIR = -md 2>NUL $(subst /,\,$1)
292RMDIR = -rd 2>NUL /q /s $(subst /,\,$1)
293endif
294
295all: $(TARGETS)
296
297$(OBJ_DIR):
298	-$(call MKDIR, $(OBJ_DIR))
299
300$(OBJ_DIR)/%.o: %.c
301	$(CC) -W -Wall $(CFLAGS) $(CPPFLAGS) -c $< -o $@
302
303clean:
304	@$(call DEL, $(TOCLEAN))
305	@$(RMDIR) $(OBJ_DIR)
306
307distclean vclean: clean
308	@$(call DEL, $(TARGETS) $(TOVCLEAN))
309
310### Local
311
312ifdef LOCAL
313
314CPPFLAGS += -DBUILDING_LIBCURL
315
316### Sources and targets
317
318# Provides CSOURCES, HHEADERS
319include Makefile.inc
320
321vpath %.c vauth vquic vssh vtls
322
323libcurl_a_LIBRARY := libcurl.a
324
325TARGETS := $(libcurl_a_LIBRARY)
326
327libcurl_a_OBJECTS := $(patsubst %.c,$(OBJ_DIR)/%.o,$(notdir $(strip $(CSOURCES))))
328libcurl_a_DEPENDENCIES := $(strip $(CSOURCES) $(HHEADERS))
329
330TOCLEAN :=
331TOVCLEAN :=
332
333### Rules
334
335$(libcurl_a_LIBRARY): $(libcurl_a_OBJECTS) $(libcurl_a_DEPENDENCIES)
336	@$(call DEL, $@)
337	$(AR) rcs $@ $(libcurl_a_OBJECTS)
338
339all: $(OBJ_DIR) $(TARGETS)
340endif
341