1# SPDX-License-Identifier: GPL-2.0
2# Makefile for vm selftests
3
4include local_config.mk
5
6uname_M := $(shell uname -m 2>/dev/null || echo not)
7MACHINE ?= $(shell echo $(uname_M) | sed -e 's/aarch64.*/arm64/' -e 's/ppc64.*/ppc64/')
8
9# Without this, failed build products remain, with up-to-date timestamps,
10# thus tricking Make (and you!) into believing that All Is Well, in subsequent
11# make invocations:
12.DELETE_ON_ERROR:
13
14# Avoid accidental wrong builds, due to built-in rules working just a little
15# bit too well--but not quite as well as required for our situation here.
16#
17# In other words, "make userfaultfd" is supposed to fail to build at all,
18# because this Makefile only supports either "make" (all), or "make /full/path".
19# However,  the built-in rules, if not suppressed, will pick up CFLAGS and the
20# initial LDLIBS (but not the target-specific LDLIBS, because those are only
21# set for the full path target!). This causes it to get pretty far into building
22# things despite using incorrect values such as an *occasionally* incomplete
23# LDLIBS.
24MAKEFLAGS += --no-builtin-rules
25
26CFLAGS = -Wall -I ../../../../usr/include $(EXTRA_CFLAGS)
27LDLIBS = -lrt -lpthread
28TEST_GEN_FILES = compaction_test
29TEST_GEN_FILES += gup_test
30TEST_GEN_FILES += hmm-tests
31TEST_GEN_FILES += hugepage-mmap
32TEST_GEN_FILES += hugepage-mremap
33TEST_GEN_FILES += hugepage-shm
34TEST_GEN_FILES += khugepaged
35TEST_GEN_FILES += madv_populate
36TEST_GEN_FILES += map_fixed_noreplace
37TEST_GEN_FILES += map_hugetlb
38TEST_GEN_FILES += map_populate
39TEST_GEN_FILES += memfd_secret
40TEST_GEN_FILES += mlock-random-test
41TEST_GEN_FILES += mlock2-tests
42TEST_GEN_FILES += mremap_dontunmap
43TEST_GEN_FILES += mremap_test
44TEST_GEN_FILES += on-fault-limit
45TEST_GEN_FILES += thuge-gen
46TEST_GEN_FILES += transhuge-stress
47TEST_GEN_FILES += userfaultfd
48TEST_GEN_FILES += split_huge_page_test
49TEST_GEN_FILES += ksm_tests
50
51ifeq ($(MACHINE),x86_64)
52CAN_BUILD_I386 := $(shell ./../x86/check_cc.sh $(CC) ../x86/trivial_32bit_program.c -m32)
53CAN_BUILD_X86_64 := $(shell ./../x86/check_cc.sh $(CC) ../x86/trivial_64bit_program.c)
54CAN_BUILD_WITH_NOPIE := $(shell ./../x86/check_cc.sh $(CC) ../x86/trivial_program.c -no-pie)
55
56TARGETS := protection_keys
57BINARIES_32 := $(TARGETS:%=%_32)
58BINARIES_64 := $(TARGETS:%=%_64)
59
60ifeq ($(CAN_BUILD_WITH_NOPIE),1)
61CFLAGS += -no-pie
62endif
63
64ifeq ($(CAN_BUILD_I386),1)
65TEST_GEN_FILES += $(BINARIES_32)
66endif
67
68ifeq ($(CAN_BUILD_X86_64),1)
69TEST_GEN_FILES += $(BINARIES_64)
70endif
71else
72
73ifneq (,$(findstring $(MACHINE),ppc64))
74TEST_GEN_FILES += protection_keys
75endif
76
77endif
78
79ifneq (,$(filter $(MACHINE),arm64 ia64 mips64 parisc64 ppc64 riscv64 s390x sh64 sparc64 x86_64))
80TEST_GEN_FILES += va_128TBswitch
81TEST_GEN_FILES += virtual_address_range
82TEST_GEN_FILES += write_to_hugetlbfs
83endif
84
85TEST_PROGS := run_vmtests.sh
86
87TEST_FILES := test_vmalloc.sh
88
89KSFT_KHDR_INSTALL := 1
90include ../lib.mk
91
92ifeq ($(MACHINE),x86_64)
93BINARIES_32 := $(patsubst %,$(OUTPUT)/%,$(BINARIES_32))
94BINARIES_64 := $(patsubst %,$(OUTPUT)/%,$(BINARIES_64))
95
96define gen-target-rule-32
97$(1) $(1)_32: $(OUTPUT)/$(1)_32
98.PHONY: $(1) $(1)_32
99endef
100
101define gen-target-rule-64
102$(1) $(1)_64: $(OUTPUT)/$(1)_64
103.PHONY: $(1) $(1)_64
104endef
105
106ifeq ($(CAN_BUILD_I386),1)
107$(BINARIES_32): CFLAGS += -m32 -mxsave
108$(BINARIES_32): LDLIBS += -lrt -ldl -lm
109$(BINARIES_32): $(OUTPUT)/%_32: %.c
110	$(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(notdir $^) $(LDLIBS) -o $@
111$(foreach t,$(TARGETS),$(eval $(call gen-target-rule-32,$(t))))
112endif
113
114ifeq ($(CAN_BUILD_X86_64),1)
115$(BINARIES_64): CFLAGS += -m64 -mxsave
116$(BINARIES_64): LDLIBS += -lrt -ldl
117$(BINARIES_64): $(OUTPUT)/%_64: %.c
118	$(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(notdir $^) $(LDLIBS) -o $@
119$(foreach t,$(TARGETS),$(eval $(call gen-target-rule-64,$(t))))
120endif
121
122# x86_64 users should be encouraged to install 32-bit libraries
123ifeq ($(CAN_BUILD_I386)$(CAN_BUILD_X86_64),01)
124all: warn_32bit_failure
125
126warn_32bit_failure:
127	@echo "Warning: you seem to have a broken 32-bit build" 2>&1;		\
128	echo  "environment. This will reduce test coverage of 64-bit" 2>&1;	\
129	echo  "kernels. If you are using a Debian-like distribution," 2>&1;	\
130	echo  "try:"; 2>&1;							\
131	echo  "";								\
132	echo  "  apt-get install gcc-multilib libc6-i386 libc6-dev-i386";	\
133	echo  "";								\
134	echo  "If you are using a Fedora-like distribution, try:";		\
135	echo  "";								\
136	echo  "  yum install glibc-devel.*i686";				\
137	exit 0;
138endif
139endif
140
141$(OUTPUT)/mlock-random-test $(OUTPUT)/memfd_secret: LDLIBS += -lcap
142
143$(OUTPUT)/gup_test: ../../../../mm/gup_test.h
144
145$(OUTPUT)/hmm-tests: local_config.h
146
147# HMM_EXTRA_LIBS may get set in local_config.mk, or it may be left empty.
148$(OUTPUT)/hmm-tests: LDLIBS += $(HMM_EXTRA_LIBS)
149
150$(OUTPUT)/ksm_tests: LDLIBS += -lnuma
151
152local_config.mk local_config.h: check_config.sh
153	/bin/sh ./check_config.sh $(CC)
154
155EXTRA_CLEAN += local_config.mk local_config.h
156
157ifeq ($(HMM_EXTRA_LIBS),)
158all: warn_missing_hugelibs
159
160warn_missing_hugelibs:
161	@echo ; \
162	echo "Warning: missing libhugetlbfs support. Some HMM tests will be skipped." ; \
163	echo
164endif
165