1GENERATED_FILES := \
2	error.c version_features.c \
3	ssl_debug_helpers_generated.c \
4	psa_crypto_driver_wrappers.h \
5	psa_crypto_driver_wrappers_no_static.c
6
7# Also see "include/mbedtls/mbedtls_config.h"
8
9CFLAGS	?= -O2
10WARNING_CFLAGS ?=  -Wall -Wextra -Wformat=2 -Wno-format-nonliteral
11LDFLAGS ?=
12
13# Include ../include for public headers and . for private headers.
14# Note that . needs to be included explicitly for the sake of library
15# files that are not in the /library directory (which currently means
16# under /3rdparty).
17LOCAL_CFLAGS = $(WARNING_CFLAGS) -I. -I../include -D_FILE_OFFSET_BITS=64
18LOCAL_LDFLAGS =
19
20ifdef DEBUG
21LOCAL_CFLAGS += -g3
22endif
23
24# MicroBlaze specific options:
25# CFLAGS += -mno-xl-soft-mul -mxl-barrel-shift
26
27# To compile on Plan9:
28# CFLAGS += -D_BSD_EXTENSION
29
30PERL ?= perl
31
32ifdef WINDOWS
33PYTHON ?= python
34else
35PYTHON ?= $(shell if type python3 >/dev/null 2>/dev/null; then echo python3; else echo python; fi)
36endif
37
38# if were running on Windows build for Windows
39ifdef WINDOWS
40WINDOWS_BUILD=1
41else ifeq ($(shell uname -s),Darwin)
42ifeq ($(AR),ar)
43APPLE_BUILD ?= 1
44endif
45endif
46
47ifdef WINDOWS_BUILD
48LOCAL_LDFLAGS += -lbcrypt
49endif
50
51# To compile as a shared library:
52ifdef SHARED
53# all code is position-indep with mingw, avoid warning about useless flag
54ifndef WINDOWS_BUILD
55LOCAL_CFLAGS += -fPIC -fpic
56endif
57endif
58
59SOEXT_TLS?=so.21
60SOEXT_X509?=so.7
61SOEXT_CRYPTO?=so.16
62
63# Set AR_DASH= (empty string) to use an ar implementation that does not accept
64# the - prefix for command line options (e.g. llvm-ar)
65AR_DASH ?= -
66
67ARFLAGS = $(AR_DASH)src
68ifdef APPLE_BUILD
69ifneq ($(APPLE_BUILD),0)
70ARFLAGS = $(AR_DASH)Src
71RLFLAGS = -no_warning_for_no_symbols -c
72RL ?= ranlib
73endif
74endif
75
76DLEXT ?= so
77ifdef WINDOWS_BUILD
78# Windows shared library extension:
79DLEXT = dll
80else ifdef APPLE_BUILD
81ifneq ($(APPLE_BUILD),0)
82# Mac OS X shared library extension:
83DLEXT = dylib
84endif
85endif
86
87OBJS_CRYPTO= \
88	     aes.o \
89	     aesni.o \
90	     aesce.o \
91	     aria.o \
92	     asn1parse.o \
93	     asn1write.o \
94	     base64.o \
95	     bignum.o \
96	     bignum_core.o \
97	     bignum_mod.o \
98	     bignum_mod_raw.o \
99	     block_cipher.o \
100	     camellia.o \
101	     ccm.o \
102	     chacha20.o \
103	     chachapoly.o \
104	     cipher.o \
105	     cipher_wrap.o \
106	     cmac.o \
107	     constant_time.o \
108	     ctr_drbg.o \
109	     des.o \
110	     dhm.o \
111	     ecdh.o \
112	     ecdsa.o \
113	     ecjpake.o \
114	     ecp.o \
115	     ecp_curves.o \
116	     ecp_curves_new.o \
117	     entropy.o \
118	     entropy_poll.o \
119	     error.o \
120	     gcm.o \
121	     hkdf.o \
122	     hmac_drbg.o \
123	     lmots.o \
124	     lms.o \
125	     md.o \
126	     md5.o \
127	     memory_buffer_alloc.o \
128	     nist_kw.o \
129	     oid.o \
130	     padlock.o \
131	     pem.o \
132	     pk.o \
133	     pk_ecc.o \
134	     pk_wrap.o \
135	     pkcs12.o \
136	     pkcs5.o \
137	     pkparse.o \
138	     pkwrite.o \
139	     platform.o \
140	     platform_util.o \
141	     poly1305.o \
142	     psa_crypto.o \
143	     psa_crypto_aead.o \
144	     psa_crypto_cipher.o \
145	     psa_crypto_client.o \
146	     psa_crypto_driver_wrappers_no_static.o \
147	     psa_crypto_ecp.o \
148	     psa_crypto_ffdh.o \
149	     psa_crypto_hash.o \
150	     psa_crypto_mac.o \
151	     psa_crypto_pake.o \
152	     psa_crypto_rsa.o \
153	     psa_crypto_se.o \
154	     psa_crypto_slot_management.o \
155	     psa_crypto_storage.o \
156	     psa_its_file.o \
157	     psa_util.o \
158	     ripemd160.o \
159	     rsa.o \
160	     rsa_alt_helpers.o \
161	     sha1.o \
162	     sha256.o \
163	     sha512.o \
164	     sha3.o \
165	     threading.o \
166	     timing.o \
167	     version.o \
168	     version_features.o \
169	     # This line is intentionally left blank
170
171include ../3rdparty/Makefile.inc
172LOCAL_CFLAGS+=$(THIRDPARTY_INCLUDES)
173OBJS_CRYPTO+=$(THIRDPARTY_CRYPTO_OBJECTS)
174
175OBJS_X509= \
176	   x509.o \
177	   x509_create.o \
178	   x509_crl.o \
179	   x509_crt.o \
180	   x509_csr.o \
181	   x509write.o \
182	   x509write_crt.o \
183	   x509write_csr.o \
184	   pkcs7.o \
185	   # This line is intentionally left blank
186
187OBJS_TLS= \
188	  debug.o \
189	  mps_reader.o \
190	  mps_trace.o \
191	  net_sockets.o \
192	  ssl_cache.o \
193	  ssl_ciphersuites.o \
194	  ssl_client.o \
195	  ssl_cookie.o \
196	  ssl_debug_helpers_generated.o \
197	  ssl_msg.o \
198	  ssl_ticket.o \
199	  ssl_tls.o \
200	  ssl_tls12_client.o \
201	  ssl_tls12_server.o \
202	  ssl_tls13_keys.o \
203	  ssl_tls13_client.o \
204	  ssl_tls13_server.o \
205	  ssl_tls13_generic.o \
206	  # This line is intentionally left blank
207
208.SILENT:
209
210.PHONY: all static shared clean
211
212ifndef SHARED
213all: static
214else
215all: shared static
216endif
217
218static: libmbedcrypto.a libmbedx509.a libmbedtls.a
219	cd ../tests && echo "This is a seedfile that contains 64 bytes (65 on Windows)......" > seedfile
220
221shared: libmbedcrypto.$(DLEXT) libmbedx509.$(DLEXT) libmbedtls.$(DLEXT)
222
223# Windows builds under Mingw can fail if make tries to create archives in the same
224# directory at the same time - see https://bugs.launchpad.net/gcc-arm-embedded/+bug/1848002.
225# This forces builds of the .a files to be serialised.
226ifdef WINDOWS
227libmbedtls.a: | libmbedx509.a
228libmbedx509.a: | libmbedcrypto.a
229endif
230
231# tls
232libmbedtls.a: $(OBJS_TLS)
233	echo "  AR    $@"
234	$(AR) $(ARFLAGS) $@ $(OBJS_TLS)
235ifdef APPLE_BUILD
236ifneq ($(APPLE_BUILD),0)
237	echo "  RL    $@"
238	$(RL) $(RLFLAGS) $@
239endif
240endif
241
242libmbedtls.$(SOEXT_TLS): $(OBJS_TLS) libmbedx509.so
243	echo "  LD    $@"
244	$(CC) -shared -Wl,-soname,$@ -o $@ $(OBJS_TLS) -L. -lmbedx509 -lmbedcrypto $(LOCAL_LDFLAGS) $(LDFLAGS)
245
246ifneq ($(SOEXT_TLS),so)
247libmbedtls.so: libmbedtls.$(SOEXT_TLS)
248	echo "  LN    $@ -> $<"
249	ln -sf $< $@
250endif
251
252libmbedtls.dylib: $(OBJS_TLS) libmbedx509.dylib
253	echo "  LD    $@"
254	$(CC) -dynamiclib -o $@ $(OBJS_TLS) -L. -lmbedx509 -lmbedcrypto $(LOCAL_LDFLAGS) $(LDFLAGS)
255
256libmbedtls.dll: $(OBJS_TLS) libmbedx509.dll
257	echo "  LD    $@"
258	$(CC) -shared -Wl,-soname,$@ -Wl,--out-implib,$@.a -o $@ $(OBJS_TLS) -lws2_32 -lwinmm -lgdi32 -L. -lmbedx509 -lmbedcrypto -static-libgcc $(LOCAL_LDFLAGS) $(LDFLAGS)
259
260# x509
261libmbedx509.a: $(OBJS_X509)
262	echo "  AR    $@"
263	$(AR) $(ARFLAGS) $@ $(OBJS_X509)
264ifdef APPLE_BUILD
265ifneq ($(APPLE_BUILD),0)
266	echo "  RL    $@"
267	$(RL) $(RLFLAGS) $@
268endif
269endif
270
271libmbedx509.$(SOEXT_X509): $(OBJS_X509) libmbedcrypto.so
272	echo "  LD    $@"
273	$(CC) -shared -Wl,-soname,$@ -o $@ $(OBJS_X509) -L. -lmbedcrypto $(LOCAL_LDFLAGS) $(LDFLAGS)
274
275ifneq ($(SOEXT_X509),so)
276libmbedx509.so: libmbedx509.$(SOEXT_X509)
277	echo "  LN    $@ -> $<"
278	ln -sf $< $@
279endif
280
281libmbedx509.dylib: $(OBJS_X509) libmbedcrypto.dylib
282	echo "  LD    $@"
283	$(CC) -dynamiclib -o $@ $(OBJS_X509) -L. -lmbedcrypto  $(LOCAL_LDFLAGS) $(LDFLAGS)
284
285libmbedx509.dll: $(OBJS_X509) libmbedcrypto.dll
286	echo "  LD    $@"
287	$(CC) -shared -Wl,-soname,$@ -Wl,--out-implib,$@.a -o $@ $(OBJS_X509) -lws2_32 -lwinmm -lgdi32 -L. -lmbedcrypto -static-libgcc $(LOCAL_LDFLAGS) $(LDFLAGS)
288
289# crypto
290libmbedcrypto.a: $(OBJS_CRYPTO)
291	echo "  AR    $@"
292	$(AR) $(ARFLAGS) $@ $(OBJS_CRYPTO)
293ifdef APPLE_BUILD
294ifneq ($(APPLE_BUILD),0)
295	echo "  RL    $@"
296	$(RL) $(RLFLAGS) $@
297endif
298endif
299
300libmbedcrypto.$(SOEXT_CRYPTO): $(OBJS_CRYPTO)
301	echo "  LD    $@"
302	$(CC) -shared -Wl,-soname,$@ -o $@ $(OBJS_CRYPTO) $(LOCAL_LDFLAGS) $(LDFLAGS)
303
304ifneq ($(SOEXT_CRYPTO),so)
305libmbedcrypto.so: libmbedcrypto.$(SOEXT_CRYPTO)
306	echo "  LN    $@ -> $<"
307	ln -sf $< $@
308endif
309
310libmbedcrypto.dylib: $(OBJS_CRYPTO)
311	echo "  LD    $@"
312	$(CC) -dynamiclib -o $@ $(OBJS_CRYPTO) $(LOCAL_LDFLAGS) $(LDFLAGS)
313
314libmbedcrypto.dll: $(OBJS_CRYPTO)
315	echo "  LD    $@"
316	$(CC) -shared -Wl,-soname,$@ -Wl,--out-implib,$@.a -o $@ $(OBJS_CRYPTO) -lws2_32 -lwinmm -lgdi32 -static-libgcc $(LOCAL_LDFLAGS) $(LDFLAGS)
317
318.c.o:
319	echo "  CC    $<"
320	$(CC) $(LOCAL_CFLAGS) $(CFLAGS) -o $@ -c $<
321
322.PHONY: generated_files
323generated_files: $(GENERATED_FILES)
324
325# See root Makefile
326GEN_FILES ?=
327ifdef GEN_FILES
328gen_file_dep =
329else
330gen_file_dep = |
331endif
332
333error.c: $(gen_file_dep) ../scripts/generate_errors.pl
334error.c: $(gen_file_dep) ../scripts/data_files/error.fmt
335error.c: $(gen_file_dep) $(filter-out %config%,$(wildcard ../include/mbedtls/*.h))
336error.c:
337	echo "  Gen   $@"
338	$(PERL) ../scripts/generate_errors.pl
339
340ssl_debug_helpers_generated.c: $(gen_file_dep) ../scripts/generate_ssl_debug_helpers.py
341ssl_debug_helpers_generated.c: $(gen_file_dep) $(filter-out %config%,$(wildcard ../include/mbedtls/*.h))
342ssl_debug_helpers_generated.c:
343	echo "  Gen   $@"
344	$(PYTHON) ../scripts/generate_ssl_debug_helpers.py --mbedtls-root .. .
345
346version_features.c: $(gen_file_dep) ../scripts/generate_features.pl
347version_features.c: $(gen_file_dep) ../scripts/data_files/version_features.fmt
348## The generated file only depends on the options that are present in mbedtls_config.h,
349## not on which options are set. To avoid regenerating this file all the time
350## when switching between configurations, don't declare mbedtls_config.h as a
351## dependency. Remove this file from your working tree if you've just added or
352## removed an option in mbedtls_config.h.
353#version_features.c: ../include/mbedtls/mbedtls_config.h
354version_features.c:
355	echo "  Gen   $@"
356	$(PERL) ../scripts/generate_features.pl
357
358GENERATED_WRAPPER_FILES = \
359                    psa_crypto_driver_wrappers.h \
360                    psa_crypto_driver_wrappers_no_static.c
361$(GENERATED_WRAPPER_FILES): ../scripts/generate_driver_wrappers.py
362$(GENERATED_WRAPPER_FILES): ../scripts/data_files/driver_templates/psa_crypto_driver_wrappers.h.jinja
363$(GENERATED_WRAPPER_FILES): ../scripts/data_files/driver_templates/psa_crypto_driver_wrappers_no_static.c.jinja
364$(GENERATED_WRAPPER_FILES):
365	echo "  Gen   $(GENERATED_WRAPPER_FILES)"
366	$(PYTHON) ../scripts/generate_driver_wrappers.py
367
368psa_crypto.o:psa_crypto_driver_wrappers.h
369
370clean:
371ifndef WINDOWS
372	rm -f *.o libmbed*
373	rm -f $(THIRDPARTY_CRYPTO_OBJECTS)
374else
375	if exist *.o del /Q /F *.o
376	if exist libmbed* del /Q /F libmbed*
377	del /Q /F del_errors_out_if_the_file_list_is_empty_but_not_if_a_file_does_not_exist $(subst /,\,$(THIRDPARTY_CRYPTO_OBJECTS))
378endif
379
380neat: clean
381ifndef WINDOWS
382	rm -f $(GENERATED_FILES)
383else
384	for %f in ($(subst /,\,$(GENERATED_FILES))) if exist %f del /Q /F %f
385endif
386