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