Merge tag 'char-misc-6.2-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[linux-block.git] / tools / testing / selftests / lib.mk
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.
3 ifneq ($(LLVM),)
4 ifneq ($(filter %/,$(LLVM)),)
5 LLVM_PREFIX := $(LLVM)
6 else ifneq ($(filter -%,$(LLVM)),)
7 LLVM_SUFFIX := $(LLVM)
8 endif
9
10 CLANG_TARGET_FLAGS_arm          := arm-linux-gnueabi
11 CLANG_TARGET_FLAGS_arm64        := aarch64-linux-gnu
12 CLANG_TARGET_FLAGS_hexagon      := hexagon-linux-musl
13 CLANG_TARGET_FLAGS_m68k         := m68k-linux-gnu
14 CLANG_TARGET_FLAGS_mips         := mipsel-linux-gnu
15 CLANG_TARGET_FLAGS_powerpc      := powerpc64le-linux-gnu
16 CLANG_TARGET_FLAGS_riscv        := riscv64-linux-gnu
17 CLANG_TARGET_FLAGS_s390         := s390x-linux-gnu
18 CLANG_TARGET_FLAGS_x86          := x86_64-linux-gnu
19 CLANG_TARGET_FLAGS              := $(CLANG_TARGET_FLAGS_$(ARCH))
20
21 ifeq ($(CROSS_COMPILE),)
22 ifeq ($(CLANG_TARGET_FLAGS),)
23 $(error Specify CROSS_COMPILE or add '--target=' option to lib.mk)
24 else
25 CLANG_FLAGS     += --target=$(CLANG_TARGET_FLAGS)
26 endif # CLANG_TARGET_FLAGS
27 else
28 CLANG_FLAGS     += --target=$(notdir $(CROSS_COMPILE:%-=%))
29 endif # CROSS_COMPILE
30
31 CC := $(LLVM_PREFIX)clang$(LLVM_SUFFIX) $(CLANG_FLAGS) -fintegrated-as
32 else
33 CC := $(CROSS_COMPILE)gcc
34 endif # LLVM
35
36 ifeq (0,$(MAKELEVEL))
37     ifeq ($(OUTPUT),)
38         OUTPUT := $(shell pwd)
39         DEFAULT_INSTALL_HDR_PATH := 1
40     endif
41 endif
42 selfdir = $(realpath $(dir $(filter %/lib.mk,$(MAKEFILE_LIST))))
43 top_srcdir = $(selfdir)/../../..
44
45 ifeq ($(KHDR_INCLUDES),)
46 KHDR_INCLUDES := -isystem $(top_srcdir)/usr/include
47 endif
48
49 # The following are built by lib.mk common compile rules.
50 # TEST_CUSTOM_PROGS should be used by tests that require
51 # custom build rule and prevent common build rule use.
52 # TEST_PROGS are for test shell scripts.
53 # TEST_CUSTOM_PROGS and TEST_PROGS will be run by common run_tests
54 # and install targets. Common clean doesn't touch them.
55 TEST_GEN_PROGS := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS))
56 TEST_GEN_PROGS_EXTENDED := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS_EXTENDED))
57 TEST_GEN_FILES := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_FILES))
58
59 all: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
60
61 define RUN_TESTS
62         BASE_DIR="$(selfdir)";                  \
63         . $(selfdir)/kselftest/runner.sh;       \
64         if [ "X$(summary)" != "X" ]; then       \
65                 per_test_logging=1;             \
66         fi;                                     \
67         run_many $(1)
68 endef
69
70 run_tests: all
71 ifdef building_out_of_srctree
72         @if [ "X$(TEST_PROGS)$(TEST_PROGS_EXTENDED)$(TEST_FILES)" != "X" ]; then \
73                 rsync -aLq $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(OUTPUT); \
74         fi
75         @if [ "X$(TEST_PROGS)" != "X" ]; then \
76                 $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) \
77                                   $(addprefix $(OUTPUT)/,$(TEST_PROGS))) ; \
78         else \
79                 $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS)); \
80         fi
81 else
82         @$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS))
83 endif
84
85 define INSTALL_SINGLE_RULE
86         $(if $(INSTALL_LIST),@mkdir -p $(INSTALL_PATH))
87         $(if $(INSTALL_LIST),rsync -aL $(INSTALL_LIST) $(INSTALL_PATH)/)
88 endef
89
90 define INSTALL_RULE
91         $(eval INSTALL_LIST = $(TEST_PROGS)) $(INSTALL_SINGLE_RULE)
92         $(eval INSTALL_LIST = $(TEST_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE)
93         $(eval INSTALL_LIST = $(TEST_FILES)) $(INSTALL_SINGLE_RULE)
94         $(eval INSTALL_LIST = $(TEST_GEN_PROGS)) $(INSTALL_SINGLE_RULE)
95         $(eval INSTALL_LIST = $(TEST_CUSTOM_PROGS)) $(INSTALL_SINGLE_RULE)
96         $(eval INSTALL_LIST = $(TEST_GEN_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE)
97         $(eval INSTALL_LIST = $(TEST_GEN_FILES)) $(INSTALL_SINGLE_RULE)
98         $(eval INSTALL_LIST = $(wildcard config settings)) $(INSTALL_SINGLE_RULE)
99 endef
100
101 install: all
102 ifdef INSTALL_PATH
103         $(INSTALL_RULE)
104 else
105         $(error Error: set INSTALL_PATH to use install)
106 endif
107
108 emit_tests:
109         for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \
110                 BASENAME_TEST=`basename $$TEST`;        \
111                 echo "$(COLLECTION):$$BASENAME_TEST";   \
112         done
113
114 # define if isn't already. It is undefined in make O= case.
115 ifeq ($(RM),)
116 RM := rm -f
117 endif
118
119 define CLEAN
120         $(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
121 endef
122
123 clean:
124         $(CLEAN)
125
126 # Enables to extend CFLAGS and LDFLAGS from command line, e.g.
127 # make USERCFLAGS=-Werror USERLDFLAGS=-static
128 CFLAGS += $(USERCFLAGS)
129 LDFLAGS += $(USERLDFLAGS)
130
131 # When make O= with kselftest target from main level
132 # the following aren't defined.
133 #
134 ifdef building_out_of_srctree
135 LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
136 COMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
137 LINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
138 endif
139
140 # Selftest makefiles can override those targets by setting
141 # OVERRIDE_TARGETS = 1.
142 ifeq ($(OVERRIDE_TARGETS),)
143 LOCAL_HDRS += $(selfdir)/kselftest_harness.h $(selfdir)/kselftest.h
144 $(OUTPUT)/%:%.c $(LOCAL_HDRS)
145         $(LINK.c) $(filter-out $(LOCAL_HDRS),$^) $(LDLIBS) -o $@
146
147 $(OUTPUT)/%.o:%.S
148         $(COMPILE.S) $^ -o $@
149
150 $(OUTPUT)/%:%.S
151         $(LINK.S) $^ $(LDLIBS) -o $@
152 endif
153
154 .PHONY: run_tests all clean install emit_tests