blob: 251a9c6fc90e0adb4fd998c10e1d5827c8d1317f [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 Peskine687d1ab2021-04-22 01:01:56 +02008default: all
9
Gilles Peskine76dd3aa2020-07-02 15:58:37 +020010# Include public header files from ../include, test-specific header files
11# from ./include, and private header files (used by some invasive tests)
12# from ../library.
Ronald Cron02c78b72020-05-27 09:22:32 +020013LOCAL_CFLAGS = $(WARNING_CFLAGS) -I./include -I../include -I../library -D_FILE_OFFSET_BITS=64
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +000014LOCAL_LDFLAGS = -L../library \
Gilles Peskine4fa9f9f2020-02-26 19:05:31 +010015 -lmbedtls$(SHARED_SUFFIX) \
16 -lmbedx509$(SHARED_SUFFIX) \
Manuel Pégourié-Gonnard21e1ac22015-06-25 08:45:12 +020017 -lmbedcrypto$(SHARED_SUFFIX)
Paul Bakker0049c2f2009-07-11 19:15:43 +000018
Christoph M. Wintersteiger6ea2dea12019-01-21 17:26:19 +000019include ../3rdparty/Makefile.inc
20LOCAL_CFLAGS+=$(THIRDPARTY_INCLUDES)
Christoph M. Wintersteigerd5fd7662018-10-25 12:47:03 +010021
Nicholas Wilson61fa4362018-06-25 12:10:00 +010022# Enable definition of various functions used throughout the testsuite
23# (gethostname, strdup, fileno...) even when compiling with -std=c99. Harmless
24# on non-POSIX platforms.
25LOCAL_CFLAGS += -D_POSIX_C_SOURCE=200809L
26
Paul Bakker674e0b02014-03-26 13:26:52 +010027ifndef SHARED
Ronald Cronb6d6d4c2020-06-03 10:11:18 +020028MBEDLIBS=../library/libmbedcrypto.a ../library/libmbedx509.a ../library/libmbedtls.a
Paul Bakker674e0b02014-03-26 13:26:52 +010029else
Ronald Cronb6d6d4c2020-06-03 10:11:18 +020030MBEDLIBS=../library/libmbedcrypto.$(DLEXT) ../library/libmbedx509.$(DLEXT) ../library/libmbedtls.$(DLEXT)
Paul Bakker674e0b02014-03-26 13:26:52 +010031endif
32
Paul Bakkerc7ffd362012-04-05 12:08:29 +000033ifdef DEBUG
Alon Bar-Levf7a9f302015-02-18 17:55:05 +020034LOCAL_CFLAGS += -g3
Paul Bakkerc7ffd362012-04-05 12:08:29 +000035endif
36
Gilles Peskine51681552019-05-20 19:35:37 +020037ifdef RECORD_PSA_STATUS_COVERAGE_LOG
38LOCAL_CFLAGS += -Werror -DRECORD_PSA_STATUS_COVERAGE_LOG
39endif
40
Manuel Pégourié-Gonnard5c59a4f2015-06-24 13:06:24 +020041# if we're running on Windows, build for Windows
Paul Bakkercd5b5292012-05-10 20:49:10 +000042ifdef WINDOWS
Alon Bar-Lev18ba0cc2015-02-14 01:04:58 +020043WINDOWS_BUILD=1
44endif
45
46ifdef WINDOWS_BUILD
47DLEXT=dll
48EXEXT=.exe
Alon Bar-Levada41052015-02-18 17:47:52 +020049LOCAL_LDFLAGS += -lws2_32
Alon Bar-Lev18ba0cc2015-02-14 01:04:58 +020050ifdef SHARED
51SHARED_SUFFIX=.$(DLEXT)
52endif
Manuel Pégourié-Gonnard8d4a6132015-06-24 12:16:20 +020053else
Andres Amaya Garcia420f0cc2018-03-27 19:17:21 +010054DLEXT ?= so
Manuel Pégourié-Gonnard8d4a6132015-06-24 12:16:20 +020055EXEXT=
56SHARED_SUFFIX=
Gilles Peskine8b427c82021-07-13 18:14:25 +020057endif
58
59ifdef WINDOWS
60PYTHON ?= python
61else
Gilles Peskineb61a6142021-04-22 00:21:58 +020062PYTHON ?= $(shell if type python3 >/dev/null 2>/dev/null; then echo python3; else echo python; fi)
Paul Bakkercd5b5292012-05-10 20:49:10 +000063endif
64
Gilles Peskine687d1ab2021-04-22 01:01:56 +020065.PHONY: generated_files
66GENERATED_DATA_FILES := $(patsubst tests/%,%,$(shell $(PYTHON) scripts/generate_psa_tests.py --list))
67GENERATED_FILES := $(GENERATED_DATA_FILES)
68generated_files: $(GENERATED_FILES)
69
Gilles Peskine5df77c62021-07-13 17:22:58 +020070# generate_psa_tests.py spends more time analyzing inputs than generating
71# outputs. Its inputs are the same no matter which files are being generated.
72# It's rare not to want all the outputs. So always generate all of its outputs.
73# Use an intermediate phony dependency so that parallel builds don't run
74# a separate instance of the recipe for each output file.
75.SECONDARY: generated_psa_test_data
76$(GENERATED_DATA_FILES): generated_psa_test_data
77generated_psa_test_data: scripts/generate_psa_tests.py
Gilles Peskine1411c7c2021-04-22 14:50:16 +020078## The generated file only depends on the options that are present in
79## crypto_config.h, not on which options are set. To avoid regenerating this
80## file all the time when switching between configurations, don't declare
81## crypto_config.h as a dependency. Remove this file from your working tree
82## if you've just added or removed an option in crypto_config.h.
Gilles Peskine5df77c62021-07-13 17:22:58 +020083#generated_psa_test_data: ../include/psa/crypto_config.h
84generated_psa_test_data: ../include/psa/crypto_values.h
85generated_psa_test_data: ../include/psa/crypto_extra.h
86generated_psa_test_data: suites/test_suite_psa_crypto_metadata.data
87generated_psa_test_data:
88 echo " Gen $(GENERATED_DATA_FILES) ..."
Gilles Peskine687d1ab2021-04-22 01:01:56 +020089 $(PYTHON) scripts/generate_psa_tests.py
90
Azim Khan27a35e72018-06-29 12:39:19 +010091# A test application is built for each suites/test_suite_*.data file.
92# Application name is same as .data file's base name and can be
93# constructed by stripping path 'suites/' and extension .data.
Gilles Peskine687d1ab2021-04-22 01:01:56 +020094DATA_FILES := $(wildcard suites/test_suite_*.data)
95# Make sure that generated data files are included even if they don't
96# exist yet when the makefile is parsed.
97DATA_FILES += $(filter-out $(DATA_FILES),$(GENERATED_DATA_FILES))
98APPS = $(basename $(subst suites/,,$(DATA_FILES)))
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +000099
Azim Khan27a35e72018-06-29 12:39:19 +0100100# Construct executable name by adding OS specific suffix $(EXEXT).
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +0000101BINARIES := $(addsuffix $(EXEXT),$(APPS))
102
Paul Bakker0049c2f2009-07-11 19:15:43 +0000103.SILENT:
104
Manuel Pégourié-Gonnard5c59a4f2015-06-24 13:06:24 +0200105.PHONY: all check test clean
106
Azim Khan1de892b2017-06-09 15:02:36 +0100107all: $(BINARIES)
108
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200109$(MBEDLIBS):
Manuel Pégourié-Gonnardfc367082015-06-26 16:50:24 +0200110 $(MAKE) -C ../library
Manuel Pégourié-Gonnard5c59a4f2015-06-24 13:06:24 +0200111
Steven Cooremana70d5882020-07-16 20:26:18 +0200112MBEDTLS_TEST_OBJS=$(patsubst %.c,%.o,$(wildcard src/*.c src/drivers/*.c))
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200113
Ronald Cronddaf99c2020-06-19 11:27:26 +0200114mbedtls_test: $(MBEDTLS_TEST_OBJS)
115
Gilles Peskinee1d51bd2021-01-20 19:47:23 +0100116TEST_OBJS_DEPS = $(wildcard include/test/*.h include/test/*/*.h)
Gilles Peskined71539f2020-11-25 18:17:17 +0100117ifdef RECORD_PSA_STATUS_COVERAGE_LOG
Gilles Peskine75829a42021-01-25 13:46:14 +0100118# Explicitly depend on this header because on a clean copy of the source tree,
119# it doesn't exist yet and must be generated as part of the build, and
120# therefore the wildcard enumeration above doesn't include it.
Gilles Peskined71539f2020-11-25 18:17:17 +0100121TEST_OBJS_DEPS += include/test/instrument_record_status.h
122endif
123
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200124# Rule to compile common test C files in src folder
Gilles Peskined71539f2020-11-25 18:17:17 +0100125src/%.o : src/%.c $(TEST_OBJS_DEPS)
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200126 echo " CC $<"
127 $(CC) $(LOCAL_CFLAGS) $(CFLAGS) -o $@ -c $<
128
Steven Cooremana70d5882020-07-16 20:26:18 +0200129src/drivers/%.o : src/drivers/%.c
130 echo " CC $<"
131 $(CC) $(LOCAL_CFLAGS) $(CFLAGS) -o $@ -c $<
132
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +0000133C_FILES := $(addsuffix .c,$(APPS))
134
Azim Khan27a35e72018-06-29 12:39:19 +0100135# Wildcard target for test code generation:
136# A .c file is generated for each .data file in the suites/ directory. Each .c
137# file depends on a .data and .function file from suites/ directory. Following
138# nameing convention is followed:
139#
140# C file | Depends on
141#-----------------------------------------------------------------------------
142# foo.c | suites/foo.function suites/foo.data
143# foo.bar.c | suites/foo.function suites/foo.bar.data
144#
145# Note above that .c and .data files have same base name.
146# However, corresponding .function file's base name is the word before first
147# dot in .c file's base name.
148#
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +0000149.SECONDEXPANSION:
Azim Khan27a35e72018-06-29 12:39:19 +0100150%.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 +0100151 echo " Gen $@"
Azim Khan27a35e72018-06-29 12:39:19 +0100152 $(PYTHON) scripts/generate_test_code.py -f suites/$(firstword $(subst ., ,$*)).function \
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100153 -d suites/$*.data \
Azim Khan1de892b2017-06-09 15:02:36 +0100154 -t suites/main_test.function \
Mohammad Azim Khan95402612017-07-19 10:15:54 +0100155 -p suites/host_test.function \
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100156 -s suites \
Azim Khane3b26af2018-06-29 02:36:57 +0100157 --helpers-file suites/helpers.function \
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100158 -o .
Paul Bakker286bf3c2013-04-08 18:09:51 +0200159
Paul Bakker286bf3c2013-04-08 18:09:51 +0200160
Gilles Peskined71539f2020-11-25 18:17:17 +0100161$(BINARIES): %$(EXEXT): %.c $(MBEDLIBS) $(TEST_OBJS_DEPS) $(MBEDTLS_TEST_OBJS)
Manuel Pégourié-Gonnard78ec2b02015-07-08 22:12:06 +0100162 echo " CC $<"
Ronald Cronf5ea29a2020-06-19 10:42:29 +0200163 $(CC) $(LOCAL_CFLAGS) $(CFLAGS) $< $(MBEDTLS_TEST_OBJS) $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
Paul Bakker286bf3c2013-04-08 18:09:51 +0200164
Paul Bakker0049c2f2009-07-11 19:15:43 +0000165clean:
Paul Bakker62f88dc2012-05-10 21:26:28 +0000166ifndef WINDOWS
Gilles Peskine15965542018-09-26 13:42:26 +0200167 rm -rf $(BINARIES) *.c *.datax TESTS
Steven Cooremana70d5882020-07-16 20:26:18 +0200168 rm -f src/*.o src/drivers/*.o src/libmbed*
Gilles Peskined71539f2020-11-25 18:17:17 +0100169 rm -f include/test/instrument_record_status.h
Manuel Pégourié-Gonnard5c59a4f2015-06-24 13:06:24 +0200170else
Darryl Green6c0f94c2018-10-17 16:12:33 +0100171 if exist *.c del /Q /F *.c
172 if exist *.exe del /Q /F *.exe
173 if exist *.datax del /Q /F *.datax
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200174 if exist src/*.o del /Q /F src/*.o
Steven Cooremana70d5882020-07-16 20:26:18 +0200175 if exist src/drivers/*.o del /Q /F src/drivers/*.o
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200176 if exist src/libmbed* del /Q /F src/libmed*
Gilles Peskined71539f2020-11-25 18:17:17 +0100177 if exist include/test/instrument_record_status.h del /Q /F include/test/instrument_record_status.h
Azim Khan27a35e72018-06-29 12:39:19 +0100178ifneq ($(wildcard TESTS/.*),)
Mohammad Azim Khan1f29be72017-03-20 22:21:22 +0000179 rmdir /Q /S TESTS
Paul Bakker62f88dc2012-05-10 21:26:28 +0000180endif
Azim Khan27a35e72018-06-29 12:39:19 +0100181endif
Paul Bakker0049c2f2009-07-11 19:15:43 +0000182
Gilles Peskine687d1ab2021-04-22 01:01:56 +0200183neat: clean
184ifndef WINDOWS
185 rm -f $(GENERATED_FILES)
186else
187 for %f in ($(subst /,\,$(GENERATED_FILES))) if exist %f del /Q /F %f
188endif
189
Gilles Peskineac372cc2018-11-29 10:15:06 +0000190# Test suites caught by SKIP_TEST_SUITES are built but not executed.
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +0000191check: $(BINARIES)
Gilles Peskineac372cc2018-11-29 10:15:06 +0000192 perl scripts/run-test-suites.pl --skip=$(SKIP_TEST_SUITES)
Manuel Pégourié-Gonnard5c59a4f2015-06-24 13:06:24 +0200193
194test: check
Mohammad Azim Khan1f29be72017-03-20 22:21:22 +0000195
Azim Khan1de892b2017-06-09 15:02:36 +0100196# Create separate targets for generating embedded tests.
197EMBEDDED_TESTS := $(addprefix embedded_,$(APPS))
Mohammad Azim Khan1f29be72017-03-20 22:21:22 +0000198
Mohammad Azim Khan95402612017-07-19 10:15:54 +0100199# Generate test code for target.
Mohammad Azim Khan1f29be72017-03-20 22:21:22 +0000200
201.SECONDEXPANSION:
Azim Khan27a35e72018-06-29 12:39:19 +0100202$(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 +0100203 echo " Gen ./TESTS/mbedtls/$*/$*.c"
Azim Khan27a35e72018-06-29 12:39:19 +0100204 $(PYTHON) scripts/generate_test_code.py -f suites/$(firstword $(subst ., ,$*)).function \
Mohammad Azim Khan1f29be72017-03-20 22:21:22 +0000205 -d suites/$*.data \
Azim Khan1de892b2017-06-09 15:02:36 +0100206 -t suites/main_test.function \
Mohammad Azim Khan95402612017-07-19 10:15:54 +0100207 -p suites/target_test.function \
Mohammad Azim Khan1f29be72017-03-20 22:21:22 +0000208 -s suites \
Azim Khane3b26af2018-06-29 02:36:57 +0100209 --helpers-file suites/helpers.function \
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100210 -o ./TESTS/mbedtls/$*
Mohammad Azim Khan1f29be72017-03-20 22:21:22 +0000211
Mohammad Azim Khanff560f22018-06-28 11:43:17 +0100212generate-target-tests: $(EMBEDDED_TESTS)
Mohammad Azim Khan1f29be72017-03-20 22:21:22 +0000213
Gilles Peskine1d102572019-06-20 17:23:58 +0200214define copy_header_to_target
Ronald Cron02c78b72020-05-27 09:22:32 +0200215TESTS/mbedtls/$(1)/$(2): include/test/$(2)
Gilles Peskine1d102572019-06-20 17:23:58 +0200216 echo " Copy ./$$@"
217ifndef WINDOWS
218 mkdir -p $$(@D)
219 cp $$< $$@
220else
221 mkdir $$(@D)
222 copy $$< $$@
223endif
224
225endef
Ronald Cron02c78b72020-05-27 09:22:32 +0200226$(foreach app, $(APPS), $(foreach file, $(notdir $(wildcard include/test/*.h)), \
Gilles Peskine1d102572019-06-20 17:23:58 +0200227 $(eval $(call copy_header_to_target,$(app),$(file)))))
Gilles Peskinee1d51bd2021-01-20 19:47:23 +0100228$(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 +0200229
230ifdef RECORD_PSA_STATUS_COVERAGE_LOG
Ronald Cron02c78b72020-05-27 09:22:32 +0200231include/test/instrument_record_status.h: ../include/psa/crypto.h Makefile
Gilles Peskined71539f2020-11-25 18:17:17 +0100232 echo " Gen $@"
Gilles Peskine51681552019-05-20 19:35:37 +0200233 sed <../include/psa/crypto.h >$@ -n 's/^psa_status_t \([A-Za-z0-9_]*\)(.*/#define \1(...) RECORD_STATUS("\1", \1(__VA_ARGS__))/p'
234endif