kbuild: Make $(LLVM) more flexible
[linux-block.git] / tools / testing / selftests / lib.mk
CommitLineData
84f887bf
ME
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.
26e6dd10 3ifneq ($(LLVM),)
e9c28192
NC
4ifneq ($(filter %/,$(LLVM)),)
5LLVM_PREFIX := $(LLVM)
6else ifneq ($(filter -%,$(LLVM)),)
7LLVM_SUFFIX := $(LLVM)
8endif
9
10CC := $(LLVM_PREFIX)clang$(LLVM_SUFFIX)
26e6dd10 11else
84f887bf 12CC := $(CROSS_COMPILE)gcc
26e6dd10 13endif
84f887bf 14
e53aff45 15ifeq (0,$(MAKELEVEL))
27d79a2b
SK
16 ifeq ($(OUTPUT),)
17 OUTPUT := $(shell pwd)
18 DEFAULT_INSTALL_HDR_PATH := 1
8ce72dc3 19 endif
e53aff45 20endif
42d46e57 21selfdir = $(realpath $(dir $(filter %/lib.mk,$(MAKEFILE_LIST))))
e53aff45 22
be16a244
SK
23# The following are built by lib.mk common compile rules.
24# TEST_CUSTOM_PROGS should be used by tests that require
25# custom build rule and prevent common build rule use.
26# TEST_PROGS are for test shell scripts.
27# TEST_CUSTOM_PROGS and TEST_PROGS will be run by common run_tests
28# and install targets. Common clean doesn't touch them.
d83c3ba0 29TEST_GEN_PROGS := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS))
8050ef2b 30TEST_GEN_PROGS_EXTENDED := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS_EXTENDED))
d83c3ba0
ME
31TEST_GEN_FILES := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_FILES))
32
211929fd 33ifdef KSFT_KHDR_INSTALL
b2d35fa5
AR
34top_srcdir ?= ../../../..
35include $(top_srcdir)/scripts/subarch.include
36ARCH ?= $(SUBARCH)
37
8ce72dc3
SK
38# set default goal to all, so make without a target runs all, even when
39# all isn't the first target in the file.
40.DEFAULT_GOAL := all
41
42# Invoke headers install with --no-builtin-rules to avoid circular
43# dependency in "make kselftest" case. In this case, second level
44# make inherits builtin-rules which will use the rule generate
45# Makefile.o and runs into
46# "Circular Makefile.o <- prepare dependency dropped."
47# and headers_install fails and test compile fails.
48# O= KBUILD_OUTPUT cases don't run into this error, since main Makefile
49# invokes them as sub-makes and --no-builtin-rules is not necessary,
50# but doesn't cause any failures. Keep it simple and use the same
51# flags in both cases.
52# Note that the support to install headers from lib.mk is necessary
53# when test Makefile is run directly with "make -C".
54# When local build is done, headers are installed in the default
55# INSTALL_HDR_PATH usr/include.
b2d35fa5 56.PHONY: khdr
8914a7a2 57.NOTPARALLEL:
b2d35fa5 58khdr:
8ce72dc3
SK
59ifndef KSFT_KHDR_INSTALL_DONE
60ifeq (1,$(DEFAULT_INSTALL_HDR_PATH))
75fa6772 61 $(MAKE) --no-builtin-rules ARCH=$(ARCH) -C $(top_srcdir) headers_install
8ce72dc3 62else
75fa6772 63 $(MAKE) --no-builtin-rules INSTALL_HDR_PATH=$$OUTPUT/usr \
8ce72dc3
SK
64 ARCH=$(ARCH) -C $(top_srcdir) headers_install
65endif
66endif
b2d35fa5 67
211929fd
SK
68all: khdr $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
69else
70all: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES)
b2d35fa5
AR
71endif
72
5e29a910 73define RUN_TESTS
99aacebe 74 BASE_DIR="$(selfdir)"; \
5c069b6d 75 . $(selfdir)/kselftest/runner.sh; \
bf660782
KC
76 if [ "X$(summary)" != "X" ]; then \
77 per_test_logging=1; \
78 fi; \
79 run_many $(1)
5e29a910
ME
80endef
81
82run_tests: all
051f278e 83ifdef building_out_of_srctree
c9f75047 84 @if [ "X$(TEST_PROGS)$(TEST_PROGS_EXTENDED)$(TEST_FILES)" != "X" ]; then \
99aacebe 85 rsync -aq $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(OUTPUT); \
1a940687 86 fi
99aacebe 87 @if [ "X$(TEST_PROGS)" != "X" ]; then \
cb4969e6
IL
88 $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) \
89 $(addprefix $(OUTPUT)/,$(TEST_PROGS))) ; \
99aacebe
YK
90 else \
91 $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS)); \
1a940687
SK
92 fi
93else
99aacebe 94 @$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS))
1a940687 95endif
5e29a910 96
c363eb48
JB
97define INSTALL_SINGLE_RULE
98 $(if $(INSTALL_LIST),@mkdir -p $(INSTALL_PATH))
99aacebe 99 $(if $(INSTALL_LIST),rsync -a $(INSTALL_LIST) $(INSTALL_PATH)/)
c363eb48
JB
100endef
101
32dcfba6 102define INSTALL_RULE
c363eb48
JB
103 $(eval INSTALL_LIST = $(TEST_PROGS)) $(INSTALL_SINGLE_RULE)
104 $(eval INSTALL_LIST = $(TEST_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE)
105 $(eval INSTALL_LIST = $(TEST_FILES)) $(INSTALL_SINGLE_RULE)
106 $(eval INSTALL_LIST = $(TEST_GEN_PROGS)) $(INSTALL_SINGLE_RULE)
107 $(eval INSTALL_LIST = $(TEST_CUSTOM_PROGS)) $(INSTALL_SINGLE_RULE)
108 $(eval INSTALL_LIST = $(TEST_GEN_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE)
109 $(eval INSTALL_LIST = $(TEST_GEN_FILES)) $(INSTALL_SINGLE_RULE)
de53fa9b 110 $(eval INSTALL_LIST = $(wildcard config settings)) $(INSTALL_SINGLE_RULE)
32dcfba6
ME
111endef
112
113install: all
114ifdef INSTALL_PATH
115 $(INSTALL_RULE)
116else
117 $(error Error: set INSTALL_PATH to use install)
118endif
119
d4e59a53 120emit_tests:
3df6131f 121 for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \
a8ba798b 122 BASENAME_TEST=`basename $$TEST`; \
f0f0a5df
KC
123 echo "$(COLLECTION):$$BASENAME_TEST"; \
124 done
32dcfba6 125
8050ef2b
SK
126# define if isn't already. It is undefined in make O= case.
127ifeq ($(RM),)
128RM := rm -f
129endif
130
df6438f9 131define CLEAN
80d443e8 132 $(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
df6438f9
SK
133endef
134
135clean:
136 $(CLEAN)
88baa78d 137
8050ef2b
SK
138# When make O= with kselftest target from main level
139# the following aren't defined.
140#
051f278e 141ifdef building_out_of_srctree
8050ef2b
SK
142LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
143COMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
144LINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
145endif
146
4e49ed2f
MD
147# Selftest makefiles can override those targets by setting
148# OVERRIDE_TARGETS = 1.
149ifeq ($(OVERRIDE_TARGETS),)
a738a4ce 150LOCAL_HDRS += $(selfdir)/kselftest_harness.h $(selfdir)/kselftest.h
1056d3d2 151$(OUTPUT)/%:%.c $(LOCAL_HDRS)
f825d3f7 152 $(LINK.c) $(filter-out $(LOCAL_HDRS),$^) $(LDLIBS) -o $@
a8ba798b 153
154$(OUTPUT)/%.o:%.S
634ce97c 155 $(COMPILE.S) $^ -o $@
a8ba798b 156
157$(OUTPUT)/%:%.S
634ce97c 158 $(LINK.S) $^ $(LDLIBS) -o $@
4e49ed2f 159endif
7d758af2 160
32dcfba6 161.PHONY: run_tests all clean install emit_tests