1 # This mimics the top-level Makefile. We do it 1 # This mimics the top-level Makefile. We do it explicitly here so that this 2 # Makefile can operate with or without the kbu 2 # Makefile can operate with or without the kbuild infrastructure. 3 ifneq ($(LLVM),) 3 ifneq ($(LLVM),) 4 ifneq ($(filter %/,$(LLVM)),) 4 ifneq ($(filter %/,$(LLVM)),) 5 LLVM_PREFIX := $(LLVM) 5 LLVM_PREFIX := $(LLVM) 6 else ifneq ($(filter -%,$(LLVM)),) 6 else ifneq ($(filter -%,$(LLVM)),) 7 LLVM_SUFFIX := $(LLVM) 7 LLVM_SUFFIX := $(LLVM) 8 endif 8 endif 9 9 10 CLANG := $(LLVM_PREFIX)clang$(LLVM_SUFFIX) 10 CLANG := $(LLVM_PREFIX)clang$(LLVM_SUFFIX) 11 11 12 CLANG_TARGET_FLAGS_arm := arm-linux-g 12 CLANG_TARGET_FLAGS_arm := arm-linux-gnueabi 13 CLANG_TARGET_FLAGS_arm64 := aarch64-lin 13 CLANG_TARGET_FLAGS_arm64 := aarch64-linux-gnu 14 CLANG_TARGET_FLAGS_hexagon := hexagon-lin 14 CLANG_TARGET_FLAGS_hexagon := hexagon-linux-musl 15 CLANG_TARGET_FLAGS_i386 := i386-linux- 15 CLANG_TARGET_FLAGS_i386 := i386-linux-gnu 16 CLANG_TARGET_FLAGS_m68k := m68k-linux- 16 CLANG_TARGET_FLAGS_m68k := m68k-linux-gnu 17 CLANG_TARGET_FLAGS_mips := mipsel-linu 17 CLANG_TARGET_FLAGS_mips := mipsel-linux-gnu 18 CLANG_TARGET_FLAGS_powerpc := powerpc64le 18 CLANG_TARGET_FLAGS_powerpc := powerpc64le-linux-gnu 19 CLANG_TARGET_FLAGS_riscv := riscv64-lin 19 CLANG_TARGET_FLAGS_riscv := riscv64-linux-gnu 20 CLANG_TARGET_FLAGS_s390 := s390x-linux 20 CLANG_TARGET_FLAGS_s390 := s390x-linux-gnu 21 CLANG_TARGET_FLAGS_x86 := x86_64-linu 21 CLANG_TARGET_FLAGS_x86 := x86_64-linux-gnu 22 CLANG_TARGET_FLAGS_x86_64 := x86_64-linu 22 CLANG_TARGET_FLAGS_x86_64 := x86_64-linux-gnu 23 23 24 # Default to host architecture if ARCH is not 24 # Default to host architecture if ARCH is not explicitly given. 25 ifeq ($(ARCH),) 25 ifeq ($(ARCH),) 26 CLANG_TARGET_FLAGS := $(shell $(CLANG) -print- 26 CLANG_TARGET_FLAGS := $(shell $(CLANG) -print-target-triple) 27 else 27 else 28 CLANG_TARGET_FLAGS := $(CLANG_TARGET_FLAGS_$(A 28 CLANG_TARGET_FLAGS := $(CLANG_TARGET_FLAGS_$(ARCH)) 29 endif 29 endif 30 30 31 ifeq ($(CROSS_COMPILE),) 31 ifeq ($(CROSS_COMPILE),) 32 ifeq ($(CLANG_TARGET_FLAGS),) 32 ifeq ($(CLANG_TARGET_FLAGS),) 33 $(error Specify CROSS_COMPILE or add '--target 33 $(error Specify CROSS_COMPILE or add '--target=' option to lib.mk) 34 else 34 else 35 CLANG_FLAGS += --target=$(CLANG_TARGET_FLA 35 CLANG_FLAGS += --target=$(CLANG_TARGET_FLAGS) 36 endif # CLANG_TARGET_FLAGS 36 endif # CLANG_TARGET_FLAGS 37 else 37 else 38 CLANG_FLAGS += --target=$(notdir $(CROSS_C 38 CLANG_FLAGS += --target=$(notdir $(CROSS_COMPILE:%-=%)) 39 endif # CROSS_COMPILE 39 endif # CROSS_COMPILE 40 40 41 # gcc defaults to silence (off) for the follow << 42 # to the opposite. The warnings are not useful << 43 # why they have remained disabled in gcc for t << 44 # only due to including kernel data structures << 45 # warnings from clang. Therefore, disable the << 46 CFLAGS += -Wno-address-of-packed-member << 47 CFLAGS += -Wno-gnu-variable-sized-type-not-at- << 48 << 49 CC := $(CLANG) $(CLANG_FLAGS) -fintegrated-as 41 CC := $(CLANG) $(CLANG_FLAGS) -fintegrated-as 50 else 42 else 51 CC := $(CROSS_COMPILE)gcc 43 CC := $(CROSS_COMPILE)gcc 52 endif # LLVM 44 endif # LLVM 53 45 54 ifeq (0,$(MAKELEVEL)) 46 ifeq (0,$(MAKELEVEL)) 55 ifeq ($(OUTPUT),) 47 ifeq ($(OUTPUT),) 56 OUTPUT := $(shell pwd) 48 OUTPUT := $(shell pwd) 57 DEFAULT_INSTALL_HDR_PATH := 1 49 DEFAULT_INSTALL_HDR_PATH := 1 58 endif 50 endif 59 endif 51 endif 60 selfdir = $(realpath $(dir $(filter %/lib.mk,$ 52 selfdir = $(realpath $(dir $(filter %/lib.mk,$(MAKEFILE_LIST)))) 61 top_srcdir = $(selfdir)/../../.. 53 top_srcdir = $(selfdir)/../../.. 62 54 63 # msg: emit succinct information message descr 55 # msg: emit succinct information message describing current building step 64 # $1 - generic step name (e.g., CC, LINK, etc) 56 # $1 - generic step name (e.g., CC, LINK, etc); 65 # $2 - optional "flavor" specifier; if provide 57 # $2 - optional "flavor" specifier; if provided, will be emitted as [flavor]; 66 # $3 - target (assumed to be file); only file 58 # $3 - target (assumed to be file); only file name will be emitted; 67 # $4 - optional extra arg, emitted as-is, if p 59 # $4 - optional extra arg, emitted as-is, if provided. 68 ifeq ($(V),1) 60 ifeq ($(V),1) 69 Q = 61 Q = 70 msg = 62 msg = 71 else 63 else 72 Q = @ 64 Q = @ 73 msg = @printf ' %-8s%s %s%s\n' "$(1)" "$(if $ 65 msg = @printf ' %-8s%s %s%s\n' "$(1)" "$(if $(2), [$(2)])" "$(notdir $(3))" "$(if $(4), $(4))"; 74 MAKEFLAGS += --no-print-directory 66 MAKEFLAGS += --no-print-directory 75 endif 67 endif 76 68 77 ifeq ($(KHDR_INCLUDES),) 69 ifeq ($(KHDR_INCLUDES),) 78 KHDR_INCLUDES := -isystem $(top_srcdir)/usr/in 70 KHDR_INCLUDES := -isystem $(top_srcdir)/usr/include 79 endif 71 endif 80 72 81 # In order to use newer items that haven't yet 73 # In order to use newer items that haven't yet been added to the user's system 82 # header files, add $(TOOLS_INCLUDES) to the c 74 # header files, add $(TOOLS_INCLUDES) to the compiler invocation in each 83 # each selftest. 75 # each selftest. 84 # You may need to add files to that location, 76 # You may need to add files to that location, or to refresh an existing file. In 85 # order to do that, run "make headers" from $( 77 # order to do that, run "make headers" from $(top_srcdir), then copy the 86 # header file that you want from $(top_srcdir) 78 # header file that you want from $(top_srcdir)/usr/include/... , to the matching 87 # subdir in $(TOOLS_INCLUDE). 79 # subdir in $(TOOLS_INCLUDE). 88 TOOLS_INCLUDES := -isystem $(top_srcdir)/tools 80 TOOLS_INCLUDES := -isystem $(top_srcdir)/tools/include/uapi 89 81 90 # The following are built by lib.mk common com 82 # The following are built by lib.mk common compile rules. 91 # TEST_CUSTOM_PROGS should be used by tests th 83 # TEST_CUSTOM_PROGS should be used by tests that require 92 # custom build rule and prevent common build r 84 # custom build rule and prevent common build rule use. 93 # TEST_PROGS are for test shell scripts. 85 # TEST_PROGS are for test shell scripts. 94 # TEST_CUSTOM_PROGS and TEST_PROGS will be run 86 # TEST_CUSTOM_PROGS and TEST_PROGS will be run by common run_tests 95 # and install targets. Common clean doesn't to 87 # and install targets. Common clean doesn't touch them. 96 TEST_GEN_PROGS := $(patsubst %,$(OUTPUT)/%,$(T 88 TEST_GEN_PROGS := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS)) 97 TEST_GEN_PROGS_EXTENDED := $(patsubst %,$(OUTP 89 TEST_GEN_PROGS_EXTENDED := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS_EXTENDED)) 98 TEST_GEN_FILES := $(patsubst %,$(OUTPUT)/%,$(T 90 TEST_GEN_FILES := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_FILES)) 99 91 100 all: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTEND 92 all: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) \ 101 $(if $(TEST_GEN_MODS_DIR),gen_mods_dir 93 $(if $(TEST_GEN_MODS_DIR),gen_mods_dir) 102 94 103 define RUN_TESTS 95 define RUN_TESTS 104 BASE_DIR="$(selfdir)"; 96 BASE_DIR="$(selfdir)"; \ 105 . $(selfdir)/kselftest/runner.sh; 97 . $(selfdir)/kselftest/runner.sh; \ 106 if [ "X$(summary)" != "X" ]; then 98 if [ "X$(summary)" != "X" ]; then \ 107 per_test_logging=1; 99 per_test_logging=1; \ 108 fi; 100 fi; \ 109 run_many $(1) 101 run_many $(1) 110 endef 102 endef 111 103 112 define INSTALL_INCLUDES 104 define INSTALL_INCLUDES 113 $(if $(TEST_INCLUDES), \ 105 $(if $(TEST_INCLUDES), \ 114 relative_files=""; \ 106 relative_files=""; \ 115 for entry in $(TEST_INCLUDES); 107 for entry in $(TEST_INCLUDES); do \ 116 entry_dir=$$(readlink 108 entry_dir=$$(readlink -e "$$(dirname "$$entry")"); \ 117 entry_name=$$(basename 109 entry_name=$$(basename "$$entry"); \ 118 relative_dir=$${entry_ 110 relative_dir=$${entry_dir#"$$SRC_PATH"/}; \ 119 if [ "$$relative_dir" 111 if [ "$$relative_dir" = "$$entry_dir" ]; then \ 120 echo "Error: T 112 echo "Error: TEST_INCLUDES entry \"$$entry\" not located inside selftests directory ($$SRC_PATH)" >&2; \ 121 exit 1; \ 113 exit 1; \ 122 fi; \ 114 fi; \ 123 relative_files="$$rela 115 relative_files="$$relative_files $$relative_dir/$$entry_name"; \ 124 done; \ 116 done; \ 125 cd $(SRC_PATH) && rsync -aR $$ 117 cd $(SRC_PATH) && rsync -aR $$relative_files $(OBJ_PATH)/ \ 126 ) 118 ) 127 endef 119 endef 128 120 129 run_tests: all 121 run_tests: all 130 ifdef building_out_of_srctree 122 ifdef building_out_of_srctree 131 @if [ "X$(TEST_PROGS)$(TEST_PROGS_EXTE 123 @if [ "X$(TEST_PROGS)$(TEST_PROGS_EXTENDED)$(TEST_FILES)$(TEST_GEN_MODS_DIR)" != "X" ]; then \ 132 rsync -aq --copy-unsafe-links 124 rsync -aq --copy-unsafe-links $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(TEST_GEN_MODS_DIR) $(OUTPUT); \ 133 fi 125 fi 134 @$(INSTALL_INCLUDES) 126 @$(INSTALL_INCLUDES) 135 @if [ "X$(TEST_PROGS)" != "X" ]; then 127 @if [ "X$(TEST_PROGS)" != "X" ]; then \ 136 $(call RUN_TESTS, $(TEST_GEN_P 128 $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) \ 137 $(addprefix 129 $(addprefix $(OUTPUT)/,$(TEST_PROGS))) ; \ 138 else \ 130 else \ 139 $(call RUN_TESTS, $(TEST_GEN_P 131 $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS)); \ 140 fi 132 fi 141 else 133 else 142 @$(call RUN_TESTS, $(TEST_GEN_PROGS) $ 134 @$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS)) 143 endif 135 endif 144 136 145 gen_mods_dir: 137 gen_mods_dir: 146 $(Q)$(MAKE) -C $(TEST_GEN_MODS_DIR) 138 $(Q)$(MAKE) -C $(TEST_GEN_MODS_DIR) 147 139 148 clean_mods_dir: 140 clean_mods_dir: 149 $(Q)$(MAKE) -C $(TEST_GEN_MODS_DIR) cl 141 $(Q)$(MAKE) -C $(TEST_GEN_MODS_DIR) clean 150 142 151 define INSTALL_SINGLE_RULE 143 define INSTALL_SINGLE_RULE 152 $(if $(INSTALL_LIST),@mkdir -p $(INSTA 144 $(if $(INSTALL_LIST),@mkdir -p $(INSTALL_PATH)) 153 $(if $(INSTALL_LIST),rsync -a --copy-u 145 $(if $(INSTALL_LIST),rsync -a --copy-unsafe-links $(INSTALL_LIST) $(INSTALL_PATH)/) 154 endef 146 endef 155 147 156 define INSTALL_MODS_RULE 148 define INSTALL_MODS_RULE 157 $(if $(INSTALL_LIST),@mkdir -p $(INSTA 149 $(if $(INSTALL_LIST),@mkdir -p $(INSTALL_PATH)/$(INSTALL_LIST)) 158 $(if $(INSTALL_LIST),rsync -a --copy-u 150 $(if $(INSTALL_LIST),rsync -a --copy-unsafe-links $(INSTALL_LIST)/*.ko $(INSTALL_PATH)/$(INSTALL_LIST)) 159 endef 151 endef 160 152 161 define INSTALL_RULE 153 define INSTALL_RULE 162 $(eval INSTALL_LIST = $(TEST_PROGS)) $ 154 $(eval INSTALL_LIST = $(TEST_PROGS)) $(INSTALL_SINGLE_RULE) 163 $(eval INSTALL_LIST = $(TEST_PROGS_EXT 155 $(eval INSTALL_LIST = $(TEST_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE) 164 $(eval INSTALL_LIST = $(TEST_FILES)) $ 156 $(eval INSTALL_LIST = $(TEST_FILES)) $(INSTALL_SINGLE_RULE) 165 $(eval INSTALL_LIST = $(TEST_GEN_PROGS 157 $(eval INSTALL_LIST = $(TEST_GEN_PROGS)) $(INSTALL_SINGLE_RULE) 166 $(eval INSTALL_LIST = $(TEST_CUSTOM_PR 158 $(eval INSTALL_LIST = $(TEST_CUSTOM_PROGS)) $(INSTALL_SINGLE_RULE) 167 $(eval INSTALL_LIST = $(TEST_GEN_PROGS 159 $(eval INSTALL_LIST = $(TEST_GEN_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE) 168 $(eval INSTALL_LIST = $(TEST_GEN_FILES 160 $(eval INSTALL_LIST = $(TEST_GEN_FILES)) $(INSTALL_SINGLE_RULE) 169 $(eval INSTALL_LIST = $(notdir $(TEST_ 161 $(eval INSTALL_LIST = $(notdir $(TEST_GEN_MODS_DIR))) $(INSTALL_MODS_RULE) 170 $(eval INSTALL_LIST = $(wildcard confi 162 $(eval INSTALL_LIST = $(wildcard config settings)) $(INSTALL_SINGLE_RULE) 171 endef 163 endef 172 164 173 install: all 165 install: all 174 ifdef INSTALL_PATH 166 ifdef INSTALL_PATH 175 $(INSTALL_RULE) 167 $(INSTALL_RULE) 176 $(INSTALL_INCLUDES) 168 $(INSTALL_INCLUDES) 177 else 169 else 178 $(error Error: set INSTALL_PATH to use 170 $(error Error: set INSTALL_PATH to use install) 179 endif 171 endif 180 172 181 emit_tests: 173 emit_tests: 182 for TEST in $(TEST_GEN_PROGS) $(TEST_C 174 for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \ 183 BASENAME_TEST=`basename $$TEST 175 BASENAME_TEST=`basename $$TEST`; \ 184 echo "$(COLLECTION):$$BASENAME 176 echo "$(COLLECTION):$$BASENAME_TEST"; \ 185 done 177 done 186 178 187 # define if isn't already. It is undefined in 179 # define if isn't already. It is undefined in make O= case. 188 ifeq ($(RM),) 180 ifeq ($(RM),) 189 RM := rm -f 181 RM := rm -f 190 endif 182 endif 191 183 192 define CLEAN 184 define CLEAN 193 $(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_ 185 $(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN) 194 endef 186 endef 195 187 196 clean: $(if $(TEST_GEN_MODS_DIR),clean_mods_di 188 clean: $(if $(TEST_GEN_MODS_DIR),clean_mods_dir) 197 $(CLEAN) 189 $(CLEAN) 198 << 199 # Build with _GNU_SOURCE by default << 200 CFLAGS += -D_GNU_SOURCE= << 201 190 202 # Enables to extend CFLAGS and LDFLAGS from co 191 # Enables to extend CFLAGS and LDFLAGS from command line, e.g. 203 # make USERCFLAGS=-Werror USERLDFLAGS=-static 192 # make USERCFLAGS=-Werror USERLDFLAGS=-static 204 CFLAGS += $(USERCFLAGS) 193 CFLAGS += $(USERCFLAGS) 205 LDFLAGS += $(USERLDFLAGS) 194 LDFLAGS += $(USERLDFLAGS) 206 195 207 # When make O= with kselftest target from main 196 # When make O= with kselftest target from main level 208 # the following aren't defined. 197 # the following aren't defined. 209 # 198 # 210 ifdef building_out_of_srctree 199 ifdef building_out_of_srctree 211 LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS 200 LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) 212 COMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TAR 201 COMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c 213 LINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAG 202 LINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) 214 endif 203 endif 215 204 216 # Selftest makefiles can override those target 205 # Selftest makefiles can override those targets by setting 217 # OVERRIDE_TARGETS = 1. 206 # OVERRIDE_TARGETS = 1. 218 ifeq ($(OVERRIDE_TARGETS),) 207 ifeq ($(OVERRIDE_TARGETS),) 219 LOCAL_HDRS += $(selfdir)/kselftest_harness.h $ 208 LOCAL_HDRS += $(selfdir)/kselftest_harness.h $(selfdir)/kselftest.h 220 $(OUTPUT)/%:%.c $(LOCAL_HDRS) 209 $(OUTPUT)/%:%.c $(LOCAL_HDRS) 221 $(call msg,CC,,$@) 210 $(call msg,CC,,$@) 222 $(Q)$(LINK.c) $(filter-out $(LOCAL_HDR 211 $(Q)$(LINK.c) $(filter-out $(LOCAL_HDRS),$^) $(LDLIBS) -o $@ 223 212 224 $(OUTPUT)/%.o:%.S 213 $(OUTPUT)/%.o:%.S 225 $(COMPILE.S) $^ -o $@ 214 $(COMPILE.S) $^ -o $@ 226 215 227 $(OUTPUT)/%:%.S 216 $(OUTPUT)/%:%.S 228 $(LINK.S) $^ $(LDLIBS) -o $@ 217 $(LINK.S) $^ $(LDLIBS) -o $@ 229 endif 218 endif 230 219 231 .PHONY: run_tests all clean install emit_tests 220 .PHONY: run_tests all clean install emit_tests gen_mods_dir clean_mods_dir
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.