1# SPDX-License-Identifier: GPL-2.0
2# Makefile for nolibc tests
3include ../../../scripts/Makefile.include
4# We need this for the "cc-option" macro.
5include ../../../build/Build.include
6
7# we're in ".../tools/testing/selftests/nolibc"
8ifeq ($(srctree),)
9srctree := $(patsubst %/tools/testing/selftests/,%,$(dir $(CURDIR)))
10endif
11
12ifeq ($(ARCH),)
13include $(srctree)/scripts/subarch.include
14ARCH = $(SUBARCH)
15endif
16
17# kernel image names by architecture
18IMAGE_i386       = arch/x86/boot/bzImage
19IMAGE_x86_64     = arch/x86/boot/bzImage
20IMAGE_x86        = arch/x86/boot/bzImage
21IMAGE_arm64      = arch/arm64/boot/Image
22IMAGE_arm        = arch/arm/boot/zImage
23IMAGE_mips       = vmlinuz
24IMAGE_riscv      = arch/riscv/boot/Image
25IMAGE_s390       = arch/s390/boot/bzImage
26IMAGE_loongarch  = arch/loongarch/boot/vmlinuz.efi
27IMAGE            = $(IMAGE_$(ARCH))
28IMAGE_NAME       = $(notdir $(IMAGE))
29
30# default kernel configurations that appear to be usable
31DEFCONFIG_i386       = defconfig
32DEFCONFIG_x86_64     = defconfig
33DEFCONFIG_x86        = defconfig
34DEFCONFIG_arm64      = defconfig
35DEFCONFIG_arm        = multi_v7_defconfig
36DEFCONFIG_mips       = malta_defconfig
37DEFCONFIG_riscv      = defconfig
38DEFCONFIG_s390       = defconfig
39DEFCONFIG_loongarch  = defconfig
40DEFCONFIG            = $(DEFCONFIG_$(ARCH))
41
42# optional tests to run (default = all)
43TEST =
44
45# QEMU_ARCH: arch names used by qemu
46QEMU_ARCH_i386       = i386
47QEMU_ARCH_x86_64     = x86_64
48QEMU_ARCH_x86        = x86_64
49QEMU_ARCH_arm64      = aarch64
50QEMU_ARCH_arm        = arm
51QEMU_ARCH_mips       = mipsel  # works with malta_defconfig
52QEMU_ARCH_riscv      = riscv64
53QEMU_ARCH_s390       = s390x
54QEMU_ARCH_loongarch  = loongarch64
55QEMU_ARCH            = $(QEMU_ARCH_$(ARCH))
56
57# QEMU_ARGS : some arch-specific args to pass to qemu
58QEMU_ARGS_i386       = -M pc -append "console=ttyS0,9600 i8042.noaux panic=-1 $(TEST:%=NOLIBC_TEST=%)"
59QEMU_ARGS_x86_64     = -M pc -append "console=ttyS0,9600 i8042.noaux panic=-1 $(TEST:%=NOLIBC_TEST=%)"
60QEMU_ARGS_x86        = -M pc -append "console=ttyS0,9600 i8042.noaux panic=-1 $(TEST:%=NOLIBC_TEST=%)"
61QEMU_ARGS_arm64      = -M virt -cpu cortex-a53 -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)"
62QEMU_ARGS_arm        = -M virt -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)"
63QEMU_ARGS_mips       = -M malta -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)"
64QEMU_ARGS_riscv      = -M virt -append "console=ttyS0 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
65QEMU_ARGS_s390       = -M s390-ccw-virtio -m 1G -append "console=ttyS0 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
66QEMU_ARGS_loongarch  = -M virt -append "console=ttyS0,115200 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
67QEMU_ARGS            = $(QEMU_ARGS_$(ARCH)) $(QEMU_ARGS_EXTRA)
68
69# OUTPUT is only set when run from the main makefile, otherwise
70# it defaults to this nolibc directory.
71OUTPUT ?= $(CURDIR)/
72
73ifeq ($(V),1)
74Q=
75else
76Q=@
77endif
78
79CFLAGS_s390 = -m64
80CFLAGS_mips = -EL
81CFLAGS_STACKPROTECTOR ?= $(call cc-option,-mstack-protector-guard=global $(call cc-option,-fstack-protector-all))
82CFLAGS  ?= -Os -fno-ident -fno-asynchronous-unwind-tables -std=c89 \
83		$(call cc-option,-fno-stack-protector) \
84		$(CFLAGS_$(ARCH)) $(CFLAGS_STACKPROTECTOR)
85LDFLAGS := -s
86
87help:
88	@echo "Supported targets under selftests/nolibc:"
89	@echo "  all          call the \"run\" target below"
90	@echo "  help         this help"
91	@echo "  sysroot      create the nolibc sysroot here (uses \$$ARCH)"
92	@echo "  nolibc-test  build the executable (uses \$$CC and \$$CROSS_COMPILE)"
93	@echo "  libc-test    build an executable using the compiler's default libc instead"
94	@echo "  run-user     runs the executable under QEMU (uses \$$ARCH, \$$TEST)"
95	@echo "  initramfs    prepare the initramfs with nolibc-test"
96	@echo "  defconfig    create a fresh new default config (uses \$$ARCH)"
97	@echo "  kernel       (re)build the kernel with the initramfs (uses \$$ARCH)"
98	@echo "  run          runs the kernel in QEMU after building it (uses \$$ARCH, \$$TEST)"
99	@echo "  rerun        runs a previously prebuilt kernel in QEMU (uses \$$ARCH, \$$TEST)"
100	@echo "  clean        clean the sysroot, initramfs, build and output files"
101	@echo ""
102	@echo "The output file is \"run.out\". Test ranges may be passed using \$$TEST."
103	@echo ""
104	@echo "Currently using the following variables:"
105	@echo "  ARCH          = $(ARCH)"
106	@echo "  CROSS_COMPILE = $(CROSS_COMPILE)"
107	@echo "  CC            = $(CC)"
108	@echo "  OUTPUT        = $(OUTPUT)"
109	@echo "  TEST          = $(TEST)"
110	@echo "  QEMU_ARCH     = $(if $(QEMU_ARCH),$(QEMU_ARCH),UNKNOWN_ARCH) [determined from \$$ARCH]"
111	@echo "  IMAGE_NAME    = $(if $(IMAGE_NAME),$(IMAGE_NAME),UNKNOWN_ARCH) [determined from \$$ARCH]"
112	@echo ""
113
114all: run
115
116sysroot: sysroot/$(ARCH)/include
117
118sysroot/$(ARCH)/include:
119	$(Q)rm -rf sysroot/$(ARCH) sysroot/sysroot
120	$(QUIET_MKDIR)mkdir -p sysroot
121	$(Q)$(MAKE) -C ../../../include/nolibc ARCH=$(ARCH) OUTPUT=$(CURDIR)/sysroot/ headers_standalone
122	$(Q)mv sysroot/sysroot sysroot/$(ARCH)
123
124nolibc-test: nolibc-test.c sysroot/$(ARCH)/include
125	$(QUIET_CC)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ \
126	  -nostdlib -static -Isysroot/$(ARCH)/include $< -lgcc
127
128libc-test: nolibc-test.c
129	$(QUIET_CC)$(CC) -o $@ $<
130
131# qemu user-land test
132run-user: nolibc-test
133	$(Q)qemu-$(QEMU_ARCH) ./nolibc-test > "$(CURDIR)/run.out" || :
134	$(Q)awk '/\[OK\][\r]*$$/{p++} /\[FAIL\][\r]*$$/{f++} /\[SKIPPED\][\r]*$$/{s++} \
135	         END{ printf("%d test(s) passed, %d skipped, %d failed.", p, s, f); \
136	         if (s+f > 0) printf(" See all results in %s\n", ARGV[1]); else print; }' \
137	         $(CURDIR)/run.out
138
139initramfs: nolibc-test
140	$(QUIET_MKDIR)mkdir -p initramfs
141	$(call QUIET_INSTALL, initramfs/init)
142	$(Q)cp nolibc-test initramfs/init
143
144defconfig:
145	$(Q)$(MAKE) -C $(srctree) ARCH=$(ARCH) CC=$(CC) CROSS_COMPILE=$(CROSS_COMPILE) mrproper $(DEFCONFIG) prepare
146
147kernel: initramfs
148	$(Q)$(MAKE) -C $(srctree) ARCH=$(ARCH) CC=$(CC) CROSS_COMPILE=$(CROSS_COMPILE) $(IMAGE_NAME) CONFIG_INITRAMFS_SOURCE=$(CURDIR)/initramfs
149
150# run the tests after building the kernel
151run: kernel
152	$(Q)qemu-system-$(QEMU_ARCH) -display none -no-reboot -kernel "$(srctree)/$(IMAGE)" -serial stdio $(QEMU_ARGS) > "$(CURDIR)/run.out"
153	$(Q)awk '/\[OK\][\r]*$$/{p++} /\[FAIL\][\r]*$$/{f++} /\[SKIPPED\][\r]*$$/{s++} \
154	         END{ printf("%d test(s) passed, %d skipped, %d failed.", p, s, f); \
155	         if (s+f > 0) printf(" See all results in %s\n", ARGV[1]); else print; }' \
156	         $(CURDIR)/run.out
157
158# re-run the tests from an existing kernel
159rerun:
160	$(Q)qemu-system-$(QEMU_ARCH) -display none -no-reboot -kernel "$(srctree)/$(IMAGE)" -serial stdio $(QEMU_ARGS) > "$(CURDIR)/run.out"
161	$(Q)awk '/\[OK\][\r]*$$/{p++} /\[FAIL\][\r]*$$/{f++} /\[SKIPPED\][\r]*$$/{s++} \
162	         END{ printf("%d test(s) passed, %d skipped, %d failed.", p, s, f); \
163	         if (s+f > 0) printf(" See all results in %s\n", ARGV[1]); else print; }' \
164	         $(CURDIR)/run.out
165
166clean:
167	$(call QUIET_CLEAN, sysroot)
168	$(Q)rm -rf sysroot
169	$(call QUIET_CLEAN, nolibc-test)
170	$(Q)rm -f nolibc-test
171	$(call QUIET_CLEAN, libc-test)
172	$(Q)rm -f libc-test
173	$(call QUIET_CLEAN, initramfs)
174	$(Q)rm -rf initramfs
175	$(call QUIET_CLEAN, run.out)
176	$(Q)rm -rf run.out
177
178.PHONY: sysroot/$(ARCH)/include
179