Makefile 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. # To compile on SunOS: add "-lsocket -lnsl" to LDFLAGS
  2. # To compile with PKCS11: add "-lpkcs11-helper" to LDFLAGS
  3. CFLAGS ?= -O2
  4. WARNING_CFLAGS ?= -Wall -Wextra -Wformat=2 -Wno-format-nonliteral
  5. LDFLAGS ?=
  6. # Set this to -v to see the details of failing test cases
  7. TEST_FLAGS ?= $(if $(filter-out 0 OFF Off off NO No no FALSE False false N n,$(CTEST_OUTPUT_ON_FAILURE)),-v,)
  8. # 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.
  11. LOCAL_CFLAGS = $(WARNING_CFLAGS) -I./include -I../include -I../library -D_FILE_OFFSET_BITS=64
  12. LOCAL_LDFLAGS = -L../library \
  13. -lmbedtls$(SHARED_SUFFIX) \
  14. -lmbedx509$(SHARED_SUFFIX) \
  15. -lmbedcrypto$(SHARED_SUFFIX)
  16. include ../3rdparty/Makefile.inc
  17. LOCAL_CFLAGS+=$(THIRDPARTY_INCLUDES)
  18. # Enable definition of various functions used throughout the testsuite
  19. # (gethostname, strdup, fileno...) even when compiling with -std=c99. Harmless
  20. # on non-POSIX platforms.
  21. LOCAL_CFLAGS += -D_POSIX_C_SOURCE=200809L
  22. ifndef SHARED
  23. MBEDLIBS=../library/libmbedcrypto.a ../library/libmbedx509.a ../library/libmbedtls.a
  24. else
  25. MBEDLIBS=../library/libmbedcrypto.$(DLEXT) ../library/libmbedx509.$(DLEXT) ../library/libmbedtls.$(DLEXT)
  26. endif
  27. ifdef DEBUG
  28. LOCAL_CFLAGS += -g3
  29. endif
  30. ifdef RECORD_PSA_STATUS_COVERAGE_LOG
  31. LOCAL_CFLAGS += -Werror -DRECORD_PSA_STATUS_COVERAGE_LOG
  32. endif
  33. # if we're running on Windows, build for Windows
  34. ifdef WINDOWS
  35. WINDOWS_BUILD=1
  36. endif
  37. ifdef WINDOWS_BUILD
  38. DLEXT=dll
  39. EXEXT=.exe
  40. LOCAL_LDFLAGS += -lws2_32
  41. ifdef SHARED
  42. SHARED_SUFFIX=.$(DLEXT)
  43. endif
  44. else
  45. DLEXT ?= so
  46. EXEXT=
  47. SHARED_SUFFIX=
  48. endif
  49. ifdef WINDOWS
  50. PYTHON ?= python
  51. else
  52. PYTHON ?= $(shell if type python3 >/dev/null 2>/dev/null; then echo python3; else echo python; fi)
  53. endif
  54. # Zlib shared library extensions:
  55. ifdef ZLIB
  56. LOCAL_LDFLAGS += -lz
  57. endif
  58. # A test application is built for each suites/test_suite_*.data file.
  59. # Application name is same as .data file's base name and can be
  60. # constructed by stripping path 'suites/' and extension .data.
  61. APPS = $(basename $(subst suites/,,$(wildcard suites/test_suite_*.data)))
  62. # Construct executable name by adding OS specific suffix $(EXEXT).
  63. BINARIES := $(addsuffix $(EXEXT),$(APPS))
  64. .SILENT:
  65. .PHONY: all check test clean
  66. all: $(BINARIES)
  67. $(MBEDLIBS):
  68. $(MAKE) -C ../library
  69. MBEDTLS_TEST_OBJS=$(patsubst %.c,%.o,$(wildcard src/*.c src/drivers/*.c))
  70. mbedtls_test: $(MBEDTLS_TEST_OBJS)
  71. TEST_OBJS_DEPS = $(wildcard include/test/*.h include/test/*/*.h)
  72. ifdef RECORD_PSA_STATUS_COVERAGE_LOG
  73. # Explicitly depend on this header because on a clean copy of the source tree,
  74. # it doesn't exist yet and must be generated as part of the build, and
  75. # therefore the wildcard enumeration above doesn't include it.
  76. TEST_OBJS_DEPS += include/test/instrument_record_status.h
  77. endif
  78. # Rule to compile common test C files in src folder
  79. src/%.o : src/%.c $(TEST_OBJS_DEPS)
  80. echo " CC $<"
  81. $(CC) $(LOCAL_CFLAGS) $(CFLAGS) -o $@ -c $<
  82. src/drivers/%.o : src/drivers/%.c
  83. echo " CC $<"
  84. $(CC) $(LOCAL_CFLAGS) $(CFLAGS) -o $@ -c $<
  85. C_FILES := $(addsuffix .c,$(APPS))
  86. # Wildcard target for test code generation:
  87. # A .c file is generated for each .data file in the suites/ directory. Each .c
  88. # file depends on a .data and .function file from suites/ directory. Following
  89. # nameing convention is followed:
  90. #
  91. # C file | Depends on
  92. #-----------------------------------------------------------------------------
  93. # foo.c | suites/foo.function suites/foo.data
  94. # foo.bar.c | suites/foo.function suites/foo.bar.data
  95. #
  96. # Note above that .c and .data files have same base name.
  97. # However, corresponding .function file's base name is the word before first
  98. # dot in .c file's base name.
  99. #
  100. .SECONDEXPANSION:
  101. %.c: suites/$$(firstword $$(subst ., ,$$*)).function suites/%.data scripts/generate_test_code.py suites/helpers.function suites/main_test.function suites/host_test.function
  102. echo " Gen $@"
  103. $(PYTHON) scripts/generate_test_code.py -f suites/$(firstword $(subst ., ,$*)).function \
  104. -d suites/$*.data \
  105. -t suites/main_test.function \
  106. -p suites/host_test.function \
  107. -s suites \
  108. --helpers-file suites/helpers.function \
  109. -o .
  110. $(BINARIES): %$(EXEXT): %.c $(MBEDLIBS) $(TEST_OBJS_DEPS) $(MBEDTLS_TEST_OBJS)
  111. echo " CC $<"
  112. $(CC) $(LOCAL_CFLAGS) $(CFLAGS) $< $(MBEDTLS_TEST_OBJS) $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
  113. clean:
  114. ifndef WINDOWS
  115. rm -rf $(BINARIES) *.c *.datax
  116. rm -f src/*.o src/drivers/*.o src/libmbed*
  117. rm -f include/test/instrument_record_status.h
  118. rm -rf libtestdriver1
  119. else
  120. if exist *.c del /Q /F *.c
  121. if exist *.exe del /Q /F *.exe
  122. if exist *.datax del /Q /F *.datax
  123. if exist src/*.o del /Q /F src/*.o
  124. if exist src/drivers/*.o del /Q /F src/drivers/*.o
  125. if exist src/libmbed* del /Q /F src/libmed*
  126. if exist include/test/instrument_record_status.h del /Q /F include/test/instrument_record_status.h
  127. endif
  128. # Test suites caught by SKIP_TEST_SUITES are built but not executed.
  129. check: $(BINARIES)
  130. perl scripts/run-test-suites.pl $(TEST_FLAGS) --skip=$(SKIP_TEST_SUITES)
  131. test: check
  132. # Generate test library
  133. # Perl code that is executed to transform each original line from a library
  134. # source file into the corresponding line in the test driver copy of the
  135. # library. Add a LIBTESTDRIVER1_/libtestdriver1_ to mbedtls_xxx and psa_xxx
  136. # symbols.
  137. define libtestdriver1_rewrite :=
  138. s!^(\s*#\s*include\s*[\"<])(mbedtls|psa)/!$${1}libtestdriver1/include/$${2}/!; \
  139. next if /^\s*#\s*include/; \
  140. s/\b(?=MBEDTLS_|PSA_)/LIBTESTDRIVER1_/g; \
  141. s/\b(?=mbedtls_|psa_)/libtestdriver1_/g;
  142. endef
  143. libtestdriver1.a:
  144. # Copy the library and fake a 3rdparty Makefile include.
  145. rm -Rf ./libtestdriver1
  146. mkdir ./libtestdriver1
  147. cp -Rf ../library ./libtestdriver1
  148. cp -Rf ../include ./libtestdriver1
  149. mkdir ./libtestdriver1/3rdparty
  150. touch ./libtestdriver1/3rdparty/Makefile.inc
  151. # Set the test driver base (minimal) configuration.
  152. cp ./include/test/drivers/config_test_driver.h ./libtestdriver1/include/mbedtls/config.h
  153. # Set the PSA cryptography configuration for the test library.
  154. # It is set from the copied include/psa/crypto_config.h of the Mbed TLS
  155. # library the test library is intended to be linked with extended by
  156. # ./include/test/drivers/crypto_config_test_driver_extension.h to
  157. # mirror the PSA_ACCEL_* macros.
  158. mv ./libtestdriver1/include/psa/crypto_config.h ./libtestdriver1/include/psa/crypto_config.h.bak
  159. head -n -1 ./libtestdriver1/include/psa/crypto_config.h.bak > ./libtestdriver1/include/psa/crypto_config.h
  160. cat ./include/test/drivers/crypto_config_test_driver_extension.h >> ./libtestdriver1/include/psa/crypto_config.h
  161. echo "#endif /* PSA_CRYPTO_CONFIG_H */" >> ./libtestdriver1/include/psa/crypto_config.h
  162. # Prefix MBEDTLS_* PSA_* symbols with LIBTESTDRIVER1_ as well as
  163. # mbedtls_* psa_* symbols with libtestdriver1_ to avoid symbol clash
  164. # when this test driver library is linked with the Mbed TLS library.
  165. perl -pi -e '$(libtestdriver1_rewrite)' ./libtestdriver1/library/*.[ch]
  166. perl -pi -e '$(libtestdriver1_rewrite)' ./libtestdriver1/include/*/*.h
  167. $(MAKE) -C ./libtestdriver1/library CFLAGS="-I../../ $(CFLAGS)" LDFLAGS="$(LDFLAGS)" libmbedcrypto.a
  168. cp ./libtestdriver1/library/libmbedcrypto.a ../library/libtestdriver1.a
  169. ifdef RECORD_PSA_STATUS_COVERAGE_LOG
  170. include/test/instrument_record_status.h: ../include/psa/crypto.h Makefile
  171. echo " Gen $@"
  172. sed <../include/psa/crypto.h >$@ -n 's/^psa_status_t \([A-Za-z0-9_]*\)(.*/#define \1(...) RECORD_STATUS("\1", \1(__VA_ARGS__))/p'
  173. endif