1# SPDX-License-Identifier: GPL-2.0
2TARGETS = arm64
3TARGETS += bpf
4TARGETS += breakpoints
5TARGETS += capabilities
6TARGETS += cgroup
7TARGETS += clone3
8TARGETS += core
9TARGETS += cpufreq
10TARGETS += cpu-hotplug
11TARGETS += drivers/dma-buf
12TARGETS += efivarfs
13TARGETS += exec
14TARGETS += filesystems
15TARGETS += filesystems/binderfs
16TARGETS += filesystems/epoll
17TARGETS += firmware
18TARGETS += fpu
19TARGETS += ftrace
20TARGETS += futex
21TARGETS += gpio
22TARGETS += intel_pstate
23TARGETS += ipc
24TARGETS += ir
25TARGETS += kcmp
26TARGETS += kexec
27TARGETS += kvm
28TARGETS += landlock
29TARGETS += lib
30TARGETS += livepatch
31TARGETS += lkdtm
32TARGETS += membarrier
33TARGETS += memfd
34TARGETS += memory-hotplug
35TARGETS += mincore
36TARGETS += mount
37TARGETS += mount_setattr
38TARGETS += mqueue
39TARGETS += nci
40TARGETS += net
41TARGETS += net/forwarding
42TARGETS += net/mptcp
43TARGETS += netfilter
44TARGETS += nsfs
45TARGETS += pidfd
46TARGETS += pid_namespace
47TARGETS += powerpc
48TARGETS += proc
49TARGETS += pstore
50TARGETS += ptrace
51TARGETS += openat2
52TARGETS += rseq
53TARGETS += rtc
54TARGETS += seccomp
55TARGETS += sgx
56TARGETS += sigaltstack
57TARGETS += size
58TARGETS += sparc64
59TARGETS += splice
60TARGETS += static_keys
61TARGETS += sync
62TARGETS += syscall_user_dispatch
63TARGETS += sysctl
64TARGETS += tc-testing
65TARGETS += timens
66ifneq (1, $(quicktest))
67TARGETS += timers
68endif
69TARGETS += tmpfs
70TARGETS += tpm2
71TARGETS += user
72TARGETS += vDSO
73TARGETS += vm
74TARGETS += x86
75TARGETS += zram
76#Please keep the TARGETS list alphabetically sorted
77# Run "make quicktest=1 run_tests" or
78# "make quicktest=1 kselftest" from top level Makefile
79
80TARGETS_HOTPLUG = cpu-hotplug
81TARGETS_HOTPLUG += memory-hotplug
82
83# User can optionally provide a TARGETS skiplist.  By default we skip
84# BPF since it has cutting edge build time dependencies which require
85# more effort to install.
86SKIP_TARGETS ?= bpf
87ifneq ($(SKIP_TARGETS),)
88	TMP := $(filter-out $(SKIP_TARGETS), $(TARGETS))
89	override TARGETS := $(TMP)
90endif
91
92# User can set FORCE_TARGETS to 1 to require all targets to be successfully
93# built; make will fail if any of the targets cannot be built. If
94# FORCE_TARGETS is not set (the default), make will succeed if at least one
95# of the targets gets built.
96FORCE_TARGETS ?=
97
98# Clear LDFLAGS and MAKEFLAGS when implicit rules are missing.  This provides
99# implicit rules to sub-test Makefiles which avoids build failures in test
100# Makefile that don't have explicit build rules.
101ifeq (,$(LINK.c))
102override LDFLAGS =
103override MAKEFLAGS =
104endif
105
106# Append kselftest to KBUILD_OUTPUT and O to avoid cluttering
107# KBUILD_OUTPUT with selftest objects and headers installed
108# by selftests Makefile or lib.mk.
109ifdef building_out_of_srctree
110override LDFLAGS =
111endif
112
113ifneq ($(O),)
114	BUILD := $(O)/kselftest
115else
116	ifneq ($(KBUILD_OUTPUT),)
117		BUILD := $(KBUILD_OUTPUT)/kselftest
118	else
119		BUILD := $(shell pwd)
120		DEFAULT_INSTALL_HDR_PATH := 1
121	endif
122endif
123
124# Prepare for headers install
125top_srcdir ?= ../../..
126include $(top_srcdir)/scripts/subarch.include
127ARCH           ?= $(SUBARCH)
128export KSFT_KHDR_INSTALL_DONE := 1
129export BUILD
130
131# set default goal to all, so make without a target runs all, even when
132# all isn't the first target in the file.
133.DEFAULT_GOAL := all
134
135# Install headers here once for all tests. KSFT_KHDR_INSTALL_DONE
136# is used to avoid running headers_install from lib.mk.
137# Invoke headers install with --no-builtin-rules to avoid circular
138# dependency in "make kselftest" case. In this case, second level
139# make inherits builtin-rules which will use the rule generate
140# Makefile.o and runs into
141# "Circular Makefile.o <- prepare dependency dropped."
142# and headers_install fails and test compile fails.
143#
144# O= KBUILD_OUTPUT cases don't run into this error, since main Makefile
145# invokes them as sub-makes and --no-builtin-rules is not necessary,
146# but doesn't cause any failures. Keep it simple and use the same
147# flags in both cases.
148# Local build cases: "make kselftest", "make -C" - headers are installed
149# in the default INSTALL_HDR_PATH usr/include.
150khdr:
151ifeq (1,$(DEFAULT_INSTALL_HDR_PATH))
152	$(MAKE) --no-builtin-rules ARCH=$(ARCH) -C $(top_srcdir) headers_install
153else
154	$(MAKE) --no-builtin-rules INSTALL_HDR_PATH=$$BUILD/usr \
155		ARCH=$(ARCH) -C $(top_srcdir) headers_install
156endif
157
158all: khdr
159	@ret=1;							\
160	for TARGET in $(TARGETS); do				\
161		BUILD_TARGET=$$BUILD/$$TARGET;			\
162		mkdir $$BUILD_TARGET  -p;			\
163		$(MAKE) OUTPUT=$$BUILD_TARGET -C $$TARGET	\
164				$(if $(FORCE_TARGETS),|| exit);	\
165		ret=$$((ret * $$?));				\
166	done; exit $$ret;
167
168run_tests: all
169	@for TARGET in $(TARGETS); do \
170		BUILD_TARGET=$$BUILD/$$TARGET;	\
171		$(MAKE) OUTPUT=$$BUILD_TARGET -C $$TARGET run_tests;\
172	done;
173
174hotplug:
175	@for TARGET in $(TARGETS_HOTPLUG); do \
176		BUILD_TARGET=$$BUILD/$$TARGET;	\
177		$(MAKE) OUTPUT=$$BUILD_TARGET -C $$TARGET;\
178	done;
179
180run_hotplug: hotplug
181	@for TARGET in $(TARGETS_HOTPLUG); do \
182		BUILD_TARGET=$$BUILD/$$TARGET;	\
183		$(MAKE) OUTPUT=$$BUILD_TARGET -C $$TARGET run_full_test;\
184	done;
185
186clean_hotplug:
187	@for TARGET in $(TARGETS_HOTPLUG); do \
188		BUILD_TARGET=$$BUILD/$$TARGET;	\
189		$(MAKE) OUTPUT=$$BUILD_TARGET -C $$TARGET clean;\
190	done;
191
192run_pstore_crash:
193	$(MAKE) -C pstore run_crash
194
195# Use $BUILD as the default install root. $BUILD points to the
196# right output location for the following cases:
197# 1. output_dir=kernel_src
198# 2. a separate output directory is specified using O= KBUILD_OUTPUT
199# 3. a separate output directory is specified using KBUILD_OUTPUT
200# Avoid conflict with INSTALL_PATH set by the main Makefile
201#
202KSFT_INSTALL_PATH ?= $(BUILD)/kselftest_install
203KSFT_INSTALL_PATH := $(abspath $(KSFT_INSTALL_PATH))
204# Avoid changing the rest of the logic here and lib.mk.
205INSTALL_PATH := $(KSFT_INSTALL_PATH)
206ALL_SCRIPT := $(INSTALL_PATH)/run_kselftest.sh
207TEST_LIST := $(INSTALL_PATH)/kselftest-list.txt
208
209install: all
210ifdef INSTALL_PATH
211	@# Ask all targets to install their files
212	mkdir -p $(INSTALL_PATH)/kselftest
213	install -m 744 kselftest/module.sh $(INSTALL_PATH)/kselftest/
214	install -m 744 kselftest/runner.sh $(INSTALL_PATH)/kselftest/
215	install -m 744 kselftest/prefix.pl $(INSTALL_PATH)/kselftest/
216	install -m 744 run_kselftest.sh $(INSTALL_PATH)/
217	rm -f $(TEST_LIST)
218	@ret=1;	\
219	for TARGET in $(TARGETS); do \
220		BUILD_TARGET=$$BUILD/$$TARGET;	\
221		$(MAKE) OUTPUT=$$BUILD_TARGET -C $$TARGET INSTALL_PATH=$(INSTALL_PATH)/$$TARGET install \
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 -n "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: khdr all run_tests hotplug run_hotplug clean_hotplug run_pstore_crash install clean gen_tar
256