Makefile.mk 10 KB

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