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