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