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 = ppc64 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_i386 = $(call cc-option,-m32) 117CFLAGS_ppc = -m32 -mbig-endian -mno-vsx $(call cc-option,-mmultiple) 118CFLAGS_ppc64 = -m64 -mbig-endian -mno-vsx $(call cc-option,-mmultiple) 119CFLAGS_ppc64le = -m64 -mlittle-endian -mno-vsx $(call cc-option,-mabi=elfv2) 120CFLAGS_s390 = -m64 121CFLAGS_mips = -EL 122CFLAGS_STACKPROTECTOR ?= $(call cc-option,-mstack-protector-guard=global $(call cc-option,-fstack-protector-all)) 123CFLAGS ?= -Os -fno-ident -fno-asynchronous-unwind-tables -std=c89 -W -Wall -Wextra \ 124 $(call cc-option,-fno-stack-protector) \ 125 $(CFLAGS_$(XARCH)) $(CFLAGS_STACKPROTECTOR) 126LDFLAGS := 127 128REPORT ?= awk '/\[OK\][\r]*$$/{p++} /\[FAIL\][\r]*$$/{if (!f) printf("\n"); f++; print;} /\[SKIPPED\][\r]*$$/{s++} \ 129 END{ printf("\n%3d test(s): %3d passed, %3d skipped, %3d failed => status: ", p+s+f, p, s, f); \ 130 if (f) printf("failure\n"); else if (s) printf("warning\n"); else printf("success\n");; \ 131 printf("\nSee all results in %s\n", ARGV[1]); }' 132 133help: 134 @echo "Supported targets under selftests/nolibc:" 135 @echo " all call the \"run\" target below" 136 @echo " help this help" 137 @echo " sysroot create the nolibc sysroot here (uses \$$ARCH)" 138 @echo " nolibc-test build the executable (uses \$$CC and \$$CROSS_COMPILE)" 139 @echo " libc-test build an executable using the compiler's default libc instead" 140 @echo " run-user runs the executable under QEMU (uses \$$XARCH, \$$TEST)" 141 @echo " initramfs.cpio prepare the initramfs archive with nolibc-test" 142 @echo " initramfs prepare the initramfs tree with nolibc-test" 143 @echo " defconfig create a fresh new default config (uses \$$XARCH)" 144 @echo " kernel (re)build the kernel (uses \$$XARCH)" 145 @echo " kernel-standalone (re)build the kernel with the initramfs (uses \$$XARCH)" 146 @echo " run runs the kernel in QEMU after building it (uses \$$XARCH, \$$TEST)" 147 @echo " rerun runs a previously prebuilt kernel in QEMU (uses \$$XARCH, \$$TEST)" 148 @echo " clean clean the sysroot, initramfs, build and output files" 149 @echo "" 150 @echo "The output file is \"run.out\". Test ranges may be passed using \$$TEST." 151 @echo "" 152 @echo "Currently using the following variables:" 153 @echo " ARCH = $(ARCH)" 154 @echo " XARCH = $(XARCH)" 155 @echo " CROSS_COMPILE = $(CROSS_COMPILE)" 156 @echo " CC = $(CC)" 157 @echo " OUTPUT = $(OUTPUT)" 158 @echo " TEST = $(TEST)" 159 @echo " QEMU_ARCH = $(if $(QEMU_ARCH),$(QEMU_ARCH),UNKNOWN_ARCH) [determined from \$$XARCH]" 160 @echo " IMAGE_NAME = $(if $(IMAGE_NAME),$(IMAGE_NAME),UNKNOWN_ARCH) [determined from \$$XARCH]" 161 @echo "" 162 163all: run 164 165sysroot: sysroot/$(ARCH)/include 166 167sysroot/$(ARCH)/include: 168 $(Q)rm -rf sysroot/$(ARCH) sysroot/sysroot 169 $(QUIET_MKDIR)mkdir -p sysroot 170 $(Q)$(MAKE) -C ../../../include/nolibc ARCH=$(ARCH) OUTPUT=$(CURDIR)/sysroot/ headers_standalone 171 $(Q)mv sysroot/sysroot sysroot/$(ARCH) 172 173ifneq ($(NOLIBC_SYSROOT),0) 174nolibc-test: nolibc-test.c nolibc-test-linkage.c sysroot/$(ARCH)/include 175 $(QUIET_CC)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ \ 176 -nostdlib -nostdinc -static -Isysroot/$(ARCH)/include nolibc-test.c nolibc-test-linkage.c -lgcc 177else 178nolibc-test: nolibc-test.c nolibc-test-linkage.c 179 $(QUIET_CC)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ \ 180 -nostdlib -static -include ../../../include/nolibc/nolibc.h nolibc-test.c nolibc-test-linkage.c -lgcc 181endif 182 183libc-test: nolibc-test.c nolibc-test-linkage.c 184 $(QUIET_CC)$(HOSTCC) -o $@ nolibc-test.c nolibc-test-linkage.c 185 186# local libc-test 187run-libc-test: libc-test 188 $(Q)./libc-test > "$(CURDIR)/run.out" || : 189 $(Q)$(REPORT) $(CURDIR)/run.out 190 191# local nolibc-test 192run-nolibc-test: nolibc-test 193 $(Q)./nolibc-test > "$(CURDIR)/run.out" || : 194 $(Q)$(REPORT) $(CURDIR)/run.out 195 196# qemu user-land test 197run-user: nolibc-test 198 $(Q)qemu-$(QEMU_ARCH) ./nolibc-test > "$(CURDIR)/run.out" || : 199 $(Q)$(REPORT) $(CURDIR)/run.out 200 201initramfs.cpio: kernel nolibc-test 202 $(QUIET_GEN)echo 'file /init nolibc-test 755 0 0' | $(srctree)/usr/gen_init_cpio - > initramfs.cpio 203 204initramfs: nolibc-test 205 $(QUIET_MKDIR)mkdir -p initramfs 206 $(call QUIET_INSTALL, initramfs/init) 207 $(Q)cp nolibc-test initramfs/init 208 209defconfig: 210 $(Q)$(MAKE) -C $(srctree) ARCH=$(ARCH) CC=$(CC) CROSS_COMPILE=$(CROSS_COMPILE) mrproper $(DEFCONFIG) prepare 211 212kernel: 213 $(Q)$(MAKE) -C $(srctree) ARCH=$(ARCH) CC=$(CC) CROSS_COMPILE=$(CROSS_COMPILE) $(IMAGE_NAME) 214 215kernel-standalone: initramfs 216 $(Q)$(MAKE) -C $(srctree) ARCH=$(ARCH) CC=$(CC) CROSS_COMPILE=$(CROSS_COMPILE) $(IMAGE_NAME) CONFIG_INITRAMFS_SOURCE=$(CURDIR)/initramfs 217 218# run the tests after building the kernel 219run: kernel initramfs.cpio 220 $(Q)qemu-system-$(QEMU_ARCH) -display none -no-reboot -kernel "$(srctree)/$(IMAGE)" -initrd initramfs.cpio -serial stdio $(QEMU_ARGS) > "$(CURDIR)/run.out" 221 $(Q)$(REPORT) $(CURDIR)/run.out 222 223# re-run the tests from an existing kernel 224rerun: 225 $(Q)qemu-system-$(QEMU_ARCH) -display none -no-reboot -kernel "$(srctree)/$(IMAGE)" -initrd initramfs.cpio -serial stdio $(QEMU_ARGS) > "$(CURDIR)/run.out" 226 $(Q)$(REPORT) $(CURDIR)/run.out 227 228# report with existing test log 229report: 230 $(Q)$(REPORT) $(CURDIR)/run.out 231 232clean: 233 $(call QUIET_CLEAN, sysroot) 234 $(Q)rm -rf sysroot 235 $(call QUIET_CLEAN, nolibc-test) 236 $(Q)rm -f nolibc-test 237 $(call QUIET_CLEAN, libc-test) 238 $(Q)rm -f libc-test 239 $(call QUIET_CLEAN, initramfs.cpio) 240 $(Q)rm -rf initramfs.cpio 241 $(call QUIET_CLEAN, initramfs) 242 $(Q)rm -rf initramfs 243 $(call QUIET_CLEAN, run.out) 244 $(Q)rm -rf run.out 245 246.PHONY: sysroot/$(ARCH)/include 247