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