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