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