Loading...
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.
3ifneq ($(LLVM),)
4ifneq ($(filter %/,$(LLVM)),)
5LLVM_PREFIX := $(LLVM)
6else ifneq ($(filter -%,$(LLVM)),)
7LLVM_SUFFIX := $(LLVM)
8endif
9
10CLANG_TARGET_FLAGS_arm := arm-linux-gnueabi
11CLANG_TARGET_FLAGS_arm64 := aarch64-linux-gnu
12CLANG_TARGET_FLAGS_hexagon := hexagon-linux-musl
13CLANG_TARGET_FLAGS_m68k := m68k-linux-gnu
14CLANG_TARGET_FLAGS_mips := mipsel-linux-gnu
15CLANG_TARGET_FLAGS_powerpc := powerpc64le-linux-gnu
16CLANG_TARGET_FLAGS_riscv := riscv64-linux-gnu
17CLANG_TARGET_FLAGS_s390 := s390x-linux-gnu
18CLANG_TARGET_FLAGS_x86 := x86_64-linux-gnu
19CLANG_TARGET_FLAGS := $(CLANG_TARGET_FLAGS_$(ARCH))
20
21ifeq ($(CROSS_COMPILE),)
22ifeq ($(CLANG_TARGET_FLAGS),)
23$(error Specify CROSS_COMPILE or add '--target=' option to lib.mk)
24else
25CLANG_FLAGS += --target=$(CLANG_TARGET_FLAGS)
26endif # CLANG_TARGET_FLAGS
27else
28CLANG_FLAGS += --target=$(notdir $(CROSS_COMPILE:%-=%))
29endif # CROSS_COMPILE
30
31CC := $(LLVM_PREFIX)clang$(LLVM_SUFFIX) $(CLANG_FLAGS) -fintegrated-as
32else
33CC := $(CROSS_COMPILE)gcc
34endif # LLVM
35
36ifeq (0,$(MAKELEVEL))
37 ifeq ($(OUTPUT),)
38 OUTPUT := $(shell pwd)
39 DEFAULT_INSTALL_HDR_PATH := 1
40 endif
41endif
42selfdir = $(realpath $(dir $(filter %/lib.mk,$(MAKEFILE_LIST))))
43top_srcdir = $(selfdir)/../../..
44
45ifeq ($(KHDR_INCLUDES),)
46KHDR_INCLUDES := -isystem $(top_srcdir)/usr/include
47endif
48
49# The following are built by lib.mk common compile rules.
50# TEST_CUSTOM_PROGS should be used by tests that require
51# custom build rule and prevent common build rule use.
52# TEST_PROGS are for test shell scripts.
53# TEST_CUSTOM_PROGS and TEST_PROGS will be run by common run_tests
54# and install targets. Common clean doesn't touch them.
55TEST_GEN_PROGS := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS))
56TEST_GEN_PROGS_EXTENDED := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS_EXTENDED))
57TEST_GEN_FILES := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_FILES))
58
59all: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
60
61define RUN_TESTS
62 BASE_DIR="$(selfdir)"; \
63 . $(selfdir)/kselftest/runner.sh; \
64 if [ "X$(summary)" != "X" ]; then \
65 per_test_logging=1; \
66 fi; \
67 run_many $(1)
68endef
69
70run_tests: all
71ifdef building_out_of_srctree
72 @if [ "X$(TEST_PROGS)$(TEST_PROGS_EXTENDED)$(TEST_FILES)" != "X" ]; then \
73 rsync -aLq $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(OUTPUT); \
74 fi
75 @if [ "X$(TEST_PROGS)" != "X" ]; then \
76 $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) \
77 $(addprefix $(OUTPUT)/,$(TEST_PROGS))) ; \
78 else \
79 $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS)); \
80 fi
81else
82 @$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS))
83endif
84
85define INSTALL_SINGLE_RULE
86 $(if $(INSTALL_LIST),@mkdir -p $(INSTALL_PATH))
87 $(if $(INSTALL_LIST),rsync -aL $(INSTALL_LIST) $(INSTALL_PATH)/)
88endef
89
90define INSTALL_RULE
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)
98 $(eval INSTALL_LIST = $(wildcard config settings)) $(INSTALL_SINGLE_RULE)
99endef
100
101install: all
102ifdef INSTALL_PATH
103 $(INSTALL_RULE)
104else
105 $(error Error: set INSTALL_PATH to use install)
106endif
107
108emit_tests:
109 for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \
110 BASENAME_TEST=`basename $$TEST`; \
111 echo "$(COLLECTION):$$BASENAME_TEST"; \
112 done
113
114# define if isn't already. It is undefined in make O= case.
115ifeq ($(RM),)
116RM := rm -f
117endif
118
119define CLEAN
120 $(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
121endef
122
123clean:
124 $(CLEAN)
125
126# Enables to extend CFLAGS and LDFLAGS from command line, e.g.
127# make USERCFLAGS=-Werror USERLDFLAGS=-static
128CFLAGS += $(USERCFLAGS)
129LDFLAGS += $(USERLDFLAGS)
130
131# When make O= with kselftest target from main level
132# the following aren't defined.
133#
134ifdef building_out_of_srctree
135LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
136COMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
137LINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
138endif
139
140# Selftest makefiles can override those targets by setting
141# OVERRIDE_TARGETS = 1.
142ifeq ($(OVERRIDE_TARGETS),)
143LOCAL_HDRS += $(selfdir)/kselftest_harness.h $(selfdir)/kselftest.h
144$(OUTPUT)/%:%.c $(LOCAL_HDRS)
145 $(LINK.c) $(filter-out $(LOCAL_HDRS),$^) $(LDLIBS) -o $@
146
147$(OUTPUT)/%.o:%.S
148 $(COMPILE.S) $^ -o $@
149
150$(OUTPUT)/%:%.S
151 $(LINK.S) $^ $(LDLIBS) -o $@
152endif
153
154.PHONY: run_tests all clean install emit_tests
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.
3CC := $(CROSS_COMPILE)gcc
4
5ifeq (0,$(MAKELEVEL))
6 ifeq ($(OUTPUT),)
7 OUTPUT := $(shell pwd)
8 DEFAULT_INSTALL_HDR_PATH := 1
9 endif
10endif
11selfdir = $(realpath $(dir $(filter %/lib.mk,$(MAKEFILE_LIST))))
12
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.
19TEST_GEN_PROGS := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS))
20TEST_GEN_PROGS_EXTENDED := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS_EXTENDED))
21TEST_GEN_FILES := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_FILES))
22
23ifdef KSFT_KHDR_INSTALL
24top_srcdir ?= ../../../..
25include $(top_srcdir)/scripts/subarch.include
26ARCH ?= $(SUBARCH)
27
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.
46.PHONY: khdr
47khdr:
48ifndef KSFT_KHDR_INSTALL_DONE
49ifeq (1,$(DEFAULT_INSTALL_HDR_PATH))
50 make --no-builtin-rules ARCH=$(ARCH) -C $(top_srcdir) headers_install
51else
52 make --no-builtin-rules INSTALL_HDR_PATH=$$OUTPUT/usr \
53 ARCH=$(ARCH) -C $(top_srcdir) headers_install
54endif
55endif
56
57all: khdr $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
58else
59all: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
60endif
61
62.ONESHELL:
63define RUN_TESTS
64 @BASE_DIR="$(selfdir)"; \
65 . $(selfdir)/kselftest/runner.sh; \
66 if [ "X$(summary)" != "X" ]; then \
67 per_test_logging=1; \
68 fi; \
69 run_many $(1)
70endef
71
72run_tests: all
73ifdef building_out_of_srctree
74 @if [ "X$(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES)" != "X" ]; then
75 @rsync -aq $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(OUTPUT)
76 fi
77 @if [ "X$(TEST_PROGS)" != "X" ]; then
78 $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(OUTPUT)/$(TEST_PROGS))
79 else
80 $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS))
81 fi
82else
83 $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS))
84endif
85
86define INSTALL_RULE
87 @if [ "X$(TEST_PROGS)$(TEST_PROGS_EXTENDED)$(TEST_FILES)" != "X" ]; then \
88 mkdir -p ${INSTALL_PATH}; \
89 echo "rsync -a $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(INSTALL_PATH)/"; \
90 rsync -a $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(INSTALL_PATH)/; \
91 fi
92 @if [ "X$(TEST_GEN_PROGS)$(TEST_CUSTOM_PROGS)$(TEST_GEN_PROGS_EXTENDED)$(TEST_GEN_FILES)" != "X" ]; then \
93 mkdir -p ${INSTALL_PATH}; \
94 echo "rsync -a $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(INSTALL_PATH)/"; \
95 rsync -a $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(INSTALL_PATH)/; \
96 fi
97endef
98
99install: all
100ifdef INSTALL_PATH
101 $(INSTALL_RULE)
102else
103 $(error Error: set INSTALL_PATH to use install)
104endif
105
106emit_tests:
107 for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \
108 BASENAME_TEST=`basename $$TEST`; \
109 echo " \\"; \
110 echo -n " \"$$BASENAME_TEST\""; \
111 done; \
112
113# define if isn't already. It is undefined in make O= case.
114ifeq ($(RM),)
115RM := rm -f
116endif
117
118define CLEAN
119 $(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
120endef
121
122clean:
123 $(CLEAN)
124
125# When make O= with kselftest target from main level
126# the following aren't defined.
127#
128ifdef building_out_of_srctree
129LINK.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
134# Selftest makefiles can override those targets by setting
135# OVERRIDE_TARGETS = 1.
136ifeq ($(OVERRIDE_TARGETS),)
137$(OUTPUT)/%:%.c
138 $(LINK.c) $^ $(LDLIBS) -o $@
139
140$(OUTPUT)/%.o:%.S
141 $(COMPILE.S) $^ -o $@
142
143$(OUTPUT)/%:%.S
144 $(LINK.S) $^ $(LDLIBS) -o $@
145endif
146
147.PHONY: run_tests all clean install emit_tests