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