respect pkg-config env settings

Rather than use the host's pkg-config, we want to use the target's.
This way we query the right .pc files.

BUG=None
TEST=`emerge vboot_reference` still works
TEST=`emerge-arm-generic vboot_reference` still works

Change-Id: I083a987ee6c23716f8d79eb14e7c38c12e18b8f8
Reviewed-on: https://gerrit.chromium.org/gerrit/24727
Reviewed-by: Bill Richardson <wfrichar@chromium.org>
Commit-Ready: Mike Frysinger <vapier@chromium.org>
Tested-by: Mike Frysinger <vapier@chromium.org>
diff --git a/Makefile b/Makefile
index dc39b95..9bca70e 100644
--- a/Makefile
+++ b/Makefile
@@ -52,6 +52,11 @@
 CC ?= gcc
 CXX ?= g++
 
+PKG_CONFIG ?= pkg-config
+ifneq ($(filter-out -static,$(LDFLAGS)),$(LDFLAGS))
+PKG_CONFIG += --static
+endif
+
 # Fix compiling directly on host (outside of emake)
 ifeq ($(ARCH),)
 export ARCH=amd64
@@ -69,7 +74,7 @@
 CFLAGS += -DNDEBUG
 endif
 
-export CC CXX CFLAGS
+export CC CXX PKG_CONFIG CFLAGS
 
 export TOP = $(shell pwd)
 export FWDIR=$(TOP)/firmware
@@ -102,7 +107,7 @@
 	done; \
 	[ -z "$(FIRMWARE_ARCH)" ] && $(MAKE) -C utility update_tlcl_structures; \
 	for i in $(SUBDIRS); do \
-		make -C $$i; \
+		$(MAKE) -C $$i; \
 	done
 
 libcgpt_cc:
diff --git a/utility/Makefile b/utility/Makefile
index 7f1e15e..b99126b 100644
--- a/utility/Makefile
+++ b/utility/Makefile
@@ -12,6 +12,7 @@
 CFLAGS += -MMD -MF $@.d
 LIBS = $(HOSTLIB)
 HOSTCC = cc
+CRYPTO_LIBS := $(shell $(PKG_CONFIG) --libs libcrypto)
 
 BUILD_ROOT = ${BUILD}/utility
 
@@ -54,11 +55,11 @@
 	$(CC) $(CFLAGS) $< -o $@ $(LIBS)
 
 ${BUILD_ROOT}/dumpRSAPublicKey: dumpRSAPublicKey.c
-	$(CC) $(CFLAGS) $< -o $@ -lcrypto
+	$(CC) $(CFLAGS) $< -o $@ $(CRYPTO_LIBS)
 
 ${BUILD_ROOT}/dump_kernel_config: dump_kernel_config_main.c \
                                   $(DUMPKERNELCONFIGLIB)
-	$(CC) $(CFLAGS) $< -o $@ $(LIBS) $(DUMPKERNELCONFIGLIB) -lcrypto
+	$(CC) $(CFLAGS) $< -o $@ $(LIBS) $(DUMPKERNELCONFIGLIB) $(CRYPTO_LIBS)
 
 ${BUILD_ROOT}/dump_kernel_config.o: dump_kernel_config.c
 	$(CC) $(CFLAGS) -c $< -o $@
@@ -107,40 +108,40 @@
 
 # TODO: rewrite load_firmware_test to support new wrapper API
 #${BUILD_ROOT}/load_firmware_test: load_firmware_test.c $(LIBS)
-#	$(CC) $(CFLAGS) $< -o $@ $(LIBS) -lcrypto
+#	$(CC) $(CFLAGS) $< -o $@ $(LIBS) $(CRYPTO_LIBS)
 
 ${BUILD_ROOT}/load_kernel_test: load_kernel_test.c $(LIBS)
-	$(CC) $(CFLAGS) $< -o $@ $(LIBS) -lcrypto
+	$(CC) $(CFLAGS) $< -o $@ $(LIBS) $(CRYPTO_LIBS)
 
 ${BUILD_ROOT}/pad_digest_utility: pad_digest_utility.c $(LIBS)
-	$(CC) $(CFLAGS) $< -o $@ $(LIBS) -lcrypto
+	$(CC) $(CFLAGS) $< -o $@ $(LIBS) $(CRYPTO_LIBS)
 
 ${BUILD_ROOT}/signature_digest_utility: signature_digest_utility.c $(LIBS)
