blob: ffa4812bdc76151a98d259c263c4ef5bd17e95d9 [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
Gilles Peskine1e657712019-07-02 20:05:20 +02006WARNING_CFLAGS ?= -Wall -Wextra
Alon Bar-Levada41052015-02-18 17:47:52 +02007LDFLAGS ?=
Paul Bakker0049c2f2009-07-11 19:15:43 +00008
Gilles Peskine76dd3aa2020-07-02 15:58:37 +02009# 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.
Ronald Cron02c78b72020-05-27 09:22:32 +020012LOCAL_CFLAGS = $(WARNING_CFLAGS) -I./include -I../include -I../library -D_FILE_OFFSET_BITS=64
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +000013LOCAL_LDFLAGS = -L../library \
Gilles Peskine4fa9f9f2020-02-26 19:05:31 +010014 -lmbedtls$(SHARED_SUFFIX) \
15 -lmbedx509$(SHARED_SUFFIX) \
Manuel Pégourié-Gonnard21e1ac22015-06-25 08:45:12 +020016 -lmbedcrypto$(SHARED_SUFFIX)
Paul Bakker0049c2f2009-07-11 19:15:43 +000017
Christoph M. Wintersteiger6ea2dea12019-01-21 17:26:19 +000018include ../3rdparty/Makefile.inc
19LOCAL_CFLAGS+=$(THIRDPARTY_INCLUDES)
Christoph M. Wintersteigerd5fd7662018-10-25 12:47:03 +010020
Nicholas Wilson61fa4362018-06-25 12:10:00 +010021# 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
Paul Bakker674e0b02014-03-26 13:26:52 +010026ifndef SHARED
Ronald Cronb6d6d4c2020-06-03 10:11:18 +020027MBEDLIBS=../library/libmbedcrypto.a ../library/libmbedx509.a ../library/libmbedtls.a
Paul Bakker674e0b02014-03-26 13:26:52 +010028else
Ronald Cronb6d6d4c2020-06-03 10:11:18 +020029MBEDLIBS=../library/libmbedcrypto.$(DLEXT) ../library/libmbedx509.$(DLEXT) ../library/libmbedtls.$(DLEXT)
Paul Bakker674e0b02014-03-26 13:26:52 +010030endif
31
Paul Bakkerc7ffd362012-04-05 12:08:29 +000032ifdef DEBUG
Alon Bar-Levf7a9f302015-02-18 17:55:05 +020033LOCAL_CFLAGS += -g3
Paul Bakkerc7ffd362012-04-05 12:08:29 +000034endif
35
Gilles Peskine51681552019-05-20 19:35:37 +020036ifdef RECORD_PSA_STATUS_COVERAGE_LOG
37LOCAL_CFLAGS += -Werror -DRECORD_PSA_STATUS_COVERAGE_LOG
38endif
39
Manuel Pégourié-Gonnard5c59a4f2015-06-24 13:06:24 +020040# if we're running on Windows, build for Windows
Paul Bakkercd5b5292012-05-10 20:49:10 +000041ifdef WINDOWS
Alon Bar-Lev18ba0cc2015-02-14 01:04:58 +020042WINDOWS_BUILD=1
43endif
44
45ifdef WINDOWS_BUILD
46DLEXT=dll
47EXEXT=.exe
Alon Bar-Levada41052015-02-18 17:47:52 +020048LOCAL_LDFLAGS += -lws2_32
Alon Bar-Lev18ba0cc2015-02-14 01:04:58 +020049ifdef SHARED
50SHARED_SUFFIX=.$(DLEXT)
51endif
Mohammad Azim Khan8a3628f2018-06-26 17:30:16 +010052PYTHON ?= python
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=
Mohammad Azim Khan8a3628f2018-06-26 17:30:16 +010057# python2 for POSIX since FreeBSD has only python2 as default.
58PYTHON ?= python2
Paul Bakkercd5b5292012-05-10 20:49:10 +000059endif
60
Gilles Peskine5bb8bec2020-02-26 19:11:43 +010061# Zlib shared library extensions:
62ifdef ZLIB
63LOCAL_LDFLAGS += -lz
64endif
65
Azim Khan27a35e72018-06-29 12:39:19 +010066# 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)))
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +000070
Azim Khan27a35e72018-06-29 12:39:19 +010071# Construct executable name by adding OS specific suffix $(EXEXT).
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +000072BINARIES := $(addsuffix $(EXEXT),$(APPS))
73
Paul Bakker0049c2f2009-07-11 19:15:43 +000074.SILENT:
75
Manuel Pégourié-Gonnard5c59a4f2015-06-24 13:06:24 +020076.PHONY: all check test clean
77
Azim Khan1de892b2017-06-09 15:02:36 +010078all: $(BINARIES)
79
Ronald Cronb6d6d4c2020-06-03 10:11:18 +020080$(MBEDLIBS):
Manuel Pégourié-Gonnardfc367082015-06-26 16:50:24 +020081 $(MAKE) -C ../library
Manuel Pégourié-Gonnard5c59a4f2015-06-24 13:06:24 +020082
Ronald Cronf5ea29a2020-06-19 10:42:29 +020083MBEDTLS_TEST_OBJS=$(patsubst %.c,%.o,$(wildcard src/*.c))
Ronald Cronb6d6d4c2020-06-03 10:11:18 +020084
Ronald Cronddaf99c2020-06-19 11:27:26 +020085mbedtls_test: $(MBEDTLS_TEST_OBJS)
86
Ronald Cronb6d6d4c2020-06-03 10:11:18 +020087# 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
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +000092C_FILES := $(addsuffix .c,$(APPS))
93
Azim Khan27a35e72018-06-29 12:39:19 +010094# Wildcard target for test code generation:
95# A .c file is generated for each .data file in the suites/ directory. Each .c
96# file depends on a .data and .function file from suites/ directory. Following
97# nameing convention is followed:
98#
99# C file | Depends on
100#-----------------------------------------------------------------------------
101# foo.c | suites/foo.function suites/foo.data
102# foo.bar.c | suites/foo.function suites/foo.bar.data
103#
104# Note above that .c and .data files have same base name.
105# However, corresponding .function file's base name is the word before first
106# dot in .c file's base name.
107#
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +0000108.SECONDEXPANSION:
Azim Khan27a35e72018-06-29 12:39:19 +0100109%.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 +0100110 echo " Gen $@"
Azim Khan27a35e72018-06-29 12:39:19 +0100111 $(PYTHON) scripts/generate_test_code.py -f suites/$(firstword $(subst ., ,$*)).function \
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100112 -d suites/$*.data \
Azim Khan1de892b2017-06-09 15:02:36 +0100113 -t suites/main_test.function \
Mohammad Azim Khan95402612017-07-19 10:15:54 +0100114 -p suites/host_test.function \
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100115 -s suites \
Azim Khane3b26af2018-06-29 02:36:57 +0100116 --helpers-file suites/helpers.function \
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100117 -o .
Paul Bakker286bf3c2013-04-08 18:09:51 +0200118
Paul Bakker286bf3c2013-04-08 18:09:51 +0200119
Ronald Cronf5ea29a2020-06-19 10:42:29 +0200120$(BINARIES): %$(EXEXT): %.c $(MBEDLIBS) $(MBEDTLS_TEST_OBJS)
Manuel Pégourié-Gonnard78ec2b02015-07-08 22:12:06 +0100121 echo " CC $<"
Ronald Cronf5ea29a2020-06-19 10:42:29 +0200122 $(CC) $(LOCAL_CFLAGS) $(CFLAGS) $< $(MBEDTLS_TEST_OBJS) $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
Paul Bakker286bf3c2013-04-08 18:09:51 +0200123
Gilles Peskine1d102572019-06-20 17:23:58 +0200124# Some test suites require additional header files.
Ronald Cron02c78b72020-05-27 09:22:32 +0200125$(filter test_suite_psa_crypto%, $(BINARIES)): include/test/psa_crypto_helpers.h
Gilles Peskine1d102572019-06-20 17:23:58 +0200126$(addprefix embedded_,$(filter test_suite_psa_crypto%, $(APPS))): embedded_%: TESTS/mbedtls/%/psa_crypto_helpers.h
Ronald Cron02c78b72020-05-27 09:22:32 +0200127$(filter test_suite_psa_%, $(BINARIES)): include/test/psa_helpers.h
Gilles Peskine1d102572019-06-20 17:23:58 +0200128$(addprefix embedded_,$(filter test_suite_psa_%, $(APPS))): embedded_%: TESTS/mbedtls/%/psa_helpers.h
Paul Bakker286bf3c2013-04-08 18:09:51 +0200129
Paul Bakker0049c2f2009-07-11 19:15:43 +0000130clean:
Paul Bakker62f88dc2012-05-10 21:26:28 +0000131ifndef WINDOWS
Gilles Peskine15965542018-09-26 13:42:26 +0200132 rm -rf $(BINARIES) *.c *.datax TESTS
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200133 rm -f src/*.o src/libmbed*
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
139 if exist src/libmbed* del /Q /F src/libmed*
Azim Khan27a35e72018-06-29 12:39:19 +0100140ifneq ($(wildcard TESTS/.*),)
Mohammad Azim Khan1f29be72017-03-20 22:21:22 +0000141 rmdir /Q /S TESTS
Paul Bakker62f88dc2012-05-10 21:26:28 +0000142endif
Azim Khan27a35e72018-06-29 12:39:19 +0100143endif
Paul Bakker0049c2f2009-07-11 19:15:43 +0000144
Gilles Peskineac372cc2018-11-29 10:15:06 +0000145# Test suites caught by SKIP_TEST_SUITES are built but not executed.
Mohammad Azim Khan94aefaf2017-03-23 12:32:54 +0000146check: $(BINARIES)
Gilles Peskineac372cc2018-11-29 10:15:06 +0000147 perl scripts/run-test-suites.pl --skip=$(SKIP_TEST_SUITES)
Manuel Pégourié-Gonnard5c59a4f2015-06-24 13:06:24 +0200148
149test: check
Mohammad Azim Khan1f29be72017-03-20 22:21:22 +0000150
Azim Khan1de892b2017-06-09 15:02:36 +0100151# Create separate targets for generating embedded tests.
152EMBEDDED_TESTS := $(addprefix embedded_,$(APPS))
Mohammad Azim Khan1f29be72017-03-20 22:21:22 +0000153
Mohammad Azim Khan95402612017-07-19 10:15:54 +0100154# Generate test code for target.
Mohammad Azim Khan1f29be72017-03-20 22:21:22 +0000155
156.SECONDEXPANSION:
Azim Khan27a35e72018-06-29 12:39:19 +0100157$(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 +0100158 echo " Gen ./TESTS/mbedtls/$*/$*.c"
Azim Khan27a35e72018-06-29 12:39:19 +0100159 $(PYTHON) scripts/generate_test_code.py -f suites/$(firstword $(subst ., ,$*)).function \
Mohammad Azim Khan1f29be72017-03-20 22:21:22 +0000160 -d suites/$*.data \
Azim Khan1de892b2017-06-09 15:02:36 +0100161 -t suites/main_test.function \
Mohammad Azim Khan95402612017-07-19 10:15:54 +0100162 -p suites/target_test.function \
Mohammad Azim Khan1f29be72017-03-20 22:21:22 +0000163 -s suites \
Azim Khane3b26af2018-06-29 02:36:57 +0100164 --helpers-file suites/helpers.function \
Mohammad Azim Khanfff49042017-03-28 01:48:31 +0100165 -o ./TESTS/mbedtls/$*
Mohammad Azim Khan1f29be72017-03-20 22:21:22 +0000166
Mohammad Azim Khanff560f22018-06-28 11:43:17 +0100167generate-target-tests: $(EMBEDDED_TESTS)
Mohammad Azim Khan1f29be72017-03-20 22:21:22 +0000168
Gilles Peskine1d102572019-06-20 17:23:58 +0200169define copy_header_to_target
Ronald Cron02c78b72020-05-27 09:22:32 +0200170TESTS/mbedtls/$(1)/$(2): include/test/$(2)
Gilles Peskine1d102572019-06-20 17:23:58 +0200171 echo " Copy ./$$@"
172ifndef WINDOWS
173 mkdir -p $$(@D)
174 cp $$< $$@
175else
176 mkdir $$(@D)
177 copy $$< $$@
178endif
179
180endef
Ronald Cron02c78b72020-05-27 09:22:32 +0200181$(foreach app, $(APPS), $(foreach file, $(notdir $(wildcard include/test/*.h)), \
Gilles Peskine1d102572019-06-20 17:23:58 +0200182 $(eval $(call copy_header_to_target,$(app),$(file)))))
Gilles Peskine51681552019-05-20 19:35:37 +0200183
184ifdef RECORD_PSA_STATUS_COVERAGE_LOG
Ronald Cron02c78b72020-05-27 09:22:32 +0200185$(BINARIES): include/test/instrument_record_status.h
186include/test/instrument_record_status.h: ../include/psa/crypto.h Makefile
Gilles Peskine51681552019-05-20 19:35:37 +0200187 sed <../include/psa/crypto.h >$@ -n 's/^psa_status_t \([A-Za-z0-9_]*\)(.*/#define \1(...) RECORD_STATUS("\1", \1(__VA_ARGS__))/p'
188endif