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.
4 ifneq ($(filter %/,$(LLVM)),)
6 else ifneq ($(filter -%,$(LLVM)),)
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))
23 ifeq ($(CROSS_COMPILE),)
24 ifeq ($(CLANG_TARGET_FLAGS),)
25 $(error Specify CROSS_COMPILE or add '--target=' option to lib.mk)
27 CLANG_FLAGS += --target=$(CLANG_TARGET_FLAGS)
28 endif # CLANG_TARGET_FLAGS
30 CLANG_FLAGS += --target=$(notdir $(CROSS_COMPILE:%-=%))
33 CC := $(LLVM_PREFIX)clang$(LLVM_SUFFIX) $(CLANG_FLAGS) -fintegrated-as
35 CC := $(CROSS_COMPILE)gcc
40 OUTPUT := $(shell pwd)
41 DEFAULT_INSTALL_HDR_PATH := 1
44 selfdir = $(realpath $(dir $(filter %/lib.mk,$(MAKEFILE_LIST))))
45 top_srcdir = $(selfdir)/../../..
47 ifeq ("$(origin O)", "command line")
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
61 abs_srctree := $(shell cd $(top_srcdir) && pwd)
62 KHDR_DIR := ${abs_srctree}/usr/include
65 KHDR_INCLUDES := -isystem $(KHDR_DIR)
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))
77 all: kernel_header_files $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) \
81 @ls $(KHDR_DIR)/linux/*.h >/dev/null 2>/dev/null; \
82 if [ $$? -ne 0 ]; then \
86 echo -e "$${RED}error$${NOCOLOR}: missing kernel header files."; \
87 echo "Please run this and try again:"; \
89 echo " cd $(top_srcdir)"; \
90 echo " make headers"; \
95 .PHONY: kernel_header_files
98 BASE_DIR="$(selfdir)"; \
99 . $(selfdir)/kselftest/runner.sh; \
100 if [ "X$(summary)" != "X" ]; then \
101 per_test_logging=1; \
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); \
111 @if [ "X$(TEST_PROGS)" != "X" ]; then \
112 $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) \
113 $(addprefix $(OUTPUT)/,$(TEST_PROGS))) ; \
115 $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS)); \
118 @$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS))
121 define INSTALL_SINGLE_RULE
122 $(if $(INSTALL_LIST),@mkdir -p $(INSTALL_PATH))
123 $(if $(INSTALL_LIST),rsync -aL $(INSTALL_LIST) $(INSTALL_PATH)/)
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)
141 $(error Error: set INSTALL_PATH to use install)
145 for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \
146 BASENAME_TEST=`basename $$TEST`; \
147 echo "$(COLLECTION):$$BASENAME_TEST"; \
150 # define if isn't already. It is undefined in make O= case.
156 $(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
162 # Enables to extend CFLAGS and LDFLAGS from command line, e.g.
163 # make USERCFLAGS=-Werror USERLDFLAGS=-static
164 CFLAGS += $(USERCFLAGS)
165 LDFLAGS += $(USERLDFLAGS)
167 # When make O= with kselftest target from main level
168 # the following aren't defined.
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)
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 $@
184 $(COMPILE.S) $^ -o $@
187 $(LINK.S) $^ $(LDLIBS) -o $@
190 .PHONY: run_tests all clean install emit_tests