xref: /linux/tools/testing/selftests/nolibc/Makefile.nolibc (revision bdcdb8ff5bf76da84b05e35d1bc119db6946093b)
1# SPDX-License-Identifier: GPL-2.0
2# Makefile for nolibc tests
3# we're in ".../tools/testing/selftests/nolibc"
4ifeq ($(srctree),)
5srctree := $(patsubst %/tools/testing/selftests/,%,$(dir $(CURDIR)))
6endif
7
8include $(srctree)/tools/scripts/utilities.mak
9# We need this for the "__cc-option" macro.
10include $(srctree)/scripts/Makefile.compiler
11
12ifneq ($(O),)
13ifneq ($(call is-absolute,$(O)),y)
14$(error Only absolute O= parameters are supported)
15endif
16objtree := $(O)
17else
18objtree ?= $(srctree)
19endif
20
21ifeq ($(ARCH),)
22include $(srctree)/scripts/subarch.include
23ARCH = $(SUBARCH)
24endif
25
26cc-option = $(call __cc-option, $(CC),$(CLANG_CROSS_FLAGS),$(1),$(2))
27
28# XARCH extends the kernel's ARCH with a few variants of the same
29# architecture that only differ by the configuration, the toolchain
30# and the Qemu program used. It is copied as-is into ARCH except for
31# a few specific values which are mapped like this:
32#
33#  XARCH        | ARCH      | config
34#  -------------|-----------|-------------------------
35#  ppc          | powerpc   | 32 bits
36#  ppc64        | powerpc   | 64 bits big endian
37#  ppc64le      | powerpc   | 64 bits little endian
38#
39# It is recommended to only use XARCH, though it does not harm if
40# ARCH is already set. For simplicity, ARCH is sufficient for all
41# architectures where both are equal.
42
43# configure default variants for target kernel supported architectures
44XARCH_powerpc    = ppc
45XARCH_mips       = mips32le
46XARCH_riscv      = riscv64
47XARCH            = $(or $(XARCH_$(ARCH)),$(ARCH))
48
49# map from user input variants to their kernel supported architectures
50ARCH_x32         = x86
51ARCH_armthumb    = arm
52ARCH_ppc         = powerpc
53ARCH_ppc64       = powerpc
54ARCH_ppc64le     = powerpc
55ARCH_mips32le    = mips
56ARCH_mips32be    = mips
57ARCH_mipsn32le   = mips
58ARCH_mipsn32be   = mips
59ARCH_mips64le    = mips
60ARCH_mips64be    = mips
61ARCH_riscv32     = riscv
62ARCH_riscv64     = riscv
63ARCH_s390x       = s390
64ARCH_sparc32     = sparc
65ARCH_sparc64     = sparc
66ARCH_sh4         = sh
67ARCH            := $(or $(ARCH_$(XARCH)),$(XARCH))
68
69# kernel image names by architecture
70IMAGE_i386       = arch/x86/boot/bzImage
71IMAGE_x86_64     = arch/x86/boot/bzImage
72IMAGE_x32        = arch/x86/boot/bzImage
73IMAGE_x86        = arch/x86/boot/bzImage
74IMAGE_arm64      = arch/arm64/boot/Image
75IMAGE_arm        = arch/arm/boot/zImage
76IMAGE_armthumb   = arch/arm/boot/zImage
77IMAGE_mips32le   = vmlinuz
78IMAGE_mips32be   = vmlinuz
79IMAGE_mipsn32le  = vmlinuz
80IMAGE_mipsn32be  = vmlinuz
81IMAGE_mips64le   = vmlinuz
82IMAGE_mips64be   = vmlinuz
83IMAGE_ppc64le    = arch/powerpc/boot/zImage
84IMAGE_riscv32    = arch/riscv/boot/Image
85IMAGE_riscv64    = arch/riscv/boot/Image
86IMAGE_s390x      = arch/s390/boot/bzImage
87IMAGE_loongarch  = arch/loongarch/boot/vmlinuz.efi
88IMAGE_sparc32    = arch/sparc/boot/image
89IMAGE_sparc64    = arch/sparc/boot/image
90IMAGE_sh4        = arch/sh/boot/zImage
91IMAGE            = $(objtree)/$(or $(IMAGE_$(XARCH)),vmlinux)
92IMAGE_NAME       = $(notdir $(IMAGE))
93
94# default kernel configurations that appear to be usable
95DEFCONFIG_arm        = multi_v7_defconfig
96DEFCONFIG_armthumb   = multi_v7_defconfig
97DEFCONFIG_mips32le   = malta_defconfig
98DEFCONFIG_mips32be   = malta_defconfig generic/eb.config
99DEFCONFIG_mipsn32le  = malta_defconfig generic/64r2.config
100DEFCONFIG_mipsn32be  = malta_defconfig generic/64r6.config generic/eb.config
101DEFCONFIG_mips64le   = malta_defconfig generic/64r6.config
102DEFCONFIG_mips64be   = malta_defconfig generic/64r2.config generic/eb.config
103DEFCONFIG_ppc        = pmac32_defconfig
104DEFCONFIG_ppc64      = powernv_be_defconfig
105DEFCONFIG_ppc64le    = powernv_defconfig
106DEFCONFIG_riscv32    = rv32_defconfig
107DEFCONFIG_sparc32    = sparc32_defconfig
108DEFCONFIG_sparc64    = sparc64_defconfig
109DEFCONFIG_m68k       = virt_defconfig
110DEFCONFIG_sh4        = rts7751r2dplus_defconfig
111DEFCONFIG            = $(or $(DEFCONFIG_$(XARCH)),defconfig)
112
113EXTRACONFIG_x32       = -e CONFIG_X86_X32_ABI
114EXTRACONFIG_arm       = -e CONFIG_NAMESPACES
115EXTRACONFIG_armthumb  = -e CONFIG_NAMESPACES
116EXTRACONFIG_m68k      = -e CONFIG_BLK_DEV_INITRD
117EXTRACONFIG_sh4       = -e CONFIG_BLK_DEV_INITRD -e CONFIG_CMDLINE_FROM_BOOTLOADER
118EXTRACONFIG           = $(EXTRACONFIG_$(XARCH))
119
120# optional tests to run (default = all)
121TEST =
122
123# QEMU_ARCH: arch names used by qemu
124QEMU_ARCH_x32        = x86_64
125QEMU_ARCH_x86        = x86_64
126QEMU_ARCH_arm64      = aarch64
127QEMU_ARCH_armthumb   = arm
128QEMU_ARCH_mips32le   = mipsel  # works with malta_defconfig
129QEMU_ARCH_mips32be   = mips
130QEMU_ARCH_mipsn32le  = mips64el
131QEMU_ARCH_mipsn32be  = mips64
132QEMU_ARCH_mips64le   = mips64el
133QEMU_ARCH_mips64be   = mips64
134QEMU_ARCH_ppc64le    = ppc64
135QEMU_ARCH_loongarch  = loongarch64
136QEMU_ARCH_sparc32    = sparc
137QEMU_ARCH            = $(or $(QEMU_ARCH_$(XARCH)),$(XARCH))
138
139QEMU_ARCH_USER_ppc64le = ppc64le
140QEMU_ARCH_USER_mipsn32le = mipsn32el
141QEMU_ARCH_USER_mipsn32be = mipsn32
142QEMU_ARCH_USER         = $(or $(QEMU_ARCH_USER_$(XARCH)),$(QEMU_ARCH))
143
144QEMU_BIOS_DIR = /usr/share/edk2/
145QEMU_BIOS_loongarch = $(QEMU_BIOS_DIR)/loongarch64/OVMF_CODE.fd
146
147ifneq ($(QEMU_BIOS_$(XARCH)),)
148QEMU_ARGS_BIOS = -bios $(QEMU_BIOS_$(XARCH))
149endif
150
151# QEMU_ARGS : some arch-specific args to pass to qemu
152QEMU_ARGS_i386       = -M pc -append "console=ttyS0,9600 i8042.noaux panic=-1 $(TEST:%=NOLIBC_TEST=%)"
153QEMU_ARGS_x86_64     = -M pc -append "console=ttyS0,9600 i8042.noaux panic=-1 $(TEST:%=NOLIBC_TEST=%)"
154QEMU_ARGS_x32        = -M pc -append "console=ttyS0,9600 i8042.noaux panic=-1 $(TEST:%=NOLIBC_TEST=%)"
155QEMU_ARGS_x86        = -M pc -append "console=ttyS0,9600 i8042.noaux panic=-1 $(TEST:%=NOLIBC_TEST=%)"
156QEMU_ARGS_arm64      = -M virt -cpu cortex-a53 -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)"
157QEMU_ARGS_arm        = -M virt -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)"
158QEMU_ARGS_armthumb   = -M virt -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)"
159QEMU_ARGS_mips32le   = -M malta -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)"
160QEMU_ARGS_mips32be   = -M malta -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)"
161QEMU_ARGS_mipsn32le  = -M malta -cpu 5KEc -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)"
162QEMU_ARGS_mipsn32be  = -M malta -cpu I6400 -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)"
163QEMU_ARGS_mips64le   = -M malta -cpu I6400 -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)"
164QEMU_ARGS_mips64be   = -M malta -cpu 5KEc -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)"
165QEMU_ARGS_ppc        = -M g3beige -append "console=ttyS0 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
166QEMU_ARGS_ppc64      = -M powernv -append "console=hvc0 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
167QEMU_ARGS_ppc64le    = -M powernv -append "console=hvc0 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
168QEMU_ARGS_riscv32    = -M virt -append "console=ttyS0 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
169QEMU_ARGS_riscv64    = -M virt -append "console=ttyS0 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
170QEMU_ARGS_s390x      = -M s390-ccw-virtio -append "console=ttyS0 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
171QEMU_ARGS_loongarch  = -M virt -append "console=ttyS0,115200 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
172QEMU_ARGS_sparc32    = -M SS-5 -m 256M -append "console=ttyS0,115200 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
173QEMU_ARGS_sparc64    = -M sun4u -append "console=ttyS0,115200 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
174QEMU_ARGS_m68k       = -M virt -append "console=ttyGF0,115200 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
175QEMU_ARGS_sh4        = -M r2d -serial file:/dev/stdout -append "console=ttySC1,115200 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
176QEMU_ARGS            = -m 1G $(QEMU_ARGS_$(XARCH)) $(QEMU_ARGS_BIOS) $(QEMU_ARGS_EXTRA)
177
178# OUTPUT is only set when run from the main makefile, otherwise
179# it defaults to this nolibc directory.
180OUTPUT ?= $(CURDIR)/
181
182ifeq ($(V),1)
183Q=
184else
185Q=@
186endif
187
188CFLAGS_i386 = $(call cc-option,-m32)
189CFLAGS_x32 = -mx32
190CFLAGS_arm = -marm
191CFLAGS_armthumb = -mthumb -march=armv6t2
192CFLAGS_ppc = -m32 -mbig-endian -mno-vsx $(call cc-option,-mmultiple)
193CFLAGS_ppc64 = -m64 -mbig-endian -mno-vsx $(call cc-option,-mmultiple)
194CFLAGS_ppc64le = -m64 -mlittle-endian -mno-vsx $(call cc-option,-mabi=elfv2)
195CFLAGS_s390x = -m64
196CFLAGS_mips32le = -EL -mabi=32 -fPIC
197CFLAGS_mips32be = -EB -mabi=32
198CFLAGS_mipsn32le = -EL -mabi=n32 -fPIC -march=mips64r2
199CFLAGS_mipsn32be = -EB -mabi=n32 -march=mips64r6
200CFLAGS_mips64le = -EL -mabi=64 -march=mips64r6
201CFLAGS_mips64be = -EB -mabi=64 -march=mips64r2
202CFLAGS_loongarch = $(if $(LLVM),-fuse-ld=lld)
203CFLAGS_sparc32 = $(call cc-option,-m32) -mcpu=v8
204CFLAGS_sh4 = -ml -m4
205ifeq ($(origin XARCH),command line)
206CFLAGS_XARCH = $(CFLAGS_$(XARCH))
207endif
208
209LDLIBS_ppc = $(if $(LLVM),,-lgcc)
210LDLIBS = $(LDLIBS_$(XARCH))
211
212include Makefile.include
213
214CFLAGS  ?= $(CFLAGS_NOLIBC_TEST) $(CFLAGS_XARCH) $(CFLAGS_EXTRA)
215LDFLAGS :=
216
217# Modify CFLAGS based on LLVM=
218include $(srctree)/tools/scripts/Makefile.include
219
220REPORT  ?= awk '/\[OK\][\r]*$$/{p++} /\[FAIL\][\r]*$$/{if (!f) printf("\n"); f++; print;} /\[SKIPPED\][\r]*$$/{s++} \
221		/^Total number of errors:/{done++} \
222		END{ printf("\n%3d test(s): %3d passed, %3d skipped, %3d failed => status: ", p+s+f, p, s, f); \
223		if (f || !p || !done) printf("failure\n"); else if (s) printf("warning\n"); else printf("success\n");; \
224		printf("\nSee all results in %s\n", ARGV[1]); }'
225
226# Execute the toplevel kernel Makefile
227KBUILD_MAKE = $(MAKE) -C $(srctree) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) LLVM=
228
229help:
230	@echo "Supported targets under selftests/nolibc:"
231	@echo "  all               call the \"run\" target below"
232	@echo "  help              this help"
233	@echo "  sysroot           create the nolibc sysroot here (uses \$$ARCH)"
234	@echo "  nolibc-test       build the executable (uses \$$CC or \$$CROSS_COMPILE)"
235	@echo "  libc-test         build an executable using the compiler's default libc instead"
236	@echo "  run-user          runs the executable under QEMU (uses \$$XARCH, \$$TEST)"
237	@echo "  initramfs.cpio    prepare the initramfs archive with nolibc-test"
238	@echo "  initramfs         prepare the initramfs tree with nolibc-test"
239	@echo "  defconfig         create a fresh new default config (uses \$$XARCH)"
240	@echo "  kernel            (re)build the kernel (uses \$$XARCH, \$$CROSS_COMPILE)"
241	@echo "  kernel-standalone (re)build the kernel with the initramfs (uses \$$XARCH, \$$CROSS_COMPILE)"
242	@echo "  run               runs the kernel in QEMU after building it (uses \$$XARCH, \$$TEST)"
243	@echo "  rerun             runs a previously prebuilt kernel in QEMU (uses \$$XARCH, \$$TEST)"
244	@echo "  clean             clean the sysroot, initramfs, build and output files"
245	@echo ""
246	@echo "The output file is \"run.out\". Test ranges may be passed using \$$TEST."
247	@echo ""
248	@echo "Currently using the following variables:"
249	@echo "  ARCH          = $(ARCH)"
250	@echo "  XARCH         = $(XARCH)"
251	@echo "  CROSS_COMPILE = $(CROSS_COMPILE)"
252	@echo "  CC            = $(CC)"
253	@echo "  OUTPUT        = $(OUTPUT)"
254	@echo "  TEST          = $(TEST)"
255	@echo "  QEMU_ARCH     = $(if $(QEMU_ARCH),$(QEMU_ARCH),UNKNOWN_ARCH) [determined from \$$XARCH]"
256	@echo "  IMAGE_NAME    = $(if $(IMAGE_NAME),$(IMAGE_NAME),UNKNOWN_ARCH) [determined from \$$XARCH]"
257	@echo ""
258
259all: run
260
261sysroot: sysroot/$(ARCH)/include
262
263sysroot/$(ARCH)/include:
264	$(Q)rm -rf sysroot/$(ARCH) sysroot/sysroot
265	$(QUIET_MKDIR)mkdir -p sysroot
266	$(Q)$(MAKE) -C $(srctree) outputmakefile
267	$(Q)$(MAKE) -C $(srctree)/tools/include/nolibc ARCH=$(ARCH) OUTPUT=$(CURDIR)/sysroot/ headers_standalone headers_check
268	$(Q)mv sysroot/sysroot sysroot/$(ARCH)
269
270nolibc-test: $(NOLIBC_TEST_SOURCES) sysroot/$(ARCH)/include
271	$(QUIET_CC)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ \
272	  -nostdlib -nostdinc -static -Isysroot/$(ARCH)/include $(NOLIBC_TEST_SOURCES) $(LDLIBS)
273
274libc-test: $(NOLIBC_TEST_SOURCES)
275	$(QUIET_CC)$(HOSTCC) -o $@ $(NOLIBC_TEST_SOURCES)
276
277# local libc-test
278run-libc-test: libc-test
279	$(Q)./libc-test > "$(CURDIR)/run.out" || :
280	$(Q)$(REPORT) $(CURDIR)/run.out
281
282# local nolibc-test
283run-nolibc-test: nolibc-test
284	$(Q)./nolibc-test > "$(CURDIR)/run.out" || :
285	$(Q)$(REPORT) $(CURDIR)/run.out
286
287# qemu user-land test
288run-user: nolibc-test
289	$(Q)qemu-$(QEMU_ARCH_USER) ./nolibc-test > "$(CURDIR)/run.out" || :
290	$(Q)$(REPORT) $(CURDIR)/run.out
291
292initramfs.cpio: kernel nolibc-test
293	$(QUIET_GEN)echo 'file /init nolibc-test 755 0 0' | $(objtree)/usr/gen_init_cpio - > initramfs.cpio
294
295initramfs: nolibc-test
296	$(QUIET_MKDIR)mkdir -p initramfs
297	$(call QUIET_INSTALL, initramfs/init)
298	$(Q)cp nolibc-test initramfs/init
299
300defconfig:
301	$(Q)$(KBUILD_MAKE) $(DEFCONFIG)
302	$(Q)if [ -n "$(EXTRACONFIG)" ]; then \
303		$(srctree)/scripts/config --file $(objtree)/.config $(EXTRACONFIG); \
304		$(KBUILD_MAKE) olddefconfig < /dev/null; \
305	fi
306
307kernel:
308	$(Q)$(KBUILD_MAKE) $(IMAGE_NAME) < /dev/null
309
310kernel-standalone: initramfs
311	$(Q)$(KBUILD_MAKE) $(IMAGE_NAME) CONFIG_INITRAMFS_SOURCE=$(CURDIR)/initramfs < /dev/null
312
313# run the tests after building the kernel
314run: kernel initramfs.cpio
315	$(Q)qemu-system-$(QEMU_ARCH) -display none -no-reboot -kernel "$(IMAGE)" -initrd initramfs.cpio -serial file:/dev/stdout $(QEMU_ARGS) > "$(CURDIR)/run.out"
316	$(Q)$(REPORT) $(CURDIR)/run.out
317
318# re-run the tests from an existing kernel
319rerun:
320	$(Q)qemu-system-$(QEMU_ARCH) -display none -no-reboot -kernel "$(IMAGE)" -initrd initramfs.cpio -serial file:/dev/stdout $(QEMU_ARGS) > "$(CURDIR)/run.out"
321	$(Q)$(REPORT) $(CURDIR)/run.out
322
323# report with existing test log
324report:
325	$(Q)$(REPORT) $(CURDIR)/run.out
326
327clean:
328	$(call QUIET_CLEAN, sysroot)
329	$(Q)rm -rf sysroot
330	$(call QUIET_CLEAN, nolibc-test)
331	$(Q)rm -f nolibc-test
332	$(call QUIET_CLEAN, libc-test)
333	$(Q)rm -f libc-test
334	$(call QUIET_CLEAN, initramfs.cpio)
335	$(Q)rm -rf initramfs.cpio
336	$(call QUIET_CLEAN, initramfs)
337	$(Q)rm -rf initramfs
338	$(call QUIET_CLEAN, run.out)
339	$(Q)rm -rf run.out
340
341.PHONY: sysroot/$(ARCH)/include
342