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