blob: a40add31a2e3ff7272942b6e8a542863b4aba461 [file] [log] [blame]
Michael Ellerman84f887b2015-03-04 21:41:31 +11001# 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 Song26e6dd12021-04-13 08:34:13 -07003ifneq ($(LLVM),)
4CC := clang
5else
Michael Ellerman84f887b2015-03-04 21:41:31 +11006CC := $(CROSS_COMPILE)gcc
Yonghong Song26e6dd12021-04-13 08:34:13 -07007endif
Michael Ellerman84f887b2015-03-04 21:41:31 +11008
Shuah Khane53aff42017-03-01 15:15:07 -07009ifeq (0,$(MAKELEVEL))
Shuah Khan27d79a22019-05-10 19:38:39 -060010 ifeq ($(OUTPUT),)
11 OUTPUT := $(shell pwd)
12 DEFAULT_INSTALL_HDR_PATH := 1
Shuah Khan8ce72dc2019-04-15 15:51:42 -060013 endif
Shuah Khane53aff42017-03-01 15:15:07 -070014endif
Kees Cook42d46e52019-04-24 16:12:30 -070015selfdir = $(realpath $(dir $(filter %/lib.mk,$(MAKEFILE_LIST))))
Shuah Khane53aff42017-03-01 15:15:07 -070016
Shuah Khanbe16a242017-09-11 13:06:33 -060017# 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 Ellermand83c3ba2017-02-09 19:56:26 +110023TEST_GEN_PROGS := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS))
Shuah Khan8050ef22017-09-06 18:36:22 -060024TEST_GEN_PROGS_EXTENDED := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS_EXTENDED))
Michael Ellermand83c3ba2017-02-09 19:56:26 +110025TEST_GEN_FILES := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_FILES))
26
Shuah Khan211929f2018-12-12 20:25:14 -070027ifdef KSFT_KHDR_INSTALL
Anders Roxellb2d35fa2018-09-04 12:47:21 +020028top_srcdir ?= ../../../..
29include $(top_srcdir)/scripts/subarch.include
30ARCH ?= $(SUBARCH)
31
Shuah Khan8ce72dc2019-04-15 15:51:42 -060032# 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 Roxellb2d35fa2018-09-04 12:47:21 +020050.PHONY: khdr
Li Zhijian8914a7a2021-09-15 21:45:54 +080051.NOTPARALLEL:
Anders Roxellb2d35fa2018-09-04 12:47:21 +020052khdr:
Shuah Khan8ce72dc2019-04-15 15:51:42 -060053ifndef KSFT_KHDR_INSTALL_DONE
54ifeq (1,$(DEFAULT_INSTALL_HDR_PATH))
Denys Vlasenko75fa67722020-08-17 17:09:46 +020055 $(MAKE) --no-builtin-rules ARCH=$(ARCH) -C $(top_srcdir) headers_install
Shuah Khan8ce72dc2019-04-15 15:51:42 -060056else
Denys Vlasenko75fa67722020-08-17 17:09:46 +020057 $(MAKE) --no-builtin-rules INSTALL_HDR_PATH=$$OUTPUT/usr \
Shuah Khan8ce72dc2019-04-15 15:51:42 -060058 ARCH=$(ARCH) -C $(top_srcdir) headers_install
59endif
60endif
Anders Roxellb2d35fa2018-09-04 12:47:21 +020061
Shuah Khan211929f2018-12-12 20:25:14 -070062all: khdr $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
63else
64all: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
Anders Roxellb2d35fa2018-09-04 12:47:21 +020065endif
66
Michael Ellerman5e29a912015-03-11 15:05:59 +110067define RUN_TESTS
Yauheni Kaliuta99aaceb2020-05-27 10:16:57 +030068 BASE_DIR="$(selfdir)"; \
Kees Cook5c069b62019-04-24 16:12:35 -070069 . $(selfdir)/kselftest/runner.sh; \
Kees Cookbf660782019-04-24 16:12:32 -070070 if [ "X$(summary)" != "X" ]; then \
71 per_test_logging=1; \
72 fi; \
73 run_many $(1)
Michael Ellerman5e29a912015-03-11 15:05:59 +110074endef
75
76run_tests: all
Masahiro Yamada051f2782019-07-06 12:07:12 +090077ifdef building_out_of_srctree
Yauheni Kaliutac9f75042020-05-27 10:16:58 +030078 @if [ "X$(TEST_PROGS)$(TEST_PROGS_EXTENDED)$(TEST_FILES)" != "X" ]; then \
Yauheni Kaliuta99aaceb2020-05-27 10:16:57 +030079 rsync -aq $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(OUTPUT); \
Shuah Khan1a940682017-09-11 19:03:11 -060080 fi
Yauheni Kaliuta99aaceb2020-05-27 10:16:57 +030081 @if [ "X$(TEST_PROGS)" != "X" ]; then \
Ilya Leoshkevichcb4969e2021-03-03 01:44:20 +010082 $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) \
83 $(addprefix $(OUTPUT)/,$(TEST_PROGS))) ; \
Yauheni Kaliuta99aaceb2020-05-27 10:16:57 +030084 else \
85 $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS)); \
Shuah Khan1a940682017-09-11 19:03:11 -060086 fi
87else
Yauheni Kaliuta99aaceb2020-05-27 10:16:57 +030088 @$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS))
Shuah Khan1a940682017-09-11 19:03:11 -060089endif
Michael Ellerman5e29a912015-03-11 15:05:59 +110090
Jiri Bencc363eb42020-02-06 09:40:52 +010091define INSTALL_SINGLE_RULE
92 $(if $(INSTALL_LIST),@mkdir -p $(INSTALL_PATH))
Yauheni Kaliuta99aaceb2020-05-27 10:16:57 +030093 $(if $(INSTALL_LIST),rsync -a $(INSTALL_LIST) $(INSTALL_PATH)/)
Jiri Bencc363eb42020-02-06 09:40:52 +010094endef
95
Michael Ellerman32dcfba2015-03-11 15:06:00 +110096define INSTALL_RULE
Jiri Bencc363eb42020-02-06 09:40:52 +010097 $(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 Cookde53fa92021-05-26 20:17:54 -0700104 $(eval INSTALL_LIST = $(wildcard config settings)) $(INSTALL_SINGLE_RULE)
Michael Ellerman32dcfba2015-03-11 15:06:00 +1100105endef
106
107install: all
108ifdef INSTALL_PATH
109 $(INSTALL_RULE)
110else
111 $(error Error: set INSTALL_PATH to use install)
112endif
113
Kees Cookd4e59a52019-04-24 16:12:31 -0700114emit_tests:
Shuah Khan (Samsung OSG)3df61312018-05-01 12:15:34 -0600115 for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \
bamvor.zhangjian@huawei.coma8ba7982016-11-29 19:55:52 +0800116 BASENAME_TEST=`basename $$TEST`; \
Kees Cookf0f0a5d2020-09-28 13:26:48 -0700117 echo "$(COLLECTION):$$BASENAME_TEST"; \
118 done
Michael Ellerman32dcfba2015-03-11 15:06:00 +1100119
Shuah Khan8050ef22017-09-06 18:36:22 -0600120# define if isn't already. It is undefined in make O= case.
121ifeq ($(RM),)
122RM := rm -f
123endif
124
Shuah Khandf6438f2017-04-21 16:07:51 -0600125define CLEAN
bamvor.zhangjian@huawei.com80d443e82016-11-29 19:55:51 +0800126 $(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
Shuah Khandf6438f2017-04-21 16:07:51 -0600127endef
128
129clean:
130 $(CLEAN)
bamvor.zhangjian@huawei.com88baa782016-11-29 19:55:47 +0800131
Shuah Khan8050ef22017-09-06 18:36:22 -0600132# When make O= with kselftest target from main level
133# the following aren't defined.
134#
Masahiro Yamada051f2782019-07-06 12:07:12 +0900135ifdef building_out_of_srctree
Shuah Khan8050ef22017-09-06 18:36:22 -0600136LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
137COMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
138LINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
139endif
140
Mathieu Desnoyers4e49ed22018-06-02 08:44:03 -0400141# Selftest makefiles can override those targets by setting
142# OVERRIDE_TARGETS = 1.
143ifeq ($(OVERRIDE_TARGETS),)
Anders Roxella738a4c2021-11-05 17:25:29 +0100144LOCAL_HDRS += $(selfdir)/kselftest_harness.h $(selfdir)/kselftest.h
Shuah Khan1056d3d22020-03-25 17:16:49 -0600145$(OUTPUT)/%:%.c $(LOCAL_HDRS)
Tommi Rantalaf825d3f2020-10-08 15:26:21 +0300146 $(LINK.c) $(filter-out $(LOCAL_HDRS),$^) $(LDLIBS) -o $@
bamvor.zhangjian@huawei.coma8ba7982016-11-29 19:55:52 +0800147
148$(OUTPUT)/%.o:%.S
Michael Ellerman634ce972017-02-09 19:56:28 +1100149 $(COMPILE.S) $^ -o $@
bamvor.zhangjian@huawei.coma8ba7982016-11-29 19:55:52 +0800150
151$(OUTPUT)/%:%.S
Michael Ellerman634ce972017-02-09 19:56:28 +1100152 $(LINK.S) $^ $(LDLIBS) -o $@
Mathieu Desnoyers4e49ed22018-06-02 08:44:03 -0400153endif
bamvor.zhangjian@huawei.com7d758af2016-11-29 19:55:49 +0800154
Michael Ellerman32dcfba2015-03-11 15:06:00 +1100155.PHONY: run_tests all clean install emit_tests