Makefile: Make pkg-config mandatory to find libpci

Use `make HAS_LIBPCI=yes/no` to override the pkg-config detection and
`CONFIG_LIBPCI_CFLAGS` and `CONFIG_LIBPCI_LDFLAGS` to set cflags and
ldflags manually. The optional dependency of libpci libz, is
automatically handled by pkg-config.

Change-Id: I21b4a261b34b7e688635fc6e20b7beebfa64c7ed
Signed-off-by: Thomas Heijligen <thomas.heijligen@secunet.com>
Original-Reviewed-on: https://review.coreboot.org/c/flashrom/+/59050
Original-Reviewed-by: Nico Huber <nico.h@gmx.de>
Original-Reviewed-by: Paul Menzel <paulepanter@mailbox.org>
Reviewed-on: https://review.coreboot.org/c/flashrom-stable/+/72281
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Nico Huber <nico.h@gmx.de>
diff --git a/Makefile b/Makefile
index d2e91a5..7bd8839 100644
--- a/Makefile
+++ b/Makefile
@@ -220,26 +220,31 @@
 CONFIG_LIBUSB1_CFLAGS      := $(call dependency_cflags, libusb-1.0)
 CONFIG_LIBUSB1_LDFLAGS     := $(call dependency_ldflags, libusb-1.0)
 
+CONFIG_LIBPCI_CFLAGS       := $(call dependency_cflags, libpci)
+CONFIG_LIBPCI_LDFLAGS      := $(call dependency_ldflags, libpci)
+
 # Determine the destination OS, architecture and endian
 # 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 := $(call c_macro_test, Makefile.d/os_test.h)
-override ARCH      := $(call c_macro_test, Makefile.d/arch_test.h)
-override ENDIAN    := $(call c_macro_test, Makefile.d/endian_test.h)
+override TARGET_OS  := $(call c_macro_test, Makefile.d/os_test.h)
+override ARCH       := $(call c_macro_test, Makefile.d/arch_test.h)
+override ENDIAN     := $(call c_macro_test, Makefile.d/endian_test.h)
 
 
-HAS_LIBFTDI1       := $(call find_dependency, libftdi1)
-HAS_LIB_NI845X     := no
-HAS_LIBJAYLINK     := $(call find_dependency, libjaylink)
-HAS_LIBUSB1        := $(call find_dependency, libusb-1.0)
+HAS_LIBFTDI1        := $(call find_dependency, libftdi1)
+HAS_LIB_NI845X      := no
+HAS_LIBJAYLINK      := $(call find_dependency, libjaylink)
+HAS_LIBUSB1         := $(call find_dependency, libusb-1.0)
+HAS_LIBPCI          := $(call find_dependency, libpci)
 
-HAS_FT232H         := $(call c_compile_test, Makefile.d/ft232h_test.c, $(CONFIG_LIBFTDI1_CFLAGS))
-HAS_UTSNAME        := $(call c_compile_test, Makefile.d/utsname_test.c)
-HAS_CLOCK_GETTIME  := $(call c_compile_test, Makefile.d/clock_gettime_test.c)
-HAS_LINUX_MTD      := $(call c_compile_test, Makefile.d/linux_mtd_test.c)
-HAS_LINUX_SPI      := $(call c_compile_test, Makefile.d/linux_spi_test.c)
-HAS_LINUX_I2C      := $(call c_compile_test, Makefile.d/linux_i2c_test.c)
+HAS_PCI_OLD_GET_DEV := $(call c_compile_test, Makefile.d/pci_old_get_dev_test.c, $(CONFIG_LIBPCI_CFLAGS))
+HAS_FT232H          := $(call c_compile_test, Makefile.d/ft232h_test.c, $(CONFIG_LIBFTDI1_CFLAGS))
+HAS_UTSNAME         := $(call c_compile_test, Makefile.d/utsname_test.c)
+HAS_CLOCK_GETTIME   := $(call c_compile_test, Makefile.d/clock_gettime_test.c)
+HAS_LINUX_MTD       := $(call c_compile_test, Makefile.d/linux_mtd_test.c)
+HAS_LINUX_SPI       := $(call c_compile_test, Makefile.d/linux_spi_test.c)
+HAS_LINUX_I2C       := $(call c_compile_test, Makefile.d/linux_i2c_test.c)
 
 ifeq ($(TARGET_OS), $(filter $(TARGET_OS), FreeBSD OpenBSD DragonFlyBSD))
 override CPPFLAGS += -I/usr/local/include
@@ -332,6 +337,10 @@
 $(call mark_unsupported,CONFIG_INTERNAL)
 endif
 
+ifeq ($(HAS_LIBPCI), no)
+$(call mark_unsupported,$(DEPENDS_ON_LIBPCI))
+endif
+
 ifeq ($(HAS_LIBFTDI1), no)
 $(call mark_unsupported,$(DEPENDS_ON_LIBFTDI1))
 endif
@@ -792,21 +801,6 @@
 endif
 endif
 
