xref: /linux/tools/testing/selftests/nolibc/Makefile (revision 55ec81f7517fad09135f65552cea0a3ee84fff30)
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# XARCH extends the kernel's ARCH with a few variants of the same
18# architecture that only differ by the configuration, the toolchain
19# and the Qemu program used. It is copied as-is into ARCH except for
20# a few specific values which are mapped like this:
21#
22#  XARCH        | ARCH      | config
23#  -------------|-----------|-------------------------
24#  ppc          | powerpc   | 32 bits
25#  ppc64        | powerpc   | 64 bits big endian
26#  ppc64le      | powerpc   | 64 bits little endian
27#
28# It is recommended to only use XARCH, though it does not harm if
29# ARCH is already set. For simplicity, ARCH is sufficient for all
30# architectures where both are equal.
31
32# configure default variants for target kernel supported architectures
33XARCH_powerpc    = ppc
34XARCH            = $(or $(XARCH_$(ARCH)),$(ARCH))
35
36# map from user input variants to their kernel supported architectures
37ARCH_ppc         = powerpc
38ARCH_ppc64       = powerpc
39ARCH_ppc64le     = powerpc
40ARCH            := $(or $(ARCH_$(XARCH)),$(XARCH))
41
42# kernel image names by architecture
43IMAGE_i386       = arch/x86/boot/bzImage
44IMAGE_x86_64     = arch/x86/boot/bzImage
45IMAGE_x86        = arch/x86/boot/bzImage
46IMAGE_arm64      = arch/arm64/boot/Image
47IMAGE_arm        = arch/arm/boot/zImage
48IMAGE_mips       = vmlinuz
49IMAGE_ppc        = vmlinux
50IMAGE_ppc64      = vmlinux
51IMAGE_ppc64le    = arch/powerpc/boot/zImage
52IMAGE_riscv      = arch/riscv/boot/Image
53IMAGE_s390       = arch/s390/boot/bzImage
54IMAGE_loongarch  = arch/loongarch/boot/vmlinuz.efi
55IMAGE            = $(IMAGE_$(XARCH))
56IMAGE_NAME       = $(notdir $(IMAGE))
57
58# default kernel configurations that appear to be usable
59DEFCONFIG_i386       = defconfig
60DEFCONFIG_x86_64     = defconfig
61DEFCONFIG_x86        = defconfig
62DEFCONFIG_arm64      = defconfig
63DEFCONFIG_arm        = multi_v7_defconfig
64DEFCONFIG_mips       = malta_defconfig
65DEFCONFIG_ppc        = pmac32_defconfig
66DEFCONFIG_ppc64      = powernv_be_defconfig
67DEFCONFIG_ppc64le    = powernv_defconfig
68DEFCONFIG_riscv      = defconfig
69DEFCONFIG_s390       = defconfig
70DEFCONFIG_loongarch  = defconfig
71DEFCONFIG            = $(DEFCONFIG_$(XARCH))
72
73# optional tests to run (default = all)
74TEST =
75
76# QEMU_ARCH: arch names used by qemu
77QEMU_ARCH_i386       = i386
78QEMU_ARCH_x86_64     = x86_64
79QEMU_ARCH_x86        = x86_64
80QEMU_ARCH_arm64      = aarch64
81QEMU_ARCH_arm        = arm
82QEMU_ARCH_mips       = mipsel  # works with malta_defconfig
83QEMU_ARCH_ppc        = ppc
84QEMU_ARCH_ppc64      = ppc64
85QEMU_ARCH_ppc64le    = ppc64le
86QEMU_ARCH_riscv      = riscv64
87QEMU_ARCH_s390       = s390x
88QEMU_ARCH_loongarch  = loongarch64
89QEMU_ARCH            = $(QEMU_ARCH_$(XARCH))
90
91# QEMU_ARGS : some arch-specific args to pass to qemu
92QEMU_ARGS_i386       = -M pc -append "console=ttyS0,9600 i8042.noaux panic=-1 $(TEST:%=NOLIBC_TEST=%)"
93QEMU_ARGS_x86_64     = -M pc -append "console=ttyS0,9600 i8042.noaux panic=-1 $(TEST:%=NOLIBC_TEST=%)"
94QEMU_ARGS_x86        = -M pc -append "console=ttyS0,9600 i8042.noaux panic=-1 $(TEST:%=NOLIBC_TEST=%)"
95QEMU_ARGS_arm64      = -M virt -cpu cortex-a53 -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)"
96QEMU_ARGS_arm        = -M virt -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)"
97QEMU_ARGS_mips       = -M malta -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)"
98QEMU_ARGS_ppc        = -M g3beige -append "console=ttyS0 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
99QEMU_ARGS_ppc64      = -M powernv -append "console=hvc0 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
100QEMU_ARGS_ppc64le    = -M powernv -append "console=hvc0 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
101QEMU_ARGS_riscv      = -M virt -append "console=ttyS0 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
102QEMU_ARGS_s390       = -M s390-ccw-virtio -m 1G -append "console=ttyS0 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
103QEMU_ARGS_loongarch  = -M virt -append "console=ttyS0,115200 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
104QEMU_ARGS            = $(QEMU_ARGS_$(XARCH)) $(QEMU_ARGS_EXTRA)
105
106# OUTPUT is only set when run from the main makefile, otherwise
107# it defaults to this nolibc directory.
108OUTPUT ?= $(CURDIR)/
109
110ifeq ($(V),1)
111Q=
112else
113Q=@
114endif
115
116CFLAGS_ppc = -m32 -mbig-endian -mno-vsx $(call cc-option,-mmultiple)
117CFLAGS_ppc64 = -m64 -mbig-endian -mno-vsx $(call cc-option,-mmultiple)
118CFLAGS_ppc64le = -m64 -mlittle-endian -mno-vsx $(call cc-option,-mabi=elfv2)
119CFLAGS_s390 = -m64
120CFLAGS_mips = -EL
121CFLAGS_STACKPROTECTOR ?= $(call cc-option,-mstack-protector-guard=global $(call cc-option,-fstack-protector-all))
122CFLAGS  ?= -Os -fno-ident -fno-asynchronous-unwind-tables -std=c89 -W -Wall -Wextra \
123		$(call cc-option,-fno-stack-protector) \
124		$(CFLAGS_$(XARCH)) $(CFLAGS_STACKPROTECTOR)
125LDFLAGS :=
126
127REPORT  ?= awk '/\[OK\][\r]*$$/{p++} /\[FAIL\][\r]*$$/{if (!f) printf("\n"); f++; print;} /\[SKIPPED\][\r]*$$/{s++} \
128		END{ printf("\n%3d test(s): %3d passed, %3d skipped, %3d failed => status: ", p+s+f, p, s, f); \
129		if (f) printf("failure\n"); else if (s) printf("warning\n"); else printf("success\n");; \
130		printf("\nSee all results in %s\n", ARGV[1]); }'
131
132help:
133	@echo "Supported targets under selftests/nolibc:"
134	@echo "  all          call the \"run\" target below"
135	@echo "  help         this help"
136	@echo "  sysroot      create the nolibc sysroot here (uses \$$ARCH)"
137	@echo "  nolibc-test  build the executable (uses \$$CC and \$$CROSS_COMPILE)"
138	@echo "  libc-test    build an executable using the compiler's default libc instead"
139	@echo "  run-user     runs the executable under QEMU (uses \$$XARCH, \$$TEST)"
140	@echo "  initramfs    prepare the initramfs with nolibc-test"
141	@echo "  defconfig    create a fresh new default config (uses \$$XARCH)"
142	@echo "  kernel       (re)build the kernel with the initramfs (uses \$$XARCH)"
143	@echo "  run          runs the kernel in QEMU after building it (uses \$$XARCH, \$$TEST)"
144	@echo "  rerun        runs a previously prebuilt kernel in QEMU (uses \$$XARCH, \$$TEST)"
145	@echo "  clean        clean the sysroot, initramfs, build and output files"
146	@echo ""
147	@echo "The output file is \"run.out\". Test ranges may be passed using \$$TEST."
148	@echo ""
149	@echo "Currently using the following variables:"
150	@echo "  ARCH          = $(ARCH)"
151	@echo "  XARCH         = $(XARCH)"
152	@echo "  CROSS_COMPILE = $(CROSS_COMPILE)"
153	@echo "  CC            = $(CC)"
154	@echo "  OUTPUT        = $(OUTPUT)"
155	@echo "  TEST          = $(TEST)"
156	@echo "  QEMU_ARCH     = $(if $(QEMU_ARCH),$(QEMU_ARCH),UNKNOWN_ARCH) [determined from \$$XARCH]"
157	@echo "  IMAGE_NAME    = $(if $(IMAGE_NAME),$(IMAGE_NAME),UNKNOWN_ARCH) [determined from \$$XARCH]"
158	@echo ""
159
160all: run
161
162sysroot: sysroot/$(ARCH)/include
163
164sysroot/$(ARCH)/include:
165	$(Q)rm -rf sysroot/$(ARCH) sysroot/sysroot
166	$(QUIET_MKDIR)mkdir -p sysroot
167	$(Q)$(MAKE) -C ../../../include/nolibc ARCH=$(ARCH) OUTPUT=$(CURDIR)/sysroot/ headers_standalone
168	$(Q)mv sysroot/sysroot sysroot/$(ARCH)
169
170ifneq ($(NOLIBC_SYSROOT),0)
171nolibc-test: nolibc-test.c sysroot/$(ARCH)/include
172	$(QUIET_CC)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ \
173	  -nostdlib -static -Isysroot/$(ARCH)/include $< -lgcc
174else
175nolibc-test: nolibc-test.c
176	$(QUIET_CC)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ \
177	  -nostdlib -static -include ../../../include/nolibc/nolibc.h $< -lgcc
178endif
179
180libc-test: nolibc-test.c
181	$(QUIET_CC)$(HOSTCC) -o $@ $<
182
183# local libc-test
184run-libc-test: libc-test
185	$(Q)./libc-test > "$(CURDIR)/run.out" || :
186	$(Q)$(REPORT) $(CURDIR)/run.out
187
188# local nolibc-test
189run-nolibc-test: nolibc-test
190	$(Q)./nolibc-test > "$(CURDIR)/run.out" || :
191	$(Q)$(REPORT) $(CURDIR)/run.out
192
193# qemu user-land test
194run-user: nolibc-test
195	$(Q)qemu-$(QEMU_ARCH) ./nolibc-test > "$(CURDIR)/run.out" || :
196	$(Q)$(REPORT) $(CURDIR)/run.out
197
198initramfs: nolibc-test
199	$(QUIET_MKDIR)mkdir -p initramfs
200	$(call QUIET_INSTALL, initramfs/init)
201	$(Q)cp nolibc-test initramfs/init
202
203defconfig:
204	$(Q)$(MAKE) -C $(srctree) ARCH=$(ARCH) CC=$(CC) CROSS_COMPILE=$(CROSS_COMPILE) mrproper $(DEFCONFIG) prepare
205
206kernel: initramfs
207	$(Q)$(MAKE) -C $(srctree) ARCH=$(ARCH) CC=$(CC) CROSS_COMPILE=$(CROSS_COMPILE) $(IMAGE_NAME) CONFIG_INITRAMFS_SOURCE=$(CURDIR)/initramfs
208
209# run the tests after building the kernel
210run: kernel
211	$(Q)qemu-system-$(QEMU_ARCH) -display none -no-reboot -kernel "$(srctree)/$(IMAGE)" -serial stdio $(QEMU_ARGS) > "$(CURDIR)/run.out"
212	$(Q)$(REPORT) $(CURDIR)/run.out
213
214# re-run the tests from an existing kernel
215rerun:
216	$(Q)qemu-system-$(QEMU_ARCH) -display none -no-reboot -kernel "$(srctree)/$(IMAGE)" -serial stdio $(QEMU_ARGS) > "$(CURDIR)/run.out"
217	$(Q)$(REPORT) $(CURDIR)/run.out
218
219# report with existing test log
220report:
221	$(Q)$(REPORT) $(CURDIR)/run.out
222
223clean:
224	$(call QUIET_CLEAN, sysroot)
225	$(Q)rm -rf sysroot
226	$(call QUIET_CLEAN, nolibc-test)
227	$(Q)rm -f nolibc-test
228	$(call QUIET_CLEAN, libc-test)
229	$(Q)rm -f libc-test
230	$(call QUIET_CLEAN, initramfs)
231	$(Q)rm -rf initramfs
232	$(call QUIET_CLEAN, run.out)
233	$(Q)rm -rf run.out
234
235.PHONY: sysroot/$(ARCH)/include
236