[flashrom] [PATCH] Makefile: fix and simplify test program compilations

Stefan Tauner stefan.tauner at student.tuwien.ac.at
Fri Aug 12 21:38:37 CEST 2011


this was totally broken due to the time behavior of make's shell
function's temporal behavior.
quote from the gnu make documentation
http://www.gnu.org/s/hello/manual/make/Shell-Function.html):
"The commands run by calls to the shell function are run when the
function calls are expanded"
we have used the shell function to echo the test programs to a file.
the file name used was equal for all tests and was overwritten for
each test. the result was that all tests (in a single target?) used
the last test program because the echoing of the test programs was
done before all test compilations(!)

also the branching for testing ifeq ($(CONFIG_FT2232_SPI), yes) was
unnecessary.

in my approach here i am using verbatim variables (allows to define
even complex test programs in the makefile without jumping through
hoops) that get exported to environment variables (via "export",
reference afterwards with "$$<varname>").

i have also added the missing redirection of stderr to the compiler test.
---
alternatively one could just use different file names of course, but
i think my approach is superior due to the separation and
readability of the tested code and the fact that the temporal
behavior is MUCH clearer now (else this bug would have not crept in
in the first place :P)

if line count is an issue one could of course compress the test
programs to something unreadable again :)

side note: we use $(CC) all over the place before we even check if
it is there. e.g.:
override ARCH = $(strip $(shell LC_ALL=C $(CC) -E arch.h|grep -v '^\#'))

this creates unwanted output like:
make -j1 CC=eiugthes
/bin/sh: eiugthes: not found
/bin/sh: eiugthes: not found
/bin/sh: eiugthes: not found
/bin/sh: eiugthes: not found
Checking for a C compiler... not found.
make: *** [compiler] Error 1

Signed-off-by: Stefan Tauner <stefan.tauner at student.tuwien.ac.at>
---
 Makefile |   91 ++++++++++++++++++++++++++++++++++++++++----------------------
 1 files changed, 59 insertions(+), 32 deletions(-)

diff --git a/Makefile b/Makefile
index c72d10a..672a0df 100644
--- a/Makefile
+++ b/Makefile
@@ -531,11 +531,62 @@ distclean: clean
 strip: $(PROGRAM)$(EXEC_SUFFIX)
 	$(STRIP) $(STRIP_ARGS) $(PROGRAM)$(EXEC_SUFFIX)
 
+# below are the definitions of test programs as verbatim variables, which get
+# exported to environment variables and are referenced with $$<varname> later
+define COMPILER_TEST
+int main(int argc, char **argv)
+{
+	(void) argafc;
+	(void) argv;
+	return 0;
+}
+endef
+export COMPILER_TEST
+
+define LIBPCI_TEST
+/* Avoid a failing test due to libpci header symbol shadowing breakage */
+#define index shadow_workaround_index
+#include <pci/pci.h>
+struct pci_access *pacc;
+int main(int argc, char **argv)
+{
+	(void) argc;
+	(void) argv;
+	pacc = pci_alloc();
+	return 0;
+}
+endef
+export LIBPCI_TEST
+
+define FTDI_TEST
+#include <ftdi.h>
+struct ftdi_context *ftdic = NULL;
+int main(int argc, char **argv)
+{
+	(void) argc;
+	(void) argv;
+	return ftdi_init(ftdic);
+}
+endef
+export FTDI_TEST
+
+define UTSNAME_TEST
+#include <sys/utsname.h>
+struct utsname osinfo;
+int main(int argc, char **argv)
+{
+	(void) argc;
+	(void) argv;
+	uname (&osinfo);
+	return 0;
+}
+endef
+export UTSNAME_TEST
+
 compiler: featuresavailable
 	@printf "Checking for a C compiler... "
-	@$(shell ( echo "int main(int argc, char **argv)"; \
-		   echo "{ (void) argc; (void) argv; return 0; }"; ) > .test.c )
-	@$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) .test.c -o .test$(EXEC_SUFFIX) >/dev/null &&	\
+	@echo "$$COMPILER_TEST" > .test.c
+	@$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) .test.c -o .test$(EXEC_SUFFIX) >/dev/null 2>&1 &&	\
 		echo "found." || ( echo "not found."; \
 		rm -f .test.c .test$(EXEC_SUFFIX); exit 1)
 	@rm -f .test.c .test$(EXEC_SUFFIX)
