1a8e1175bSopenharmony_ciDESTDIR=/usr/local
2a8e1175bSopenharmony_ciPREFIX=mbedtls_
3a8e1175bSopenharmony_ciPERL ?= perl
4a8e1175bSopenharmony_ci
5a8e1175bSopenharmony_ciifneq (,$(filter-out lib library/%,$(or $(MAKECMDGOALS),all)))
6a8e1175bSopenharmony_ci    ifeq (,$(wildcard framework/exported.make))
7a8e1175bSopenharmony_ci        # Use the define keyword to get a multi-line message.
8a8e1175bSopenharmony_ci        # GNU make appends ".  Stop.", so tweak the ending of our message accordingly.
9a8e1175bSopenharmony_ci        define error_message
10a8e1175bSopenharmony_ci$(MBEDTLS_PATH)/framework/exported.make not found.
11a8e1175bSopenharmony_ciRun `git submodule update --init` to fetch the submodule contents.
12a8e1175bSopenharmony_ciThis is a fatal error
13a8e1175bSopenharmony_ci        endef
14a8e1175bSopenharmony_ci        $(error $(error_message))
15a8e1175bSopenharmony_ci    endif
16a8e1175bSopenharmony_ci    include framework/exported.make
17a8e1175bSopenharmony_ciendif
18a8e1175bSopenharmony_ci
19a8e1175bSopenharmony_ci.SILENT:
20a8e1175bSopenharmony_ci
21a8e1175bSopenharmony_ci.PHONY: all no_test programs lib tests install uninstall clean test check lcov apidoc apidoc_clean
22a8e1175bSopenharmony_ci
23a8e1175bSopenharmony_ciall: programs tests
24a8e1175bSopenharmony_ci	$(MAKE) post_build
25a8e1175bSopenharmony_ci
26a8e1175bSopenharmony_cino_test: programs
27a8e1175bSopenharmony_ci
28a8e1175bSopenharmony_ciprograms: lib mbedtls_test
29a8e1175bSopenharmony_ci	$(MAKE) -C programs
30a8e1175bSopenharmony_ci
31a8e1175bSopenharmony_cilib:
32a8e1175bSopenharmony_ci	$(MAKE) -C library
33a8e1175bSopenharmony_ci
34a8e1175bSopenharmony_citests: lib mbedtls_test
35a8e1175bSopenharmony_ci	$(MAKE) -C tests
36a8e1175bSopenharmony_ci
37a8e1175bSopenharmony_cimbedtls_test:
38a8e1175bSopenharmony_ci	$(MAKE) -C tests mbedtls_test
39a8e1175bSopenharmony_ci
40a8e1175bSopenharmony_cilibrary/%:
41a8e1175bSopenharmony_ci	$(MAKE) -C library $*
42a8e1175bSopenharmony_ciprograms/%:
43a8e1175bSopenharmony_ci	$(MAKE) -C programs $*
44a8e1175bSopenharmony_citests/%:
45a8e1175bSopenharmony_ci	$(MAKE) -C tests $*
46a8e1175bSopenharmony_ci
47a8e1175bSopenharmony_ci.PHONY: generated_files
48a8e1175bSopenharmony_cigenerated_files: library/generated_files
49a8e1175bSopenharmony_cigenerated_files: programs/generated_files
50a8e1175bSopenharmony_cigenerated_files: tests/generated_files
51a8e1175bSopenharmony_cigenerated_files: visualc_files
52a8e1175bSopenharmony_ci
53a8e1175bSopenharmony_ci# Set GEN_FILES to the empty string to disable dependencies on generated
54a8e1175bSopenharmony_ci# source files. Then `make generated_files` will only build files that
55a8e1175bSopenharmony_ci# are missing, it will not rebuilt files that are present but out of date.
56a8e1175bSopenharmony_ci# This is useful, for example, if you have a source tree where
57a8e1175bSopenharmony_ci# `make generated_files` has already run and file timestamps reflect the
58a8e1175bSopenharmony_ci# time the files were copied or extracted, and you are now in an environment
59a8e1175bSopenharmony_ci# that lacks some of the necessary tools to re-generate the files.
60a8e1175bSopenharmony_ci# If $(GEN_FILES) is non-empty, the generated source files' dependencies
61a8e1175bSopenharmony_ci# are treated ordinarily, based on file timestamps.
62a8e1175bSopenharmony_ciGEN_FILES ?=
63a8e1175bSopenharmony_ci
64a8e1175bSopenharmony_ci# In dependencies where the target is a configuration-independent generated
65a8e1175bSopenharmony_ci# file, use `TARGET: $(gen_file_dep) DEPENDENCY1 DEPENDENCY2 ...`
66a8e1175bSopenharmony_ci# rather than directly `TARGET: DEPENDENCY1 DEPENDENCY2 ...`. This
67a8e1175bSopenharmony_ci# enables the re-generation to be turned off when GEN_FILES is disabled.
68a8e1175bSopenharmony_ciifdef GEN_FILES
69a8e1175bSopenharmony_cigen_file_dep =
70a8e1175bSopenharmony_cielse
71a8e1175bSopenharmony_ci# Order-only dependency: generate the target if it's absent, but don't
72a8e1175bSopenharmony_ci# re-generate it if it's present but older than its dependencies.
73a8e1175bSopenharmony_cigen_file_dep = |
74a8e1175bSopenharmony_ciendif
75a8e1175bSopenharmony_ci
76a8e1175bSopenharmony_ci.PHONY: visualc_files
77a8e1175bSopenharmony_ciVISUALC_FILES = visualc/VS2017/mbedTLS.sln visualc/VS2017/mbedTLS.vcxproj
78a8e1175bSopenharmony_ci# TODO: $(app).vcxproj for each $(app) in programs/
79a8e1175bSopenharmony_civisualc_files: $(VISUALC_FILES)
80a8e1175bSopenharmony_ci
81a8e1175bSopenharmony_ci# Ensure that the .c files that generate_visualc_files.pl enumerates are
82a8e1175bSopenharmony_ci# present before it runs. It doesn't matter if the files aren't up-to-date,
83a8e1175bSopenharmony_ci# they just need to be present.
84a8e1175bSopenharmony_ci$(VISUALC_FILES): | library/generated_files
85a8e1175bSopenharmony_ci$(VISUALC_FILES): $(gen_file_dep) scripts/generate_visualc_files.pl
86a8e1175bSopenharmony_ci$(VISUALC_FILES): $(gen_file_dep) scripts/data_files/vs2017-app-template.vcxproj
87a8e1175bSopenharmony_ci$(VISUALC_FILES): $(gen_file_dep) scripts/data_files/vs2017-main-template.vcxproj
88a8e1175bSopenharmony_ci$(VISUALC_FILES): $(gen_file_dep) scripts/data_files/vs2017-sln-template.sln
89a8e1175bSopenharmony_ci# TODO: also the list of .c and .h source files, but not their content
90a8e1175bSopenharmony_ci$(VISUALC_FILES):
91a8e1175bSopenharmony_ci	echo "  Gen   $@ ..."
92a8e1175bSopenharmony_ci	$(PERL) scripts/generate_visualc_files.pl
93a8e1175bSopenharmony_ci
94a8e1175bSopenharmony_ciifndef WINDOWS
95a8e1175bSopenharmony_ciinstall: no_test
96a8e1175bSopenharmony_ci	mkdir -p $(DESTDIR)/include/mbedtls
97a8e1175bSopenharmony_ci	cp -rp include/mbedtls $(DESTDIR)/include
98a8e1175bSopenharmony_ci	mkdir -p $(DESTDIR)/include/psa
99a8e1175bSopenharmony_ci	cp -rp include/psa $(DESTDIR)/include
100a8e1175bSopenharmony_ci
101a8e1175bSopenharmony_ci	mkdir -p $(DESTDIR)/lib
102a8e1175bSopenharmony_ci	cp -RP library/libmbedtls.*    $(DESTDIR)/lib
103a8e1175bSopenharmony_ci	cp -RP library/libmbedx509.*   $(DESTDIR)/lib
104a8e1175bSopenharmony_ci	cp -RP library/libmbedcrypto.* $(DESTDIR)/lib
105a8e1175bSopenharmony_ci
106a8e1175bSopenharmony_ci	mkdir -p $(DESTDIR)/bin
107a8e1175bSopenharmony_ci	for p in programs/*/* ; do              \
108a8e1175bSopenharmony_ci	    if [ -x $$p ] && [ ! -d $$p ] ;     \
109a8e1175bSopenharmony_ci	    then                                \
110a8e1175bSopenharmony_ci	        f=$(PREFIX)`basename $$p` ;     \
111a8e1175bSopenharmony_ci	        cp $$p $(DESTDIR)/bin/$$f ;     \
112a8e1175bSopenharmony_ci	    fi                                  \
113a8e1175bSopenharmony_ci	done
114a8e1175bSopenharmony_ci
115a8e1175bSopenharmony_ciuninstall:
116a8e1175bSopenharmony_ci	rm -rf $(DESTDIR)/include/mbedtls
117a8e1175bSopenharmony_ci	rm -rf $(DESTDIR)/include/psa
118a8e1175bSopenharmony_ci	rm -f $(DESTDIR)/lib/libmbedtls.*
119a8e1175bSopenharmony_ci	rm -f $(DESTDIR)/lib/libmbedx509.*
120a8e1175bSopenharmony_ci	rm -f $(DESTDIR)/lib/libmbedcrypto.*
121a8e1175bSopenharmony_ci
122a8e1175bSopenharmony_ci	for p in programs/*/* ; do              \
123a8e1175bSopenharmony_ci	    if [ -x $$p ] && [ ! -d $$p ] ;     \
124a8e1175bSopenharmony_ci	    then                                \
125a8e1175bSopenharmony_ci	        f=$(PREFIX)`basename $$p` ;     \
126a8e1175bSopenharmony_ci	        rm -f $(DESTDIR)/bin/$$f ;      \
127a8e1175bSopenharmony_ci	    fi                                  \
128a8e1175bSopenharmony_ci	done
129a8e1175bSopenharmony_ciendif
130a8e1175bSopenharmony_ci
131a8e1175bSopenharmony_ci
132a8e1175bSopenharmony_ciWARNING_BORDER_LONG      =**********************************************************************************\n
133a8e1175bSopenharmony_ciCTR_DRBG_128_BIT_KEY_WARN_L1=****  WARNING!  MBEDTLS_CTR_DRBG_USE_128_BIT_KEY defined!                      ****\n
134a8e1175bSopenharmony_ciCTR_DRBG_128_BIT_KEY_WARN_L2=****  Using 128-bit keys for CTR_DRBG limits the security of generated         ****\n
135a8e1175bSopenharmony_ciCTR_DRBG_128_BIT_KEY_WARN_L3=****  keys and operations that use random values generated to 128-bit security ****\n
136a8e1175bSopenharmony_ci
137a8e1175bSopenharmony_ciCTR_DRBG_128_BIT_KEY_WARNING=\n$(WARNING_BORDER_LONG)$(CTR_DRBG_128_BIT_KEY_WARN_L1)$(CTR_DRBG_128_BIT_KEY_WARN_L2)$(CTR_DRBG_128_BIT_KEY_WARN_L3)$(WARNING_BORDER_LONG)
138a8e1175bSopenharmony_ci
139a8e1175bSopenharmony_ci# Post build steps
140a8e1175bSopenharmony_cipost_build:
141a8e1175bSopenharmony_ciifndef WINDOWS
142a8e1175bSopenharmony_ci
143a8e1175bSopenharmony_ci	# If 128-bit keys are configured for CTR_DRBG, display an appropriate warning
144a8e1175bSopenharmony_ci	-scripts/config.py get MBEDTLS_CTR_DRBG_USE_128_BIT_KEY && ([ $$? -eq 0 ]) && \
145a8e1175bSopenharmony_ci	    echo '$(CTR_DRBG_128_BIT_KEY_WARNING)'
146a8e1175bSopenharmony_ci
147a8e1175bSopenharmony_ciendif
148a8e1175bSopenharmony_ci
149a8e1175bSopenharmony_ciclean: clean_more_on_top
150a8e1175bSopenharmony_ci	$(MAKE) -C library clean
151a8e1175bSopenharmony_ci	$(MAKE) -C programs clean
152a8e1175bSopenharmony_ci	$(MAKE) -C tests clean
153a8e1175bSopenharmony_ci
154a8e1175bSopenharmony_ciclean_more_on_top:
155a8e1175bSopenharmony_ciifndef WINDOWS
156a8e1175bSopenharmony_ci	find . \( -name \*.gcno -o -name \*.gcda -o -name \*.info \) -exec rm {} +
157a8e1175bSopenharmony_ciendif
158a8e1175bSopenharmony_ci
159a8e1175bSopenharmony_cineat: clean_more_on_top
160a8e1175bSopenharmony_ci	$(MAKE) -C library neat
161a8e1175bSopenharmony_ci	$(MAKE) -C programs neat
162a8e1175bSopenharmony_ci	$(MAKE) -C tests neat
163a8e1175bSopenharmony_ciifndef WINDOWS
164a8e1175bSopenharmony_ci	rm -f visualc/VS2017/*.vcxproj visualc/VS2017/mbedTLS.sln
165a8e1175bSopenharmony_cielse
166a8e1175bSopenharmony_ci	if exist visualc\VS2017\*.vcxproj del /Q /F visualc\VS2017\*.vcxproj
167a8e1175bSopenharmony_ci	if exist visualc\VS2017\mbedTLS.sln del /Q /F visualc\VS2017\mbedTLS.sln
168a8e1175bSopenharmony_ciendif
169a8e1175bSopenharmony_ci
170a8e1175bSopenharmony_cicheck: lib tests
171a8e1175bSopenharmony_ci	$(MAKE) -C tests check
172a8e1175bSopenharmony_ci
173a8e1175bSopenharmony_citest: check
174a8e1175bSopenharmony_ci
175a8e1175bSopenharmony_ciifndef WINDOWS
176a8e1175bSopenharmony_ci# For coverage testing:
177a8e1175bSopenharmony_ci# 1. Build with:
178a8e1175bSopenharmony_ci#         make CFLAGS='--coverage -g3 -O0' LDFLAGS='--coverage'
179a8e1175bSopenharmony_ci# 2. Run the relevant tests for the part of the code you're interested in.
180a8e1175bSopenharmony_ci#    For the reference coverage measurement, see
181a8e1175bSopenharmony_ci#    tests/scripts/basic-build-test.sh
182a8e1175bSopenharmony_ci# 3. Run scripts/lcov.sh to generate an HTML report.
183a8e1175bSopenharmony_cilcov:
184a8e1175bSopenharmony_ci	scripts/lcov.sh
185a8e1175bSopenharmony_ci
186a8e1175bSopenharmony_ciapidoc:
187a8e1175bSopenharmony_ci	mkdir -p apidoc
188a8e1175bSopenharmony_ci	cd doxygen && doxygen mbedtls.doxyfile
189a8e1175bSopenharmony_ci
190a8e1175bSopenharmony_ciapidoc_clean:
191a8e1175bSopenharmony_ci	rm -rf apidoc
192a8e1175bSopenharmony_ciendif
193a8e1175bSopenharmony_ci
194a8e1175bSopenharmony_ci## Editor navigation files
195a8e1175bSopenharmony_ciC_SOURCE_FILES = $(wildcard \
196a8e1175bSopenharmony_ci	3rdparty/*/include/*/*.h 3rdparty/*/include/*/*/*.h 3rdparty/*/include/*/*/*/*.h \
197a8e1175bSopenharmony_ci	3rdparty/*/*.c 3rdparty/*/*/*.c 3rdparty/*/*/*/*.c 3rdparty/*/*/*/*/*.c \
198a8e1175bSopenharmony_ci	include/*/*.h \
199a8e1175bSopenharmony_ci	library/*.[hc] \
200a8e1175bSopenharmony_ci	programs/*/*.[hc] \
201a8e1175bSopenharmony_ci	tests/include/*/*.h tests/include/*/*/*.h \
202a8e1175bSopenharmony_ci	tests/src/*.c tests/src/*/*.c \
203a8e1175bSopenharmony_ci	tests/suites/*.function \
204a8e1175bSopenharmony_ci)
205a8e1175bSopenharmony_ci# Exuberant-ctags invocation. Other ctags implementations may require different options.
206a8e1175bSopenharmony_ciCTAGS = ctags --langmap=c:+.h.function --line-directives=no -o
207a8e1175bSopenharmony_citags: $(C_SOURCE_FILES)
208a8e1175bSopenharmony_ci	$(CTAGS) $@ $(C_SOURCE_FILES)
209a8e1175bSopenharmony_ciTAGS: $(C_SOURCE_FILES)
210a8e1175bSopenharmony_ci	etags --no-line-directive -o $@ $(C_SOURCE_FILES)
211a8e1175bSopenharmony_ciglobal: GPATH GRTAGS GSYMS GTAGS
212a8e1175bSopenharmony_ciGPATH GRTAGS GSYMS GTAGS: $(C_SOURCE_FILES)
213a8e1175bSopenharmony_ci	ls $(C_SOURCE_FILES) | gtags -f - --gtagsconf .globalrc
214a8e1175bSopenharmony_cicscope: cscope.in.out cscope.po.out cscope.out
215a8e1175bSopenharmony_cicscope.in.out cscope.po.out cscope.out: $(C_SOURCE_FILES)
216a8e1175bSopenharmony_ci	cscope -bq -u -Iinclude -Ilibrary $(patsubst %,-I%,$(wildcard 3rdparty/*/include)) -Itests/include $(C_SOURCE_FILES)
217a8e1175bSopenharmony_ci.PHONY: cscope global
218