rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 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 | |
| 5 | ifeq (0,$(MAKELEVEL)) |
| 6 | OUTPUT := $(shell pwd) |
| 7 | endif |
| 8 | |
| 9 | # The following are built by lib.mk common compile rules. |
| 10 | # TEST_CUSTOM_PROGS should be used by tests that require |
| 11 | # custom build rule and prevent common build rule use. |
| 12 | # TEST_PROGS are for test shell scripts. |
| 13 | # TEST_CUSTOM_PROGS and TEST_PROGS will be run by common run_tests |
| 14 | # and install targets. Common clean doesn't touch them. |
| 15 | TEST_GEN_PROGS := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS)) |
| 16 | TEST_GEN_PROGS_EXTENDED := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS_EXTENDED)) |
| 17 | TEST_GEN_FILES := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_FILES)) |
| 18 | |
| 19 | all: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) |
| 20 | |
| 21 | .ONESHELL: |
| 22 | define RUN_TESTS |
| 23 | @test_num=`echo 0`; |
| 24 | @echo "TAP version 13"; |
| 25 | @for TEST in $(1); do \ |
| 26 | BASENAME_TEST=`basename $$TEST`; \ |
| 27 | test_num=`echo $$test_num+1 | bc`; \ |
| 28 | echo "selftests: $$BASENAME_TEST"; \ |
| 29 | echo "========================================"; \ |
| 30 | if [ ! -x $$TEST ]; then \ |
| 31 | echo "selftests: Warning: file $$BASENAME_TEST is not executable, correct this.";\ |
| 32 | echo "not ok 1..$$test_num selftests: $$BASENAME_TEST [FAIL]"; \ |
| 33 | else \ |
| 34 | if [ "X$(summary)" != "X" ]; then \ |
| 35 | 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;\ |
| 36 | else \ |
| 37 | 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;\ |
| 38 | fi; \ |
| 39 | fi; \ |
| 40 | done; |
| 41 | endef |
| 42 | |
| 43 | run_tests: all |
| 44 | ifneq ($(KBUILD_SRC),) |
| 45 | @if [ "X$(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES)" != "X" ]; then |
| 46 | @rsync -aq $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(OUTPUT) |
| 47 | fi |
| 48 | @if [ "X$(TEST_PROGS)" != "X" ]; then |
| 49 | $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(OUTPUT)/$(TEST_PROGS)) |
| 50 | else |
| 51 | $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS)) |
| 52 | fi |
| 53 | else |
| 54 | $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS)) |
| 55 | endif |
| 56 | |
| 57 | define INSTALL_SINGLE_RULE |
| 58 | $(if $(INSTALL_LIST),@mkdir -p $(INSTALL_PATH)) |
| 59 | $(if $(INSTALL_LIST),@echo rsync -a $(INSTALL_LIST) $(INSTALL_PATH)/) |
| 60 | $(if $(INSTALL_LIST),@rsync -a $(INSTALL_LIST) $(INSTALL_PATH)/) |
| 61 | endef |
| 62 | |
| 63 | define INSTALL_RULE |
| 64 | $(eval INSTALL_LIST = $(TEST_PROGS)) $(INSTALL_SINGLE_RULE) |
| 65 | $(eval INSTALL_LIST = $(TEST_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE) |
| 66 | $(eval INSTALL_LIST = $(TEST_FILES)) $(INSTALL_SINGLE_RULE) |
| 67 | $(eval INSTALL_LIST = $(TEST_GEN_PROGS)) $(INSTALL_SINGLE_RULE) |
| 68 | $(eval INSTALL_LIST = $(TEST_CUSTOM_PROGS)) $(INSTALL_SINGLE_RULE) |
| 69 | $(eval INSTALL_LIST = $(TEST_GEN_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE) |
| 70 | $(eval INSTALL_LIST = $(TEST_GEN_FILES)) $(INSTALL_SINGLE_RULE) |
| 71 | endef |
| 72 | |
| 73 | install: all |
| 74 | ifdef INSTALL_PATH |
| 75 | $(INSTALL_RULE) |
| 76 | else |
| 77 | $(error Error: set INSTALL_PATH to use install) |
| 78 | endif |
| 79 | |
| 80 | define EMIT_TESTS |
| 81 | @for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \ |
| 82 | BASENAME_TEST=`basename $$TEST`; \ |
| 83 | echo "(./$$BASENAME_TEST > /tmp/$$BASENAME_TEST 2>&1 && echo \"selftests: $$BASENAME_TEST [PASS]\") || echo \"selftests: $$BASENAME_TEST [FAIL]\""; \ |
| 84 | done; |
| 85 | endef |
| 86 | |
| 87 | emit_tests: |
| 88 | $(EMIT_TESTS) |
| 89 | |
| 90 | # define if isn't already. It is undefined in make O= case. |
| 91 | ifeq ($(RM),) |
| 92 | RM := rm -f |
| 93 | endif |
| 94 | |
| 95 | define CLEAN |
| 96 | $(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN) |
| 97 | endef |
| 98 | |
| 99 | clean: |
| 100 | $(CLEAN) |
| 101 | |
| 102 | # When make O= with kselftest target from main level |
| 103 | # the following aren't defined. |
| 104 | # |
| 105 | ifneq ($(KBUILD_SRC),) |
| 106 | LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) |
| 107 | COMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c |
| 108 | LINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) |
| 109 | endif |
| 110 | |
| 111 | $(OUTPUT)/%:%.c |
| 112 | $(LINK.c) $^ $(LDLIBS) -o $@ |
| 113 | |
| 114 | $(OUTPUT)/%.o:%.S |
| 115 | $(COMPILE.S) $^ -o $@ |
| 116 | |
| 117 | $(OUTPUT)/%:%.S |
| 118 | $(LINK.S) $^ $(LDLIBS) -o $@ |
| 119 | |
| 120 | .PHONY: run_tests all clean install emit_tests |