Merge git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next
[linux-2.6-microblaze.git] / tools / testing / selftests / Makefile
1 # SPDX-License-Identifier: GPL-2.0
2 TARGETS = android
3 TARGETS += arm64
4 TARGETS += bpf
5 TARGETS += breakpoints
6 TARGETS += capabilities
7 TARGETS += cgroup
8 TARGETS += clone3
9 TARGETS += core
10 TARGETS += cpufreq
11 TARGETS += cpu-hotplug
12 TARGETS += drivers/dma-buf
13 TARGETS += efivarfs
14 TARGETS += exec
15 TARGETS += filesystems
16 TARGETS += filesystems/binderfs
17 TARGETS += filesystems/epoll
18 TARGETS += firmware
19 TARGETS += fpu
20 TARGETS += ftrace
21 TARGETS += futex
22 TARGETS += gpio
23 TARGETS += intel_pstate
24 TARGETS += ipc
25 TARGETS += ir
26 TARGETS += kcmp
27 TARGETS += kexec
28 TARGETS += kvm
29 TARGETS += lib
30 TARGETS += livepatch
31 TARGETS += lkdtm
32 TARGETS += membarrier
33 TARGETS += memfd
34 TARGETS += memory-hotplug
35 TARGETS += mount
36 TARGETS += mqueue
37 TARGETS += net
38 TARGETS += net/forwarding
39 TARGETS += net/mptcp
40 TARGETS += netfilter
41 TARGETS += nsfs
42 TARGETS += pidfd
43 TARGETS += pid_namespace
44 TARGETS += powerpc
45 TARGETS += proc
46 TARGETS += pstore
47 TARGETS += ptrace
48 TARGETS += openat2
49 TARGETS += rseq
50 TARGETS += rtc
51 TARGETS += seccomp
52 TARGETS += sigaltstack
53 TARGETS += size
54 TARGETS += sparc64
55 TARGETS += splice
56 TARGETS += static_keys
57 TARGETS += sync
58 TARGETS += sysctl
59 TARGETS += tc-testing
60 TARGETS += timens
61 ifneq (1, $(quicktest))
62 TARGETS += timers
63 endif
64 TARGETS += tmpfs
65 TARGETS += tpm2
66 TARGETS += user
67 TARGETS += vm
68 TARGETS += x86
69 TARGETS += zram
70 #Please keep the TARGETS list alphabetically sorted
71 # Run "make quicktest=1 run_tests" or
72 # "make quicktest=1 kselftest" from top level Makefile
73
74 TARGETS_HOTPLUG = cpu-hotplug
75 TARGETS_HOTPLUG += memory-hotplug
76
77 # User can optionally provide a TARGETS skiplist.
78 SKIP_TARGETS ?=
79 ifneq ($(SKIP_TARGETS),)
80         TMP := $(filter-out $(SKIP_TARGETS), $(TARGETS))
81         override TARGETS := $(TMP)
82 endif
83
84 # User can set FORCE_TARGETS to 1 to require all targets to be successfully
85 # built; make will fail if any of the targets cannot be built. If
86 # FORCE_TARGETS is not set (the default), make will succeed if at least one
87 # of the targets gets built.
88 FORCE_TARGETS ?=
89
90 # Clear LDFLAGS and MAKEFLAGS if called from main
91 # Makefile to avoid test build failures when test
92 # Makefile doesn't have explicit build rules.
93 ifeq (1,$(MAKELEVEL))
94 override LDFLAGS =
95 override MAKEFLAGS =
96 endif
97
98 # Append kselftest to KBUILD_OUTPUT and O to avoid cluttering
99 # KBUILD_OUTPUT with selftest objects and headers installed
100 # by selftests Makefile or lib.mk.
101 ifdef building_out_of_srctree
102 override LDFLAGS =
103 endif
104
105 ifneq ($(O),)
106         BUILD := $(O)/kselftest
107 else
108         ifneq ($(KBUILD_OUTPUT),)
109                 BUILD := $(KBUILD_OUTPUT)/kselftest
110         else
111                 BUILD := $(shell pwd)
112                 DEFAULT_INSTALL_HDR_PATH := 1
113         endif
114 endif
115
116 # Prepare for headers install
117 top_srcdir ?= ../../..
118 include $(top_srcdir)/scripts/subarch.include
119 ARCH           ?= $(SUBARCH)
120 export KSFT_KHDR_INSTALL_DONE := 1
121 export BUILD
122
123 # build and run gpio when output directory is the src dir.
124 # gpio has dependency on tools/gpio and builds tools/gpio
125 # objects in the src directory in all cases making the src
126 # repo dirty even when objects are relocated.
127 ifneq (1,$(DEFAULT_INSTALL_HDR_PATH))
128         TMP := $(filter-out gpio, $(TARGETS))
129         TARGETS := $(TMP)
130 endif
131
132 # set default goal to all, so make without a target runs all, even when
133 # all isn't the first target in the file.
134 .DEFAULT_GOAL := all
135
136 # Install headers here once for all tests. KSFT_KHDR_INSTALL_DONE
137 # is used to avoid running headers_install from lib.mk.
138 # Invoke headers install with --no-builtin-rules to avoid circular
139 # dependency in "make kselftest" case. In this case, second level
140 # make inherits builtin-rules which will use the rule generate
141 # Makefile.o and runs into
142 # "Circular Makefile.o <- prepare dependency dropped."
143 # and headers_install fails and test compile fails.
144 #
145 # O= KBUILD_OUTPUT cases don't run into this error, since main Makefile
146 # invokes them as sub-makes and --no-builtin-rules is not necessary,
147 # but doesn't cause any failures. Keep it simple and use the same
148 # flags in both cases.
149 # Local build cases: "make kselftest", "make -C" - headers are installed
150 # in the default INSTALL_HDR_PATH usr/include.
151 khdr:
152 ifeq (1,$(DEFAULT_INSTALL_HDR_PATH))
153         $(MAKE) --no-builtin-rules ARCH=$(ARCH) -C $(top_srcdir) headers_install
154 else
155         $(MAKE) --no-builtin-rules INSTALL_HDR_PATH=$$BUILD/usr \
156                 ARCH=$(ARCH) -C $(top_srcdir) headers_install
157 endif
158
159 all: khdr
160         @ret=1;                                                 \
161         for TARGET in $(TARGETS); do                            \
162                 BUILD_TARGET=$$BUILD/$$TARGET;                  \
163                 mkdir $$BUILD_TARGET  -p;                       \
164                 $(MAKE) OUTPUT=$$BUILD_TARGET -C $$TARGET       \
165                                 $(if $(FORCE_TARGETS),|| exit); \
166                 ret=$$((ret * $$?));                            \
167         done; exit $$ret;
168
169 run_tests: all
170         @for TARGET in $(TARGETS); do \
171                 BUILD_TARGET=$$BUILD/$$TARGET;  \
172                 $(MAKE) OUTPUT=$$BUILD_TARGET -C $$TARGET run_tests;\
173         done;
174
175 hotplug:
176         @for TARGET in $(TARGETS_HOTPLUG); do \
177                 BUILD_TARGET=$$BUILD/$$TARGET;  \
178                 $(MAKE) OUTPUT=$$BUILD_TARGET -C $$TARGET;\
179         done;
180
181 run_hotplug: hotplug
182         @for TARGET in $(TARGETS_HOTPLUG); do \
183                 BUILD_TARGET=$$BUILD/$$TARGET;  \
184                 $(MAKE) OUTPUT=$$BUILD_TARGET -C $$TARGET run_full_test;\
185         done;
186
187 clean_hotplug:
188         @for TARGET in $(TARGETS_HOTPLUG); do \
189                 BUILD_TARGET=$$BUILD/$$TARGET;  \
190                 $(MAKE) OUTPUT=$$BUILD_TARGET -C $$TARGET clean;\
191         done;
192
193 run_pstore_crash:
194         $(MAKE) -C pstore run_crash
195
196 # Use $BUILD as the default install root. $BUILD points to the
197 # right output location for the following cases:
198 # 1. output_dir=kernel_src
199 # 2. a separate output directory is specified using O= KBUILD_OUTPUT
200 # 3. a separate output directory is specified using KBUILD_OUTPUT
201 # Avoid conflict with INSTALL_PATH set by the main Makefile
202 #
203 KSFT_INSTALL_PATH ?= $(BUILD)/kselftest_install
204 KSFT_INSTALL_PATH := $(abspath $(KSFT_INSTALL_PATH))
205 # Avoid changing the rest of the logic here and lib.mk.
206 INSTALL_PATH := $(KSFT_INSTALL_PATH)
207 ALL_SCRIPT := $(INSTALL_PATH)/run_kselftest.sh
208
209 install: all
210 ifdef INSTALL_PATH
211         @# Ask all targets to install their files
212         mkdir -p $(INSTALL_PATH)/kselftest
213         install -m 744 kselftest/module.sh $(INSTALL_PATH)/kselftest/
214         install -m 744 kselftest/runner.sh $(INSTALL_PATH)/kselftest/
215         install -m 744 kselftest/prefix.pl $(INSTALL_PATH)/kselftest/
216         @ret=1; \
217         for TARGET in $(TARGETS); do \
218                 BUILD_TARGET=$$BUILD/$$TARGET;  \
219                 $(MAKE) OUTPUT=$$BUILD_TARGET -C $$TARGET INSTALL_PATH=$(INSTALL_PATH)/$$TARGET install \
220                                 $(if $(FORCE_TARGETS),|| exit); \
221                 ret=$$((ret * $$?));            \
222         done; exit $$ret;
223
224         @# Ask all targets to emit their test scripts
225         echo "#!/bin/sh" > $(ALL_SCRIPT)
226         echo "BASE_DIR=\$$(realpath \$$(dirname \$$0))" >> $(ALL_SCRIPT)
227         echo "cd \$$BASE_DIR" >> $(ALL_SCRIPT)
228         echo ". ./kselftest/runner.sh" >> $(ALL_SCRIPT)
229         echo "ROOT=\$$PWD" >> $(ALL_SCRIPT)
230         echo "if [ \"\$$1\" = \"--summary\" ]; then" >> $(ALL_SCRIPT)
231         echo "  logfile=\$$BASE_DIR/output.log" >> $(ALL_SCRIPT)
232         echo "  cat /dev/null > \$$logfile" >> $(ALL_SCRIPT)
233         echo "fi" >> $(ALL_SCRIPT)
234
235         @# While building run_kselftest.sh skip also non-existent TARGET dirs:
236         @# they could be the result of a build failure and should NOT be
237         @# included in the generated runlist.
238         for TARGET in $(TARGETS); do \
239                 BUILD_TARGET=$$BUILD/$$TARGET;  \
240                 [ ! -d $(INSTALL_PATH)/$$TARGET ] && echo "Skipping non-existent dir: $$TARGET" && continue; \
241                 echo "[ -w /dev/kmsg ] && echo \"kselftest: Running tests in $$TARGET\" >> /dev/kmsg" >> $(ALL_SCRIPT); \
242                 echo "cd $$TARGET" >> $(ALL_SCRIPT); \
243                 echo -n "run_many" >> $(ALL_SCRIPT); \
244                 echo -n "Emit Tests for $$TARGET\n"; \
245                 $(MAKE) -s --no-print-directory OUTPUT=$$BUILD_TARGET -C $$TARGET emit_tests >> $(ALL_SCRIPT); \
246                 echo "" >> $(ALL_SCRIPT);           \
247                 echo "cd \$$ROOT" >> $(ALL_SCRIPT); \
248         done;
249
250         chmod u+x $(ALL_SCRIPT)
251 else
252         $(error Error: set INSTALL_PATH to use install)
253 endif
254
255 FORMAT ?= .gz
256 TAR_PATH = $(abspath ${INSTALL_PATH}/kselftest-packages/kselftest.tar${FORMAT})
257 gen_tar: install
258         @mkdir -p ${INSTALL_PATH}/kselftest-packages/
259         @tar caf ${TAR_PATH} --exclude=kselftest-packages -C ${INSTALL_PATH} .
260         @echo "Created ${TAR_PATH}"
261
262 clean:
263         @for TARGET in $(TARGETS); do \
264                 BUILD_TARGET=$$BUILD/$$TARGET;  \
265                 $(MAKE) OUTPUT=$$BUILD_TARGET -C $$TARGET clean;\
266         done;
267
268 .PHONY: khdr all run_tests hotplug run_hotplug clean_hotplug run_pstore_crash install clean gen_tar