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 += netfilter 37TARGETS += networking/timestamping 38TARGETS += nsfs 39TARGETS += pidfd 40TARGETS += powerpc 41TARGETS += proc 42TARGETS += pstore 43TARGETS += ptrace 44TARGETS += rseq 45TARGETS += rtc 46TARGETS += seccomp 47TARGETS += sigaltstack 48TARGETS += size 49TARGETS += sparc64 50TARGETS += splice 51TARGETS += static_keys 52TARGETS += sync 53TARGETS += sysctl 54ifneq (1, $(quicktest)) 55TARGETS += timers 56endif 57TARGETS += tmpfs 58TARGETS += tpm2 59TARGETS += user 60TARGETS += vm 61TARGETS += x86 62TARGETS += zram 63#Please keep the TARGETS list alphabetically sorted 64# Run "make quicktest=1 run_tests" or 65# "make quicktest=1 kselftest" from top level Makefile 66 67TARGETS_HOTPLUG = cpu-hotplug 68TARGETS_HOTPLUG += memory-hotplug 69 70# User can optionally provide a TARGETS skiplist. 71SKIP_TARGETS ?= 72ifneq ($(SKIP_TARGETS),) 73 TMP := $(filter-out $(SKIP_TARGETS), $(TARGETS)) 74 override TARGETS := $(TMP) 75endif 76 77# Clear LDFLAGS and MAKEFLAGS if called from main 78# Makefile to avoid test build failures when test 79# Makefile doesn't have explicit build rules. 80ifeq (1,$(MAKELEVEL)) 81override LDFLAGS = 82override MAKEFLAGS = 83endif 84 85# Append kselftest to KBUILD_OUTPUT to avoid cluttering 86# KBUILD_OUTPUT with selftest objects and headers installed 87# by selftests Makefile or lib.mk. 88ifdef building_out_of_srctree 89override LDFLAGS = 90endif 91 92ifneq ($(O),) 93 BUILD := $(O) 94else 95 ifneq ($(KBUILD_OUTPUT),) 96 BUILD := $(KBUILD_OUTPUT)/kselftest 97 else 98 BUILD := $(shell pwd) 99 DEFAULT_INSTALL_HDR_PATH := 1 100 endif 101endif 102 103# Prepare for headers install 104top_srcdir ?= ../../.. 105include $(top_srcdir)/scripts/subarch.include 106ARCH ?= $(SUBARCH) 107export KSFT_KHDR_INSTALL_DONE := 1 108export BUILD 109 110# build and run gpio when output directory is the src dir. 111# gpio has dependency on tools/gpio and builds tools/gpio 112# objects in the src directory in all cases making the src 113# repo dirty even when objects are relocated. 114ifneq (1,$(DEFAULT_INSTALL_HDR_PATH)) 115 TMP := $(filter-out gpio, $(TARGETS)) 116 TARGETS := $(TMP) 117endif 118 119# set default goal to all, so make without a target runs all, even when 120# all isn't the first target in the file. 121.DEFAULT_GOAL := all 122 123# Install headers here once for all tests. KSFT_KHDR_INSTALL_DONE 124# is used to avoid running headers_install from lib.mk. 125# Invoke headers install with --no-builtin-rules to avoid circular 126# dependency in "make kselftest" case. In this case, second level 127# make inherits builtin-rules which will use the rule generate 128# Makefile.o and runs into 129# "Circular Makefile.o <- prepare dependency dropped." 130# and headers_install fails and test compile fails. 131# 132# O= KBUILD_OUTPUT cases don't run into this error, since main Makefile 133# invokes them as sub-makes and --no-builtin-rules is not necessary, 134# but doesn't cause any failures. Keep it simple and use the same 135# flags in both cases. 136# Local build cases: "make kselftest", "make -C" - headers are installed 137# in the default INSTALL_HDR_PATH usr/include. 138khdr: 139ifeq (1,$(DEFAULT_INSTALL_HDR_PATH)) 140 $(MAKE) --no-builtin-rules ARCH=$(ARCH) -C $(top_srcdir) headers_install 141else 142 $(MAKE) --no-builtin-rules INSTALL_HDR_PATH=$$BUILD/usr \ 143 ARCH=$(ARCH) -C $(top_srcdir) headers_install 144endif 145 146all: khdr 147 @ret=1; \ 148 for TARGET in $(TARGETS); do \ 149 BUILD_TARGET=$$BUILD/$$TARGET; \ 150 mkdir $$BUILD_TARGET -p; \ 151 $(MAKE) OUTPUT=$$BUILD_TARGET -C $$TARGET; \ 152 ret=$$((ret * $$?)); \ 153 done; exit $$ret; 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 @ret=1; \ 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 ret=$$((ret * $$?)); \ 207 done; exit $$ret; 208 209 @# Ask all targets to emit their test scripts 210 echo "#!/bin/sh" > $(ALL_SCRIPT) 211 echo "BASE_DIR=\$$(realpath \$$(dirname \$$0))" >> $(ALL_SCRIPT) 212 echo "cd \$$BASE_DIR" >> $(ALL_SCRIPT) 213 echo ". ./kselftest/runner.sh" >> $(ALL_SCRIPT) 214 echo "ROOT=\$$PWD" >> $(ALL_SCRIPT) 215 echo "if [ \"\$$1\" = \"--summary\" ]; then" >> $(ALL_SCRIPT) 216 echo " logfile=\$$BASE_DIR/output.log" >> $(ALL_SCRIPT) 217 echo " cat /dev/null > \$$logfile" >> $(ALL_SCRIPT) 218 echo "fi" >> $(ALL_SCRIPT) 219 220 @# While building run_kselftest.sh skip also non-existent TARGET dirs: 221 @# they could be the result of a build failure and should NOT be 222 @# included in the generated runlist. 223 for TARGET in $(TARGETS); do \ 224 BUILD_TARGET=$$BUILD/$$TARGET; \ 225 [ ! -d $(INSTALL_PATH)/$$TARGET ] && echo "Skipping non-existent dir: $$TARGET" && continue; \ 226 echo "[ -w /dev/kmsg ] && echo \"kselftest: Running tests in $$TARGET\" >> /dev/kmsg" >> $(ALL_SCRIPT); \ 227 echo "cd $$TARGET" >> $(ALL_SCRIPT); \ 228 echo -n "run_many" >> $(ALL_SCRIPT); \ 229 echo -n "Emit Tests for $$TARGET\n"; \ 230 $(MAKE) -s --no-print-directory OUTPUT=$$BUILD_TARGET -C $$TARGET emit_tests >> $(ALL_SCRIPT); \ 231 echo "" >> $(ALL_SCRIPT); \ 232 echo "cd \$$ROOT" >> $(ALL_SCRIPT); \ 233 done; 234 235 chmod u+x $(ALL_SCRIPT) 236else 237 $(error Error: set INSTALL_PATH to use install) 238endif 239 240clean: 241 @for TARGET in $(TARGETS); do \ 242 BUILD_TARGET=$$BUILD/$$TARGET; \ 243 $(MAKE) OUTPUT=$$BUILD_TARGET -C $$TARGET clean;\ 244 done; 245 246.PHONY: khdr all run_tests hotplug run_hotplug clean_hotplug run_pstore_crash install clean 247