Merge back cpufreq updates for v5.11.
[linux-2.6-microblaze.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     ifeq ($(OUTPUT),)
7         OUTPUT := $(shell pwd)
8         DEFAULT_INSTALL_HDR_PATH := 1
9     endif
10 endif
11 selfdir = $(realpath $(dir $(filter %/lib.mk,$(MAKEFILE_LIST))))
12
13 # The following are built by lib.mk common compile rules.
14 # TEST_CUSTOM_PROGS should be used by tests that require
15 # custom build rule and prevent common build rule use.
16 # TEST_PROGS are for test shell scripts.
17 # TEST_CUSTOM_PROGS and TEST_PROGS will be run by common run_tests
18 # and install targets. Common clean doesn't touch them.
19 TEST_GEN_PROGS := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS))
20 TEST_GEN_PROGS_EXTENDED := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS_EXTENDED))
21 TEST_GEN_FILES := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_FILES))
22
23 ifdef KSFT_KHDR_INSTALL
24 top_srcdir ?= ../../../..
25 include $(top_srcdir)/scripts/subarch.include
26 ARCH            ?= $(SUBARCH)
27
28 # set default goal to all, so make without a target runs all, even when
29 # all isn't the first target in the file.
30 .DEFAULT_GOAL := all
31
32 # Invoke headers install with --no-builtin-rules to avoid circular
33 # dependency in "make kselftest" case. In this case, second level
34 # make inherits builtin-rules which will use the rule generate
35 # Makefile.o and runs into
36 # "Circular Makefile.o <- prepare dependency dropped."
37 # and headers_install fails and test compile fails.
38 # O= KBUILD_OUTPUT cases don't run into this error, since main Makefile
39 # invokes them as sub-makes and --no-builtin-rules is not necessary,
40 # but doesn't cause any failures. Keep it simple and use the same
41 # flags in both cases.
42 # Note that the support to install headers from lib.mk is necessary
43 # when test Makefile is run directly with "make -C".
44 # When local build is done, headers are installed in the default
45 # INSTALL_HDR_PATH usr/include.
46 .PHONY: khdr
47 khdr:
48 ifndef KSFT_KHDR_INSTALL_DONE
49 ifeq (1,$(DEFAULT_INSTALL_HDR_PATH))
50         $(MAKE) --no-builtin-rules ARCH=$(ARCH) -C $(top_srcdir) headers_install
51 else
52         $(MAKE) --no-builtin-rules INSTALL_HDR_PATH=$$OUTPUT/usr \
53                 ARCH=$(ARCH) -C $(top_srcdir) headers_install
54 endif
55 endif
56
57 all: khdr $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
58 else
59 all: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
60 endif
61
62 define RUN_TESTS
63         BASE_DIR="$(selfdir)";                  \
64         . $(selfdir)/kselftest/runner.sh;       \
65         if [ "X$(summary)" != "X" ]; then       \
66                 per_test_logging=1;             \
67         fi;                                     \
68         run_many $(1)
69 endef
70
71 run_tests: all
72 ifdef building_out_of_srctree
73         @if [ "X$(TEST_PROGS)$(TEST_PROGS_EXTENDED)$(TEST_FILES)" != "X" ]; then \
74                 rsync -aq $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(OUTPUT); \
75         fi
76         @if [ "X$(TEST_PROGS)" != "X" ]; then \
77                 $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(OUTPUT)/$(TEST_PROGS)) ; \
78         else \
79                 $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS)); \
80         fi
81 else
82         @$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS))
83 endif
84
85 define INSTALL_SINGLE_RULE
86         $(if $(INSTALL_LIST),@mkdir -p $(INSTALL_PATH))
87         $(if $(INSTALL_LIST),rsync -a $(INSTALL_LIST) $(INSTALL_PATH)/)
88 endef
89
90 define INSTALL_RULE
91         $(eval INSTALL_LIST = $(TEST_PROGS)) $(INSTALL_SINGLE_RULE)
92         $(eval INSTALL_LIST = $(TEST_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE)
93         $(eval INSTALL_LIST = $(TEST_FILES)) $(INSTALL_SINGLE_RULE)
94         $(eval INSTALL_LIST = $(TEST_GEN_PROGS)) $(INSTALL_SINGLE_RULE)
95         $(eval INSTALL_LIST = $(TEST_CUSTOM_PROGS)) $(INSTALL_SINGLE_RULE)
96         $(eval INSTALL_LIST = $(TEST_GEN_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE)
97         $(eval INSTALL_LIST = $(TEST_GEN_FILES)) $(INSTALL_SINGLE_RULE)
98 endef
99
100 install: all
101 ifdef INSTALL_PATH
102         $(INSTALL_RULE)
103 else
104         $(error Error: set INSTALL_PATH to use install)
105 endif
106
107 emit_tests:
108         for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \
109                 BASENAME_TEST=`basename $$TEST`;        \
110                 echo "$(COLLECTION):$$BASENAME_TEST";   \
111         done
112
113 # define if isn't already. It is undefined in make O= case.
114 ifeq ($(RM),)
115 RM := rm -f
116 endif
117
118 define CLEAN
119         $(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
120 endef
121
122 clean:
123         $(CLEAN)
124
125 # When make O= with kselftest target from main level
126 # the following aren't defined.
127 #
128 ifdef building_out_of_srctree
129 LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
130 COMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
131 LINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
132 endif
133
134 # Selftest makefiles can override those targets by setting
135 # OVERRIDE_TARGETS = 1.
136 ifeq ($(OVERRIDE_TARGETS),)
137 LOCAL_HDRS := $(selfdir)/kselftest_harness.h $(selfdir)/kselftest.h
138 $(OUTPUT)/%:%.c $(LOCAL_HDRS)
139         $(LINK.c) $(filter-out $(LOCAL_HDRS),$^) $(LDLIBS) -o $@
140
141 $(OUTPUT)/%.o:%.S
142         $(COMPILE.S) $^ -o $@
143
144 $(OUTPUT)/%:%.S
145         $(LINK.S) $^ $(LDLIBS) -o $@
146 endif
147
148 .PHONY: run_tests all clean install emit_tests