Makefile: Make pkg-config mandatory to find libjaylink

Use `make HAS_LIBJAYLINK=yes/no` to override the pkg-config detection
and `CONFIG_LIBJAYLINK_CFLAGS` and `CONFIG_LIBJAYLINK_LDFLAGS` to set
cflags and ldflags manually.

Change-Id: I99df547046bb9820ab502f89f6d4452c1bc0cfd4
Signed-off-by: Thomas Heijligen <thomas.heijligen@secunet.com>
Original-Reviewed-on: https://review.coreboot.org/c/flashrom/+/59048
Original-Reviewed-by: Nico Huber <nico.h@gmx.de>
Reviewed-on: https://review.coreboot.org/c/flashrom-stable/+/72262
Reviewed-by: Nico Huber <nico.h@gmx.de>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
diff --git a/Makefile b/Makefile
index 8587dec..a789142 100644
--- a/Makefile
+++ b/Makefile
@@ -188,6 +188,9 @@
 CONFIG_LIB_NI845X_CFLAGS   := -I$(CONFIG_NI845X_LIBRARY_PATH) $(if NI854_X86_LIBRARY_PATH, -I${NI854_X86_LIBRARY_PATH})
 CONFIG_LIB_NI845X_LDFLAGS  := -L$(CONFIG_NI845X_LIBRARY_PATH) $(if NI854_X86_LIBRARY_PATH, -L${NI854_X86_LIBRARY_PATH}) -lni845x
 
+CONFIG_LIBJAYLINK_CFLAGS   := $(call dependency_cflags, libjaylink)
+CONFIG_LIBJAYLINK_LDFLAGS  := $(call dependency_ldflags, libjaylink)
+
 # 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
@@ -199,6 +202,7 @@
 
 HAS_LIBFTDI1       := $(call find_dependency, libftdi1)
 HAS_LIB_NI845X     := no
+HAS_LIBJAYLINK     := $(call find_dependency, libjaylink)
 
 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)
@@ -232,7 +236,7 @@
 # Missing serial support.
 $(call mark_unsupported,$(DEPENDS_ON_SERIAL))
 # Libraries not available for DOS
-$(call mark_unsupported,$(DEPENDS_ON_LIBUSB1) $(DEPENDS_ON_LIBJAYLINK))
+$(call mark_unsupported,$(DEPENDS_ON_LIBUSB1))
 endif
 
 ifeq ($(TARGET_OS), $(filter $(TARGET_OS), MinGW Cygwin))
@@ -268,7 +272,7 @@
 # Bus Pirate, Serprog and PonyProg are not supported with libpayload (missing serial support).
 $(call mark_unsupported,CONFIG_BUSPIRATE_SPI CONFIG_SERPROG CONFIG_PONY_SPI)
 # Dediprog, Developerbox, USB-Blaster, PICkit2, CH341A and FT2232 are not supported with libpayload (missing libusb support).
-$(call mark_unsupported,$(DEPENDS_ON_LIBUSB1) $(DEPENDS_ON_LIBJAYLINK))
+$(call mark_unsupported,$(DEPENDS_ON_LIBUSB1))
 endif
 
 ifeq ($(HAS_LINUX_MTD), no)
@@ -301,6 +305,10 @@
 $(call mark_unsupported,$(DEPENDS_ON_LIB_NI845X))
 endif
 
+ifeq ($(HAS_LIBJAYLINK), no)
+$(call mark_unsupported,$(DEPENDS_ON_LIBJAYLINK))
+endif
+
 ifeq ($(ENDIAN), little)
 FEATURE_CFLAGS += -D'__FLASHROM_LITTLE_ENDIAN__=1'
 endif
@@ -825,11 +833,10 @@
 override LDFLAGS += $(CONFIG_LIB_NI845X_LDFLAGS)
 endif
 
