Test.mak 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # Common makefile rules for tests
  2. # Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  3. shellescape='$(subst ','\'',$(1))'
  4. ifeq ($(TESTS),)
  5. TESTS := $(patsubst %.c,%,$(wildcard *.c))
  6. endif
  7. ifneq ($(TESTS_DISABLED),)
  8. TESTS := $(filter-out $(TESTS_DISABLED),$(TESTS))
  9. endif
  10. ifneq ($(filter-out test,$(strip $(TESTS))),$(strip $(TESTS)))
  11. $(error Sanity check: cannot have a test named "test.c")
  12. endif
  13. TARGETS := $(TESTS)
  14. CLEAN_TARGETS := $(TARGETS)
  15. CLEAN_TARGETS += $(TESTS_DISABLED)
  16. COMPILE_TARGETS := $(TARGETS)
  17. # We sort the targets so uClibc and host-libc tests are run adjacent
  18. RUN_TARGETS := $(sort $(addsuffix .exe,$(TARGETS)))
  19. COMPILE_TARGETS := $(sort $(COMPILE_TARGETS))
  20. # provide build rules even for disabled tests:
  21. TARGETS += $(TESTS_DISABLED)
  22. CFLAGS += $(CFLAGS_$(notdir $(CURDIR)))
  23. ifeq (1,$(UCLIBCNG_GENERATE_TESTRUNNER))
  24. UCLIBCNG_TEST_SUBDIR ?= $(patsubst $(realpath $(TESTDIR))/%,%,$(CURDIR))
  25. endif
  26. define binary_name
  27. $(patsubst %.exe,%,$@)
  28. endef
  29. define tst_src_name
  30. $(patsubst %_glibc,%,$(binary_name))
  31. endef
  32. define diff_test
  33. $(Q)\
  34. for x in "$(binary_name).out" "$(tst_src_name).out" ; do \
  35. test -e "$$x.good" && $(do_showdiff) "$(binary_name).out" "$$x.good" && exec diff -u "$(binary_name).out" "$$x.good" ; \
  36. done ; \
  37. true
  38. endef
  39. define uclibc_glibc_diff_test
  40. $(Q)\
  41. test -z "$(DODIFF_$(tst_src_name))" && exec true ; \
  42. uclibc_out="$(binary_name).out" ; \
  43. glibc_out="$(tst_src_name).out" ; \
  44. $(do_showdiff) $$uclibc_out $$glibc_out ; \
  45. exec diff -u "$$uclibc_out" "$$glibc_out"
  46. endef
  47. define exec_test
  48. $(showtest)
  49. $(Q)\
  50. $(SIMULATOR) $(WRAPPER) $(WRAPPER_$(tst_src_name)) \
  51. ./$(binary_name) $(OPTS) $(OPTS_$(tst_src_name)) > "$(binary_name).out" 2>&1 ; \
  52. ret=$$? ; \
  53. expected_ret="$(RET_$(tst_src_name))" ; \
  54. test -z "$$expected_ret" && export expected_ret=0 ; \
  55. if ! test $$ret -eq $$expected_ret ; then \
  56. echo "ret == $$ret ; expected_ret == $$expected_ret" ; \
  57. echo "The output of the failed test is:"; \
  58. cat "$(binary_name).out"; \
  59. exit 1 ; \
  60. fi
  61. $(SCAT) "$(binary_name).out"
  62. endef
  63. test check all: run
  64. run: $(RUN_TARGETS)
  65. $(addsuffix .exe,$(TARGETS)): SIMULATOR:=$(SIMULATOR)
  66. $(RUN_TARGETS):
  67. ifeq (1,$(UCLIBCNG_GENERATE_TESTRUNNER))
  68. $(Q)\
  69. expected_ret="$(RET_$(tst_src_name))"; echo \
  70. "$${expected_ret:-0}" \
  71. $(call shellescape,$(tst_src_name)) \
  72. $(call shellescape,$(binary_name)) \
  73. $(call shellescape,$(UCLIBCNG_TEST_SUBDIR)) \
  74. $(call shellescape,$(WRAPPER) $(WRAPPER_$(tst_src_name)) ./$(binary_name) $(OPTS) $(OPTS_$(tst_src_name))) \
  75. >>$(top_builddir)/test/uclibcng-testrunner.in
  76. else
  77. $(exec_test)
  78. $(diff_test)
  79. endif
  80. compile: $(COMPILE_TARGETS)
  81. TARGET_SRCS := $(addsuffix .c,$(TARGETS))
  82. MAKE_SRCS := $(wildcard Makefile.in) $(TESTDIR)Makefile $(TESTDIR)Rules.mak $(TESTDIR)Test.mak
  83. $(TARGETS): $(TARGET_SRCS) $(MAKE_SRCS)
  84. $(showlink)
  85. $(Q)$(CC) $(filter-out $(CFLAGS-OMIT-$@),$(CFLAGS)) $(EXTRA_CFLAGS) $(CFLAGS_$(notdir $(CURDIR))) $(CFLAGS_$@) -c $@.c -o $@.o
  86. $(Q)$(CC) $(filter-out $(LDFLAGS-OMIT-$@),$(LDFLAGS)) $@.o -o $@ $(EXTRA_LDFLAGS) $(LDFLAGS_$@)
  87. %.so: %.c
  88. $(showlink)
  89. $(Q)$(CC) \
  90. $(filter-out $(CFLAGS-OMIT-$<),$(CFLAGS)) $(EXTRA_CFLAGS) \
  91. $(CFLAGS_$(patsubst %_glibc,%,$@)) \
  92. -fPIC -shared $< -o $@ -Wl,-soname,$@ \
  93. $(filter-out $(LDFLAGS-OMIT-$<),$(LDFLAGS)) $(EXTRA_LIBS) \
  94. $(LDFLAGS_$(patsubst %_glibc,%,$@))
  95. clean:
  96. $(showclean)
  97. $(Q)$(RM) *.a *.o *.so *~ core *.out *.gdb $(CLEAN_TARGETS) $(EXTRA_CLEAN)
  98. $(Q)$(RM_R) $(EXTRA_DIRS)
  99. .PHONY: all check clean test run compile