1 2# To compile on SunOS: add "-lsocket -lnsl" to LDFLAGS 3 4CFLAGS ?= -O2 5WARNING_CFLAGS ?= -Wall -Wextra -Wformat=2 -Wno-format-nonliteral 6LDFLAGS ?= 7 8# Set this to -v to see the details of failing test cases 9TEST_FLAGS ?= $(if $(filter-out 0 OFF Off off NO No no FALSE False false N n,$(CTEST_OUTPUT_ON_FAILURE)),-v,) 10 11default: all 12 13# Include public header files from ../include, test-specific header files 14# from ./include, and private header files (used by some invasive tests) 15# from ../library. 16LOCAL_CFLAGS = $(WARNING_CFLAGS) -I./include -I../include -I../library -D_FILE_OFFSET_BITS=64 17LOCAL_LDFLAGS = -L../library \ 18 -lmbedtls$(SHARED_SUFFIX) \ 19 -lmbedx509$(SHARED_SUFFIX) \ 20 -lmbedcrypto$(SHARED_SUFFIX) 21 22include ../3rdparty/Makefile.inc 23LOCAL_CFLAGS+=$(THIRDPARTY_INCLUDES) 24 25# Enable definition of various functions used throughout the testsuite 26# (gethostname, strdup, fileno...) even when compiling with -std=c99. Harmless 27# on non-POSIX platforms. 28LOCAL_CFLAGS += -D_POSIX_C_SOURCE=200809L 29 30ifndef SHARED 31MBEDLIBS=../library/libmbedcrypto.a ../library/libmbedx509.a ../library/libmbedtls.a 32else 33MBEDLIBS=../library/libmbedcrypto.$(DLEXT) ../library/libmbedx509.$(DLEXT) ../library/libmbedtls.$(DLEXT) 34endif 35 36ifdef DEBUG 37LOCAL_CFLAGS += -g3 38endif 39 40ifdef RECORD_PSA_STATUS_COVERAGE_LOG 41LOCAL_CFLAGS += -Werror -DRECORD_PSA_STATUS_COVERAGE_LOG 42endif 43 44# if we're running on Windows, build for Windows 45ifdef WINDOWS 46WINDOWS_BUILD=1 47endif 48 49ifdef WINDOWS_BUILD 50DLEXT=dll 51EXEXT=.exe 52LOCAL_LDFLAGS += -lws2_32 53ifdef SHARED 54SHARED_SUFFIX=.$(DLEXT) 55endif 56else 57DLEXT ?= so 58EXEXT= 59SHARED_SUFFIX= 60endif 61 62ifdef WINDOWS 63PYTHON ?= python 64else 65PYTHON ?= $(shell if type python3 >/dev/null 2>/dev/null; then echo python3; else echo python; fi) 66endif 67 68.PHONY: generated_files 69GENERATED_DATA_FILES := $(patsubst tests/%,%,$(shell \ 70 $(PYTHON) scripts/generate_psa_tests.py --list || \ 71 echo FAILED \ 72)) 73ifeq ($(GENERATED_DATA_FILES),FAILED) 74$(error "$(PYTHON) scripts/generate_psa_tests.py --list" failed) 75endif 76GENERATED_FILES := $(GENERATED_DATA_FILES) 77generated_files: $(GENERATED_FILES) 78 79# generate_psa_tests.py spends more time analyzing inputs than generating 80# outputs. Its inputs are the same no matter which files are being generated. 81# It's rare not to want all the outputs. So always generate all of its outputs. 82# Use an intermediate phony dependency so that parallel builds don't run 83# a separate instance of the recipe for each output file. 84.SECONDARY: generated_psa_test_data 85$(GENERATED_DATA_FILES): generated_psa_test_data 86generated_psa_test_data: scripts/generate_psa_tests.py 87## The generated file only depends on the options that are present in 88## crypto_config.h, not on which options are set. To avoid regenerating this 89## file all the time when switching between configurations, don't declare 90## crypto_config.h as a dependency. Remove this file from your working tree 91## if you've just added or removed an option in crypto_config.h. 92#generated_psa_test_data: ../include/psa/crypto_config.h 93generated_psa_test_data: ../include/psa/crypto_values.h 94generated_psa_test_data: ../include/psa/crypto_extra.h 95generated_psa_test_data: suites/test_suite_psa_crypto_metadata.data 96generated_psa_test_data: 97 echo " Gen $(GENERATED_DATA_FILES) ..." 98 $(PYTHON) scripts/generate_psa_tests.py 99 100# A test application is built for each suites/test_suite_*.data file. 101# Application name is same as .data file's base name and can be 102# constructed by stripping path 'suites/' and extension .data. 103DATA_FILES := $(wildcard suites/test_suite_*.data) 104# Make sure that generated data files are included even if they don't 105# exist yet when the makefile is parsed. 106DATA_FILES += $(filter-out $(DATA_FILES),$(GENERATED_DATA_FILES)) 107APPS = $(basename $(subst suites/,,$(DATA_FILES))) 108 109# Construct executable name by adding OS specific suffix $(EXEXT). 110BINARIES := $(addsuffix $(EXEXT),$(APPS)) 111 112.SILENT: 113 114.PHONY: all check test clean 115 116all: $(BINARIES) 117 118$(MBEDLIBS): 119 $(MAKE) -C ../library 120 121MBEDTLS_TEST_OBJS=$(patsubst %.c,%.o,$(wildcard src/*.c src/drivers/*.c)) 122 123mbedtls_test: $(MBEDTLS_TEST_OBJS) 124 125TEST_OBJS_DEPS = $(wildcard include/test/*.h include/test/*/*.h) 126ifdef RECORD_PSA_STATUS_COVERAGE_LOG 127# Explicitly depend on this header because on a clean copy of the source tree, 128# it doesn't exist yet and must be generated as part of the build, and 129# therefore the wildcard enumeration above doesn't include it. 130TEST_OBJS_DEPS += include/test/instrument_record_status.h 131endif 132 133# Rule to compile common test C files in src folder 134src/%.o : src/%.c $(TEST_OBJS_DEPS) 135 echo " CC $<" 136 $(CC) $(LOCAL_CFLAGS) $(CFLAGS) -o $@ -c $< 137 138src/drivers/%.o : src/drivers/%.c 139 echo " CC $<" 140 $(CC) $(LOCAL_CFLAGS) $(CFLAGS) -o $@ -c $< 141 142C_FILES := $(addsuffix .c,$(APPS)) 143 144# Wildcard target for test code generation: 145# A .c file is generated for each .data file in the suites/ directory. Each .c 146# file depends on a .data and .function file from suites/ directory. Following 147# nameing convention is followed: 148# 149# C file | Depends on 150#----------------------------------------------------------------------------- 151# foo.c | suites/foo.function suites/foo.data 152# foo.bar.c | suites/foo.function suites/foo.bar.data 153# 154# Note above that .c and .data files have same base name. 155# However, corresponding .function file's base name is the word before first 156# dot in .c file's base name. 157# 158.SECONDEXPANSION: 159%.c: suites/$$(firstword $$(subst ., ,$$*)).function suites/%.data scripts/generate_test_code.py suites/helpers.function suites/main_test.function suites/host_test.function 160 echo " Gen $@" 161 $(PYTHON) scripts/generate_test_code.py -f suites/$(firstword $(subst ., ,$*)).function \ 162 -d suites/$*.data \ 163 -t suites/main_test.function \ 164 -p suites/host_test.function \ 165 -s suites \ 166 --helpers-file suites/helpers.function \ 167 -o . 168 169 170$(BINARIES): %$(EXEXT): %.c $(MBEDLIBS) $(TEST_OBJS_DEPS) $(MBEDTLS_TEST_OBJS) 171 echo " CC $<" 172 $(CC) $(LOCAL_CFLAGS) $(CFLAGS) $< $(MBEDTLS_TEST_OBJS) $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@ 173 174clean: 175ifndef WINDOWS 176 rm -rf $(BINARIES) *.c *.datax 177 rm -f src/*.o src/drivers/*.o src/libmbed* 178 rm -f include/test/instrument_record_status.h 179 rm -rf libtestdriver1 180else 181 if exist *.c del /Q /F *.c 182 if exist *.exe del /Q /F *.exe 183 if exist *.datax del /Q /F *.datax 184 if exist src/*.o del /Q /F src/*.o 185 if exist src/drivers/*.o del /Q /F src/drivers/*.o 186 if exist src/libmbed* del /Q /F src/libmed* 187 if exist include/test/instrument_record_status.h del /Q /F include/test/instrument_record_status.h 188endif 189 190neat: clean 191ifndef WINDOWS 192 rm -f $(GENERATED_FILES) 193else 194 for %f in ($(subst /,\,$(GENERATED_FILES))) if exist %f del /Q /F %f 195endif 196 197# Test suites caught by SKIP_TEST_SUITES are built but not executed. 198check: $(BINARIES) 199 perl scripts/run-test-suites.pl $(TEST_FLAGS) --skip=$(SKIP_TEST_SUITES) 200 201test: check 202 203# Generate test library 204 205# Perl code that is executed to transform each original line from a library 206# source file into the corresponding line in the test driver copy of the 207# library. Add a LIBTESTDRIVER1_/libtestdriver1_ to mbedtls_xxx and psa_xxx 208# symbols. 209define libtestdriver1_rewrite := 210 s!^(\s*#\s*include\s*[\"<])(mbedtls|psa)/!$${1}libtestdriver1/include/$${2}/!; \ 211 next if /^\s*#\s*include/; \ 212 s/\b(?=MBEDTLS_|PSA_)/LIBTESTDRIVER1_/g; \ 213 s/\b(?=mbedtls_|psa_)/libtestdriver1_/g; 214endef 215 216libtestdriver1.a: 217 # Copy the library and fake a 3rdparty Makefile include. 218 rm -Rf ./libtestdriver1 219 mkdir ./libtestdriver1 220 cp -Rf ../library ./libtestdriver1 221 cp -Rf ../include ./libtestdriver1 222 cp -Rf ../scripts ./libtestdriver1 223 mkdir ./libtestdriver1/3rdparty 224 touch ./libtestdriver1/3rdparty/Makefile.inc 225 226 # Set the test driver base (minimal) configuration. 227 cp ./include/test/drivers/config_test_driver.h ./libtestdriver1/include/mbedtls/mbedtls_config.h 228 229 # Set the PSA cryptography configuration for the test library. 230 # It is set from the copied include/psa/crypto_config.h of the Mbed TLS 231 # library the test library is intended to be linked with extended by 232 # ./include/test/drivers/crypto_config_test_driver_extension.h to 233 # mirror the PSA_ACCEL_* macros. 234 mv ./libtestdriver1/include/psa/crypto_config.h ./libtestdriver1/include/psa/crypto_config.h.bak 235 head -n -1 ./libtestdriver1/include/psa/crypto_config.h.bak > ./libtestdriver1/include/psa/crypto_config.h 236 cat ./include/test/drivers/crypto_config_test_driver_extension.h >> ./libtestdriver1/include/psa/crypto_config.h 237 echo "#endif /* PSA_CRYPTO_CONFIG_H */" >> ./libtestdriver1/include/psa/crypto_config.h 238 239 # Prefix MBEDTLS_* PSA_* symbols with LIBTESTDRIVER1_ as well as 240 # mbedtls_* psa_* symbols with libtestdriver1_ to avoid symbol clash 241 # when this test driver library is linked with the Mbed TLS library. 242 perl -pi -e '$(libtestdriver1_rewrite)' ./libtestdriver1/library/*.[ch] 243 perl -pi -e '$(libtestdriver1_rewrite)' ./libtestdriver1/include/*/*.h 244 245 $(MAKE) -C ./libtestdriver1/library CFLAGS="-I../../ $(CFLAGS)" LDFLAGS="$(LDFLAGS)" libmbedcrypto.a 246 cp ./libtestdriver1/library/libmbedcrypto.a ../library/libtestdriver1.a 247 248ifdef RECORD_PSA_STATUS_COVERAGE_LOG 249include/test/instrument_record_status.h: ../include/psa/crypto.h Makefile 250 echo " Gen $@" 251 sed <../include/psa/crypto.h >$@ -n 's/^psa_status_t \([A-Za-z0-9_]*\)(.*/#define \1(...) RECORD_STATUS("\1", \1(__VA_ARGS__))/p' 252endif 253