1From da6b3b70cb852dd8e9f9e21aef95fa83e7f7ab0d Mon Sep 17 00:00:00 2001
2From: Pyry Kontio <pyry.kontio@drasa.eu>
3Date: Mon, 6 Jul 2020 12:57:35 +0900
4Subject: [PATCH] Makefile: Fix building on AArch64 NixOS
5
6The parsing of the output of archtest.c produced an unexpected
7value on AArch64 NixOS. For example, the make variable ARCH was set to:
8
9```
10bit outside of fd_set selected
11arm
12```
13
14This made the arch and OS checks fail.
15
16This commit simplifies the parsing, making it more robust.
17
18The C files archtest.c, endiantest.c and os.h used to set the
19TARGET_OS, ARCH and ENDIAN variables, respectively, output
20the result of the test as the final line, so just extracting
21the final line and removing double quoting is enough.
22
23This commit also fixes a bug with debug_shell lacking escaping
24single quotes, which prevented using the single quote in the
25debug_shell calls. It used to work by accident before this fix;
26the line in the call happened to contain a balanced pair of double
27quotes and lacked other characters that needed escaping, which
28didn't break the debug_shell, but this was accidental and very
29brittle.
30
31Signed-off-by: Pyry Kontio <pyry.kontio@drasa.eu>
32Change-Id: Iaa4477a71e758cf9ecad2c22f3b77bc6508a3510
33Reviewed-on: https://review.coreboot.org/c/flashrom/+/43140
34Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
35Reviewed-by: Angel Pons <th3fanbus@gmail.com>
36
37[Retrieved from:
38https://github.com/flashrom/flashrom/commit/da6b3b70cb852dd8e9f9e21aef95fa83e7f7ab0d]
39Signed-off-by: Fabrice Fontaine <fontaine.fabrice@gmail.com>
40---
41 Makefile | 16 ++++++++++------
42 1 file changed, 10 insertions(+), 6 deletions(-)
43
44diff --git a/Makefile b/Makefile
45index f3f7717e2..e475cbdbd 100644
46--- a/Makefile
47+++ b/Makefile
48@@ -83,7 +83,8 @@ dummy_for_make_3_80:=$(shell printf "Build started on %s\n\n" "$$(date)" >$(BUIL
49
50 # Provide an easy way to execute a command, print its output to stdout and capture any error message on stderr
51 # in the build details file together with the original stdout output.
52-debug_shell = $(shell export LC_ALL=C ; { echo 'exec: export LC_ALL=C ; { $(1) ; }' >&2;  { $(1) ; } | tee -a $(BUILD_DETAILS_FILE) ; echo >&2 ; } 2>>$(BUILD_DETAILS_FILE))
53+debug_shell = $(shell export LC_ALL=C ; { echo 'exec: export LC_ALL=C ; { $(subst ','\'',$(1)) ; }' >&2; \
54+    { $(1) ; } | tee -a $(BUILD_DETAILS_FILE) ; echo >&2 ; } 2>>$(BUILD_DETAILS_FILE))
55
56 ###############################################################################
57 # General OS-specific settings.
58@@ -106,7 +107,8 @@ endif
59 # IMPORTANT: The following line must be placed before TARGET_OS is ever used
60 # (of course), but should come after any lines setting CC because the line
61 # below uses CC itself.
62-override TARGET_OS := $(strip $(call debug_shell,$(CC) $(CPPFLAGS) -E os.h 2>/dev/null | grep -v '^\#' | grep '"' | cut -f 2 -d'"'))
63+override TARGET_OS := $(strip $(call debug_shell,$(CC) $(CPPFLAGS) -E os.h 2>/dev/null \
64+    | tail -1 | cut -f 2 -d'"'))
65
66 ifeq ($(TARGET_OS), Darwin)
67 override CPPFLAGS += -I/opt/local/include -I/usr/local/include
68@@ -490,8 +492,10 @@ endif
69 # IMPORTANT: The following line must be placed before ARCH is ever used
70 # (of course), but should come after any lines setting CC because the line
71 # below uses CC itself.
72-override ARCH := $(strip $(call debug_shell,$(CC) $(CPPFLAGS) -E archtest.c 2>/dev/null | grep -v '^\#' | grep '"' | cut -f 2 -d'"'))
73-override ENDIAN := $(strip $(call debug_shell,$(CC) $(CPPFLAGS) -E endiantest.c 2>/dev/null | grep -v '^\#'))
74+override ARCH := $(strip $(call debug_shell,$(CC) $(CPPFLAGS) -E archtest.c 2>/dev/null \
75+    | tail -1 | cut -f 2 -d'"'))
76+override ENDIAN := $(strip $(call debug_shell,$(CC) $(CPPFLAGS) -E endiantest.c 2>/dev/null \
77+    | tail -1))
78
79 # Disable the internal programmer on unsupported architectures (everything but x86 and mipsel)
80 ifneq ($(ARCH)-little, $(filter $(ARCH),x86 mips)-$(ENDIAN))
81@@ -1299,12 +1303,12 @@ compiler: featuresavailable
82 	@printf "Target arch is "
83 	@# FreeBSD wc will output extraneous whitespace.
84 	@echo $(ARCH)|wc -w|grep -q '^[[:blank:]]*1[[:blank:]]*$$' ||	\
85-		( echo "unknown. Aborting."; exit 1)
86+		( echo "unknown (\"$(ARCH)\"). Aborting."; exit 1)
87 	@printf "%s\n" '$(ARCH)'
88 	@printf "Target OS is "
89 	@# FreeBSD wc will output extraneous whitespace.
90 	@echo $(TARGET_OS)|wc -w|grep -q '^[[:blank:]]*1[[:blank:]]*$$' ||	\
91-		( echo "unknown. Aborting."; exit 1)
92+		( echo "unknown (\"$(TARGET_OS)\"). Aborting."; exit 1)
93 	@printf "%s\n" '$(TARGET_OS)'
94 ifeq ($(TARGET_OS), libpayload)
95 	@$(CC) --version 2>&1 | grep -q coreboot || \
96