1
2# To compile on SunOS: add "-lsocket -lnsl" to LDFLAGS
3# To compile with PKCS11: add "-lpkcs11-helper" to LDFLAGS
4
5CFLAGS	?= -O2
6WARNING_CFLAGS ?= -Wall -Wextra
7LDFLAGS ?=
8
9# Include public header files from ../include, test-specific header files
10# from ./include, and private header files (used by some invasive tests)
11# from ../library.
12LOCAL_CFLAGS = $(WARNING_CFLAGS) -I./include -I../include -I../library -D_FILE_OFFSET_BITS=64
13LOCAL_LDFLAGS = -L../library			\
14		-lmbedtls$(SHARED_SUFFIX)	\
15		-lmbedx509$(SHARED_SUFFIX)	\
16		-lmbedcrypto$(SHARED_SUFFIX)
17
18include ../3rdparty/Makefile.inc
19LOCAL_CFLAGS+=$(THIRDPARTY_INCLUDES)
20
21# Enable definition of various functions used throughout the testsuite
22# (gethostname, strdup, fileno...) even when compiling with -std=c99. Harmless
23# on non-POSIX platforms.
24LOCAL_CFLAGS += -D_POSIX_C_SOURCE=200809L
25
26ifndef SHARED
27MBEDLIBS=../library/libmbedcrypto.a ../library/libmbedx509.a ../library/libmbedtls.a
28else
29MBEDLIBS=../library/libmbedcrypto.$(DLEXT) ../library/libmbedx509.$(DLEXT) ../library/libmbedtls.$(DLEXT)
30endif
31
32ifdef DEBUG
33LOCAL_CFLAGS += -g3
34endif
35
36ifdef RECORD_PSA_STATUS_COVERAGE_LOG
37LOCAL_CFLAGS += -Werror -DRECORD_PSA_STATUS_COVERAGE_LOG
38endif
39
40# if we're running on Windows, build for Windows
41ifdef WINDOWS
42WINDOWS_BUILD=1
43endif
44
45ifdef WINDOWS_BUILD
46DLEXT=dll
47EXEXT=.exe
48LOCAL_LDFLAGS += -lws2_32
49ifdef SHARED
50SHARED_SUFFIX=.$(DLEXT)
51endif
52PYTHON ?= python
53else
54DLEXT ?= so
55EXEXT=
56SHARED_SUFFIX=
57# python2 for POSIX since FreeBSD has only python2 as default.
58PYTHON ?= python2
59endif
60
61# Zlib shared library extensions:
62ifdef ZLIB
63LOCAL_LDFLAGS += -lz
64endif
65
66# A test application is built for each suites/test_suite_*.data file.
67# Application name is same as .data file's base name and can be
68# constructed by stripping path 'suites/' and extension .data.
69APPS = $(basename $(subst suites/,,$(wildcard suites/test_suite_*.data)))
70
71# Construct executable name by adding OS specific suffix $(EXEXT).
72BINARIES := $(addsuffix $(EXEXT),$(APPS))
73
74.SILENT:
75
76.PHONY: all check test clean
77
78all: $(BINARIES)
79
80$(MBEDLIBS):
81	$(MAKE) -C ../library
82
83MBEDTLS_TEST_OBJS=$(patsubst %.c,%.o,$(wildcard src/*.c src/drivers/*.c))
84
85mbedtls_test: $(MBEDTLS_TEST_OBJS)
86
87# Rule to compile common test C files in src folder
88src/%.o : src/%.c
89	echo "  CC    $<"
90	$(CC) $(LOCAL_CFLAGS) $(CFLAGS) -o $@ -c $<
91
92src/drivers/%.o : src/drivers/%.c
93	echo "  CC    $<"
94	$(CC) $(LOCAL_CFLAGS) $(CFLAGS) -o $@ -c $<
95
96C_FILES := $(addsuffix .c,$(APPS))
97
98# Wildcard target for test code generation:
99# A .c file is generated for each .data file in the suites/ directory. Each .c
100# file depends on a .data and .function file from suites/ directory. Following
101# nameing convention is followed:
102#
103#     C file        |        Depends on
104#-----------------------------------------------------------------------------
105#  foo.c            | suites/foo.function suites/foo.data
106#  foo.bar.c        | suites/foo.function suites/foo.bar.data
107#
108# Note above that .c and .data files have same base name.
109# However, corresponding .function file's base name is the word before first
110# dot in .c file's base name.
111#
112.SECONDEXPANSION:
113%.c: suites/$$(firstword $$(subst ., ,$$*)).function suites/%.data scripts/generate_test_code.py suites/helpers.function suites/main_test.function suites/host_test.function
114	echo "  Gen   $@"
115	$(PYTHON) scripts/generate_test_code.py -f suites/$(firstword $(subst ., ,$*)).function \
116		-d suites/$*.data \
117		-t suites/main_test.function \
118		-p suites/host_test.function \
119		-s suites  \
120		--helpers-file suites/helpers.function \
121		-o .
122
123
124$(BINARIES): %$(EXEXT): %.c $(MBEDLIBS) $(MBEDTLS_TEST_OBJS)
125	echo "  CC    $<"
126	$(CC) $(LOCAL_CFLAGS) $(CFLAGS) $< $(MBEDTLS_TEST_OBJS) $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
127
128# Some test suites require additional header files.
129$(filter test_suite_psa_crypto%, $(BINARIES)): include/test/psa_crypto_helpers.h
130$(addprefix embedded_,$(filter test_suite_psa_crypto%, $(APPS))): embedded_%: TESTS/mbedtls/%/psa_crypto_helpers.h
131$(filter test_suite_psa_%, $(BINARIES)): include/test/psa_helpers.h
132$(addprefix embedded_,$(filter test_suite_psa_%, $(APPS))): embedded_%: TESTS/mbedtls/%/psa_helpers.h
133
134clean:
135ifndef WINDOWS
136	rm -rf $(BINARIES) *.c *.datax TESTS
137	rm -f src/*.o src/drivers/*.o src/libmbed*
138else
139	if exist *.c del /Q /F *.c
140	if exist *.exe del /Q /F *.exe
141	if exist *.datax del /Q /F *.datax
142	if exist src/*.o del /Q /F src/*.o
143	if exist src/drivers/*.o del /Q /F src/drivers/*.o
144	if exist src/libmbed* del /Q /F src/libmed*
145ifneq ($(wildcard TESTS/.*),)
146	rmdir /Q /S TESTS
147endif
148endif
149
150# Test suites caught by SKIP_TEST_SUITES are built but not executed.
151check: $(BINARIES)
152	perl scripts/run-test-suites.pl --skip=$(SKIP_TEST_SUITES)
153
154test: check
155
156# Create separate targets for generating embedded tests.
157EMBEDDED_TESTS := $(addprefix embedded_,$(APPS))
158
159# Generate test code for target.
160
161.SECONDEXPANSION:
162$(EMBEDDED_TESTS): embedded_%: suites/$$(firstword $$(subst ., ,$$*)).function suites/%.data scripts/generate_test_code.py suites/helpers.function suites/main_test.function suites/target_test.function
163	echo "  Gen  ./TESTS/mbedtls/$*/$*.c"
164	$(PYTHON) scripts/generate_test_code.py -f suites/$(firstword $(subst ., ,$*)).function \
165		-d suites/$*.data \
166		-t suites/main_test.function \
167		-p suites/target_test.function \
168		-s suites  \
169		--helpers-file suites/helpers.function \
170		-o ./TESTS/mbedtls/$*
171
172generate-target-tests: $(EMBEDDED_TESTS)
173
174define copy_header_to_target
175TESTS/mbedtls/$(1)/$(2): include/test/$(2)
176	echo "  Copy ./$$@"
177ifndef WINDOWS
178	mkdir -p $$(@D)
179	cp $$< $$@
180else
181	mkdir $$(@D)
182	copy $$< $$@
183endif
184
185endef
186$(foreach app, $(APPS), $(foreach file, $(notdir $(wildcard include/test/*.h)), \
187	$(eval $(call copy_header_to_target,$(app),$(file)))))
188
189ifdef RECORD_PSA_STATUS_COVERAGE_LOG
190$(BINARIES): include/test/instrument_record_status.h
191include/test/instrument_record_status.h: ../include/psa/crypto.h Makefile
192	sed <../include/psa/crypto.h >$@ -n 's/^psa_status_t \([A-Za-z0-9_]*\)(.*/#define \1(...) RECORD_STATUS("\1", \1(__VA_ARGS__))/p'
193endif
194