[PATCH v1 01/22] selftests/nolibc: add test for -include /path/to/nolibc.h

From: Zhangjin Wu
Date: Sun Jun 25 2023 - 12:12:25 EST


As the head comment of nolibc-test.c shows, it can be built in 3 ways:

The program may be built in 3 ways:
$(CC) -nostdlib -include /path/to/nolibc.h => NOLIBC already defined
$(CC) -nostdlib -I/path/to/nolibc/sysroot => _NOLIBC_* guards are present
$(CC) with default libc => NOLIBC* never defined

Only last two of them are tested currently, let's allow test the first one too.

This may help to find issues about using nolibc.h to build programs. it
derives from this change:

commit 3a8039e289a3 ("tools/nolibc: Fix build of stdio.h due to header ordering")

Usage:

// test with sysroot
$ make run-user SYSROOT=1

// test without sysroot, using nolibc.h directly
$ make run-user SYSROOT=0

Signed-off-by: Zhangjin Wu <falcon@xxxxxxxxxxx>
---
tools/testing/selftests/nolibc/Makefile | 9 +++++++++
1 file changed, 9 insertions(+)

diff --git a/tools/testing/selftests/nolibc/Makefile b/tools/testing/selftests/nolibc/Makefile
index 848884204a84..0b66b7a79cea 100644
--- a/tools/testing/selftests/nolibc/Makefile
+++ b/tools/testing/selftests/nolibc/Makefile
@@ -102,6 +102,9 @@ REPORT ?= awk '/\[OK\][\r]*$$/{p++} /\[FAIL\][\r]*$$/{f++;print} /\[SKIPPED\][\
END{ printf("%d test(s) passed, %d skipped, %d failed.\n", p, s, f); \
printf("See all results in %s\n", ARGV[1]); }'

+# allow switch nolibc headers include method: 1 for sysroot, 0 for nolibc.h
+SYSROOT ?= 1
+
help:
@echo "Supported targets under selftests/nolibc:"
@echo " all call the \"run\" target below"
@@ -139,9 +142,15 @@ sysroot/$(ARCH)/include:
$(Q)$(MAKE) -C ../../../include/nolibc ARCH=$(ARCH) OUTPUT=$(CURDIR)/sysroot/ headers_standalone
$(Q)mv sysroot/sysroot sysroot/$(ARCH)

+ifeq ($(SYSROOT), 1)
nolibc-test: nolibc-test.c sysroot/$(ARCH)/include
$(QUIET_CC)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ \
-nostdlib -static -Isysroot/$(ARCH)/include $< -lgcc
+else
+nolibc-test: nolibc-test.c
+ $(QUIET_CC)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ \
+ -nostdlib -static -include ../../../include/nolibc/nolibc.h $< -lgcc
+endif

libc-test: nolibc-test.c
$(QUIET_CC)$(CC) -o $@ $<
--
2.25.1