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