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