lib.mk 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # This mimics the top-level Makefile. We do it explicitly here so that this
  2. # Makefile can operate with or without the kbuild infrastructure.
  3. CC := $(CROSS_COMPILE)gcc
  4. ifeq (0,$(MAKELEVEL))
  5. OUTPUT := $(shell pwd)
  6. endif
  7. # The following are built by lib.mk common compile rules.
  8. # TEST_CUSTOM_PROGS should be used by tests that require
  9. # custom build rule and prevent common build rule use.
  10. # TEST_PROGS are for test shell scripts.
  11. # TEST_CUSTOM_PROGS and TEST_PROGS will be run by common run_tests
  12. # and install targets. Common clean doesn't touch them.
  13. TEST_GEN_PROGS := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS))
  14. TEST_GEN_PROGS_EXTENDED := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS_EXTENDED))
  15. TEST_GEN_FILES := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_FILES))
  16. top_srcdir ?= ../../../..
  17. include $(top_srcdir)/scripts/subarch.include
  18. ARCH ?= $(SUBARCH)
  19. all: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
  20. .PHONY: khdr
  21. khdr:
  22. make ARCH=$(ARCH) -C $(top_srcdir) headers_install
  23. ifdef KSFT_KHDR_INSTALL
  24. $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES):| khdr
  25. endif
  26. .ONESHELL:
  27. define RUN_TEST_PRINT_RESULT
  28. TEST_HDR_MSG="selftests: "`basename $$PWD`:" $$BASENAME_TEST"; \
  29. echo $$TEST_HDR_MSG; \
  30. echo "========================================"; \
  31. if [ ! -x $$TEST ]; then \
  32. echo "$$TEST_HDR_MSG: Warning: file $$BASENAME_TEST is not executable, correct this.";\
  33. echo "not ok 1..$$test_num $$TEST_HDR_MSG [FAIL]"; \
  34. else \
  35. cd `dirname $$TEST` > /dev/null; \
  36. if [ "X$(summary)" != "X" ]; then \
  37. (./$$BASENAME_TEST > /tmp/$$BASENAME_TEST 2>&1 && \
  38. echo "ok 1..$$test_num $$TEST_HDR_MSG [PASS]") || \
  39. (if [ $$? -eq $$skip ]; then \
  40. echo "not ok 1..$$test_num $$TEST_HDR_MSG [SKIP]"; \
  41. else echo "not ok 1..$$test_num $$TEST_HDR_MSG [FAIL]"; \
  42. fi;) \
  43. else \
  44. (./$$BASENAME_TEST && \
  45. echo "ok 1..$$test_num $$TEST_HDR_MSG [PASS]") || \
  46. (if [ $$? -eq $$skip ]; then \
  47. echo "not ok 1..$$test_num $$TEST_HDR_MSG [SKIP]"; \
  48. else echo "not ok 1..$$test_num $$TEST_HDR_MSG [FAIL]"; \
  49. fi;) \
  50. fi; \
  51. cd - > /dev/null; \
  52. fi;
  53. endef
  54. define RUN_TESTS
  55. @export KSFT_TAP_LEVEL=`echo 1`; \
  56. test_num=`echo 0`; \
  57. skip=`echo 4`; \
  58. echo "TAP version 13"; \
  59. for TEST in $(1); do \
  60. BASENAME_TEST=`basename $$TEST`; \
  61. test_num=`echo $$test_num+1 | bc`; \
  62. $(call RUN_TEST_PRINT_RESULT,$(TEST),$(BASENAME_TEST),$(test_num),$(skip)) \
  63. done;
  64. endef
  65. run_tests: all
  66. ifneq ($(KBUILD_SRC),)
  67. @if [ "X$(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES)" != "X" ]; then
  68. @rsync -aq $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(OUTPUT)
  69. fi
  70. @if [ "X$(TEST_PROGS)" != "X" ]; then
  71. $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(OUTPUT)/$(TEST_PROGS))
  72. else
  73. $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS))
  74. fi
  75. else
  76. $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS))
  77. endif
  78. define INSTALL_RULE
  79. @if [ "X$(TEST_PROGS)$(TEST_PROGS_EXTENDED)$(TEST_FILES)" != "X" ]; then \
  80. mkdir -p ${INSTALL_PATH}; \
  81. echo "rsync -a $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(INSTALL_PATH)/"; \
  82. rsync -a $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(INSTALL_PATH)/; \
  83. fi
  84. @if [ "X$(TEST_GEN_PROGS)$(TEST_CUSTOM_PROGS)$(TEST_GEN_PROGS_EXTENDED)$(TEST_GEN_FILES)" != "X" ]; then \
  85. mkdir -p ${INSTALL_PATH}; \
  86. echo "rsync -a $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(INSTALL_PATH)/"; \
  87. rsync -a $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(INSTALL_PATH)/; \
  88. fi
  89. endef
  90. install: all
  91. ifdef INSTALL_PATH
  92. $(INSTALL_RULE)
  93. else
  94. $(error Error: set INSTALL_PATH to use install)
  95. endif
  96. define EMIT_TESTS
  97. @test_num=`echo 0`; \
  98. for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \
  99. BASENAME_TEST=`basename $$TEST`; \
  100. test_num=`echo $$test_num+1 | bc`; \
  101. TEST_HDR_MSG="selftests: "`basename $$PWD`:" $$BASENAME_TEST"; \
  102. echo "echo $$TEST_HDR_MSG"; \
  103. if [ ! -x $$TEST ]; then \
  104. echo "echo \"$$TEST_HDR_MSG: Warning: file $$BASENAME_TEST is not executable, correct this.\""; \
  105. echo "echo \"not ok 1..$$test_num $$TEST_HDR_MSG [FAIL]\""; \
  106. else
  107. echo "(./$$BASENAME_TEST >> \$$OUTPUT 2>&1 && echo \"ok 1..$$test_num $$TEST_HDR_MSG [PASS]\") || (if [ \$$? -eq \$$skip ]; then echo \"not ok 1..$$test_num $$TEST_HDR_MSG [SKIP]\"; else echo \"not ok 1..$$test_num $$TEST_HDR_MSG [FAIL]\"; fi;)"; \
  108. fi; \
  109. done;
  110. endef
  111. emit_tests:
  112. $(EMIT_TESTS)
  113. # define if isn't already. It is undefined in make O= case.
  114. ifeq ($(RM),)
  115. RM := rm -f
  116. endif
  117. define CLEAN
  118. $(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
  119. endef
  120. clean:
  121. $(CLEAN)
  122. # When make O= with kselftest target from main level
  123. # the following aren't defined.
  124. #
  125. ifneq ($(KBUILD_SRC),)
  126. LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
  127. COMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
  128. LINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
  129. endif
  130. # Selftest makefiles can override those targets by setting
  131. # OVERRIDE_TARGETS = 1.
  132. ifeq ($(OVERRIDE_TARGETS),)
  133. $(OUTPUT)/%:%.c
  134. $(LINK.c) $^ $(LDLIBS) -o $@
  135. $(OUTPUT)/%.o:%.S
  136. $(COMPILE.S) $^ -o $@
  137. $(OUTPUT)/%:%.S
  138. $(LINK.S) $^ $(LDLIBS) -o $@
  139. endif
  140. .PHONY: run_tests all clean install emit_tests