-NEED_LIBPCI := $(call filter_deps,$(DEPENDS_ON_LIBPCI))
-ifneq ($(NEED_LIBPCI), )
-CHECK_LIBPCI = yes
-FEATURE_CFLAGS += -D'NEED_PCI=1'
-FEATURE_CFLAGS += $(call debug_shell,grep -q "OLD_PCI_GET_DEV := yes" .libdeps && printf "%s" "-D'OLD_PCI_GET_DEV=1'")
-
-PROGRAMMER_OBJS += pcidev.o
-ifeq ($(TARGET_OS), NetBSD)
-# The libpci we want is called libpciutils on NetBSD and needs NetBSD libpci.
-PCILIBS += -lpciutils -lpci
-else
-PCILIBS += -lpci
-endif
-endif
-
 USE_X86_MSR := $(if $(call filter_deps,$(DEPENDS_ON_X86_MSR)),yes,no)
 ifeq ($(USE_X86_MSR), yes)
 PROGRAMMER_OBJS += hwaccess_x86_msr.o
@@ -835,6 +829,14 @@
 override LDFLAGS += -l$(shell uname -m)
 endif
 
+USE_LIBPCI := $(if $(call filter_deps,$(DEPENDS_ON_LIBPCI)),yes,no)
+ifeq ($(USE_LIBPCI), yes)
+PROGRAMMER_OBJS += pcidev.o
+FEATURE_CFLAGS += -D'NEED_PCI=1'
+override CFLAGS  += $(CONFIG_LIBPCI_CFLAGS)
+override LDFLAGS += $(CONFIG_LIBPCI_LDFLAGS)
+endif
+
 USE_LIBUSB1 := $(if $(call filter_deps,$(DEPENDS_ON_LIBUSB1)),yes,no)
 ifeq ($(USE_LIBUSB1), yes)
 override CFLAGS  += $(CONFIG_LIBUSB1_CFLAGS)
@@ -868,9 +870,6 @@
 CLI_OBJS += print_wiki.o
 endif
 
-# We could use PULLED_IN_LIBS, but that would be ugly.
-FEATURE_LIBS += $(call debug_shell,grep -q "NEEDLIBZ := yes" .libdeps && printf "%s" "-lz")
-
 ifeq ($(HAS_UTSNAME), yes)
 FEATURE_CFLAGS += -D'HAVE_UTSNAME=1'
 endif
@@ -889,7 +888,7 @@
 endif
 
 $(PROGRAM)$(EXEC_SUFFIX): $(OBJS)
-	$(CC) -o $(PROGRAM)$(EXEC_SUFFIX) $(OBJS) $(LDFLAGS) $(LIBS) $(PCILIBS) $(FEATURE_LIBS)
+	$(CC) -o $(PROGRAM)$(EXEC_SUFFIX) $(OBJS) $(LDFLAGS) $(LIBS) $(FEATURE_LIBS)
 
 libflashrom.a: $(LIBFLASHROM_OBJS)
 	$(AR) rcs $@ $^
@@ -935,39 +934,12 @@
 	@if [ $(ENDIAN) = unknown ]; then echo Aborting.; exit 1; fi
 
 hwlibs: compiler
