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