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