xref: /openbmc/linux/tools/testing/selftests/Makefile (revision ec62a746)
1# SPDX-License-Identifier: GPL-2.0
2TARGETS += alsa
3TARGETS += amd-pstate
4TARGETS += arm64
5TARGETS += bpf
6TARGETS += breakpoints
7TARGETS += cachestat
8TARGETS += capabilities
9TARGETS += cgroup
10TARGETS += clone3
11TARGETS += core
12TARGETS += cpufreq
13TARGETS += cpu-hotplug
14TARGETS += damon
15TARGETS += drivers/dma-buf
16TARGETS += drivers/s390x/uvdevice
17TARGETS += drivers/net/bonding
18TARGETS += drivers/net/team
19TARGETS += efivarfs
20TARGETS += exec
21TARGETS += filesystems
22TARGETS += filesystems/binderfs
23TARGETS += filesystems/epoll
24TARGETS += filesystems/fat
25TARGETS += firmware
26TARGETS += fpu
27TARGETS += ftrace
28TARGETS += futex
29TARGETS += gpio
30TARGETS += hid
31TARGETS += intel_pstate
32TARGETS += iommu
33TARGETS += ipc
34TARGETS += ir
35TARGETS += kcmp
36TARGETS += kexec
37TARGETS += kvm
38TARGETS += landlock
39TARGETS += lib
40TARGETS += livepatch
41TARGETS += lkdtm
42TARGETS += membarrier
43TARGETS += memfd
44TARGETS += memory-hotplug
45TARGETS += mincore
46TARGETS += mount
47TARGETS += mount_setattr
48TARGETS += move_mount_set_group
49TARGETS += mqueue
50TARGETS += nci
51TARGETS += net
52TARGETS += net/af_unix
53TARGETS += net/forwarding
54TARGETS += net/hsr
55TARGETS += net/mptcp
56TARGETS += net/openvswitch
57TARGETS += netfilter
58TARGETS += nsfs
59TARGETS += pidfd
60TARGETS += pid_namespace
61TARGETS += powerpc
62TARGETS += prctl
63TARGETS += proc
64TARGETS += pstore
65TARGETS += ptrace
66TARGETS += openat2
67TARGETS += resctrl
68TARGETS += riscv
69TARGETS += rlimits
70TARGETS += rseq
71TARGETS += rtc
72TARGETS += seccomp
73TARGETS += sgx
74TARGETS += sigaltstack
75TARGETS += size
76TARGETS += sparc64
77TARGETS += splice
78TARGETS += static_keys
79TARGETS += sync
80TARGETS += syscall_user_dispatch
81TARGETS += sysctl
82TARGETS += tc-testing
83TARGETS += tdx
84TARGETS += timens
85ifneq (1, $(quicktest))
86TARGETS += timers
87endif
88TARGETS += tmpfs
89TARGETS += tpm2
90TARGETS += tty
91TARGETS += user
92TARGETS += vDSO
93TARGETS += mm
94TARGETS += x86
95TARGETS += zram
96#Please keep the TARGETS list alphabetically sorted
97# Run "make quicktest=1 run_tests" or
98# "make quicktest=1 kselftest" from top level Makefile
99
100TARGETS_HOTPLUG = cpu-hotplug
101TARGETS_HOTPLUG += memory-hotplug
102
103# User can optionally provide a TARGETS skiplist.  By default we skip
104# BPF since it has cutting edge build time dependencies which require
105# more effort to install.
106SKIP_TARGETS ?= bpf
107ifneq ($(SKIP_TARGETS),)
108	TMP := $(filter-out $(SKIP_TARGETS), $(TARGETS))
109	override TARGETS := $(TMP)
110endif
111
112# User can set FORCE_TARGETS to 1 to require all targets to be successfully
113# built; make will fail if any of the targets cannot be built. If
114# FORCE_TARGETS is not set (the default), make will succeed if at least one
115# of the targets gets built.
116FORCE_TARGETS ?=
117
118# Clear LDFLAGS and MAKEFLAGS when implicit rules are missing.  This provides
119# implicit rules to sub-test Makefiles which avoids build failures in test
120# Makefile that don't have explicit build rules.
121ifeq (,$(LINK.c))
122override LDFLAGS =
123override MAKEFLAGS =
124endif
125
126# Append kselftest to KBUILD_OUTPUT and O to avoid cluttering
127# KBUILD_OUTPUT with selftest objects and headers installed
128# by selftests Makefile or lib.mk.
129ifdef building_out_of_srctree
130override LDFLAGS =
131endif
132
133top_srcdir ?= ../../..
134
135ifeq ("$(origin O)", "command line")
136  KBUILD_OUTPUT := $(O)
137endif
138
139ifneq ($(KBUILD_OUTPUT),)
140  # Make's built-in functions such as $(abspath ...), $(realpath ...) cannot
141  # expand a shell special character '~'. We use a somewhat tedious way here.
142  abs_objtree := $(shell cd $(top_srcdir) && mkdir -p $(KBUILD_OUTPUT) && cd $(KBUILD_OUTPUT) && pwd)
143  $(if $(abs_objtree),, \
144    $(error failed to create output directory "$(KBUILD_OUTPUT)"))
145  # $(realpath ...) resolves symlinks
146  abs_objtree := $(realpath $(abs_objtree))
147  BUILD := $(abs_objtree)/kselftest
148  KHDR_INCLUDES := -isystem ${abs_objtree}/usr/include
149  KHDR_DIR := ${abs_objtree}/usr/include
150else
151  BUILD := $(CURDIR)
152  abs_srctree := $(shell cd $(top_srcdir) && pwd)
153  KHDR_INCLUDES := -isystem ${abs_srctree}/usr/include
154  KHDR_DIR := ${abs_srctree}/usr/include
155  DEFAULT_INSTALL_HDR_PATH := 1
156endif
157
158# Prepare for headers install
159include $(top_srcdir)/scripts/subarch.include
160ARCH           ?= $(SUBARCH)
161export BUILD
162export KHDR_INCLUDES
163
164# set default goal to all, so make without a target runs all, even when
165# all isn't the first target in the file.
166.DEFAULT_GOAL := all
167
168all: kernel_header_files
169	@ret=1;							\
170	for TARGET in $(TARGETS); do				\
171		BUILD_TARGET=$$BUILD/$$TARGET;			\
172		mkdir $$BUILD_TARGET  -p;			\
173		$(MAKE) OUTPUT=$$BUILD_TARGET -C $$TARGET	\
174				O=$(abs_objtree)		\
175				$(if $(FORCE_TARGETS),|| exit);	\
176		ret=$$((ret * $$?));				\
177	done; exit $$ret;
178
179kernel_header_files:
180	@ls $(KHDR_DIR)/linux/*.h >/dev/null 2>/dev/null;                          \
181	if [ $$? -ne 0 ]; then                                                     \
182            RED='\033[1;31m';                                                  \
183            NOCOLOR='\033[0m';                                                 \
184            echo;                                                              \
185            echo -e "$${RED}error$${NOCOLOR}: missing kernel header files.";   \
186            echo "Please run this and try again:";                             \
187            echo;                                                              \
188            echo "    cd $(top_srcdir)";                                       \
189            echo "    make headers";                                           \
190            echo;                                                              \
191	    exit 1;                                                                \
192	fi
193
194.PHONY: kernel_header_files
195
196run_tests: all
197	@for TARGET in $(TARGETS); do \
198		BUILD_TARGET=$$BUILD/$$TARGET;	\
199		$(MAKE) OUTPUT=$$BUILD_TARGET -C $$TARGET run_tests \
200				O=$(abs_objtree);		    \
201	done;
202
203hotplug:
204	@for TARGET in $(TARGETS_HOTPLUG); do \
205		BUILD_TARGET=$$BUILD/$$TARGET;	\
206		$(MAKE) OUTPUT=$$BUILD_TARGET -C $$TARGET;\
207	done;
208
209run_hotplug: hotplug
210	@for TARGET in $(TARGETS_HOTPLUG); do \
211		BUILD_TARGET=$$BUILD/$$TARGET;	\
212		$(MAKE) OUTPUT=$$BUILD_TARGET -C $$TARGET run_full_test;\
213	done;
214
215clean_hotplug:
216	@for TARGET in $(TARGETS_HOTPLUG); do \
217		BUILD_TARGET=$$BUILD/$$TARGET;	\
218		$(MAKE) OUTPUT=$$BUILD_TARGET -C $$TARGET clean;\
219	done;
220
221run_pstore_crash:
222	$(MAKE) -C pstore run_crash
223
224# Use $BUILD as the default install root. $BUILD points to the
225# right output location for the following cases:
226# 1. output_dir=kernel_src
227# 2. a separate output directory is specified using O= KBUILD_OUTPUT
228# 3. a separate output directory is specified using KBUILD_OUTPUT
229# Avoid conflict with INSTALL_PATH set by the main Makefile
230#
231KSFT_INSTALL_PATH ?= $(BUILD)/kselftest_install
232KSFT_INSTALL_PATH := $(abspath $(KSFT_INSTALL_PATH))
233# Avoid changing the rest of the logic here and lib.mk.
234INSTALL_PATH := $(KSFT_INSTALL_PATH)
235ALL_SCRIPT := $(INSTALL_PATH)/run_kselftest.sh
236TEST_LIST := $(INSTALL_PATH)/kselftest-list.txt
237
238install: all
239ifdef INSTALL_PATH
240	@# Ask all targets to install their files
241	mkdir -p $(INSTALL_PATH)/kselftest
242	install -m 744 kselftest/module.sh $(INSTALL_PATH)/kselftest/
243	install -m 744 kselftest/runner.sh $(INSTALL_PATH)/kselftest/
244	install -m 744 kselftest/prefix.pl $(INSTALL_PATH)/kselftest/
245	install -m 744 run_kselftest.sh $(INSTALL_PATH)/
246	rm -f $(TEST_LIST)
247	@ret=1;	\
248	for TARGET in $(TARGETS); do \
249		BUILD_TARGET=$$BUILD/$$TARGET;	\
250		$(MAKE) OUTPUT=$$BUILD_TARGET -C $$TARGET INSTALL_PATH=$(INSTALL_PATH)/$$TARGET install \
251				O=$(abs_objtree)		\
252				$(if $(FORCE_TARGETS),|| exit);	\
253		ret=$$((ret * $$?));		\
254	done; exit $$ret;
255
256
257	@# Ask all targets to emit their test scripts
258	@# While building kselftest-list.text skip also non-existent TARGET dirs:
259	@# they could be the result of a build failure and should NOT be
260	@# included in the generated runlist.
261	for TARGET in $(TARGETS); do \
262		BUILD_TARGET=$$BUILD/$$TARGET;	\
263		[ ! -d $(INSTALL_PATH)/$$TARGET ] && printf "Skipping non-existent dir: $$TARGET\n" && continue; \
264		printf "Emit Tests for $$TARGET\n"; \
265		$(MAKE) -s --no-print-directory OUTPUT=$$BUILD_TARGET COLLECTION=$$TARGET \
266			-C $$TARGET emit_tests >> $(TEST_LIST); \
267	done;
268else
269	$(error Error: set INSTALL_PATH to use install)
270endif
271
272FORMAT ?= .gz
273TAR_PATH = $(abspath ${INSTALL_PATH}/kselftest-packages/kselftest.tar${FORMAT})
274gen_tar: install
275	@mkdir -p ${INSTALL_PATH}/kselftest-packages/
276	@tar caf ${TAR_PATH} --exclude=kselftest-packages -C ${INSTALL_PATH} .
277	@echo "Created ${TAR_PATH}"
278
279clean:
280	@for TARGET in $(TARGETS); do \
281		BUILD_TARGET=$$BUILD/$$TARGET;	\
282		$(MAKE) OUTPUT=$$BUILD_TARGET -C $$TARGET clean;\
283	done;
284
285.PHONY: all run_tests hotplug run_hotplug clean_hotplug run_pstore_crash install clean gen_tar
286