1
2# To compile on SunOS: add "-lsocket -lnsl" to LDFLAGS
3
4CFLAGS	?= -O2
5WARNING_CFLAGS ?= -Wall -Wextra -Wformat=2 -Wno-format-nonliteral
6LDFLAGS ?=
7
8# Set this to -v to see the details of failing test cases
9TEST_FLAGS ?= $(if $(filter-out 0 OFF Off off NO No no FALSE False false N n,$(CTEST_OUTPUT_ON_FAILURE)),-v,)
10
11default: all
12
13# Include public header files from ../include, test-specific header files
14# from ./include, and private header files (used by some invasive tests)
15# from ../library.
16LOCAL_CFLAGS = $(WARNING_CFLAGS) -I./include -I../include -I../library -D_FILE_OFFSET_BITS=64
17LOCAL_LDFLAGS = -L../library			\
18		-lmbedtls$(SHARED_SUFFIX)	\
19		-lmbedx509$(SHARED_SUFFIX)	\
20		-lmbedcrypto$(SHARED_SUFFIX)
21
22include ../3rdparty/Makefile.inc
23LOCAL_CFLAGS+=$(THIRDPARTY_INCLUDES)
24
25# Enable definition of various functions used throughout the testsuite
26# (gethostname, strdup, fileno...) even when compiling with -std=c99. Harmless
27# on non-POSIX platforms.
28LOCAL_CFLAGS += -D_POSIX_C_SOURCE=200809L
29
30ifndef SHARED
31MBEDLIBS=../library/libmbedcrypto.a ../library/libmbedx509.a ../library/libmbedtls.a
32else
33MBEDLIBS=../library/libmbedcrypto.$(DLEXT) ../library/libmbedx509.$(DLEXT) ../library/libmbedtls.$(DLEXT)
34endif
35
36ifdef DEBUG
37LOCAL_CFLAGS += -g3
38endif
39
40ifdef RECORD_PSA_STATUS_COVERAGE_LOG
41LOCAL_CFLAGS += -Werror -DRECORD_PSA_STATUS_COVERAGE_LOG
42endif
43
44# if we're running on Windows, build for Windows
45ifdef WINDOWS
46WINDOWS_BUILD=1
47endif
48
49ifdef WINDOWS_BUILD
50DLEXT=dll
51EXEXT=.exe
52LOCAL_LDFLAGS += -lws2_32
53ifdef SHARED
54SHARED_SUFFIX=.$(DLEXT)
55endif
56else
57DLEXT ?= so
58EXEXT=
59SHARED_SUFFIX=
60endif
61
62ifdef WINDOWS
63PYTHON ?= python
64else
65PYTHON ?= $(shell if type python3 >/dev/null 2>/dev/null; then echo python3; else echo python; fi)
66endif
67
68.PHONY: generated_files
69GENERATED_DATA_FILES := $(patsubst tests/%,%,$(shell \
70	$(PYTHON) scripts/generate_psa_tests.py --list || \
71	echo FAILED \
72))
73ifeq ($(GENERATED_DATA_FILES),FAILED)
74$(error "$(PYTHON) scripts/generate_psa_tests.py --list" failed)
75endif
76GENERATED_FILES := $(GENERATED_DATA_FILES)
77generated_files: $(GENERATED_FILES)
78
79# generate_psa_tests.py spends more time analyzing inputs than generating
80# outputs. Its inputs are the same no matter which files are being generated.
81# It's rare not to want all the outputs. So always generate all of its outputs.
82# Use an intermediate phony dependency so that parallel builds don't run
83# a separate instance of the recipe for each output file.
84.SECONDARY: generated_psa_test_data
85$(GENERATED_DATA_FILES): generated_psa_test_data
86generated_psa_test_data: scripts/generate_psa_tests.py
87## The generated file only depends on the options that are present in
88## crypto_config.h, not on which options are set. To avoid regenerating this
89## file all the time when switching between configurations, don't declare
90## crypto_config.h as a dependency. Remove this file from your working tree
91## if you've just added or removed an option in crypto_config.h.
92#generated_psa_test_data: ../include/psa/crypto_config.h
93generated_psa_test_data: ../include/psa/crypto_values.h
94generated_psa_test_data: ../include/psa/crypto_extra.h
95generated_psa_test_data: suites/test_suite_psa_crypto_metadata.data
96generated_psa_test_data:
97	echo "  Gen   $(GENERATED_DATA_FILES) ..."
98	$(PYTHON) scripts/generate_psa_tests.py
99
100# A test application is built for each suites/test_suite_*.data file.
101# Application name is same as .data file's base name and can be
102# constructed by stripping path 'suites/' and extension .data.
103DATA_FILES := $(wildcard suites/test_suite_*.data)
104# Make sure that generated data files are included even if they don't
105# exist yet when the makefile is parsed.
106DATA_FILES += $(filter-out $(DATA_FILES),$(GENERATED_DATA_FILES))
107APPS = $(basename $(subst suites/,,$(DATA_FILES)))
108
109# Construct executable name by adding OS specific suffix $(EXEXT).
110BINARIES := $(addsuffix $(EXEXT),$(APPS))
111
112.SILENT:
113
114.PHONY: all check test clean
115
116all: $(BINARIES)
117
118$(MBEDLIBS):
119	$(MAKE) -C ../library
120
121MBEDTLS_TEST_OBJS=$(patsubst %.c,%.o,$(wildcard src/*.c src/drivers/*.c))
122
123mbedtls_test: $(MBEDTLS_TEST_OBJS)
124
125TEST_OBJS_DEPS = $(wildcard include/test/*.h include/test/*/*.h)
126ifdef RECORD_PSA_STATUS_COVERAGE_LOG
127# Explicitly depend on this header because on a clean copy of the source tree,
128# it doesn't exist yet and must be generated as part of the build, and
129# therefore the wildcard enumeration above doesn't include it.
130TEST_OBJS_DEPS += include/test/instrument_record_status.h
131endif
132
133# Rule to compile common test C files in src folder
134src/%.o : src/%.c $(TEST_OBJS_DEPS)
135	echo "  CC    $<"
136	$(CC) $(LOCAL_CFLAGS) $(CFLAGS) -o $@ -c $<
137
138src/drivers/%.o : src/drivers/%.c
139	echo "  CC    $<"
140	$(CC) $(LOCAL_CFLAGS) $(CFLAGS) -o $@ -c $<
141
142C_FILES := $(addsuffix .c,$(APPS))
143
144# Wildcard target for test code generation:
145# A .c file is generated for each .data file in the suites/ directory. Each .c
146# file depends on a .data and .function file from suites/ directory. Following
147# nameing convention is followed:
148#
149#     C file        |        Depends on
150#-----------------------------------------------------------------------------
151#  foo.c            | suites/foo.function suites/foo.data
152#  foo.bar.c        | suites/foo.function suites/foo.bar.data
153#
154# Note above that .c and .data files have same base name.
155# However, corresponding .function file's base name is the word before first
156# dot in .c file's base name.
157#
158.SECONDEXPANSION:
159%.c: suites/$$(firstword $$(subst ., ,$$*)).function suites/%.data scripts/generate_test_code.py suites/helpers.function suites/main_test.function suites/host_test.function
160	echo "  Gen   $@"
161	$(PYTHON) scripts/generate_test_code.py -f suites/$(firstword $(subst ., ,$*)).function \
162		-d suites/$*.data \
163		-t suites/main_test.function \
164		-p suites/host_test.function \
165		-s suites  \
166		--helpers-file suites/helpers.function \
167		-o .
168
169
170$(BINARIES): %$(EXEXT): %.c $(MBEDLIBS) $(TEST_OBJS_DEPS) $(MBEDTLS_TEST_OBJS)
171	echo "  CC    $<"
172	$(CC) $(LOCAL_CFLAGS) $(CFLAGS) $< $(MBEDTLS_TEST_OBJS) $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
173
174clean:
175ifndef WINDOWS
176	rm -rf $(BINARIES) *.c *.datax
177	rm -f src/*.o src/drivers/*.o src/libmbed*
178	rm -f include/test/instrument_record_status.h
179else
180	if exist *.c del /Q /F *.c
181	if exist *.exe del /Q /F *.exe
182	if exist *.datax del /Q /F *.datax
183	if exist src/*.o del /Q /F src/*.o
184	if exist src/drivers/*.o del /Q /F src/drivers/*.o
185	if exist src/libmbed* del /Q /F src/libmed*
186	if exist include/test/instrument_record_status.h del /Q /F include/test/instrument_record_status.h
187endif
188
189neat: clean
190ifndef WINDOWS
191	rm -f $(GENERATED_FILES)
192else
193	for %f in ($(subst /,\,$(GENERATED_FILES))) if exist %f del /Q /F %f
194endif
195
196# Test suites caught by SKIP_TEST_SUITES are built but not executed.
197check: $(BINARIES)
198	perl scripts/run-test-suites.pl $(TEST_FLAGS) --skip=$(SKIP_TEST_SUITES)
199
200test: check
201
202ifdef RECORD_PSA_STATUS_COVERAGE_LOG
203include/test/instrument_record_status.h: ../include/psa/crypto.h Makefile
204	echo "  Gen  $@"
205	sed <../include/psa/crypto.h >$@ -n 's/^psa_status_t \([A-Za-z0-9_]*\)(.*/#define \1(...) RECORD_STATUS("\1", \1(__VA_ARGS__))/p'
206endif
207