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