build: enable code coverage

Enable code coverage for the project.  Valgrind not enabled in this
step.

Change-Id: I439d58b23d2bc29d6f602600319074c8fd8c04d6
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/.gitignore b/.gitignore
index dde04e4..2d173c1 100644
--- a/.gitignore
+++ b/.gitignore
@@ -10,6 +10,12 @@
 /test-driver
 /ylwrap
 
+# Code Coverage
+*.gcda
+*.gcno
+*.trs
+*-coverage*
+
 # http://www.gnu.org/software/autoconf
 
 /autom4te.cache
diff --git a/.lcovrc b/.lcovrc
new file mode 100644
index 0000000..360a8b4
--- /dev/null
+++ b/.lcovrc
@@ -0,0 +1,3 @@
+geninfo_gcov_all_blocks = 1
+lcov_function_coverage = 1
+lcov_branch_coverage = 0
diff --git a/Makefile.am b/Makefile.am
index 3b57953..a5d2779 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,6 +1,32 @@
 ACLOCAL_AMFLAGS = -I m4
 AM_DEFAULT_SOURCE_EXT = .cpp
 
+# Ignore system headers
+CODE_COVERAGE_IGNORE_PATTERN = '/include/*' '/usr/include/*' '$(includedir)/*'
+export CODE_COVERAGE_IGNORE_PATTERN
+
+CODE_COVERAGE_LCOV_SHOPTS = $(CODE_COVERAGE_LCOV_SHOPTS_DEFAULT)
+# Use our configuration file for lcov
+CODE_COVERAGE_LCOV_SHOPTS += --config-file $(abs_srcdir)/.lcovrc
+export CODE_COVERAGE_LCOV_SHOPTS
+
+CODE_COVERAGE_LCOV_OPTIONS = $(CODE_COVERAGE_LCOV_OPTIONS_DEFAULT)
+# Use our configuration file for lcov
+CODE_COVERAGE_LCOV_OPTIONS += --config-file $(abs_srcdir)/.lcovrc
+export CODE_COVERAGE_LCOV_OPTIONS
+
+CODE_COVERAGE_LCOV_RMOPTS = $(CODE_COVERAGE_LCOV_RMOPTS_DEFAULT)
+# Use our configuration file for lcov
+CODE_COVERAGE_LCOV_RMOPTS += --config-file $(abs_srcdir)/.lcovrc
+export CODE_COVERAGE_LCOV_RMOPTS
+
+CODE_COVERAGE_GENHTML_OPTIONS = $(CODE_COVERAGE_GENHTML_OPTIONS_DEFAULT)
+# Use our configuration file for genhtml
+CODE_COVERAGE_GENHTML_OPTIONS += --config-file $(abs_srcdir)/.lcovrc
+# Don't generate the absolute path for each file in the HTML output
+CODE_COVERAGE_GENHTML_OPTIONS += --prefix $(abs_srcdir) --prefix $(abs_builddir)
+export CODE_COVERAGE_GENHTML_OPTIONS
+
 libblobcmdsdir = ${libdir}/ipmid-providers
 libblobcmds_LTLIBRARIES = libblobcmds.la
 libblobcmds_la_SOURCES = \
@@ -18,6 +44,7 @@
 	$(LIBADD_DLOPEN) \
 	$(PHOSPHOR_LOGGING_LIBS) \
 	$(LIBIPMID_LIBS) \
+	$(CODE_COVERAGE_LIBS) \
 	-lstdc++fs \
 	-export-dynamic \
 	-version-info 0:0:0 -shared
@@ -26,6 +53,7 @@
 	$(SYSTEMD_CFLAGS) \
 	$(PHOSPHOR_LOGGING_CFLAGS) \
 	$(LIBIPMID_CFLAGS) \
+	$(CODE_COVERAGE_CXXFLAGS) \
 	-flto
 
 nobase_include_HEADERS = \
@@ -43,9 +71,11 @@
 libblobexample_la_SOURCES = example/example.cpp
 libblobexample_la_LDFLAGS = \
 	$(PHOSPHOR_LOGGING_LIBS) \
+	$(CODE_COVERAGE_LIBS) \
 	-version-info 0:0:0 -shared
 libblobexample_la_CXXFLAGS = \
 	$(PHOSPHOR_LOGGING_CFLAGS) \
+	$(CODE_COVERAGE_CXXFLAGS) \
 	-flto
 
 SUBDIRS = . test
diff --git a/configure.ac b/configure.ac
index 73960b5..d6fbe00 100644
--- a/configure.ac
+++ b/configure.ac
@@ -7,6 +7,19 @@
 AM_INIT_AUTOMAKE([subdir-objects -Wall -Werror foreign dist-xz])
 AM_SILENT_RULES([yes])
 
+# Make sure the default CFLAGS of `-O2 -g` don't override CODE_COVERAGE_CFLAGS
+# It is important that this comes before AC_PROG_C{C,XX}, as we are attempting
+# to stop them from populating default CFLAGS and CXXFLAGS.
+AS_IF([test "x$enable_tests" = "xno"], [enable_code_coverage=no])
+AS_IF([test "x$enable_code_coverage" != "xno"], [
+    AS_IF([test "x${CXXFLAGS+set}" != "xset"], [
+        AC_SUBST(CXXFLAGS, [""])
+    ])
+    AS_IF([test "x${CFLAGS+set}" != "xset"], [
+        AC_SUBST(CFLAGS, [""])
+    ])
+])
+
 # Checks for programs.
 AC_PROG_CXX
 AM_PROG_AR
@@ -105,6 +118,13 @@
     AX_RESTORE_FLAGS_WITH_PREFIX(OLD, [LDFLAGS])
 ])
 
+# Code coverage
+AX_CODE_COVERAGE
+AM_EXTRA_RECURSIVE_TARGETS([check-code-coverage])
+AS_IF([test "x$CODE_COVERAGE_ENABLED" = "xyes"], [
+    AX_APPEND_COMPILE_FLAGS([-DHAVE_GCOV], [CODE_COVERAGE_CPPFLAGS])
+])
+
 # Add --enable-oe-sdk flag to configure script
 AC_ARG_ENABLE([oe-sdk],
     AS_HELP_STRING([--enable-oe-sdk], [Link testcases absolutely against OE SDK so they can be ran within it.])
diff --git a/test/Makefile.am b/test/Makefile.am
index 2144c85..a1dfba5 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -1,12 +1,20 @@
+@CODE_COVERAGE_RULES@
+
 AM_CPPFLAGS = \
 	-I$(top_srcdir)/ \
 	$(GTEST_CFLAGS) \
-	$(GMOCK_CFLAGS)
+	$(GMOCK_CFLAGS) \
+	$(CODE_COVERAGE_CPPFLAGS)
+AM_CFLAGS = \
+	$(CODE_COVERAGE_CFLAGS)
+AM_CXXFLAGS = \
+	$(CODE_COVERAGE_CXXFLAGS)
 AM_LDFLAGS = \
 	$(GTEST_LIBS) \
 	$(GMOCK_LIBS) \
 	-lgmock_main \
-	$(OESDK_TESTCASE_FLAGS)
+	$(OESDK_TESTCASE_FLAGS) \
+	$(CODE_COVERAGE_LIBS)
 
 # Run all 'check' test programs
 check_PROGRAMS = \