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