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