From 6b4cb0b42be465e4a662b1530546c5b249702879 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9F=83=E5=8D=9A=E6=8B=89=E9=85=B1?= Date: Sat, 4 Apr 2026 22:58:28 +0800 Subject: [PATCH] GNUmakefile: fix portability issues for out-of-tree and cross builds - Use $(VPATH) instead of $(SRC) in define_from_arch.h: the comment on line 4 says "the VPATH variable must point to the actual makefile directory", making $(VPATH) the designated source-directory variable. $(SRC) happens to resolve to the same value in normal builds, but $(VPATH) is semantically correct and consistent with the rest of the file (CPPFLAGS already uses -I$(VPATH)). - Use $< instead of $(SRC)$< in COMPILE macro: VPATH already resolves $< to the correct source path. $(SRC)$< duplicates the directory prefix in out-of-tree builds (e.g. ../../src/../../src/cli/cli.c). - Replace egrep with grep -E: egrep has been deprecated by POSIX (IEEE Std 1003.1-2024) and GNU grep now emits a warning. On some systems (e.g. MSYS2), egrep is a wrapper script whose shebang path may contain spaces, breaking $(shell) invocations. - Add READELF variable (?= $(CROSS_COMPILE)readelf) and use it in the loader-info.c rule instead of hardcoded readelf, matching the existing pattern for CC, STRIP, OBJCOPY, and OBJDUMP. - Use $(VPATH) prefix for loader-info.awk in the loader-info.c rule so out-of-tree builds can find the awk script. --- src/GNUmakefile | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/GNUmakefile b/src/GNUmakefile index 96035576..785efab0 100644 --- a/src/GNUmakefile +++ b/src/GNUmakefile @@ -11,6 +11,7 @@ INSTALL = install CC ?= $(CROSS_COMPILE)gcc LD = $(CC) STRIP ?= $(CROSS_COMPILE)strip +READELF ?= $(CROSS_COMPILE)readelf OBJCOPY ?= $(CROSS_COMPILE)objcopy OBJDUMP ?= $(CROSS_COMPILE)objdump @@ -86,7 +87,7 @@ OBJECTS += \ extension/fix_symlink_size/fix_symlink_size.o define define_from_arch.h -$2$1 := $(shell $(CC) $1 -E -dM -DNO_LIBC_HEADER $(SRC)/arch.h | grep -w $2 | cut -f 3 -d ' ') +$2$1 := $(shell $(CC) $1 -E -dM -DNO_LIBC_HEADER $(VPATH)/arch.h | grep -w $2 | cut -f 3 -d ' ') endef $(eval $(call define_from_arch.h,,HAS_LOADER_32BIT)) @@ -169,7 +170,9 @@ BUILD_ID_NONE := $(shell if ld --build-id=none --version >/dev/null 2>&1; then e ###################################################################### # Build rules -COMPILE = $($(quiet)CC) $(CPPFLAGS) $(CFLAGS) -MD -c $(SRC)$< -o $@ +# Use $< instead of $(SRC)$< because VPATH already resolves source paths. +# $(SRC)$< duplicates the prefix in out-of-tree builds. +COMPILE = $($(quiet)CC) $(CPPFLAGS) $(CFLAGS) -MD -c $< -o $@ LINK = $($(quiet)LD) -o $@ $^ $(LDFLAGS) OBJIFY = $($(quiet)GEN) \ @@ -186,7 +189,7 @@ proot: $(OBJECTS) # Special case to compute which files depend on the auto-generated # file "build.h". -USE_BUILD_H := $(patsubst $(SRC)%.c,%.o,$(shell egrep -sl 'include[[:space:]]+"build.h"' $(patsubst %.o,$(SRC)%.c,$(OBJECTS)))) +USE_BUILD_H := $(patsubst $(SRC)%.c,%.o,$(shell grep -E -sl 'include[[:space:]]+"build.h"' $(patsubst %.o,$(SRC)%.c,$(OBJECTS)))) $(USE_BUILD_H): build.h %.o: %.c @@ -235,7 +238,7 @@ $(eval $(call build_loader,-m32)) endif loader/loader-info.c: loader/loader - readelf -s $< | awk -f loader/loader-info.awk > $@ + $(READELF) -s $< | awk -f $(VPATH)/loader/loader-info.awk > $@ ###################################################################### # Dependencies