build: add support for building with meson

Follow the OpenBMC herd and support a more modern, more comprehensible,
more performant build framework.

To build using meson:
  meson build
  ninja -C build
  ninja -C build install

Maintained support for:
  Configurable dbus parameters:
    INPUT_HISTORY_BUSNAME_ROOT= -> -Dinput-history-busname-root=
    INPUT_HISTORY_SENSOR_ROOT = -> -Dinput-history-sensor-root=
  UCD90160 support:
    UCD90160_DEF_YAML_FILE= -> -Ducd90160-yaml=
    --enable-turn-off-ucd90160-access = -> -Ducd90160-access=

Added -Dtests to match de-facto OpenBMC meson usage conventions.

Dropped UCD90160_DEF_OUTPUT_DIR.  Much like the destination of
intermediate artifacts like object files are not configurable, the
location of the generated UCD definition file should not be configurable
either.

Dropped support for --enable-oe-sdk rpath munging.  This was a
workaround for broken oe sdks that don't figure out the correct rpath
when running make check or ninja test.

Tested by building with both meson and autotools and then compared build
artifacts from each build system:
  generated source code is the same
  generated binaries have the same symbols

Ran existing unit tests (which passed) using ninja test.

Change-Id: Iac80ba1fe5c2d01abe0e0a95e0bec03b8b828ef5
Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
diff --git a/.gitignore b/.gitignore
index c0912c0..8a7f3f1 100644
--- a/.gitignore
+++ b/.gitignore
@@ -29,5 +29,4 @@
 pseq-monitor
 psu-monitor
 ucd90160_defs.cpp
-org/open_power/Witherspoon/Fault/
 test-driver
