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