x86/hyperv: Enable PV qspinlock for Hyper-V
[linux-2.6-block.git] / tools / testing / selftests / lib.mk
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_TEST_PRINT_RESULT
23         TEST_HDR_MSG="selftests: "`basename $$PWD`:" $$BASENAME_TEST";  \
24         echo $$TEST_HDR_MSG;                                    \
25         echo "========================================";        \
26         if [ ! -x $$TEST ]; then        \
27                 echo "$$TEST_HDR_MSG: Warning: file $$BASENAME_TEST is not executable, correct this.";\
28                 echo "not ok 1..$$test_num $$TEST_HDR_MSG [FAIL]"; \
29         else                                    \
30                 cd `dirname $$TEST` > /dev/null; \
31                 if [ "X$(summary)" != "X" ]; then       \
32                         (./$$BASENAME_TEST > /tmp/$$BASENAME_TEST 2>&1 && \
33                         echo "ok 1..$$test_num $$TEST_HDR_MSG [PASS]") || \
34                         (if [ $$? -eq $$skip ]; then    \
35                                 echo "not ok 1..$$test_num $$TEST_HDR_MSG [SKIP]";                              \
36                         else echo "not ok 1..$$test_num $$TEST_HDR_MSG [FAIL]";                                 \
37                         fi;)                    \
38                 else                            \
39                         (./$$BASENAME_TEST &&   \
40                         echo "ok 1..$$test_num $$TEST_HDR_MSG [PASS]") ||                                               \
41                         (if [ $$? -eq $$skip ]; then \
42                                 echo "not ok 1..$$test_num $$TEST_HDR_MSG [SKIP]"; \
43                         else echo "not ok 1..$$test_num $$TEST_HDR_MSG [FAIL]";                         \
44                         fi;)            \
45                 fi;                             \
46                 cd - > /dev/null;               \
47         fi;
48 endef
49
50 define RUN_TESTS
51         @export KSFT_TAP_LEVEL=`echo 1`;                \
52         test_num=`echo 0`;                              \
53         skip=`echo 4`;                                  \
54         echo "TAP version 13";                          \
55         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         done;
60 endef
61
62 run_tests: all
63 ifneq ($(KBUILD_SRC),)
64         @if [ "X$(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES)" != "X" ]; then
65                 @rsync -aq $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(OUTPUT)
66         fi
67         @if [ "X$(TEST_PROGS)" != "X" ]; then
68                 $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(OUTPUT)/$(TEST_PROGS))
69         else
70                 $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS))
71         fi
72 else
73         $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS))
74 endif
75
76 define INSTALL_RULE
77         @if [ "X$(TEST_PROGS)$(TEST_PROGS_EXTENDED)$(TEST_FILES)" != "X" ]; then                                        \
78                 mkdir -p ${INSTALL_PATH};                                                                               \
79                 echo "rsync -a $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(INSTALL_PATH)/";    \
80                 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                 echo "rsync -a $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(INSTALL_PATH)/";   \
85                 rsync -a $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(INSTALL_PATH)/;          \
86         fi
87 endef
88
89 install: all
90 ifdef INSTALL_PATH
91         $(INSTALL_RULE)
92 else
93         $(error Error: set INSTALL_PATH to use install)
94 endif
95
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
112 emit_tests:
113         $(EMIT_TESTS)
114
115 # define if isn't already. It is undefined in make O= case.
116 ifeq ($(RM),)
117 RM := rm -f
118 endif
119
120 define CLEAN
121         $(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
122 endef
123
124 clean:
125         $(CLEAN)
126
127 # When make O= with kselftest target from main level
128 # the following aren't defined.
129 #
130 ifneq ($(KBUILD_SRC),)
131 LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
132 COMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
133 LINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
134 endif
135
136 # Selftest makefiles can override those targets by setting
137 # OVERRIDE_TARGETS = 1.
138 ifeq ($(OVERRIDE_TARGETS),)
139 $(OUTPUT)/%:%.c
140         $(LINK.c) $^ $(LDLIBS) -o $@
141
142 $(OUTPUT)/%.o:%.S
143         $(COMPILE.S) $^ -o $@
144
145 $(OUTPUT)/%:%.S
146         $(LINK.S) $^ $(LDLIBS) -o $@
147 endif
148
149 .PHONY: run_tests all clean install emit_tests