xref: /linux/tools/testing/selftests/Makefile (revision 048d19d444be1e42abca19a6b969343954ae4e17)
1# SPDX-License-Identifier: GPL-2.0
2TARGETS = android
3TARGETS += arm64
4TARGETS += bpf
5TARGETS += breakpoints
6TARGETS += capabilities
7TARGETS += cgroup
8TARGETS += clone3
9TARGETS += cpufreq
10TARGETS += cpu-hotplug
11TARGETS += drivers/dma-buf
12TARGETS += efivarfs
13TARGETS += exec
14TARGETS += filesystems
15TARGETS += filesystems/binderfs
16TARGETS += filesystems/epoll
17TARGETS += firmware
18TARGETS += ftrace
19TARGETS += futex
20TARGETS += gpio
21TARGETS += intel_pstate
22TARGETS += ipc
23TARGETS += ir
24TARGETS += kcmp
25TARGETS += kexec
26TARGETS += kvm
27TARGETS += lib
28TARGETS += livepatch
29TARGETS += membarrier
30TARGETS += memfd
31TARGETS += memory-hotplug
32TARGETS += mount
33TARGETS += mqueue
34TARGETS += net
35TARGETS += net/mptcp
36TARGETS += netfilter
37TARGETS += networking/timestamping
38TARGETS += nsfs
39TARGETS += pidfd
40TARGETS += powerpc
41TARGETS += proc
42TARGETS += pstore
43TARGETS += ptrace
44TARGETS += rseq
45TARGETS += rtc
46TARGETS += seccomp
47TARGETS += sigaltstack
48TARGETS += size
49TARGETS += sparc64
50TARGETS += splice
51TARGETS += static_keys
52TARGETS += sync
53TARGETS += sysctl
54ifneq (1, $(quicktest))
55TARGETS += timers
56endif
57TARGETS += tmpfs
58TARGETS += tpm2
59TARGETS += user
60TARGETS += vm
61TARGETS += x86
62TARGETS += zram
63#Please keep the TARGETS list alphabetically sorted
64# Run "make quicktest=1 run_tests" or
65# "make quicktest=1 kselftest" from top level Makefile
66
67TARGETS_HOTPLUG = cpu-hotplug
68TARGETS_HOTPLUG += memory-hotplug
69
70# User can optionally provide a TARGETS skiplist.
71SKIP_TARGETS ?=
72ifneq ($(SKIP_TARGETS),)
73	TMP := $(filter-out $(SKIP_TARGETS), $(TARGETS))
74	override TARGETS := $(TMP)
75endif
76
77# Clear LDFLAGS and MAKEFLAGS if called from main
78# Makefile to avoid test build failures when test
79# Makefile doesn't have explicit build rules.
80ifeq (1,$(MAKELEVEL))
81override LDFLAGS =
82override MAKEFLAGS =
83endif
84
85# Append kselftest to KBUILD_OUTPUT to avoid cluttering
86# KBUILD_OUTPUT with selftest objects and headers installed
87# by selftests Makefile or lib.mk.
88ifdef building_out_of_srctree
89override LDFLAGS =
90endif
91
92ifneq ($(O),)
93	BUILD := $(O)
94else
95	ifneq ($(KBUILD_OUTPUT),)
96		BUILD := $(KBUILD_OUTPUT)/kselftest
97	else
98		BUILD := $(shell pwd)
99		DEFAULT_INSTALL_HDR_PATH := 1
100	endif
101endif
102
103# Prepare for headers install
104top_srcdir ?= ../../..
105include $(top_srcdir)/scripts/subarch.include
106ARCH           ?= $(SUBARCH)
107export KSFT_KHDR_INSTALL_DONE := 1
108export BUILD
109
110# build and run gpio when output directory is the src dir.
111# gpio has dependency on tools/gpio and builds tools/gpio
112# objects in the src directory in all cases making the src
113# repo dirty even when objects are relocated.
114ifneq (1,$(DEFAULT_INSTALL_HDR_PATH))
115	TMP := $(filter-out gpio, $(TARGETS))
116	TARGETS := $(TMP)
117endif
118
119# set default goal to all, so make without a target runs all, even when
120# all isn't the first target in the file.
121.DEFAULT_GOAL := all
122
123# Install headers here once for all tests. KSFT_KHDR_INSTALL_DONE
124# is used to avoid running headers_install from lib.mk.
125# Invoke headers install with --no-builtin-rules to avoid circular
126# dependency in "make kselftest" case. In this case, second level
127# make inherits builtin-rules which will use the rule generate
128# Makefile.o and runs into
129# "Circular Makefile.o <- prepare dependency dropped."
130# and headers_install fails and test compile fails.
131#
132# O= KBUILD_OUTPUT cases don't run into this error, since main Makefile
133# invokes them as sub-makes and --no-builtin-rules is not necessary,
134# but doesn't cause any failures. Keep it simple and use the same
135# flags in both cases.
136# Local build cases: "make kselftest", "make -C" - headers are installed
137# in the default INSTALL_HDR_PATH usr/include.
138khdr:
139ifeq (1,$(DEFAULT_INSTALL_HDR_PATH))
140	$(MAKE) --no-builtin-rules ARCH=$(ARCH) -C $(top_srcdir) headers_install
141else
142	$(MAKE) --no-builtin-rules INSTALL_HDR_PATH=$$BUILD/usr \
143		ARCH=$(ARCH) -C $(top_srcdir) headers_install
144endif
145
146all: khdr
147	@for TARGET in $(TARGETS); do		\
148		BUILD_TARGET=$$BUILD/$$TARGET;	\
149		mkdir $$BUILD_TARGET  -p;	\
150		$(MAKE) OUTPUT=$$BUILD_TARGET -C $$TARGET;\
151	done;
152
153run_tests: all
154	@for TARGET in $(TARGETS); do \
155		BUILD_TARGET=$$BUILD/$$TARGET;	\
156		$(MAKE) OUTPUT=$$BUILD_TARGET -C $$TARGET run_tests;\
157	done;
158
159hotplug:
160	@for TARGET in $(TARGETS_HOTPLUG); do \
161		BUILD_TARGET=$$BUILD/$$TARGET;	\
162		$(MAKE) OUTPUT=$$BUILD_TARGET -C $$TARGET;\
163	done;
164
165run_hotplug: hotplug
166	@for TARGET in $(TARGETS_HOTPLUG); do \
167		BUILD_TARGET=$$BUILD/$$TARGET;	\
168		$(MAKE) OUTPUT=$$BUILD_TARGET -C $$TARGET run_full_test;\
169	done;
170
171clean_hotplug:
172	@for TARGET in $(TARGETS_HOTPLUG); do \
173		BUILD_TARGET=$$BUILD/$$TARGET;	\
174		$(MAKE) OUTPUT=$$BUILD_TARGET -C $$TARGET clean;\
175	done;
176
177run_pstore_crash:
178	$(MAKE) -C pstore run_crash
179
180# Use $BUILD as the default install root. $BUILD points to the
181# right output location for the following cases:
182# 1. output_dir=kernel_src
183# 2. a separate output directory is specified using O= KBUILD_OUTPUT
184# 3. a separate output directory is specified using KBUILD_OUTPUT
185# Avoid conflict with INSTALL_PATH set by the main Makefile
186#
187KSFT_INSTALL_PATH ?= $(BUILD)/kselftest_install
188KSFT_INSTALL_PATH := $(abspath $(KSFT_INSTALL_PATH))
189# Avoid changing the rest of the logic here and lib.mk.
190INSTALL_PATH := $(KSFT_INSTALL_PATH)
191ALL_SCRIPT := $(INSTALL_PATH)/run_kselftest.sh
192
193install: all
194ifdef INSTALL_PATH
195	@# Ask all targets to install their files
196	mkdir -p $(INSTALL_PATH)/kselftest
197	install -m 744 kselftest/module.sh $(INSTALL_PATH)/kselftest/
198	install -m 744 kselftest/runner.sh $(INSTALL_PATH)/kselftest/
199	install -m 744 kselftest/prefix.pl $(INSTALL_PATH)/kselftest/
200	@for TARGET in $(TARGETS); do \
201		BUILD_TARGET=$$BUILD/$$TARGET;	\
202		$(MAKE) OUTPUT=$$BUILD_TARGET -C $$TARGET INSTALL_PATH=$(INSTALL_PATH)/$$TARGET install; \
203	done;
204
205	@# Ask all targets to emit their test scripts
206	echo "#!/bin/sh" > $(ALL_SCRIPT)
207	echo "BASE_DIR=\$$(realpath \$$(dirname \$$0))" >> $(ALL_SCRIPT)
208	echo "cd \$$BASE_DIR" >> $(ALL_SCRIPT)
209	echo ". ./kselftest/runner.sh" >> $(ALL_SCRIPT)
210	echo "ROOT=\$$PWD" >> $(ALL_SCRIPT)
211	echo "if [ \"\$$1\" = \"--summary\" ]; then" >> $(ALL_SCRIPT)
212	echo "  logfile=\$$BASE_DIR/output.log" >> $(ALL_SCRIPT)
213	echo "  cat /dev/null > \$$logfile" >> $(ALL_SCRIPT)
214	echo "fi" >> $(ALL_SCRIPT)
215
216	@# While building run_kselftest.sh skip also non-existent TARGET dirs:
217	@# they could be the result of a build failure and should NOT be
218	@# included in the generated runlist.
219	for TARGET in $(TARGETS); do \
220		BUILD_TARGET=$$BUILD/$$TARGET;	\
221		[ ! -d $(INSTALL_PATH)/$$TARGET ] && echo "Skipping non-existent dir: $$TARGET" && continue; \
222		echo "[ -w /dev/kmsg ] && echo \"kselftest: Running tests in $$TARGET\" >> /dev/kmsg" >> $(ALL_SCRIPT); \
223		echo "cd $$TARGET" >> $(ALL_SCRIPT); \
224		echo -n "run_many" >> $(ALL_SCRIPT); \
225		echo -n "Emit Tests for $$TARGET\n"; \
226		$(MAKE) -s --no-print-directory OUTPUT=$$BUILD_TARGET -C $$TARGET emit_tests >> $(ALL_SCRIPT); \
227		echo "" >> $(ALL_SCRIPT);	    \
228		echo "cd \$$ROOT" >> $(ALL_SCRIPT); \
229	done;
230
231	chmod u+x $(ALL_SCRIPT)
232else
233	$(error Error: set INSTALL_PATH to use install)
234endif
235
236clean:
237	@for TARGET in $(TARGETS); do \
238		BUILD_TARGET=$$BUILD/$$TARGET;	\
239		$(MAKE) OUTPUT=$$BUILD_TARGET -C $$TARGET clean;\
240	done;
241
242.PHONY: khdr all run_tests hotplug run_hotplug clean_hotplug run_pstore_crash install clean
243