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

Maintain support for:
  Configurable dbus parameters:
    BUSNAME_PREFIX= -> -Dbusname-prefix=
    SENSOR_ROOT= -> -Dsensor-root=
  System call failure behavior:
    --enable-negative-errno-on-failure -> -Dnegative-errno-on-fail
    --enable-update-functional-on-fail -> -Dupdate-functional-on-fail

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

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

Upstream meson does not currently have support for custom code coverage
parameters:
  https://github.com/mesonbuild/meson/issues/4628

Autotools does support this, so support of our custom code coverage
parameters cannot be maintained using meson, without meson enhancements.

Change-Id: I312f1af4c3fcc20ca8bdf2bbf53b06f18abfbfe2
Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
diff --git a/meson.build b/meson.build
new file mode 100644
index 0000000..41ffb9d
--- /dev/null
+++ b/meson.build
@@ -0,0 +1,110 @@
+project(
+    'phosphor-hwmon',
+    'cpp',
+    default_options: [
+        'warning_level=3',
+        'werror=true',
+        'cpp_std=c++17'
+    ],
+    license: 'Apache-2.0',
+    version: '1.0',
+)
+
+build_tests = get_option('tests')
+
+gmock = dependency('gmock')
+gpioplus = dependency('gpioplus')
+gtest = dependency('gtest', main: true)
+phosphor_dbus_interfaces = dependency('phosphor-dbus-interfaces')
+phosphor_logging = dependency('phosphor-logging')
+sdbusplus = dependency('sdbusplus')
+sdeventplus = dependency('sdeventplus')
+stdplus = dependency('stdplus')
+threads = dependency('threads')
+
+conf = configuration_data()
+conf.set_quoted('BUSNAME_PREFIX', get_option('busname-prefix'))
+conf.set_quoted('SENSOR_ROOT', get_option('sensor-root'))
+conf.set10('NEGATIVE_ERRNO_ON_FAIL', get_option('negative-errno-on-fail'))
+conf.set10('UPDATE_FUNCTIONAL_ON_FAIL', get_option('update-functional-on-fail'))
+
+configure_file(output: 'config.h', configuration: conf)
+
+libaverage = static_library(
+    'average',
+    'average.cpp',
+)
+
+libfan_pwm = static_library(
+    'fan_pwm',
+    'fan_pwm.cpp',
+)
+
+libhwmon = static_library(
+    'hwmon',
+    'hwmon.cpp',
+)
+
+libhwmonio = static_library(
+    'hwmonio',
+    'hwmonio.cpp',
+)
+
+libsensor = static_library(
+    'sensor',
+    'sensor.cpp',
+    dependencies: [
+        phosphor_dbus_interfaces,
+        phosphor_logging,
+        sdbusplus,
+    ],
+    link_with: [
+        libhwmon,
+    ],
+)
+
+libsysfs = static_library(
+    'sysfs',
+    'sysfs.cpp',
+    dependencies: [
+        sdbusplus,
+    ],
+)
+
+libhwmon_all = static_library(
+    'hwmon_all',
+    'env.cpp',
+    'fan_speed.cpp',
+    'gpio_handle.cpp',
+    'mainloop.cpp',
+    'sensorset.cpp',
+    dependencies: [
+        gpioplus,
+        phosphor_dbus_interfaces,
+        phosphor_logging,
+    ],
+    link_with: [
+        libaverage,
+        libfan_pwm,
+        libhwmon,
+        libhwmonio,
+        libsensor,
+        libsysfs,
+    ],
+)
+
+executable(
+    'phosphor-hwmon-readd',
+    'readd.cpp',
+    dependencies: [
+        sdeventplus,
+    ],
+    install: true,
+    link_with: [
+        libhwmon_all,
+    ],
+)
+
+subdir('msl')
+subdir('test')
+subdir('tools')
diff --git a/meson_options.txt b/meson_options.txt
new file mode 100644
index 0000000..24fe700
--- /dev/null
+++ b/meson_options.txt
@@ -0,0 +1,21 @@
+option(
+    'busname-prefix',
+    description: 'The DBus busname prefix.',
+    type: 'string',
+    value: 'xyz.openbmc_project.Hwmon')
+option(
+    'negative-errno-on-fail',
+    description: 'Set sensor value to -errno on read failures.',
+    type: 'boolean')
+option(
+    'sensor-root',
+    description: 'The DBus sensors namespace root.',
+    type: 'string',
+    value: '/xyz/openbmc_project/sensors')
+option(
+    'tests', type: 'feature', description: 'Build tests.',
+)
+option(
+    'update-functional-on-fail',
+    description: 'Update functional property on read failures.  Incompatible with negative-errno-on-fail.',
+    type: 'boolean')
diff --git a/msl/meson.build b/msl/meson.build
new file mode 100644
index 0000000..f92bb3e
--- /dev/null
+++ b/msl/meson.build
@@ -0,0 +1,7 @@
+configure_file(
+    copy: true,
+    input: 'max31785-msl',
+    install: true,
+    install_dir: get_option('bindir'),
+    output: 'max31785-msl',
+)
diff --git a/test/meson.build b/test/meson.build
new file mode 100644
index 0000000..54daaa8
--- /dev/null
+++ b/test/meson.build
@@ -0,0 +1,76 @@
+average_unittest = executable(
+    'average_unittest',
+    'average_unittest.cpp',
+    dependencies: [
+        gmock,
+        gtest,
+    ],
+    include_directories: '..',
+    link_with: [
+        libaverage,
+    ],
+)
+
+env_unittest = executable(
+    'env_unittest',
+    'env_unittest.cpp',
+    dependencies: [
+        gmock,
+        gtest,
+    ],
+    include_directories: '..',
+)
+
+fanpwm_unittest = executable(
+    'fanpwm_unittest',
+    'fanpwm_unittest.cpp',
+    dependencies: [
+        gmock,
+        gtest,
+        phosphor_logging,
+    ],
+    include_directories: '..',
+    link_with: [
+        libfan_pwm,
+    ],
+)
+
+hwmon_unittest = executable(
+    'hwmon_unittest',
+    'hwmon_unittest.cpp',
+    dependencies: [
+        gmock,
+        gtest,
+    ],
+    include_directories: '..',
+    link_with: [
+        libhwmon,
+    ],
+)
+
+hwmonio_default_unittest = executable(
+    'hwmonio_default_unittest',
+    'hwmonio_default_unittest.cpp',
+    dependencies: [
+        gtest,
+        gmock,
+    ],
+    include_directories: '..',
+    link_with: [
+        libhwmonio,
+    ],
+)
+
+sensor_unittest = executable(
+    'sensor_unittest',
+    'gpio.cpp',
+    'sensor_unittest.cpp',
+    dependencies: [
+        gtest,
+        gmock,
+    ],
+    include_directories: '..',
+    link_with: [
+        libsensor,
+    ],
+)
diff --git a/tools/meson.build b/tools/meson.build
new file mode 100644
index 0000000..4b8ea4e
--- /dev/null
+++ b/tools/meson.build
@@ -0,0 +1,13 @@
+executable(
+    'find_hwmon',
+    'find_hwmon.cpp',
+    include_directories: '..',
+    link_with: libsysfs,
+)
+
+executable(
+    'find_callout_path',
+    'find_callout_path.cpp',
+    include_directories: '..',
+    link_with: libsysfs,
+)