build: enable unit-tests

Enable unit-tests in the configure and makefile.

Tested: Compiled.
Change-Id: I8a573bb97c4752254797104c75660ecd372fb120
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/Makefile.am b/Makefile.am
index ac68e5e..218ea38 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -12,3 +12,5 @@
 libethstatscmd_la_CXXFLAGS = \
 	$(LIBIPMID_CFLAGS) \
 	-flto
+
+SUBDIRS = . test
diff --git a/configure.ac b/configure.ac
index 35bb02c..0aa9a59 100644
--- a/configure.ac
+++ b/configure.ac
@@ -23,6 +23,75 @@
 # Checks for library functions.
 LT_INIT # Required for systemd linking
 
+# Make it possible for users to choose if they want test support
+# explicitly or not at all
+AC_ARG_ENABLE([tests], AC_HELP_STRING([--disable-tests],
+                                      [Build test cases]))
+
+ # Check/set gtest specific functions.
+AS_IF([test "x$enable_tests" != "xno"], [
+    PKG_CHECK_MODULES([GTEST], [gtest], [], [true])
+    PKG_CHECK_MODULES([GMOCK], [gmock], [], [true])
+    AX_PTHREAD
+
+    AX_SAVE_FLAGS_WITH_PREFIX(OLD, [CPPFLAGS])
+    AX_APPEND_COMPILE_FLAGS([$GTEST_CFLAGS], [CPPFLAGS])
+    AC_LANG_PUSH([C++])
+    AC_CHECK_HEADERS([gtest/gtest.h], [
+        AS_IF([test "x$GTEST_CFLAGS" = "x"], [
+            AS_IF([test "x$PTHREAD_CFLAGS" = "x"], [
+                AX_APPEND_COMPILE_FLAGS(["-DGTEST_HAS_PTHREAD=0"], [GTEST_CFLAGS])
+            ], [
+                AX_APPEND_COMPILE_FLAGS(["-DGTEST_HAS_PTHREAD=1"], [GTEST_CFLAGS])
+                AX_APPEND_COMPILE_FLAGS([$PTHREAD_CFLAGS], [GTEST_CFLAGS])
+            ])
+        ])
+    ], [
+        AS_IF([test "x$enable_tests" = "xyes"], [
+            AC_MSG_ERROR([Testing enabled but could not find gtest/gtest.h])
+        ])
+    ])
+    AC_LANG_POP([C++])
+    AX_RESTORE_FLAGS_WITH_PREFIX(OLD, [CPPFLAGS])
+
+    AX_SAVE_FLAGS_WITH_PREFIX(OLD, [CPPFLAGS])
+    AX_APPEND_COMPILE_FLAGS([$GMOCK_CFLAGS], [CPPFLAGS])
+    AC_LANG_PUSH([C++])
+    AC_CHECK_HEADERS([gmock/gmock.h], [], [
+        AS_IF([test "x$enable_tests" = "xyes"], [
+            AC_MSG_ERROR([Testing enabled but could not find gmock/gmock.h])
+        ])
+    ])
+    AC_LANG_POP([C++])
+    AX_RESTORE_FLAGS_WITH_PREFIX(OLD, [CPPFLAGS])
+
+    AX_SAVE_FLAGS_WITH_PREFIX(OLD, [LDFLAGS])
+    AX_APPEND_COMPILE_FLAGS([$GTEST_LIBS], [LDFLAGS])
+    AC_CHECK_LIB([gtest], [main], [
+        AS_IF([test "x$GTEST_LIBS" = "x"], [
+            AX_APPEND_COMPILE_FLAGS([-lgtest], [GTEST_LIBS])
+        ])
+    ], [
+        AS_IF([test "x$enable_tests" = "xyes"], [
+            AC_MSG_ERROR([Testing enabled but couldn't find gtest libs])
+        ])
+    ])
+    AX_RESTORE_FLAGS_WITH_PREFIX(OLD, [LDFLAGS])
+
+    AX_SAVE_FLAGS_WITH_PREFIX(OLD, [LDFLAGS])
+    AX_APPEND_COMPILE_FLAGS([$GMOCK_LIBS], [LDFLAGS])
+    AC_CHECK_LIB([gmock], [main], [
+        AS_IF([test "x$GMOCK_LIBS" = "x"], [
+            AX_APPEND_COMPILE_FLAGS([-lgmock], [GMOCK_LIBS])
+        ])
+    ], [
+        AS_IF([test "x$enable_tests" = "xyes"], [
+            AC_MSG_ERROR([Testing enabled but couldn't find gmock libs])
+        ])
+    ])
+    AX_RESTORE_FLAGS_WITH_PREFIX(OLD, [LDFLAGS])
+])
+
 # Do you want to install with the Google OEM Number as well.
 AC_ARG_ENABLE([google],
     AS_HELP_STRING(
@@ -36,5 +105,5 @@
 )
 
 # Create configured output
-AC_CONFIG_FILES([Makefile])
+AC_CONFIG_FILES([Makefile test/Makefile])
 AC_OUTPUT
diff --git a/test/Makefile.am b/test/Makefile.am
new file mode 100644
index 0000000..194e582
--- /dev/null
+++ b/test/Makefile.am
@@ -0,0 +1,12 @@
+AM_CPPFLAGS = \
+	-I$(top_srcdir)/ \
+	$(GTEST_CFLAGS) \
+	$(GMOCK_CFLAGS)
+AM_LDFLAGS = \
+	$(GTEST_LIBS) \
+	$(GMOCK_LIBS) \
+	-lgmock_main \
+	$(OESDK_TESTCASE_FLAGS)
+
+check_PROGRAMS =
+TESTS = $(check_PROGRAMS)