Makefile: Rework NI-845x detection

Since the NI-845x is a Windows only proprietary library, disable it by
default. Use `HAS_LIB_NI845X=yes` to enable it. The default search path
is `${PROGRAMFILES}\National Instruments\NI-845x\MS Visual C` and can be
overwritten by `CONFIG_NI845X_LIBRARY_PATH`. Use
`CONFIG_LIB_NI845X_CFLAGS` and `CONFIG_LIB_NI845X_LDFLAGS` for setting
the cflags and ld flags manually.

flashrom-stable:
s/PROGRAMFILES_X86DIR/PROGRAMFILES_X86/ where it was missed

Change-Id: I918c3605a5ac168708a6a10fd92ee2a1aae9729b
Signed-off-by: Thomas Heijligen <thomas.heijligen@secunet.com>
Original-Reviewed-on: https://review.coreboot.org/c/flashrom/+/59047
Original-Reviewed-by: Nico Huber <nico.h@gmx.de>
Reviewed-on: https://review.coreboot.org/c/flashrom-stable/+/72261
Reviewed-by: Nico Huber <nico.h@gmx.de>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
diff --git a/Makefile b/Makefile
index cb77c95..8587dec 100644
--- a/Makefile
+++ b/Makefile
@@ -143,6 +143,9 @@
 DEPENDS_ON_LIBJAYLINK := \
 	CONFIG_JLINK_SPI \
 
+DEPENDS_ON_LIB_NI845X := \
+	CONFIG_NI845X_SPI \
+
 
 ifeq ($(CONFIG_ENABLE_LIBUSB1_PROGRAMMERS), no)
 $(call disable_all,$(DEPENDS_ON_LIBUSB1))
@@ -168,8 +171,22 @@
 CC_WORKING := $(call c_compile_test, Makefile.d/cc_test.c)
 
 # Configs for dependencies. Can be overwritten by commandline
-CONFIG_LIBFTDI1_CFLAGS  := $(call dependency_cflags, libftdi1)
-CONFIG_LIBFTDI1_LDFLAGS := $(call dependency_ldflags, libftdi1)
+CONFIG_LIBFTDI1_CFLAGS     := $(call dependency_cflags, libftdi1)
+CONFIG_LIBFTDI1_LDFLAGS    := $(call dependency_ldflags, libftdi1)
+
+# Hack to keep legacy auto detection of Program Files (x86), Only active if none of the CONFIG_ variables for ni845x are set.
+ifeq ($(CONFIG_NI845X_LIBRARY_PATH)$(CONFIG_LIB_NI845X_CFLAGS)$(CONFIG_LIB_NI845X_LDFLAGS),)
+PROGRAMFILES_X86 = $(shell env | sed -n "s/^PROGRAMFILES(X86)=//p")
+ifneq ($(PROGRAMFILES_X86),)
+ifneq ($(PROGRAMFILES_X86), ${PROGRAMFILES})
+NI854_X86_LIBRARY_PATH := '${PROGRAMFILES_X86}\National Instruments\NI-845x\MS Visual C'
+endif
+endif
+endif
+
+CONFIG_NI845X_LIBRARY_PATH := '${PROGRAMFILES}\National Instruments\NI-845x\MS Visual C'
+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
 
 # Determine the destination OS, architecture and endian
 # IMPORTANT: The following lines must be placed before TARGET_OS, ARCH or ENDIAN
@@ -181,6 +198,7 @@
 
 
 HAS_LIBFTDI1       := $(call find_dependency, libftdi1)
+HAS_LIB_NI845X     := no
 
 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)
@@ -236,12 +254,6 @@
 $(call mark_unsupported,$(DEPENDS_ON_LIBPCI))
 # And programmers that need raw access.
 $(call mark_unsupported,CONFIG_RAYER_SPI)
-
-else # No MinGW
-
-# NI USB-845x only supported on Windows at the moment
-$(call mark_unsupported,CONFIG_NI845X_SPI)
-
 endif
 
 ifeq ($(TARGET_OS), libpayload)
@@ -285,6 +297,10 @@
 $(call mark_unsupported,$(DEPENDS_ON_LIBFTDI1))
 endif
 
+ifeq ($(HAS_LIB_NI845X), no)
+$(call mark_unsupported,$(DEPENDS_ON_LIB_NI845X))
+endif
+
 ifeq ($(ENDIAN), little)
 FEATURE_CFLAGS += -D'__FLASHROM_LITTLE_ENDIAN__=1'
 endif
@@ -715,30 +731,6 @@
 
 ifeq ($(CONFIG_NI845X_SPI), yes)
 FEATURE_CFLAGS += -D'CONFIG_NI845X_SPI=1'