-	$(CC) $(CFLAGS) $< -o $@ $(LIBS) -lcrypto
+	$(CC) $(CFLAGS) $< -o $@ $(LIBS) $(CRYPTO_LIBS)
 
 ${BUILD_ROOT}/tlcl_generator: tlcl_generator.c
 	$(HOSTCC) $(CFLAGS) -fpack-struct $< -o $@
 
 ${BUILD_ROOT}/vbutil_ec: vbutil_ec.c $(LIBS)
-	$(CC) $(CFLAGS) $< -o $@ $(LIBS) -lcrypto
+	$(CC) $(CFLAGS) $< -o $@ $(LIBS) $(CRYPTO_LIBS)
 
 ${BUILD_ROOT}/vbutil_firmware: vbutil_firmware.c $(LIBS)
-	$(CC) $(CFLAGS) $< -o $@ $(LIBS) -lcrypto
+	$(CC) $(CFLAGS) $< -o $@ $(LIBS) $(CRYPTO_LIBS)
 
 ${BUILD_ROOT}/vbutil_kernel: vbutil_kernel.c $(LIBS)
-	$(CC) $(CFLAGS) $< -o $@ $(LIBS) -lcrypto
+	$(CC) $(CFLAGS) $< -o $@ $(LIBS) $(CRYPTO_LIBS)
 
 ${BUILD_ROOT}/vbutil_key: vbutil_key.c $(LIBS)
-	$(CC) $(CFLAGS) $< -o $@ $(LIBS) -lcrypto
+	$(CC) $(CFLAGS) $< -o $@ $(LIBS) $(CRYPTO_LIBS)
 
 ${BUILD_ROOT}/vbutil_keyblock: vbutil_keyblock.c $(LIBS)
-	$(CC) $(CFLAGS) $< -o $@ $(LIBS) -lcrypto
+	$(CC) $(CFLAGS) $< -o $@ $(LIBS) $(CRYPTO_LIBS)
 
 ${BUILD_ROOT}/verify_data: verify_data.c $(LIBS)
-	$(CC) $(CFLAGS) $< -o $@ $(LIBS) -lcrypto
+	$(CC) $(CFLAGS) $< -o $@ $(LIBS) $(CRYPTO_LIBS)
 
 ${BUILD_ROOT}/sign_image: sign_image.c $(LIBS)
-	$(CC) $(CFLAGS) $< -o $@ $(LIBS) -lcrypto
+	$(CC) $(CFLAGS) $< -o $@ $(LIBS) $(CRYPTO_LIBS)
 
 ${BUILD_ROOT}/tpm_init_temp_fix: tpm_init_temp_fix.c $(LIBS)
 	$(CC) $(CFLAGS) $< -o $@ $(LIBS)
@@ -165,7 +166,7 @@
 
 ${BUILD_ROOT}/mount-helpers.o: mount-helpers.c mount-helpers.h mount-encrypted.h
 	$(CC) -Wall -Werror -O2 -D_FORTIFY_SOURCE=2 -fstack-protector \
-		$(shell pkg-config --cflags glib-2.0 openssl) \
+		$(shell $(PKG_CONFIG) --cflags glib-2.0 openssl) \
 		-c $< -o $@
 
 # The embedded libcrypto includes conflict with the shipped openssl,
@@ -173,14 +174,14 @@
 ${BUILD_ROOT}/mount-encrypted: mount-encrypted.c mount-encrypted.h \
 				${BUILD_ROOT}/mount-helpers.o $(LIBS)
 	$(CC) -Wall -Werror -O2 -D_FORTIFY_SOURCE=2 -fstack-protector \
-		$(shell pkg-config --cflags glib-2.0 openssl) \
+		$(shell $(PKG_CONFIG) --cflags glib-2.0 openssl) \
 		-I$(FWDIR)/include \
 		-I$(HOSTDIR)/include \
-		$< -o $@ $(shell pkg-config --libs glib-2.0 openssl) \
+		$< -o $@ $(shell $(PKG_CONFIG) --libs glib-2.0 openssl) \
 		${BUILD_ROOT}/mount-helpers.o $(LIBS)
 
 ${BUILD_ROOT}/dev_sign_file: dev_sign_file.c $(LIBS)
-	$(CC) $(CFLAGS) $< -o $@ $(LIBS) -lcrypto
+	$(CC) $(CFLAGS) $< -o $@ $(LIBS) $(CRYPTO_LIBS)
 
 ${BUILD_ROOT}/dump_fmap: dump_fmap.c $(LIBS)
 	$(CC) $(CFLAGS) $< -o $@ $(LIBS)