Makefile: move determination test for OS to Makefile.d

Move the test code for OS detection in a extra directory to split it
from the main flashrom code.

flashrom-stable:
* Use `tail -n` to be standard compliant
* Fix some typos in comment

Change-Id: Id911f17f4100f242e1fde10d23a8459ddf38b369
Signed-off-by: Thomas Heijligen <thomas.heijligen@secunet.de>
Original-Reviewed-on: https://review.coreboot.org/c/flashrom/+/58015
Original-Reviewed-by: Nico Huber <nico.h@gmx.de>
Reviewed-on: https://review.coreboot.org/c/flashrom-stable/+/72241
Reviewed-by: Nico Huber <nico.h@gmx.de>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
diff --git a/.gitignore b/.gitignore
index bcb11b7..b824e15 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
 *.d
+!Makefile.d/
 *.o
 /.features
 /.dependencies
diff --git a/Makefile b/Makefile
index ab7a315..b47d51e 100644
--- a/Makefile
+++ b/Makefile
@@ -164,14 +164,12 @@
 # IMPORTANT: The following lines must be placed before TARGET_OS, ARCH or ENDIAN
 # is ever used (of course), but should come after any lines setting CC because
 # the lines below use CC itself.
-override TARGET_OS := $(strip $(call debug_shell,$(CC) $(CPPFLAGS) -E os.h 2>/dev/null \
-    | tail -1 | cut -f 2 -d'"'))
+override TARGET_OS := $(call c_macro_test, Makefile.d/os_test.h)
 override ARCH := $(strip $(call debug_shell,$(CC) $(CPPFLAGS) -E archtest.c 2>/dev/null \
     | tail -1 | cut -f 2 -d'"'))
 override ENDIAN := $(strip $(call debug_shell,$(CC) $(CPPFLAGS) -E endiantest.c 2>/dev/null \
     | tail -1))
 
-
 ifeq ($(TARGET_OS), $(filter $(TARGET_OS), FreeBSD OpenBSD DragonFlyBSD))
 override CPPFLAGS += -I/usr/local/include
 override LDFLAGS += -L/usr/local/lib
@@ -823,7 +821,7 @@
 # This includes all frontends and libflashrom.
 # We don't use EXEC_SUFFIX here because we want to clean everything.
 clean:
-	rm -f $(PROGRAM) $(PROGRAM).exe libflashrom.a *.o *.d $(PROGRAM).8 $(PROGRAM).8.html $(BUILD_DETAILS_FILE)
+	rm -f $(PROGRAM) $(PROGRAM).exe libflashrom.a $(filter-out Makefile.d, $(wildcard *.d *.o)) $(PROGRAM).8 $(PROGRAM).8.html $(BUILD_DETAILS_FILE)
 	@+$(MAKE) -C util/ich_descriptors_tool/ clean
 
 distclean: clean
@@ -848,11 +846,8 @@
 	@echo $(ARCH)|wc -w|grep -q '^[[:blank:]]*1[[:blank:]]*$$' ||	\
 		( echo "unknown (\"$(ARCH)\"). Aborting."; exit 1)
 	@printf "%s\n" '$(ARCH)'
-	@printf "Target OS is "
-	@# FreeBSD wc will output extraneous whitespace.
-	@echo $(TARGET_OS)|wc -w|grep -q '^[[:blank:]]*1[[:blank:]]*$$' ||	\
-		( echo "unknown (\"$(TARGET_OS)\"). Aborting."; exit 1)
-	@printf "%s\n" '$(TARGET_OS)'
+	@echo Target OS is $(TARGET_OS)
+	@if [ $(TARGET_OS) = unknown ]; then echo Aborting.; exit 1; fi
 ifeq ($(TARGET_OS), libpayload)
 	@$(CC) --version 2>&1 | grep -q coreboot || \
 		( echo "Warning: It seems you are not using coreboot's reference compiler."; \
diff --git a/os.h b/Makefile.d/os_test.h
similarity index 93%
rename from os.h
rename to Makefile.d/os_test.h
index d6fbfe0..2cf6be0 100644
--- a/os.h
+++ b/Makefile.d/os_test.h
@@ -14,7 +14,7 @@
  */
 
 /*
- * Header file for OS checking.
+ * This file determines the target os. It should only be used by the Makefile
  */
 
 // Solaris
@@ -61,5 +61,7 @@
 	#else
 		#define __FLASHROM_OS__ "Linux"
 	#endif
+#else
+#define __FLASHROM_OS__ "unknown"
 #endif
 __FLASHROM_OS__
diff --git a/Makefile.include b/Makefile.include
index 62c040c..12fdd81 100644
--- a/Makefile.include
+++ b/Makefile.include
@@ -31,6 +31,10 @@
 	$(eval override $(p) := no))
 endef
 
+# Run the C Preprocessor with file $1 and return the last line, removing quotes.
+define c_macro_test
+$(strip $(shell $(CC) -E $1 2>/dev/null | tail -n 1 | tr -d '"'))
+endef
 
 define COMPILER_TEST
 int main(int argc, char **argv)
diff --git a/util/ich_descriptors_tool/Makefile b/util/ich_descriptors_tool/Makefile
index 42b950a..a84894e 100644
--- a/util/ich_descriptors_tool/Makefile
+++ b/util/ich_descriptors_tool/Makefile
@@ -4,6 +4,8 @@
 # This Makefile works standalone, but it is usually called from the main
 # Makefile in the flashrom directory.
 
+include ../../Makefile.include
+
 PROGRAM=ich_descriptors_tool
 EXTRAINCDIRS = ../../ .
 DEPPATH = .dep
@@ -28,8 +30,7 @@
 CC = gcc
 endif
 
-override TARGET_OS := $(shell $(CC) $(CPPFLAGS) -E $(SHAREDSRCDIR)/os.h | grep -v '^\#' | grep '"' | \
-			cut -f 2 -d'"')
+override TARGET_OS := $(call c_macro_test, ../../Makefile.d/os_test.h)
 
 ifeq ($(TARGET_OS), DOS)
 EXEC_SUFFIX := .exe