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