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