diff --git a/meson.build b/meson.build
new file mode 100644
index 0000000..a9e7fb4
--- /dev/null
+++ b/meson.build
@@ -0,0 +1,63 @@
+project(
+    'witherspoon-pfault-analysis',
+    'cpp',
+    default_options: [
+        'warning_level=3',
+        'werror=true',
+        'cpp_std=c++17'
+    ],
+    license: 'Apache-2.0',
+    version: '1.0',
+)
+
+build_tests = get_option('tests')
+
+cppfs = meson.get_compiler('cpp').find_library('stdc++fs')
+gmock = dependency('gmock', disabler: true, required: build_tests)
+gtest = dependency('gtest', main: true, disabler: true, required: build_tests)
+openpower_dbus_interfaces = dependency('openpower-dbus-interfaces')
+phosphor_dbus_interfaces = dependency('phosphor-dbus-interfaces')
+phosphor_logging = dependency('phosphor-logging')
+prog_python = import('python').find_installation('python')
+sdbusplus = dependency('sdbusplus')
+sdbuspp = find_program('sdbus++')
+sdeventplus = dependency('sdeventplus')
+
+conf = configuration_data()
+conf.set_quoted(
+    'INPUT_HISTORY_BUSNAME_ROOT', get_option('input-history-busname-root'))
+conf.set_quoted(
+    'INPUT_HISTORY_SENSOR_ROOT', get_option('input-history-sensor-root'))
+conf.set10(
+    'UCD90160_DEVICE_ACCESS', get_option('ucd90160-access'))
+
+configure_file(output: 'config.h', configuration: conf)
+
+# Ensure the generated header here winds up in the correct path in the build
+# tree such that it actually get used and doesn't get found in the sysroot
+# somewhere.  Meson doesn't allow path elements (rightfully so) when specifying
+# the output filename of a target definition so the target must be defined in
+# the directory where the artifacts need to be placed.  Do that now, because
+# the generated source (cpp) is needed to define the library target.
+subdir('org/open_power/Witherspoon/Fault')
+
+libpower = static_library(
+    'power',
+    error_cpp,
+    error_hpp,
+    'gpio.cpp',
+    'pmbus.cpp',
+    'utility.cpp',
+    dependencies: [
+        cppfs,
+        openpower_dbus_interfaces,
+        phosphor_dbus_interfaces,
+        phosphor_logging,
+        sdbusplus,
+        sdeventplus,
+    ],
+)
+
+subdir('power-sequencer')
+subdir('power-supply')
+subdir('test')
diff --git a/meson_options.txt b/meson_options.txt
new file mode 100644
index 0000000..d8dcc95
--- /dev/null
+++ b/meson_options.txt
@@ -0,0 +1,21 @@
+option(
+    'input-history-busname-root', type: 'string',
+    value: 'org.open_power.powersupply',
+    description: 'The D-Bus busname root for the PS input history.',
+)
+option(
+    'input-history-sensor-root', type: 'string',
+    value: '/org/open_power/sensors/aggregation/per_30s',
+    description: 'The D-Bus power sensors namespace root.',
+)
+option(
+    'tests', type: 'feature', description: 'Build tests.',
+)
+option(
+    'ucd90160-access', type: 'boolean', value: true,
+    description: 'Enable UCD90160 hardware access.',
+)
+option(
+    'ucd90160-yaml', type: 'string', value: 'example/ucd90160.yaml',
+    description: 'The sequencer definition file to use.',
+)
diff --git a/org/open_power/Witherspoon/Fault/meson.build b/org/open_power/Witherspoon/Fault/meson.build
new file mode 100644
index 0000000..0669c2e
--- /dev/null
+++ b/org/open_power/Witherspoon/Fault/meson.build
@@ -0,0 +1,29 @@
+error_hpp = custom_target(
+    'error.hpp',
+    capture: true,
+    command: [
+        sdbuspp,
+        '-r', meson.source_root(),
+        'error',
+        'exception-header',
+        'org.open_power.Witherspoon.Fault',
+    ],
+    input: '../Fault.errors.yaml',
+    install: true,
+    install_dir: get_option('includedir') / 'org/open_power/Witherspoon/Fault',
+    output: 'error.hpp',
+)
+
+error_cpp = custom_target(
+    'error.cpp',
+    capture: true,
+    command: [
+        sdbuspp,
+        '-r', meson.source_root(),
+        'error',
+        'exception-cpp',
+        'org.open_power.Witherspoon.Fault',
+    ],
+    input: '../Fault.errors.yaml',
+    output: 'error.cpp',
+)
diff --git a/power-sequencer/meson.build b/power-sequencer/meson.build
new file mode 100644
index 0000000..a52fe02
--- /dev/null
+++ b/power-sequencer/meson.build
@@ -0,0 +1,33 @@
+ucd90160_defs_cpp = custom_target(
+    'ucd90160_defs.cpp',
+    command: [
+        prog_python, '@INPUT0@',
+        '-i', '@INPUT1@', '-o', meson.current_build_dir(),
+    ],
+    input: [
+        'gen-ucd90160-defs.py',
+        get_option('ucd90160-yaml'),
+        'templates/ucd90160_defs.mako.cpp'
+    ],
+    output: 'ucd90160_defs.cpp',
+)
+
+executable(
+    'pseq-monitor',
+    'argument.cpp',
+    error_hpp,
+    'main.cpp',
+    'pgood_monitor.cpp',
+    'runtime_monitor.cpp',
+    'ucd90160.cpp',
+    ucd90160_defs_cpp,
+    dependencies: [
+        phosphor_dbus_interfaces,
+        phosphor_logging,
+        sdbusplus,
+        sdeventplus,
+    ],
+    include_directories: '..',
+    install: true,
+    link_with: libpower,
+)
diff --git a/power-supply/meson.build b/power-supply/meson.build
new file mode 100644
index 0000000..f58e02f
--- /dev/null
+++ b/power-supply/meson.build
@@ -0,0 +1,22 @@
+psu_monitor = executable(
+    'psu-monitor',
+    'argument.cpp',
+    error_hpp,
+    'main.cpp',
+    'power_supply.cpp',
+    'record_manager.cpp',
+    dependencies: [
+        phosphor_dbus_interfaces,
+        phosphor_logging,
+        sdbusplus,
+    ],
+    include_directories: '..',
+    install: true,
+    link_with: [
+        libpower,
+    ]
+)
+
+record_manager = psu_monitor.extract_objects('record_manager.cpp')
+
+subdir('test')
diff --git a/power-supply/test/meson.build b/power-supply/test/meson.build
new file mode 100644
index 0000000..8006d63
--- /dev/null
+++ b/power-supply/test/meson.build
@@ -0,0 +1,19 @@
+test(
+    'test_records',
+    executable(
+        'test_records',
+        'test_records.cpp',
+        dependencies: [
+            gtest,
+            phosphor_dbus_interfaces,
+            phosphor_logging,
+            sdbusplus,
+        ],
+        implicit_include_directories: false,
+        include_directories: '../..',
+        link_with: [
+            libpower,
+        ],
+        objects: record_manager,
+    )
+)
diff --git a/test/meson.build b/test/meson.build
new file mode 100644
index 0000000..aaa21b4
--- /dev/null
+++ b/test/meson.build
@@ -0,0 +1,12 @@
+test(
+    'nvtest',
+    executable(
+        'nvtest', 'nvtest.cpp',
+        dependencies: [
+            gmock,
+            gtest,
+        ],
+        implicit_include_directories: false,
+        include_directories: '..',
+    )
+)