Test.mak 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. ifeq ($(TESTS),)
  7. TESTS := $(patsubst %.c,%,$(wildcard *.c))
  8. endif
  9. ifneq ($(TESTS_DISABLED),)
  10. TESTS := $(filter-out $(TESTS_DISABLED),$(TESTS))
  11. endif
  12. ifeq ($(SHELL_TESTS),)
  13. SHELL_TESTS := $(patsubst %.sh,shell_%,$(wildcard *.sh))
  14. endif
  15. ifneq ($(filter-out test,$(strip $(TESTS))),$(strip $(TESTS)))
  16. $(error Sanity check: cannot have a test named "test.c")
  17. endif
  18. U_TARGETS := $(TESTS)
  19. G_TARGETS := $(addsuffix _glibc,$(U_TARGETS))
  20. ifneq ($(GLIBC_TESTS_DISABLED),)
  21. G_TARGETS := $(filter-out $(GLIBC_TESTS_DISABLED),$(G_TARGETS))
  22. endif
  23. ifeq ($(GLIBC_ONLY),)
  24. TARGETS += $(U_TARGETS)
  25. endif
  26. ifeq ($(UCLIBC_ONLY),)
  27. TARGETS += $(G_TARGETS)
  28. endif
  29. CLEAN_TARGETS := $(U_TARGETS) $(G_TARGETS)
  30. CLEAN_TARGETS += $(TESTS_DISABLED) $(addsuffix _glibc,$(TESTS_DISABLED)) $(GLIBC_TESTS_DISABLED)
  31. COMPILE_TARGETS := $(TARGETS)
  32. RUN_TARGETS := $(addsuffix .exe,$(TARGETS))
  33. # provide build rules even for disabled tests:
  34. U_TARGETS += $(TESTS_DISABLED)
  35. G_TARGETS += $(addsuffix _glibc,$(TESTS_DISABLED)) $(GLIBC_TESTS_DISABLED)
  36. TARGETS += $(SHELL_TESTS)
  37. CFLAGS += $(CFLAGS_$(notdir $(CURDIR)))
  38. define binary_name
  39. $(patsubst %.exe,%,$@)
  40. endef
  41. define tst_src_name
  42. $(patsubst %_glibc,%,$(binary_name))
  43. endef
  44. define diff_test
  45. $(Q)\
  46. for x in "$(binary_name).out" "$(tst_src_name).out" ; do \
  47. test -e "$$x.good" && $(do_showdiff) "$(binary_name).out" "$$x.good" && exec diff -u "$(binary_name).out" "$$x.good" ; \
  48. done ; \
  49. true
  50. endef
  51. define uclibc_glibc_diff_test
  52. $(Q)\
  53. test -z "$(DODIFF_$(tst_src_name))" && exec true ; \
  54. uclibc_out="$(binary_name).out" ; \
  55. glibc_out="$(tst_src_name).out" ; \
  56. $(do_showdiff) $$uclibc_out $$glibc_out ; \
  57. exec diff -u "$$uclibc_out" "$$glibc_out"
  58. endef
  59. define exec_test
  60. $(showtest)
  61. $(Q)\
  62. $(WRAPPER) $(WRAPPER_$(tst_src_name)) \
  63. ./$(binary_name) $(OPTS) $(OPTS_$(tst_src_name)) > "$(binary_name).out" 2>&1 ; \
  64. ret=$$? ; \
  65. expected_ret="$(RET_$(tst_src_name))" ; \
  66. test -z "$$expected_ret" && export expected_ret=0 ; \
  67. if ! test $$ret -eq $$expected_ret ; then \
  68. echo "ret == $$ret ; expected_ret == $$expected_ret" ; \
  69. echo "The output of failed test is:"; \
  70. cat "$(binary_name).out"; \
  71. exit 1 ; \
  72. fi
  73. $(SCAT) "$(binary_name).out"
  74. endef
  75. test check all: run
  76. run: $(RUN_TARGETS)
  77. $(RUN_TARGETS):
  78. $(exec_test)
  79. $(diff_test)
  80. ifeq ($(UCLIBC_ONLY),)
  81. $(uclibc_glibc_diff_test)
  82. endif
  83. compile: $(COMPILE_TARGETS)
  84. G_TARGET_SRCS := $(addsuffix .c,$(G_TARGETS))
  85. U_TARGET_SRCS := $(addsuffix .c,$(U_TARGETS))
  86. $(MAKE_SRCS): Makefile $(TESTDIR)Makefile $(TESTDIR)Rules.mak $(TESTDIR)Test.mak
  87. $(U_TARGETS): $(U_TARGET_SRCS) $(MAKE_SRCS)
  88. $(showlink)
  89. $(Q)$(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(CFLAGS_$(notdir $(CURDIR))) $(CFLAGS_$@) -c $@.c -o $@.o
  90. $(Q)$(CC) $(LDFLAGS) $@.o -o $@ $(EXTRA_LDFLAGS) $(LDFLAGS_$@)
  91. $(G_TARGETS): $(U_TARGET_SRCS) $(MAKE_SRCS)
  92. $(showlink)
  93. $(Q)$(HOSTCC) $(HOST_CFLAGS) $(CFLAGS_$(notdir $(CURDIR))) $(CFLAGS_$(patsubst %_glibc,%,$@)) -c $(patsubst %_glibc,%,$@).c -o $@.o
  94. $(Q)$(HOSTCC) $(HOST_LDFLAGS) $@.o -o $@ $(EXTRA_LDFLAGS) $(LDFLAGS_$(patsubst %_glibc,%,$@))
  95. shell_%:
  96. $(showtest)
  97. $(Q)$(if $(PRE_RUN_$(@)),$(PRE_RUN_$(@)))
  98. $(Q)$(SHELL) $(patsubst shell_%,%.sh,$(binary_name))
  99. $(Q)$(if $(POST_RUN_$(@)),$(POST_RUN_$(@)))
  100. %.so: %.c
  101. $(showlink)
  102. $(Q)$(CC) \
  103. $(CFLAGS) $(EXTRA_CFLAGS) $(CFLAGS_$(patsubst %_glibc,%,$@)) \
  104. -fPIC -shared $< -o $@ -Wl,-soname,$@ \
  105. $(LDFLAGS) $(EXTRA_LIBS) $(LDFLAGS_$(patsubst %_glibc,%,$@))
  106. clean:
  107. $(showclean)
  108. $(Q)$(RM) *.a *.o *.so *~ core *.out *.gdb $(CLEAN_TARGETS) $(EXTRA_CLEAN)
  109. $(Q)$(RM_R) $(EXTRA_DIRS)
  110. .PHONY: all check clean test run compile