selftests: forwarding: bridge_mdb: Fix failing test with old libnet
[platform/kernel/linux-starfive.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 ("$(origin O)", "command line")
48   KBUILD_OUTPUT := $(O)
49 endif
50
51 ifneq ($(KBUILD_OUTPUT),)
52   # Make's built-in functions such as $(abspath ...), $(realpath ...) cannot
53   # expand a shell special character '~'. We use a somewhat tedious way here.
54   abs_objtree := $(shell cd $(top_srcdir) && mkdir -p $(KBUILD_OUTPUT) && cd $(KBUILD_OUTPUT) && pwd)
55   $(if $(abs_objtree),, \
56     $(error failed to create output directory "$(KBUILD_OUTPUT)"))
57   # $(realpath ...) resolves symlinks
58   abs_objtree := $(realpath $(abs_objtree))
59   KHDR_DIR := ${abs_objtree}/usr/include
60 else
61   abs_srctree := $(shell cd $(top_srcdir) && pwd)
62   KHDR_DIR := ${abs_srctree}/usr/include
63 endif
64
65 KHDR_INCLUDES := -isystem $(KHDR_DIR)
66
67 # The following are built by lib.mk common compile rules.
68 # TEST_CUSTOM_PROGS should be used by tests that require
69 # custom build rule and prevent common build rule use.
70 # TEST_PROGS are for test shell scripts.
71 # TEST_CUSTOM_PROGS and TEST_PROGS will be run by common run_tests
72 # and install targets. Common clean doesn't touch them.
73 TEST_GEN_PROGS := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS))
74 TEST_GEN_PROGS_EXTENDED := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS_EXTENDED))
75 TEST_GEN_FILES := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_FILES))
76
77 all: kernel_header_files $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) \
78      $(TEST_GEN_FILES)
79
80 kernel_header_files:
81         @ls $(KHDR_DIR)/linux/*.h >/dev/null 2>/dev/null;                      \
82         if [ $$? -ne 0 ]; then                                                 \
83             RED='\033[1;31m';                                                  \
84             NOCOLOR='\033[0m';                                                 \
85             echo;                                                              \
86             echo -e "$${RED}error$${NOCOLOR}: missing kernel header files.";   \
87             echo "Please run this and try again:";                             \
88             echo;                                                              \
89             echo "    cd $(top_srcdir)";                                       \
90             echo "    make headers";                                           \
91             echo;                                                              \
92             exit 1; \
93         fi
94
95 .PHONY: kernel_header_files
96
97 define RUN_TESTS
98         BASE_DIR="$(selfdir)";                  \
99         . $(selfdir)/kselftest/runner.sh;       \
100         if [ "X$(summary)" != "X" ]; then       \
101                 per_test_logging=1;             \
102         fi;                                     \
103         run_many $(1)
104 endef
105
106 run_tests: all
107 ifdef building_out_of_srctree
108         @if [ "X$(TEST_PROGS)$(TEST_PROGS_EXTENDED)$(TEST_FILES)" != "X" ]; then \
109                 rsync -aLq $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(OUTPUT); \
110         fi
111         @if [ "X$(TEST_PROGS)" != "X" ]; then \
112                 $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) \
113                                   $(addprefix $(OUTPUT)/,$(TEST_PROGS))) ; \
114         else \
115                 $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS)); \
116         fi
117 else
118         @$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS))
119 endif
120
121 define INSTALL_SINGLE_RULE
122         $(if $(INSTALL_LIST),@mkdir -p $(INSTALL_PATH))
123         $(if $(INSTALL_LIST),rsync -aL $(INSTALL_LIST) $(INSTALL_PATH)/)
124 endef
125
126 define INSTALL_RULE
127         $(eval INSTALL_LIST = $(TEST_PROGS)) $(INSTALL_SINGLE_RULE)
128         $(eval INSTALL_LIST = $(TEST_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE)
129         $(eval INSTALL_LIST = $(TEST_FILES)) $(INSTALL_SINGLE_RULE)
130         $(eval INSTALL_LIST = $(TEST_GEN_PROGS)) $(INSTALL_SINGLE_RULE)
131         $(eval INSTALL_LIST = $(TEST_CUSTOM_PROGS)) $(INSTALL_SINGLE_RULE)
132         $(eval INSTALL_LIST = $(TEST_GEN_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE)
133         $(eval INSTALL_LIST = $(TEST_GEN_FILES)) $(INSTALL_SINGLE_RULE)
134         $(eval INSTALL_LIST = $(wildcard config settings)) $(INSTALL_SINGLE_RULE)
135 endef
136
137 install: all
138 ifdef INSTALL_PATH
139         $(INSTALL_RULE)
140 else
141         $(error Error: set INSTALL_PATH to use install)
142 endif
143
144 emit_tests:
145         for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \
146                 BASENAME_TEST=`basename $$TEST`;        \
147                 echo "$(COLLECTION):$$BASENAME_TEST";   \
148         done
149
150 # define if isn't already. It is undefined in make O= case.
151 ifeq ($(RM),)
152 RM := rm -f
153 endif
154
155 define CLEAN
156         $(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
157 endef
158
159 clean:
160         $(CLEAN)
161
162 # Enables to extend CFLAGS and LDFLAGS from command line, e.g.
163 # make USERCFLAGS=-Werror USERLDFLAGS=-static
164 CFLAGS += $(USERCFLAGS)
165 LDFLAGS += $(USERLDFLAGS)
166
167 # When make O= with kselftest target from main level
168 # the following aren't defined.
169 #
170 ifdef building_out_of_srctree
171 LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
172 COMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
173 LINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
174 endif
175
176 # Selftest makefiles can override those targets by setting
177 # OVERRIDE_TARGETS = 1.
178 ifeq ($(OVERRIDE_TARGETS),)
179 LOCAL_HDRS += $(selfdir)/kselftest_harness.h $(selfdir)/kselftest.h
180 $(OUTPUT)/%:%.c $(LOCAL_HDRS)
181         $(LINK.c) $(filter-out $(LOCAL_HDRS),$^) $(LDLIBS) -o $@
182
183 $(OUTPUT)/%.o:%.S
184         $(COMPILE.S) $^ -o $@
185
186 $(OUTPUT)/%:%.S
187         $(LINK.S) $^ $(LDLIBS) -o $@
188 endif
189
190 .PHONY: run_tests all clean install emit_tests