lib.mk 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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.
  3. ifneq ($(LLVM),)
  4. ifneq ($(filter %/,$(LLVM)),)
  5. LLVM_PREFIX := $(LLVM)
  6. else ifneq ($(filter -%,$(LLVM)),)
  7. LLVM_SUFFIX := $(LLVM)
  8. endif
  9. CLANG := $(LLVM_PREFIX)clang$(LLVM_SUFFIX)
  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. # Default to host architecture if ARCH is not explicitly given.
  22. ifeq ($(ARCH),)
  23. CLANG_TARGET_FLAGS := $(shell $(CLANG) -print-target-triple)
  24. else
  25. CLANG_TARGET_FLAGS := $(CLANG_TARGET_FLAGS_$(ARCH))
  26. endif
  27. ifeq ($(CROSS_COMPILE),)
  28. ifeq ($(CLANG_TARGET_FLAGS),)
  29. $(error Specify CROSS_COMPILE or add '--target=' option to lib.mk)
  30. else
  31. CLANG_FLAGS += --target=$(CLANG_TARGET_FLAGS)
  32. endif # CLANG_TARGET_FLAGS
  33. else
  34. CLANG_FLAGS += --target=$(notdir $(CROSS_COMPILE:%-=%))
  35. endif # CROSS_COMPILE
  36. # gcc defaults to silence (off) for the following warnings, but clang defaults
  37. # to the opposite. The warnings are not useful for the kernel itself, which is
  38. # why they have remained disabled in gcc for the main kernel build. And it is
  39. # only due to including kernel data structures in the selftests, that we get the
  40. # warnings from clang. Therefore, disable the warnings for clang builds.
  41. CFLAGS += -Wno-address-of-packed-member
  42. CFLAGS += -Wno-gnu-variable-sized-type-not-at-end
  43. CC := $(CLANG) $(CLANG_FLAGS) -fintegrated-as
  44. else
  45. CC := $(CROSS_COMPILE)gcc
  46. endif # LLVM
  47. ifeq (0,$(MAKELEVEL))
  48. ifeq ($(OUTPUT),)
  49. OUTPUT := $(shell pwd)
  50. DEFAULT_INSTALL_HDR_PATH := 1
  51. endif
  52. endif
  53. selfdir = $(realpath $(dir $(filter %/lib.mk,$(MAKEFILE_LIST))))
  54. top_srcdir = $(selfdir)/../../..
  55. # msg: emit succinct information message describing current building step
  56. # $1 - generic step name (e.g., CC, LINK, etc);
  57. # $2 - optional "flavor" specifier; if provided, will be emitted as [flavor];
  58. # $3 - target (assumed to be file); only file name will be emitted;
  59. # $4 - optional extra arg, emitted as-is, if provided.
  60. ifeq ($(V),1)
  61. Q =
  62. msg =
  63. else
  64. Q = @
  65. msg = @printf ' %-8s%s %s%s\n' "$(1)" "$(if $(2), [$(2)])" "$(notdir $(3))" "$(if $(4), $(4))";
  66. MAKEFLAGS += --no-print-directory
  67. endif
  68. ifeq ($(KHDR_INCLUDES),)
  69. KHDR_INCLUDES := -isystem $(top_srcdir)/usr/include
  70. endif
  71. # In order to use newer items that haven't yet been added to the user's system
  72. # header files, add $(TOOLS_INCLUDES) to the compiler invocation in each
  73. # each selftest.
  74. # You may need to add files to that location, or to refresh an existing file. In
  75. # order to do that, run "make headers" from $(top_srcdir), then copy the
  76. # header file that you want from $(top_srcdir)/usr/include/... , to the matching
  77. # subdir in $(TOOLS_INCLUDE).
  78. TOOLS_INCLUDES := -isystem $(top_srcdir)/tools/include/uapi
  79. # The following are built by lib.mk common compile rules.
  80. # TEST_CUSTOM_PROGS should be used by tests that require
  81. # custom build rule and prevent common build rule use.
  82. # TEST_PROGS are for test shell scripts.
  83. # TEST_CUSTOM_PROGS and TEST_PROGS will be run by common run_tests
  84. # and install targets. Common clean doesn't touch them.
  85. TEST_GEN_PROGS := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS))
  86. TEST_GEN_PROGS_EXTENDED := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS_EXTENDED))
  87. TEST_GEN_FILES := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_FILES))
  88. all: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) \
  89. $(if $(TEST_GEN_MODS_DIR),gen_mods_dir)
  90. define RUN_TESTS
  91. BASE_DIR="$(selfdir)"; \
  92. . $(selfdir)/kselftest/runner.sh; \
  93. if [ "X$(summary)" != "X" ]; then \
  94. per_test_logging=1; \
  95. fi; \
  96. run_many $(1)
  97. endef
  98. define INSTALL_INCLUDES
  99. $(if $(TEST_INCLUDES), \
  100. relative_files=""; \
  101. for entry in $(TEST_INCLUDES); do \
  102. entry_dir=$$(readlink -e "$$(dirname "$$entry")"); \
  103. entry_name=$$(basename "$$entry"); \
  104. relative_dir=$${entry_dir#"$$SRC_PATH"/}; \
  105. if [ "$$relative_dir" = "$$entry_dir" ]; then \
  106. echo "Error: TEST_INCLUDES entry \"$$entry\" not located inside selftests directory ($$SRC_PATH)" >&2; \
  107. exit 1; \
  108. fi; \
  109. relative_files="$$relative_files $$relative_dir/$$entry_name"; \
  110. done; \
  111. cd $(SRC_PATH) && rsync -aR $$relative_files $(OBJ_PATH)/ \
  112. )
  113. endef
  114. run_tests: all
  115. ifdef building_out_of_srctree
  116. @if [ "X$(TEST_PROGS)$(TEST_PROGS_EXTENDED)$(TEST_FILES)$(TEST_GEN_MODS_DIR)" != "X" ]; then \
  117. rsync -aq --copy-unsafe-links $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(TEST_GEN_MODS_DIR) $(OUTPUT); \
  118. fi
  119. @$(INSTALL_INCLUDES)
  120. @if [ "X$(TEST_PROGS)" != "X" ]; then \
  121. $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) \
  122. $(addprefix $(OUTPUT)/,$(TEST_PROGS))) ; \
  123. else \
  124. $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS)); \
  125. fi
  126. else
  127. @$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS))
  128. endif
  129. gen_mods_dir:
  130. $(Q)$(MAKE) -C $(TEST_GEN_MODS_DIR)
  131. clean_mods_dir:
  132. $(Q)$(MAKE) -C $(TEST_GEN_MODS_DIR) clean
  133. define INSTALL_SINGLE_RULE
  134. $(if $(INSTALL_LIST),@mkdir -p $(INSTALL_PATH))
  135. $(if $(INSTALL_LIST),rsync -a --copy-unsafe-links $(INSTALL_LIST) $(INSTALL_PATH)/)
  136. endef
  137. define INSTALL_MODS_RULE
  138. $(if $(INSTALL_LIST),@mkdir -p $(INSTALL_PATH)/$(INSTALL_LIST))
  139. $(if $(INSTALL_LIST),rsync -a --copy-unsafe-links $(INSTALL_LIST)/*.ko $(INSTALL_PATH)/$(INSTALL_LIST))
  140. endef
  141. define INSTALL_RULE
  142. $(eval INSTALL_LIST = $(TEST_PROGS)) $(INSTALL_SINGLE_RULE)
  143. $(eval INSTALL_LIST = $(TEST_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE)
  144. $(eval INSTALL_LIST = $(TEST_FILES)) $(INSTALL_SINGLE_RULE)
  145. $(eval INSTALL_LIST = $(TEST_GEN_PROGS)) $(INSTALL_SINGLE_RULE)
  146. $(eval INSTALL_LIST = $(TEST_CUSTOM_PROGS)) $(INSTALL_SINGLE_RULE)
  147. $(eval INSTALL_LIST = $(TEST_GEN_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE)
  148. $(eval INSTALL_LIST = $(TEST_GEN_FILES)) $(INSTALL_SINGLE_RULE)
  149. $(eval INSTALL_LIST = $(notdir $(TEST_GEN_MODS_DIR))) $(INSTALL_MODS_RULE)
  150. $(eval INSTALL_LIST = $(wildcard config settings)) $(INSTALL_SINGLE_RULE)
  151. endef
  152. install: all
  153. ifdef INSTALL_PATH
  154. $(INSTALL_RULE)
  155. $(INSTALL_INCLUDES)
  156. else
  157. $(error Error: set INSTALL_PATH to use install)
  158. endif
  159. emit_tests:
  160. for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \
  161. BASENAME_TEST=`basename $$TEST`; \
  162. echo "$(COLLECTION):$$BASENAME_TEST"; \
  163. done
  164. # define if isn't already. It is undefined in make O= case.
  165. ifeq ($(RM),)
  166. RM := rm -f
  167. endif
  168. define CLEAN
  169. $(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
  170. endef
  171. clean: $(if $(TEST_GEN_MODS_DIR),clean_mods_dir)
  172. $(CLEAN)
  173. # Build with _GNU_SOURCE by default
  174. CFLAGS += -D_GNU_SOURCE=
  175. # Additional include paths needed by kselftest.h and local headers
  176. CFLAGS += -I${top_srcdir}/tools/testing/selftests
  177. # Enables to extend CFLAGS and LDFLAGS from command line, e.g.
  178. # make USERCFLAGS=-Werror USERLDFLAGS=-static
  179. CFLAGS += $(USERCFLAGS)
  180. LDFLAGS += $(USERLDFLAGS)
  181. # When make O= with kselftest target from main level
  182. # the following aren't defined.
  183. #
  184. ifdef building_out_of_srctree
  185. LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
  186. COMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
  187. LINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
  188. endif
  189. # Selftest makefiles can override those targets by setting
  190. # OVERRIDE_TARGETS = 1.
  191. ifeq ($(OVERRIDE_TARGETS),)
  192. LOCAL_HDRS += $(selfdir)/kselftest_harness.h $(selfdir)/kselftest.h
  193. $(OUTPUT)/%:%.c $(LOCAL_HDRS)
  194. $(call msg,CC,,$@)
  195. $(Q)$(LINK.c) $(filter-out $(LOCAL_HDRS),$^) $(LDLIBS) -o $@
  196. $(OUTPUT)/%.o:%.S
  197. $(COMPILE.S) $^ -o $@
  198. $(OUTPUT)/%:%.S
  199. $(LINK.S) $^ $(LDLIBS) -o $@
  200. endif
  201. # Extract the expected header directory
  202. khdr_output := $(patsubst %/usr/include,%,$(filter %/usr/include,$(KHDR_INCLUDES)))
  203. headers:
  204. $(Q)$(MAKE) -f $(top_srcdir)/Makefile -C $(khdr_output) headers
  205. .PHONY: run_tests all clean install emit_tests gen_mods_dir clean_mods_dir headers