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