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