Skip to content

GNUmakefile: fix portability issues for out-of-tree and cross builds#342

Open
Ebola-Chan-bot wants to merge 1 commit intotermux:masterfrom
Ebola-Chan-bot:fix/makefile-portability
Open

GNUmakefile: fix portability issues for out-of-tree and cross builds#342
Ebola-Chan-bot wants to merge 1 commit intotermux:masterfrom
Ebola-Chan-bot:fix/makefile-portability

Conversation

@Ebola-Chan-bot
Copy link
Copy Markdown

@Ebola-Chan-bot Ebola-Chan-bot commented Apr 4, 2026

Five small fixes to make GNUmakefile work correctly in out-of-tree builds (make -f /path/to/src/GNUmakefile) and cross-compilation setups where the toolchain has a $(CROSS_COMPILE) prefix.

1. Fix source path duplication in COMPILE macro

# Before
COMPILE = ... -c $(SRC)$< -o $@
# After
COMPILE = ... -c $< -o $@

VPATH already causes make to resolve $< to the full source path. Prepending $(SRC) duplicates the prefix in out-of-tree builds, producing paths like ../../src/../../src/cli/cli.c.

2. Use $(VPATH) instead of $(SRC) in define_from_arch.h

# Before
$2$1 := $(shell $(CC) $1 -E -dM -DNO_LIBC_HEADER $(SRC)/arch.h | ...)
# After
$2$1 := $(shell $(CC) $1 -E -dM -DNO_LIBC_HEADER $(VPATH)/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. CPPFLAGS already uses -I$(VPATH). This change aligns define_from_arch.h with the rest of the file.

3. Replace deprecated egrep with grep -E

# Before
USE_BUILD_H := $(patsubst ...,$(shell egrep -sl ...))
# After
USE_BUILD_H := $(patsubst ...,$(shell grep -E -sl ...))

egrep has been deprecated by POSIX (IEEE Std 1003.1-2024) and GNU grep now emits a warning. On some systems (e.g. MSYS2/Git-for-Windows), egrep is a wrapper shell script whose shebang path may contain spaces, which breaks $(shell) invocations on native-Windows make implementations.

4. Add READELF variable

READELF  ?= $(CROSS_COMPILE)readelf

This follows the existing pattern for CC, STRIP, OBJCOPY, and OBJDUMP, which all use ?= with $(CROSS_COMPILE) prefix. The hardcoded readelf in the loader-info.c rule is replaced with $(READELF).

5. Use $(VPATH) for loader-info.awk path

# Before
readelf -s $< | awk -f loader/loader-info.awk > $@
# After
$(READELF) -s $< | awk -f $(VPATH)/loader/loader-info.awk > $@

In out-of-tree builds, the awk script is in the source tree, not the build directory. Without $(VPATH), awk -f loader/loader-info.awk fails with "file not found".

- 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.
Copilot AI review requested due to automatic review settings April 4, 2026 15:15
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Improves src/GNUmakefile portability and correctness for out-of-tree builds and cross-compilation by relying on VPATH-resolved paths and toolchain-prefixed utilities.

Changes:

  • Use $(VPATH) (not $(SRC)) for source-directory references where the Makefile declares VPATH as the canonical makefile/source dir.
  • Fix out-of-tree compile path handling by compiling $< directly (letting VPATH resolve the correct source path).
  • Replace deprecated egrep usage with grep -E, and add a configurable READELF for cross builds (also VPATH-prefix the AWK script path).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants