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	$(QUIET_MKDIR)mkdir -p sysroot
99	$(Q)$(MAKE) -C ../../../include/nolibc ARCH=$(ARCH) OUTPUT=$(CURDIR)/sysroot/ headers_standalone
100	$(Q)mv sysroot/sysroot sysroot/$(ARCH)
101
102nolibc-test: nolibc-test.c sysroot/$(ARCH)/include
103	$(QUIET_CC)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ \
104	  -nostdlib -static -Isysroot/$(ARCH)/include $< -lgcc
105
106initramfs: nolibc-test
107	$(QUIET_MKDIR)mkdir -p initramfs
108	$(call QUIET_INSTALL, initramfs/init)
109	$(Q)cp nolibc-test initramfs/init
110
111defconfig:
112	$(Q)$(MAKE) -C $(srctree) ARCH=$(ARCH) CC=$(CC) CROSS_COMPILE=$(CROSS_COMPILE) mrproper $(DEFCONFIG) prepare
113
114kernel: initramfs
115	$(Q)$(MAKE) -C $(srctree) ARCH=$(ARCH) CC=$(CC) CROSS_COMPILE=$(CROSS_COMPILE) $(IMAGE_NAME) CONFIG_INITRAMFS_SOURCE=$(CURDIR)/initramfs
116
117# run the tests after building the kernel
118run: kernel
119	$(Q)qemu-system-$(QEMU_ARCH) -display none -no-reboot -kernel "$(srctree)/$(IMAGE)" -serial stdio $(QEMU_ARGS) > "$(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
122# re-run the tests from an existing kernel
123rerun:
124	$(Q)qemu-system-$(QEMU_ARCH) -display none -no-reboot -kernel "$(srctree)/$(IMAGE)" -serial stdio $(QEMU_ARGS) > "$(CURDIR)/run.out"
125	$(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."
126
127clean:
128	$(call QUIET_CLEAN, sysroot)
129	$(Q)rm -rf sysroot
130	$(call QUIET_CLEAN, nolibc-test)
131	$(Q)rm -f nolibc-test
132	$(call QUIET_CLEAN, initramfs)
133	$(Q)rm -rf initramfs
134	$(call QUIET_CLEAN, run.out)
135	$(Q)rm -rf run.out
136