297 lines · plain
1# SPDX-License-Identifier: GPL-2.02# Makefile for nolibc tests3# we're in ".../tools/testing/selftests/nolibc"4ifeq ($(srctree),)5srctree := $(patsubst %/tools/testing/selftests/,%,$(dir $(CURDIR)))6endif7 8include $(srctree)/tools/scripts/utilities.mak9# We need this for the "__cc-option" macro.10include $(srctree)/scripts/Makefile.compiler11 12ifneq ($(O),)13ifneq ($(call is-absolute,$(O)),y)14$(error Only absolute O= parameters are supported)15endif16objtree := $(O)17else18objtree ?= $(srctree)19endif20 21ifeq ($(ARCH),)22include $(srctree)/scripts/subarch.include23ARCH = $(SUBARCH)24endif25 26cc-option = $(call __cc-option, $(CC),$(CLANG_CROSS_FLAGS),$(1),$(2))27 28# XARCH extends the kernel's ARCH with a few variants of the same29# architecture that only differ by the configuration, the toolchain30# and the Qemu program used. It is copied as-is into ARCH except for31# a few specific values which are mapped like this:32#33# XARCH | ARCH | config34# -------------|-----------|-------------------------35# ppc | powerpc | 32 bits36# ppc64 | powerpc | 64 bits big endian37# ppc64le | powerpc | 64 bits little endian38#39# It is recommended to only use XARCH, though it does not harm if40# ARCH is already set. For simplicity, ARCH is sufficient for all41# architectures where both are equal.42 43# configure default variants for target kernel supported architectures44XARCH_powerpc = ppc45XARCH_mips = mips32le46XARCH = $(or $(XARCH_$(ARCH)),$(ARCH))47 48# map from user input variants to their kernel supported architectures49ARCH_ppc = powerpc50ARCH_ppc64 = powerpc51ARCH_ppc64le = powerpc52ARCH_mips32le = mips53ARCH_mips32be = mips54ARCH := $(or $(ARCH_$(XARCH)),$(XARCH))55 56# kernel image names by architecture57IMAGE_i386 = arch/x86/boot/bzImage58IMAGE_x86_64 = arch/x86/boot/bzImage59IMAGE_x86 = arch/x86/boot/bzImage60IMAGE_arm64 = arch/arm64/boot/Image61IMAGE_arm = arch/arm/boot/zImage62IMAGE_mips32le = vmlinuz63IMAGE_mips32be = vmlinuz64IMAGE_ppc = vmlinux65IMAGE_ppc64 = vmlinux66IMAGE_ppc64le = arch/powerpc/boot/zImage67IMAGE_riscv = arch/riscv/boot/Image68IMAGE_s390 = arch/s390/boot/bzImage69IMAGE_loongarch = arch/loongarch/boot/vmlinuz.efi70IMAGE = $(objtree)/$(IMAGE_$(XARCH))71IMAGE_NAME = $(notdir $(IMAGE))72 73# default kernel configurations that appear to be usable74DEFCONFIG_i386 = defconfig75DEFCONFIG_x86_64 = defconfig76DEFCONFIG_x86 = defconfig77DEFCONFIG_arm64 = defconfig78DEFCONFIG_arm = multi_v7_defconfig79DEFCONFIG_mips32le = malta_defconfig80DEFCONFIG_mips32be = malta_defconfig81DEFCONFIG_ppc = pmac32_defconfig82DEFCONFIG_ppc64 = powernv_be_defconfig83DEFCONFIG_ppc64le = powernv_defconfig84DEFCONFIG_riscv = defconfig85DEFCONFIG_s390 = defconfig86DEFCONFIG_loongarch = defconfig87DEFCONFIG = $(DEFCONFIG_$(XARCH))88 89EXTRACONFIG_mips32be = -d CONFIG_CPU_LITTLE_ENDIAN -e CONFIG_CPU_BIG_ENDIAN90EXTRACONFIG = $(EXTRACONFIG_$(XARCH))91 92# optional tests to run (default = all)93TEST =94 95# QEMU_ARCH: arch names used by qemu96QEMU_ARCH_i386 = i38697QEMU_ARCH_x86_64 = x86_6498QEMU_ARCH_x86 = x86_6499QEMU_ARCH_arm64 = aarch64100QEMU_ARCH_arm = arm101QEMU_ARCH_mips32le = mipsel # works with malta_defconfig102QEMU_ARCH_mips32be = mips103QEMU_ARCH_ppc = ppc104QEMU_ARCH_ppc64 = ppc64105QEMU_ARCH_ppc64le = ppc64106QEMU_ARCH_riscv = riscv64107QEMU_ARCH_s390 = s390x108QEMU_ARCH_loongarch = loongarch64109QEMU_ARCH = $(QEMU_ARCH_$(XARCH))110 111QEMU_ARCH_USER_ppc64le = ppc64le112QEMU_ARCH_USER = $(or $(QEMU_ARCH_USER_$(XARCH)),$(QEMU_ARCH_$(XARCH)))113 114QEMU_BIOS_DIR = /usr/share/edk2/115QEMU_BIOS_loongarch = $(QEMU_BIOS_DIR)/loongarch64/OVMF_CODE.fd116 117ifneq ($(QEMU_BIOS_$(XARCH)),)118QEMU_ARGS_BIOS = -bios $(QEMU_BIOS_$(XARCH))119endif120 121# QEMU_ARGS : some arch-specific args to pass to qemu122QEMU_ARGS_i386 = -M pc -append "console=ttyS0,9600 i8042.noaux panic=-1 $(TEST:%=NOLIBC_TEST=%)"123QEMU_ARGS_x86_64 = -M pc -append "console=ttyS0,9600 i8042.noaux panic=-1 $(TEST:%=NOLIBC_TEST=%)"124QEMU_ARGS_x86 = -M pc -append "console=ttyS0,9600 i8042.noaux panic=-1 $(TEST:%=NOLIBC_TEST=%)"125QEMU_ARGS_arm64 = -M virt -cpu cortex-a53 -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)"126QEMU_ARGS_arm = -M virt -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)"127QEMU_ARGS_mips32le = -M malta -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)"128QEMU_ARGS_mips32be = -M malta -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)"129QEMU_ARGS_ppc = -M g3beige -append "console=ttyS0 panic=-1 $(TEST:%=NOLIBC_TEST=%)"130QEMU_ARGS_ppc64 = -M powernv -append "console=hvc0 panic=-1 $(TEST:%=NOLIBC_TEST=%)"131QEMU_ARGS_ppc64le = -M powernv -append "console=hvc0 panic=-1 $(TEST:%=NOLIBC_TEST=%)"132QEMU_ARGS_riscv = -M virt -append "console=ttyS0 panic=-1 $(TEST:%=NOLIBC_TEST=%)"133QEMU_ARGS_s390 = -M s390-ccw-virtio -m 1G -append "console=ttyS0 panic=-1 $(TEST:%=NOLIBC_TEST=%)"134QEMU_ARGS_loongarch = -M virt -append "console=ttyS0,115200 panic=-1 $(TEST:%=NOLIBC_TEST=%)"135QEMU_ARGS = $(QEMU_ARGS_$(XARCH)) $(QEMU_ARGS_BIOS) $(QEMU_ARGS_EXTRA)136 137# OUTPUT is only set when run from the main makefile, otherwise138# it defaults to this nolibc directory.139OUTPUT ?= $(CURDIR)/140 141ifeq ($(V),1)142Q=143else144Q=@145endif146 147CFLAGS_i386 = $(call cc-option,-m32)148CFLAGS_ppc = -m32 -mbig-endian -mno-vsx $(call cc-option,-mmultiple)149CFLAGS_ppc64 = -m64 -mbig-endian -mno-vsx $(call cc-option,-mmultiple)150CFLAGS_ppc64le = -m64 -mlittle-endian -mno-vsx $(call cc-option,-mabi=elfv2)151CFLAGS_s390 = -m64152CFLAGS_mips32le = -EL -mabi=32 -fPIC153CFLAGS_mips32be = -EB -mabi=32154CFLAGS_STACKPROTECTOR ?= $(call cc-option,-mstack-protector-guard=global $(call cc-option,-fstack-protector-all))155CFLAGS ?= -Os -fno-ident -fno-asynchronous-unwind-tables -std=c89 -W -Wall -Wextra \156 $(call cc-option,-fno-stack-protector) \157 $(CFLAGS_$(XARCH)) $(CFLAGS_STACKPROTECTOR) $(CFLAGS_EXTRA)158LDFLAGS :=159 160LIBGCC := -lgcc161 162ifneq ($(LLVM),)163# Not needed for clang164LIBGCC :=165endif166 167# Modify CFLAGS based on LLVM=168include $(srctree)/tools/scripts/Makefile.include169 170# GCC uses "s390", clang "systemz"171CLANG_CROSS_FLAGS := $(subst --target=s390-linux,--target=systemz-linux,$(CLANG_CROSS_FLAGS))172 173REPORT ?= awk '/\[OK\][\r]*$$/{p++} /\[FAIL\][\r]*$$/{if (!f) printf("\n"); f++; print;} /\[SKIPPED\][\r]*$$/{s++} \174 END{ printf("\n%3d test(s): %3d passed, %3d skipped, %3d failed => status: ", p+s+f, p, s, f); \175 if (f || !p) printf("failure\n"); else if (s) printf("warning\n"); else printf("success\n");; \176 printf("\nSee all results in %s\n", ARGV[1]); }'177 178help:179 @echo "Supported targets under selftests/nolibc:"180 @echo " all call the \"run\" target below"181 @echo " help this help"182 @echo " sysroot create the nolibc sysroot here (uses \$$ARCH)"183 @echo " nolibc-test build the executable (uses \$$CC and \$$CROSS_COMPILE)"184 @echo " libc-test build an executable using the compiler's default libc instead"185 @echo " run-user runs the executable under QEMU (uses \$$XARCH, \$$TEST)"186 @echo " initramfs.cpio prepare the initramfs archive with nolibc-test"187 @echo " initramfs prepare the initramfs tree with nolibc-test"188 @echo " defconfig create a fresh new default config (uses \$$XARCH)"189 @echo " kernel (re)build the kernel (uses \$$XARCH)"190 @echo " kernel-standalone (re)build the kernel with the initramfs (uses \$$XARCH)"191 @echo " run runs the kernel in QEMU after building it (uses \$$XARCH, \$$TEST)"192 @echo " rerun runs a previously prebuilt kernel in QEMU (uses \$$XARCH, \$$TEST)"193 @echo " clean clean the sysroot, initramfs, build and output files"194 @echo ""195 @echo "The output file is \"run.out\". Test ranges may be passed using \$$TEST."196 @echo ""197 @echo "Currently using the following variables:"198 @echo " ARCH = $(ARCH)"199 @echo " XARCH = $(XARCH)"200 @echo " CROSS_COMPILE = $(CROSS_COMPILE)"201 @echo " CC = $(CC)"202 @echo " OUTPUT = $(OUTPUT)"203 @echo " TEST = $(TEST)"204 @echo " QEMU_ARCH = $(if $(QEMU_ARCH),$(QEMU_ARCH),UNKNOWN_ARCH) [determined from \$$XARCH]"205 @echo " IMAGE_NAME = $(if $(IMAGE_NAME),$(IMAGE_NAME),UNKNOWN_ARCH) [determined from \$$XARCH]"206 @echo ""207 208all: run209 210sysroot: sysroot/$(ARCH)/include211 212sysroot/$(ARCH)/include:213 $(Q)rm -rf sysroot/$(ARCH) sysroot/sysroot214 $(QUIET_MKDIR)mkdir -p sysroot215 $(Q)$(MAKE) -C $(srctree) outputmakefile216 $(Q)$(MAKE) -C $(srctree)/tools/include/nolibc ARCH=$(ARCH) OUTPUT=$(CURDIR)/sysroot/ headers_standalone217 $(Q)mv sysroot/sysroot sysroot/$(ARCH)218 219ifneq ($(NOLIBC_SYSROOT),0)220nolibc-test: nolibc-test.c nolibc-test-linkage.c sysroot/$(ARCH)/include221 $(QUIET_CC)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ \222 -nostdlib -nostdinc -static -Isysroot/$(ARCH)/include nolibc-test.c nolibc-test-linkage.c $(LIBGCC)223else224nolibc-test: nolibc-test.c nolibc-test-linkage.c225 $(QUIET_CC)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ \226 -nostdlib -static -include $(srctree)/tools/include/nolibc/nolibc.h nolibc-test.c nolibc-test-linkage.c $(LIBGCC)227endif228 229libc-test: nolibc-test.c nolibc-test-linkage.c230 $(QUIET_CC)$(HOSTCC) -o $@ nolibc-test.c nolibc-test-linkage.c231 232# local libc-test233run-libc-test: libc-test234 $(Q)./libc-test > "$(CURDIR)/run.out" || :235 $(Q)$(REPORT) $(CURDIR)/run.out236 237# local nolibc-test238run-nolibc-test: nolibc-test239 $(Q)./nolibc-test > "$(CURDIR)/run.out" || :240 $(Q)$(REPORT) $(CURDIR)/run.out241 242# qemu user-land test243run-user: nolibc-test244 $(Q)qemu-$(QEMU_ARCH_USER) ./nolibc-test > "$(CURDIR)/run.out" || :245 $(Q)$(REPORT) $(CURDIR)/run.out246 247initramfs.cpio: kernel nolibc-test248 $(QUIET_GEN)echo 'file /init nolibc-test 755 0 0' | $(objtree)/usr/gen_init_cpio - > initramfs.cpio249 250initramfs: nolibc-test251 $(QUIET_MKDIR)mkdir -p initramfs252 $(call QUIET_INSTALL, initramfs/init)253 $(Q)cp nolibc-test initramfs/init254 255defconfig:256 $(Q)$(MAKE) -C $(srctree) ARCH=$(ARCH) CC=$(CC) CROSS_COMPILE=$(CROSS_COMPILE) mrproper $(DEFCONFIG) prepare257 $(Q)if [ -n "$(EXTRACONFIG)" ]; then \258 $(srctree)/scripts/config --file $(objtree)/.config $(EXTRACONFIG); \259 $(MAKE) -C $(srctree) ARCH=$(ARCH) CC=$(CC) CROSS_COMPILE=$(CROSS_COMPILE) olddefconfig < /dev/null; \260 fi261 262kernel:263 $(Q)$(MAKE) -C $(srctree) ARCH=$(ARCH) CC=$(CC) CROSS_COMPILE=$(CROSS_COMPILE) $(IMAGE_NAME) < /dev/null264 265kernel-standalone: initramfs266 $(Q)$(MAKE) -C $(srctree) ARCH=$(ARCH) CC=$(CC) CROSS_COMPILE=$(CROSS_COMPILE) $(IMAGE_NAME) CONFIG_INITRAMFS_SOURCE=$(CURDIR)/initramfs < /dev/null267 268# run the tests after building the kernel269run: kernel initramfs.cpio270 $(Q)qemu-system-$(QEMU_ARCH) -display none -no-reboot -kernel "$(IMAGE)" -initrd initramfs.cpio -serial stdio $(QEMU_ARGS) > "$(CURDIR)/run.out"271 $(Q)$(REPORT) $(CURDIR)/run.out272 273# re-run the tests from an existing kernel274rerun:275 $(Q)qemu-system-$(QEMU_ARCH) -display none -no-reboot -kernel "$(IMAGE)" -initrd initramfs.cpio -serial stdio $(QEMU_ARGS) > "$(CURDIR)/run.out"276 $(Q)$(REPORT) $(CURDIR)/run.out277 278# report with existing test log279report:280 $(Q)$(REPORT) $(CURDIR)/run.out281 282clean:283 $(call QUIET_CLEAN, sysroot)284 $(Q)rm -rf sysroot285 $(call QUIET_CLEAN, nolibc-test)286 $(Q)rm -f nolibc-test287 $(call QUIET_CLEAN, libc-test)288 $(Q)rm -f libc-test289 $(call QUIET_CLEAN, initramfs.cpio)290 $(Q)rm -rf initramfs.cpio291 $(call QUIET_CLEAN, initramfs)292 $(Q)rm -rf initramfs293 $(call QUIET_CLEAN, run.out)294 $(Q)rm -rf run.out295 296.PHONY: sysroot/$(ARCH)/include297