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