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