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