Makefile.mk 10 KB

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