blob: a5ce26d548e4f37c67c9ea71f2eec470a7378c61 [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.
3CC := $(CROSS_COMPILE)gcc
4
Shuah Khane53aff42017-03-01 15:15:07 -07005ifeq (0,$(MAKELEVEL))
Shuah Khan27d79a22019-05-10 19:38:39 -06006 ifeq ($(OUTPUT),)
7 OUTPUT := $(shell pwd)
8 DEFAULT_INSTALL_HDR_PATH := 1
Shuah Khan8ce72dc2019-04-15 15:51:42 -06009 endif
Shuah Khane53aff42017-03-01 15:15:07 -070010endif
Kees Cook42d46e52019-04-24 16:12:30 -070011selfdir = $(realpath $(dir $(filter %/lib.mk,$(MAKEFILE_LIST))))
Shuah Khane53aff42017-03-01 15:15:07 -070012
Shuah Khanbe16a242017-09-11 13:06:33 -060013# 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 Ellermand83c3ba2017-02-09 19:56:26 +110019TEST_GEN_PROGS := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS))
Shuah Khan8050ef22017-09-06 18:36:22 -060020TEST_GEN_PROGS_EXTENDED := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS_EXTENDED))
Michael Ellermand83c3ba2017-02-09 19:56:26 +110021TEST_GEN_FILES := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_FILES))
22
Shuah Khan211929f2018-12-12 20:25:14 -070023ifdef KSFT_KHDR_INSTALL
Anders Roxellb2d35fa2018-09-04 12:47:21 +020024top_srcdir ?= ../../../..
25include $(top_srcdir)/scripts/subarch.include
26ARCH ?= $(SUBARCH)
27
Shuah Khan8ce72dc2019-04-15 15:51:42 -060028# 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 Roxellb2d35fa2018-09-04 12:47:21 +020046.PHONY: khdr
47khdr:
Shuah Khan8ce72dc2019-04-15 15:51:42 -060048ifndef KSFT_KHDR_INSTALL_DONE
49ifeq (1,$(DEFAULT_INSTALL_HDR_PATH))
Denys Vlasenko75fa67722020-08-17 17:09:46 +020050 $(MAKE) --no-builtin-rules ARCH=$(ARCH) -C $(top_srcdir) headers_install
Shuah Khan8ce72dc2019-04-15 15:51:42 -060051else
Denys Vlasenko75fa67722020-08-17 17:09:46 +020052 $(MAKE) --no-builtin-rules INSTALL_HDR_PATH=$$OUTPUT/usr \
Shuah Khan8ce72dc2019-04-15 15:51:42 -060053 ARCH=$(ARCH) -C $(top_srcdir) headers_install
54endif
55endif
Anders Roxellb2d35fa2018-09-04 12:47:21 +020056
Shuah Khan211929f2018-12-12 20:25:14 -070057all: khdr $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
58else
59all: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
Anders Roxellb2d35fa2018-09-04 12:47:21 +020060endif
61
Michael Ellerman5e29a912015-03-11 15:05:59 +110062define RUN_TESTS
Yauheni Kaliuta99aaceb2020-05-27 10:16:57 +030063 BASE_DIR="$(selfdir)"; \
Kees Cook5c069b62019-04-24 16:12:35 -070064 . $(selfdir)/kselftest/runner.sh; \
Kees Cookbf660782019-04-24 16:12:32 -070065 if [ "X$(summary)" != "X" ]; then \
66 per_test_logging=1; \
67 fi; \
68 run_many $(1)
Michael Ellerman5e29a912015-03-11 15:05:59 +110069endef
70
71run_tests: all
Masahiro Yamada051f2782019-07-06 12:07:12 +090072ifdef building_out_of_srctree
Yauheni Kaliutac9f75042020-05-27 10:16:58 +030073 @if [ "X$(TEST_PROGS)$(TEST_PROGS_EXTENDED)$(TEST_FILES)" != "X" ]; then \
Yauheni Kaliuta99aaceb2020-05-27 10:16:57 +030074 rsync -aq $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(OUTPUT); \
Shuah Khan1a940682017-09-11 19:03:11 -060075 fi
Yauheni Kaliuta99aaceb2020-05-27 10:16:57 +030076 @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 Khan1a940682017-09-11 19:03:11 -060080 fi
81else
Yauheni Kaliuta99aaceb2020-05-27 10:16:57 +030082 @$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS))
Shuah Khan1a940682017-09-11 19:03:11 -060083endif
Michael Ellerman5e29a912015-03-11 15:05:59 +110084
Jiri Bencc363eb42020-02-06 09:40:52 +010085define INSTALL_SINGLE_RULE
86 $(if $(INSTALL_LIST),@mkdir -p $(INSTALL_PATH))
Yauheni Kaliuta99aaceb2020-05-27 10:16:57 +030087 $(if $(INSTALL_LIST),rsync -a $(INSTALL_LIST) $(INSTALL_PATH)/)
Jiri Bencc363eb42020-02-06 09:40:52 +010088endef
89
Michael Ellerman32dcfba2015-03-11 15:06:00 +110090define INSTALL_RULE
Jiri Bencc363eb42020-02-06 09:40:52 +010091 $(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 Ellerman32dcfba2015-03-11 15:06:00 +110098endef
99
100install: all
101ifdef INSTALL_PATH
102 $(INSTALL_RULE)
103else
104 $(error Error: set INSTALL_PATH to use install)
105endif
106
Kees Cookd4e59a52019-04-24 16:12:31 -0700107emit_tests:
Shuah Khan (Samsung OSG)3df61312018-05-01 12:15:34 -0600108 for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \
bamvor.zhangjian@huawei.coma8ba7982016-11-29 19:55:52 +0800109 BASENAME_TEST=`basename $$TEST`; \
Kees Cookf0f0a5d2020-09-28 13:26:48 -0700110 echo "$(COLLECTION):$$BASENAME_TEST"; \
111 done
Michael Ellerman32dcfba2015-03-11 15:06:00 +1100112
Shuah Khan8050ef22017-09-06 18:36:22 -0600113# define if isn't already. It is undefined in make O= case.
114ifeq ($(RM),)
115RM := rm -f
116endif
117
Shuah Khandf6438f2017-04-21 16:07:51 -0600118define CLEAN
bamvor.zhangjian@huawei.com80d443e82016-11-29 19:55:51 +0800119 $(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
Shuah Khandf6438f2017-04-21 16:07:51 -0600120endef
121
122clean:
123 $(CLEAN)
bamvor.zhangjian@huawei.com88baa782016-11-29 19:55:47 +0800124
Shuah Khan8050ef22017-09-06 18:36:22 -0600125# When make O= with kselftest target from main level
126# the following aren't defined.
127#
Masahiro Yamada051f2782019-07-06 12:07:12 +0900128ifdef building_out_of_srctree
Shuah Khan8050ef22017-09-06 18:36:22 -0600129LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
130COMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
131LINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
132endif
133
Mathieu Desnoyers4e49ed22018-06-02 08:44:03 -0400134# Selftest makefiles can override those targets by setting
135# OVERRIDE_TARGETS = 1.
136ifeq ($(OVERRIDE_TARGETS),)
Shuah Khan1056d3d22020-03-25 17:16:49 -0600137LOCAL_HDRS := $(selfdir)/kselftest_harness.h $(selfdir)/kselftest.h
138$(OUTPUT)/%:%.c $(LOCAL_HDRS)
Tommi Rantalaf825d3f2020-10-08 15:26:21 +0300139 $(LINK.c) $(filter-out $(LOCAL_HDRS),$^) $(LDLIBS) -o $@
bamvor.zhangjian@huawei.coma8ba7982016-11-29 19:55:52 +0800140
141$(OUTPUT)/%.o:%.S
Michael Ellerman634ce972017-02-09 19:56:28 +1100142 $(COMPILE.S) $^ -o $@
bamvor.zhangjian@huawei.coma8ba7982016-11-29 19:55:52 +0800143
144$(OUTPUT)/%:%.S
Michael Ellerman634ce972017-02-09 19:56:28 +1100145 $(LINK.S) $^ $(LDLIBS) -o $@
Mathieu Desnoyers4e49ed22018-06-02 08:44:03 -0400146endif
bamvor.zhangjian@huawei.com7d758af2016-11-29 19:55:49 +0800147
Michael Ellerman32dcfba2015-03-11 15:06:00 +1100148.PHONY: run_tests all clean install emit_tests