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