1project(
2    'phosphor-fan-presence',
3    'cpp',
4    default_options: [
5        'warning_level=3',
6        'werror=true',
7        'cpp_std=c++20',
8        'buildtype=debugoptimized',
9        'prefix=/usr'
10    ],
11    license: 'Apache-2.0',
12    version: '1.0',
13    meson_version: '>=0.57.0',
14)
15
16python_prog = find_program('python3', native: true)
17
18cpp = meson.get_compiler('cpp')
19
20cli11_dep = dependency('cli11', required: false)
21
22if not meson.get_compiler('cpp').has_header_symbol(
23    'CLI/CLI.hpp',
24    'CLI::App',
25    dependencies: cli11_dep,
26    required: false)
27    cli11_proj = subproject('cli11', required:false)
28    assert(cli11_proj.found(), 'CLI11 is required')
29    cli11_dep = cli11_proj.get_variable('CLI11_dep')
30endif
31
32fmt_dep = dependency('fmt')
33json_dep = declare_dependency()
34
35phosphor_dbus_interfaces_dep = dependency('phosphor-dbus-interfaces')
36phosphor_logging_dep = dependency('phosphor-logging')
37sdbusplus_dep = dependency('sdbusplus')
38sdeventplus_dep = dependency('sdeventplus')
39stdplus_dep = dependency('stdplus')
40systemd_dep = dependency('systemd')
41
42if(get_option('tests').enabled())
43    gmock_dep = dependency('gmock', disabler: true)
44    gtest_dep = dependency('gtest', main: true, disabler: true, required: false)
45
46    if not gtest_dep.found() or not gmock_dep.found()
47        gtest_proj = import('cmake').subproject('googletest', required: false)
48        if gtest_proj.found()
49            gtest_dep = declare_dependency(
50                dependencies: [
51                    dependency('threads'),
52                    gtest_proj.dependency('gtest'),
53                    gtest_proj.dependency('gtest_main'),
54                ]
55            )
56            gmock_dep = gtest_proj.dependency('gmock')
57        else
58            assert(
59                not get_option('tests').enabled(),
60                'Googletest is required if tests are enabled'
61            )
62        endif
63    endif
64    subdir('test')
65endif
66
67
68servicedir = systemd_dep.get_variable('systemdsystemunitdir')
69usr_share_dir = '/usr/share/phosphor-fan-presence'
70
71conf = configuration_data()
72
73# Control
74conf.set_quoted(
75    'CONTROL_PERSIST_ROOT_PATH', get_option('control-persist-root-path'))
76conf.set_quoted(
77    'CONTROL_BUSNAME', get_option('control-busname'))
78conf.set_quoted(
79    'CONTROL_OBJPATH', get_option('control-objpath'))
80conf.set_quoted(
81    'CONTROL_PERSIST_ROOT_PATH', get_option('control-persist-root-path'))
82
83conf.set_quoted(
84    'FAN_DEF_YAML_FILE', get_option('fan-def-yaml-file'))
85conf.set_quoted(
86    'FAN_ZONE_YAML_FILE', get_option('fan-zone-yaml-file'))
87conf.set_quoted(
88    'ZONE_EVENTS_YAML_FILE', get_option('zone-events-yaml-file'))
89conf.set_quoted(
90    'ZONE_CONDITIONS_YAML_FILE', get_option('zone-conditions-yaml-file'))
91
92# Monitor
93conf.set(
94    'NUM_MONITOR_LOG_ENTRIES', get_option('num-monitor-log-entries'))
95conf.set_quoted(
96    'THERMAL_ALERT_BUSNAME', get_option('thermal-alert-busname'))
97conf.set_quoted(
98    'THERMAL_ALERT_OBJPATH', get_option('thermal-alert-objpath'))
99conf.set_quoted(
100    'FAN_MONITOR_YAML_FILE', get_option('fan-monitor-yaml-file'))
101
102# Fan control can be in YAML mode when everything else is in JSON mode
103control_conf_type = 'yaml'
104if get_option('json-config').enabled() and get_option('json-control').enabled()
105    control_conf_type = 'json'
106endif
107
108# JSON-or-YAML (all programs)
109if get_option('json-config').enabled()
110    conf.set('PRESENCE_USE_JSON', '')
111    if control_conf_type == 'json'
112        conf.set('CONTROL_USE_JSON', '')
113    endif
114    conf.set('MONITOR_USE_JSON', '')
115
116    if not cpp.has_header('nlohmann/json.hpp')
117        json_dep = dependency('nlohmann_json')
118    endif
119    conf_type = 'json'
120else
121    conf_type = 'yaml'
122endif
123
124conf.set(
125    'NUM_PRESENCE_LOG_ENTRIES', get_option('num-presence-log-entries'))
126conf.set_quoted(
127    'PRESENCE_YAML_FILE', get_option('presence-config'))
128
129# Sensor
130if get_option('enable-host-state').enabled()
131    conf.set('ENABLE_HOST_STATE', '')
132endif
133
134conf.set_quoted(
135    'SENSOR_MONITOR_PERSIST_ROOT_PATH', get_option('sensor-monitor-root-path'))
136conf.set(
137    'SHUTDOWN_ALARM_HARD_SHUTDOWN_DELAY_MS', get_option('sensor-monitor-hard-shutdown-delay'))
138conf.set(
139    'SHUTDOWN_ALARM_SOFT_SHUTDOWN_DELAY_MS', get_option('sensor-monitor-soft-shutdown-delay'))
140
141configure_file(output: 'config.h', configuration: conf)
142
143# Service: [name,[svcfiles]]
144services = []
145
146if get_option('control-service').enabled()
147    subdir('control')
148    service_files = ['phosphor-fan-control@.service']
149    if control_conf_type == 'yaml'
150        service_files += 'phosphor-fan-control-init@.service'
151    endif
152    services += [['control', service_files]]
153endif
154
155if get_option('monitor-service').enabled()
156    subdir('monitor')
157    service_files = ['phosphor-fan-monitor@.service']
158    if not get_option('json-config').enabled()
159        service_files += 'phosphor-fan-monitor-init@.service'
160    endif
161    services += [['monitor', service_files]]
162endif
163
164if get_option('cooling-type-service').enabled()
165    libevdev_dep = dependency('libevdev')
166    subdir('cooling-type')
167endif
168
169if get_option('presence-service').enabled()
170    libevdev_dep = dependency('libevdev')
171    subdir('presence')
172    services += [['presence', ['phosphor-fan-presence-tach@.service']]]
173endif
174
175if get_option('sensor-monitor-service').enabled()
176    subdir('sensor-monitor')
177    install_data('sensor-monitor/service_files/sensor-monitor.service',
178        install_dir: servicedir)
179endif
180
181foreach service : services
182    this_conf_type = conf_type
183
184    if service[0] == 'control'
185        this_conf_type = control_conf_type
186    endif
187
188    foreach service_file : service[1]
189        install_data(service[0] / 'service_files' / this_conf_type / service_file,
190                     install_dir: servicedir)
191    endforeach
192
193    if this_conf_type == 'json'
194        fs = import('fs')
195        dir = meson.current_source_dir() / service[0] / 'config_files' / get_option('machine-name')
196        if fs.is_dir(dir)
197            install_subdir(service[0] / 'config_files' / get_option('machine-name'),
198                          install_dir: usr_share_dir / service[0],
199                          strip_directory: true)
200        endif
201    endif
202endforeach
203
204