lib.mk 4.4 KB
Newer Older
1 2 3 4
# This mimics the top-level Makefile. We do it explicitly here so that this
# Makefile can operate with or without the kbuild infrastructure.
CC := $(CROSS_COMPILE)gcc

5 6 7 8
ifeq (0,$(MAKELEVEL))
OUTPUT := $(shell pwd)
endif

9 10 11 12 13 14
# The following are built by lib.mk common compile rules.
# TEST_CUSTOM_PROGS should be used by tests that require
# custom build rule and prevent common build rule use.
# TEST_PROGS are for test shell scripts.
# TEST_CUSTOM_PROGS and TEST_PROGS will be run by common run_tests
# 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 18 19 20
TEST_GEN_FILES := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_FILES))

all: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)

21
.ONESHELL:
22
define RUN_TEST_PRINT_RESULT
23 24
	TEST_HDR_MSG="selftests: "`basename $$PWD`:" $$BASENAME_TEST";	\
	echo $$TEST_HDR_MSG;					\
25 26
	echo "========================================";	\
	if [ ! -x $$TEST ]; then	\
27 28
		echo "$$TEST_HDR_MSG: Warning: file $$BASENAME_TEST is not executable, correct this.";\
		echo "not ok 1..$$test_num $$TEST_HDR_MSG [FAIL]"; \
29 30 31 32
	else					\
		cd `dirname $$TEST` > /dev/null; \
		if [ "X$(summary)" != "X" ]; then	\
			(./$$BASENAME_TEST > /tmp/$$BASENAME_TEST 2>&1 && \
33
			echo "ok 1..$$test_num $$TEST_HDR_MSG [PASS]") || \
34
			(if [ $$? -eq $$skip ]; then	\
35 36
				echo "not ok 1..$$test_num $$TEST_HDR_MSG [SKIP]";				\
			else echo "not ok 1..$$test_num $$TEST_HDR_MSG [FAIL]";					\
37 38 39
			fi;)			\
		else				\
			(./$$BASENAME_TEST &&	\
40
			echo "ok 1..$$test_num $$TEST_HDR_MSG [PASS]") ||						\
41
			(if [ $$? -eq $$skip ]; then \
42 43
				echo "not ok 1..$$test_num $$TEST_HDR_MSG [SKIP]"; \
			else echo "not ok 1..$$test_num $$TEST_HDR_MSG [FAIL]";				\
44 45 46 47 48 49
			fi;)		\
		fi;				\
		cd - > /dev/null;		\
	fi;
endef

50
define RUN_TESTS
51 52
	@export KSFT_TAP_LEVEL=`echo 1`;		\
	test_num=`echo 0`;				\
53
	skip=`echo 4`;					\
54 55
	echo "TAP version 13";				\
	for TEST in $(1); do				\
56
		BASENAME_TEST=`basename $$TEST`;	\
57
		test_num=`echo $$test_num+1 | bc`;	\
58
		$(call RUN_TEST_PRINT_RESULT,$(TEST),$(BASENAME_TEST),$(test_num),$(skip))						\
59 60 61 62
	done;
endef

run_tests: all
63 64 65 66 67 68 69 70 71 72
ifneq ($(KBUILD_SRC),)
	@if [ "X$(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES)" != "X" ]; then
		@rsync -aq $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(OUTPUT)
	fi
	@if [ "X$(TEST_PROGS)" != "X" ]; then
		$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(OUTPUT)/$(TEST_PROGS))
	else
		$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS))
	fi
else
73
	$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS))
74
endif
75

M
Michael Ellerman 已提交
76
define INSTALL_RULE
77 78
	@if [ "X$(TEST_PROGS)$(TEST_PROGS_EXTENDED)$(TEST_FILES)" != "X" ]; then					\
		mkdir -p ${INSTALL_PATH};										\
79 80
		echo "rsync -a $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(INSTALL_PATH)/";	\
		rsync -a $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(INSTALL_PATH)/;		\
81
	fi
82
	@if [ "X$(TEST_GEN_PROGS)$(TEST_CUSTOM_PROGS)$(TEST_GEN_PROGS_EXTENDED)$(TEST_GEN_FILES)" != "X" ]; then					\
83
		mkdir -p ${INSTALL_PATH};										\
84 85
		echo "rsync -a $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(INSTALL_PATH)/";	\
		rsync -a $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(INSTALL_PATH)/;		\
86
	fi
M
Michael Ellerman 已提交
87 88 89 90 91 92 93 94 95 96
endef

install: all
ifdef INSTALL_PATH
	$(INSTALL_RULE)
else
	$(error Error: set INSTALL_PATH to use install)
endif

define EMIT_TESTS
97
	@for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \
98
		BASENAME_TEST=`basename $$TEST`;	\
99
		echo "(./$$BASENAME_TEST >> \$$OUTPUT 2>&1 && echo \"selftests: $$BASENAME_TEST [PASS]\") || echo \"selftests: $$BASENAME_TEST [FAIL]\""; \
M
Michael Ellerman 已提交
100 101 102 103 104 105
	done;
endef

emit_tests:
	$(EMIT_TESTS)

106 107 108 109 110
# define if isn't already. It is undefined in make O= case.
ifeq ($(RM),)
RM := rm -f
endif

111
define CLEAN
112
	$(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
113 114 115 116
endef

clean:
	$(CLEAN)
117

118 119 120 121 122 123 124 125 126
# When make O= with kselftest target from main level
# the following aren't defined.
#
ifneq ($(KBUILD_SRC),)
LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
COMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
LINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
endif

127
$(OUTPUT)/%:%.c
128
	$(LINK.c) $^ $(LDLIBS) -o $@
129 130

$(OUTPUT)/%.o:%.S
131
	$(COMPILE.S) $^ -o $@
132 133

$(OUTPUT)/%:%.S
134
	$(LINK.S) $^ $(LDLIBS) -o $@
135

M
Michael Ellerman 已提交
136
.PHONY: run_tests all clean install emit_tests