-NEED_LIBJAYLINK := $(call filter_deps,$(DEPENDS_ON_LIBJAYLINK))
-ifneq ($(NEED_LIBJAYLINK), )
-CHECK_LIBJAYLINK = yes
-JAYLINKLIBS += $(call debug_shell,[ -n "$(PKG_CONFIG_LIBDIR)" ] && export PKG_CONFIG_LIBDIR="$(PKG_CONFIG_LIBDIR)"; $(PKG_CONFIG) --libs libjaylink)
-override CPPFLAGS += $(call debug_shell,[ -n "$(PKG_CONFIG_LIBDIR)" ] && export PKG_CONFIG_LIBDIR="$(PKG_CONFIG_LIBDIR)"; $(PKG_CONFIG) --cflags-only-I libjaylink)
+USE_LIBJAYLINK := $(if $(call filter_deps,$(DEPENDS_ON_LIBJAYLINK)),yes,no)
+ifeq ($(USE_LIBJAYLINK), yes)
+override CFLAGS  += $(CONFIG_LIBJAYLINK_CFLAGS)
+override LDFLAGS += $(CONFIG_LIBJAYLINK_LDFLAGS)
 endif
 
 ifeq ($(CONFIG_PRINT_WIKI), yes)
@@ -858,7 +865,7 @@
 endif
 
 $(PROGRAM)$(EXEC_SUFFIX): $(OBJS)
-	$(CC) -o $(PROGRAM)$(EXEC_SUFFIX) $(OBJS) $(LDFLAGS) $(LIBS) $(PCILIBS) $(FEATURE_LIBS) $(USBLIBS) $(USB1LIBS) $(JAYLINKLIBS)
+	$(CC) -o $(PROGRAM)$(EXEC_SUFFIX) $(OBJS) $(LDFLAGS) $(LIBS) $(PCILIBS) $(FEATURE_LIBS) $(USBLIBS) $(USB1LIBS)
 
 libflashrom.a: $(LIBFLASHROM_OBJS)
 	$(AR) rcs $@ $^
@@ -959,28 +966,11 @@
 		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
-ifeq ($(CHECK_LIBJAYLINK), yes)
-	@printf "Checking for libjaylink headers... " | tee -a $(BUILD_DETAILS_FILE)
-	@echo "$$LIBJAYLINK_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 feature requires libjaylink: $(NEED_LIBJAYLINK).";	\
-		echo "Please install libjaylink headers or disable the feature"; \
-		echo "mentioned above by specifying make CONFIG_JLINK_SPI=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 if libjaylink is usable... " | tee -a $(BUILD_DETAILS_FILE)
-	@printf "\nexec: %s\n" "$(CC) $(LDFLAGS) .test.o -o .test$(EXEC_SUFFIX) $(LIBS) $(JAYLINKLIBS)" >>$(BUILD_DETAILS_FILE)
-	@{ { { { { $(CC) $(LDFLAGS) .test.o -o .test$(EXEC_SUFFIX) $(LIBS) $(JAYLINKLIBS) >&2 && \
-		echo "yes." || { echo "no.";						\
-		echo "The following feature requires libjaylink: $(NEED_LIBJAYLINK).";	\
-		echo "Please install libjaylink or disable the feature"; \
-		echo "mentioned above by specifying make CONFIG_JLINK_SPI=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 libjaylink found: $(HAS_LIBJAYLINK)
+	@if [ $(HAS_LIBJAYLINK) = yes ]; then			\
+		echo "  CFLAGS: $(CONFIG_LIBJAYLINK_CFLAGS)";	\
+		echo "  LDFLAGS: $(CONFIG_LIBJAYLINK_LDFLAGS)";	\
+	fi
 	@echo Dependency NI-845x found: $(HAS_LIB_NI845X)
 	@if [ $(HAS_LIB_NI845X) = yes ]; then			\
 		echo "  CFLAGS: $(CONFIG_LIB_NI845X_CFLAGS)";	\