| b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 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 | CC := clang |
| 5 | else |
| 6 | CC := $(CROSS_COMPILE)gcc |
| 7 | endif |
| 8 | |
| 9 | ifeq (0,$(MAKELEVEL)) |
| 10 | ifeq ($(OUTPUT),) |
| 11 | OUTPUT := $(shell pwd) |
| 12 | DEFAULT_INSTALL_HDR_PATH := 1 |
| 13 | endif |
| 14 | endif |
| 15 | selfdir = $(realpath $(dir $(filter %/lib.mk,$(MAKEFILE_LIST)))) |
| 16 | |
| 17 | # The following are built by lib.mk common compile rules. |
| 18 | # TEST_CUSTOM_PROGS should be used by tests that require |
| 19 | # custom build rule and prevent common build rule use. |
| 20 | # TEST_PROGS are for test shell scripts. |
| 21 | # TEST_CUSTOM_PROGS and TEST_PROGS will be run by common run_tests |
| 22 | # and install targets. Common clean doesn't touch them. |
| 23 | TEST_GEN_PROGS := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS)) |
| 24 | TEST_GEN_PROGS_EXTENDED := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS_EXTENDED)) |
| 25 | TEST_GEN_FILES := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_FILES)) |
| 26 | |
| 27 | ifdef KSFT_KHDR_INSTALL |
| 28 | top_srcdir ?= ../../../.. |
| 29 | include $(top_srcdir)/scripts/subarch.include |
| 30 | ARCH ?= $(SUBARCH) |
| 31 | |
| 32 | # set default goal to all, so make without a target runs all, even when |
| 33 | # all isn't the first target in the file. |
| 34 | .DEFAULT_GOAL := all |
| 35 | |
| 36 | # Invoke headers install with --no-builtin-rules to avoid circular |
| 37 | # dependency in "make kselftest" case. In this case, second level |
| 38 | # make inherits builtin-rules which will use the rule generate |
| 39 | # Makefile.o and runs into |
| 40 | # "Circular Makefile.o <- prepare dependency dropped." |
| 41 | # and headers_install fails and test compile fails. |
| 42 | # O= KBUILD_OUTPUT cases don't run into this error, since main Makefile |
| 43 | # invokes them as sub-makes and --no-builtin-rules is not necessary, |
| 44 | # but doesn't cause any failures. Keep it simple and use the same |
| 45 | # flags in both cases. |
| 46 | # Note that the support to install headers from lib.mk is necessary |
| 47 | # when test Makefile is run directly with "make -C". |
| 48 | # When local build is done, headers are installed in the default |
| 49 | # INSTALL_HDR_PATH usr/include. |
| 50 | .PHONY: khdr |
| 51 | .NOTPARALLEL: |
| 52 | khdr: |
| 53 | ifndef KSFT_KHDR_INSTALL_DONE |
| 54 | ifeq (1,$(DEFAULT_INSTALL_HDR_PATH)) |
| 55 | make --no-builtin-rules ARCH=$(ARCH) -C $(top_srcdir) headers_install |
| 56 | else |
| 57 | make --no-builtin-rules INSTALL_HDR_PATH=$$OUTPUT/usr \ |
| 58 | ARCH=$(ARCH) -C $(top_srcdir) headers_install |
| 59 | endif |
| 60 | endif |
| 61 | |
| 62 | all: khdr $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) |
| 63 | else |
| 64 | all: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) |
| 65 | endif |
| 66 | |
| 67 | .ONESHELL: |
| 68 | define RUN_TESTS |
| 69 | @BASE_DIR="$(selfdir)"; \ |
| 70 | . $(selfdir)/kselftest/runner.sh; \ |
| 71 | if [ "X$(summary)" != "X" ]; then \ |
| 72 | per_test_logging=1; \ |
| 73 | fi; \ |
| 74 | run_many $(1) |
| 75 | endef |
| 76 | |
| 77 | run_tests: all |
| 78 | ifdef building_out_of_srctree |
| 79 | @if [ "X$(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES)" != "X" ]; then |
| 80 | @rsync -aq $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(OUTPUT) |
| 81 | fi |
| 82 | @if [ "X$(TEST_PROGS)" != "X" ]; then |
| 83 | $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(OUTPUT)/$(TEST_PROGS)) |
| 84 | else |
| 85 | $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS)) |
| 86 | fi |
| 87 | else |
| 88 | $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS)) |
| 89 | endif |
| 90 | |
| 91 | define INSTALL_SINGLE_RULE |
| 92 | $(if $(INSTALL_LIST),@mkdir -p $(INSTALL_PATH)) |
| 93 | $(if $(INSTALL_LIST),@echo rsync -a $(INSTALL_LIST) $(INSTALL_PATH)/) |
| 94 | $(if $(INSTALL_LIST),@rsync -a $(INSTALL_LIST) $(INSTALL_PATH)/) |
| 95 | endef |
| 96 | |
| 97 | define INSTALL_RULE |
| 98 | $(eval INSTALL_LIST = $(TEST_PROGS)) $(INSTALL_SINGLE_RULE) |
| 99 | $(eval INSTALL_LIST = $(TEST_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE) |
| 100 | $(eval INSTALL_LIST = $(TEST_FILES)) $(INSTALL_SINGLE_RULE) |
| 101 | $(eval INSTALL_LIST = $(TEST_GEN_PROGS)) $(INSTALL_SINGLE_RULE) |
| 102 | $(eval INSTALL_LIST = $(TEST_CUSTOM_PROGS)) $(INSTALL_SINGLE_RULE) |
| 103 | $(eval INSTALL_LIST = $(TEST_GEN_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE) |
| 104 | $(eval INSTALL_LIST = $(TEST_GEN_FILES)) $(INSTALL_SINGLE_RULE) |
| 105 | endef |
| 106 | |
| 107 | install: all |
| 108 | ifdef INSTALL_PATH |
| 109 | $(INSTALL_RULE) |
| 110 | else |
| 111 | $(error Error: set INSTALL_PATH to use install) |
| 112 | endif |
| 113 | |
| 114 | emit_tests: |
| 115 | for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \ |
| 116 | BASENAME_TEST=`basename $$TEST`; \ |
| 117 | echo " \\"; \ |
| 118 | echo -n " \"$$BASENAME_TEST\""; \ |
| 119 | done; \ |
| 120 | |
| 121 | # define if isn't already. It is undefined in make O= case. |
| 122 | ifeq ($(RM),) |
| 123 | RM := rm -f |
| 124 | endif |
| 125 | |
| 126 | define CLEAN |
| 127 | $(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN) |
| 128 | endef |
| 129 | |
| 130 | clean: |
| 131 | $(CLEAN) |
| 132 | |
| 133 | # Enables to extend CFLAGS and LDFLAGS from command line, e.g. |
| 134 | # make USERCFLAGS=-Werror USERLDFLAGS=-static |
| 135 | CFLAGS += $(USERCFLAGS) |
| 136 | LDFLAGS += $(USERLDFLAGS) |
| 137 | |
| 138 | # When make O= with kselftest target from main level |
| 139 | # the following aren't defined. |
| 140 | # |
| 141 | ifdef building_out_of_srctree |
| 142 | LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) |
| 143 | COMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c |
| 144 | LINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) |
| 145 | endif |
| 146 | |
| 147 | # Selftest makefiles can override those targets by setting |
| 148 | # OVERRIDE_TARGETS = 1. |
| 149 | ifeq ($(OVERRIDE_TARGETS),) |
| 150 | $(OUTPUT)/%:%.c |
| 151 | $(LINK.c) $^ $(LDLIBS) -o $@ |
| 152 | |
| 153 | $(OUTPUT)/%.o:%.S |
| 154 | $(COMPILE.S) $^ -o $@ |
| 155 | |
| 156 | $(OUTPUT)/%:%.S |
| 157 | $(LINK.S) $^ $(LDLIBS) -o $@ |
| 158 | endif |
| 159 | |
| 160 | .PHONY: run_tests all clean install emit_tests |