lib.mk 5.0 KB

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