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. |
| 3 | CC := $(CROSS_COMPILE)gcc |
| 4 | |
Shuah Khan | e53aff4 | 2017-03-01 15:15:07 -0700 | [diff] [blame] | 5 | ifeq (0,$(MAKELEVEL)) |
Shuah Khan | 27d79a2 | 2019-05-10 19:38:39 -0600 | [diff] [blame] | 6 | ifeq ($(OUTPUT),) |
| 7 | OUTPUT := $(shell pwd) |
| 8 | DEFAULT_INSTALL_HDR_PATH := 1 |
Shuah Khan | 8ce72dc | 2019-04-15 15:51:42 -0600 | [diff] [blame] | 9 | endif |
Shuah Khan | e53aff4 | 2017-03-01 15:15:07 -0700 | [diff] [blame] | 10 | endif |
Kees Cook | 42d46e5 | 2019-04-24 16:12:30 -0700 | [diff] [blame] | 11 | selfdir = $(realpath $(dir $(filter %/lib.mk,$(MAKEFILE_LIST)))) |
Shuah Khan | e53aff4 | 2017-03-01 15:15:07 -0700 | [diff] [blame] | 12 | |
Shuah Khan | be16a24 | 2017-09-11 13:06:33 -0600 | [diff] [blame] | 13 | # The following are built by lib.mk common compile rules. |
| 14 | # TEST_CUSTOM_PROGS should be used by tests that require |
| 15 | # custom build rule and prevent common build rule use. |
| 16 | # TEST_PROGS are for test shell scripts. |
| 17 | # TEST_CUSTOM_PROGS and TEST_PROGS will be run by common run_tests |
| 18 | # and install targets. Common clean doesn't touch them. |
Michael Ellerman | d83c3ba | 2017-02-09 19:56:26 +1100 | [diff] [blame] | 19 | TEST_GEN_PROGS := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS)) |
Shuah Khan | 8050ef2 | 2017-09-06 18:36:22 -0600 | [diff] [blame] | 20 | TEST_GEN_PROGS_EXTENDED := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS_EXTENDED)) |
Michael Ellerman | d83c3ba | 2017-02-09 19:56:26 +1100 | [diff] [blame] | 21 | TEST_GEN_FILES := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_FILES)) |
| 22 | |
Shuah Khan | 211929f | 2018-12-12 20:25:14 -0700 | [diff] [blame] | 23 | ifdef KSFT_KHDR_INSTALL |
Anders Roxell | b2d35fa | 2018-09-04 12:47:21 +0200 | [diff] [blame] | 24 | top_srcdir ?= ../../../.. |
| 25 | include $(top_srcdir)/scripts/subarch.include |
| 26 | ARCH ?= $(SUBARCH) |
| 27 | |
Shuah Khan | 8ce72dc | 2019-04-15 15:51:42 -0600 | [diff] [blame] | 28 | # set default goal to all, so make without a target runs all, even when |
| 29 | # all isn't the first target in the file. |
| 30 | .DEFAULT_GOAL := all |
| 31 | |
| 32 | # Invoke headers install with --no-builtin-rules to avoid circular |
| 33 | # dependency in "make kselftest" case. In this case, second level |
| 34 | # make inherits builtin-rules which will use the rule generate |
| 35 | # Makefile.o and runs into |
| 36 | # "Circular Makefile.o <- prepare dependency dropped." |
| 37 | # and headers_install fails and test compile fails. |
| 38 | # O= KBUILD_OUTPUT cases don't run into this error, since main Makefile |
| 39 | # invokes them as sub-makes and --no-builtin-rules is not necessary, |
| 40 | # but doesn't cause any failures. Keep it simple and use the same |
| 41 | # flags in both cases. |
| 42 | # Note that the support to install headers from lib.mk is necessary |
| 43 | # when test Makefile is run directly with "make -C". |
| 44 | # When local build is done, headers are installed in the default |
| 45 | # INSTALL_HDR_PATH usr/include. |
Anders Roxell | b2d35fa | 2018-09-04 12:47:21 +0200 | [diff] [blame] | 46 | .PHONY: khdr |
| 47 | khdr: |
Shuah Khan | 8ce72dc | 2019-04-15 15:51:42 -0600 | [diff] [blame] | 48 | ifndef KSFT_KHDR_INSTALL_DONE |
| 49 | ifeq (1,$(DEFAULT_INSTALL_HDR_PATH)) |
Denys Vlasenko | 75fa6772 | 2020-08-17 17:09:46 +0200 | [diff] [blame] | 50 | $(MAKE) --no-builtin-rules ARCH=$(ARCH) -C $(top_srcdir) headers_install |
Shuah Khan | 8ce72dc | 2019-04-15 15:51:42 -0600 | [diff] [blame] | 51 | else |
Denys Vlasenko | 75fa6772 | 2020-08-17 17:09:46 +0200 | [diff] [blame] | 52 | $(MAKE) --no-builtin-rules INSTALL_HDR_PATH=$$OUTPUT/usr \ |
Shuah Khan | 8ce72dc | 2019-04-15 15:51:42 -0600 | [diff] [blame] | 53 | ARCH=$(ARCH) -C $(top_srcdir) headers_install |
| 54 | endif |
| 55 | endif |
Anders Roxell | b2d35fa | 2018-09-04 12:47:21 +0200 | [diff] [blame] | 56 | |
Shuah Khan | 211929f | 2018-12-12 20:25:14 -0700 | [diff] [blame] | 57 | all: khdr $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) |
| 58 | else |
| 59 | all: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) |
Anders Roxell | b2d35fa | 2018-09-04 12:47:21 +0200 | [diff] [blame] | 60 | endif |
| 61 | |
Michael Ellerman | 5e29a91 | 2015-03-11 15:05:59 +1100 | [diff] [blame] | 62 | define RUN_TESTS |
Yauheni Kaliuta | 99aaceb | 2020-05-27 10:16:57 +0300 | [diff] [blame] | 63 | BASE_DIR="$(selfdir)"; \ |
Kees Cook | 5c069b6 | 2019-04-24 16:12:35 -0700 | [diff] [blame] | 64 | . $(selfdir)/kselftest/runner.sh; \ |
Kees Cook | bf66078 | 2019-04-24 16:12:32 -0700 | [diff] [blame] | 65 | if [ "X$(summary)" != "X" ]; then \ |
| 66 | per_test_logging=1; \ |
| 67 | fi; \ |
| 68 | run_many $(1) |
Michael Ellerman | 5e29a91 | 2015-03-11 15:05:59 +1100 | [diff] [blame] | 69 | endef |
| 70 | |
| 71 | run_tests: all |
Masahiro Yamada | 051f278 | 2019-07-06 12:07:12 +0900 | [diff] [blame] | 72 | ifdef building_out_of_srctree |
Yauheni Kaliuta | c9f7504 | 2020-05-27 10:16:58 +0300 | [diff] [blame] | 73 | @if [ "X$(TEST_PROGS)$(TEST_PROGS_EXTENDED)$(TEST_FILES)" != "X" ]; then \ |
Yauheni Kaliuta | 99aaceb | 2020-05-27 10:16:57 +0300 | [diff] [blame] | 74 | rsync -aq $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(OUTPUT); \ |
Shuah Khan | 1a94068 | 2017-09-11 19:03:11 -0600 | [diff] [blame] | 75 | fi |
Yauheni Kaliuta | 99aaceb | 2020-05-27 10:16:57 +0300 | [diff] [blame] | 76 | @if [ "X$(TEST_PROGS)" != "X" ]; then \ |
| 77 | $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(OUTPUT)/$(TEST_PROGS)) ; \ |
| 78 | else \ |
| 79 | $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS)); \ |
Shuah Khan | 1a94068 | 2017-09-11 19:03:11 -0600 | [diff] [blame] | 80 | fi |
| 81 | else |
Yauheni Kaliuta | 99aaceb | 2020-05-27 10:16:57 +0300 | [diff] [blame] | 82 | @$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS)) |
Shuah Khan | 1a94068 | 2017-09-11 19:03:11 -0600 | [diff] [blame] | 83 | endif |
Michael Ellerman | 5e29a91 | 2015-03-11 15:05:59 +1100 | [diff] [blame] | 84 | |
Jiri Benc | c363eb4 | 2020-02-06 09:40:52 +0100 | [diff] [blame] | 85 | define INSTALL_SINGLE_RULE |
| 86 | $(if $(INSTALL_LIST),@mkdir -p $(INSTALL_PATH)) |
Yauheni Kaliuta | 99aaceb | 2020-05-27 10:16:57 +0300 | [diff] [blame] | 87 | $(if $(INSTALL_LIST),rsync -a $(INSTALL_LIST) $(INSTALL_PATH)/) |
Jiri Benc | c363eb4 | 2020-02-06 09:40:52 +0100 | [diff] [blame] | 88 | endef |
| 89 | |
Michael Ellerman | 32dcfba | 2015-03-11 15:06:00 +1100 | [diff] [blame] | 90 | define INSTALL_RULE |
Jiri Benc | c363eb4 | 2020-02-06 09:40:52 +0100 | [diff] [blame] | 91 | $(eval INSTALL_LIST = $(TEST_PROGS)) $(INSTALL_SINGLE_RULE) |
| 92 | $(eval INSTALL_LIST = $(TEST_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE) |
| 93 | $(eval INSTALL_LIST = $(TEST_FILES)) $(INSTALL_SINGLE_RULE) |
| 94 | $(eval INSTALL_LIST = $(TEST_GEN_PROGS)) $(INSTALL_SINGLE_RULE) |
| 95 | $(eval INSTALL_LIST = $(TEST_CUSTOM_PROGS)) $(INSTALL_SINGLE_RULE) |
| 96 | $(eval INSTALL_LIST = $(TEST_GEN_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE) |
| 97 | $(eval INSTALL_LIST = $(TEST_GEN_FILES)) $(INSTALL_SINGLE_RULE) |
Michael Ellerman | 32dcfba | 2015-03-11 15:06:00 +1100 | [diff] [blame] | 98 | endef |
| 99 | |
| 100 | install: all |
| 101 | ifdef INSTALL_PATH |
| 102 | $(INSTALL_RULE) |
| 103 | else |
| 104 | $(error Error: set INSTALL_PATH to use install) |
| 105 | endif |
| 106 | |
Kees Cook | d4e59a5 | 2019-04-24 16:12:31 -0700 | [diff] [blame] | 107 | emit_tests: |
Shuah Khan (Samsung OSG) | 3df6131 | 2018-05-01 12:15:34 -0600 | [diff] [blame] | 108 | 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] | 109 | BASENAME_TEST=`basename $$TEST`; \ |
Kees Cook | f0f0a5d | 2020-09-28 13:26:48 -0700 | [diff] [blame] | 110 | echo "$(COLLECTION):$$BASENAME_TEST"; \ |
| 111 | done |
Michael Ellerman | 32dcfba | 2015-03-11 15:06:00 +1100 | [diff] [blame] | 112 | |
Shuah Khan | 8050ef2 | 2017-09-06 18:36:22 -0600 | [diff] [blame] | 113 | # define if isn't already. It is undefined in make O= case. |
| 114 | ifeq ($(RM),) |
| 115 | RM := rm -f |
| 116 | endif |
| 117 | |
Shuah Khan | df6438f | 2017-04-21 16:07:51 -0600 | [diff] [blame] | 118 | define CLEAN |
bamvor.zhangjian@huawei.com | 80d443e8 | 2016-11-29 19:55:51 +0800 | [diff] [blame] | 119 | $(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] | 120 | endef |
| 121 | |
| 122 | clean: |
| 123 | $(CLEAN) |
bamvor.zhangjian@huawei.com | 88baa78 | 2016-11-29 19:55:47 +0800 | [diff] [blame] | 124 | |
Shuah Khan | 8050ef2 | 2017-09-06 18:36:22 -0600 | [diff] [blame] | 125 | # When make O= with kselftest target from main level |
| 126 | # the following aren't defined. |
| 127 | # |
Masahiro Yamada | 051f278 | 2019-07-06 12:07:12 +0900 | [diff] [blame] | 128 | ifdef building_out_of_srctree |
Shuah Khan | 8050ef2 | 2017-09-06 18:36:22 -0600 | [diff] [blame] | 129 | LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) |
| 130 | COMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c |
| 131 | LINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) |
| 132 | endif |
| 133 | |
Mathieu Desnoyers | 4e49ed2 | 2018-06-02 08:44:03 -0400 | [diff] [blame] | 134 | # Selftest makefiles can override those targets by setting |
| 135 | # OVERRIDE_TARGETS = 1. |
| 136 | ifeq ($(OVERRIDE_TARGETS),) |
Shuah Khan | 1056d3d2 | 2020-03-25 17:16:49 -0600 | [diff] [blame] | 137 | LOCAL_HDRS := $(selfdir)/kselftest_harness.h $(selfdir)/kselftest.h |
| 138 | $(OUTPUT)/%:%.c $(LOCAL_HDRS) |
Tommi Rantala | f825d3f | 2020-10-08 15:26:21 +0300 | [diff] [blame] | 139 | $(LINK.c) $(filter-out $(LOCAL_HDRS),$^) $(LDLIBS) -o $@ |
bamvor.zhangjian@huawei.com | a8ba798 | 2016-11-29 19:55:52 +0800 | [diff] [blame] | 140 | |
| 141 | $(OUTPUT)/%.o:%.S |
Michael Ellerman | 634ce97 | 2017-02-09 19:56:28 +1100 | [diff] [blame] | 142 | $(COMPILE.S) $^ -o $@ |
bamvor.zhangjian@huawei.com | a8ba798 | 2016-11-29 19:55:52 +0800 | [diff] [blame] | 143 | |
| 144 | $(OUTPUT)/%:%.S |
Michael Ellerman | 634ce97 | 2017-02-09 19:56:28 +1100 | [diff] [blame] | 145 | $(LINK.S) $^ $(LDLIBS) -o $@ |
Mathieu Desnoyers | 4e49ed2 | 2018-06-02 08:44:03 -0400 | [diff] [blame] | 146 | endif |
bamvor.zhangjian@huawei.com | 7d758af | 2016-11-29 19:55:49 +0800 | [diff] [blame] | 147 | |
Michael Ellerman | 32dcfba | 2015-03-11 15:06:00 +1100 | [diff] [blame] | 148 | .PHONY: run_tests all clean install emit_tests |