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