lib.mk 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. all: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
  17. .ONESHELL:
  18. define RUN_TESTS
  19. @test_num=`echo 0`;
  20. @echo "TAP version 13";
  21. @for TEST in $(1); do \
  22. BASENAME_TEST=`basename $$TEST`; \
  23. test_num=`echo $$test_num+1 | bc`; \
  24. echo "selftests: $$BASENAME_TEST"; \
  25. echo "========================================"; \
  26. if [ ! -x $$TEST ]; then \
  27. echo "selftests: Warning: file $$BASENAME_TEST is not executable, correct this.";\
  28. echo "not ok 1..$$test_num selftests: $$BASENAME_TEST [FAIL]"; \
  29. else \
  30. if [ "X$(summary)" != "X" ]; then \
  31. cd `dirname $$TEST` > /dev/null; (./$$BASENAME_TEST > /tmp/$$BASENAME_TEST 2>&1 && echo "ok 1..$$test_num selftests: $$BASENAME_TEST [PASS]") || echo "not ok 1..$$test_num selftests: $$BASENAME_TEST [FAIL]"; cd - > /dev/null;\
  32. else \
  33. cd `dirname $$TEST` > /dev/null; (./$$BASENAME_TEST && echo "ok 1..$$test_num selftests: $$BASENAME_TEST [PASS]") || echo "not ok 1..$$test_num selftests: $$BASENAME_TEST [FAIL]"; cd - > /dev/null;\
  34. fi; \
  35. fi; \
  36. done;
  37. endef
  38. run_tests: all
  39. ifneq ($(KBUILD_SRC),)
  40. @if [ "X$(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES)" != "X" ]; then
  41. @rsync -aq $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(OUTPUT)
  42. fi
  43. @if [ "X$(TEST_PROGS)" != "X" ]; then
  44. $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(OUTPUT)/$(TEST_PROGS))
  45. else
  46. $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS))
  47. fi
  48. else
  49. $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS))
  50. endif
  51. define INSTALL_RULE
  52. @if [ "X$(TEST_PROGS)$(TEST_PROGS_EXTENDED)$(TEST_FILES)" != "X" ]; then \
  53. mkdir -p ${INSTALL_PATH}; \
  54. echo "rsync -a $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(INSTALL_PATH)/"; \
  55. rsync -a $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(INSTALL_PATH)/; \
  56. fi
  57. @if [ "X$(TEST_GEN_PROGS)$(TEST_CUSTOM_PROGS)$(TEST_GEN_PROGS_EXTENDED)$(TEST_GEN_FILES)" != "X" ]; then \
  58. mkdir -p ${INSTALL_PATH}; \
  59. echo "rsync -a $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(INSTALL_PATH)/"; \
  60. rsync -a $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(INSTALL_PATH)/; \
  61. fi
  62. endef
  63. install: all
  64. ifdef INSTALL_PATH
  65. $(INSTALL_RULE)
  66. else
  67. $(error Error: set INSTALL_PATH to use install)
  68. endif
  69. define EMIT_TESTS
  70. @for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \
  71. BASENAME_TEST=`basename $$TEST`; \
  72. echo "(./$$BASENAME_TEST >> \$$OUTPUT 2>&1 && echo \"selftests: $$BASENAME_TEST [PASS]\") || echo \"selftests: $$BASENAME_TEST [FAIL]\""; \
  73. done;
  74. endef
  75. emit_tests:
  76. $(EMIT_TESTS)
  77. # define if isn't already. It is undefined in make O= case.
  78. ifeq ($(RM),)
  79. RM := rm -f
  80. endif
  81. define CLEAN
  82. $(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
  83. endef
  84. clean:
  85. $(CLEAN)
  86. # When make O= with kselftest target from main level
  87. # the following aren't defined.
  88. #
  89. ifneq ($(KBUILD_SRC),)
  90. LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
  91. COMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
  92. LINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
  93. endif
  94. $(OUTPUT)/%:%.c
  95. $(LINK.c) $^ $(LDLIBS) -o $@
  96. $(OUTPUT)/%.o:%.S
  97. $(COMPILE.S) $^ -o $@
  98. $(OUTPUT)/%:%.S
  99. $(LINK.S) $^ $(LDLIBS) -o $@
  100. .PHONY: run_tests all clean install emit_tests