Test.mak 4.8 KB

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