1 2# To compile on SunOS: add "-lsocket -lnsl" to LDFLAGS 3# To compile with PKCS11: add "-lpkcs11-helper" to LDFLAGS 4 5CFLAGS ?= -O2 6WARNING_CFLAGS ?= -Wall -Wextra -Wformat=2 -Wno-format-nonliteral 7LDFLAGS ?= 8 9# 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 12# 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. 15LOCAL_CFLAGS = $(WARNING_CFLAGS) -I./include -I../include -I../library -D_FILE_OFFSET_BITS=64 16LOCAL_LDFLAGS = -L../library \ 17 -lmbedtls$(SHARED_SUFFIX) \ 18 -lmbedx509$(SHARED_SUFFIX) \ 19 -lmbedcrypto$(SHARED_SUFFIX) 20 21include ../3rdparty/Makefile.inc 22LOCAL_CFLAGS+=$(THIRDPARTY_INCLUDES) 23 24# 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 29ifndef SHARED 30MBEDLIBS=../library/libmbedcrypto.a ../library/libmbedx509.a ../library/libmbedtls.a 31else 32MBEDLIBS=../library/libmbedcrypto.$(DLEXT) ../library/libmbedx509.$(DLEXT) ../library/libmbedtls.$(DLEXT) 33endif 34 35ifdef DEBUG 36LOCAL_CFLAGS += -g3 37endif 38 39ifdef RECORD_PSA_STATUS_COVERAGE_LOG 40LOCAL_CFLAGS += -Werror -DRECORD_PSA_STATUS_COVERAGE_LOG 41endif 42 43# if we're running on Windows, build for Windows 44ifdef WINDOWS 45WINDOWS_BUILD=1 46endif 47 48ifdef WINDOWS_BUILD 49DLEXT=dll 50EXEXT=.exe 51LOCAL_LDFLAGS += -lws2_32 52ifdef SHARED 53SHARED_SUFFIX=.$(DLEXT) 54endif 55else 56DLEXT ?= so 57EXEXT= 58SHARED_SUFFIX= 59endif 60 61ifdef WINDOWS 62PYTHON ?= python 63else 64PYTHON ?= $(shell if type python3 >/dev/null 2>/dev/null; then echo python3; else echo python; fi) 65endif 66 67# Zlib shared library extensions: 68ifdef ZLIB 69LOCAL_LDFLAGS += -lz 70endif 71 72# 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))) 76 77# Construct executable name by adding OS specific suffix $(EXEXT). 78BINARIES := $(addsuffix $(EXEXT),$(APPS)) 79 80.SILENT: 81 82.PHONY: all check test clean 83 84all: $(BINARIES) 85 86$(MBEDLIBS): 87 $(MAKE) -C ../library 88 89MBEDTLS_TEST_OBJS=$(patsubst %.c,%.o,$(wildcard src/*.c src/drivers/*.c src/test_helpers/*.c)) 90 91mbedtls_test: $(MBEDTLS_TEST_OBJS) 92 93TEST_OBJS_DEPS = $(wildcard include/test/*.h include/test/*/*.h) 94ifdef RECORD_PSA_STATUS_COVERAGE_LOG 95# 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. 98TEST_OBJS_DEPS += include/test/instrument_record_status.h 99endif 100 101# Rule to compile common test C files in src folder 102src/%.o : src/%.c $(TEST_OBJS_DEPS) 103 echo " CC $<" 104 $(CC) $(LOCAL_CFLAGS) $(CFLAGS) -o $@ -c $< 105 106src/drivers/%.o : src/drivers/%.c 107 echo " CC $<" 108 $(CC) $(LOCAL_CFLAGS) $(CFLAGS) -o $@ -c $< 109 110src/test_helpers/%.o : src/test_helpers/%.c 111 echo " CC $<" 112 $(CC) $(LOCAL_CFLAGS) $(CFLAGS) -o $@ -c $< 113 114C_FILES := $(addsuffix .c,$(APPS)) 115c: $(C_FILES) 116 117# Wildcard target for test code generation: 118# A .c file is generated for each .data file in the suites/ directory. Each .c 119# file depends on a .data and .function file from suites/ directory. Following 120# nameing convention is followed: 121# 122# C file | Depends on 123#----------------------------------------------------------------------------- 124# foo.c | suites/foo.function suites/foo.data 125# foo.bar.c | suites/foo.function suites/foo.bar.data 126# 127# Note above that .c and .data files have same base name. 128# However, corresponding .function file's base name is the word before first 129# dot in .c file's base name. 130# 131.SECONDEXPANSION: 132%.c: suites/$$(firstword $$(subst ., ,$$*)).function suites/%.data scripts/generate_test_code.py suites/helpers.function suites/main_test.function suites/host_test.function 133 echo " Gen $@" 134 $(PYTHON) scripts/generate_test_code.py -f suites/$(firstword $(subst ., ,$*)).function \ 135 -d suites/$*.data \ 136 -t suites/main_test.function \ 137 -p suites/host_test.function \ 138 -s suites \ 139 --helpers-file suites/helpers.function \ 140 -o . 141 142 143$(BINARIES): %$(EXEXT): %.c $(MBEDLIBS) $(TEST_OBJS_DEPS) $(MBEDTLS_TEST_OBJS) 144 echo " CC $<" 145 $(CC) $(LOCAL_CFLAGS) $(CFLAGS) $< $(MBEDTLS_TEST_OBJS) $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@ 146 147clean: 148ifndef WINDOWS 149 rm -rf $(BINARIES) *.c *.datax 150 rm -f src/*.o src/drivers/*.o src/test_helpers/*.o src/libmbed* 151 rm -f include/test/instrument_record_status.h 152 rm -rf libtestdriver1 153else 154 if exist *.c del /Q /F *.c 155 if exist *.exe del /Q /F *.exe 156 if exist *.datax del /Q /F *.datax 157 if exist src/*.o del /Q /F src/*.o 158 if exist src/drivers/*.o del /Q /F src/drivers/*.o 159 if exist src/test_helpers/*.o del /Q /F src/test_helpers/*.o 160 if exist src/libmbed* del /Q /F src/libmed* 161 if exist include/test/instrument_record_status.h del /Q /F include/test/instrument_record_status.h 162endif 163 164# Test suites caught by SKIP_TEST_SUITES are built but not executed. 165check: $(BINARIES) 166 perl scripts/run-test-suites.pl $(TEST_FLAGS) --skip=$(SKIP_TEST_SUITES) 167 168test: check 169 170# Generate test library 171 172# Perl code that is executed to transform each original line from a library 173# source file into the corresponding line in the test driver copy of the 174# library. Add a LIBTESTDRIVER1_/libtestdriver1_ to mbedtls_xxx and psa_xxx 175# symbols. 176define libtestdriver1_rewrite := 177 s!^(\s*#\s*include\s*[\"<])(mbedtls|psa)/!$${1}libtestdriver1/include/$${2}/!; \ 178 next if /^\s*#\s*include/; \ 179 s/\b(?=MBEDTLS_|PSA_)/LIBTESTDRIVER1_/g; \ 180 s/\b(?=mbedtls_|psa_)/libtestdriver1_/g; 181endef 182 183libtestdriver1.a: 184 # Copy the library and fake a 3rdparty Makefile include. 185 rm -Rf ./libtestdriver1 186 mkdir ./libtestdriver1 187 cp -Rf ../library ./libtestdriver1 188 cp -Rf ../include ./libtestdriver1 189 mkdir ./libtestdriver1/3rdparty 190 touch ./libtestdriver1/3rdparty/Makefile.inc 191 192 # Set the test driver base (minimal) configuration. 193 cp ./include/test/drivers/config_test_driver.h ./libtestdriver1/include/mbedtls/config.h 194 195 # Set the PSA cryptography configuration for the test library. 196 # It is set from the copied include/psa/crypto_config.h of the Mbed TLS 197 # library the test library is intended to be linked with extended by 198 # ./include/test/drivers/crypto_config_test_driver_extension.h to 199 # mirror the PSA_ACCEL_* macros. 200 mv ./libtestdriver1/include/psa/crypto_config.h ./libtestdriver1/include/psa/crypto_config.h.bak 201 head -n -1 ./libtestdriver1/include/psa/crypto_config.h.bak > ./libtestdriver1/include/psa/crypto_config.h 202 cat ./include/test/drivers/crypto_config_test_driver_extension.h >> ./libtestdriver1/include/psa/crypto_config.h 203 echo "#endif /* PSA_CRYPTO_CONFIG_H */" >> ./libtestdriver1/include/psa/crypto_config.h 204 205 # Prefix MBEDTLS_* PSA_* symbols with LIBTESTDRIVER1_ as well as 206 # mbedtls_* psa_* symbols with libtestdriver1_ to avoid symbol clash 207 # when this test driver library is linked with the Mbed TLS library. 208 perl -pi -e '$(libtestdriver1_rewrite)' ./libtestdriver1/library/*.[ch] 209 perl -pi -e '$(libtestdriver1_rewrite)' ./libtestdriver1/include/*/*.h 210 211 $(MAKE) -C ./libtestdriver1/library CFLAGS="-I../../ $(CFLAGS)" LDFLAGS="$(LDFLAGS)" libmbedcrypto.a 212 cp ./libtestdriver1/library/libmbedcrypto.a ../library/libtestdriver1.a 213 214ifdef RECORD_PSA_STATUS_COVERAGE_LOG 215include/test/instrument_record_status.h: ../include/psa/crypto.h Makefile 216 echo " Gen $@" 217 sed <../include/psa/crypto.h >$@ -n 's/^psa_status_t \([A-Za-z0-9_]*\)(.*/#define \1(...) RECORD_STATUS("\1", \1(__VA_ARGS__))/p' 218endif 219