xref: /openbmc/bmcweb/meson.build (revision 3b28fa2b201513be89807dfb76b47b3ca7ee3f9b)
1project(
2    'bmcweb',
3    'cpp',
4    version: '1.0',
5    meson_version: '>=1.3.0',
6    default_options: [
7        'b_lto_mode=default',
8        'b_lto_threads=0',
9        'b_lto=true',
10        'b_ndebug=if-release',
11        'buildtype=debugoptimized',
12        'cpp_rtti=false',
13        'cpp_std=c++23',
14        'warning_level=3',
15        'werror=true',
16    ],
17)
18
19# Project related links
20
21project_pretty_name = 'bmcweb'
22project_url = 'https://github.com/openbmc/' + project_pretty_name
23project_issues_url = project_url + '/issues/new'
24summary('Issues', project_issues_url, section: 'Report Issues')
25
26# Validate the c++ Standard
27
28if get_option('cpp_std') != 'c++23'
29    error('This project requires c++23 support')
30endif
31
32# Get compiler and default build type
33
34cxx = meson.get_compiler('cpp')
35build = get_option('buildtype')
36optimization = get_option('optimization')
37summary('Build Type', build, section: 'Build Info')
38summary('Optimization', optimization, section: 'Build Info')
39
40# remove debug information for minsize buildtype
41if (get_option('buildtype') == 'minsize')
42    add_project_arguments(
43        ['-fdata-sections', '-ffunction-sections'],
44        language: 'cpp',
45    )
46    add_project_arguments('-DNDEBUG', language: 'cpp')
47endif
48
49if (get_option('dns-resolver') == 'systemd-dbus')
50    add_project_arguments('-DBMCWEB_DBUS_DNS_RESOLVER', language: 'cpp')
51endif
52
53# Disable lto when compiling with no optimization
54if (get_option('optimization') == '0')
55    add_project_arguments('-fno-lto', language: 'cpp')
56    message('Disabling lto & its supported features as optimization is disabled')
57endif
58
59# Include Directories
60incdir = [
61    include_directories(
62        'include',
63        'redfish-core/include',
64        'redfish-core/lib',
65        'http',
66    ),
67]
68
69incdir_cli = include_directories('http', 'include')
70
71# Add compiler arguments
72boost_flags = ['-Wno-unused-parameter']
73nghttp2_flags = []
74if (cxx.get_id() == 'clang')
75    if (cxx.version().version_compare('<17.0'))
76        error('This project requires clang-17 or higher')
77    endif
78    add_project_arguments(
79        '-Weverything',
80        '-Wformat=2',
81        # https://github.com/llvm/llvm-project/issues/101614
82        '-fno-builtin-std-forward_like',
83        '-Wno-c++20-extensions',
84        '-Wno-c++23-extensions',
85        '-Wno-c++26-extensions',
86        '-Wno-c++98-compat',
87        '-Wno-c++98-compat-pedantic',
88        '-Wno-covered-switch-default',
89        '-Wno-disabled-macro-expansion',
90        '-Wno-documentation',
91        '-Wno-documentation-unknown-command',
92        '-Wno-exit-time-destructors',
93        '-Wno-global-constructors',
94        '-Wno-missing-include-dirs',
95        '-Wno-newline-eof',
96        '-Wno-padded',
97        '-Wno-shadow',
98        '-Wno-switch-enum',
99        '-Wno-unneeded-internal-declaration',
100        '-Wno-unsafe-buffer-usage-in-container',
101        '-Wno-unsafe-buffer-usage-in-libc-call',
102        '-Wno-unused-macros',
103        '-Wno-used-but-marked-unused',
104        '-Wno-weak-vtables',
105        language: 'cpp',
106    )
107    if (cxx.version().version_compare('>=21.0'))
108        # TODO(Ed) These warnings look valid and need cleaned up.
109        add_project_arguments(
110            '-Wno-unique-object-duplication',
111            '-Wno-nrvo',
112            language: 'cpp',
113        )
114    endif
115
116    boost_flags += ['-Wno-strict-prototypes', '-Wno-unused-but-set-variable']
117    nghttp2_flags += ['-Wno-extra-semi']
118endif
119
120if (cxx.get_id() == 'gcc')
121    if (cxx.version().version_compare('<13.0'))
122        error('This project requires gcc-13 or higher')
123    endif
124
125    add_project_arguments(
126        '-Wformat=2',
127        '-Wcast-align',
128        '-Wconversion',
129        '-Woverloaded-virtual',
130        '-Wsign-conversion',
131        '-Wunused',
132        '-Wduplicated-cond',
133        '-Wduplicated-branches',
134        '-Wlogical-op',
135        '-Wnull-dereference',
136        '-Wunused-parameter',
137        '-Wdouble-promotion',
138        '-Wshadow',
139        '-Wno-psabi',
140        '-Wno-attributes',
141        language: 'cpp',
142    )
143endif
144
145if (get_option('buildtype') != 'plain')
146    if (get_option('b_lto') == true and get_option('optimization') != '0')
147        # Reduce the binary size by removing unnecessary
148        # dynamic symbol table entries
149
150        add_project_arguments(
151            cxx.get_supported_arguments(
152                [
153                    '-fno-fat-lto-objects',
154                    '-fvisibility=hidden',
155                    '-fvisibility-inlines-hidden',
156                ],
157            ),
158            language: 'cpp',
159        )
160
161        if cxx.has_link_argument('-Wl,--exclude-libs,ALL')
162            add_project_link_arguments('-Wl,--exclude-libs,ALL', language: 'cpp')
163        endif
164    endif
165endif
166# Set Compiler Security flags
167
168security_flags = [
169    '-fstack-protector-strong',
170    '-fPIE',
171    '-fPIC',
172    '-D_FORTIFY_SOURCE=2',
173    '-Wformat',
174    '-Wformat-security',
175]
176
177## Add security flags for builds of type 'release','debugoptimized' and 'minsize'
178
179if not (get_option('buildtype') == 'plain'
180or get_option('buildtype').startswith('debug')
181)
182    add_project_arguments(
183        cxx.get_supported_arguments([security_flags]),
184        language: 'cpp',
185    )
186endif
187
188# Boost dependency configuration
189
190add_project_arguments(
191    cxx.get_supported_arguments(
192        [
193            '-DBOOST_ALL_NO_LIB',
194            '-DBOOST_ALLOW_DEPRECATED_HEADERS',
195            '-DBOOST_ASIO_DISABLE_THREADS',
196            '-DBOOST_ASIO_NO_DEPRECATED',
197            '-DBOOST_ASIO_SEPARATE_COMPILATION',
198            '-DBOOST_BEAST_SEPARATE_COMPILATION',
199            '-DBOOST_EXCEPTION_DISABLE',
200            '-DBOOST_NO_EXCEPTIONS',
201            '-DBOOST_URL_NO_SOURCE_LOCATION',
202            '-DBOOST_SPIRIT_X3_NO_RTTI',
203            '-DJSON_NOEXCEPTION',
204            '-DJSON_USE_IMPLICIT_CONVERSIONS=0',
205            '-DOPENSSL_NO_FILENAMES',
206            '-DSDBUSPLUS_DISABLE_BOOST_COROUTINES',
207        ],
208    ),
209    language: 'cpp',
210)
211
212# Find the dependency modules, if not found use meson wrap to get them
213# automatically during the configure step
214bmcweb_dependencies = []
215bmcweb_cli_dependencies = []
216
217pam = cxx.find_library('pam', required: true)
218atomic = cxx.find_library('atomic', required: true)
219bmcweb_dependencies += [pam, atomic]
220
221openssl = dependency('openssl', required: false, version: '>=3.0.0')
222if not openssl.found()
223    openssl_proj = subproject(
224        'openssl',
225        required: true,
226        default_options: ['warning_level=0', 'werror=false'],
227    )
228    openssl = openssl_proj.get_variable('openssl_dep')
229    openssl = openssl.as_system('system')
230endif
231bmcweb_dependencies += [openssl]
232
233nghttp2 = dependency('libnghttp2', version: '>=1.52.0', required: false)
234if not nghttp2.found()
235    cmake = import('cmake')
236    opt_var = cmake.subproject_options()
237    opt_var.add_cmake_defines(
238        {
239            'CMAKE_C_FLAGS': ' '.join(nghttp2_flags),
240            'CMAKE_CXX_FLAGS': ' '.join(nghttp2_flags),
241            'ENABLE_LIB_ONLY': true,
242            'ENABLE_STATIC_LIB': true,
243        },
244    )
245    nghttp2_ex = cmake.subproject('nghttp2', options: opt_var)
246    nghttp2 = nghttp2_ex.dependency('nghttp2')
247endif
248bmcweb_dependencies += nghttp2
249
250sdbusplus = dependency('sdbusplus', required: false, include_type: 'system')
251if not sdbusplus.found()
252    sdbusplus_proj = subproject('sdbusplus', required: true)
253    sdbusplus = sdbusplus_proj.get_variable('sdbusplus_dep')
254    sdbusplus = sdbusplus.as_system('system')
255endif
256bmcweb_dependencies += sdbusplus
257bmcweb_cli_dependencies += sdbusplus
258
259cli11 = dependency('CLI11', required: false, include_type: 'system')
260if not cli11.found()
261    cli11_proj = subproject('cli11', required: true)
262    cli11 = cli11_proj.get_variable('CLI11_dep')
263    cli11 = cli11.as_system('system')
264endif
265bmcweb_cli_dependencies += cli11
266
267
268tinyxml = dependency(
269    'tinyxml2',
270    include_type: 'system',
271    version: '>=9.0.0',
272    default_options: ['tests=false'],
273)
274if not tinyxml.found()
275    tinyxml_proj = subproject('tinyxml2', required: true)
276    tinyxml = tinyxml_proj.get_variable('tinyxml_dep')
277    tinyxml = tinyxml.as_system('system')
278endif
279bmcweb_dependencies += tinyxml
280
281systemd = dependency('systemd')
282libsystemd = dependency('libsystemd')
283add_project_arguments(
284    '-DLIBSYSTEMD_VERSION=' + libsystemd.version(),
285    language: 'cpp',
286)
287
288zlib = dependency('zlib')
289bmcweb_dependencies += [libsystemd, zlib]
290
291nlohmann_json_dep = dependency(
292    'nlohmann_json',
293    version: '>=3.11.3',
294    include_type: 'system',
295)
296bmcweb_dependencies += nlohmann_json_dep
297
298boost = dependency(
299    'boost',
300    modules: ['url'],
301    version: '>=1.84.0',
302    required: false,
303    static: true,
304    include_type: 'system',
305)
306
307# Boost version is 1.86 or higher to include the 'process' module
308if boost.version().version_compare('>=1.86.0')
309    boost = dependency(
310        'boost',
311        modules: ['url', 'process'],
312        version: '>=1.86.0',
313        static: true,
314        required: false,
315        include_type: 'system',
316    )
317endif
318
319if boost.found()
320    bmcweb_dependencies += [boost]
321    bmcweb_cli_dependencies += [boost]
322else
323    cmake = import('cmake')
324    opt = cmake.subproject_options()
325    boost_libs = [
326        'asio',
327        'beast',
328        'circular_buffer',
329        'callable_traits',
330        'headers',
331        'process',
332        'type_index',
333        'url',
334        'uuid',
335        'spirit',
336    ]
337    opt.add_cmake_defines(
338        {
339            'CMAKE_CXX_FLAGS': ' '.join(boost_flags),
340            'CMAKE_C_FLAGS': ' '.join(boost_flags),
341            'BOOST_INCLUDE_LIBRARIES': ';'.join(boost_libs),
342            'BUILD_SHARED_LIBS': 'OFF',
343        },
344    )
345
346    boost = cmake.subproject('boost', required: true, options: opt)
347    foreach boost_lib : boost_libs
348        boost_lib_instance = boost.dependency('boost_' + boost_lib).as_system()
349        bmcweb_dependencies += [boost_lib_instance]
350        bmcweb_cli_dependencies += [boost_lib_instance]
351    endforeach
352endif
353
354systemd_system_unit_dir = systemd.get_variable('systemdsystemunitdir')
355
356bindir = get_option('prefix') + '/' + get_option('bindir')
357libexec = get_option('prefix') + '/' + get_option('libexecdir')
358
359summary(
360    {
361        'prefix': get_option('prefix'),
362        'bindir': bindir,
363        'systemd unit directory': systemd_system_unit_dir,
364    },
365    section: 'Directories',
366)
367
368subdir('static')
369subdir('redfish-core')
370
371# Config subdirectory
372subdir('config')
373bmcweb_dependencies += conf_h_dep
374bmcweb_cli_dependencies += conf_h_dep
375
376test_sources = []
377subdir('features')
378
379# Source files
380fs = import('fs')
381
382srcfiles_bmcweb = files(
383    'http/mutual_tls.cpp',
384    'http/routing/sserule.cpp',
385    'http/routing/websocketrule.cpp',
386    'redfish-core/src/dbus_log_watcher.cpp',
387    'redfish-core/src/error_message_utils.cpp',
388    'redfish-core/src/error_messages.cpp',
389    'redfish-core/src/event_log.cpp',
390    'redfish-core/src/filesystem_log_watcher.cpp',
391    'redfish-core/src/filter_expr_executor.cpp',
392    'redfish-core/src/filter_expr_printer.cpp',
393    'redfish-core/src/heartbeat_messages.cpp',
394    'redfish-core/src/redfish.cpp',
395    'redfish-core/src/registries.cpp',
396    'redfish-core/src/resource_messages.cpp',
397    'redfish-core/src/subscription.cpp',
398    'redfish-core/src/task_messages.cpp',
399    'redfish-core/src/utils/dbus_utils.cpp',
400    'redfish-core/src/utils/json_utils.cpp',
401    'redfish-core/src/utils/time_utils.cpp',
402    'src/boost_asio.cpp',
403    'src/boost_asio_ssl.cpp',
404    'src/boost_beast.cpp',
405    'src/dbus_singleton.cpp',
406    'src/dbus_utility.cpp',
407    'src/json_html_serializer.cpp',
408    'src/ossl_random.cpp',
409    'src/ssl_key_handler.cpp',
410    'src/webserver_run.cpp',
411)
412
413bmcweblib = static_library(
414    'bmcweblib',
415    srcfiles_bmcweb,
416    include_directories: incdir,
417    dependencies: bmcweb_dependencies,
418)
419
420# Generate the bmcwebd daemon
421executable(
422    'bmcwebd',
423    'src/webserver_main.cpp',
424    include_directories: incdir,
425    dependencies: bmcweb_dependencies,
426    link_with: bmcweblib,
427    link_args: '-Wl,--gc-sections',
428    install: true,
429    install_dir: libexec,
430)
431
432# Generate the bmcweb CLI application
433executable(
434    'bmcweb',
435    ['src/webserver_cli.cpp', 'src/boost_asio.cpp'],
436    include_directories: incdir_cli,
437    dependencies: bmcweb_cli_dependencies,
438    install: true,
439    install_dir: bindir,
440)
441
442srcfiles_unittest = files(
443    'test/http/crow_getroutes_test.cpp',
444    'test/http/http2_connection_test.cpp',
445    'test/http/http_body_test.cpp',
446    'test/http/http_connection_test.cpp',
447    'test/http/http_response_test.cpp',
448    'test/http/mutual_tls.cpp',
449    'test/http/parsing_test.cpp',
450    'test/http/router_test.cpp',
451    'test/http/server_sent_event_test.cpp',
452    'test/http/utility_test.cpp',
453    'test/http/verb_test.cpp',
454    'test/include/async_resolve_test.cpp',
455    'test/include/credential_pipe_test.cpp',
456    'test/include/dbus_utility_test.cpp',
457    'test/include/http_utility_test.cpp',
458    'test/include/human_sort_test.cpp',
459    'test/include/json_html_serializer.cpp',
460    'test/include/multipart_test.cpp',
461    'test/include/ossl_random.cpp',
462    'test/include/sessions_test.cpp',
463    'test/include/ssl_key_handler_test.cpp',
464    'test/include/str_utility_test.cpp',
465    'test/redfish-core/include/dbus_log_watcher_test.cpp',
466    'test/redfish-core/include/event_log_test.cpp',
467    'test/redfish-core/include/event_matches_filter_test.cpp',
468    'test/redfish-core/include/filter_expr_executor_test.cpp',
469    'test/redfish-core/include/filter_expr_parser_test.cpp',
470    'test/redfish-core/include/privileges_test.cpp',
471    'test/redfish-core/include/redfish_aggregator_test.cpp',
472    'test/redfish-core/include/redfish_oem_routing_test.cpp',
473    'test/redfish-core/include/redfish_test.cpp',
474    'test/redfish-core/include/registries_test.cpp',
475    'test/redfish-core/include/submit_test_event_test.cpp',
476    'test/redfish-core/include/utils/dbus_utils.cpp',
477    'test/redfish-core/include/utils/error_code_test.cpp',
478    'test/redfish-core/include/utils/hex_utils_test.cpp',
479    'test/redfish-core/include/utils/ip_utils_test.cpp',
480    'test/redfish-core/include/utils/json_utils_test.cpp',
481    'test/redfish-core/include/utils/query_param_test.cpp',
482    'test/redfish-core/include/utils/sensor_utils_test.cpp',
483    'test/redfish-core/include/utils/stl_utils_test.cpp',
484    'test/redfish-core/include/utils/time_utils_test.cpp',
485    'test/redfish-core/lib/chassis_test.cpp',
486    'test/redfish-core/lib/ethernet_test.cpp',
487    'test/redfish-core/lib/log_services_dump_test.cpp',
488    'test/redfish-core/lib/manager_diagnostic_data_test.cpp',
489    'test/redfish-core/lib/metadata_test.cpp',
490    'test/redfish-core/lib/power_subsystem_test.cpp',
491    'test/redfish-core/lib/service_root_test.cpp',
492    'test/redfish-core/lib/system_test.cpp',
493    'test/redfish-core/lib/systems_logservices_postcode.cpp',
494    'test/redfish-core/lib/thermal_subsystem_test.cpp',
495    'test/redfish-core/lib/update_service_test.cpp',
496) + test_sources
497
498if (get_option('tests').allowed())
499    gtest = dependency(
500        'gtest_main',
501        main: true,
502        version: '>=1.15.0',
503        required: true,
504    )
505    gmock = dependency('gmock', required: true)
506    gtestlib = static_library('gtestlib', dependencies: [gtest, gmock])
507    gtestdep = declare_dependency(
508        link_with: gtestlib,
509        dependencies: [
510            gtest.partial_dependency(includes: true),
511            gmock.partial_dependency(includes: true),
512        ],
513    )
514    # generate the test executable
515    foreach test_src : srcfiles_unittest
516        test_bin = executable(
517            fs.stem(test_src),
518            test_src,
519            link_with: bmcweblib,
520            include_directories: incdir,
521            install_dir: bindir,
522            dependencies: bmcweb_dependencies + [gtestdep],
523        )
524        test(fs.stem(test_src), test_bin, protocol: 'gtest')
525    endforeach
526endif
527