Skip to contentSkip to conversation

Makefile: Fix the feature checks when SHELL isn't /bin/sh

PATCH Superseded Mika Tammi 1 participant 1 message Oct 30 KVM

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Mika Tammi

author
#0 lore raw

PATCH 1/1 Makefile: Fix the feature checks when SHELL isn't /bin/sh

try-build and try-cc pass their test program to the compiler with echo "$(1)" in a $(shell sh -c '...'). The newlines in the program survive only while make's SHELL is its default, /bin/sh: GNU make then runs the command without a shell, splitting it into arguments itself, and the whole script is one single-quoted argument to sh -c.

With any other SHELL, GNU make runs "$(SHELL) -c" with the command line, which it first rewrites with a backslash before each special character and then parses again. A newline becomes a backslash-newline there, which make removes [1]. The compiler gets the program as one line, ignores everything after its first #include, and fails to link it:

<stdin>:1:20: warning: extra tokens at end of '#include' directive (.text+0x1b): undefined reference to `main'

So with make SHELL=/bin/bash, and in every nixpkgs build, as its stdenv passes SHELL to make, every feature check fails:

- without HAVE_STRLCPY, include/kvm/strbuf.h declares strlcpy() and strlcat(), which glibc 2.38 and later and musl declare too, and the build stops at -Werror=redundant-decls
- no static libc is found, so the guest init is left out
- on arm64, powerpc and riscv, the build stops as libfdt isn't found, unless LIBFDT_DIR is given
- none of the optional libraries is found

Give printf each line of the program as an argument of its own, so that the command line has no newlines for make to remove. This needs nothing newer from make than the rest of the file, unlike writing the program into a file with $(file), which needs GNU make 4.0. And printf "%s\n" leaves backslashes alone, which echo doesn't in every shell.

[1] construct_command_argv_internal() in src/job.c of GNU make 4.4.1

Fixes 328a03bc3f07 ("kvm tools: Add conditional compilation of symbol resolving")
Fixes 2c82d39a080d ("kvm tools: Add test for -static libc present")
Assisted-by Claude:claude-opus-5-5

---
I used Claude Opus 5.5 to track down the cause, write this patch and its description, and run the tests below; I have reviewed the result.

Tested with nixpkgs' kvmtool package, without its workaround for this (it defines HAVE_STRLCPY): natively on x86_64 and arm64 with glibc, linked dynamically and statically, and with static musl on arm64; cross-compiled for arm64, riscv32 and riscv64. With this patch, the feature checks give the same results with SHELL=bash as with make's default SHELL, and with the default SHELL the same results as before. The dynamically linked lkvm is byte-identical to the one built with the workaround, a statically linked lkvm now includes the guest init, and a static musl lkvm on QEMU rv32 and rv64 KVM hosts still boots a guest.

 config/utilities.mak | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
config/utilities.mak +2 -2
index 612ed1f..3c7cf2b 100644
@@ -183,7 +183,7 @@ _gea_err  = $(if $(1),$(error Please set '$(1)' appropriately))
 # Usage: option = $(call try-cc, source-to-build, cc-options)
 try-cc = $(shell sh -c						  \
 	'TMP="$(OUTPUT)$(TMPOUT).$$$$";				  \
-	 echo "$(1)" |						  \
+	 printf "%s\n" "$(subst $(newline)," ",$(1))" |		  \
 	 $(CC) -x c -c - $(2) -o "$$TMP" > /dev/null 2>&1 && echo y; \
 	 rm -f "$$TMP"')
 
@@ -191,7 +191,7 @@ try-cc = $(shell sh -c						  \
 # Usage: option = $(call try-build, source-to-build, cc-options, link-options)
 try-build = $(shell sh -c							\
 	'TMP="$(OUTPUT)$(TMPOUT).$$$$";						\
-	echo "$(1)" |								\
+	printf "%s\n" "$(subst $(newline)," ",$(1))" |				\
 	$(CC) -x c - $(2) $(3) -o "$$TMP" > /dev/null 2>&1 && echo y;		\
 	rm -f "$$TMP"')
 
Signature · 1 line
2.54.0 (Apple Git-157)