build: enable code coverage

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

Tested: Verified code coverage step now running.
Change-Id: Ieb3606e5ad741fb0dd181f8da594c18258efa6bc
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/.gitignore b/.gitignore
index 18c7f73..51fda67 100644
--- a/.gitignore
+++ b/.gitignore
@@ -22,6 +22,7 @@
 .dirstamp
 .libs
 /aclocal.m4
+/aminclude_static.am
 /autom4te.cache
 /build-aux
 /confdefs.h
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 b1052e8..c01f490 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,8 +1,42 @@
+# 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
+
+if AUTOCONF_CODE_COVERAGE_2019_01_06
+include $(top_srcdir)/aminclude_static.am
+clean-local: code-coverage-clean
+distclean-local: code-coverage-dist-clean
+else
+@CODE_COVERAGE_RULES@
+endif
+
 sbin_PROGRAMS = phosphor-hwmon-readd
 
 phosphor_hwmon_readd_SOURCES = readd.cpp
-phosphor_hwmon_readd_LDADD = $(SDBUSPLUS_LIBS) libhwmon.la
-phosphor_hwmon_readd_CXXFLAGS = $(SDBUSPLUS_CFLAGS)
+phosphor_hwmon_readd_LDADD = $(SDBUSPLUS_LIBS) $(CODE_COVERAGE_LIBS) libhwmon.la
+phosphor_hwmon_readd_CXXFLAGS = $(SDBUSPLUS_CFLAGS) $(CODE_COVERAGE_CXXFLAGS)
 
 noinst_LTLIBRARIES = libhwmon.la
 libhwmon_la_LDFLAGS = -static
@@ -12,13 +46,15 @@
 	$(SDEVENTPLUS_LIBS) \
 	$(PHOSPHOR_DBUS_INTERFACES_LIBS) \
 	$(PHOSPHOR_LOGGING_LIBS) \
-	$(GPIOPLUS_LIBS)
+	$(GPIOPLUS_LIBS) \
+	$(CODE_COVERAGE_LIBS)
 libhwmon_la_CXXFLAGS = \
 	$(SDBUSPLUS_CFLAGS) \
 	$(SDEVENTPLUS_CFLAGS) \
 	$(PHOSPHOR_DBUS_INTERFACES_CFLAGS) \
 	$(PHOSPHOR_LOGGING_CFLAGS) \
-	$(GPIOPLUS_CFLAGS)
+	$(GPIOPLUS_CFLAGS) \
+	$(CODE_COVERAGE_CXXFLAGS)
 
 libhwmon_la_SOURCES = \
 	argument.cpp \
diff --git a/configure.ac b/configure.ac
index 0a23f28..b203944 100644
--- a/configure.ac
+++ b/configure.ac
@@ -3,9 +3,22 @@
 AC_INIT([phosphor-hwmon], [1.0], [https://github.com/openbmc/phosphor-hwmon/issues])
 AC_LANG([C++])
 AC_CONFIG_HEADERS([config.h])
-AM_INIT_AUTOMAKE([subdir-objects -Wall -Werror foreign dist-xz])
+AM_INIT_AUTOMAKE([subdir-objects -Wall -Wno-portability -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
@@ -104,6 +117,13 @@
     )
 )
 
+# Code coverage
+AX_CODE_COVERAGE
+m4_ifdef([_AX_CODE_COVERAGE_RULES],
+    [AM_CONDITIONAL(AUTOCONF_CODE_COVERAGE_2019_01_06, [true])],
+    [AM_CONDITIONAL(AUTOCONF_CODE_COVERAGE_2019_01_06, [false])])
+AX_ADD_AM_MACRO_STATIC([])
+
 # Check for OECORE_TARGET_SYSROOT in the environment.
 AC_ARG_VAR(OECORE_TARGET_SYSROOT,
     [Path to the OE SDK SYSROOT]
diff --git a/test/Makefile.am b/test/Makefile.am
index 562de03..60d07e0 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -3,14 +3,20 @@
 	$(GTEST_CFLAGS) \
 	$(GMOCK_CFLAGS) \
 	$(SDBUSPLUS_CFLAGS) \
-	$(PHOSPHOR_DBUS_INTERFACES_CFLAGS)
+	$(PHOSPHOR_DBUS_INTERFACES_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) \
 	$(SDBUSPLUS_LIBS) \
-	$(PHOSPHOR_DBUS_INTERFACES_LIBS)
+	$(PHOSPHOR_DBUS_INTERFACES_LIBS) \
+	$(CODE_COVERAGE_LIBS)
 
 # Run all 'check' test programs
 check_PROGRAMS = hwmon_unittest fanpwm_unittest sensor_unittest