Add pkg-config support to flashmap

This adds a rule to the Makefile to generate a pkg-config .pc file
and install it if:
1) /usr/lib/pkgconfig is a directory
2) The user sets an environment variable.

The latter is useful for sandboxed environments since /usr/lib/pkgconfig
might exist in the target environment but not the sandbox where flashmap
is being built.

BUG=none
TEST=Test compiled on Goobuntu, cros_sdk (emerge), and x86-generic (emerge-x86-generic)

Change-Id: Id45ef5300b693f8258671729b624b82ea1780965
diff --git a/Makefile b/Makefile
index e6822db..9fc5c59 100644
--- a/Makefile
+++ b/Makefile
@@ -51,6 +51,8 @@
 libdir		= $(prefix)/lib
 includedir	= $(prefix)/include
 
+pkgconfig_dir	= /usr/lib/pkgconfig
+
 DEFS		= -DVERSION_MAJOR=$(VERSION_MAJOR)\
                   -DVERSION_MINOR=$(VERSION_MINOR)
 CFLAGS		+= -O2 -Wall -Werror -Wno-unused-parameter -Ilib/ $(DEFS)
@@ -131,7 +133,34 @@
 	@rm -rf $(EXPORTDIR)/$(RELEASENAME)
 	@echo Created $(EXPORTDIR)/$(RELEASENAME).tar.bz2
 
-install: all
+USE_PKG_CONFIG ?= $(shell if [ -d "$(ROOT)$(pkgconfig_dir)" ]; then echo 1; else echo 0; fi)
+
+ifeq ($(USE_PKG_CONFIG),1)
+define FMAP_PC
+prefix=$(prefix)
+exec_prefix=$(prefix)
+includedir=$(includedir)
+libdir=$(libdir)
+
+Name: fmap
+Description: Flashmap library
+Version: $(VERSION_MAJOR).$(VERSION_MINOR)
+Requires:
+Cflags: -I$(includedir)
+Libs: -L$(libdir) -lfmap
+endef
+export FMAP_PC
+
+pkgconfig_install:
+	@echo "Installing pkg-config .pc file"
+	@echo "$$FMAP_PC" > fmap.pc
+	$(INSTALL_DIR) $(DESTDIR)$(pkgconfig_dir)
+	$(INSTALL_DATA) fmap.pc $(DESTDIR)$(pkgconfig_dir)
+else
+pkgconfig_install:
+endif
+
+install: all pkgconfig_install
 	$(INSTALL_DIR) $(DESTDIR)$(sbindir)
 	$(INSTALL_DIR) $(DESTDIR)$(libdir)
 	$(INSTALL_DIR) $(DESTDIR)$(includedir)
@@ -143,7 +172,15 @@
 	$(INSTALL_DATA) $(SHARED_OBJ_FILE) $(DESTDIR)$(libdir)
 	@echo Installed shared library, please run ldconfig to set up symlinks.
 
-uninstall:
+ifeq ($(USE_PKG_CONFIG),1)
+pkgconfig_uninstall:
+	@echo "Removing pkg-config .pc file"
+	$(RM) $(DESTDIR)$(pkgconfig_dir)/fmap.pc
+else
+pkgconfig_uninstall:
+endif
+
+uninstall: pkgconfig_uninstall
 	$(RM) $(DESTDIR)$(sbindir)/fmap_decode
 	$(RM) $(DESTDIR)$(sbindir)/fmap_encode
 	$(RM) $(DESTDIR)$(sbindir)/fmap_csum