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