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