Build openpower-libhei as a static library

Build a static library that can be linked to by other applications.

Signed-off-by: Ben Tyner <ben.tyner@ibm.com>
Change-Id: I4dec91928381712674e6621792830f811c44c30d
diff --git a/meson.build b/meson.build
index 68a303a..5359e97 100644
--- a/meson.build
+++ b/meson.build
@@ -7,10 +7,8 @@
             'cpp_args=-Wno-unused-parameter'
        ])
 
-incdir = [include_directories('src')]
+incdir = include_directories('src')
 
-# libhei_dep provides dependencies needed to build with libhei as a subproject
-# rather than a base project.
 libhei_src = ['src/isolator/hei_isolator.cpp',
               'src/isolator/hei_isolation_node.cpp',
               'src/register/hei_hardware_register.cpp',
@@ -19,9 +17,48 @@
 libhei_dep = declare_dependency(include_directories : incdir,
                                 sources : libhei_src)
 
+# build static library libhei.a (note that the libray name is hei, the
+# resulting filename will be libhei.a)
+libhei_static = static_library('hei',
+                                dependencies: libhei_dep,
+                                install: true)
+
+install_headers('src/hei_chip.hpp',
+                'src/hei_includes.hpp',
+                'src/hei_isolation_data.hpp',
+                'src/hei_macros.hpp',
+                'src/hei_main.hpp',
+                'src/hei_return_code.hpp',
+                'src/hei_types.hpp',
+                'src/hei_user_interface.hpp',
+                subdir : 'libhei')
+
+install_headers('src/isolator/hei_signature.hpp',
+                'src/isolator/hei_isolator.hpp',
+                'src/isolator/hei_isolation_node.hpp',
+                subdir : 'libhei/isolator')
+
+install_headers('src/register/hei_hardware_register.hpp',
+                'src/register/hei_operator_register.hpp',
+                'src/register/hei_scom_register.hpp',
+                'src/register/hei_register.hpp',
+                subdir : 'libhei/register')
+
+install_headers('src/util/hei_bit_string.hpp',
+                'src/util/hei_flyweight.hpp',
+                subdir : 'libhei/util')
+
+pkg_mod = import('pkgconfig')
+
+pkg_mod.generate(libraries : libhei_static,
+                 version : '0.1',
+                 name : 'libhei',
+                 subdirs: 'libhei',
+                 filebase : 'hei',
+                 description : 'Openpower Hardware Error Isolator')
+
 build_tests = get_option('tests')
 
 if not build_tests.disabled()
-  incdir += include_directories('test')
   subdir('test')
 endif
diff --git a/src/chip_data/hei_chip_data_stream.hpp b/src/chip_data/hei_chip_data_stream.hpp
index c9cbde7..e9cfc75 100644
--- a/src/chip_data/hei_chip_data_stream.hpp
+++ b/src/chip_data/hei_chip_data_stream.hpp
@@ -4,6 +4,7 @@
 #include <string.h>
 
 #include <hei_includes.hpp>
+#include <hei_macros.hpp>
 
 namespace libhei
 {
diff --git a/src/hei_includes.hpp b/src/hei_includes.hpp
index 2f225f1..bf771e5 100644
--- a/src/hei_includes.hpp
+++ b/src/hei_includes.hpp
@@ -23,7 +23,6 @@
 //  Assertion (at a minimum should work like assert() from <cassert>):
 //      HEI_ASSERT(expression)
 //
-#include <hei_user_defines.hpp>
 
 // Internal includes
 #include <hei_chip.hpp>
diff --git a/src/hei_macros.hpp b/src/hei_macros.hpp
new file mode 100644
index 0000000..cada4d7
--- /dev/null
+++ b/src/hei_macros.hpp
@@ -0,0 +1,20 @@
+#pragma once
+
+#include <assert.h>
+#include <inttypes.h> // for PRIu64
+
+#include <hei_user_interface.hpp>
+
+/** @brief Common defines used throughout this library */
+
+#define HEI_INF(...)                                                           \
+    {                                                                          \
+        libhei::hei_inf((char*)__VA_ARGS__);                                   \
+    }
+
+#define HEI_ERR(...)                                                           \
+    {                                                                          \
+        libhei::hei_err((char*)__VA_ARGS__);                                   \
+    }
+
+#define HEI_ASSERT(expression) assert(expression);
diff --git a/src/hei_user_interface.hpp b/src/hei_user_interface.hpp
index 9849ca1..9fa908d 100644
--- a/src/hei_user_interface.hpp
+++ b/src/hei_user_interface.hpp
@@ -6,6 +6,41 @@
  * The method for actions like hardware register access will vary per user
  * application. Therefore, the user application must define all of the APIs
  * listed below.
+ *
+ *  1.  ReturnCode libhei::registerRead(const Chip& i_chip, void* o_buffer,
+ *                      size_t& io_bufSize, uint64_t i_regType,
+ *                      uint64_t i_address);
+ *
+ *  2.  ReturnCode libhei::registerWrite(const Chip& i_chip, void* i_buffer,
+ *                            size_t& io_bufSize, uint64_t i_regType,
+ *                            uint64_t i_address);
+ *
+ *  3. void libhei::hei_inf(...)
+ *  4. void libhei::hei_err(...)
+ *
+ *  Example user application implementation of hei_inf(...) and hei_err(...)
+ *
+ *  void hei_inf(char* format, ...)
+ *  {
+ *      va_list args;
+ *
+ *      printf("I> ");
+ *      va_start(args, format);
+ *      vprintf(format, args);
+ *      va_end(args);
+ *      printf("\n");
+ *  }
+ *
+ *  void hei_err(char* format, ...)
+ *  {
+ *      va_list args;
+ *
+ *      printf("E> ");
+ *      va_start(args, format);
+ *      vprintf(format, args);
+ *      va_end(args);
+ *      printf("\n");
+ *  }
  */
 
 #include <hei_includes.hpp>
@@ -77,6 +112,12 @@
 ReturnCode registerWrite(const Chip& i_chip, void* i_buffer, size_t& io_bufSize,
                          uint64_t i_regType, uint64_t i_address);
 
+// used by HEI_INF macro in this library
+void hei_inf(char* format, ...); // implemented in user application
+
+// used by HEI_ERR macro in this library
+void hei_err(char* format, ...); // implemented in user application
+
 #endif
 
 } // end namespace libhei
diff --git a/src/isolator/hei_isolation_node.cpp b/src/isolator/hei_isolation_node.cpp
index e2bf61a..7f8422a 100644
--- a/src/isolator/hei_isolation_node.cpp
+++ b/src/isolator/hei_isolation_node.cpp
@@ -1,3 +1,4 @@
+#include <hei_macros.hpp>
 #include <isolator/hei_isolation_node.hpp>
 
 namespace libhei
diff --git a/src/register/hei_hardware_register.hpp b/src/register/hei_hardware_register.hpp
index ee57b2a..d8edfd8 100644
--- a/src/register/hei_hardware_register.hpp
+++ b/src/register/hei_hardware_register.hpp
@@ -1,6 +1,7 @@
 #pragma once
 
 #include <hei_includes.hpp>
+#include <hei_macros.hpp>
 #include <register/hei_register.hpp>
 #include <util/hei_bit_string.hpp>
 
diff --git a/src/register/prdfCaptureData.C b/src/register/prdfCaptureData.C
index d6777aa..a984f24 100755
--- a/src/register/prdfCaptureData.C
+++ b/src/register/prdfCaptureData.C
@@ -32,6 +32,7 @@
 //  Includes
 //----------------------------------------------------------------------
 
+#include <hei_macros.hpp>
 #include <register/hei_hardware_register.hpp>
 #include <util/hei_bit_string.hpp>
 
diff --git a/src/util/hei_bit_string.cpp b/src/util/hei_bit_string.cpp
index 8fc0776..2795cf1 100644
--- a/src/util/hei_bit_string.cpp
+++ b/src/util/hei_bit_string.cpp
@@ -2,7 +2,7 @@
  *  @brief BitString and BitStringBuffer class definitions
  */
 
-#include <hei_user_defines.hpp>
+#include <hei_macros.hpp>
 #include <util/hei_bit_string.hpp>
 
 #include <algorithm>
diff --git a/test/bit_string_test.cpp b/test/bit_string_test.cpp
index aaf5b90..c763cf6 100644
--- a/test/bit_string_test.cpp
+++ b/test/bit_string_test.cpp
@@ -1,4 +1,4 @@
-#include <hei_user_defines.hpp>
+#include <hei_macros.hpp>
 #include <util/hei_bit_string.hpp>
 
 #include "gtest/gtest.h"
diff --git a/test/hei_user_defines.cpp b/test/hei_user_defines.cpp
new file mode 100644
index 0000000..f6e39f0
--- /dev/null
+++ b/test/hei_user_defines.cpp
@@ -0,0 +1,30 @@
+#include <assert.h>
+#include <stdarg.h>
+#include <stdio.h>
+
+namespace libhei
+{
+
+void hei_inf(char* format, ...)
+{
+    va_list args;
+
+    printf("I> ");
+    va_start(args, format);
+    vprintf(format, args);
+    va_end(args);
+    printf("\n");
+}
+
+void hei_err(char* format, ...)
+{
+    va_list args;
+
+    printf("E> ");
+    va_start(args, format);
+    vprintf(format, args);
+    va_end(args);
+    printf("\n");
+}
+
+} // namespace libhei
diff --git a/test/hei_user_defines.hpp b/test/hei_user_defines.hpp
deleted file mode 100644
index 08ddda9..0000000
--- a/test/hei_user_defines.hpp
+++ /dev/null
@@ -1,25 +0,0 @@
-#pragma once
-
-/**
- * @file hei_user_defines.hpp
- * @brief The purpose of this file is to create common defines that
- *        will be used throughout this library.
- */
-
-#include <assert.h>
-#include <inttypes.h>
-#include <stdio.h>
-
-#define HEI_INF(...)                                                           \
-    {                                                                          \
-        printf("I> " __VA_ARGS__);                                             \
-        printf("\n");                                                          \
-    }
-
-#define HEI_ERR(...)                                                           \
-    {                                                                          \
-        printf("E> " __VA_ARGS__);                                             \
-        printf("\n");                                                          \
-    }
-
-#define HEI_ASSERT(expression) assert(expression);
diff --git a/test/meson.build b/test/meson.build
index ce1e5da..209bad3 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -2,7 +2,8 @@
 subdir('simulator')
 
 # supporting files that need compiled/linked
-test_src = ['../src/util/hei_bit_string.cpp']
+test_src = ['../src/util/hei_bit_string.cpp',
+               'hei_user_defines.cpp']
 
 # build g-test framework unit tests
 gtests = [
diff --git a/test/simulator/user_interface.cpp b/test/simulator/hei_user_interface.cpp
similarity index 98%
rename from test/simulator/user_interface.cpp
rename to test/simulator/hei_user_interface.cpp
index 9f841bb..658a1ef 100644
--- a/test/simulator/user_interface.cpp
+++ b/test/simulator/hei_user_interface.cpp
@@ -7,6 +7,7 @@
 
 #include <endian.h>
 
+#include <hei_macros.hpp>
 #include <hei_user_interface.hpp>
 
 namespace libhei
diff --git a/test/simulator/meson.build b/test/simulator/meson.build
index 0f96bb3..ec5410f 100644
--- a/test/simulator/meson.build
+++ b/test/simulator/meson.build
@@ -1,28 +1,22 @@
 # Simulator sources
-sim_src = [
+sim_libhei = [
     'simulator.cpp',
-    'user_interface.cpp',
-]
-
-# Isolator sources
-iso_src = [
-    '../../src/isolator/hei_isolator.cpp',
-    '../../src/isolator/hei_isolation_chip.cpp',
-    '../../src/isolator/hei_isolation_node.cpp',
-    '../../src/register/hei_hardware_register.cpp',
-    '../../src/util/hei_bit_string.cpp',
+    'hei_user_interface.cpp',
+    '../hei_user_defines.cpp'
 ]
 
 # Test cases
-test_src = [
+test_libhei = [
     'sample_test_case.cpp',
 ]
 
 gtest = dependency('gtest', main : true, required : false, method : 'system')
 
+# Build simulator linked locally
 if gtest.found()
-    test('simulator', \
-         executable('simulator', sim_src, iso_src, test_src, \
-                    dependencies : gtest, include_directories: incdir))
+    test('simulator',
+         executable('simulator', sim_libhei, test_libhei,
+                    dependencies : gtest,
+                    link_with : libhei_static,
+                    include_directories: incdir))
 endif
-