xref: /linux/tools/testing/selftests/Makefile (revision 2324d50d051ec0f14a548e78554fb02513d6dcef)
1# SPDX-License-Identifier: GPL-2.0
2TARGETS = android
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 += lib
30TARGETS += livepatch
31TARGETS += lkdtm
32TARGETS += membarrier
33TARGETS += memfd
34TARGETS += memory-hotplug
35TARGETS += mount
36TARGETS += mqueue
37TARGETS += net
38TARGETS += net/forwarding
39TARGETS += net/mptcp
40TARGETS += netfilter
41TARGETS += nsfs
42TARGETS += pidfd
43TARGETS += pid_namespace
44TARGETS += powerpc
45TARGETS += proc
46TARGETS += pstore
47TARGETS += ptrace
48TARGETS += openat2
49TARGETS += rseq
50TARGETS += rtc
51TARGETS += seccomp
52TARGETS += sigaltstack
53TARGETS += size
54TARGETS += sparc64
55TARGETS += splice
56TARGETS += static_keys
57TARGETS += sync
58TARGETS += sysctl
59TARGETS += timens
60ifneq (1, $(quicktest))
61TARGETS += timers
62endif
63TARGETS += tmpfs
64TARGETS += tpm2
65TARGETS += user
66TARGETS += vm
67TARGETS += x86
68TARGETS += zram
69#Please keep the TARGETS list alphabetically sorted
70# Run "make quicktest=1 run_tests" or
71# "make quicktest=1 kselftest" from top level Makefile
72
73TARGETS_HOTPLUG = cpu-hotplug
74TARGETS_HOTPLUG += memory-hotplug
75
76# User can optionally provide a TARGETS skiplist.
77SKIP_TARGETS ?=
78ifneq ($(SKIP_TARGETS),)
79	TMP := $(filter-out $(SKIP_TARGETS), $(TARGETS))
80	override TARGETS := $(TMP)
81endif
82
83# User can set FORCE_TARGETS to 1 to require all targets to be successfully
84# built; make will fail if any of the targets cannot be built. If
85# FORCE_TARGETS is not set (the default), make will succeed if at least one
86# of the targets gets built.
87FORCE_TARGETS ?=
88
89# Clear LDFLAGS and MAKEFLAGS if called from main
90# Makefile to avoid test build failures when test
91# Makefile doesn't have explicit build rules.
92ifeq (1,$(MAKELEVEL))
93override LDFLAGS =
94override MAKEFLAGS =
95endif
96
97# Append kselftest to KBUILD_OUTPUT and O to avoid cluttering
98# KBUILD_OUTPUT with selftest objects and headers installed
99# by selftests Makefile or lib.mk.
100ifdef building_out_of_srctree
101override LDFLAGS =
102endif
103
104ifneq ($(O),)
105	BUILD := $(O)/kselftest
106else
107	ifneq ($(KBUILD_OUTPUT),)
108		BUILD := $(KBUILD_OUTPUT)/kselftest
109	else
110		BUILD := $(shell pwd)
111		DEFAULT_INSTALL_HDR_PATH := 1
112	endif
113endif
114
115# Prepare for headers install
116top_srcdir ?= ../../..
117include $(top_srcdir)/scripts/subarch.include
118ARCH           ?= $(SUBARCH)
119export KSFT_KHDR_INSTALL_DONE := 1
120export BUILD
121
122# build and run gpio when output directory is the src dir.
123# gpio has dependency on tools/gpio and builds tools/gpio
124# objects in the src directory in all cases making the src
125# repo dirty even when objects are relocated.
126ifneq (1,$(DEFAULT_INSTALL_HDR_PATH))
127	TMP := $(filter-out gpio, $(TARGETS))
128	TARGETS := $(TMP)
129endif
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
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	@ret=1;	\
216	for TARGET in $(TARGETS); do \
217		BUILD_TARGET=$$BUILD/$$TARGET;	\
218		$(MAKE) OUTPUT=$$BUILD_TARGET -C $$TARGET INSTALL_PATH=$(INSTALL_PATH)/$$TARGET install \
219				$(if $(FORCE_TARGETS),|| exit);	\
220		ret=$$((ret * $$?));		\
221	done; exit $$ret;
222
223	@# Ask all targets to emit their test scripts
224	echo "#!/bin/sh" > $(ALL_SCRIPT)
225	echo "BASE_DIR=\$$(realpath \$$(dirname \$$0))" >> $(ALL_SCRIPT)
226	echo "cd \$$BASE_DIR" >> $(ALL_SCRIPT)
227	echo ". ./kselftest/runner.sh" >> $(ALL_SCRIPT)
228	echo "ROOT=\$$PWD" >> $(ALL_SCRIPT)
229	echo "if [ \"\$$1\" = \"--summary\" ]; then" >> $(ALL_SCRIPT)
230	echo "  logfile=\$$BASE_DIR/output.log" >> $(ALL_SCRIPT)
231	echo "  cat /dev/null > \$$logfile" >> $(ALL_SCRIPT)
232	echo "fi" >> $(ALL_SCRIPT)
233
234	@# While building run_kselftest.sh skip also non-existent TARGET dirs:
235	@# they could be the result of a build failure and should NOT be
236	@# included in the generated runlist.
237	for TARGET in $(TARGETS); do \
238		BUILD_TARGET=$$BUILD/$$TARGET;	\
239		[ ! -d $(INSTALL_PATH)/$$TARGET ] && echo "Skipping non-existent dir: $$TARGET" && continue; \
240		echo "[ -w /dev/kmsg ] && echo \"kselftest: Running tests in $$TARGET\" >> /dev/kmsg" >> $(ALL_SCRIPT); \
241		echo "cd $$TARGET" >> $(ALL_SCRIPT); \
242		echo -n "run_many" >> $(ALL_SCRIPT); \
243		echo -n "Emit Tests for $$TARGET\n"; \
244		$(MAKE) -s --no-print-directory OUTPUT=$$BUILD_TARGET -C $$TARGET emit_tests >> $(ALL_SCRIPT); \
245		echo "" >> $(ALL_SCRIPT);	    \
246		echo "cd \$$ROOT" >> $(ALL_SCRIPT); \
247	done;
248
249	chmod u+x $(ALL_SCRIPT)
250else
251	$(error Error: set INSTALL_PATH to use install)
252endif
253
254FORMAT ?= .gz
255TAR_PATH = $(abspath ${INSTALL_PATH}/kselftest-packages/kselftest.tar${FORMAT})
256gen_tar: install
257	@mkdir -p ${INSTALL_PATH}/kselftest-packages/
258	@tar caf ${TAR_PATH} --exclude=kselftest-packages -C ${INSTALL_PATH} .
259	@echo "Created ${TAR_PATH}"
260
261clean:
262	@for TARGET in $(TARGETS); do \
263		BUILD_TARGET=$$BUILD/$$TARGET;	\
264		$(MAKE) OUTPUT=$$BUILD_TARGET -C $$TARGET clean;\
265	done;
266
267.PHONY: khdr all run_tests hotplug run_hotplug clean_hotplug run_pstore_crash install clean gen_tar
268