Michael Ellerman | 84f887b | 2015-03-04 21:41:31 +1100 | [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. |
Yonghong Song | 26e6dd1 | 2021-04-13 08:34:13 -0700 | [diff] [blame] | 3 | ifneq ($(LLVM),) |
| 4 | CC := clang |
| 5 | else |
Michael Ellerman | 84f887b | 2015-03-04 21:41:31 +1100 | [diff] [blame] | 6 | CC := $(CROSS_COMPILE)gcc |
Yonghong Song | 26e6dd1 | 2021-04-13 08:34:13 -0700 | [diff] [blame] | 7 | endif |
Michael Ellerman | 84f887b | 2015-03-04 21:41:31 +1100 | [diff] [blame] | 8 | |
Shuah Khan | e53aff4 | 2017-03-01 15:15:07 -0700 | [diff] [blame] | 9 | ifeq (0,$(MAKELEVEL)) |
Shuah Khan | 27d79a2 | 2019-05-10 19:38:39 -0600 | [diff] [blame] | 10 | ifeq ($(OUTPUT),) |
| 11 | OUTPUT := $(shell pwd) |
| 12 | DEFAULT_INSTALL_HDR_PATH := 1 |
Shuah Khan | 8ce72dc | 2019-04-15 15:51:42 -0600 | [diff] [blame] | 13 | endif |
Shuah Khan | e53aff4 | 2017-03-01 15:15:07 -0700 | [diff] [blame] | 14 | endif |
Kees Cook | 42d46e5 | 2019-04-24 16:12:30 -0700 | [diff] [blame] | 15 | selfdir = $(realpath $(dir $(filter %/lib.mk,$(MAKEFILE_LIST)))) |
Shuah Khan | e53aff4 | 2017-03-01 15:15:07 -0700 | [diff] [blame] | 16 | |
Shuah Khan | be16a24 | 2017-09-11 13:06:33 -0600 | [diff] [blame] | 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. |
Michael Ellerman | d83c3ba | 2017-02-09 19:56:26 +1100 | [diff] [blame] | 23 | TEST_GEN_PROGS := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS)) |
Shuah Khan | 8050ef2 | 2017-09-06 18:36:22 -0600 | [diff] [blame] | 24 | TEST_GEN_PROGS_EXTENDED := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS_EXTENDED)) |
Michael Ellerman | d83c3ba | 2017-02-09 19:56:26 +1100 | [diff] [blame] | 25 | TEST_GEN_FILES := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_FILES)) |
| 26 | |
Shuah Khan | 211929f | 2018-12-12 20:25:14 -0700 | [diff] [blame] | 27 | ifdef KSFT_KHDR_INSTALL |
Anders Roxell | b2d35fa | 2018-09-04 12:47:21 +0200 | [diff] [blame] | 28 | top_srcdir ?= ../../../.. |
| 29 | include $(top_srcdir)/scripts/subarch.include |
| 30 | ARCH ?= $(SUBARCH) |
| 31 | |
Shuah Khan | 8ce72dc | 2019-04-15 15:51:42 -0600 | [diff] [blame] | 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. |
Anders Roxell | b2d35fa | 2018-09-04 12:47:21 +0200 | [diff] [blame] | 50 | .PHONY: khdr |
Li Zhijian | 8914a7a | 2021-09-15 21:45:54 +0800 | [diff] [blame] | 51 | .NOTPARALLEL: |
Anders Roxell | b2d35fa | 2018-09-04 12:47:21 +0200 | [diff] [blame] | 52 | khdr: |
Shuah Khan | 8ce72dc | 2019-04-15 15:51:42 -0600 | [diff] [blame] | 53 | ifndef KSFT_KHDR_INSTALL_DONE |
| 54 | ifeq (1,$(DEFAULT_INSTALL_HDR_PATH)) |
Denys Vlasenko | 75fa6772 | 2020-08-17 17:09:46 +0200 | [diff] [blame] | 55 | $(MAKE) --no-builtin-rules ARCH=$(ARCH) -C $(top_srcdir) headers_install |
Shuah Khan | 8ce72dc | 2019-04-15 15:51:42 -0600 | [diff] [blame] | 56 | else |
Denys Vlasenko | 75fa6772 | 2020-08-17 17:09:46 +0200 | [diff] [blame] | 57 | $(MAKE) --no-builtin-rules INSTALL_HDR_PATH=$$OUTPUT/usr \ |
Shuah Khan | 8ce72dc | 2019-04-15 15:51:42 -0600 | [diff] [blame] | 58 | ARCH=$(ARCH) -C $(top_srcdir) headers_install |
| 59 | endif |
| 60 | endif |
Anders Roxell | b2d35fa | 2018-09-04 12:47:21 +0200 | [diff] [blame] | 61 | |
Shuah Khan | 211929f | 2018-12-12 20:25:14 -0700 | [diff] [blame] | 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) |
Anders Roxell | b2d35fa | 2018-09-04 12:47:21 +0200 | [diff] [blame] | 65 | endif |
| 66 | |
Michael Ellerman | 5e29a91 | 2015-03-11 15:05:59 +1100 | [diff] [blame] | 67 | define RUN_TESTS |
Yauheni Kaliuta | 99aaceb | 2020-05-27 10:16:57 +0300 | [diff] [blame] | 68 | BASE_DIR="$(selfdir)"; \ |
Kees Cook | 5c069b6 | 2019-04-24 16:12:35 -0700 | [diff] [blame] | 69 | . $(selfdir)/kselftest/runner.sh; \ |
Kees Cook | bf66078 | 2019-04-24 16:12:32 -0700 | [diff] [blame] | 70 | if [ "X$(summary)" != "X" ]; then \ |
| 71 | per_test_logging=1; \ |
| 72 | fi; \ |
| 73 | run_many $(1) |
Michael Ellerman | 5e29a91 | 2015-03-11 15:05:59 +1100 | [diff] [blame] | 74 | endef |
| 75 | |
| 76 | run_tests: all |
Masahiro Yamada | 051f278 | 2019-07-06 12:07:12 +0900 | [diff] [blame] | 77 | ifdef building_out_of_srctree |
Yauheni Kaliuta | c9f7504 | 2020-05-27 10:16:58 +0300 | [diff] [blame] | 78 | @if [ "X$(TEST_PROGS)$(TEST_PROGS_EXTENDED)$(TEST_FILES)" != "X" ]; then \ |
Yauheni Kaliuta | 99aaceb | 2020-05-27 10:16:57 +0300 | [diff] [blame] | 79 | rsync -aq $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(OUTPUT); \ |
Shuah Khan | 1a94068 | 2017-09-11 19:03:11 -0600 | [diff] [blame] | 80 | fi |
Yauheni Kaliuta | 99aaceb | 2020-05-27 10:16:57 +0300 | [diff] [blame] | 81 | @if [ "X$(TEST_PROGS)" != "X" ]; then \ |
Ilya Leoshkevich | cb4969e | 2021-03-03 01:44:20 +0100 | [diff] [blame] | 82 | $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) \ |
| 83 | $(addprefix $(OUTPUT)/,$(TEST_PROGS))) ; \ |
Yauheni Kaliuta | 99aaceb | 2020-05-27 10:16:57 +0300 | [diff] [blame] | 84 | else \ |
| 85 | $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS)); \ |
Shuah Khan | 1a94068 | 2017-09-11 19:03:11 -0600 | [diff] [blame] | 86 | fi |
| 87 | else |
Yauheni Kaliuta | 99aaceb | 2020-05-27 10:16:57 +0300 | [diff] [blame] | 88 | @$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS)) |
Shuah Khan | 1a94068 | 2017-09-11 19:03:11 -0600 | [diff] [blame] | 89 | endif |
Michael Ellerman | 5e29a91 | 2015-03-11 15:05:59 +1100 | [diff] [blame] | 90 | |
Jiri Benc | c363eb4 | 2020-02-06 09:40:52 +0100 | [diff] [blame] | 91 | define INSTALL_SINGLE_RULE |
| 92 | $(if $(INSTALL_LIST),@mkdir -p $(INSTALL_PATH)) |
Yauheni Kaliuta | 99aaceb | 2020-05-27 10:16:57 +0300 | [diff] [blame] | 93 | $(if $(INSTALL_LIST),rsync -a $(INSTALL_LIST) $(INSTALL_PATH)/) |
Jiri Benc | c363eb4 | 2020-02-06 09:40:52 +0100 | [diff] [blame] | 94 | endef |
| 95 | |
Michael Ellerman | 32dcfba | 2015-03-11 15:06:00 +1100 | [diff] [blame] | 96 | define INSTALL_RULE |
Jiri Benc | c363eb4 | 2020-02-06 09:40:52 +0100 | [diff] [blame] | 97 | $(eval INSTALL_LIST = $(TEST_PROGS)) $(INSTALL_SINGLE_RULE) |
| 98 | $(eval INSTALL_LIST = $(TEST_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE) |
| 99 | $(eval INSTALL_LIST = $(TEST_FILES)) $(INSTALL_SINGLE_RULE) |
| 100 | $(eval INSTALL_LIST = $(TEST_GEN_PROGS)) $(INSTALL_SINGLE_RULE) |
| 101 | $(eval INSTALL_LIST = $(TEST_CUSTOM_PROGS)) $(INSTALL_SINGLE_RULE) |
| 102 | $(eval INSTALL_LIST = $(TEST_GEN_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE) |
| 103 | $(eval INSTALL_LIST = $(TEST_GEN_FILES)) $(INSTALL_SINGLE_RULE) |
Kees Cook | de53fa9 | 2021-05-26 20:17:54 -0700 | [diff] [blame] | 104 | $(eval INSTALL_LIST = $(wildcard config settings)) $(INSTALL_SINGLE_RULE) |
Michael Ellerman | 32dcfba | 2015-03-11 15:06:00 +1100 | [diff] [blame] | 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 | |
Kees Cook | d4e59a5 | 2019-04-24 16:12:31 -0700 | [diff] [blame] | 114 | emit_tests: |
Shuah Khan (Samsung OSG) | 3df6131 | 2018-05-01 12:15:34 -0600 | [diff] [blame] | 115 | for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \ |
bamvor.zhangjian@huawei.com | a8ba798 | 2016-11-29 19:55:52 +0800 | [diff] [blame] | 116 | BASENAME_TEST=`basename $$TEST`; \ |
Kees Cook | f0f0a5d | 2020-09-28 13:26:48 -0700 | [diff] [blame] | 117 | echo "$(COLLECTION):$$BASENAME_TEST"; \ |
| 118 | done |
Michael Ellerman | 32dcfba | 2015-03-11 15:06:00 +1100 | [diff] [blame] | 119 | |
Shuah Khan | 8050ef2 | 2017-09-06 18:36:22 -0600 | [diff] [blame] | 120 | # define if isn't already. It is undefined in make O= case. |
| 121 | ifeq ($(RM),) |
| 122 | RM := rm -f |
| 123 | endif |
| 124 | |
Shuah Khan | df6438f | 2017-04-21 16:07:51 -0600 | [diff] [blame] | 125 | define CLEAN |
bamvor.zhangjian@huawei.com | 80d443e8 | 2016-11-29 19:55:51 +0800 | [diff] [blame] | 126 | $(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN) |
Shuah Khan | df6438f | 2017-04-21 16:07:51 -0600 | [diff] [blame] | 127 | endef |
| 128 | |
| 129 | clean: |
| 130 | $(CLEAN) |
bamvor.zhangjian@huawei.com | 88baa78 | 2016-11-29 19:55:47 +0800 | [diff] [blame] | 131 | |
Shuah Khan | 8050ef2 | 2017-09-06 18:36:22 -0600 | [diff] [blame] | 132 | # When make O= with kselftest target from main level |
| 133 | # the following aren't defined. |
| 134 | # |
Masahiro Yamada | 051f278 | 2019-07-06 12:07:12 +0900 | [diff] [blame] | 135 | ifdef building_out_of_srctree |
Shuah Khan | 8050ef2 | 2017-09-06 18:36:22 -0600 | [diff] [blame] | 136 | LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) |
| 137 | COMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c |
| 138 | LINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) |
| 139 | endif |
| 140 | |
Mathieu Desnoyers | 4e49ed2 | 2018-06-02 08:44:03 -0400 | [diff] [blame] | 141 | # Selftest makefiles can override those targets by setting |
| 142 | # OVERRIDE_TARGETS = 1. |
| 143 | ifeq ($(OVERRIDE_TARGETS),) |
Anders Roxell | a738a4c | 2021-11-05 17:25:29 +0100 | [diff] [blame] | 144 | LOCAL_HDRS += $(selfdir)/kselftest_harness.h $(selfdir)/kselftest.h |
Shuah Khan | 1056d3d2 | 2020-03-25 17:16:49 -0600 | [diff] [blame] | 145 | $(OUTPUT)/%:%.c $(LOCAL_HDRS) |
Tommi Rantala | f825d3f | 2020-10-08 15:26:21 +0300 | [diff] [blame] | 146 | $(LINK.c) $(filter-out $(LOCAL_HDRS),$^) $(LDLIBS) -o $@ |
bamvor.zhangjian@huawei.com | a8ba798 | 2016-11-29 19:55:52 +0800 | [diff] [blame] | 147 | |
| 148 | $(OUTPUT)/%.o:%.S |
Michael Ellerman | 634ce97 | 2017-02-09 19:56:28 +1100 | [diff] [blame] | 149 | $(COMPILE.S) $^ -o $@ |
bamvor.zhangjian@huawei.com | a8ba798 | 2016-11-29 19:55:52 +0800 | [diff] [blame] | 150 | |
| 151 | $(OUTPUT)/%:%.S |
Michael Ellerman | 634ce97 | 2017-02-09 19:56:28 +1100 | [diff] [blame] | 152 | $(LINK.S) $^ $(LDLIBS) -o $@ |
Mathieu Desnoyers | 4e49ed2 | 2018-06-02 08:44:03 -0400 | [diff] [blame] | 153 | endif |
bamvor.zhangjian@huawei.com | 7d758af | 2016-11-29 19:55:49 +0800 | [diff] [blame] | 154 | |
Michael Ellerman | 32dcfba | 2015-03-11 15:06:00 +1100 | [diff] [blame] | 155 | .PHONY: run_tests all clean install emit_tests |