xref: /linux/tools/testing/selftests/Makefile (revision da1d9caf95def6f0320819cf941c9fd1069ba9e1)
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 KSFT_KHDR_INSTALL_DONE := 1
147export BUILD
148export KHDR_INCLUDES
149
150# set default goal to all, so make without a target runs all, even when
151# all isn't the first target in the file.
152.DEFAULT_GOAL := all
153
154# Install headers here once for all tests. KSFT_KHDR_INSTALL_DONE
155# is used to avoid running headers_install from lib.mk.
156# Invoke headers install with --no-builtin-rules to avoid circular
157# dependency in "make kselftest" case. In this case, second level
158# make inherits builtin-rules which will use the rule generate
159# Makefile.o and runs into
160# "Circular Makefile.o <- prepare dependency dropped."
161# and headers_install fails and test compile fails.
162#
163# O= KBUILD_OUTPUT cases don't run into this error, since main Makefile
164# invokes them as sub-makes and --no-builtin-rules is not necessary,
165# but doesn't cause any failures. Keep it simple and use the same
166# flags in both cases.
167# Local build cases: "make kselftest", "make -C" - headers are installed
168# in the default INSTALL_HDR_PATH usr/include.
169khdr:
170ifeq (1,$(DEFAULT_INSTALL_HDR_PATH))
171	$(MAKE) --no-builtin-rules ARCH=$(ARCH) -C $(top_srcdir) headers_install
172else
173	$(MAKE) --no-builtin-rules INSTALL_HDR_PATH=$(abs_objtree)/usr \
174		ARCH=$(ARCH) -C $(top_srcdir) headers_install
175endif
176
177all: khdr
178	@ret=1;							\
179	for TARGET in $(TARGETS); do				\
180		BUILD_TARGET=$$BUILD/$$TARGET;			\
181		mkdir $$BUILD_TARGET  -p;			\
182		$(MAKE) OUTPUT=$$BUILD_TARGET -C $$TARGET	\
183				O=$(abs_objtree)		\
184				$(if $(FORCE_TARGETS),|| exit);	\
185		ret=$$((ret * $$?));				\
186	done; exit $$ret;
187
188run_tests: all
189	@for TARGET in $(TARGETS); do \
190		BUILD_TARGET=$$BUILD/$$TARGET;	\
191		$(MAKE) OUTPUT=$$BUILD_TARGET -C $$TARGET run_tests \
192				O=$(abs_objtree);		    \
193	done;
194
195hotplug:
196	@for TARGET in $(TARGETS_HOTPLUG); do \
197		BUILD_TARGET=$$BUILD/$$TARGET;	\
198		$(MAKE) OUTPUT=$$BUILD_TARGET -C $$TARGET;\
199	done;
200
201run_hotplug: hotplug
202	@for TARGET in $(TARGETS_HOTPLUG); do \
203		BUILD_TARGET=$$BUILD/$$TARGET;	\
204		$(MAKE) OUTPUT=$$BUILD_TARGET -C $$TARGET run_full_test;\
205	done;
206
207clean_hotplug:
208	@for TARGET in $(TARGETS_HOTPLUG); do \
209		BUILD_TARGET=$$BUILD/$$TARGET;	\
210		$(MAKE) OUTPUT=$$BUILD_TARGET -C $$TARGET clean;\
211	done;
212
213run_pstore_crash:
214	$(MAKE) -C pstore run_crash
215
216# Use $BUILD as the default install root. $BUILD points to the
217# right output location for the following cases:
218# 1. output_dir=kernel_src
219# 2. a separate output directory is specified using O= KBUILD_OUTPUT
220# 3. a separate output directory is specified using KBUILD_OUTPUT
221# Avoid conflict with INSTALL_PATH set by the main Makefile
222#
223KSFT_INSTALL_PATH ?= $(BUILD)/kselftest_install
224KSFT_INSTALL_PATH := $(abspath $(KSFT_INSTALL_PATH))
225# Avoid changing the rest of the logic here and lib.mk.
226INSTALL_PATH := $(KSFT_INSTALL_PATH)
227ALL_SCRIPT := $(INSTALL_PATH)/run_kselftest.sh
228TEST_LIST := $(INSTALL_PATH)/kselftest-list.txt
229
230install: all
231ifdef INSTALL_PATH
232	@# Ask all targets to install their files
233	mkdir -p $(INSTALL_PATH)/kselftest
234	install -m 744 kselftest/module.sh $(INSTALL_PATH)/kselftest/
235	install -m 744 kselftest/runner.sh $(INSTALL_PATH)/kselftest/
236	install -m 744 kselftest/prefix.pl $(INSTALL_PATH)/kselftest/
237	install -m 744 run_kselftest.sh $(INSTALL_PATH)/
238	rm -f $(TEST_LIST)
239	@ret=1;	\
240	for TARGET in $(TARGETS); do \
241		BUILD_TARGET=$$BUILD/$$TARGET;	\
242		$(MAKE) OUTPUT=$$BUILD_TARGET -C $$TARGET INSTALL_PATH=$(INSTALL_PATH)/$$TARGET install \
243				O=$(abs_objtree)		\
244				$(if $(FORCE_TARGETS),|| exit);	\
245		ret=$$((ret * $$?));		\
246	done; exit $$ret;
247
248
249	@# Ask all targets to emit their test scripts
250	@# While building kselftest-list.text skip also non-existent TARGET dirs:
251	@# they could be the result of a build failure and should NOT be
252	@# included in the generated runlist.
253	for TARGET in $(TARGETS); do \
254		BUILD_TARGET=$$BUILD/$$TARGET;	\
255		[ ! -d $(INSTALL_PATH)/$$TARGET ] && echo "Skipping non-existent dir: $$TARGET" && continue; \
256		echo -n "Emit Tests for $$TARGET\n"; \
257		$(MAKE) -s --no-print-directory OUTPUT=$$BUILD_TARGET COLLECTION=$$TARGET \
258			-C $$TARGET emit_tests >> $(TEST_LIST); \
259	done;
260else
261	$(error Error: set INSTALL_PATH to use install)
262endif
263
264FORMAT ?= .gz
265TAR_PATH = $(abspath ${INSTALL_PATH}/kselftest-packages/kselftest.tar${FORMAT})
266gen_tar: install
267	@mkdir -p ${INSTALL_PATH}/kselftest-packages/
268	@tar caf ${TAR_PATH} --exclude=kselftest-packages -C ${INSTALL_PATH} .
269	@echo "Created ${TAR_PATH}"
270
271clean:
272	@for TARGET in $(TARGETS); do \
273		BUILD_TARGET=$$BUILD/$$TARGET;	\
274		$(MAKE) OUTPUT=$$BUILD_TARGET -C $$TARGET clean;\
275	done;
276
277.PHONY: khdr all run_tests hotplug run_hotplug clean_hotplug run_pstore_crash install clean gen_tar
278