From b9014f858d1fd963a466228ec15572b0892a8490 Mon Sep 17 00:00:00 2001 From: Ahmad Fatoum Date: Tue, 12 Mar 2024 21:23:09 +0100 Subject: [PATCH] feat(build): redirect stdin to nul during toolchain detection It's common for Makefiles to use variables like CC, AS or LD instead of hardcoding the name of binaries. These can be defined by the user to use a differnet toolchain or even as a crutch to enable cross-compilation. In TF-A, this is not needed, as support for cross-compilation is baked in via the CROSS_COMPILE option. TF-A still defined AS for its internal use, but unlike most other projects, the default was setting it to the C compiler. Overriding it wasn't possible from the environment though, only as a make argument, so this didn't cause much issue. With commit cc277de81692 ("build: refactor toolchain detection"), AS can now also be set from the environment. This breaks any scripts that supply make with a cross environment that sets AS to an assembler. Doing so was without effect before, but now leads to a quite ugly failure mode: As TF-A now tries to detect the toolchain, it will call AS with the option -v, which for GNU as(1) prints the version, but doesn't exit. Thus, as(1) will continue waiting on stdin input and the build hangs without much indication what's wrong. Avoid this failure mode by ensuring any tool that attempts to read stdin during toolchain detection will immediately get EOF and exit, leading to an error message later on instead of the build hang. Change-Id: I79a84961f5a69250292caa7f9e879a65be4bd9f2 Signed-off-by: Ahmad Fatoum --- make_helpers/toolchain.mk | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/make_helpers/toolchain.mk b/make_helpers/toolchain.mk index 09f80dbf2..5293d4033 100644 --- a/make_helpers/toolchain.mk +++ b/make_helpers/toolchain.mk @@ -222,27 +222,27 @@ $(foreach toolchain,$(toolchains), \ # # Arm Compiler for Embedded -guess-tool-arm-clang = $(shell $(1) --version 2>&1 | grep -o "Tool: armclang") -guess-tool-arm-link = $(shell $(1) --help 2>&1 | grep -o "Tool: armlink") -guess-tool-arm-fromelf = $(shell $(1) --help 2>&1 | grep -o "Tool: fromelf") -guess-tool-arm-ar = $(shell $(1) --version 2>&1 | grep -o "Tool: armar") +guess-tool-arm-clang = $(shell $(1) --version 2>&1 <$(nul) | grep -o "Tool: armclang") +guess-tool-arm-link = $(shell $(1) --help 2>&1 <$(nul) | grep -o "Tool: armlink") +guess-tool-arm-fromelf = $(shell $(1) --help 2>&1 <$(nul) | grep -o "Tool: fromelf") +guess-tool-arm-ar = $(shell $(1) --version 2>&1 <$(nul) | grep -o "Tool: armar") # LLVM Project -guess-tool-llvm-clang = $(shell $(1) -v 2>&1 | grep -o "clang version") -guess-tool-llvm-lld = $(shell $(1) --help 2>&1 | grep -o "OVERVIEW: lld") -guess-tool-llvm-objcopy = $(shell $(1) --help 2>&1 | grep -o "llvm-objcopy tool") -guess-tool-llvm-objdump = $(shell $(1) --help 2>&1 | grep -o "llvm object file dumper") -guess-tool-llvm-ar = $(shell $(1) --help 2>&1 | grep -o "LLVM Archiver") +guess-tool-llvm-clang = $(shell $(1) -v 2>&1 <$(nul) | grep -o "clang version") +guess-tool-llvm-lld = $(shell $(1) --help 2>&1 <$(nul) | grep -o "OVERVIEW: lld") +guess-tool-llvm-objcopy = $(shell $(1) --help 2>&1 <$(nul) | grep -o "llvm-objcopy tool") +guess-tool-llvm-objdump = $(shell $(1) --help 2>&1 <$(nul) | grep -o "llvm object file dumper") +guess-tool-llvm-ar = $(shell $(1) --help 2>&1 <$(nul) | grep -o "LLVM Archiver") # GNU Compiler Collection & GNU Binary Utilities -guess-tool-gnu-gcc = $(shell $(1) -v 2>&1 | grep -o "gcc version") -guess-tool-gnu-ld = $(shell $(1) -v 2>&1 | grep -o "GNU ld") -guess-tool-gnu-objcopy = $(shell $(1) --version 2>&1 | grep -o "GNU objcopy") -guess-tool-gnu-objdump = $(shell $(1) --version 2>&1 | grep -o "GNU objdump") -guess-tool-gnu-ar = $(shell $(1) --version 2>&1 | grep -o "GNU ar") +guess-tool-gnu-gcc = $(shell $(1) -v 2>&1 <$(nul) | grep -o "gcc version") +guess-tool-gnu-ld = $(shell $(1) -v 2>&1 <$(nul) | grep -o "GNU ld") +guess-tool-gnu-objcopy = $(shell $(1) --version 2>&1 <$(nul) | grep -o "GNU objcopy") +guess-tool-gnu-objdump = $(shell $(1) --version 2>&1 <$(nul) | grep -o "GNU objdump") +guess-tool-gnu-ar = $(shell $(1) --version 2>&1 <$(nul) | grep -o "GNU ar") # Other tools -guess-tool-dtc = $(shell $(1) --version 2>&1 | grep -o "Version: DTC") +guess-tool-dtc = $(shell $(1) --version 2>&1 <$(nul) | grep -o "Version: DTC") guess-tool = $(firstword $(foreach candidate,$(1), \ $(if $(call guess-tool-$(candidate),$(2)),$(candidate))))