xref: /openbmc/linux/tools/testing/selftests/lib.mk (revision 93696d8f)
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
80run_tests: all
81ifdef building_out_of_srctree
82	@if [ "X$(TEST_PROGS)$(TEST_PROGS_EXTENDED)$(TEST_FILES)" != "X" ]; then \
83		rsync -aq --copy-unsafe-links $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(OUTPUT); \
84	fi
85	@if [ "X$(TEST_PROGS)" != "X" ]; then \
86		$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) \
87				  $(addprefix $(OUTPUT)/,$(TEST_PROGS))) ; \
88	else \
89		$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS)); \
90	fi
91else
92	@$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS))
93endif
94
95define INSTALL_SINGLE_RULE
96	$(if $(INSTALL_LIST),@mkdir -p $(INSTALL_PATH))
97	$(if $(INSTALL_LIST),rsync -a --copy-unsafe-links $(INSTALL_LIST) $(INSTALL_PATH)/)
98endef
99
100define INSTALL_RULE
101	$(eval INSTALL_LIST = $(TEST_PROGS)) $(INSTALL_SINGLE_RULE)
102	$(eval INSTALL_LIST = $(TEST_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE)
103	$(eval INSTALL_LIST = $(TEST_FILES)) $(INSTALL_SINGLE_RULE)
104	$(eval INSTALL_LIST = $(TEST_GEN_PROGS)) $(INSTALL_SINGLE_RULE)
105	$(eval INSTALL_LIST = $(TEST_CUSTOM_PROGS)) $(INSTALL_SINGLE_RULE)
106	$(eval INSTALL_LIST = $(TEST_GEN_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE)
107	$(eval INSTALL_LIST = $(TEST_GEN_FILES)) $(INSTALL_SINGLE_RULE)
108	$(eval INSTALL_LIST = $(wildcard config settings)) $(INSTALL_SINGLE_RULE)
109endef
110
111install: all
112ifdef INSTALL_PATH
113	$(INSTALL_RULE)
114else
115	$(error Error: set INSTALL_PATH to use install)
116endif
117
118emit_tests:
119	for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \
120		BASENAME_TEST=`basename $$TEST`;	\
121		echo "$(COLLECTION):$$BASENAME_TEST";	\
122	done
123
124# define if isn't already. It is undefined in make O= case.
125ifeq ($(RM),)
126RM := rm -f
127endif
128
129define CLEAN
130	$(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
131endef
132
133clean:
134	$(CLEAN)
135
136# Enables to extend CFLAGS and LDFLAGS from command line, e.g.
137# make USERCFLAGS=-Werror USERLDFLAGS=-static
138CFLAGS += $(USERCFLAGS)
139LDFLAGS += $(USERLDFLAGS)
140
141# When make O= with kselftest target from main level
142# the following aren't defined.
143#
144ifdef building_out_of_srctree
145LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
146COMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
147LINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
148endif
149
150# Selftest makefiles can override those targets by setting
151# OVERRIDE_TARGETS = 1.
152ifeq ($(OVERRIDE_TARGETS),)
153LOCAL_HDRS += $(selfdir)/kselftest_harness.h $(selfdir)/kselftest.h
154$(OUTPUT)/%:%.c $(LOCAL_HDRS)
155	$(LINK.c) $(filter-out $(LOCAL_HDRS),$^) $(LDLIBS) -o $@
156
157$(OUTPUT)/%.o:%.S
158	$(COMPILE.S) $^ -o $@
159
160$(OUTPUT)/%:%.S
161	$(LINK.S) $^ $(LDLIBS) -o $@
162endif
163
164.PHONY: run_tests all clean install emit_tests
165