Merge branch 'core-objtool-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-block.git] / tools / testing / selftests / lib.mk
CommitLineData
84f887bf
ME
1# This mimics the top-level Makefile. We do it explicitly here so that this
2# Makefile can operate with or without the kbuild infrastructure.
3CC := $(CROSS_COMPILE)gcc
4
e53aff45 5ifeq (0,$(MAKELEVEL))
27d79a2b
SK
6 ifeq ($(OUTPUT),)
7 OUTPUT := $(shell pwd)
8 DEFAULT_INSTALL_HDR_PATH := 1
8ce72dc3 9 endif
e53aff45 10endif
42d46e57 11selfdir = $(realpath $(dir $(filter %/lib.mk,$(MAKEFILE_LIST))))
e53aff45 12
be16a244
SK
13# The following are built by lib.mk common compile rules.
14# TEST_CUSTOM_PROGS should be used by tests that require
15# custom build rule and prevent common build rule use.
16# TEST_PROGS are for test shell scripts.
17# TEST_CUSTOM_PROGS and TEST_PROGS will be run by common run_tests
18# and install targets. Common clean doesn't touch them.
d83c3ba0 19TEST_GEN_PROGS := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS))
8050ef2b 20TEST_GEN_PROGS_EXTENDED := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS_EXTENDED))
d83c3ba0
ME
21TEST_GEN_FILES := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_FILES))
22
211929fd 23ifdef KSFT_KHDR_INSTALL
b2d35fa5
AR
24top_srcdir ?= ../../../..
25include $(top_srcdir)/scripts/subarch.include
26ARCH ?= $(SUBARCH)
27
8ce72dc3
SK
28# set default goal to all, so make without a target runs all, even when
29# all isn't the first target in the file.
30.DEFAULT_GOAL := all
31
32# Invoke headers install with --no-builtin-rules to avoid circular
33# dependency in "make kselftest" case. In this case, second level
34# make inherits builtin-rules which will use the rule generate
35# Makefile.o and runs into
36# "Circular Makefile.o <- prepare dependency dropped."
37# and headers_install fails and test compile fails.
38# O= KBUILD_OUTPUT cases don't run into this error, since main Makefile
39# invokes them as sub-makes and --no-builtin-rules is not necessary,
40# but doesn't cause any failures. Keep it simple and use the same
41# flags in both cases.
42# Note that the support to install headers from lib.mk is necessary
43# when test Makefile is run directly with "make -C".
44# When local build is done, headers are installed in the default
45# INSTALL_HDR_PATH usr/include.
b2d35fa5
AR
46.PHONY: khdr
47khdr:
8ce72dc3
SK
48ifndef KSFT_KHDR_INSTALL_DONE
49ifeq (1,$(DEFAULT_INSTALL_HDR_PATH))
50 make --no-builtin-rules ARCH=$(ARCH) -C $(top_srcdir) headers_install
51else
52 make --no-builtin-rules INSTALL_HDR_PATH=$$OUTPUT/usr \
53 ARCH=$(ARCH) -C $(top_srcdir) headers_install
54endif
55endif
b2d35fa5 56
211929fd
SK
57all: khdr $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
58else
59all: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
b2d35fa5
AR
60endif
61
1f87c7c1 62.ONESHELL:
5e29a910 63define RUN_TESTS
5c069b6d
KC
64 @BASE_DIR="$(selfdir)"; \
65 . $(selfdir)/kselftest/runner.sh; \
bf660782
KC
66 if [ "X$(summary)" != "X" ]; then \
67 per_test_logging=1; \
68 fi; \
69 run_many $(1)
5e29a910
ME
70endef
71
72run_tests: all
051f278e 73ifdef building_out_of_srctree
1a940687
SK
74 @if [ "X$(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES)" != "X" ]; then
75 @rsync -aq $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(OUTPUT)
76 fi
77 @if [ "X$(TEST_PROGS)" != "X" ]; then
78 $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(OUTPUT)/$(TEST_PROGS))
79 else
80 $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS))
81 fi
82else
be16a244 83 $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS))
1a940687 84endif
5e29a910 85
c363eb48
JB
86define INSTALL_SINGLE_RULE
87 $(if $(INSTALL_LIST),@mkdir -p $(INSTALL_PATH))
88 $(if $(INSTALL_LIST),@echo rsync -a $(INSTALL_LIST) $(INSTALL_PATH)/)
89 $(if $(INSTALL_LIST),@rsync -a $(INSTALL_LIST) $(INSTALL_PATH)/)
90endef
91
32dcfba6 92define INSTALL_RULE
c363eb48
JB
93 $(eval INSTALL_LIST = $(TEST_PROGS)) $(INSTALL_SINGLE_RULE)
94 $(eval INSTALL_LIST = $(TEST_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE)
95 $(eval INSTALL_LIST = $(TEST_FILES)) $(INSTALL_SINGLE_RULE)
96 $(eval INSTALL_LIST = $(TEST_GEN_PROGS)) $(INSTALL_SINGLE_RULE)
97 $(eval INSTALL_LIST = $(TEST_CUSTOM_PROGS)) $(INSTALL_SINGLE_RULE)
98 $(eval INSTALL_LIST = $(TEST_GEN_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE)
99 $(eval INSTALL_LIST = $(TEST_GEN_FILES)) $(INSTALL_SINGLE_RULE)
32dcfba6
ME
100endef
101
102install: all
103ifdef INSTALL_PATH
104 $(INSTALL_RULE)
105else
106 $(error Error: set INSTALL_PATH to use install)
107endif
108
d4e59a53 109emit_tests:
3df6131f 110 for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \
a8ba798b 111 BASENAME_TEST=`basename $$TEST`; \
bf660782
KC
112 echo " \\"; \
113 echo -n " \"$$BASENAME_TEST\""; \
114 done; \
32dcfba6 115
8050ef2b
SK
116# define if isn't already. It is undefined in make O= case.
117ifeq ($(RM),)
118RM := rm -f
119endif
120
df6438f9 121define CLEAN
80d443e8 122 $(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
df6438f9
SK
123endef
124
125clean:
126 $(CLEAN)
88baa78d 127
8050ef2b
SK
128# When make O= with kselftest target from main level
129# the following aren't defined.
130#
051f278e 131ifdef building_out_of_srctree
8050ef2b
SK
132LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
133COMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
134LINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
135endif
136
4e49ed2f
MD
137# Selftest makefiles can override those targets by setting
138# OVERRIDE_TARGETS = 1.
139ifeq ($(OVERRIDE_TARGETS),)
a8ba798b 140$(OUTPUT)/%:%.c
2047f1d8 141 $(LINK.c) $^ $(LDLIBS) -o $@
a8ba798b 142
143$(OUTPUT)/%.o:%.S
634ce97c 144 $(COMPILE.S) $^ -o $@
a8ba798b 145
146$(OUTPUT)/%:%.S
634ce97c 147 $(LINK.S) $^ $(LDLIBS) -o $@
4e49ed2f 148endif
7d758af2 149
32dcfba6 150.PHONY: run_tests all clean install emit_tests