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