-	@printf "" > .libdeps
-ifeq ($(CHECK_LIBPCI), yes)
-	@printf "Checking for libpci headers... " | tee -a $(BUILD_DETAILS_FILE)
-	@echo "$$LIBPCI_TEST" > .test.c
-	@printf "\nexec: %s\n" "$(CC) -c $(CPPFLAGS) $(CFLAGS) .test.c -o .test.o" >>$(BUILD_DETAILS_FILE)
-	@{ { { { { $(CC) -c $(CPPFLAGS) $(CFLAGS) .test.c -o .test.o >&2 && \
-		echo "found." || { echo "not found."; echo;			\
-		echo "The following features require libpci: $(NEED_LIBPCI).";	\
-		echo "Please install libpci headers or disable all features"; \
-		echo "mentioned above by specifying make CONFIG_ENABLE_LIBPCI_PROGRAMMERS=no"; \
-		echo "See README for more information."; echo;			\
-		rm -f .test.c .test.o; exit 1; }; } 2>>$(BUILD_DETAILS_FILE); echo $? >&3 ; } | tee -a $(BUILD_DETAILS_FILE) >&4; } 3>&1;} | { read rc ; exit ${rc}; } } 4>&1
-	@printf "Checking version of pci_get_dev... " | tee -a $(BUILD_DETAILS_FILE)
-	@echo "$$PCI_GET_DEV_TEST" > .test.c
-	@printf "\nexec: %s\n" "$(CC) -c $(CPPFLAGS) $(CFLAGS) .test.c -o .test.o" >>$(BUILD_DETAILS_FILE)
-	@ { $(CC) -c $(CPPFLAGS) $(CFLAGS) .test.c -o .test.o >&2 && \
-		( echo "new version (including PCI domain parameter)."; echo "OLD_PCI_GET_DEV := no" >> .libdeps ) ||	\
-		( echo "old version (without PCI domain parameter)."; echo "OLD_PCI_GET_DEV := yes" >> .libdeps ) } 2>>$(BUILD_DETAILS_FILE) | tee -a $(BUILD_DETAILS_FILE)
-	@printf "Checking if libpci is present and sufficient... " | tee -a $(BUILD_DETAILS_FILE)
-	@printf "\nexec: %s\n" "$(CC) $(LDFLAGS) .test.o -o .test$(EXEC_SUFFIX) $(LIBS) $(PCILIBS)" >>$(BUILD_DETAILS_FILE)
-	@{ { { { $(CC) $(LDFLAGS) .test.o -o .test$(EXEC_SUFFIX) $(LIBS) $(PCILIBS) 2>>$(BUILD_DETAILS_FILE) >&2 && \
-		echo "yes." || { echo "no.";							\
-		printf "Checking if libz+libpci are present and sufficient..." ; \
-		{ printf "\nexec: %s\n" "$(CC) $(LDFLAGS) .test.o -o .test$(EXEC_SUFFIX) $(LIBS) $(PCILIBS) -lz" >>$(BUILD_DETAILS_FILE) ; \
-		$(CC) $(LDFLAGS) .test.o -o .test$(EXEC_SUFFIX) $(LIBS) $(PCILIBS) -lz >&2 && \
-		echo "yes." && echo "NEEDLIBZ := yes" > .libdeps } || { echo "no."; echo;	\
-		echo "The following features require libpci: $(NEED_LIBPCI).";			\
-		echo "Please install libpci (package pciutils) and/or libz or disable all features"; \
-		echo "mentioned above by specifying make CONFIG_ENABLE_LIBPCI_PROGRAMMERS=no"; \
-		echo "See README for more information."; echo;				\
-		rm -f .test.c .test.o .test$(EXEC_SUFFIX); exit 1; }; }; } 2>>$(BUILD_DETAILS_FILE); echo $? >&3 ; } | tee -a $(BUILD_DETAILS_FILE) >&4; } 3>&1;} | { read rc ; exit ${rc}; } } 4>&1
-	@rm -f .test.c .test.o .test$(EXEC_SUFFIX)
-endif
+	@echo Dependency libpci found: $(HAS_LIBPCI)
+	@if [ $(HAS_LIBPCI) = yes ]; then			\
+		echo "  Checking for old \"pci_get_dev()\": $(HAS_PCI_OLD_GET_DEV)";\
+		echo "  CFLAGS: $(CONFIG_LIBPCI_CFLAGS)";	\
+		echo "  LDFLAGS: $(CONFIG_LIBPCI_LDFLAGS)";	\
+	fi
 	@echo Dependency libusb1 found: $(HAS_LIBUSB1)
 	@if [ $(HAS_LIBUSB1) = yes ]; then			\
 		echo "  CFLAGS: $(CONFIG_LIBUSB1_CFLAGS)";	\
diff --git a/Makefile.d/pci_old_get_dev_test.c b/Makefile.d/pci_old_get_dev_test.c
new file mode 100644
index 0000000..35739c1
--- /dev/null
+++ b/Makefile.d/pci_old_get_dev_test.c
@@ -0,0 +1,17 @@
+/* Avoid a failing test due to libpci header symbol shadowing breakage */
+#define index shadow_workaround_index
+#if defined (PCIUTILS_PCI_H)
+#include <pciutils/pci.h>
+#else
+#include <pci/pci.h>
+#endif
+struct pci_access *pacc;
+struct pci_dev *dev = {0};
+int main(int argc, char **argv)
+{
+	(void) argc;
+	(void) argv;
+	pacc = pci_alloc();
+	dev = pci_get_dev(pacc, dev->bus, dev->dev, 1);
+	return 0;
+}
diff --git a/Makefile.include b/Makefile.include
index 20836ae..819e7f5 100644
--- a/Makefile.include
+++ b/Makefile.include
@@ -51,43 +51,3 @@
 define dependency_ldflags
 $(call debug_shell, $(if $(PKG_CONFIG_LIBDIR),PKG_CONFIG_LIBDIR=$(PKG_CONFIG_LIBDIR),) $(PKG_CONFIG) --libs $1 2>/dev/null)
 endef
-
-define LIBPCI_TEST
-/* Avoid a failing test due to libpci header symbol shadowing breakage */
-#define index shadow_workaround_index
-#if !defined PCIUTILS_PCI_H
-#include <pci/pci.h>
-#else
-#include <pciutils/pci.h>
-#endif
-struct pci_access *pacc;
-int main(int argc, char **argv)
-{
-	(void) argc;
-	(void) argv;
-	pacc = pci_alloc();
-	return 0;
-}
-endef
-export LIBPCI_TEST
-
-define PCI_GET_DEV_TEST
-/* Avoid a failing test due to libpci header symbol shadowing breakage */
-#define index shadow_workaround_index
-#if !defined PCIUTILS_PCI_H
-#include <pci/pci.h>
-#else
-#include <pciutils/pci.h>
-#endif
-struct pci_access *pacc;
-struct pci_dev *dev = {0};
-int main(int argc, char **argv)
-{
-	(void) argc;
-	(void) argv;
-	pacc = pci_alloc();
-	dev = pci_get_dev(pacc, dev->domain, dev->bus, dev->dev, 1);
-	return 0;
-}
-endef
-export PCI_GET_DEV_TEST