xref: /linux/tools/testing/selftests/nolibc/Makefile.nolibc (revision b1c21075d30c40762750be0cded9822791df422b)
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_ppc        = vmlinux
84IMAGE_ppc64      = vmlinux
85IMAGE_ppc64le    = arch/powerpc/boot/zImage
86IMAGE_riscv      = arch/riscv/boot/Image
87IMAGE_riscv32    = arch/riscv/boot/Image
88IMAGE_riscv64    = arch/riscv/boot/Image
89IMAGE_s390x      = arch/s390/boot/bzImage
90IMAGE_s390       = arch/s390/boot/bzImage
91IMAGE_loongarch  = arch/loongarch/boot/vmlinuz.efi
92IMAGE_sparc32    = arch/sparc/boot/image
93IMAGE_sparc64    = arch/sparc/boot/image
94IMAGE_m68k       = vmlinux
95IMAGE_sh4        = arch/sh/boot/zImage
96IMAGE            = $(objtree)/$(IMAGE_$(XARCH))
97IMAGE_NAME       = $(notdir $(IMAGE))
98
99# default kernel configurations that appear to be usable
100DEFCONFIG_i386       = defconfig
101DEFCONFIG_x86_64     = defconfig
102DEFCONFIG_x32        = defconfig
103DEFCONFIG_x86        = defconfig
104DEFCONFIG_arm64      = defconfig
105DEFCONFIG_arm        = multi_v7_defconfig
106DEFCONFIG_armthumb   = multi_v7_defconfig
107DEFCONFIG_mips32le   = malta_defconfig
108DEFCONFIG_mips32be   = malta_defconfig generic/eb.config
109DEFCONFIG_mipsn32le  = malta_defconfig generic/64r2.config
110DEFCONFIG_mipsn32be  = malta_defconfig generic/64r6.config generic/eb.config
111DEFCONFIG_mips64le   = malta_defconfig generic/64r6.config
112DEFCONFIG_mips64be   = malta_defconfig generic/64r2.config generic/eb.config
113DEFCONFIG_ppc        = pmac32_defconfig
114DEFCONFIG_ppc64      = powernv_be_defconfig
115DEFCONFIG_ppc64le    = powernv_defconfig
116DEFCONFIG_riscv      = defconfig
117DEFCONFIG_riscv32    = rv32_defconfig
118DEFCONFIG_riscv64    = defconfig
119DEFCONFIG_s390x      = defconfig
120DEFCONFIG_s390       = defconfig compat.config
121DEFCONFIG_loongarch  = defconfig
122DEFCONFIG_sparc32    = sparc32_defconfig
123DEFCONFIG_sparc64    = sparc64_defconfig
124DEFCONFIG_m68k       = virt_defconfig
125DEFCONFIG_sh4        = rts7751r2dplus_defconfig
126DEFCONFIG            = $(DEFCONFIG_$(XARCH))
127
128EXTRACONFIG_x32       = -e CONFIG_X86_X32_ABI
129EXTRACONFIG_arm       = -e CONFIG_NAMESPACES
130EXTRACONFIG_armthumb  = -e CONFIG_NAMESPACES
131EXTRACONFIG_m68k      = -e CONFIG_BLK_DEV_INITRD
132EXTRACONFIG_sh4       = -e CONFIG_BLK_DEV_INITRD -e CONFIG_CMDLINE_FROM_BOOTLOADER
133EXTRACONFIG           = $(EXTRACONFIG_$(XARCH))
134
135# optional tests to run (default = all)
136TEST =
137
138# QEMU_ARCH: arch names used by qemu
139QEMU_ARCH_i386       = i386
140QEMU_ARCH_x86_64     = x86_64
141QEMU_ARCH_x32        = x86_64
142QEMU_ARCH_x86        = x86_64
143QEMU_ARCH_arm64      = aarch64
144QEMU_ARCH_arm        = arm
145QEMU_ARCH_armthumb   = arm
146QEMU_ARCH_mips32le   = mipsel  # works with malta_defconfig
147QEMU_ARCH_mips32be  = mips
148QEMU_ARCH_mipsn32le  = mips64el
149QEMU_ARCH_mipsn32be  = mips64
150QEMU_ARCH_mips64le   = mips64el
151QEMU_ARCH_mips64be   = mips64
152QEMU_ARCH_ppc        = ppc
153QEMU_ARCH_ppc64      = ppc64
154QEMU_ARCH_ppc64le    = ppc64
155QEMU_ARCH_riscv      = riscv64
156QEMU_ARCH_riscv32    = riscv32
157QEMU_ARCH_riscv64    = riscv64
158QEMU_ARCH_s390x      = s390x
159QEMU_ARCH_s390       = s390x
160QEMU_ARCH_loongarch  = loongarch64
161QEMU_ARCH_sparc32    = sparc
162QEMU_ARCH_sparc64    = sparc64
163QEMU_ARCH_m68k       = m68k
164QEMU_ARCH_sh4        = sh4
165QEMU_ARCH            = $(QEMU_ARCH_$(XARCH))
166
167QEMU_ARCH_USER_ppc64le = ppc64le
168QEMU_ARCH_USER_mipsn32le = mipsn32el
169QEMU_ARCH_USER_mipsn32be = mipsn32
170QEMU_ARCH_USER         = $(or $(QEMU_ARCH_USER_$(XARCH)),$(QEMU_ARCH_$(XARCH)))
171
172QEMU_BIOS_DIR = /usr/share/edk2/
173QEMU_BIOS_loongarch = $(QEMU_BIOS_DIR)/loongarch64/OVMF_CODE.fd
174
175ifneq ($(QEMU_BIOS_$(XARCH)),)
176QEMU_ARGS_BIOS = -bios $(QEMU_BIOS_$(XARCH))
177endif
178
179# QEMU_ARGS : some arch-specific args to pass to qemu
180QEMU_ARGS_i386       = -M pc -append "console=ttyS0,9600 i8042.noaux panic=-1 $(TEST:%=NOLIBC_TEST=%)"
181QEMU_ARGS_x86_64     = -M pc -append "console=ttyS0,9600 i8042.noaux panic=-1 $(TEST:%=NOLIBC_TEST=%)"
182QEMU_ARGS_x32        = -M pc -append "console=ttyS0,9600 i8042.noaux panic=-1 $(TEST:%=NOLIBC_TEST=%)"
183QEMU_ARGS_x86        = -M pc -append "console=ttyS0,9600 i8042.noaux panic=-1 $(TEST:%=NOLIBC_TEST=%)"
184QEMU_ARGS_arm64      = -M virt -cpu cortex-a53 -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)"
185QEMU_ARGS_arm        = -M virt -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)"
186QEMU_ARGS_armthumb   = -M virt -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)"
187QEMU_ARGS_mips32le   = -M malta -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)"
188QEMU_ARGS_mips32be   = -M malta -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)"
189QEMU_ARGS_mipsn32le  = -M malta -cpu 5KEc -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)"
190QEMU_ARGS_mipsn32be  = -M malta -cpu I6400 -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)"
191QEMU_ARGS_mips64le   = -M malta -cpu I6400 -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)"
192QEMU_ARGS_mips64be   = -M malta -cpu 5KEc -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)"
193QEMU_ARGS_ppc        = -M g3beige -append "console=ttyS0 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
194QEMU_ARGS_ppc64      = -M powernv -append "console=hvc0 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
195QEMU_ARGS_ppc64le    = -M powernv -append "console=hvc0 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
196QEMU_ARGS_riscv      = -M virt -append "console=ttyS0 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
197QEMU_ARGS_riscv32    = -M virt -append "console=ttyS0 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
198QEMU_ARGS_riscv64    = -M virt -append "console=ttyS0 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
199QEMU_ARGS_s390x      = -M s390-ccw-virtio -append "console=ttyS0 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
200QEMU_ARGS_s390       = -M s390-ccw-virtio -append "console=ttyS0 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
201QEMU_ARGS_loongarch  = -M virt -append "console=ttyS0,115200 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
202QEMU_ARGS_sparc32    = -M SS-5 -m 256M -append "console=ttyS0,115200 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
203QEMU_ARGS_sparc64    = -M sun4u -append "console=ttyS0,115200 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
204QEMU_ARGS_m68k       = -M virt -append "console=ttyGF0,115200 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
205QEMU_ARGS_sh4        = -M r2d -serial file:/dev/stdout -append "console=ttySC1,115200 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
206QEMU_ARGS            = -m 1G $(QEMU_ARGS_$(XARCH)) $(QEMU_ARGS_BIOS) $(QEMU_ARGS_EXTRA)
207
208# OUTPUT is only set when run from the main makefile, otherwise
209# it defaults to this nolibc directory.
210OUTPUT ?= $(CURDIR)/
211
212ifeq ($(V),1)
213Q=
214else
215Q=@
216endif
217
218CFLAGS_i386 = $(call cc-option,-m32)
219CFLAGS_x32 = -mx32
220CFLAGS_arm = -marm
221CFLAGS_armthumb = -mthumb -march=armv6t2
222CFLAGS_ppc = -m32 -mbig-endian -mno-vsx $(call cc-option,-mmultiple)
223CFLAGS_ppc64 = -m64 -mbig-endian -mno-vsx $(call cc-option,-mmultiple)
224CFLAGS_ppc64le = -m64 -mlittle-endian -mno-vsx $(call cc-option,-mabi=elfv2)
225CFLAGS_s390x = -m64
226CFLAGS_s390 = -m31
227CFLAGS_mips32le = -EL -mabi=32 -fPIC
228CFLAGS_mips32be = -EB -mabi=32
229CFLAGS_mipsn32le = -EL -mabi=n32 -fPIC -march=mips64r2
230CFLAGS_mipsn32be = -EB -mabi=n32 -march=mips64r6
231CFLAGS_mips64le = -EL -mabi=64 -march=mips64r6
232CFLAGS_mips64be = -EB -mabi=64 -march=mips64r2
233CFLAGS_sparc32 = $(call cc-option,-m32)
234CFLAGS_sh4 = -ml -m4
235ifeq ($(origin XARCH),command line)
236CFLAGS_XARCH = $(CFLAGS_$(XARCH))
237endif
238
239include Makefile.include
240
241CFLAGS  ?= $(CFLAGS_NOLIBC_TEST) $(CFLAGS_XARCH) $(CFLAGS_EXTRA)
242LDFLAGS :=
243
244LIBGCC := -lgcc
245
246ifeq ($(ARCH),x86)
247# Not needed on x86, probably not present for x32
248LIBGCC :=
249endif
250
251ifneq ($(LLVM),)
252# Not needed for clang
253LIBGCC :=
254endif
255
256# Modify CFLAGS based on LLVM=
257include $(srctree)/tools/scripts/Makefile.include
258
259REPORT  ?= awk '/\[OK\][\r]*$$/{p++} /\[FAIL\][\r]*$$/{if (!f) printf("\n"); f++; print;} /\[SKIPPED\][\r]*$$/{s++} \
260		/^Total number of errors:/{done++} \
261		END{ printf("\n%3d test(s): %3d passed, %3d skipped, %3d failed => status: ", p+s+f, p, s, f); \
262		if (f || !p || !done) printf("failure\n"); else if (s) printf("warning\n"); else printf("success\n");; \
263		printf("\nSee all results in %s\n", ARGV[1]); }'
264
265help:
266	@echo "Supported targets under selftests/nolibc:"
267	@echo "  all               call the \"run\" target below"
268	@echo "  help              this help"
269	@echo "  sysroot           create the nolibc sysroot here (uses \$$ARCH)"
270	@echo "  nolibc-test       build the executable (uses \$$CC and \$$CROSS_COMPILE)"
271	@echo "  libc-test         build an executable using the compiler's default libc instead"
272	@echo "  run-user          runs the executable under QEMU (uses \$$XARCH, \$$TEST)"
273	@echo "  initramfs.cpio    prepare the initramfs archive with nolibc-test"
274	@echo "  initramfs         prepare the initramfs tree with nolibc-test"
275	@echo "  defconfig         create a fresh new default config (uses \$$XARCH)"
276	@echo "  kernel            (re)build the kernel (uses \$$XARCH)"
277	@echo "  kernel-standalone (re)build the kernel with the initramfs (uses \$$XARCH)"
278	@echo "  run               runs the kernel in QEMU after building it (uses \$$XARCH, \$$TEST)"
279	@echo "  rerun             runs a previously prebuilt kernel in QEMU (uses \$$XARCH, \$$TEST)"
280	@echo "  clean             clean the sysroot, initramfs, build and output files"
281	@echo ""
282	@echo "The output file is \"run.out\". Test ranges may be passed using \$$TEST."
283	@echo ""
284	@echo "Currently using the following variables:"
285	@echo "  ARCH          = $(ARCH)"
286	@echo "  XARCH         = $(XARCH)"
287	@echo "  CROSS_COMPILE = $(CROSS_COMPILE)"
288	@echo "  CC            = $(CC)"
289	@echo "  OUTPUT        = $(OUTPUT)"
290	@echo "  TEST          = $(TEST)"
291	@echo "  QEMU_ARCH     = $(if $(QEMU_ARCH),$(QEMU_ARCH),UNKNOWN_ARCH) [determined from \$$XARCH]"
292	@echo "  IMAGE_NAME    = $(if $(IMAGE_NAME),$(IMAGE_NAME),UNKNOWN_ARCH) [determined from \$$XARCH]"
293	@echo ""
294
295all: run
296
297sysroot: sysroot/$(ARCH)/include
298
299sysroot/$(ARCH)/include:
300	$(Q)rm -rf sysroot/$(ARCH) sysroot/sysroot
301	$(QUIET_MKDIR)mkdir -p sysroot
302	$(Q)$(MAKE) -C $(srctree) outputmakefile
303	$(Q)$(MAKE) -C $(srctree)/tools/include/nolibc ARCH=$(ARCH) OUTPUT=$(CURDIR)/sysroot/ headers_standalone headers_check
304	$(Q)mv sysroot/sysroot sysroot/$(ARCH)
305
306ifneq ($(NOLIBC_SYSROOT),0)
307nolibc-test: nolibc-test.c nolibc-test-linkage.c sysroot/$(ARCH)/include
308	$(QUIET_CC)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ \
309	  -nostdlib -nostdinc -static -Isysroot/$(ARCH)/include nolibc-test.c nolibc-test-linkage.c $(LIBGCC)
310else
311nolibc-test: nolibc-test.c nolibc-test-linkage.c
312	$(QUIET_CC)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ \
313	  -nostdlib -static -include $(srctree)/tools/include/nolibc/nolibc.h nolibc-test.c nolibc-test-linkage.c $(LIBGCC)
314endif
315
316libc-test: nolibc-test.c nolibc-test-linkage.c
317	$(QUIET_CC)$(HOSTCC) -o $@ nolibc-test.c nolibc-test-linkage.c
318
319# local libc-test
320run-libc-test: libc-test
321	$(Q)./libc-test > "$(CURDIR)/run.out" || :
322	$(Q)$(REPORT) $(CURDIR)/run.out
323
324# local nolibc-test
325run-nolibc-test: nolibc-test
326	$(Q)./nolibc-test > "$(CURDIR)/run.out" || :
327	$(Q)$(REPORT) $(CURDIR)/run.out
328
329# qemu user-land test
330run-user: nolibc-test
331	$(Q)qemu-$(QEMU_ARCH_USER) ./nolibc-test > "$(CURDIR)/run.out" || :
332	$(Q)$(REPORT) $(CURDIR)/run.out
333
334initramfs.cpio: kernel nolibc-test
335	$(QUIET_GEN)echo 'file /init nolibc-test 755 0 0' | $(objtree)/usr/gen_init_cpio - > initramfs.cpio
336
337initramfs: nolibc-test
338	$(QUIET_MKDIR)mkdir -p initramfs
339	$(call QUIET_INSTALL, initramfs/init)
340	$(Q)cp nolibc-test initramfs/init
341
342defconfig:
343	$(Q)$(MAKE) -C $(srctree) ARCH=$(ARCH) CC=$(CC) CROSS_COMPILE=$(CROSS_COMPILE) $(DEFCONFIG)
344	$(Q)if [ -n "$(EXTRACONFIG)" ]; then \
345		$(srctree)/scripts/config --file $(objtree)/.config $(EXTRACONFIG); \
346		$(MAKE) -C $(srctree) ARCH=$(ARCH) CC=$(CC) CROSS_COMPILE=$(CROSS_COMPILE) olddefconfig < /dev/null; \
347	fi
348
349kernel:
350	$(Q)$(MAKE) -C $(srctree) ARCH=$(ARCH) CC=$(CC) CROSS_COMPILE=$(CROSS_COMPILE) $(IMAGE_NAME) < /dev/null
351
352kernel-standalone: initramfs
353	$(Q)$(MAKE) -C $(srctree) ARCH=$(ARCH) CC=$(CC) CROSS_COMPILE=$(CROSS_COMPILE) $(IMAGE_NAME) CONFIG_INITRAMFS_SOURCE=$(CURDIR)/initramfs < /dev/null
354
355# run the tests after building the kernel
356run: kernel initramfs.cpio
357	$(Q)qemu-system-$(QEMU_ARCH) -display none -no-reboot -kernel "$(IMAGE)" -initrd initramfs.cpio -serial file:/dev/stdout $(QEMU_ARGS) > "$(CURDIR)/run.out"
358	$(Q)$(REPORT) $(CURDIR)/run.out
359
360# re-run the tests from an existing kernel
361rerun:
362	$(Q)qemu-system-$(QEMU_ARCH) -display none -no-reboot -kernel "$(IMAGE)" -initrd initramfs.cpio -serial file:/dev/stdout $(QEMU_ARGS) > "$(CURDIR)/run.out"
363	$(Q)$(REPORT) $(CURDIR)/run.out
364
365# report with existing test log
366report:
367	$(Q)$(REPORT) $(CURDIR)/run.out
368
369clean:
370	$(call QUIET_CLEAN, sysroot)
371	$(Q)rm -rf sysroot
372	$(call QUIET_CLEAN, nolibc-test)
373	$(Q)rm -f nolibc-test
374	$(call QUIET_CLEAN, libc-test)
375	$(Q)rm -f libc-test
376	$(call QUIET_CLEAN, initramfs.cpio)
377	$(Q)rm -rf initramfs.cpio
378	$(call QUIET_CLEAN, initramfs)
379	$(Q)rm -rf initramfs
380	$(call QUIET_CLEAN, run.out)
381	$(Q)rm -rf run.out
382
383.PHONY: sysroot/$(ARCH)/include
384