@@ -548,12 +599,7 @@ compiler: featuresavailable
 ifeq ($(CHECK_LIBPCI), yes)
 pciutils: compiler
 	@printf "Checking for libpci headers... "
-	@# Avoid a failing test due to libpci header symbol shadowing breakage
-	@$(shell ( echo "#define index shadow_workaround_index"; \
-		   echo "#include <pci/pci.h>";		   \
-		   echo "struct pci_access *pacc;";	   \
-		   echo "int main(int argc, char **argv)"; \
-		   echo "{ (void) argc; (void) argv; pacc = pci_alloc(); return 0; }"; ) > .test.c )
+	@echo "$$LIBPCI_TEST" > .test.c
 	@$(CC) -c $(CPPFLAGS) $(CFLAGS) .test.c -o .test.o >/dev/null 2>&1 &&		\
 		echo "found." || ( echo "not found."; echo;			\
 		echo "Please install libpci headers (package pciutils-devel).";	\
@@ -588,41 +634,22 @@ featuresavailable:
 	@false
 endif
 
-ifeq ($(CONFIG_FT2232_SPI), yes)
 features: compiler
 	@echo "FEATURES := yes" > .features.tmp
+ifeq ($(CONFIG_FT2232_SPI), yes)
 	@printf "Checking for FTDI support... "
-	@$(shell ( echo "#include <ftdi.h>";		   \
-		   echo "struct ftdi_context *ftdic = NULL;";	   \
-		   echo "int main(int argc, char **argv)"; \
-		   echo "{ (void) argc; (void) argv; return ftdi_init(ftdic); }"; ) > .featuretest.c )
+	@echo "$$FTDI_TEST" > .featuretest.c
 	@$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) .featuretest.c -o .featuretest$(EXEC_SUFFIX) $(FTDILIBS) $(LIBS) >/dev/null 2>&1 &&	\
 		( echo "found."; echo "FTDISUPPORT := yes" >> .features.tmp ) ||	\
 		( echo "not found."; echo "FTDISUPPORT := no" >> .features.tmp )
+endif
 	@printf "Checking for utsname support... "
-	@$(shell ( echo "#include <sys/utsname.h>";		   \
-		   echo "struct utsname osinfo;";	   \
-		   echo "int main(int argc, char **argv)"; \
-		   echo "{ (void) argc; (void) argv; uname (&osinfo); return 0; }"; ) > .featuretest.c )
-	@$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) .featuretest.c -o .featuretest$(EXEC_SUFFIX) >/dev/null 2>&1 &&	\
-		( echo "found."; echo "UTSNAME := yes" >> .features.tmp ) ||	\
-		( echo "not found."; echo "UTSNAME := no" >> .features.tmp )
-	@$(DIFF) -q .features.tmp .features >/dev/null 2>&1 && rm .features.tmp || mv .features.tmp .features
-	@rm -f .featuretest.c .featuretest$(EXEC_SUFFIX)
-else
-features: compiler
-	@echo "FEATURES := yes" > .features.tmp
-	@printf "Checking for utsname support... "
-	@$(shell ( echo "#include <sys/utsname.h>";		   \
-		   echo "struct utsname osinfo;";	   \
-		   echo "int main(int argc, char **argv)"; \
-		   echo "{ (void) argc; (void) argv; uname (&osinfo); return 0; }"; ) > .featuretest.c )
+	@echo "$$UTSNAME_TEST" > .featuretest.c
 	@$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) .featuretest.c -o .featuretest$(EXEC_SUFFIX) >/dev/null 2>&1 &&	\
 		( echo "found."; echo "UTSNAME := yes" >> .features.tmp ) ||	\
 		( echo "not found."; echo "UTSNAME := no" >> .features.tmp )
 	@$(DIFF) -q .features.tmp .features >/dev/null 2>&1 && rm .features.tmp || mv .features.tmp .features
 	@rm -f .featuretest.c .featuretest$(EXEC_SUFFIX)
-endif
 
 install: $(PROGRAM)$(EXEC_SUFFIX)
 	mkdir -p $(DESTDIR)$(PREFIX)/sbin
-- 
1.7.1





More information about the flashrom mailing list