-
-ifeq ($(CONFIG_NI845X_LIBRARY_PATH),)
-# if the user did not specified the NI-845x headers/lib path
-# do a guess for both 32 and 64 bit Windows versions
-NI845X_LIBS += -L'${PROGRAMFILES}\National Instruments\NI-845x\MS Visual C'
-NI845X_INCLUDES += -I'${PROGRAMFILES}\National Instruments\NI-845x\MS Visual C'
-
-# hack to access env variable containing brackets...
-PROGRAMFILES_X86DIR = $(shell env | sed -n "s/^PROGRAMFILES(X86)=//p")
-
-ifneq ($(PROGRAMFILES_X86DIR),)
-ifneq ($(PROGRAMFILES_X86DIR), ${PROGRAMFILES})
-NI845X_LIBS += -L'$(PROGRAMFILES_X86DIR)\National Instruments\NI-845x\MS Visual C'
-NI845X_INCLUDES += -I'$(PROGRAMFILES_X86DIR)\National Instruments\NI-845x\MS Visual C'
-endif
-endif
-
-else
-NI845X_LIBS += -L'$(CONFIG_NI845X_LIBRARY_PATH)'
-NI845X_INCLUDES += -I'$(CONFIG_NI845X_LIBRARY_PATH)'
-endif
-
-FEATURE_CFLAGS += $(NI845X_INCLUDES)
-LIBS += -lni845x
 PROGRAMMER_OBJS += ni845x_spi.o
 endif
 
@@ -827,6 +819,12 @@
 endif
 endif
 
+USE_LIB_NI845X := $(if $(call filter_deps,$(DEPENDS_ON_LIB_NI845X)),yes,no)
+ifeq ($(USE_LIB_NI845X), yes)
+override CFLAGS += $(CONFIG_LIB_NI845X_CFLAGS)
+override LDFLAGS += $(CONFIG_LIB_NI845X_LDFLAGS)
+endif
+
 NEED_LIBJAYLINK := $(call filter_deps,$(DEPENDS_ON_LIBJAYLINK))
 ifneq ($(NEED_LIBJAYLINK), )
 CHECK_LIBJAYLINK = yes
@@ -860,7 +858,7 @@
 endif
 
 $(PROGRAM)$(EXEC_SUFFIX): $(OBJS)
-	$(CC) -o $(PROGRAM)$(EXEC_SUFFIX) $(OBJS) $(LDFLAGS) $(LIBS) $(PCILIBS) $(FEATURE_LIBS) $(USBLIBS) $(USB1LIBS) $(JAYLINKLIBS) $(NI845X_LIBS)
+	$(CC) -o $(PROGRAM)$(EXEC_SUFFIX) $(OBJS) $(LDFLAGS) $(LIBS) $(PCILIBS) $(FEATURE_LIBS) $(USBLIBS) $(USB1LIBS) $(JAYLINKLIBS)
 
 libflashrom.a: $(LIBFLASHROM_OBJS)
 	$(AR) rcs $@ $^
@@ -983,21 +981,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 ($(CONFIG_NI845X_SPI), yes)
-	@printf "Checking for NI USB-845x installation... " | tee -a $(BUILD_DETAILS_FILE)
-	@echo "$$NI845X_TEST" > .test.c
-	@printf "\nexec: %s\n" "$(CC) $(CPPFLAGS) $(CFLAGS) $(NI845X_INCLUDES) $(LDFLAGS) .test.c -o .test$(EXEC_SUFFIX) $(NI845X_LIBS) $(LIBS)" >>$(BUILD_DETAILS_FILE)
-	@ { { { { { $(CC) $(CPPFLAGS) $(CFLAGS) $(NI845X_INCLUDES) $(LDFLAGS) .test.c -o .test$(EXEC_SUFFIX) $(NI845X_LIBS) $(LIBS) >&2 && \
-		echo "yes." || { echo "no"; 				\
-		echo "Unable to find NI-845x headers or libraries.";	\
-		echo "Please pass the NI-845x library path to the make with the CONFIG_NI845X_LIBRARY_PATH parameter,"; \
-		echo "or disable the NI-845x support by specifying make CONFIG_NI845X_SPI=no"; \
-		echo "For the NI-845x 17.0 the library path is:"; \
-		echo " On 32 bit systems: C:\Program Files)\National Instruments\NI-845x\MS Visual C"; \
-		echo " On 64 bit systems: C:\Program Files (x86)\National Instruments\NI-845x\MS Visual C"; 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
-	@rm -f .test.c .test.o .test$(EXEC_SUFFIX)
-endif
+	@echo Dependency NI-845x found: $(HAS_LIB_NI845X)
+	@if [ $(HAS_LIB_NI845X) = yes ]; then			\
+		echo "  CFLAGS: $(CONFIG_LIB_NI845X_CFLAGS)";	\
+		echo "  LDFLAGS: $(CONFIG_LIB_NI845X_LDFLAGS)";	\
+	fi
 	@echo Dependency libftdi1 found: $(HAS_LIBFTDI1)
 	@if [ $(HAS_LIBFTDI1) = yes ]; then 			\
 		echo "  Checking for \"TYPE_232H\" in \"enum ftdi_chip_type\": $(HAS_FT232H)"; \