blob: 44ff43c675902dd59b8aa8ad07d24709deb69025 [file] [log] [blame]
Paul Bakker0049c2f2009-07-11 19:15:43 +00001
2# To compile on SunOS: add "-lsocket -lnsl" to LDFLAGS
Gilles Peskine6bbe7832020-02-26 19:13:28 +01003# To compile with PKCS11: add "-lpkcs11-helper" to LDFLAGS
Paul Bakker0049c2f2009-07-11 19:15:43 +00004
Mohammad Azim Khan95402612017-07-19 10:15:54 +01005CFLAGS ?= -O2
Paul Elliott9e3256a2020-12-17 18:17:32 +00006WARNING_CFLAGS ?= -Wall -Wextra -Wformat=2 -Wno-format-nonliteral
Alon Bar-Levada41052015-02-18 17:47:52 +02007LDFLAGS ?=
Paul Bakker0049c2f2009-07-11 19:15:43 +00008
Gilles Peskinec761d8f2021-09-20 18:57:55 +02009# Set this to -v to see the details of failing test cases
10TEST_FLAGS ?= $(if $(filter-out 0 OFF Off off NO No no FALSE False false N n,$(CTEST_OUTPUT_ON_FAILURE)),-v,)
11
Gilles Peskine76dd3aa2020-07-02 15:58:37 +020012# Include public header files from ../include, test-specific header files
13# from ./include, and private header files (used by some invasive tests)
14# from ../library.
Ronald Cron02c78b72020-05-27 09:22:32 +020015LOCAL_CFLAGS = $(WARNING_CFLAGS) -I./include -I../include -I../library -D_FILE_OFFSET_BITS=64
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +000016LOCAL_LDFLAGS = -L../library \
Gilles Peskine4fa9f9f2020-02-26 19:05:31 +010017 -lmbedtls$(SHARED_SUFFIX) \
18 -lmbedx509$(SHARED_SUFFIX) \
Manuel Pégourié-Gonnard21e1ac22015-06-25 08:45:12 +020019 -lmbedcrypto$(SHARED_SUFFIX)
Paul Bakker0049c2f2009-07-11 19:15:43 +000020
Christoph M. Wintersteiger6ea2dea12019-01-21 17:26:19 +000021include ../3rdparty/Makefile.inc
22LOCAL_CFLAGS+=$(THIRDPARTY_INCLUDES)
Christoph M. Wintersteigerd5fd7662018-10-25 12:47:03 +010023
Nicholas Wilson61fa4362018-06-25 12:10:00 +010024# Enable definition of various functions used throughout the testsuite
25# (gethostname, strdup, fileno...) even when compiling with -std=c99. Harmless
26# on non-POSIX platforms.
27LOCAL_CFLAGS += -D_POSIX_C_SOURCE=200809L
28
Paul Bakker674e0b02014-03-26 13:26:52 +010029ifndef SHARED
Ronald Cronb6d6d4c2020-06-03 10:11:18 +020030MBEDLIBS=../library/libmbedcrypto.a ../library/libmbedx509.a ../library/libmbedtls.a
Paul Bakker674e0b02014-03-26 13:26:52 +010031else
Ronald Cronb6d6d4c2020-06-03 10:11:18 +020032MBEDLIBS=../library/libmbedcrypto.$(DLEXT) ../library/libmbedx509.$(DLEXT) ../library/libmbedtls.$(DLEXT)
Paul Bakker674e0b02014-03-26 13:26:52 +010033endif
34
Paul Bakkerc7ffd362012-04-05 12:08:29 +000035ifdef DEBUG
Alon Bar-Levf7a9f302015-02-18 17:55:05 +020036LOCAL_CFLAGS += -g3
Paul Bakkerc7ffd362012-04-05 12:08:29 +000037endif
38
Gilles Peskine51681552019-05-20 19:35:37 +020039ifdef RECORD_PSA_STATUS_COVERAGE_LOG
40LOCAL_CFLAGS += -Werror -DRECORD_PSA_STATUS_COVERAGE_LOG
41endif
42
Manuel Pégourié-Gonnard5c59a4f2015-06-24 13:06:24 +020043# if we're running on Windows, build for Windows
Paul Bakkercd5b5292012-05-10 20:49:10 +000044ifdef WINDOWS
Alon Bar-Lev18ba0cc2015-02-14 01:04:58 +020045WINDOWS_BUILD=1
46endif
47
48ifdef WINDOWS_BUILD
49DLEXT=dll
50EXEXT=.exe
Alon Bar-Levada41052015-02-18 17:47:52 +020051LOCAL_LDFLAGS += -lws2_32
Alon Bar-Lev18ba0cc2015-02-14 01:04:58 +020052ifdef SHARED
53SHARED_SUFFIX=.$(DLEXT)
54endif
Manuel Pégourié-Gonnard8d4a6132015-06-24 12:16:20 +020055else
Andres Amaya Garcia420f0cc2018-03-27 19:17:21 +010056DLEXT ?= so
Manuel Pégourié-Gonnard8d4a6132015-06-24 12:16:20 +020057EXEXT=
58SHARED_SUFFIX=
Gilles Peskinecdd80c42021-07-13 18:14:25 +020059endif
60
61ifdef WINDOWS
62PYTHON ?= python
63else
Gilles Peskine6b0f9112021-04-22 00:21:58 +020064PYTHON ?= $(shell if type python3 >/dev/null 2>/dev/null; then echo python3; else echo python; fi)
Paul Bakkercd5b5292012-05-10 20:49:10 +000065endif
66
Gilles Peskine5bb8bec2020-02-26 19:11:43 +010067# Zlib shared library extensions:
68ifdef ZLIB
69LOCAL_LDFLAGS += -lz
70endif
71
Azim Khan27a35e72018-06-29 12:39:19 +010072# A test application is built for each suites/test_suite_*.data file.
73# Application name is same as .data file's base name and can be
74# constructed by stripping path 'suites/' and extension .data.
75APPS = $(basename $(subst suites/,,$(wildcard suites/test_suite_*.data)))
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +000076
Azim Khan27a35e72018-06-29 12:39:19 +010077# Construct executable name by adding OS specific suffix $(EXEXT).
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +000078BINARIES := $(addsuffix $(EXEXT),$(APPS))
79
Paul Bakker0049c2f2009-07-11 19:15:43 +000080.SILENT:
81
Manuel Pégourié-Gonnard5c59a4f2015-06-24 13:06:24 +020082.PHONY: all check test clean
83
Azim Khan1de892b2017-06-09 15:02:36 +010084all: $(BINARIES)
85
Ronald Cronb6d6d4c2020-06-03 10:11:18 +020086$(MBEDLIBS):
Manuel Pégourié-Gonnardfc367082015-06-26 16:50:24 +020087 $(MAKE) -C ../library
Manuel Pégourié-Gonnard5c59a4f2015-06-24 13:06:24 +020088
Steven Cooremana70d5882020-07-16 20:26:18 +020089MBEDTLS_TEST_OBJS=$(patsubst %.c,%.o,$(wildcard src/*.c src/drivers/*.c))
Ronald Cronb6d6d4c2020-06-03 10:11:18 +020090
Ronald Cronddaf99c2020-06-19 11:27:26 +020091mbedtls_test: $(MBEDTLS_TEST_OBJS)
92
Gilles Peskinee1d51bd2021-01-20 19:47:23 +010093TEST_OBJS_DEPS = $(wildcard include/test/*.h include/test/*/*.h)
Gilles Peskined71539f2020-11-25 18:17:17 +010094ifdef RECORD_PSA_STATUS_COVERAGE_LOG
Gilles Peskine75829a42021-01-25 13:46:14 +010095# Explicitly depend on this header because on a clean copy of the source tree,
96# it doesn't exist yet and must be generated as part of the build, and
97# therefore the wildcard enumeration above doesn't include it.
Gilles Peskined71539f2020-11-25 18:17:17 +010098TEST_OBJS_DEPS += include/test/instrument_record_status.h
99endif
100
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200101# Rule to compile common test C files in src folder
Gilles Peskined71539f2020-11-25 18:17:17 +0100102src/%.o : src/%.c $(TEST_OBJS_DEPS)
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200103 echo " CC $<"
104 $(CC) $(LOCAL_CFLAGS) $(CFLAGS) -o $@ -c $<
105
Steven Cooremana70d5882020-07-16 20:26:18 +0200106src/drivers/%.o : src/drivers/%.c
107 echo " CC $<"
108 $(CC) $(LOCAL_CFLAGS) $(CFLAGS) -o $@ -c $<
109
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +0000110C_FILES := $(addsuffix .c,$(APPS))
Gilles Peskine9e509fc2022-11-10 19:50:34 +0100111c: $(C_FILES)
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +0000112
Azim Khan27a35e72018-06-29 12:39:19 +0100113# Wildcard target for test code generation:
114# A .c file is generated for each .data file in the suites/ directory. Each .c
115# file depends on a .data and .function file from suites/ directory. Following
116# nameing convention is followed:
117#
118# C file | Depends on
119#-----------------------------------------------------------------------------
120# foo.c | suites/foo.function suites/foo.data
121# foo.bar.c | suites/foo.function suites/foo.bar.data
122#
123# Note above that .c and .data files have same base name.
124# However, corresponding .function file's base name is the word before first
125# dot in .c file's base name.
126#
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +0000127.SECONDEXPANSION:
Azim Khan27a35e72018-06-29 12:39:19 +0100128%.c: suites/$$(firstword $$(subst ., ,$$*)).function suites/%.data scripts/generate_test_code.py suites/helpers.function suites/main_test.function suites/host_test.function
Manuel Pégourié-Gonnard78ec2b02015-07-08 22:12:06 +0100129 echo " Gen $@"
Azim Khan27a35e72018-06-29 12:39:19 +0100130 $(PYTHON) scripts/generate_test_code.py -f suites/$(firstword $(subst ., ,$*)).function \
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100131 -d suites/$*.data \
Azim Khan1de892b2017-06-09 15:02:36 +0100132 -t suites/main_test.function \
Mohammad Azim Khan95402612017-07-19 10:15:54 +0100133 -p suites/host_test.function \
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100134 -s suites \
Azim Khane3b26af2018-06-29 02:36:57 +0100135 --helpers-file suites/helpers.function \
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100136 -o .
Paul Bakker286bf3c2013-04-08 18:09:51 +0200137
Paul Bakker286bf3c2013-04-08 18:09:51 +0200138
Gilles Peskined71539f2020-11-25 18:17:17 +0100139$(BINARIES): %$(EXEXT): %.c $(MBEDLIBS) $(TEST_OBJS_DEPS) $(MBEDTLS_TEST_OBJS)
Manuel Pégourié-Gonnard78ec2b02015-07-08 22:12:06 +0100140 echo " CC $<"
Ronald Cronf5ea29a2020-06-19 10:42:29 +0200141 $(CC) $(LOCAL_CFLAGS) $(CFLAGS) $< $(MBEDTLS_TEST_OBJS) $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
Paul Bakker286bf3c2013-04-08 18:09:51 +0200142
Paul Bakker0049c2f2009-07-11 19:15:43 +0000143clean:
Paul Bakker62f88dc2012-05-10 21:26:28 +0000144ifndef WINDOWS
Gilles Peskineff8c80a2021-09-14 11:28:22 +0200145 rm -rf $(BINARIES) *.c *.datax
Steven Cooremana70d5882020-07-16 20:26:18 +0200146 rm -f src/*.o src/drivers/*.o src/libmbed*
Gilles Peskined71539f2020-11-25 18:17:17 +0100147 rm -f include/test/instrument_record_status.h
Ronald Crondf885c02021-04-28 18:29:24 +0200148 rm -rf libtestdriver1
Manuel Pégourié-Gonnard5c59a4f2015-06-24 13:06:24 +0200149else
Darryl Green6c0f94c2018-10-17 16:12:33 +0100150 if exist *.c del /Q /F *.c
151 if exist *.exe del /Q /F *.exe
152 if exist *.datax del /Q /F *.datax
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200153 if exist src/*.o del /Q /F src/*.o
Steven Cooremana70d5882020-07-16 20:26:18 +0200154 if exist src/drivers/*.o del /Q /F src/drivers/*.o
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200155 if exist src/libmbed* del /Q /F src/libmed*
Gilles Peskined71539f2020-11-25 18:17:17 +0100156 if exist include/test/instrument_record_status.h del /Q /F include/test/instrument_record_status.h
Azim Khan27a35e72018-06-29 12:39:19 +0100157endif
Paul Bakker0049c2f2009-07-11 19:15:43 +0000158
Gilles Peskineac372cc2018-11-29 10:15:06 +0000159# Test suites caught by SKIP_TEST_SUITES are built but not executed.
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +0000160check: $(BINARIES)
Gilles Peskinec761d8f2021-09-20 18:57:55 +0200161 perl scripts/run-test-suites.pl $(TEST_FLAGS) --skip=$(SKIP_TEST_SUITES)
Manuel Pégourié-Gonnard5c59a4f2015-06-24 13:06:24 +0200162
163test: check
Mohammad Azim Khan1f29be72017-03-20 22:21:22 +0000164
Ronald Crondf885c02021-04-28 18:29:24 +0200165# Generate test library
166
167# Perl code that is executed to transform each original line from a library
168# source file into the corresponding line in the test driver copy of the
169# library. Add a LIBTESTDRIVER1_/libtestdriver1_ to mbedtls_xxx and psa_xxx
170# symbols.
171define libtestdriver1_rewrite :=
172 s!^(\s*#\s*include\s*[\"<])(mbedtls|psa)/!$${1}libtestdriver1/include/$${2}/!; \
173 next if /^\s*#\s*include/; \
174 s/\b(?=MBEDTLS_|PSA_)/LIBTESTDRIVER1_/g; \
175 s/\b(?=mbedtls_|psa_)/libtestdriver1_/g;
176endef
177
178libtestdriver1.a:
179 # Copy the library and fake a 3rdparty Makefile include.
180 rm -Rf ./libtestdriver1
181 mkdir ./libtestdriver1
182 cp -Rf ../library ./libtestdriver1
183 cp -Rf ../include ./libtestdriver1
184 mkdir ./libtestdriver1/3rdparty
185 touch ./libtestdriver1/3rdparty/Makefile.inc
186
187 # Set the test driver base (minimal) configuration.
188 cp ./include/test/drivers/config_test_driver.h ./libtestdriver1/include/mbedtls/config.h
189
190 # Set the PSA cryptography configuration for the test library.
191 # It is set from the copied include/psa/crypto_config.h of the Mbed TLS
192 # library the test library is intended to be linked with extended by
193 # ./include/test/drivers/crypto_config_test_driver_extension.h to
194 # mirror the PSA_ACCEL_* macros.
195 mv ./libtestdriver1/include/psa/crypto_config.h ./libtestdriver1/include/psa/crypto_config.h.bak
196 head -n -1 ./libtestdriver1/include/psa/crypto_config.h.bak > ./libtestdriver1/include/psa/crypto_config.h
197 cat ./include/test/drivers/crypto_config_test_driver_extension.h >> ./libtestdriver1/include/psa/crypto_config.h
198 echo "#endif /* PSA_CRYPTO_CONFIG_H */" >> ./libtestdriver1/include/psa/crypto_config.h
199
200 # Prefix MBEDTLS_* PSA_* symbols with LIBTESTDRIVER1_ as well as
201 # mbedtls_* psa_* symbols with libtestdriver1_ to avoid symbol clash
202 # when this test driver library is linked with the Mbed TLS library.
203 perl -pi -e '$(libtestdriver1_rewrite)' ./libtestdriver1/library/*.[ch]
204 perl -pi -e '$(libtestdriver1_rewrite)' ./libtestdriver1/include/*/*.h
205
206 $(MAKE) -C ./libtestdriver1/library CFLAGS="-I../../ $(CFLAGS)" LDFLAGS="$(LDFLAGS)" libmbedcrypto.a
207 cp ./libtestdriver1/library/libmbedcrypto.a ../library/libtestdriver1.a
208
Gilles Peskine51681552019-05-20 19:35:37 +0200209ifdef RECORD_PSA_STATUS_COVERAGE_LOG
Ronald Cron02c78b72020-05-27 09:22:32 +0200210include/test/instrument_record_status.h: ../include/psa/crypto.h Makefile
Gilles Peskined71539f2020-11-25 18:17:17 +0100211 echo " Gen $@"
Gilles Peskine51681552019-05-20 19:35:37 +0200212 sed <../include/psa/crypto.h >$@ -n 's/^psa_status_t \([A-Za-z0-9_]*\)(.*/#define \1(...) RECORD_STATUS("\1", \1(__VA_ARGS__))/p'
213endif