project(
    'phosphor-hwmon',
    'cpp',
    default_options: [
        'warning_level=3',
        'werror=true',
        'cpp_std=c++17'
    ],
    license: 'Apache-2.0',
    version: '1.0',
)

fmt = dependency('fmt')

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'))

sysfs_headers = include_directories('.')

sysfs_deps = [
    fmt,
]

sysfs_lib = static_library(
    'sysfs',
    'sysfs.cpp',
    include_directories: sysfs_headers,
    dependencies: sysfs_deps)

sysfs_dep = declare_dependency(
    dependencies: sysfs_deps,
    include_directories: sysfs_headers,
    link_with: sysfs_lib)

hwmon_headers = include_directories('.')

hwmon_deps = [
    fmt,
    dependency('gpioplus'),
    dependency('phosphor-dbus-interfaces'),
    dependency('phosphor-logging'),
    dependency('sdbusplus'),
    dependency('sdeventplus'),
    dependency('stdplus'),
    sysfs_dep,
]

hwmon_lib = static_library(
    'hwmon',
    'average.cpp',
    configure_file(output: 'config.h', configuration: conf),
    'env.cpp',
    'fan_pwm.cpp',
    'fan_speed.cpp',
    'gpio_handle.cpp',
    'hwmon.cpp',
    'hwmonio.cpp',
    'mainloop.cpp',
    'sensor.cpp',
    'sensorset.cpp',
    dependencies: hwmon_deps,
    include_directories: hwmon_headers)

hwmon_dep = declare_dependency(
    dependencies: hwmon_deps,
    include_directories: hwmon_headers,
    link_with: hwmon_lib)

# CLI11 might not have a pkg-config. It is header only so just make
# sure we can access the needed symbols from the header.
cli11_dep = dependency('cli11', required: false)
has_cli11 = meson.get_compiler('cpp').has_header_symbol(
    'CLI/CLI.hpp',
    'CLI::App',
    dependencies: cli11_dep,
    required: false)
if not has_cli11
    cli11_proj = subproject('cli11', required: false)
    assert(cli11_proj.found(), 'CLI11 is required')
    cli11_dep = cli11_proj.get_variable('CLI11_dep')
endif

executable(
    'phosphor-hwmon-readd',
    'readd.cpp',
    dependencies: [
        cli11_dep,
        hwmon_dep,
    ],
    install: true)

systemd_system_unit_dir = dependency('systemd').get_pkgconfig_variable(
    'systemdsystemunitdir',
    define_variable: ['prefix', get_option('prefix')])
udev_dir = dependency('udev').get_pkgconfig_variable(
    'udev_dir',
    define_variable: ['prefix', get_option('prefix')])

install_data(
    'xyz.openbmc_project.Hwmon@.service',
    install_dir: systemd_system_unit_dir
)

install_data(
    'phosphor-hwmon.conf',
    install_dir: get_option('sysconfdir') / 'dbus-1/system.d'
)

install_data(
    ['70-hwmon.rules', '70-iio.rules'],
    install_dir: udev_dir / 'rules.d'
)

install_data(
    'start_hwmon.sh',
    install_dir: get_option('bindir'),
    install_mode: 'rwxr-xr-x'
)

subdir('msl')
if not get_option('tests').disabled()
  subdir('test')
endif
subdir('tools')