blob: 7afd0f59f6f5b42df94acbf7af4061448cfae5ca [file] [log] [blame]
Paul Bakker0049c2f2009-07-11 19:15:43 +00001
2# To compile on SunOS: add "-lsocket -lnsl" to LDFLAGS
Paul Bakker0049c2f2009-07-11 19:15:43 +00003
Mohammad Azim Khan95402612017-07-19 10:15:54 +01004CFLAGS ?= -O2
Paul Elliott9e3256a2020-12-17 18:17:32 +00005WARNING_CFLAGS ?= -Wall -Wextra -Wformat=2 -Wno-format-nonliteral
Alon Bar-Levada41052015-02-18 17:47:52 +02006LDFLAGS ?=
Paul Bakker0049c2f2009-07-11 19:15:43 +00007
Gilles Peskine76dd3aa2020-07-02 15:58:37 +02008# Include public header files from ../include, test-specific header files
9# from ./include, and private header files (used by some invasive tests)
10# from ../library.
Ronald Cron02c78b72020-05-27 09:22:32 +020011LOCAL_CFLAGS = $(WARNING_CFLAGS) -I./include -I../include -I../library -D_FILE_OFFSET_BITS=64
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +000012LOCAL_LDFLAGS = -L../library \
Gilles Peskine4fa9f9f2020-02-26 19:05:31 +010013 -lmbedtls$(SHARED_SUFFIX) \
14 -lmbedx509$(SHARED_SUFFIX) \
Manuel Pégourié-Gonnard21e1ac22015-06-25 08:45:12 +020015 -lmbedcrypto$(SHARED_SUFFIX)
Paul Bakker0049c2f2009-07-11 19:15:43 +000016
Christoph M. Wintersteiger6ea2dea12019-01-21 17:26:19 +000017include ../3rdparty/Makefile.inc
18LOCAL_CFLAGS+=$(THIRDPARTY_INCLUDES)
Christoph M. Wintersteigerd5fd7662018-10-25 12:47:03 +010019
Nicholas Wilson61fa4362018-06-25 12:10:00 +010020# Enable definition of various functions used throughout the testsuite
21# (gethostname, strdup, fileno...) even when compiling with -std=c99. Harmless
22# on non-POSIX platforms.
23LOCAL_CFLAGS += -D_POSIX_C_SOURCE=200809L
24
Paul Bakker674e0b02014-03-26 13:26:52 +010025ifndef SHARED
Ronald Cronb6d6d4c2020-06-03 10:11:18 +020026MBEDLIBS=../library/libmbedcrypto.a ../library/libmbedx509.a ../library/libmbedtls.a
Paul Bakker674e0b02014-03-26 13:26:52 +010027else
Ronald Cronb6d6d4c2020-06-03 10:11:18 +020028MBEDLIBS=../library/libmbedcrypto.$(DLEXT) ../library/libmbedx509.$(DLEXT) ../library/libmbedtls.$(DLEXT)
Paul Bakker674e0b02014-03-26 13:26:52 +010029endif
30
Paul Bakkerc7ffd362012-04-05 12:08:29 +000031ifdef DEBUG
Alon Bar-Levf7a9f302015-02-18 17:55:05 +020032LOCAL_CFLAGS += -g3
Paul Bakkerc7ffd362012-04-05 12:08:29 +000033endif
34
Gilles Peskine51681552019-05-20 19:35:37 +020035ifdef RECORD_PSA_STATUS_COVERAGE_LOG
36LOCAL_CFLAGS += -Werror -DRECORD_PSA_STATUS_COVERAGE_LOG
37endif
38
Manuel Pégourié-Gonnard5c59a4f2015-06-24 13:06:24 +020039# if we're running on Windows, build for Windows
Paul Bakkercd5b5292012-05-10 20:49:10 +000040ifdef WINDOWS
Alon Bar-Lev18ba0cc2015-02-14 01:04:58 +020041WINDOWS_BUILD=1
42endif
43
44ifdef WINDOWS_BUILD
45DLEXT=dll
46EXEXT=.exe
Alon Bar-Levada41052015-02-18 17:47:52 +020047LOCAL_LDFLAGS += -lws2_32
Alon Bar-Lev18ba0cc2015-02-14 01:04:58 +020048ifdef SHARED
49SHARED_SUFFIX=.$(DLEXT)
50endif
Mohammad Azim Khan8a3628f2018-06-26 17:30:16 +010051PYTHON ?= python
Manuel Pégourié-Gonnard8d4a6132015-06-24 12:16:20 +020052else
Andres Amaya Garcia420f0cc2018-03-27 19:17:21 +010053DLEXT ?= so
Manuel Pégourié-Gonnard8d4a6132015-06-24 12:16:20 +020054EXEXT=
55SHARED_SUFFIX=
Gilles Peskineb61a6142021-04-22 00:21:58 +020056PYTHON ?= $(shell if type python3 >/dev/null 2>/dev/null; then echo python3; else echo python; fi)
Paul Bakkercd5b5292012-05-10 20:49:10 +000057endif
58
Azim Khan27a35e72018-06-29 12:39:19 +010059# A test application is built for each suites/test_suite_*.data file.
60# Application name is same as .data file's base name and can be
61# constructed by stripping path 'suites/' and extension .data.
62APPS = $(basename $(subst suites/,,$(wildcard suites/test_suite_*.data)))
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +000063
Azim Khan27a35e72018-06-29 12:39:19 +010064# Construct executable name by adding OS specific suffix $(EXEXT).
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +000065BINARIES := $(addsuffix $(EXEXT),$(APPS))
66
Paul Bakker0049c2f2009-07-11 19:15:43 +000067.SILENT:
68
Manuel Pégourié-Gonnard5c59a4f2015-06-24 13:06:24 +020069.PHONY: all check test clean
70
Azim Khan1de892b2017-06-09 15:02:36 +010071all: $(BINARIES)
72
Ronald Cronb6d6d4c2020-06-03 10:11:18 +020073$(MBEDLIBS):
Manuel Pégourié-Gonnardfc367082015-06-26 16:50:24 +020074 $(MAKE) -C ../library
Manuel Pégourié-Gonnard5c59a4f2015-06-24 13:06:24 +020075
Steven Cooremana70d5882020-07-16 20:26:18 +020076MBEDTLS_TEST_OBJS=$(patsubst %.c,%.o,$(wildcard src/*.c src/drivers/*.c))
Ronald Cronb6d6d4c2020-06-03 10:11:18 +020077
Ronald Cronddaf99c2020-06-19 11:27:26 +020078mbedtls_test: $(MBEDTLS_TEST_OBJS)
79
Gilles Peskinee1d51bd2021-01-20 19:47:23 +010080TEST_OBJS_DEPS = $(wildcard include/test/*.h include/test/*/*.h)
Gilles Peskined71539f2020-11-25 18:17:17 +010081ifdef RECORD_PSA_STATUS_COVERAGE_LOG
Gilles Peskine75829a42021-01-25 13:46:14 +010082# Explicitly depend on this header because on a clean copy of the source tree,
83# it doesn't exist yet and must be generated as part of the build, and
84# therefore the wildcard enumeration above doesn't include it.
Gilles Peskined71539f2020-11-25 18:17:17 +010085TEST_OBJS_DEPS += include/test/instrument_record_status.h
86endif
87
Ronald Cronb6d6d4c2020-06-03 10:11:18 +020088# Rule to compile common test C files in src folder
Gilles Peskined71539f2020-11-25 18:17:17 +010089src/%.o : src/%.c $(TEST_OBJS_DEPS)
Ronald Cronb6d6d4c2020-06-03 10:11:18 +020090 echo " CC $<"
91 $(CC) $(LOCAL_CFLAGS) $(CFLAGS) -o $@ -c $<
92
Steven Cooremana70d5882020-07-16 20:26:18 +020093src/drivers/%.o : src/drivers/%.c
94 echo " CC $<"
95 $(CC) $(LOCAL_CFLAGS) $(CFLAGS) -o $@ -c $<
96
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +000097C_FILES := $(addsuffix .c,$(APPS))
98
Azim Khan27a35e72018-06-29 12:39:19 +010099# Wildcard target for test code generation:
100# A .c file is generated for each .data file in the suites/ directory. Each .c
101# file depends on a .data and .function file from suites/ directory. Following
102# nameing convention is followed:
103#
104# C file | Depends on
105#-----------------------------------------------------------------------------
106# foo.c | suites/foo.function suites/foo.data
107# foo.bar.c | suites/foo.function suites/foo.bar.data
108#
109# Note above that .c and .data files have same base name.
110# However, corresponding .function file's base name is the word before first
111# dot in .c file's base name.
112#
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +0000113.SECONDEXPANSION:
Azim Khan27a35e72018-06-29 12:39:19 +0100114%.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 +0100115 echo " Gen $@"
Azim Khan27a35e72018-06-29 12:39:19 +0100116 $(PYTHON) scripts/generate_test_code.py -f suites/$(firstword $(subst ., ,$*)).function \
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100117 -d suites/$*.data \
Azim Khan1de892b2017-06-09 15:02:36 +0100118 -t suites/main_test.function \
Mohammad Azim Khan95402612017-07-19 10:15:54 +0100119 -p suites/host_test.function \
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100120 -s suites \
Azim Khane3b26af2018-06-29 02:36:57 +0100121 --helpers-file suites/helpers.function \
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100122 -o .
Paul Bakker286bf3c2013-04-08 18:09:51 +0200123
Paul Bakker286bf3c2013-04-08 18:09:51 +0200124
Gilles Peskined71539f2020-11-25 18:17:17 +0100125$(BINARIES): %$(EXEXT): %.c $(MBEDLIBS) $(TEST_OBJS_DEPS) $(MBEDTLS_TEST_OBJS)
Manuel Pégourié-Gonnard78ec2b02015-07-08 22:12:06 +0100126 echo " CC $<"
Ronald Cronf5ea29a2020-06-19 10:42:29 +0200127 $(CC) $(LOCAL_CFLAGS) $(CFLAGS) $< $(MBEDTLS_TEST_OBJS) $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
Paul Bakker286bf3c2013-04-08 18:09:51 +0200128
Paul Bakker0049c2f2009-07-11 19:15:43 +0000129clean:
Paul Bakker62f88dc2012-05-10 21:26:28 +0000130ifndef WINDOWS
Gilles Peskine15965542018-09-26 13:42:26 +0200131 rm -rf $(BINARIES) *.c *.datax TESTS
Steven Cooremana70d5882020-07-16 20:26:18 +0200132 rm -f src/*.o src/drivers/*.o src/libmbed*
Gilles Peskined71539f2020-11-25 18:17:17 +0100133 rm -f include/test/instrument_record_status.h
Manuel Pégourié-Gonnard5c59a4f2015-06-24 13:06:24 +0200134else
Darryl Green6c0f94c2018-10-17 16:12:33 +0100135 if exist *.c del /Q /F *.c
136 if exist *.exe del /Q /F *.exe
137 if exist *.datax del /Q /F *.datax
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200138 if exist src/*.o del /Q /F src/*.o
Steven Cooremana70d5882020-07-16 20:26:18 +0200139 if exist src/drivers/*.o del /Q /F src/drivers/*.o
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200140 if exist src/libmbed* del /Q /F src/libmed*
Gilles Peskined71539f2020-11-25 18:17:17 +0100141 if exist include/test/instrument_record_status.h del /Q /F include/test/instrument_record_status.h
Azim Khan27a35e72018-06-29 12:39:19 +0100142ifneq ($(wildcard TESTS/.*),)
Mohammad Azim Khan1f29be72017-03-20 22:21:22 +0000143 rmdir /Q /S TESTS
Paul Bakker62f88dc2012-05-10 21:26:28 +0000144endif
Azim Khan27a35e72018-06-29 12:39:19 +0100145endif
Paul Bakker0049c2f2009-07-11 19:15:43 +0000146
Gilles Peskineac372cc2018-11-29 10:15:06 +0000147# Test suites caught by SKIP_TEST_SUITES are built but not executed.
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +0000148check: $(BINARIES)
Gilles Peskineac372cc2018-11-29 10:15:06 +0000149 perl scripts/run-test-suites.pl --skip=$(SKIP_TEST_SUITES)
Manuel Pégourié-Gonnard5c59a4f2015-06-24 13:06:24 +0200150
151test: check
Mohammad Azim Khan1f29be72017-03-20 22:21:22 +0000152
Azim Khan1de892b2017-06-09 15:02:36 +0100153# Create separate targets for generating embedded tests.
154EMBEDDED_TESTS := $(addprefix embedded_,$(APPS))
Mohammad Azim Khan1f29be72017-03-20 22:21:22 +0000155
Mohammad Azim Khan95402612017-07-19 10:15:54 +0100156# Generate test code for target.
Mohammad Azim Khan1f29be72017-03-20 22:21:22 +0000157
158.SECONDEXPANSION:
Azim Khan27a35e72018-06-29 12:39:19 +0100159$(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
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100160 echo " Gen ./TESTS/mbedtls/$*/$*.c"
Azim Khan27a35e72018-06-29 12:39:19 +0100161 $(PYTHON) scripts/generate_test_code.py -f suites/$(firstword $(subst ., ,$*)).function \
Mohammad Azim Khan1f29be72017-03-20 22:21:22 +0000162 -d suites/$*.data \
Azim Khan1de892b2017-06-09 15:02:36 +0100163 -t suites/main_test.function \
Mohammad Azim Khan95402612017-07-19 10:15:54 +0100164 -p suites/target_test.function \
Mohammad Azim Khan1f29be72017-03-20 22:21:22 +0000165 -s suites \
Azim Khane3b26af2018-06-29 02:36:57 +0100166 --helpers-file suites/helpers.function \
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100167 -o ./TESTS/mbedtls/$*
Mohammad Azim Khan1f29be72017-03-20 22:21:22 +0000168
Mohammad Azim Khanff560f22018-06-28 11:43:17 +0100169generate-target-tests: $(EMBEDDED_TESTS)
Mohammad Azim Khan1f29be72017-03-20 22:21:22 +0000170
Gilles Peskine1d102572019-06-20 17:23:58 +0200171define copy_header_to_target
Ronald Cron02c78b72020-05-27 09:22:32 +0200172TESTS/mbedtls/$(1)/$(2): include/test/$(2)
Gilles Peskine1d102572019-06-20 17:23:58 +0200173 echo " Copy ./$$@"
174ifndef WINDOWS
175 mkdir -p $$(@D)
176 cp $$< $$@
177else
178 mkdir $$(@D)
179 copy $$< $$@
180endif
181
182endef
Ronald Cron02c78b72020-05-27 09:22:32 +0200183$(foreach app, $(APPS), $(foreach file, $(notdir $(wildcard include/test/*.h)), \
Gilles Peskine1d102572019-06-20 17:23:58 +0200184 $(eval $(call copy_header_to_target,$(app),$(file)))))
Gilles Peskinee1d51bd2021-01-20 19:47:23 +0100185$(addprefix embedded_,$(filter test_suite_psa_%, $(APPS))): embedded_%: $(patsubst TESTS/mbedtls/%, include/test/%, $(wildcard include/test/*. include/test/*/*.h))
Gilles Peskine51681552019-05-20 19:35:37 +0200186
187ifdef RECORD_PSA_STATUS_COVERAGE_LOG
Ronald Cron02c78b72020-05-27 09:22:32 +0200188include/test/instrument_record_status.h: ../include/psa/crypto.h Makefile
Gilles Peskined71539f2020-11-25 18:17:17 +0100189 echo " Gen $@"
Gilles Peskine51681552019-05-20 19:35:37 +0200190 sed <../include/psa/crypto.h >$@ -n 's/^psa_status_t \([A-Za-z0-9_]*\)(.*/#define \1(...) RECORD_STATUS("\1", \1(__VA_ARGS__))/p'
191endif