1# SPDX-License-Identifier: GPL-2.0
2all:
3
4include ../lib.mk
5
6.PHONY: all all_32 all_64 warn_32bit_failure clean
7
8UNAME_M := $(shell uname -m)
9CAN_BUILD_I386 := $(shell ./check_cc.sh $(CC) trivial_32bit_program.c -m32)
10CAN_BUILD_X86_64 := $(shell ./check_cc.sh $(CC) trivial_64bit_program.c)
11
12TARGETS_C_BOTHBITS := single_step_syscall sysret_ss_attrs syscall_nt test_mremap_vdso \
13			check_initial_reg_state sigreturn iopl mpx-mini-test ioperm \
14			protection_keys test_vdso test_vsyscall
15TARGETS_C_32BIT_ONLY := entry_from_vm86 syscall_arg_fault test_syscall_vdso unwind_vdso \
16			test_FCMOV test_FCOMI test_FISTTP \
17			vdso_restorer
18TARGETS_C_64BIT_ONLY := fsgsbase sysret_rip
19# Some selftests require 32bit support enabled also on 64bit systems
20TARGETS_C_32BIT_NEEDED := ldt_gdt ptrace_syscall
21
22TARGETS_C_32BIT_ALL := $(TARGETS_C_BOTHBITS) $(TARGETS_C_32BIT_ONLY) $(TARGETS_C_32BIT_NEEDED)
23TARGETS_C_64BIT_ALL := $(TARGETS_C_BOTHBITS) $(TARGETS_C_64BIT_ONLY)
24ifeq ($(CAN_BUILD_I386)$(CAN_BUILD_X86_64),11)
25TARGETS_C_64BIT_ALL += $(TARGETS_C_32BIT_NEEDED)
26endif
27
28BINARIES_32 := $(TARGETS_C_32BIT_ALL:%=%_32)
29BINARIES_64 := $(TARGETS_C_64BIT_ALL:%=%_64)
30
31BINARIES_32 := $(patsubst %,$(OUTPUT)/%,$(BINARIES_32))
32BINARIES_64 := $(patsubst %,$(OUTPUT)/%,$(BINARIES_64))
33
34CFLAGS := -O2 -g -std=gnu99 -pthread -Wall -no-pie
35
36define gen-target-rule-32
37$(1) $(1)_32: $(OUTPUT)/$(1)_32
38.PHONY: $(1) $(1)_32
39endef
40
41define gen-target-rule-64
42$(1) $(1)_64: $(OUTPUT)/$(1)_64
43.PHONY: $(1) $(1)_64
44endef
45
46ifeq ($(CAN_BUILD_I386),1)
47all: all_32
48TEST_PROGS += $(BINARIES_32)
49EXTRA_CFLAGS += -DCAN_BUILD_32
50$(foreach t,$(TARGETS_C_32BIT_ALL),$(eval $(call gen-target-rule-32,$(t))))
51endif
52
53ifeq ($(CAN_BUILD_X86_64),1)
54all: all_64
55TEST_PROGS += $(BINARIES_64)
56EXTRA_CFLAGS += -DCAN_BUILD_64
57$(foreach t,$(TARGETS_C_64BIT_ALL),$(eval $(call gen-target-rule-64,$(t))))
58endif
59
60all_32: $(BINARIES_32)
61
62all_64: $(BINARIES_64)
63
64EXTRA_CLEAN := $(BINARIES_32) $(BINARIES_64)
65
66$(BINARIES_32): $(OUTPUT)/%_32: %.c
67	$(CC) -m32 -o $@ $(CFLAGS) $(EXTRA_CFLAGS) $^ -lrt -ldl -lm
68
69$(BINARIES_64): $(OUTPUT)/%_64: %.c
70	$(CC) -m64 -o $@ $(CFLAGS) $(EXTRA_CFLAGS) $^ -lrt -ldl
71
72# x86_64 users should be encouraged to install 32-bit libraries
73ifeq ($(CAN_BUILD_I386)$(CAN_BUILD_X86_64),01)
74all: warn_32bit_failure
75
76warn_32bit_failure:
77	@echo "Warning: you seem to have a broken 32-bit build" 2>&1; 	\
78	echo "environment.  This will reduce test coverage of 64-bit" 2>&1; \
79	echo "kernels.  If you are using a Debian-like distribution," 2>&1; \
80	echo "try:"; 2>&1; \
81	echo "";							\
82	echo "  apt-get install gcc-multilib libc6-i386 libc6-dev-i386"; \
83	echo "";							\
84	echo "If you are using a Fedora-like distribution, try:";	\
85	echo "";							\
86	echo "  yum install glibc-devel.*i686";				\
87	exit 0;
88endif
89
90# Some tests have additional dependencies.
91$(OUTPUT)/sysret_ss_attrs_64: thunks.S
92$(OUTPUT)/ptrace_syscall_32: raw_syscall_helper_32.S
93$(OUTPUT)/test_syscall_vdso_32: thunks_32.S
94
95# check_initial_reg_state is special: it needs a custom entry, and it
96# needs to be static so that its interpreter doesn't destroy its initial
97# state.
98$(OUTPUT)/check_initial_reg_state_32: CFLAGS += -Wl,-ereal_start -static
99$(OUTPUT)/check_initial_reg_state_64: CFLAGS += -Wl,-ereal_start -static
100